blob: a265c1d688766882a046dda5622ace56f05e02f5 [file] [log] [blame]
Takashi Iwai7639a062015-03-03 10:07:24 +01001/*
2 * HD-audio codec core device
3 */
4
5#include <linux/init.h>
Abhijeet Kumar09787492018-01-23 23:00:51 +05306#include <linux/delay.h>
Takashi Iwai7639a062015-03-03 10:07:24 +01007#include <linux/device.h>
8#include <linux/slab.h>
9#include <linux/module.h>
10#include <linux/export.h>
11#include <linux/pm_runtime.h>
12#include <sound/hdaudio.h>
Takashi Iwai01ed3c02015-02-26 13:57:47 +010013#include <sound/hda_regmap.h>
Takashi Iwaib7d023e2015-04-16 08:19:06 +020014#include <sound/pcm.h>
Takashi Iwai7639a062015-03-03 10:07:24 +010015#include "local.h"
16
17static void setup_fg_nodes(struct hdac_device *codec);
18static int get_codec_vendor_name(struct hdac_device *codec);
19
20static void default_release(struct device *dev)
21{
22 snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
23}
24
25/**
26 * snd_hdac_device_init - initialize the HD-audio codec base device
27 * @codec: device to initialize
28 * @bus: but to attach
29 * @name: device name string
30 * @addr: codec address
31 *
32 * Returns zero for success or a negative error code.
33 *
34 * This function increments the runtime PM counter and marks it active.
35 * The caller needs to turn it off appropriately later.
36 *
37 * The caller needs to set the device's release op properly by itself.
38 */
39int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
40 const char *name, unsigned int addr)
41{
42 struct device *dev;
43 hda_nid_t fg;
44 int err;
45
46 dev = &codec->dev;
47 device_initialize(dev);
48 dev->parent = bus->dev;
49 dev->bus = &snd_hda_bus_type;
50 dev->release = default_release;
Takashi Iwai3256be62015-02-24 14:59:42 +010051 dev->groups = hdac_dev_attr_groups;
Takashi Iwai7639a062015-03-03 10:07:24 +010052 dev_set_name(dev, "%s", name);
53 device_enable_async_suspend(dev);
54
55 codec->bus = bus;
56 codec->addr = addr;
57 codec->type = HDA_DEV_CORE;
Amadeusz Sławińskied180ab2019-05-13 11:18:01 +020058 mutex_init(&codec->widget_lock);
Takashi Iwai7639a062015-03-03 10:07:24 +010059 pm_runtime_set_active(&codec->dev);
60 pm_runtime_get_noresume(&codec->dev);
61 atomic_set(&codec->in_pm, 0);
62
63 err = snd_hdac_bus_add_device(bus, codec);
64 if (err < 0)
65 goto error;
66
67 /* fill parameters */
68 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
69 AC_PAR_VENDOR_ID);
70 if (codec->vendor_id == -1) {
71 /* read again, hopefully the access method was corrected
72 * in the last read...
73 */
74 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
75 AC_PAR_VENDOR_ID);
76 }
77
78 codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
79 AC_PAR_SUBSYSTEM_ID);
80 codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
81 AC_PAR_REV_ID);
82
83 setup_fg_nodes(codec);
84 if (!codec->afg && !codec->mfg) {
85 dev_err(dev, "no AFG or MFG node found\n");
86 err = -ENODEV;
87 goto error;
88 }
89
90 fg = codec->afg ? codec->afg : codec->mfg;
91
Takashi Iwai774a0752019-07-03 14:35:12 +020092 err = snd_hdac_refresh_widgets(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +010093 if (err < 0)
94 goto error;
95
96 codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
97 /* reread ssid if not set by parameter */
David Henningssonffda5682015-04-01 12:43:00 +020098 if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
Takashi Iwai7639a062015-03-03 10:07:24 +010099 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
100 &codec->subsystem_id);
101
102 err = get_codec_vendor_name(codec);
103 if (err < 0)
104 goto error;
105
106 codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
107 codec->vendor_id & 0xffff);
108 if (!codec->chip_name) {
109 err = -ENOMEM;
110 goto error;
111 }
112
113 return 0;
114
115 error:
Takashi Iwai7639a062015-03-03 10:07:24 +0100116 put_device(&codec->dev);
117 return err;
118}
119EXPORT_SYMBOL_GPL(snd_hdac_device_init);
120
121/**
122 * snd_hdac_device_exit - clean up the HD-audio codec base device
123 * @codec: device to clean up
124 */
125void snd_hdac_device_exit(struct hdac_device *codec)
126{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100127 pm_runtime_put_noidle(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100128 snd_hdac_bus_remove_device(codec->bus, codec);
129 kfree(codec->vendor_name);
130 kfree(codec->chip_name);
131}
132EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
133
134/**
Takashi Iwai3256be62015-02-24 14:59:42 +0100135 * snd_hdac_device_register - register the hd-audio codec base device
136 * codec: the device to register
137 */
138int snd_hdac_device_register(struct hdac_device *codec)
139{
140 int err;
141
142 err = device_add(&codec->dev);
143 if (err < 0)
144 return err;
Amadeusz Sławińskied180ab2019-05-13 11:18:01 +0200145 mutex_lock(&codec->widget_lock);
Takashi Iwai3256be62015-02-24 14:59:42 +0100146 err = hda_widget_sysfs_init(codec);
Amadeusz Sławińskied180ab2019-05-13 11:18:01 +0200147 mutex_unlock(&codec->widget_lock);
Takashi Iwai3256be62015-02-24 14:59:42 +0100148 if (err < 0) {
149 device_del(&codec->dev);
150 return err;
151 }
152
153 return 0;
154}
155EXPORT_SYMBOL_GPL(snd_hdac_device_register);
156
157/**
158 * snd_hdac_device_unregister - unregister the hd-audio codec base device
159 * codec: the device to unregister
160 */
161void snd_hdac_device_unregister(struct hdac_device *codec)
162{
163 if (device_is_registered(&codec->dev)) {
Amadeusz Sławińskied180ab2019-05-13 11:18:01 +0200164 mutex_lock(&codec->widget_lock);
Takashi Iwai3256be62015-02-24 14:59:42 +0100165 hda_widget_sysfs_exit(codec);
Amadeusz Sławińskied180ab2019-05-13 11:18:01 +0200166 mutex_unlock(&codec->widget_lock);
Takashi Iwai3256be62015-02-24 14:59:42 +0100167 device_del(&codec->dev);
Takashi Iwaieb8d0ea2017-06-19 17:49:48 +0200168 snd_hdac_bus_remove_device(codec->bus, codec);
Takashi Iwai3256be62015-02-24 14:59:42 +0100169 }
170}
171EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
172
173/**
Takashi Iwaided255b2015-10-01 17:59:43 +0200174 * snd_hdac_device_set_chip_name - set/update the codec name
175 * @codec: the HDAC device
176 * @name: name string to set
177 *
178 * Returns 0 if the name is set or updated, or a negative error code.
179 */
180int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
181{
182 char *newname;
183
184 if (!name)
185 return 0;
186 newname = kstrdup(name, GFP_KERNEL);
187 if (!newname)
188 return -ENOMEM;
189 kfree(codec->chip_name);
190 codec->chip_name = newname;
191 return 0;
192}
193EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
194
195/**
Takashi Iwai4f9e0c32015-10-16 11:35:49 +0200196 * snd_hdac_codec_modalias - give the module alias name
197 * @codec: HDAC device
198 * @buf: string buffer to store
199 * @size: string buffer size
200 *
201 * Returns the size of string, like snprintf(), or a negative error code.
202 */
203int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
204{
205 return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
206 codec->vendor_id, codec->revision_id, codec->type);
207}
208EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
209
210/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100211 * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
212 * HD-audio controller
213 * @codec: the codec object
214 * @nid: NID to encode
215 * @verb: verb to encode
216 * @parm: parameter to encode
217 *
218 * Return an encoded command verb or -1 for error.
219 */
220unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
221 unsigned int verb, unsigned int parm)
222{
223 u32 val, addr;
224
225 addr = codec->addr;
226 if ((addr & ~0xf) || (nid & ~0x7f) ||
227 (verb & ~0xfff) || (parm & ~0xffff)) {
228 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
229 addr, nid, verb, parm);
230 return -1;
231 }
232
233 val = addr << 28;
234 val |= (u32)nid << 20;
235 val |= verb << 8;
236 val |= parm;
237 return val;
238}
239EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
240
241/**
Takashi Iwai058524482015-03-03 15:40:08 +0100242 * snd_hdac_exec_verb - execute an encoded verb
243 * @codec: the codec object
244 * @cmd: encoded verb to execute
245 * @flags: optional flags, pass zero for default
246 * @res: the pointer to store the result, NULL if running async
247 *
248 * Returns zero if successful, or a negative error code.
249 *
250 * This calls the exec_verb op when set in hdac_codec. If not,
251 * call the default snd_hdac_bus_exec_verb().
252 */
253int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
254 unsigned int flags, unsigned int *res)
255{
256 if (codec->exec_verb)
257 return codec->exec_verb(codec, cmd, flags, res);
258 return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
259}
260EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
261
262
263/**
Takashi Iwai7639a062015-03-03 10:07:24 +0100264 * snd_hdac_read - execute a verb
265 * @codec: the codec object
266 * @nid: NID to execute a verb
267 * @verb: verb to execute
268 * @parm: parameter for a verb
269 * @res: the pointer to store the result, NULL if running async
270 *
271 * Returns zero if successful, or a negative error code.
272 */
273int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
274 unsigned int verb, unsigned int parm, unsigned int *res)
275{
276 unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
277
Takashi Iwai058524482015-03-03 15:40:08 +0100278 return snd_hdac_exec_verb(codec, cmd, 0, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100279}
280EXPORT_SYMBOL_GPL(snd_hdac_read);
281
282/**
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100283 * _snd_hdac_read_parm - read a parmeter
Takashi Iwai7639a062015-03-03 10:07:24 +0100284 *
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100285 * This function returns zero or an error unlike snd_hdac_read_parm().
Takashi Iwai7639a062015-03-03 10:07:24 +0100286 */
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100287int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
288 unsigned int *res)
Takashi Iwai7639a062015-03-03 10:07:24 +0100289{
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100290 unsigned int cmd;
Takashi Iwai7639a062015-03-03 10:07:24 +0100291
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100292 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
293 return snd_hdac_regmap_read_raw(codec, cmd, res);
Takashi Iwai7639a062015-03-03 10:07:24 +0100294}
Takashi Iwai01ed3c02015-02-26 13:57:47 +0100295EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100296
297/**
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100298 * snd_hdac_read_parm_uncached - read a codec parameter without caching
Takashi Iwai7639a062015-03-03 10:07:24 +0100299 * @codec: the codec object
300 * @nid: NID to read a parameter
301 * @parm: parameter to read
302 *
303 * Returns -1 for error. If you need to distinguish the error more
304 * strictly, use snd_hdac_read() directly.
305 */
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100306int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
307 int parm)
Takashi Iwai7639a062015-03-03 10:07:24 +0100308{
Takashi Iwai3194ed42016-04-21 17:49:11 +0200309 unsigned int cmd, val;
Takashi Iwai7639a062015-03-03 10:07:24 +0100310
Takashi Iwai3194ed42016-04-21 17:49:11 +0200311 cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
312 if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
313 return -1;
Takashi Iwai7639a062015-03-03 10:07:24 +0100314 return val;
315}
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100316EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
317
318/**
Takashi Iwaifaa75f82015-02-26 08:54:56 +0100319 * snd_hdac_override_parm - override read-only parameters
320 * @codec: the codec object
321 * @nid: NID for the parameter
322 * @parm: the parameter to change
323 * @val: the parameter value to overwrite
324 */
325int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
326 unsigned int parm, unsigned int val)
327{
328 unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
329 int err;
330
331 if (!codec->regmap)
332 return -EINVAL;
333
334 codec->caps_overwriting = true;
335 err = snd_hdac_regmap_write_raw(codec, verb, val);
336 codec->caps_overwriting = false;
337 return err;
338}
339EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100340
341/**
342 * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
343 * @codec: the codec object
344 * @nid: NID to inspect
345 * @start_id: the pointer to store the starting NID
346 *
347 * Returns the number of subtree nodes or zero if not found.
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100348 * This function reads parameters always without caching.
Takashi Iwai7639a062015-03-03 10:07:24 +0100349 */
350int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
351 hda_nid_t *start_id)
352{
353 unsigned int parm;
354
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100355 parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
Takashi Iwai7639a062015-03-03 10:07:24 +0100356 if (parm == -1) {
357 *start_id = 0;
358 return 0;
359 }
360 *start_id = (parm >> 16) & 0x7fff;
361 return (int)(parm & 0x7fff);
362}
363EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
364
365/*
366 * look for an AFG and MFG nodes
367 */
368static void setup_fg_nodes(struct hdac_device *codec)
369{
370 int i, total_nodes, function_id;
371 hda_nid_t nid;
372
373 total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
374 for (i = 0; i < total_nodes; i++, nid++) {
375 function_id = snd_hdac_read_parm(codec, nid,
376 AC_PAR_FUNCTION_TYPE);
377 switch (function_id & 0xff) {
378 case AC_GRP_AUDIO_FUNCTION:
379 codec->afg = nid;
380 codec->afg_function_id = function_id & 0xff;
381 codec->afg_unsol = (function_id >> 8) & 1;
382 break;
383 case AC_GRP_MODEM_FUNCTION:
384 codec->mfg = nid;
385 codec->mfg_function_id = function_id & 0xff;
386 codec->mfg_unsol = (function_id >> 8) & 1;
387 break;
388 default:
389 break;
390 }
391 }
392}
393
394/**
395 * snd_hdac_refresh_widgets - Reset the widget start/end nodes
396 * @codec: the codec object
397 */
Takashi Iwai774a0752019-07-03 14:35:12 +0200398int snd_hdac_refresh_widgets(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100399{
400 hda_nid_t start_nid;
Evan Green98482372019-07-01 10:30:30 -0700401 int nums, err = 0;
Takashi Iwai7639a062015-03-03 10:07:24 +0100402
Evan Green98482372019-07-01 10:30:30 -0700403 /*
404 * Serialize against multiple threads trying to update the sysfs
405 * widgets array.
406 */
407 mutex_lock(&codec->widget_lock);
Takashi Iwai7639a062015-03-03 10:07:24 +0100408 nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
409 if (!start_nid || nums <= 0 || nums >= 0xff) {
410 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
411 codec->afg);
Evan Green98482372019-07-01 10:30:30 -0700412 err = -EINVAL;
413 goto unlock;
Takashi Iwai7639a062015-03-03 10:07:24 +0100414 }
415
Takashi Iwai774a0752019-07-03 14:35:12 +0200416 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
417 if (err < 0)
418 goto unlock;
Takashi Iwai9780ded2017-10-18 15:51:59 +0200419
Takashi Iwai7639a062015-03-03 10:07:24 +0100420 codec->num_nodes = nums;
421 codec->start_nid = start_nid;
422 codec->end_nid = start_nid + nums;
Evan Green98482372019-07-01 10:30:30 -0700423unlock:
424 mutex_unlock(&codec->widget_lock);
425 return err;
Takashi Iwai7639a062015-03-03 10:07:24 +0100426}
427EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
428
429/* return CONNLIST_LEN parameter of the given widget */
430static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 return 0;
438
439 parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm == -1)
441 parm = 0;
442 return parm;
443}
444
445/**
446 * snd_hdac_get_connections - get a widget connection list
447 * @codec: the codec object
448 * @nid: NID
449 * @conn_list: the array to store the results, can be NULL
450 * @max_conns: the max size of the given array
451 *
452 * Returns the number of connected widgets, zero for no connection, or a
453 * negative error code. When the number of elements don't fit with the
454 * given array size, it returns -ENOSPC.
455 *
456 * When @conn_list is NULL, it just checks the number of connections.
457 */
458int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
459 hda_nid_t *conn_list, int max_conns)
460{
461 unsigned int parm;
462 int i, conn_len, conns, err;
463 unsigned int shift, num_elems, mask;
464 hda_nid_t prev_nid;
465 int null_count = 0;
466
467 parm = get_num_conns(codec, nid);
468 if (!parm)
469 return 0;
470
471 if (parm & AC_CLIST_LONG) {
472 /* long form */
473 shift = 16;
474 num_elems = 2;
475 } else {
476 /* short form */
477 shift = 8;
478 num_elems = 4;
479 }
480 conn_len = parm & AC_CLIST_LENGTH;
481 mask = (1 << (shift-1)) - 1;
482
483 if (!conn_len)
484 return 0; /* no connection */
485
486 if (conn_len == 1) {
487 /* single connection */
488 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
489 &parm);
490 if (err < 0)
491 return err;
492 if (conn_list)
493 conn_list[0] = parm & mask;
494 return 1;
495 }
496
497 /* multi connection */
498 conns = 0;
499 prev_nid = 0;
500 for (i = 0; i < conn_len; i++) {
501 int range_val;
502 hda_nid_t val, n;
503
504 if (i % num_elems == 0) {
505 err = snd_hdac_read(codec, nid,
506 AC_VERB_GET_CONNECT_LIST, i,
507 &parm);
508 if (err < 0)
509 return -EIO;
510 }
511 range_val = !!(parm & (1 << (shift-1))); /* ranges */
512 val = parm & mask;
513 if (val == 0 && null_count++) { /* no second chance */
514 dev_dbg(&codec->dev,
515 "invalid CONNECT_LIST verb %x[%i]:%x\n",
516 nid, i, parm);
517 return 0;
518 }
519 parm >>= shift;
520 if (range_val) {
521 /* ranges between the previous and this one */
522 if (!prev_nid || prev_nid >= val) {
523 dev_warn(&codec->dev,
524 "invalid dep_range_val %x:%x\n",
525 prev_nid, val);
526 continue;
527 }
528 for (n = prev_nid + 1; n <= val; n++) {
529 if (conn_list) {
530 if (conns >= max_conns)
531 return -ENOSPC;
532 conn_list[conns] = n;
533 }
534 conns++;
535 }
536 } else {
537 if (conn_list) {
538 if (conns >= max_conns)
539 return -ENOSPC;
540 conn_list[conns] = val;
541 }
542 conns++;
543 }
544 prev_nid = val;
545 }
546 return conns;
547}
548EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
549
550#ifdef CONFIG_PM
551/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200552 * snd_hdac_power_up - power up the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100553 * @codec: the codec object
Takashi Iwai664c7152015-04-08 11:43:14 +0200554 *
555 * This function calls the runtime PM helper to power up the given codec.
556 * Unlike snd_hdac_power_up_pm(), you should call this only for the code
557 * path that isn't included in PM path. Otherwise it gets stuck.
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200558 *
559 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100560 */
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200561int snd_hdac_power_up(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100562{
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200563 return pm_runtime_get_sync(&codec->dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100564}
565EXPORT_SYMBOL_GPL(snd_hdac_power_up);
566
567/**
Takashi Iwai664c7152015-04-08 11:43:14 +0200568 * snd_hdac_power_down - power down the codec
Takashi Iwai7639a062015-03-03 10:07:24 +0100569 * @codec: the codec object
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200570 *
571 * Returns zero if successful, or a negative error code.
Takashi Iwai7639a062015-03-03 10:07:24 +0100572 */
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200573int snd_hdac_power_down(struct hdac_device *codec)
Takashi Iwai7639a062015-03-03 10:07:24 +0100574{
575 struct device *dev = &codec->dev;
576
Takashi Iwai7639a062015-03-03 10:07:24 +0100577 pm_runtime_mark_last_busy(dev);
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200578 return pm_runtime_put_autosuspend(dev);
Takashi Iwai7639a062015-03-03 10:07:24 +0100579}
580EXPORT_SYMBOL_GPL(snd_hdac_power_down);
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200581
582/**
583 * snd_hdac_power_up_pm - power up the codec
584 * @codec: the codec object
585 *
586 * This function can be called in a recursive code path like init code
587 * which may be called by PM suspend/resume again. OTOH, if a power-up
588 * call must wake up the sleeper (e.g. in a kctl callback), use
589 * snd_hdac_power_up() instead.
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200590 *
591 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200592 */
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200593int snd_hdac_power_up_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200594{
595 if (!atomic_inc_not_zero(&codec->in_pm))
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200596 return snd_hdac_power_up(codec);
597 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200598}
599EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
600
Takashi Iwaifc4f0002016-03-04 11:34:18 +0100601/* like snd_hdac_power_up_pm(), but only increment the pm count when
602 * already powered up. Returns -1 if not powered up, 1 if incremented
603 * or 0 if unchanged. Only used in hdac_regmap.c
604 */
605int snd_hdac_keep_power_up(struct hdac_device *codec)
606{
607 if (!atomic_inc_not_zero(&codec->in_pm)) {
608 int ret = pm_runtime_get_if_in_use(&codec->dev);
609 if (!ret)
610 return -1;
611 if (ret < 0)
612 return 0;
613 }
614 return 1;
615}
616
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200617/**
618 * snd_hdac_power_down_pm - power down the codec
619 * @codec: the codec object
620 *
621 * Like snd_hdac_power_up_pm(), this function is used in a recursive
622 * code path like init code which may be called by PM suspend/resume again.
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200623 *
624 * Returns zero if successful, or a negative error code.
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200625 */
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200626int snd_hdac_power_down_pm(struct hdac_device *codec)
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200627{
628 if (atomic_dec_if_positive(&codec->in_pm) < 0)
Takashi Iwaifbce23a02015-07-17 16:27:33 +0200629 return snd_hdac_power_down(codec);
630 return 0;
Takashi Iwaic3aeda62015-04-13 11:01:14 +0200631}
632EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
Takashi Iwai7639a062015-03-03 10:07:24 +0100633#endif
634
635/* codec vendor labels */
636struct hda_vendor_id {
637 unsigned int id;
638 const char *name;
639};
640
641static struct hda_vendor_id hda_vendor_ids[] = {
642 { 0x1002, "ATI" },
643 { 0x1013, "Cirrus Logic" },
644 { 0x1057, "Motorola" },
645 { 0x1095, "Silicon Image" },
646 { 0x10de, "Nvidia" },
647 { 0x10ec, "Realtek" },
648 { 0x1102, "Creative" },
649 { 0x1106, "VIA" },
650 { 0x111d, "IDT" },
651 { 0x11c1, "LSI" },
652 { 0x11d4, "Analog Devices" },
653 { 0x13f6, "C-Media" },
654 { 0x14f1, "Conexant" },
655 { 0x17e8, "Chrontel" },
656 { 0x1854, "LG" },
657 { 0x1aec, "Wolfson Microelectronics" },
658 { 0x1af4, "QEMU" },
659 { 0x434d, "C-Media" },
660 { 0x8086, "Intel" },
661 { 0x8384, "SigmaTel" },
662 {} /* terminator */
663};
664
665/* store the codec vendor name */
666static int get_codec_vendor_name(struct hdac_device *codec)
667{
668 const struct hda_vendor_id *c;
669 u16 vendor_id = codec->vendor_id >> 16;
670
671 for (c = hda_vendor_ids; c->id; c++) {
672 if (c->id == vendor_id) {
673 codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
674 return codec->vendor_name ? 0 : -ENOMEM;
675 }
676 }
677
678 codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
679 return codec->vendor_name ? 0 : -ENOMEM;
680}
Takashi Iwaib7d023e2015-04-16 08:19:06 +0200681
682/*
683 * stream formats
684 */
685struct hda_rate_tbl {
686 unsigned int hz;
687 unsigned int alsa_bits;
688 unsigned int hda_fmt;
689};
690
691/* rate = base * mult / div */
692#define HDA_RATE(base, mult, div) \
693 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
694 (((div) - 1) << AC_FMT_DIV_SHIFT))
695
696static struct hda_rate_tbl rate_bits[] = {
697 /* rate in Hz, ALSA rate bitmask, HDA format value */
698
699 /* autodetected value used in snd_hda_query_supported_pcm */
700 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
701 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
702 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
703 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
704 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
705 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
706 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
707 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
708 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
709 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
710 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
711#define AC_PAR_PCM_RATE_BITS 11
712 /* up to bits 10, 384kHZ isn't supported properly */
713
714 /* not autodetected value */
715 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
716
717 { 0 } /* terminator */
718};
719
720/**
721 * snd_hdac_calc_stream_format - calculate the format bitset
722 * @rate: the sample rate
723 * @channels: the number of channels
724 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
725 * @maxbps: the max. bps
726 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
727 *
728 * Calculate the format bitset from the given rate, channels and th PCM format.
729 *
730 * Return zero if invalid.
731 */
732unsigned int snd_hdac_calc_stream_format(unsigned int rate,
733 unsigned int channels,
Takashi Iwaia6ea5fe2018-07-25 23:19:36 +0200734 snd_pcm_format_t format,
Takashi Iwaib7d023e2015-04-16 08:19:06 +0200735 unsigned int maxbps,
736 unsigned short spdif_ctls)
737{
738 int i;
739 unsigned int val = 0;
740
741 for (i = 0; rate_bits[i].hz; i++)
742 if (rate_bits[i].hz == rate) {
743 val = rate_bits[i].hda_fmt;
744 break;
745 }
746 if (!rate_bits[i].hz)
747 return 0;
748
749 if (channels == 0 || channels > 8)
750 return 0;
751 val |= channels - 1;
752
753 switch (snd_pcm_format_width(format)) {
754 case 8:
755 val |= AC_FMT_BITS_8;
756 break;
757 case 16:
758 val |= AC_FMT_BITS_16;
759 break;
760 case 20:
761 case 24:
762 case 32:
763 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
764 val |= AC_FMT_BITS_32;
765 else if (maxbps >= 24)
766 val |= AC_FMT_BITS_24;
767 else
768 val |= AC_FMT_BITS_20;
769 break;
770 default:
771 return 0;
772 }
773
774 if (spdif_ctls & AC_DIG1_NONAUDIO)
775 val |= AC_FMT_TYPE_NON_PCM;
776
777 return val;
778}
779EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
780
781static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
782{
783 unsigned int val = 0;
784
785 if (nid != codec->afg &&
786 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
787 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
788 if (!val || val == -1)
789 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
790 if (!val || val == -1)
791 return 0;
792 return val;
793}
794
795static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
796{
797 unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
798
799 if (!streams || streams == -1)
800 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
801 if (!streams || streams == -1)
802 return 0;
803 return streams;
804}
805
806/**
807 * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
808 * @codec: the codec object
809 * @nid: NID to query
810 * @ratesp: the pointer to store the detected rate bitflags
811 * @formatsp: the pointer to store the detected formats
812 * @bpsp: the pointer to store the detected format widths
813 *
814 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
815 * or @bsps argument is ignored.
816 *
817 * Returns 0 if successful, otherwise a negative error code.
818 */
819int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
820 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
821{
822 unsigned int i, val, wcaps;
823
824 wcaps = get_wcaps(codec, nid);
825 val = query_pcm_param(codec, nid);
826
827 if (ratesp) {
828 u32 rates = 0;
829 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
830 if (val & (1 << i))
831 rates |= rate_bits[i].alsa_bits;
832 }
833 if (rates == 0) {
834 dev_err(&codec->dev,
835 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
836 nid, val,
837 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
838 return -EIO;
839 }
840 *ratesp = rates;
841 }
842
843 if (formatsp || bpsp) {
844 u64 formats = 0;
845 unsigned int streams, bps;
846
847 streams = query_stream_param(codec, nid);
848 if (!streams)
849 return -EIO;
850
851 bps = 0;
852 if (streams & AC_SUPFMT_PCM) {
853 if (val & AC_SUPPCM_BITS_8) {
854 formats |= SNDRV_PCM_FMTBIT_U8;
855 bps = 8;
856 }
857 if (val & AC_SUPPCM_BITS_16) {
858 formats |= SNDRV_PCM_FMTBIT_S16_LE;
859 bps = 16;
860 }
861 if (wcaps & AC_WCAP_DIGITAL) {
862 if (val & AC_SUPPCM_BITS_32)
863 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
864 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
865 formats |= SNDRV_PCM_FMTBIT_S32_LE;
866 if (val & AC_SUPPCM_BITS_24)
867 bps = 24;
868 else if (val & AC_SUPPCM_BITS_20)
869 bps = 20;
870 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
871 AC_SUPPCM_BITS_32)) {
872 formats |= SNDRV_PCM_FMTBIT_S32_LE;
873 if (val & AC_SUPPCM_BITS_32)
874 bps = 32;
875 else if (val & AC_SUPPCM_BITS_24)
876 bps = 24;
877 else if (val & AC_SUPPCM_BITS_20)
878 bps = 20;
879 }
880 }
881#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
882 if (streams & AC_SUPFMT_FLOAT32) {
883 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
884 if (!bps)
885 bps = 32;
886 }
887#endif
888 if (streams == AC_SUPFMT_AC3) {
889 /* should be exclusive */
890 /* temporary hack: we have still no proper support
891 * for the direct AC3 stream...
892 */
893 formats |= SNDRV_PCM_FMTBIT_U8;
894 bps = 8;
895 }
896 if (formats == 0) {
897 dev_err(&codec->dev,
898 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
899 nid, val,
900 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
901 streams);
902 return -EIO;
903 }
904 if (formatsp)
905 *formatsp = formats;
906 if (bpsp)
907 *bpsp = bps;
908 }
909
910 return 0;
911}
912EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
913
914/**
915 * snd_hdac_is_supported_format - Check the validity of the format
916 * @codec: the codec object
917 * @nid: NID to check
918 * @format: the HD-audio format value to check
919 *
920 * Check whether the given node supports the format value.
921 *
922 * Returns true if supported, false if not.
923 */
924bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
925 unsigned int format)
926{
927 int i;
928 unsigned int val = 0, rate, stream;
929
930 val = query_pcm_param(codec, nid);
931 if (!val)
932 return false;
933
934 rate = format & 0xff00;
935 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
936 if (rate_bits[i].hda_fmt == rate) {
937 if (val & (1 << i))
938 break;
939 return false;
940 }
941 if (i >= AC_PAR_PCM_RATE_BITS)
942 return false;
943
944 stream = query_stream_param(codec, nid);
945 if (!stream)
946 return false;
947
948 if (stream & AC_SUPFMT_PCM) {
949 switch (format & 0xf0) {
950 case 0x00:
951 if (!(val & AC_SUPPCM_BITS_8))
952 return false;
953 break;
954 case 0x10:
955 if (!(val & AC_SUPPCM_BITS_16))
956 return false;
957 break;
958 case 0x20:
959 if (!(val & AC_SUPPCM_BITS_20))
960 return false;
961 break;
962 case 0x30:
963 if (!(val & AC_SUPPCM_BITS_24))
964 return false;
965 break;
966 case 0x40:
967 if (!(val & AC_SUPPCM_BITS_32))
968 return false;
969 break;
970 default:
971 return false;
972 }
973 } else {
974 /* FIXME: check for float32 and AC3? */
975 }
976
977 return true;
978}
979EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
Subhransu S. Prusty1b5e6162015-10-08 09:48:05 +0100980
981static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
982 int flags, unsigned int verb, unsigned int parm)
983{
984 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
985 unsigned int res;
986
987 if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
988 return -1;
989
990 return res;
991}
992
993static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
994 int flags, unsigned int verb, unsigned int parm)
995{
996 unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
997
998 return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
999}
1000
1001/**
1002 * snd_hdac_codec_read - send a command and get the response
1003 * @hdac: the HDAC device
1004 * @nid: NID to send the command
1005 * @flags: optional bit flags
1006 * @verb: the verb to send
1007 * @parm: the parameter for the verb
1008 *
1009 * Send a single command and read the corresponding response.
1010 *
1011 * Returns the obtained response value, or -1 for an error.
1012 */
1013int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1014 int flags, unsigned int verb, unsigned int parm)
1015{
1016 return codec_read(hdac, nid, flags, verb, parm);
1017}
1018EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1019
1020/**
1021 * snd_hdac_codec_write - send a single command without waiting for response
1022 * @hdac: the HDAC device
1023 * @nid: NID to send the command
1024 * @flags: optional bit flags
1025 * @verb: the verb to send
1026 * @parm: the parameter for the verb
1027 *
1028 * Send a single command without waiting for response.
1029 *
1030 * Returns 0 if successful, or a negative error code.
1031 */
1032int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1033 int flags, unsigned int verb, unsigned int parm)
1034{
1035 return codec_write(hdac, nid, flags, verb, parm);
1036}
1037EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1038
Takashi Iwai78dd5e22015-10-28 12:26:48 +01001039/**
1040 * snd_hdac_check_power_state - check whether the actual power state matches
Subhransu S. Prusty1b5e6162015-10-08 09:48:05 +01001041 * with the target state
1042 *
1043 * @hdac: the HDAC device
1044 * @nid: NID to send the command
1045 * @target_state: target state to check for
1046 *
1047 * Return true if state matches, false if not
1048 */
1049bool snd_hdac_check_power_state(struct hdac_device *hdac,
1050 hda_nid_t nid, unsigned int target_state)
1051{
1052 unsigned int state = codec_read(hdac, nid, 0,
1053 AC_VERB_GET_POWER_STATE, 0);
1054
1055 if (state & AC_PWRST_ERROR)
1056 return true;
1057 state = (state >> 4) & 0x0f;
1058 return (state == target_state);
1059}
1060EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
Abhijeet Kumar09787492018-01-23 23:00:51 +05301061/**
1062 * snd_hdac_sync_power_state - wait until actual power state matches
1063 * with the target state
1064 *
1065 * @hdac: the HDAC device
1066 * @nid: NID to send the command
1067 * @target_state: target state to check for
1068 *
1069 * Return power state or PS_ERROR if codec rejects GET verb.
1070 */
1071unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1072 hda_nid_t nid, unsigned int power_state)
1073{
1074 unsigned long end_time = jiffies + msecs_to_jiffies(500);
1075 unsigned int state, actual_state, count;
1076
1077 for (count = 0; count < 500; count++) {
1078 state = snd_hdac_codec_read(codec, nid, 0,
1079 AC_VERB_GET_POWER_STATE, 0);
1080 if (state & AC_PWRST_ERROR) {
1081 msleep(20);
1082 break;
1083 }
1084 actual_state = (state >> 4) & 0x0f;
1085 if (actual_state == power_state)
1086 break;
1087 if (time_after_eq(jiffies, end_time))
1088 break;
1089 /* wait until the codec reachs to the target state */
1090 msleep(1);
1091 }
1092 return state;
1093}
1094EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);