2 * HD-audio codec core device
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>
14 static void setup_fg_nodes(struct hdac_device *codec);
15 static int get_codec_vendor_name(struct hdac_device *codec);
17 static void default_release(struct device *dev)
19 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
23 * snd_hdac_device_init - initialize the HD-audio codec base device
24 * @codec: device to initialize
26 * @name: device name string
27 * @addr: codec address
29 * Returns zero for success or a negative error code.
31 * This function increments the runtime PM counter and marks it active.
32 * The caller needs to turn it off appropriately later.
34 * The caller needs to set the device's release op properly by itself.
36 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
37 const char *name, unsigned int addr)
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);
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);
59 err = snd_hdac_bus_add_device(bus, codec);
64 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
66 if (codec->vendor_id == -1) {
67 /* read again, hopefully the access method was corrected
70 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
74 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
76 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 setup_fg_nodes(codec);
80 if (!codec->afg && !codec->mfg) {
81 dev_err(dev, "no AFG or MFG node found\n");
86 fg = codec->afg ? codec->afg : codec->mfg;
88 err = snd_hdac_refresh_widgets(codec);
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);
98 err = get_codec_vendor_name(codec);
102 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
103 codec->vendor_id & 0xffff);
104 if (!codec->chip_name) {
112 put_device(&codec->dev);
115 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
118 * snd_hdac_device_exit - clean up the HD-audio codec base device
119 * @codec: device to clean up
121 void snd_hdac_device_exit(struct hdac_device *codec)
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);
128 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
131 * snd_hdac_device_register - register the hd-audio codec base device
132 * codec: the device to register
134 int snd_hdac_device_register(struct hdac_device *codec)
138 err = device_add(&codec->dev);
141 err = hda_widget_sysfs_init(codec);
143 device_del(&codec->dev);
149 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
152 * snd_hdac_device_unregister - unregister the hd-audio codec base device
153 * codec: the device to unregister
155 void snd_hdac_device_unregister(struct hdac_device *codec)
157 if (device_is_registered(&codec->dev)) {
158 hda_widget_sysfs_exit(codec);
159 device_del(&codec->dev);
162 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
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
172 * Return an encoded command verb or -1 for error.
174 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
175 unsigned int verb, unsigned int parm)
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);
188 val |= (u32)nid << 20;
193 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
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
202 * Returns zero if successful, or a negative error code.
204 * This calls the exec_verb op when set in hdac_codec. If not,
205 * call the default snd_hdac_bus_exec_verb().
207 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
208 unsigned int flags, unsigned int *res)
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);
214 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
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
225 * Returns zero if successful, or a negative error code.
227 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
228 unsigned int verb, unsigned int parm, unsigned int *res)
230 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
232 return snd_hdac_exec_verb(codec, cmd, 0, res);
234 EXPORT_SYMBOL_GPL(snd_hdac_read);
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
242 * Returns -1 for error. If you need to distinguish the error more
243 * strictly, use snd_hdac_read() directly.
245 int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm)
249 if (snd_hdac_read(codec, nid, AC_VERB_PARAMETERS, parm, &val))
253 EXPORT_SYMBOL_GPL(snd_hdac_read_parm);
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
261 * Returns the number of subtree nodes or zero if not found.
263 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
268 parm = snd_hdac_read_parm(codec, nid, AC_PAR_NODE_COUNT);
273 *start_id = (parm >> 16) & 0x7fff;
274 return (int)(parm & 0x7fff);
276 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
279 * look for an AFG and MFG nodes
281 static void setup_fg_nodes(struct hdac_device *codec)
283 int i, total_nodes, function_id;
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:
293 codec->afg_function_id = function_id & 0xff;
294 codec->afg_unsol = (function_id >> 8) & 1;
296 case AC_GRP_MODEM_FUNCTION:
298 codec->mfg_function_id = function_id & 0xff;
299 codec->mfg_unsol = (function_id >> 8) & 1;
308 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
309 * @codec: the codec object
311 int snd_hdac_refresh_widgets(struct hdac_device *codec)
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",
323 codec->num_nodes = nums;
324 codec->start_nid = start_nid;
325 codec->end_nid = start_nid + nums;
328 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
330 /* return CONNLIST_LEN parameter of the given widget */
331 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
333 unsigned int wcaps = get_wcaps(codec, nid);
336 if (!(wcaps & AC_WCAP_CONN_LIST) &&
337 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
340 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
347 * snd_hdac_get_connections - get a widget connection list
348 * @codec: the codec object
350 * @conn_list: the array to store the results, can be NULL
351 * @max_conns: the max size of the given array
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.
357 * When @conn_list is NULL, it just checks the number of connections.
359 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
360 hda_nid_t *conn_list, int max_conns)
363 int i, conn_len, conns, err;
364 unsigned int shift, num_elems, mask;
368 parm = get_num_conns(codec, nid);
372 if (parm & AC_CLIST_LONG) {
381 conn_len = parm & AC_CLIST_LENGTH;
382 mask = (1 << (shift-1)) - 1;
385 return 0; /* no connection */
388 /* single connection */
389 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
394 conn_list[0] = parm & mask;
398 /* multi connection */
401 for (i = 0; i < conn_len; i++) {
405 if (i % num_elems == 0) {
406 err = snd_hdac_read(codec, nid,
407 AC_VERB_GET_CONNECT_LIST, i,
412 range_val = !!(parm & (1 << (shift-1))); /* ranges */
414 if (val == 0 && null_count++) { /* no second chance */
416 "invalid CONNECT_LIST verb %x[%i]:%x\n",
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",
429 for (n = prev_nid + 1; n <= val; n++) {
431 if (conns >= max_conns)
433 conn_list[conns] = n;
439 if (conns >= max_conns)
441 conn_list[conns] = val;
449 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
453 * snd_hdac_power_up - increment the runtime pm counter
454 * @codec: the codec object
456 void snd_hdac_power_up(struct hdac_device *codec)
458 struct device *dev = &codec->dev;
460 if (atomic_read(&codec->in_pm))
462 pm_runtime_get_sync(dev);
464 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
467 * snd_hdac_power_up - decrement the runtime pm counter
468 * @codec: the codec object
470 void snd_hdac_power_down(struct hdac_device *codec)
472 struct device *dev = &codec->dev;
474 if (atomic_read(&codec->in_pm))
476 pm_runtime_mark_last_busy(dev);
477 pm_runtime_put_autosuspend(dev);
479 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
482 /* codec vendor labels */
483 struct hda_vendor_id {
488 static struct hda_vendor_id hda_vendor_ids[] = {
490 { 0x1013, "Cirrus Logic" },
491 { 0x1057, "Motorola" },
492 { 0x1095, "Silicon Image" },
493 { 0x10de, "Nvidia" },
494 { 0x10ec, "Realtek" },
495 { 0x1102, "Creative" },
499 { 0x11d4, "Analog Devices" },
500 { 0x13f6, "C-Media" },
501 { 0x14f1, "Conexant" },
502 { 0x17e8, "Chrontel" },
504 { 0x1aec, "Wolfson Microelectronics" },
506 { 0x434d, "C-Media" },
508 { 0x8384, "SigmaTel" },
512 /* store the codec vendor name */
513 static int get_codec_vendor_name(struct hdac_device *codec)
515 const struct hda_vendor_id *c;
516 u16 vendor_id = codec->vendor_id >> 16;
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;
525 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
526 return codec->vendor_name ? 0 : -ENOMEM;