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