Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7639a06c TI |
2 | /* |
3 | * HD-audio codec core device | |
4 | */ | |
5 | ||
6 | #include <linux/init.h> | |
09787492 | 7 | #include <linux/delay.h> |
7639a06c TI |
8 | #include <linux/device.h> |
9 | #include <linux/slab.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/export.h> | |
12 | #include <linux/pm_runtime.h> | |
13 | #include <sound/hdaudio.h> | |
01ed3c06 | 14 | #include <sound/hda_regmap.h> |
b7d023e1 | 15 | #include <sound/pcm.h> |
d24f1a09 | 16 | #include <sound/pcm_params.h> |
7639a06c TI |
17 | #include "local.h" |
18 | ||
19 | static void setup_fg_nodes(struct hdac_device *codec); | |
20 | static int get_codec_vendor_name(struct hdac_device *codec); | |
21 | ||
22 | static void default_release(struct device *dev) | |
23 | { | |
50f0bf55 | 24 | snd_hdac_device_exit(dev_to_hdac_dev(dev)); |
7639a06c TI |
25 | } |
26 | ||
27 | /** | |
28 | * snd_hdac_device_init - initialize the HD-audio codec base device | |
29 | * @codec: device to initialize | |
30 | * @bus: but to attach | |
31 | * @name: device name string | |
32 | * @addr: codec address | |
33 | * | |
34 | * Returns zero for success or a negative error code. | |
35 | * | |
36 | * This function increments the runtime PM counter and marks it active. | |
37 | * The caller needs to turn it off appropriately later. | |
38 | * | |
39 | * The caller needs to set the device's release op properly by itself. | |
40 | */ | |
41 | int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus, | |
42 | const char *name, unsigned int addr) | |
43 | { | |
44 | struct device *dev; | |
45 | hda_nid_t fg; | |
46 | int err; | |
47 | ||
48 | dev = &codec->dev; | |
49 | device_initialize(dev); | |
50 | dev->parent = bus->dev; | |
51 | dev->bus = &snd_hda_bus_type; | |
52 | dev->release = default_release; | |
3256be65 | 53 | dev->groups = hdac_dev_attr_groups; |
7639a06c TI |
54 | dev_set_name(dev, "%s", name); |
55 | device_enable_async_suspend(dev); | |
56 | ||
57 | codec->bus = bus; | |
58 | codec->addr = addr; | |
59 | codec->type = HDA_DEV_CORE; | |
ed180abb | 60 | mutex_init(&codec->widget_lock); |
1a462be5 | 61 | mutex_init(&codec->regmap_lock); |
7639a06c TI |
62 | pm_runtime_set_active(&codec->dev); |
63 | pm_runtime_get_noresume(&codec->dev); | |
64 | atomic_set(&codec->in_pm, 0); | |
65 | ||
246bb4aa TI |
66 | err = snd_hdac_bus_add_device(bus, codec); |
67 | if (err < 0) | |
68 | goto error; | |
69 | ||
7639a06c TI |
70 | /* fill parameters */ |
71 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
72 | AC_PAR_VENDOR_ID); | |
73 | if (codec->vendor_id == -1) { | |
74 | /* read again, hopefully the access method was corrected | |
75 | * in the last read... | |
76 | */ | |
77 | codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
78 | AC_PAR_VENDOR_ID); | |
79 | } | |
80 | ||
81 | codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
82 | AC_PAR_SUBSYSTEM_ID); | |
83 | codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT, | |
84 | AC_PAR_REV_ID); | |
85 | ||
86 | setup_fg_nodes(codec); | |
87 | if (!codec->afg && !codec->mfg) { | |
88 | dev_err(dev, "no AFG or MFG node found\n"); | |
89 | err = -ENODEV; | |
90 | goto error; | |
91 | } | |
92 | ||
93 | fg = codec->afg ? codec->afg : codec->mfg; | |
94 | ||
774a075a | 95 | err = snd_hdac_refresh_widgets(codec); |
7639a06c TI |
96 | if (err < 0) |
97 | goto error; | |
98 | ||
99 | codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE); | |
100 | /* reread ssid if not set by parameter */ | |
ffda568e | 101 | if (codec->subsystem_id == -1 || codec->subsystem_id == 0) |
7639a06c TI |
102 | snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0, |
103 | &codec->subsystem_id); | |
104 | ||
105 | err = get_codec_vendor_name(codec); | |
106 | if (err < 0) | |
107 | goto error; | |
108 | ||
109 | codec->chip_name = kasprintf(GFP_KERNEL, "ID %x", | |
110 | codec->vendor_id & 0xffff); | |
111 | if (!codec->chip_name) { | |
112 | err = -ENOMEM; | |
113 | goto error; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | ||
118 | error: | |
7639a06c TI |
119 | put_device(&codec->dev); |
120 | return err; | |
121 | } | |
122 | EXPORT_SYMBOL_GPL(snd_hdac_device_init); | |
123 | ||
124 | /** | |
125 | * snd_hdac_device_exit - clean up the HD-audio codec base device | |
126 | * @codec: device to clean up | |
127 | */ | |
128 | void snd_hdac_device_exit(struct hdac_device *codec) | |
129 | { | |
c4c2533f | 130 | pm_runtime_put_noidle(&codec->dev); |
13774d81 RW |
131 | /* keep balance of runtime PM child_count in parent device */ |
132 | pm_runtime_set_suspended(&codec->dev); | |
7639a06c TI |
133 | snd_hdac_bus_remove_device(codec->bus, codec); |
134 | kfree(codec->vendor_name); | |
135 | kfree(codec->chip_name); | |
136 | } | |
137 | EXPORT_SYMBOL_GPL(snd_hdac_device_exit); | |
138 | ||
3256be65 TI |
139 | /** |
140 | * snd_hdac_device_register - register the hd-audio codec base device | |
6e57188f | 141 | * @codec: the device to register |
3256be65 TI |
142 | */ |
143 | int snd_hdac_device_register(struct hdac_device *codec) | |
144 | { | |
145 | int err; | |
146 | ||
147 | err = device_add(&codec->dev); | |
148 | if (err < 0) | |
149 | return err; | |
ed180abb | 150 | mutex_lock(&codec->widget_lock); |
3256be65 | 151 | err = hda_widget_sysfs_init(codec); |
ed180abb | 152 | mutex_unlock(&codec->widget_lock); |
246bb4aa TI |
153 | if (err < 0) { |
154 | device_del(&codec->dev); | |
155 | return err; | |
156 | } | |
3256be65 TI |
157 | |
158 | return 0; | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(snd_hdac_device_register); | |
161 | ||
162 | /** | |
163 | * snd_hdac_device_unregister - unregister the hd-audio codec base device | |
6e57188f | 164 | * @codec: the device to unregister |
3256be65 TI |
165 | */ |
166 | void snd_hdac_device_unregister(struct hdac_device *codec) | |
167 | { | |
168 | if (device_is_registered(&codec->dev)) { | |
ed180abb | 169 | mutex_lock(&codec->widget_lock); |
3256be65 | 170 | hda_widget_sysfs_exit(codec); |
ed180abb | 171 | mutex_unlock(&codec->widget_lock); |
ee5f85d9 | 172 | device_del(&codec->dev); |
246bb4aa | 173 | snd_hdac_bus_remove_device(codec->bus, codec); |
3256be65 TI |
174 | } |
175 | } | |
176 | EXPORT_SYMBOL_GPL(snd_hdac_device_unregister); | |
177 | ||
ded255be TI |
178 | /** |
179 | * snd_hdac_device_set_chip_name - set/update the codec name | |
180 | * @codec: the HDAC device | |
181 | * @name: name string to set | |
182 | * | |
183 | * Returns 0 if the name is set or updated, or a negative error code. | |
184 | */ | |
185 | int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name) | |
186 | { | |
187 | char *newname; | |
188 | ||
189 | if (!name) | |
190 | return 0; | |
191 | newname = kstrdup(name, GFP_KERNEL); | |
192 | if (!newname) | |
193 | return -ENOMEM; | |
194 | kfree(codec->chip_name); | |
195 | codec->chip_name = newname; | |
196 | return 0; | |
197 | } | |
198 | EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name); | |
199 | ||
4f9e0c38 TI |
200 | /** |
201 | * snd_hdac_codec_modalias - give the module alias name | |
202 | * @codec: HDAC device | |
203 | * @buf: string buffer to store | |
204 | * @size: string buffer size | |
205 | * | |
206 | * Returns the size of string, like snprintf(), or a negative error code. | |
207 | */ | |
2a81ada3 | 208 | int snd_hdac_codec_modalias(const struct hdac_device *codec, char *buf, size_t size) |
4f9e0c38 | 209 | { |
0a7efa14 | 210 | return scnprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n", |
4f9e0c38 TI |
211 | codec->vendor_id, codec->revision_id, codec->type); |
212 | } | |
213 | EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias); | |
214 | ||
7639a06c TI |
215 | /** |
216 | * snd_hdac_make_cmd - compose a 32bit command word to be sent to the | |
217 | * HD-audio controller | |
218 | * @codec: the codec object | |
219 | * @nid: NID to encode | |
220 | * @verb: verb to encode | |
221 | * @parm: parameter to encode | |
222 | * | |
223 | * Return an encoded command verb or -1 for error. | |
224 | */ | |
ddf7cb83 TI |
225 | static unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid, |
226 | unsigned int verb, unsigned int parm) | |
7639a06c TI |
227 | { |
228 | u32 val, addr; | |
229 | ||
230 | addr = codec->addr; | |
231 | if ((addr & ~0xf) || (nid & ~0x7f) || | |
232 | (verb & ~0xfff) || (parm & ~0xffff)) { | |
233 | dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n", | |
234 | addr, nid, verb, parm); | |
235 | return -1; | |
236 | } | |
237 | ||
238 | val = addr << 28; | |
239 | val |= (u32)nid << 20; | |
240 | val |= verb << 8; | |
241 | val |= parm; | |
242 | return val; | |
243 | } | |
7639a06c | 244 | |
05852448 TI |
245 | /** |
246 | * snd_hdac_exec_verb - execute an encoded verb | |
247 | * @codec: the codec object | |
248 | * @cmd: encoded verb to execute | |
249 | * @flags: optional flags, pass zero for default | |
250 | * @res: the pointer to store the result, NULL if running async | |
251 | * | |
252 | * Returns zero if successful, or a negative error code. | |
253 | * | |
254 | * This calls the exec_verb op when set in hdac_codec. If not, | |
255 | * call the default snd_hdac_bus_exec_verb(). | |
256 | */ | |
257 | int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd, | |
258 | unsigned int flags, unsigned int *res) | |
259 | { | |
260 | if (codec->exec_verb) | |
261 | return codec->exec_verb(codec, cmd, flags, res); | |
262 | return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res); | |
263 | } | |
05852448 TI |
264 | |
265 | ||
7639a06c TI |
266 | /** |
267 | * snd_hdac_read - execute a verb | |
268 | * @codec: the codec object | |
269 | * @nid: NID to execute a verb | |
270 | * @verb: verb to execute | |
271 | * @parm: parameter for a verb | |
272 | * @res: the pointer to store the result, NULL if running async | |
273 | * | |
274 | * Returns zero if successful, or a negative error code. | |
275 | */ | |
276 | int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid, | |
277 | unsigned int verb, unsigned int parm, unsigned int *res) | |
278 | { | |
279 | unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm); | |
280 | ||
05852448 | 281 | return snd_hdac_exec_verb(codec, cmd, 0, res); |
7639a06c TI |
282 | } |
283 | EXPORT_SYMBOL_GPL(snd_hdac_read); | |
284 | ||
285 | /** | |
01ed3c06 | 286 | * _snd_hdac_read_parm - read a parmeter |
6e57188f KJ |
287 | * @codec: the codec object |
288 | * @nid: NID to read a parameter | |
289 | * @parm: parameter to read | |
290 | * @res: pointer to store the read value | |
7639a06c | 291 | * |
01ed3c06 | 292 | * This function returns zero or an error unlike snd_hdac_read_parm(). |
7639a06c | 293 | */ |
01ed3c06 TI |
294 | int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm, |
295 | unsigned int *res) | |
7639a06c | 296 | { |
01ed3c06 | 297 | unsigned int cmd; |
7639a06c | 298 | |
01ed3c06 TI |
299 | cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; |
300 | return snd_hdac_regmap_read_raw(codec, cmd, res); | |
7639a06c | 301 | } |
01ed3c06 | 302 | EXPORT_SYMBOL_GPL(_snd_hdac_read_parm); |
7639a06c | 303 | |
9ba17b4d TI |
304 | /** |
305 | * snd_hdac_read_parm_uncached - read a codec parameter without caching | |
7639a06c TI |
306 | * @codec: the codec object |
307 | * @nid: NID to read a parameter | |
308 | * @parm: parameter to read | |
309 | * | |
310 | * Returns -1 for error. If you need to distinguish the error more | |
311 | * strictly, use snd_hdac_read() directly. | |
312 | */ | |
9ba17b4d TI |
313 | int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid, |
314 | int parm) | |
7639a06c | 315 | { |
3194ed49 | 316 | unsigned int cmd, val; |
7639a06c | 317 | |
3194ed49 TI |
318 | cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm; |
319 | if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0) | |
320 | return -1; | |
7639a06c TI |
321 | return val; |
322 | } | |
9ba17b4d TI |
323 | EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached); |
324 | ||
faa75f8a TI |
325 | /** |
326 | * snd_hdac_override_parm - override read-only parameters | |
327 | * @codec: the codec object | |
328 | * @nid: NID for the parameter | |
329 | * @parm: the parameter to change | |
330 | * @val: the parameter value to overwrite | |
331 | */ | |
332 | int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid, | |
333 | unsigned int parm, unsigned int val) | |
334 | { | |
335 | unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm; | |
336 | int err; | |
337 | ||
338 | if (!codec->regmap) | |
339 | return -EINVAL; | |
340 | ||
341 | codec->caps_overwriting = true; | |
342 | err = snd_hdac_regmap_write_raw(codec, verb, val); | |
343 | codec->caps_overwriting = false; | |
344 | return err; | |
345 | } | |
346 | EXPORT_SYMBOL_GPL(snd_hdac_override_parm); | |
7639a06c TI |
347 | |
348 | /** | |
349 | * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes | |
350 | * @codec: the codec object | |
351 | * @nid: NID to inspect | |
352 | * @start_id: the pointer to store the starting NID | |
353 | * | |
354 | * Returns the number of subtree nodes or zero if not found. | |
9ba17b4d | 355 | * This function reads parameters always without caching. |
7639a06c TI |
356 | */ |
357 | int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid, | |
358 | hda_nid_t *start_id) | |
359 | { | |
360 | unsigned int parm; | |
361 | ||
9ba17b4d | 362 | parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT); |
7639a06c TI |
363 | if (parm == -1) { |
364 | *start_id = 0; | |
365 | return 0; | |
366 | } | |
367 | *start_id = (parm >> 16) & 0x7fff; | |
368 | return (int)(parm & 0x7fff); | |
369 | } | |
370 | EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes); | |
371 | ||
372 | /* | |
373 | * look for an AFG and MFG nodes | |
374 | */ | |
375 | static void setup_fg_nodes(struct hdac_device *codec) | |
376 | { | |
377 | int i, total_nodes, function_id; | |
378 | hda_nid_t nid; | |
379 | ||
380 | total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | |
381 | for (i = 0; i < total_nodes; i++, nid++) { | |
382 | function_id = snd_hdac_read_parm(codec, nid, | |
383 | AC_PAR_FUNCTION_TYPE); | |
384 | switch (function_id & 0xff) { | |
385 | case AC_GRP_AUDIO_FUNCTION: | |
386 | codec->afg = nid; | |
387 | codec->afg_function_id = function_id & 0xff; | |
388 | codec->afg_unsol = (function_id >> 8) & 1; | |
389 | break; | |
390 | case AC_GRP_MODEM_FUNCTION: | |
391 | codec->mfg = nid; | |
392 | codec->mfg_function_id = function_id & 0xff; | |
393 | codec->mfg_unsol = (function_id >> 8) & 1; | |
394 | break; | |
395 | default: | |
396 | break; | |
397 | } | |
398 | } | |
399 | } | |
400 | ||
401 | /** | |
402 | * snd_hdac_refresh_widgets - Reset the widget start/end nodes | |
403 | * @codec: the codec object | |
404 | */ | |
774a075a | 405 | int snd_hdac_refresh_widgets(struct hdac_device *codec) |
7639a06c TI |
406 | { |
407 | hda_nid_t start_nid; | |
98482377 | 408 | int nums, err = 0; |
7639a06c | 409 | |
98482377 EG |
410 | /* |
411 | * Serialize against multiple threads trying to update the sysfs | |
412 | * widgets array. | |
413 | */ | |
414 | mutex_lock(&codec->widget_lock); | |
7639a06c TI |
415 | nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid); |
416 | if (!start_nid || nums <= 0 || nums >= 0xff) { | |
417 | dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n", | |
418 | codec->afg); | |
98482377 EG |
419 | err = -EINVAL; |
420 | goto unlock; | |
7639a06c TI |
421 | } |
422 | ||
774a075a TI |
423 | err = hda_widget_sysfs_reinit(codec, start_nid, nums); |
424 | if (err < 0) | |
425 | goto unlock; | |
9780ded3 | 426 | |
7639a06c TI |
427 | codec->num_nodes = nums; |
428 | codec->start_nid = start_nid; | |
429 | codec->end_nid = start_nid + nums; | |
98482377 EG |
430 | unlock: |
431 | mutex_unlock(&codec->widget_lock); | |
432 | return err; | |
7639a06c TI |
433 | } |
434 | EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets); | |
435 | ||
436 | /* return CONNLIST_LEN parameter of the given widget */ | |
437 | static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid) | |
438 | { | |
439 | unsigned int wcaps = get_wcaps(codec, nid); | |
440 | unsigned int parm; | |
441 | ||
442 | if (!(wcaps & AC_WCAP_CONN_LIST) && | |
443 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) | |
444 | return 0; | |
445 | ||
446 | parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN); | |
447 | if (parm == -1) | |
448 | parm = 0; | |
449 | return parm; | |
450 | } | |
451 | ||
452 | /** | |
453 | * snd_hdac_get_connections - get a widget connection list | |
454 | * @codec: the codec object | |
455 | * @nid: NID | |
456 | * @conn_list: the array to store the results, can be NULL | |
457 | * @max_conns: the max size of the given array | |
458 | * | |
459 | * Returns the number of connected widgets, zero for no connection, or a | |
460 | * negative error code. When the number of elements don't fit with the | |
461 | * given array size, it returns -ENOSPC. | |
462 | * | |
463 | * When @conn_list is NULL, it just checks the number of connections. | |
464 | */ | |
465 | int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid, | |
466 | hda_nid_t *conn_list, int max_conns) | |
467 | { | |
468 | unsigned int parm; | |
469 | int i, conn_len, conns, err; | |
470 | unsigned int shift, num_elems, mask; | |
471 | hda_nid_t prev_nid; | |
472 | int null_count = 0; | |
473 | ||
474 | parm = get_num_conns(codec, nid); | |
475 | if (!parm) | |
476 | return 0; | |
477 | ||
478 | if (parm & AC_CLIST_LONG) { | |
479 | /* long form */ | |
480 | shift = 16; | |
481 | num_elems = 2; | |
482 | } else { | |
483 | /* short form */ | |
484 | shift = 8; | |
485 | num_elems = 4; | |
486 | } | |
487 | conn_len = parm & AC_CLIST_LENGTH; | |
488 | mask = (1 << (shift-1)) - 1; | |
489 | ||
490 | if (!conn_len) | |
491 | return 0; /* no connection */ | |
492 | ||
493 | if (conn_len == 1) { | |
494 | /* single connection */ | |
495 | err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0, | |
496 | &parm); | |
497 | if (err < 0) | |
498 | return err; | |
499 | if (conn_list) | |
500 | conn_list[0] = parm & mask; | |
501 | return 1; | |
502 | } | |
503 | ||
504 | /* multi connection */ | |
505 | conns = 0; | |
506 | prev_nid = 0; | |
507 | for (i = 0; i < conn_len; i++) { | |
508 | int range_val; | |
509 | hda_nid_t val, n; | |
510 | ||
511 | if (i % num_elems == 0) { | |
512 | err = snd_hdac_read(codec, nid, | |
513 | AC_VERB_GET_CONNECT_LIST, i, | |
514 | &parm); | |
515 | if (err < 0) | |
516 | return -EIO; | |
517 | } | |
518 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ | |
519 | val = parm & mask; | |
520 | if (val == 0 && null_count++) { /* no second chance */ | |
521 | dev_dbg(&codec->dev, | |
522 | "invalid CONNECT_LIST verb %x[%i]:%x\n", | |
523 | nid, i, parm); | |
524 | return 0; | |
525 | } | |
526 | parm >>= shift; | |
527 | if (range_val) { | |
528 | /* ranges between the previous and this one */ | |
529 | if (!prev_nid || prev_nid >= val) { | |
530 | dev_warn(&codec->dev, | |
531 | "invalid dep_range_val %x:%x\n", | |
532 | prev_nid, val); | |
533 | continue; | |
534 | } | |
535 | for (n = prev_nid + 1; n <= val; n++) { | |
536 | if (conn_list) { | |
537 | if (conns >= max_conns) | |
538 | return -ENOSPC; | |
539 | conn_list[conns] = n; | |
540 | } | |
541 | conns++; | |
542 | } | |
543 | } else { | |
544 | if (conn_list) { | |
545 | if (conns >= max_conns) | |
546 | return -ENOSPC; | |
547 | conn_list[conns] = val; | |
548 | } | |
549 | conns++; | |
550 | } | |
551 | prev_nid = val; | |
552 | } | |
553 | return conns; | |
554 | } | |
555 | EXPORT_SYMBOL_GPL(snd_hdac_get_connections); | |
556 | ||
557 | #ifdef CONFIG_PM | |
558 | /** | |
664c7155 | 559 | * snd_hdac_power_up - power up the codec |
7639a06c | 560 | * @codec: the codec object |
664c7155 TI |
561 | * |
562 | * This function calls the runtime PM helper to power up the given codec. | |
563 | * Unlike snd_hdac_power_up_pm(), you should call this only for the code | |
564 | * path that isn't included in PM path. Otherwise it gets stuck. | |
fbce23a0 TI |
565 | * |
566 | * Returns zero if successful, or a negative error code. | |
7639a06c | 567 | */ |
fbce23a0 | 568 | int snd_hdac_power_up(struct hdac_device *codec) |
7639a06c | 569 | { |
fbce23a0 | 570 | return pm_runtime_get_sync(&codec->dev); |
7639a06c TI |
571 | } |
572 | EXPORT_SYMBOL_GPL(snd_hdac_power_up); | |
573 | ||
574 | /** | |
664c7155 | 575 | * snd_hdac_power_down - power down the codec |
7639a06c | 576 | * @codec: the codec object |
fbce23a0 TI |
577 | * |
578 | * Returns zero if successful, or a negative error code. | |
7639a06c | 579 | */ |
fbce23a0 | 580 | int snd_hdac_power_down(struct hdac_device *codec) |
7639a06c TI |
581 | { |
582 | struct device *dev = &codec->dev; | |
583 | ||
7639a06c | 584 | pm_runtime_mark_last_busy(dev); |
fbce23a0 | 585 | return pm_runtime_put_autosuspend(dev); |
7639a06c TI |
586 | } |
587 | EXPORT_SYMBOL_GPL(snd_hdac_power_down); | |
c3aeda62 TI |
588 | |
589 | /** | |
590 | * snd_hdac_power_up_pm - power up the codec | |
591 | * @codec: the codec object | |
592 | * | |
593 | * This function can be called in a recursive code path like init code | |
594 | * which may be called by PM suspend/resume again. OTOH, if a power-up | |
595 | * call must wake up the sleeper (e.g. in a kctl callback), use | |
596 | * snd_hdac_power_up() instead. | |
fbce23a0 TI |
597 | * |
598 | * Returns zero if successful, or a negative error code. | |
c3aeda62 | 599 | */ |
fbce23a0 | 600 | int snd_hdac_power_up_pm(struct hdac_device *codec) |
c3aeda62 TI |
601 | { |
602 | if (!atomic_inc_not_zero(&codec->in_pm)) | |
fbce23a0 TI |
603 | return snd_hdac_power_up(codec); |
604 | return 0; | |
c3aeda62 TI |
605 | } |
606 | EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm); | |
607 | ||
fc4f000b TI |
608 | /* like snd_hdac_power_up_pm(), but only increment the pm count when |
609 | * already powered up. Returns -1 if not powered up, 1 if incremented | |
610 | * or 0 if unchanged. Only used in hdac_regmap.c | |
611 | */ | |
612 | int snd_hdac_keep_power_up(struct hdac_device *codec) | |
613 | { | |
614 | if (!atomic_inc_not_zero(&codec->in_pm)) { | |
c0ef3df8 | 615 | int ret = pm_runtime_get_if_active(&codec->dev); |
fc4f000b TI |
616 | if (!ret) |
617 | return -1; | |
618 | if (ret < 0) | |
619 | return 0; | |
620 | } | |
621 | return 1; | |
622 | } | |
623 | ||
c3aeda62 TI |
624 | /** |
625 | * snd_hdac_power_down_pm - power down the codec | |
626 | * @codec: the codec object | |
627 | * | |
628 | * Like snd_hdac_power_up_pm(), this function is used in a recursive | |
629 | * code path like init code which may be called by PM suspend/resume again. | |
fbce23a0 TI |
630 | * |
631 | * Returns zero if successful, or a negative error code. | |
c3aeda62 | 632 | */ |
fbce23a0 | 633 | int snd_hdac_power_down_pm(struct hdac_device *codec) |
c3aeda62 TI |
634 | { |
635 | if (atomic_dec_if_positive(&codec->in_pm) < 0) | |
fbce23a0 TI |
636 | return snd_hdac_power_down(codec); |
637 | return 0; | |
c3aeda62 TI |
638 | } |
639 | EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm); | |
7639a06c TI |
640 | #endif |
641 | ||
642 | /* codec vendor labels */ | |
643 | struct hda_vendor_id { | |
644 | unsigned int id; | |
645 | const char *name; | |
646 | }; | |
647 | ||
bf82326f | 648 | static const struct hda_vendor_id hda_vendor_ids[] = { |
28bd137a | 649 | { 0x0014, "Loongson" }, |
7639a06c TI |
650 | { 0x1002, "ATI" }, |
651 | { 0x1013, "Cirrus Logic" }, | |
652 | { 0x1057, "Motorola" }, | |
653 | { 0x1095, "Silicon Image" }, | |
654 | { 0x10de, "Nvidia" }, | |
655 | { 0x10ec, "Realtek" }, | |
656 | { 0x1102, "Creative" }, | |
657 | { 0x1106, "VIA" }, | |
658 | { 0x111d, "IDT" }, | |
659 | { 0x11c1, "LSI" }, | |
660 | { 0x11d4, "Analog Devices" }, | |
661 | { 0x13f6, "C-Media" }, | |
662 | { 0x14f1, "Conexant" }, | |
663 | { 0x17e8, "Chrontel" }, | |
664 | { 0x1854, "LG" }, | |
527f4643 | 665 | { 0x19e5, "Huawei" }, |
7639a06c TI |
666 | { 0x1aec, "Wolfson Microelectronics" }, |
667 | { 0x1af4, "QEMU" }, | |
668 | { 0x434d, "C-Media" }, | |
669 | { 0x8086, "Intel" }, | |
670 | { 0x8384, "SigmaTel" }, | |
671 | {} /* terminator */ | |
672 | }; | |
673 | ||
674 | /* store the codec vendor name */ | |
675 | static int get_codec_vendor_name(struct hdac_device *codec) | |
676 | { | |
677 | const struct hda_vendor_id *c; | |
678 | u16 vendor_id = codec->vendor_id >> 16; | |
679 | ||
680 | for (c = hda_vendor_ids; c->id; c++) { | |
681 | if (c->id == vendor_id) { | |
682 | codec->vendor_name = kstrdup(c->name, GFP_KERNEL); | |
683 | return codec->vendor_name ? 0 : -ENOMEM; | |
684 | } | |
685 | } | |
686 | ||
687 | codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id); | |
688 | return codec->vendor_name ? 0 : -ENOMEM; | |
689 | } | |
b7d023e1 TI |
690 | |
691 | /* | |
692 | * stream formats | |
693 | */ | |
694 | struct hda_rate_tbl { | |
695 | unsigned int hz; | |
696 | unsigned int alsa_bits; | |
697 | unsigned int hda_fmt; | |
698 | }; | |
699 | ||
700 | /* rate = base * mult / div */ | |
701 | #define HDA_RATE(base, mult, div) \ | |
702 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ | |
703 | (((div) - 1) << AC_FMT_DIV_SHIFT)) | |
704 | ||
bf82326f | 705 | static const struct hda_rate_tbl rate_bits[] = { |
b7d023e1 TI |
706 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
707 | ||
708 | /* autodetected value used in snd_hda_query_supported_pcm */ | |
709 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, | |
710 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, | |
711 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, | |
712 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, | |
713 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, | |
714 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, | |
715 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, | |
716 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, | |
717 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, | |
718 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, | |
719 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, | |
720 | #define AC_PAR_PCM_RATE_BITS 11 | |
721 | /* up to bits 10, 384kHZ isn't supported properly */ | |
722 | ||
723 | /* not autodetected value */ | |
724 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, | |
725 | ||
726 | { 0 } /* terminator */ | |
727 | }; | |
728 | ||
d24f1a09 CR |
729 | static snd_pcm_format_t snd_hdac_format_normalize(snd_pcm_format_t format) |
730 | { | |
731 | switch (format) { | |
732 | case SNDRV_PCM_FORMAT_S20_LE: | |
733 | case SNDRV_PCM_FORMAT_S24_LE: | |
734 | return SNDRV_PCM_FORMAT_S32_LE; | |
735 | ||
736 | case SNDRV_PCM_FORMAT_U20_LE: | |
737 | case SNDRV_PCM_FORMAT_U24_LE: | |
738 | return SNDRV_PCM_FORMAT_U32_LE; | |
739 | ||
740 | case SNDRV_PCM_FORMAT_S20_BE: | |
741 | case SNDRV_PCM_FORMAT_S24_BE: | |
742 | return SNDRV_PCM_FORMAT_S32_BE; | |
743 | ||
744 | case SNDRV_PCM_FORMAT_U20_BE: | |
745 | case SNDRV_PCM_FORMAT_U24_BE: | |
746 | return SNDRV_PCM_FORMAT_U32_BE; | |
747 | ||
748 | default: | |
749 | return format; | |
750 | } | |
751 | } | |
752 | ||
753 | /** | |
754 | * snd_hdac_stream_format_bits - obtain bits per sample value. | |
755 | * @format: the PCM format. | |
756 | * @subformat: the PCM subformat. | |
757 | * @maxbits: the maximum bits per sample. | |
758 | * | |
759 | * Return: The number of bits per sample. | |
760 | */ | |
761 | unsigned int snd_hdac_stream_format_bits(snd_pcm_format_t format, snd_pcm_subformat_t subformat, | |
762 | unsigned int maxbits) | |
763 | { | |
764 | struct snd_pcm_hw_params params; | |
765 | unsigned int bits; | |
766 | ||
767 | memset(¶ms, 0, sizeof(params)); | |
768 | ||
769 | params_set_format(¶ms, snd_hdac_format_normalize(format)); | |
770 | snd_mask_set(hw_param_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT), | |
771 | (__force unsigned int)subformat); | |
772 | ||
773 | bits = snd_pcm_hw_params_bits(¶ms); | |
774 | if (maxbits) | |
775 | return min(bits, maxbits); | |
776 | return bits; | |
777 | } | |
778 | EXPORT_SYMBOL_GPL(snd_hdac_stream_format_bits); | |
779 | ||
780 | /** | |
781 | * snd_hdac_stream_format - convert format parameters to SDxFMT value. | |
782 | * @channels: the number of channels. | |
783 | * @bits: bits per sample. | |
784 | * @rate: the sample rate. | |
785 | * | |
786 | * Return: The format bitset or zero if invalid. | |
787 | */ | |
788 | unsigned int snd_hdac_stream_format(unsigned int channels, unsigned int bits, unsigned int rate) | |
789 | { | |
790 | unsigned int val = 0; | |
791 | int i; | |
792 | ||
793 | for (i = 0; rate_bits[i].hz; i++) { | |
794 | if (rate_bits[i].hz == rate) { | |
795 | val = rate_bits[i].hda_fmt; | |
796 | break; | |
797 | } | |
798 | } | |
799 | ||
800 | if (!rate_bits[i].hz) | |
801 | return 0; | |
802 | ||
803 | if (channels == 0 || channels > 8) | |
804 | return 0; | |
805 | val |= channels - 1; | |
806 | ||
807 | switch (bits) { | |
808 | case 8: | |
809 | val |= AC_FMT_BITS_8; | |
810 | break; | |
811 | case 16: | |
812 | val |= AC_FMT_BITS_16; | |
813 | break; | |
814 | case 20: | |
815 | val |= AC_FMT_BITS_20; | |
816 | break; | |
817 | case 24: | |
818 | val |= AC_FMT_BITS_24; | |
819 | break; | |
820 | case 32: | |
821 | val |= AC_FMT_BITS_32; | |
822 | break; | |
823 | default: | |
824 | return 0; | |
825 | } | |
826 | ||
827 | return val; | |
828 | } | |
829 | EXPORT_SYMBOL_GPL(snd_hdac_stream_format); | |
830 | ||
831 | /** | |
832 | * snd_hdac_spdif_stream_format - convert format parameters to SDxFMT value. | |
833 | * @channels: the number of channels. | |
834 | * @bits: bits per sample. | |
835 | * @rate: the sample rate. | |
836 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant). | |
837 | * | |
838 | * Return: The format bitset or zero if invalid. | |
839 | */ | |
840 | unsigned int snd_hdac_spdif_stream_format(unsigned int channels, unsigned int bits, | |
841 | unsigned int rate, unsigned short spdif_ctls) | |
842 | { | |
843 | unsigned int val = snd_hdac_stream_format(channels, bits, rate); | |
844 | ||
845 | if (val && spdif_ctls & AC_DIG1_NONAUDIO) | |
846 | val |= AC_FMT_TYPE_NON_PCM; | |
847 | ||
848 | return val; | |
849 | } | |
850 | EXPORT_SYMBOL_GPL(snd_hdac_spdif_stream_format); | |
851 | ||
b7d023e1 TI |
852 | static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid) |
853 | { | |
854 | unsigned int val = 0; | |
855 | ||
856 | if (nid != codec->afg && | |
857 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) | |
858 | val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM); | |
859 | if (!val || val == -1) | |
860 | val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM); | |
861 | if (!val || val == -1) | |
862 | return 0; | |
863 | return val; | |
864 | } | |
865 | ||
866 | static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid) | |
867 | { | |
868 | unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM); | |
869 | ||
870 | if (!streams || streams == -1) | |
871 | streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM); | |
872 | if (!streams || streams == -1) | |
873 | return 0; | |
874 | return streams; | |
875 | } | |
876 | ||
877 | /** | |
878 | * snd_hdac_query_supported_pcm - query the supported PCM rates and formats | |
879 | * @codec: the codec object | |
880 | * @nid: NID to query | |
881 | * @ratesp: the pointer to store the detected rate bitflags | |
882 | * @formatsp: the pointer to store the detected formats | |
a7fc8b86 | 883 | * @subformatsp: the pointer to store the detected subformats for S32_LE format |
b7d023e1 TI |
884 | * @bpsp: the pointer to store the detected format widths |
885 | * | |
a7fc8b86 CR |
886 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp, |
887 | * @subformatsp or @bpsp argument is ignored. | |
b7d023e1 TI |
888 | * |
889 | * Returns 0 if successful, otherwise a negative error code. | |
890 | */ | |
891 | int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid, | |
a7fc8b86 CR |
892 | u32 *ratesp, u64 *formatsp, u32 *subformatsp, |
893 | unsigned int *bpsp) | |
b7d023e1 TI |
894 | { |
895 | unsigned int i, val, wcaps; | |
896 | ||
897 | wcaps = get_wcaps(codec, nid); | |
898 | val = query_pcm_param(codec, nid); | |
899 | ||
900 | if (ratesp) { | |
901 | u32 rates = 0; | |
902 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { | |
903 | if (val & (1 << i)) | |
904 | rates |= rate_bits[i].alsa_bits; | |
905 | } | |
906 | if (rates == 0) { | |
907 | dev_err(&codec->dev, | |
908 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", | |
909 | nid, val, | |
910 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); | |
911 | return -EIO; | |
912 | } | |
913 | *ratesp = rates; | |
914 | } | |
915 | ||
a7fc8b86 | 916 | if (formatsp || subformatsp || bpsp) { |
b7d023e1 | 917 | unsigned int streams, bps; |
a7fc8b86 CR |
918 | u32 subformats = 0; |
919 | u64 formats = 0; | |
b7d023e1 TI |
920 | |
921 | streams = query_stream_param(codec, nid); | |
922 | if (!streams) | |
923 | return -EIO; | |
924 | ||
925 | bps = 0; | |
926 | if (streams & AC_SUPFMT_PCM) { | |
927 | if (val & AC_SUPPCM_BITS_8) { | |
928 | formats |= SNDRV_PCM_FMTBIT_U8; | |
929 | bps = 8; | |
930 | } | |
931 | if (val & AC_SUPPCM_BITS_16) { | |
932 | formats |= SNDRV_PCM_FMTBIT_S16_LE; | |
933 | bps = 16; | |
934 | } | |
a7fc8b86 CR |
935 | if (val & AC_SUPPCM_BITS_20) { |
936 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
937 | subformats |= SNDRV_PCM_SUBFMTBIT_MSBITS_20; | |
938 | bps = 20; | |
939 | } | |
940 | if (val & AC_SUPPCM_BITS_24) { | |
941 | formats |= SNDRV_PCM_FMTBIT_S32_LE; | |
942 | subformats |= SNDRV_PCM_SUBFMTBIT_MSBITS_24; | |
943 | bps = 24; | |
944 | } | |
945 | if (val & AC_SUPPCM_BITS_32) { | |
946 | if (wcaps & AC_WCAP_DIGITAL) { | |
b7d023e1 | 947 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
a7fc8b86 | 948 | } else { |
b7d023e1 | 949 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
a7fc8b86 | 950 | subformats |= SNDRV_PCM_SUBFMTBIT_MSBITS_MAX; |
b7d023e1 | 951 | bps = 32; |
a7fc8b86 | 952 | } |
b7d023e1 TI |
953 | } |
954 | } | |
955 | #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ | |
956 | if (streams & AC_SUPFMT_FLOAT32) { | |
957 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; | |
958 | if (!bps) | |
959 | bps = 32; | |
960 | } | |
961 | #endif | |
962 | if (streams == AC_SUPFMT_AC3) { | |
963 | /* should be exclusive */ | |
964 | /* temporary hack: we have still no proper support | |
965 | * for the direct AC3 stream... | |
966 | */ | |
967 | formats |= SNDRV_PCM_FMTBIT_U8; | |
968 | bps = 8; | |
969 | } | |
970 | if (formats == 0) { | |
971 | dev_err(&codec->dev, | |
972 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", | |
973 | nid, val, | |
974 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, | |
975 | streams); | |
976 | return -EIO; | |
977 | } | |
978 | if (formatsp) | |
979 | *formatsp = formats; | |
a7fc8b86 CR |
980 | if (subformatsp) |
981 | *subformatsp = subformats; | |
b7d023e1 TI |
982 | if (bpsp) |
983 | *bpsp = bps; | |
984 | } | |
985 | ||
986 | return 0; | |
987 | } | |
988 | EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm); | |
989 | ||
990 | /** | |
991 | * snd_hdac_is_supported_format - Check the validity of the format | |
992 | * @codec: the codec object | |
993 | * @nid: NID to check | |
994 | * @format: the HD-audio format value to check | |
995 | * | |
996 | * Check whether the given node supports the format value. | |
997 | * | |
998 | * Returns true if supported, false if not. | |
999 | */ | |
1000 | bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid, | |
1001 | unsigned int format) | |
1002 | { | |
1003 | int i; | |
1004 | unsigned int val = 0, rate, stream; | |
1005 | ||
1006 | val = query_pcm_param(codec, nid); | |
1007 | if (!val) | |
1008 | return false; | |
1009 | ||
1010 | rate = format & 0xff00; | |
1011 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) | |
1012 | if (rate_bits[i].hda_fmt == rate) { | |
1013 | if (val & (1 << i)) | |
1014 | break; | |
1015 | return false; | |
1016 | } | |
1017 | if (i >= AC_PAR_PCM_RATE_BITS) | |
1018 | return false; | |
1019 | ||
1020 | stream = query_stream_param(codec, nid); | |
1021 | if (!stream) | |
1022 | return false; | |
1023 | ||
1024 | if (stream & AC_SUPFMT_PCM) { | |
1025 | switch (format & 0xf0) { | |
1026 | case 0x00: | |
1027 | if (!(val & AC_SUPPCM_BITS_8)) | |
1028 | return false; | |
1029 | break; | |
1030 | case 0x10: | |
1031 | if (!(val & AC_SUPPCM_BITS_16)) | |
1032 | return false; | |
1033 | break; | |
1034 | case 0x20: | |
1035 | if (!(val & AC_SUPPCM_BITS_20)) | |
1036 | return false; | |
1037 | break; | |
1038 | case 0x30: | |
1039 | if (!(val & AC_SUPPCM_BITS_24)) | |
1040 | return false; | |
1041 | break; | |
1042 | case 0x40: | |
1043 | if (!(val & AC_SUPPCM_BITS_32)) | |
1044 | return false; | |
1045 | break; | |
1046 | default: | |
1047 | return false; | |
1048 | } | |
1049 | } else { | |
1050 | /* FIXME: check for float32 and AC3? */ | |
1051 | } | |
1052 | ||
1053 | return true; | |
1054 | } | |
1055 | EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format); | |
1b5e6167 SP |
1056 | |
1057 | static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid, | |
1058 | int flags, unsigned int verb, unsigned int parm) | |
1059 | { | |
1060 | unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); | |
1061 | unsigned int res; | |
1062 | ||
1063 | if (snd_hdac_exec_verb(hdac, cmd, flags, &res)) | |
1064 | return -1; | |
1065 | ||
1066 | return res; | |
1067 | } | |
1068 | ||
1069 | static int codec_write(struct hdac_device *hdac, hda_nid_t nid, | |
1070 | int flags, unsigned int verb, unsigned int parm) | |
1071 | { | |
1072 | unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm); | |
1073 | ||
1074 | return snd_hdac_exec_verb(hdac, cmd, flags, NULL); | |
1075 | } | |
1076 | ||
1077 | /** | |
1078 | * snd_hdac_codec_read - send a command and get the response | |
1079 | * @hdac: the HDAC device | |
1080 | * @nid: NID to send the command | |
1081 | * @flags: optional bit flags | |
1082 | * @verb: the verb to send | |
1083 | * @parm: the parameter for the verb | |
1084 | * | |
1085 | * Send a single command and read the corresponding response. | |
1086 | * | |
1087 | * Returns the obtained response value, or -1 for an error. | |
1088 | */ | |
1089 | int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid, | |
1090 | int flags, unsigned int verb, unsigned int parm) | |
1091 | { | |
1092 | return codec_read(hdac, nid, flags, verb, parm); | |
1093 | } | |
1094 | EXPORT_SYMBOL_GPL(snd_hdac_codec_read); | |
1095 | ||
1096 | /** | |
1097 | * snd_hdac_codec_write - send a single command without waiting for response | |
1098 | * @hdac: the HDAC device | |
1099 | * @nid: NID to send the command | |
1100 | * @flags: optional bit flags | |
1101 | * @verb: the verb to send | |
1102 | * @parm: the parameter for the verb | |
1103 | * | |
1104 | * Send a single command without waiting for response. | |
1105 | * | |
1106 | * Returns 0 if successful, or a negative error code. | |
1107 | */ | |
1108 | int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid, | |
1109 | int flags, unsigned int verb, unsigned int parm) | |
1110 | { | |
1111 | return codec_write(hdac, nid, flags, verb, parm); | |
1112 | } | |
1113 | EXPORT_SYMBOL_GPL(snd_hdac_codec_write); | |
1114 | ||
78dd5e21 TI |
1115 | /** |
1116 | * snd_hdac_check_power_state - check whether the actual power state matches | |
1b5e6167 SP |
1117 | * with the target state |
1118 | * | |
1119 | * @hdac: the HDAC device | |
1120 | * @nid: NID to send the command | |
1121 | * @target_state: target state to check for | |
1122 | * | |
1123 | * Return true if state matches, false if not | |
1124 | */ | |
1125 | bool snd_hdac_check_power_state(struct hdac_device *hdac, | |
1126 | hda_nid_t nid, unsigned int target_state) | |
1127 | { | |
1128 | unsigned int state = codec_read(hdac, nid, 0, | |
1129 | AC_VERB_GET_POWER_STATE, 0); | |
1130 | ||
1131 | if (state & AC_PWRST_ERROR) | |
1132 | return true; | |
1133 | state = (state >> 4) & 0x0f; | |
1134 | return (state == target_state); | |
1135 | } | |
1136 | EXPORT_SYMBOL_GPL(snd_hdac_check_power_state); | |
09787492 AK |
1137 | /** |
1138 | * snd_hdac_sync_power_state - wait until actual power state matches | |
1139 | * with the target state | |
1140 | * | |
6e57188f | 1141 | * @codec: the HDAC device |
09787492 | 1142 | * @nid: NID to send the command |
6e57188f | 1143 | * @power_state: target power state to wait for |
09787492 AK |
1144 | * |
1145 | * Return power state or PS_ERROR if codec rejects GET verb. | |
1146 | */ | |
1147 | unsigned int snd_hdac_sync_power_state(struct hdac_device *codec, | |
1148 | hda_nid_t nid, unsigned int power_state) | |
1149 | { | |
1150 | unsigned long end_time = jiffies + msecs_to_jiffies(500); | |
1151 | unsigned int state, actual_state, count; | |
1152 | ||
1153 | for (count = 0; count < 500; count++) { | |
1154 | state = snd_hdac_codec_read(codec, nid, 0, | |
1155 | AC_VERB_GET_POWER_STATE, 0); | |
1156 | if (state & AC_PWRST_ERROR) { | |
1157 | msleep(20); | |
1158 | break; | |
1159 | } | |
1160 | actual_state = (state >> 4) & 0x0f; | |
1161 | if (actual_state == power_state) | |
1162 | break; | |
1163 | if (time_after_eq(jiffies, end_time)) | |
1164 | break; | |
1165 | /* wait until the codec reachs to the target state */ | |
1166 | msleep(1); | |
1167 | } | |
1168 | return state; | |
1169 | } | |
1170 | EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state); |