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