]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/pci/hda/hda_codec.c
ALSA: hda: fix oopses in snd-hda-intel after digital slave support additions
[linux-2.6-omap-h63xx.git] / sound / pci / hda / hda_codec.c
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 <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33 #include <sound/hda_hwdep.h>
34 #include "hda_patch.h"  /* codec presets */
35
36 #ifdef CONFIG_SND_HDA_POWER_SAVE
37 /* define this option here to hide as static */
38 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
39 module_param(power_save, int, 0644);
40 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41                  "(in second, 0 = disable).");
42 #endif
43
44 /*
45  * vendor / preset table
46  */
47
48 struct hda_vendor_id {
49         unsigned int id;
50         const char *name;
51 };
52
53 /* codec vendor labels */
54 static struct hda_vendor_id hda_vendor_ids[] = {
55         { 0x1002, "ATI" },
56         { 0x1057, "Motorola" },
57         { 0x1095, "Silicon Image" },
58         { 0x10ec, "Realtek" },
59         { 0x1106, "VIA" },
60         { 0x111d, "IDT" },
61         { 0x11c1, "LSI" },
62         { 0x11d4, "Analog Devices" },
63         { 0x13f6, "C-Media" },
64         { 0x14f1, "Conexant" },
65         { 0x17e8, "Chrontel" },
66         { 0x1854, "LG" },
67         { 0x434d, "C-Media" },
68         { 0x8384, "SigmaTel" },
69         {} /* terminator */
70 };
71
72 static const struct hda_codec_preset *hda_preset_tables[] = {
73 #ifdef CONFIG_SND_HDA_CODEC_REALTEK
74         snd_hda_preset_realtek,
75 #endif
76 #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77         snd_hda_preset_cmedia,
78 #endif
79 #ifdef CONFIG_SND_HDA_CODEC_ANALOG
80         snd_hda_preset_analog,
81 #endif
82 #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83         snd_hda_preset_sigmatel,
84 #endif
85 #ifdef CONFIG_SND_HDA_CODEC_SI3054
86         snd_hda_preset_si3054,
87 #endif
88 #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89         snd_hda_preset_atihdmi,
90 #endif
91 #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92         snd_hda_preset_conexant,
93 #endif
94 #ifdef CONFIG_SND_HDA_CODEC_VIA
95         snd_hda_preset_via,
96 #endif
97         NULL
98 };
99
100 #ifdef CONFIG_SND_HDA_POWER_SAVE
101 static void hda_power_work(struct work_struct *work);
102 static void hda_keep_power_on(struct hda_codec *codec);
103 #else
104 static inline void hda_keep_power_on(struct hda_codec *codec) {}
105 #endif
106
107 /**
108  * snd_hda_codec_read - send a command and get the response
109  * @codec: the HDA codec
110  * @nid: NID to send the command
111  * @direct: direct flag
112  * @verb: the verb to send
113  * @parm: the parameter for the verb
114  *
115  * Send a single command and read the corresponding response.
116  *
117  * Returns the obtained response value, or -1 for an error.
118  */
119 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
120                                 int direct,
121                                 unsigned int verb, unsigned int parm)
122 {
123         unsigned int res;
124         snd_hda_power_up(codec);
125         mutex_lock(&codec->bus->cmd_mutex);
126         if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
127                 res = codec->bus->ops.get_response(codec);
128         else
129                 res = (unsigned int)-1;
130         mutex_unlock(&codec->bus->cmd_mutex);
131         snd_hda_power_down(codec);
132         return res;
133 }
134
135 /**
136  * snd_hda_codec_write - send a single command without waiting for response
137  * @codec: the HDA codec
138  * @nid: NID to send the command
139  * @direct: direct flag
140  * @verb: the verb to send
141  * @parm: the parameter for the verb
142  *
143  * Send a single command without waiting for response.
144  *
145  * Returns 0 if successful, or a negative error code.
146  */
147 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
148                          unsigned int verb, unsigned int parm)
149 {
150         int err;
151         snd_hda_power_up(codec);
152         mutex_lock(&codec->bus->cmd_mutex);
153         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
154         mutex_unlock(&codec->bus->cmd_mutex);
155         snd_hda_power_down(codec);
156         return err;
157 }
158
159 /**
160  * snd_hda_sequence_write - sequence writes
161  * @codec: the HDA codec
162  * @seq: VERB array to send
163  *
164  * Send the commands sequentially from the given array.
165  * The array must be terminated with NID=0.
166  */
167 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
168 {
169         for (; seq->nid; seq++)
170                 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
171 }
172
173 /**
174  * snd_hda_get_sub_nodes - get the range of sub nodes
175  * @codec: the HDA codec
176  * @nid: NID to parse
177  * @start_id: the pointer to store the start NID
178  *
179  * Parse the NID and store the start NID of its sub-nodes.
180  * Returns the number of sub-nodes.
181  */
182 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
183                           hda_nid_t *start_id)
184 {
185         unsigned int parm;
186
187         parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
188         if (parm == -1)
189                 return 0;
190         *start_id = (parm >> 16) & 0x7fff;
191         return (int)(parm & 0x7fff);
192 }
193
194 /**
195  * snd_hda_get_connections - get connection list
196  * @codec: the HDA codec
197  * @nid: NID to parse
198  * @conn_list: connection list array
199  * @max_conns: max. number of connections to store
200  *
201  * Parses the connection list of the given widget and stores the list
202  * of NIDs.
203  *
204  * Returns the number of connections, or a negative error code.
205  */
206 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
207                             hda_nid_t *conn_list, int max_conns)
208 {
209         unsigned int parm;
210         int i, conn_len, conns;
211         unsigned int shift, num_elems, mask;
212         hda_nid_t prev_nid;
213
214         if (snd_BUG_ON(!conn_list || max_conns <= 0))
215                 return -EINVAL;
216
217         parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
218         if (parm & AC_CLIST_LONG) {
219                 /* long form */
220                 shift = 16;
221                 num_elems = 2;
222         } else {
223                 /* short form */
224                 shift = 8;
225                 num_elems = 4;
226         }
227         conn_len = parm & AC_CLIST_LENGTH;
228         mask = (1 << (shift-1)) - 1;
229
230         if (!conn_len)
231                 return 0; /* no connection */
232
233         if (conn_len == 1) {
234                 /* single connection */
235                 parm = snd_hda_codec_read(codec, nid, 0,
236                                           AC_VERB_GET_CONNECT_LIST, 0);
237                 conn_list[0] = parm & mask;
238                 return 1;
239         }
240
241         /* multi connection */
242         conns = 0;
243         prev_nid = 0;
244         for (i = 0; i < conn_len; i++) {
245                 int range_val;
246                 hda_nid_t val, n;
247
248                 if (i % num_elems == 0)
249                         parm = snd_hda_codec_read(codec, nid, 0,
250                                                   AC_VERB_GET_CONNECT_LIST, i);
251                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
252                 val = parm & mask;
253                 parm >>= shift;
254                 if (range_val) {
255                         /* ranges between the previous and this one */
256                         if (!prev_nid || prev_nid >= val) {
257                                 snd_printk(KERN_WARNING "hda_codec: "
258                                            "invalid dep_range_val %x:%x\n",
259                                            prev_nid, val);
260                                 continue;
261                         }
262                         for (n = prev_nid + 1; n <= val; n++) {
263                                 if (conns >= max_conns) {
264                                         snd_printk(KERN_ERR
265                                                    "Too many connections\n");
266                                         return -EINVAL;
267                                 }
268                                 conn_list[conns++] = n;
269                         }
270                 } else {
271                         if (conns >= max_conns) {
272                                 snd_printk(KERN_ERR "Too many connections\n");
273                                 return -EINVAL;
274                         }
275                         conn_list[conns++] = val;
276                 }
277                 prev_nid = val;
278         }
279         return conns;
280 }
281
282
283 /**
284  * snd_hda_queue_unsol_event - add an unsolicited event to queue
285  * @bus: the BUS
286  * @res: unsolicited event (lower 32bit of RIRB entry)
287  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
288  *
289  * Adds the given event to the queue.  The events are processed in
290  * the workqueue asynchronously.  Call this function in the interrupt
291  * hanlder when RIRB receives an unsolicited event.
292  *
293  * Returns 0 if successful, or a negative error code.
294  */
295 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
296 {
297         struct hda_bus_unsolicited *unsol;
298         unsigned int wp;
299
300         unsol = bus->unsol;
301         if (!unsol)
302                 return 0;
303
304         wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
305         unsol->wp = wp;
306
307         wp <<= 1;
308         unsol->queue[wp] = res;
309         unsol->queue[wp + 1] = res_ex;
310
311         schedule_work(&unsol->work);
312
313         return 0;
314 }
315
316 /*
317  * process queueud unsolicited events
318  */
319 static void process_unsol_events(struct work_struct *work)
320 {
321         struct hda_bus_unsolicited *unsol =
322                 container_of(work, struct hda_bus_unsolicited, work);
323         struct hda_bus *bus = unsol->bus;
324         struct hda_codec *codec;
325         unsigned int rp, caddr, res;
326
327         while (unsol->rp != unsol->wp) {
328                 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
329                 unsol->rp = rp;
330                 rp <<= 1;
331                 res = unsol->queue[rp];
332                 caddr = unsol->queue[rp + 1];
333                 if (!(caddr & (1 << 4))) /* no unsolicited event? */
334                         continue;
335                 codec = bus->caddr_tbl[caddr & 0x0f];
336                 if (codec && codec->patch_ops.unsol_event)
337                         codec->patch_ops.unsol_event(codec, res);
338         }
339 }
340
341 /*
342  * initialize unsolicited queue
343  */
344 static int __devinit init_unsol_queue(struct hda_bus *bus)
345 {
346         struct hda_bus_unsolicited *unsol;
347
348         if (bus->unsol) /* already initialized */
349                 return 0;
350
351         unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
352         if (!unsol) {
353                 snd_printk(KERN_ERR "hda_codec: "
354                            "can't allocate unsolicited queue\n");
355                 return -ENOMEM;
356         }
357         INIT_WORK(&unsol->work, process_unsol_events);
358         unsol->bus = bus;
359         bus->unsol = unsol;
360         return 0;
361 }
362
363 /*
364  * destructor
365  */
366 static void snd_hda_codec_free(struct hda_codec *codec);
367
368 static int snd_hda_bus_free(struct hda_bus *bus)
369 {
370         struct hda_codec *codec, *n;
371
372         if (!bus)
373                 return 0;
374         if (bus->unsol) {
375                 flush_scheduled_work();
376                 kfree(bus->unsol);
377         }
378         list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
379                 snd_hda_codec_free(codec);
380         }
381         if (bus->ops.private_free)
382                 bus->ops.private_free(bus);
383         kfree(bus);
384         return 0;
385 }
386
387 static int snd_hda_bus_dev_free(struct snd_device *device)
388 {
389         struct hda_bus *bus = device->device_data;
390         return snd_hda_bus_free(bus);
391 }
392
393 /**
394  * snd_hda_bus_new - create a HDA bus
395  * @card: the card entry
396  * @temp: the template for hda_bus information
397  * @busp: the pointer to store the created bus instance
398  *
399  * Returns 0 if successful, or a negative error code.
400  */
401 int __devinit snd_hda_bus_new(struct snd_card *card,
402                               const struct hda_bus_template *temp,
403                               struct hda_bus **busp)
404 {
405         struct hda_bus *bus;
406         int err;
407         static struct snd_device_ops dev_ops = {
408                 .dev_free = snd_hda_bus_dev_free,
409         };
410
411         if (snd_BUG_ON(!temp))
412                 return -EINVAL;
413         if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
414                 return -EINVAL;
415
416         if (busp)
417                 *busp = NULL;
418
419         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
420         if (bus == NULL) {
421                 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
422                 return -ENOMEM;
423         }
424
425         bus->card = card;
426         bus->private_data = temp->private_data;
427         bus->pci = temp->pci;
428         bus->modelname = temp->modelname;
429         bus->ops = temp->ops;
430
431         mutex_init(&bus->cmd_mutex);
432         INIT_LIST_HEAD(&bus->codec_list);
433
434         err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
435         if (err < 0) {
436                 snd_hda_bus_free(bus);
437                 return err;
438         }
439         if (busp)
440                 *busp = bus;
441         return 0;
442 }
443
444 #ifdef CONFIG_SND_HDA_GENERIC
445 #define is_generic_config(codec) \
446         (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
447 #else
448 #define is_generic_config(codec)        0
449 #endif
450
451 /*
452  * find a matching codec preset
453  */
454 static const struct hda_codec_preset __devinit *
455 find_codec_preset(struct hda_codec *codec)
456 {
457         const struct hda_codec_preset **tbl, *preset;
458
459         if (is_generic_config(codec))
460                 return NULL; /* use the generic parser */
461
462         for (tbl = hda_preset_tables; *tbl; tbl++) {
463                 for (preset = *tbl; preset->id; preset++) {
464                         u32 mask = preset->mask;
465                         if (preset->afg && preset->afg != codec->afg)
466                                 continue;
467                         if (preset->mfg && preset->mfg != codec->mfg)
468                                 continue;
469                         if (!mask)
470                                 mask = ~0;
471                         if (preset->id == (codec->vendor_id & mask) &&
472                             (!preset->rev ||
473                              preset->rev == codec->revision_id))
474                                 return preset;
475                 }
476         }
477         return NULL;
478 }
479
480 /*
481  * snd_hda_get_codec_name - store the codec name
482  */
483 void snd_hda_get_codec_name(struct hda_codec *codec,
484                             char *name, int namelen)
485 {
486         const struct hda_vendor_id *c;
487         const char *vendor = NULL;
488         u16 vendor_id = codec->vendor_id >> 16;
489         char tmp[16];
490
491         for (c = hda_vendor_ids; c->id; c++) {
492                 if (c->id == vendor_id) {
493                         vendor = c->name;
494                         break;
495                 }
496         }
497         if (!vendor) {
498                 sprintf(tmp, "Generic %04x", vendor_id);
499                 vendor = tmp;
500         }
501         if (codec->preset && codec->preset->name)
502                 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
503         else
504                 snprintf(name, namelen, "%s ID %x", vendor,
505                          codec->vendor_id & 0xffff);
506 }
507
508 /*
509  * look for an AFG and MFG nodes
510  */
511 static void __devinit setup_fg_nodes(struct hda_codec *codec)
512 {
513         int i, total_nodes;
514         hda_nid_t nid;
515
516         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
517         for (i = 0; i < total_nodes; i++, nid++) {
518                 unsigned int func;
519                 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
520                 switch (func & 0xff) {
521                 case AC_GRP_AUDIO_FUNCTION:
522                         codec->afg = nid;
523                         break;
524                 case AC_GRP_MODEM_FUNCTION:
525                         codec->mfg = nid;
526                         break;
527                 default:
528                         break;
529                 }
530         }
531 }
532
533 /*
534  * read widget caps for each widget and store in cache
535  */
536 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
537 {
538         int i;
539         hda_nid_t nid;
540
541         codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
542                                                  &codec->start_nid);
543         codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
544         if (!codec->wcaps)
545                 return -ENOMEM;
546         nid = codec->start_nid;
547         for (i = 0; i < codec->num_nodes; i++, nid++)
548                 codec->wcaps[i] = snd_hda_param_read(codec, nid,
549                                                      AC_PAR_AUDIO_WIDGET_CAP);
550         return 0;
551 }
552
553
554 static void init_hda_cache(struct hda_cache_rec *cache,
555                            unsigned int record_size);
556 static void free_hda_cache(struct hda_cache_rec *cache);
557
558 /*
559  * codec destructor
560  */
561 static void snd_hda_codec_free(struct hda_codec *codec)
562 {
563         if (!codec)
564                 return;
565 #ifdef CONFIG_SND_HDA_POWER_SAVE
566         cancel_delayed_work(&codec->power_work);
567         flush_scheduled_work();
568 #endif
569         list_del(&codec->list);
570         codec->bus->caddr_tbl[codec->addr] = NULL;
571         if (codec->patch_ops.free)
572                 codec->patch_ops.free(codec);
573         free_hda_cache(&codec->amp_cache);
574         free_hda_cache(&codec->cmd_cache);
575         kfree(codec->wcaps);
576         kfree(codec);
577 }
578
579 /**
580  * snd_hda_codec_new - create a HDA codec
581  * @bus: the bus to assign
582  * @codec_addr: the codec address
583  * @codecp: the pointer to store the generated codec
584  *
585  * Returns 0 if successful, or a negative error code.
586  */
587 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
588                                 struct hda_codec **codecp)
589 {
590         struct hda_codec *codec;
591         char component[31];
592         int err;
593
594         if (snd_BUG_ON(!bus))
595                 return -EINVAL;
596         if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
597                 return -EINVAL;
598
599         if (bus->caddr_tbl[codec_addr]) {
600                 snd_printk(KERN_ERR "hda_codec: "
601                            "address 0x%x is already occupied\n", codec_addr);
602                 return -EBUSY;
603         }
604
605         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
606         if (codec == NULL) {
607                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
608                 return -ENOMEM;
609         }
610
611         codec->bus = bus;
612         codec->addr = codec_addr;
613         mutex_init(&codec->spdif_mutex);
614         init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
615         init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
616
617 #ifdef CONFIG_SND_HDA_POWER_SAVE
618         INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
619         /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
620          * the caller has to power down appropriatley after initialization
621          * phase.
622          */
623         hda_keep_power_on(codec);
624 #endif
625
626         list_add_tail(&codec->list, &bus->codec_list);
627         bus->caddr_tbl[codec_addr] = codec;
628
629         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
630                                               AC_PAR_VENDOR_ID);
631         if (codec->vendor_id == -1)
632                 /* read again, hopefully the access method was corrected
633                  * in the last read...
634                  */
635                 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
636                                                       AC_PAR_VENDOR_ID);
637         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
638                                                  AC_PAR_SUBSYSTEM_ID);
639         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
640                                                 AC_PAR_REV_ID);
641
642         setup_fg_nodes(codec);
643         if (!codec->afg && !codec->mfg) {
644                 snd_printdd("hda_codec: no AFG or MFG node found\n");
645                 snd_hda_codec_free(codec);
646                 return -ENODEV;
647         }
648
649         if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
650                 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
651                 snd_hda_codec_free(codec);
652                 return -ENOMEM;
653         }
654
655         if (!codec->subsystem_id) {
656                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
657                 codec->subsystem_id =
658                         snd_hda_codec_read(codec, nid, 0,
659                                            AC_VERB_GET_SUBSYSTEM_ID, 0);
660         }
661
662         codec->preset = find_codec_preset(codec);
663         /* audio codec should override the mixer name */
664         if (codec->afg || !*bus->card->mixername)
665                 snd_hda_get_codec_name(codec, bus->card->mixername,
666                                        sizeof(bus->card->mixername));
667
668         if (is_generic_config(codec)) {
669                 err = snd_hda_parse_generic_codec(codec);
670                 goto patched;
671         }
672         if (codec->preset && codec->preset->patch) {
673                 err = codec->preset->patch(codec);
674                 goto patched;
675         }
676
677         /* call the default parser */
678         err = snd_hda_parse_generic_codec(codec);
679         if (err < 0)
680                 printk(KERN_ERR "hda-codec: No codec parser is available\n");
681
682  patched:
683         if (err < 0) {
684                 snd_hda_codec_free(codec);
685                 return err;
686         }
687
688         if (codec->patch_ops.unsol_event)
689                 init_unsol_queue(bus);
690
691         snd_hda_codec_proc_new(codec);
692 #ifdef CONFIG_SND_HDA_HWDEP
693         snd_hda_create_hwdep(codec);
694 #endif
695
696         sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
697         snd_component_add(codec->bus->card, component);
698
699         if (codecp)
700                 *codecp = codec;
701         return 0;
702 }
703
704 /**
705  * snd_hda_codec_setup_stream - set up the codec for streaming
706  * @codec: the CODEC to set up
707  * @nid: the NID to set up
708  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
709  * @channel_id: channel id to pass, zero based.
710  * @format: stream format.
711  */
712 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
713                                 u32 stream_tag,
714                                 int channel_id, int format)
715 {
716         if (!nid)
717                 return;
718
719         snd_printdd("hda_codec_setup_stream: "
720                     "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
721                     nid, stream_tag, channel_id, format);
722         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
723                             (stream_tag << 4) | channel_id);
724         msleep(1);
725         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
726 }
727
728 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
729 {
730         if (!nid)
731                 return;
732
733         snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
734         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
735 #if 0 /* keep the format */
736         msleep(1);
737         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
738 #endif
739 }
740
741 /*
742  * amp access functions
743  */
744
745 /* FIXME: more better hash key? */
746 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
747 #define INFO_AMP_CAPS   (1<<0)
748 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
749
750 /* initialize the hash table */
751 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
752                                      unsigned int record_size)
753 {
754         memset(cache, 0, sizeof(*cache));
755         memset(cache->hash, 0xff, sizeof(cache->hash));
756         cache->record_size = record_size;
757 }
758
759 static void free_hda_cache(struct hda_cache_rec *cache)
760 {
761         kfree(cache->buffer);
762 }
763
764 /* query the hash.  allocate an entry if not found. */
765 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
766                                               u32 key)
767 {
768         u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
769         u16 cur = cache->hash[idx];
770         struct hda_cache_head *info;
771
772         while (cur != 0xffff) {
773                 info = (struct hda_cache_head *)(cache->buffer +
774                                                  cur * cache->record_size);
775                 if (info->key == key)
776                         return info;
777                 cur = info->next;
778         }
779
780         /* add a new hash entry */
781         if (cache->num_entries >= cache->size) {
782                 /* reallocate the array */
783                 unsigned int new_size = cache->size + 64;
784                 void *new_buffer;
785                 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
786                 if (!new_buffer) {
787                         snd_printk(KERN_ERR "hda_codec: "
788                                    "can't malloc amp_info\n");
789                         return NULL;
790                 }
791                 if (cache->buffer) {
792                         memcpy(new_buffer, cache->buffer,
793                                cache->size * cache->record_size);
794                         kfree(cache->buffer);
795                 }
796                 cache->size = new_size;
797                 cache->buffer = new_buffer;
798         }
799         cur = cache->num_entries++;
800         info = (struct hda_cache_head *)(cache->buffer +
801                                          cur * cache->record_size);
802         info->key = key;
803         info->val = 0;
804         info->next = cache->hash[idx];
805         cache->hash[idx] = cur;
806
807         return info;
808 }
809
810 /* query and allocate an amp hash entry */
811 static inline struct hda_amp_info *
812 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
813 {
814         return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
815 }
816
817 /*
818  * query AMP capabilities for the given widget and direction
819  */
820 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
821 {
822         struct hda_amp_info *info;
823
824         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
825         if (!info)
826                 return 0;
827         if (!(info->head.val & INFO_AMP_CAPS)) {
828                 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
829                         nid = codec->afg;
830                 info->amp_caps = snd_hda_param_read(codec, nid,
831                                                     direction == HDA_OUTPUT ?
832                                                     AC_PAR_AMP_OUT_CAP :
833                                                     AC_PAR_AMP_IN_CAP);
834                 if (info->amp_caps)
835                         info->head.val |= INFO_AMP_CAPS;
836         }
837         return info->amp_caps;
838 }
839
840 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
841                               unsigned int caps)
842 {
843         struct hda_amp_info *info;
844
845         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
846         if (!info)
847                 return -EINVAL;
848         info->amp_caps = caps;
849         info->head.val |= INFO_AMP_CAPS;
850         return 0;
851 }
852
853 /*
854  * read the current volume to info
855  * if the cache exists, read the cache value.
856  */
857 static unsigned int get_vol_mute(struct hda_codec *codec,
858                                  struct hda_amp_info *info, hda_nid_t nid,
859                                  int ch, int direction, int index)
860 {
861         u32 val, parm;
862
863         if (info->head.val & INFO_AMP_VOL(ch))
864                 return info->vol[ch];
865
866         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
867         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
868         parm |= index;
869         val = snd_hda_codec_read(codec, nid, 0,
870                                  AC_VERB_GET_AMP_GAIN_MUTE, parm);
871         info->vol[ch] = val & 0xff;
872         info->head.val |= INFO_AMP_VOL(ch);
873         return info->vol[ch];
874 }
875
876 /*
877  * write the current volume in info to the h/w and update the cache
878  */
879 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
880                          hda_nid_t nid, int ch, int direction, int index,
881                          int val)
882 {
883         u32 parm;
884
885         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
886         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
887         parm |= index << AC_AMP_SET_INDEX_SHIFT;
888         parm |= val;
889         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
890         info->vol[ch] = val;
891 }
892
893 /*
894  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
895  */
896 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
897                            int direction, int index)
898 {
899         struct hda_amp_info *info;
900         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
901         if (!info)
902                 return 0;
903         return get_vol_mute(codec, info, nid, ch, direction, index);
904 }
905
906 /*
907  * update the AMP value, mask = bit mask to set, val = the value
908  */
909 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
910                              int direction, int idx, int mask, int val)
911 {
912         struct hda_amp_info *info;
913
914         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
915         if (!info)
916                 return 0;
917         val &= mask;
918         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
919         if (info->vol[ch] == val)
920                 return 0;
921         put_vol_mute(codec, info, nid, ch, direction, idx, val);
922         return 1;
923 }
924
925 /*
926  * update the AMP stereo with the same mask and value
927  */
928 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
929                              int direction, int idx, int mask, int val)
930 {
931         int ch, ret = 0;
932         for (ch = 0; ch < 2; ch++)
933                 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
934                                                 idx, mask, val);
935         return ret;
936 }
937
938 #ifdef SND_HDA_NEEDS_RESUME
939 /* resume the all amp commands from the cache */
940 void snd_hda_codec_resume_amp(struct hda_codec *codec)
941 {
942         struct hda_amp_info *buffer = codec->amp_cache.buffer;
943         int i;
944
945         for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
946                 u32 key = buffer->head.key;
947                 hda_nid_t nid;
948                 unsigned int idx, dir, ch;
949                 if (!key)
950                         continue;
951                 nid = key & 0xff;
952                 idx = (key >> 16) & 0xff;
953                 dir = (key >> 24) & 0xff;
954                 for (ch = 0; ch < 2; ch++) {
955                         if (!(buffer->head.val & INFO_AMP_VOL(ch)))
956                                 continue;
957                         put_vol_mute(codec, buffer, nid, ch, dir, idx,
958                                      buffer->vol[ch]);
959                 }
960         }
961 }
962 #endif /* SND_HDA_NEEDS_RESUME */
963
964 /*
965  * AMP control callbacks
966  */
967 /* retrieve parameters from private_value */
968 #define get_amp_nid(kc)         ((kc)->private_value & 0xffff)
969 #define get_amp_channels(kc)    (((kc)->private_value >> 16) & 0x3)
970 #define get_amp_direction(kc)   (((kc)->private_value >> 18) & 0x1)
971 #define get_amp_index(kc)       (((kc)->private_value >> 19) & 0xf)
972
973 /* volume */
974 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
975                                   struct snd_ctl_elem_info *uinfo)
976 {
977         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
978         u16 nid = get_amp_nid(kcontrol);
979         u8 chs = get_amp_channels(kcontrol);
980         int dir = get_amp_direction(kcontrol);
981         u32 caps;
982
983         caps = query_amp_caps(codec, nid, dir);
984         /* num steps */
985         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
986         if (!caps) {
987                 printk(KERN_WARNING "hda_codec: "
988                        "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
989                        kcontrol->id.name);
990                 return -EINVAL;
991         }
992         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
993         uinfo->count = chs == 3 ? 2 : 1;
994         uinfo->value.integer.min = 0;
995         uinfo->value.integer.max = caps;
996         return 0;
997 }
998
999 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1000                                  struct snd_ctl_elem_value *ucontrol)
1001 {
1002         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1003         hda_nid_t nid = get_amp_nid(kcontrol);
1004         int chs = get_amp_channels(kcontrol);
1005         int dir = get_amp_direction(kcontrol);
1006         int idx = get_amp_index(kcontrol);
1007         long *valp = ucontrol->value.integer.value;
1008
1009         if (chs & 1)
1010                 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1011                         & HDA_AMP_VOLMASK;
1012         if (chs & 2)
1013                 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1014                         & HDA_AMP_VOLMASK;
1015         return 0;
1016 }
1017
1018 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1019                                  struct snd_ctl_elem_value *ucontrol)
1020 {
1021         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1022         hda_nid_t nid = get_amp_nid(kcontrol);
1023         int chs = get_amp_channels(kcontrol);
1024         int dir = get_amp_direction(kcontrol);
1025         int idx = get_amp_index(kcontrol);
1026         long *valp = ucontrol->value.integer.value;
1027         int change = 0;
1028
1029         snd_hda_power_up(codec);
1030         if (chs & 1) {
1031                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1032                                                   0x7f, *valp);
1033                 valp++;
1034         }
1035         if (chs & 2)
1036                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1037                                                    0x7f, *valp);
1038         snd_hda_power_down(codec);
1039         return change;
1040 }
1041
1042 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1043                           unsigned int size, unsigned int __user *_tlv)
1044 {
1045         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1046         hda_nid_t nid = get_amp_nid(kcontrol);
1047         int dir = get_amp_direction(kcontrol);
1048         u32 caps, val1, val2;
1049
1050         if (size < 4 * sizeof(unsigned int))
1051                 return -ENOMEM;
1052         caps = query_amp_caps(codec, nid, dir);
1053         val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1054         val2 = (val2 + 1) * 25;
1055         val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1056         val1 = ((int)val1) * ((int)val2);
1057         if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1058                 return -EFAULT;
1059         if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1060                 return -EFAULT;
1061         if (put_user(val1, _tlv + 2))
1062                 return -EFAULT;
1063         if (put_user(val2, _tlv + 3))
1064                 return -EFAULT;
1065         return 0;
1066 }
1067
1068 /*
1069  * set (static) TLV for virtual master volume; recalculated as max 0dB
1070  */
1071 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1072                              unsigned int *tlv)
1073 {
1074         u32 caps;
1075         int nums, step;
1076
1077         caps = query_amp_caps(codec, nid, dir);
1078         nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1079         step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1080         step = (step + 1) * 25;
1081         tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1082         tlv[1] = 2 * sizeof(unsigned int);
1083         tlv[2] = -nums * step;
1084         tlv[3] = step;
1085 }
1086
1087 /* find a mixer control element with the given name */
1088 static struct snd_kcontrol *
1089 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1090                         const char *name, int idx)
1091 {
1092         struct snd_ctl_elem_id id;
1093         memset(&id, 0, sizeof(id));
1094         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1095         id.index = idx;
1096         strcpy(id.name, name);
1097         return snd_ctl_find_id(codec->bus->card, &id);
1098 }
1099
1100 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1101                                             const char *name)
1102 {
1103         return _snd_hda_find_mixer_ctl(codec, name, 0);
1104 }
1105
1106 /* create a virtual master control and add slaves */
1107 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1108                         unsigned int *tlv, const char **slaves)
1109 {
1110         struct snd_kcontrol *kctl;
1111         const char **s;
1112         int err;
1113
1114         for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1115                 ;
1116         if (!*s) {
1117                 snd_printdd("No slave found for %s\n", name);
1118                 return 0;
1119         }
1120         kctl = snd_ctl_make_virtual_master(name, tlv);
1121         if (!kctl)
1122                 return -ENOMEM;
1123         err = snd_ctl_add(codec->bus->card, kctl);
1124         if (err < 0)
1125                 return err;
1126         
1127         for (s = slaves; *s; s++) {
1128                 struct snd_kcontrol *sctl;
1129
1130                 sctl = snd_hda_find_mixer_ctl(codec, *s);
1131                 if (!sctl) {
1132                         snd_printdd("Cannot find slave %s, skipped\n", *s);
1133                         continue;
1134                 }
1135                 err = snd_ctl_add_slave(kctl, sctl);
1136                 if (err < 0)
1137                         return err;
1138         }
1139         return 0;
1140 }
1141
1142 /* switch */
1143 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1144                                   struct snd_ctl_elem_info *uinfo)
1145 {
1146         int chs = get_amp_channels(kcontrol);
1147
1148         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1149         uinfo->count = chs == 3 ? 2 : 1;
1150         uinfo->value.integer.min = 0;
1151         uinfo->value.integer.max = 1;
1152         return 0;
1153 }
1154
1155 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1156                                  struct snd_ctl_elem_value *ucontrol)
1157 {
1158         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1159         hda_nid_t nid = get_amp_nid(kcontrol);
1160         int chs = get_amp_channels(kcontrol);
1161         int dir = get_amp_direction(kcontrol);
1162         int idx = get_amp_index(kcontrol);
1163         long *valp = ucontrol->value.integer.value;
1164
1165         if (chs & 1)
1166                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1167                            HDA_AMP_MUTE) ? 0 : 1;
1168         if (chs & 2)
1169                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1170                          HDA_AMP_MUTE) ? 0 : 1;
1171         return 0;
1172 }
1173
1174 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1175                                  struct snd_ctl_elem_value *ucontrol)
1176 {
1177         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1178         hda_nid_t nid = get_amp_nid(kcontrol);
1179         int chs = get_amp_channels(kcontrol);
1180         int dir = get_amp_direction(kcontrol);
1181         int idx = get_amp_index(kcontrol);
1182         long *valp = ucontrol->value.integer.value;
1183         int change = 0;
1184
1185         snd_hda_power_up(codec);
1186         if (chs & 1) {
1187                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1188                                                   HDA_AMP_MUTE,
1189                                                   *valp ? 0 : HDA_AMP_MUTE);
1190                 valp++;
1191         }
1192         if (chs & 2)
1193                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1194                                                    HDA_AMP_MUTE,
1195                                                    *valp ? 0 : HDA_AMP_MUTE);
1196 #ifdef CONFIG_SND_HDA_POWER_SAVE
1197         if (codec->patch_ops.check_power_status)
1198                 codec->patch_ops.check_power_status(codec, nid);
1199 #endif
1200         snd_hda_power_down(codec);
1201         return change;
1202 }
1203
1204 /*
1205  * bound volume controls
1206  *
1207  * bind multiple volumes (# indices, from 0)
1208  */
1209
1210 #define AMP_VAL_IDX_SHIFT       19
1211 #define AMP_VAL_IDX_MASK        (0x0f<<19)
1212
1213 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1214                                   struct snd_ctl_elem_value *ucontrol)
1215 {
1216         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1217         unsigned long pval;
1218         int err;
1219
1220         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1221         pval = kcontrol->private_value;
1222         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1223         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1224         kcontrol->private_value = pval;
1225         mutex_unlock(&codec->spdif_mutex);
1226         return err;
1227 }
1228
1229 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1230                                   struct snd_ctl_elem_value *ucontrol)
1231 {
1232         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233         unsigned long pval;
1234         int i, indices, err = 0, change = 0;
1235
1236         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1237         pval = kcontrol->private_value;
1238         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1239         for (i = 0; i < indices; i++) {
1240                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1241                         (i << AMP_VAL_IDX_SHIFT);
1242                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1243                 if (err < 0)
1244                         break;
1245                 change |= err;
1246         }
1247         kcontrol->private_value = pval;
1248         mutex_unlock(&codec->spdif_mutex);
1249         return err < 0 ? err : change;
1250 }
1251
1252 /*
1253  * generic bound volume/swtich controls
1254  */
1255 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1256                                  struct snd_ctl_elem_info *uinfo)
1257 {
1258         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1259         struct hda_bind_ctls *c;
1260         int err;
1261
1262         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1263         c = (struct hda_bind_ctls *)kcontrol->private_value;
1264         kcontrol->private_value = *c->values;
1265         err = c->ops->info(kcontrol, uinfo);
1266         kcontrol->private_value = (long)c;
1267         mutex_unlock(&codec->spdif_mutex);
1268         return err;
1269 }
1270
1271 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1272                                 struct snd_ctl_elem_value *ucontrol)
1273 {
1274         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1275         struct hda_bind_ctls *c;
1276         int err;
1277
1278         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1279         c = (struct hda_bind_ctls *)kcontrol->private_value;
1280         kcontrol->private_value = *c->values;
1281         err = c->ops->get(kcontrol, ucontrol);
1282         kcontrol->private_value = (long)c;
1283         mutex_unlock(&codec->spdif_mutex);
1284         return err;
1285 }
1286
1287 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1288                                 struct snd_ctl_elem_value *ucontrol)
1289 {
1290         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1291         struct hda_bind_ctls *c;
1292         unsigned long *vals;
1293         int err = 0, change = 0;
1294
1295         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1296         c = (struct hda_bind_ctls *)kcontrol->private_value;
1297         for (vals = c->values; *vals; vals++) {
1298                 kcontrol->private_value = *vals;
1299                 err = c->ops->put(kcontrol, ucontrol);
1300                 if (err < 0)
1301                         break;
1302                 change |= err;
1303         }
1304         kcontrol->private_value = (long)c;
1305         mutex_unlock(&codec->spdif_mutex);
1306         return err < 0 ? err : change;
1307 }
1308
1309 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1310                            unsigned int size, unsigned int __user *tlv)
1311 {
1312         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1313         struct hda_bind_ctls *c;
1314         int err;
1315
1316         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1317         c = (struct hda_bind_ctls *)kcontrol->private_value;
1318         kcontrol->private_value = *c->values;
1319         err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1320         kcontrol->private_value = (long)c;
1321         mutex_unlock(&codec->spdif_mutex);
1322         return err;
1323 }
1324
1325 struct hda_ctl_ops snd_hda_bind_vol = {
1326         .info = snd_hda_mixer_amp_volume_info,
1327         .get = snd_hda_mixer_amp_volume_get,
1328         .put = snd_hda_mixer_amp_volume_put,
1329         .tlv = snd_hda_mixer_amp_tlv
1330 };
1331
1332 struct hda_ctl_ops snd_hda_bind_sw = {
1333         .info = snd_hda_mixer_amp_switch_info,
1334         .get = snd_hda_mixer_amp_switch_get,
1335         .put = snd_hda_mixer_amp_switch_put,
1336         .tlv = snd_hda_mixer_amp_tlv
1337 };
1338
1339 /*
1340  * SPDIF out controls
1341  */
1342
1343 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1344                                    struct snd_ctl_elem_info *uinfo)
1345 {
1346         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1347         uinfo->count = 1;
1348         return 0;
1349 }
1350
1351 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1352                                    struct snd_ctl_elem_value *ucontrol)
1353 {
1354         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1355                                            IEC958_AES0_NONAUDIO |
1356                                            IEC958_AES0_CON_EMPHASIS_5015 |
1357                                            IEC958_AES0_CON_NOT_COPYRIGHT;
1358         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1359                                            IEC958_AES1_CON_ORIGINAL;
1360         return 0;
1361 }
1362
1363 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1364                                    struct snd_ctl_elem_value *ucontrol)
1365 {
1366         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1367                                            IEC958_AES0_NONAUDIO |
1368                                            IEC958_AES0_PRO_EMPHASIS_5015;
1369         return 0;
1370 }
1371
1372 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1373                                      struct snd_ctl_elem_value *ucontrol)
1374 {
1375         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1376
1377         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1378         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1379         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1380         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1381
1382         return 0;
1383 }
1384
1385 /* convert from SPDIF status bits to HDA SPDIF bits
1386  * bit 0 (DigEn) is always set zero (to be filled later)
1387  */
1388 static unsigned short convert_from_spdif_status(unsigned int sbits)
1389 {
1390         unsigned short val = 0;
1391
1392         if (sbits & IEC958_AES0_PROFESSIONAL)
1393                 val |= AC_DIG1_PROFESSIONAL;
1394         if (sbits & IEC958_AES0_NONAUDIO)
1395                 val |= AC_DIG1_NONAUDIO;
1396         if (sbits & IEC958_AES0_PROFESSIONAL) {
1397                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1398                     IEC958_AES0_PRO_EMPHASIS_5015)
1399                         val |= AC_DIG1_EMPHASIS;
1400         } else {
1401                 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1402                     IEC958_AES0_CON_EMPHASIS_5015)
1403                         val |= AC_DIG1_EMPHASIS;
1404                 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1405                         val |= AC_DIG1_COPYRIGHT;
1406                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1407                         val |= AC_DIG1_LEVEL;
1408                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1409         }
1410         return val;
1411 }
1412
1413 /* convert to SPDIF status bits from HDA SPDIF bits
1414  */
1415 static unsigned int convert_to_spdif_status(unsigned short val)
1416 {
1417         unsigned int sbits = 0;
1418
1419         if (val & AC_DIG1_NONAUDIO)
1420                 sbits |= IEC958_AES0_NONAUDIO;
1421         if (val & AC_DIG1_PROFESSIONAL)
1422                 sbits |= IEC958_AES0_PROFESSIONAL;
1423         if (sbits & IEC958_AES0_PROFESSIONAL) {
1424                 if (sbits & AC_DIG1_EMPHASIS)
1425                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1426         } else {
1427                 if (val & AC_DIG1_EMPHASIS)
1428                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1429                 if (!(val & AC_DIG1_COPYRIGHT))
1430                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1431                 if (val & AC_DIG1_LEVEL)
1432                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1433                 sbits |= val & (0x7f << 8);
1434         }
1435         return sbits;
1436 }
1437
1438 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1439                                      struct snd_ctl_elem_value *ucontrol)
1440 {
1441         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1442         hda_nid_t nid = kcontrol->private_value;
1443         unsigned short val;
1444         int change;
1445
1446         mutex_lock(&codec->spdif_mutex);
1447         codec->spdif_status = ucontrol->value.iec958.status[0] |
1448                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1449                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1450                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1451         val = convert_from_spdif_status(codec->spdif_status);
1452         val |= codec->spdif_ctls & 1;
1453         change = codec->spdif_ctls != val;
1454         codec->spdif_ctls = val;
1455
1456         if (change) {
1457                 hda_nid_t *d;
1458                 snd_hda_codec_write_cache(codec, nid, 0,
1459                                           AC_VERB_SET_DIGI_CONVERT_1,
1460                                           val & 0xff);
1461                 snd_hda_codec_write_cache(codec, nid, 0,
1462                                           AC_VERB_SET_DIGI_CONVERT_2,
1463                                           val >> 8);
1464
1465                 if (codec->slave_dig_outs)
1466                         for (d = codec->slave_dig_outs; *d; d++) {
1467                                 snd_hda_codec_write_cache(codec, *d, 0,
1468                                           AC_VERB_SET_DIGI_CONVERT_1,
1469                                           val & 0xff);
1470                                 snd_hda_codec_write_cache(codec, *d, 0,
1471                                           AC_VERB_SET_DIGI_CONVERT_2,
1472                                           val >> 8);
1473                         }
1474         }
1475
1476         mutex_unlock(&codec->spdif_mutex);
1477         return change;
1478 }
1479
1480 #define snd_hda_spdif_out_switch_info   snd_ctl_boolean_mono_info
1481
1482 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1483                                         struct snd_ctl_elem_value *ucontrol)
1484 {
1485         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1486
1487         ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1488         return 0;
1489 }
1490
1491 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1492                                         struct snd_ctl_elem_value *ucontrol)
1493 {
1494         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1495         hda_nid_t nid = kcontrol->private_value;
1496         unsigned short val;
1497         int change;
1498
1499         mutex_lock(&codec->spdif_mutex);
1500         val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1501         if (ucontrol->value.integer.value[0])
1502                 val |= AC_DIG1_ENABLE;
1503         change = codec->spdif_ctls != val;
1504         if (change) {
1505                 hda_nid_t *d;
1506                 codec->spdif_ctls = val;
1507                 snd_hda_codec_write_cache(codec, nid, 0,
1508                                           AC_VERB_SET_DIGI_CONVERT_1,
1509                                           val & 0xff);
1510
1511                 if (codec->slave_dig_outs)
1512                         for (d = codec->slave_dig_outs; *d; d++)
1513                                 snd_hda_codec_write_cache(codec, *d, 0,
1514                                           AC_VERB_SET_DIGI_CONVERT_1,
1515                                           val & 0xff);
1516                 /* unmute amp switch (if any) */
1517                 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1518                     (val & AC_DIG1_ENABLE))
1519                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1520                                                  HDA_AMP_MUTE, 0);
1521         }
1522         mutex_unlock(&codec->spdif_mutex);
1523         return change;
1524 }
1525
1526 static struct snd_kcontrol_new dig_mixes[] = {
1527         {
1528                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1529                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1530                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1531                 .info = snd_hda_spdif_mask_info,
1532                 .get = snd_hda_spdif_cmask_get,
1533         },
1534         {
1535                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1536                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1537                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1538                 .info = snd_hda_spdif_mask_info,
1539                 .get = snd_hda_spdif_pmask_get,
1540         },
1541         {
1542                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1543                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1544                 .info = snd_hda_spdif_mask_info,
1545                 .get = snd_hda_spdif_default_get,
1546                 .put = snd_hda_spdif_default_put,
1547         },
1548         {
1549                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1550                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1551                 .info = snd_hda_spdif_out_switch_info,
1552                 .get = snd_hda_spdif_out_switch_get,
1553                 .put = snd_hda_spdif_out_switch_put,
1554         },
1555         { } /* end */
1556 };
1557
1558 #define SPDIF_MAX_IDX   4       /* 4 instances should be enough to probe */
1559
1560 /**
1561  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1562  * @codec: the HDA codec
1563  * @nid: audio out widget NID
1564  *
1565  * Creates controls related with the SPDIF output.
1566  * Called from each patch supporting the SPDIF out.
1567  *
1568  * Returns 0 if successful, or a negative error code.
1569  */
1570 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1571 {
1572         int err;
1573         struct snd_kcontrol *kctl;
1574         struct snd_kcontrol_new *dig_mix;
1575         int idx;
1576
1577         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1578                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1579                                              idx))
1580                         break;
1581         }
1582         if (idx >= SPDIF_MAX_IDX) {
1583                 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1584                 return -EBUSY;
1585         }
1586         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1587                 kctl = snd_ctl_new1(dig_mix, codec);
1588                 kctl->id.index = idx;
1589                 kctl->private_value = nid;
1590                 err = snd_ctl_add(codec->bus->card, kctl);
1591                 if (err < 0)
1592                         return err;
1593         }
1594         codec->spdif_ctls =
1595                 snd_hda_codec_read(codec, nid, 0,
1596                                    AC_VERB_GET_DIGI_CONVERT_1, 0);
1597         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1598         return 0;
1599 }
1600
1601 /*
1602  * SPDIF sharing with analog output
1603  */
1604 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1605                               struct snd_ctl_elem_value *ucontrol)
1606 {
1607         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1608         ucontrol->value.integer.value[0] = mout->share_spdif;
1609         return 0;
1610 }
1611
1612 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1613                               struct snd_ctl_elem_value *ucontrol)
1614 {
1615         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1616         mout->share_spdif = !!ucontrol->value.integer.value[0];
1617         return 0;
1618 }
1619
1620 static struct snd_kcontrol_new spdif_share_sw = {
1621         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1622         .name = "IEC958 Default PCM Playback Switch",
1623         .info = snd_ctl_boolean_mono_info,
1624         .get = spdif_share_sw_get,
1625         .put = spdif_share_sw_put,
1626 };
1627
1628 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1629                                   struct hda_multi_out *mout)
1630 {
1631         if (!mout->dig_out_nid)
1632                 return 0;
1633         /* ATTENTION: here mout is passed as private_data, instead of codec */
1634         return snd_ctl_add(codec->bus->card,
1635                            snd_ctl_new1(&spdif_share_sw, mout));
1636 }
1637
1638 /*
1639  * SPDIF input
1640  */
1641
1642 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
1643
1644 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1645                                        struct snd_ctl_elem_value *ucontrol)
1646 {
1647         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648
1649         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1650         return 0;
1651 }
1652
1653 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1654                                        struct snd_ctl_elem_value *ucontrol)
1655 {
1656         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1657         hda_nid_t nid = kcontrol->private_value;
1658         unsigned int val = !!ucontrol->value.integer.value[0];
1659         int change;
1660
1661         mutex_lock(&codec->spdif_mutex);
1662         change = codec->spdif_in_enable != val;
1663         if (change) {
1664                 hda_nid_t *d;
1665                 codec->spdif_in_enable = val;
1666                 snd_hda_codec_write_cache(codec, nid, 0,
1667                                           AC_VERB_SET_DIGI_CONVERT_1, val);
1668
1669                 if (codec->slave_dig_outs)
1670                         for (d = codec->slave_dig_outs; *d; d++)
1671                                 snd_hda_codec_write_cache(codec, *d, 0,
1672                                           AC_VERB_SET_DIGI_CONVERT_1, val);
1673         }
1674         mutex_unlock(&codec->spdif_mutex);
1675         return change;
1676 }
1677
1678 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1679                                        struct snd_ctl_elem_value *ucontrol)
1680 {
1681         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1682         hda_nid_t nid = kcontrol->private_value;
1683         unsigned short val;
1684         unsigned int sbits;
1685
1686         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1687         sbits = convert_to_spdif_status(val);
1688         ucontrol->value.iec958.status[0] = sbits;
1689         ucontrol->value.iec958.status[1] = sbits >> 8;
1690         ucontrol->value.iec958.status[2] = sbits >> 16;
1691         ucontrol->value.iec958.status[3] = sbits >> 24;
1692         return 0;
1693 }
1694
1695 static struct snd_kcontrol_new dig_in_ctls[] = {
1696         {
1697                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1698                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1699                 .info = snd_hda_spdif_in_switch_info,
1700                 .get = snd_hda_spdif_in_switch_get,
1701                 .put = snd_hda_spdif_in_switch_put,
1702         },
1703         {
1704                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1705                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1706                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1707                 .info = snd_hda_spdif_mask_info,
1708                 .get = snd_hda_spdif_in_status_get,
1709         },
1710         { } /* end */
1711 };
1712
1713 /**
1714  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1715  * @codec: the HDA codec
1716  * @nid: audio in widget NID
1717  *
1718  * Creates controls related with the SPDIF input.
1719  * Called from each patch supporting the SPDIF in.
1720  *
1721  * Returns 0 if successful, or a negative error code.
1722  */
1723 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1724 {
1725         int err;
1726         struct snd_kcontrol *kctl;
1727         struct snd_kcontrol_new *dig_mix;
1728         int idx;
1729
1730         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1731                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1732                                              idx))
1733                         break;
1734         }
1735         if (idx >= SPDIF_MAX_IDX) {
1736                 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1737                 return -EBUSY;
1738         }
1739         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1740                 kctl = snd_ctl_new1(dig_mix, codec);
1741                 kctl->private_value = nid;
1742                 err = snd_ctl_add(codec->bus->card, kctl);
1743                 if (err < 0)
1744                         return err;
1745         }
1746         codec->spdif_in_enable =
1747                 snd_hda_codec_read(codec, nid, 0,
1748                                    AC_VERB_GET_DIGI_CONVERT_1, 0) &
1749                 AC_DIG1_ENABLE;
1750         return 0;
1751 }
1752
1753 #ifdef SND_HDA_NEEDS_RESUME
1754 /*
1755  * command cache
1756  */
1757
1758 /* build a 32bit cache key with the widget id and the command parameter */
1759 #define build_cmd_cache_key(nid, verb)  ((verb << 8) | nid)
1760 #define get_cmd_cache_nid(key)          ((key) & 0xff)
1761 #define get_cmd_cache_cmd(key)          (((key) >> 8) & 0xffff)
1762
1763 /**
1764  * snd_hda_codec_write_cache - send a single command with caching
1765  * @codec: the HDA codec
1766  * @nid: NID to send the command
1767  * @direct: direct flag
1768  * @verb: the verb to send
1769  * @parm: the parameter for the verb
1770  *
1771  * Send a single command without waiting for response.
1772  *
1773  * Returns 0 if successful, or a negative error code.
1774  */
1775 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1776                               int direct, unsigned int verb, unsigned int parm)
1777 {
1778         int err;
1779         snd_hda_power_up(codec);
1780         mutex_lock(&codec->bus->cmd_mutex);
1781         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1782         if (!err) {
1783                 struct hda_cache_head *c;
1784                 u32 key = build_cmd_cache_key(nid, verb);
1785                 c = get_alloc_hash(&codec->cmd_cache, key);
1786                 if (c)
1787                         c->val = parm;
1788         }
1789         mutex_unlock(&codec->bus->cmd_mutex);
1790         snd_hda_power_down(codec);
1791         return err;
1792 }
1793
1794 /* resume the all commands from the cache */
1795 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1796 {
1797         struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1798         int i;
1799
1800         for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1801                 u32 key = buffer->key;
1802                 if (!key)
1803                         continue;
1804                 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1805                                     get_cmd_cache_cmd(key), buffer->val);
1806         }
1807 }
1808
1809 /**
1810  * snd_hda_sequence_write_cache - sequence writes with caching
1811  * @codec: the HDA codec
1812  * @seq: VERB array to send
1813  *
1814  * Send the commands sequentially from the given array.
1815  * Thte commands are recorded on cache for power-save and resume.
1816  * The array must be terminated with NID=0.
1817  */
1818 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1819                                   const struct hda_verb *seq)
1820 {
1821         for (; seq->nid; seq++)
1822                 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1823                                           seq->param);
1824 }
1825 #endif /* SND_HDA_NEEDS_RESUME */
1826
1827 /*
1828  * set power state of the codec
1829  */
1830 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1831                                 unsigned int power_state)
1832 {
1833         hda_nid_t nid;
1834         int i;
1835
1836         snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1837                             power_state);
1838         msleep(10); /* partial workaround for "azx_get_response timeout" */
1839
1840         nid = codec->start_nid;
1841         for (i = 0; i < codec->num_nodes; i++, nid++) {
1842                 unsigned int wcaps = get_wcaps(codec, nid);
1843                 if (wcaps & AC_WCAP_POWER) {
1844                         unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1845                                 AC_WCAP_TYPE_SHIFT;
1846                         if (wid_type == AC_WID_PIN) {
1847                                 unsigned int pincap;
1848                                 /*
1849                                  * don't power down the widget if it controls
1850                                  * eapd and EAPD_BTLENABLE is set.
1851                                  */
1852                                 pincap = snd_hda_param_read(codec, nid,
1853                                                             AC_PAR_PIN_CAP);
1854                                 if (pincap & AC_PINCAP_EAPD) {
1855                                         int eapd = snd_hda_codec_read(codec,
1856                                                 nid, 0,
1857                                                 AC_VERB_GET_EAPD_BTLENABLE, 0);
1858                                         eapd &= 0x02;
1859                                         if (power_state == AC_PWRST_D3 && eapd)
1860                                                 continue;
1861                                 }
1862                         }
1863                         snd_hda_codec_write(codec, nid, 0,
1864                                             AC_VERB_SET_POWER_STATE,
1865                                             power_state);
1866                 }
1867         }
1868
1869         if (power_state == AC_PWRST_D0) {
1870                 unsigned long end_time;
1871                 int state;
1872                 msleep(10);
1873                 /* wait until the codec reachs to D0 */
1874                 end_time = jiffies + msecs_to_jiffies(500);
1875                 do {
1876                         state = snd_hda_codec_read(codec, fg, 0,
1877                                                    AC_VERB_GET_POWER_STATE, 0);
1878                         if (state == power_state)
1879                                 break;
1880                         msleep(1);
1881                 } while (time_after_eq(end_time, jiffies));
1882         }
1883 }
1884
1885 #ifdef SND_HDA_NEEDS_RESUME
1886 /*
1887  * call suspend and power-down; used both from PM and power-save
1888  */
1889 static void hda_call_codec_suspend(struct hda_codec *codec)
1890 {
1891         if (codec->patch_ops.suspend)
1892                 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1893         hda_set_power_state(codec,
1894                             codec->afg ? codec->afg : codec->mfg,
1895                             AC_PWRST_D3);
1896 #ifdef CONFIG_SND_HDA_POWER_SAVE
1897         cancel_delayed_work(&codec->power_work);
1898         codec->power_on = 0;
1899         codec->power_transition = 0;
1900 #endif
1901 }
1902
1903 /*
1904  * kick up codec; used both from PM and power-save
1905  */
1906 static void hda_call_codec_resume(struct hda_codec *codec)
1907 {
1908         hda_set_power_state(codec,
1909                             codec->afg ? codec->afg : codec->mfg,
1910                             AC_PWRST_D0);
1911         if (codec->patch_ops.resume)
1912                 codec->patch_ops.resume(codec);
1913         else {
1914                 if (codec->patch_ops.init)
1915                         codec->patch_ops.init(codec);
1916                 snd_hda_codec_resume_amp(codec);
1917                 snd_hda_codec_resume_cache(codec);
1918         }
1919 }
1920 #endif /* SND_HDA_NEEDS_RESUME */
1921
1922
1923 /**
1924  * snd_hda_build_controls - build mixer controls
1925  * @bus: the BUS
1926  *
1927  * Creates mixer controls for each codec included in the bus.
1928  *
1929  * Returns 0 if successful, otherwise a negative error code.
1930  */
1931 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1932 {
1933         struct hda_codec *codec;
1934
1935         list_for_each_entry(codec, &bus->codec_list, list) {
1936                 int err = 0;
1937                 /* fake as if already powered-on */
1938                 hda_keep_power_on(codec);
1939                 /* then fire up */
1940                 hda_set_power_state(codec,
1941                                     codec->afg ? codec->afg : codec->mfg,
1942                                     AC_PWRST_D0);
1943                 /* continue to initialize... */
1944                 if (codec->patch_ops.init)
1945                         err = codec->patch_ops.init(codec);
1946                 if (!err && codec->patch_ops.build_controls)
1947                         err = codec->patch_ops.build_controls(codec);
1948                 snd_hda_power_down(codec);
1949                 if (err < 0)
1950                         return err;
1951         }
1952
1953         return 0;
1954 }
1955
1956 /*
1957  * stream formats
1958  */
1959 struct hda_rate_tbl {
1960         unsigned int hz;
1961         unsigned int alsa_bits;
1962         unsigned int hda_fmt;
1963 };
1964
1965 static struct hda_rate_tbl rate_bits[] = {
1966         /* rate in Hz, ALSA rate bitmask, HDA format value */
1967
1968         /* autodetected value used in snd_hda_query_supported_pcm */
1969         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1970         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1971         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1972         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1973         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1974         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1975         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1976         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1977         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1978         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1979         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1980 #define AC_PAR_PCM_RATE_BITS    11
1981         /* up to bits 10, 384kHZ isn't supported properly */
1982
1983         /* not autodetected value */
1984         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1985
1986         { 0 } /* terminator */
1987 };
1988
1989 /**
1990  * snd_hda_calc_stream_format - calculate format bitset
1991  * @rate: the sample rate
1992  * @channels: the number of channels
1993  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1994  * @maxbps: the max. bps
1995  *
1996  * Calculate the format bitset from the given rate, channels and th PCM format.
1997  *
1998  * Return zero if invalid.
1999  */
2000 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2001                                         unsigned int channels,
2002                                         unsigned int format,
2003                                         unsigned int maxbps)
2004 {
2005         int i;
2006         unsigned int val = 0;
2007
2008         for (i = 0; rate_bits[i].hz; i++)
2009                 if (rate_bits[i].hz == rate) {
2010                         val = rate_bits[i].hda_fmt;
2011                         break;
2012                 }
2013         if (!rate_bits[i].hz) {
2014                 snd_printdd("invalid rate %d\n", rate);
2015                 return 0;
2016         }
2017
2018         if (channels == 0 || channels > 8) {
2019                 snd_printdd("invalid channels %d\n", channels);
2020                 return 0;
2021         }
2022         val |= channels - 1;
2023
2024         switch (snd_pcm_format_width(format)) {
2025         case 8:  val |= 0x00; break;
2026         case 16: val |= 0x10; break;
2027         case 20:
2028         case 24:
2029         case 32:
2030                 if (maxbps >= 32)
2031                         val |= 0x40;
2032                 else if (maxbps >= 24)
2033                         val |= 0x30;
2034                 else
2035                         val |= 0x20;
2036                 break;
2037         default:
2038                 snd_printdd("invalid format width %d\n",
2039                             snd_pcm_format_width(format));
2040                 return 0;
2041         }
2042
2043         return val;
2044 }
2045
2046 /**
2047  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2048  * @codec: the HDA codec
2049  * @nid: NID to query
2050  * @ratesp: the pointer to store the detected rate bitflags
2051  * @formatsp: the pointer to store the detected formats
2052  * @bpsp: the pointer to store the detected format widths
2053  *
2054  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
2055  * or @bsps argument is ignored.
2056  *
2057  * Returns 0 if successful, otherwise a negative error code.
2058  */
2059 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2060                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2061 {
2062         int i;
2063         unsigned int val, streams;
2064
2065         val = 0;
2066         if (nid != codec->afg &&
2067             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2068                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2069                 if (val == -1)
2070                         return -EIO;
2071         }
2072         if (!val)
2073                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2074
2075         if (ratesp) {
2076                 u32 rates = 0;
2077                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2078                         if (val & (1 << i))
2079                                 rates |= rate_bits[i].alsa_bits;
2080                 }
2081                 *ratesp = rates;
2082         }
2083
2084         if (formatsp || bpsp) {
2085                 u64 formats = 0;
2086                 unsigned int bps;
2087                 unsigned int wcaps;
2088
2089                 wcaps = get_wcaps(codec, nid);
2090                 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2091                 if (streams == -1)
2092                         return -EIO;
2093                 if (!streams) {
2094                         streams = snd_hda_param_read(codec, codec->afg,
2095                                                      AC_PAR_STREAM);
2096                         if (streams == -1)
2097                                 return -EIO;
2098                 }
2099
2100                 bps = 0;
2101                 if (streams & AC_SUPFMT_PCM) {
2102                         if (val & AC_SUPPCM_BITS_8) {
2103                                 formats |= SNDRV_PCM_FMTBIT_U8;
2104                                 bps = 8;
2105                         }
2106                         if (val & AC_SUPPCM_BITS_16) {
2107                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2108                                 bps = 16;
2109                         }
2110                         if (wcaps & AC_WCAP_DIGITAL) {
2111                                 if (val & AC_SUPPCM_BITS_32)
2112                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2113                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2114                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
2115                                 if (val & AC_SUPPCM_BITS_24)
2116                                         bps = 24;
2117                                 else if (val & AC_SUPPCM_BITS_20)
2118                                         bps = 20;
2119                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2120                                           AC_SUPPCM_BITS_32)) {
2121                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2122                                 if (val & AC_SUPPCM_BITS_32)
2123                                         bps = 32;
2124                                 else if (val & AC_SUPPCM_BITS_24)
2125                                         bps = 24;
2126                                 else if (val & AC_SUPPCM_BITS_20)
2127                                         bps = 20;
2128                         }
2129                 }
2130                 else if (streams == AC_SUPFMT_FLOAT32) {
2131                         /* should be exclusive */
2132                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2133                         bps = 32;
2134                 } else if (streams == AC_SUPFMT_AC3) {
2135                         /* should be exclusive */
2136                         /* temporary hack: we have still no proper support
2137                          * for the direct AC3 stream...
2138                          */
2139                         formats |= SNDRV_PCM_FMTBIT_U8;
2140                         bps = 8;
2141                 }
2142                 if (formatsp)
2143                         *formatsp = formats;
2144                 if (bpsp)
2145                         *bpsp = bps;
2146         }
2147
2148         return 0;
2149 }
2150
2151 /**
2152  * snd_hda_is_supported_format - check whether the given node supports
2153  * the format val
2154  *
2155  * Returns 1 if supported, 0 if not.
2156  */
2157 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2158                                 unsigned int format)
2159 {
2160         int i;
2161         unsigned int val = 0, rate, stream;
2162
2163         if (nid != codec->afg &&
2164             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2165                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2166                 if (val == -1)
2167                         return 0;
2168         }
2169         if (!val) {
2170                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2171                 if (val == -1)
2172                         return 0;
2173         }
2174
2175         rate = format & 0xff00;
2176         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2177                 if (rate_bits[i].hda_fmt == rate) {
2178                         if (val & (1 << i))
2179                                 break;
2180                         return 0;
2181                 }
2182         if (i >= AC_PAR_PCM_RATE_BITS)
2183                 return 0;
2184
2185         stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2186         if (stream == -1)
2187                 return 0;
2188         if (!stream && nid != codec->afg)
2189                 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2190         if (!stream || stream == -1)
2191                 return 0;
2192
2193         if (stream & AC_SUPFMT_PCM) {
2194                 switch (format & 0xf0) {
2195                 case 0x00:
2196                         if (!(val & AC_SUPPCM_BITS_8))
2197                                 return 0;
2198                         break;
2199                 case 0x10:
2200                         if (!(val & AC_SUPPCM_BITS_16))
2201                                 return 0;
2202                         break;
2203                 case 0x20:
2204                         if (!(val & AC_SUPPCM_BITS_20))
2205                                 return 0;
2206                         break;
2207                 case 0x30:
2208                         if (!(val & AC_SUPPCM_BITS_24))
2209                                 return 0;
2210                         break;
2211                 case 0x40:
2212                         if (!(val & AC_SUPPCM_BITS_32))
2213                                 return 0;
2214                         break;
2215                 default:
2216                         return 0;
2217                 }
2218         } else {
2219                 /* FIXME: check for float32 and AC3? */
2220         }
2221
2222         return 1;
2223 }
2224
2225 /*
2226  * PCM stuff
2227  */
2228 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2229                                       struct hda_codec *codec,
2230                                       struct snd_pcm_substream *substream)
2231 {
2232         return 0;
2233 }
2234
2235 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2236                                    struct hda_codec *codec,
2237                                    unsigned int stream_tag,
2238                                    unsigned int format,
2239                                    struct snd_pcm_substream *substream)
2240 {
2241         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2242         return 0;
2243 }
2244
2245 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2246                                    struct hda_codec *codec,
2247                                    struct snd_pcm_substream *substream)
2248 {
2249         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2250         return 0;
2251 }
2252
2253 static int __devinit set_pcm_default_values(struct hda_codec *codec,
2254                                             struct hda_pcm_stream *info)
2255 {
2256         /* query support PCM information from the given NID */
2257         if (info->nid && (!info->rates || !info->formats)) {
2258                 snd_hda_query_supported_pcm(codec, info->nid,
2259                                 info->rates ? NULL : &info->rates,
2260                                 info->formats ? NULL : &info->formats,
2261                                 info->maxbps ? NULL : &info->maxbps);
2262         }
2263         if (info->ops.open == NULL)
2264                 info->ops.open = hda_pcm_default_open_close;
2265         if (info->ops.close == NULL)
2266                 info->ops.close = hda_pcm_default_open_close;
2267         if (info->ops.prepare == NULL) {
2268                 if (snd_BUG_ON(!info->nid))
2269                         return -EINVAL;
2270                 info->ops.prepare = hda_pcm_default_prepare;
2271         }
2272         if (info->ops.cleanup == NULL) {
2273                 if (snd_BUG_ON(!info->nid))
2274                         return -EINVAL;
2275                 info->ops.cleanup = hda_pcm_default_cleanup;
2276         }
2277         return 0;
2278 }
2279
2280 /**
2281  * snd_hda_build_pcms - build PCM information
2282  * @bus: the BUS
2283  *
2284  * Create PCM information for each codec included in the bus.
2285  *
2286  * The build_pcms codec patch is requested to set up codec->num_pcms and
2287  * codec->pcm_info properly.  The array is referred by the top-level driver
2288  * to create its PCM instances.
2289  * The allocated codec->pcm_info should be released in codec->patch_ops.free
2290  * callback.
2291  *
2292  * At least, substreams, channels_min and channels_max must be filled for
2293  * each stream.  substreams = 0 indicates that the stream doesn't exist.
2294  * When rates and/or formats are zero, the supported values are queried
2295  * from the given nid.  The nid is used also by the default ops.prepare
2296  * and ops.cleanup callbacks.
2297  *
2298  * The driver needs to call ops.open in its open callback.  Similarly,
2299  * ops.close is supposed to be called in the close callback.
2300  * ops.prepare should be called in the prepare or hw_params callback
2301  * with the proper parameters for set up.
2302  * ops.cleanup should be called in hw_free for clean up of streams.
2303  *
2304  * This function returns 0 if successfull, or a negative error code.
2305  */
2306 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2307 {
2308         struct hda_codec *codec;
2309
2310         list_for_each_entry(codec, &bus->codec_list, list) {
2311                 unsigned int pcm, s;
2312                 int err;
2313                 if (!codec->patch_ops.build_pcms)
2314                         continue;
2315                 err = codec->patch_ops.build_pcms(codec);
2316                 if (err < 0)
2317                         return err;
2318                 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2319                         for (s = 0; s < 2; s++) {
2320                                 struct hda_pcm_stream *info;
2321                                 info = &codec->pcm_info[pcm].stream[s];
2322                                 if (!info->substreams)
2323                                         continue;
2324                                 err = set_pcm_default_values(codec, info);
2325                                 if (err < 0)
2326                                         return err;
2327                         }
2328                 }
2329         }
2330         return 0;
2331 }
2332
2333 /**
2334  * snd_hda_check_board_config - compare the current codec with the config table
2335  * @codec: the HDA codec
2336  * @num_configs: number of config enums
2337  * @models: array of model name strings
2338  * @tbl: configuration table, terminated by null entries
2339  *
2340  * Compares the modelname or PCI subsystem id of the current codec with the
2341  * given configuration table.  If a matching entry is found, returns its
2342  * config value (supposed to be 0 or positive).
2343  *
2344  * If no entries are matching, the function returns a negative value.
2345  */
2346 int snd_hda_check_board_config(struct hda_codec *codec,
2347                                int num_configs, const char **models,
2348                                const struct snd_pci_quirk *tbl)
2349 {
2350         if (codec->bus->modelname && models) {
2351                 int i;
2352                 for (i = 0; i < num_configs; i++) {
2353                         if (models[i] &&
2354                             !strcmp(codec->bus->modelname, models[i])) {
2355                                 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2356                                            "selected\n", models[i]);
2357                                 return i;
2358                         }
2359                 }
2360         }
2361
2362         if (!codec->bus->pci || !tbl)
2363                 return -1;
2364
2365         tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2366         if (!tbl)
2367                 return -1;
2368         if (tbl->value >= 0 && tbl->value < num_configs) {
2369 #ifdef CONFIG_SND_DEBUG_VERBOSE
2370                 char tmp[10];
2371                 const char *model = NULL;
2372                 if (models)
2373                         model = models[tbl->value];
2374                 if (!model) {
2375                         sprintf(tmp, "#%d", tbl->value);
2376                         model = tmp;
2377                 }
2378                 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2379                             "for config %x:%x (%s)\n",
2380                             model, tbl->subvendor, tbl->subdevice,
2381                             (tbl->name ? tbl->name : "Unknown device"));
2382 #endif
2383                 return tbl->value;
2384         }
2385         return -1;
2386 }
2387
2388 /**
2389  * snd_hda_add_new_ctls - create controls from the array
2390  * @codec: the HDA codec
2391  * @knew: the array of struct snd_kcontrol_new
2392  *
2393  * This helper function creates and add new controls in the given array.
2394  * The array must be terminated with an empty entry as terminator.
2395  *
2396  * Returns 0 if successful, or a negative error code.
2397  */
2398 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2399 {
2400         int err;
2401
2402         for (; knew->name; knew++) {
2403                 struct snd_kcontrol *kctl;
2404                 kctl = snd_ctl_new1(knew, codec);
2405                 if (!kctl)
2406                         return -ENOMEM;
2407                 err = snd_ctl_add(codec->bus->card, kctl);
2408                 if (err < 0) {
2409                         if (!codec->addr)
2410                                 return err;
2411                         kctl = snd_ctl_new1(knew, codec);
2412                         if (!kctl)
2413                                 return -ENOMEM;
2414                         kctl->id.device = codec->addr;
2415                         err = snd_ctl_add(codec->bus->card, kctl);
2416                         if (err < 0)
2417                                 return err;
2418                 }
2419         }
2420         return 0;
2421 }
2422
2423 #ifdef CONFIG_SND_HDA_POWER_SAVE
2424 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2425                                 unsigned int power_state);
2426
2427 static void hda_power_work(struct work_struct *work)
2428 {
2429         struct hda_codec *codec =
2430                 container_of(work, struct hda_codec, power_work.work);
2431
2432         if (!codec->power_on || codec->power_count) {
2433                 codec->power_transition = 0;
2434                 return;
2435         }
2436
2437         hda_call_codec_suspend(codec);
2438         if (codec->bus->ops.pm_notify)
2439                 codec->bus->ops.pm_notify(codec);
2440 }
2441
2442 static void hda_keep_power_on(struct hda_codec *codec)
2443 {
2444         codec->power_count++;
2445         codec->power_on = 1;
2446 }
2447
2448 void snd_hda_power_up(struct hda_codec *codec)
2449 {
2450         codec->power_count++;
2451         if (codec->power_on || codec->power_transition)
2452                 return;
2453
2454         codec->power_on = 1;
2455         if (codec->bus->ops.pm_notify)
2456                 codec->bus->ops.pm_notify(codec);
2457         hda_call_codec_resume(codec);
2458         cancel_delayed_work(&codec->power_work);
2459         codec->power_transition = 0;
2460 }
2461
2462 void snd_hda_power_down(struct hda_codec *codec)
2463 {
2464         --codec->power_count;
2465         if (!codec->power_on || codec->power_count || codec->power_transition)
2466                 return;
2467         if (power_save) {
2468                 codec->power_transition = 1; /* avoid reentrance */
2469                 schedule_delayed_work(&codec->power_work,
2470                                       msecs_to_jiffies(power_save * 1000));
2471         }
2472 }
2473
2474 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2475                                  struct hda_loopback_check *check,
2476                                  hda_nid_t nid)
2477 {
2478         struct hda_amp_list *p;
2479         int ch, v;
2480
2481         if (!check->amplist)
2482                 return 0;
2483         for (p = check->amplist; p->nid; p++) {
2484                 if (p->nid == nid)
2485                         break;
2486         }
2487         if (!p->nid)
2488                 return 0; /* nothing changed */
2489
2490         for (p = check->amplist; p->nid; p++) {
2491                 for (ch = 0; ch < 2; ch++) {
2492                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2493                                                    p->idx);
2494                         if (!(v & HDA_AMP_MUTE) && v > 0) {
2495                                 if (!check->power_on) {
2496                                         check->power_on = 1;
2497                                         snd_hda_power_up(codec);
2498                                 }
2499                                 return 1;
2500                         }
2501                 }
2502         }
2503         if (check->power_on) {
2504                 check->power_on = 0;
2505                 snd_hda_power_down(codec);
2506         }
2507         return 0;
2508 }
2509 #endif
2510
2511 /*
2512  * Channel mode helper
2513  */
2514 int snd_hda_ch_mode_info(struct hda_codec *codec,
2515                          struct snd_ctl_elem_info *uinfo,
2516                          const struct hda_channel_mode *chmode,
2517                          int num_chmodes)
2518 {
2519         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2520         uinfo->count = 1;
2521         uinfo->value.enumerated.items = num_chmodes;
2522         if (uinfo->value.enumerated.item >= num_chmodes)
2523                 uinfo->value.enumerated.item = num_chmodes - 1;
2524         sprintf(uinfo->value.enumerated.name, "%dch",
2525                 chmode[uinfo->value.enumerated.item].channels);
2526         return 0;
2527 }
2528
2529 int snd_hda_ch_mode_get(struct hda_codec *codec,
2530                         struct snd_ctl_elem_value *ucontrol,
2531                         const struct hda_channel_mode *chmode,
2532                         int num_chmodes,
2533                         int max_channels)
2534 {
2535         int i;
2536
2537         for (i = 0; i < num_chmodes; i++) {
2538                 if (max_channels == chmode[i].channels) {
2539                         ucontrol->value.enumerated.item[0] = i;
2540                         break;
2541                 }
2542         }
2543         return 0;
2544 }
2545
2546 int snd_hda_ch_mode_put(struct hda_codec *codec,
2547                         struct snd_ctl_elem_value *ucontrol,
2548                         const struct hda_channel_mode *chmode,
2549                         int num_chmodes,
2550                         int *max_channelsp)
2551 {
2552         unsigned int mode;
2553
2554         mode = ucontrol->value.enumerated.item[0];
2555         if (mode >= num_chmodes)
2556                 return -EINVAL;
2557         if (*max_channelsp == chmode[mode].channels)
2558                 return 0;
2559         /* change the current channel setting */
2560         *max_channelsp = chmode[mode].channels;
2561         if (chmode[mode].sequence)
2562                 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2563         return 1;
2564 }
2565
2566 /*
2567  * input MUX helper
2568  */
2569 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2570                            struct snd_ctl_elem_info *uinfo)
2571 {
2572         unsigned int index;
2573
2574         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2575         uinfo->count = 1;
2576         uinfo->value.enumerated.items = imux->num_items;
2577         if (!imux->num_items)
2578                 return 0;
2579         index = uinfo->value.enumerated.item;
2580         if (index >= imux->num_items)
2581                 index = imux->num_items - 1;
2582         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2583         return 0;
2584 }
2585
2586 int snd_hda_input_mux_put(struct hda_codec *codec,
2587                           const struct hda_input_mux *imux,
2588                           struct snd_ctl_elem_value *ucontrol,
2589                           hda_nid_t nid,
2590                           unsigned int *cur_val)
2591 {
2592         unsigned int idx;
2593
2594         if (!imux->num_items)
2595                 return 0;
2596         idx = ucontrol->value.enumerated.item[0];
2597         if (idx >= imux->num_items)
2598                 idx = imux->num_items - 1;
2599         if (*cur_val == idx)
2600                 return 0;
2601         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2602                                   imux->items[idx].index);
2603         *cur_val = idx;
2604         return 1;
2605 }
2606
2607
2608 /*
2609  * Multi-channel / digital-out PCM helper functions
2610  */
2611
2612 /* setup SPDIF output stream */
2613 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2614                                  unsigned int stream_tag, unsigned int format)
2615 {
2616         hda_nid_t *d;
2617
2618         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2619         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
2620                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2621                             codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2622
2623                 if (codec->slave_dig_outs)
2624                         for (d = codec->slave_dig_outs; *d; d++)
2625                                 snd_hda_codec_write(codec, *d, 0,
2626                                     AC_VERB_SET_DIGI_CONVERT_1,
2627                                     codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2628         }
2629         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2630         /* turn on again (if needed) */
2631         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
2632                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2633                                     codec->spdif_ctls & 0xff);
2634
2635                 if (codec->slave_dig_outs)
2636                         for (d = codec->slave_dig_outs; *d; d++)
2637                                 snd_hda_codec_write(codec, *d, 0,
2638                                     AC_VERB_SET_DIGI_CONVERT_1,
2639                                     codec->spdif_ctls & 0xff);
2640         }
2641
2642 }
2643
2644 /*
2645  * open the digital out in the exclusive mode
2646  */
2647 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2648                                struct hda_multi_out *mout)
2649 {
2650         mutex_lock(&codec->spdif_mutex);
2651         if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2652                 /* already opened as analog dup; reset it once */
2653                 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
2654         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2655         mutex_unlock(&codec->spdif_mutex);
2656         return 0;
2657 }
2658
2659 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2660                                   struct hda_multi_out *mout,
2661                                   unsigned int stream_tag,
2662                                   unsigned int format,
2663                                   struct snd_pcm_substream *substream)
2664 {
2665         hda_nid_t *nid;
2666         mutex_lock(&codec->spdif_mutex);
2667         setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2668         if (codec->slave_dig_outs)
2669                 for (nid = codec->slave_dig_outs; *nid; nid++)
2670                         setup_dig_out_stream(codec, *nid, stream_tag, format);
2671         mutex_unlock(&codec->spdif_mutex);
2672         return 0;
2673 }
2674
2675 /*
2676  * release the digital out
2677  */
2678 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2679                                 struct hda_multi_out *mout)
2680 {
2681         mutex_lock(&codec->spdif_mutex);
2682         mout->dig_out_used = 0;
2683         mutex_unlock(&codec->spdif_mutex);
2684         return 0;
2685 }
2686
2687 /*
2688  * set up more restrictions for analog out
2689  */
2690 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2691                                   struct hda_multi_out *mout,
2692                                   struct snd_pcm_substream *substream,
2693                                   struct hda_pcm_stream *hinfo)
2694 {
2695         struct snd_pcm_runtime *runtime = substream->runtime;
2696         runtime->hw.channels_max = mout->max_channels;
2697         if (mout->dig_out_nid) {
2698                 if (!mout->analog_rates) {
2699                         mout->analog_rates = hinfo->rates;
2700                         mout->analog_formats = hinfo->formats;
2701                         mout->analog_maxbps = hinfo->maxbps;
2702                 } else {
2703                         runtime->hw.rates = mout->analog_rates;
2704                         runtime->hw.formats = mout->analog_formats;
2705                         hinfo->maxbps = mout->analog_maxbps;
2706                 }
2707                 if (!mout->spdif_rates) {
2708                         snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2709                                                     &mout->spdif_rates,
2710                                                     &mout->spdif_formats,
2711                                                     &mout->spdif_maxbps);
2712                 }
2713                 mutex_lock(&codec->spdif_mutex);
2714                 if (mout->share_spdif) {
2715                         runtime->hw.rates &= mout->spdif_rates;
2716                         runtime->hw.formats &= mout->spdif_formats;
2717                         if (mout->spdif_maxbps < hinfo->maxbps)
2718                                 hinfo->maxbps = mout->spdif_maxbps;
2719                 }
2720                 mutex_unlock(&codec->spdif_mutex);
2721         }
2722         return snd_pcm_hw_constraint_step(substream->runtime, 0,
2723                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2724 }
2725
2726 /*
2727  * set up the i/o for analog out
2728  * when the digital out is available, copy the front out to digital out, too.
2729  */
2730 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2731                                      struct hda_multi_out *mout,
2732                                      unsigned int stream_tag,
2733                                      unsigned int format,
2734                                      struct snd_pcm_substream *substream)
2735 {
2736         hda_nid_t *nids = mout->dac_nids;
2737         hda_nid_t *d;
2738         int chs = substream->runtime->channels;
2739         int i;
2740
2741         mutex_lock(&codec->spdif_mutex);
2742         if (mout->dig_out_nid && mout->share_spdif &&
2743             mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2744                 if (chs == 2 &&
2745                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
2746                                                 format) &&
2747                     !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2748                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2749                         setup_dig_out_stream(codec, mout->dig_out_nid,
2750                                              stream_tag, format);
2751                         if (codec->slave_dig_outs)
2752                                 for (d = codec->slave_dig_outs; *d; d++)
2753                                         setup_dig_out_stream(codec, *d,
2754                                                 stream_tag, format);
2755                 } else {
2756                         mout->dig_out_used = 0;
2757                         snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
2758                         if (codec->slave_dig_outs)
2759                                 for (d = codec->slave_dig_outs; *d; d++)
2760                                         snd_hda_codec_cleanup_stream(codec, *d);
2761                 }
2762         }
2763         mutex_unlock(&codec->spdif_mutex);
2764
2765         /* front */
2766         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2767                                    0, format);
2768         if (!mout->no_share_stream &&
2769             mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2770                 /* headphone out will just decode front left/right (stereo) */
2771                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2772                                            0, format);
2773         /* extra outputs copied from front */
2774         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2775                 if (!mout->no_share_stream && mout->extra_out_nid[i])
2776                         snd_hda_codec_setup_stream(codec,
2777                                                    mout->extra_out_nid[i],
2778                                                    stream_tag, 0, format);
2779
2780         /* surrounds */
2781         for (i = 1; i < mout->num_dacs; i++) {
2782                 if (chs >= (i + 1) * 2) /* independent out */
2783                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2784                                                    i * 2, format);
2785                 else if (!mout->no_share_stream) /* copy front */
2786                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2787                                                    0, format);
2788         }
2789         return 0;
2790 }
2791
2792 /*
2793  * clean up the setting for analog out
2794  */
2795 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2796                                      struct hda_multi_out *mout)
2797 {
2798         hda_nid_t *nids = mout->dac_nids;
2799         int i;
2800
2801         for (i = 0; i < mout->num_dacs; i++)
2802                 snd_hda_codec_cleanup_stream(codec, nids[i]);
2803         if (mout->hp_nid)
2804                 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2805         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2806                 if (mout->extra_out_nid[i])
2807                         snd_hda_codec_cleanup_stream(codec,
2808                                                      mout->extra_out_nid[i]);
2809         mutex_lock(&codec->spdif_mutex);
2810         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2811                 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
2812                 mout->dig_out_used = 0;
2813         }
2814         mutex_unlock(&codec->spdif_mutex);
2815         return 0;
2816 }
2817
2818 /*
2819  * Helper for automatic ping configuration
2820  */
2821
2822 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2823 {
2824         for (; *list; list++)
2825                 if (*list == nid)
2826                         return 1;
2827         return 0;
2828 }
2829
2830
2831 /*
2832  * Sort an associated group of pins according to their sequence numbers.
2833  */
2834 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2835                                   int num_pins)
2836 {
2837         int i, j;
2838         short seq;
2839         hda_nid_t nid;
2840         
2841         for (i = 0; i < num_pins; i++) {
2842                 for (j = i + 1; j < num_pins; j++) {
2843                         if (sequences[i] > sequences[j]) {
2844                                 seq = sequences[i];
2845                                 sequences[i] = sequences[j];
2846                                 sequences[j] = seq;
2847                                 nid = pins[i];
2848                                 pins[i] = pins[j];
2849                                 pins[j] = nid;
2850                         }
2851                 }
2852         }
2853 }
2854
2855
2856 /*
2857  * Parse all pin widgets and store the useful pin nids to cfg
2858  *
2859  * The number of line-outs or any primary output is stored in line_outs,
2860  * and the corresponding output pins are assigned to line_out_pins[],
2861  * in the order of front, rear, CLFE, side, ...
2862  *
2863  * If more extra outputs (speaker and headphone) are found, the pins are
2864  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
2865  * is detected, one of speaker of HP pins is assigned as the primary
2866  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
2867  * if any analog output exists.
2868  * 
2869  * The analog input pins are assigned to input_pins array.
2870  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2871  * respectively.
2872  */
2873 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2874                                  struct auto_pin_cfg *cfg,
2875                                  hda_nid_t *ignore_nids)
2876 {
2877         hda_nid_t nid, end_nid;
2878         short seq, assoc_line_out, assoc_speaker;
2879         short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2880         short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2881         short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
2882
2883         memset(cfg, 0, sizeof(*cfg));
2884
2885         memset(sequences_line_out, 0, sizeof(sequences_line_out));
2886         memset(sequences_speaker, 0, sizeof(sequences_speaker));
2887         memset(sequences_hp, 0, sizeof(sequences_hp));
2888         assoc_line_out = assoc_speaker = 0;
2889
2890         end_nid = codec->start_nid + codec->num_nodes;
2891         for (nid = codec->start_nid; nid < end_nid; nid++) {
2892                 unsigned int wid_caps = get_wcaps(codec, nid);
2893                 unsigned int wid_type =
2894                         (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2895                 unsigned int def_conf;
2896                 short assoc, loc;
2897
2898                 /* read all default configuration for pin complex */
2899                 if (wid_type != AC_WID_PIN)
2900                         continue;
2901                 /* ignore the given nids (e.g. pc-beep returns error) */
2902                 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2903                         continue;
2904
2905                 def_conf = snd_hda_codec_read(codec, nid, 0,
2906                                               AC_VERB_GET_CONFIG_DEFAULT, 0);
2907                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2908                         continue;
2909                 loc = get_defcfg_location(def_conf);
2910                 switch (get_defcfg_device(def_conf)) {
2911                 case AC_JACK_LINE_OUT:
2912                         seq = get_defcfg_sequence(def_conf);
2913                         assoc = get_defcfg_association(def_conf);
2914
2915                         if (!(wid_caps & AC_WCAP_STEREO))
2916                                 if (!cfg->mono_out_pin)
2917                                         cfg->mono_out_pin = nid;
2918                         if (!assoc)
2919                                 continue;
2920                         if (!assoc_line_out)
2921                                 assoc_line_out = assoc;
2922                         else if (assoc_line_out != assoc)
2923                                 continue;
2924                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2925                                 continue;
2926                         cfg->line_out_pins[cfg->line_outs] = nid;
2927                         sequences_line_out[cfg->line_outs] = seq;
2928                         cfg->line_outs++;
2929                         break;
2930                 case AC_JACK_SPEAKER:
2931                         seq = get_defcfg_sequence(def_conf);
2932                         assoc = get_defcfg_association(def_conf);
2933                         if (! assoc)
2934                                 continue;
2935                         if (! assoc_speaker)
2936                                 assoc_speaker = assoc;
2937                         else if (assoc_speaker != assoc)
2938                                 continue;
2939                         if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2940                                 continue;
2941                         cfg->speaker_pins[cfg->speaker_outs] = nid;
2942                         sequences_speaker[cfg->speaker_outs] = seq;
2943                         cfg->speaker_outs++;
2944                         break;
2945                 case AC_JACK_HP_OUT:
2946                         seq = get_defcfg_sequence(def_conf);
2947                         assoc = get_defcfg_association(def_conf);
2948                         if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2949                                 continue;
2950                         cfg->hp_pins[cfg->hp_outs] = nid;
2951                         sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
2952                         cfg->hp_outs++;
2953                         break;
2954                 case AC_JACK_MIC_IN: {
2955                         int preferred, alt;
2956                         if (loc == AC_JACK_LOC_FRONT) {
2957                                 preferred = AUTO_PIN_FRONT_MIC;
2958                                 alt = AUTO_PIN_MIC;
2959                         } else {
2960                                 preferred = AUTO_PIN_MIC;
2961                                 alt = AUTO_PIN_FRONT_MIC;
2962                         }
2963                         if (!cfg->input_pins[preferred])
2964                                 cfg->input_pins[preferred] = nid;
2965                         else if (!cfg->input_pins[alt])
2966                                 cfg->input_pins[alt] = nid;
2967                         break;
2968                 }
2969                 case AC_JACK_LINE_IN:
2970                         if (loc == AC_JACK_LOC_FRONT)
2971                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2972                         else
2973                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
2974                         break;
2975                 case AC_JACK_CD:
2976                         cfg->input_pins[AUTO_PIN_CD] = nid;
2977                         break;
2978                 case AC_JACK_AUX:
2979                         cfg->input_pins[AUTO_PIN_AUX] = nid;
2980                         break;
2981                 case AC_JACK_SPDIF_OUT:
2982                         cfg->dig_out_pin = nid;
2983                         break;
2984                 case AC_JACK_SPDIF_IN:
2985                         cfg->dig_in_pin = nid;
2986                         break;
2987                 }
2988         }
2989
2990         /* FIX-UP:
2991          * If no line-out is defined but multiple HPs are found,
2992          * some of them might be the real line-outs.
2993          */
2994         if (!cfg->line_outs && cfg->hp_outs > 1) {
2995                 int i = 0;
2996                 while (i < cfg->hp_outs) {
2997                         /* The real HPs should have the sequence 0x0f */
2998                         if ((sequences_hp[i] & 0x0f) == 0x0f) {
2999                                 i++;
3000                                 continue;
3001                         }
3002                         /* Move it to the line-out table */
3003                         cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3004                         sequences_line_out[cfg->line_outs] = sequences_hp[i];
3005                         cfg->line_outs++;
3006                         cfg->hp_outs--;
3007                         memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3008                                 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3009                         memmove(sequences_hp + i - 1, sequences_hp + i,
3010                                 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3011                 }
3012         }
3013
3014         /* sort by sequence */
3015         sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3016                               cfg->line_outs);
3017         sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3018                               cfg->speaker_outs);
3019         sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3020                               cfg->hp_outs);
3021         
3022         /* if we have only one mic, make it AUTO_PIN_MIC */
3023         if (!cfg->input_pins[AUTO_PIN_MIC] &&
3024             cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3025                 cfg->input_pins[AUTO_PIN_MIC] =
3026                         cfg->input_pins[AUTO_PIN_FRONT_MIC];
3027                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3028         }
3029         /* ditto for line-in */
3030         if (!cfg->input_pins[AUTO_PIN_LINE] &&
3031             cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3032                 cfg->input_pins[AUTO_PIN_LINE] =
3033                         cfg->input_pins[AUTO_PIN_FRONT_LINE];
3034                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3035         }
3036
3037         /*
3038          * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3039          * as a primary output
3040          */
3041         if (!cfg->line_outs) {
3042                 if (cfg->speaker_outs) {
3043                         cfg->line_outs = cfg->speaker_outs;
3044                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3045                                sizeof(cfg->speaker_pins));
3046                         cfg->speaker_outs = 0;
3047                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3048                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3049                 } else if (cfg->hp_outs) {
3050                         cfg->line_outs = cfg->hp_outs;
3051                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3052                                sizeof(cfg->hp_pins));
3053                         cfg->hp_outs = 0;
3054                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3055                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3056                 }
3057         }
3058
3059         /* Reorder the surround channels
3060          * ALSA sequence is front/surr/clfe/side
3061          * HDA sequence is:
3062          *    4-ch: front/surr  =>  OK as it is
3063          *    6-ch: front/clfe/surr
3064          *    8-ch: front/clfe/rear/side|fc
3065          */
3066         switch (cfg->line_outs) {
3067         case 3:
3068         case 4:
3069                 nid = cfg->line_out_pins[1];
3070                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3071                 cfg->line_out_pins[2] = nid;
3072                 break;
3073         }
3074
3075         /*
3076          * debug prints of the parsed results
3077          */
3078         snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3079                    cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3080                    cfg->line_out_pins[2], cfg->line_out_pins[3],
3081                    cfg->line_out_pins[4]);
3082         snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3083                    cfg->speaker_outs, cfg->speaker_pins[0],
3084                    cfg->speaker_pins[1], cfg->speaker_pins[2],
3085                    cfg->speaker_pins[3], cfg->speaker_pins[4]);
3086         snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3087                    cfg->hp_outs, cfg->hp_pins[0],
3088                    cfg->hp_pins[1], cfg->hp_pins[2],
3089                    cfg->hp_pins[3], cfg->hp_pins[4]);
3090         snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
3091         snd_printd("   inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3092                    " cd=0x%x, aux=0x%x\n",
3093                    cfg->input_pins[AUTO_PIN_MIC],
3094                    cfg->input_pins[AUTO_PIN_FRONT_MIC],
3095                    cfg->input_pins[AUTO_PIN_LINE],
3096                    cfg->input_pins[AUTO_PIN_FRONT_LINE],
3097                    cfg->input_pins[AUTO_PIN_CD],
3098                    cfg->input_pins[AUTO_PIN_AUX]);
3099
3100         return 0;
3101 }
3102
3103 /* labels for input pins */
3104 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3105         "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3106 };
3107
3108
3109 #ifdef CONFIG_PM
3110 /*
3111  * power management
3112  */
3113
3114 /**
3115  * snd_hda_suspend - suspend the codecs
3116  * @bus: the HDA bus
3117  * @state: suspsend state
3118  *
3119  * Returns 0 if successful.
3120  */
3121 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3122 {
3123         struct hda_codec *codec;
3124
3125         list_for_each_entry(codec, &bus->codec_list, list) {
3126 #ifdef CONFIG_SND_HDA_POWER_SAVE
3127                 if (!codec->power_on)
3128                         continue;
3129 #endif
3130                 hda_call_codec_suspend(codec);
3131         }
3132         return 0;
3133 }
3134
3135 /**
3136  * snd_hda_resume - resume the codecs
3137  * @bus: the HDA bus
3138  * @state: resume state
3139  *
3140  * Returns 0 if successful.
3141  *
3142  * This fucntion is defined only when POWER_SAVE isn't set.
3143  * In the power-save mode, the codec is resumed dynamically.
3144  */
3145 int snd_hda_resume(struct hda_bus *bus)
3146 {
3147         struct hda_codec *codec;
3148
3149         list_for_each_entry(codec, &bus->codec_list, list) {
3150                 if (snd_hda_codec_needs_resume(codec))
3151                         hda_call_codec_resume(codec);
3152         }
3153         return 0;
3154 }
3155 #ifdef CONFIG_SND_HDA_POWER_SAVE
3156 int snd_hda_codecs_inuse(struct hda_bus *bus)
3157 {
3158         struct hda_codec *codec;
3159
3160         list_for_each_entry(codec, &bus->codec_list, list) {
3161                 if (snd_hda_codec_needs_resume(codec))
3162                         return 1;
3163         }
3164         return 0;
3165 }
3166 #endif
3167 #endif