blob: fc91256e42eb6d00803802eb6867a7606f52b033 [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>
Ingo Molnar62932df2006-01-16 16:34:20 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
32#include <sound/initval.h>
33#include "hda_local.h"
34
35
36MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
37MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
38MODULE_LICENSE("GPL");
39
40
41/*
42 * vendor / preset table
43 */
44
45struct hda_vendor_id {
46 unsigned int id;
47 const char *name;
48};
49
50/* codec vendor labels */
51static struct hda_vendor_id hda_vendor_ids[] = {
52 { 0x10ec, "Realtek" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020053 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 { 0x13f6, "C-Media" },
55 { 0x434d, "C-Media" },
Matt2f2f4252005-04-13 14:45:30 +020056 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 {} /* terminator */
58};
59
60/* codec presets */
61#include "hda_patch.h"
62
63
64/**
65 * snd_hda_codec_read - send a command and get the response
66 * @codec: the HDA codec
67 * @nid: NID to send the command
68 * @direct: direct flag
69 * @verb: the verb to send
70 * @parm: the parameter for the verb
71 *
72 * Send a single command and read the corresponding response.
73 *
74 * Returns the obtained response value, or -1 for an error.
75 */
76unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
77 unsigned int verb, unsigned int parm)
78{
79 unsigned int res;
Ingo Molnar62932df2006-01-16 16:34:20 +010080 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
82 res = codec->bus->ops.get_response(codec);
83 else
84 res = (unsigned int)-1;
Ingo Molnar62932df2006-01-16 16:34:20 +010085 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return res;
87}
88
89/**
90 * snd_hda_codec_write - send a single command without waiting for response
91 * @codec: the HDA codec
92 * @nid: NID to send the command
93 * @direct: direct flag
94 * @verb: the verb to send
95 * @parm: the parameter for the verb
96 *
97 * Send a single command without waiting for response.
98 *
99 * Returns 0 if successful, or a negative error code.
100 */
101int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
102 unsigned int verb, unsigned int parm)
103{
104 int err;
Ingo Molnar62932df2006-01-16 16:34:20 +0100105 mutex_lock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
Ingo Molnar62932df2006-01-16 16:34:20 +0100107 mutex_unlock(&codec->bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return err;
109}
110
111/**
112 * snd_hda_sequence_write - sequence writes
113 * @codec: the HDA codec
114 * @seq: VERB array to send
115 *
116 * Send the commands sequentially from the given array.
117 * The array must be terminated with NID=0.
118 */
119void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
120{
121 for (; seq->nid; seq++)
122 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
123}
124
125/**
126 * snd_hda_get_sub_nodes - get the range of sub nodes
127 * @codec: the HDA codec
128 * @nid: NID to parse
129 * @start_id: the pointer to store the start NID
130 *
131 * Parse the NID and store the start NID of its sub-nodes.
132 * Returns the number of sub-nodes.
133 */
134int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
135{
136 unsigned int parm;
137
138 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
139 *start_id = (parm >> 16) & 0x7fff;
140 return (int)(parm & 0x7fff);
141}
142
143/**
144 * snd_hda_get_connections - get connection list
145 * @codec: the HDA codec
146 * @nid: NID to parse
147 * @conn_list: connection list array
148 * @max_conns: max. number of connections to store
149 *
150 * Parses the connection list of the given widget and stores the list
151 * of NIDs.
152 *
153 * Returns the number of connections, or a negative error code.
154 */
155int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
156 hda_nid_t *conn_list, int max_conns)
157{
158 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100159 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100161 hda_nid_t prev_nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 snd_assert(conn_list && max_conns > 0, return -EINVAL);
164
165 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
166 if (parm & AC_CLIST_LONG) {
167 /* long form */
168 shift = 16;
169 num_elems = 2;
170 } else {
171 /* short form */
172 shift = 8;
173 num_elems = 4;
174 }
175 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 mask = (1 << (shift-1)) - 1;
177
178 if (! conn_len)
179 return 0; /* no connection */
180
181 if (conn_len == 1) {
182 /* single connection */
183 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
184 conn_list[0] = parm & mask;
185 return 1;
186 }
187
188 /* multi connection */
189 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100190 prev_nid = 0;
191 for (i = 0; i < conn_len; i++) {
192 int range_val;
193 hda_nid_t val, n;
194
195 if (i % num_elems == 0)
196 parm = snd_hda_codec_read(codec, nid, 0,
197 AC_VERB_GET_CONNECT_LIST, i);
198 range_val = !! (parm & (1 << (shift-1))); /* ranges */
199 val = parm & mask;
200 parm >>= shift;
201 if (range_val) {
202 /* ranges between the previous and this one */
203 if (! prev_nid || prev_nid >= val) {
204 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
205 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100207 for (n = prev_nid + 1; n <= val; n++) {
208 if (conns >= max_conns) {
209 snd_printk(KERN_ERR "Too many connections\n");
210 return -EINVAL;
211 }
212 conn_list[conns++] = n;
213 }
214 } else {
215 if (conns >= max_conns) {
216 snd_printk(KERN_ERR "Too many connections\n");
217 return -EINVAL;
218 }
219 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100221 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 return conns;
224}
225
226
227/**
228 * snd_hda_queue_unsol_event - add an unsolicited event to queue
229 * @bus: the BUS
230 * @res: unsolicited event (lower 32bit of RIRB entry)
231 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
232 *
233 * Adds the given event to the queue. The events are processed in
234 * the workqueue asynchronously. Call this function in the interrupt
235 * hanlder when RIRB receives an unsolicited event.
236 *
237 * Returns 0 if successful, or a negative error code.
238 */
239int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
240{
241 struct hda_bus_unsolicited *unsol;
242 unsigned int wp;
243
244 if ((unsol = bus->unsol) == NULL)
245 return 0;
246
247 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
248 unsol->wp = wp;
249
250 wp <<= 1;
251 unsol->queue[wp] = res;
252 unsol->queue[wp + 1] = res_ex;
253
254 queue_work(unsol->workq, &unsol->work);
255
256 return 0;
257}
258
259/*
260 * process queueud unsolicited events
261 */
262static void process_unsol_events(void *data)
263{
264 struct hda_bus *bus = data;
265 struct hda_bus_unsolicited *unsol = bus->unsol;
266 struct hda_codec *codec;
267 unsigned int rp, caddr, res;
268
269 while (unsol->rp != unsol->wp) {
270 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
271 unsol->rp = rp;
272 rp <<= 1;
273 res = unsol->queue[rp];
274 caddr = unsol->queue[rp + 1];
275 if (! (caddr & (1 << 4))) /* no unsolicited event? */
276 continue;
277 codec = bus->caddr_tbl[caddr & 0x0f];
278 if (codec && codec->patch_ops.unsol_event)
279 codec->patch_ops.unsol_event(codec, res);
280 }
281}
282
283/*
284 * initialize unsolicited queue
285 */
286static int init_unsol_queue(struct hda_bus *bus)
287{
288 struct hda_bus_unsolicited *unsol;
289
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100290 if (bus->unsol) /* already initialized */
291 return 0;
292
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200293 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (! unsol) {
295 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
296 return -ENOMEM;
297 }
298 unsol->workq = create_workqueue("hda_codec");
299 if (! unsol->workq) {
300 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
301 kfree(unsol);
302 return -ENOMEM;
303 }
304 INIT_WORK(&unsol->work, process_unsol_events, bus);
305 bus->unsol = unsol;
306 return 0;
307}
308
309/*
310 * destructor
311 */
312static void snd_hda_codec_free(struct hda_codec *codec);
313
314static int snd_hda_bus_free(struct hda_bus *bus)
315{
316 struct list_head *p, *n;
317
318 if (! bus)
319 return 0;
320 if (bus->unsol) {
321 destroy_workqueue(bus->unsol->workq);
322 kfree(bus->unsol);
323 }
324 list_for_each_safe(p, n, &bus->codec_list) {
325 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
326 snd_hda_codec_free(codec);
327 }
328 if (bus->ops.private_free)
329 bus->ops.private_free(bus);
330 kfree(bus);
331 return 0;
332}
333
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100334static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct hda_bus *bus = device->device_data;
337 return snd_hda_bus_free(bus);
338}
339
340/**
341 * snd_hda_bus_new - create a HDA bus
342 * @card: the card entry
343 * @temp: the template for hda_bus information
344 * @busp: the pointer to store the created bus instance
345 *
346 * Returns 0 if successful, or a negative error code.
347 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100348int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct hda_bus **busp)
350{
351 struct hda_bus *bus;
352 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100353 static struct snd_device_ops dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .dev_free = snd_hda_bus_dev_free,
355 };
356
357 snd_assert(temp, return -EINVAL);
358 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
359
360 if (busp)
361 *busp = NULL;
362
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200363 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (bus == NULL) {
365 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
366 return -ENOMEM;
367 }
368
369 bus->card = card;
370 bus->private_data = temp->private_data;
371 bus->pci = temp->pci;
372 bus->modelname = temp->modelname;
373 bus->ops = temp->ops;
374
Ingo Molnar62932df2006-01-16 16:34:20 +0100375 mutex_init(&bus->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 INIT_LIST_HEAD(&bus->codec_list);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 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/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100458 * read widget caps for each widget and store in cache
459 */
460static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
461{
462 int i;
463 hda_nid_t nid;
464
465 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
466 &codec->start_nid);
467 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
468 if (! codec->wcaps)
469 return -ENOMEM;
470 nid = codec->start_nid;
471 for (i = 0; i < codec->num_nodes; i++, nid++)
472 codec->wcaps[i] = snd_hda_param_read(codec, nid,
473 AC_PAR_AUDIO_WIDGET_CAP);
474 return 0;
475}
476
477
478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * codec destructor
480 */
481static void snd_hda_codec_free(struct hda_codec *codec)
482{
483 if (! codec)
484 return;
485 list_del(&codec->list);
486 codec->bus->caddr_tbl[codec->addr] = NULL;
487 if (codec->patch_ops.free)
488 codec->patch_ops.free(codec);
Takashi Iwaid0311662005-11-07 14:38:44 +0100489 kfree(codec->amp_info);
Takashi Iwai54d17402005-11-21 16:33:22 +0100490 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 kfree(codec);
492}
493
494static void init_amp_hash(struct hda_codec *codec);
495
496/**
497 * snd_hda_codec_new - create a HDA codec
498 * @bus: the bus to assign
499 * @codec_addr: the codec address
500 * @codecp: the pointer to store the generated codec
501 *
502 * Returns 0 if successful, or a negative error code.
503 */
504int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
505 struct hda_codec **codecp)
506{
507 struct hda_codec *codec;
508 char component[13];
509 int err;
510
511 snd_assert(bus, return -EINVAL);
512 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
513
514 if (bus->caddr_tbl[codec_addr]) {
515 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
516 return -EBUSY;
517 }
518
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200519 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (codec == NULL) {
521 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
522 return -ENOMEM;
523 }
524
525 codec->bus = bus;
526 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100527 mutex_init(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 init_amp_hash(codec);
529
530 list_add_tail(&codec->list, &bus->codec_list);
531 bus->caddr_tbl[codec_addr] = codec;
532
533 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +0100534 if (codec->vendor_id == -1)
535 /* read again, hopefully the access method was corrected
536 * in the last read...
537 */
538 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
539 AC_PAR_VENDOR_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
541 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
542
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200543 setup_fg_nodes(codec);
544 if (! codec->afg && ! codec->mfg) {
545 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 snd_hda_codec_free(codec);
547 return -ENODEV;
548 }
549
Takashi Iwai54d17402005-11-21 16:33:22 +0100550 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
551 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
552 snd_hda_codec_free(codec);
553 return -ENOMEM;
554 }
555
Takashi Iwai86284e42005-10-11 15:05:54 +0200556 if (! codec->subsystem_id) {
557 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
558 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_SUBSYSTEM_ID,
560 0);
561 }
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 codec->preset = find_codec_preset(codec);
564 if (! *bus->card->mixername)
565 snd_hda_get_codec_name(codec, bus->card->mixername,
566 sizeof(bus->card->mixername));
567
568 if (codec->preset && codec->preset->patch)
569 err = codec->preset->patch(codec);
570 else
571 err = snd_hda_parse_generic_codec(codec);
572 if (err < 0) {
573 snd_hda_codec_free(codec);
574 return err;
575 }
576
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100577 if (codec->patch_ops.unsol_event)
578 init_unsol_queue(bus);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 snd_hda_codec_proc_new(codec);
581
582 sprintf(component, "HDA:%08x", codec->vendor_id);
583 snd_component_add(codec->bus->card, component);
584
585 if (codecp)
586 *codecp = codec;
587 return 0;
588}
589
590/**
591 * snd_hda_codec_setup_stream - set up the codec for streaming
592 * @codec: the CODEC to set up
593 * @nid: the NID to set up
594 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
595 * @channel_id: channel id to pass, zero based.
596 * @format: stream format.
597 */
598void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
599 int channel_id, int format)
600{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200601 if (! nid)
602 return;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
605 nid, stream_tag, channel_id, format);
606 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
607 (stream_tag << 4) | channel_id);
608 msleep(1);
609 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
610}
611
612
613/*
614 * amp access functions
615 */
616
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200617/* FIXME: more better hash key? */
618#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200620#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/* initialize the hash table */
623static void init_amp_hash(struct hda_codec *codec)
624{
625 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
626 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100627 codec->amp_info_size = 0;
628 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* query the hash. allocate an entry if not found. */
632static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
633{
634 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
635 u16 cur = codec->amp_hash[idx];
636 struct hda_amp_info *info;
637
638 while (cur != 0xffff) {
639 info = &codec->amp_info[cur];
640 if (info->key == key)
641 return info;
642 cur = info->next;
643 }
644
645 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100646 if (codec->num_amp_entries >= codec->amp_info_size) {
647 /* reallocate the array */
648 int new_size = codec->amp_info_size + 64;
649 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
650 GFP_KERNEL);
651 if (! new_info) {
652 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
653 return NULL;
654 }
655 if (codec->amp_info) {
656 memcpy(new_info, codec->amp_info,
657 codec->amp_info_size * sizeof(struct hda_amp_info));
658 kfree(codec->amp_info);
659 }
660 codec->amp_info_size = new_size;
661 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 cur = codec->num_amp_entries++;
664 info = &codec->amp_info[cur];
665 info->key = key;
666 info->status = 0; /* not initialized yet */
667 info->next = codec->amp_hash[idx];
668 codec->amp_hash[idx] = cur;
669
670 return info;
671}
672
673/*
674 * query AMP capabilities for the given widget and direction
675 */
676static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
677{
678 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
679
680 if (! info)
681 return 0;
682 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100683 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 nid = codec->afg;
685 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
686 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
687 info->status |= INFO_AMP_CAPS;
688 }
689 return info->amp_caps;
690}
691
692/*
693 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200694 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200696static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 hda_nid_t nid, int ch, int direction, int index)
698{
699 u32 val, parm;
700
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200701 if (info->status & INFO_AMP_VOL(ch))
702 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
705 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
706 parm |= index;
707 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
708 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200709 info->status |= INFO_AMP_VOL(ch);
710 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200714 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200716static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 hda_nid_t nid, int ch, int direction, int index, int val)
718{
719 u32 parm;
720
721 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
722 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
723 parm |= index << AC_AMP_SET_INDEX_SHIFT;
724 parm |= val;
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200726 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200730 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Takashi Iwai834be882006-03-01 14:16:17 +0100732int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
733 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
736 if (! info)
737 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200738 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200741/*
742 * update the AMP value, mask = bit mask to set, val = the value
743 */
Takashi Iwai834be882006-03-01 14:16:17 +0100744int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
745 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (! info)
750 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200751 val &= mask;
752 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (info->vol[ch] == val && ! codec->in_resume)
754 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200755 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 1;
757}
758
759
760/*
761 * AMP control callbacks
762 */
763/* retrieve parameters from private_value */
764#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
765#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
766#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
767#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
768
769/* volume */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100770int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
773 u16 nid = get_amp_nid(kcontrol);
774 u8 chs = get_amp_channels(kcontrol);
775 int dir = get_amp_direction(kcontrol);
776 u32 caps;
777
778 caps = query_amp_caps(codec, nid, dir);
779 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
780 if (! caps) {
781 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
782 return -EINVAL;
783 }
784 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
785 uinfo->count = chs == 3 ? 2 : 1;
786 uinfo->value.integer.min = 0;
787 uinfo->value.integer.max = caps;
788 return 0;
789}
790
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100791int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
794 hda_nid_t nid = get_amp_nid(kcontrol);
795 int chs = get_amp_channels(kcontrol);
796 int dir = get_amp_direction(kcontrol);
797 int idx = get_amp_index(kcontrol);
798 long *valp = ucontrol->value.integer.value;
799
800 if (chs & 1)
801 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
802 if (chs & 2)
803 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
804 return 0;
805}
806
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100807int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
810 hda_nid_t nid = get_amp_nid(kcontrol);
811 int chs = get_amp_channels(kcontrol);
812 int dir = get_amp_direction(kcontrol);
813 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 long *valp = ucontrol->value.integer.value;
815 int change = 0;
816
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200817 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200818 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
819 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200820 valp++;
821 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200822 if (chs & 2)
823 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200824 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return change;
826}
827
828/* switch */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100829int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 int chs = get_amp_channels(kcontrol);
832
833 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
834 uinfo->count = chs == 3 ? 2 : 1;
835 uinfo->value.integer.min = 0;
836 uinfo->value.integer.max = 1;
837 return 0;
838}
839
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100840int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
843 hda_nid_t nid = get_amp_nid(kcontrol);
844 int chs = get_amp_channels(kcontrol);
845 int dir = get_amp_direction(kcontrol);
846 int idx = get_amp_index(kcontrol);
847 long *valp = ucontrol->value.integer.value;
848
849 if (chs & 1)
850 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
851 if (chs & 2)
852 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
853 return 0;
854}
855
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100856int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
859 hda_nid_t nid = get_amp_nid(kcontrol);
860 int chs = get_amp_channels(kcontrol);
861 int dir = get_amp_direction(kcontrol);
862 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 long *valp = ucontrol->value.integer.value;
864 int change = 0;
865
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200866 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200867 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
868 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200869 valp++;
870 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200871 if (chs & 2)
872 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200873 0x80, *valp ? 0 : 0x80);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return change;
876}
877
878/*
Takashi Iwai985be542005-11-02 18:26:49 +0100879 * bound volume controls
880 *
881 * bind multiple volumes (# indices, from 0)
882 */
883
884#define AMP_VAL_IDX_SHIFT 19
885#define AMP_VAL_IDX_MASK (0x0f<<19)
886
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100887int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100888{
889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
890 unsigned long pval;
891 int err;
892
Ingo Molnar62932df2006-01-16 16:34:20 +0100893 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100894 pval = kcontrol->private_value;
895 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
896 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
897 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100898 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100899 return err;
900}
901
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100902int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100903{
904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
905 unsigned long pval;
906 int i, indices, err = 0, change = 0;
907
Ingo Molnar62932df2006-01-16 16:34:20 +0100908 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100909 pval = kcontrol->private_value;
910 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
911 for (i = 0; i < indices; i++) {
912 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
913 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
914 if (err < 0)
915 break;
916 change |= err;
917 }
918 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100919 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100920 return err < 0 ? err : change;
921}
922
923/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * SPDIF out controls
925 */
926
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100927static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
930 uinfo->count = 1;
931 return 0;
932}
933
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100934static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
937 IEC958_AES0_NONAUDIO |
938 IEC958_AES0_CON_EMPHASIS_5015 |
939 IEC958_AES0_CON_NOT_COPYRIGHT;
940 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
941 IEC958_AES1_CON_ORIGINAL;
942 return 0;
943}
944
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100945static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
948 IEC958_AES0_NONAUDIO |
949 IEC958_AES0_PRO_EMPHASIS_5015;
950 return 0;
951}
952
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100953static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
956
957 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
958 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
959 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
960 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
961
962 return 0;
963}
964
965/* convert from SPDIF status bits to HDA SPDIF bits
966 * bit 0 (DigEn) is always set zero (to be filled later)
967 */
968static unsigned short convert_from_spdif_status(unsigned int sbits)
969{
970 unsigned short val = 0;
971
972 if (sbits & IEC958_AES0_PROFESSIONAL)
973 val |= 1 << 6;
974 if (sbits & IEC958_AES0_NONAUDIO)
975 val |= 1 << 5;
976 if (sbits & IEC958_AES0_PROFESSIONAL) {
977 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
978 val |= 1 << 3;
979 } else {
980 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
981 val |= 1 << 3;
982 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
983 val |= 1 << 4;
984 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
985 val |= 1 << 7;
986 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
987 }
988 return val;
989}
990
991/* convert to SPDIF status bits from HDA SPDIF bits
992 */
993static unsigned int convert_to_spdif_status(unsigned short val)
994{
995 unsigned int sbits = 0;
996
997 if (val & (1 << 5))
998 sbits |= IEC958_AES0_NONAUDIO;
999 if (val & (1 << 6))
1000 sbits |= IEC958_AES0_PROFESSIONAL;
1001 if (sbits & IEC958_AES0_PROFESSIONAL) {
1002 if (sbits & (1 << 3))
1003 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1004 } else {
1005 if (val & (1 << 3))
1006 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1007 if (! (val & (1 << 4)))
1008 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1009 if (val & (1 << 7))
1010 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1011 sbits |= val & (0x7f << 8);
1012 }
1013 return sbits;
1014}
1015
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001016static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1019 hda_nid_t nid = kcontrol->private_value;
1020 unsigned short val;
1021 int change;
1022
Ingo Molnar62932df2006-01-16 16:34:20 +01001023 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 codec->spdif_status = ucontrol->value.iec958.status[0] |
1025 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1026 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1027 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1028 val = convert_from_spdif_status(codec->spdif_status);
1029 val |= codec->spdif_ctls & 1;
1030 change = codec->spdif_ctls != val;
1031 codec->spdif_ctls = val;
1032
1033 if (change || codec->in_resume) {
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_DIGI_CONVERT_2, val >> 8);
1036 }
1037
Ingo Molnar62932df2006-01-16 16:34:20 +01001038 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return change;
1040}
1041
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001042static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1045 uinfo->count = 1;
1046 uinfo->value.integer.min = 0;
1047 uinfo->value.integer.max = 1;
1048 return 0;
1049}
1050
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001051static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1054
1055 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1056 return 0;
1057}
1058
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001059static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1062 hda_nid_t nid = kcontrol->private_value;
1063 unsigned short val;
1064 int change;
1065
Ingo Molnar62932df2006-01-16 16:34:20 +01001066 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 val = codec->spdif_ctls & ~1;
1068 if (ucontrol->value.integer.value[0])
1069 val |= 1;
1070 change = codec->spdif_ctls != val;
1071 if (change || codec->in_resume) {
1072 codec->spdif_ctls = val;
1073 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1074 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1075 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1076 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1077 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001078 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return change;
1080}
1081
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001082static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 {
1084 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1086 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1087 .info = snd_hda_spdif_mask_info,
1088 .get = snd_hda_spdif_cmask_get,
1089 },
1090 {
1091 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1093 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1094 .info = snd_hda_spdif_mask_info,
1095 .get = snd_hda_spdif_pmask_get,
1096 },
1097 {
1098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1099 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1100 .info = snd_hda_spdif_mask_info,
1101 .get = snd_hda_spdif_default_get,
1102 .put = snd_hda_spdif_default_put,
1103 },
1104 {
1105 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1106 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1107 .info = snd_hda_spdif_out_switch_info,
1108 .get = snd_hda_spdif_out_switch_get,
1109 .put = snd_hda_spdif_out_switch_put,
1110 },
1111 { } /* end */
1112};
1113
1114/**
1115 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1116 * @codec: the HDA codec
1117 * @nid: audio out widget NID
1118 *
1119 * Creates controls related with the SPDIF output.
1120 * Called from each patch supporting the SPDIF out.
1121 *
1122 * Returns 0 if successful, or a negative error code.
1123 */
1124int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1125{
1126 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001127 struct snd_kcontrol *kctl;
1128 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1131 kctl = snd_ctl_new1(dig_mix, codec);
1132 kctl->private_value = nid;
1133 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1134 return err;
1135 }
1136 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1137 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1138 return 0;
1139}
1140
1141/*
1142 * SPDIF input
1143 */
1144
1145#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1146
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001147static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1150
1151 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1152 return 0;
1153}
1154
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001155static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1158 hda_nid_t nid = kcontrol->private_value;
1159 unsigned int val = !!ucontrol->value.integer.value[0];
1160 int change;
1161
Ingo Molnar62932df2006-01-16 16:34:20 +01001162 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 change = codec->spdif_in_enable != val;
1164 if (change || codec->in_resume) {
1165 codec->spdif_in_enable = val;
1166 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1167 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001168 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return change;
1170}
1171
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001172static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1175 hda_nid_t nid = kcontrol->private_value;
1176 unsigned short val;
1177 unsigned int sbits;
1178
1179 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1180 sbits = convert_to_spdif_status(val);
1181 ucontrol->value.iec958.status[0] = sbits;
1182 ucontrol->value.iec958.status[1] = sbits >> 8;
1183 ucontrol->value.iec958.status[2] = sbits >> 16;
1184 ucontrol->value.iec958.status[3] = sbits >> 24;
1185 return 0;
1186}
1187
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001188static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 {
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1192 .info = snd_hda_spdif_in_switch_info,
1193 .get = snd_hda_spdif_in_switch_get,
1194 .put = snd_hda_spdif_in_switch_put,
1195 },
1196 {
1197 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1198 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1199 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1200 .info = snd_hda_spdif_mask_info,
1201 .get = snd_hda_spdif_in_status_get,
1202 },
1203 { } /* end */
1204};
1205
1206/**
1207 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1208 * @codec: the HDA codec
1209 * @nid: audio in widget NID
1210 *
1211 * Creates controls related with the SPDIF input.
1212 * Called from each patch supporting the SPDIF in.
1213 *
1214 * Returns 0 if successful, or a negative error code.
1215 */
1216int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1217{
1218 int err;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001219 struct snd_kcontrol *kctl;
1220 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1223 kctl = snd_ctl_new1(dig_mix, codec);
1224 kctl->private_value = nid;
1225 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1226 return err;
1227 }
1228 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1229 return 0;
1230}
1231
1232
Takashi Iwai54d17402005-11-21 16:33:22 +01001233/*
1234 * set power state of the codec
1235 */
1236static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1237 unsigned int power_state)
1238{
1239 hda_nid_t nid, nid_start;
1240 int nodes;
1241
1242 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1243 power_state);
1244
1245 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1246 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1247 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_POWER_STATE,
1250 power_state);
1251 }
1252
1253 if (power_state == AC_PWRST_D0)
1254 msleep(10);
1255}
1256
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/**
1259 * snd_hda_build_controls - build mixer controls
1260 * @bus: the BUS
1261 *
1262 * Creates mixer controls for each codec included in the bus.
1263 *
1264 * Returns 0 if successful, otherwise a negative error code.
1265 */
1266int snd_hda_build_controls(struct hda_bus *bus)
1267{
1268 struct list_head *p;
1269
1270 /* build controls */
1271 list_for_each(p, &bus->codec_list) {
1272 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1273 int err;
1274 if (! codec->patch_ops.build_controls)
1275 continue;
1276 err = codec->patch_ops.build_controls(codec);
1277 if (err < 0)
1278 return err;
1279 }
1280
1281 /* initialize */
1282 list_for_each(p, &bus->codec_list) {
1283 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1284 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001285 hda_set_power_state(codec,
1286 codec->afg ? codec->afg : codec->mfg,
1287 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (! codec->patch_ops.init)
1289 continue;
1290 err = codec->patch_ops.init(codec);
1291 if (err < 0)
1292 return err;
1293 }
1294 return 0;
1295}
1296
1297
1298/*
1299 * stream formats
1300 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001301struct hda_rate_tbl {
1302 unsigned int hz;
1303 unsigned int alsa_bits;
1304 unsigned int hda_fmt;
1305};
1306
1307static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001309
1310 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1312 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1313 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1314 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1315 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1316 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1317 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1318 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1319 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1320 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1321 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001322
1323 /* not autodetected value */
1324 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001325
1326 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327};
1328
1329/**
1330 * snd_hda_calc_stream_format - calculate format bitset
1331 * @rate: the sample rate
1332 * @channels: the number of channels
1333 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1334 * @maxbps: the max. bps
1335 *
1336 * Calculate the format bitset from the given rate, channels and th PCM format.
1337 *
1338 * Return zero if invalid.
1339 */
1340unsigned int snd_hda_calc_stream_format(unsigned int rate,
1341 unsigned int channels,
1342 unsigned int format,
1343 unsigned int maxbps)
1344{
1345 int i;
1346 unsigned int val = 0;
1347
Takashi Iwaibefdf312005-08-22 13:57:55 +02001348 for (i = 0; rate_bits[i].hz; i++)
1349 if (rate_bits[i].hz == rate) {
1350 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
1352 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001353 if (! rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 snd_printdd("invalid rate %d\n", rate);
1355 return 0;
1356 }
1357
1358 if (channels == 0 || channels > 8) {
1359 snd_printdd("invalid channels %d\n", channels);
1360 return 0;
1361 }
1362 val |= channels - 1;
1363
1364 switch (snd_pcm_format_width(format)) {
1365 case 8: val |= 0x00; break;
1366 case 16: val |= 0x10; break;
1367 case 20:
1368 case 24:
1369 case 32:
1370 if (maxbps >= 32)
1371 val |= 0x40;
1372 else if (maxbps >= 24)
1373 val |= 0x30;
1374 else
1375 val |= 0x20;
1376 break;
1377 default:
1378 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1379 return 0;
1380 }
1381
1382 return val;
1383}
1384
1385/**
1386 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1387 * @codec: the HDA codec
1388 * @nid: NID to query
1389 * @ratesp: the pointer to store the detected rate bitflags
1390 * @formatsp: the pointer to store the detected formats
1391 * @bpsp: the pointer to store the detected format widths
1392 *
1393 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1394 * or @bsps argument is ignored.
1395 *
1396 * Returns 0 if successful, otherwise a negative error code.
1397 */
1398int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1399 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1400{
1401 int i;
1402 unsigned int val, streams;
1403
1404 val = 0;
1405 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001406 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1408 if (val == -1)
1409 return -EIO;
1410 }
1411 if (! val)
1412 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1413
1414 if (ratesp) {
1415 u32 rates = 0;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001416 for (i = 0; rate_bits[i].hz; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001418 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 *ratesp = rates;
1421 }
1422
1423 if (formatsp || bpsp) {
1424 u64 formats = 0;
1425 unsigned int bps;
1426 unsigned int wcaps;
1427
Takashi Iwai54d17402005-11-21 16:33:22 +01001428 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1430 if (streams == -1)
1431 return -EIO;
1432 if (! streams) {
1433 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1434 if (streams == -1)
1435 return -EIO;
1436 }
1437
1438 bps = 0;
1439 if (streams & AC_SUPFMT_PCM) {
1440 if (val & AC_SUPPCM_BITS_8) {
1441 formats |= SNDRV_PCM_FMTBIT_U8;
1442 bps = 8;
1443 }
1444 if (val & AC_SUPPCM_BITS_16) {
1445 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1446 bps = 16;
1447 }
1448 if (wcaps & AC_WCAP_DIGITAL) {
1449 if (val & AC_SUPPCM_BITS_32)
1450 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1451 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1452 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1453 if (val & AC_SUPPCM_BITS_24)
1454 bps = 24;
1455 else if (val & AC_SUPPCM_BITS_20)
1456 bps = 20;
1457 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1458 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1459 if (val & AC_SUPPCM_BITS_32)
1460 bps = 32;
1461 else if (val & AC_SUPPCM_BITS_20)
1462 bps = 20;
1463 else if (val & AC_SUPPCM_BITS_24)
1464 bps = 24;
1465 }
1466 }
1467 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1468 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1469 bps = 32;
1470 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1471 /* temporary hack: we have still no proper support
1472 * for the direct AC3 stream...
1473 */
1474 formats |= SNDRV_PCM_FMTBIT_U8;
1475 bps = 8;
1476 }
1477 if (formatsp)
1478 *formatsp = formats;
1479 if (bpsp)
1480 *bpsp = bps;
1481 }
1482
1483 return 0;
1484}
1485
1486/**
1487 * snd_hda_is_supported_format - check whether the given node supports the format val
1488 *
1489 * Returns 1 if supported, 0 if not.
1490 */
1491int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1492 unsigned int format)
1493{
1494 int i;
1495 unsigned int val = 0, rate, stream;
1496
1497 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001498 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1500 if (val == -1)
1501 return 0;
1502 }
1503 if (! val) {
1504 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1505 if (val == -1)
1506 return 0;
1507 }
1508
1509 rate = format & 0xff00;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001510 for (i = 0; rate_bits[i].hz; i++)
1511 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (val & (1 << i))
1513 break;
1514 return 0;
1515 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001516 if (! rate_bits[i].hz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
1518
1519 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1520 if (stream == -1)
1521 return 0;
1522 if (! stream && nid != codec->afg)
1523 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1524 if (! stream || stream == -1)
1525 return 0;
1526
1527 if (stream & AC_SUPFMT_PCM) {
1528 switch (format & 0xf0) {
1529 case 0x00:
1530 if (! (val & AC_SUPPCM_BITS_8))
1531 return 0;
1532 break;
1533 case 0x10:
1534 if (! (val & AC_SUPPCM_BITS_16))
1535 return 0;
1536 break;
1537 case 0x20:
1538 if (! (val & AC_SUPPCM_BITS_20))
1539 return 0;
1540 break;
1541 case 0x30:
1542 if (! (val & AC_SUPPCM_BITS_24))
1543 return 0;
1544 break;
1545 case 0x40:
1546 if (! (val & AC_SUPPCM_BITS_32))
1547 return 0;
1548 break;
1549 default:
1550 return 0;
1551 }
1552 } else {
1553 /* FIXME: check for float32 and AC3? */
1554 }
1555
1556 return 1;
1557}
1558
1559/*
1560 * PCM stuff
1561 */
1562static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1563 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001564 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 return 0;
1567}
1568
1569static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1570 struct hda_codec *codec,
1571 unsigned int stream_tag,
1572 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001573 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1576 return 0;
1577}
1578
1579static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1580 struct hda_codec *codec,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001581 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1584 return 0;
1585}
1586
1587static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1588{
1589 if (info->nid) {
1590 /* query support PCM information from the given NID */
1591 if (! info->rates || ! info->formats)
1592 snd_hda_query_supported_pcm(codec, info->nid,
1593 info->rates ? NULL : &info->rates,
1594 info->formats ? NULL : &info->formats,
1595 info->maxbps ? NULL : &info->maxbps);
1596 }
1597 if (info->ops.open == NULL)
1598 info->ops.open = hda_pcm_default_open_close;
1599 if (info->ops.close == NULL)
1600 info->ops.close = hda_pcm_default_open_close;
1601 if (info->ops.prepare == NULL) {
1602 snd_assert(info->nid, return -EINVAL);
1603 info->ops.prepare = hda_pcm_default_prepare;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (info->ops.cleanup == NULL) {
1606 snd_assert(info->nid, return -EINVAL);
1607 info->ops.cleanup = hda_pcm_default_cleanup;
1608 }
1609 return 0;
1610}
1611
1612/**
1613 * snd_hda_build_pcms - build PCM information
1614 * @bus: the BUS
1615 *
1616 * Create PCM information for each codec included in the bus.
1617 *
1618 * The build_pcms codec patch is requested to set up codec->num_pcms and
1619 * codec->pcm_info properly. The array is referred by the top-level driver
1620 * to create its PCM instances.
1621 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1622 * callback.
1623 *
1624 * At least, substreams, channels_min and channels_max must be filled for
1625 * each stream. substreams = 0 indicates that the stream doesn't exist.
1626 * When rates and/or formats are zero, the supported values are queried
1627 * from the given nid. The nid is used also by the default ops.prepare
1628 * and ops.cleanup callbacks.
1629 *
1630 * The driver needs to call ops.open in its open callback. Similarly,
1631 * ops.close is supposed to be called in the close callback.
1632 * ops.prepare should be called in the prepare or hw_params callback
1633 * with the proper parameters for set up.
1634 * ops.cleanup should be called in hw_free for clean up of streams.
1635 *
1636 * This function returns 0 if successfull, or a negative error code.
1637 */
1638int snd_hda_build_pcms(struct hda_bus *bus)
1639{
1640 struct list_head *p;
1641
1642 list_for_each(p, &bus->codec_list) {
1643 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1644 unsigned int pcm, s;
1645 int err;
1646 if (! codec->patch_ops.build_pcms)
1647 continue;
1648 err = codec->patch_ops.build_pcms(codec);
1649 if (err < 0)
1650 return err;
1651 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1652 for (s = 0; s < 2; s++) {
1653 struct hda_pcm_stream *info;
1654 info = &codec->pcm_info[pcm].stream[s];
1655 if (! info->substreams)
1656 continue;
1657 err = set_pcm_default_values(codec, info);
1658 if (err < 0)
1659 return err;
1660 }
1661 }
1662 }
1663 return 0;
1664}
1665
1666
1667/**
1668 * snd_hda_check_board_config - compare the current codec with the config table
1669 * @codec: the HDA codec
1670 * @tbl: configuration table, terminated by null entries
1671 *
1672 * Compares the modelname or PCI subsystem id of the current codec with the
1673 * given configuration table. If a matching entry is found, returns its
1674 * config value (supposed to be 0 or positive).
1675 *
1676 * If no entries are matching, the function returns a negative value.
1677 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001678int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001680 const struct hda_board_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (codec->bus->modelname) {
Takashi Iwai72915482005-05-12 16:49:45 +02001683 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (c->modelname &&
1685 ! strcmp(codec->bus->modelname, c->modelname)) {
1686 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1687 return c->config;
1688 }
1689 }
1690 }
1691
1692 if (codec->bus->pci) {
1693 u16 subsystem_vendor, subsystem_device;
1694 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1695 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
Takashi Iwai72915482005-05-12 16:49:45 +02001696 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1697 if (c->pci_subvendor == subsystem_vendor &&
Takashi Iwai5ecd7022005-06-10 19:54:23 +02001698 (! c->pci_subdevice /* all match */||
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001699 (c->pci_subdevice == subsystem_device))) {
1700 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1701 subsystem_vendor, subsystem_device, c->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return c->config;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 }
1706 return -1;
1707}
1708
1709/**
1710 * snd_hda_add_new_ctls - create controls from the array
1711 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001712 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *
1714 * This helper function creates and add new controls in the given array.
1715 * The array must be terminated with an empty entry as terminator.
1716 *
1717 * Returns 0 if successful, or a negative error code.
1718 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001719int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 int err;
1722
1723 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001724 struct snd_kcontrol *kctl;
1725 kctl = snd_ctl_new1(knew, codec);
1726 if (! kctl)
1727 return -ENOMEM;
1728 err = snd_ctl_add(codec->bus->card, kctl);
1729 if (err < 0) {
1730 if (! codec->addr)
1731 return err;
1732 kctl = snd_ctl_new1(knew, codec);
1733 if (! kctl)
1734 return -ENOMEM;
1735 kctl->id.device = codec->addr;
1736 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1737 return err;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 return 0;
1741}
1742
1743
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001744/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001745 * Channel mode helper
1746 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001747int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001748 const struct hda_channel_mode *chmode, int num_chmodes)
1749{
1750 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1751 uinfo->count = 1;
1752 uinfo->value.enumerated.items = num_chmodes;
1753 if (uinfo->value.enumerated.item >= num_chmodes)
1754 uinfo->value.enumerated.item = num_chmodes - 1;
1755 sprintf(uinfo->value.enumerated.name, "%dch",
1756 chmode[uinfo->value.enumerated.item].channels);
1757 return 0;
1758}
1759
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001760int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001761 const struct hda_channel_mode *chmode, int num_chmodes,
1762 int max_channels)
1763{
1764 int i;
1765
1766 for (i = 0; i < num_chmodes; i++) {
1767 if (max_channels == chmode[i].channels) {
1768 ucontrol->value.enumerated.item[0] = i;
1769 break;
1770 }
1771 }
1772 return 0;
1773}
1774
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001775int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001776 const struct hda_channel_mode *chmode, int num_chmodes,
1777 int *max_channelsp)
1778{
1779 unsigned int mode;
1780
1781 mode = ucontrol->value.enumerated.item[0];
1782 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001783 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001784 return 0;
1785 /* change the current channel setting */
1786 *max_channelsp = chmode[mode].channels;
1787 if (chmode[mode].sequence)
1788 snd_hda_sequence_write(codec, chmode[mode].sequence);
1789 return 1;
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
1793 * input MUX helper
1794 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001795int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 unsigned int index;
1798
1799 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1800 uinfo->count = 1;
1801 uinfo->value.enumerated.items = imux->num_items;
1802 index = uinfo->value.enumerated.item;
1803 if (index >= imux->num_items)
1804 index = imux->num_items - 1;
1805 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1806 return 0;
1807}
1808
1809int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001810 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned int *cur_val)
1812{
1813 unsigned int idx;
1814
1815 idx = ucontrol->value.enumerated.item[0];
1816 if (idx >= imux->num_items)
1817 idx = imux->num_items - 1;
1818 if (*cur_val == idx && ! codec->in_resume)
1819 return 0;
1820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1821 imux->items[idx].index);
1822 *cur_val = idx;
1823 return 1;
1824}
1825
1826
1827/*
1828 * Multi-channel / digital-out PCM helper functions
1829 */
1830
1831/*
1832 * open the digital out in the exclusive mode
1833 */
1834int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1835{
Ingo Molnar62932df2006-01-16 16:34:20 +01001836 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (mout->dig_out_used) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001838 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return -EBUSY; /* already being used */
1840 }
1841 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001842 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return 0;
1844}
1845
1846/*
1847 * release the digital out
1848 */
1849int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1850{
Ingo Molnar62932df2006-01-16 16:34:20 +01001851 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001853 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return 0;
1855}
1856
1857/*
1858 * set up more restrictions for analog out
1859 */
1860int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001861 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 substream->runtime->hw.channels_max = mout->max_channels;
1864 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1865 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1866}
1867
1868/*
1869 * set up the i/o for analog out
1870 * when the digital out is available, copy the front out to digital out, too.
1871 */
1872int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1873 unsigned int stream_tag,
1874 unsigned int format,
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01001875 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 hda_nid_t *nids = mout->dac_nids;
1878 int chs = substream->runtime->channels;
1879 int i;
1880
Ingo Molnar62932df2006-01-16 16:34:20 +01001881 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1883 if (chs == 2 &&
1884 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1885 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1886 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1887 /* setup digital receiver */
1888 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1889 stream_tag, 0, format);
1890 } else {
1891 mout->dig_out_used = 0;
1892 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1893 }
1894 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001895 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* front */
1898 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1899 if (mout->hp_nid)
1900 /* headphone out will just decode front left/right (stereo) */
1901 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1902 /* surrounds */
1903 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001904 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1906 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001907 else /* copy front */
1908 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1909 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 return 0;
1912}
1913
1914/*
1915 * clean up the setting for analog out
1916 */
1917int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1918{
1919 hda_nid_t *nids = mout->dac_nids;
1920 int i;
1921
1922 for (i = 0; i < mout->num_dacs; i++)
1923 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1924 if (mout->hp_nid)
1925 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01001926 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1928 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1929 mout->dig_out_used = 0;
1930 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001931 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001935/*
1936 * Helper for automatic ping configuration
1937 */
Kailang Yangdf694da2005-12-05 19:42:22 +01001938
1939static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
1940{
1941 for (; *list; list++)
1942 if (*list == nid)
1943 return 1;
1944 return 0;
1945}
1946
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001947/* parse all pin widgets and store the useful pin nids to cfg */
Kailang Yangdf694da2005-12-05 19:42:22 +01001948int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
1949 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001950{
1951 hda_nid_t nid, nid_start;
1952 int i, j, nodes;
1953 short seq, sequences[4], assoc_line_out;
1954
1955 memset(cfg, 0, sizeof(*cfg));
1956
1957 memset(sequences, 0, sizeof(sequences));
1958 assoc_line_out = 0;
1959
1960 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1961 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001962 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001963 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1964 unsigned int def_conf;
1965 short assoc, loc;
1966
1967 /* read all default configuration for pin complex */
1968 if (wid_type != AC_WID_PIN)
1969 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01001970 /* ignore the given nids (e.g. pc-beep returns error) */
1971 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
1972 continue;
1973
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001974 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
1975 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1976 continue;
1977 loc = get_defcfg_location(def_conf);
1978 switch (get_defcfg_device(def_conf)) {
1979 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001980 seq = get_defcfg_sequence(def_conf);
1981 assoc = get_defcfg_association(def_conf);
1982 if (! assoc)
1983 continue;
1984 if (! assoc_line_out)
1985 assoc_line_out = assoc;
1986 else if (assoc_line_out != assoc)
1987 continue;
1988 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
1989 continue;
1990 cfg->line_out_pins[cfg->line_outs] = nid;
1991 sequences[cfg->line_outs] = seq;
1992 cfg->line_outs++;
1993 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01001994 case AC_JACK_SPEAKER:
1995 cfg->speaker_pin = nid;
1996 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001997 case AC_JACK_HP_OUT:
1998 cfg->hp_pin = nid;
1999 break;
2000 case AC_JACK_MIC_IN:
2001 if (loc == AC_JACK_LOC_FRONT)
2002 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
2003 else
2004 cfg->input_pins[AUTO_PIN_MIC] = nid;
2005 break;
2006 case AC_JACK_LINE_IN:
2007 if (loc == AC_JACK_LOC_FRONT)
2008 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2009 else
2010 cfg->input_pins[AUTO_PIN_LINE] = nid;
2011 break;
2012 case AC_JACK_CD:
2013 cfg->input_pins[AUTO_PIN_CD] = nid;
2014 break;
2015 case AC_JACK_AUX:
2016 cfg->input_pins[AUTO_PIN_AUX] = nid;
2017 break;
2018 case AC_JACK_SPDIF_OUT:
2019 cfg->dig_out_pin = nid;
2020 break;
2021 case AC_JACK_SPDIF_IN:
2022 cfg->dig_in_pin = nid;
2023 break;
2024 }
2025 }
2026
2027 /* sort by sequence */
2028 for (i = 0; i < cfg->line_outs; i++)
2029 for (j = i + 1; j < cfg->line_outs; j++)
2030 if (sequences[i] > sequences[j]) {
2031 seq = sequences[i];
2032 sequences[i] = sequences[j];
2033 sequences[j] = seq;
2034 nid = cfg->line_out_pins[i];
2035 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2036 cfg->line_out_pins[j] = nid;
2037 }
2038
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002039 /* Reorder the surround channels
2040 * ALSA sequence is front/surr/clfe/side
2041 * HDA sequence is:
2042 * 4-ch: front/surr => OK as it is
2043 * 6-ch: front/clfe/surr
2044 * 8-ch: front/clfe/side/surr
2045 */
2046 switch (cfg->line_outs) {
2047 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002048 nid = cfg->line_out_pins[1];
2049 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2050 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002051 break;
2052 case 4:
2053 nid = cfg->line_out_pins[1];
2054 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2055 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2056 cfg->line_out_pins[2] = nid;
2057 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002058 }
2059
2060 return 0;
2061}
2062
Takashi Iwai4a471b72005-12-07 13:56:29 +01002063/* labels for input pins */
2064const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2065 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2066};
2067
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069#ifdef CONFIG_PM
2070/*
2071 * power management
2072 */
2073
2074/**
2075 * snd_hda_suspend - suspend the codecs
2076 * @bus: the HDA bus
2077 * @state: suspsend state
2078 *
2079 * Returns 0 if successful.
2080 */
2081int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2082{
2083 struct list_head *p;
2084
2085 /* FIXME: should handle power widget capabilities */
2086 list_for_each(p, &bus->codec_list) {
2087 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2088 if (codec->patch_ops.suspend)
2089 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002090 hda_set_power_state(codec,
2091 codec->afg ? codec->afg : codec->mfg,
2092 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 return 0;
2095}
2096
2097/**
2098 * snd_hda_resume - resume the codecs
2099 * @bus: the HDA bus
2100 * @state: resume state
2101 *
2102 * Returns 0 if successful.
2103 */
2104int snd_hda_resume(struct hda_bus *bus)
2105{
2106 struct list_head *p;
2107
2108 list_for_each(p, &bus->codec_list) {
2109 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002110 hda_set_power_state(codec,
2111 codec->afg ? codec->afg : codec->mfg,
2112 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (codec->patch_ops.resume)
2114 codec->patch_ops.resume(codec);
2115 }
2116 return 0;
2117}
2118
2119/**
2120 * snd_hda_resume_ctls - resume controls in the new control list
2121 * @codec: the HDA codec
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002122 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 *
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002124 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * originally for snd_hda_add_new_ctls().
2126 * The array must be terminated with an empty entry as terminator.
2127 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002128int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +01002130 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 val = kmalloc(sizeof(*val), GFP_KERNEL);
2133 if (! val)
2134 return -ENOMEM;
2135 codec->in_resume = 1;
2136 for (; knew->name; knew++) {
2137 int i, count;
2138 count = knew->count ? knew->count : 1;
2139 for (i = 0; i < count; i++) {
2140 memset(val, 0, sizeof(*val));
2141 val->id.iface = knew->iface;
2142 val->id.device = knew->device;
2143 val->id.subdevice = knew->subdevice;
2144 strcpy(val->id.name, knew->name);
2145 val->id.index = knew->index ? knew->index : i;
2146 /* Assume that get callback reads only from cache,
2147 * not accessing to the real hardware
2148 */
2149 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2150 continue;
2151 snd_ctl_elem_write(codec->bus->card, NULL, val);
2152 }
2153 }
2154 codec->in_resume = 0;
2155 kfree(val);
2156 return 0;
2157}
2158
2159/**
2160 * snd_hda_resume_spdif_out - resume the digital out
2161 * @codec: the HDA codec
2162 */
2163int snd_hda_resume_spdif_out(struct hda_codec *codec)
2164{
2165 return snd_hda_resume_ctls(codec, dig_mixes);
2166}
2167
2168/**
2169 * snd_hda_resume_spdif_in - resume the digital in
2170 * @codec: the HDA codec
2171 */
2172int snd_hda_resume_spdif_in(struct hda_codec *codec)
2173{
2174 return snd_hda_resume_ctls(codec, dig_in_ctls);
2175}
2176#endif
2177
2178/*
2179 * symbols exported for controller modules
2180 */
2181EXPORT_SYMBOL(snd_hda_codec_read);
2182EXPORT_SYMBOL(snd_hda_codec_write);
2183EXPORT_SYMBOL(snd_hda_sequence_write);
2184EXPORT_SYMBOL(snd_hda_get_sub_nodes);
2185EXPORT_SYMBOL(snd_hda_queue_unsol_event);
2186EXPORT_SYMBOL(snd_hda_bus_new);
2187EXPORT_SYMBOL(snd_hda_codec_new);
2188EXPORT_SYMBOL(snd_hda_codec_setup_stream);
2189EXPORT_SYMBOL(snd_hda_calc_stream_format);
2190EXPORT_SYMBOL(snd_hda_build_pcms);
2191EXPORT_SYMBOL(snd_hda_build_controls);
2192#ifdef CONFIG_PM
2193EXPORT_SYMBOL(snd_hda_suspend);
2194EXPORT_SYMBOL(snd_hda_resume);
2195#endif
2196
2197/*
2198 * INIT part
2199 */
2200
2201static int __init alsa_hda_init(void)
2202{
2203 return 0;
2204}
2205
2206static void __exit alsa_hda_exit(void)
2207{
2208}
2209
2210module_init(alsa_hda_init)
2211module_exit(alsa_hda_exit)