blob: 55c7d086b9dd0582a45e216b64699d992939e4f1 [file] [log] [blame]
Takashi Iwai7639a062015-03-03 10:07:24 +01001/*
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>
Takashi Iwai01ed3c02015-02-26 13:57:47 +010012#include <sound/hda_regmap.h>
Takashi Iwaib7d023e2015-04-16 08:19:06 +020013#include <sound/pcm.h>
Takashi Iwai7639a062015-03-03 10:07:24 +010014#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;
Takashi Iwai3256be62015-02-24 14:59:42 +010050 dev->groups = hdac_dev_attr_groups;
Takashi Iwai7639a062015-03-03 10:07:24 +010051 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 */
David Henningssonffda5682015-04-01 12:43:00 +020096 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
Takashi Iwai7639a062015-03-03 10:07:24 +010097 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:
Takashi Iwai7639a062015-03-03 10:07:24 +0100114 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{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100125 pm_runtime_put_noidle(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100126 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
132/**
Takashi Iwai3256be62015-02-24 14:59:42 +0100133 * 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
166/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100167 * 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
197/**
Takashi Iwai05852442015-03-03 15:40:08 +0100198 * 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
219/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100220 * 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
Takashi Iwai05852442015-03-03 15:40:08 +0100234 return snd_hdac_exec_verb(codec, cmd, 0, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100235}
236EXPORT_SYMBOL_GPL(snd_hdac_read);
237
238/**
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100239 * _snd_hdac_read_parm - read a parmeter
Takashi Iwai7639a062015-03-03 10:07:24 +0100240 *
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100241 * This function returns zero or an error unlike snd_hdac_read_parm().
Takashi Iwai7639a062015-03-03 10:07:24 +0100242 */
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100243int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
244 unsigned int *res)
Takashi Iwai7639a062015-03-03 10:07:24 +0100245{
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100246 unsigned int cmd;
Takashi Iwai7639a062015-03-03 10:07:24 +0100247
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100248 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
249 return snd_hdac_regmap_read_raw(codec, cmd, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100250}
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100251EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100252
253/**
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100254 * snd_hdac_read_parm_uncached - read a codec parameter without caching
Takashi Iwai7639a062015-03-03 10:07:24 +0100255 * @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 */
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100262int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
263 int parm)
Takashi Iwai7639a062015-03-03 10:07:24 +0100264{
265 int val;
266
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100267 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);
Takashi Iwai7639a062015-03-03 10:07:24 +0100272 return val;
273}
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100274EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
275
276/**
Takashi Iwaifaa75f82015-02-26 08:54:56 +0100277 * 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);
Takashi Iwai7639a062015-03-03 10:07:24 +0100298
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.
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100306 * This function reads parameters always without caching.
Takashi Iwai7639a062015-03-03 10:07:24 +0100307 */
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
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100313 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
Takashi Iwai7639a062015-03-03 10:07:24 +0100314 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
375/* return CONNLIST_LEN parameter of the given widget */
376static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
377{
378 unsigned int wcaps = get_wcaps(codec, nid);
379 unsigned int parm;
380
381 if (!(wcaps & AC_WCAP_CONN_LIST) &&
382 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
383 return 0;
384
385 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
386 if (parm == -1)
387 parm = 0;
388 return parm;
389}
390
391/**
392 * snd_hdac_get_connections - get a widget connection list
393 * @codec: the codec object
394 * @nid: NID
395 * @conn_list: the array to store the results, can be NULL
396 * @max_conns: the max size of the given array
397 *
398 * Returns the number of connected widgets, zero for no connection, or a
399 * negative error code. When the number of elements don't fit with the
400 * given array size, it returns -ENOSPC.
401 *
402 * When @conn_list is NULL, it just checks the number of connections.
403 */
404int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
405 hda_nid_t *conn_list, int max_conns)
406{
407 unsigned int parm;
408 int i, conn_len, conns, err;
409 unsigned int shift, num_elems, mask;
410 hda_nid_t prev_nid;
411 int null_count = 0;
412
413 parm = get_num_conns(codec, nid);
414 if (!parm)
415 return 0;
416
417 if (parm & AC_CLIST_LONG) {
418 /* long form */
419 shift = 16;
420 num_elems = 2;
421 } else {
422 /* short form */
423 shift = 8;
424 num_elems = 4;
425 }
426 conn_len = parm & AC_CLIST_LENGTH;
427 mask = (1 << (shift-1)) - 1;
428
429 if (!conn_len)
430 return 0; /* no connection */
431
432 if (conn_len == 1) {
433 /* single connection */
434 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
435 &parm);
436 if (err < 0)
437 return err;
438 if (conn_list)
439 conn_list[0] = parm & mask;
440 return 1;
441 }
442
443 /* multi connection */
444 conns = 0;
445 prev_nid = 0;
446 for (i = 0; i < conn_len; i++) {
447 int range_val;
448 hda_nid_t val, n;
449
450 if (i % num_elems == 0) {
451 err = snd_hdac_read(codec, nid,
452 AC_VERB_GET_CONNECT_LIST, i,
453 &parm);
454 if (err < 0)
455 return -EIO;
456 }
457 range_val = !!(parm & (1 << (shift-1))); /* ranges */
458 val = parm & mask;
459 if (val == 0 && null_count++) { /* no second chance */
460 dev_dbg(&codec->dev,
461 "invalid CONNECT_LIST verb %x[%i]:%x\n",
462 nid, i, parm);
463 return 0;
464 }
465 parm >>= shift;
466 if (range_val) {
467 /* ranges between the previous and this one */
468 if (!prev_nid || prev_nid >= val) {
469 dev_warn(&codec->dev,
470 "invalid dep_range_val %x:%x\n",
471 prev_nid, val);
472 continue;
473 }
474 for (n = prev_nid + 1; n <= val; n++) {
475 if (conn_list) {
476 if (conns >= max_conns)
477 return -ENOSPC;
478 conn_list[conns] = n;
479 }
480 conns++;
481 }
482 } else {
483 if (conn_list) {
484 if (conns >= max_conns)
485 return -ENOSPC;
486 conn_list[conns] = val;
487 }
488 conns++;
489 }
490 prev_nid = val;
491 }
492 return conns;
493}
494EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
495
496#ifdef CONFIG_PM
497/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200498 * snd_hdac_power_up - power up the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100499 * @codec: the codec object
Takashi Iwai664c7152015-04-08 11:43:14 +0200500 *
501 * This function calls the runtime PM helper to power up the given codec.
502 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
503 * path that isn't included in PM path. Otherwise it gets stuck.
Takashi Iwai7639a062015-03-03 10:07:24 +0100504 */
505void snd_hdac_power_up(struct hdac_device *codec)
506{
Takashi Iwai664c7152015-04-08 11:43:14 +0200507 pm_runtime_get_sync(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100508}
509EXPORT_SYMBOL_GPL(snd_hdac_power_up);
510
511/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200512 * snd_hdac_power_down - power down the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100513 * @codec: the codec object
514 */
515void snd_hdac_power_down(struct hdac_device *codec)
516{
517 struct device *dev = &codec->dev;
518
Takashi Iwai7639a062015-03-03 10:07:24 +0100519 pm_runtime_mark_last_busy(dev);
520 pm_runtime_put_autosuspend(dev);
521}
522EXPORT_SYMBOL_GPL(snd_hdac_power_down);
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200523
524/**
525 * snd_hdac_power_up_pm - power up the codec
526 * @codec: the codec object
527 *
528 * This function can be called in a recursive code path like init code
529 * which may be called by PM suspend/resume again. OTOH, if a power-up
530 * call must wake up the sleeper (e.g. in a kctl callback), use
531 * snd_hdac_power_up() instead.
532 */
533void snd_hdac_power_up_pm(struct hdac_device *codec)
534{
535 if (!atomic_inc_not_zero(&codec->in_pm))
536 snd_hdac_power_up(codec);
537}
538EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
539
540/**
541 * snd_hdac_power_down_pm - power down the codec
542 * @codec: the codec object
543 *
544 * Like snd_hdac_power_up_pm(), this function is used in a recursive
545 * code path like init code which may be called by PM suspend/resume again.
546 */
547void snd_hdac_power_down_pm(struct hdac_device *codec)
548{
549 if (atomic_dec_if_positive(&codec->in_pm) < 0)
550 snd_hdac_power_down(codec);
551}
552EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100553#endif
554
555/* codec vendor labels */
556struct hda_vendor_id {
557 unsigned int id;
558 const char *name;
559};
560
561static struct hda_vendor_id hda_vendor_ids[] = {
562 { 0x1002, "ATI" },
563 { 0x1013, "Cirrus Logic" },
564 { 0x1057, "Motorola" },
565 { 0x1095, "Silicon Image" },
566 { 0x10de, "Nvidia" },
567 { 0x10ec, "Realtek" },
568 { 0x1102, "Creative" },
569 { 0x1106, "VIA" },
570 { 0x111d, "IDT" },
571 { 0x11c1, "LSI" },
572 { 0x11d4, "Analog Devices" },
573 { 0x13f6, "C-Media" },
574 { 0x14f1, "Conexant" },
575 { 0x17e8, "Chrontel" },
576 { 0x1854, "LG" },
577 { 0x1aec, "Wolfson Microelectronics" },
578 { 0x1af4, "QEMU" },
579 { 0x434d, "C-Media" },
580 { 0x8086, "Intel" },
581 { 0x8384, "SigmaTel" },
582 {} /* terminator */
583};
584
585/* store the codec vendor name */
586static int get_codec_vendor_name(struct hdac_device *codec)
587{
588 const struct hda_vendor_id *c;
589 u16 vendor_id = codec->vendor_id >> 16;
590
591 for (c = hda_vendor_ids; c->id; c++) {
592 if (c->id == vendor_id) {
593 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
594 return codec->vendor_name ? 0 : -ENOMEM;
595 }
596 }
597
598 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
599 return codec->vendor_name ? 0 : -ENOMEM;
600}
Takashi Iwaib7d023e2015-04-16 08:19:06 +0200601
602/*
603 * stream formats
604 */
605struct hda_rate_tbl {
606 unsigned int hz;
607 unsigned int alsa_bits;
608 unsigned int hda_fmt;
609};
610
611/* rate = base * mult / div */
612#define HDA_RATE(base, mult, div) \
613 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
614 (((div) - 1) << AC_FMT_DIV_SHIFT))
615
616static struct hda_rate_tbl rate_bits[] = {
617 /* rate in Hz, ALSA rate bitmask, HDA format value */
618
619 /* autodetected value used in snd_hda_query_supported_pcm */
620 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
621 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
622 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
623 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
624 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
625 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
626 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
627 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
628 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
629 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
630 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
631#define AC_PAR_PCM_RATE_BITS 11
632 /* up to bits 10, 384kHZ isn't supported properly */
633
634 /* not autodetected value */
635 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
636
637 { 0 } /* terminator */
638};
639
640/**
641 * snd_hdac_calc_stream_format - calculate the format bitset
642 * @rate: the sample rate
643 * @channels: the number of channels
644 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
645 * @maxbps: the max. bps
646 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
647 *
648 * Calculate the format bitset from the given rate, channels and th PCM format.
649 *
650 * Return zero if invalid.
651 */
652unsigned int snd_hdac_calc_stream_format(unsigned int rate,
653 unsigned int channels,
654 unsigned int format,
655 unsigned int maxbps,
656 unsigned short spdif_ctls)
657{
658 int i;
659 unsigned int val = 0;
660
661 for (i = 0; rate_bits[i].hz; i++)
662 if (rate_bits[i].hz == rate) {
663 val = rate_bits[i].hda_fmt;
664 break;
665 }
666 if (!rate_bits[i].hz)
667 return 0;
668
669 if (channels == 0 || channels > 8)
670 return 0;
671 val |= channels - 1;
672
673 switch (snd_pcm_format_width(format)) {
674 case 8:
675 val |= AC_FMT_BITS_8;
676 break;
677 case 16:
678 val |= AC_FMT_BITS_16;
679 break;
680 case 20:
681 case 24:
682 case 32:
683 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
684 val |= AC_FMT_BITS_32;
685 else if (maxbps >= 24)
686 val |= AC_FMT_BITS_24;
687 else
688 val |= AC_FMT_BITS_20;
689 break;
690 default:
691 return 0;
692 }
693
694 if (spdif_ctls & AC_DIG1_NONAUDIO)
695 val |= AC_FMT_TYPE_NON_PCM;
696
697 return val;
698}
699EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
700
701static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
702{
703 unsigned int val = 0;
704
705 if (nid != codec->afg &&
706 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
707 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
708 if (!val || val == -1)
709 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
710 if (!val || val == -1)
711 return 0;
712 return val;
713}
714
715static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
716{
717 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
718
719 if (!streams || streams == -1)
720 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
721 if (!streams || streams == -1)
722 return 0;
723 return streams;
724}
725
726/**
727 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
728 * @codec: the codec object
729 * @nid: NID to query
730 * @ratesp: the pointer to store the detected rate bitflags
731 * @formatsp: the pointer to store the detected formats
732 * @bpsp: the pointer to store the detected format widths
733 *
734 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
735 * or @bsps argument is ignored.
736 *
737 * Returns 0 if successful, otherwise a negative error code.
738 */
739int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
740 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
741{
742 unsigned int i, val, wcaps;
743
744 wcaps = get_wcaps(codec, nid);
745 val = query_pcm_param(codec, nid);
746
747 if (ratesp) {
748 u32 rates = 0;
749 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
750 if (val & (1 << i))
751 rates |= rate_bits[i].alsa_bits;
752 }
753 if (rates == 0) {
754 dev_err(&codec->dev,
755 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
756 nid, val,
757 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
758 return -EIO;
759 }
760 *ratesp = rates;
761 }
762
763 if (formatsp || bpsp) {
764 u64 formats = 0;
765 unsigned int streams, bps;
766
767 streams = query_stream_param(codec, nid);
768 if (!streams)
769 return -EIO;
770
771 bps = 0;
772 if (streams & AC_SUPFMT_PCM) {
773 if (val & AC_SUPPCM_BITS_8) {
774 formats |= SNDRV_PCM_FMTBIT_U8;
775 bps = 8;
776 }
777 if (val & AC_SUPPCM_BITS_16) {
778 formats |= SNDRV_PCM_FMTBIT_S16_LE;
779 bps = 16;
780 }
781 if (wcaps & AC_WCAP_DIGITAL) {
782 if (val & AC_SUPPCM_BITS_32)
783 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
784 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
785 formats |= SNDRV_PCM_FMTBIT_S32_LE;
786 if (val & AC_SUPPCM_BITS_24)
787 bps = 24;
788 else if (val & AC_SUPPCM_BITS_20)
789 bps = 20;
790 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
791 AC_SUPPCM_BITS_32)) {
792 formats |= SNDRV_PCM_FMTBIT_S32_LE;
793 if (val & AC_SUPPCM_BITS_32)
794 bps = 32;
795 else if (val & AC_SUPPCM_BITS_24)
796 bps = 24;
797 else if (val & AC_SUPPCM_BITS_20)
798 bps = 20;
799 }
800 }
801#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
802 if (streams & AC_SUPFMT_FLOAT32) {
803 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
804 if (!bps)
805 bps = 32;
806 }
807#endif
808 if (streams == AC_SUPFMT_AC3) {
809 /* should be exclusive */
810 /* temporary hack: we have still no proper support
811 * for the direct AC3 stream...
812 */
813 formats |= SNDRV_PCM_FMTBIT_U8;
814 bps = 8;
815 }
816 if (formats == 0) {
817 dev_err(&codec->dev,
818 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
819 nid, val,
820 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
821 streams);
822 return -EIO;
823 }
824 if (formatsp)
825 *formatsp = formats;
826 if (bpsp)
827 *bpsp = bps;
828 }
829
830 return 0;
831}
832EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
833
834/**
835 * snd_hdac_is_supported_format - Check the validity of the format
836 * @codec: the codec object
837 * @nid: NID to check
838 * @format: the HD-audio format value to check
839 *
840 * Check whether the given node supports the format value.
841 *
842 * Returns true if supported, false if not.
843 */
844bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
845 unsigned int format)
846{
847 int i;
848 unsigned int val = 0, rate, stream;
849
850 val = query_pcm_param(codec, nid);
851 if (!val)
852 return false;
853
854 rate = format & 0xff00;
855 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
856 if (rate_bits[i].hda_fmt == rate) {
857 if (val & (1 << i))
858 break;
859 return false;
860 }
861 if (i >= AC_PAR_PCM_RATE_BITS)
862 return false;
863
864 stream = query_stream_param(codec, nid);
865 if (!stream)
866 return false;
867
868 if (stream & AC_SUPFMT_PCM) {
869 switch (format & 0xf0) {
870 case 0x00:
871 if (!(val & AC_SUPPCM_BITS_8))
872 return false;
873 break;
874 case 0x10:
875 if (!(val & AC_SUPPCM_BITS_16))
876 return false;
877 break;
878 case 0x20:
879 if (!(val & AC_SUPPCM_BITS_20))
880 return false;
881 break;
882 case 0x30:
883 if (!(val & AC_SUPPCM_BITS_24))
884 return false;
885 break;
886 case 0x40:
887 if (!(val & AC_SUPPCM_BITS_32))
888 return false;
889 break;
890 default:
891 return false;
892 }
893 } else {
894 /* FIXME: check for float32 and AC3? */
895 }
896
897 return true;
898}
899EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);