blob: 4b06b26cee06411d490d8ff49c1c2429f033dc4c [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 Iwai7639a062015-03-03 10:07:24 +0100189 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
190 * HD-audio controller
191 * @codec: the codec object
192 * @nid: NID to encode
193 * @verb: verb to encode
194 * @parm: parameter to encode
195 *
196 * Return an encoded command verb or -1 for error.
197 */
198unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
199 unsigned int verb, unsigned int parm)
200{
201 u32 val, addr;
202
203 addr = codec->addr;
204 if ((addr & ~0xf) || (nid & ~0x7f) ||
205 (verb & ~0xfff) || (parm & ~0xffff)) {
206 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
207 addr, nid, verb, parm);
208 return -1;
209 }
210
211 val = addr << 28;
212 val |= (u32)nid << 20;
213 val |= verb << 8;
214 val |= parm;
215 return val;
216}
217EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
218
219/**
Takashi Iwai05852442015-03-03 15:40:08 +0100220 * snd_hdac_exec_verb - execute an encoded verb
221 * @codec: the codec object
222 * @cmd: encoded verb to execute
223 * @flags: optional flags, pass zero for default
224 * @res: the pointer to store the result, NULL if running async
225 *
226 * Returns zero if successful, or a negative error code.
227 *
228 * This calls the exec_verb op when set in hdac_codec. If not,
229 * call the default snd_hdac_bus_exec_verb().
230 */
231int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
232 unsigned int flags, unsigned int *res)
233{
234 if (codec->exec_verb)
235 return codec->exec_verb(codec, cmd, flags, res);
236 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
237}
238EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
239
240
241/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100242 * snd_hdac_read - execute a verb
243 * @codec: the codec object
244 * @nid: NID to execute a verb
245 * @verb: verb to execute
246 * @parm: parameter for a verb
247 * @res: the pointer to store the result, NULL if running async
248 *
249 * Returns zero if successful, or a negative error code.
250 */
251int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
252 unsigned int verb, unsigned int parm, unsigned int *res)
253{
254 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
255
Takashi Iwai05852442015-03-03 15:40:08 +0100256 return snd_hdac_exec_verb(codec, cmd, 0, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100257}
258EXPORT_SYMBOL_GPL(snd_hdac_read);
259
260/**
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100261 * _snd_hdac_read_parm - read a parmeter
Takashi Iwai7639a062015-03-03 10:07:24 +0100262 *
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100263 * This function returns zero or an error unlike snd_hdac_read_parm().
Takashi Iwai7639a062015-03-03 10:07:24 +0100264 */
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100265int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
266 unsigned int *res)
Takashi Iwai7639a062015-03-03 10:07:24 +0100267{
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100268 unsigned int cmd;
Takashi Iwai7639a062015-03-03 10:07:24 +0100269
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100270 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
271 return snd_hdac_regmap_read_raw(codec, cmd, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100272}
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100273EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100274
275/**
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100276 * snd_hdac_read_parm_uncached - read a codec parameter without caching
Takashi Iwai7639a062015-03-03 10:07:24 +0100277 * @codec: the codec object
278 * @nid: NID to read a parameter
279 * @parm: parameter to read
280 *
281 * Returns -1 for error. If you need to distinguish the error more
282 * strictly, use snd_hdac_read() directly.
283 */
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100284int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
285 int parm)
Takashi Iwai7639a062015-03-03 10:07:24 +0100286{
287 int val;
288
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100289 if (codec->regmap)
290 regcache_cache_bypass(codec->regmap, true);
291 val = snd_hdac_read_parm(codec, nid, parm);
292 if (codec->regmap)
293 regcache_cache_bypass(codec->regmap, false);
Takashi Iwai7639a062015-03-03 10:07:24 +0100294 return val;
295}
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100296EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
297
298/**
Takashi Iwaifaa75f82015-02-26 08:54:56 +0100299 * snd_hdac_override_parm - override read-only parameters
300 * @codec: the codec object
301 * @nid: NID for the parameter
302 * @parm: the parameter to change
303 * @val: the parameter value to overwrite
304 */
305int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
306 unsigned int parm, unsigned int val)
307{
308 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
309 int err;
310
311 if (!codec->regmap)
312 return -EINVAL;
313
314 codec->caps_overwriting = true;
315 err = snd_hdac_regmap_write_raw(codec, verb, val);
316 codec->caps_overwriting = false;
317 return err;
318}
319EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100320
321/**
322 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
323 * @codec: the codec object
324 * @nid: NID to inspect
325 * @start_id: the pointer to store the starting NID
326 *
327 * Returns the number of subtree nodes or zero if not found.
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100328 * This function reads parameters always without caching.
Takashi Iwai7639a062015-03-03 10:07:24 +0100329 */
330int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
331 hda_nid_t *start_id)
332{
333 unsigned int parm;
334
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100335 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
Takashi Iwai7639a062015-03-03 10:07:24 +0100336 if (parm == -1) {
337 *start_id = 0;
338 return 0;
339 }
340 *start_id = (parm >> 16) & 0x7fff;
341 return (int)(parm & 0x7fff);
342}
343EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
344
345/*
346 * look for an AFG and MFG nodes
347 */
348static void setup_fg_nodes(struct hdac_device *codec)
349{
350 int i, total_nodes, function_id;
351 hda_nid_t nid;
352
353 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
354 for (i = 0; i < total_nodes; i++, nid++) {
355 function_id = snd_hdac_read_parm(codec, nid,
356 AC_PAR_FUNCTION_TYPE);
357 switch (function_id & 0xff) {
358 case AC_GRP_AUDIO_FUNCTION:
359 codec->afg = nid;
360 codec->afg_function_id = function_id & 0xff;
361 codec->afg_unsol = (function_id >> 8) & 1;
362 break;
363 case AC_GRP_MODEM_FUNCTION:
364 codec->mfg = nid;
365 codec->mfg_function_id = function_id & 0xff;
366 codec->mfg_unsol = (function_id >> 8) & 1;
367 break;
368 default:
369 break;
370 }
371 }
372}
373
374/**
375 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
376 * @codec: the codec object
377 */
378int snd_hdac_refresh_widgets(struct hdac_device *codec)
379{
380 hda_nid_t start_nid;
381 int nums;
382
383 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
384 if (!start_nid || nums <= 0 || nums >= 0xff) {
385 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
386 codec->afg);
387 return -EINVAL;
388 }
389
390 codec->num_nodes = nums;
391 codec->start_nid = start_nid;
392 codec->end_nid = start_nid + nums;
393 return 0;
394}
395EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
396
Vinod Koul18dfd792015-08-21 15:47:43 +0530397/**
398 * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
399 * codec sysfs
400 * @codec: the codec object
401 *
402 * first we need to remove sysfs, then refresh widgets and lastly
403 * recreate it
404 */
405int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
406{
407 int ret;
408
Takashi Iwaia92d5ee2015-08-26 07:22:49 +0200409 if (device_is_registered(&codec->dev))
410 hda_widget_sysfs_exit(codec);
Vinod Koul18dfd792015-08-21 15:47:43 +0530411 ret = snd_hdac_refresh_widgets(codec);
412 if (ret) {
413 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
414 return ret;
415 }
Takashi Iwaia92d5ee2015-08-26 07:22:49 +0200416 if (device_is_registered(&codec->dev)) {
417 ret = hda_widget_sysfs_init(codec);
418 if (ret) {
419 dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
420 return ret;
421 }
Vinod Koul18dfd792015-08-21 15:47:43 +0530422 }
Vinod Koul18dfd792015-08-21 15:47:43 +0530423 return ret;
424}
425EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
426
Takashi Iwai7639a062015-03-03 10:07:24 +0100427/* return CONNLIST_LEN parameter of the given widget */
428static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
429{
430 unsigned int wcaps = get_wcaps(codec, nid);
431 unsigned int parm;
432
433 if (!(wcaps & AC_WCAP_CONN_LIST) &&
434 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
435 return 0;
436
437 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
438 if (parm == -1)
439 parm = 0;
440 return parm;
441}
442
443/**
444 * snd_hdac_get_connections - get a widget connection list
445 * @codec: the codec object
446 * @nid: NID
447 * @conn_list: the array to store the results, can be NULL
448 * @max_conns: the max size of the given array
449 *
450 * Returns the number of connected widgets, zero for no connection, or a
451 * negative error code. When the number of elements don't fit with the
452 * given array size, it returns -ENOSPC.
453 *
454 * When @conn_list is NULL, it just checks the number of connections.
455 */
456int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
457 hda_nid_t *conn_list, int max_conns)
458{
459 unsigned int parm;
460 int i, conn_len, conns, err;
461 unsigned int shift, num_elems, mask;
462 hda_nid_t prev_nid;
463 int null_count = 0;
464
465 parm = get_num_conns(codec, nid);
466 if (!parm)
467 return 0;
468
469 if (parm & AC_CLIST_LONG) {
470 /* long form */
471 shift = 16;
472 num_elems = 2;
473 } else {
474 /* short form */
475 shift = 8;
476 num_elems = 4;
477 }
478 conn_len = parm & AC_CLIST_LENGTH;
479 mask = (1 << (shift-1)) - 1;
480
481 if (!conn_len)
482 return 0; /* no connection */
483
484 if (conn_len == 1) {
485 /* single connection */
486 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
487 &parm);
488 if (err < 0)
489 return err;
490 if (conn_list)
491 conn_list[0] = parm & mask;
492 return 1;
493 }
494
495 /* multi connection */
496 conns = 0;
497 prev_nid = 0;
498 for (i = 0; i < conn_len; i++) {
499 int range_val;
500 hda_nid_t val, n;
501
502 if (i % num_elems == 0) {
503 err = snd_hdac_read(codec, nid,
504 AC_VERB_GET_CONNECT_LIST, i,
505 &parm);
506 if (err < 0)
507 return -EIO;
508 }
509 range_val = !!(parm & (1 << (shift-1))); /* ranges */
510 val = parm & mask;
511 if (val == 0 && null_count++) { /* no second chance */
512 dev_dbg(&codec->dev,
513 "invalid CONNECT_LIST verb %x[%i]:%x\n",
514 nid, i, parm);
515 return 0;
516 }
517 parm >>= shift;
518 if (range_val) {
519 /* ranges between the previous and this one */
520 if (!prev_nid || prev_nid >= val) {
521 dev_warn(&codec->dev,
522 "invalid dep_range_val %x:%x\n",
523 prev_nid, val);
524 continue;
525 }
526 for (n = prev_nid + 1; n <= val; n++) {
527 if (conn_list) {
528 if (conns >= max_conns)
529 return -ENOSPC;
530 conn_list[conns] = n;
531 }
532 conns++;
533 }
534 } else {
535 if (conn_list) {
536 if (conns >= max_conns)
537 return -ENOSPC;
538 conn_list[conns] = val;
539 }
540 conns++;
541 }
542 prev_nid = val;
543 }
544 return conns;
545}
546EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
547
548#ifdef CONFIG_PM
549/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200550 * snd_hdac_power_up - power up the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100551 * @codec: the codec object
Takashi Iwai664c7152015-04-08 11:43:14 +0200552 *
553 * This function calls the runtime PM helper to power up the given codec.
554 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
555 * path that isn't included in PM path. Otherwise it gets stuck.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200556 *
557 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100558 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200559int snd_hdac_power_up(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100560{
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200561 return pm_runtime_get_sync(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100562}
563EXPORT_SYMBOL_GPL(snd_hdac_power_up);
564
565/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200566 * snd_hdac_power_down - power down the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100567 * @codec: the codec object
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200568 *
569 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100570 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200571int snd_hdac_power_down(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100572{
573 struct device *dev = &codec->dev;
574
Takashi Iwai7639a062015-03-03 10:07:24 +0100575 pm_runtime_mark_last_busy(dev);
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200576 return pm_runtime_put_autosuspend(dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100577}
578EXPORT_SYMBOL_GPL(snd_hdac_power_down);
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200579
580/**
581 * snd_hdac_power_up_pm - power up the codec
582 * @codec: the codec object
583 *
584 * This function can be called in a recursive code path like init code
585 * which may be called by PM suspend/resume again. OTOH, if a power-up
586 * call must wake up the sleeper (e.g. in a kctl callback), use
587 * snd_hdac_power_up() instead.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200588 *
589 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200590 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200591int snd_hdac_power_up_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200592{
593 if (!atomic_inc_not_zero(&codec->in_pm))
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200594 return snd_hdac_power_up(codec);
595 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200596}
597EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
598
599/**
600 * snd_hdac_power_down_pm - power down the codec
601 * @codec: the codec object
602 *
603 * Like snd_hdac_power_up_pm(), this function is used in a recursive
604 * code path like init code which may be called by PM suspend/resume again.
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200605 *
606 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200607 */
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200608int snd_hdac_power_down_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200609{
610 if (atomic_dec_if_positive(&codec->in_pm) < 0)
Takashi Iwaifbce23a2015-07-17 16:27:33 +0200611 return snd_hdac_power_down(codec);
612 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200613}
614EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100615#endif
616
Mengdong Lina5e7e072015-04-29 17:43:20 +0800617/*
618 * Enable/disable the link power for a codec.
619 */
620int snd_hdac_link_power(struct hdac_device *codec, bool enable)
621{
622 if (!codec->link_power_control)
623 return 0;
624
625 if (codec->bus->ops->link_power)
626 return codec->bus->ops->link_power(codec->bus, enable);
627 else
628 return -EINVAL;
629}
630EXPORT_SYMBOL_GPL(snd_hdac_link_power);
631
Takashi Iwai7639a062015-03-03 10:07:24 +0100632/* codec vendor labels */
633struct hda_vendor_id {
634 unsigned int id;
635 const char *name;
636};
637
638static struct hda_vendor_id hda_vendor_ids[] = {
639 { 0x1002, "ATI" },
640 { 0x1013, "Cirrus Logic" },
641 { 0x1057, "Motorola" },
642 { 0x1095, "Silicon Image" },
643 { 0x10de, "Nvidia" },
644 { 0x10ec, "Realtek" },
645 { 0x1102, "Creative" },
646 { 0x1106, "VIA" },
647 { 0x111d, "IDT" },
648 { 0x11c1, "LSI" },
649 { 0x11d4, "Analog Devices" },
650 { 0x13f6, "C-Media" },
651 { 0x14f1, "Conexant" },
652 { 0x17e8, "Chrontel" },
653 { 0x1854, "LG" },
654 { 0x1aec, "Wolfson Microelectronics" },
655 { 0x1af4, "QEMU" },
656 { 0x434d, "C-Media" },
657 { 0x8086, "Intel" },
658 { 0x8384, "SigmaTel" },
659 {} /* terminator */
660};
661
662/* store the codec vendor name */
663static int get_codec_vendor_name(struct hdac_device *codec)
664{
665 const struct hda_vendor_id *c;
666 u16 vendor_id = codec->vendor_id >> 16;
667
668 for (c = hda_vendor_ids; c->id; c++) {
669 if (c->id == vendor_id) {
670 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
671 return codec->vendor_name ? 0 : -ENOMEM;
672 }
673 }
674
675 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
676 return codec->vendor_name ? 0 : -ENOMEM;
677}
Takashi Iwaib7d023e2015-04-16 08:19:06 +0200678
679/*
680 * stream formats
681 */
682struct hda_rate_tbl {
683 unsigned int hz;
684 unsigned int alsa_bits;
685 unsigned int hda_fmt;
686};
687
688/* rate = base * mult / div */
689#define HDA_RATE(base, mult, div) \
690 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
691 (((div) - 1) << AC_FMT_DIV_SHIFT))
692
693static struct hda_rate_tbl rate_bits[] = {
694 /* rate in Hz, ALSA rate bitmask, HDA format value */
695
696 /* autodetected value used in snd_hda_query_supported_pcm */
697 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
698 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
699 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
700 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
701 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
702 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
703 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
704 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
705 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
706 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
707 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
708#define AC_PAR_PCM_RATE_BITS 11
709 /* up to bits 10, 384kHZ isn't supported properly */
710
711 /* not autodetected value */
712 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
713
714 { 0 } /* terminator */
715};
716
717/**
718 * snd_hdac_calc_stream_format - calculate the format bitset
719 * @rate: the sample rate
720 * @channels: the number of channels
721 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
722 * @maxbps: the max. bps
723 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
724 *
725 * Calculate the format bitset from the given rate, channels and th PCM format.
726 *
727 * Return zero if invalid.
728 */
729unsigned int snd_hdac_calc_stream_format(unsigned int rate,
730 unsigned int channels,
731 unsigned int format,
732 unsigned int maxbps,
733 unsigned short spdif_ctls)
734{
735 int i;
736 unsigned int val = 0;
737
738 for (i = 0; rate_bits[i].hz; i++)
739 if (rate_bits[i].hz == rate) {
740 val = rate_bits[i].hda_fmt;
741 break;
742 }
743 if (!rate_bits[i].hz)
744 return 0;
745
746 if (channels == 0 || channels > 8)
747 return 0;
748 val |= channels - 1;
749
750 switch (snd_pcm_format_width(format)) {
751 case 8:
752 val |= AC_FMT_BITS_8;
753 break;
754 case 16:
755 val |= AC_FMT_BITS_16;
756 break;
757 case 20:
758 case 24:
759 case 32:
760 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
761 val |= AC_FMT_BITS_32;
762 else if (maxbps >= 24)
763 val |= AC_FMT_BITS_24;
764 else
765 val |= AC_FMT_BITS_20;
766 break;
767 default:
768 return 0;
769 }
770
771 if (spdif_ctls & AC_DIG1_NONAUDIO)
772 val |= AC_FMT_TYPE_NON_PCM;
773
774 return val;
775}
776EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
777
778static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
779{
780 unsigned int val = 0;
781
782 if (nid != codec->afg &&
783 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
784 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
785 if (!val || val == -1)
786 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
787 if (!val || val == -1)
788 return 0;
789 return val;
790}
791
792static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
793{
794 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
795
796 if (!streams || streams == -1)
797 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
798 if (!streams || streams == -1)
799 return 0;
800 return streams;
801}
802
803/**
804 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
805 * @codec: the codec object
806 * @nid: NID to query
807 * @ratesp: the pointer to store the detected rate bitflags
808 * @formatsp: the pointer to store the detected formats
809 * @bpsp: the pointer to store the detected format widths
810 *
811 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
812 * or @bsps argument is ignored.
813 *
814 * Returns 0 if successful, otherwise a negative error code.
815 */
816int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
817 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
818{
819 unsigned int i, val, wcaps;
820
821 wcaps = get_wcaps(codec, nid);
822 val = query_pcm_param(codec, nid);
823
824 if (ratesp) {
825 u32 rates = 0;
826 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
827 if (val & (1 << i))
828 rates |= rate_bits[i].alsa_bits;
829 }
830 if (rates == 0) {
831 dev_err(&codec->dev,
832 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
833 nid, val,
834 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
835 return -EIO;
836 }
837 *ratesp = rates;
838 }
839
840 if (formatsp || bpsp) {
841 u64 formats = 0;
842 unsigned int streams, bps;
843
844 streams = query_stream_param(codec, nid);
845 if (!streams)
846 return -EIO;
847
848 bps = 0;
849 if (streams & AC_SUPFMT_PCM) {
850 if (val & AC_SUPPCM_BITS_8) {
851 formats |= SNDRV_PCM_FMTBIT_U8;
852 bps = 8;
853 }
854 if (val & AC_SUPPCM_BITS_16) {
855 formats |= SNDRV_PCM_FMTBIT_S16_LE;
856 bps = 16;
857 }
858 if (wcaps & AC_WCAP_DIGITAL) {
859 if (val & AC_SUPPCM_BITS_32)
860 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
861 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
862 formats |= SNDRV_PCM_FMTBIT_S32_LE;
863 if (val & AC_SUPPCM_BITS_24)
864 bps = 24;
865 else if (val & AC_SUPPCM_BITS_20)
866 bps = 20;
867 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
868 AC_SUPPCM_BITS_32)) {
869 formats |= SNDRV_PCM_FMTBIT_S32_LE;
870 if (val & AC_SUPPCM_BITS_32)
871 bps = 32;
872 else if (val & AC_SUPPCM_BITS_24)
873 bps = 24;
874 else if (val & AC_SUPPCM_BITS_20)
875 bps = 20;
876 }
877 }
878#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
879 if (streams & AC_SUPFMT_FLOAT32) {
880 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
881 if (!bps)
882 bps = 32;
883 }
884#endif
885 if (streams == AC_SUPFMT_AC3) {
886 /* should be exclusive */
887 /* temporary hack: we have still no proper support
888 * for the direct AC3 stream...
889 */
890 formats |= SNDRV_PCM_FMTBIT_U8;
891 bps = 8;
892 }
893 if (formats == 0) {
894 dev_err(&codec->dev,
895 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
896 nid, val,
897 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
898 streams);
899 return -EIO;
900 }
901 if (formatsp)
902 *formatsp = formats;
903 if (bpsp)
904 *bpsp = bps;
905 }
906
907 return 0;
908}
909EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
910
911/**
912 * snd_hdac_is_supported_format - Check the validity of the format
913 * @codec: the codec object
914 * @nid: NID to check
915 * @format: the HD-audio format value to check
916 *
917 * Check whether the given node supports the format value.
918 *
919 * Returns true if supported, false if not.
920 */
921bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
922 unsigned int format)
923{
924 int i;
925 unsigned int val = 0, rate, stream;
926
927 val = query_pcm_param(codec, nid);
928 if (!val)
929 return false;
930
931 rate = format & 0xff00;
932 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
933 if (rate_bits[i].hda_fmt == rate) {
934 if (val & (1 << i))
935 break;
936 return false;
937 }
938 if (i >= AC_PAR_PCM_RATE_BITS)
939 return false;
940
941 stream = query_stream_param(codec, nid);
942 if (!stream)
943 return false;
944
945 if (stream & AC_SUPFMT_PCM) {
946 switch (format & 0xf0) {
947 case 0x00:
948 if (!(val & AC_SUPPCM_BITS_8))
949 return false;
950 break;
951 case 0x10:
952 if (!(val & AC_SUPPCM_BITS_16))
953 return false;
954 break;
955 case 0x20:
956 if (!(val & AC_SUPPCM_BITS_20))
957 return false;
958 break;
959 case 0x30:
960 if (!(val & AC_SUPPCM_BITS_24))
961 return false;
962 break;
963 case 0x40:
964 if (!(val & AC_SUPPCM_BITS_32))
965 return false;
966 break;
967 default:
968 return false;
969 }
970 } else {
971 /* FIXME: check for float32 and AC3? */
972 }
973
974 return true;
975}
976EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
Subhransu S. Prusty1b5e6162015-10-08 09:48:05 +0100977
978static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
979 int flags, unsigned int verb, unsigned int parm)
980{
981 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
982 unsigned int res;
983
984 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
985 return -1;
986
987 return res;
988}
989
990static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
991 int flags, unsigned int verb, unsigned int parm)
992{
993 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
994
995 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
996}
997
998/**
999 * snd_hdac_codec_read - send a command and get the response
1000 * @hdac: the HDAC device
1001 * @nid: NID to send the command
1002 * @flags: optional bit flags
1003 * @verb: the verb to send
1004 * @parm: the parameter for the verb
1005 *
1006 * Send a single command and read the corresponding response.
1007 *
1008 * Returns the obtained response value, or -1 for an error.
1009 */
1010int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1011 int flags, unsigned int verb, unsigned int parm)
1012{
1013 return codec_read(hdac, nid, flags, verb, parm);
1014}
1015EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1016
1017/**
1018 * snd_hdac_codec_write - send a single command without waiting for response
1019 * @hdac: the HDAC device
1020 * @nid: NID to send the command
1021 * @flags: optional bit flags
1022 * @verb: the verb to send
1023 * @parm: the parameter for the verb
1024 *
1025 * Send a single command without waiting for response.
1026 *
1027 * Returns 0 if successful, or a negative error code.
1028 */
1029int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1030 int flags, unsigned int verb, unsigned int parm)
1031{
1032 return codec_write(hdac, nid, flags, verb, parm);
1033}
1034EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1035
1036/*
1037 * snd_hdac_check_power_state: check whether the actual power state matches
1038 * with the target state
1039 *
1040 * @hdac: the HDAC device
1041 * @nid: NID to send the command
1042 * @target_state: target state to check for
1043 *
1044 * Return true if state matches, false if not
1045 */
1046bool snd_hdac_check_power_state(struct hdac_device *hdac,
1047 hda_nid_t nid, unsigned int target_state)
1048{
1049 unsigned int state = codec_read(hdac, nid, 0,
1050 AC_VERB_GET_POWER_STATE, 0);
1051
1052 if (state & AC_PWRST_ERROR)
1053 return true;
1054 state = (state >> 4) & 0x0f;
1055 return (state == target_state);
1056}
1057EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);