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