]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/hda/hdac_device.c
Merge tag 'for-linus-timers-conversion-final-v4.15-rc1' of git://git.kernel.org/pub...
[mirror_ubuntu-bionic-kernel.git] / sound / hda / hdac_device.c
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>
12 #include <sound/hda_regmap.h>
13 #include <sound/pcm.h>
14 #include "local.h"
15
16 static void setup_fg_nodes(struct hdac_device *codec);
17 static int get_codec_vendor_name(struct hdac_device *codec);
18
19 static 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 */
38 int 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;
50 dev->groups = hdac_dev_attr_groups;
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, false);
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 */
96 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
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:
114 put_device(&codec->dev);
115 return err;
116 }
117 EXPORT_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 */
123 void snd_hdac_device_exit(struct hdac_device *codec)
124 {
125 pm_runtime_put_noidle(&codec->dev);
126 snd_hdac_bus_remove_device(codec->bus, codec);
127 kfree(codec->vendor_name);
128 kfree(codec->chip_name);
129 }
130 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
131
132 /**
133 * snd_hdac_device_register - register the hd-audio codec base device
134 * codec: the device to register
135 */
136 int 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 }
151 EXPORT_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 */
157 void 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 snd_hdac_bus_remove_device(codec->bus, codec);
163 }
164 }
165 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
166
167 /**
168 * snd_hdac_device_set_chip_name - set/update the codec name
169 * @codec: the HDAC device
170 * @name: name string to set
171 *
172 * Returns 0 if the name is set or updated, or a negative error code.
173 */
174 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
175 {
176 char *newname;
177
178 if (!name)
179 return 0;
180 newname = kstrdup(name, GFP_KERNEL);
181 if (!newname)
182 return -ENOMEM;
183 kfree(codec->chip_name);
184 codec->chip_name = newname;
185 return 0;
186 }
187 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
188
189 /**
190 * snd_hdac_codec_modalias - give the module alias name
191 * @codec: HDAC device
192 * @buf: string buffer to store
193 * @size: string buffer size
194 *
195 * Returns the size of string, like snprintf(), or a negative error code.
196 */
197 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
198 {
199 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
200 codec->vendor_id, codec->revision_id, codec->type);
201 }
202 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
203
204 /**
205 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
206 * HD-audio controller
207 * @codec: the codec object
208 * @nid: NID to encode
209 * @verb: verb to encode
210 * @parm: parameter to encode
211 *
212 * Return an encoded command verb or -1 for error.
213 */
214 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
215 unsigned int verb, unsigned int parm)
216 {
217 u32 val, addr;
218
219 addr = codec->addr;
220 if ((addr & ~0xf) || (nid & ~0x7f) ||
221 (verb & ~0xfff) || (parm & ~0xffff)) {
222 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
223 addr, nid, verb, parm);
224 return -1;
225 }
226
227 val = addr << 28;
228 val |= (u32)nid << 20;
229 val |= verb << 8;
230 val |= parm;
231 return val;
232 }
233 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
234
235 /**
236 * snd_hdac_exec_verb - execute an encoded verb
237 * @codec: the codec object
238 * @cmd: encoded verb to execute
239 * @flags: optional flags, pass zero for default
240 * @res: the pointer to store the result, NULL if running async
241 *
242 * Returns zero if successful, or a negative error code.
243 *
244 * This calls the exec_verb op when set in hdac_codec. If not,
245 * call the default snd_hdac_bus_exec_verb().
246 */
247 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
248 unsigned int flags, unsigned int *res)
249 {
250 if (codec->exec_verb)
251 return codec->exec_verb(codec, cmd, flags, res);
252 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
253 }
254 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
255
256
257 /**
258 * snd_hdac_read - execute a verb
259 * @codec: the codec object
260 * @nid: NID to execute a verb
261 * @verb: verb to execute
262 * @parm: parameter for a verb
263 * @res: the pointer to store the result, NULL if running async
264 *
265 * Returns zero if successful, or a negative error code.
266 */
267 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
268 unsigned int verb, unsigned int parm, unsigned int *res)
269 {
270 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
271
272 return snd_hdac_exec_verb(codec, cmd, 0, res);
273 }
274 EXPORT_SYMBOL_GPL(snd_hdac_read);
275
276 /**
277 * _snd_hdac_read_parm - read a parmeter
278 *
279 * This function returns zero or an error unlike snd_hdac_read_parm().
280 */
281 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
282 unsigned int *res)
283 {
284 unsigned int cmd;
285
286 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
287 return snd_hdac_regmap_read_raw(codec, cmd, res);
288 }
289 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
290
291 /**
292 * snd_hdac_read_parm_uncached - read a codec parameter without caching
293 * @codec: the codec object
294 * @nid: NID to read a parameter
295 * @parm: parameter to read
296 *
297 * Returns -1 for error. If you need to distinguish the error more
298 * strictly, use snd_hdac_read() directly.
299 */
300 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
301 int parm)
302 {
303 unsigned int cmd, val;
304
305 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
306 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
307 return -1;
308 return val;
309 }
310 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
311
312 /**
313 * snd_hdac_override_parm - override read-only parameters
314 * @codec: the codec object
315 * @nid: NID for the parameter
316 * @parm: the parameter to change
317 * @val: the parameter value to overwrite
318 */
319 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
320 unsigned int parm, unsigned int val)
321 {
322 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
323 int err;
324
325 if (!codec->regmap)
326 return -EINVAL;
327
328 codec->caps_overwriting = true;
329 err = snd_hdac_regmap_write_raw(codec, verb, val);
330 codec->caps_overwriting = false;
331 return err;
332 }
333 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
334
335 /**
336 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
337 * @codec: the codec object
338 * @nid: NID to inspect
339 * @start_id: the pointer to store the starting NID
340 *
341 * Returns the number of subtree nodes or zero if not found.
342 * This function reads parameters always without caching.
343 */
344 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
345 hda_nid_t *start_id)
346 {
347 unsigned int parm;
348
349 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
350 if (parm == -1) {
351 *start_id = 0;
352 return 0;
353 }
354 *start_id = (parm >> 16) & 0x7fff;
355 return (int)(parm & 0x7fff);
356 }
357 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
358
359 /*
360 * look for an AFG and MFG nodes
361 */
362 static void setup_fg_nodes(struct hdac_device *codec)
363 {
364 int i, total_nodes, function_id;
365 hda_nid_t nid;
366
367 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
368 for (i = 0; i < total_nodes; i++, nid++) {
369 function_id = snd_hdac_read_parm(codec, nid,
370 AC_PAR_FUNCTION_TYPE);
371 switch (function_id & 0xff) {
372 case AC_GRP_AUDIO_FUNCTION:
373 codec->afg = nid;
374 codec->afg_function_id = function_id & 0xff;
375 codec->afg_unsol = (function_id >> 8) & 1;
376 break;
377 case AC_GRP_MODEM_FUNCTION:
378 codec->mfg = nid;
379 codec->mfg_function_id = function_id & 0xff;
380 codec->mfg_unsol = (function_id >> 8) & 1;
381 break;
382 default:
383 break;
384 }
385 }
386 }
387
388 /**
389 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
390 * @codec: the codec object
391 * @sysfs: re-initialize sysfs tree, too
392 */
393 int snd_hdac_refresh_widgets(struct hdac_device *codec, bool sysfs)
394 {
395 hda_nid_t start_nid;
396 int nums, err;
397
398 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
399 if (!start_nid || nums <= 0 || nums >= 0xff) {
400 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
401 codec->afg);
402 return -EINVAL;
403 }
404
405 if (sysfs) {
406 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
407 if (err < 0)
408 return err;
409 }
410
411 codec->num_nodes = nums;
412 codec->start_nid = start_nid;
413 codec->end_nid = start_nid + nums;
414 return 0;
415 }
416 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
417
418 /* return CONNLIST_LEN parameter of the given widget */
419 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
420 {
421 unsigned int wcaps = get_wcaps(codec, nid);
422 unsigned int parm;
423
424 if (!(wcaps & AC_WCAP_CONN_LIST) &&
425 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
426 return 0;
427
428 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
429 if (parm == -1)
430 parm = 0;
431 return parm;
432 }
433
434 /**
435 * snd_hdac_get_connections - get a widget connection list
436 * @codec: the codec object
437 * @nid: NID
438 * @conn_list: the array to store the results, can be NULL
439 * @max_conns: the max size of the given array
440 *
441 * Returns the number of connected widgets, zero for no connection, or a
442 * negative error code. When the number of elements don't fit with the
443 * given array size, it returns -ENOSPC.
444 *
445 * When @conn_list is NULL, it just checks the number of connections.
446 */
447 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
448 hda_nid_t *conn_list, int max_conns)
449 {
450 unsigned int parm;
451 int i, conn_len, conns, err;
452 unsigned int shift, num_elems, mask;
453 hda_nid_t prev_nid;
454 int null_count = 0;
455
456 parm = get_num_conns(codec, nid);
457 if (!parm)
458 return 0;
459
460 if (parm & AC_CLIST_LONG) {
461 /* long form */
462 shift = 16;
463 num_elems = 2;
464 } else {
465 /* short form */
466 shift = 8;
467 num_elems = 4;
468 }
469 conn_len = parm & AC_CLIST_LENGTH;
470 mask = (1 << (shift-1)) - 1;
471
472 if (!conn_len)
473 return 0; /* no connection */
474
475 if (conn_len == 1) {
476 /* single connection */
477 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
478 &parm);
479 if (err < 0)
480 return err;
481 if (conn_list)
482 conn_list[0] = parm & mask;
483 return 1;
484 }
485
486 /* multi connection */
487 conns = 0;
488 prev_nid = 0;
489 for (i = 0; i < conn_len; i++) {
490 int range_val;
491 hda_nid_t val, n;
492
493 if (i % num_elems == 0) {
494 err = snd_hdac_read(codec, nid,
495 AC_VERB_GET_CONNECT_LIST, i,
496 &parm);
497 if (err < 0)
498 return -EIO;
499 }
500 range_val = !!(parm & (1 << (shift-1))); /* ranges */
501 val = parm & mask;
502 if (val == 0 && null_count++) { /* no second chance */
503 dev_dbg(&codec->dev,
504 "invalid CONNECT_LIST verb %x[%i]:%x\n",
505 nid, i, parm);
506 return 0;
507 }
508 parm >>= shift;
509 if (range_val) {
510 /* ranges between the previous and this one */
511 if (!prev_nid || prev_nid >= val) {
512 dev_warn(&codec->dev,
513 "invalid dep_range_val %x:%x\n",
514 prev_nid, val);
515 continue;
516 }
517 for (n = prev_nid + 1; n <= val; n++) {
518 if (conn_list) {
519 if (conns >= max_conns)
520 return -ENOSPC;
521 conn_list[conns] = n;
522 }
523 conns++;
524 }
525 } else {
526 if (conn_list) {
527 if (conns >= max_conns)
528 return -ENOSPC;
529 conn_list[conns] = val;
530 }
531 conns++;
532 }
533 prev_nid = val;
534 }
535 return conns;
536 }
537 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
538
539 #ifdef CONFIG_PM
540 /**
541 * snd_hdac_power_up - power up the codec
542 * @codec: the codec object
543 *
544 * This function calls the runtime PM helper to power up the given codec.
545 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
546 * path that isn't included in PM path. Otherwise it gets stuck.
547 *
548 * Returns zero if successful, or a negative error code.
549 */
550 int snd_hdac_power_up(struct hdac_device *codec)
551 {
552 return pm_runtime_get_sync(&codec->dev);
553 }
554 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
555
556 /**
557 * snd_hdac_power_down - power down the codec
558 * @codec: the codec object
559 *
560 * Returns zero if successful, or a negative error code.
561 */
562 int snd_hdac_power_down(struct hdac_device *codec)
563 {
564 struct device *dev = &codec->dev;
565
566 pm_runtime_mark_last_busy(dev);
567 return pm_runtime_put_autosuspend(dev);
568 }
569 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
570
571 /**
572 * snd_hdac_power_up_pm - power up the codec
573 * @codec: the codec object
574 *
575 * This function can be called in a recursive code path like init code
576 * which may be called by PM suspend/resume again. OTOH, if a power-up
577 * call must wake up the sleeper (e.g. in a kctl callback), use
578 * snd_hdac_power_up() instead.
579 *
580 * Returns zero if successful, or a negative error code.
581 */
582 int snd_hdac_power_up_pm(struct hdac_device *codec)
583 {
584 if (!atomic_inc_not_zero(&codec->in_pm))
585 return snd_hdac_power_up(codec);
586 return 0;
587 }
588 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
589
590 /* like snd_hdac_power_up_pm(), but only increment the pm count when
591 * already powered up. Returns -1 if not powered up, 1 if incremented
592 * or 0 if unchanged. Only used in hdac_regmap.c
593 */
594 int snd_hdac_keep_power_up(struct hdac_device *codec)
595 {
596 if (!atomic_inc_not_zero(&codec->in_pm)) {
597 int ret = pm_runtime_get_if_in_use(&codec->dev);
598 if (!ret)
599 return -1;
600 if (ret < 0)
601 return 0;
602 }
603 return 1;
604 }
605
606 /**
607 * snd_hdac_power_down_pm - power down the codec
608 * @codec: the codec object
609 *
610 * Like snd_hdac_power_up_pm(), this function is used in a recursive
611 * code path like init code which may be called by PM suspend/resume again.
612 *
613 * Returns zero if successful, or a negative error code.
614 */
615 int snd_hdac_power_down_pm(struct hdac_device *codec)
616 {
617 if (atomic_dec_if_positive(&codec->in_pm) < 0)
618 return snd_hdac_power_down(codec);
619 return 0;
620 }
621 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
622 #endif
623
624 /**
625 * snd_hdac_link_power - Enable/disable the link power for a codec
626 * @codec: the codec object
627 * @bool: enable or disable the link power
628 */
629 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
630 {
631 if (!codec->link_power_control)
632 return 0;
633
634 if (codec->bus->ops->link_power)
635 return codec->bus->ops->link_power(codec->bus, enable);
636 else
637 return -EINVAL;
638 }
639 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
640
641 /* codec vendor labels */
642 struct hda_vendor_id {
643 unsigned int id;
644 const char *name;
645 };
646
647 static 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 {} /* terminator */
669 };
670
671 /* store the codec vendor name */
672 static 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 * stream formats
690 */
691 struct hda_rate_tbl {
692 unsigned int hz;
693 unsigned int alsa_bits;
694 unsigned int hda_fmt;
695 };
696
697 /* rate = base * mult / div */
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
702 static struct hda_rate_tbl rate_bits[] = {
703 /* rate in Hz, ALSA rate bitmask, HDA format value */
704
705 /* autodetected value used in snd_hda_query_supported_pcm */
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 /* up to bits 10, 384kHZ isn't supported properly */
719
720 /* not autodetected value */
721 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
722
723 { 0 } /* terminator */
724 };
725
726 /**
727 * snd_hdac_calc_stream_format - calculate the format bitset
728 * @rate: the sample rate
729 * @channels: the number of channels
730 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
731 * @maxbps: the max. bps
732 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
733 *
734 * Calculate the format bitset from the given rate, channels and th PCM format.
735 *
736 * Return zero if invalid.
737 */
738 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
739 unsigned int channels,
740 unsigned int 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 }
785 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
786
787 static 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
801 static 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 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
814 * @codec: the codec object
815 * @nid: NID to query
816 * @ratesp: the pointer to store the detected rate bitflags
817 * @formatsp: the pointer to store the detected formats
818 * @bpsp: the pointer to store the detected format widths
819 *
820 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
821 * or @bsps argument is ignored.
822 *
823 * Returns 0 if successful, otherwise a negative error code.
824 */
825 int 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 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
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 /* should be exclusive */
896 /* temporary hack: we have still no proper support
897 * for the direct AC3 stream...
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 }
918 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
919
920 /**
921 * snd_hdac_is_supported_format - Check the validity of the format
922 * @codec: the codec object
923 * @nid: NID to check
924 * @format: the HD-audio format value to check
925 *
926 * Check whether the given node supports the format value.
927 *
928 * Returns true if supported, false if not.
929 */
930 bool 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 /* FIXME: check for float32 and AC3? */
981 }
982
983 return true;
984 }
985 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
986
987 static 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
999 static 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 * snd_hdac_codec_read - send a command and get the response
1009 * @hdac: the HDAC device
1010 * @nid: NID to send the command
1011 * @flags: optional bit flags
1012 * @verb: the verb to send
1013 * @parm: the parameter for the verb
1014 *
1015 * Send a single command and read the corresponding response.
1016 *
1017 * Returns the obtained response value, or -1 for an error.
1018 */
1019 int 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 }
1024 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1025
1026 /**
1027 * snd_hdac_codec_write - send a single command without waiting for response
1028 * @hdac: the HDAC device
1029 * @nid: NID to send the command
1030 * @flags: optional bit flags
1031 * @verb: the verb to send
1032 * @parm: the parameter for the verb
1033 *
1034 * Send a single command without waiting for response.
1035 *
1036 * Returns 0 if successful, or a negative error code.
1037 */
1038 int 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 }
1043 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1044
1045 /**
1046 * snd_hdac_check_power_state - check whether the actual power state matches
1047 * with the target state
1048 *
1049 * @hdac: the HDAC device
1050 * @nid: NID to send the command
1051 * @target_state: target state to check for
1052 *
1053 * Return true if state matches, false if not
1054 */
1055 bool 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 }
1066 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);