ALSA: ppc: Add missing inclusion of linux/module.h
[deliverable/linux.git] / sound / hda / hdac_device.c
CommitLineData
7639a06c
TI
1/*
2 * HD-audio codec core device
3 */
4
5#include <linux/init.h>
6#include <linux/device.h>
7#include <linux/slab.h>
8#include <linux/module.h>
9#include <linux/export.h>
10#include <linux/pm_runtime.h>
11#include <sound/hdaudio.h>
01ed3c06 12#include <sound/hda_regmap.h>
b7d023e1 13#include <sound/pcm.h>
7639a06c
TI
14#include "local.h"
15
16static void setup_fg_nodes(struct hdac_device *codec);
17static int get_codec_vendor_name(struct hdac_device *codec);
18
19static void default_release(struct device *dev)
20{
21 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
22}
23
24/**
25 * snd_hdac_device_init - initialize the HD-audio codec base device
26 * @codec: device to initialize
27 * @bus: but to attach
28 * @name: device name string
29 * @addr: codec address
30 *
31 * Returns zero for success or a negative error code.
32 *
33 * This function increments the runtime PM counter and marks it active.
34 * The caller needs to turn it off appropriately later.
35 *
36 * The caller needs to set the device's release op properly by itself.
37 */
38int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
39 const char *name, unsigned int addr)
40{
41 struct device *dev;
42 hda_nid_t fg;
43 int err;
44
45 dev = &codec->dev;
46 device_initialize(dev);
47 dev->parent = bus->dev;
48 dev->bus = &snd_hda_bus_type;
49 dev->release = default_release;
3256be65 50 dev->groups = hdac_dev_attr_groups;
7639a06c
TI
51 dev_set_name(dev, "%s", name);
52 device_enable_async_suspend(dev);
53
54 codec->bus = bus;
55 codec->addr = addr;
56 codec->type = HDA_DEV_CORE;
57 pm_runtime_set_active(&codec->dev);
58 pm_runtime_get_noresume(&codec->dev);
59 atomic_set(&codec->in_pm, 0);
60
61 err = snd_hdac_bus_add_device(bus, codec);
62 if (err < 0)
63 goto error;
64
65 /* fill parameters */
66 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
67 AC_PAR_VENDOR_ID);
68 if (codec->vendor_id == -1) {
69 /* read again, hopefully the access method was corrected
70 * in the last read...
71 */
72 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
73 AC_PAR_VENDOR_ID);
74 }
75
76 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
77 AC_PAR_SUBSYSTEM_ID);
78 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 AC_PAR_REV_ID);
80
81 setup_fg_nodes(codec);
82 if (!codec->afg && !codec->mfg) {
83 dev_err(dev, "no AFG or MFG node found\n");
84 err = -ENODEV;
85 goto error;
86 }
87
88 fg = codec->afg ? codec->afg : codec->mfg;
89
90 err = snd_hdac_refresh_widgets(codec);
91 if (err < 0)
92 goto error;
93
94 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
95 /* reread ssid if not set by parameter */
ffda568e 96 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
7639a06c
TI
97 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
98 &codec->subsystem_id);
99
100 err = get_codec_vendor_name(codec);
101 if (err < 0)
102 goto error;
103
104 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
105 codec->vendor_id & 0xffff);
106 if (!codec->chip_name) {
107 err = -ENOMEM;
108 goto error;
109 }
110
111 return 0;
112
113 error:
7639a06c
TI
114 put_device(&codec->dev);
115 return err;
116}
117EXPORT_SYMBOL_GPL(snd_hdac_device_init);
118
119/**
120 * snd_hdac_device_exit - clean up the HD-audio codec base device
121 * @codec: device to clean up
122 */
123void snd_hdac_device_exit(struct hdac_device *codec)
124{
c4c2533f 125 pm_runtime_put_noidle(&codec->dev);
7639a06c
TI
126 snd_hdac_bus_remove_device(codec->bus, codec);
127 kfree(codec->vendor_name);
128 kfree(codec->chip_name);
129}
130EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
131
3256be65
TI
132/**
133 * snd_hdac_device_register - register the hd-audio codec base device
134 * codec: the device to register
135 */
136int snd_hdac_device_register(struct hdac_device *codec)
137{
138 int err;
139
140 err = device_add(&codec->dev);
141 if (err < 0)
142 return err;
143 err = hda_widget_sysfs_init(codec);
144 if (err < 0) {
145 device_del(&codec->dev);
146 return err;
147 }
148
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_hdac_device_register);
152
153/**
154 * snd_hdac_device_unregister - unregister the hd-audio codec base device
155 * codec: the device to unregister
156 */
157void snd_hdac_device_unregister(struct hdac_device *codec)
158{
159 if (device_is_registered(&codec->dev)) {
160 hda_widget_sysfs_exit(codec);
161 device_del(&codec->dev);
162 }
163}
164EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
165
7639a06c
TI
166/**
167 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
168 * HD-audio controller
169 * @codec: the codec object
170 * @nid: NID to encode
171 * @verb: verb to encode
172 * @parm: parameter to encode
173 *
174 * Return an encoded command verb or -1 for error.
175 */
176unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
177 unsigned int verb, unsigned int parm)
178{
179 u32 val, addr;
180
181 addr = codec->addr;
182 if ((addr & ~0xf) || (nid & ~0x7f) ||
183 (verb & ~0xfff) || (parm & ~0xffff)) {
184 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
185 addr, nid, verb, parm);
186 return -1;
187 }
188
189 val = addr << 28;
190 val |= (u32)nid << 20;
191 val |= verb << 8;
192 val |= parm;
193 return val;
194}
195EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
196
05852448
TI
197/**
198 * snd_hdac_exec_verb - execute an encoded verb
199 * @codec: the codec object
200 * @cmd: encoded verb to execute
201 * @flags: optional flags, pass zero for default
202 * @res: the pointer to store the result, NULL if running async
203 *
204 * Returns zero if successful, or a negative error code.
205 *
206 * This calls the exec_verb op when set in hdac_codec. If not,
207 * call the default snd_hdac_bus_exec_verb().
208 */
209int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
210 unsigned int flags, unsigned int *res)
211{
212 if (codec->exec_verb)
213 return codec->exec_verb(codec, cmd, flags, res);
214 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
215}
216EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
217
218
7639a06c
TI
219/**
220 * snd_hdac_read - execute a verb
221 * @codec: the codec object
222 * @nid: NID to execute a verb
223 * @verb: verb to execute
224 * @parm: parameter for a verb
225 * @res: the pointer to store the result, NULL if running async
226 *
227 * Returns zero if successful, or a negative error code.
228 */
229int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
230 unsigned int verb, unsigned int parm, unsigned int *res)
231{
232 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
233
05852448 234 return snd_hdac_exec_verb(codec, cmd, 0, res);
7639a06c
TI
235}
236EXPORT_SYMBOL_GPL(snd_hdac_read);
237
238/**
01ed3c06 239 * _snd_hdac_read_parm - read a parmeter
7639a06c 240 *
01ed3c06 241 * This function returns zero or an error unlike snd_hdac_read_parm().
7639a06c 242 */
01ed3c06
TI
243int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
244 unsigned int *res)
7639a06c 245{
01ed3c06 246 unsigned int cmd;
7639a06c 247
01ed3c06
TI
248 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
249 return snd_hdac_regmap_read_raw(codec, cmd, res);
7639a06c 250}
01ed3c06 251EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
7639a06c 252
9ba17b4d
TI
253/**
254 * snd_hdac_read_parm_uncached - read a codec parameter without caching
7639a06c
TI
255 * @codec: the codec object
256 * @nid: NID to read a parameter
257 * @parm: parameter to read
258 *
259 * Returns -1 for error. If you need to distinguish the error more
260 * strictly, use snd_hdac_read() directly.
261 */
9ba17b4d
TI
262int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
263 int parm)
7639a06c
TI
264{
265 int val;
266
9ba17b4d
TI
267 if (codec->regmap)
268 regcache_cache_bypass(codec->regmap, true);
269 val = snd_hdac_read_parm(codec, nid, parm);
270 if (codec->regmap)
271 regcache_cache_bypass(codec->regmap, false);
7639a06c
TI
272 return val;
273}
9ba17b4d
TI
274EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
275
faa75f8a
TI
276/**
277 * snd_hdac_override_parm - override read-only parameters
278 * @codec: the codec object
279 * @nid: NID for the parameter
280 * @parm: the parameter to change
281 * @val: the parameter value to overwrite
282 */
283int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
284 unsigned int parm, unsigned int val)
285{
286 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
287 int err;
288
289 if (!codec->regmap)
290 return -EINVAL;
291
292 codec->caps_overwriting = true;
293 err = snd_hdac_regmap_write_raw(codec, verb, val);
294 codec->caps_overwriting = false;
295 return err;
296}
297EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
7639a06c
TI
298
299/**
300 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
301 * @codec: the codec object
302 * @nid: NID to inspect
303 * @start_id: the pointer to store the starting NID
304 *
305 * Returns the number of subtree nodes or zero if not found.
9ba17b4d 306 * This function reads parameters always without caching.
7639a06c
TI
307 */
308int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
309 hda_nid_t *start_id)
310{
311 unsigned int parm;
312
9ba17b4d 313 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
7639a06c
TI
314 if (parm == -1) {
315 *start_id = 0;
316 return 0;
317 }
318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
320}
321EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
322
323/*
324 * look for an AFG and MFG nodes
325 */
326static void setup_fg_nodes(struct hdac_device *codec)
327{
328 int i, total_nodes, function_id;
329 hda_nid_t nid;
330
331 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
332 for (i = 0; i < total_nodes; i++, nid++) {
333 function_id = snd_hdac_read_parm(codec, nid,
334 AC_PAR_FUNCTION_TYPE);
335 switch (function_id & 0xff) {
336 case AC_GRP_AUDIO_FUNCTION:
337 codec->afg = nid;
338 codec->afg_function_id = function_id & 0xff;
339 codec->afg_unsol = (function_id >> 8) & 1;
340 break;
341 case AC_GRP_MODEM_FUNCTION:
342 codec->mfg = nid;
343 codec->mfg_function_id = function_id & 0xff;
344 codec->mfg_unsol = (function_id >> 8) & 1;
345 break;
346 default:
347 break;
348 }
349 }
350}
351
352/**
353 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
354 * @codec: the codec object
355 */
356int snd_hdac_refresh_widgets(struct hdac_device *codec)
357{
358 hda_nid_t start_nid;
359 int nums;
360
361 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
362 if (!start_nid || nums <= 0 || nums >= 0xff) {
363 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
364 codec->afg);
365 return -EINVAL;
366 }
367
368 codec->num_nodes = nums;
369 codec->start_nid = start_nid;
370 codec->end_nid = start_nid + nums;
371 return 0;
372}
373EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
374
18dfd79d
VK
375/**
376 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
377 * codec sysfs
378 * @codec: the codec object
379 *
380 * first we need to remove sysfs, then refresh widgets and lastly
381 * recreate it
382 */
383int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
384{
385 int ret;
386
387 hda_widget_sysfs_exit(codec);
388 ret = snd_hdac_refresh_widgets(codec);
389 if (ret) {
390 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
391 return ret;
392 }
393 ret = hda_widget_sysfs_init(codec);
394 if (ret) {
395 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
396 return ret;
397 }
398
399 return ret;
400}
401EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
402
7639a06c
TI
403/* return CONNLIST_LEN parameter of the given widget */
404static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
405{
406 unsigned int wcaps = get_wcaps(codec, nid);
407 unsigned int parm;
408
409 if (!(wcaps & AC_WCAP_CONN_LIST) &&
410 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
411 return 0;
412
413 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
414 if (parm == -1)
415 parm = 0;
416 return parm;
417}
418
419/**
420 * snd_hdac_get_connections - get a widget connection list
421 * @codec: the codec object
422 * @nid: NID
423 * @conn_list: the array to store the results, can be NULL
424 * @max_conns: the max size of the given array
425 *
426 * Returns the number of connected widgets, zero for no connection, or a
427 * negative error code. When the number of elements don't fit with the
428 * given array size, it returns -ENOSPC.
429 *
430 * When @conn_list is NULL, it just checks the number of connections.
431 */
432int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
433 hda_nid_t *conn_list, int max_conns)
434{
435 unsigned int parm;
436 int i, conn_len, conns, err;
437 unsigned int shift, num_elems, mask;
438 hda_nid_t prev_nid;
439 int null_count = 0;
440
441 parm = get_num_conns(codec, nid);
442 if (!parm)
443 return 0;
444
445 if (parm & AC_CLIST_LONG) {
446 /* long form */
447 shift = 16;
448 num_elems = 2;
449 } else {
450 /* short form */
451 shift = 8;
452 num_elems = 4;
453 }
454 conn_len = parm & AC_CLIST_LENGTH;
455 mask = (1 << (shift-1)) - 1;
456
457 if (!conn_len)
458 return 0; /* no connection */
459
460 if (conn_len == 1) {
461 /* single connection */
462 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
463 &parm);
464 if (err < 0)
465 return err;
466 if (conn_list)
467 conn_list[0] = parm & mask;
468 return 1;
469 }
470
471 /* multi connection */
472 conns = 0;
473 prev_nid = 0;
474 for (i = 0; i < conn_len; i++) {
475 int range_val;
476 hda_nid_t val, n;
477
478 if (i % num_elems == 0) {
479 err = snd_hdac_read(codec, nid,
480 AC_VERB_GET_CONNECT_LIST, i,
481 &parm);
482 if (err < 0)
483 return -EIO;
484 }
485 range_val = !!(parm & (1 << (shift-1))); /* ranges */
486 val = parm & mask;
487 if (val == 0 && null_count++) { /* no second chance */
488 dev_dbg(&codec->dev,
489 "invalid CONNECT_LIST verb %x[%i]:%x\n",
490 nid, i, parm);
491 return 0;
492 }
493 parm >>= shift;
494 if (range_val) {
495 /* ranges between the previous and this one */
496 if (!prev_nid || prev_nid >= val) {
497 dev_warn(&codec->dev,
498 "invalid dep_range_val %x:%x\n",
499 prev_nid, val);
500 continue;
501 }
502 for (n = prev_nid + 1; n <= val; n++) {
503 if (conn_list) {
504 if (conns >= max_conns)
505 return -ENOSPC;
506 conn_list[conns] = n;
507 }
508 conns++;
509 }
510 } else {
511 if (conn_list) {
512 if (conns >= max_conns)
513 return -ENOSPC;
514 conn_list[conns] = val;
515 }
516 conns++;
517 }
518 prev_nid = val;
519 }
520 return conns;
521}
522EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
523
524#ifdef CONFIG_PM
525/**
664c7155 526 * snd_hdac_power_up - power up the codec
7639a06c 527 * @codec: the codec object
664c7155
TI
528 *
529 * This function calls the runtime PM helper to power up the given codec.
530 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
531 * path that isn't included in PM path. Otherwise it gets stuck.
fbce23a0
TI
532 *
533 * Returns zero if successful, or a negative error code.
7639a06c 534 */
fbce23a0 535int snd_hdac_power_up(struct hdac_device *codec)
7639a06c 536{
fbce23a0 537 return pm_runtime_get_sync(&codec->dev);
7639a06c
TI
538}
539EXPORT_SYMBOL_GPL(snd_hdac_power_up);
540
541/**
664c7155 542 * snd_hdac_power_down - power down the codec
7639a06c 543 * @codec: the codec object
fbce23a0
TI
544 *
545 * Returns zero if successful, or a negative error code.
7639a06c 546 */
fbce23a0 547int snd_hdac_power_down(struct hdac_device *codec)
7639a06c
TI
548{
549 struct device *dev = &codec->dev;
550
7639a06c 551 pm_runtime_mark_last_busy(dev);
fbce23a0 552 return pm_runtime_put_autosuspend(dev);
7639a06c
TI
553}
554EXPORT_SYMBOL_GPL(snd_hdac_power_down);
c3aeda62
TI
555
556/**
557 * snd_hdac_power_up_pm - power up the codec
558 * @codec: the codec object
559 *
560 * This function can be called in a recursive code path like init code
561 * which may be called by PM suspend/resume again. OTOH, if a power-up
562 * call must wake up the sleeper (e.g. in a kctl callback), use
563 * snd_hdac_power_up() instead.
fbce23a0
TI
564 *
565 * Returns zero if successful, or a negative error code.
c3aeda62 566 */
fbce23a0 567int snd_hdac_power_up_pm(struct hdac_device *codec)
c3aeda62
TI
568{
569 if (!atomic_inc_not_zero(&codec->in_pm))
fbce23a0
TI
570 return snd_hdac_power_up(codec);
571 return 0;
c3aeda62
TI
572}
573EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
574
575/**
576 * snd_hdac_power_down_pm - power down the codec
577 * @codec: the codec object
578 *
579 * Like snd_hdac_power_up_pm(), this function is used in a recursive
580 * code path like init code which may be called by PM suspend/resume again.
fbce23a0
TI
581 *
582 * Returns zero if successful, or a negative error code.
c3aeda62 583 */
fbce23a0 584int snd_hdac_power_down_pm(struct hdac_device *codec)
c3aeda62
TI
585{
586 if (atomic_dec_if_positive(&codec->in_pm) < 0)
fbce23a0
TI
587 return snd_hdac_power_down(codec);
588 return 0;
c3aeda62
TI
589}
590EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
7639a06c
TI
591#endif
592
a5e7e07c
ML
593/*
594 * Enable/disable the link power for a codec.
595 */
596int snd_hdac_link_power(struct hdac_device *codec, bool enable)
597{
598 if (!codec->link_power_control)
599 return 0;
600
601 if (codec->bus->ops->link_power)
602 return codec->bus->ops->link_power(codec->bus, enable);
603 else
604 return -EINVAL;
605}
606EXPORT_SYMBOL_GPL(snd_hdac_link_power);
607
7639a06c
TI
608/* codec vendor labels */
609struct hda_vendor_id {
610 unsigned int id;
611 const char *name;
612};
613
614static struct hda_vendor_id hda_vendor_ids[] = {
615 { 0x1002, "ATI" },
616 { 0x1013, "Cirrus Logic" },
617 { 0x1057, "Motorola" },
618 { 0x1095, "Silicon Image" },
619 { 0x10de, "Nvidia" },
620 { 0x10ec, "Realtek" },
621 { 0x1102, "Creative" },
622 { 0x1106, "VIA" },
623 { 0x111d, "IDT" },
624 { 0x11c1, "LSI" },
625 { 0x11d4, "Analog Devices" },
626 { 0x13f6, "C-Media" },
627 { 0x14f1, "Conexant" },
628 { 0x17e8, "Chrontel" },
629 { 0x1854, "LG" },
630 { 0x1aec, "Wolfson Microelectronics" },
631 { 0x1af4, "QEMU" },
632 { 0x434d, "C-Media" },
633 { 0x8086, "Intel" },
634 { 0x8384, "SigmaTel" },
635 {} /* terminator */
636};
637
638/* store the codec vendor name */
639static int get_codec_vendor_name(struct hdac_device *codec)
640{
641 const struct hda_vendor_id *c;
642 u16 vendor_id = codec->vendor_id >> 16;
643
644 for (c = hda_vendor_ids; c->id; c++) {
645 if (c->id == vendor_id) {
646 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
647 return codec->vendor_name ? 0 : -ENOMEM;
648 }
649 }
650
651 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
652 return codec->vendor_name ? 0 : -ENOMEM;
653}
b7d023e1
TI
654
655/*
656 * stream formats
657 */
658struct hda_rate_tbl {
659 unsigned int hz;
660 unsigned int alsa_bits;
661 unsigned int hda_fmt;
662};
663
664/* rate = base * mult / div */
665#define HDA_RATE(base, mult, div) \
666 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
667 (((div) - 1) << AC_FMT_DIV_SHIFT))
668
669static struct hda_rate_tbl rate_bits[] = {
670 /* rate in Hz, ALSA rate bitmask, HDA format value */
671
672 /* autodetected value used in snd_hda_query_supported_pcm */
673 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
674 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
675 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
676 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
677 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
678 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
679 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
680 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
681 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
682 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
683 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
684#define AC_PAR_PCM_RATE_BITS 11
685 /* up to bits 10, 384kHZ isn't supported properly */
686
687 /* not autodetected value */
688 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
689
690 { 0 } /* terminator */
691};
692
693/**
694 * snd_hdac_calc_stream_format - calculate the format bitset
695 * @rate: the sample rate
696 * @channels: the number of channels
697 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
698 * @maxbps: the max. bps
699 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
700 *
701 * Calculate the format bitset from the given rate, channels and th PCM format.
702 *
703 * Return zero if invalid.
704 */
705unsigned int snd_hdac_calc_stream_format(unsigned int rate,
706 unsigned int channels,
707 unsigned int format,
708 unsigned int maxbps,
709 unsigned short spdif_ctls)
710{
711 int i;
712 unsigned int val = 0;
713
714 for (i = 0; rate_bits[i].hz; i++)
715 if (rate_bits[i].hz == rate) {
716 val = rate_bits[i].hda_fmt;
717 break;
718 }
719 if (!rate_bits[i].hz)
720 return 0;
721
722 if (channels == 0 || channels > 8)
723 return 0;
724 val |= channels - 1;
725
726 switch (snd_pcm_format_width(format)) {
727 case 8:
728 val |= AC_FMT_BITS_8;
729 break;
730 case 16:
731 val |= AC_FMT_BITS_16;
732 break;
733 case 20:
734 case 24:
735 case 32:
736 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
737 val |= AC_FMT_BITS_32;
738 else if (maxbps >= 24)
739 val |= AC_FMT_BITS_24;
740 else
741 val |= AC_FMT_BITS_20;
742 break;
743 default:
744 return 0;
745 }
746
747 if (spdif_ctls & AC_DIG1_NONAUDIO)
748 val |= AC_FMT_TYPE_NON_PCM;
749
750 return val;
751}
752EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
753
754static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
755{
756 unsigned int val = 0;
757
758 if (nid != codec->afg &&
759 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
760 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
761 if (!val || val == -1)
762 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
763 if (!val || val == -1)
764 return 0;
765 return val;
766}
767
768static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
769{
770 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
771
772 if (!streams || streams == -1)
773 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
774 if (!streams || streams == -1)
775 return 0;
776 return streams;
777}
778
779/**
780 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
781 * @codec: the codec object
782 * @nid: NID to query
783 * @ratesp: the pointer to store the detected rate bitflags
784 * @formatsp: the pointer to store the detected formats
785 * @bpsp: the pointer to store the detected format widths
786 *
787 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
788 * or @bsps argument is ignored.
789 *
790 * Returns 0 if successful, otherwise a negative error code.
791 */
792int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
793 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
794{
795 unsigned int i, val, wcaps;
796
797 wcaps = get_wcaps(codec, nid);
798 val = query_pcm_param(codec, nid);
799
800 if (ratesp) {
801 u32 rates = 0;
802 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
803 if (val & (1 << i))
804 rates |= rate_bits[i].alsa_bits;
805 }
806 if (rates == 0) {
807 dev_err(&codec->dev,
808 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
809 nid, val,
810 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
811 return -EIO;
812 }
813 *ratesp = rates;
814 }
815
816 if (formatsp || bpsp) {
817 u64 formats = 0;
818 unsigned int streams, bps;
819
820 streams = query_stream_param(codec, nid);
821 if (!streams)
822 return -EIO;
823
824 bps = 0;
825 if (streams & AC_SUPFMT_PCM) {
826 if (val & AC_SUPPCM_BITS_8) {
827 formats |= SNDRV_PCM_FMTBIT_U8;
828 bps = 8;
829 }
830 if (val & AC_SUPPCM_BITS_16) {
831 formats |= SNDRV_PCM_FMTBIT_S16_LE;
832 bps = 16;
833 }
834 if (wcaps & AC_WCAP_DIGITAL) {
835 if (val & AC_SUPPCM_BITS_32)
836 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
837 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
838 formats |= SNDRV_PCM_FMTBIT_S32_LE;
839 if (val & AC_SUPPCM_BITS_24)
840 bps = 24;
841 else if (val & AC_SUPPCM_BITS_20)
842 bps = 20;
843 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
844 AC_SUPPCM_BITS_32)) {
845 formats |= SNDRV_PCM_FMTBIT_S32_LE;
846 if (val & AC_SUPPCM_BITS_32)
847 bps = 32;
848 else if (val & AC_SUPPCM_BITS_24)
849 bps = 24;
850 else if (val & AC_SUPPCM_BITS_20)
851 bps = 20;
852 }
853 }
854#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
855 if (streams & AC_SUPFMT_FLOAT32) {
856 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
857 if (!bps)
858 bps = 32;
859 }
860#endif
861 if (streams == AC_SUPFMT_AC3) {
862 /* should be exclusive */
863 /* temporary hack: we have still no proper support
864 * for the direct AC3 stream...
865 */
866 formats |= SNDRV_PCM_FMTBIT_U8;
867 bps = 8;
868 }
869 if (formats == 0) {
870 dev_err(&codec->dev,
871 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
872 nid, val,
873 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
874 streams);
875 return -EIO;
876 }
877 if (formatsp)
878 *formatsp = formats;
879 if (bpsp)
880 *bpsp = bps;
881 }
882
883 return 0;
884}
885EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
886
887/**
888 * snd_hdac_is_supported_format - Check the validity of the format
889 * @codec: the codec object
890 * @nid: NID to check
891 * @format: the HD-audio format value to check
892 *
893 * Check whether the given node supports the format value.
894 *
895 * Returns true if supported, false if not.
896 */
897bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
898 unsigned int format)
899{
900 int i;
901 unsigned int val = 0, rate, stream;
902
903 val = query_pcm_param(codec, nid);
904 if (!val)
905 return false;
906
907 rate = format & 0xff00;
908 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
909 if (rate_bits[i].hda_fmt == rate) {
910 if (val & (1 << i))
911 break;
912 return false;
913 }
914 if (i >= AC_PAR_PCM_RATE_BITS)
915 return false;
916
917 stream = query_stream_param(codec, nid);
918 if (!stream)
919 return false;
920
921 if (stream & AC_SUPFMT_PCM) {
922 switch (format & 0xf0) {
923 case 0x00:
924 if (!(val & AC_SUPPCM_BITS_8))
925 return false;
926 break;
927 case 0x10:
928 if (!(val & AC_SUPPCM_BITS_16))
929 return false;
930 break;
931 case 0x20:
932 if (!(val & AC_SUPPCM_BITS_20))
933 return false;
934 break;
935 case 0x30:
936 if (!(val & AC_SUPPCM_BITS_24))
937 return false;
938 break;
939 case 0x40:
940 if (!(val & AC_SUPPCM_BITS_32))
941 return false;
942 break;
943 default:
944 return false;
945 }
946 } else {
947 /* FIXME: check for float32 and AC3? */
948 }
949
950 return true;
951}
952EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
This page took 0.072319 seconds and 5 git commands to generate.