[ALSA] Add helper functions for frequently used callbacks
[linux-2.6-block.git] / sound / pci / hda / hda_codec.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
62932df8 27#include <linux/mutex.h>
1da177e4
LT
28#include <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
302e9c5a 31#include <sound/tlv.h>
1da177e4
LT
32#include <sound/initval.h>
33#include "hda_local.h"
34
35
1da177e4
LT
36/*
37 * vendor / preset table
38 */
39
40struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
43};
44
45/* codec vendor labels */
46static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x10ec, "Realtek" },
a9226251 48 { 0x1057, "Motorola" },
c577b8a1 49 { 0x1106, "VIA" },
54b903ec 50 { 0x11d4, "Analog Devices" },
1da177e4 51 { 0x13f6, "C-Media" },
a9226251 52 { 0x14f1, "Conexant" },
1da177e4 53 { 0x434d, "C-Media" },
2f2f4251 54 { 0x8384, "SigmaTel" },
1da177e4
LT
55 {} /* terminator */
56};
57
58/* codec presets */
59#include "hda_patch.h"
60
61
62/**
63 * snd_hda_codec_read - send a command and get the response
64 * @codec: the HDA codec
65 * @nid: NID to send the command
66 * @direct: direct flag
67 * @verb: the verb to send
68 * @parm: the parameter for the verb
69 *
70 * Send a single command and read the corresponding response.
71 *
72 * Returns the obtained response value, or -1 for an error.
73 */
0ba21762
TI
74unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
75 int direct,
1da177e4
LT
76 unsigned int verb, unsigned int parm)
77{
78 unsigned int res;
62932df8 79 mutex_lock(&codec->bus->cmd_mutex);
0ba21762 80 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
1da177e4
LT
81 res = codec->bus->ops.get_response(codec);
82 else
83 res = (unsigned int)-1;
62932df8 84 mutex_unlock(&codec->bus->cmd_mutex);
1da177e4
LT
85 return res;
86}
87
88/**
89 * snd_hda_codec_write - send a single command without waiting for response
90 * @codec: the HDA codec
91 * @nid: NID to send the command
92 * @direct: direct flag
93 * @verb: the verb to send
94 * @parm: the parameter for the verb
95 *
96 * Send a single command without waiting for response.
97 *
98 * Returns 0 if successful, or a negative error code.
99 */
100int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
101 unsigned int verb, unsigned int parm)
102{
103 int err;
62932df8 104 mutex_lock(&codec->bus->cmd_mutex);
1da177e4 105 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
62932df8 106 mutex_unlock(&codec->bus->cmd_mutex);
1da177e4
LT
107 return err;
108}
109
110/**
111 * snd_hda_sequence_write - sequence writes
112 * @codec: the HDA codec
113 * @seq: VERB array to send
114 *
115 * Send the commands sequentially from the given array.
116 * The array must be terminated with NID=0.
117 */
118void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
119{
120 for (; seq->nid; seq++)
121 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
122}
123
124/**
125 * snd_hda_get_sub_nodes - get the range of sub nodes
126 * @codec: the HDA codec
127 * @nid: NID to parse
128 * @start_id: the pointer to store the start NID
129 *
130 * Parse the NID and store the start NID of its sub-nodes.
131 * Returns the number of sub-nodes.
132 */
0ba21762
TI
133int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
134 hda_nid_t *start_id)
1da177e4
LT
135{
136 unsigned int parm;
137
138 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
139 *start_id = (parm >> 16) & 0x7fff;
140 return (int)(parm & 0x7fff);
141}
142
143/**
144 * snd_hda_get_connections - get connection list
145 * @codec: the HDA codec
146 * @nid: NID to parse
147 * @conn_list: connection list array
148 * @max_conns: max. number of connections to store
149 *
150 * Parses the connection list of the given widget and stores the list
151 * of NIDs.
152 *
153 * Returns the number of connections, or a negative error code.
154 */
155int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
156 hda_nid_t *conn_list, int max_conns)
157{
158 unsigned int parm;
54d17403 159 int i, conn_len, conns;
1da177e4 160 unsigned int shift, num_elems, mask;
54d17403 161 hda_nid_t prev_nid;
1da177e4
LT
162
163 snd_assert(conn_list && max_conns > 0, return -EINVAL);
164
165 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
166 if (parm & AC_CLIST_LONG) {
167 /* long form */
168 shift = 16;
169 num_elems = 2;
170 } else {
171 /* short form */
172 shift = 8;
173 num_elems = 4;
174 }
175 conn_len = parm & AC_CLIST_LENGTH;
1da177e4
LT
176 mask = (1 << (shift-1)) - 1;
177
0ba21762 178 if (!conn_len)
1da177e4
LT
179 return 0; /* no connection */
180
181 if (conn_len == 1) {
182 /* single connection */
0ba21762
TI
183 parm = snd_hda_codec_read(codec, nid, 0,
184 AC_VERB_GET_CONNECT_LIST, 0);
1da177e4
LT
185 conn_list[0] = parm & mask;
186 return 1;
187 }
188
189 /* multi connection */
190 conns = 0;
54d17403
TI
191 prev_nid = 0;
192 for (i = 0; i < conn_len; i++) {
193 int range_val;
194 hda_nid_t val, n;
195
196 if (i % num_elems == 0)
197 parm = snd_hda_codec_read(codec, nid, 0,
198 AC_VERB_GET_CONNECT_LIST, i);
0ba21762 199 range_val = !!(parm & (1 << (shift-1))); /* ranges */
54d17403
TI
200 val = parm & mask;
201 parm >>= shift;
202 if (range_val) {
203 /* ranges between the previous and this one */
0ba21762
TI
204 if (!prev_nid || prev_nid >= val) {
205 snd_printk(KERN_WARNING "hda_codec: "
206 "invalid dep_range_val %x:%x\n",
207 prev_nid, val);
54d17403
TI
208 continue;
209 }
210 for (n = prev_nid + 1; n <= val; n++) {
211 if (conns >= max_conns) {
0ba21762
TI
212 snd_printk(KERN_ERR
213 "Too many connections\n");
1da177e4 214 return -EINVAL;
54d17403
TI
215 }
216 conn_list[conns++] = n;
1da177e4 217 }
54d17403
TI
218 } else {
219 if (conns >= max_conns) {
220 snd_printk(KERN_ERR "Too many connections\n");
221 return -EINVAL;
222 }
223 conn_list[conns++] = val;
1da177e4 224 }
54d17403 225 prev_nid = val;
1da177e4
LT
226 }
227 return conns;
228}
229
230
231/**
232 * snd_hda_queue_unsol_event - add an unsolicited event to queue
233 * @bus: the BUS
234 * @res: unsolicited event (lower 32bit of RIRB entry)
235 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
236 *
237 * Adds the given event to the queue. The events are processed in
238 * the workqueue asynchronously. Call this function in the interrupt
239 * hanlder when RIRB receives an unsolicited event.
240 *
241 * Returns 0 if successful, or a negative error code.
242 */
243int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
244{
245 struct hda_bus_unsolicited *unsol;
246 unsigned int wp;
247
0ba21762
TI
248 unsol = bus->unsol;
249 if (!unsol)
1da177e4
LT
250 return 0;
251
252 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
253 unsol->wp = wp;
254
255 wp <<= 1;
256 unsol->queue[wp] = res;
257 unsol->queue[wp + 1] = res_ex;
258
e250af29 259 schedule_work(&unsol->work);
1da177e4
LT
260
261 return 0;
262}
263
264/*
265 * process queueud unsolicited events
266 */
c4028958 267static void process_unsol_events(struct work_struct *work)
1da177e4 268{
c4028958
DH
269 struct hda_bus_unsolicited *unsol =
270 container_of(work, struct hda_bus_unsolicited, work);
271 struct hda_bus *bus = unsol->bus;
1da177e4
LT
272 struct hda_codec *codec;
273 unsigned int rp, caddr, res;
274
275 while (unsol->rp != unsol->wp) {
276 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
277 unsol->rp = rp;
278 rp <<= 1;
279 res = unsol->queue[rp];
280 caddr = unsol->queue[rp + 1];
0ba21762 281 if (!(caddr & (1 << 4))) /* no unsolicited event? */
1da177e4
LT
282 continue;
283 codec = bus->caddr_tbl[caddr & 0x0f];
284 if (codec && codec->patch_ops.unsol_event)
285 codec->patch_ops.unsol_event(codec, res);
286 }
287}
288
289/*
290 * initialize unsolicited queue
291 */
756e2b01 292static int __devinit init_unsol_queue(struct hda_bus *bus)
1da177e4
LT
293{
294 struct hda_bus_unsolicited *unsol;
295
9f146bb6
TI
296 if (bus->unsol) /* already initialized */
297 return 0;
298
e560d8d8 299 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
0ba21762
TI
300 if (!unsol) {
301 snd_printk(KERN_ERR "hda_codec: "
302 "can't allocate unsolicited queue\n");
1da177e4
LT
303 return -ENOMEM;
304 }
c4028958
DH
305 INIT_WORK(&unsol->work, process_unsol_events);
306 unsol->bus = bus;
1da177e4
LT
307 bus->unsol = unsol;
308 return 0;
309}
310
311/*
312 * destructor
313 */
314static void snd_hda_codec_free(struct hda_codec *codec);
315
316static int snd_hda_bus_free(struct hda_bus *bus)
317{
0ba21762 318 struct hda_codec *codec, *n;
1da177e4 319
0ba21762 320 if (!bus)
1da177e4
LT
321 return 0;
322 if (bus->unsol) {
e250af29 323 flush_scheduled_work();
1da177e4
LT
324 kfree(bus->unsol);
325 }
0ba21762 326 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
1da177e4
LT
327 snd_hda_codec_free(codec);
328 }
329 if (bus->ops.private_free)
330 bus->ops.private_free(bus);
331 kfree(bus);
332 return 0;
333}
334
c8b6bf9b 335static int snd_hda_bus_dev_free(struct snd_device *device)
1da177e4
LT
336{
337 struct hda_bus *bus = device->device_data;
338 return snd_hda_bus_free(bus);
339}
340
341/**
342 * snd_hda_bus_new - create a HDA bus
343 * @card: the card entry
344 * @temp: the template for hda_bus information
345 * @busp: the pointer to store the created bus instance
346 *
347 * Returns 0 if successful, or a negative error code.
348 */
756e2b01
TI
349int __devinit snd_hda_bus_new(struct snd_card *card,
350 const struct hda_bus_template *temp,
351 struct hda_bus **busp)
1da177e4
LT
352{
353 struct hda_bus *bus;
354 int err;
c8b6bf9b 355 static struct snd_device_ops dev_ops = {
1da177e4
LT
356 .dev_free = snd_hda_bus_dev_free,
357 };
358
359 snd_assert(temp, return -EINVAL);
360 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
361
362 if (busp)
363 *busp = NULL;
364
e560d8d8 365 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
366 if (bus == NULL) {
367 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
368 return -ENOMEM;
369 }
370
371 bus->card = card;
372 bus->private_data = temp->private_data;
373 bus->pci = temp->pci;
374 bus->modelname = temp->modelname;
375 bus->ops = temp->ops;
376
62932df8 377 mutex_init(&bus->cmd_mutex);
1da177e4
LT
378 INIT_LIST_HEAD(&bus->codec_list);
379
0ba21762
TI
380 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
381 if (err < 0) {
1da177e4
LT
382 snd_hda_bus_free(bus);
383 return err;
384 }
385 if (busp)
386 *busp = bus;
387 return 0;
388}
389
1da177e4
LT
390/*
391 * find a matching codec preset
392 */
756e2b01
TI
393static const struct hda_codec_preset __devinit *
394find_codec_preset(struct hda_codec *codec)
1da177e4
LT
395{
396 const struct hda_codec_preset **tbl, *preset;
397
d5ad630b
TI
398 if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
399 return NULL; /* use the generic parser */
400
1da177e4
LT
401 for (tbl = hda_preset_tables; *tbl; tbl++) {
402 for (preset = *tbl; preset->id; preset++) {
403 u32 mask = preset->mask;
0ba21762 404 if (!mask)
1da177e4 405 mask = ~0;
9c7f852e 406 if (preset->id == (codec->vendor_id & mask) &&
0ba21762 407 (!preset->rev ||
9c7f852e 408 preset->rev == codec->revision_id))
1da177e4
LT
409 return preset;
410 }
411 }
412 return NULL;
413}
414
415/*
416 * snd_hda_get_codec_name - store the codec name
417 */
418void snd_hda_get_codec_name(struct hda_codec *codec,
419 char *name, int namelen)
420{
421 const struct hda_vendor_id *c;
422 const char *vendor = NULL;
423 u16 vendor_id = codec->vendor_id >> 16;
424 char tmp[16];
425
426 for (c = hda_vendor_ids; c->id; c++) {
427 if (c->id == vendor_id) {
428 vendor = c->name;
429 break;
430 }
431 }
0ba21762 432 if (!vendor) {
1da177e4
LT
433 sprintf(tmp, "Generic %04x", vendor_id);
434 vendor = tmp;
435 }
436 if (codec->preset && codec->preset->name)
437 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
438 else
0ba21762
TI
439 snprintf(name, namelen, "%s ID %x", vendor,
440 codec->vendor_id & 0xffff);
1da177e4
LT
441}
442
443/*
673b683a 444 * look for an AFG and MFG nodes
1da177e4 445 */
756e2b01 446static void __devinit setup_fg_nodes(struct hda_codec *codec)
1da177e4
LT
447{
448 int i, total_nodes;
449 hda_nid_t nid;
450
451 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
452 for (i = 0; i < total_nodes; i++, nid++) {
0ba21762
TI
453 unsigned int func;
454 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
455 switch (func & 0xff) {
673b683a
SK
456 case AC_GRP_AUDIO_FUNCTION:
457 codec->afg = nid;
458 break;
459 case AC_GRP_MODEM_FUNCTION:
460 codec->mfg = nid;
461 break;
462 default:
463 break;
464 }
1da177e4 465 }
1da177e4
LT
466}
467
54d17403
TI
468/*
469 * read widget caps for each widget and store in cache
470 */
471static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
472{
473 int i;
474 hda_nid_t nid;
475
476 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
477 &codec->start_nid);
478 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
0ba21762 479 if (!codec->wcaps)
54d17403
TI
480 return -ENOMEM;
481 nid = codec->start_nid;
482 for (i = 0; i < codec->num_nodes; i++, nid++)
483 codec->wcaps[i] = snd_hda_param_read(codec, nid,
484 AC_PAR_AUDIO_WIDGET_CAP);
485 return 0;
486}
487
488
1da177e4
LT
489/*
490 * codec destructor
491 */
492static void snd_hda_codec_free(struct hda_codec *codec)
493{
0ba21762 494 if (!codec)
1da177e4
LT
495 return;
496 list_del(&codec->list);
497 codec->bus->caddr_tbl[codec->addr] = NULL;
498 if (codec->patch_ops.free)
499 codec->patch_ops.free(codec);
d031166f 500 kfree(codec->amp_info);
54d17403 501 kfree(codec->wcaps);
1da177e4
LT
502 kfree(codec);
503}
504
505static void init_amp_hash(struct hda_codec *codec);
506
507/**
508 * snd_hda_codec_new - create a HDA codec
509 * @bus: the bus to assign
510 * @codec_addr: the codec address
511 * @codecp: the pointer to store the generated codec
512 *
513 * Returns 0 if successful, or a negative error code.
514 */
756e2b01
TI
515int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
516 struct hda_codec **codecp)
1da177e4
LT
517{
518 struct hda_codec *codec;
519 char component[13];
520 int err;
521
522 snd_assert(bus, return -EINVAL);
523 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
524
525 if (bus->caddr_tbl[codec_addr]) {
0ba21762
TI
526 snd_printk(KERN_ERR "hda_codec: "
527 "address 0x%x is already occupied\n", codec_addr);
1da177e4
LT
528 return -EBUSY;
529 }
530
e560d8d8 531 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1da177e4
LT
532 if (codec == NULL) {
533 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
534 return -ENOMEM;
535 }
536
537 codec->bus = bus;
538 codec->addr = codec_addr;
62932df8 539 mutex_init(&codec->spdif_mutex);
1da177e4
LT
540 init_amp_hash(codec);
541
542 list_add_tail(&codec->list, &bus->codec_list);
543 bus->caddr_tbl[codec_addr] = codec;
544
0ba21762
TI
545 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
546 AC_PAR_VENDOR_ID);
111d3af5
TI
547 if (codec->vendor_id == -1)
548 /* read again, hopefully the access method was corrected
549 * in the last read...
550 */
551 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
552 AC_PAR_VENDOR_ID);
0ba21762
TI
553 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
554 AC_PAR_SUBSYSTEM_ID);
555 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
556 AC_PAR_REV_ID);
1da177e4 557
673b683a 558 setup_fg_nodes(codec);
0ba21762 559 if (!codec->afg && !codec->mfg) {
673b683a 560 snd_printdd("hda_codec: no AFG or MFG node found\n");
1da177e4
LT
561 snd_hda_codec_free(codec);
562 return -ENODEV;
563 }
564
54d17403
TI
565 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
566 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
567 snd_hda_codec_free(codec);
568 return -ENOMEM;
569 }
570
0ba21762 571 if (!codec->subsystem_id) {
86284e45 572 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
0ba21762
TI
573 codec->subsystem_id =
574 snd_hda_codec_read(codec, nid, 0,
575 AC_VERB_GET_SUBSYSTEM_ID, 0);
86284e45
TI
576 }
577
d5ad630b 578 codec->preset = find_codec_preset(codec);
43ea1d47
TI
579 /* audio codec should override the mixer name */
580 if (codec->afg || !*bus->card->mixername)
1da177e4
LT
581 snd_hda_get_codec_name(codec, bus->card->mixername,
582 sizeof(bus->card->mixername));
583
584 if (codec->preset && codec->preset->patch)
585 err = codec->preset->patch(codec);
586 else
587 err = snd_hda_parse_generic_codec(codec);
588 if (err < 0) {
589 snd_hda_codec_free(codec);
590 return err;
591 }
592
9f146bb6
TI
593 if (codec->patch_ops.unsol_event)
594 init_unsol_queue(bus);
595
1da177e4
LT
596 snd_hda_codec_proc_new(codec);
597
598 sprintf(component, "HDA:%08x", codec->vendor_id);
599 snd_component_add(codec->bus->card, component);
600
601 if (codecp)
602 *codecp = codec;
603 return 0;
604}
605
606/**
607 * snd_hda_codec_setup_stream - set up the codec for streaming
608 * @codec: the CODEC to set up
609 * @nid: the NID to set up
610 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
611 * @channel_id: channel id to pass, zero based.
612 * @format: stream format.
613 */
0ba21762
TI
614void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
615 u32 stream_tag,
1da177e4
LT
616 int channel_id, int format)
617{
0ba21762 618 if (!nid)
d21b37ea
TI
619 return;
620
0ba21762
TI
621 snd_printdd("hda_codec_setup_stream: "
622 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1da177e4
LT
623 nid, stream_tag, channel_id, format);
624 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
625 (stream_tag << 4) | channel_id);
626 msleep(1);
627 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
628}
629
1da177e4
LT
630/*
631 * amp access functions
632 */
633
4a19faee
TI
634/* FIXME: more better hash key? */
635#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1da177e4 636#define INFO_AMP_CAPS (1<<0)
4a19faee 637#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1da177e4
LT
638
639/* initialize the hash table */
756e2b01 640static void __devinit init_amp_hash(struct hda_codec *codec)
1da177e4
LT
641{
642 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
643 codec->num_amp_entries = 0;
d031166f
TI
644 codec->amp_info_size = 0;
645 codec->amp_info = NULL;
1da177e4
LT
646}
647
648/* query the hash. allocate an entry if not found. */
649static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
650{
651 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
652 u16 cur = codec->amp_hash[idx];
653 struct hda_amp_info *info;
654
655 while (cur != 0xffff) {
656 info = &codec->amp_info[cur];
657 if (info->key == key)
658 return info;
659 cur = info->next;
660 }
661
662 /* add a new hash entry */
d031166f
TI
663 if (codec->num_amp_entries >= codec->amp_info_size) {
664 /* reallocate the array */
665 int new_size = codec->amp_info_size + 64;
0ba21762
TI
666 struct hda_amp_info *new_info;
667 new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
668 GFP_KERNEL);
669 if (!new_info) {
670 snd_printk(KERN_ERR "hda_codec: "
671 "can't malloc amp_info\n");
d031166f
TI
672 return NULL;
673 }
674 if (codec->amp_info) {
675 memcpy(new_info, codec->amp_info,
0ba21762
TI
676 codec->amp_info_size *
677 sizeof(struct hda_amp_info));
d031166f
TI
678 kfree(codec->amp_info);
679 }
680 codec->amp_info_size = new_size;
681 codec->amp_info = new_info;
1da177e4
LT
682 }
683 cur = codec->num_amp_entries++;
684 info = &codec->amp_info[cur];
685 info->key = key;
686 info->status = 0; /* not initialized yet */
687 info->next = codec->amp_hash[idx];
688 codec->amp_hash[idx] = cur;
689
690 return info;
691}
692
693/*
694 * query AMP capabilities for the given widget and direction
695 */
696static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
697{
0ba21762 698 struct hda_amp_info *info;
1da177e4 699
0ba21762
TI
700 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
701 if (!info)
1da177e4 702 return 0;
0ba21762
TI
703 if (!(info->status & INFO_AMP_CAPS)) {
704 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1da177e4 705 nid = codec->afg;
0ba21762
TI
706 info->amp_caps = snd_hda_param_read(codec, nid,
707 direction == HDA_OUTPUT ?
708 AC_PAR_AMP_OUT_CAP :
709 AC_PAR_AMP_IN_CAP);
b75e53f0
TI
710 if (info->amp_caps)
711 info->status |= INFO_AMP_CAPS;
1da177e4
LT
712 }
713 return info->amp_caps;
714}
715
897cc188
TI
716int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
717 unsigned int caps)
718{
719 struct hda_amp_info *info;
720
721 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
722 if (!info)
723 return -EINVAL;
724 info->amp_caps = caps;
725 info->status |= INFO_AMP_CAPS;
726 return 0;
727}
728
1da177e4
LT
729/*
730 * read the current volume to info
4a19faee 731 * if the cache exists, read the cache value.
1da177e4 732 */
0ba21762
TI
733static unsigned int get_vol_mute(struct hda_codec *codec,
734 struct hda_amp_info *info, hda_nid_t nid,
735 int ch, int direction, int index)
1da177e4
LT
736{
737 u32 val, parm;
738
4a19faee
TI
739 if (info->status & INFO_AMP_VOL(ch))
740 return info->vol[ch];
1da177e4
LT
741
742 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
743 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
744 parm |= index;
0ba21762
TI
745 val = snd_hda_codec_read(codec, nid, 0,
746 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1da177e4 747 info->vol[ch] = val & 0xff;
4a19faee
TI
748 info->status |= INFO_AMP_VOL(ch);
749 return info->vol[ch];
1da177e4
LT
750}
751
752/*
4a19faee 753 * write the current volume in info to the h/w and update the cache
1da177e4 754 */
4a19faee 755static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
0ba21762
TI
756 hda_nid_t nid, int ch, int direction, int index,
757 int val)
1da177e4
LT
758{
759 u32 parm;
760
761 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
762 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
763 parm |= index << AC_AMP_SET_INDEX_SHIFT;
764 parm |= val;
765 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
4a19faee 766 info->vol[ch] = val;
1da177e4
LT
767}
768
769/*
4a19faee 770 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1da177e4 771 */
834be88d
TI
772int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
773 int direction, int index)
1da177e4 774{
0ba21762
TI
775 struct hda_amp_info *info;
776 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
777 if (!info)
1da177e4 778 return 0;
4a19faee 779 return get_vol_mute(codec, info, nid, ch, direction, index);
1da177e4
LT
780}
781
4a19faee
TI
782/*
783 * update the AMP value, mask = bit mask to set, val = the value
784 */
834be88d
TI
785int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
786 int direction, int idx, int mask, int val)
1da177e4 787{
0ba21762 788 struct hda_amp_info *info;
4a19faee 789
0ba21762
TI
790 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
791 if (!info)
1da177e4 792 return 0;
4a19faee
TI
793 val &= mask;
794 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
0ba21762 795 if (info->vol[ch] == val && !codec->in_resume)
1da177e4 796 return 0;
4a19faee 797 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1da177e4
LT
798 return 1;
799}
800
801
802/*
803 * AMP control callbacks
804 */
805/* retrieve parameters from private_value */
806#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
807#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
808#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
809#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
810
811/* volume */
0ba21762
TI
812int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
814{
815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
816 u16 nid = get_amp_nid(kcontrol);
817 u8 chs = get_amp_channels(kcontrol);
818 int dir = get_amp_direction(kcontrol);
819 u32 caps;
820
821 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
822 /* num steps */
823 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
824 if (!caps) {
825 printk(KERN_WARNING "hda_codec: "
826 "num_steps = 0 for NID=0x%x\n", nid);
1da177e4
LT
827 return -EINVAL;
828 }
829 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
830 uinfo->count = chs == 3 ? 2 : 1;
831 uinfo->value.integer.min = 0;
832 uinfo->value.integer.max = caps;
833 return 0;
834}
835
0ba21762
TI
836int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
837 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
838{
839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
840 hda_nid_t nid = get_amp_nid(kcontrol);
841 int chs = get_amp_channels(kcontrol);
842 int dir = get_amp_direction(kcontrol);
843 int idx = get_amp_index(kcontrol);
844 long *valp = ucontrol->value.integer.value;
845
846 if (chs & 1)
847 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
848 if (chs & 2)
849 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
850 return 0;
851}
852
0ba21762
TI
853int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
854 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
855{
856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
857 hda_nid_t nid = get_amp_nid(kcontrol);
858 int chs = get_amp_channels(kcontrol);
859 int dir = get_amp_direction(kcontrol);
860 int idx = get_amp_index(kcontrol);
1da177e4
LT
861 long *valp = ucontrol->value.integer.value;
862 int change = 0;
863
b9f5a89c 864 if (chs & 1) {
4a19faee
TI
865 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
866 0x7f, *valp);
b9f5a89c
NG
867 valp++;
868 }
4a19faee
TI
869 if (chs & 2)
870 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
b9f5a89c 871 0x7f, *valp);
1da177e4
LT
872 return change;
873}
874
302e9c5a
JK
875int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
876 unsigned int size, unsigned int __user *_tlv)
877{
878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
879 hda_nid_t nid = get_amp_nid(kcontrol);
880 int dir = get_amp_direction(kcontrol);
881 u32 caps, val1, val2;
882
883 if (size < 4 * sizeof(unsigned int))
884 return -ENOMEM;
885 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
886 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
887 val2 = (val2 + 1) * 25;
302e9c5a
JK
888 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
889 val1 = ((int)val1) * ((int)val2);
302e9c5a
JK
890 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
891 return -EFAULT;
892 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
893 return -EFAULT;
894 if (put_user(val1, _tlv + 2))
895 return -EFAULT;
896 if (put_user(val2, _tlv + 3))
897 return -EFAULT;
898 return 0;
899}
900
1da177e4 901/* switch */
0ba21762
TI
902int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
903 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
904{
905 int chs = get_amp_channels(kcontrol);
906
907 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
908 uinfo->count = chs == 3 ? 2 : 1;
909 uinfo->value.integer.min = 0;
910 uinfo->value.integer.max = 1;
911 return 0;
912}
913
0ba21762
TI
914int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
915 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
916{
917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
918 hda_nid_t nid = get_amp_nid(kcontrol);
919 int chs = get_amp_channels(kcontrol);
920 int dir = get_amp_direction(kcontrol);
921 int idx = get_amp_index(kcontrol);
922 long *valp = ucontrol->value.integer.value;
923
924 if (chs & 1)
0ba21762
TI
925 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
926 0x80) ? 0 : 1;
1da177e4 927 if (chs & 2)
0ba21762
TI
928 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
929 0x80) ? 0 : 1;
1da177e4
LT
930 return 0;
931}
932
0ba21762
TI
933int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
934 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
935{
936 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
937 hda_nid_t nid = get_amp_nid(kcontrol);
938 int chs = get_amp_channels(kcontrol);
939 int dir = get_amp_direction(kcontrol);
940 int idx = get_amp_index(kcontrol);
1da177e4
LT
941 long *valp = ucontrol->value.integer.value;
942 int change = 0;
943
b9f5a89c 944 if (chs & 1) {
4a19faee
TI
945 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
946 0x80, *valp ? 0 : 0x80);
b9f5a89c
NG
947 valp++;
948 }
4a19faee
TI
949 if (chs & 2)
950 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
b9f5a89c
NG
951 0x80, *valp ? 0 : 0x80);
952
1da177e4
LT
953 return change;
954}
955
985be54b
TI
956/*
957 * bound volume controls
958 *
959 * bind multiple volumes (# indices, from 0)
960 */
961
962#define AMP_VAL_IDX_SHIFT 19
963#define AMP_VAL_IDX_MASK (0x0f<<19)
964
0ba21762
TI
965int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
967{
968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
969 unsigned long pval;
970 int err;
971
62932df8 972 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
985be54b
TI
973 pval = kcontrol->private_value;
974 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
975 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
976 kcontrol->private_value = pval;
62932df8 977 mutex_unlock(&codec->spdif_mutex);
985be54b
TI
978 return err;
979}
980
0ba21762
TI
981int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
983{
984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
985 unsigned long pval;
986 int i, indices, err = 0, change = 0;
987
62932df8 988 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
985be54b
TI
989 pval = kcontrol->private_value;
990 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
991 for (i = 0; i < indices; i++) {
0ba21762
TI
992 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
993 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
994 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
995 if (err < 0)
996 break;
997 change |= err;
998 }
999 kcontrol->private_value = pval;
62932df8 1000 mutex_unlock(&codec->spdif_mutex);
985be54b
TI
1001 return err < 0 ? err : change;
1002}
1003
1da177e4
LT
1004/*
1005 * SPDIF out controls
1006 */
1007
0ba21762
TI
1008static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1010{
1011 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1012 uinfo->count = 1;
1013 return 0;
1014}
1015
0ba21762
TI
1016static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1017 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1018{
1019 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1020 IEC958_AES0_NONAUDIO |
1021 IEC958_AES0_CON_EMPHASIS_5015 |
1022 IEC958_AES0_CON_NOT_COPYRIGHT;
1023 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1024 IEC958_AES1_CON_ORIGINAL;
1025 return 0;
1026}
1027
0ba21762
TI
1028static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1029 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1030{
1031 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1032 IEC958_AES0_NONAUDIO |
1033 IEC958_AES0_PRO_EMPHASIS_5015;
1034 return 0;
1035}
1036
0ba21762
TI
1037static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1038 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1039{
1040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1041
1042 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1043 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1044 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1045 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1046
1047 return 0;
1048}
1049
1050/* convert from SPDIF status bits to HDA SPDIF bits
1051 * bit 0 (DigEn) is always set zero (to be filled later)
1052 */
1053static unsigned short convert_from_spdif_status(unsigned int sbits)
1054{
1055 unsigned short val = 0;
1056
1057 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 1058 val |= AC_DIG1_PROFESSIONAL;
1da177e4 1059 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 1060 val |= AC_DIG1_NONAUDIO;
1da177e4 1061 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
1062 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1063 IEC958_AES0_PRO_EMPHASIS_5015)
1064 val |= AC_DIG1_EMPHASIS;
1da177e4 1065 } else {
0ba21762
TI
1066 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1067 IEC958_AES0_CON_EMPHASIS_5015)
1068 val |= AC_DIG1_EMPHASIS;
1069 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1070 val |= AC_DIG1_COPYRIGHT;
1da177e4 1071 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 1072 val |= AC_DIG1_LEVEL;
1da177e4
LT
1073 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1074 }
1075 return val;
1076}
1077
1078/* convert to SPDIF status bits from HDA SPDIF bits
1079 */
1080static unsigned int convert_to_spdif_status(unsigned short val)
1081{
1082 unsigned int sbits = 0;
1083
0ba21762 1084 if (val & AC_DIG1_NONAUDIO)
1da177e4 1085 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 1086 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
1087 sbits |= IEC958_AES0_PROFESSIONAL;
1088 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762 1089 if (sbits & AC_DIG1_EMPHASIS)
1da177e4
LT
1090 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1091 } else {
0ba21762 1092 if (val & AC_DIG1_EMPHASIS)
1da177e4 1093 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 1094 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 1095 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 1096 if (val & AC_DIG1_LEVEL)
1da177e4
LT
1097 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1098 sbits |= val & (0x7f << 8);
1099 }
1100 return sbits;
1101}
1102
0ba21762
TI
1103static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1104 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1105{
1106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1107 hda_nid_t nid = kcontrol->private_value;
1108 unsigned short val;
1109 int change;
1110
62932df8 1111 mutex_lock(&codec->spdif_mutex);
1da177e4
LT
1112 codec->spdif_status = ucontrol->value.iec958.status[0] |
1113 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1114 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1115 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1116 val = convert_from_spdif_status(codec->spdif_status);
1117 val |= codec->spdif_ctls & 1;
1118 change = codec->spdif_ctls != val;
1119 codec->spdif_ctls = val;
1120
1121 if (change || codec->in_resume) {
0ba21762
TI
1122 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1123 val & 0xff);
1124 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1125 val >> 8);
1da177e4
LT
1126 }
1127
62932df8 1128 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1129 return change;
1130}
1131
0ba21762
TI
1132static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol,
1133 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1134{
1135 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1136 uinfo->count = 1;
1137 uinfo->value.integer.min = 0;
1138 uinfo->value.integer.max = 1;
1139 return 0;
1140}
1141
0ba21762
TI
1142static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1143 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1144{
1145 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1146
0ba21762 1147 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1da177e4
LT
1148 return 0;
1149}
1150
0ba21762
TI
1151static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1152 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1153{
1154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1155 hda_nid_t nid = kcontrol->private_value;
1156 unsigned short val;
1157 int change;
1158
62932df8 1159 mutex_lock(&codec->spdif_mutex);
0ba21762 1160 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1da177e4 1161 if (ucontrol->value.integer.value[0])
0ba21762 1162 val |= AC_DIG1_ENABLE;
1da177e4
LT
1163 change = codec->spdif_ctls != val;
1164 if (change || codec->in_resume) {
1165 codec->spdif_ctls = val;
6b97eb45
TI
1166 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1167 val & 0xff);
0ba21762
TI
1168 /* unmute amp switch (if any) */
1169 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1170 (val & AC_DIG1_ENABLE))
6b97eb45
TI
1171 snd_hda_codec_write(codec, nid, 0,
1172 AC_VERB_SET_AMP_GAIN_MUTE,
1173 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
0ba21762 1174 AC_AMP_SET_OUTPUT);
1da177e4 1175 }
62932df8 1176 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1177 return change;
1178}
1179
c8b6bf9b 1180static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
1181 {
1182 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1183 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1184 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1185 .info = snd_hda_spdif_mask_info,
1186 .get = snd_hda_spdif_cmask_get,
1187 },
1188 {
1189 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1192 .info = snd_hda_spdif_mask_info,
1193 .get = snd_hda_spdif_pmask_get,
1194 },
1195 {
1196 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1197 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1198 .info = snd_hda_spdif_mask_info,
1199 .get = snd_hda_spdif_default_get,
1200 .put = snd_hda_spdif_default_put,
1201 },
1202 {
1203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1204 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1205 .info = snd_hda_spdif_out_switch_info,
1206 .get = snd_hda_spdif_out_switch_get,
1207 .put = snd_hda_spdif_out_switch_put,
1208 },
1209 { } /* end */
1210};
1211
1212/**
1213 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1214 * @codec: the HDA codec
1215 * @nid: audio out widget NID
1216 *
1217 * Creates controls related with the SPDIF output.
1218 * Called from each patch supporting the SPDIF out.
1219 *
1220 * Returns 0 if successful, or a negative error code.
1221 */
756e2b01
TI
1222int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
1223 hda_nid_t nid)
1da177e4
LT
1224{
1225 int err;
c8b6bf9b
TI
1226 struct snd_kcontrol *kctl;
1227 struct snd_kcontrol_new *dig_mix;
1da177e4
LT
1228
1229 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1230 kctl = snd_ctl_new1(dig_mix, codec);
1231 kctl->private_value = nid;
0ba21762
TI
1232 err = snd_ctl_add(codec->bus->card, kctl);
1233 if (err < 0)
1da177e4
LT
1234 return err;
1235 }
0ba21762
TI
1236 codec->spdif_ctls =
1237 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1da177e4
LT
1238 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1239 return 0;
1240}
1241
1242/*
1243 * SPDIF input
1244 */
1245
1246#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1247
0ba21762
TI
1248static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1249 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1250{
1251 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1252
1253 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1254 return 0;
1255}
1256
0ba21762
TI
1257static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1258 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1259{
1260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1261 hda_nid_t nid = kcontrol->private_value;
1262 unsigned int val = !!ucontrol->value.integer.value[0];
1263 int change;
1264
62932df8 1265 mutex_lock(&codec->spdif_mutex);
1da177e4
LT
1266 change = codec->spdif_in_enable != val;
1267 if (change || codec->in_resume) {
1268 codec->spdif_in_enable = val;
0ba21762
TI
1269 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1270 val);
1da177e4 1271 }
62932df8 1272 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1273 return change;
1274}
1275
0ba21762
TI
1276static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1278{
1279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1280 hda_nid_t nid = kcontrol->private_value;
1281 unsigned short val;
1282 unsigned int sbits;
1283
1284 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1285 sbits = convert_to_spdif_status(val);
1286 ucontrol->value.iec958.status[0] = sbits;
1287 ucontrol->value.iec958.status[1] = sbits >> 8;
1288 ucontrol->value.iec958.status[2] = sbits >> 16;
1289 ucontrol->value.iec958.status[3] = sbits >> 24;
1290 return 0;
1291}
1292
c8b6bf9b 1293static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
1294 {
1295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1296 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1297 .info = snd_hda_spdif_in_switch_info,
1298 .get = snd_hda_spdif_in_switch_get,
1299 .put = snd_hda_spdif_in_switch_put,
1300 },
1301 {
1302 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1303 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1304 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1305 .info = snd_hda_spdif_mask_info,
1306 .get = snd_hda_spdif_in_status_get,
1307 },
1308 { } /* end */
1309};
1310
1311/**
1312 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1313 * @codec: the HDA codec
1314 * @nid: audio in widget NID
1315 *
1316 * Creates controls related with the SPDIF input.
1317 * Called from each patch supporting the SPDIF in.
1318 *
1319 * Returns 0 if successful, or a negative error code.
1320 */
756e2b01
TI
1321int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
1322 hda_nid_t nid)
1da177e4
LT
1323{
1324 int err;
c8b6bf9b
TI
1325 struct snd_kcontrol *kctl;
1326 struct snd_kcontrol_new *dig_mix;
1da177e4
LT
1327
1328 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1329 kctl = snd_ctl_new1(dig_mix, codec);
1330 kctl->private_value = nid;
0ba21762
TI
1331 err = snd_ctl_add(codec->bus->card, kctl);
1332 if (err < 0)
1da177e4
LT
1333 return err;
1334 }
0ba21762
TI
1335 codec->spdif_in_enable =
1336 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1337 AC_DIG1_ENABLE;
1da177e4
LT
1338 return 0;
1339}
1340
1341
54d17403
TI
1342/*
1343 * set power state of the codec
1344 */
1345static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1346 unsigned int power_state)
1347{
1348 hda_nid_t nid, nid_start;
1349 int nodes;
1350
1351 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1352 power_state);
1353
1354 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1355 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1356 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1357 snd_hda_codec_write(codec, nid, 0,
1358 AC_VERB_SET_POWER_STATE,
1359 power_state);
1360 }
1361
1362 if (power_state == AC_PWRST_D0)
1363 msleep(10);
1364}
1365
1366
1da177e4
LT
1367/**
1368 * snd_hda_build_controls - build mixer controls
1369 * @bus: the BUS
1370 *
1371 * Creates mixer controls for each codec included in the bus.
1372 *
1373 * Returns 0 if successful, otherwise a negative error code.
1374 */
756e2b01 1375int __devinit snd_hda_build_controls(struct hda_bus *bus)
1da177e4 1376{
0ba21762 1377 struct hda_codec *codec;
1da177e4
LT
1378
1379 /* build controls */
0ba21762 1380 list_for_each_entry(codec, &bus->codec_list, list) {
1da177e4 1381 int err;
0ba21762 1382 if (!codec->patch_ops.build_controls)
1da177e4
LT
1383 continue;
1384 err = codec->patch_ops.build_controls(codec);
1385 if (err < 0)
1386 return err;
1387 }
1388
1389 /* initialize */
0ba21762 1390 list_for_each_entry(codec, &bus->codec_list, list) {
1da177e4 1391 int err;
54d17403
TI
1392 hda_set_power_state(codec,
1393 codec->afg ? codec->afg : codec->mfg,
1394 AC_PWRST_D0);
0ba21762 1395 if (!codec->patch_ops.init)
1da177e4
LT
1396 continue;
1397 err = codec->patch_ops.init(codec);
1398 if (err < 0)
1399 return err;
1400 }
1401 return 0;
1402}
1403
1da177e4
LT
1404/*
1405 * stream formats
1406 */
befdf316
TI
1407struct hda_rate_tbl {
1408 unsigned int hz;
1409 unsigned int alsa_bits;
1410 unsigned int hda_fmt;
1411};
1412
1413static struct hda_rate_tbl rate_bits[] = {
1da177e4 1414 /* rate in Hz, ALSA rate bitmask, HDA format value */
9d8f53f2
NG
1415
1416 /* autodetected value used in snd_hda_query_supported_pcm */
1da177e4
LT
1417 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1418 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1419 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1420 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1421 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1422 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1423 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1424 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1425 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1426 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1427 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
a961f9fe
TI
1428#define AC_PAR_PCM_RATE_BITS 11
1429 /* up to bits 10, 384kHZ isn't supported properly */
1430
1431 /* not autodetected value */
1432 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
9d8f53f2 1433
befdf316 1434 { 0 } /* terminator */
1da177e4
LT
1435};
1436
1437/**
1438 * snd_hda_calc_stream_format - calculate format bitset
1439 * @rate: the sample rate
1440 * @channels: the number of channels
1441 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1442 * @maxbps: the max. bps
1443 *
1444 * Calculate the format bitset from the given rate, channels and th PCM format.
1445 *
1446 * Return zero if invalid.
1447 */
1448unsigned int snd_hda_calc_stream_format(unsigned int rate,
1449 unsigned int channels,
1450 unsigned int format,
1451 unsigned int maxbps)
1452{
1453 int i;
1454 unsigned int val = 0;
1455
befdf316
TI
1456 for (i = 0; rate_bits[i].hz; i++)
1457 if (rate_bits[i].hz == rate) {
1458 val = rate_bits[i].hda_fmt;
1da177e4
LT
1459 break;
1460 }
0ba21762 1461 if (!rate_bits[i].hz) {
1da177e4
LT
1462 snd_printdd("invalid rate %d\n", rate);
1463 return 0;
1464 }
1465
1466 if (channels == 0 || channels > 8) {
1467 snd_printdd("invalid channels %d\n", channels);
1468 return 0;
1469 }
1470 val |= channels - 1;
1471
1472 switch (snd_pcm_format_width(format)) {
1473 case 8: val |= 0x00; break;
1474 case 16: val |= 0x10; break;
1475 case 20:
1476 case 24:
1477 case 32:
1478 if (maxbps >= 32)
1479 val |= 0x40;
1480 else if (maxbps >= 24)
1481 val |= 0x30;
1482 else
1483 val |= 0x20;
1484 break;
1485 default:
0ba21762
TI
1486 snd_printdd("invalid format width %d\n",
1487 snd_pcm_format_width(format));
1da177e4
LT
1488 return 0;
1489 }
1490
1491 return val;
1492}
1493
1494/**
1495 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1496 * @codec: the HDA codec
1497 * @nid: NID to query
1498 * @ratesp: the pointer to store the detected rate bitflags
1499 * @formatsp: the pointer to store the detected formats
1500 * @bpsp: the pointer to store the detected format widths
1501 *
1502 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1503 * or @bsps argument is ignored.
1504 *
1505 * Returns 0 if successful, otherwise a negative error code.
1506 */
1507int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1508 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1509{
1510 int i;
1511 unsigned int val, streams;
1512
1513 val = 0;
1514 if (nid != codec->afg &&
54d17403 1515 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1da177e4
LT
1516 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1517 if (val == -1)
1518 return -EIO;
1519 }
0ba21762 1520 if (!val)
1da177e4
LT
1521 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1522
1523 if (ratesp) {
1524 u32 rates = 0;
a961f9fe 1525 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1da177e4 1526 if (val & (1 << i))
befdf316 1527 rates |= rate_bits[i].alsa_bits;
1da177e4
LT
1528 }
1529 *ratesp = rates;
1530 }
1531
1532 if (formatsp || bpsp) {
1533 u64 formats = 0;
1534 unsigned int bps;
1535 unsigned int wcaps;
1536
54d17403 1537 wcaps = get_wcaps(codec, nid);
1da177e4
LT
1538 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1539 if (streams == -1)
1540 return -EIO;
0ba21762
TI
1541 if (!streams) {
1542 streams = snd_hda_param_read(codec, codec->afg,
1543 AC_PAR_STREAM);
1da177e4
LT
1544 if (streams == -1)
1545 return -EIO;
1546 }
1547
1548 bps = 0;
1549 if (streams & AC_SUPFMT_PCM) {
1550 if (val & AC_SUPPCM_BITS_8) {
1551 formats |= SNDRV_PCM_FMTBIT_U8;
1552 bps = 8;
1553 }
1554 if (val & AC_SUPPCM_BITS_16) {
1555 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1556 bps = 16;
1557 }
1558 if (wcaps & AC_WCAP_DIGITAL) {
1559 if (val & AC_SUPPCM_BITS_32)
1560 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1561 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1562 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1563 if (val & AC_SUPPCM_BITS_24)
1564 bps = 24;
1565 else if (val & AC_SUPPCM_BITS_20)
1566 bps = 20;
0ba21762
TI
1567 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1568 AC_SUPPCM_BITS_32)) {
1da177e4
LT
1569 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1570 if (val & AC_SUPPCM_BITS_32)
1571 bps = 32;
1da177e4
LT
1572 else if (val & AC_SUPPCM_BITS_24)
1573 bps = 24;
33ef7651
NG
1574 else if (val & AC_SUPPCM_BITS_20)
1575 bps = 20;
1da177e4
LT
1576 }
1577 }
0ba21762
TI
1578 else if (streams == AC_SUPFMT_FLOAT32) {
1579 /* should be exclusive */
1da177e4
LT
1580 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1581 bps = 32;
0ba21762
TI
1582 } else if (streams == AC_SUPFMT_AC3) {
1583 /* should be exclusive */
1da177e4
LT
1584 /* temporary hack: we have still no proper support
1585 * for the direct AC3 stream...
1586 */
1587 formats |= SNDRV_PCM_FMTBIT_U8;
1588 bps = 8;
1589 }
1590 if (formatsp)
1591 *formatsp = formats;
1592 if (bpsp)
1593 *bpsp = bps;
1594 }
1595
1596 return 0;
1597}
1598
1599/**
0ba21762
TI
1600 * snd_hda_is_supported_format - check whether the given node supports
1601 * the format val
1da177e4
LT
1602 *
1603 * Returns 1 if supported, 0 if not.
1604 */
1605int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1606 unsigned int format)
1607{
1608 int i;
1609 unsigned int val = 0, rate, stream;
1610
1611 if (nid != codec->afg &&
54d17403 1612 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1da177e4
LT
1613 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1614 if (val == -1)
1615 return 0;
1616 }
0ba21762 1617 if (!val) {
1da177e4
LT
1618 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1619 if (val == -1)
1620 return 0;
1621 }
1622
1623 rate = format & 0xff00;
a961f9fe 1624 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
befdf316 1625 if (rate_bits[i].hda_fmt == rate) {
1da177e4
LT
1626 if (val & (1 << i))
1627 break;
1628 return 0;
1629 }
a961f9fe 1630 if (i >= AC_PAR_PCM_RATE_BITS)
1da177e4
LT
1631 return 0;
1632
1633 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1634 if (stream == -1)
1635 return 0;
0ba21762 1636 if (!stream && nid != codec->afg)
1da177e4 1637 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
0ba21762 1638 if (!stream || stream == -1)
1da177e4
LT
1639 return 0;
1640
1641 if (stream & AC_SUPFMT_PCM) {
1642 switch (format & 0xf0) {
1643 case 0x00:
0ba21762 1644 if (!(val & AC_SUPPCM_BITS_8))
1da177e4
LT
1645 return 0;
1646 break;
1647 case 0x10:
0ba21762 1648 if (!(val & AC_SUPPCM_BITS_16))
1da177e4
LT
1649 return 0;
1650 break;
1651 case 0x20:
0ba21762 1652 if (!(val & AC_SUPPCM_BITS_20))
1da177e4
LT
1653 return 0;
1654 break;
1655 case 0x30:
0ba21762 1656 if (!(val & AC_SUPPCM_BITS_24))
1da177e4
LT
1657 return 0;
1658 break;
1659 case 0x40:
0ba21762 1660 if (!(val & AC_SUPPCM_BITS_32))
1da177e4
LT
1661 return 0;
1662 break;
1663 default:
1664 return 0;
1665 }
1666 } else {
1667 /* FIXME: check for float32 and AC3? */
1668 }
1669
1670 return 1;
1671}
1672
1673/*
1674 * PCM stuff
1675 */
1676static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1677 struct hda_codec *codec,
c8b6bf9b 1678 struct snd_pcm_substream *substream)
1da177e4
LT
1679{
1680 return 0;
1681}
1682
1683static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1684 struct hda_codec *codec,
1685 unsigned int stream_tag,
1686 unsigned int format,
c8b6bf9b 1687 struct snd_pcm_substream *substream)
1da177e4
LT
1688{
1689 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1690 return 0;
1691}
1692
1693static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1694 struct hda_codec *codec,
c8b6bf9b 1695 struct snd_pcm_substream *substream)
1da177e4
LT
1696{
1697 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1698 return 0;
1699}
1700
0ba21762
TI
1701static int __devinit set_pcm_default_values(struct hda_codec *codec,
1702 struct hda_pcm_stream *info)
1da177e4 1703{
0ba21762
TI
1704 /* query support PCM information from the given NID */
1705 if (info->nid && (!info->rates || !info->formats)) {
1706 snd_hda_query_supported_pcm(codec, info->nid,
1707 info->rates ? NULL : &info->rates,
1708 info->formats ? NULL : &info->formats,
1709 info->maxbps ? NULL : &info->maxbps);
1da177e4
LT
1710 }
1711 if (info->ops.open == NULL)
1712 info->ops.open = hda_pcm_default_open_close;
1713 if (info->ops.close == NULL)
1714 info->ops.close = hda_pcm_default_open_close;
1715 if (info->ops.prepare == NULL) {
1716 snd_assert(info->nid, return -EINVAL);
1717 info->ops.prepare = hda_pcm_default_prepare;
1718 }
1da177e4
LT
1719 if (info->ops.cleanup == NULL) {
1720 snd_assert(info->nid, return -EINVAL);
1721 info->ops.cleanup = hda_pcm_default_cleanup;
1722 }
1723 return 0;
1724}
1725
1726/**
1727 * snd_hda_build_pcms - build PCM information
1728 * @bus: the BUS
1729 *
1730 * Create PCM information for each codec included in the bus.
1731 *
1732 * The build_pcms codec patch is requested to set up codec->num_pcms and
1733 * codec->pcm_info properly. The array is referred by the top-level driver
1734 * to create its PCM instances.
1735 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1736 * callback.
1737 *
1738 * At least, substreams, channels_min and channels_max must be filled for
1739 * each stream. substreams = 0 indicates that the stream doesn't exist.
1740 * When rates and/or formats are zero, the supported values are queried
1741 * from the given nid. The nid is used also by the default ops.prepare
1742 * and ops.cleanup callbacks.
1743 *
1744 * The driver needs to call ops.open in its open callback. Similarly,
1745 * ops.close is supposed to be called in the close callback.
1746 * ops.prepare should be called in the prepare or hw_params callback
1747 * with the proper parameters for set up.
1748 * ops.cleanup should be called in hw_free for clean up of streams.
1749 *
1750 * This function returns 0 if successfull, or a negative error code.
1751 */
756e2b01 1752int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1da177e4 1753{
0ba21762 1754 struct hda_codec *codec;
1da177e4 1755
0ba21762 1756 list_for_each_entry(codec, &bus->codec_list, list) {
1da177e4
LT
1757 unsigned int pcm, s;
1758 int err;
0ba21762 1759 if (!codec->patch_ops.build_pcms)
1da177e4
LT
1760 continue;
1761 err = codec->patch_ops.build_pcms(codec);
1762 if (err < 0)
1763 return err;
1764 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1765 for (s = 0; s < 2; s++) {
1766 struct hda_pcm_stream *info;
1767 info = &codec->pcm_info[pcm].stream[s];
0ba21762 1768 if (!info->substreams)
1da177e4
LT
1769 continue;
1770 err = set_pcm_default_values(codec, info);
1771 if (err < 0)
1772 return err;
1773 }
1774 }
1775 }
1776 return 0;
1777}
1778
1da177e4
LT
1779/**
1780 * snd_hda_check_board_config - compare the current codec with the config table
1781 * @codec: the HDA codec
f5fcc13c
TI
1782 * @num_configs: number of config enums
1783 * @models: array of model name strings
1da177e4
LT
1784 * @tbl: configuration table, terminated by null entries
1785 *
1786 * Compares the modelname or PCI subsystem id of the current codec with the
1787 * given configuration table. If a matching entry is found, returns its
1788 * config value (supposed to be 0 or positive).
1789 *
1790 * If no entries are matching, the function returns a negative value.
1791 */
756e2b01
TI
1792int __devinit snd_hda_check_board_config(struct hda_codec *codec,
1793 int num_configs, const char **models,
1794 const struct snd_pci_quirk *tbl)
1da177e4 1795{
f5fcc13c
TI
1796 if (codec->bus->modelname && models) {
1797 int i;
1798 for (i = 0; i < num_configs; i++) {
1799 if (models[i] &&
1800 !strcmp(codec->bus->modelname, models[i])) {
1801 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1802 "selected\n", models[i]);
1803 return i;
1da177e4
LT
1804 }
1805 }
1806 }
1807
f5fcc13c
TI
1808 if (!codec->bus->pci || !tbl)
1809 return -1;
1810
1811 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1812 if (!tbl)
1813 return -1;
1814 if (tbl->value >= 0 && tbl->value < num_configs) {
1815#ifdef CONFIG_SND_DEBUG_DETECT
1816 char tmp[10];
1817 const char *model = NULL;
1818 if (models)
1819 model = models[tbl->value];
1820 if (!model) {
1821 sprintf(tmp, "#%d", tbl->value);
1822 model = tmp;
1da177e4 1823 }
f5fcc13c
TI
1824 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1825 "for config %x:%x (%s)\n",
1826 model, tbl->subvendor, tbl->subdevice,
1827 (tbl->name ? tbl->name : "Unknown device"));
1828#endif
1829 return tbl->value;
1da177e4
LT
1830 }
1831 return -1;
1832}
1833
1834/**
1835 * snd_hda_add_new_ctls - create controls from the array
1836 * @codec: the HDA codec
c8b6bf9b 1837 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
1838 *
1839 * This helper function creates and add new controls in the given array.
1840 * The array must be terminated with an empty entry as terminator.
1841 *
1842 * Returns 0 if successful, or a negative error code.
1843 */
756e2b01
TI
1844int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
1845 struct snd_kcontrol_new *knew)
1da177e4
LT
1846{
1847 int err;
1848
1849 for (; knew->name; knew++) {
54d17403
TI
1850 struct snd_kcontrol *kctl;
1851 kctl = snd_ctl_new1(knew, codec);
0ba21762 1852 if (!kctl)
54d17403
TI
1853 return -ENOMEM;
1854 err = snd_ctl_add(codec->bus->card, kctl);
1855 if (err < 0) {
0ba21762 1856 if (!codec->addr)
54d17403
TI
1857 return err;
1858 kctl = snd_ctl_new1(knew, codec);
0ba21762 1859 if (!kctl)
54d17403
TI
1860 return -ENOMEM;
1861 kctl->id.device = codec->addr;
0ba21762
TI
1862 err = snd_ctl_add(codec->bus->card, kctl);
1863 if (err < 0)
54d17403
TI
1864 return err;
1865 }
1da177e4
LT
1866 }
1867 return 0;
1868}
1869
1870
c8b6bf9b 1871/*
d2a6d7dc
TI
1872 * Channel mode helper
1873 */
0ba21762
TI
1874int snd_hda_ch_mode_info(struct hda_codec *codec,
1875 struct snd_ctl_elem_info *uinfo,
1876 const struct hda_channel_mode *chmode,
1877 int num_chmodes)
d2a6d7dc
TI
1878{
1879 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1880 uinfo->count = 1;
1881 uinfo->value.enumerated.items = num_chmodes;
1882 if (uinfo->value.enumerated.item >= num_chmodes)
1883 uinfo->value.enumerated.item = num_chmodes - 1;
1884 sprintf(uinfo->value.enumerated.name, "%dch",
1885 chmode[uinfo->value.enumerated.item].channels);
1886 return 0;
1887}
1888
0ba21762
TI
1889int snd_hda_ch_mode_get(struct hda_codec *codec,
1890 struct snd_ctl_elem_value *ucontrol,
1891 const struct hda_channel_mode *chmode,
1892 int num_chmodes,
d2a6d7dc
TI
1893 int max_channels)
1894{
1895 int i;
1896
1897 for (i = 0; i < num_chmodes; i++) {
1898 if (max_channels == chmode[i].channels) {
1899 ucontrol->value.enumerated.item[0] = i;
1900 break;
1901 }
1902 }
1903 return 0;
1904}
1905
0ba21762
TI
1906int snd_hda_ch_mode_put(struct hda_codec *codec,
1907 struct snd_ctl_elem_value *ucontrol,
1908 const struct hda_channel_mode *chmode,
1909 int num_chmodes,
d2a6d7dc
TI
1910 int *max_channelsp)
1911{
1912 unsigned int mode;
1913
1914 mode = ucontrol->value.enumerated.item[0];
1915 snd_assert(mode < num_chmodes, return -EINVAL);
0ba21762 1916 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
d2a6d7dc
TI
1917 return 0;
1918 /* change the current channel setting */
1919 *max_channelsp = chmode[mode].channels;
1920 if (chmode[mode].sequence)
1921 snd_hda_sequence_write(codec, chmode[mode].sequence);
1922 return 1;
1923}
1924
1da177e4
LT
1925/*
1926 * input MUX helper
1927 */
0ba21762
TI
1928int snd_hda_input_mux_info(const struct hda_input_mux *imux,
1929 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1930{
1931 unsigned int index;
1932
1933 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1934 uinfo->count = 1;
1935 uinfo->value.enumerated.items = imux->num_items;
1936 index = uinfo->value.enumerated.item;
1937 if (index >= imux->num_items)
1938 index = imux->num_items - 1;
1939 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1940 return 0;
1941}
1942
0ba21762
TI
1943int snd_hda_input_mux_put(struct hda_codec *codec,
1944 const struct hda_input_mux *imux,
1945 struct snd_ctl_elem_value *ucontrol,
1946 hda_nid_t nid,
1da177e4
LT
1947 unsigned int *cur_val)
1948{
1949 unsigned int idx;
1950
1951 idx = ucontrol->value.enumerated.item[0];
1952 if (idx >= imux->num_items)
1953 idx = imux->num_items - 1;
0ba21762 1954 if (*cur_val == idx && !codec->in_resume)
1da177e4
LT
1955 return 0;
1956 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1957 imux->items[idx].index);
1958 *cur_val = idx;
1959 return 1;
1960}
1961
1962
1963/*
1964 * Multi-channel / digital-out PCM helper functions
1965 */
1966
6b97eb45
TI
1967/* setup SPDIF output stream */
1968static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
1969 unsigned int stream_tag, unsigned int format)
1970{
1971 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
1972 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1973 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1974 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
1975 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
1976 /* turn on again (if needed) */
1977 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1978 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1979 codec->spdif_ctls & 0xff);
1980}
1981
1da177e4
LT
1982/*
1983 * open the digital out in the exclusive mode
1984 */
0ba21762
TI
1985int snd_hda_multi_out_dig_open(struct hda_codec *codec,
1986 struct hda_multi_out *mout)
1da177e4 1987{
62932df8 1988 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
1989 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
1990 /* already opened as analog dup; reset it once */
1991 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1da177e4 1992 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 1993 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1994 return 0;
1995}
1996
6b97eb45
TI
1997int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
1998 struct hda_multi_out *mout,
1999 unsigned int stream_tag,
2000 unsigned int format,
2001 struct snd_pcm_substream *substream)
2002{
2003 mutex_lock(&codec->spdif_mutex);
2004 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2005 mutex_unlock(&codec->spdif_mutex);
2006 return 0;
2007}
2008
1da177e4
LT
2009/*
2010 * release the digital out
2011 */
0ba21762
TI
2012int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2013 struct hda_multi_out *mout)
1da177e4 2014{
62932df8 2015 mutex_lock(&codec->spdif_mutex);
1da177e4 2016 mout->dig_out_used = 0;
62932df8 2017 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2018 return 0;
2019}
2020
2021/*
2022 * set up more restrictions for analog out
2023 */
0ba21762
TI
2024int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2025 struct hda_multi_out *mout,
c8b6bf9b 2026 struct snd_pcm_substream *substream)
1da177e4
LT
2027{
2028 substream->runtime->hw.channels_max = mout->max_channels;
2029 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2030 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2031}
2032
2033/*
2034 * set up the i/o for analog out
2035 * when the digital out is available, copy the front out to digital out, too.
2036 */
0ba21762
TI
2037int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2038 struct hda_multi_out *mout,
1da177e4
LT
2039 unsigned int stream_tag,
2040 unsigned int format,
c8b6bf9b 2041 struct snd_pcm_substream *substream)
1da177e4
LT
2042{
2043 hda_nid_t *nids = mout->dac_nids;
2044 int chs = substream->runtime->channels;
2045 int i;
2046
62932df8 2047 mutex_lock(&codec->spdif_mutex);
1da177e4
LT
2048 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2049 if (chs == 2 &&
0ba21762
TI
2050 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2051 format) &&
2052 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1da177e4 2053 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
2054 setup_dig_out_stream(codec, mout->dig_out_nid,
2055 stream_tag, format);
1da177e4
LT
2056 } else {
2057 mout->dig_out_used = 0;
0ba21762
TI
2058 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2059 0, 0, 0);
1da177e4
LT
2060 }
2061 }
62932df8 2062 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2063
2064 /* front */
0ba21762
TI
2065 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2066 0, format);
35aec4e2 2067 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 2068 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
2069 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2070 0, format);
82bc955f
TI
2071 /* extra outputs copied from front */
2072 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2073 if (mout->extra_out_nid[i])
2074 snd_hda_codec_setup_stream(codec,
2075 mout->extra_out_nid[i],
2076 stream_tag, 0, format);
2077
1da177e4
LT
2078 /* surrounds */
2079 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 2080 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
2081 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2082 i * 2, format);
4b3acaf5 2083 else /* copy front */
0ba21762
TI
2084 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2085 0, format);
1da177e4
LT
2086 }
2087 return 0;
2088}
2089
2090/*
2091 * clean up the setting for analog out
2092 */
0ba21762
TI
2093int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2094 struct hda_multi_out *mout)
1da177e4
LT
2095{
2096 hda_nid_t *nids = mout->dac_nids;
2097 int i;
2098
2099 for (i = 0; i < mout->num_dacs; i++)
2100 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2101 if (mout->hp_nid)
2102 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
82bc955f
TI
2103 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2104 if (mout->extra_out_nid[i])
2105 snd_hda_codec_setup_stream(codec,
2106 mout->extra_out_nid[i],
2107 0, 0, 0);
62932df8 2108 mutex_lock(&codec->spdif_mutex);
1da177e4
LT
2109 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2110 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2111 mout->dig_out_used = 0;
2112 }
62932df8 2113 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2114 return 0;
2115}
2116
e9edcee0
TI
2117/*
2118 * Helper for automatic ping configuration
2119 */
df694daa 2120
756e2b01 2121static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
df694daa
KY
2122{
2123 for (; *list; list++)
2124 if (*list == nid)
2125 return 1;
2126 return 0;
2127}
2128
81937d3b
SL
2129
2130/*
2131 * Sort an associated group of pins according to their sequence numbers.
2132 */
2133static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2134 int num_pins)
2135{
2136 int i, j;
2137 short seq;
2138 hda_nid_t nid;
2139
2140 for (i = 0; i < num_pins; i++) {
2141 for (j = i + 1; j < num_pins; j++) {
2142 if (sequences[i] > sequences[j]) {
2143 seq = sequences[i];
2144 sequences[i] = sequences[j];
2145 sequences[j] = seq;
2146 nid = pins[i];
2147 pins[i] = pins[j];
2148 pins[j] = nid;
2149 }
2150 }
2151 }
2152}
2153
2154
82bc955f
TI
2155/*
2156 * Parse all pin widgets and store the useful pin nids to cfg
2157 *
2158 * The number of line-outs or any primary output is stored in line_outs,
2159 * and the corresponding output pins are assigned to line_out_pins[],
2160 * in the order of front, rear, CLFE, side, ...
2161 *
2162 * If more extra outputs (speaker and headphone) are found, the pins are
eb06ed8f 2163 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
82bc955f
TI
2164 * is detected, one of speaker of HP pins is assigned as the primary
2165 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2166 * if any analog output exists.
2167 *
2168 * The analog input pins are assigned to input_pins array.
2169 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2170 * respectively.
2171 */
756e2b01
TI
2172int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
2173 struct auto_pin_cfg *cfg,
2174 hda_nid_t *ignore_nids)
e9edcee0
TI
2175{
2176 hda_nid_t nid, nid_start;
81937d3b
SL
2177 int nodes;
2178 short seq, assoc_line_out, assoc_speaker;
2179 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2180 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
e9edcee0
TI
2181
2182 memset(cfg, 0, sizeof(*cfg));
2183
81937d3b
SL
2184 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2185 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2186 assoc_line_out = assoc_speaker = 0;
e9edcee0
TI
2187
2188 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2189 for (nid = nid_start; nid < nodes + nid_start; nid++) {
54d17403 2190 unsigned int wid_caps = get_wcaps(codec, nid);
0ba21762
TI
2191 unsigned int wid_type =
2192 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
e9edcee0
TI
2193 unsigned int def_conf;
2194 short assoc, loc;
2195
2196 /* read all default configuration for pin complex */
2197 if (wid_type != AC_WID_PIN)
2198 continue;
df694daa
KY
2199 /* ignore the given nids (e.g. pc-beep returns error) */
2200 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2201 continue;
2202
0ba21762
TI
2203 def_conf = snd_hda_codec_read(codec, nid, 0,
2204 AC_VERB_GET_CONFIG_DEFAULT, 0);
e9edcee0
TI
2205 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2206 continue;
2207 loc = get_defcfg_location(def_conf);
2208 switch (get_defcfg_device(def_conf)) {
2209 case AC_JACK_LINE_OUT:
e9edcee0
TI
2210 seq = get_defcfg_sequence(def_conf);
2211 assoc = get_defcfg_association(def_conf);
0ba21762 2212 if (!assoc)
e9edcee0 2213 continue;
0ba21762 2214 if (!assoc_line_out)
e9edcee0
TI
2215 assoc_line_out = assoc;
2216 else if (assoc_line_out != assoc)
2217 continue;
2218 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2219 continue;
2220 cfg->line_out_pins[cfg->line_outs] = nid;
81937d3b 2221 sequences_line_out[cfg->line_outs] = seq;
e9edcee0
TI
2222 cfg->line_outs++;
2223 break;
8d88bc3d 2224 case AC_JACK_SPEAKER:
81937d3b
SL
2225 seq = get_defcfg_sequence(def_conf);
2226 assoc = get_defcfg_association(def_conf);
2227 if (! assoc)
2228 continue;
2229 if (! assoc_speaker)
2230 assoc_speaker = assoc;
2231 else if (assoc_speaker != assoc)
2232 continue;
82bc955f
TI
2233 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2234 continue;
2235 cfg->speaker_pins[cfg->speaker_outs] = nid;
81937d3b 2236 sequences_speaker[cfg->speaker_outs] = seq;
82bc955f 2237 cfg->speaker_outs++;
8d88bc3d 2238 break;
e9edcee0 2239 case AC_JACK_HP_OUT:
eb06ed8f
TI
2240 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2241 continue;
2242 cfg->hp_pins[cfg->hp_outs] = nid;
2243 cfg->hp_outs++;
e9edcee0 2244 break;
314634bc
TI
2245 case AC_JACK_MIC_IN: {
2246 int preferred, alt;
2247 if (loc == AC_JACK_LOC_FRONT) {
2248 preferred = AUTO_PIN_FRONT_MIC;
2249 alt = AUTO_PIN_MIC;
2250 } else {
2251 preferred = AUTO_PIN_MIC;
2252 alt = AUTO_PIN_FRONT_MIC;
2253 }
2254 if (!cfg->input_pins[preferred])
2255 cfg->input_pins[preferred] = nid;
2256 else if (!cfg->input_pins[alt])
2257 cfg->input_pins[alt] = nid;
e9edcee0 2258 break;
314634bc 2259 }
e9edcee0
TI
2260 case AC_JACK_LINE_IN:
2261 if (loc == AC_JACK_LOC_FRONT)
2262 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2263 else
2264 cfg->input_pins[AUTO_PIN_LINE] = nid;
2265 break;
2266 case AC_JACK_CD:
2267 cfg->input_pins[AUTO_PIN_CD] = nid;
2268 break;
2269 case AC_JACK_AUX:
2270 cfg->input_pins[AUTO_PIN_AUX] = nid;
2271 break;
2272 case AC_JACK_SPDIF_OUT:
2273 cfg->dig_out_pin = nid;
2274 break;
2275 case AC_JACK_SPDIF_IN:
2276 cfg->dig_in_pin = nid;
2277 break;
2278 }
2279 }
2280
2281 /* sort by sequence */
81937d3b
SL
2282 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2283 cfg->line_outs);
2284 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2285 cfg->speaker_outs);
2286
2287 /*
2288 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2289 * as a primary output
2290 */
2291 if (!cfg->line_outs) {
2292 if (cfg->speaker_outs) {
2293 cfg->line_outs = cfg->speaker_outs;
2294 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2295 sizeof(cfg->speaker_pins));
2296 cfg->speaker_outs = 0;
2297 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2298 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2299 } else if (cfg->hp_outs) {
2300 cfg->line_outs = cfg->hp_outs;
2301 memcpy(cfg->line_out_pins, cfg->hp_pins,
2302 sizeof(cfg->hp_pins));
2303 cfg->hp_outs = 0;
2304 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2305 cfg->line_out_type = AUTO_PIN_HP_OUT;
2306 }
2307 }
e9edcee0 2308
cb8e2f83
TI
2309 /* Reorder the surround channels
2310 * ALSA sequence is front/surr/clfe/side
2311 * HDA sequence is:
2312 * 4-ch: front/surr => OK as it is
2313 * 6-ch: front/clfe/surr
9422db40 2314 * 8-ch: front/clfe/rear/side|fc
cb8e2f83
TI
2315 */
2316 switch (cfg->line_outs) {
2317 case 3:
cb8e2f83
TI
2318 case 4:
2319 nid = cfg->line_out_pins[1];
9422db40 2320 cfg->line_out_pins[1] = cfg->line_out_pins[2];
cb8e2f83
TI
2321 cfg->line_out_pins[2] = nid;
2322 break;
e9edcee0
TI
2323 }
2324
82bc955f
TI
2325 /*
2326 * debug prints of the parsed results
2327 */
2328 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2329 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2330 cfg->line_out_pins[2], cfg->line_out_pins[3],
2331 cfg->line_out_pins[4]);
2332 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2333 cfg->speaker_outs, cfg->speaker_pins[0],
2334 cfg->speaker_pins[1], cfg->speaker_pins[2],
2335 cfg->speaker_pins[3], cfg->speaker_pins[4]);
eb06ed8f
TI
2336 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2337 cfg->hp_outs, cfg->hp_pins[0],
2338 cfg->hp_pins[1], cfg->hp_pins[2],
2339 cfg->hp_pins[3], cfg->hp_pins[4]);
82bc955f
TI
2340 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2341 " cd=0x%x, aux=0x%x\n",
2342 cfg->input_pins[AUTO_PIN_MIC],
2343 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2344 cfg->input_pins[AUTO_PIN_LINE],
2345 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2346 cfg->input_pins[AUTO_PIN_CD],
2347 cfg->input_pins[AUTO_PIN_AUX]);
2348
e9edcee0
TI
2349 return 0;
2350}
2351
4a471b7d
TI
2352/* labels for input pins */
2353const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2354 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2355};
2356
2357
1da177e4
LT
2358#ifdef CONFIG_PM
2359/*
2360 * power management
2361 */
2362
2363/**
2364 * snd_hda_suspend - suspend the codecs
2365 * @bus: the HDA bus
2366 * @state: suspsend state
2367 *
2368 * Returns 0 if successful.
2369 */
2370int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2371{
0ba21762 2372 struct hda_codec *codec;
1da177e4
LT
2373
2374 /* FIXME: should handle power widget capabilities */
0ba21762 2375 list_for_each_entry(codec, &bus->codec_list, list) {
1da177e4
LT
2376 if (codec->patch_ops.suspend)
2377 codec->patch_ops.suspend(codec, state);
54d17403
TI
2378 hda_set_power_state(codec,
2379 codec->afg ? codec->afg : codec->mfg,
2380 AC_PWRST_D3);
1da177e4
LT
2381 }
2382 return 0;
2383}
2384
2385/**
2386 * snd_hda_resume - resume the codecs
2387 * @bus: the HDA bus
2388 * @state: resume state
2389 *
2390 * Returns 0 if successful.
2391 */
2392int snd_hda_resume(struct hda_bus *bus)
2393{
0ba21762 2394 struct hda_codec *codec;
1da177e4 2395
0ba21762 2396 list_for_each_entry(codec, &bus->codec_list, list) {
54d17403
TI
2397 hda_set_power_state(codec,
2398 codec->afg ? codec->afg : codec->mfg,
2399 AC_PWRST_D0);
1da177e4
LT
2400 if (codec->patch_ops.resume)
2401 codec->patch_ops.resume(codec);
2402 }
2403 return 0;
2404}
2405
2406/**
2407 * snd_hda_resume_ctls - resume controls in the new control list
2408 * @codec: the HDA codec
c8b6bf9b 2409 * @knew: the array of struct snd_kcontrol_new
1da177e4 2410 *
c8b6bf9b 2411 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
1da177e4
LT
2412 * originally for snd_hda_add_new_ctls().
2413 * The array must be terminated with an empty entry as terminator.
2414 */
c8b6bf9b 2415int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
1da177e4 2416{
c8b6bf9b 2417 struct snd_ctl_elem_value *val;
1da177e4
LT
2418
2419 val = kmalloc(sizeof(*val), GFP_KERNEL);
0ba21762 2420 if (!val)
1da177e4
LT
2421 return -ENOMEM;
2422 codec->in_resume = 1;
2423 for (; knew->name; knew++) {
2424 int i, count;
2425 count = knew->count ? knew->count : 1;
2426 for (i = 0; i < count; i++) {
2427 memset(val, 0, sizeof(*val));
2428 val->id.iface = knew->iface;
2429 val->id.device = knew->device;
2430 val->id.subdevice = knew->subdevice;
2431 strcpy(val->id.name, knew->name);
2432 val->id.index = knew->index ? knew->index : i;
2433 /* Assume that get callback reads only from cache,
2434 * not accessing to the real hardware
2435 */
2436 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2437 continue;
2438 snd_ctl_elem_write(codec->bus->card, NULL, val);
2439 }
2440 }
2441 codec->in_resume = 0;
2442 kfree(val);
2443 return 0;
2444}
2445
2446/**
2447 * snd_hda_resume_spdif_out - resume the digital out
2448 * @codec: the HDA codec
2449 */
2450int snd_hda_resume_spdif_out(struct hda_codec *codec)
2451{
2452 return snd_hda_resume_ctls(codec, dig_mixes);
2453}
2454
2455/**
2456 * snd_hda_resume_spdif_in - resume the digital in
2457 * @codec: the HDA codec
2458 */
2459int snd_hda_resume_spdif_in(struct hda_codec *codec)
2460{
2461 return snd_hda_resume_ctls(codec, dig_in_ctls);
2462}
2463#endif