gpio: bcm-kona: fix bcm_kona_gpio_reset() warnings
[linux/fpc-iii.git] / sound / hda / hdac_device.c
blob961ca32ee9898c57bd9d39ae05ba3f6aaea2ef63
1 /*
2 * HD-audio codec core device
3 */
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 "local.h"
15 static void setup_fg_nodes(struct hdac_device *codec);
16 static int get_codec_vendor_name(struct hdac_device *codec);
18 static void default_release(struct device *dev)
20 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
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
30 * Returns zero for success or a negative error code.
32 * This function increments the runtime PM counter and marks it active.
33 * The caller needs to turn it off appropriately later.
35 * The caller needs to set the device's release op properly by itself.
37 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
38 const char *name, unsigned int addr)
40 struct device *dev;
41 hda_nid_t fg;
42 int err;
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;
49 dev->groups = hdac_dev_attr_groups;
50 dev_set_name(dev, "%s", name);
51 device_enable_async_suspend(dev);
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);
60 err = snd_hdac_bus_add_device(bus, codec);
61 if (err < 0)
62 goto error;
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...
71 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
72 AC_PAR_VENDOR_ID);
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);
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;
87 fg = codec->afg ? codec->afg : codec->mfg;
89 err = snd_hdac_refresh_widgets(codec);
90 if (err < 0)
91 goto error;
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 || codec->subsystem_id == 0)
96 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
97 &codec->subsystem_id);
99 err = get_codec_vendor_name(codec);
100 if (err < 0)
101 goto error;
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;
110 return 0;
112 error:
113 put_device(&codec->dev);
114 return err;
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)
137 int err;
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;
148 return 0;
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)
178 u32 val, addr;
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;
188 val = addr << 28;
189 val |= (u32)nid << 20;
190 val |= verb << 8;
191 val |= parm;
192 return val;
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 parmeter
240 * This function returns zero or an error unlike snd_hdac_read_parm().
242 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
243 unsigned int *res)
245 unsigned int cmd;
247 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
248 return snd_hdac_regmap_read_raw(codec, cmd, res);
250 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
253 * 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
258 * Returns -1 for error. If you need to distinguish the error more
259 * strictly, use snd_hdac_read() directly.
261 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
262 int parm)
264 int val;
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;
273 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
276 * snd_hdac_override_parm - override read-only parameters
277 * @codec: the codec object
278 * @nid: NID for the parameter
279 * @parm: the parameter to change
280 * @val: the parameter value to overwrite
282 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
283 unsigned int parm, unsigned int val)
285 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
286 int err;
288 if (!codec->regmap)
289 return -EINVAL;
291 codec->caps_overwriting = true;
292 err = snd_hdac_regmap_write_raw(codec, verb, val);
293 codec->caps_overwriting = false;
294 return err;
296 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
299 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
300 * @codec: the codec object
301 * @nid: NID to inspect
302 * @start_id: the pointer to store the starting NID
304 * Returns the number of subtree nodes or zero if not found.
305 * This function reads parameters always without caching.
307 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
308 hda_nid_t *start_id)
310 unsigned int parm;
312 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
313 if (parm == -1) {
314 *start_id = 0;
315 return 0;
317 *start_id = (parm >> 16) & 0x7fff;
318 return (int)(parm & 0x7fff);
320 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
323 * look for an AFG and MFG nodes
325 static void setup_fg_nodes(struct hdac_device *codec)
327 int i, total_nodes, function_id;
328 hda_nid_t nid;
330 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
331 for (i = 0; i < total_nodes; i++, nid++) {
332 function_id = snd_hdac_read_parm(codec, nid,
333 AC_PAR_FUNCTION_TYPE);
334 switch (function_id & 0xff) {
335 case AC_GRP_AUDIO_FUNCTION:
336 codec->afg = nid;
337 codec->afg_function_id = function_id & 0xff;
338 codec->afg_unsol = (function_id >> 8) & 1;
339 break;
340 case AC_GRP_MODEM_FUNCTION:
341 codec->mfg = nid;
342 codec->mfg_function_id = function_id & 0xff;
343 codec->mfg_unsol = (function_id >> 8) & 1;
344 break;
345 default:
346 break;
352 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
353 * @codec: the codec object
355 int snd_hdac_refresh_widgets(struct hdac_device *codec)
357 hda_nid_t start_nid;
358 int nums;
360 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
361 if (!start_nid || nums <= 0 || nums >= 0xff) {
362 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
363 codec->afg);
364 return -EINVAL;
367 codec->num_nodes = nums;
368 codec->start_nid = start_nid;
369 codec->end_nid = start_nid + nums;
370 return 0;
372 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
374 /* return CONNLIST_LEN parameter of the given widget */
375 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
377 unsigned int wcaps = get_wcaps(codec, nid);
378 unsigned int parm;
380 if (!(wcaps & AC_WCAP_CONN_LIST) &&
381 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
382 return 0;
384 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
385 if (parm == -1)
386 parm = 0;
387 return parm;
391 * snd_hdac_get_connections - get a widget connection list
392 * @codec: the codec object
393 * @nid: NID
394 * @conn_list: the array to store the results, can be NULL
395 * @max_conns: the max size of the given array
397 * Returns the number of connected widgets, zero for no connection, or a
398 * negative error code. When the number of elements don't fit with the
399 * given array size, it returns -ENOSPC.
401 * When @conn_list is NULL, it just checks the number of connections.
403 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
404 hda_nid_t *conn_list, int max_conns)
406 unsigned int parm;
407 int i, conn_len, conns, err;
408 unsigned int shift, num_elems, mask;
409 hda_nid_t prev_nid;
410 int null_count = 0;
412 parm = get_num_conns(codec, nid);
413 if (!parm)
414 return 0;
416 if (parm & AC_CLIST_LONG) {
417 /* long form */
418 shift = 16;
419 num_elems = 2;
420 } else {
421 /* short form */
422 shift = 8;
423 num_elems = 4;
425 conn_len = parm & AC_CLIST_LENGTH;
426 mask = (1 << (shift-1)) - 1;
428 if (!conn_len)
429 return 0; /* no connection */
431 if (conn_len == 1) {
432 /* single connection */
433 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
434 &parm);
435 if (err < 0)
436 return err;
437 if (conn_list)
438 conn_list[0] = parm & mask;
439 return 1;
442 /* multi connection */
443 conns = 0;
444 prev_nid = 0;
445 for (i = 0; i < conn_len; i++) {
446 int range_val;
447 hda_nid_t val, n;
449 if (i % num_elems == 0) {
450 err = snd_hdac_read(codec, nid,
451 AC_VERB_GET_CONNECT_LIST, i,
452 &parm);
453 if (err < 0)
454 return -EIO;
456 range_val = !!(parm & (1 << (shift-1))); /* ranges */
457 val = parm & mask;
458 if (val == 0 && null_count++) { /* no second chance */
459 dev_dbg(&codec->dev,
460 "invalid CONNECT_LIST verb %x[%i]:%x\n",
461 nid, i, parm);
462 return 0;
464 parm >>= shift;
465 if (range_val) {
466 /* ranges between the previous and this one */
467 if (!prev_nid || prev_nid >= val) {
468 dev_warn(&codec->dev,
469 "invalid dep_range_val %x:%x\n",
470 prev_nid, val);
471 continue;
473 for (n = prev_nid + 1; n <= val; n++) {
474 if (conn_list) {
475 if (conns >= max_conns)
476 return -ENOSPC;
477 conn_list[conns] = n;
479 conns++;
481 } else {
482 if (conn_list) {
483 if (conns >= max_conns)
484 return -ENOSPC;
485 conn_list[conns] = val;
487 conns++;
489 prev_nid = val;
491 return conns;
493 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
495 #ifdef CONFIG_PM
497 * snd_hdac_power_up - power up the codec
498 * @codec: the codec object
500 * This function calls the runtime PM helper to power up the given codec.
501 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
502 * path that isn't included in PM path. Otherwise it gets stuck.
504 * Returns zero if successful, or a negative error code.
506 int snd_hdac_power_up(struct hdac_device *codec)
508 return pm_runtime_get_sync(&codec->dev);
510 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
513 * snd_hdac_power_down - power down the codec
514 * @codec: the codec object
516 * Returns zero if successful, or a negative error code.
518 int snd_hdac_power_down(struct hdac_device *codec)
520 struct device *dev = &codec->dev;
522 pm_runtime_mark_last_busy(dev);
523 return pm_runtime_put_autosuspend(dev);
525 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
528 * snd_hdac_power_up_pm - power up the codec
529 * @codec: the codec object
531 * This function can be called in a recursive code path like init code
532 * which may be called by PM suspend/resume again. OTOH, if a power-up
533 * call must wake up the sleeper (e.g. in a kctl callback), use
534 * snd_hdac_power_up() instead.
536 * Returns zero if successful, or a negative error code.
538 int snd_hdac_power_up_pm(struct hdac_device *codec)
540 if (!atomic_inc_not_zero(&codec->in_pm))
541 return snd_hdac_power_up(codec);
542 return 0;
544 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
547 * snd_hdac_power_down_pm - power down the codec
548 * @codec: the codec object
550 * Like snd_hdac_power_up_pm(), this function is used in a recursive
551 * code path like init code which may be called by PM suspend/resume again.
553 * Returns zero if successful, or a negative error code.
555 int snd_hdac_power_down_pm(struct hdac_device *codec)
557 if (atomic_dec_if_positive(&codec->in_pm) < 0)
558 return snd_hdac_power_down(codec);
559 return 0;
561 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
562 #endif
564 /* codec vendor labels */
565 struct hda_vendor_id {
566 unsigned int id;
567 const char *name;
570 static struct hda_vendor_id hda_vendor_ids[] = {
571 { 0x1002, "ATI" },
572 { 0x1013, "Cirrus Logic" },
573 { 0x1057, "Motorola" },
574 { 0x1095, "Silicon Image" },
575 { 0x10de, "Nvidia" },
576 { 0x10ec, "Realtek" },
577 { 0x1102, "Creative" },
578 { 0x1106, "VIA" },
579 { 0x111d, "IDT" },
580 { 0x11c1, "LSI" },
581 { 0x11d4, "Analog Devices" },
582 { 0x13f6, "C-Media" },
583 { 0x14f1, "Conexant" },
584 { 0x17e8, "Chrontel" },
585 { 0x1854, "LG" },
586 { 0x1aec, "Wolfson Microelectronics" },
587 { 0x1af4, "QEMU" },
588 { 0x434d, "C-Media" },
589 { 0x8086, "Intel" },
590 { 0x8384, "SigmaTel" },
591 {} /* terminator */
594 /* store the codec vendor name */
595 static int get_codec_vendor_name(struct hdac_device *codec)
597 const struct hda_vendor_id *c;
598 u16 vendor_id = codec->vendor_id >> 16;
600 for (c = hda_vendor_ids; c->id; c++) {
601 if (c->id == vendor_id) {
602 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
603 return codec->vendor_name ? 0 : -ENOMEM;
607 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
608 return codec->vendor_name ? 0 : -ENOMEM;