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