blob: e361024eabb6371bef25a888bcd8ea58a5499913 [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 Iwaib7d023e2015-04-16 08:19:06 +020013#include <sound/pcm.h>
Takashi Iwai7639a062015-03-03 10:07:24 +010014#include "local.h"
15
16static void setup_fg_nodes(struct hdac_device *codec);
17static int get_codec_vendor_name(struct hdac_device *codec);
18
19static void default_release(struct device *dev)
20{
21 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
22}
23
24/**
25 * snd_hdac_device_init - initialize the HD-audio codec base device
26 * @codec: device to initialize
27 * @bus: but to attach
28 * @name: device name string
29 * @addr: codec address
30 *
31 * Returns zero for success or a negative error code.
32 *
33 * This function increments the runtime PM counter and marks it active.
34 * The caller needs to turn it off appropriately later.
35 *
36 * The caller needs to set the device's release op properly by itself.
37 */
38int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
39 const char *name, unsigned int addr)
40{
41 struct device *dev;
42 hda_nid_t fg;
43 int err;
44
45 dev = &codec->dev;
46 device_initialize(dev);
47 dev->parent = bus->dev;
48 dev->bus = &snd_hda_bus_type;
49 dev->release = default_release;
Takashi Iwai3256be62015-02-24 14:59:42 +010050 dev->groups = hdac_dev_attr_groups;
Takashi Iwai7639a062015-03-03 10:07:24 +010051 dev_set_name(dev, "%s", name);
52 device_enable_async_suspend(dev);
53
54 codec->bus = bus;
55 codec->addr = addr;
56 codec->type = HDA_DEV_CORE;
57 pm_runtime_set_active(&codec->dev);
58 pm_runtime_get_noresume(&codec->dev);
59 atomic_set(&codec->in_pm, 0);
60
61 err = snd_hdac_bus_add_device(bus, codec);
62 if (err < 0)
63 goto error;
64
65 /* fill parameters */
66 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
67 AC_PAR_VENDOR_ID);
68 if (codec->vendor_id == -1) {
69 /* read again, hopefully the access method was corrected
70 * in the last read...
71 */
72 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
73 AC_PAR_VENDOR_ID);
74 }
75
76 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
77 AC_PAR_SUBSYSTEM_ID);
78 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 AC_PAR_REV_ID);
80
81 setup_fg_nodes(codec);
82 if (!codec->afg && !codec->mfg) {
83 dev_err(dev, "no AFG or MFG node found\n");
84 err = -ENODEV;
85 goto error;
86 }
87
88 fg = codec->afg ? codec->afg : codec->mfg;
89
90 err = snd_hdac_refresh_widgets(codec);
91 if (err < 0)
92 goto error;
93
94 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
95 /* reread ssid if not set by parameter */
David Henningssonffda5682015-04-01 12:43:00 +020096 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
Takashi Iwai7639a062015-03-03 10:07:24 +010097 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
98 &codec->subsystem_id);
99
100 err = get_codec_vendor_name(codec);
101 if (err < 0)
102 goto error;
103
104 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
105 codec->vendor_id & 0xffff);
106 if (!codec->chip_name) {
107 err = -ENOMEM;
108 goto error;
109 }
110
111 return 0;
112
113 error:
Takashi Iwai7639a062015-03-03 10:07:24 +0100114 put_device(&codec->dev);
115 return err;
116}
117EXPORT_SYMBOL_GPL(snd_hdac_device_init);
118
119/**
120 * snd_hdac_device_exit - clean up the HD-audio codec base device
121 * @codec: device to clean up
122 */
123void snd_hdac_device_exit(struct hdac_device *codec)
124{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100125 pm_runtime_put_noidle(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100126 snd_hdac_bus_remove_device(codec->bus, codec);
127 kfree(codec->vendor_name);
128 kfree(codec->chip_name);
129}
130EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
131
132/**
Takashi Iwai3256be62015-02-24 14:59:42 +0100133 * snd_hdac_device_register - register the hd-audio codec base device
134 * codec: the device to register
135 */
136int snd_hdac_device_register(struct hdac_device *codec)
137{
138 int err;
139
140 err = device_add(&codec->dev);
141 if (err < 0)
142 return err;
143 err = hda_widget_sysfs_init(codec);
144 if (err < 0) {
145 device_del(&codec->dev);
146 return err;
147 }
148
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_hdac_device_register);
152
153/**
154 * snd_hdac_device_unregister - unregister the hd-audio codec base device
155 * codec: the device to unregister
156 */
157void snd_hdac_device_unregister(struct hdac_device *codec)
158{
159 if (device_is_registered(&codec->dev)) {
160 hda_widget_sysfs_exit(codec);
161 device_del(&codec->dev);
162 }
163}
164EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
165
166/**
Takashi Iwaided255b2015-10-01 17:59:43 +0200167 * snd_hdac_device_set_chip_name - set/update the codec name
168 * @codec: the HDAC device
169 * @name: name string to set
170 *
171 * Returns 0 if the name is set or updated, or a negative error code.
172 */
173int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
174{
175 char *newname;
176
177 if (!name)
178 return 0;
179 newname = kstrdup(name, GFP_KERNEL);
180 if (!newname)
181 return -ENOMEM;
182 kfree(codec->chip_name);
183 codec->chip_name = newname;
184 return 0;
185}
186EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
187
188/**
Takashi Iwai4f9e0c32015-10-16 11:35:49 +0200189 * snd_hdac_codec_modalias - give the module alias name
190 * @codec: HDAC device
191 * @buf: string buffer to store
192 * @size: string buffer size
193 *
194 * Returns the size of string, like snprintf(), or a negative error code.
195 */
196int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
197{
198 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
199 codec->vendor_id, codec->revision_id, codec->type);
200}
201EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
202
203/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100204 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
205 * HD-audio controller
206 * @codec: the codec object
207 * @nid: NID to encode
208 * @verb: verb to encode
209 * @parm: parameter to encode
210 *
211 * Return an encoded command verb or -1 for error.
212 */
213unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
214 unsigned int verb, unsigned int parm)
215{
216 u32 val, addr;
217
218 addr = codec->addr;
219 if ((addr & ~0xf) || (nid & ~0x7f) ||
220 (verb & ~0xfff) || (parm & ~0xffff)) {
221 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
222 addr, nid, verb, parm);
223 return -1;
224 }
225
226 val = addr << 28;
227 val |= (u32)nid << 20;
228 val |= verb << 8;
229 val |= parm;
230 return val;
231}
232EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
233
234/**
Takashi Iwai05852442015-03-03 15:40:08 +0100235 * snd_hdac_exec_verb - execute an encoded verb
236 * @codec: the codec object
237 * @cmd: encoded verb to execute
238 * @flags: optional flags, pass zero for default
239 * @res: the pointer to store the result, NULL if running async
240 *
241 * Returns zero if successful, or a negative error code.
242 *
243 * This calls the exec_verb op when set in hdac_codec. If not,
244 * call the default snd_hdac_bus_exec_verb().
245 */
246int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
247 unsigned int flags, unsigned int *res)
248{
249 if (codec->exec_verb)
250 return codec->exec_verb(codec, cmd, flags, res);
251 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
252}
253EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
254
255
256/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100257 * snd_hdac_read - execute a verb
258 * @codec: the codec object
259 * @nid: NID to execute a verb
260 * @verb: verb to execute
261 * @parm: parameter for a verb
262 * @res: the pointer to store the result, NULL if running async
263 *
264 * Returns zero if successful, or a negative error code.
265 */
266int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
267 unsigned int verb, unsigned int parm, unsigned int *res)
268{
269 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
270
Takashi Iwai05852442015-03-03 15:40:08 +0100271 return snd_hdac_exec_verb(codec, cmd, 0, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100272}
273EXPORT_SYMBOL_GPL(snd_hdac_read);
274
275/**
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100276 * _snd_hdac_read_parm - read a parmeter
Takashi Iwai7639a062015-03-03 10:07:24 +0100277 *
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100278 * This function returns zero or an error unlike snd_hdac_read_parm().
Takashi Iwai7639a062015-03-03 10:07:24 +0100279 */
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100280int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
281 unsigned int *res)
Takashi Iwai7639a062015-03-03 10:07:24 +0100282{
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100283 unsigned int cmd;
Takashi Iwai7639a062015-03-03 10:07:24 +0100284
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100285 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
286 return snd_hdac_regmap_read_raw(codec, cmd, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100287}
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100288EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100289
290/**
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100291 * snd_hdac_read_parm_uncached - read a codec parameter without caching
Takashi Iwai7639a062015-03-03 10:07:24 +0100292 * @codec: the codec object
293 * @nid: NID to read a parameter
294 * @parm: parameter to read
295 *
296 * Returns -1 for error. If you need to distinguish the error more
297 * strictly, use snd_hdac_read() directly.
298 */
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100299int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
300 int parm)
Takashi Iwai7639a062015-03-03 10:07:24 +0100301{
302 int val;
303
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100304 if (codec->regmap)
305 regcache_cache_bypass(codec->regmap, true);
306 val = snd_hdac_read_parm(codec, nid, parm);
307 if (codec->regmap)
308 regcache_cache_bypass(codec->regmap, false);
Takashi Iwai7639a062015-03-03 10:07:24 +0100309 return val;
310}
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100311EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
312
313/**
Takashi Iwaifaa75f82015-02-26 08:54:56 +0100314 * snd_hdac_override_parm - override read-only parameters
315 * @codec: the codec object
316 * @nid: NID for the parameter
317 * @parm: the parameter to change
318 * @val: the parameter value to overwrite
319 */
320int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
321 unsigned int parm, unsigned int val)
322{
323 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
324 int err;
325
326 if (!codec->regmap)
327 return -EINVAL;
328
329 codec->caps_overwriting = true;
330 err = snd_hdac_regmap_write_raw(codec, verb, val);
331 codec->caps_overwriting = false;
332 return err;
333}
334EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100335
336/**
337 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
338 * @codec: the codec object
339 * @nid: NID to inspect
340 * @start_id: the pointer to store the starting NID
341 *
342 * Returns the number of subtree nodes or zero if not found.
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100343 * This function reads parameters always without caching.
Takashi Iwai7639a062015-03-03 10:07:24 +0100344 */
345int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
346 hda_nid_t *start_id)
347{
348 unsigned int parm;
349
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100350 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
Takashi Iwai7639a062015-03-03 10:07:24 +0100351 if (parm == -1) {
352 *start_id = 0;
353 return 0;
354 }
355 *start_id = (parm >> 16) & 0x7fff;
356 return (int)(parm & 0x7fff);
357}
358EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
359
360/*
361 * look for an AFG and MFG nodes
362 */
363static void setup_fg_nodes(struct hdac_device *codec)
364{
365 int i, total_nodes, function_id;
366 hda_nid_t nid;
367
368 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
369 for (i = 0; i < total_nodes; i++, nid++) {
370 function_id = snd_hdac_read_parm(codec, nid,
371 AC_PAR_FUNCTION_TYPE);
372 switch (function_id & 0xff) {
373 case AC_GRP_AUDIO_FUNCTION:
374 codec->afg = nid;
375 codec->afg_function_id = function_id & 0xff;
376 codec->afg_unsol = (function_id >> 8) & 1;
377 break;
378 case AC_GRP_MODEM_FUNCTION:
379 codec->mfg = nid;
380 codec->mfg_function_id = function_id & 0xff;
381 codec->mfg_unsol = (function_id >> 8) & 1;
382 break;
383 default:
384 break;
385 }
386 }
387}
388
389/**
390 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
391 * @codec: the codec object
392 */
393int snd_hdac_refresh_widgets(struct hdac_device *codec)
394{
395 hda_nid_t start_nid;
396 int nums;
397
398 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
399 if (!start_nid || nums <= 0 || nums >= 0xff) {
400 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
401 codec->afg);
402 return -EINVAL;
403 }
404
405 codec->num_nodes = nums;
406 codec->start_nid = start_nid;
407 codec->end_nid = start_nid + nums;
408 return 0;
409}
410EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
411
Vinod Koul18dfd792015-08-21 15:47:43 +0530412/**
413 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
414 * codec sysfs
415 * @codec: the codec object
416 *
417 * first we need to remove sysfs, then refresh widgets and lastly
418 * recreate it
419 */
420int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
421{
422 int ret;
423
Takashi Iwaia92d5ee2015-08-26 07:22:49 +0200424 if (device_is_registered(&codec->dev))
425 hda_widget_sysfs_exit(codec);
Vinod Koul18dfd792015-08-21 15:47:43 +0530426 ret = snd_hdac_refresh_widgets(codec);
427 if (ret) {
428 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
429 return ret;
430 }
Takashi Iwaia92d5ee2015-08-26 07:22:49 +0200431 if (device_is_registered(&codec->dev)) {
432 ret = hda_widget_sysfs_init(codec);
433 if (ret) {
434 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
435 return ret;
436 }
Vinod Koul18dfd792015-08-21 15:47:43 +0530437 }
Vinod Koul18dfd792015-08-21 15:47:43 +0530438 return ret;
439}
440EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
441
Takashi Iwai7639a062015-03-03 10:07:24 +0100442/* return CONNLIST_LEN parameter of the given widget */
443static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
444{
445 unsigned int wcaps = get_wcaps(codec, nid);
446 unsigned int parm;
447
448 if (!(wcaps & AC_WCAP_CONN_LIST) &&
449 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
450 return 0;
451
452 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
453 if (parm == -1)
454 parm = 0;
455 return parm;
456}
457
458/**
459 * snd_hdac_get_connections - get a widget connection list
460 * @codec: the codec object
461 * @nid: NID
462 * @conn_list: the array to store the results, can be NULL
463 * @max_conns: the max size of the given array
464 *
465 * Returns the number of connected widgets, zero for no connection, or a
466 * negative error code. When the number of elements don't fit with the
467 * given array size, it returns -ENOSPC.
468 *
469 * When @conn_list is NULL, it just checks the number of connections.
470 */
471int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
472 hda_nid_t *conn_list, int max_conns)
473{
474 unsigned int parm;
475 int i, conn_len, conns, err;
476 unsigned int shift, num_elems, mask;
477 hda_nid_t prev_nid;
478 int null_count = 0;
479
480 parm = get_num_conns(codec, nid);
481 if (!parm)
482 return 0;
483
484 if (parm & AC_CLIST_LONG) {
485 /* long form */
486 shift = 16;
487 num_elems = 2;
488 } else {
489 /* short form */
490 shift = 8;
491 num_elems = 4;
492 }
493 conn_len = parm & AC_CLIST_LENGTH;
494 mask = (1 << (shift-1)) - 1;
495
496 if (!conn_len)
497 return 0; /* no connection */
498
499 if (conn_len == 1) {
500 /* single connection */
501 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
502 &parm);
503 if (err < 0)
504 return err;
505 if (conn_list)
506 conn_list[0] = parm & mask;
507 return 1;
508 }
509
510 /* multi connection */
511 conns = 0;
512 prev_nid = 0;
513 for (i = 0; i < conn_len; i++) {
514 int range_val;
515 hda_nid_t val, n;
516
517 if (i % num_elems == 0) {
518 err = snd_hdac_read(codec, nid,
519 AC_VERB_GET_CONNECT_LIST, i,
520 &parm);
521 if (err < 0)
522 return -EIO;
523 }
524 range_val = !!(parm & (1 << (shift-1))); /* ranges */
525 val = parm & mask;
526 if (val == 0 && null_count++) { /* no second chance */
527 dev_dbg(&codec->dev,
528 "invalid CONNECT_LIST verb %x[%i]:%x\n",
529 nid, i, parm);
530 return 0;
531 }
532 parm >>= shift;
533 if (range_val) {
534 /* ranges between the previous and this one */
535 if (!prev_nid || prev_nid >= val) {
536 dev_warn(&codec->dev,
537 "invalid dep_range_val %x:%x\n",
538 prev_nid, val);
539 continue;
540 }
541 for (n = prev_nid + 1; n <= val; n++) {
542 if (conn_list) {
543 if (conns >= max_conns)
544 return -ENOSPC;
545 conn_list[conns] = n;
546 }
547 conns++;
548 }
549 } else {
550 if (conn_list) {
551 if (conns >= max_conns)
552 return -ENOSPC;
553 conn_list[conns] = val;
554 }
555 conns++;
556 }
557 prev_nid = val;
558 }
559 return conns;
560}
561EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
562
563#ifdef CONFIG_PM
564/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200565 * snd_hdac_power_up - power up the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100566 * @codec: the codec object
Takashi Iwai664c7152015-04-08 11:43:14 +0200567 *
568 * This function calls the runtime PM helper to power up the given codec.
569 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
570 * path that isn't included in PM path. Otherwise it gets stuck.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200571 *
572 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100573 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200574int snd_hdac_power_up(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100575{
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200576 return pm_runtime_get_sync(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100577}
578EXPORT_SYMBOL_GPL(snd_hdac_power_up);
579
580/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200581 * snd_hdac_power_down - power down the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100582 * @codec: the codec object
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200583 *
584 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100585 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200586int snd_hdac_power_down(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100587{
588 struct device *dev = &codec->dev;
589
Takashi Iwai7639a062015-03-03 10:07:24 +0100590 pm_runtime_mark_last_busy(dev);
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200591 return pm_runtime_put_autosuspend(dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100592}
593EXPORT_SYMBOL_GPL(snd_hdac_power_down);
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200594
595/**
596 * snd_hdac_power_up_pm - power up the codec
597 * @codec: the codec object
598 *
599 * This function can be called in a recursive code path like init code
600 * which may be called by PM suspend/resume again. OTOH, if a power-up
601 * call must wake up the sleeper (e.g. in a kctl callback), use
602 * snd_hdac_power_up() instead.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200603 *
604 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200605 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200606int snd_hdac_power_up_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200607{
608 if (!atomic_inc_not_zero(&codec->in_pm))
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200609 return snd_hdac_power_up(codec);
610 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200611}
612EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
613
614/**
615 * snd_hdac_power_down_pm - power down the codec
616 * @codec: the codec object
617 *
618 * Like snd_hdac_power_up_pm(), this function is used in a recursive
619 * code path like init code which may be called by PM suspend/resume again.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200620 *
621 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200622 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200623int snd_hdac_power_down_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200624{
625 if (atomic_dec_if_positive(&codec->in_pm) < 0)
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200626 return snd_hdac_power_down(codec);
627 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200628}
629EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100630#endif
631
Takashi Iwai78dd5e22015-10-28 12:26:48 +0100632/**
633 * snd_hdac_link_power - Enable/disable the link power for a codec
634 * @codec: the codec object
635 * @bool: enable or disable the link power
Mengdong Lina5e7e072015-04-29 17:43:20 +0800636 */
637int snd_hdac_link_power(struct hdac_device *codec, bool enable)
638{
639 if (!codec->link_power_control)
640 return 0;
641
642 if (codec->bus->ops->link_power)
643 return codec->bus->ops->link_power(codec->bus, enable);
644 else
645 return -EINVAL;
646}
647EXPORT_SYMBOL_GPL(snd_hdac_link_power);
648
Takashi Iwai7639a062015-03-03 10:07:24 +0100649/* codec vendor labels */
650struct hda_vendor_id {
651 unsigned int id;
652 const char *name;
653};
654
655static struct hda_vendor_id hda_vendor_ids[] = {
656 { 0x1002, "ATI" },
657 { 0x1013, "Cirrus Logic" },
658 { 0x1057, "Motorola" },
659 { 0x1095, "Silicon Image" },
660 { 0x10de, "Nvidia" },
661 { 0x10ec, "Realtek" },
662 { 0x1102, "Creative" },
663 { 0x1106, "VIA" },
664 { 0x111d, "IDT" },
665 { 0x11c1, "LSI" },
666 { 0x11d4, "Analog Devices" },
667 { 0x13f6, "C-Media" },
668 { 0x14f1, "Conexant" },
669 { 0x17e8, "Chrontel" },
670 { 0x1854, "LG" },
671 { 0x1aec, "Wolfson Microelectronics" },
672 { 0x1af4, "QEMU" },
673 { 0x434d, "C-Media" },
674 { 0x8086, "Intel" },
675 { 0x8384, "SigmaTel" },
676 {} /* terminator */
677};
678
679/* store the codec vendor name */
680static int get_codec_vendor_name(struct hdac_device *codec)
681{
682 const struct hda_vendor_id *c;
683 u16 vendor_id = codec->vendor_id >> 16;
684
685 for (c = hda_vendor_ids; c->id; c++) {
686 if (c->id == vendor_id) {
687 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
688 return codec->vendor_name ? 0 : -ENOMEM;
689 }
690 }
691
692 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
693 return codec->vendor_name ? 0 : -ENOMEM;
694}
Takashi Iwaib7d023e2015-04-16 08:19:06 +0200695
696/*
697 * stream formats
698 */
699struct hda_rate_tbl {
700 unsigned int hz;
701 unsigned int alsa_bits;
702 unsigned int hda_fmt;
703};
704
705/* rate = base * mult / div */
706#define HDA_RATE(base, mult, div) \
707 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
708 (((div) - 1) << AC_FMT_DIV_SHIFT))
709
710static struct hda_rate_tbl rate_bits[] = {
711 /* rate in Hz, ALSA rate bitmask, HDA format value */
712
713 /* autodetected value used in snd_hda_query_supported_pcm */
714 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
715 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
716 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
717 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
718 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
719 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
720 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
721 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
722 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
723 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
724 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
725#define AC_PAR_PCM_RATE_BITS 11
726 /* up to bits 10, 384kHZ isn't supported properly */
727
728 /* not autodetected value */
729 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
730
731 { 0 } /* terminator */
732};
733
734/**
735 * snd_hdac_calc_stream_format - calculate the format bitset
736 * @rate: the sample rate
737 * @channels: the number of channels
738 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
739 * @maxbps: the max. bps
740 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
741 *
742 * Calculate the format bitset from the given rate, channels and th PCM format.
743 *
744 * Return zero if invalid.
745 */
746unsigned int snd_hdac_calc_stream_format(unsigned int rate,
747 unsigned int channels,
748 unsigned int format,
749 unsigned int maxbps,
750 unsigned short spdif_ctls)
751{
752 int i;
753 unsigned int val = 0;
754
755 for (i = 0; rate_bits[i].hz; i++)
756 if (rate_bits[i].hz == rate) {
757 val = rate_bits[i].hda_fmt;
758 break;
759 }
760 if (!rate_bits[i].hz)
761 return 0;
762
763 if (channels == 0 || channels > 8)
764 return 0;
765 val |= channels - 1;
766
767 switch (snd_pcm_format_width(format)) {
768 case 8:
769 val |= AC_FMT_BITS_8;
770 break;
771 case 16:
772 val |= AC_FMT_BITS_16;
773 break;
774 case 20:
775 case 24:
776 case 32:
777 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
778 val |= AC_FMT_BITS_32;
779 else if (maxbps >= 24)
780 val |= AC_FMT_BITS_24;
781 else
782 val |= AC_FMT_BITS_20;
783 break;
784 default:
785 return 0;
786 }
787
788 if (spdif_ctls & AC_DIG1_NONAUDIO)
789 val |= AC_FMT_TYPE_NON_PCM;
790
791 return val;
792}
793EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
794
795static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
796{
797 unsigned int val = 0;
798
799 if (nid != codec->afg &&
800 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
801 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
802 if (!val || val == -1)
803 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
804 if (!val || val == -1)
805 return 0;
806 return val;
807}
808
809static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
810{
811 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
812
813 if (!streams || streams == -1)
814 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
815 if (!streams || streams == -1)
816 return 0;
817 return streams;
818}
819
820/**
821 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
822 * @codec: the codec object
823 * @nid: NID to query
824 * @ratesp: the pointer to store the detected rate bitflags
825 * @formatsp: the pointer to store the detected formats
826 * @bpsp: the pointer to store the detected format widths
827 *
828 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
829 * or @bsps argument is ignored.
830 *
831 * Returns 0 if successful, otherwise a negative error code.
832 */
833int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
834 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
835{
836 unsigned int i, val, wcaps;
837
838 wcaps = get_wcaps(codec, nid);
839 val = query_pcm_param(codec, nid);
840
841 if (ratesp) {
842 u32 rates = 0;
843 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
844 if (val & (1 << i))
845 rates |= rate_bits[i].alsa_bits;
846 }
847 if (rates == 0) {
848 dev_err(&codec->dev,
849 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
850 nid, val,
851 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
852 return -EIO;
853 }
854 *ratesp = rates;
855 }
856
857 if (formatsp || bpsp) {
858 u64 formats = 0;
859 unsigned int streams, bps;
860
861 streams = query_stream_param(codec, nid);
862 if (!streams)
863 return -EIO;
864
865 bps = 0;
866 if (streams & AC_SUPFMT_PCM) {
867 if (val & AC_SUPPCM_BITS_8) {
868 formats |= SNDRV_PCM_FMTBIT_U8;
869 bps = 8;
870 }
871 if (val & AC_SUPPCM_BITS_16) {
872 formats |= SNDRV_PCM_FMTBIT_S16_LE;
873 bps = 16;
874 }
875 if (wcaps & AC_WCAP_DIGITAL) {
876 if (val & AC_SUPPCM_BITS_32)
877 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
878 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
879 formats |= SNDRV_PCM_FMTBIT_S32_LE;
880 if (val & AC_SUPPCM_BITS_24)
881 bps = 24;
882 else if (val & AC_SUPPCM_BITS_20)
883 bps = 20;
884 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
885 AC_SUPPCM_BITS_32)) {
886 formats |= SNDRV_PCM_FMTBIT_S32_LE;
887 if (val & AC_SUPPCM_BITS_32)
888 bps = 32;
889 else if (val & AC_SUPPCM_BITS_24)
890 bps = 24;
891 else if (val & AC_SUPPCM_BITS_20)
892 bps = 20;
893 }
894 }
895#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
896 if (streams & AC_SUPFMT_FLOAT32) {
897 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
898 if (!bps)
899 bps = 32;
900 }
901#endif
902 if (streams == AC_SUPFMT_AC3) {
903 /* should be exclusive */
904 /* temporary hack: we have still no proper support
905 * for the direct AC3 stream...
906 */
907 formats |= SNDRV_PCM_FMTBIT_U8;
908 bps = 8;
909 }
910 if (formats == 0) {
911 dev_err(&codec->dev,
912 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
913 nid, val,
914 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
915 streams);
916 return -EIO;
917 }
918 if (formatsp)
919 *formatsp = formats;
920 if (bpsp)
921 *bpsp = bps;
922 }
923
924 return 0;
925}
926EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
927
928/**
929 * snd_hdac_is_supported_format - Check the validity of the format
930 * @codec: the codec object
931 * @nid: NID to check
932 * @format: the HD-audio format value to check
933 *
934 * Check whether the given node supports the format value.
935 *
936 * Returns true if supported, false if not.
937 */
938bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
939 unsigned int format)
940{
941 int i;
942 unsigned int val = 0, rate, stream;
943
944 val = query_pcm_param(codec, nid);
945 if (!val)
946 return false;
947
948 rate = format & 0xff00;
949 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
950 if (rate_bits[i].hda_fmt == rate) {
951 if (val & (1 << i))
952 break;
953 return false;
954 }
955 if (i >= AC_PAR_PCM_RATE_BITS)
956 return false;
957
958 stream = query_stream_param(codec, nid);
959 if (!stream)
960 return false;
961
962 if (stream & AC_SUPFMT_PCM) {
963 switch (format & 0xf0) {
964 case 0x00:
965 if (!(val & AC_SUPPCM_BITS_8))
966 return false;
967 break;
968 case 0x10:
969 if (!(val & AC_SUPPCM_BITS_16))
970 return false;
971 break;
972 case 0x20:
973 if (!(val & AC_SUPPCM_BITS_20))
974 return false;
975 break;
976 case 0x30:
977 if (!(val & AC_SUPPCM_BITS_24))
978 return false;
979 break;
980 case 0x40:
981 if (!(val & AC_SUPPCM_BITS_32))
982 return false;
983 break;
984 default:
985 return false;
986 }
987 } else {
988 /* FIXME: check for float32 and AC3? */
989 }
990
991 return true;
992}
993EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
Subhransu S. Prusty1b5e6162015-10-08 09:48:05 +0100994
995static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
996 int flags, unsigned int verb, unsigned int parm)
997{
998 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
999 unsigned int res;
1000
1001 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1002 return -1;
1003
1004 return res;
1005}
1006
1007static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1008 int flags, unsigned int verb, unsigned int parm)
1009{
1010 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1011
1012 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1013}
1014
1015/**
1016 * snd_hdac_codec_read - send a command and get the response
1017 * @hdac: the HDAC device
1018 * @nid: NID to send the command
1019 * @flags: optional bit flags
1020 * @verb: the verb to send
1021 * @parm: the parameter for the verb
1022 *
1023 * Send a single command and read the corresponding response.
1024 *
1025 * Returns the obtained response value, or -1 for an error.
1026 */
1027int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1028 int flags, unsigned int verb, unsigned int parm)
1029{
1030 return codec_read(hdac, nid, flags, verb, parm);
1031}
1032EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1033
1034/**
1035 * snd_hdac_codec_write - send a single command without waiting for response
1036 * @hdac: the HDAC device
1037 * @nid: NID to send the command
1038 * @flags: optional bit flags
1039 * @verb: the verb to send
1040 * @parm: the parameter for the verb
1041 *
1042 * Send a single command without waiting for response.
1043 *
1044 * Returns 0 if successful, or a negative error code.
1045 */
1046int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1047 int flags, unsigned int verb, unsigned int parm)
1048{
1049 return codec_write(hdac, nid, flags, verb, parm);
1050}
1051EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1052
Takashi Iwai78dd5e22015-10-28 12:26:48 +01001053/**
1054 * snd_hdac_check_power_state - check whether the actual power state matches
Subhransu S. Prusty1b5e6162015-10-08 09:48:05 +01001055 * with the target state
1056 *
1057 * @hdac: the HDAC device
1058 * @nid: NID to send the command
1059 * @target_state: target state to check for
1060 *
1061 * Return true if state matches, false if not
1062 */
1063bool snd_hdac_check_power_state(struct hdac_device *hdac,
1064 hda_nid_t nid, unsigned int target_state)
1065{
1066 unsigned int state = codec_read(hdac, nid, 0,
1067 AC_VERB_GET_POWER_STATE, 0);
1068
1069 if (state & AC_PWRST_ERROR)
1070 return true;
1071 state = (state >> 4) & 0x0f;
1072 return (state == target_state);
1073}
1074EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);