]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/pci/hda/hda_codec.c
ALSA: hda - Retry codec-verbs at errors
[mirror_ubuntu-bionic-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)
3160 {
3161 struct hda_bus *bus = codec->bus;
3162
3163 codec->power_count++;
3164 if (codec->power_on || codec->power_transition)
3165 return;
3166
3167 codec->power_on = 1;
3168 if (bus->ops.pm_notify)
3169 bus->ops.pm_notify(bus);
3170 hda_call_codec_resume(codec);
3171 cancel_delayed_work(&codec->power_work);
3172 codec->power_transition = 0;
3173 }
3174 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3175
3176 #define power_save(codec) \
3177 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3178
3179 #define power_save(codec) \
3180 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3181
3182 void snd_hda_power_down(struct hda_codec *codec)
3183 {
3184 --codec->power_count;
3185 if (!codec->power_on || codec->power_count || codec->power_transition)
3186 return;
3187 if (power_save(codec)) {
3188 codec->power_transition = 1; /* avoid reentrance */
3189 queue_delayed_work(codec->bus->workq, &codec->power_work,
3190 msecs_to_jiffies(power_save(codec) * 1000));
3191 }
3192 }
3193 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3194
3195 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3196 struct hda_loopback_check *check,
3197 hda_nid_t nid)
3198 {
3199 struct hda_amp_list *p;
3200 int ch, v;
3201
3202 if (!check->amplist)
3203 return 0;
3204 for (p = check->amplist; p->nid; p++) {
3205 if (p->nid == nid)
3206 break;
3207 }
3208 if (!p->nid)
3209 return 0; /* nothing changed */
3210
3211 for (p = check->amplist; p->nid; p++) {
3212 for (ch = 0; ch < 2; ch++) {
3213 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3214 p->idx);
3215 if (!(v & HDA_AMP_MUTE) && v > 0) {
3216 if (!check->power_on) {
3217 check->power_on = 1;
3218 snd_hda_power_up(codec);
3219 }
3220 return 1;
3221 }
3222 }
3223 }
3224 if (check->power_on) {
3225 check->power_on = 0;
3226 snd_hda_power_down(codec);
3227 }
3228 return 0;
3229 }
3230 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3231 #endif
3232
3233 /*
3234 * Channel mode helper
3235 */
3236 int snd_hda_ch_mode_info(struct hda_codec *codec,
3237 struct snd_ctl_elem_info *uinfo,
3238 const struct hda_channel_mode *chmode,
3239 int num_chmodes)
3240 {
3241 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3242 uinfo->count = 1;
3243 uinfo->value.enumerated.items = num_chmodes;
3244 if (uinfo->value.enumerated.item >= num_chmodes)
3245 uinfo->value.enumerated.item = num_chmodes - 1;
3246 sprintf(uinfo->value.enumerated.name, "%dch",
3247 chmode[uinfo->value.enumerated.item].channels);
3248 return 0;
3249 }
3250 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3251
3252 int snd_hda_ch_mode_get(struct hda_codec *codec,
3253 struct snd_ctl_elem_value *ucontrol,
3254 const struct hda_channel_mode *chmode,
3255 int num_chmodes,
3256 int max_channels)
3257 {
3258 int i;
3259
3260 for (i = 0; i < num_chmodes; i++) {
3261 if (max_channels == chmode[i].channels) {
3262 ucontrol->value.enumerated.item[0] = i;
3263 break;
3264 }
3265 }
3266 return 0;
3267 }
3268 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3269
3270 int snd_hda_ch_mode_put(struct hda_codec *codec,
3271 struct snd_ctl_elem_value *ucontrol,
3272 const struct hda_channel_mode *chmode,
3273 int num_chmodes,
3274 int *max_channelsp)
3275 {
3276 unsigned int mode;
3277
3278 mode = ucontrol->value.enumerated.item[0];
3279 if (mode >= num_chmodes)
3280 return -EINVAL;
3281 if (*max_channelsp == chmode[mode].channels)
3282 return 0;
3283 /* change the current channel setting */
3284 *max_channelsp = chmode[mode].channels;
3285 if (chmode[mode].sequence)
3286 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3287 return 1;
3288 }
3289 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3290
3291 /*
3292 * input MUX helper
3293 */
3294 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3295 struct snd_ctl_elem_info *uinfo)
3296 {
3297 unsigned int index;
3298
3299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3300 uinfo->count = 1;
3301 uinfo->value.enumerated.items = imux->num_items;
3302 if (!imux->num_items)
3303 return 0;
3304 index = uinfo->value.enumerated.item;
3305 if (index >= imux->num_items)
3306 index = imux->num_items - 1;
3307 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3308 return 0;
3309 }
3310 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3311
3312 int snd_hda_input_mux_put(struct hda_codec *codec,
3313 const struct hda_input_mux *imux,
3314 struct snd_ctl_elem_value *ucontrol,
3315 hda_nid_t nid,
3316 unsigned int *cur_val)
3317 {
3318 unsigned int idx;
3319
3320 if (!imux->num_items)
3321 return 0;
3322 idx = ucontrol->value.enumerated.item[0];
3323 if (idx >= imux->num_items)
3324 idx = imux->num_items - 1;
3325 if (*cur_val == idx)
3326 return 0;
3327 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3328 imux->items[idx].index);
3329 *cur_val = idx;
3330 return 1;
3331 }
3332 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3333
3334
3335 /*
3336 * Multi-channel / digital-out PCM helper functions
3337 */
3338
3339 /* setup SPDIF output stream */
3340 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3341 unsigned int stream_tag, unsigned int format)
3342 {
3343 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3344 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3345 set_dig_out_convert(codec, nid,
3346 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3347 -1);
3348 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3349 if (codec->slave_dig_outs) {
3350 hda_nid_t *d;
3351 for (d = codec->slave_dig_outs; *d; d++)
3352 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3353 format);
3354 }
3355 /* turn on again (if needed) */
3356 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3357 set_dig_out_convert(codec, nid,
3358 codec->spdif_ctls & 0xff, -1);
3359 }
3360
3361 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3362 {
3363 snd_hda_codec_cleanup_stream(codec, nid);
3364 if (codec->slave_dig_outs) {
3365 hda_nid_t *d;
3366 for (d = codec->slave_dig_outs; *d; d++)
3367 snd_hda_codec_cleanup_stream(codec, *d);
3368 }
3369 }
3370
3371 /*
3372 * open the digital out in the exclusive mode
3373 */
3374 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3375 struct hda_multi_out *mout)
3376 {
3377 mutex_lock(&codec->spdif_mutex);
3378 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3379 /* already opened as analog dup; reset it once */
3380 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3381 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3382 mutex_unlock(&codec->spdif_mutex);
3383 return 0;
3384 }
3385 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3386
3387 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3388 struct hda_multi_out *mout,
3389 unsigned int stream_tag,
3390 unsigned int format,
3391 struct snd_pcm_substream *substream)
3392 {
3393 mutex_lock(&codec->spdif_mutex);
3394 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3395 mutex_unlock(&codec->spdif_mutex);
3396 return 0;
3397 }
3398 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3399
3400 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3401 struct hda_multi_out *mout)
3402 {
3403 mutex_lock(&codec->spdif_mutex);
3404 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3405 mutex_unlock(&codec->spdif_mutex);
3406 return 0;
3407 }
3408 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3409
3410 /*
3411 * release the digital out
3412 */
3413 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3414 struct hda_multi_out *mout)
3415 {
3416 mutex_lock(&codec->spdif_mutex);
3417 mout->dig_out_used = 0;
3418 mutex_unlock(&codec->spdif_mutex);
3419 return 0;
3420 }
3421 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3422
3423 /*
3424 * set up more restrictions for analog out
3425 */
3426 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3427 struct hda_multi_out *mout,
3428 struct snd_pcm_substream *substream,
3429 struct hda_pcm_stream *hinfo)
3430 {
3431 struct snd_pcm_runtime *runtime = substream->runtime;
3432 runtime->hw.channels_max = mout->max_channels;
3433 if (mout->dig_out_nid) {
3434 if (!mout->analog_rates) {
3435 mout->analog_rates = hinfo->rates;
3436 mout->analog_formats = hinfo->formats;
3437 mout->analog_maxbps = hinfo->maxbps;
3438 } else {
3439 runtime->hw.rates = mout->analog_rates;
3440 runtime->hw.formats = mout->analog_formats;
3441 hinfo->maxbps = mout->analog_maxbps;
3442 }
3443 if (!mout->spdif_rates) {
3444 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3445 &mout->spdif_rates,
3446 &mout->spdif_formats,
3447 &mout->spdif_maxbps);
3448 }
3449 mutex_lock(&codec->spdif_mutex);
3450 if (mout->share_spdif) {
3451 runtime->hw.rates &= mout->spdif_rates;
3452 runtime->hw.formats &= mout->spdif_formats;
3453 if (mout->spdif_maxbps < hinfo->maxbps)
3454 hinfo->maxbps = mout->spdif_maxbps;
3455 }
3456 mutex_unlock(&codec->spdif_mutex);
3457 }
3458 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3459 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3460 }
3461 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3462
3463 /*
3464 * set up the i/o for analog out
3465 * when the digital out is available, copy the front out to digital out, too.
3466 */
3467 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3468 struct hda_multi_out *mout,
3469 unsigned int stream_tag,
3470 unsigned int format,
3471 struct snd_pcm_substream *substream)
3472 {
3473 hda_nid_t *nids = mout->dac_nids;
3474 int chs = substream->runtime->channels;
3475 int i;
3476
3477 mutex_lock(&codec->spdif_mutex);
3478 if (mout->dig_out_nid && mout->share_spdif &&
3479 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3480 if (chs == 2 &&
3481 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3482 format) &&
3483 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3484 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3485 setup_dig_out_stream(codec, mout->dig_out_nid,
3486 stream_tag, format);
3487 } else {
3488 mout->dig_out_used = 0;
3489 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3490 }
3491 }
3492 mutex_unlock(&codec->spdif_mutex);
3493
3494 /* front */
3495 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3496 0, format);
3497 if (!mout->no_share_stream &&
3498 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3499 /* headphone out will just decode front left/right (stereo) */
3500 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3501 0, format);
3502 /* extra outputs copied from front */
3503 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3504 if (!mout->no_share_stream && mout->extra_out_nid[i])
3505 snd_hda_codec_setup_stream(codec,
3506 mout->extra_out_nid[i],
3507 stream_tag, 0, format);
3508
3509 /* surrounds */
3510 for (i = 1; i < mout->num_dacs; i++) {
3511 if (chs >= (i + 1) * 2) /* independent out */
3512 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3513 i * 2, format);
3514 else if (!mout->no_share_stream) /* copy front */
3515 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3516 0, format);
3517 }
3518 return 0;
3519 }
3520 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3521
3522 /*
3523 * clean up the setting for analog out
3524 */
3525 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3526 struct hda_multi_out *mout)
3527 {
3528 hda_nid_t *nids = mout->dac_nids;
3529 int i;
3530
3531 for (i = 0; i < mout->num_dacs; i++)
3532 snd_hda_codec_cleanup_stream(codec, nids[i]);
3533 if (mout->hp_nid)
3534 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3535 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3536 if (mout->extra_out_nid[i])
3537 snd_hda_codec_cleanup_stream(codec,
3538 mout->extra_out_nid[i]);
3539 mutex_lock(&codec->spdif_mutex);
3540 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3541 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3542 mout->dig_out_used = 0;
3543 }
3544 mutex_unlock(&codec->spdif_mutex);
3545 return 0;
3546 }
3547 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3548
3549 /*
3550 * Helper for automatic pin configuration
3551 */
3552
3553 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3554 {
3555 for (; *list; list++)
3556 if (*list == nid)
3557 return 1;
3558 return 0;
3559 }
3560
3561
3562 /*
3563 * Sort an associated group of pins according to their sequence numbers.
3564 */
3565 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3566 int num_pins)
3567 {
3568 int i, j;
3569 short seq;
3570 hda_nid_t nid;
3571
3572 for (i = 0; i < num_pins; i++) {
3573 for (j = i + 1; j < num_pins; j++) {
3574 if (sequences[i] > sequences[j]) {
3575 seq = sequences[i];
3576 sequences[i] = sequences[j];
3577 sequences[j] = seq;
3578 nid = pins[i];
3579 pins[i] = pins[j];
3580 pins[j] = nid;
3581 }
3582 }
3583 }
3584 }
3585
3586
3587 /*
3588 * Parse all pin widgets and store the useful pin nids to cfg
3589 *
3590 * The number of line-outs or any primary output is stored in line_outs,
3591 * and the corresponding output pins are assigned to line_out_pins[],
3592 * in the order of front, rear, CLFE, side, ...
3593 *
3594 * If more extra outputs (speaker and headphone) are found, the pins are
3595 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3596 * is detected, one of speaker of HP pins is assigned as the primary
3597 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3598 * if any analog output exists.
3599 *
3600 * The analog input pins are assigned to input_pins array.
3601 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3602 * respectively.
3603 */
3604 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3605 struct auto_pin_cfg *cfg,
3606 hda_nid_t *ignore_nids)
3607 {
3608 hda_nid_t nid, end_nid;
3609 short seq, assoc_line_out, assoc_speaker;
3610 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3611 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3612 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3613
3614 memset(cfg, 0, sizeof(*cfg));
3615
3616 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3617 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3618 memset(sequences_hp, 0, sizeof(sequences_hp));
3619 assoc_line_out = assoc_speaker = 0;
3620
3621 end_nid = codec->start_nid + codec->num_nodes;
3622 for (nid = codec->start_nid; nid < end_nid; nid++) {
3623 unsigned int wid_caps = get_wcaps(codec, nid);
3624 unsigned int wid_type =
3625 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3626 unsigned int def_conf;
3627 short assoc, loc;
3628
3629 /* read all default configuration for pin complex */
3630 if (wid_type != AC_WID_PIN)
3631 continue;
3632 /* ignore the given nids (e.g. pc-beep returns error) */
3633 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3634 continue;
3635
3636 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3637 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3638 continue;
3639 loc = get_defcfg_location(def_conf);
3640 switch (get_defcfg_device(def_conf)) {
3641 case AC_JACK_LINE_OUT:
3642 seq = get_defcfg_sequence(def_conf);
3643 assoc = get_defcfg_association(def_conf);
3644
3645 if (!(wid_caps & AC_WCAP_STEREO))
3646 if (!cfg->mono_out_pin)
3647 cfg->mono_out_pin = nid;
3648 if (!assoc)
3649 continue;
3650 if (!assoc_line_out)
3651 assoc_line_out = assoc;
3652 else if (assoc_line_out != assoc)
3653 continue;
3654 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3655 continue;
3656 cfg->line_out_pins[cfg->line_outs] = nid;
3657 sequences_line_out[cfg->line_outs] = seq;
3658 cfg->line_outs++;
3659 break;
3660 case AC_JACK_SPEAKER:
3661 seq = get_defcfg_sequence(def_conf);
3662 assoc = get_defcfg_association(def_conf);
3663 if (! assoc)
3664 continue;
3665 if (! assoc_speaker)
3666 assoc_speaker = assoc;
3667 else if (assoc_speaker != assoc)
3668 continue;
3669 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3670 continue;
3671 cfg->speaker_pins[cfg->speaker_outs] = nid;
3672 sequences_speaker[cfg->speaker_outs] = seq;
3673 cfg->speaker_outs++;
3674 break;
3675 case AC_JACK_HP_OUT:
3676 seq = get_defcfg_sequence(def_conf);
3677 assoc = get_defcfg_association(def_conf);
3678 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3679 continue;
3680 cfg->hp_pins[cfg->hp_outs] = nid;
3681 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3682 cfg->hp_outs++;
3683 break;
3684 case AC_JACK_MIC_IN: {
3685 int preferred, alt;
3686 if (loc == AC_JACK_LOC_FRONT) {
3687 preferred = AUTO_PIN_FRONT_MIC;
3688 alt = AUTO_PIN_MIC;
3689 } else {
3690 preferred = AUTO_PIN_MIC;
3691 alt = AUTO_PIN_FRONT_MIC;
3692 }
3693 if (!cfg->input_pins[preferred])
3694 cfg->input_pins[preferred] = nid;
3695 else if (!cfg->input_pins[alt])
3696 cfg->input_pins[alt] = nid;
3697 break;
3698 }
3699 case AC_JACK_LINE_IN:
3700 if (loc == AC_JACK_LOC_FRONT)
3701 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3702 else
3703 cfg->input_pins[AUTO_PIN_LINE] = nid;
3704 break;
3705 case AC_JACK_CD:
3706 cfg->input_pins[AUTO_PIN_CD] = nid;
3707 break;
3708 case AC_JACK_AUX:
3709 cfg->input_pins[AUTO_PIN_AUX] = nid;
3710 break;
3711 case AC_JACK_SPDIF_OUT:
3712 case AC_JACK_DIG_OTHER_OUT:
3713 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3714 continue;
3715 cfg->dig_out_pins[cfg->dig_outs] = nid;
3716 cfg->dig_out_type[cfg->dig_outs] =
3717 (loc == AC_JACK_LOC_HDMI) ?
3718 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3719 cfg->dig_outs++;
3720 break;
3721 case AC_JACK_SPDIF_IN:
3722 case AC_JACK_DIG_OTHER_IN:
3723 cfg->dig_in_pin = nid;
3724 if (loc == AC_JACK_LOC_HDMI)
3725 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3726 else
3727 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3728 break;
3729 }
3730 }
3731
3732 /* FIX-UP:
3733 * If no line-out is defined but multiple HPs are found,
3734 * some of them might be the real line-outs.
3735 */
3736 if (!cfg->line_outs && cfg->hp_outs > 1) {
3737 int i = 0;
3738 while (i < cfg->hp_outs) {
3739 /* The real HPs should have the sequence 0x0f */
3740 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3741 i++;
3742 continue;
3743 }
3744 /* Move it to the line-out table */
3745 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3746 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3747 cfg->line_outs++;
3748 cfg->hp_outs--;
3749 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3750 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3751 memmove(sequences_hp + i - 1, sequences_hp + i,
3752 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3753 }
3754 }
3755
3756 /* sort by sequence */
3757 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3758 cfg->line_outs);
3759 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3760 cfg->speaker_outs);
3761 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3762 cfg->hp_outs);
3763
3764 /* if we have only one mic, make it AUTO_PIN_MIC */
3765 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3766 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3767 cfg->input_pins[AUTO_PIN_MIC] =
3768 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3769 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3770 }
3771 /* ditto for line-in */
3772 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3773 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3774 cfg->input_pins[AUTO_PIN_LINE] =
3775 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3776 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3777 }
3778
3779 /*
3780 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3781 * as a primary output
3782 */
3783 if (!cfg->line_outs) {
3784 if (cfg->speaker_outs) {
3785 cfg->line_outs = cfg->speaker_outs;
3786 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3787 sizeof(cfg->speaker_pins));
3788 cfg->speaker_outs = 0;
3789 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3790 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3791 } else if (cfg->hp_outs) {
3792 cfg->line_outs = cfg->hp_outs;
3793 memcpy(cfg->line_out_pins, cfg->hp_pins,
3794 sizeof(cfg->hp_pins));
3795 cfg->hp_outs = 0;
3796 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3797 cfg->line_out_type = AUTO_PIN_HP_OUT;
3798 }
3799 }
3800
3801 /* Reorder the surround channels
3802 * ALSA sequence is front/surr/clfe/side
3803 * HDA sequence is:
3804 * 4-ch: front/surr => OK as it is
3805 * 6-ch: front/clfe/surr
3806 * 8-ch: front/clfe/rear/side|fc
3807 */
3808 switch (cfg->line_outs) {
3809 case 3:
3810 case 4:
3811 nid = cfg->line_out_pins[1];
3812 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3813 cfg->line_out_pins[2] = nid;
3814 break;
3815 }
3816
3817 /*
3818 * debug prints of the parsed results
3819 */
3820 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3821 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3822 cfg->line_out_pins[2], cfg->line_out_pins[3],
3823 cfg->line_out_pins[4]);
3824 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3825 cfg->speaker_outs, cfg->speaker_pins[0],
3826 cfg->speaker_pins[1], cfg->speaker_pins[2],
3827 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3828 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3829 cfg->hp_outs, cfg->hp_pins[0],
3830 cfg->hp_pins[1], cfg->hp_pins[2],
3831 cfg->hp_pins[3], cfg->hp_pins[4]);
3832 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3833 if (cfg->dig_outs)
3834 snd_printd(" dig-out=0x%x/0x%x\n",
3835 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3836 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3837 " cd=0x%x, aux=0x%x\n",
3838 cfg->input_pins[AUTO_PIN_MIC],
3839 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3840 cfg->input_pins[AUTO_PIN_LINE],
3841 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3842 cfg->input_pins[AUTO_PIN_CD],
3843 cfg->input_pins[AUTO_PIN_AUX]);
3844 if (cfg->dig_in_pin)
3845 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3846
3847 return 0;
3848 }
3849 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3850
3851 /* labels for input pins */
3852 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3853 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3854 };
3855 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3856
3857
3858 #ifdef CONFIG_PM
3859 /*
3860 * power management
3861 */
3862
3863 /**
3864 * snd_hda_suspend - suspend the codecs
3865 * @bus: the HDA bus
3866 * @state: suspsend state
3867 *
3868 * Returns 0 if successful.
3869 */
3870 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3871 {
3872 struct hda_codec *codec;
3873
3874 list_for_each_entry(codec, &bus->codec_list, list) {
3875 #ifdef CONFIG_SND_HDA_POWER_SAVE
3876 if (!codec->power_on)
3877 continue;
3878 #endif
3879 hda_call_codec_suspend(codec);
3880 }
3881 return 0;
3882 }
3883 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3884
3885 /**
3886 * snd_hda_resume - resume the codecs
3887 * @bus: the HDA bus
3888 *
3889 * Returns 0 if successful.
3890 *
3891 * This fucntion is defined only when POWER_SAVE isn't set.
3892 * In the power-save mode, the codec is resumed dynamically.
3893 */
3894 int snd_hda_resume(struct hda_bus *bus)
3895 {
3896 struct hda_codec *codec;
3897
3898 list_for_each_entry(codec, &bus->codec_list, list) {
3899 if (snd_hda_codec_needs_resume(codec))
3900 hda_call_codec_resume(codec);
3901 }
3902 return 0;
3903 }
3904 EXPORT_SYMBOL_HDA(snd_hda_resume);
3905 #endif /* CONFIG_PM */
3906
3907 /*
3908 * generic arrays
3909 */
3910
3911 /* get a new element from the given array
3912 * if it exceeds the pre-allocated array size, re-allocate the array
3913 */
3914 void *snd_array_new(struct snd_array *array)
3915 {
3916 if (array->used >= array->alloced) {
3917 int num = array->alloced + array->alloc_align;
3918 void *nlist;
3919 if (snd_BUG_ON(num >= 4096))
3920 return NULL;
3921 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3922 if (!nlist)
3923 return NULL;
3924 if (array->list) {
3925 memcpy(nlist, array->list,
3926 array->elem_size * array->alloced);
3927 kfree(array->list);
3928 }
3929 array->list = nlist;
3930 array->alloced = num;
3931 }
3932 return snd_array_elem(array, array->used++);
3933 }
3934 EXPORT_SYMBOL_HDA(snd_array_new);
3935
3936 /* free the given array elements */
3937 void snd_array_free(struct snd_array *array)
3938 {
3939 kfree(array->list);
3940 array->used = 0;
3941 array->alloced = 0;
3942 array->list = NULL;
3943 }
3944 EXPORT_SYMBOL_HDA(snd_array_free);
3945
3946 /*
3947 * used by hda_proc.c and hda_eld.c
3948 */
3949 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3950 {
3951 static unsigned int rates[] = {
3952 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3953 96000, 176400, 192000, 384000
3954 };
3955 int i, j;
3956
3957 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3958 if (pcm & (1 << i))
3959 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3960
3961 buf[j] = '\0'; /* necessary when j == 0 */
3962 }
3963 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
3964
3965 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3966 {
3967 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3968 int i, j;
3969
3970 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3971 if (pcm & (AC_SUPPCM_BITS_8 << i))
3972 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3973
3974 buf[j] = '\0'; /* necessary when j == 0 */
3975 }
3976 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
3977
3978 MODULE_DESCRIPTION("HDA codec core");
3979 MODULE_LICENSE("GPL");