1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 <sound/jack.h>
33#include "hda_local.h"
34#include "hda_beep.h"
35#include <sound/hda_hwdep.h>
36
37
38
39
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46
47static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x1002, "ATI" },
49 { 0x1013, "Cirrus Logic" },
50 { 0x1057, "Motorola" },
51 { 0x1095, "Silicon Image" },
52 { 0x10de, "Nvidia" },
53 { 0x10ec, "Realtek" },
54 { 0x1102, "Creative" },
55 { 0x1106, "VIA" },
56 { 0x111d, "IDT" },
57 { 0x11c1, "LSI" },
58 { 0x11d4, "Analog Devices" },
59 { 0x13f6, "C-Media" },
60 { 0x14f1, "Conexant" },
61 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
63 { 0x1aec, "Wolfson Microelectronics" },
64 { 0x434d, "C-Media" },
65 { 0x8086, "Intel" },
66 { 0x8384, "SigmaTel" },
67 {}
68};
69
70static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
80EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
81
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
89EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
90
91#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
98
99
100
101
102
103
104
105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
131
132
133
134
135
136
137
138
139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
146
147
148
149
150
151
152
153
154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
167
168
169
170
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
192
193
194
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
199 int err;
200
201 if (cmd == ~0)
202 return -1;
203
204 if (res)
205 *res = -1;
206 again:
207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
209 err = bus->ops.command(bus, cmd);
210 if (!err && res)
211 *res = bus->ops.get_response(bus, codec->addr);
212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222
223 if (!err)
224 bus->response_reset = 0;
225 return err;
226}
227
228
229
230
231
232
233
234
235
236
237
238
239
240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
242 unsigned int verb, unsigned int parm)
243{
244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
246 codec_exec_verb(codec, cmd, &res);
247 return res;
248}
249EXPORT_SYMBOL_HDA(snd_hda_codec_read);
250
251
252
253
254
255
256
257
258
259
260
261
262
263int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
264 unsigned int verb, unsigned int parm)
265{
266 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
267 unsigned int res;
268 return codec_exec_verb(codec, cmd,
269 codec->bus->sync_write ? &res : NULL);
270}
271EXPORT_SYMBOL_HDA(snd_hda_codec_write);
272
273
274
275
276
277
278
279
280
281void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282{
283 for (; seq->nid; seq++)
284 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285}
286EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
287
288
289
290
291
292
293
294
295
296
297int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
298 hda_nid_t *start_id)
299{
300 unsigned int parm;
301
302 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
303 if (parm == -1)
304 return 0;
305 *start_id = (parm >> 16) & 0x7fff;
306 return (int)(parm & 0x7fff);
307}
308EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
309
310
311
312
313
314
315
316
317
318
319
320
321
322int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
323 hda_nid_t *conn_list, int max_conns)
324{
325 unsigned int parm;
326 int i, conn_len, conns;
327 unsigned int shift, num_elems, mask;
328 unsigned int wcaps;
329 hda_nid_t prev_nid;
330
331 if (snd_BUG_ON(!conn_list || max_conns <= 0))
332 return -EINVAL;
333
334 wcaps = get_wcaps(codec, nid);
335 if (!(wcaps & AC_WCAP_CONN_LIST) &&
336 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
337 snd_printk(KERN_WARNING "hda_codec: "
338 "connection list not available for 0x%x\n", nid);
339 return -EINVAL;
340 }
341
342 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
343 if (parm & AC_CLIST_LONG) {
344
345 shift = 16;
346 num_elems = 2;
347 } else {
348
349 shift = 8;
350 num_elems = 4;
351 }
352 conn_len = parm & AC_CLIST_LENGTH;
353 mask = (1 << (shift-1)) - 1;
354
355 if (!conn_len)
356 return 0;
357
358 if (conn_len == 1) {
359
360 parm = snd_hda_codec_read(codec, nid, 0,
361 AC_VERB_GET_CONNECT_LIST, 0);
362 if (parm == -1 && codec->bus->rirb_error)
363 return -EIO;
364 conn_list[0] = parm & mask;
365 return 1;
366 }
367
368
369 conns = 0;
370 prev_nid = 0;
371 for (i = 0; i < conn_len; i++) {
372 int range_val;
373 hda_nid_t val, n;
374
375 if (i % num_elems == 0) {
376 parm = snd_hda_codec_read(codec, nid, 0,
377 AC_VERB_GET_CONNECT_LIST, i);
378 if (parm == -1 && codec->bus->rirb_error)
379 return -EIO;
380 }
381 range_val = !!(parm & (1 << (shift-1)));
382 val = parm & mask;
383 if (val == 0) {
384 snd_printk(KERN_WARNING "hda_codec: "
385 "invalid CONNECT_LIST verb %x[%i]:%x\n",
386 nid, i, parm);
387 return 0;
388 }
389 parm >>= shift;
390 if (range_val) {
391
392 if (!prev_nid || prev_nid >= val) {
393 snd_printk(KERN_WARNING "hda_codec: "
394 "invalid dep_range_val %x:%x\n",
395 prev_nid, val);
396 continue;
397 }
398 for (n = prev_nid + 1; n <= val; n++) {
399 if (conns >= max_conns) {
400 snd_printk(KERN_ERR "hda_codec: "
401 "Too many connections %d for NID 0x%x\n",
402 conns, nid);
403 return -EINVAL;
404 }
405 conn_list[conns++] = n;
406 }
407 } else {
408 if (conns >= max_conns) {
409 snd_printk(KERN_ERR "hda_codec: "
410 "Too many connections %d for NID 0x%x\n",
411 conns, nid);
412 return -EINVAL;
413 }
414 conn_list[conns++] = val;
415 }
416 prev_nid = val;
417 }
418 return conns;
419}
420EXPORT_SYMBOL_HDA(snd_hda_get_connections);
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
436{
437 struct hda_bus_unsolicited *unsol;
438 unsigned int wp;
439
440 unsol = bus->unsol;
441 if (!unsol)
442 return 0;
443
444 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
445 unsol->wp = wp;
446
447 wp <<= 1;
448 unsol->queue[wp] = res;
449 unsol->queue[wp + 1] = res_ex;
450
451 queue_work(bus->workq, &unsol->work);
452
453 return 0;
454}
455EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
456
457
458
459
460static void process_unsol_events(struct work_struct *work)
461{
462 struct hda_bus_unsolicited *unsol =
463 container_of(work, struct hda_bus_unsolicited, work);
464 struct hda_bus *bus = unsol->bus;
465 struct hda_codec *codec;
466 unsigned int rp, caddr, res;
467
468 while (unsol->rp != unsol->wp) {
469 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
470 unsol->rp = rp;
471 rp <<= 1;
472 res = unsol->queue[rp];
473 caddr = unsol->queue[rp + 1];
474 if (!(caddr & (1 << 4)))
475 continue;
476 codec = bus->caddr_tbl[caddr & 0x0f];
477 if (codec && codec->patch_ops.unsol_event)
478 codec->patch_ops.unsol_event(codec, res);
479 }
480}
481
482
483
484
485static int init_unsol_queue(struct hda_bus *bus)
486{
487 struct hda_bus_unsolicited *unsol;
488
489 if (bus->unsol)
490 return 0;
491
492 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
493 if (!unsol) {
494 snd_printk(KERN_ERR "hda_codec: "
495 "can't allocate unsolicited queue\n");
496 return -ENOMEM;
497 }
498 INIT_WORK(&unsol->work, process_unsol_events);
499 unsol->bus = bus;
500 bus->unsol = unsol;
501 return 0;
502}
503
504
505
506
507static void snd_hda_codec_free(struct hda_codec *codec);
508
509static int snd_hda_bus_free(struct hda_bus *bus)
510{
511 struct hda_codec *codec, *n;
512
513 if (!bus)
514 return 0;
515 if (bus->workq)
516 flush_workqueue(bus->workq);
517 if (bus->unsol)
518 kfree(bus->unsol);
519 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
520 snd_hda_codec_free(codec);
521 }
522 if (bus->ops.private_free)
523 bus->ops.private_free(bus);
524 if (bus->workq)
525 destroy_workqueue(bus->workq);
526 kfree(bus);
527 return 0;
528}
529
530static int snd_hda_bus_dev_free(struct snd_device *device)
531{
532 struct hda_bus *bus = device->device_data;
533 bus->shutdown = 1;
534 return snd_hda_bus_free(bus);
535}
536
537#ifdef CONFIG_SND_HDA_HWDEP
538static int snd_hda_bus_dev_register(struct snd_device *device)
539{
540 struct hda_bus *bus = device->device_data;
541 struct hda_codec *codec;
542 list_for_each_entry(codec, &bus->codec_list, list) {
543 snd_hda_hwdep_add_sysfs(codec);
544 snd_hda_hwdep_add_power_sysfs(codec);
545 }
546 return 0;
547}
548#else
549#define snd_hda_bus_dev_register NULL
550#endif
551
552
553
554
555
556
557
558
559
560int snd_hda_bus_new(struct snd_card *card,
561 const struct hda_bus_template *temp,
562 struct hda_bus **busp)
563{
564 struct hda_bus *bus;
565 int err;
566 static struct snd_device_ops dev_ops = {
567 .dev_register = snd_hda_bus_dev_register,
568 .dev_free = snd_hda_bus_dev_free,
569 };
570
571 if (snd_BUG_ON(!temp))
572 return -EINVAL;
573 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
574 return -EINVAL;
575
576 if (busp)
577 *busp = NULL;
578
579 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
580 if (bus == NULL) {
581 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
582 return -ENOMEM;
583 }
584
585 bus->card = card;
586 bus->private_data = temp->private_data;
587 bus->pci = temp->pci;
588 bus->modelname = temp->modelname;
589 bus->power_save = temp->power_save;
590 bus->ops = temp->ops;
591
592 mutex_init(&bus->cmd_mutex);
593 mutex_init(&bus->prepare_mutex);
594 INIT_LIST_HEAD(&bus->codec_list);
595
596 snprintf(bus->workq_name, sizeof(bus->workq_name),
597 "hd-audio%d", card->number);
598 bus->workq = create_singlethread_workqueue(bus->workq_name);
599 if (!bus->workq) {
600 snd_printk(KERN_ERR "cannot create workqueue %s\n",
601 bus->workq_name);
602 kfree(bus);
603 return -ENOMEM;
604 }
605
606 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
607 if (err < 0) {
608 snd_hda_bus_free(bus);
609 return err;
610 }
611 if (busp)
612 *busp = bus;
613 return 0;
614}
615EXPORT_SYMBOL_HDA(snd_hda_bus_new);
616
617#ifdef CONFIG_SND_HDA_GENERIC
618#define is_generic_config(codec) \
619 (codec->modelname && !strcmp(codec->modelname, "generic"))
620#else
621#define is_generic_config(codec) 0
622#endif
623
624#ifdef MODULE
625#define HDA_MODREQ_MAX_COUNT 2
626#else
627#define HDA_MODREQ_MAX_COUNT 0
628#endif
629
630
631
632
633static const struct hda_codec_preset *
634find_codec_preset(struct hda_codec *codec)
635{
636 struct hda_codec_preset_list *tbl;
637 const struct hda_codec_preset *preset;
638 int mod_requested = 0;
639
640 if (is_generic_config(codec))
641 return NULL;
642
643 again:
644 mutex_lock(&preset_mutex);
645 list_for_each_entry(tbl, &hda_preset_tables, list) {
646 if (!try_module_get(tbl->owner)) {
647 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
648 continue;
649 }
650 for (preset = tbl->preset; preset->id; preset++) {
651 u32 mask = preset->mask;
652 if (preset->afg && preset->afg != codec->afg)
653 continue;
654 if (preset->mfg && preset->mfg != codec->mfg)
655 continue;
656 if (!mask)
657 mask = ~0;
658 if (preset->id == (codec->vendor_id & mask) &&
659 (!preset->rev ||
660 preset->rev == codec->revision_id)) {
661 mutex_unlock(&preset_mutex);
662 codec->owner = tbl->owner;
663 return preset;
664 }
665 }
666 module_put(tbl->owner);
667 }
668 mutex_unlock(&preset_mutex);
669
670 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
671 char name[32];
672 if (!mod_requested)
673 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
674 codec->vendor_id);
675 else
676 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
677 (codec->vendor_id >> 16) & 0xffff);
678 request_module(name);
679 mod_requested++;
680 goto again;
681 }
682 return NULL;
683}
684
685
686
687
688static int get_codec_name(struct hda_codec *codec)
689{
690 const struct hda_vendor_id *c;
691 const char *vendor = NULL;
692 u16 vendor_id = codec->vendor_id >> 16;
693 char tmp[16];
694
695 if (codec->vendor_name)
696 goto get_chip_name;
697
698 for (c = hda_vendor_ids; c->id; c++) {
699 if (c->id == vendor_id) {
700 vendor = c->name;
701 break;
702 }
703 }
704 if (!vendor) {
705 sprintf(tmp, "Generic %04x", vendor_id);
706 vendor = tmp;
707 }
708 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
709 if (!codec->vendor_name)
710 return -ENOMEM;
711
712 get_chip_name:
713 if (codec->chip_name)
714 return 0;
715
716 if (codec->preset && codec->preset->name)
717 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
718 else {
719 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
720 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
721 }
722 if (!codec->chip_name)
723 return -ENOMEM;
724 return 0;
725}
726
727
728
729
730static void setup_fg_nodes(struct hda_codec *codec)
731{
732 int i, total_nodes, function_id;
733 hda_nid_t nid;
734
735 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
736 for (i = 0; i < total_nodes; i++, nid++) {
737 function_id = snd_hda_param_read(codec, nid,
738 AC_PAR_FUNCTION_TYPE);
739 switch (function_id & 0xff) {
740 case AC_GRP_AUDIO_FUNCTION:
741 codec->afg = nid;
742 codec->afg_function_id = function_id & 0xff;
743 codec->afg_unsol = (function_id >> 8) & 1;
744 break;
745 case AC_GRP_MODEM_FUNCTION:
746 codec->mfg = nid;
747 codec->mfg_function_id = function_id & 0xff;
748 codec->mfg_unsol = (function_id >> 8) & 1;
749 break;
750 default:
751 break;
752 }
753 }
754}
755
756
757
758
759static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
760{
761 int i;
762 hda_nid_t nid;
763
764 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
765 &codec->start_nid);
766 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
767 if (!codec->wcaps)
768 return -ENOMEM;
769 nid = codec->start_nid;
770 for (i = 0; i < codec->num_nodes; i++, nid++)
771 codec->wcaps[i] = snd_hda_param_read(codec, nid,
772 AC_PAR_AUDIO_WIDGET_CAP);
773 return 0;
774}
775
776
777static int read_pin_defaults(struct hda_codec *codec)
778{
779 int i;
780 hda_nid_t nid = codec->start_nid;
781
782 for (i = 0; i < codec->num_nodes; i++, nid++) {
783 struct hda_pincfg *pin;
784 unsigned int wcaps = get_wcaps(codec, nid);
785 unsigned int wid_type = get_wcaps_type(wcaps);
786 if (wid_type != AC_WID_PIN)
787 continue;
788 pin = snd_array_new(&codec->init_pins);
789 if (!pin)
790 return -ENOMEM;
791 pin->nid = nid;
792 pin->cfg = snd_hda_codec_read(codec, nid, 0,
793 AC_VERB_GET_CONFIG_DEFAULT, 0);
794 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
795 AC_VERB_GET_PIN_WIDGET_CONTROL,
796 0);
797 }
798 return 0;
799}
800
801
802static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
803 struct snd_array *array,
804 hda_nid_t nid)
805{
806 int i;
807 for (i = 0; i < array->used; i++) {
808 struct hda_pincfg *pin = snd_array_elem(array, i);
809 if (pin->nid == nid)
810 return pin;
811 }
812 return NULL;
813}
814
815
816static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
817 unsigned int cfg)
818{
819 int i;
820 for (i = 0; i < 4; i++) {
821 snd_hda_codec_write(codec, nid, 0,
822 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
823 cfg & 0xff);
824 cfg >>= 8;
825 }
826}
827
828
829
830
831int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
832 hda_nid_t nid, unsigned int cfg)
833{
834 struct hda_pincfg *pin;
835 unsigned int oldcfg;
836
837 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
838 return -EINVAL;
839
840 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
841 pin = look_up_pincfg(codec, list, nid);
842 if (!pin) {
843 pin = snd_array_new(list);
844 if (!pin)
845 return -ENOMEM;
846 pin->nid = nid;
847 }
848 pin->cfg = cfg;
849
850
851
852
853 cfg = snd_hda_codec_get_pincfg(codec, nid);
854 if (oldcfg != cfg)
855 set_pincfg(codec, nid, cfg);
856 return 0;
857}
858
859
860
861
862
863
864
865
866
867
868
869int snd_hda_codec_set_pincfg(struct hda_codec *codec,
870 hda_nid_t nid, unsigned int cfg)
871{
872 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
873}
874EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
875
876
877
878
879
880
881
882
883
884
885unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
886{
887 struct hda_pincfg *pin;
888
889#ifdef CONFIG_SND_HDA_HWDEP
890 pin = look_up_pincfg(codec, &codec->user_pins, nid);
891 if (pin)
892 return pin->cfg;
893#endif
894 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
895 if (pin)
896 return pin->cfg;
897 pin = look_up_pincfg(codec, &codec->init_pins, nid);
898 if (pin)
899 return pin->cfg;
900 return 0;
901}
902EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
903
904
905static void restore_pincfgs(struct hda_codec *codec)
906{
907 int i;
908 for (i = 0; i < codec->init_pins.used; i++) {
909 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
910 set_pincfg(codec, pin->nid,
911 snd_hda_codec_get_pincfg(codec, pin->nid));
912 }
913}
914
915
916
917
918
919
920
921
922void snd_hda_shutup_pins(struct hda_codec *codec)
923{
924 int i;
925
926
927
928 if (codec->bus->shutdown)
929 return;
930 for (i = 0; i < codec->init_pins.used; i++) {
931 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
932
933 snd_hda_codec_read(codec, pin->nid, 0,
934 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
935 }
936 codec->pins_shutup = 1;
937}
938EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
939
940#ifdef SND_HDA_NEEDS_RESUME
941
942static void restore_shutup_pins(struct hda_codec *codec)
943{
944 int i;
945 if (!codec->pins_shutup)
946 return;
947 if (codec->bus->shutdown)
948 return;
949 for (i = 0; i < codec->init_pins.used; i++) {
950 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
951 snd_hda_codec_write(codec, pin->nid, 0,
952 AC_VERB_SET_PIN_WIDGET_CONTROL,
953 pin->ctrl);
954 }
955 codec->pins_shutup = 0;
956}
957#endif
958
959static void init_hda_cache(struct hda_cache_rec *cache,
960 unsigned int record_size);
961static void free_hda_cache(struct hda_cache_rec *cache);
962
963
964static void restore_init_pincfgs(struct hda_codec *codec)
965{
966
967
968
969 snd_array_free(&codec->driver_pins);
970#ifdef CONFIG_SND_HDA_HWDEP
971 snd_array_free(&codec->user_pins);
972#endif
973 restore_pincfgs(codec);
974 snd_array_free(&codec->init_pins);
975}
976
977
978
979
980struct hda_cvt_setup {
981 hda_nid_t nid;
982 u8 stream_tag;
983 u8 channel_id;
984 u16 format_id;
985 unsigned char active;
986 unsigned char dirty;
987};
988
989
990static struct hda_cvt_setup *
991get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
992{
993 struct hda_cvt_setup *p;
994 int i;
995
996 for (i = 0; i < codec->cvt_setups.used; i++) {
997 p = snd_array_elem(&codec->cvt_setups, i);
998 if (p->nid == nid)
999 return p;
1000 }
1001 p = snd_array_new(&codec->cvt_setups);
1002 if (p)
1003 p->nid = nid;
1004 return p;
1005}
1006
1007
1008
1009
1010static void snd_hda_codec_free(struct hda_codec *codec)
1011{
1012 if (!codec)
1013 return;
1014 restore_init_pincfgs(codec);
1015#ifdef CONFIG_SND_HDA_POWER_SAVE
1016 cancel_delayed_work(&codec->power_work);
1017 flush_workqueue(codec->bus->workq);
1018#endif
1019 list_del(&codec->list);
1020 snd_array_free(&codec->mixers);
1021 snd_array_free(&codec->nids);
1022 codec->bus->caddr_tbl[codec->addr] = NULL;
1023 if (codec->patch_ops.free)
1024 codec->patch_ops.free(codec);
1025 module_put(codec->owner);
1026 free_hda_cache(&codec->amp_cache);
1027 free_hda_cache(&codec->cmd_cache);
1028 kfree(codec->vendor_name);
1029 kfree(codec->chip_name);
1030 kfree(codec->modelname);
1031 kfree(codec->wcaps);
1032 kfree(codec);
1033}
1034
1035static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1036 unsigned int power_state);
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046int snd_hda_codec_new(struct hda_bus *bus,
1047 unsigned int codec_addr,
1048 struct hda_codec **codecp)
1049{
1050 struct hda_codec *codec;
1051 char component[31];
1052 int err;
1053
1054 if (snd_BUG_ON(!bus))
1055 return -EINVAL;
1056 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1057 return -EINVAL;
1058
1059 if (bus->caddr_tbl[codec_addr]) {
1060 snd_printk(KERN_ERR "hda_codec: "
1061 "address 0x%x is already occupied\n", codec_addr);
1062 return -EBUSY;
1063 }
1064
1065 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1066 if (codec == NULL) {
1067 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1068 return -ENOMEM;
1069 }
1070
1071 codec->bus = bus;
1072 codec->addr = codec_addr;
1073 mutex_init(&codec->spdif_mutex);
1074 mutex_init(&codec->control_mutex);
1075 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1076 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1077 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1078 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1079 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1080 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1081 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1082 if (codec->bus->modelname) {
1083 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1084 if (!codec->modelname) {
1085 snd_hda_codec_free(codec);
1086 return -ENODEV;
1087 }
1088 }
1089
1090#ifdef CONFIG_SND_HDA_POWER_SAVE
1091 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1092
1093
1094
1095
1096 hda_keep_power_on(codec);
1097#endif
1098
1099 list_add_tail(&codec->list, &bus->codec_list);
1100 bus->caddr_tbl[codec_addr] = codec;
1101
1102 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1103 AC_PAR_VENDOR_ID);
1104 if (codec->vendor_id == -1)
1105
1106
1107
1108 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1109 AC_PAR_VENDOR_ID);
1110 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1111 AC_PAR_SUBSYSTEM_ID);
1112 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1113 AC_PAR_REV_ID);
1114
1115 setup_fg_nodes(codec);
1116 if (!codec->afg && !codec->mfg) {
1117 snd_printdd("hda_codec: no AFG or MFG node found\n");
1118 err = -ENODEV;
1119 goto error;
1120 }
1121
1122 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1123 if (err < 0) {
1124 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1125 goto error;
1126 }
1127 err = read_pin_defaults(codec);
1128 if (err < 0)
1129 goto error;
1130
1131 if (!codec->subsystem_id) {
1132 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1133 codec->subsystem_id =
1134 snd_hda_codec_read(codec, nid, 0,
1135 AC_VERB_GET_SUBSYSTEM_ID, 0);
1136 }
1137
1138
1139 hda_set_power_state(codec,
1140 codec->afg ? codec->afg : codec->mfg,
1141 AC_PWRST_D0);
1142
1143 snd_hda_codec_proc_new(codec);
1144
1145 snd_hda_create_hwdep(codec);
1146
1147 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1148 codec->subsystem_id, codec->revision_id);
1149 snd_component_add(codec->bus->card, component);
1150
1151 if (codecp)
1152 *codecp = codec;
1153 return 0;
1154
1155 error:
1156 snd_hda_codec_free(codec);
1157 return err;
1158}
1159EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170int snd_hda_codec_configure(struct hda_codec *codec)
1171{
1172 int err;
1173
1174 codec->preset = find_codec_preset(codec);
1175 if (!codec->vendor_name || !codec->chip_name) {
1176 err = get_codec_name(codec);
1177 if (err < 0)
1178 return err;
1179 }
1180
1181 if (is_generic_config(codec)) {
1182 err = snd_hda_parse_generic_codec(codec);
1183 goto patched;
1184 }
1185 if (codec->preset && codec->preset->patch) {
1186 err = codec->preset->patch(codec);
1187 goto patched;
1188 }
1189
1190
1191 err = snd_hda_parse_generic_codec(codec);
1192 if (err < 0)
1193 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1194
1195 patched:
1196 if (!err && codec->patch_ops.unsol_event)
1197 err = init_unsol_queue(codec->bus);
1198
1199 if (!err && (codec->afg || !*codec->bus->card->mixername))
1200 snprintf(codec->bus->card->mixername,
1201 sizeof(codec->bus->card->mixername),
1202 "%s %s", codec->vendor_name, codec->chip_name);
1203 return err;
1204}
1205EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1216 u32 stream_tag,
1217 int channel_id, int format)
1218{
1219 struct hda_codec *c;
1220 struct hda_cvt_setup *p;
1221 unsigned int oldval, newval;
1222 int type;
1223 int i;
1224
1225 if (!nid)
1226 return;
1227
1228 snd_printdd("hda_codec_setup_stream: "
1229 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1230 nid, stream_tag, channel_id, format);
1231 p = get_hda_cvt_setup(codec, nid);
1232 if (!p)
1233 return;
1234
1235 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1236 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1237 newval = (stream_tag << 4) | channel_id;
1238 if (oldval != newval)
1239 snd_hda_codec_write(codec, nid, 0,
1240 AC_VERB_SET_CHANNEL_STREAMID,
1241 newval);
1242 p->stream_tag = stream_tag;
1243 p->channel_id = channel_id;
1244 }
1245
1246 if (p->format_id != format) {
1247 oldval = snd_hda_codec_read(codec, nid, 0,
1248 AC_VERB_GET_STREAM_FORMAT, 0);
1249 if (oldval != format) {
1250 msleep(1);
1251 snd_hda_codec_write(codec, nid, 0,
1252 AC_VERB_SET_STREAM_FORMAT,
1253 format);
1254 }
1255 p->format_id = format;
1256 }
1257 p->active = 1;
1258 p->dirty = 0;
1259
1260
1261 type = get_wcaps_type(get_wcaps(codec, nid));
1262 list_for_each_entry(c, &codec->bus->codec_list, list) {
1263 for (i = 0; i < c->cvt_setups.used; i++) {
1264 p = snd_array_elem(&c->cvt_setups, i);
1265 if (!p->active && p->stream_tag == stream_tag &&
1266 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
1267 p->dirty = 1;
1268 }
1269 }
1270}
1271EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1272
1273static void really_cleanup_stream(struct hda_codec *codec,
1274 struct hda_cvt_setup *q);
1275
1276
1277
1278
1279
1280
1281
1282void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1283 int do_now)
1284{
1285 struct hda_cvt_setup *p;
1286
1287 if (!nid)
1288 return;
1289
1290 if (codec->no_sticky_stream)
1291 do_now = 1;
1292
1293 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1294 p = get_hda_cvt_setup(codec, nid);
1295 if (p) {
1296
1297
1298
1299
1300 if (do_now)
1301 really_cleanup_stream(codec, p);
1302 else
1303 p->active = 0;
1304 }
1305}
1306EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1307
1308static void really_cleanup_stream(struct hda_codec *codec,
1309 struct hda_cvt_setup *q)
1310{
1311 hda_nid_t nid = q->nid;
1312 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1313 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1314 memset(q, 0, sizeof(*q));
1315 q->nid = nid;
1316}
1317
1318
1319static void purify_inactive_streams(struct hda_codec *codec)
1320{
1321 struct hda_codec *c;
1322 int i;
1323
1324 list_for_each_entry(c, &codec->bus->codec_list, list) {
1325 for (i = 0; i < c->cvt_setups.used; i++) {
1326 struct hda_cvt_setup *p;
1327 p = snd_array_elem(&c->cvt_setups, i);
1328 if (p->dirty)
1329 really_cleanup_stream(c, p);
1330 }
1331 }
1332}
1333
1334#ifdef SND_HDA_NEEDS_RESUME
1335
1336static void hda_cleanup_all_streams(struct hda_codec *codec)
1337{
1338 int i;
1339
1340 for (i = 0; i < codec->cvt_setups.used; i++) {
1341 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1342 if (p->stream_tag)
1343 really_cleanup_stream(codec, p);
1344 }
1345}
1346#endif
1347
1348
1349
1350
1351
1352
1353#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1354#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1355#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1356#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1357#define INFO_AMP_CAPS (1<<0)
1358#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1359
1360
1361static void init_hda_cache(struct hda_cache_rec *cache,
1362 unsigned int record_size)
1363{
1364 memset(cache, 0, sizeof(*cache));
1365 memset(cache->hash, 0xff, sizeof(cache->hash));
1366 snd_array_init(&cache->buf, record_size, 64);
1367}
1368
1369static void free_hda_cache(struct hda_cache_rec *cache)
1370{
1371 snd_array_free(&cache->buf);
1372}
1373
1374
1375static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1376{
1377 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1378 u16 cur = cache->hash[idx];
1379 struct hda_cache_head *info;
1380
1381 while (cur != 0xffff) {
1382 info = snd_array_elem(&cache->buf, cur);
1383 if (info->key == key)
1384 return info;
1385 cur = info->next;
1386 }
1387 return NULL;
1388}
1389
1390
1391static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1392 u32 key)
1393{
1394 struct hda_cache_head *info = get_hash(cache, key);
1395 if (!info) {
1396 u16 idx, cur;
1397
1398 info = snd_array_new(&cache->buf);
1399 if (!info)
1400 return NULL;
1401 cur = snd_array_index(&cache->buf, info);
1402 info->key = key;
1403 info->val = 0;
1404 idx = key % (u16)ARRAY_SIZE(cache->hash);
1405 info->next = cache->hash[idx];
1406 cache->hash[idx] = cur;
1407 }
1408 return info;
1409}
1410
1411
1412static inline struct hda_amp_info *
1413get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1414{
1415 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1416}
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1431{
1432 struct hda_amp_info *info;
1433
1434 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1435 if (!info)
1436 return 0;
1437 if (!(info->head.val & INFO_AMP_CAPS)) {
1438 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1439 nid = codec->afg;
1440 info->amp_caps = snd_hda_param_read(codec, nid,
1441 direction == HDA_OUTPUT ?
1442 AC_PAR_AMP_OUT_CAP :
1443 AC_PAR_AMP_IN_CAP);
1444 if (info->amp_caps)
1445 info->head.val |= INFO_AMP_CAPS;
1446 }
1447 return info->amp_caps;
1448}
1449EXPORT_SYMBOL_HDA(query_amp_caps);
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1465 unsigned int caps)
1466{
1467 struct hda_amp_info *info;
1468
1469 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1470 if (!info)
1471 return -EINVAL;
1472 info->amp_caps = caps;
1473 info->head.val |= INFO_AMP_CAPS;
1474 return 0;
1475}
1476EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1477
1478static unsigned int
1479query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1480 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1481{
1482 struct hda_amp_info *info;
1483
1484 info = get_alloc_amp_hash(codec, key);
1485 if (!info)
1486 return 0;
1487 if (!info->head.val) {
1488 info->head.val |= INFO_AMP_CAPS;
1489 info->amp_caps = func(codec, nid);
1490 }
1491 return info->amp_caps;
1492}
1493
1494static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1495{
1496 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1497}
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1511{
1512 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1513 read_pin_cap);
1514}
1515EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1526{
1527 u32 pincap;
1528
1529 if (!codec->no_trigger_sense) {
1530 pincap = snd_hda_query_pin_caps(codec, nid);
1531 if (pincap & AC_PINCAP_TRIG_REQ)
1532 snd_hda_codec_read(codec, nid, 0,
1533 AC_VERB_SET_PIN_SENSE, 0);
1534 }
1535 return snd_hda_codec_read(codec, nid, 0,
1536 AC_VERB_GET_PIN_SENSE, 0);
1537}
1538EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1539
1540
1541
1542
1543
1544
1545
1546
1547int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1548{
1549 u32 sense = snd_hda_pin_sense(codec, nid);
1550 return !!(sense & AC_PINSENSE_PRESENCE);
1551}
1552EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1553
1554
1555
1556
1557
1558static unsigned int get_vol_mute(struct hda_codec *codec,
1559 struct hda_amp_info *info, hda_nid_t nid,
1560 int ch, int direction, int index)
1561{
1562 u32 val, parm;
1563
1564 if (info->head.val & INFO_AMP_VOL(ch))
1565 return info->vol[ch];
1566
1567 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1568 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1569 parm |= index;
1570 val = snd_hda_codec_read(codec, nid, 0,
1571 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1572 info->vol[ch] = val & 0xff;
1573 info->head.val |= INFO_AMP_VOL(ch);
1574 return info->vol[ch];
1575}
1576
1577
1578
1579
1580static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1581 hda_nid_t nid, int ch, int direction, int index,
1582 int val)
1583{
1584 u32 parm;
1585
1586 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1587 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1588 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1589 parm |= val;
1590 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1591 info->vol[ch] = val;
1592}
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1605 int direction, int index)
1606{
1607 struct hda_amp_info *info;
1608 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1609 if (!info)
1610 return 0;
1611 return get_vol_mute(codec, info, nid, ch, direction, index);
1612}
1613EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1629 int direction, int idx, int mask, int val)
1630{
1631 struct hda_amp_info *info;
1632
1633 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1634 if (!info)
1635 return 0;
1636 if (snd_BUG_ON(mask & ~0xff))
1637 mask &= 0xff;
1638 val &= mask;
1639 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1640 if (info->vol[ch] == val)
1641 return 0;
1642 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1643 return 1;
1644}
1645EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1660 int direction, int idx, int mask, int val)
1661{
1662 int ch, ret = 0;
1663
1664 if (snd_BUG_ON(mask & ~0xff))
1665 mask &= 0xff;
1666 for (ch = 0; ch < 2; ch++)
1667 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1668 idx, mask, val);
1669 return ret;
1670}
1671EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1672
1673#ifdef SND_HDA_NEEDS_RESUME
1674
1675
1676
1677
1678
1679
1680void snd_hda_codec_resume_amp(struct hda_codec *codec)
1681{
1682 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1683 int i;
1684
1685 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1686 u32 key = buffer->head.key;
1687 hda_nid_t nid;
1688 unsigned int idx, dir, ch;
1689 if (!key)
1690 continue;
1691 nid = key & 0xff;
1692 idx = (key >> 16) & 0xff;
1693 dir = (key >> 24) & 0xff;
1694 for (ch = 0; ch < 2; ch++) {
1695 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1696 continue;
1697 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1698 buffer->vol[ch]);
1699 }
1700 }
1701}
1702EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1703#endif
1704
1705static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1706 unsigned int ofs)
1707{
1708 u32 caps = query_amp_caps(codec, nid, dir);
1709
1710 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1711 if (ofs < caps)
1712 caps -= ofs;
1713 return caps;
1714}
1715
1716
1717
1718
1719
1720
1721
1722int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_info *uinfo)
1724{
1725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1726 u16 nid = get_amp_nid(kcontrol);
1727 u8 chs = get_amp_channels(kcontrol);
1728 int dir = get_amp_direction(kcontrol);
1729 unsigned int ofs = get_amp_offset(kcontrol);
1730
1731 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1732 uinfo->count = chs == 3 ? 2 : 1;
1733 uinfo->value.integer.min = 0;
1734 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1735 if (!uinfo->value.integer.max) {
1736 printk(KERN_WARNING "hda_codec: "
1737 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1738 kcontrol->id.name);
1739 return -EINVAL;
1740 }
1741 return 0;
1742}
1743EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1744
1745
1746static inline unsigned int
1747read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1748 int ch, int dir, int idx, unsigned int ofs)
1749{
1750 unsigned int val;
1751 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1752 val &= HDA_AMP_VOLMASK;
1753 if (val >= ofs)
1754 val -= ofs;
1755 else
1756 val = 0;
1757 return val;
1758}
1759
1760static inline int
1761update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1762 int ch, int dir, int idx, unsigned int ofs,
1763 unsigned int val)
1764{
1765 unsigned int maxval;
1766
1767 if (val > 0)
1768 val += ofs;
1769
1770 maxval = get_amp_max_value(codec, nid, dir, 0);
1771 if (val > maxval)
1772 val = maxval;
1773 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1774 HDA_AMP_VOLMASK, val);
1775}
1776
1777
1778
1779
1780
1781
1782
1783int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_value *ucontrol)
1785{
1786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1787 hda_nid_t nid = get_amp_nid(kcontrol);
1788 int chs = get_amp_channels(kcontrol);
1789 int dir = get_amp_direction(kcontrol);
1790 int idx = get_amp_index(kcontrol);
1791 unsigned int ofs = get_amp_offset(kcontrol);
1792 long *valp = ucontrol->value.integer.value;
1793
1794 if (chs & 1)
1795 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1796 if (chs & 2)
1797 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1798 return 0;
1799}
1800EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1801
1802
1803
1804
1805
1806
1807
1808int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1810{
1811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1812 hda_nid_t nid = get_amp_nid(kcontrol);
1813 int chs = get_amp_channels(kcontrol);
1814 int dir = get_amp_direction(kcontrol);
1815 int idx = get_amp_index(kcontrol);
1816 unsigned int ofs = get_amp_offset(kcontrol);
1817 long *valp = ucontrol->value.integer.value;
1818 int change = 0;
1819
1820 snd_hda_power_up(codec);
1821 if (chs & 1) {
1822 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1823 valp++;
1824 }
1825 if (chs & 2)
1826 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1827 snd_hda_power_down(codec);
1828 return change;
1829}
1830EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1831
1832
1833
1834
1835
1836
1837
1838int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1839 unsigned int size, unsigned int __user *_tlv)
1840{
1841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1842 hda_nid_t nid = get_amp_nid(kcontrol);
1843 int dir = get_amp_direction(kcontrol);
1844 unsigned int ofs = get_amp_offset(kcontrol);
1845 bool min_mute = get_amp_min_mute(kcontrol);
1846 u32 caps, val1, val2;
1847
1848 if (size < 4 * sizeof(unsigned int))
1849 return -ENOMEM;
1850 caps = query_amp_caps(codec, nid, dir);
1851 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1852 val2 = (val2 + 1) * 25;
1853 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1854 val1 += ofs;
1855 val1 = ((int)val1) * ((int)val2);
1856 if (min_mute)
1857 val2 |= TLV_DB_SCALE_MUTE;
1858 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1859 return -EFAULT;
1860 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1861 return -EFAULT;
1862 if (put_user(val1, _tlv + 2))
1863 return -EFAULT;
1864 if (put_user(val2, _tlv + 3))
1865 return -EFAULT;
1866 return 0;
1867}
1868EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1882 unsigned int *tlv)
1883{
1884 u32 caps;
1885 int nums, step;
1886
1887 caps = query_amp_caps(codec, nid, dir);
1888 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1889 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1890 step = (step + 1) * 25;
1891 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1892 tlv[1] = 2 * sizeof(unsigned int);
1893 tlv[2] = -nums * step;
1894 tlv[3] = step;
1895}
1896EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1897
1898
1899static struct snd_kcontrol *
1900_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1901 const char *name, int idx)
1902{
1903 struct snd_ctl_elem_id id;
1904 memset(&id, 0, sizeof(id));
1905 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1906 id.index = idx;
1907 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1908 return NULL;
1909 strcpy(id.name, name);
1910 return snd_ctl_find_id(codec->bus->card, &id);
1911}
1912
1913
1914
1915
1916
1917
1918
1919
1920struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1921 const char *name)
1922{
1923 return _snd_hda_find_mixer_ctl(codec, name, 0);
1924}
1925EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1926
1927static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
1928{
1929 int idx;
1930 for (idx = 0; idx < 16; idx++) {
1931 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
1932 return idx;
1933 }
1934 return -EBUSY;
1935}
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1957 struct snd_kcontrol *kctl)
1958{
1959 int err;
1960 unsigned short flags = 0;
1961 struct hda_nid_item *item;
1962
1963 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1964 flags |= HDA_NID_ITEM_AMP;
1965 if (nid == 0)
1966 nid = get_amp_nid_(kctl->private_value);
1967 }
1968 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1969 nid = kctl->id.subdevice & 0xffff;
1970 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1971 kctl->id.subdevice = 0;
1972 err = snd_ctl_add(codec->bus->card, kctl);
1973 if (err < 0)
1974 return err;
1975 item = snd_array_new(&codec->mixers);
1976 if (!item)
1977 return -ENOMEM;
1978 item->kctl = kctl;
1979 item->nid = nid;
1980 item->flags = flags;
1981 return 0;
1982}
1983EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1997 unsigned int index, hda_nid_t nid)
1998{
1999 struct hda_nid_item *item;
2000
2001 if (nid > 0) {
2002 item = snd_array_new(&codec->nids);
2003 if (!item)
2004 return -ENOMEM;
2005 item->kctl = kctl;
2006 item->index = index;
2007 item->nid = nid;
2008 return 0;
2009 }
2010 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2011 kctl->id.name, kctl->id.index, index);
2012 return -EINVAL;
2013}
2014EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2015
2016
2017
2018
2019
2020void snd_hda_ctls_clear(struct hda_codec *codec)
2021{
2022 int i;
2023 struct hda_nid_item *items = codec->mixers.list;
2024 for (i = 0; i < codec->mixers.used; i++)
2025 snd_ctl_remove(codec->bus->card, items[i].kctl);
2026 snd_array_free(&codec->mixers);
2027 snd_array_free(&codec->nids);
2028}
2029
2030
2031
2032
2033static int hda_lock_devices(struct snd_card *card)
2034{
2035 spin_lock(&card->files_lock);
2036 if (card->shutdown) {
2037 spin_unlock(&card->files_lock);
2038 return -EINVAL;
2039 }
2040 card->shutdown = 1;
2041 spin_unlock(&card->files_lock);
2042 return 0;
2043}
2044
2045static void hda_unlock_devices(struct snd_card *card)
2046{
2047 spin_lock(&card->files_lock);
2048 card->shutdown = 0;
2049 spin_unlock(&card->files_lock);
2050}
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062int snd_hda_codec_reset(struct hda_codec *codec)
2063{
2064 struct snd_card *card = codec->bus->card;
2065 int i, pcm;
2066
2067 if (hda_lock_devices(card) < 0)
2068 return -EBUSY;
2069
2070 if (!list_empty(&card->ctl_files)) {
2071 hda_unlock_devices(card);
2072 return -EBUSY;
2073 }
2074 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2075 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2076 if (!cpcm->pcm)
2077 continue;
2078 if (cpcm->pcm->streams[0].substream_opened ||
2079 cpcm->pcm->streams[1].substream_opened) {
2080 hda_unlock_devices(card);
2081 return -EBUSY;
2082 }
2083 }
2084
2085
2086
2087#ifdef CONFIG_SND_HDA_POWER_SAVE
2088 cancel_delayed_work(&codec->power_work);
2089 flush_workqueue(codec->bus->workq);
2090#endif
2091 snd_hda_ctls_clear(codec);
2092
2093 for (i = 0; i < codec->num_pcms; i++) {
2094 if (codec->pcm_info[i].pcm) {
2095 snd_device_free(card, codec->pcm_info[i].pcm);
2096 clear_bit(codec->pcm_info[i].device,
2097 codec->bus->pcm_dev_bits);
2098 }
2099 }
2100 if (codec->patch_ops.free)
2101 codec->patch_ops.free(codec);
2102 codec->proc_widget_hook = NULL;
2103 codec->spec = NULL;
2104 free_hda_cache(&codec->amp_cache);
2105 free_hda_cache(&codec->cmd_cache);
2106 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2107 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2108
2109 snd_array_free(&codec->driver_pins);
2110 restore_pincfgs(codec);
2111 codec->num_pcms = 0;
2112 codec->pcm_info = NULL;
2113 codec->preset = NULL;
2114 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2115 codec->slave_dig_outs = NULL;
2116 codec->spdif_status_reset = 0;
2117 module_put(codec->owner);
2118 codec->owner = NULL;
2119
2120
2121 hda_unlock_devices(card);
2122 return 0;
2123}
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2142 unsigned int *tlv, const char * const *slaves)
2143{
2144 struct snd_kcontrol *kctl;
2145 const char * const *s;
2146 int err;
2147
2148 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2149 ;
2150 if (!*s) {
2151 snd_printdd("No slave found for %s\n", name);
2152 return 0;
2153 }
2154 kctl = snd_ctl_make_virtual_master(name, tlv);
2155 if (!kctl)
2156 return -ENOMEM;
2157 err = snd_hda_ctl_add(codec, 0, kctl);
2158 if (err < 0)
2159 return err;
2160
2161 for (s = slaves; *s; s++) {
2162 struct snd_kcontrol *sctl;
2163 int i = 0;
2164 for (;;) {
2165 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2166 if (!sctl) {
2167 if (!i)
2168 snd_printdd("Cannot find slave %s, "
2169 "skipped\n", *s);
2170 break;
2171 }
2172 err = snd_ctl_add_slave(kctl, sctl);
2173 if (err < 0)
2174 return err;
2175 i++;
2176 }
2177 }
2178 return 0;
2179}
2180EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2181
2182
2183
2184
2185
2186
2187
2188int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_info *uinfo)
2190{
2191 int chs = get_amp_channels(kcontrol);
2192
2193 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2194 uinfo->count = chs == 3 ? 2 : 1;
2195 uinfo->value.integer.min = 0;
2196 uinfo->value.integer.max = 1;
2197 return 0;
2198}
2199EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2200
2201
2202
2203
2204
2205
2206
2207int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2208 struct snd_ctl_elem_value *ucontrol)
2209{
2210 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2211 hda_nid_t nid = get_amp_nid(kcontrol);
2212 int chs = get_amp_channels(kcontrol);
2213 int dir = get_amp_direction(kcontrol);
2214 int idx = get_amp_index(kcontrol);
2215 long *valp = ucontrol->value.integer.value;
2216
2217 if (chs & 1)
2218 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2219 HDA_AMP_MUTE) ? 0 : 1;
2220 if (chs & 2)
2221 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2222 HDA_AMP_MUTE) ? 0 : 1;
2223 return 0;
2224}
2225EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2226
2227
2228
2229
2230
2231
2232
2233int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2234 struct snd_ctl_elem_value *ucontrol)
2235{
2236 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2237 hda_nid_t nid = get_amp_nid(kcontrol);
2238 int chs = get_amp_channels(kcontrol);
2239 int dir = get_amp_direction(kcontrol);
2240 int idx = get_amp_index(kcontrol);
2241 long *valp = ucontrol->value.integer.value;
2242 int change = 0;
2243
2244 snd_hda_power_up(codec);
2245 if (chs & 1) {
2246 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2247 HDA_AMP_MUTE,
2248 *valp ? 0 : HDA_AMP_MUTE);
2249 valp++;
2250 }
2251 if (chs & 2)
2252 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2253 HDA_AMP_MUTE,
2254 *valp ? 0 : HDA_AMP_MUTE);
2255 hda_call_check_power_status(codec, nid);
2256 snd_hda_power_down(codec);
2257 return change;
2258}
2259EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2260
2261#ifdef CONFIG_SND_HDA_INPUT_BEEP
2262
2263
2264
2265
2266
2267
2268int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2269 struct snd_ctl_elem_value *ucontrol)
2270{
2271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2272 long *valp = ucontrol->value.integer.value;
2273
2274 snd_hda_enable_beep_device(codec, *valp);
2275 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2276}
2277EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2278#endif
2279
2280
2281
2282
2283
2284
2285
2286#define AMP_VAL_IDX_SHIFT 19
2287#define AMP_VAL_IDX_MASK (0x0f<<19)
2288
2289
2290
2291
2292
2293
2294
2295int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2297{
2298 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2299 unsigned long pval;
2300 int err;
2301
2302 mutex_lock(&codec->control_mutex);
2303 pval = kcontrol->private_value;
2304 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK;
2305 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2306 kcontrol->private_value = pval;
2307 mutex_unlock(&codec->control_mutex);
2308 return err;
2309}
2310EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2311
2312
2313
2314
2315
2316
2317
2318int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_value *ucontrol)
2320{
2321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2322 unsigned long pval;
2323 int i, indices, err = 0, change = 0;
2324
2325 mutex_lock(&codec->control_mutex);
2326 pval = kcontrol->private_value;
2327 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2328 for (i = 0; i < indices; i++) {
2329 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2330 (i << AMP_VAL_IDX_SHIFT);
2331 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2332 if (err < 0)
2333 break;
2334 change |= err;
2335 }
2336 kcontrol->private_value = pval;
2337 mutex_unlock(&codec->control_mutex);
2338 return err < 0 ? err : change;
2339}
2340EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2341
2342
2343
2344
2345
2346
2347
2348int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_info *uinfo)
2350{
2351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2352 struct hda_bind_ctls *c;
2353 int err;
2354
2355 mutex_lock(&codec->control_mutex);
2356 c = (struct hda_bind_ctls *)kcontrol->private_value;
2357 kcontrol->private_value = *c->values;
2358 err = c->ops->info(kcontrol, uinfo);
2359 kcontrol->private_value = (long)c;
2360 mutex_unlock(&codec->control_mutex);
2361 return err;
2362}
2363EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2364
2365
2366
2367
2368
2369
2370
2371int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2372 struct snd_ctl_elem_value *ucontrol)
2373{
2374 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2375 struct hda_bind_ctls *c;
2376 int err;
2377
2378 mutex_lock(&codec->control_mutex);
2379 c = (struct hda_bind_ctls *)kcontrol->private_value;
2380 kcontrol->private_value = *c->values;
2381 err = c->ops->get(kcontrol, ucontrol);
2382 kcontrol->private_value = (long)c;
2383 mutex_unlock(&codec->control_mutex);
2384 return err;
2385}
2386EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2387
2388
2389
2390
2391
2392
2393
2394int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
2396{
2397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2398 struct hda_bind_ctls *c;
2399 unsigned long *vals;
2400 int err = 0, change = 0;
2401
2402 mutex_lock(&codec->control_mutex);
2403 c = (struct hda_bind_ctls *)kcontrol->private_value;
2404 for (vals = c->values; *vals; vals++) {
2405 kcontrol->private_value = *vals;
2406 err = c->ops->put(kcontrol, ucontrol);
2407 if (err < 0)
2408 break;
2409 change |= err;
2410 }
2411 kcontrol->private_value = (long)c;
2412 mutex_unlock(&codec->control_mutex);
2413 return err < 0 ? err : change;
2414}
2415EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2416
2417
2418
2419
2420
2421
2422
2423int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2424 unsigned int size, unsigned int __user *tlv)
2425{
2426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2427 struct hda_bind_ctls *c;
2428 int err;
2429
2430 mutex_lock(&codec->control_mutex);
2431 c = (struct hda_bind_ctls *)kcontrol->private_value;
2432 kcontrol->private_value = *c->values;
2433 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2434 kcontrol->private_value = (long)c;
2435 mutex_unlock(&codec->control_mutex);
2436 return err;
2437}
2438EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2439
2440struct hda_ctl_ops snd_hda_bind_vol = {
2441 .info = snd_hda_mixer_amp_volume_info,
2442 .get = snd_hda_mixer_amp_volume_get,
2443 .put = snd_hda_mixer_amp_volume_put,
2444 .tlv = snd_hda_mixer_amp_tlv
2445};
2446EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2447
2448struct hda_ctl_ops snd_hda_bind_sw = {
2449 .info = snd_hda_mixer_amp_switch_info,
2450 .get = snd_hda_mixer_amp_switch_get,
2451 .put = snd_hda_mixer_amp_switch_put,
2452 .tlv = snd_hda_mixer_amp_tlv
2453};
2454EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2455
2456
2457
2458
2459
2460static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_info *uinfo)
2462{
2463 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2464 uinfo->count = 1;
2465 return 0;
2466}
2467
2468static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2472 IEC958_AES0_NONAUDIO |
2473 IEC958_AES0_CON_EMPHASIS_5015 |
2474 IEC958_AES0_CON_NOT_COPYRIGHT;
2475 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2476 IEC958_AES1_CON_ORIGINAL;
2477 return 0;
2478}
2479
2480static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2482{
2483 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2484 IEC958_AES0_NONAUDIO |
2485 IEC958_AES0_PRO_EMPHASIS_5015;
2486 return 0;
2487}
2488
2489static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2490 struct snd_ctl_elem_value *ucontrol)
2491{
2492 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2493
2494 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2495 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2496 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2497 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2498
2499 return 0;
2500}
2501
2502
2503
2504
2505static unsigned short convert_from_spdif_status(unsigned int sbits)
2506{
2507 unsigned short val = 0;
2508
2509 if (sbits & IEC958_AES0_PROFESSIONAL)
2510 val |= AC_DIG1_PROFESSIONAL;
2511 if (sbits & IEC958_AES0_NONAUDIO)
2512 val |= AC_DIG1_NONAUDIO;
2513 if (sbits & IEC958_AES0_PROFESSIONAL) {
2514 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2515 IEC958_AES0_PRO_EMPHASIS_5015)
2516 val |= AC_DIG1_EMPHASIS;
2517 } else {
2518 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2519 IEC958_AES0_CON_EMPHASIS_5015)
2520 val |= AC_DIG1_EMPHASIS;
2521 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2522 val |= AC_DIG1_COPYRIGHT;
2523 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2524 val |= AC_DIG1_LEVEL;
2525 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2526 }
2527 return val;
2528}
2529
2530
2531
2532static unsigned int convert_to_spdif_status(unsigned short val)
2533{
2534 unsigned int sbits = 0;
2535
2536 if (val & AC_DIG1_NONAUDIO)
2537 sbits |= IEC958_AES0_NONAUDIO;
2538 if (val & AC_DIG1_PROFESSIONAL)
2539 sbits |= IEC958_AES0_PROFESSIONAL;
2540 if (sbits & IEC958_AES0_PROFESSIONAL) {
2541 if (sbits & AC_DIG1_EMPHASIS)
2542 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2543 } else {
2544 if (val & AC_DIG1_EMPHASIS)
2545 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2546 if (!(val & AC_DIG1_COPYRIGHT))
2547 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2548 if (val & AC_DIG1_LEVEL)
2549 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2550 sbits |= val & (0x7f << 8);
2551 }
2552 return sbits;
2553}
2554
2555
2556static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2557 int verb, int val)
2558{
2559 hda_nid_t *d;
2560
2561 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2562 d = codec->slave_dig_outs;
2563 if (!d)
2564 return;
2565 for (; *d; d++)
2566 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2567}
2568
2569static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2570 int dig1, int dig2)
2571{
2572 if (dig1 != -1)
2573 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2574 if (dig2 != -1)
2575 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2576}
2577
2578static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_value *ucontrol)
2580{
2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2582 hda_nid_t nid = kcontrol->private_value;
2583 unsigned short val;
2584 int change;
2585
2586 mutex_lock(&codec->spdif_mutex);
2587 codec->spdif_status = ucontrol->value.iec958.status[0] |
2588 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2589 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2590 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2591 val = convert_from_spdif_status(codec->spdif_status);
2592 val |= codec->spdif_ctls & 1;
2593 change = codec->spdif_ctls != val;
2594 codec->spdif_ctls = val;
2595
2596 if (change)
2597 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2598
2599 mutex_unlock(&codec->spdif_mutex);
2600 return change;
2601}
2602
2603#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2604
2605static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_value *ucontrol)
2607{
2608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2609
2610 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2611 return 0;
2612}
2613
2614static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2615 struct snd_ctl_elem_value *ucontrol)
2616{
2617 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2618 hda_nid_t nid = kcontrol->private_value;
2619 unsigned short val;
2620 int change;
2621
2622 mutex_lock(&codec->spdif_mutex);
2623 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2624 if (ucontrol->value.integer.value[0])
2625 val |= AC_DIG1_ENABLE;
2626 change = codec->spdif_ctls != val;
2627 if (change) {
2628 codec->spdif_ctls = val;
2629 set_dig_out_convert(codec, nid, val & 0xff, -1);
2630
2631 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2632 (val & AC_DIG1_ENABLE))
2633 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2634 HDA_AMP_MUTE, 0);
2635 }
2636 mutex_unlock(&codec->spdif_mutex);
2637 return change;
2638}
2639
2640static struct snd_kcontrol_new dig_mixes[] = {
2641 {
2642 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2643 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2644 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2645 .info = snd_hda_spdif_mask_info,
2646 .get = snd_hda_spdif_cmask_get,
2647 },
2648 {
2649 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2652 .info = snd_hda_spdif_mask_info,
2653 .get = snd_hda_spdif_pmask_get,
2654 },
2655 {
2656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2657 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2658 .info = snd_hda_spdif_mask_info,
2659 .get = snd_hda_spdif_default_get,
2660 .put = snd_hda_spdif_default_put,
2661 },
2662 {
2663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2664 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2665 .info = snd_hda_spdif_out_switch_info,
2666 .get = snd_hda_spdif_out_switch_get,
2667 .put = snd_hda_spdif_out_switch_put,
2668 },
2669 { }
2670};
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2683{
2684 int err;
2685 struct snd_kcontrol *kctl;
2686 struct snd_kcontrol_new *dig_mix;
2687 int idx;
2688
2689 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2690 if (idx < 0) {
2691 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2692 return -EBUSY;
2693 }
2694 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2695 kctl = snd_ctl_new1(dig_mix, codec);
2696 if (!kctl)
2697 return -ENOMEM;
2698 kctl->id.index = idx;
2699 kctl->private_value = nid;
2700 err = snd_hda_ctl_add(codec, nid, kctl);
2701 if (err < 0)
2702 return err;
2703 }
2704 codec->spdif_ctls =
2705 snd_hda_codec_read(codec, nid, 0,
2706 AC_VERB_GET_DIGI_CONVERT_1, 0);
2707 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2708 return 0;
2709}
2710EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2711
2712
2713
2714
2715static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2716 struct snd_ctl_elem_value *ucontrol)
2717{
2718 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2719 ucontrol->value.integer.value[0] = mout->share_spdif;
2720 return 0;
2721}
2722
2723static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2725{
2726 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2727 mout->share_spdif = !!ucontrol->value.integer.value[0];
2728 return 0;
2729}
2730
2731static struct snd_kcontrol_new spdif_share_sw = {
2732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2733 .name = "IEC958 Default PCM Playback Switch",
2734 .info = snd_ctl_boolean_mono_info,
2735 .get = spdif_share_sw_get,
2736 .put = spdif_share_sw_put,
2737};
2738
2739
2740
2741
2742
2743
2744int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2745 struct hda_multi_out *mout)
2746{
2747 if (!mout->dig_out_nid)
2748 return 0;
2749
2750 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2751 snd_ctl_new1(&spdif_share_sw, mout));
2752}
2753EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2754
2755
2756
2757
2758
2759#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2760
2761static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2763{
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2765
2766 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2767 return 0;
2768}
2769
2770static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2771 struct snd_ctl_elem_value *ucontrol)
2772{
2773 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2774 hda_nid_t nid = kcontrol->private_value;
2775 unsigned int val = !!ucontrol->value.integer.value[0];
2776 int change;
2777
2778 mutex_lock(&codec->spdif_mutex);
2779 change = codec->spdif_in_enable != val;
2780 if (change) {
2781 codec->spdif_in_enable = val;
2782 snd_hda_codec_write_cache(codec, nid, 0,
2783 AC_VERB_SET_DIGI_CONVERT_1, val);
2784 }
2785 mutex_unlock(&codec->spdif_mutex);
2786 return change;
2787}
2788
2789static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
2791{
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2793 hda_nid_t nid = kcontrol->private_value;
2794 unsigned short val;
2795 unsigned int sbits;
2796
2797 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2798 sbits = convert_to_spdif_status(val);
2799 ucontrol->value.iec958.status[0] = sbits;
2800 ucontrol->value.iec958.status[1] = sbits >> 8;
2801 ucontrol->value.iec958.status[2] = sbits >> 16;
2802 ucontrol->value.iec958.status[3] = sbits >> 24;
2803 return 0;
2804}
2805
2806static struct snd_kcontrol_new dig_in_ctls[] = {
2807 {
2808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2809 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2810 .info = snd_hda_spdif_in_switch_info,
2811 .get = snd_hda_spdif_in_switch_get,
2812 .put = snd_hda_spdif_in_switch_put,
2813 },
2814 {
2815 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2816 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2817 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2818 .info = snd_hda_spdif_mask_info,
2819 .get = snd_hda_spdif_in_status_get,
2820 },
2821 { }
2822};
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2835{
2836 int err;
2837 struct snd_kcontrol *kctl;
2838 struct snd_kcontrol_new *dig_mix;
2839 int idx;
2840
2841 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
2842 if (idx < 0) {
2843 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2844 return -EBUSY;
2845 }
2846 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2847 kctl = snd_ctl_new1(dig_mix, codec);
2848 if (!kctl)
2849 return -ENOMEM;
2850 kctl->private_value = nid;
2851 err = snd_hda_ctl_add(codec, nid, kctl);
2852 if (err < 0)
2853 return err;
2854 }
2855 codec->spdif_in_enable =
2856 snd_hda_codec_read(codec, nid, 0,
2857 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2858 AC_DIG1_ENABLE;
2859 return 0;
2860}
2861EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2862
2863#ifdef SND_HDA_NEEDS_RESUME
2864
2865
2866
2867
2868
2869#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2870#define get_cmd_cache_nid(key) ((key) & 0xff)
2871#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2886 int direct, unsigned int verb, unsigned int parm)
2887{
2888 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2889 struct hda_cache_head *c;
2890 u32 key;
2891
2892 if (err < 0)
2893 return err;
2894
2895 verb = verb | (parm >> 8);
2896 parm &= 0xff;
2897 key = build_cmd_cache_key(nid, verb);
2898 mutex_lock(&codec->bus->cmd_mutex);
2899 c = get_alloc_hash(&codec->cmd_cache, key);
2900 if (c)
2901 c->val = parm;
2902 mutex_unlock(&codec->bus->cmd_mutex);
2903 return 0;
2904}
2905EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2922 int direct, unsigned int verb, unsigned int parm)
2923{
2924 struct hda_cache_head *c;
2925 u32 key;
2926
2927
2928 verb = verb | (parm >> 8);
2929 parm &= 0xff;
2930 key = build_cmd_cache_key(nid, verb);
2931 mutex_lock(&codec->bus->cmd_mutex);
2932 c = get_hash(&codec->cmd_cache, key);
2933 if (c && c->val == parm) {
2934 mutex_unlock(&codec->bus->cmd_mutex);
2935 return 0;
2936 }
2937 mutex_unlock(&codec->bus->cmd_mutex);
2938 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2939}
2940EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2941
2942
2943
2944
2945
2946
2947
2948void snd_hda_codec_resume_cache(struct hda_codec *codec)
2949{
2950 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2951 int i;
2952
2953 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2954 u32 key = buffer->key;
2955 if (!key)
2956 continue;
2957 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2958 get_cmd_cache_cmd(key), buffer->val);
2959 }
2960}
2961EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972void snd_hda_sequence_write_cache(struct hda_codec *codec,
2973 const struct hda_verb *seq)
2974{
2975 for (; seq->nid; seq++)
2976 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2977 seq->param);
2978}
2979EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2980#endif
2981
2982
2983
2984
2985static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2986 unsigned int power_state)
2987{
2988 hda_nid_t nid;
2989 int i;
2990
2991
2992 if (power_state == AC_PWRST_D3)
2993 msleep(100);
2994 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2995 power_state);
2996
2997 if (power_state == AC_PWRST_D0 &&
2998 (codec->vendor_id & 0xffff0000) == 0x14f10000)
2999 msleep(10);
3000
3001 nid = codec->start_nid;
3002 for (i = 0; i < codec->num_nodes; i++, nid++) {
3003 unsigned int wcaps = get_wcaps(codec, nid);
3004 if (wcaps & AC_WCAP_POWER) {
3005 unsigned int wid_type = get_wcaps_type(wcaps);
3006 if (power_state == AC_PWRST_D3 &&
3007 wid_type == AC_WID_PIN) {
3008 unsigned int pincap;
3009
3010
3011
3012
3013 pincap = snd_hda_query_pin_caps(codec, nid);
3014 if (pincap & AC_PINCAP_EAPD) {
3015 int eapd = snd_hda_codec_read(codec,
3016 nid, 0,
3017 AC_VERB_GET_EAPD_BTLENABLE, 0);
3018 eapd &= 0x02;
3019 if (eapd)
3020 continue;
3021 }
3022 }
3023 snd_hda_codec_write(codec, nid, 0,
3024 AC_VERB_SET_POWER_STATE,
3025 power_state);
3026 }
3027 }
3028
3029 if (power_state == AC_PWRST_D0) {
3030 unsigned long end_time;
3031 int state;
3032
3033 end_time = jiffies + msecs_to_jiffies(500);
3034 do {
3035 state = snd_hda_codec_read(codec, fg, 0,
3036 AC_VERB_GET_POWER_STATE, 0);
3037 if (state == power_state)
3038 break;
3039 msleep(1);
3040 } while (time_after_eq(end_time, jiffies));
3041 }
3042}
3043
3044#ifdef CONFIG_SND_HDA_HWDEP
3045
3046static void hda_exec_init_verbs(struct hda_codec *codec)
3047{
3048 if (codec->init_verbs.list)
3049 snd_hda_sequence_write(codec, codec->init_verbs.list);
3050}
3051#else
3052static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3053#endif
3054
3055#ifdef SND_HDA_NEEDS_RESUME
3056
3057
3058
3059static void hda_call_codec_suspend(struct hda_codec *codec)
3060{
3061 if (codec->patch_ops.suspend)
3062 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3063 hda_cleanup_all_streams(codec);
3064 hda_set_power_state(codec,
3065 codec->afg ? codec->afg : codec->mfg,
3066 AC_PWRST_D3);
3067#ifdef CONFIG_SND_HDA_POWER_SAVE
3068 snd_hda_update_power_acct(codec);
3069 cancel_delayed_work(&codec->power_work);
3070 codec->power_on = 0;
3071 codec->power_transition = 0;
3072 codec->power_jiffies = jiffies;
3073#endif
3074}
3075
3076
3077
3078
3079static void hda_call_codec_resume(struct hda_codec *codec)
3080{
3081 hda_set_power_state(codec,
3082 codec->afg ? codec->afg : codec->mfg,
3083 AC_PWRST_D0);
3084 restore_pincfgs(codec);
3085 restore_shutup_pins(codec);
3086 hda_exec_init_verbs(codec);
3087 if (codec->patch_ops.resume)
3088 codec->patch_ops.resume(codec);
3089 else {
3090 if (codec->patch_ops.init)
3091 codec->patch_ops.init(codec);
3092 snd_hda_codec_resume_amp(codec);
3093 snd_hda_codec_resume_cache(codec);
3094 }
3095}
3096#endif
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107int snd_hda_build_controls(struct hda_bus *bus)
3108{
3109 struct hda_codec *codec;
3110
3111 list_for_each_entry(codec, &bus->codec_list, list) {
3112 int err = snd_hda_codec_build_controls(codec);
3113 if (err < 0) {
3114 printk(KERN_ERR "hda_codec: cannot build controls "
3115 "for #%d (error %d)\n", codec->addr, err);
3116 err = snd_hda_codec_reset(codec);
3117 if (err < 0) {
3118 printk(KERN_ERR
3119 "hda_codec: cannot revert codec\n");
3120 return err;
3121 }
3122 }
3123 }
3124 return 0;
3125}
3126EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3127
3128int snd_hda_codec_build_controls(struct hda_codec *codec)
3129{
3130 int err = 0;
3131 hda_exec_init_verbs(codec);
3132
3133 if (codec->patch_ops.init)
3134 err = codec->patch_ops.init(codec);
3135 if (!err && codec->patch_ops.build_controls)
3136 err = codec->patch_ops.build_controls(codec);
3137 if (err < 0)
3138 return err;
3139 return 0;
3140}
3141
3142
3143
3144
3145struct hda_rate_tbl {
3146 unsigned int hz;
3147 unsigned int alsa_bits;
3148 unsigned int hda_fmt;
3149};
3150
3151
3152#define HDA_RATE(base, mult, div) \
3153 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3154 (((div) - 1) << AC_FMT_DIV_SHIFT))
3155
3156static struct hda_rate_tbl rate_bits[] = {
3157
3158
3159
3160 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3161 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3162 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3163 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3164 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3165 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3166 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3167 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3168 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3169 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3170 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3171#define AC_PAR_PCM_RATE_BITS 11
3172
3173
3174
3175 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3176
3177 { 0 }
3178};
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191unsigned int snd_hda_calc_stream_format(unsigned int rate,
3192 unsigned int channels,
3193 unsigned int format,
3194 unsigned int maxbps,
3195 unsigned short spdif_ctls)
3196{
3197 int i;
3198 unsigned int val = 0;
3199
3200 for (i = 0; rate_bits[i].hz; i++)
3201 if (rate_bits[i].hz == rate) {
3202 val = rate_bits[i].hda_fmt;
3203 break;
3204 }
3205 if (!rate_bits[i].hz) {
3206 snd_printdd("invalid rate %d\n", rate);
3207 return 0;
3208 }
3209
3210 if (channels == 0 || channels > 8) {
3211 snd_printdd("invalid channels %d\n", channels);
3212 return 0;
3213 }
3214 val |= channels - 1;
3215
3216 switch (snd_pcm_format_width(format)) {
3217 case 8:
3218 val |= AC_FMT_BITS_8;
3219 break;
3220 case 16:
3221 val |= AC_FMT_BITS_16;
3222 break;
3223 case 20:
3224 case 24:
3225 case 32:
3226 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3227 val |= AC_FMT_BITS_32;
3228 else if (maxbps >= 24)
3229 val |= AC_FMT_BITS_24;
3230 else
3231 val |= AC_FMT_BITS_20;
3232 break;
3233 default:
3234 snd_printdd("invalid format width %d\n",
3235 snd_pcm_format_width(format));
3236 return 0;
3237 }
3238
3239 if (spdif_ctls & AC_DIG1_NONAUDIO)
3240 val |= AC_FMT_TYPE_NON_PCM;
3241
3242 return val;
3243}
3244EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3245
3246static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3247{
3248 unsigned int val = 0;
3249 if (nid != codec->afg &&
3250 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3251 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3252 if (!val || val == -1)
3253 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3254 if (!val || val == -1)
3255 return 0;
3256 return val;
3257}
3258
3259static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3260{
3261 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3262 get_pcm_param);
3263}
3264
3265static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3266{
3267 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3268 if (!streams || streams == -1)
3269 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3270 if (!streams || streams == -1)
3271 return 0;
3272 return streams;
3273}
3274
3275static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3276{
3277 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3278 get_stream_param);
3279}
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3295 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3296{
3297 unsigned int i, val, wcaps;
3298
3299 wcaps = get_wcaps(codec, nid);
3300 val = query_pcm_param(codec, nid);
3301
3302 if (ratesp) {
3303 u32 rates = 0;
3304 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3305 if (val & (1 << i))
3306 rates |= rate_bits[i].alsa_bits;
3307 }
3308 if (rates == 0) {
3309 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3310 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3311 nid, val,
3312 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3313 return -EIO;
3314 }
3315 *ratesp = rates;
3316 }
3317
3318 if (formatsp || bpsp) {
3319 u64 formats = 0;
3320 unsigned int streams, bps;
3321
3322 streams = query_stream_param(codec, nid);
3323 if (!streams)
3324 return -EIO;
3325
3326 bps = 0;
3327 if (streams & AC_SUPFMT_PCM) {
3328 if (val & AC_SUPPCM_BITS_8) {
3329 formats |= SNDRV_PCM_FMTBIT_U8;
3330 bps = 8;
3331 }
3332 if (val & AC_SUPPCM_BITS_16) {
3333 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3334 bps = 16;
3335 }
3336 if (wcaps & AC_WCAP_DIGITAL) {
3337 if (val & AC_SUPPCM_BITS_32)
3338 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3339 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3340 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3341 if (val & AC_SUPPCM_BITS_24)
3342 bps = 24;
3343 else if (val & AC_SUPPCM_BITS_20)
3344 bps = 20;
3345 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3346 AC_SUPPCM_BITS_32)) {
3347 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3348 if (val & AC_SUPPCM_BITS_32)
3349 bps = 32;
3350 else if (val & AC_SUPPCM_BITS_24)
3351 bps = 24;
3352 else if (val & AC_SUPPCM_BITS_20)
3353 bps = 20;
3354 }
3355 }
3356 if (streams & AC_SUPFMT_FLOAT32) {
3357 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3358 if (!bps)
3359 bps = 32;
3360 }
3361 if (streams == AC_SUPFMT_AC3) {
3362
3363
3364
3365
3366 formats |= SNDRV_PCM_FMTBIT_U8;
3367 bps = 8;
3368 }
3369 if (formats == 0) {
3370 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3371 "(nid=0x%x, val=0x%x, ovrd=%i, "
3372 "streams=0x%x)\n",
3373 nid, val,
3374 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3375 streams);
3376 return -EIO;
3377 }
3378 if (formatsp)
3379 *formatsp = formats;
3380 if (bpsp)
3381 *bpsp = bps;
3382 }
3383
3384 return 0;
3385}
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3398 unsigned int format)
3399{
3400 int i;
3401 unsigned int val = 0, rate, stream;
3402
3403 val = query_pcm_param(codec, nid);
3404 if (!val)
3405 return 0;
3406
3407 rate = format & 0xff00;
3408 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3409 if (rate_bits[i].hda_fmt == rate) {
3410 if (val & (1 << i))
3411 break;
3412 return 0;
3413 }
3414 if (i >= AC_PAR_PCM_RATE_BITS)
3415 return 0;
3416
3417 stream = query_stream_param(codec, nid);
3418 if (!stream)
3419 return 0;
3420
3421 if (stream & AC_SUPFMT_PCM) {
3422 switch (format & 0xf0) {
3423 case 0x00:
3424 if (!(val & AC_SUPPCM_BITS_8))
3425 return 0;
3426 break;
3427 case 0x10:
3428 if (!(val & AC_SUPPCM_BITS_16))
3429 return 0;
3430 break;
3431 case 0x20:
3432 if (!(val & AC_SUPPCM_BITS_20))
3433 return 0;
3434 break;
3435 case 0x30:
3436 if (!(val & AC_SUPPCM_BITS_24))
3437 return 0;
3438 break;
3439 case 0x40:
3440 if (!(val & AC_SUPPCM_BITS_32))
3441 return 0;
3442 break;
3443 default:
3444 return 0;
3445 }
3446 } else {
3447
3448 }
3449
3450 return 1;
3451}
3452EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3453
3454
3455
3456
3457static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3458 struct hda_codec *codec,
3459 struct snd_pcm_substream *substream)
3460{
3461 return 0;
3462}
3463
3464static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3465 struct hda_codec *codec,
3466 unsigned int stream_tag,
3467 unsigned int format,
3468 struct snd_pcm_substream *substream)
3469{
3470 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3471 return 0;
3472}
3473
3474static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3475 struct hda_codec *codec,
3476 struct snd_pcm_substream *substream)
3477{
3478 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3479 return 0;
3480}
3481
3482static int set_pcm_default_values(struct hda_codec *codec,
3483 struct hda_pcm_stream *info)
3484{
3485 int err;
3486
3487
3488 if (info->nid && (!info->rates || !info->formats)) {
3489 err = snd_hda_query_supported_pcm(codec, info->nid,
3490 info->rates ? NULL : &info->rates,
3491 info->formats ? NULL : &info->formats,
3492 info->maxbps ? NULL : &info->maxbps);
3493 if (err < 0)
3494 return err;
3495 }
3496 if (info->ops.open == NULL)
3497 info->ops.open = hda_pcm_default_open_close;
3498 if (info->ops.close == NULL)
3499 info->ops.close = hda_pcm_default_open_close;
3500 if (info->ops.prepare == NULL) {
3501 if (snd_BUG_ON(!info->nid))
3502 return -EINVAL;
3503 info->ops.prepare = hda_pcm_default_prepare;
3504 }
3505 if (info->ops.cleanup == NULL) {
3506 if (snd_BUG_ON(!info->nid))
3507 return -EINVAL;
3508 info->ops.cleanup = hda_pcm_default_cleanup;
3509 }
3510 return 0;
3511}
3512
3513
3514
3515
3516int snd_hda_codec_prepare(struct hda_codec *codec,
3517 struct hda_pcm_stream *hinfo,
3518 unsigned int stream,
3519 unsigned int format,
3520 struct snd_pcm_substream *substream)
3521{
3522 int ret;
3523 mutex_lock(&codec->bus->prepare_mutex);
3524 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3525 if (ret >= 0)
3526 purify_inactive_streams(codec);
3527 mutex_unlock(&codec->bus->prepare_mutex);
3528 return ret;
3529}
3530EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3531
3532void snd_hda_codec_cleanup(struct hda_codec *codec,
3533 struct hda_pcm_stream *hinfo,
3534 struct snd_pcm_substream *substream)
3535{
3536 mutex_lock(&codec->bus->prepare_mutex);
3537 hinfo->ops.cleanup(hinfo, codec, substream);
3538 mutex_unlock(&codec->bus->prepare_mutex);
3539}
3540EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3541
3542
3543const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3544 "Audio", "SPDIF", "HDMI", "Modem"
3545};
3546
3547
3548
3549
3550
3551
3552static int get_empty_pcm_device(struct hda_bus *bus, int type)
3553{
3554
3555 static int audio_idx[HDA_PCM_NTYPES][5] = {
3556 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3557 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3558 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3559 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3560 };
3561 int i;
3562
3563 if (type >= HDA_PCM_NTYPES) {
3564 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3565 return -EINVAL;
3566 }
3567
3568 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3569 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3570 return audio_idx[type][i];
3571
3572 snd_printk(KERN_WARNING "Too many %s devices\n",
3573 snd_hda_pcm_type_name[type]);
3574 return -EAGAIN;
3575}
3576
3577
3578
3579
3580static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3581{
3582 struct hda_bus *bus = codec->bus;
3583 struct hda_pcm_stream *info;
3584 int stream, err;
3585
3586 if (snd_BUG_ON(!pcm->name))
3587 return -EINVAL;
3588 for (stream = 0; stream < 2; stream++) {
3589 info = &pcm->stream[stream];
3590 if (info->substreams) {
3591 err = set_pcm_default_values(codec, info);
3592 if (err < 0)
3593 return err;
3594 }
3595 }
3596 return bus->ops.attach_pcm(bus, codec, pcm);
3597}
3598
3599
3600int snd_hda_codec_build_pcms(struct hda_codec *codec)
3601{
3602 unsigned int pcm;
3603 int err;
3604
3605 if (!codec->num_pcms) {
3606 if (!codec->patch_ops.build_pcms)
3607 return 0;
3608 err = codec->patch_ops.build_pcms(codec);
3609 if (err < 0) {
3610 printk(KERN_ERR "hda_codec: cannot build PCMs"
3611 "for #%d (error %d)\n", codec->addr, err);
3612 err = snd_hda_codec_reset(codec);
3613 if (err < 0) {
3614 printk(KERN_ERR
3615 "hda_codec: cannot revert codec\n");
3616 return err;
3617 }
3618 }
3619 }
3620 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3621 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3622 int dev;
3623
3624 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3625 continue;
3626
3627 if (!cpcm->pcm) {
3628 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3629 if (dev < 0)
3630 continue;
3631 cpcm->device = dev;
3632 err = snd_hda_attach_pcm(codec, cpcm);
3633 if (err < 0) {
3634 printk(KERN_ERR "hda_codec: cannot attach "
3635 "PCM stream %d for codec #%d\n",
3636 dev, codec->addr);
3637 continue;
3638 }
3639 }
3640 }
3641 return 0;
3642}
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3671{
3672 struct hda_codec *codec;
3673
3674 list_for_each_entry(codec, &bus->codec_list, list) {
3675 int err = snd_hda_codec_build_pcms(codec);
3676 if (err < 0)
3677 return err;
3678 }
3679 return 0;
3680}
3681EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696int snd_hda_check_board_config(struct hda_codec *codec,
3697 int num_configs, const char * const *models,
3698 const struct snd_pci_quirk *tbl)
3699{
3700 if (codec->modelname && models) {
3701 int i;
3702 for (i = 0; i < num_configs; i++) {
3703 if (models[i] &&
3704 !strcmp(codec->modelname, models[i])) {
3705 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3706 "selected\n", models[i]);
3707 return i;
3708 }
3709 }
3710 }
3711
3712 if (!codec->bus->pci || !tbl)
3713 return -1;
3714
3715 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3716 if (!tbl)
3717 return -1;
3718 if (tbl->value >= 0 && tbl->value < num_configs) {
3719#ifdef CONFIG_SND_DEBUG_VERBOSE
3720 char tmp[10];
3721 const char *model = NULL;
3722 if (models)
3723 model = models[tbl->value];
3724 if (!model) {
3725 sprintf(tmp, "#%d", tbl->value);
3726 model = tmp;
3727 }
3728 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3729 "for config %x:%x (%s)\n",
3730 model, tbl->subvendor, tbl->subdevice,
3731 (tbl->name ? tbl->name : "Unknown device"));
3732#endif
3733 return tbl->value;
3734 }
3735 return -1;
3736}
3737EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3761 int num_configs, const char * const *models,
3762 const struct snd_pci_quirk *tbl)
3763{
3764 const struct snd_pci_quirk *q;
3765
3766
3767 for (q = tbl; q->subvendor; q++) {
3768 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3769
3770 if (vendorid == codec->subsystem_id)
3771 break;
3772 }
3773
3774 if (!q->subvendor)
3775 return -1;
3776
3777 tbl = q;
3778
3779 if (tbl->value >= 0 && tbl->value < num_configs) {
3780#ifdef CONFIG_SND_DEBUG_VERBOSE
3781 char tmp[10];
3782 const char *model = NULL;
3783 if (models)
3784 model = models[tbl->value];
3785 if (!model) {
3786 sprintf(tmp, "#%d", tbl->value);
3787 model = tmp;
3788 }
3789 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3790 "for config %x:%x (%s)\n",
3791 model, tbl->subvendor, tbl->subdevice,
3792 (tbl->name ? tbl->name : "Unknown device"));
3793#endif
3794 return tbl->value;
3795 }
3796 return -1;
3797}
3798EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3811{
3812 int err;
3813
3814 for (; knew->name; knew++) {
3815 struct snd_kcontrol *kctl;
3816 int addr = 0, idx = 0;
3817 if (knew->iface == -1)
3818 continue;
3819 for (;;) {
3820 kctl = snd_ctl_new1(knew, codec);
3821 if (!kctl)
3822 return -ENOMEM;
3823 if (addr > 0)
3824 kctl->id.device = addr;
3825 if (idx > 0)
3826 kctl->id.index = idx;
3827 err = snd_hda_ctl_add(codec, 0, kctl);
3828 if (!err)
3829 break;
3830
3831
3832
3833
3834 if (!addr && codec->addr)
3835 addr = codec->addr;
3836 else if (!idx && !knew->index) {
3837 idx = find_empty_mixer_ctl_idx(codec,
3838 knew->name);
3839 if (idx <= 0)
3840 return err;
3841 } else
3842 return err;
3843 }
3844 }
3845 return 0;
3846}
3847EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3848
3849#ifdef CONFIG_SND_HDA_POWER_SAVE
3850static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3851 unsigned int power_state);
3852
3853static void hda_power_work(struct work_struct *work)
3854{
3855 struct hda_codec *codec =
3856 container_of(work, struct hda_codec, power_work.work);
3857 struct hda_bus *bus = codec->bus;
3858
3859 if (!codec->power_on || codec->power_count) {
3860 codec->power_transition = 0;
3861 return;
3862 }
3863
3864 hda_call_codec_suspend(codec);
3865 if (bus->ops.pm_notify)
3866 bus->ops.pm_notify(bus);
3867}
3868
3869static void hda_keep_power_on(struct hda_codec *codec)
3870{
3871 codec->power_count++;
3872 codec->power_on = 1;
3873 codec->power_jiffies = jiffies;
3874}
3875
3876
3877void snd_hda_update_power_acct(struct hda_codec *codec)
3878{
3879 unsigned long delta = jiffies - codec->power_jiffies;
3880 if (codec->power_on)
3881 codec->power_on_acct += delta;
3882 else
3883 codec->power_off_acct += delta;
3884 codec->power_jiffies += delta;
3885}
3886
3887
3888
3889
3890
3891
3892
3893
3894void snd_hda_power_up(struct hda_codec *codec)
3895{
3896 struct hda_bus *bus = codec->bus;
3897
3898 codec->power_count++;
3899 if (codec->power_on || codec->power_transition)
3900 return;
3901
3902 snd_hda_update_power_acct(codec);
3903 codec->power_on = 1;
3904 codec->power_jiffies = jiffies;
3905 if (bus->ops.pm_notify)
3906 bus->ops.pm_notify(bus);
3907 hda_call_codec_resume(codec);
3908 cancel_delayed_work(&codec->power_work);
3909 codec->power_transition = 0;
3910}
3911EXPORT_SYMBOL_HDA(snd_hda_power_up);
3912
3913#define power_save(codec) \
3914 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3915
3916
3917
3918
3919
3920
3921
3922
3923void snd_hda_power_down(struct hda_codec *codec)
3924{
3925 --codec->power_count;
3926 if (!codec->power_on || codec->power_count || codec->power_transition)
3927 return;
3928 if (power_save(codec)) {
3929 codec->power_transition = 1;
3930 queue_delayed_work(codec->bus->workq, &codec->power_work,
3931 msecs_to_jiffies(power_save(codec) * 1000));
3932 }
3933}
3934EXPORT_SYMBOL_HDA(snd_hda_power_down);
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949int snd_hda_check_amp_list_power(struct hda_codec *codec,
3950 struct hda_loopback_check *check,
3951 hda_nid_t nid)
3952{
3953 struct hda_amp_list *p;
3954 int ch, v;
3955
3956 if (!check->amplist)
3957 return 0;
3958 for (p = check->amplist; p->nid; p++) {
3959 if (p->nid == nid)
3960 break;
3961 }
3962 if (!p->nid)
3963 return 0;
3964
3965 for (p = check->amplist; p->nid; p++) {
3966 for (ch = 0; ch < 2; ch++) {
3967 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3968 p->idx);
3969 if (!(v & HDA_AMP_MUTE) && v > 0) {
3970 if (!check->power_on) {
3971 check->power_on = 1;
3972 snd_hda_power_up(codec);
3973 }
3974 return 1;
3975 }
3976 }
3977 }
3978 if (check->power_on) {
3979 check->power_on = 0;
3980 snd_hda_power_down(codec);
3981 }
3982 return 0;
3983}
3984EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3985#endif
3986
3987
3988
3989
3990
3991
3992
3993
3994int snd_hda_ch_mode_info(struct hda_codec *codec,
3995 struct snd_ctl_elem_info *uinfo,
3996 const struct hda_channel_mode *chmode,
3997 int num_chmodes)
3998{
3999 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4000 uinfo->count = 1;
4001 uinfo->value.enumerated.items = num_chmodes;
4002 if (uinfo->value.enumerated.item >= num_chmodes)
4003 uinfo->value.enumerated.item = num_chmodes - 1;
4004 sprintf(uinfo->value.enumerated.name, "%dch",
4005 chmode[uinfo->value.enumerated.item].channels);
4006 return 0;
4007}
4008EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4009
4010
4011
4012
4013int snd_hda_ch_mode_get(struct hda_codec *codec,
4014 struct snd_ctl_elem_value *ucontrol,
4015 const struct hda_channel_mode *chmode,
4016 int num_chmodes,
4017 int max_channels)
4018{
4019 int i;
4020
4021 for (i = 0; i < num_chmodes; i++) {
4022 if (max_channels == chmode[i].channels) {
4023 ucontrol->value.enumerated.item[0] = i;
4024 break;
4025 }
4026 }
4027 return 0;
4028}
4029EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4030
4031
4032
4033
4034int snd_hda_ch_mode_put(struct hda_codec *codec,
4035 struct snd_ctl_elem_value *ucontrol,
4036 const struct hda_channel_mode *chmode,
4037 int num_chmodes,
4038 int *max_channelsp)
4039{
4040 unsigned int mode;
4041
4042 mode = ucontrol->value.enumerated.item[0];
4043 if (mode >= num_chmodes)
4044 return -EINVAL;
4045 if (*max_channelsp == chmode[mode].channels)
4046 return 0;
4047
4048 *max_channelsp = chmode[mode].channels;
4049 if (chmode[mode].sequence)
4050 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4051 return 1;
4052}
4053EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4054
4055
4056
4057
4058
4059
4060
4061
4062int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4063 struct snd_ctl_elem_info *uinfo)
4064{
4065 unsigned int index;
4066
4067 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4068 uinfo->count = 1;
4069 uinfo->value.enumerated.items = imux->num_items;
4070 if (!imux->num_items)
4071 return 0;
4072 index = uinfo->value.enumerated.item;
4073 if (index >= imux->num_items)
4074 index = imux->num_items - 1;
4075 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4076 return 0;
4077}
4078EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4079
4080
4081
4082
4083int snd_hda_input_mux_put(struct hda_codec *codec,
4084 const struct hda_input_mux *imux,
4085 struct snd_ctl_elem_value *ucontrol,
4086 hda_nid_t nid,
4087 unsigned int *cur_val)
4088{
4089 unsigned int idx;
4090
4091 if (!imux->num_items)
4092 return 0;
4093 idx = ucontrol->value.enumerated.item[0];
4094 if (idx >= imux->num_items)
4095 idx = imux->num_items - 1;
4096 if (*cur_val == idx)
4097 return 0;
4098 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4099 imux->items[idx].index);
4100 *cur_val = idx;
4101 return 1;
4102}
4103EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4104
4105
4106
4107
4108
4109
4110
4111static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4112 unsigned int stream_tag, unsigned int format)
4113{
4114
4115 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4116 set_dig_out_convert(codec, nid,
4117 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4118 -1);
4119 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4120 if (codec->slave_dig_outs) {
4121 hda_nid_t *d;
4122 for (d = codec->slave_dig_outs; *d; d++)
4123 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4124 format);
4125 }
4126
4127 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4128 set_dig_out_convert(codec, nid,
4129 codec->spdif_ctls & 0xff, -1);
4130}
4131
4132static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4133{
4134 snd_hda_codec_cleanup_stream(codec, nid);
4135 if (codec->slave_dig_outs) {
4136 hda_nid_t *d;
4137 for (d = codec->slave_dig_outs; *d; d++)
4138 snd_hda_codec_cleanup_stream(codec, *d);
4139 }
4140}
4141
4142
4143
4144
4145
4146void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4147{
4148 struct hda_codec *codec;
4149
4150 if (!bus)
4151 return;
4152 list_for_each_entry(codec, &bus->codec_list, list) {
4153#ifdef CONFIG_SND_HDA_POWER_SAVE
4154 if (!codec->power_on)
4155 continue;
4156#endif
4157 if (codec->patch_ops.reboot_notify)
4158 codec->patch_ops.reboot_notify(codec);
4159 }
4160}
4161EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4162
4163
4164
4165
4166int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4167 struct hda_multi_out *mout)
4168{
4169 mutex_lock(&codec->spdif_mutex);
4170 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4171
4172 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4173 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4174 mutex_unlock(&codec->spdif_mutex);
4175 return 0;
4176}
4177EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4178
4179
4180
4181
4182int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4183 struct hda_multi_out *mout,
4184 unsigned int stream_tag,
4185 unsigned int format,
4186 struct snd_pcm_substream *substream)
4187{
4188 mutex_lock(&codec->spdif_mutex);
4189 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4190 mutex_unlock(&codec->spdif_mutex);
4191 return 0;
4192}
4193EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4194
4195
4196
4197
4198int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4199 struct hda_multi_out *mout)
4200{
4201 mutex_lock(&codec->spdif_mutex);
4202 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4203 mutex_unlock(&codec->spdif_mutex);
4204 return 0;
4205}
4206EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4207
4208
4209
4210
4211int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4212 struct hda_multi_out *mout)
4213{
4214 mutex_lock(&codec->spdif_mutex);
4215 mout->dig_out_used = 0;
4216 mutex_unlock(&codec->spdif_mutex);
4217 return 0;
4218}
4219EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4220
4221
4222
4223
4224
4225
4226
4227
4228int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4229 struct hda_multi_out *mout,
4230 struct snd_pcm_substream *substream,
4231 struct hda_pcm_stream *hinfo)
4232{
4233 struct snd_pcm_runtime *runtime = substream->runtime;
4234 runtime->hw.channels_max = mout->max_channels;
4235 if (mout->dig_out_nid) {
4236 if (!mout->analog_rates) {
4237 mout->analog_rates = hinfo->rates;
4238 mout->analog_formats = hinfo->formats;
4239 mout->analog_maxbps = hinfo->maxbps;
4240 } else {
4241 runtime->hw.rates = mout->analog_rates;
4242 runtime->hw.formats = mout->analog_formats;
4243 hinfo->maxbps = mout->analog_maxbps;
4244 }
4245 if (!mout->spdif_rates) {
4246 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4247 &mout->spdif_rates,
4248 &mout->spdif_formats,
4249 &mout->spdif_maxbps);
4250 }
4251 mutex_lock(&codec->spdif_mutex);
4252 if (mout->share_spdif) {
4253 if ((runtime->hw.rates & mout->spdif_rates) &&
4254 (runtime->hw.formats & mout->spdif_formats)) {
4255 runtime->hw.rates &= mout->spdif_rates;
4256 runtime->hw.formats &= mout->spdif_formats;
4257 if (mout->spdif_maxbps < hinfo->maxbps)
4258 hinfo->maxbps = mout->spdif_maxbps;
4259 } else {
4260 mout->share_spdif = 0;
4261
4262 }
4263 }
4264 mutex_unlock(&codec->spdif_mutex);
4265 }
4266 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4267 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4268}
4269EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4270
4271
4272
4273
4274
4275
4276
4277int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4278 struct hda_multi_out *mout,
4279 unsigned int stream_tag,
4280 unsigned int format,
4281 struct snd_pcm_substream *substream)
4282{
4283 hda_nid_t *nids = mout->dac_nids;
4284 int chs = substream->runtime->channels;
4285 int i;
4286
4287 mutex_lock(&codec->spdif_mutex);
4288 if (mout->dig_out_nid && mout->share_spdif &&
4289 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4290 if (chs == 2 &&
4291 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4292 format) &&
4293 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
4294 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4295 setup_dig_out_stream(codec, mout->dig_out_nid,
4296 stream_tag, format);
4297 } else {
4298 mout->dig_out_used = 0;
4299 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4300 }
4301 }
4302 mutex_unlock(&codec->spdif_mutex);
4303
4304
4305 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4306 0, format);
4307 if (!mout->no_share_stream &&
4308 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4309
4310 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4311 0, format);
4312
4313 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4314 if (!mout->no_share_stream && mout->extra_out_nid[i])
4315 snd_hda_codec_setup_stream(codec,
4316 mout->extra_out_nid[i],
4317 stream_tag, 0, format);
4318
4319
4320 for (i = 1; i < mout->num_dacs; i++) {
4321 if (chs >= (i + 1) * 2)
4322 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4323 i * 2, format);
4324 else if (!mout->no_share_stream)
4325 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4326 0, format);
4327 }
4328 return 0;
4329}
4330EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4331
4332
4333
4334
4335int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4336 struct hda_multi_out *mout)
4337{
4338 hda_nid_t *nids = mout->dac_nids;
4339 int i;
4340
4341 for (i = 0; i < mout->num_dacs; i++)
4342 snd_hda_codec_cleanup_stream(codec, nids[i]);
4343 if (mout->hp_nid)
4344 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4345 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4346 if (mout->extra_out_nid[i])
4347 snd_hda_codec_cleanup_stream(codec,
4348 mout->extra_out_nid[i]);
4349 mutex_lock(&codec->spdif_mutex);
4350 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4351 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4352 mout->dig_out_used = 0;
4353 }
4354 mutex_unlock(&codec->spdif_mutex);
4355 return 0;
4356}
4357EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4358
4359
4360
4361
4362
4363static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
4364{
4365 for (; *list; list++)
4366 if (*list == nid)
4367 return 1;
4368 return 0;
4369}
4370
4371
4372
4373
4374
4375static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4376 int num_pins)
4377{
4378 int i, j;
4379 short seq;
4380 hda_nid_t nid;
4381
4382 for (i = 0; i < num_pins; i++) {
4383 for (j = i + 1; j < num_pins; j++) {
4384 if (sequences[i] > sequences[j]) {
4385 seq = sequences[i];
4386 sequences[i] = sequences[j];
4387 sequences[j] = seq;
4388 nid = pins[i];
4389 pins[i] = pi