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