blob: 208a3341ec201bc91a1d92758f6974e35d35aae6 [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 Iwaic8b6bf92005-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 Iwaic8b6bf92005-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 Iwaic8b6bf92005-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);
534 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
535 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
536
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200537 setup_fg_nodes(codec);
538 if (! codec->afg && ! codec->mfg) {
539 snd_printdd("hda_codec: no AFG or MFG node found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 snd_hda_codec_free(codec);
541 return -ENODEV;
542 }
543
Takashi Iwai54d17402005-11-21 16:33:22 +0100544 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
545 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
546 snd_hda_codec_free(codec);
547 return -ENOMEM;
548 }
549
Takashi Iwai86284e42005-10-11 15:05:54 +0200550 if (! codec->subsystem_id) {
551 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
552 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
553 AC_VERB_GET_SUBSYSTEM_ID,
554 0);
555 }
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 codec->preset = find_codec_preset(codec);
558 if (! *bus->card->mixername)
559 snd_hda_get_codec_name(codec, bus->card->mixername,
560 sizeof(bus->card->mixername));
561
562 if (codec->preset && codec->preset->patch)
563 err = codec->preset->patch(codec);
564 else
565 err = snd_hda_parse_generic_codec(codec);
566 if (err < 0) {
567 snd_hda_codec_free(codec);
568 return err;
569 }
570
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100571 if (codec->patch_ops.unsol_event)
572 init_unsol_queue(bus);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 snd_hda_codec_proc_new(codec);
575
576 sprintf(component, "HDA:%08x", codec->vendor_id);
577 snd_component_add(codec->bus->card, component);
578
579 if (codecp)
580 *codecp = codec;
581 return 0;
582}
583
584/**
585 * snd_hda_codec_setup_stream - set up the codec for streaming
586 * @codec: the CODEC to set up
587 * @nid: the NID to set up
588 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
589 * @channel_id: channel id to pass, zero based.
590 * @format: stream format.
591 */
592void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
593 int channel_id, int format)
594{
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200595 if (! nid)
596 return;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
599 nid, stream_tag, channel_id, format);
600 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
601 (stream_tag << 4) | channel_id);
602 msleep(1);
603 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
604}
605
606
607/*
608 * amp access functions
609 */
610
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200611/* FIXME: more better hash key? */
612#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200614#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* initialize the hash table */
617static void init_amp_hash(struct hda_codec *codec)
618{
619 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
620 codec->num_amp_entries = 0;
Takashi Iwaid0311662005-11-07 14:38:44 +0100621 codec->amp_info_size = 0;
622 codec->amp_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/* query the hash. allocate an entry if not found. */
626static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
627{
628 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
629 u16 cur = codec->amp_hash[idx];
630 struct hda_amp_info *info;
631
632 while (cur != 0xffff) {
633 info = &codec->amp_info[cur];
634 if (info->key == key)
635 return info;
636 cur = info->next;
637 }
638
639 /* add a new hash entry */
Takashi Iwaid0311662005-11-07 14:38:44 +0100640 if (codec->num_amp_entries >= codec->amp_info_size) {
641 /* reallocate the array */
642 int new_size = codec->amp_info_size + 64;
643 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
644 GFP_KERNEL);
645 if (! new_info) {
646 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
647 return NULL;
648 }
649 if (codec->amp_info) {
650 memcpy(new_info, codec->amp_info,
651 codec->amp_info_size * sizeof(struct hda_amp_info));
652 kfree(codec->amp_info);
653 }
654 codec->amp_info_size = new_size;
655 codec->amp_info = new_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 cur = codec->num_amp_entries++;
658 info = &codec->amp_info[cur];
659 info->key = key;
660 info->status = 0; /* not initialized yet */
661 info->next = codec->amp_hash[idx];
662 codec->amp_hash[idx] = cur;
663
664 return info;
665}
666
667/*
668 * query AMP capabilities for the given widget and direction
669 */
670static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
671{
672 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
673
674 if (! info)
675 return 0;
676 if (! (info->status & INFO_AMP_CAPS)) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100677 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 nid = codec->afg;
679 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
680 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
681 info->status |= INFO_AMP_CAPS;
682 }
683 return info->amp_caps;
684}
685
686/*
687 * read the current volume to info
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200688 * if the cache exists, read the cache value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200690static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 hda_nid_t nid, int ch, int direction, int index)
692{
693 u32 val, parm;
694
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200695 if (info->status & INFO_AMP_VOL(ch))
696 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
699 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
700 parm |= index;
701 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
702 info->vol[ch] = val & 0xff;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200703 info->status |= INFO_AMP_VOL(ch);
704 return info->vol[ch];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200708 * write the current volume in info to the h/w and update the cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200710static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 hda_nid_t nid, int ch, int direction, int index, int val)
712{
713 u32 parm;
714
715 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
716 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
717 parm |= index << AC_AMP_SET_INDEX_SHIFT;
718 parm |= val;
719 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200720 info->vol[ch] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200724 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Adrian Bunk89c87bf2005-05-13 15:28:08 +0200726static 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 -0700727{
728 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
729 if (! info)
730 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200731 return get_vol_mute(codec, info, nid, ch, direction, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200734/*
735 * update the AMP value, mask = bit mask to set, val = the value
736 */
737static 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 -0700738{
739 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (! info)
742 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200743 val &= mask;
744 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (info->vol[ch] == val && ! codec->in_resume)
746 return 0;
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200747 put_vol_mute(codec, info, nid, ch, direction, idx, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 1;
749}
750
751
752/*
753 * AMP control callbacks
754 */
755/* retrieve parameters from private_value */
756#define get_amp_nid(kc) ((kc)->private_value & 0xffff)
757#define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
758#define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
759#define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
760
761/* volume */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100762int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
765 u16 nid = get_amp_nid(kcontrol);
766 u8 chs = get_amp_channels(kcontrol);
767 int dir = get_amp_direction(kcontrol);
768 u32 caps;
769
770 caps = query_amp_caps(codec, nid, dir);
771 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
772 if (! caps) {
773 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
774 return -EINVAL;
775 }
776 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
777 uinfo->count = chs == 3 ? 2 : 1;
778 uinfo->value.integer.min = 0;
779 uinfo->value.integer.max = caps;
780 return 0;
781}
782
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100783int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
786 hda_nid_t nid = get_amp_nid(kcontrol);
787 int chs = get_amp_channels(kcontrol);
788 int dir = get_amp_direction(kcontrol);
789 int idx = get_amp_index(kcontrol);
790 long *valp = ucontrol->value.integer.value;
791
792 if (chs & 1)
793 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
794 if (chs & 2)
795 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
796 return 0;
797}
798
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100799int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
802 hda_nid_t nid = get_amp_nid(kcontrol);
803 int chs = get_amp_channels(kcontrol);
804 int dir = get_amp_direction(kcontrol);
805 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 long *valp = ucontrol->value.integer.value;
807 int change = 0;
808
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200809 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200810 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
811 0x7f, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200812 valp++;
813 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200814 if (chs & 2)
815 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200816 0x7f, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return change;
818}
819
820/* switch */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100821int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int chs = get_amp_channels(kcontrol);
824
825 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
826 uinfo->count = chs == 3 ? 2 : 1;
827 uinfo->value.integer.min = 0;
828 uinfo->value.integer.max = 1;
829 return 0;
830}
831
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100832int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
835 hda_nid_t nid = get_amp_nid(kcontrol);
836 int chs = get_amp_channels(kcontrol);
837 int dir = get_amp_direction(kcontrol);
838 int idx = get_amp_index(kcontrol);
839 long *valp = ucontrol->value.integer.value;
840
841 if (chs & 1)
842 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
843 if (chs & 2)
844 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
845 return 0;
846}
847
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100848int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
851 hda_nid_t nid = get_amp_nid(kcontrol);
852 int chs = get_amp_channels(kcontrol);
853 int dir = get_amp_direction(kcontrol);
854 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 long *valp = ucontrol->value.integer.value;
856 int change = 0;
857
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200858 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200859 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
860 0x80, *valp ? 0 : 0x80);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200861 valp++;
862 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +0200863 if (chs & 2)
864 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Nicolas Grazianob9f5a892005-07-29 12:17:20 +0200865 0x80, *valp ? 0 : 0x80);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return change;
868}
869
870/*
Takashi Iwai985be542005-11-02 18:26:49 +0100871 * bound volume controls
872 *
873 * bind multiple volumes (# indices, from 0)
874 */
875
876#define AMP_VAL_IDX_SHIFT 19
877#define AMP_VAL_IDX_MASK (0x0f<<19)
878
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100879int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100880{
881 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
882 unsigned long pval;
883 int err;
884
Ingo Molnar62932df2006-01-16 16:34:20 +0100885 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100886 pval = kcontrol->private_value;
887 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
888 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
889 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100890 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100891 return err;
892}
893
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100894int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +0100895{
896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
897 unsigned long pval;
898 int i, indices, err = 0, change = 0;
899
Ingo Molnar62932df2006-01-16 16:34:20 +0100900 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
Takashi Iwai985be542005-11-02 18:26:49 +0100901 pval = kcontrol->private_value;
902 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
903 for (i = 0; i < indices; i++) {
904 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
905 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
906 if (err < 0)
907 break;
908 change |= err;
909 }
910 kcontrol->private_value = pval;
Ingo Molnar62932df2006-01-16 16:34:20 +0100911 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +0100912 return err < 0 ? err : change;
913}
914
915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * SPDIF out controls
917 */
918
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100919static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
922 uinfo->count = 1;
923 return 0;
924}
925
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100926static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
929 IEC958_AES0_NONAUDIO |
930 IEC958_AES0_CON_EMPHASIS_5015 |
931 IEC958_AES0_CON_NOT_COPYRIGHT;
932 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
933 IEC958_AES1_CON_ORIGINAL;
934 return 0;
935}
936
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100937static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
940 IEC958_AES0_NONAUDIO |
941 IEC958_AES0_PRO_EMPHASIS_5015;
942 return 0;
943}
944
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100945static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
948
949 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
950 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
951 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
952 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
953
954 return 0;
955}
956
957/* convert from SPDIF status bits to HDA SPDIF bits
958 * bit 0 (DigEn) is always set zero (to be filled later)
959 */
960static unsigned short convert_from_spdif_status(unsigned int sbits)
961{
962 unsigned short val = 0;
963
964 if (sbits & IEC958_AES0_PROFESSIONAL)
965 val |= 1 << 6;
966 if (sbits & IEC958_AES0_NONAUDIO)
967 val |= 1 << 5;
968 if (sbits & IEC958_AES0_PROFESSIONAL) {
969 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
970 val |= 1 << 3;
971 } else {
972 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
973 val |= 1 << 3;
974 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
975 val |= 1 << 4;
976 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
977 val |= 1 << 7;
978 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
979 }
980 return val;
981}
982
983/* convert to SPDIF status bits from HDA SPDIF bits
984 */
985static unsigned int convert_to_spdif_status(unsigned short val)
986{
987 unsigned int sbits = 0;
988
989 if (val & (1 << 5))
990 sbits |= IEC958_AES0_NONAUDIO;
991 if (val & (1 << 6))
992 sbits |= IEC958_AES0_PROFESSIONAL;
993 if (sbits & IEC958_AES0_PROFESSIONAL) {
994 if (sbits & (1 << 3))
995 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
996 } else {
997 if (val & (1 << 3))
998 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
999 if (! (val & (1 << 4)))
1000 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1001 if (val & (1 << 7))
1002 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1003 sbits |= val & (0x7f << 8);
1004 }
1005 return sbits;
1006}
1007
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001008static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1011 hda_nid_t nid = kcontrol->private_value;
1012 unsigned short val;
1013 int change;
1014
Ingo Molnar62932df2006-01-16 16:34:20 +01001015 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 codec->spdif_status = ucontrol->value.iec958.status[0] |
1017 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1018 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1019 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1020 val = convert_from_spdif_status(codec->spdif_status);
1021 val |= codec->spdif_ctls & 1;
1022 change = codec->spdif_ctls != val;
1023 codec->spdif_ctls = val;
1024
1025 if (change || codec->in_resume) {
1026 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1027 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1028 }
1029
Ingo Molnar62932df2006-01-16 16:34:20 +01001030 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return change;
1032}
1033
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001034static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1037 uinfo->count = 1;
1038 uinfo->value.integer.min = 0;
1039 uinfo->value.integer.max = 1;
1040 return 0;
1041}
1042
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001043static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1046
1047 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1048 return 0;
1049}
1050
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001051static int snd_hda_spdif_out_switch_put(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 hda_nid_t nid = kcontrol->private_value;
1055 unsigned short val;
1056 int change;
1057
Ingo Molnar62932df2006-01-16 16:34:20 +01001058 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 val = codec->spdif_ctls & ~1;
1060 if (ucontrol->value.integer.value[0])
1061 val |= 1;
1062 change = codec->spdif_ctls != val;
1063 if (change || codec->in_resume) {
1064 codec->spdif_ctls = val;
1065 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1066 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1067 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1068 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1069 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001070 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return change;
1072}
1073
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001074static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 {
1076 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1077 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1078 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1079 .info = snd_hda_spdif_mask_info,
1080 .get = snd_hda_spdif_cmask_get,
1081 },
1082 {
1083 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1084 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1085 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1086 .info = snd_hda_spdif_mask_info,
1087 .get = snd_hda_spdif_pmask_get,
1088 },
1089 {
1090 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1091 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1092 .info = snd_hda_spdif_mask_info,
1093 .get = snd_hda_spdif_default_get,
1094 .put = snd_hda_spdif_default_put,
1095 },
1096 {
1097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1098 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1099 .info = snd_hda_spdif_out_switch_info,
1100 .get = snd_hda_spdif_out_switch_get,
1101 .put = snd_hda_spdif_out_switch_put,
1102 },
1103 { } /* end */
1104};
1105
1106/**
1107 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1108 * @codec: the HDA codec
1109 * @nid: audio out widget NID
1110 *
1111 * Creates controls related with the SPDIF output.
1112 * Called from each patch supporting the SPDIF out.
1113 *
1114 * Returns 0 if successful, or a negative error code.
1115 */
1116int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1117{
1118 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001119 struct snd_kcontrol *kctl;
1120 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1123 kctl = snd_ctl_new1(dig_mix, codec);
1124 kctl->private_value = nid;
1125 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1126 return err;
1127 }
1128 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1129 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1130 return 0;
1131}
1132
1133/*
1134 * SPDIF input
1135 */
1136
1137#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1138
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001139static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1142
1143 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1144 return 0;
1145}
1146
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001147static int snd_hda_spdif_in_switch_put(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 hda_nid_t nid = kcontrol->private_value;
1151 unsigned int val = !!ucontrol->value.integer.value[0];
1152 int change;
1153
Ingo Molnar62932df2006-01-16 16:34:20 +01001154 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 change = codec->spdif_in_enable != val;
1156 if (change || codec->in_resume) {
1157 codec->spdif_in_enable = val;
1158 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1159 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001160 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return change;
1162}
1163
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001164static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1167 hda_nid_t nid = kcontrol->private_value;
1168 unsigned short val;
1169 unsigned int sbits;
1170
1171 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1172 sbits = convert_to_spdif_status(val);
1173 ucontrol->value.iec958.status[0] = sbits;
1174 ucontrol->value.iec958.status[1] = sbits >> 8;
1175 ucontrol->value.iec958.status[2] = sbits >> 16;
1176 ucontrol->value.iec958.status[3] = sbits >> 24;
1177 return 0;
1178}
1179
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001180static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 {
1182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1183 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1184 .info = snd_hda_spdif_in_switch_info,
1185 .get = snd_hda_spdif_in_switch_get,
1186 .put = snd_hda_spdif_in_switch_put,
1187 },
1188 {
1189 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1192 .info = snd_hda_spdif_mask_info,
1193 .get = snd_hda_spdif_in_status_get,
1194 },
1195 { } /* end */
1196};
1197
1198/**
1199 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1200 * @codec: the HDA codec
1201 * @nid: audio in widget NID
1202 *
1203 * Creates controls related with the SPDIF input.
1204 * Called from each patch supporting the SPDIF in.
1205 *
1206 * Returns 0 if successful, or a negative error code.
1207 */
1208int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1209{
1210 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001211 struct snd_kcontrol *kctl;
1212 struct snd_kcontrol_new *dig_mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1215 kctl = snd_ctl_new1(dig_mix, codec);
1216 kctl->private_value = nid;
1217 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1218 return err;
1219 }
1220 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1221 return 0;
1222}
1223
1224
Takashi Iwai54d17402005-11-21 16:33:22 +01001225/*
1226 * set power state of the codec
1227 */
1228static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1229 unsigned int power_state)
1230{
1231 hda_nid_t nid, nid_start;
1232 int nodes;
1233
1234 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1235 power_state);
1236
1237 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1238 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1239 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1240 snd_hda_codec_write(codec, nid, 0,
1241 AC_VERB_SET_POWER_STATE,
1242 power_state);
1243 }
1244
1245 if (power_state == AC_PWRST_D0)
1246 msleep(10);
1247}
1248
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/**
1251 * snd_hda_build_controls - build mixer controls
1252 * @bus: the BUS
1253 *
1254 * Creates mixer controls for each codec included in the bus.
1255 *
1256 * Returns 0 if successful, otherwise a negative error code.
1257 */
1258int snd_hda_build_controls(struct hda_bus *bus)
1259{
1260 struct list_head *p;
1261
1262 /* build controls */
1263 list_for_each(p, &bus->codec_list) {
1264 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1265 int err;
1266 if (! codec->patch_ops.build_controls)
1267 continue;
1268 err = codec->patch_ops.build_controls(codec);
1269 if (err < 0)
1270 return err;
1271 }
1272
1273 /* initialize */
1274 list_for_each(p, &bus->codec_list) {
1275 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1276 int err;
Takashi Iwai54d17402005-11-21 16:33:22 +01001277 hda_set_power_state(codec,
1278 codec->afg ? codec->afg : codec->mfg,
1279 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (! codec->patch_ops.init)
1281 continue;
1282 err = codec->patch_ops.init(codec);
1283 if (err < 0)
1284 return err;
1285 }
1286 return 0;
1287}
1288
1289
1290/*
1291 * stream formats
1292 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001293struct hda_rate_tbl {
1294 unsigned int hz;
1295 unsigned int alsa_bits;
1296 unsigned int hda_fmt;
1297};
1298
1299static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001301
1302 /* autodetected value used in snd_hda_query_supported_pcm */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1304 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1305 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1306 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1307 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1308 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1309 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1310 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1311 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1312 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1313 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02001314
1315 /* not autodetected value */
1316 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02001317
1318 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319};
1320
1321/**
1322 * snd_hda_calc_stream_format - calculate format bitset
1323 * @rate: the sample rate
1324 * @channels: the number of channels
1325 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1326 * @maxbps: the max. bps
1327 *
1328 * Calculate the format bitset from the given rate, channels and th PCM format.
1329 *
1330 * Return zero if invalid.
1331 */
1332unsigned int snd_hda_calc_stream_format(unsigned int rate,
1333 unsigned int channels,
1334 unsigned int format,
1335 unsigned int maxbps)
1336{
1337 int i;
1338 unsigned int val = 0;
1339
Takashi Iwaibefdf312005-08-22 13:57:55 +02001340 for (i = 0; rate_bits[i].hz; i++)
1341 if (rate_bits[i].hz == rate) {
1342 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 break;
1344 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001345 if (! rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 snd_printdd("invalid rate %d\n", rate);
1347 return 0;
1348 }
1349
1350 if (channels == 0 || channels > 8) {
1351 snd_printdd("invalid channels %d\n", channels);
1352 return 0;
1353 }
1354 val |= channels - 1;
1355
1356 switch (snd_pcm_format_width(format)) {
1357 case 8: val |= 0x00; break;
1358 case 16: val |= 0x10; break;
1359 case 20:
1360 case 24:
1361 case 32:
1362 if (maxbps >= 32)
1363 val |= 0x40;
1364 else if (maxbps >= 24)
1365 val |= 0x30;
1366 else
1367 val |= 0x20;
1368 break;
1369 default:
1370 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1371 return 0;
1372 }
1373
1374 return val;
1375}
1376
1377/**
1378 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1379 * @codec: the HDA codec
1380 * @nid: NID to query
1381 * @ratesp: the pointer to store the detected rate bitflags
1382 * @formatsp: the pointer to store the detected formats
1383 * @bpsp: the pointer to store the detected format widths
1384 *
1385 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1386 * or @bsps argument is ignored.
1387 *
1388 * Returns 0 if successful, otherwise a negative error code.
1389 */
1390int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1391 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1392{
1393 int i;
1394 unsigned int val, streams;
1395
1396 val = 0;
1397 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001398 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1400 if (val == -1)
1401 return -EIO;
1402 }
1403 if (! val)
1404 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1405
1406 if (ratesp) {
1407 u32 rates = 0;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001408 for (i = 0; rate_bits[i].hz; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02001410 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 *ratesp = rates;
1413 }
1414
1415 if (formatsp || bpsp) {
1416 u64 formats = 0;
1417 unsigned int bps;
1418 unsigned int wcaps;
1419
Takashi Iwai54d17402005-11-21 16:33:22 +01001420 wcaps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1422 if (streams == -1)
1423 return -EIO;
1424 if (! streams) {
1425 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1426 if (streams == -1)
1427 return -EIO;
1428 }
1429
1430 bps = 0;
1431 if (streams & AC_SUPFMT_PCM) {
1432 if (val & AC_SUPPCM_BITS_8) {
1433 formats |= SNDRV_PCM_FMTBIT_U8;
1434 bps = 8;
1435 }
1436 if (val & AC_SUPPCM_BITS_16) {
1437 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1438 bps = 16;
1439 }
1440 if (wcaps & AC_WCAP_DIGITAL) {
1441 if (val & AC_SUPPCM_BITS_32)
1442 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1443 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1444 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1445 if (val & AC_SUPPCM_BITS_24)
1446 bps = 24;
1447 else if (val & AC_SUPPCM_BITS_20)
1448 bps = 20;
1449 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1450 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1451 if (val & AC_SUPPCM_BITS_32)
1452 bps = 32;
1453 else if (val & AC_SUPPCM_BITS_20)
1454 bps = 20;
1455 else if (val & AC_SUPPCM_BITS_24)
1456 bps = 24;
1457 }
1458 }
1459 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1460 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1461 bps = 32;
1462 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1463 /* temporary hack: we have still no proper support
1464 * for the direct AC3 stream...
1465 */
1466 formats |= SNDRV_PCM_FMTBIT_U8;
1467 bps = 8;
1468 }
1469 if (formatsp)
1470 *formatsp = formats;
1471 if (bpsp)
1472 *bpsp = bps;
1473 }
1474
1475 return 0;
1476}
1477
1478/**
1479 * snd_hda_is_supported_format - check whether the given node supports the format val
1480 *
1481 * Returns 1 if supported, 0 if not.
1482 */
1483int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1484 unsigned int format)
1485{
1486 int i;
1487 unsigned int val = 0, rate, stream;
1488
1489 if (nid != codec->afg &&
Takashi Iwai54d17402005-11-21 16:33:22 +01001490 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1492 if (val == -1)
1493 return 0;
1494 }
1495 if (! val) {
1496 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1497 if (val == -1)
1498 return 0;
1499 }
1500
1501 rate = format & 0xff00;
Takashi Iwaibefdf312005-08-22 13:57:55 +02001502 for (i = 0; rate_bits[i].hz; i++)
1503 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (val & (1 << i))
1505 break;
1506 return 0;
1507 }
Takashi Iwaibefdf312005-08-22 13:57:55 +02001508 if (! rate_bits[i].hz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510
1511 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1512 if (stream == -1)
1513 return 0;
1514 if (! stream && nid != codec->afg)
1515 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1516 if (! stream || stream == -1)
1517 return 0;
1518
1519 if (stream & AC_SUPFMT_PCM) {
1520 switch (format & 0xf0) {
1521 case 0x00:
1522 if (! (val & AC_SUPPCM_BITS_8))
1523 return 0;
1524 break;
1525 case 0x10:
1526 if (! (val & AC_SUPPCM_BITS_16))
1527 return 0;
1528 break;
1529 case 0x20:
1530 if (! (val & AC_SUPPCM_BITS_20))
1531 return 0;
1532 break;
1533 case 0x30:
1534 if (! (val & AC_SUPPCM_BITS_24))
1535 return 0;
1536 break;
1537 case 0x40:
1538 if (! (val & AC_SUPPCM_BITS_32))
1539 return 0;
1540 break;
1541 default:
1542 return 0;
1543 }
1544 } else {
1545 /* FIXME: check for float32 and AC3? */
1546 }
1547
1548 return 1;
1549}
1550
1551/*
1552 * PCM stuff
1553 */
1554static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1555 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001556 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 return 0;
1559}
1560
1561static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1562 struct hda_codec *codec,
1563 unsigned int stream_tag,
1564 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001565 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1568 return 0;
1569}
1570
1571static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1572 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-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, 0, 0, 0);
1576 return 0;
1577}
1578
1579static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1580{
1581 if (info->nid) {
1582 /* query support PCM information from the given NID */
1583 if (! info->rates || ! info->formats)
1584 snd_hda_query_supported_pcm(codec, info->nid,
1585 info->rates ? NULL : &info->rates,
1586 info->formats ? NULL : &info->formats,
1587 info->maxbps ? NULL : &info->maxbps);
1588 }
1589 if (info->ops.open == NULL)
1590 info->ops.open = hda_pcm_default_open_close;
1591 if (info->ops.close == NULL)
1592 info->ops.close = hda_pcm_default_open_close;
1593 if (info->ops.prepare == NULL) {
1594 snd_assert(info->nid, return -EINVAL);
1595 info->ops.prepare = hda_pcm_default_prepare;
1596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (info->ops.cleanup == NULL) {
1598 snd_assert(info->nid, return -EINVAL);
1599 info->ops.cleanup = hda_pcm_default_cleanup;
1600 }
1601 return 0;
1602}
1603
1604/**
1605 * snd_hda_build_pcms - build PCM information
1606 * @bus: the BUS
1607 *
1608 * Create PCM information for each codec included in the bus.
1609 *
1610 * The build_pcms codec patch is requested to set up codec->num_pcms and
1611 * codec->pcm_info properly. The array is referred by the top-level driver
1612 * to create its PCM instances.
1613 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1614 * callback.
1615 *
1616 * At least, substreams, channels_min and channels_max must be filled for
1617 * each stream. substreams = 0 indicates that the stream doesn't exist.
1618 * When rates and/or formats are zero, the supported values are queried
1619 * from the given nid. The nid is used also by the default ops.prepare
1620 * and ops.cleanup callbacks.
1621 *
1622 * The driver needs to call ops.open in its open callback. Similarly,
1623 * ops.close is supposed to be called in the close callback.
1624 * ops.prepare should be called in the prepare or hw_params callback
1625 * with the proper parameters for set up.
1626 * ops.cleanup should be called in hw_free for clean up of streams.
1627 *
1628 * This function returns 0 if successfull, or a negative error code.
1629 */
1630int snd_hda_build_pcms(struct hda_bus *bus)
1631{
1632 struct list_head *p;
1633
1634 list_for_each(p, &bus->codec_list) {
1635 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1636 unsigned int pcm, s;
1637 int err;
1638 if (! codec->patch_ops.build_pcms)
1639 continue;
1640 err = codec->patch_ops.build_pcms(codec);
1641 if (err < 0)
1642 return err;
1643 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1644 for (s = 0; s < 2; s++) {
1645 struct hda_pcm_stream *info;
1646 info = &codec->pcm_info[pcm].stream[s];
1647 if (! info->substreams)
1648 continue;
1649 err = set_pcm_default_values(codec, info);
1650 if (err < 0)
1651 return err;
1652 }
1653 }
1654 }
1655 return 0;
1656}
1657
1658
1659/**
1660 * snd_hda_check_board_config - compare the current codec with the config table
1661 * @codec: the HDA codec
1662 * @tbl: configuration table, terminated by null entries
1663 *
1664 * Compares the modelname or PCI subsystem id of the current codec with the
1665 * given configuration table. If a matching entry is found, returns its
1666 * config value (supposed to be 0 or positive).
1667 *
1668 * If no entries are matching, the function returns a negative value.
1669 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001670int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001672 const struct hda_board_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (codec->bus->modelname) {
Takashi Iwai72915482005-05-12 16:49:45 +02001675 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (c->modelname &&
1677 ! strcmp(codec->bus->modelname, c->modelname)) {
1678 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1679 return c->config;
1680 }
1681 }
1682 }
1683
1684 if (codec->bus->pci) {
1685 u16 subsystem_vendor, subsystem_device;
1686 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1687 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
Takashi Iwai72915482005-05-12 16:49:45 +02001688 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1689 if (c->pci_subvendor == subsystem_vendor &&
Takashi Iwai5ecd7022005-06-10 19:54:23 +02001690 (! c->pci_subdevice /* all match */||
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001691 (c->pci_subdevice == subsystem_device))) {
1692 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1693 subsystem_vendor, subsystem_device, c->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return c->config;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 }
1698 return -1;
1699}
1700
1701/**
1702 * snd_hda_add_new_ctls - create controls from the array
1703 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001704 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *
1706 * This helper function creates and add new controls in the given array.
1707 * The array must be terminated with an empty entry as terminator.
1708 *
1709 * Returns 0 if successful, or a negative error code.
1710 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001711int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int err;
1714
1715 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001716 struct snd_kcontrol *kctl;
1717 kctl = snd_ctl_new1(knew, codec);
1718 if (! kctl)
1719 return -ENOMEM;
1720 err = snd_ctl_add(codec->bus->card, kctl);
1721 if (err < 0) {
1722 if (! codec->addr)
1723 return err;
1724 kctl = snd_ctl_new1(knew, codec);
1725 if (! kctl)
1726 return -ENOMEM;
1727 kctl->id.device = codec->addr;
1728 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1729 return err;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 return 0;
1733}
1734
1735
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001736/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001737 * Channel mode helper
1738 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001739int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001740 const struct hda_channel_mode *chmode, int num_chmodes)
1741{
1742 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1743 uinfo->count = 1;
1744 uinfo->value.enumerated.items = num_chmodes;
1745 if (uinfo->value.enumerated.item >= num_chmodes)
1746 uinfo->value.enumerated.item = num_chmodes - 1;
1747 sprintf(uinfo->value.enumerated.name, "%dch",
1748 chmode[uinfo->value.enumerated.item].channels);
1749 return 0;
1750}
1751
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001752int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001753 const struct hda_channel_mode *chmode, int num_chmodes,
1754 int max_channels)
1755{
1756 int i;
1757
1758 for (i = 0; i < num_chmodes; i++) {
1759 if (max_channels == chmode[i].channels) {
1760 ucontrol->value.enumerated.item[0] = i;
1761 break;
1762 }
1763 }
1764 return 0;
1765}
1766
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001767int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001768 const struct hda_channel_mode *chmode, int num_chmodes,
1769 int *max_channelsp)
1770{
1771 unsigned int mode;
1772
1773 mode = ucontrol->value.enumerated.item[0];
1774 snd_assert(mode < num_chmodes, return -EINVAL);
Takashi Iwaib2ec6422005-11-24 16:05:04 +01001775 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01001776 return 0;
1777 /* change the current channel setting */
1778 *max_channelsp = chmode[mode].channels;
1779 if (chmode[mode].sequence)
1780 snd_hda_sequence_write(codec, chmode[mode].sequence);
1781 return 1;
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784/*
1785 * input MUX helper
1786 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001787int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 unsigned int index;
1790
1791 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1792 uinfo->count = 1;
1793 uinfo->value.enumerated.items = imux->num_items;
1794 index = uinfo->value.enumerated.item;
1795 if (index >= imux->num_items)
1796 index = imux->num_items - 1;
1797 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1798 return 0;
1799}
1800
1801int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001802 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 unsigned int *cur_val)
1804{
1805 unsigned int idx;
1806
1807 idx = ucontrol->value.enumerated.item[0];
1808 if (idx >= imux->num_items)
1809 idx = imux->num_items - 1;
1810 if (*cur_val == idx && ! codec->in_resume)
1811 return 0;
1812 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1813 imux->items[idx].index);
1814 *cur_val = idx;
1815 return 1;
1816}
1817
1818
1819/*
1820 * Multi-channel / digital-out PCM helper functions
1821 */
1822
1823/*
1824 * open the digital out in the exclusive mode
1825 */
1826int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1827{
Ingo Molnar62932df2006-01-16 16:34:20 +01001828 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (mout->dig_out_used) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001830 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -EBUSY; /* already being used */
1832 }
1833 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01001834 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
1838/*
1839 * release the digital out
1840 */
1841int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1842{
Ingo Molnar62932df2006-01-16 16:34:20 +01001843 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01001845 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
1847}
1848
1849/*
1850 * set up more restrictions for analog out
1851 */
1852int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001853 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 substream->runtime->hw.channels_max = mout->max_channels;
1856 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1857 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1858}
1859
1860/*
1861 * set up the i/o for analog out
1862 * when the digital out is available, copy the front out to digital out, too.
1863 */
1864int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1865 unsigned int stream_tag,
1866 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01001867 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 hda_nid_t *nids = mout->dac_nids;
1870 int chs = substream->runtime->channels;
1871 int i;
1872
Ingo Molnar62932df2006-01-16 16:34:20 +01001873 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1875 if (chs == 2 &&
1876 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1877 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1878 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1879 /* setup digital receiver */
1880 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1881 stream_tag, 0, format);
1882 } else {
1883 mout->dig_out_used = 0;
1884 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1885 }
1886 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001887 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /* front */
1890 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1891 if (mout->hp_nid)
1892 /* headphone out will just decode front left/right (stereo) */
1893 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1894 /* surrounds */
1895 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001896 if (chs >= (i + 1) * 2) /* independent out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1898 format);
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02001899 else /* copy front */
1900 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1901 format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 return 0;
1904}
1905
1906/*
1907 * clean up the setting for analog out
1908 */
1909int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1910{
1911 hda_nid_t *nids = mout->dac_nids;
1912 int i;
1913
1914 for (i = 0; i < mout->num_dacs; i++)
1915 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1916 if (mout->hp_nid)
1917 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
Ingo Molnar62932df2006-01-16 16:34:20 +01001918 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1920 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1921 mout->dig_out_used = 0;
1922 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001923 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001927/*
1928 * Helper for automatic ping configuration
1929 */
Kailang Yangdf694da2005-12-05 19:42:22 +01001930
1931static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
1932{
1933 for (; *list; list++)
1934 if (*list == nid)
1935 return 1;
1936 return 0;
1937}
1938
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001939/* parse all pin widgets and store the useful pin nids to cfg */
Kailang Yangdf694da2005-12-05 19:42:22 +01001940int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
1941 hda_nid_t *ignore_nids)
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001942{
1943 hda_nid_t nid, nid_start;
1944 int i, j, nodes;
1945 short seq, sequences[4], assoc_line_out;
1946
1947 memset(cfg, 0, sizeof(*cfg));
1948
1949 memset(sequences, 0, sizeof(sequences));
1950 assoc_line_out = 0;
1951
1952 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1953 for (nid = nid_start; nid < nodes + nid_start; nid++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001954 unsigned int wid_caps = get_wcaps(codec, nid);
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001955 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1956 unsigned int def_conf;
1957 short assoc, loc;
1958
1959 /* read all default configuration for pin complex */
1960 if (wid_type != AC_WID_PIN)
1961 continue;
Kailang Yangdf694da2005-12-05 19:42:22 +01001962 /* ignore the given nids (e.g. pc-beep returns error) */
1963 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
1964 continue;
1965
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001966 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
1967 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1968 continue;
1969 loc = get_defcfg_location(def_conf);
1970 switch (get_defcfg_device(def_conf)) {
1971 case AC_JACK_LINE_OUT:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001972 seq = get_defcfg_sequence(def_conf);
1973 assoc = get_defcfg_association(def_conf);
1974 if (! assoc)
1975 continue;
1976 if (! assoc_line_out)
1977 assoc_line_out = assoc;
1978 else if (assoc_line_out != assoc)
1979 continue;
1980 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
1981 continue;
1982 cfg->line_out_pins[cfg->line_outs] = nid;
1983 sequences[cfg->line_outs] = seq;
1984 cfg->line_outs++;
1985 break;
Takashi Iwai8d88bc32005-11-17 11:09:23 +01001986 case AC_JACK_SPEAKER:
1987 cfg->speaker_pin = nid;
1988 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02001989 case AC_JACK_HP_OUT:
1990 cfg->hp_pin = nid;
1991 break;
1992 case AC_JACK_MIC_IN:
1993 if (loc == AC_JACK_LOC_FRONT)
1994 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
1995 else
1996 cfg->input_pins[AUTO_PIN_MIC] = nid;
1997 break;
1998 case AC_JACK_LINE_IN:
1999 if (loc == AC_JACK_LOC_FRONT)
2000 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2001 else
2002 cfg->input_pins[AUTO_PIN_LINE] = nid;
2003 break;
2004 case AC_JACK_CD:
2005 cfg->input_pins[AUTO_PIN_CD] = nid;
2006 break;
2007 case AC_JACK_AUX:
2008 cfg->input_pins[AUTO_PIN_AUX] = nid;
2009 break;
2010 case AC_JACK_SPDIF_OUT:
2011 cfg->dig_out_pin = nid;
2012 break;
2013 case AC_JACK_SPDIF_IN:
2014 cfg->dig_in_pin = nid;
2015 break;
2016 }
2017 }
2018
2019 /* sort by sequence */
2020 for (i = 0; i < cfg->line_outs; i++)
2021 for (j = i + 1; j < cfg->line_outs; j++)
2022 if (sequences[i] > sequences[j]) {
2023 seq = sequences[i];
2024 sequences[i] = sequences[j];
2025 sequences[j] = seq;
2026 nid = cfg->line_out_pins[i];
2027 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2028 cfg->line_out_pins[j] = nid;
2029 }
2030
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002031 /* Reorder the surround channels
2032 * ALSA sequence is front/surr/clfe/side
2033 * HDA sequence is:
2034 * 4-ch: front/surr => OK as it is
2035 * 6-ch: front/clfe/surr
2036 * 8-ch: front/clfe/side/surr
2037 */
2038 switch (cfg->line_outs) {
2039 case 3:
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002040 nid = cfg->line_out_pins[1];
2041 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2042 cfg->line_out_pins[2] = nid;
Takashi Iwaicb8e2f82005-07-29 11:54:32 +02002043 break;
2044 case 4:
2045 nid = cfg->line_out_pins[1];
2046 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2047 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2048 cfg->line_out_pins[2] = nid;
2049 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +02002050 }
2051
2052 return 0;
2053}
2054
Takashi Iwai4a471b72005-12-07 13:56:29 +01002055/* labels for input pins */
2056const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2057 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2058};
2059
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061#ifdef CONFIG_PM
2062/*
2063 * power management
2064 */
2065
2066/**
2067 * snd_hda_suspend - suspend the codecs
2068 * @bus: the HDA bus
2069 * @state: suspsend state
2070 *
2071 * Returns 0 if successful.
2072 */
2073int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2074{
2075 struct list_head *p;
2076
2077 /* FIXME: should handle power widget capabilities */
2078 list_for_each(p, &bus->codec_list) {
2079 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2080 if (codec->patch_ops.suspend)
2081 codec->patch_ops.suspend(codec, state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002082 hda_set_power_state(codec,
2083 codec->afg ? codec->afg : codec->mfg,
2084 AC_PWRST_D3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 return 0;
2087}
2088
2089/**
2090 * snd_hda_resume - resume the codecs
2091 * @bus: the HDA bus
2092 * @state: resume state
2093 *
2094 * Returns 0 if successful.
2095 */
2096int snd_hda_resume(struct hda_bus *bus)
2097{
2098 struct list_head *p;
2099
2100 list_for_each(p, &bus->codec_list) {
2101 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
Takashi Iwai54d17402005-11-21 16:33:22 +01002102 hda_set_power_state(codec,
2103 codec->afg ? codec->afg : codec->mfg,
2104 AC_PWRST_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (codec->patch_ops.resume)
2106 codec->patch_ops.resume(codec);
2107 }
2108 return 0;
2109}
2110
2111/**
2112 * snd_hda_resume_ctls - resume controls in the new control list
2113 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002114 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002116 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * originally for snd_hda_add_new_ctls().
2118 * The array must be terminated with an empty entry as terminator.
2119 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002120int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002122 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 val = kmalloc(sizeof(*val), GFP_KERNEL);
2125 if (! val)
2126 return -ENOMEM;
2127 codec->in_resume = 1;
2128 for (; knew->name; knew++) {
2129 int i, count;
2130 count = knew->count ? knew->count : 1;
2131 for (i = 0; i < count; i++) {
2132 memset(val, 0, sizeof(*val));
2133 val->id.iface = knew->iface;
2134 val->id.device = knew->device;
2135 val->id.subdevice = knew->subdevice;
2136 strcpy(val->id.name, knew->name);
2137 val->id.index = knew->index ? knew->index : i;
2138 /* Assume that get callback reads only from cache,
2139 * not accessing to the real hardware
2140 */
2141 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2142 continue;
2143 snd_ctl_elem_write(codec->bus->card, NULL, val);
2144 }
2145 }
2146 codec->in_resume = 0;
2147 kfree(val);
2148 return 0;
2149}
2150
2151/**
2152 * snd_hda_resume_spdif_out - resume the digital out
2153 * @codec: the HDA codec
2154 */
2155int snd_hda_resume_spdif_out(struct hda_codec *codec)
2156{
2157 return snd_hda_resume_ctls(codec, dig_mixes);
2158}
2159
2160/**
2161 * snd_hda_resume_spdif_in - resume the digital in
2162 * @codec: the HDA codec
2163 */
2164int snd_hda_resume_spdif_in(struct hda_codec *codec)
2165{
2166 return snd_hda_resume_ctls(codec, dig_in_ctls);
2167}
2168#endif
2169
2170/*
2171 * symbols exported for controller modules
2172 */
2173EXPORT_SYMBOL(snd_hda_codec_read);
2174EXPORT_SYMBOL(snd_hda_codec_write);
2175EXPORT_SYMBOL(snd_hda_sequence_write);
2176EXPORT_SYMBOL(snd_hda_get_sub_nodes);
2177EXPORT_SYMBOL(snd_hda_queue_unsol_event);
2178EXPORT_SYMBOL(snd_hda_bus_new);
2179EXPORT_SYMBOL(snd_hda_codec_new);
2180EXPORT_SYMBOL(snd_hda_codec_setup_stream);
2181EXPORT_SYMBOL(snd_hda_calc_stream_format);
2182EXPORT_SYMBOL(snd_hda_build_pcms);
2183EXPORT_SYMBOL(snd_hda_build_controls);
2184#ifdef CONFIG_PM
2185EXPORT_SYMBOL(snd_hda_suspend);
2186EXPORT_SYMBOL(snd_hda_resume);
2187#endif
2188
2189/*
2190 * INIT part
2191 */
2192
2193static int __init alsa_hda_init(void)
2194{
2195 return 0;
2196}
2197
2198static void __exit alsa_hda_exit(void)
2199{
2200}
2201
2202module_init(alsa_hda_init)
2203module_exit(alsa_hda_exit)