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