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