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