xref: /openbmc/linux/sound/hda/hdac_device.c (revision 034f90b3)
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 "local.h"
13 
14 static void setup_fg_nodes(struct hdac_device *codec);
15 static int get_codec_vendor_name(struct hdac_device *codec);
16 
17 static void default_release(struct device *dev)
18 {
19 	snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
20 }
21 
22 /**
23  * snd_hdac_device_init - initialize the HD-audio codec base device
24  * @codec: device to initialize
25  * @bus: but to attach
26  * @name: device name string
27  * @addr: codec address
28  *
29  * Returns zero for success or a negative error code.
30  *
31  * This function increments the runtime PM counter and marks it active.
32  * The caller needs to turn it off appropriately later.
33  *
34  * The caller needs to set the device's release op properly by itself.
35  */
36 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
37 			 const char *name, unsigned int addr)
38 {
39 	struct device *dev;
40 	hda_nid_t fg;
41 	int err;
42 
43 	dev = &codec->dev;
44 	device_initialize(dev);
45 	dev->parent = bus->dev;
46 	dev->bus = &snd_hda_bus_type;
47 	dev->release = default_release;
48 	dev->groups = hdac_dev_attr_groups;
49 	dev_set_name(dev, "%s", name);
50 	device_enable_async_suspend(dev);
51 
52 	codec->bus = bus;
53 	codec->addr = addr;
54 	codec->type = HDA_DEV_CORE;
55 	pm_runtime_set_active(&codec->dev);
56 	pm_runtime_get_noresume(&codec->dev);
57 	atomic_set(&codec->in_pm, 0);
58 
59 	err = snd_hdac_bus_add_device(bus, codec);
60 	if (err < 0)
61 		goto error;
62 
63 	/* fill parameters */
64 	codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
65 					      AC_PAR_VENDOR_ID);
66 	if (codec->vendor_id == -1) {
67 		/* read again, hopefully the access method was corrected
68 		 * in the last read...
69 		 */
70 		codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
71 						      AC_PAR_VENDOR_ID);
72 	}
73 
74 	codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
75 						 AC_PAR_SUBSYSTEM_ID);
76 	codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
77 						AC_PAR_REV_ID);
78 
79 	setup_fg_nodes(codec);
80 	if (!codec->afg && !codec->mfg) {
81 		dev_err(dev, "no AFG or MFG node found\n");
82 		err = -ENODEV;
83 		goto error;
84 	}
85 
86 	fg = codec->afg ? codec->afg : codec->mfg;
87 
88 	err = snd_hdac_refresh_widgets(codec);
89 	if (err < 0)
90 		goto error;
91 
92 	codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
93 	/* reread ssid if not set by parameter */
94 	if (codec->subsystem_id == -1)
95 		snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
96 			      &codec->subsystem_id);
97 
98 	err = get_codec_vendor_name(codec);
99 	if (err < 0)
100 		goto error;
101 
102 	codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
103 				     codec->vendor_id & 0xffff);
104 	if (!codec->chip_name) {
105 		err = -ENOMEM;
106 		goto error;
107 	}
108 
109 	return 0;
110 
111  error:
112 	put_device(&codec->dev);
113 	return err;
114 }
115 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
116 
117 /**
118  * snd_hdac_device_exit - clean up the HD-audio codec base device
119  * @codec: device to clean up
120  */
121 void snd_hdac_device_exit(struct hdac_device *codec)
122 {
123 	pm_runtime_put_noidle(&codec->dev);
124 	snd_hdac_bus_remove_device(codec->bus, codec);
125 	kfree(codec->vendor_name);
126 	kfree(codec->chip_name);
127 }
128 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
129 
130 /**
131  * snd_hdac_device_register - register the hd-audio codec base device
132  * codec: the device to register
133  */
134 int snd_hdac_device_register(struct hdac_device *codec)
135 {
136 	int err;
137 
138 	err = device_add(&codec->dev);
139 	if (err < 0)
140 		return err;
141 	err = hda_widget_sysfs_init(codec);
142 	if (err < 0) {
143 		device_del(&codec->dev);
144 		return err;
145 	}
146 
147 	return 0;
148 }
149 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
150 
151 /**
152  * snd_hdac_device_unregister - unregister the hd-audio codec base device
153  * codec: the device to unregister
154  */
155 void snd_hdac_device_unregister(struct hdac_device *codec)
156 {
157 	if (device_is_registered(&codec->dev)) {
158 		hda_widget_sysfs_exit(codec);
159 		device_del(&codec->dev);
160 	}
161 }
162 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
163 
164 /**
165  * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
166  *	HD-audio controller
167  * @codec: the codec object
168  * @nid: NID to encode
169  * @verb: verb to encode
170  * @parm: parameter to encode
171  *
172  * Return an encoded command verb or -1 for error.
173  */
174 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
175 			       unsigned int verb, unsigned int parm)
176 {
177 	u32 val, addr;
178 
179 	addr = codec->addr;
180 	if ((addr & ~0xf) || (nid & ~0x7f) ||
181 	    (verb & ~0xfff) || (parm & ~0xffff)) {
182 		dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
183 			addr, nid, verb, parm);
184 		return -1;
185 	}
186 
187 	val = addr << 28;
188 	val |= (u32)nid << 20;
189 	val |= verb << 8;
190 	val |= parm;
191 	return val;
192 }
193 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
194 
195 /**
196  * snd_hdac_exec_verb - execute an encoded verb
197  * @codec: the codec object
198  * @cmd: encoded verb to execute
199  * @flags: optional flags, pass zero for default
200  * @res: the pointer to store the result, NULL if running async
201  *
202  * Returns zero if successful, or a negative error code.
203  *
204  * This calls the exec_verb op when set in hdac_codec.  If not,
205  * call the default snd_hdac_bus_exec_verb().
206  */
207 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
208 		       unsigned int flags, unsigned int *res)
209 {
210 	if (codec->exec_verb)
211 		return codec->exec_verb(codec, cmd, flags, res);
212 	return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
213 }
214 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
215 
216 
217 /**
218  * snd_hdac_read - execute a verb
219  * @codec: the codec object
220  * @nid: NID to execute a verb
221  * @verb: verb to execute
222  * @parm: parameter for a verb
223  * @res: the pointer to store the result, NULL if running async
224  *
225  * Returns zero if successful, or a negative error code.
226  */
227 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
228 		  unsigned int verb, unsigned int parm, unsigned int *res)
229 {
230 	unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
231 
232 	return snd_hdac_exec_verb(codec, cmd, 0, res);
233 }
234 EXPORT_SYMBOL_GPL(snd_hdac_read);
235 
236 /**
237  * snd_hdac_read_parm - read a codec parameter
238  * @codec: the codec object
239  * @nid: NID to read a parameter
240  * @parm: parameter to read
241  *
242  * Returns -1 for error.  If you need to distinguish the error more
243  * strictly, use snd_hdac_read() directly.
244  */
245 int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm)
246 {
247 	int val;
248 
249 	if (snd_hdac_read(codec, nid, AC_VERB_PARAMETERS, parm, &val))
250 		return -1;
251 	return val;
252 }
253 EXPORT_SYMBOL_GPL(snd_hdac_read_parm);
254 
255 /**
256  * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
257  * @codec: the codec object
258  * @nid: NID to inspect
259  * @start_id: the pointer to store the starting NID
260  *
261  * Returns the number of subtree nodes or zero if not found.
262  */
263 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
264 			   hda_nid_t *start_id)
265 {
266 	unsigned int parm;
267 
268 	parm = snd_hdac_read_parm(codec, nid, AC_PAR_NODE_COUNT);
269 	if (parm == -1) {
270 		*start_id = 0;
271 		return 0;
272 	}
273 	*start_id = (parm >> 16) & 0x7fff;
274 	return (int)(parm & 0x7fff);
275 }
276 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
277 
278 /*
279  * look for an AFG and MFG nodes
280  */
281 static void setup_fg_nodes(struct hdac_device *codec)
282 {
283 	int i, total_nodes, function_id;
284 	hda_nid_t nid;
285 
286 	total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
287 	for (i = 0; i < total_nodes; i++, nid++) {
288 		function_id = snd_hdac_read_parm(codec, nid,
289 						 AC_PAR_FUNCTION_TYPE);
290 		switch (function_id & 0xff) {
291 		case AC_GRP_AUDIO_FUNCTION:
292 			codec->afg = nid;
293 			codec->afg_function_id = function_id & 0xff;
294 			codec->afg_unsol = (function_id >> 8) & 1;
295 			break;
296 		case AC_GRP_MODEM_FUNCTION:
297 			codec->mfg = nid;
298 			codec->mfg_function_id = function_id & 0xff;
299 			codec->mfg_unsol = (function_id >> 8) & 1;
300 			break;
301 		default:
302 			break;
303 		}
304 	}
305 }
306 
307 /**
308  * snd_hdac_refresh_widgets - Reset the widget start/end nodes
309  * @codec: the codec object
310  */
311 int snd_hdac_refresh_widgets(struct hdac_device *codec)
312 {
313 	hda_nid_t start_nid;
314 	int nums;
315 
316 	nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
317 	if (!start_nid || nums <= 0 || nums >= 0xff) {
318 		dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
319 			codec->afg);
320 		return -EINVAL;
321 	}
322 
323 	codec->num_nodes = nums;
324 	codec->start_nid = start_nid;
325 	codec->end_nid = start_nid + nums;
326 	return 0;
327 }
328 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
329 
330 /* return CONNLIST_LEN parameter of the given widget */
331 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
332 {
333 	unsigned int wcaps = get_wcaps(codec, nid);
334 	unsigned int parm;
335 
336 	if (!(wcaps & AC_WCAP_CONN_LIST) &&
337 	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
338 		return 0;
339 
340 	parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
341 	if (parm == -1)
342 		parm = 0;
343 	return parm;
344 }
345 
346 /**
347  * snd_hdac_get_connections - get a widget connection list
348  * @codec: the codec object
349  * @nid: NID
350  * @conn_list: the array to store the results, can be NULL
351  * @max_conns: the max size of the given array
352  *
353  * Returns the number of connected widgets, zero for no connection, or a
354  * negative error code.  When the number of elements don't fit with the
355  * given array size, it returns -ENOSPC.
356  *
357  * When @conn_list is NULL, it just checks the number of connections.
358  */
359 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
360 			     hda_nid_t *conn_list, int max_conns)
361 {
362 	unsigned int parm;
363 	int i, conn_len, conns, err;
364 	unsigned int shift, num_elems, mask;
365 	hda_nid_t prev_nid;
366 	int null_count = 0;
367 
368 	parm = get_num_conns(codec, nid);
369 	if (!parm)
370 		return 0;
371 
372 	if (parm & AC_CLIST_LONG) {
373 		/* long form */
374 		shift = 16;
375 		num_elems = 2;
376 	} else {
377 		/* short form */
378 		shift = 8;
379 		num_elems = 4;
380 	}
381 	conn_len = parm & AC_CLIST_LENGTH;
382 	mask = (1 << (shift-1)) - 1;
383 
384 	if (!conn_len)
385 		return 0; /* no connection */
386 
387 	if (conn_len == 1) {
388 		/* single connection */
389 		err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
390 				    &parm);
391 		if (err < 0)
392 			return err;
393 		if (conn_list)
394 			conn_list[0] = parm & mask;
395 		return 1;
396 	}
397 
398 	/* multi connection */
399 	conns = 0;
400 	prev_nid = 0;
401 	for (i = 0; i < conn_len; i++) {
402 		int range_val;
403 		hda_nid_t val, n;
404 
405 		if (i % num_elems == 0) {
406 			err = snd_hdac_read(codec, nid,
407 					    AC_VERB_GET_CONNECT_LIST, i,
408 					    &parm);
409 			if (err < 0)
410 				return -EIO;
411 		}
412 		range_val = !!(parm & (1 << (shift-1))); /* ranges */
413 		val = parm & mask;
414 		if (val == 0 && null_count++) {  /* no second chance */
415 			dev_dbg(&codec->dev,
416 				"invalid CONNECT_LIST verb %x[%i]:%x\n",
417 				nid, i, parm);
418 			return 0;
419 		}
420 		parm >>= shift;
421 		if (range_val) {
422 			/* ranges between the previous and this one */
423 			if (!prev_nid || prev_nid >= val) {
424 				dev_warn(&codec->dev,
425 					 "invalid dep_range_val %x:%x\n",
426 					 prev_nid, val);
427 				continue;
428 			}
429 			for (n = prev_nid + 1; n <= val; n++) {
430 				if (conn_list) {
431 					if (conns >= max_conns)
432 						return -ENOSPC;
433 					conn_list[conns] = n;
434 				}
435 				conns++;
436 			}
437 		} else {
438 			if (conn_list) {
439 				if (conns >= max_conns)
440 					return -ENOSPC;
441 				conn_list[conns] = val;
442 			}
443 			conns++;
444 		}
445 		prev_nid = val;
446 	}
447 	return conns;
448 }
449 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
450 
451 #ifdef CONFIG_PM
452 /**
453  * snd_hdac_power_up - increment the runtime pm counter
454  * @codec: the codec object
455  */
456 void snd_hdac_power_up(struct hdac_device *codec)
457 {
458 	struct device *dev = &codec->dev;
459 
460 	if (atomic_read(&codec->in_pm))
461 		return;
462 	pm_runtime_get_sync(dev);
463 }
464 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
465 
466 /**
467  * snd_hdac_power_up - decrement the runtime pm counter
468  * @codec: the codec object
469  */
470 void snd_hdac_power_down(struct hdac_device *codec)
471 {
472 	struct device *dev = &codec->dev;
473 
474 	if (atomic_read(&codec->in_pm))
475 		return;
476 	pm_runtime_mark_last_busy(dev);
477 	pm_runtime_put_autosuspend(dev);
478 }
479 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
480 #endif
481 
482 /* codec vendor labels */
483 struct hda_vendor_id {
484 	unsigned int id;
485 	const char *name;
486 };
487 
488 static struct hda_vendor_id hda_vendor_ids[] = {
489 	{ 0x1002, "ATI" },
490 	{ 0x1013, "Cirrus Logic" },
491 	{ 0x1057, "Motorola" },
492 	{ 0x1095, "Silicon Image" },
493 	{ 0x10de, "Nvidia" },
494 	{ 0x10ec, "Realtek" },
495 	{ 0x1102, "Creative" },
496 	{ 0x1106, "VIA" },
497 	{ 0x111d, "IDT" },
498 	{ 0x11c1, "LSI" },
499 	{ 0x11d4, "Analog Devices" },
500 	{ 0x13f6, "C-Media" },
501 	{ 0x14f1, "Conexant" },
502 	{ 0x17e8, "Chrontel" },
503 	{ 0x1854, "LG" },
504 	{ 0x1aec, "Wolfson Microelectronics" },
505 	{ 0x1af4, "QEMU" },
506 	{ 0x434d, "C-Media" },
507 	{ 0x8086, "Intel" },
508 	{ 0x8384, "SigmaTel" },
509 	{} /* terminator */
510 };
511 
512 /* store the codec vendor name */
513 static int get_codec_vendor_name(struct hdac_device *codec)
514 {
515 	const struct hda_vendor_id *c;
516 	u16 vendor_id = codec->vendor_id >> 16;
517 
518 	for (c = hda_vendor_ids; c->id; c++) {
519 		if (c->id == vendor_id) {
520 			codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
521 			return codec->vendor_name ? 0 : -ENOMEM;
522 		}
523 	}
524 
525 	codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
526 	return codec->vendor_name ? 0 : -ENOMEM;
527 }
528