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