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