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