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