xref: /openbmc/linux/sound/hda/hdac_device.c (revision eb3fcf00)
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);
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 	}
163 }
164 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
165 
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  */
176 unsigned 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 }
195 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
196 
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  */
209 int 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 }
216 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
217 
218 
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  */
229 int 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 
234 	return snd_hdac_exec_verb(codec, cmd, 0, res);
235 }
236 EXPORT_SYMBOL_GPL(snd_hdac_read);
237 
238 /**
239  * _snd_hdac_read_parm - read a parmeter
240  *
241  * This function returns zero or an error unlike snd_hdac_read_parm().
242  */
243 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
244 			unsigned int *res)
245 {
246 	unsigned int cmd;
247 
248 	cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
249 	return snd_hdac_regmap_read_raw(codec, cmd, res);
250 }
251 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
252 
253 /**
254  * snd_hdac_read_parm_uncached - read a codec parameter without caching
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  */
262 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
263 				int parm)
264 {
265 	int val;
266 
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);
272 	return val;
273 }
274 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
275 
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  */
283 int 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 }
297 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
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.
306  * This function reads parameters always without caching.
307  */
308 int 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 
313 	parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
314 	if (parm == -1) {
315 		*start_id = 0;
316 		return 0;
317 	}
318 	*start_id = (parm >> 16) & 0x7fff;
319 	return (int)(parm & 0x7fff);
320 }
321 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
322 
323 /*
324  * look for an AFG and MFG nodes
325  */
326 static 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  */
356 int 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 }
373 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
374 
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  */
383 int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
384 {
385 	int ret;
386 
387 	if (device_is_registered(&codec->dev))
388 		hda_widget_sysfs_exit(codec);
389 	ret = snd_hdac_refresh_widgets(codec);
390 	if (ret) {
391 		dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
392 		return ret;
393 	}
394 	if (device_is_registered(&codec->dev)) {
395 		ret = hda_widget_sysfs_init(codec);
396 		if (ret) {
397 			dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
398 			return ret;
399 		}
400 	}
401 	return ret;
402 }
403 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
404 
405 /* return CONNLIST_LEN parameter of the given widget */
406 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
407 {
408 	unsigned int wcaps = get_wcaps(codec, nid);
409 	unsigned int parm;
410 
411 	if (!(wcaps & AC_WCAP_CONN_LIST) &&
412 	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
413 		return 0;
414 
415 	parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
416 	if (parm == -1)
417 		parm = 0;
418 	return parm;
419 }
420 
421 /**
422  * snd_hdac_get_connections - get a widget connection list
423  * @codec: the codec object
424  * @nid: NID
425  * @conn_list: the array to store the results, can be NULL
426  * @max_conns: the max size of the given array
427  *
428  * Returns the number of connected widgets, zero for no connection, or a
429  * negative error code.  When the number of elements don't fit with the
430  * given array size, it returns -ENOSPC.
431  *
432  * When @conn_list is NULL, it just checks the number of connections.
433  */
434 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
435 			     hda_nid_t *conn_list, int max_conns)
436 {
437 	unsigned int parm;
438 	int i, conn_len, conns, err;
439 	unsigned int shift, num_elems, mask;
440 	hda_nid_t prev_nid;
441 	int null_count = 0;
442 
443 	parm = get_num_conns(codec, nid);
444 	if (!parm)
445 		return 0;
446 
447 	if (parm & AC_CLIST_LONG) {
448 		/* long form */
449 		shift = 16;
450 		num_elems = 2;
451 	} else {
452 		/* short form */
453 		shift = 8;
454 		num_elems = 4;
455 	}
456 	conn_len = parm & AC_CLIST_LENGTH;
457 	mask = (1 << (shift-1)) - 1;
458 
459 	if (!conn_len)
460 		return 0; /* no connection */
461 
462 	if (conn_len == 1) {
463 		/* single connection */
464 		err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
465 				    &parm);
466 		if (err < 0)
467 			return err;
468 		if (conn_list)
469 			conn_list[0] = parm & mask;
470 		return 1;
471 	}
472 
473 	/* multi connection */
474 	conns = 0;
475 	prev_nid = 0;
476 	for (i = 0; i < conn_len; i++) {
477 		int range_val;
478 		hda_nid_t val, n;
479 
480 		if (i % num_elems == 0) {
481 			err = snd_hdac_read(codec, nid,
482 					    AC_VERB_GET_CONNECT_LIST, i,
483 					    &parm);
484 			if (err < 0)
485 				return -EIO;
486 		}
487 		range_val = !!(parm & (1 << (shift-1))); /* ranges */
488 		val = parm & mask;
489 		if (val == 0 && null_count++) {  /* no second chance */
490 			dev_dbg(&codec->dev,
491 				"invalid CONNECT_LIST verb %x[%i]:%x\n",
492 				nid, i, parm);
493 			return 0;
494 		}
495 		parm >>= shift;
496 		if (range_val) {
497 			/* ranges between the previous and this one */
498 			if (!prev_nid || prev_nid >= val) {
499 				dev_warn(&codec->dev,
500 					 "invalid dep_range_val %x:%x\n",
501 					 prev_nid, val);
502 				continue;
503 			}
504 			for (n = prev_nid + 1; n <= val; n++) {
505 				if (conn_list) {
506 					if (conns >= max_conns)
507 						return -ENOSPC;
508 					conn_list[conns] = n;
509 				}
510 				conns++;
511 			}
512 		} else {
513 			if (conn_list) {
514 				if (conns >= max_conns)
515 					return -ENOSPC;
516 				conn_list[conns] = val;
517 			}
518 			conns++;
519 		}
520 		prev_nid = val;
521 	}
522 	return conns;
523 }
524 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
525 
526 #ifdef CONFIG_PM
527 /**
528  * snd_hdac_power_up - power up the codec
529  * @codec: the codec object
530  *
531  * This function calls the runtime PM helper to power up the given codec.
532  * Unlike snd_hdac_power_up_pm(), you should call this only for the code
533  * path that isn't included in PM path.  Otherwise it gets stuck.
534  *
535  * Returns zero if successful, or a negative error code.
536  */
537 int snd_hdac_power_up(struct hdac_device *codec)
538 {
539 	return pm_runtime_get_sync(&codec->dev);
540 }
541 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
542 
543 /**
544  * snd_hdac_power_down - power down the codec
545  * @codec: the codec object
546  *
547  * Returns zero if successful, or a negative error code.
548  */
549 int snd_hdac_power_down(struct hdac_device *codec)
550 {
551 	struct device *dev = &codec->dev;
552 
553 	pm_runtime_mark_last_busy(dev);
554 	return pm_runtime_put_autosuspend(dev);
555 }
556 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
557 
558 /**
559  * snd_hdac_power_up_pm - power up the codec
560  * @codec: the codec object
561  *
562  * This function can be called in a recursive code path like init code
563  * which may be called by PM suspend/resume again.  OTOH, if a power-up
564  * call must wake up the sleeper (e.g. in a kctl callback), use
565  * snd_hdac_power_up() instead.
566  *
567  * Returns zero if successful, or a negative error code.
568  */
569 int snd_hdac_power_up_pm(struct hdac_device *codec)
570 {
571 	if (!atomic_inc_not_zero(&codec->in_pm))
572 		return snd_hdac_power_up(codec);
573 	return 0;
574 }
575 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
576 
577 /**
578  * snd_hdac_power_down_pm - power down the codec
579  * @codec: the codec object
580  *
581  * Like snd_hdac_power_up_pm(), this function is used in a recursive
582  * code path like init code which may be called by PM suspend/resume again.
583  *
584  * Returns zero if successful, or a negative error code.
585  */
586 int snd_hdac_power_down_pm(struct hdac_device *codec)
587 {
588 	if (atomic_dec_if_positive(&codec->in_pm) < 0)
589 		return snd_hdac_power_down(codec);
590 	return 0;
591 }
592 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
593 #endif
594 
595 /*
596  * Enable/disable the link power for a codec.
597  */
598 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
599 {
600 	if  (!codec->link_power_control)
601 		return 0;
602 
603 	if  (codec->bus->ops->link_power)
604 		return codec->bus->ops->link_power(codec->bus, enable);
605 	else
606 		return -EINVAL;
607 }
608 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
609 
610 /* codec vendor labels */
611 struct hda_vendor_id {
612 	unsigned int id;
613 	const char *name;
614 };
615 
616 static struct hda_vendor_id hda_vendor_ids[] = {
617 	{ 0x1002, "ATI" },
618 	{ 0x1013, "Cirrus Logic" },
619 	{ 0x1057, "Motorola" },
620 	{ 0x1095, "Silicon Image" },
621 	{ 0x10de, "Nvidia" },
622 	{ 0x10ec, "Realtek" },
623 	{ 0x1102, "Creative" },
624 	{ 0x1106, "VIA" },
625 	{ 0x111d, "IDT" },
626 	{ 0x11c1, "LSI" },
627 	{ 0x11d4, "Analog Devices" },
628 	{ 0x13f6, "C-Media" },
629 	{ 0x14f1, "Conexant" },
630 	{ 0x17e8, "Chrontel" },
631 	{ 0x1854, "LG" },
632 	{ 0x1aec, "Wolfson Microelectronics" },
633 	{ 0x1af4, "QEMU" },
634 	{ 0x434d, "C-Media" },
635 	{ 0x8086, "Intel" },
636 	{ 0x8384, "SigmaTel" },
637 	{} /* terminator */
638 };
639 
640 /* store the codec vendor name */
641 static int get_codec_vendor_name(struct hdac_device *codec)
642 {
643 	const struct hda_vendor_id *c;
644 	u16 vendor_id = codec->vendor_id >> 16;
645 
646 	for (c = hda_vendor_ids; c->id; c++) {
647 		if (c->id == vendor_id) {
648 			codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
649 			return codec->vendor_name ? 0 : -ENOMEM;
650 		}
651 	}
652 
653 	codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
654 	return codec->vendor_name ? 0 : -ENOMEM;
655 }
656 
657 /*
658  * stream formats
659  */
660 struct hda_rate_tbl {
661 	unsigned int hz;
662 	unsigned int alsa_bits;
663 	unsigned int hda_fmt;
664 };
665 
666 /* rate = base * mult / div */
667 #define HDA_RATE(base, mult, div) \
668 	(AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
669 	 (((div) - 1) << AC_FMT_DIV_SHIFT))
670 
671 static struct hda_rate_tbl rate_bits[] = {
672 	/* rate in Hz, ALSA rate bitmask, HDA format value */
673 
674 	/* autodetected value used in snd_hda_query_supported_pcm */
675 	{ 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
676 	{ 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
677 	{ 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
678 	{ 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
679 	{ 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
680 	{ 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
681 	{ 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
682 	{ 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
683 	{ 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
684 	{ 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
685 	{ 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
686 #define AC_PAR_PCM_RATE_BITS	11
687 	/* up to bits 10, 384kHZ isn't supported properly */
688 
689 	/* not autodetected value */
690 	{ 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
691 
692 	{ 0 } /* terminator */
693 };
694 
695 /**
696  * snd_hdac_calc_stream_format - calculate the format bitset
697  * @rate: the sample rate
698  * @channels: the number of channels
699  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
700  * @maxbps: the max. bps
701  * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
702  *
703  * Calculate the format bitset from the given rate, channels and th PCM format.
704  *
705  * Return zero if invalid.
706  */
707 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
708 					 unsigned int channels,
709 					 unsigned int format,
710 					 unsigned int maxbps,
711 					 unsigned short spdif_ctls)
712 {
713 	int i;
714 	unsigned int val = 0;
715 
716 	for (i = 0; rate_bits[i].hz; i++)
717 		if (rate_bits[i].hz == rate) {
718 			val = rate_bits[i].hda_fmt;
719 			break;
720 		}
721 	if (!rate_bits[i].hz)
722 		return 0;
723 
724 	if (channels == 0 || channels > 8)
725 		return 0;
726 	val |= channels - 1;
727 
728 	switch (snd_pcm_format_width(format)) {
729 	case 8:
730 		val |= AC_FMT_BITS_8;
731 		break;
732 	case 16:
733 		val |= AC_FMT_BITS_16;
734 		break;
735 	case 20:
736 	case 24:
737 	case 32:
738 		if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
739 			val |= AC_FMT_BITS_32;
740 		else if (maxbps >= 24)
741 			val |= AC_FMT_BITS_24;
742 		else
743 			val |= AC_FMT_BITS_20;
744 		break;
745 	default:
746 		return 0;
747 	}
748 
749 	if (spdif_ctls & AC_DIG1_NONAUDIO)
750 		val |= AC_FMT_TYPE_NON_PCM;
751 
752 	return val;
753 }
754 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
755 
756 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
757 {
758 	unsigned int val = 0;
759 
760 	if (nid != codec->afg &&
761 	    (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
762 		val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
763 	if (!val || val == -1)
764 		val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
765 	if (!val || val == -1)
766 		return 0;
767 	return val;
768 }
769 
770 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
771 {
772 	unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
773 
774 	if (!streams || streams == -1)
775 		streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
776 	if (!streams || streams == -1)
777 		return 0;
778 	return streams;
779 }
780 
781 /**
782  * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
783  * @codec: the codec object
784  * @nid: NID to query
785  * @ratesp: the pointer to store the detected rate bitflags
786  * @formatsp: the pointer to store the detected formats
787  * @bpsp: the pointer to store the detected format widths
788  *
789  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
790  * or @bsps argument is ignored.
791  *
792  * Returns 0 if successful, otherwise a negative error code.
793  */
794 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
795 				 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
796 {
797 	unsigned int i, val, wcaps;
798 
799 	wcaps = get_wcaps(codec, nid);
800 	val = query_pcm_param(codec, nid);
801 
802 	if (ratesp) {
803 		u32 rates = 0;
804 		for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
805 			if (val & (1 << i))
806 				rates |= rate_bits[i].alsa_bits;
807 		}
808 		if (rates == 0) {
809 			dev_err(&codec->dev,
810 				"rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
811 				nid, val,
812 				(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
813 			return -EIO;
814 		}
815 		*ratesp = rates;
816 	}
817 
818 	if (formatsp || bpsp) {
819 		u64 formats = 0;
820 		unsigned int streams, bps;
821 
822 		streams = query_stream_param(codec, nid);
823 		if (!streams)
824 			return -EIO;
825 
826 		bps = 0;
827 		if (streams & AC_SUPFMT_PCM) {
828 			if (val & AC_SUPPCM_BITS_8) {
829 				formats |= SNDRV_PCM_FMTBIT_U8;
830 				bps = 8;
831 			}
832 			if (val & AC_SUPPCM_BITS_16) {
833 				formats |= SNDRV_PCM_FMTBIT_S16_LE;
834 				bps = 16;
835 			}
836 			if (wcaps & AC_WCAP_DIGITAL) {
837 				if (val & AC_SUPPCM_BITS_32)
838 					formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
839 				if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
840 					formats |= SNDRV_PCM_FMTBIT_S32_LE;
841 				if (val & AC_SUPPCM_BITS_24)
842 					bps = 24;
843 				else if (val & AC_SUPPCM_BITS_20)
844 					bps = 20;
845 			} else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
846 					  AC_SUPPCM_BITS_32)) {
847 				formats |= SNDRV_PCM_FMTBIT_S32_LE;
848 				if (val & AC_SUPPCM_BITS_32)
849 					bps = 32;
850 				else if (val & AC_SUPPCM_BITS_24)
851 					bps = 24;
852 				else if (val & AC_SUPPCM_BITS_20)
853 					bps = 20;
854 			}
855 		}
856 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
857 		if (streams & AC_SUPFMT_FLOAT32) {
858 			formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
859 			if (!bps)
860 				bps = 32;
861 		}
862 #endif
863 		if (streams == AC_SUPFMT_AC3) {
864 			/* should be exclusive */
865 			/* temporary hack: we have still no proper support
866 			 * for the direct AC3 stream...
867 			 */
868 			formats |= SNDRV_PCM_FMTBIT_U8;
869 			bps = 8;
870 		}
871 		if (formats == 0) {
872 			dev_err(&codec->dev,
873 				"formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
874 				nid, val,
875 				(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
876 				streams);
877 			return -EIO;
878 		}
879 		if (formatsp)
880 			*formatsp = formats;
881 		if (bpsp)
882 			*bpsp = bps;
883 	}
884 
885 	return 0;
886 }
887 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
888 
889 /**
890  * snd_hdac_is_supported_format - Check the validity of the format
891  * @codec: the codec object
892  * @nid: NID to check
893  * @format: the HD-audio format value to check
894  *
895  * Check whether the given node supports the format value.
896  *
897  * Returns true if supported, false if not.
898  */
899 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
900 				  unsigned int format)
901 {
902 	int i;
903 	unsigned int val = 0, rate, stream;
904 
905 	val = query_pcm_param(codec, nid);
906 	if (!val)
907 		return false;
908 
909 	rate = format & 0xff00;
910 	for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
911 		if (rate_bits[i].hda_fmt == rate) {
912 			if (val & (1 << i))
913 				break;
914 			return false;
915 		}
916 	if (i >= AC_PAR_PCM_RATE_BITS)
917 		return false;
918 
919 	stream = query_stream_param(codec, nid);
920 	if (!stream)
921 		return false;
922 
923 	if (stream & AC_SUPFMT_PCM) {
924 		switch (format & 0xf0) {
925 		case 0x00:
926 			if (!(val & AC_SUPPCM_BITS_8))
927 				return false;
928 			break;
929 		case 0x10:
930 			if (!(val & AC_SUPPCM_BITS_16))
931 				return false;
932 			break;
933 		case 0x20:
934 			if (!(val & AC_SUPPCM_BITS_20))
935 				return false;
936 			break;
937 		case 0x30:
938 			if (!(val & AC_SUPPCM_BITS_24))
939 				return false;
940 			break;
941 		case 0x40:
942 			if (!(val & AC_SUPPCM_BITS_32))
943 				return false;
944 			break;
945 		default:
946 			return false;
947 		}
948 	} else {
949 		/* FIXME: check for float32 and AC3? */
950 	}
951 
952 	return true;
953 }
954 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
955