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 pm_runtime_put_noidle(&codec->dev);
113 put_device(&codec->dev);
116 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
119 * snd_hdac_device_exit - clean up the HD-audio codec base device
120 * @codec: device to clean up
122 void snd_hdac_device_exit(struct hdac_device *codec)
124 /* pm_runtime_put_noidle(&codec->dev); */
125 snd_hdac_bus_remove_device(codec->bus, codec);
126 kfree(codec->vendor_name);
127 kfree(codec->chip_name);
129 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
132 * snd_hdac_device_register - register the hd-audio codec base device
133 * codec: the device to register
135 int snd_hdac_device_register(struct hdac_device *codec)
139 err = device_add(&codec->dev);
142 err = hda_widget_sysfs_init(codec);
144 device_del(&codec->dev);
150 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
153 * snd_hdac_device_unregister - unregister the hd-audio codec base device
154 * codec: the device to unregister
156 void snd_hdac_device_unregister(struct hdac_device *codec)
158 if (device_is_registered(&codec->dev)) {
159 hda_widget_sysfs_exit(codec);
160 device_del(&codec->dev);
163 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
166 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
167 * HD-audio controller
168 * @codec: the codec object
169 * @nid: NID to encode
170 * @verb: verb to encode
171 * @parm: parameter to encode
173 * Return an encoded command verb or -1 for error.
175 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
176 unsigned int verb, unsigned int parm)
181 if ((addr & ~0xf) || (nid & ~0x7f) ||
182 (verb & ~0xfff) || (parm & ~0xffff)) {
183 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
184 addr, nid, verb, parm);
189 val |= (u32)nid << 20;
194 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
197 * snd_hdac_exec_verb - execute an encoded verb
198 * @codec: the codec object
199 * @cmd: encoded verb to execute
200 * @flags: optional flags, pass zero for default
201 * @res: the pointer to store the result, NULL if running async
203 * Returns zero if successful, or a negative error code.
205 * This calls the exec_verb op when set in hdac_codec. If not,
206 * call the default snd_hdac_bus_exec_verb().
208 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
209 unsigned int flags, unsigned int *res)
211 if (codec->exec_verb)
212 return codec->exec_verb(codec, cmd, flags, res);
213 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
215 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
219 * snd_hdac_read - execute a verb
220 * @codec: the codec object
221 * @nid: NID to execute a verb
222 * @verb: verb to execute
223 * @parm: parameter for a verb
224 * @res: the pointer to store the result, NULL if running async
226 * Returns zero if successful, or a negative error code.
228 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
229 unsigned int verb, unsigned int parm, unsigned int *res)
231 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
233 return snd_hdac_exec_verb(codec, cmd, 0, res);
235 EXPORT_SYMBOL_GPL(snd_hdac_read);
238 * snd_hdac_read_parm - read a codec parameter
239 * @codec: the codec object
240 * @nid: NID to read a parameter
241 * @parm: parameter to read
243 * Returns -1 for error. If you need to distinguish the error more
244 * strictly, use snd_hdac_read() directly.
246 int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm)
250 if (snd_hdac_read(codec, nid, AC_VERB_PARAMETERS, parm, &val))
254 EXPORT_SYMBOL_GPL(snd_hdac_read_parm);
257 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
258 * @codec: the codec object
259 * @nid: NID to inspect
260 * @start_id: the pointer to store the starting NID
262 * Returns the number of subtree nodes or zero if not found.
264 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
269 parm = snd_hdac_read_parm(codec, nid, AC_PAR_NODE_COUNT);
274 *start_id = (parm >> 16) & 0x7fff;
275 return (int)(parm & 0x7fff);
277 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
280 * look for an AFG and MFG nodes
282 static void setup_fg_nodes(struct hdac_device *codec)
284 int i, total_nodes, function_id;
287 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
288 for (i = 0; i < total_nodes; i++, nid++) {
289 function_id = snd_hdac_read_parm(codec, nid,
290 AC_PAR_FUNCTION_TYPE);
291 switch (function_id & 0xff) {
292 case AC_GRP_AUDIO_FUNCTION:
294 codec->afg_function_id = function_id & 0xff;
295 codec->afg_unsol = (function_id >> 8) & 1;
297 case AC_GRP_MODEM_FUNCTION:
299 codec->mfg_function_id = function_id & 0xff;
300 codec->mfg_unsol = (function_id >> 8) & 1;
309 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
310 * @codec: the codec object
312 int snd_hdac_refresh_widgets(struct hdac_device *codec)
317 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
318 if (!start_nid || nums <= 0 || nums >= 0xff) {
319 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
324 codec->num_nodes = nums;
325 codec->start_nid = start_nid;
326 codec->end_nid = start_nid + nums;
329 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
331 /* return CONNLIST_LEN parameter of the given widget */
332 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
334 unsigned int wcaps = get_wcaps(codec, nid);
337 if (!(wcaps & AC_WCAP_CONN_LIST) &&
338 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
341 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
348 * snd_hdac_get_connections - get a widget connection list
349 * @codec: the codec object
351 * @conn_list: the array to store the results, can be NULL
352 * @max_conns: the max size of the given array
354 * Returns the number of connected widgets, zero for no connection, or a
355 * negative error code. When the number of elements don't fit with the
356 * given array size, it returns -ENOSPC.
358 * When @conn_list is NULL, it just checks the number of connections.
360 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
361 hda_nid_t *conn_list, int max_conns)
364 int i, conn_len, conns, err;
365 unsigned int shift, num_elems, mask;
369 parm = get_num_conns(codec, nid);
373 if (parm & AC_CLIST_LONG) {
382 conn_len = parm & AC_CLIST_LENGTH;
383 mask = (1 << (shift-1)) - 1;
386 return 0; /* no connection */
389 /* single connection */
390 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
395 conn_list[0] = parm & mask;
399 /* multi connection */
402 for (i = 0; i < conn_len; i++) {
406 if (i % num_elems == 0) {
407 err = snd_hdac_read(codec, nid,
408 AC_VERB_GET_CONNECT_LIST, i,
413 range_val = !!(parm & (1 << (shift-1))); /* ranges */
415 if (val == 0 && null_count++) { /* no second chance */
417 "invalid CONNECT_LIST verb %x[%i]:%x\n",
423 /* ranges between the previous and this one */
424 if (!prev_nid || prev_nid >= val) {
425 dev_warn(&codec->dev,
426 "invalid dep_range_val %x:%x\n",
430 for (n = prev_nid + 1; n <= val; n++) {
432 if (conns >= max_conns)
434 conn_list[conns] = n;
440 if (conns >= max_conns)
442 conn_list[conns] = val;
450 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
454 * snd_hdac_power_up - increment the runtime pm counter
455 * @codec: the codec object
457 void snd_hdac_power_up(struct hdac_device *codec)
459 struct device *dev = &codec->dev;
461 if (atomic_read(&codec->in_pm))
463 pm_runtime_get_sync(dev);
465 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
468 * snd_hdac_power_up - decrement the runtime pm counter
469 * @codec: the codec object
471 void snd_hdac_power_down(struct hdac_device *codec)
473 struct device *dev = &codec->dev;
475 if (atomic_read(&codec->in_pm))
477 pm_runtime_mark_last_busy(dev);
478 pm_runtime_put_autosuspend(dev);
480 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
483 /* codec vendor labels */
484 struct hda_vendor_id {
489 static struct hda_vendor_id hda_vendor_ids[] = {
491 { 0x1013, "Cirrus Logic" },
492 { 0x1057, "Motorola" },
493 { 0x1095, "Silicon Image" },
494 { 0x10de, "Nvidia" },
495 { 0x10ec, "Realtek" },
496 { 0x1102, "Creative" },
500 { 0x11d4, "Analog Devices" },
501 { 0x13f6, "C-Media" },
502 { 0x14f1, "Conexant" },
503 { 0x17e8, "Chrontel" },
505 { 0x1aec, "Wolfson Microelectronics" },
507 { 0x434d, "C-Media" },
509 { 0x8384, "SigmaTel" },
513 /* store the codec vendor name */
514 static int get_codec_vendor_name(struct hdac_device *codec)
516 const struct hda_vendor_id *c;
517 u16 vendor_id = codec->vendor_id >> 16;
519 for (c = hda_vendor_ids; c->id; c++) {
520 if (c->id == vendor_id) {
521 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
522 return codec->vendor_name ? 0 : -ENOMEM;
526 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
527 return codec->vendor_name ? 0 : -ENOMEM;