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