1
2
3
4
5
6#include <linux/init.h>
7#include <linux/delay.h>
8#include <linux/device.h>
9#include <linux/slab.h>
10#include <linux/module.h>
11#include <linux/export.h>
12#include <linux/pm_runtime.h>
13#include <sound/hdaudio.h>
14#include <sound/hda_regmap.h>
15#include <sound/pcm.h>
16#include "local.h"
17
18static void setup_fg_nodes(struct hdac_device *codec);
19static int get_codec_vendor_name(struct hdac_device *codec);
20
21static void default_release(struct device *dev)
22{
23 snd_hdac_device_exit(dev_to_hdac_dev(dev));
24}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
41 const char *name, unsigned int addr)
42{
43 struct device *dev;
44 hda_nid_t fg;
45 int err;
46
47 dev = &codec->dev;
48 device_initialize(dev);
49 dev->parent = bus->dev;
50 dev->bus = &snd_hda_bus_type;
51 dev->release = default_release;
52 dev->groups = hdac_dev_attr_groups;
53 dev_set_name(dev, "%s", name);
54 device_enable_async_suspend(dev);
55
56 codec->bus = bus;
57 codec->addr = addr;
58 codec->type = HDA_DEV_CORE;
59 mutex_init(&codec->widget_lock);
60 mutex_init(&codec->regmap_lock);
61 pm_runtime_set_active(&codec->dev);
62 pm_runtime_get_noresume(&codec->dev);
63 atomic_set(&codec->in_pm, 0);
64
65 err = snd_hdac_bus_add_device(bus, codec);
66 if (err < 0)
67 goto error;
68
69
70 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
71 AC_PAR_VENDOR_ID);
72 if (codec->vendor_id == -1) {
73
74
75
76 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
77 AC_PAR_VENDOR_ID);
78 }
79
80 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
81 AC_PAR_SUBSYSTEM_ID);
82 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
83 AC_PAR_REV_ID);
84
85 setup_fg_nodes(codec);
86 if (!codec->afg && !codec->mfg) {
87 dev_err(dev, "no AFG or MFG node found\n");
88 err = -ENODEV;
89 goto error;
90 }
91
92 fg = codec->afg ? codec->afg : codec->mfg;
93
94 err = snd_hdac_refresh_widgets(codec);
95 if (err < 0)
96 goto error;
97
98 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
99
100 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
101 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
102 &codec->subsystem_id);
103
104 err = get_codec_vendor_name(codec);
105 if (err < 0)
106 goto error;
107
108 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
109 codec->vendor_id & 0xffff);
110 if (!codec->chip_name) {
111 err = -ENOMEM;
112 goto error;
113 }
114
115 return 0;
116
117 error:
118 put_device(&codec->dev);
119 return err;
120}
121EXPORT_SYMBOL_GPL(snd_hdac_device_init);
122
123
124
125
126
127void snd_hdac_device_exit(struct hdac_device *codec)
128{
129 pm_runtime_put_noidle(&codec->dev);
130
131 pm_runtime_set_suspended(&codec->dev);
132 snd_hdac_bus_remove_device(codec->bus, codec);
133 kfree(codec->vendor_name);
134 kfree(codec->chip_name);
135}
136EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
137
138
139
140
141
142int snd_hdac_device_register(struct hdac_device *codec)
143{
144 int err;
145
146 err = device_add(&codec->dev);
147 if (err < 0)
148 return err;
149 mutex_lock(&codec->widget_lock);
150 err = hda_widget_sysfs_init(codec);
151 mutex_unlock(&codec->widget_lock);
152 if (err < 0) {
153 device_del(&codec->dev);
154 return err;
155 }
156
157 return 0;
158}
159EXPORT_SYMBOL_GPL(snd_hdac_device_register);
160
161
162
163
164
165void snd_hdac_device_unregister(struct hdac_device *codec)
166{
167 if (device_is_registered(&codec->dev)) {
168 mutex_lock(&codec->widget_lock);
169 hda_widget_sysfs_exit(codec);
170 mutex_unlock(&codec->widget_lock);
171 device_del(&codec->dev);
172 snd_hdac_bus_remove_device(codec->bus, codec);
173 }
174}
175EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
176
177
178
179
180
181
182
183
184int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
185{
186 char *newname;
187
188 if (!name)
189 return 0;
190 newname = kstrdup(name, GFP_KERNEL);
191 if (!newname)
192 return -ENOMEM;
193 kfree(codec->chip_name);
194 codec->chip_name = newname;
195 return 0;
196}
197EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
198
199
200
201
202
203
204
205
206
207int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
208{
209 return scnprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
210 codec->vendor_id, codec->revision_id, codec->type);
211}
212EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
213
214
215
216
217
218
219
220
221
222
223
224static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
225 unsigned int verb, unsigned int parm)
226{
227 u32 val, addr;
228
229 addr = codec->addr;
230 if ((addr & ~0xf) || (nid & ~0x7f) ||
231 (verb & ~0xfff) || (parm & ~0xffff)) {
232 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
233 addr, nid, verb, parm);
234 return -1;
235 }
236
237 val = addr << 28;
238 val |= (u32)nid << 20;
239 val |= verb << 8;
240 val |= parm;
241 return val;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
257 unsigned int flags, unsigned int *res)
258{
259 if (codec->exec_verb)
260 return codec->exec_verb(codec, cmd, flags, res);
261 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
262}
263
264
265
266
267
268
269
270
271
272
273
274
275int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
276 unsigned int verb, unsigned int parm, unsigned int *res)
277{
278 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
279
280 return snd_hdac_exec_verb(codec, cmd, 0, res);
281}
282EXPORT_SYMBOL_GPL(snd_hdac_read);
283
284
285
286
287
288
289
290
291
292
293int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
294 unsigned int *res)
295{
296 unsigned int cmd;
297
298 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
299 return snd_hdac_regmap_read_raw(codec, cmd, res);
300}
301EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
302
303
304
305
306
307
308
309
310
311
312int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
313 int parm)
314{
315 unsigned int cmd, val;
316
317 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
318 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
319 return -1;
320 return val;
321}
322EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
323
324
325
326
327
328
329
330
331int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
332 unsigned int parm, unsigned int val)
333{
334 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
335 int err;
336
337 if (!codec->regmap)
338 return -EINVAL;
339
340 codec->caps_overwriting = true;
341 err = snd_hdac_regmap_write_raw(codec, verb, val);
342 codec->caps_overwriting = false;
343 return err;
344}
345EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
346
347
348
349
350
351
352
353
354
355
356int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
357 hda_nid_t *start_id)
358{
359 unsigned int parm;
360
361 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
362 if (parm == -1) {
363 *start_id = 0;
364 return 0;
365 }
366 *start_id = (parm >> 16) & 0x7fff;
367 return (int)(parm & 0x7fff);
368}
369EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
370
371
372
373
374static void setup_fg_nodes(struct hdac_device *codec)
375{
376 int i, total_nodes, function_id;
377 hda_nid_t nid;
378
379 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
380 for (i = 0; i < total_nodes; i++, nid++) {
381 function_id = snd_hdac_read_parm(codec, nid,
382 AC_PAR_FUNCTION_TYPE);
383 switch (function_id & 0xff) {
384 case AC_GRP_AUDIO_FUNCTION:
385 codec->afg = nid;
386 codec->afg_function_id = function_id & 0xff;
387 codec->afg_unsol = (function_id >> 8) & 1;
388 break;
389 case AC_GRP_MODEM_FUNCTION:
390 codec->mfg = nid;
391 codec->mfg_function_id = function_id & 0xff;
392 codec->mfg_unsol = (function_id >> 8) & 1;
393 break;
394 default:
395 break;
396 }
397 }
398}
399
400
401
402
403
404int snd_hdac_refresh_widgets(struct hdac_device *codec)
405{
406 hda_nid_t start_nid;
407 int nums, err = 0;
408
409
410
411
412
413 mutex_lock(&codec->widget_lock);
414 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
415 if (!start_nid || nums <= 0 || nums >= 0xff) {
416 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
417 codec->afg);
418 err = -EINVAL;
419 goto unlock;
420 }
421
422 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
423 if (err < 0)
424 goto unlock;
425
426 codec->num_nodes = nums;
427 codec->start_nid = start_nid;
428 codec->end_nid = start_nid + nums;
429unlock:
430 mutex_unlock(&codec->widget_lock);
431 return err;
432}
433EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
434
435
436static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
437{
438 unsigned int wcaps = get_wcaps(codec, nid);
439 unsigned int parm;
440
441 if (!(wcaps & AC_WCAP_CONN_LIST) &&
442 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
443 return 0;
444
445 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
446 if (parm == -1)
447 parm = 0;
448 return parm;
449}
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
465 hda_nid_t *conn_list, int max_conns)
466{
467 unsigned int parm;
468 int i, conn_len, conns, err;
469 unsigned int shift, num_elems, mask;
470 hda_nid_t prev_nid;
471 int null_count = 0;
472
473 parm = get_num_conns(codec, nid);
474 if (!parm)
475 return 0;
476
477 if (parm & AC_CLIST_LONG) {
478
479 shift = 16;
480 num_elems = 2;
481 } else {
482
483 shift = 8;
484 num_elems = 4;
485 }
486 conn_len = parm & AC_CLIST_LENGTH;
487 mask = (1 << (shift-1)) - 1;
488
489 if (!conn_len)
490 return 0;
491
492 if (conn_len == 1) {
493
494 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
495 &parm);
496 if (err < 0)
497 return err;
498 if (conn_list)
499 conn_list[0] = parm & mask;
500 return 1;
501 }
502
503
504 conns = 0;
505 prev_nid = 0;
506 for (i = 0; i < conn_len; i++) {
507 int range_val;
508 hda_nid_t val, n;
509
510 if (i % num_elems == 0) {
511 err = snd_hdac_read(codec, nid,
512 AC_VERB_GET_CONNECT_LIST, i,
513 &parm);
514 if (err < 0)
515 return -EIO;
516 }
517 range_val = !!(parm & (1 << (shift-1)));
518 val = parm & mask;
519 if (val == 0 && null_count++) {
520 dev_dbg(&codec->dev,
521 "invalid CONNECT_LIST verb %x[%i]:%x\n",
522 nid, i, parm);
523 return 0;
524 }
525 parm >>= shift;
526 if (range_val) {
527
528 if (!prev_nid || prev_nid >= val) {
529 dev_warn(&codec->dev,
530 "invalid dep_range_val %x:%x\n",
531 prev_nid, val);
532 continue;
533 }
534 for (n = prev_nid + 1; n <= val; n++) {
535 if (conn_list) {
536 if (conns >= max_conns)
537 return -ENOSPC;
538 conn_list[conns] = n;
539 }
540 conns++;
541 }
542 } else {
543 if (conn_list) {
544 if (conns >= max_conns)
545 return -ENOSPC;
546 conn_list[conns] = val;
547 }
548 conns++;
549 }
550 prev_nid = val;
551 }
552 return conns;
553}
554EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
555
556#ifdef CONFIG_PM
557
558
559
560
561
562
563
564
565
566
567int snd_hdac_power_up(struct hdac_device *codec)
568{
569 return pm_runtime_get_sync(&codec->dev);
570}
571EXPORT_SYMBOL_GPL(snd_hdac_power_up);
572
573
574
575
576
577
578
579int snd_hdac_power_down(struct hdac_device *codec)
580{
581 struct device *dev = &codec->dev;
582
583 pm_runtime_mark_last_busy(dev);
584 return pm_runtime_put_autosuspend(dev);
585}
586EXPORT_SYMBOL_GPL(snd_hdac_power_down);
587
588
589
590
591
592
593
594
595
596
597
598
599int snd_hdac_power_up_pm(struct hdac_device *codec)
600{
601 if (!atomic_inc_not_zero(&codec->in_pm))
602 return snd_hdac_power_up(codec);
603 return 0;
604}
605EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
606
607
608
609
610
611int snd_hdac_keep_power_up(struct hdac_device *codec)
612{
613 if (!atomic_inc_not_zero(&codec->in_pm)) {
614 int ret = pm_runtime_get_if_in_use(&codec->dev);
615 if (!ret)
616 return -1;
617 if (ret < 0)
618 return 0;
619 }
620 return 1;
621}
622
623
624
625
626
627
628
629
630
631
632int snd_hdac_power_down_pm(struct hdac_device *codec)
633{
634 if (atomic_dec_if_positive(&codec->in_pm) < 0)
635 return snd_hdac_power_down(codec);
636 return 0;
637}
638EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
639#endif
640
641
642struct hda_vendor_id {
643 unsigned int id;
644 const char *name;
645};
646
647static const struct hda_vendor_id hda_vendor_ids[] = {
648 { 0x1002, "ATI" },
649 { 0x1013, "Cirrus Logic" },
650 { 0x1057, "Motorola" },
651 { 0x1095, "Silicon Image" },
652 { 0x10de, "Nvidia" },
653 { 0x10ec, "Realtek" },
654 { 0x1102, "Creative" },
655 { 0x1106, "VIA" },
656 { 0x111d, "IDT" },
657 { 0x11c1, "LSI" },
658 { 0x11d4, "Analog Devices" },
659 { 0x13f6, "C-Media" },
660 { 0x14f1, "Conexant" },
661 { 0x17e8, "Chrontel" },
662 { 0x1854, "LG" },
663 { 0x1aec, "Wolfson Microelectronics" },
664 { 0x1af4, "QEMU" },
665 { 0x434d, "C-Media" },
666 { 0x8086, "Intel" },
667 { 0x8384, "SigmaTel" },
668 {}
669};
670
671
672static int get_codec_vendor_name(struct hdac_device *codec)
673{
674 const struct hda_vendor_id *c;
675 u16 vendor_id = codec->vendor_id >> 16;
676
677 for (c = hda_vendor_ids; c->id; c++) {
678 if (c->id == vendor_id) {
679 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
680 return codec->vendor_name ? 0 : -ENOMEM;
681 }
682 }
683
684 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
685 return codec->vendor_name ? 0 : -ENOMEM;
686}
687
688
689
690
691struct hda_rate_tbl {
692 unsigned int hz;
693 unsigned int alsa_bits;
694 unsigned int hda_fmt;
695};
696
697
698#define HDA_RATE(base, mult, div) \
699 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
700 (((div) - 1) << AC_FMT_DIV_SHIFT))
701
702static const struct hda_rate_tbl rate_bits[] = {
703
704
705
706 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
707 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
708 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
709 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
710 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
711 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
712 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
713 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
714 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
715 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
716 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
717#define AC_PAR_PCM_RATE_BITS 11
718
719
720
721 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
722
723 { 0 }
724};
725
726
727
728
729
730
731
732
733
734
735
736
737
738unsigned int snd_hdac_calc_stream_format(unsigned int rate,
739 unsigned int channels,
740 snd_pcm_format_t format,
741 unsigned int maxbps,
742 unsigned short spdif_ctls)
743{
744 int i;
745 unsigned int val = 0;
746
747 for (i = 0; rate_bits[i].hz; i++)
748 if (rate_bits[i].hz == rate) {
749 val = rate_bits[i].hda_fmt;
750 break;
751 }
752 if (!rate_bits[i].hz)
753 return 0;
754
755 if (channels == 0 || channels > 8)
756 return 0;
757 val |= channels - 1;
758
759 switch (snd_pcm_format_width(format)) {
760 case 8:
761 val |= AC_FMT_BITS_8;
762 break;
763 case 16:
764 val |= AC_FMT_BITS_16;
765 break;
766 case 20:
767 case 24:
768 case 32:
769 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
770 val |= AC_FMT_BITS_32;
771 else if (maxbps >= 24)
772 val |= AC_FMT_BITS_24;
773 else
774 val |= AC_FMT_BITS_20;
775 break;
776 default:
777 return 0;
778 }
779
780 if (spdif_ctls & AC_DIG1_NONAUDIO)
781 val |= AC_FMT_TYPE_NON_PCM;
782
783 return val;
784}
785EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
786
787static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
788{
789 unsigned int val = 0;
790
791 if (nid != codec->afg &&
792 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
793 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
794 if (!val || val == -1)
795 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
796 if (!val || val == -1)
797 return 0;
798 return val;
799}
800
801static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
802{
803 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
804
805 if (!streams || streams == -1)
806 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
807 if (!streams || streams == -1)
808 return 0;
809 return streams;
810}
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
826 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
827{
828 unsigned int i, val, wcaps;
829
830 wcaps = get_wcaps(codec, nid);
831 val = query_pcm_param(codec, nid);
832
833 if (ratesp) {
834 u32 rates = 0;
835 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
836 if (val & (1 << i))
837 rates |= rate_bits[i].alsa_bits;
838 }
839 if (rates == 0) {
840 dev_err(&codec->dev,
841 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
842 nid, val,
843 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
844 return -EIO;
845 }
846 *ratesp = rates;
847 }
848
849 if (formatsp || bpsp) {
850 u64 formats = 0;
851 unsigned int streams, bps;
852
853 streams = query_stream_param(codec, nid);
854 if (!streams)
855 return -EIO;
856
857 bps = 0;
858 if (streams & AC_SUPFMT_PCM) {
859 if (val & AC_SUPPCM_BITS_8) {
860 formats |= SNDRV_PCM_FMTBIT_U8;
861 bps = 8;
862 }
863 if (val & AC_SUPPCM_BITS_16) {
864 formats |= SNDRV_PCM_FMTBIT_S16_LE;
865 bps = 16;
866 }
867 if (wcaps & AC_WCAP_DIGITAL) {
868 if (val & AC_SUPPCM_BITS_32)
869 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
870 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
871 formats |= SNDRV_PCM_FMTBIT_S32_LE;
872 if (val & AC_SUPPCM_BITS_24)
873 bps = 24;
874 else if (val & AC_SUPPCM_BITS_20)
875 bps = 20;
876 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
877 AC_SUPPCM_BITS_32)) {
878 formats |= SNDRV_PCM_FMTBIT_S32_LE;
879 if (val & AC_SUPPCM_BITS_32)
880 bps = 32;
881 else if (val & AC_SUPPCM_BITS_24)
882 bps = 24;
883 else if (val & AC_SUPPCM_BITS_20)
884 bps = 20;
885 }
886 }
887#if 0
888 if (streams & AC_SUPFMT_FLOAT32) {
889 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
890 if (!bps)
891 bps = 32;
892 }
893#endif
894 if (streams == AC_SUPFMT_AC3) {
895
896
897
898
899 formats |= SNDRV_PCM_FMTBIT_U8;
900 bps = 8;
901 }
902 if (formats == 0) {
903 dev_err(&codec->dev,
904 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
905 nid, val,
906 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
907 streams);
908 return -EIO;
909 }
910 if (formatsp)
911 *formatsp = formats;
912 if (bpsp)
913 *bpsp = bps;
914 }
915
916 return 0;
917}
918EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
919
920
921
922
923
924
925
926
927
928
929
930bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
931 unsigned int format)
932{
933 int i;
934 unsigned int val = 0, rate, stream;
935
936 val = query_pcm_param(codec, nid);
937 if (!val)
938 return false;
939
940 rate = format & 0xff00;
941 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
942 if (rate_bits[i].hda_fmt == rate) {
943 if (val & (1 << i))
944 break;
945 return false;
946 }
947 if (i >= AC_PAR_PCM_RATE_BITS)
948 return false;
949
950 stream = query_stream_param(codec, nid);
951 if (!stream)
952 return false;
953
954 if (stream & AC_SUPFMT_PCM) {
955 switch (format & 0xf0) {
956 case 0x00:
957 if (!(val & AC_SUPPCM_BITS_8))
958 return false;
959 break;
960 case 0x10:
961 if (!(val & AC_SUPPCM_BITS_16))
962 return false;
963 break;
964 case 0x20:
965 if (!(val & AC_SUPPCM_BITS_20))
966 return false;
967 break;
968 case 0x30:
969 if (!(val & AC_SUPPCM_BITS_24))
970 return false;
971 break;
972 case 0x40:
973 if (!(val & AC_SUPPCM_BITS_32))
974 return false;
975 break;
976 default:
977 return false;
978 }
979 } else {
980
981 }
982
983 return true;
984}
985EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
986
987static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
988 int flags, unsigned int verb, unsigned int parm)
989{
990 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
991 unsigned int res;
992
993 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
994 return -1;
995
996 return res;
997}
998
999static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1000 int flags, unsigned int verb, unsigned int parm)
1001{
1002 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1003
1004 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1005}
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1020 int flags, unsigned int verb, unsigned int parm)
1021{
1022 return codec_read(hdac, nid, flags, verb, parm);
1023}
1024EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1039 int flags, unsigned int verb, unsigned int parm)
1040{
1041 return codec_write(hdac, nid, flags, verb, parm);
1042}
1043EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055bool snd_hdac_check_power_state(struct hdac_device *hdac,
1056 hda_nid_t nid, unsigned int target_state)
1057{
1058 unsigned int state = codec_read(hdac, nid, 0,
1059 AC_VERB_GET_POWER_STATE, 0);
1060
1061 if (state & AC_PWRST_ERROR)
1062 return true;
1063 state = (state >> 4) & 0x0f;
1064 return (state == target_state);
1065}
1066EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1078 hda_nid_t nid, unsigned int power_state)
1079{
1080 unsigned long end_time = jiffies + msecs_to_jiffies(500);
1081 unsigned int state, actual_state, count;
1082
1083 for (count = 0; count < 500; count++) {
1084 state = snd_hdac_codec_read(codec, nid, 0,
1085 AC_VERB_GET_POWER_STATE, 0);
1086 if (state & AC_PWRST_ERROR) {
1087 msleep(20);
1088 break;
1089 }
1090 actual_state = (state >> 4) & 0x0f;
1091 if (actual_state == power_state)
1092 break;
1093 if (time_after_eq(jiffies, end_time))
1094 break;
1095
1096 msleep(1);
1097 }
1098 return state;
1099}
1100EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);
1101