aboutsummaryrefslogtreecommitdiffstats
path: root/sound/pci/hda/hda_codec.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /sound/pci/hda/hda_codec.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'sound/pci/hda/hda_codec.c')
-rw-r--r--sound/pci/hda/hda_codec.c1856
1 files changed, 1856 insertions, 0 deletions
diff --git a/sound/pci/hda/hda_codec.c b/sound/pci/hda/hda_codec.c
new file mode 100644
index 000000000000..9ed117ac0c09
--- /dev/null
+++ b/sound/pci/hda/hda_codec.c
@@ -0,0 +1,1856 @@
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 <sound/core.h>
29#include "hda_codec.h"
30#include <sound/asoundef.h>
31#include <sound/initval.h>
32#include "hda_local.h"
33
34
35MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
37MODULE_LICENSE("GPL");
38
39
40/*
41 * vendor / preset table
42 */
43
44struct hda_vendor_id {
45 unsigned int id;
46 const char *name;
47};
48
49/* codec vendor labels */
50static struct hda_vendor_id hda_vendor_ids[] = {
51 { 0x10ec, "Realtek" },
52 { 0x13f6, "C-Media" },
53 { 0x434d, "C-Media" },
54 {} /* terminator */
55};
56
57/* codec presets */
58#include "hda_patch.h"
59
60
61/**
62 * snd_hda_codec_read - send a command and get the response
63 * @codec: the HDA codec
64 * @nid: NID to send the command
65 * @direct: direct flag
66 * @verb: the verb to send
67 * @parm: the parameter for the verb
68 *
69 * Send a single command and read the corresponding response.
70 *
71 * Returns the obtained response value, or -1 for an error.
72 */
73unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
74 unsigned int verb, unsigned int parm)
75{
76 unsigned int res;
77 down(&codec->bus->cmd_mutex);
78 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
79 res = codec->bus->ops.get_response(codec);
80 else
81 res = (unsigned int)-1;
82 up(&codec->bus->cmd_mutex);
83 return res;
84}
85
86/**
87 * snd_hda_codec_write - send a single command without waiting for response
88 * @codec: the HDA codec
89 * @nid: NID to send the command
90 * @direct: direct flag
91 * @verb: the verb to send
92 * @parm: the parameter for the verb
93 *
94 * Send a single command without waiting for response.
95 *
96 * Returns 0 if successful, or a negative error code.
97 */
98int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
99 unsigned int verb, unsigned int parm)
100{
101 int err;
102 down(&codec->bus->cmd_mutex);
103 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
104 up(&codec->bus->cmd_mutex);
105 return err;
106}
107
108/**
109 * snd_hda_sequence_write - sequence writes
110 * @codec: the HDA codec
111 * @seq: VERB array to send
112 *
113 * Send the commands sequentially from the given array.
114 * The array must be terminated with NID=0.
115 */
116void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
117{
118 for (; seq->nid; seq++)
119 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
120}
121
122/**
123 * snd_hda_get_sub_nodes - get the range of sub nodes
124 * @codec: the HDA codec
125 * @nid: NID to parse
126 * @start_id: the pointer to store the start NID
127 *
128 * Parse the NID and store the start NID of its sub-nodes.
129 * Returns the number of sub-nodes.
130 */
131int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
132{
133 unsigned int parm;
134
135 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
136 *start_id = (parm >> 16) & 0x7fff;
137 return (int)(parm & 0x7fff);
138}
139
140/**
141 * snd_hda_get_connections - get connection list
142 * @codec: the HDA codec
143 * @nid: NID to parse
144 * @conn_list: connection list array
145 * @max_conns: max. number of connections to store
146 *
147 * Parses the connection list of the given widget and stores the list
148 * of NIDs.
149 *
150 * Returns the number of connections, or a negative error code.
151 */
152int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
153 hda_nid_t *conn_list, int max_conns)
154{
155 unsigned int parm;
156 int i, j, conn_len, num_tupples, conns;
157 unsigned int shift, num_elems, mask;
158
159 snd_assert(conn_list && max_conns > 0, return -EINVAL);
160
161 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
162 if (parm & AC_CLIST_LONG) {
163 /* long form */
164 shift = 16;
165 num_elems = 2;
166 } else {
167 /* short form */
168 shift = 8;
169 num_elems = 4;
170 }
171 conn_len = parm & AC_CLIST_LENGTH;
172 num_tupples = num_elems / 2;
173 mask = (1 << (shift-1)) - 1;
174
175 if (! conn_len)
176 return 0; /* no connection */
177
178 if (conn_len == 1) {
179 /* single connection */
180 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
181 conn_list[0] = parm & mask;
182 return 1;
183 }
184
185 /* multi connection */
186 conns = 0;
187 for (i = 0; i < conn_len; i += num_elems) {
188 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, i);
189 for (j = 0; j < num_tupples; j++) {
190 int range_val;
191 hda_nid_t val1, val2, n;
192 range_val = parm & (1 << (shift-1)); /* ranges */
193 val1 = parm & mask;
194 parm >>= shift;
195 val2 = parm & mask;
196 parm >>= shift;
197 if (range_val) {
198 /* ranges between val1 and val2 */
199 if (val1 > val2) {
200 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
201 continue;
202 }
203 for (n = val1; n <= val2; n++) {
204 if (conns >= max_conns)
205 return -EINVAL;
206 conn_list[conns++] = n;
207 }
208 } else {
209 if (! val1)
210 break;
211 if (conns >= max_conns)
212 return -EINVAL;
213 conn_list[conns++] = val1;
214 if (! val2)
215 break;
216 if (conns >= max_conns)
217 return -EINVAL;
218 conn_list[conns++] = val2;
219 }
220 }
221 }
222 return conns;
223}
224
225
226/**
227 * snd_hda_queue_unsol_event - add an unsolicited event to queue
228 * @bus: the BUS
229 * @res: unsolicited event (lower 32bit of RIRB entry)
230 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
231 *
232 * Adds the given event to the queue. The events are processed in
233 * the workqueue asynchronously. Call this function in the interrupt
234 * hanlder when RIRB receives an unsolicited event.
235 *
236 * Returns 0 if successful, or a negative error code.
237 */
238int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
239{
240 struct hda_bus_unsolicited *unsol;
241 unsigned int wp;
242
243 if ((unsol = bus->unsol) == NULL)
244 return 0;
245
246 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
247 unsol->wp = wp;
248
249 wp <<= 1;
250 unsol->queue[wp] = res;
251 unsol->queue[wp + 1] = res_ex;
252
253 queue_work(unsol->workq, &unsol->work);
254
255 return 0;
256}
257
258/*
259 * process queueud unsolicited events
260 */
261static void process_unsol_events(void *data)
262{
263 struct hda_bus *bus = data;
264 struct hda_bus_unsolicited *unsol = bus->unsol;
265 struct hda_codec *codec;
266 unsigned int rp, caddr, res;
267
268 while (unsol->rp != unsol->wp) {
269 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
270 unsol->rp = rp;
271 rp <<= 1;
272 res = unsol->queue[rp];
273 caddr = unsol->queue[rp + 1];
274 if (! (caddr & (1 << 4))) /* no unsolicited event? */
275 continue;
276 codec = bus->caddr_tbl[caddr & 0x0f];
277 if (codec && codec->patch_ops.unsol_event)
278 codec->patch_ops.unsol_event(codec, res);
279 }
280}
281
282/*
283 * initialize unsolicited queue
284 */
285static int init_unsol_queue(struct hda_bus *bus)
286{
287 struct hda_bus_unsolicited *unsol;
288
289 unsol = kcalloc(1, sizeof(*unsol), GFP_KERNEL);
290 if (! unsol) {
291 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
292 return -ENOMEM;
293 }
294 unsol->workq = create_workqueue("hda_codec");
295 if (! unsol->workq) {
296 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
297 kfree(unsol);
298 return -ENOMEM;
299 }
300 INIT_WORK(&unsol->work, process_unsol_events, bus);
301 bus->unsol = unsol;
302 return 0;
303}
304
305/*
306 * destructor
307 */
308static void snd_hda_codec_free(struct hda_codec *codec);
309
310static int snd_hda_bus_free(struct hda_bus *bus)
311{
312 struct list_head *p, *n;
313
314 if (! bus)
315 return 0;
316 if (bus->unsol) {
317 destroy_workqueue(bus->unsol->workq);
318 kfree(bus->unsol);
319 }
320 list_for_each_safe(p, n, &bus->codec_list) {
321 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
322 snd_hda_codec_free(codec);
323 }
324 if (bus->ops.private_free)
325 bus->ops.private_free(bus);
326 kfree(bus);
327 return 0;
328}
329
330static int snd_hda_bus_dev_free(snd_device_t *device)
331{
332 struct hda_bus *bus = device->device_data;
333 return snd_hda_bus_free(bus);
334}
335
336/**
337 * snd_hda_bus_new - create a HDA bus
338 * @card: the card entry
339 * @temp: the template for hda_bus information
340 * @busp: the pointer to store the created bus instance
341 *
342 * Returns 0 if successful, or a negative error code.
343 */
344int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
345 struct hda_bus **busp)
346{
347 struct hda_bus *bus;
348 int err;
349 static snd_device_ops_t dev_ops = {
350 .dev_free = snd_hda_bus_dev_free,
351 };
352
353 snd_assert(temp, return -EINVAL);
354 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
355
356 if (busp)
357 *busp = NULL;
358
359 bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
360 if (bus == NULL) {
361 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
362 return -ENOMEM;
363 }
364
365 bus->card = card;
366 bus->private_data = temp->private_data;
367 bus->pci = temp->pci;
368 bus->modelname = temp->modelname;
369 bus->ops = temp->ops;
370
371 init_MUTEX(&bus->cmd_mutex);
372 INIT_LIST_HEAD(&bus->codec_list);
373
374 init_unsol_queue(bus);
375
376 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
377 snd_hda_bus_free(bus);
378 return err;
379 }
380 if (busp)
381 *busp = bus;
382 return 0;
383}
384
385
386/*
387 * find a matching codec preset
388 */
389static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
390{
391 const struct hda_codec_preset **tbl, *preset;
392
393 for (tbl = hda_preset_tables; *tbl; tbl++) {
394 for (preset = *tbl; preset->id; preset++) {
395 u32 mask = preset->mask;
396 if (! mask)
397 mask = ~0;
398 if (preset->id == (codec->vendor_id & mask))
399 return preset;
400 }
401 }
402 return NULL;
403}
404
405/*
406 * snd_hda_get_codec_name - store the codec name
407 */
408void snd_hda_get_codec_name(struct hda_codec *codec,
409 char *name, int namelen)
410{
411 const struct hda_vendor_id *c;
412 const char *vendor = NULL;
413 u16 vendor_id = codec->vendor_id >> 16;
414 char tmp[16];
415
416 for (c = hda_vendor_ids; c->id; c++) {
417 if (c->id == vendor_id) {
418 vendor = c->name;
419 break;
420 }
421 }
422 if (! vendor) {
423 sprintf(tmp, "Generic %04x", vendor_id);
424 vendor = tmp;
425 }
426 if (codec->preset && codec->preset->name)
427 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
428 else
429 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
430}
431
432/*
433 * look for an AFG node
434 *
435 * return 0 if not found
436 */
437static int look_for_afg_node(struct hda_codec *codec)
438{
439 int i, total_nodes;
440 hda_nid_t nid;
441
442 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
443 for (i = 0; i < total_nodes; i++, nid++) {
444 if ((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff) ==
445 AC_GRP_AUDIO_FUNCTION)
446 return nid;
447 }
448 return 0;
449}
450
451/*
452 * codec destructor
453 */
454static void snd_hda_codec_free(struct hda_codec *codec)
455{
456 if (! codec)
457 return;
458 list_del(&codec->list);
459 codec->bus->caddr_tbl[codec->addr] = NULL;
460 if (codec->patch_ops.free)
461 codec->patch_ops.free(codec);
462 kfree(codec);
463}
464
465static void init_amp_hash(struct hda_codec *codec);
466
467/**
468 * snd_hda_codec_new - create a HDA codec
469 * @bus: the bus to assign
470 * @codec_addr: the codec address
471 * @codecp: the pointer to store the generated codec
472 *
473 * Returns 0 if successful, or a negative error code.
474 */
475int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
476 struct hda_codec **codecp)
477{
478 struct hda_codec *codec;
479 char component[13];
480 int err;
481
482 snd_assert(bus, return -EINVAL);
483 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
484
485 if (bus->caddr_tbl[codec_addr]) {
486 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
487 return -EBUSY;
488 }
489
490 codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
491 if (codec == NULL) {
492 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
493 return -ENOMEM;
494 }
495
496 codec->bus = bus;
497 codec->addr = codec_addr;
498 init_MUTEX(&codec->spdif_mutex);
499 init_amp_hash(codec);
500
501 list_add_tail(&codec->list, &bus->codec_list);
502 bus->caddr_tbl[codec_addr] = codec;
503
504 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
505 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
506 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
507
508 /* FIXME: support for multiple AFGs? */
509 codec->afg = look_for_afg_node(codec);
510 if (! codec->afg) {
511 snd_printk(KERN_ERR "hda_codec: no AFG node found\n");
512 snd_hda_codec_free(codec);
513 return -ENODEV;
514 }
515
516 codec->preset = find_codec_preset(codec);
517 if (! *bus->card->mixername)
518 snd_hda_get_codec_name(codec, bus->card->mixername,
519 sizeof(bus->card->mixername));
520
521 if (codec->preset && codec->preset->patch)
522 err = codec->preset->patch(codec);
523 else
524 err = snd_hda_parse_generic_codec(codec);
525 if (err < 0) {
526 snd_hda_codec_free(codec);
527 return err;
528 }
529
530 snd_hda_codec_proc_new(codec);
531
532 sprintf(component, "HDA:%08x", codec->vendor_id);
533 snd_component_add(codec->bus->card, component);
534
535 if (codecp)
536 *codecp = codec;
537 return 0;
538}
539
540/**
541 * snd_hda_codec_setup_stream - set up the codec for streaming
542 * @codec: the CODEC to set up
543 * @nid: the NID to set up
544 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
545 * @channel_id: channel id to pass, zero based.
546 * @format: stream format.
547 */
548void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
549 int channel_id, int format)
550{
551 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
552 nid, stream_tag, channel_id, format);
553 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
554 (stream_tag << 4) | channel_id);
555 msleep(1);
556 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
557}
558
559
560/*
561 * amp access functions
562 */
563
564#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + (idx) * 32 + (dir) * 64)
565#define INFO_AMP_CAPS (1<<0)
566#define INFO_AMP_VOL (1<<1)
567
568/* initialize the hash table */
569static void init_amp_hash(struct hda_codec *codec)
570{
571 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
572 codec->num_amp_entries = 0;
573}
574
575/* query the hash. allocate an entry if not found. */
576static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
577{
578 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
579 u16 cur = codec->amp_hash[idx];
580 struct hda_amp_info *info;
581
582 while (cur != 0xffff) {
583 info = &codec->amp_info[cur];
584 if (info->key == key)
585 return info;
586 cur = info->next;
587 }
588
589 /* add a new hash entry */
590 if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
591 snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
592 return NULL;
593 }
594 cur = codec->num_amp_entries++;
595 info = &codec->amp_info[cur];
596 info->key = key;
597 info->status = 0; /* not initialized yet */
598 info->next = codec->amp_hash[idx];
599 codec->amp_hash[idx] = cur;
600
601 return info;
602}
603
604/*
605 * query AMP capabilities for the given widget and direction
606 */
607static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
608{
609 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
610
611 if (! info)
612 return 0;
613 if (! (info->status & INFO_AMP_CAPS)) {
614 if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
615 nid = codec->afg;
616 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
617 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
618 info->status |= INFO_AMP_CAPS;
619 }
620 return info->amp_caps;
621}
622
623/*
624 * read the current volume to info
625 * if the cache exists, read from the cache.
626 */
627static void get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
628 hda_nid_t nid, int ch, int direction, int index)
629{
630 u32 val, parm;
631
632 if (info->status & (INFO_AMP_VOL << ch))
633 return;
634
635 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
636 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
637 parm |= index;
638 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
639 info->vol[ch] = val & 0xff;
640 info->status |= INFO_AMP_VOL << ch;
641}
642
643/*
644 * write the current volume in info to the h/w
645 */
646static void put_vol_mute(struct hda_codec *codec,
647 hda_nid_t nid, int ch, int direction, int index, int val)
648{
649 u32 parm;
650
651 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
652 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
653 parm |= index << AC_AMP_SET_INDEX_SHIFT;
654 parm |= val;
655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
656}
657
658/*
659 * read/write AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
660 */
661int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
662{
663 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
664 if (! info)
665 return 0;
666 get_vol_mute(codec, info, nid, ch, direction, index);
667 return info->vol[ch];
668}
669
670int snd_hda_codec_amp_write(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int val)
671{
672 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
673 if (! info)
674 return 0;
675 get_vol_mute(codec, info, nid, ch, direction, idx);
676 if (info->vol[ch] == val && ! codec->in_resume)
677 return 0;
678 put_vol_mute(codec, nid, ch, direction, idx, val);
679 info->vol[ch] = val;
680 return 1;
681}
682
683
684/*
685 * AMP control callbacks
686 */
687/* retrieve parameters from private_value */
688#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
689#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
690#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
691#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
692
693/* volume */
694int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
695{
696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
697 u16 nid = get_amp_nid(kcontrol);
698 u8 chs = get_amp_channels(kcontrol);
699 int dir = get_amp_direction(kcontrol);
700 u32 caps;
701
702 caps = query_amp_caps(codec, nid, dir);
703 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
704 if (! caps) {
705 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
706 return -EINVAL;
707 }
708 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
709 uinfo->count = chs == 3 ? 2 : 1;
710 uinfo->value.integer.min = 0;
711 uinfo->value.integer.max = caps;
712 return 0;
713}
714
715int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
716{
717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
718 hda_nid_t nid = get_amp_nid(kcontrol);
719 int chs = get_amp_channels(kcontrol);
720 int dir = get_amp_direction(kcontrol);
721 int idx = get_amp_index(kcontrol);
722 long *valp = ucontrol->value.integer.value;
723
724 if (chs & 1)
725 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
726 if (chs & 2)
727 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
728 return 0;
729}
730
731int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
732{
733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
734 hda_nid_t nid = get_amp_nid(kcontrol);
735 int chs = get_amp_channels(kcontrol);
736 int dir = get_amp_direction(kcontrol);
737 int idx = get_amp_index(kcontrol);
738 int val;
739 long *valp = ucontrol->value.integer.value;
740 int change = 0;
741
742 if (chs & 1) {
743 val = *valp & 0x7f;
744 val |= snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80;
745 change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
746 valp++;
747 }
748 if (chs & 2) {
749 val = *valp & 0x7f;
750 val |= snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80;
751 change |= snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
752 }
753 return change;
754}
755
756/* switch */
757int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
758{
759 int chs = get_amp_channels(kcontrol);
760
761 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
762 uinfo->count = chs == 3 ? 2 : 1;
763 uinfo->value.integer.min = 0;
764 uinfo->value.integer.max = 1;
765 return 0;
766}
767
768int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
769{
770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
771 hda_nid_t nid = get_amp_nid(kcontrol);
772 int chs = get_amp_channels(kcontrol);
773 int dir = get_amp_direction(kcontrol);
774 int idx = get_amp_index(kcontrol);
775 long *valp = ucontrol->value.integer.value;
776
777 if (chs & 1)
778 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
779 if (chs & 2)
780 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
781 return 0;
782}
783
784int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
785{
786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
787 hda_nid_t nid = get_amp_nid(kcontrol);
788 int chs = get_amp_channels(kcontrol);
789 int dir = get_amp_direction(kcontrol);
790 int idx = get_amp_index(kcontrol);
791 int val;
792 long *valp = ucontrol->value.integer.value;
793 int change = 0;
794
795 if (chs & 1) {
796 val = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
797 val |= *valp ? 0 : 0x80;
798 change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
799 valp++;
800 }
801 if (chs & 2) {
802 val = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
803 val |= *valp ? 0 : 0x80;
804 change = snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
805 }
806 return change;
807}
808
809/*
810 * SPDIF out controls
811 */
812
813static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
814{
815 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
816 uinfo->count = 1;
817 return 0;
818}
819
820static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
821{
822 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
823 IEC958_AES0_NONAUDIO |
824 IEC958_AES0_CON_EMPHASIS_5015 |
825 IEC958_AES0_CON_NOT_COPYRIGHT;
826 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
827 IEC958_AES1_CON_ORIGINAL;
828 return 0;
829}
830
831static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
832{
833 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
834 IEC958_AES0_NONAUDIO |
835 IEC958_AES0_PRO_EMPHASIS_5015;
836 return 0;
837}
838
839static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
840{
841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
842
843 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
844 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
845 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
846 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
847
848 return 0;
849}
850
851/* convert from SPDIF status bits to HDA SPDIF bits
852 * bit 0 (DigEn) is always set zero (to be filled later)
853 */
854static unsigned short convert_from_spdif_status(unsigned int sbits)
855{
856 unsigned short val = 0;
857
858 if (sbits & IEC958_AES0_PROFESSIONAL)
859 val |= 1 << 6;
860 if (sbits & IEC958_AES0_NONAUDIO)
861 val |= 1 << 5;
862 if (sbits & IEC958_AES0_PROFESSIONAL) {
863 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
864 val |= 1 << 3;
865 } else {
866 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
867 val |= 1 << 3;
868 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
869 val |= 1 << 4;
870 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
871 val |= 1 << 7;
872 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
873 }
874 return val;
875}
876
877/* convert to SPDIF status bits from HDA SPDIF bits
878 */
879static unsigned int convert_to_spdif_status(unsigned short val)
880{
881 unsigned int sbits = 0;
882
883 if (val & (1 << 5))
884 sbits |= IEC958_AES0_NONAUDIO;
885 if (val & (1 << 6))
886 sbits |= IEC958_AES0_PROFESSIONAL;
887 if (sbits & IEC958_AES0_PROFESSIONAL) {
888 if (sbits & (1 << 3))
889 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
890 } else {
891 if (val & (1 << 3))
892 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
893 if (! (val & (1 << 4)))
894 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
895 if (val & (1 << 7))
896 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
897 sbits |= val & (0x7f << 8);
898 }
899 return sbits;
900}
901
902static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
903{
904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
905 hda_nid_t nid = kcontrol->private_value;
906 unsigned short val;
907 int change;
908
909 down(&codec->spdif_mutex);
910 codec->spdif_status = ucontrol->value.iec958.status[0] |
911 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
912 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
913 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
914 val = convert_from_spdif_status(codec->spdif_status);
915 val |= codec->spdif_ctls & 1;
916 change = codec->spdif_ctls != val;
917 codec->spdif_ctls = val;
918
919 if (change || codec->in_resume) {
920 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
921 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
922 }
923
924 up(&codec->spdif_mutex);
925 return change;
926}
927
928static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
929{
930 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
931 uinfo->count = 1;
932 uinfo->value.integer.min = 0;
933 uinfo->value.integer.max = 1;
934 return 0;
935}
936
937static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
938{
939 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
940
941 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
942 return 0;
943}
944
945static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
946{
947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
948 hda_nid_t nid = kcontrol->private_value;
949 unsigned short val;
950 int change;
951
952 down(&codec->spdif_mutex);
953 val = codec->spdif_ctls & ~1;
954 if (ucontrol->value.integer.value[0])
955 val |= 1;
956 change = codec->spdif_ctls != val;
957 if (change || codec->in_resume) {
958 codec->spdif_ctls = val;
959 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
960 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
961 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
962 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
963 }
964 up(&codec->spdif_mutex);
965 return change;
966}
967
968static snd_kcontrol_new_t dig_mixes[] = {
969 {
970 .access = SNDRV_CTL_ELEM_ACCESS_READ,
971 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
972 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
973 .info = snd_hda_spdif_mask_info,
974 .get = snd_hda_spdif_cmask_get,
975 },
976 {
977 .access = SNDRV_CTL_ELEM_ACCESS_READ,
978 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
979 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
980 .info = snd_hda_spdif_mask_info,
981 .get = snd_hda_spdif_pmask_get,
982 },
983 {
984 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
985 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
986 .info = snd_hda_spdif_mask_info,
987 .get = snd_hda_spdif_default_get,
988 .put = snd_hda_spdif_default_put,
989 },
990 {
991 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
992 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
993 .info = snd_hda_spdif_out_switch_info,
994 .get = snd_hda_spdif_out_switch_get,
995 .put = snd_hda_spdif_out_switch_put,
996 },
997 { } /* end */
998};
999
1000/**
1001 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1002 * @codec: the HDA codec
1003 * @nid: audio out widget NID
1004 *
1005 * Creates controls related with the SPDIF output.
1006 * Called from each patch supporting the SPDIF out.
1007 *
1008 * Returns 0 if successful, or a negative error code.
1009 */
1010int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1011{
1012 int err;
1013 snd_kcontrol_t *kctl;
1014 snd_kcontrol_new_t *dig_mix;
1015
1016 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1017 kctl = snd_ctl_new1(dig_mix, codec);
1018 kctl->private_value = nid;
1019 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1020 return err;
1021 }
1022 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1023 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1024 return 0;
1025}
1026
1027/*
1028 * SPDIF input
1029 */
1030
1031#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1032
1033static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1034{
1035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1036
1037 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1038 return 0;
1039}
1040
1041static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1042{
1043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1044 hda_nid_t nid = kcontrol->private_value;
1045 unsigned int val = !!ucontrol->value.integer.value[0];
1046 int change;
1047
1048 down(&codec->spdif_mutex);
1049 change = codec->spdif_in_enable != val;
1050 if (change || codec->in_resume) {
1051 codec->spdif_in_enable = val;
1052 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1053 }
1054 up(&codec->spdif_mutex);
1055 return change;
1056}
1057
1058static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1059{
1060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1061 hda_nid_t nid = kcontrol->private_value;
1062 unsigned short val;
1063 unsigned int sbits;
1064
1065 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1066 sbits = convert_to_spdif_status(val);
1067 ucontrol->value.iec958.status[0] = sbits;
1068 ucontrol->value.iec958.status[1] = sbits >> 8;
1069 ucontrol->value.iec958.status[2] = sbits >> 16;
1070 ucontrol->value.iec958.status[3] = sbits >> 24;
1071 return 0;
1072}
1073
1074static snd_kcontrol_new_t dig_in_ctls[] = {
1075 {
1076 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1077 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1078 .info = snd_hda_spdif_in_switch_info,
1079 .get = snd_hda_spdif_in_switch_get,
1080 .put = snd_hda_spdif_in_switch_put,
1081 },
1082 {
1083 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1084 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1085 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1086 .info = snd_hda_spdif_mask_info,
1087 .get = snd_hda_spdif_in_status_get,
1088 },
1089 { } /* end */
1090};
1091
1092/**
1093 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1094 * @codec: the HDA codec
1095 * @nid: audio in widget NID
1096 *
1097 * Creates controls related with the SPDIF input.
1098 * Called from each patch supporting the SPDIF in.
1099 *
1100 * Returns 0 if successful, or a negative error code.
1101 */
1102int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1103{
1104 int err;
1105 snd_kcontrol_t *kctl;
1106 snd_kcontrol_new_t *dig_mix;
1107
1108 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1109 kctl = snd_ctl_new1(dig_mix, codec);
1110 kctl->private_value = nid;
1111 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1112 return err;
1113 }
1114 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1115 return 0;
1116}
1117
1118
1119/**
1120 * snd_hda_build_controls - build mixer controls
1121 * @bus: the BUS
1122 *
1123 * Creates mixer controls for each codec included in the bus.
1124 *
1125 * Returns 0 if successful, otherwise a negative error code.
1126 */
1127int snd_hda_build_controls(struct hda_bus *bus)
1128{
1129 struct list_head *p;
1130
1131 /* build controls */
1132 list_for_each(p, &bus->codec_list) {
1133 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1134 int err;
1135 if (! codec->patch_ops.build_controls)
1136 continue;
1137 err = codec->patch_ops.build_controls(codec);
1138 if (err < 0)
1139 return err;
1140 }
1141
1142 /* initialize */
1143 list_for_each(p, &bus->codec_list) {
1144 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1145 int err;
1146 if (! codec->patch_ops.init)
1147 continue;
1148 err = codec->patch_ops.init(codec);
1149 if (err < 0)
1150 return err;
1151 }
1152 return 0;
1153}
1154
1155
1156/*
1157 * stream formats
1158 */
1159static unsigned int rate_bits[][3] = {
1160 /* rate in Hz, ALSA rate bitmask, HDA format value */
1161 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1162 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1163 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1164 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1165 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1166 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1167 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1168 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1169 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1170 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1171 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1172 { 0 }
1173};
1174
1175/**
1176 * snd_hda_calc_stream_format - calculate format bitset
1177 * @rate: the sample rate
1178 * @channels: the number of channels
1179 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1180 * @maxbps: the max. bps
1181 *
1182 * Calculate the format bitset from the given rate, channels and th PCM format.
1183 *
1184 * Return zero if invalid.
1185 */
1186unsigned int snd_hda_calc_stream_format(unsigned int rate,
1187 unsigned int channels,
1188 unsigned int format,
1189 unsigned int maxbps)
1190{
1191 int i;
1192 unsigned int val = 0;
1193
1194 for (i = 0; rate_bits[i][0]; i++)
1195 if (rate_bits[i][0] == rate) {
1196 val = rate_bits[i][2];
1197 break;
1198 }
1199 if (! rate_bits[i][0]) {
1200 snd_printdd("invalid rate %d\n", rate);
1201 return 0;
1202 }
1203
1204 if (channels == 0 || channels > 8) {
1205 snd_printdd("invalid channels %d\n", channels);
1206 return 0;
1207 }
1208 val |= channels - 1;
1209
1210 switch (snd_pcm_format_width(format)) {
1211 case 8: val |= 0x00; break;
1212 case 16: val |= 0x10; break;
1213 case 20:
1214 case 24:
1215 case 32:
1216 if (maxbps >= 32)
1217 val |= 0x40;
1218 else if (maxbps >= 24)
1219 val |= 0x30;
1220 else
1221 val |= 0x20;
1222 break;
1223 default:
1224 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1225 return 0;
1226 }
1227
1228 return val;
1229}
1230
1231/**
1232 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1233 * @codec: the HDA codec
1234 * @nid: NID to query
1235 * @ratesp: the pointer to store the detected rate bitflags
1236 * @formatsp: the pointer to store the detected formats
1237 * @bpsp: the pointer to store the detected format widths
1238 *
1239 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1240 * or @bsps argument is ignored.
1241 *
1242 * Returns 0 if successful, otherwise a negative error code.
1243 */
1244int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1245 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1246{
1247 int i;
1248 unsigned int val, streams;
1249
1250 val = 0;
1251 if (nid != codec->afg &&
1252 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1253 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1254 if (val == -1)
1255 return -EIO;
1256 }
1257 if (! val)
1258 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1259
1260 if (ratesp) {
1261 u32 rates = 0;
1262 for (i = 0; rate_bits[i][0]; i++) {
1263 if (val & (1 << i))
1264 rates |= rate_bits[i][1];
1265 }
1266 *ratesp = rates;
1267 }
1268
1269 if (formatsp || bpsp) {
1270 u64 formats = 0;
1271 unsigned int bps;
1272 unsigned int wcaps;
1273
1274 wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1275 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1276 if (streams == -1)
1277 return -EIO;
1278 if (! streams) {
1279 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1280 if (streams == -1)
1281 return -EIO;
1282 }
1283
1284 bps = 0;
1285 if (streams & AC_SUPFMT_PCM) {
1286 if (val & AC_SUPPCM_BITS_8) {
1287 formats |= SNDRV_PCM_FMTBIT_U8;
1288 bps = 8;
1289 }
1290 if (val & AC_SUPPCM_BITS_16) {
1291 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1292 bps = 16;
1293 }
1294 if (wcaps & AC_WCAP_DIGITAL) {
1295 if (val & AC_SUPPCM_BITS_32)
1296 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1297 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1298 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1299 if (val & AC_SUPPCM_BITS_24)
1300 bps = 24;
1301 else if (val & AC_SUPPCM_BITS_20)
1302 bps = 20;
1303 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1304 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1305 if (val & AC_SUPPCM_BITS_32)
1306 bps = 32;
1307 else if (val & AC_SUPPCM_BITS_20)
1308 bps = 20;
1309 else if (val & AC_SUPPCM_BITS_24)
1310 bps = 24;
1311 }
1312 }
1313 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1314 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1315 bps = 32;
1316 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1317 /* temporary hack: we have still no proper support
1318 * for the direct AC3 stream...
1319 */
1320 formats |= SNDRV_PCM_FMTBIT_U8;
1321 bps = 8;
1322 }
1323 if (formatsp)
1324 *formatsp = formats;
1325 if (bpsp)
1326 *bpsp = bps;
1327 }
1328
1329 return 0;
1330}
1331
1332/**
1333 * snd_hda_is_supported_format - check whether the given node supports the format val
1334 *
1335 * Returns 1 if supported, 0 if not.
1336 */
1337int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1338 unsigned int format)
1339{
1340 int i;
1341 unsigned int val = 0, rate, stream;
1342
1343 if (nid != codec->afg &&
1344 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1345 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1346 if (val == -1)
1347 return 0;
1348 }
1349 if (! val) {
1350 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1351 if (val == -1)
1352 return 0;
1353 }
1354
1355 rate = format & 0xff00;
1356 for (i = 0; rate_bits[i][0]; i++)
1357 if (rate_bits[i][2] == rate) {
1358 if (val & (1 << i))
1359 break;
1360 return 0;
1361 }
1362 if (! rate_bits[i][0])
1363 return 0;
1364
1365 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1366 if (stream == -1)
1367 return 0;
1368 if (! stream && nid != codec->afg)
1369 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1370 if (! stream || stream == -1)
1371 return 0;
1372
1373 if (stream & AC_SUPFMT_PCM) {
1374 switch (format & 0xf0) {
1375 case 0x00:
1376 if (! (val & AC_SUPPCM_BITS_8))
1377 return 0;
1378 break;
1379 case 0x10:
1380 if (! (val & AC_SUPPCM_BITS_16))
1381 return 0;
1382 break;
1383 case 0x20:
1384 if (! (val & AC_SUPPCM_BITS_20))
1385 return 0;
1386 break;
1387 case 0x30:
1388 if (! (val & AC_SUPPCM_BITS_24))
1389 return 0;
1390 break;
1391 case 0x40:
1392 if (! (val & AC_SUPPCM_BITS_32))
1393 return 0;
1394 break;
1395 default:
1396 return 0;
1397 }
1398 } else {
1399 /* FIXME: check for float32 and AC3? */
1400 }
1401
1402 return 1;
1403}
1404
1405/*
1406 * PCM stuff
1407 */
1408static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1409 struct hda_codec *codec,
1410 snd_pcm_substream_t *substream)
1411{
1412 return 0;
1413}
1414
1415static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1416 struct hda_codec *codec,
1417 unsigned int stream_tag,
1418 unsigned int format,
1419 snd_pcm_substream_t *substream)
1420{
1421 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1422 return 0;
1423}
1424
1425static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1426 struct hda_codec *codec,
1427 snd_pcm_substream_t *substream)
1428{
1429 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1430 return 0;
1431}
1432
1433static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1434{
1435 if (info->nid) {
1436 /* query support PCM information from the given NID */
1437 if (! info->rates || ! info->formats)
1438 snd_hda_query_supported_pcm(codec, info->nid,
1439 info->rates ? NULL : &info->rates,
1440 info->formats ? NULL : &info->formats,
1441 info->maxbps ? NULL : &info->maxbps);
1442 }
1443 if (info->ops.open == NULL)
1444 info->ops.open = hda_pcm_default_open_close;
1445 if (info->ops.close == NULL)
1446 info->ops.close = hda_pcm_default_open_close;
1447 if (info->ops.prepare == NULL) {
1448 snd_assert(info->nid, return -EINVAL);
1449 info->ops.prepare = hda_pcm_default_prepare;
1450 }
1451 if (info->ops.prepare == NULL) {
1452 snd_assert(info->nid, return -EINVAL);
1453 info->ops.prepare = hda_pcm_default_prepare;
1454 }
1455 if (info->ops.cleanup == NULL) {
1456 snd_assert(info->nid, return -EINVAL);
1457 info->ops.cleanup = hda_pcm_default_cleanup;
1458 }
1459 return 0;
1460}
1461
1462/**
1463 * snd_hda_build_pcms - build PCM information
1464 * @bus: the BUS
1465 *
1466 * Create PCM information for each codec included in the bus.
1467 *
1468 * The build_pcms codec patch is requested to set up codec->num_pcms and
1469 * codec->pcm_info properly. The array is referred by the top-level driver
1470 * to create its PCM instances.
1471 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1472 * callback.
1473 *
1474 * At least, substreams, channels_min and channels_max must be filled for
1475 * each stream. substreams = 0 indicates that the stream doesn't exist.
1476 * When rates and/or formats are zero, the supported values are queried
1477 * from the given nid. The nid is used also by the default ops.prepare
1478 * and ops.cleanup callbacks.
1479 *
1480 * The driver needs to call ops.open in its open callback. Similarly,
1481 * ops.close is supposed to be called in the close callback.
1482 * ops.prepare should be called in the prepare or hw_params callback
1483 * with the proper parameters for set up.
1484 * ops.cleanup should be called in hw_free for clean up of streams.
1485 *
1486 * This function returns 0 if successfull, or a negative error code.
1487 */
1488int snd_hda_build_pcms(struct hda_bus *bus)
1489{
1490 struct list_head *p;
1491
1492 list_for_each(p, &bus->codec_list) {
1493 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1494 unsigned int pcm, s;
1495 int err;
1496 if (! codec->patch_ops.build_pcms)
1497 continue;
1498 err = codec->patch_ops.build_pcms(codec);
1499 if (err < 0)
1500 return err;
1501 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1502 for (s = 0; s < 2; s++) {
1503 struct hda_pcm_stream *info;
1504 info = &codec->pcm_info[pcm].stream[s];
1505 if (! info->substreams)
1506 continue;
1507 err = set_pcm_default_values(codec, info);
1508 if (err < 0)
1509 return err;
1510 }
1511 }
1512 }
1513 return 0;
1514}
1515
1516
1517/**
1518 * snd_hda_check_board_config - compare the current codec with the config table
1519 * @codec: the HDA codec
1520 * @tbl: configuration table, terminated by null entries
1521 *
1522 * Compares the modelname or PCI subsystem id of the current codec with the
1523 * given configuration table. If a matching entry is found, returns its
1524 * config value (supposed to be 0 or positive).
1525 *
1526 * If no entries are matching, the function returns a negative value.
1527 */
1528int snd_hda_check_board_config(struct hda_codec *codec, struct hda_board_config *tbl)
1529{
1530 struct hda_board_config *c;
1531
1532 if (codec->bus->modelname) {
1533 for (c = tbl; c->modelname || c->pci_vendor; c++) {
1534 if (c->modelname &&
1535 ! strcmp(codec->bus->modelname, c->modelname)) {
1536 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1537 return c->config;
1538 }
1539 }
1540 }
1541
1542 if (codec->bus->pci) {
1543 u16 subsystem_vendor, subsystem_device;
1544 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1545 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1546 for (c = tbl; c->modelname || c->pci_vendor; c++) {
1547 if (c->pci_vendor == subsystem_vendor &&
1548 c->pci_device == subsystem_device)
1549 return c->config;
1550 }
1551 }
1552 return -1;
1553}
1554
1555/**
1556 * snd_hda_add_new_ctls - create controls from the array
1557 * @codec: the HDA codec
1558 * @knew: the array of snd_kcontrol_new_t
1559 *
1560 * This helper function creates and add new controls in the given array.
1561 * The array must be terminated with an empty entry as terminator.
1562 *
1563 * Returns 0 if successful, or a negative error code.
1564 */
1565int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1566{
1567 int err;
1568
1569 for (; knew->name; knew++) {
1570 err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
1571 if (err < 0)
1572 return err;
1573 }
1574 return 0;
1575}
1576
1577
1578/*
1579 * input MUX helper
1580 */
1581int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
1582{
1583 unsigned int index;
1584
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1586 uinfo->count = 1;
1587 uinfo->value.enumerated.items = imux->num_items;
1588 index = uinfo->value.enumerated.item;
1589 if (index >= imux->num_items)
1590 index = imux->num_items - 1;
1591 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1592 return 0;
1593}
1594
1595int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1596 snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
1597 unsigned int *cur_val)
1598{
1599 unsigned int idx;
1600
1601 idx = ucontrol->value.enumerated.item[0];
1602 if (idx >= imux->num_items)
1603 idx = imux->num_items - 1;
1604 if (*cur_val == idx && ! codec->in_resume)
1605 return 0;
1606 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1607 imux->items[idx].index);
1608 *cur_val = idx;
1609 return 1;
1610}
1611
1612
1613/*
1614 * Multi-channel / digital-out PCM helper functions
1615 */
1616
1617/*
1618 * open the digital out in the exclusive mode
1619 */
1620int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1621{
1622 down(&codec->spdif_mutex);
1623 if (mout->dig_out_used) {
1624 up(&codec->spdif_mutex);
1625 return -EBUSY; /* already being used */
1626 }
1627 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1628 up(&codec->spdif_mutex);
1629 return 0;
1630}
1631
1632/*
1633 * release the digital out
1634 */
1635int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1636{
1637 down(&codec->spdif_mutex);
1638 mout->dig_out_used = 0;
1639 up(&codec->spdif_mutex);
1640 return 0;
1641}
1642
1643/*
1644 * set up more restrictions for analog out
1645 */
1646int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1647 snd_pcm_substream_t *substream)
1648{
1649 substream->runtime->hw.channels_max = mout->max_channels;
1650 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1651 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1652}
1653
1654/*
1655 * set up the i/o for analog out
1656 * when the digital out is available, copy the front out to digital out, too.
1657 */
1658int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1659 unsigned int stream_tag,
1660 unsigned int format,
1661 snd_pcm_substream_t *substream)
1662{
1663 hda_nid_t *nids = mout->dac_nids;
1664 int chs = substream->runtime->channels;
1665 int i;
1666
1667 down(&codec->spdif_mutex);
1668 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1669 if (chs == 2 &&
1670 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1671 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1672 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1673 /* setup digital receiver */
1674 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1675 stream_tag, 0, format);
1676 } else {
1677 mout->dig_out_used = 0;
1678 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1679 }
1680 }
1681 up(&codec->spdif_mutex);
1682
1683 /* front */
1684 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1685 if (mout->hp_nid)
1686 /* headphone out will just decode front left/right (stereo) */
1687 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1688 /* surrounds */
1689 for (i = 1; i < mout->num_dacs; i++) {
1690 if (i == HDA_REAR && chs == 2) /* copy front to rear */
1691 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0, format);
1692 else if (chs >= (i + 1) * 2) /* independent out */
1693 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1694 format);
1695 }
1696 return 0;
1697}
1698
1699/*
1700 * clean up the setting for analog out
1701 */
1702int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1703{
1704 hda_nid_t *nids = mout->dac_nids;
1705 int i;
1706
1707 for (i = 0; i < mout->num_dacs; i++)
1708 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1709 if (mout->hp_nid)
1710 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1711 down(&codec->spdif_mutex);
1712 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1713 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1714 mout->dig_out_used = 0;
1715 }
1716 up(&codec->spdif_mutex);
1717 return 0;
1718}
1719
1720#ifdef CONFIG_PM
1721/*
1722 * power management
1723 */
1724
1725/**
1726 * snd_hda_suspend - suspend the codecs
1727 * @bus: the HDA bus
1728 * @state: suspsend state
1729 *
1730 * Returns 0 if successful.
1731 */
1732int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
1733{
1734 struct list_head *p;
1735
1736 /* FIXME: should handle power widget capabilities */
1737 list_for_each(p, &bus->codec_list) {
1738 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1739 if (codec->patch_ops.suspend)
1740 codec->patch_ops.suspend(codec, state);
1741 }
1742 return 0;
1743}
1744
1745/**
1746 * snd_hda_resume - resume the codecs
1747 * @bus: the HDA bus
1748 * @state: resume state
1749 *
1750 * Returns 0 if successful.
1751 */
1752int snd_hda_resume(struct hda_bus *bus)
1753{
1754 struct list_head *p;
1755
1756 list_for_each(p, &bus->codec_list) {
1757 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1758 if (codec->patch_ops.resume)
1759 codec->patch_ops.resume(codec);
1760 }
1761 return 0;
1762}
1763
1764/**
1765 * snd_hda_resume_ctls - resume controls in the new control list
1766 * @codec: the HDA codec
1767 * @knew: the array of snd_kcontrol_new_t
1768 *
1769 * This function resumes the mixer controls in the snd_kcontrol_new_t array,
1770 * originally for snd_hda_add_new_ctls().
1771 * The array must be terminated with an empty entry as terminator.
1772 */
1773int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1774{
1775 snd_ctl_elem_value_t *val;
1776
1777 val = kmalloc(sizeof(*val), GFP_KERNEL);
1778 if (! val)
1779 return -ENOMEM;
1780 codec->in_resume = 1;
1781 for (; knew->name; knew++) {
1782 int i, count;
1783 count = knew->count ? knew->count : 1;
1784 for (i = 0; i < count; i++) {
1785 memset(val, 0, sizeof(*val));
1786 val->id.iface = knew->iface;
1787 val->id.device = knew->device;
1788 val->id.subdevice = knew->subdevice;
1789 strcpy(val->id.name, knew->name);
1790 val->id.index = knew->index ? knew->index : i;
1791 /* Assume that get callback reads only from cache,
1792 * not accessing to the real hardware
1793 */
1794 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
1795 continue;
1796 snd_ctl_elem_write(codec->bus->card, NULL, val);
1797 }
1798 }
1799 codec->in_resume = 0;
1800 kfree(val);
1801 return 0;
1802}
1803
1804/**
1805 * snd_hda_resume_spdif_out - resume the digital out
1806 * @codec: the HDA codec
1807 */
1808int snd_hda_resume_spdif_out(struct hda_codec *codec)
1809{
1810 return snd_hda_resume_ctls(codec, dig_mixes);
1811}
1812
1813/**
1814 * snd_hda_resume_spdif_in - resume the digital in
1815 * @codec: the HDA codec
1816 */
1817int snd_hda_resume_spdif_in(struct hda_codec *codec)
1818{
1819 return snd_hda_resume_ctls(codec, dig_in_ctls);
1820}
1821#endif
1822
1823/*
1824 * symbols exported for controller modules
1825 */
1826EXPORT_SYMBOL(snd_hda_codec_read);
1827EXPORT_SYMBOL(snd_hda_codec_write);
1828EXPORT_SYMBOL(snd_hda_sequence_write);
1829EXPORT_SYMBOL(snd_hda_get_sub_nodes);
1830EXPORT_SYMBOL(snd_hda_queue_unsol_event);
1831EXPORT_SYMBOL(snd_hda_bus_new);
1832EXPORT_SYMBOL(snd_hda_codec_new);
1833EXPORT_SYMBOL(snd_hda_codec_setup_stream);
1834EXPORT_SYMBOL(snd_hda_calc_stream_format);
1835EXPORT_SYMBOL(snd_hda_build_pcms);
1836EXPORT_SYMBOL(snd_hda_build_controls);
1837#ifdef CONFIG_PM
1838EXPORT_SYMBOL(snd_hda_suspend);
1839EXPORT_SYMBOL(snd_hda_resume);
1840#endif
1841
1842/*
1843 * INIT part
1844 */
1845
1846static int __init alsa_hda_init(void)
1847{
1848 return 0;
1849}
1850
1851static void __exit alsa_hda_exit(void)
1852{
1853}
1854
1855module_init(alsa_hda_init)
1856module_exit(alsa_hda_exit)