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