b2d58998dbddbb6c99e0b93c2a9b390565c8f0a6
[deliverable/linux.git] / sound / pci / hda / hda_codec.c
1 /*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38 #include <sound/hda_hwdep.h>
39
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
42
43 /*
44 * vendor / preset table
45 */
46
47 struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50 };
51
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
54 { 0x1002, "ATI" },
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
58 { 0x10de, "Nvidia" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
61 { 0x1106, "VIA" },
62 { 0x111d, "IDT" },
63 { 0x11c1, "LSI" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x1af4, "QEMU" },
71 { 0x434d, "C-Media" },
72 { 0x8086, "Intel" },
73 { 0x8384, "SigmaTel" },
74 {} /* terminator */
75 };
76
77 static DEFINE_MUTEX(preset_mutex);
78 static LIST_HEAD(hda_preset_tables);
79
80 /**
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
83 */
84 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
85 {
86 mutex_lock(&preset_mutex);
87 list_add_tail(&preset->list, &hda_preset_tables);
88 mutex_unlock(&preset_mutex);
89 return 0;
90 }
91 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
92
93 /**
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
96 */
97 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
98 {
99 mutex_lock(&preset_mutex);
100 list_del(&preset->list);
101 mutex_unlock(&preset_mutex);
102 return 0;
103 }
104 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
105
106 #ifdef CONFIG_PM
107 #define codec_in_pm(codec) ((codec)->in_pm)
108 static void hda_power_work(struct work_struct *work);
109 static void hda_keep_power_on(struct hda_codec *codec);
110 #define hda_codec_is_power_on(codec) ((codec)->power_on)
111
112 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
113 {
114 struct hda_bus *bus = codec->bus;
115
116 if ((power_up && codec->pm_up_notified) ||
117 (!power_up && !codec->pm_up_notified))
118 return;
119 if (bus->ops.pm_notify)
120 bus->ops.pm_notify(bus, power_up);
121 codec->pm_up_notified = power_up;
122 }
123
124 #else
125 #define codec_in_pm(codec) 0
126 static inline void hda_keep_power_on(struct hda_codec *codec) {}
127 #define hda_codec_is_power_on(codec) 1
128 #define hda_call_pm_notify(codec, state) {}
129 #endif
130
131 /**
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
134 *
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
137 */
138 const char *snd_hda_get_jack_location(u32 cfg)
139 {
140 static char *bases[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
142 };
143 static unsigned char specials_idx[] = {
144 0x07, 0x08,
145 0x17, 0x18, 0x19,
146 0x37, 0x38
147 };
148 static char *specials[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
152 };
153 int i;
154 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
155 if ((cfg & 0x0f) < 7)
156 return bases[cfg & 0x0f];
157 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
158 if (cfg == specials_idx[i])
159 return specials[i];
160 }
161 return "UNKNOWN";
162 }
163 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
164
165 /**
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
168 *
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
171 */
172 const char *snd_hda_get_jack_connectivity(u32 cfg)
173 {
174 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
175
176 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
177 }
178 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
179
180 /**
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
183 *
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
186 */
187 const char *snd_hda_get_jack_type(u32 cfg)
188 {
189 static char *jack_types[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
193 "SPDIF In", "Digital In", "Reserved", "Other"
194 };
195
196 return jack_types[(cfg & AC_DEFCFG_DEVICE)
197 >> AC_DEFCFG_DEVICE_SHIFT];
198 }
199 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
200
201 /*
202 * Compose a 32bit command word to be sent to the HD-audio controller
203 */
204 static inline unsigned int
205 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
206 unsigned int verb, unsigned int parm)
207 {
208 u32 val;
209
210 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
211 (verb & ~0xfff) || (parm & ~0xffff)) {
212 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
213 codec->addr, nid, verb, parm);
214 return ~0;
215 }
216
217 val = (u32)codec->addr << 28;
218 val |= (u32)nid << 20;
219 val |= verb << 8;
220 val |= parm;
221 return val;
222 }
223
224 /*
225 * Send and receive a verb
226 */
227 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
228 int flags, unsigned int *res)
229 {
230 struct hda_bus *bus = codec->bus;
231 int err;
232
233 if (cmd == ~0)
234 return -1;
235
236 if (res)
237 *res = -1;
238 again:
239 snd_hda_power_up(codec);
240 mutex_lock(&bus->cmd_mutex);
241 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
242 bus->no_response_fallback = 1;
243 for (;;) {
244 trace_hda_send_cmd(codec, cmd);
245 err = bus->ops.command(bus, cmd);
246 if (err != -EAGAIN)
247 break;
248 /* process pending verbs */
249 bus->ops.get_response(bus, codec->addr);
250 }
251 if (!err && res) {
252 *res = bus->ops.get_response(bus, codec->addr);
253 trace_hda_get_response(codec, *res);
254 }
255 bus->no_response_fallback = 0;
256 mutex_unlock(&bus->cmd_mutex);
257 snd_hda_power_down(codec);
258 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
259 if (bus->response_reset) {
260 codec_dbg(codec,
261 "resetting BUS due to fatal communication error\n");
262 trace_hda_bus_reset(bus);
263 bus->ops.bus_reset(bus);
264 }
265 goto again;
266 }
267 /* clear reset-flag when the communication gets recovered */
268 if (!err || codec_in_pm(codec))
269 bus->response_reset = 0;
270 return err;
271 }
272
273 /**
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
277 * @flags: optional bit flags
278 * @verb: the verb to send
279 * @parm: the parameter for the verb
280 *
281 * Send a single command and read the corresponding response.
282 *
283 * Returns the obtained response value, or -1 for an error.
284 */
285 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
286 int flags,
287 unsigned int verb, unsigned int parm)
288 {
289 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
290 unsigned int res;
291 if (codec_exec_verb(codec, cmd, flags, &res))
292 return -1;
293 return res;
294 }
295 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
296
297 /**
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
301 * @flags: optional bit flags
302 * @verb: the verb to send
303 * @parm: the parameter for the verb
304 *
305 * Send a single command without waiting for response.
306 *
307 * Returns 0 if successful, or a negative error code.
308 */
309 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
310 unsigned int verb, unsigned int parm)
311 {
312 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
313 unsigned int res;
314 return codec_exec_verb(codec, cmd, flags,
315 codec->bus->sync_write ? &res : NULL);
316 }
317 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
318
319 /**
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
323 *
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
326 */
327 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
328 {
329 for (; seq->nid; seq++)
330 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
331 }
332 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
333
334 /**
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
337 * @nid: NID to parse
338 * @start_id: the pointer to store the start NID
339 *
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
342 */
343 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *start_id)
345 {
346 unsigned int parm;
347
348 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
349 if (parm == -1)
350 return 0;
351 *start_id = (parm >> 16) & 0x7fff;
352 return (int)(parm & 0x7fff);
353 }
354 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
355
356 /* connection list element */
357 struct hda_conn_list {
358 struct list_head list;
359 int len;
360 hda_nid_t nid;
361 hda_nid_t conns[0];
362 };
363
364 /* look up the cached results */
365 static struct hda_conn_list *
366 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
367 {
368 struct hda_conn_list *p;
369 list_for_each_entry(p, &codec->conn_list, list) {
370 if (p->nid == nid)
371 return p;
372 }
373 return NULL;
374 }
375
376 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
377 const hda_nid_t *list)
378 {
379 struct hda_conn_list *p;
380
381 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
382 if (!p)
383 return -ENOMEM;
384 p->len = len;
385 p->nid = nid;
386 memcpy(p->conns, list, len * sizeof(hda_nid_t));
387 list_add(&p->list, &codec->conn_list);
388 return 0;
389 }
390
391 static void remove_conn_list(struct hda_codec *codec)
392 {
393 while (!list_empty(&codec->conn_list)) {
394 struct hda_conn_list *p;
395 p = list_first_entry(&codec->conn_list, typeof(*p), list);
396 list_del(&p->list);
397 kfree(p);
398 }
399 }
400
401 /* read the connection and add to the cache */
402 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
403 {
404 hda_nid_t list[32];
405 hda_nid_t *result = list;
406 int len;
407
408 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
409 if (len == -ENOSPC) {
410 len = snd_hda_get_num_raw_conns(codec, nid);
411 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
412 if (!result)
413 return -ENOMEM;
414 len = snd_hda_get_raw_connections(codec, nid, result, len);
415 }
416 if (len >= 0)
417 len = snd_hda_override_conn_list(codec, nid, len, result);
418 if (result != list)
419 kfree(result);
420 return len;
421 }
422
423 /**
424 * snd_hda_get_conn_list - get connection list
425 * @codec: the HDA codec
426 * @nid: NID to parse
427 * @listp: the pointer to store NID list
428 *
429 * Parses the connection list of the given widget and stores the pointer
430 * to the list of NIDs.
431 *
432 * Returns the number of connections, or a negative error code.
433 *
434 * Note that the returned pointer isn't protected against the list
435 * modification. If snd_hda_override_conn_list() might be called
436 * concurrently, protect with a mutex appropriately.
437 */
438 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
439 const hda_nid_t **listp)
440 {
441 bool added = false;
442
443 for (;;) {
444 int err;
445 const struct hda_conn_list *p;
446
447 /* if the connection-list is already cached, read it */
448 p = lookup_conn_list(codec, nid);
449 if (p) {
450 if (listp)
451 *listp = p->conns;
452 return p->len;
453 }
454 if (snd_BUG_ON(added))
455 return -EINVAL;
456
457 err = read_and_add_raw_conns(codec, nid);
458 if (err < 0)
459 return err;
460 added = true;
461 }
462 }
463 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
464
465 /**
466 * snd_hda_get_connections - copy connection list
467 * @codec: the HDA codec
468 * @nid: NID to parse
469 * @conn_list: connection list array; when NULL, checks only the size
470 * @max_conns: max. number of connections to store
471 *
472 * Parses the connection list of the given widget and stores the list
473 * of NIDs.
474 *
475 * Returns the number of connections, or a negative error code.
476 */
477 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
478 hda_nid_t *conn_list, int max_conns)
479 {
480 const hda_nid_t *list;
481 int len = snd_hda_get_conn_list(codec, nid, &list);
482
483 if (len > 0 && conn_list) {
484 if (len > max_conns) {
485 codec_err(codec, "Too many connections %d for NID 0x%x\n",
486 len, nid);
487 return -EINVAL;
488 }
489 memcpy(conn_list, list, len * sizeof(hda_nid_t));
490 }
491
492 return len;
493 }
494 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
495
496 /* return CONNLIST_LEN parameter of the given widget */
497 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
498 {
499 unsigned int wcaps = get_wcaps(codec, nid);
500 unsigned int parm;
501
502 if (!(wcaps & AC_WCAP_CONN_LIST) &&
503 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
504 return 0;
505
506 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
507 if (parm == -1)
508 parm = 0;
509 return parm;
510 }
511
512 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
513 {
514 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
515 }
516
517 /**
518 * snd_hda_get_raw_connections - copy connection list without cache
519 * @codec: the HDA codec
520 * @nid: NID to parse
521 * @conn_list: connection list array
522 * @max_conns: max. number of connections to store
523 *
524 * Like snd_hda_get_connections(), copy the connection list but without
525 * checking through the connection-list cache.
526 * Currently called only from hda_proc.c, so not exported.
527 */
528 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
529 hda_nid_t *conn_list, int max_conns)
530 {
531 unsigned int parm;
532 int i, conn_len, conns;
533 unsigned int shift, num_elems, mask;
534 hda_nid_t prev_nid;
535 int null_count = 0;
536
537 parm = get_num_conns(codec, nid);
538 if (!parm)
539 return 0;
540
541 if (parm & AC_CLIST_LONG) {
542 /* long form */
543 shift = 16;
544 num_elems = 2;
545 } else {
546 /* short form */
547 shift = 8;
548 num_elems = 4;
549 }
550 conn_len = parm & AC_CLIST_LENGTH;
551 mask = (1 << (shift-1)) - 1;
552
553 if (!conn_len)
554 return 0; /* no connection */
555
556 if (conn_len == 1) {
557 /* single connection */
558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, 0);
560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
562 if (conn_list)
563 conn_list[0] = parm & mask;
564 return 1;
565 }
566
567 /* multi connection */
568 conns = 0;
569 prev_nid = 0;
570 for (i = 0; i < conn_len; i++) {
571 int range_val;
572 hda_nid_t val, n;
573
574 if (i % num_elems == 0) {
575 parm = snd_hda_codec_read(codec, nid, 0,
576 AC_VERB_GET_CONNECT_LIST, i);
577 if (parm == -1 && codec->bus->rirb_error)
578 return -EIO;
579 }
580 range_val = !!(parm & (1 << (shift-1))); /* ranges */
581 val = parm & mask;
582 if (val == 0 && null_count++) { /* no second chance */
583 codec_dbg(codec,
584 "invalid CONNECT_LIST verb %x[%i]:%x\n",
585 nid, i, parm);
586 return 0;
587 }
588 parm >>= shift;
589 if (range_val) {
590 /* ranges between the previous and this one */
591 if (!prev_nid || prev_nid >= val) {
592 codec_warn(codec,
593 "invalid dep_range_val %x:%x\n",
594 prev_nid, val);
595 continue;
596 }
597 for (n = prev_nid + 1; n <= val; n++) {
598 if (conn_list) {
599 if (conns >= max_conns)
600 return -ENOSPC;
601 conn_list[conns] = n;
602 }
603 conns++;
604 }
605 } else {
606 if (conn_list) {
607 if (conns >= max_conns)
608 return -ENOSPC;
609 conn_list[conns] = val;
610 }
611 conns++;
612 }
613 prev_nid = val;
614 }
615 return conns;
616 }
617
618 /**
619 * snd_hda_override_conn_list - add/modify the connection-list to cache
620 * @codec: the HDA codec
621 * @nid: NID to parse
622 * @len: number of connection list entries
623 * @list: the list of connection entries
624 *
625 * Add or modify the given connection-list to the cache. If the corresponding
626 * cache already exists, invalidate it and append a new one.
627 *
628 * Returns zero or a negative error code.
629 */
630 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
631 const hda_nid_t *list)
632 {
633 struct hda_conn_list *p;
634
635 p = lookup_conn_list(codec, nid);
636 if (p) {
637 list_del(&p->list);
638 kfree(p);
639 }
640
641 return add_conn_list(codec, nid, len, list);
642 }
643 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
644
645 /**
646 * snd_hda_get_conn_index - get the connection index of the given NID
647 * @codec: the HDA codec
648 * @mux: NID containing the list
649 * @nid: NID to select
650 * @recursive: 1 when searching NID recursively, otherwise 0
651 *
652 * Parses the connection list of the widget @mux and checks whether the
653 * widget @nid is present. If it is, return the connection index.
654 * Otherwise it returns -1.
655 */
656 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
657 hda_nid_t nid, int recursive)
658 {
659 const hda_nid_t *conn;
660 int i, nums;
661
662 nums = snd_hda_get_conn_list(codec, mux, &conn);
663 for (i = 0; i < nums; i++)
664 if (conn[i] == nid)
665 return i;
666 if (!recursive)
667 return -1;
668 if (recursive > 10) {
669 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
670 return -1;
671 }
672 recursive++;
673 for (i = 0; i < nums; i++) {
674 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
675 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
676 continue;
677 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
678 return i;
679 }
680 return -1;
681 }
682 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
683
684
685 /* return DEVLIST_LEN parameter of the given widget */
686 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
687 {
688 unsigned int wcaps = get_wcaps(codec, nid);
689 unsigned int parm;
690
691 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
692 get_wcaps_type(wcaps) != AC_WID_PIN)
693 return 0;
694
695 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
696 if (parm == -1 && codec->bus->rirb_error)
697 parm = 0;
698 return parm & AC_DEV_LIST_LEN_MASK;
699 }
700
701 /**
702 * snd_hda_get_devices - copy device list without cache
703 * @codec: the HDA codec
704 * @nid: NID of the pin to parse
705 * @dev_list: device list array
706 * @max_devices: max. number of devices to store
707 *
708 * Copy the device list. This info is dynamic and so not cached.
709 * Currently called only from hda_proc.c, so not exported.
710 */
711 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
712 u8 *dev_list, int max_devices)
713 {
714 unsigned int parm;
715 int i, dev_len, devices;
716
717 parm = get_num_devices(codec, nid);
718 if (!parm) /* not multi-stream capable */
719 return 0;
720
721 dev_len = parm + 1;
722 dev_len = dev_len < max_devices ? dev_len : max_devices;
723
724 devices = 0;
725 while (devices < dev_len) {
726 parm = snd_hda_codec_read(codec, nid, 0,
727 AC_VERB_GET_DEVICE_LIST, devices);
728 if (parm == -1 && codec->bus->rirb_error)
729 break;
730
731 for (i = 0; i < 8; i++) {
732 dev_list[devices] = (u8)parm;
733 parm >>= 4;
734 devices++;
735 if (devices >= dev_len)
736 break;
737 }
738 }
739 return devices;
740 }
741
742 /**
743 * snd_hda_queue_unsol_event - add an unsolicited event to queue
744 * @bus: the BUS
745 * @res: unsolicited event (lower 32bit of RIRB entry)
746 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
747 *
748 * Adds the given event to the queue. The events are processed in
749 * the workqueue asynchronously. Call this function in the interrupt
750 * hanlder when RIRB receives an unsolicited event.
751 *
752 * Returns 0 if successful, or a negative error code.
753 */
754 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
755 {
756 struct hda_bus_unsolicited *unsol;
757 unsigned int wp;
758
759 if (!bus || !bus->workq)
760 return 0;
761
762 trace_hda_unsol_event(bus, res, res_ex);
763 unsol = bus->unsol;
764 if (!unsol)
765 return 0;
766
767 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
768 unsol->wp = wp;
769
770 wp <<= 1;
771 unsol->queue[wp] = res;
772 unsol->queue[wp + 1] = res_ex;
773
774 queue_work(bus->workq, &unsol->work);
775
776 return 0;
777 }
778 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
779
780 /*
781 * process queued unsolicited events
782 */
783 static void process_unsol_events(struct work_struct *work)
784 {
785 struct hda_bus_unsolicited *unsol =
786 container_of(work, struct hda_bus_unsolicited, work);
787 struct hda_bus *bus = unsol->bus;
788 struct hda_codec *codec;
789 unsigned int rp, caddr, res;
790
791 while (unsol->rp != unsol->wp) {
792 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
793 unsol->rp = rp;
794 rp <<= 1;
795 res = unsol->queue[rp];
796 caddr = unsol->queue[rp + 1];
797 if (!(caddr & (1 << 4))) /* no unsolicited event? */
798 continue;
799 codec = bus->caddr_tbl[caddr & 0x0f];
800 if (codec && codec->patch_ops.unsol_event)
801 codec->patch_ops.unsol_event(codec, res);
802 }
803 }
804
805 /*
806 * initialize unsolicited queue
807 */
808 static int init_unsol_queue(struct hda_bus *bus)
809 {
810 struct hda_bus_unsolicited *unsol;
811
812 if (bus->unsol) /* already initialized */
813 return 0;
814
815 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
816 if (!unsol) {
817 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
818 return -ENOMEM;
819 }
820 INIT_WORK(&unsol->work, process_unsol_events);
821 unsol->bus = bus;
822 bus->unsol = unsol;
823 return 0;
824 }
825
826 /*
827 * destructor
828 */
829 static void snd_hda_bus_free(struct hda_bus *bus)
830 {
831 if (!bus)
832 return;
833
834 WARN_ON(!list_empty(&bus->codec_list));
835 if (bus->workq)
836 flush_workqueue(bus->workq);
837 kfree(bus->unsol);
838 if (bus->ops.private_free)
839 bus->ops.private_free(bus);
840 if (bus->workq)
841 destroy_workqueue(bus->workq);
842
843 kfree(bus);
844 }
845
846 static int snd_hda_bus_dev_free(struct snd_device *device)
847 {
848 snd_hda_bus_free(device->device_data);
849 return 0;
850 }
851
852 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
853 {
854 struct hda_bus *bus = device->device_data;
855 bus->shutdown = 1;
856 return 0;
857 }
858
859 /**
860 * snd_hda_bus_new - create a HDA bus
861 * @card: the card entry
862 * @temp: the template for hda_bus information
863 * @busp: the pointer to store the created bus instance
864 *
865 * Returns 0 if successful, or a negative error code.
866 */
867 int snd_hda_bus_new(struct snd_card *card,
868 const struct hda_bus_template *temp,
869 struct hda_bus **busp)
870 {
871 struct hda_bus *bus;
872 int err;
873 static struct snd_device_ops dev_ops = {
874 .dev_disconnect = snd_hda_bus_dev_disconnect,
875 .dev_free = snd_hda_bus_dev_free,
876 };
877
878 if (snd_BUG_ON(!temp))
879 return -EINVAL;
880 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
881 return -EINVAL;
882
883 if (busp)
884 *busp = NULL;
885
886 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
887 if (bus == NULL) {
888 dev_err(card->dev, "can't allocate struct hda_bus\n");
889 return -ENOMEM;
890 }
891
892 bus->card = card;
893 bus->private_data = temp->private_data;
894 bus->pci = temp->pci;
895 bus->modelname = temp->modelname;
896 bus->power_save = temp->power_save;
897 bus->ops = temp->ops;
898
899 mutex_init(&bus->cmd_mutex);
900 mutex_init(&bus->prepare_mutex);
901 INIT_LIST_HEAD(&bus->codec_list);
902
903 snprintf(bus->workq_name, sizeof(bus->workq_name),
904 "hd-audio%d", card->number);
905 bus->workq = create_singlethread_workqueue(bus->workq_name);
906 if (!bus->workq) {
907 dev_err(card->dev, "cannot create workqueue %s\n",
908 bus->workq_name);
909 kfree(bus);
910 return -ENOMEM;
911 }
912
913 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
914 if (err < 0) {
915 snd_hda_bus_free(bus);
916 return err;
917 }
918 if (busp)
919 *busp = bus;
920 return 0;
921 }
922 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
923
924 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
925 #define is_generic_config(codec) \
926 (codec->modelname && !strcmp(codec->modelname, "generic"))
927 #else
928 #define is_generic_config(codec) 0
929 #endif
930
931 #ifdef MODULE
932 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
933 #else
934 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
935 #endif
936
937 /*
938 * find a matching codec preset
939 */
940 static const struct hda_codec_preset *
941 find_codec_preset(struct hda_codec *codec)
942 {
943 struct hda_codec_preset_list *tbl;
944 const struct hda_codec_preset *preset;
945 unsigned int mod_requested = 0;
946
947 again:
948 mutex_lock(&preset_mutex);
949 list_for_each_entry(tbl, &hda_preset_tables, list) {
950 if (!try_module_get(tbl->owner)) {
951 codec_err(codec, "cannot module_get\n");
952 continue;
953 }
954 for (preset = tbl->preset; preset->id; preset++) {
955 u32 mask = preset->mask;
956 if (preset->afg && preset->afg != codec->afg)
957 continue;
958 if (preset->mfg && preset->mfg != codec->mfg)
959 continue;
960 if (!mask)
961 mask = ~0;
962 if (preset->id == (codec->vendor_id & mask) &&
963 (!preset->rev ||
964 preset->rev == codec->revision_id)) {
965 mutex_unlock(&preset_mutex);
966 codec->owner = tbl->owner;
967 return preset;
968 }
969 }
970 module_put(tbl->owner);
971 }
972 mutex_unlock(&preset_mutex);
973
974 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
975 if (!mod_requested)
976 request_module("snd-hda-codec-id:%08x",
977 codec->vendor_id);
978 else
979 request_module("snd-hda-codec-id:%04x*",
980 (codec->vendor_id >> 16) & 0xffff);
981 mod_requested++;
982 goto again;
983 }
984 return NULL;
985 }
986
987 /*
988 * get_codec_name - store the codec name
989 */
990 static int get_codec_name(struct hda_codec *codec)
991 {
992 const struct hda_vendor_id *c;
993 const char *vendor = NULL;
994 u16 vendor_id = codec->vendor_id >> 16;
995 char tmp[16];
996
997 if (codec->vendor_name)
998 goto get_chip_name;
999
1000 for (c = hda_vendor_ids; c->id; c++) {
1001 if (c->id == vendor_id) {
1002 vendor = c->name;
1003 break;
1004 }
1005 }
1006 if (!vendor) {
1007 sprintf(tmp, "Generic %04x", vendor_id);
1008 vendor = tmp;
1009 }
1010 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1011 if (!codec->vendor_name)
1012 return -ENOMEM;
1013
1014 get_chip_name:
1015 if (codec->chip_name)
1016 return 0;
1017
1018 if (codec->preset && codec->preset->name)
1019 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1020 else {
1021 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1022 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1023 }
1024 if (!codec->chip_name)
1025 return -ENOMEM;
1026 return 0;
1027 }
1028
1029 /*
1030 * look for an AFG and MFG nodes
1031 */
1032 static void setup_fg_nodes(struct hda_codec *codec)
1033 {
1034 int i, total_nodes, function_id;
1035 hda_nid_t nid;
1036
1037 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1038 for (i = 0; i < total_nodes; i++, nid++) {
1039 function_id = snd_hda_param_read(codec, nid,
1040 AC_PAR_FUNCTION_TYPE);
1041 switch (function_id & 0xff) {
1042 case AC_GRP_AUDIO_FUNCTION:
1043 codec->afg = nid;
1044 codec->afg_function_id = function_id & 0xff;
1045 codec->afg_unsol = (function_id >> 8) & 1;
1046 break;
1047 case AC_GRP_MODEM_FUNCTION:
1048 codec->mfg = nid;
1049 codec->mfg_function_id = function_id & 0xff;
1050 codec->mfg_unsol = (function_id >> 8) & 1;
1051 break;
1052 default:
1053 break;
1054 }
1055 }
1056 }
1057
1058 /*
1059 * read widget caps for each widget and store in cache
1060 */
1061 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1062 {
1063 int i;
1064 hda_nid_t nid;
1065
1066 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1067 &codec->start_nid);
1068 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1069 if (!codec->wcaps)
1070 return -ENOMEM;
1071 nid = codec->start_nid;
1072 for (i = 0; i < codec->num_nodes; i++, nid++)
1073 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1074 AC_PAR_AUDIO_WIDGET_CAP);
1075 return 0;
1076 }
1077
1078 /* read all pin default configurations and save codec->init_pins */
1079 static int read_pin_defaults(struct hda_codec *codec)
1080 {
1081 int i;
1082 hda_nid_t nid = codec->start_nid;
1083
1084 for (i = 0; i < codec->num_nodes; i++, nid++) {
1085 struct hda_pincfg *pin;
1086 unsigned int wcaps = get_wcaps(codec, nid);
1087 unsigned int wid_type = get_wcaps_type(wcaps);
1088 if (wid_type != AC_WID_PIN)
1089 continue;
1090 pin = snd_array_new(&codec->init_pins);
1091 if (!pin)
1092 return -ENOMEM;
1093 pin->nid = nid;
1094 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1095 AC_VERB_GET_CONFIG_DEFAULT, 0);
1096 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1097 AC_VERB_GET_PIN_WIDGET_CONTROL,
1098 0);
1099 }
1100 return 0;
1101 }
1102
1103 /* look up the given pin config list and return the item matching with NID */
1104 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1105 struct snd_array *array,
1106 hda_nid_t nid)
1107 {
1108 int i;
1109 for (i = 0; i < array->used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(array, i);
1111 if (pin->nid == nid)
1112 return pin;
1113 }
1114 return NULL;
1115 }
1116
1117 /* set the current pin config value for the given NID.
1118 * the value is cached, and read via snd_hda_codec_get_pincfg()
1119 */
1120 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1121 hda_nid_t nid, unsigned int cfg)
1122 {
1123 struct hda_pincfg *pin;
1124
1125 /* the check below may be invalid when pins are added by a fixup
1126 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1127 * for now
1128 */
1129 /*
1130 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1131 return -EINVAL;
1132 */
1133
1134 pin = look_up_pincfg(codec, list, nid);
1135 if (!pin) {
1136 pin = snd_array_new(list);
1137 if (!pin)
1138 return -ENOMEM;
1139 pin->nid = nid;
1140 }
1141 pin->cfg = cfg;
1142 return 0;
1143 }
1144
1145 /**
1146 * snd_hda_codec_set_pincfg - Override a pin default configuration
1147 * @codec: the HDA codec
1148 * @nid: NID to set the pin config
1149 * @cfg: the pin default config value
1150 *
1151 * Override a pin default configuration value in the cache.
1152 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1153 * priority than the real hardware value.
1154 */
1155 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1156 hda_nid_t nid, unsigned int cfg)
1157 {
1158 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1159 }
1160 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
1161
1162 /**
1163 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1164 * @codec: the HDA codec
1165 * @nid: NID to get the pin config
1166 *
1167 * Get the current pin config value of the given pin NID.
1168 * If the pincfg value is cached or overridden via sysfs or driver,
1169 * returns the cached value.
1170 */
1171 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1172 {
1173 struct hda_pincfg *pin;
1174
1175 #ifdef CONFIG_SND_HDA_RECONFIG
1176 {
1177 unsigned int cfg = 0;
1178 mutex_lock(&codec->user_mutex);
1179 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1180 if (pin)
1181 cfg = pin->cfg;
1182 mutex_unlock(&codec->user_mutex);
1183 if (cfg)
1184 return cfg;
1185 }
1186 #endif
1187 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1188 if (pin)
1189 return pin->cfg;
1190 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1191 if (pin)
1192 return pin->cfg;
1193 return 0;
1194 }
1195 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
1196
1197 /**
1198 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1199 * @codec: the HDA codec
1200 * @nid: pin NID
1201 * @val: assigned pinctl value
1202 *
1203 * This function stores the given value to a pinctl target value in the
1204 * pincfg table. This isn't always as same as the actually written value
1205 * but can be referred at any time via snd_hda_codec_get_pin_target().
1206 */
1207 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1208 unsigned int val)
1209 {
1210 struct hda_pincfg *pin;
1211
1212 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1213 if (!pin)
1214 return -EINVAL;
1215 pin->target = val;
1216 return 0;
1217 }
1218 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1219
1220 /**
1221 * snd_hda_codec_get_pin_target - return the current pinctl target value
1222 * @codec: the HDA codec
1223 * @nid: pin NID
1224 */
1225 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1226 {
1227 struct hda_pincfg *pin;
1228
1229 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1230 if (!pin)
1231 return 0;
1232 return pin->target;
1233 }
1234 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1235
1236 /**
1237 * snd_hda_shutup_pins - Shut up all pins
1238 * @codec: the HDA codec
1239 *
1240 * Clear all pin controls to shup up before suspend for avoiding click noise.
1241 * The controls aren't cached so that they can be resumed properly.
1242 */
1243 void snd_hda_shutup_pins(struct hda_codec *codec)
1244 {
1245 int i;
1246 /* don't shut up pins when unloading the driver; otherwise it breaks
1247 * the default pin setup at the next load of the driver
1248 */
1249 if (codec->bus->shutdown)
1250 return;
1251 for (i = 0; i < codec->init_pins.used; i++) {
1252 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1253 /* use read here for syncing after issuing each verb */
1254 snd_hda_codec_read(codec, pin->nid, 0,
1255 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1256 }
1257 codec->pins_shutup = 1;
1258 }
1259 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1260
1261 #ifdef CONFIG_PM
1262 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1263 static void restore_shutup_pins(struct hda_codec *codec)
1264 {
1265 int i;
1266 if (!codec->pins_shutup)
1267 return;
1268 if (codec->bus->shutdown)
1269 return;
1270 for (i = 0; i < codec->init_pins.used; i++) {
1271 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1272 snd_hda_codec_write(codec, pin->nid, 0,
1273 AC_VERB_SET_PIN_WIDGET_CONTROL,
1274 pin->ctrl);
1275 }
1276 codec->pins_shutup = 0;
1277 }
1278 #endif
1279
1280 static void hda_jackpoll_work(struct work_struct *work)
1281 {
1282 struct hda_codec *codec =
1283 container_of(work, struct hda_codec, jackpoll_work.work);
1284
1285 snd_hda_jack_set_dirty_all(codec);
1286 snd_hda_jack_poll_all(codec);
1287
1288 if (!codec->jackpoll_interval)
1289 return;
1290
1291 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1292 codec->jackpoll_interval);
1293 }
1294
1295 static void init_hda_cache(struct hda_cache_rec *cache,
1296 unsigned int record_size);
1297 static void free_hda_cache(struct hda_cache_rec *cache);
1298
1299 /* release all pincfg lists */
1300 static void free_init_pincfgs(struct hda_codec *codec)
1301 {
1302 snd_array_free(&codec->driver_pins);
1303 #ifdef CONFIG_SND_HDA_RECONFIG
1304 snd_array_free(&codec->user_pins);
1305 #endif
1306 snd_array_free(&codec->init_pins);
1307 }
1308
1309 /*
1310 * audio-converter setup caches
1311 */
1312 struct hda_cvt_setup {
1313 hda_nid_t nid;
1314 u8 stream_tag;
1315 u8 channel_id;
1316 u16 format_id;
1317 unsigned char active; /* cvt is currently used */
1318 unsigned char dirty; /* setups should be cleared */
1319 };
1320
1321 /* get or create a cache entry for the given audio converter NID */
1322 static struct hda_cvt_setup *
1323 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1324 {
1325 struct hda_cvt_setup *p;
1326 int i;
1327
1328 for (i = 0; i < codec->cvt_setups.used; i++) {
1329 p = snd_array_elem(&codec->cvt_setups, i);
1330 if (p->nid == nid)
1331 return p;
1332 }
1333 p = snd_array_new(&codec->cvt_setups);
1334 if (p)
1335 p->nid = nid;
1336 return p;
1337 }
1338
1339 /*
1340 * Dynamic symbol binding for the codec parsers
1341 */
1342
1343 #define load_parser(codec, sym) \
1344 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
1345
1346 static void unload_parser(struct hda_codec *codec)
1347 {
1348 if (codec->parser)
1349 symbol_put_addr(codec->parser);
1350 codec->parser = NULL;
1351 }
1352
1353 /*
1354 * codec destructor
1355 */
1356 static void snd_hda_codec_free(struct hda_codec *codec)
1357 {
1358 if (!codec)
1359 return;
1360 cancel_delayed_work_sync(&codec->jackpoll_work);
1361 snd_hda_jack_tbl_clear(codec);
1362 free_init_pincfgs(codec);
1363 #ifdef CONFIG_PM
1364 cancel_delayed_work(&codec->power_work);
1365 flush_workqueue(codec->bus->workq);
1366 #endif
1367 list_del(&codec->list);
1368 snd_array_free(&codec->mixers);
1369 snd_array_free(&codec->nids);
1370 snd_array_free(&codec->cvt_setups);
1371 snd_array_free(&codec->spdif_out);
1372 remove_conn_list(codec);
1373 codec->bus->caddr_tbl[codec->addr] = NULL;
1374 if (codec->patch_ops.free)
1375 codec->patch_ops.free(codec);
1376 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1377 snd_hda_sysfs_clear(codec);
1378 unload_parser(codec);
1379 module_put(codec->owner);
1380 free_hda_cache(&codec->amp_cache);
1381 free_hda_cache(&codec->cmd_cache);
1382 kfree(codec->vendor_name);
1383 kfree(codec->chip_name);
1384 kfree(codec->modelname);
1385 kfree(codec->wcaps);
1386 codec->bus->num_codecs--;
1387 put_device(&codec->dev);
1388 }
1389
1390 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1391 hda_nid_t fg, unsigned int power_state);
1392
1393 static unsigned int hda_set_power_state(struct hda_codec *codec,
1394 unsigned int power_state);
1395
1396 static int snd_hda_codec_dev_register(struct snd_device *device)
1397 {
1398 struct hda_codec *codec = device->device_data;
1399 int err = device_add(&codec->dev);
1400
1401 if (err < 0)
1402 return err;
1403 snd_hda_register_beep_device(codec);
1404 return 0;
1405 }
1406
1407 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1408 {
1409 struct hda_codec *codec = device->device_data;
1410
1411 snd_hda_detach_beep_device(codec);
1412 device_del(&codec->dev);
1413 return 0;
1414 }
1415
1416 static int snd_hda_codec_dev_free(struct snd_device *device)
1417 {
1418 snd_hda_codec_free(device->device_data);
1419 return 0;
1420 }
1421
1422 /* just free the container */
1423 static void snd_hda_codec_dev_release(struct device *dev)
1424 {
1425 kfree(container_of(dev, struct hda_codec, dev));
1426 }
1427
1428 /**
1429 * snd_hda_codec_new - create a HDA codec
1430 * @bus: the bus to assign
1431 * @codec_addr: the codec address
1432 * @codecp: the pointer to store the generated codec
1433 *
1434 * Returns 0 if successful, or a negative error code.
1435 */
1436 int snd_hda_codec_new(struct hda_bus *bus,
1437 unsigned int codec_addr,
1438 struct hda_codec **codecp)
1439 {
1440 struct hda_codec *codec;
1441 char component[31];
1442 hda_nid_t fg;
1443 int err;
1444 static struct snd_device_ops dev_ops = {
1445 .dev_register = snd_hda_codec_dev_register,
1446 .dev_disconnect = snd_hda_codec_dev_disconnect,
1447 .dev_free = snd_hda_codec_dev_free,
1448 };
1449
1450 if (snd_BUG_ON(!bus))
1451 return -EINVAL;
1452 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1453 return -EINVAL;
1454
1455 if (bus->caddr_tbl[codec_addr]) {
1456 dev_err(bus->card->dev,
1457 "address 0x%x is already occupied\n",
1458 codec_addr);
1459 return -EBUSY;
1460 }
1461
1462 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1463 if (codec == NULL) {
1464 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
1465 return -ENOMEM;
1466 }
1467
1468 device_initialize(&codec->dev);
1469 codec->dev.parent = &bus->card->card_dev;
1470 codec->dev.class = sound_class;
1471 codec->dev.release = snd_hda_codec_dev_release;
1472 codec->dev.groups = snd_hda_dev_attr_groups;
1473 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1474 codec_addr);
1475 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
1476
1477 codec->bus = bus;
1478 codec->addr = codec_addr;
1479 mutex_init(&codec->spdif_mutex);
1480 mutex_init(&codec->control_mutex);
1481 mutex_init(&codec->hash_mutex);
1482 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1483 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1484 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1485 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1486 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1487 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1488 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1489 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1490 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1491 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1492 INIT_LIST_HEAD(&codec->conn_list);
1493
1494 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1495 codec->depop_delay = -1;
1496 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1497
1498 #ifdef CONFIG_PM
1499 spin_lock_init(&codec->power_lock);
1500 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1501 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1502 * the caller has to power down appropriatley after initialization
1503 * phase.
1504 */
1505 hda_keep_power_on(codec);
1506 #endif
1507
1508 snd_hda_sysfs_init(codec);
1509
1510 if (codec->bus->modelname) {
1511 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1512 if (!codec->modelname) {
1513 err = -ENODEV;
1514 goto error;
1515 }
1516 }
1517
1518 list_add_tail(&codec->list, &bus->codec_list);
1519 bus->num_codecs++;
1520
1521 bus->caddr_tbl[codec_addr] = codec;
1522
1523 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1524 AC_PAR_VENDOR_ID);
1525 if (codec->vendor_id == -1)
1526 /* read again, hopefully the access method was corrected
1527 * in the last read...
1528 */
1529 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1530 AC_PAR_VENDOR_ID);
1531 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1532 AC_PAR_SUBSYSTEM_ID);
1533 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1534 AC_PAR_REV_ID);
1535
1536 setup_fg_nodes(codec);
1537 if (!codec->afg && !codec->mfg) {
1538 dev_err(bus->card->dev, "no AFG or MFG node found\n");
1539 err = -ENODEV;
1540 goto error;
1541 }
1542
1543 fg = codec->afg ? codec->afg : codec->mfg;
1544 err = read_widget_caps(codec, fg);
1545 if (err < 0) {
1546 dev_err(bus->card->dev, "cannot malloc\n");
1547 goto error;
1548 }
1549 err = read_pin_defaults(codec);
1550 if (err < 0)
1551 goto error;
1552
1553 if (!codec->subsystem_id) {
1554 codec->subsystem_id =
1555 snd_hda_codec_read(codec, fg, 0,
1556 AC_VERB_GET_SUBSYSTEM_ID, 0);
1557 }
1558
1559 #ifdef CONFIG_PM
1560 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1561 AC_PWRST_CLKSTOP);
1562 #endif
1563 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1564 AC_PWRST_EPSS);
1565 #ifdef CONFIG_PM
1566 if (!codec->d3_stop_clk || !codec->epss)
1567 bus->power_keep_link_on = 1;
1568 #endif
1569
1570
1571 /* power-up all before initialization */
1572 hda_set_power_state(codec, AC_PWRST_D0);
1573
1574 snd_hda_codec_proc_new(codec);
1575
1576 snd_hda_create_hwdep(codec);
1577
1578 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1579 codec->subsystem_id, codec->revision_id);
1580 snd_component_add(codec->bus->card, component);
1581
1582 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1583 if (err < 0)
1584 goto error;
1585
1586 if (codecp)
1587 *codecp = codec;
1588 return 0;
1589
1590 error:
1591 snd_hda_codec_free(codec);
1592 return err;
1593 }
1594 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1595
1596 /**
1597 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1598 * @codec: the HDA codec
1599 *
1600 * Forcibly refresh the all widget caps and the init pin configurations of
1601 * the given codec.
1602 */
1603 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1604 {
1605 hda_nid_t fg;
1606 int err;
1607
1608 /* Assume the function group node does not change,
1609 * only the widget nodes may change.
1610 */
1611 kfree(codec->wcaps);
1612 fg = codec->afg ? codec->afg : codec->mfg;
1613 err = read_widget_caps(codec, fg);
1614 if (err < 0) {
1615 codec_err(codec, "cannot malloc\n");
1616 return err;
1617 }
1618
1619 snd_array_free(&codec->init_pins);
1620 err = read_pin_defaults(codec);
1621
1622 return err;
1623 }
1624 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1625
1626
1627 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1628 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1629 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1630 {
1631 hda_nid_t nid = codec->start_nid;
1632 int i;
1633
1634 for (i = 0; i < codec->num_nodes; i++, nid++) {
1635 unsigned int wcaps = get_wcaps(codec, nid);
1636 switch (get_wcaps_type(wcaps)) {
1637 case AC_WID_AUD_IN:
1638 return false; /* HDMI parser supports only HDMI out */
1639 case AC_WID_AUD_OUT:
1640 if (!(wcaps & AC_WCAP_DIGITAL))
1641 return false;
1642 break;
1643 }
1644 }
1645 return true;
1646 }
1647 #else
1648 /* no HDMI codec parser support */
1649 #define is_likely_hdmi_codec(codec) false
1650 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1651
1652 /**
1653 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1654 * @codec: the HDA codec
1655 *
1656 * Start parsing of the given codec tree and (re-)initialize the whole
1657 * patch instance.
1658 *
1659 * Returns 0 if successful or a negative error code.
1660 */
1661 int snd_hda_codec_configure(struct hda_codec *codec)
1662 {
1663 int (*patch)(struct hda_codec *) = NULL;
1664 int err;
1665
1666 codec->preset = find_codec_preset(codec);
1667 if (!codec->vendor_name || !codec->chip_name) {
1668 err = get_codec_name(codec);
1669 if (err < 0)
1670 return err;
1671 }
1672
1673 if (!is_generic_config(codec) && codec->preset)
1674 patch = codec->preset->patch;
1675 if (!patch) {
1676 unload_parser(codec); /* to be sure */
1677 if (is_likely_hdmi_codec(codec)) {
1678 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1679 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1680 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1681 patch = snd_hda_parse_hdmi_codec;
1682 #endif
1683 }
1684 if (!patch) {
1685 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1686 patch = load_parser(codec, snd_hda_parse_generic_codec);
1687 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1688 patch = snd_hda_parse_generic_codec;
1689 #endif
1690 }
1691 if (!patch) {
1692 codec_err(codec, "No codec parser is available\n");
1693 return -ENODEV;
1694 }
1695 }
1696
1697 err = patch(codec);
1698 if (err < 0) {
1699 unload_parser(codec);
1700 return err;
1701 }
1702
1703 if (codec->patch_ops.unsol_event) {
1704 err = init_unsol_queue(codec->bus);
1705 if (err < 0)
1706 return err;
1707 }
1708
1709 /* audio codec should override the mixer name */
1710 if (codec->afg || !*codec->bus->card->mixername)
1711 snprintf(codec->bus->card->mixername,
1712 sizeof(codec->bus->card->mixername),
1713 "%s %s", codec->vendor_name, codec->chip_name);
1714 return 0;
1715 }
1716 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
1717
1718 /* update the stream-id if changed */
1719 static void update_pcm_stream_id(struct hda_codec *codec,
1720 struct hda_cvt_setup *p, hda_nid_t nid,
1721 u32 stream_tag, int channel_id)
1722 {
1723 unsigned int oldval, newval;
1724
1725 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1726 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1727 newval = (stream_tag << 4) | channel_id;
1728 if (oldval != newval)
1729 snd_hda_codec_write(codec, nid, 0,
1730 AC_VERB_SET_CHANNEL_STREAMID,
1731 newval);
1732 p->stream_tag = stream_tag;
1733 p->channel_id = channel_id;
1734 }
1735 }
1736
1737 /* update the format-id if changed */
1738 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1739 hda_nid_t nid, int format)
1740 {
1741 unsigned int oldval;
1742
1743 if (p->format_id != format) {
1744 oldval = snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_STREAM_FORMAT, 0);
1746 if (oldval != format) {
1747 msleep(1);
1748 snd_hda_codec_write(codec, nid, 0,
1749 AC_VERB_SET_STREAM_FORMAT,
1750 format);
1751 }
1752 p->format_id = format;
1753 }
1754 }
1755
1756 /**
1757 * snd_hda_codec_setup_stream - set up the codec for streaming
1758 * @codec: the CODEC to set up
1759 * @nid: the NID to set up
1760 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1761 * @channel_id: channel id to pass, zero based.
1762 * @format: stream format.
1763 */
1764 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1765 u32 stream_tag,
1766 int channel_id, int format)
1767 {
1768 struct hda_codec *c;
1769 struct hda_cvt_setup *p;
1770 int type;
1771 int i;
1772
1773 if (!nid)
1774 return;
1775
1776 codec_dbg(codec,
1777 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1778 nid, stream_tag, channel_id, format);
1779 p = get_hda_cvt_setup(codec, nid);
1780 if (!p)
1781 return;
1782
1783 if (codec->pcm_format_first)
1784 update_pcm_format(codec, p, nid, format);
1785 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1786 if (!codec->pcm_format_first)
1787 update_pcm_format(codec, p, nid, format);
1788
1789 p->active = 1;
1790 p->dirty = 0;
1791
1792 /* make other inactive cvts with the same stream-tag dirty */
1793 type = get_wcaps_type(get_wcaps(codec, nid));
1794 list_for_each_entry(c, &codec->bus->codec_list, list) {
1795 for (i = 0; i < c->cvt_setups.used; i++) {
1796 p = snd_array_elem(&c->cvt_setups, i);
1797 if (!p->active && p->stream_tag == stream_tag &&
1798 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1799 p->dirty = 1;
1800 }
1801 }
1802 }
1803 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1804
1805 static void really_cleanup_stream(struct hda_codec *codec,
1806 struct hda_cvt_setup *q);
1807
1808 /**
1809 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1810 * @codec: the CODEC to clean up
1811 * @nid: the NID to clean up
1812 * @do_now: really clean up the stream instead of clearing the active flag
1813 */
1814 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1815 int do_now)
1816 {
1817 struct hda_cvt_setup *p;
1818
1819 if (!nid)
1820 return;
1821
1822 if (codec->no_sticky_stream)
1823 do_now = 1;
1824
1825 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1826 p = get_hda_cvt_setup(codec, nid);
1827 if (p) {
1828 /* here we just clear the active flag when do_now isn't set;
1829 * actual clean-ups will be done later in
1830 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1831 */
1832 if (do_now)
1833 really_cleanup_stream(codec, p);
1834 else
1835 p->active = 0;
1836 }
1837 }
1838 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1839
1840 static void really_cleanup_stream(struct hda_codec *codec,
1841 struct hda_cvt_setup *q)
1842 {
1843 hda_nid_t nid = q->nid;
1844 if (q->stream_tag || q->channel_id)
1845 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1846 if (q->format_id)
1847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1848 );
1849 memset(q, 0, sizeof(*q));
1850 q->nid = nid;
1851 }
1852
1853 /* clean up the all conflicting obsolete streams */
1854 static void purify_inactive_streams(struct hda_codec *codec)
1855 {
1856 struct hda_codec *c;
1857 int i;
1858
1859 list_for_each_entry(c, &codec->bus->codec_list, list) {
1860 for (i = 0; i < c->cvt_setups.used; i++) {
1861 struct hda_cvt_setup *p;
1862 p = snd_array_elem(&c->cvt_setups, i);
1863 if (p->dirty)
1864 really_cleanup_stream(c, p);
1865 }
1866 }
1867 }
1868
1869 #ifdef CONFIG_PM
1870 /* clean up all streams; called from suspend */
1871 static void hda_cleanup_all_streams(struct hda_codec *codec)
1872 {
1873 int i;
1874
1875 for (i = 0; i < codec->cvt_setups.used; i++) {
1876 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1877 if (p->stream_tag)
1878 really_cleanup_stream(codec, p);
1879 }
1880 }
1881 #endif
1882
1883 /*
1884 * amp access functions
1885 */
1886
1887 /* FIXME: more better hash key? */
1888 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1889 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1890 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1891 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1892 #define INFO_AMP_CAPS (1<<0)
1893 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1894
1895 /* initialize the hash table */
1896 static void init_hda_cache(struct hda_cache_rec *cache,
1897 unsigned int record_size)
1898 {
1899 memset(cache, 0, sizeof(*cache));
1900 memset(cache->hash, 0xff, sizeof(cache->hash));
1901 snd_array_init(&cache->buf, record_size, 64);
1902 }
1903
1904 static void free_hda_cache(struct hda_cache_rec *cache)
1905 {
1906 snd_array_free(&cache->buf);
1907 }
1908
1909 /* query the hash. allocate an entry if not found. */
1910 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1911 {
1912 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1913 u16 cur = cache->hash[idx];
1914 struct hda_cache_head *info;
1915
1916 while (cur != 0xffff) {
1917 info = snd_array_elem(&cache->buf, cur);
1918 if (info->key == key)
1919 return info;
1920 cur = info->next;
1921 }
1922 return NULL;
1923 }
1924
1925 /* query the hash. allocate an entry if not found. */
1926 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1927 u32 key)
1928 {
1929 struct hda_cache_head *info = get_hash(cache, key);
1930 if (!info) {
1931 u16 idx, cur;
1932 /* add a new hash entry */
1933 info = snd_array_new(&cache->buf);
1934 if (!info)
1935 return NULL;
1936 cur = snd_array_index(&cache->buf, info);
1937 info->key = key;
1938 info->val = 0;
1939 info->dirty = 0;
1940 idx = key % (u16)ARRAY_SIZE(cache->hash);
1941 info->next = cache->hash[idx];
1942 cache->hash[idx] = cur;
1943 }
1944 return info;
1945 }
1946
1947 /* query and allocate an amp hash entry */
1948 static inline struct hda_amp_info *
1949 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1950 {
1951 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1952 }
1953
1954 /* overwrite the value with the key in the caps hash */
1955 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1956 {
1957 struct hda_amp_info *info;
1958
1959 mutex_lock(&codec->hash_mutex);
1960 info = get_alloc_amp_hash(codec, key);
1961 if (!info) {
1962 mutex_unlock(&codec->hash_mutex);
1963 return -EINVAL;
1964 }
1965 info->amp_caps = val;
1966 info->head.val |= INFO_AMP_CAPS;
1967 mutex_unlock(&codec->hash_mutex);
1968 return 0;
1969 }
1970
1971 /* query the value from the caps hash; if not found, fetch the current
1972 * value from the given function and store in the hash
1973 */
1974 static unsigned int
1975 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1976 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1977 {
1978 struct hda_amp_info *info;
1979 unsigned int val;
1980
1981 mutex_lock(&codec->hash_mutex);
1982 info = get_alloc_amp_hash(codec, key);
1983 if (!info) {
1984 mutex_unlock(&codec->hash_mutex);
1985 return 0;
1986 }
1987 if (!(info->head.val & INFO_AMP_CAPS)) {
1988 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1989 val = func(codec, nid, dir);
1990 write_caps_hash(codec, key, val);
1991 } else {
1992 val = info->amp_caps;
1993 mutex_unlock(&codec->hash_mutex);
1994 }
1995 return val;
1996 }
1997
1998 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1999 int direction)
2000 {
2001 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
2002 nid = codec->afg;
2003 return snd_hda_param_read(codec, nid,
2004 direction == HDA_OUTPUT ?
2005 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
2006 }
2007
2008 /**
2009 * query_amp_caps - query AMP capabilities
2010 * @codec: the HD-auio codec
2011 * @nid: the NID to query
2012 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2013 *
2014 * Query AMP capabilities for the given widget and direction.
2015 * Returns the obtained capability bits.
2016 *
2017 * When cap bits have been already read, this doesn't read again but
2018 * returns the cached value.
2019 */
2020 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
2021 {
2022 return query_caps_hash(codec, nid, direction,
2023 HDA_HASH_KEY(nid, direction, 0),
2024 read_amp_cap);
2025 }
2026 EXPORT_SYMBOL_GPL(query_amp_caps);
2027
2028 /**
2029 * snd_hda_check_amp_caps - query AMP capabilities
2030 * @codec: the HD-audio codec
2031 * @nid: the NID to query
2032 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2033 * @bits: bit mask to check the result
2034 *
2035 * Check whether the widget has the given amp capability for the direction.
2036 */
2037 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2038 int dir, unsigned int bits)
2039 {
2040 if (!nid)
2041 return false;
2042 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2043 if (query_amp_caps(codec, nid, dir) & bits)
2044 return true;
2045 return false;
2046 }
2047 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2048
2049 /**
2050 * snd_hda_override_amp_caps - Override the AMP capabilities
2051 * @codec: the CODEC to clean up
2052 * @nid: the NID to clean up
2053 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2054 * @caps: the capability bits to set
2055 *
2056 * Override the cached AMP caps bits value by the given one.
2057 * This function is useful if the driver needs to adjust the AMP ranges,
2058 * e.g. limit to 0dB, etc.
2059 *
2060 * Returns zero if successful or a negative error code.
2061 */
2062 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2063 unsigned int caps)
2064 {
2065 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2066 }
2067 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
2068
2069 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2070 int dir)
2071 {
2072 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2073 }
2074
2075 /**
2076 * snd_hda_query_pin_caps - Query PIN capabilities
2077 * @codec: the HD-auio codec
2078 * @nid: the NID to query
2079 *
2080 * Query PIN capabilities for the given widget.
2081 * Returns the obtained capability bits.
2082 *
2083 * When cap bits have been already read, this doesn't read again but
2084 * returns the cached value.
2085 */
2086 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2087 {
2088 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2089 read_pin_cap);
2090 }
2091 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
2092
2093 /**
2094 * snd_hda_override_pin_caps - Override the pin capabilities
2095 * @codec: the CODEC
2096 * @nid: the NID to override
2097 * @caps: the capability bits to set
2098 *
2099 * Override the cached PIN capabilitiy bits value by the given one.
2100 *
2101 * Returns zero if successful or a negative error code.
2102 */
2103 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2104 unsigned int caps)
2105 {
2106 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2107 }
2108 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
2109
2110 /* read or sync the hash value with the current value;
2111 * call within hash_mutex
2112 */
2113 static struct hda_amp_info *
2114 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2115 int direction, int index, bool init_only)
2116 {
2117 struct hda_amp_info *info;
2118 unsigned int parm, val = 0;
2119 bool val_read = false;
2120
2121 retry:
2122 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2123 if (!info)
2124 return NULL;
2125 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2126 if (!val_read) {
2127 mutex_unlock(&codec->hash_mutex);
2128 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2129 parm |= direction == HDA_OUTPUT ?
2130 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2131 parm |= index;
2132 val = snd_hda_codec_read(codec, nid, 0,
2133 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2134 val &= 0xff;
2135 val_read = true;
2136 mutex_lock(&codec->hash_mutex);
2137 goto retry;
2138 }
2139 info->vol[ch] = val;
2140 info->head.val |= INFO_AMP_VOL(ch);
2141 } else if (init_only)
2142 return NULL;
2143 return info;
2144 }
2145
2146 /*
2147 * write the current volume in info to the h/w
2148 */
2149 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2150 hda_nid_t nid, int ch, int direction, int index,
2151 int val)
2152 {
2153 u32 parm;
2154
2155 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2156 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2157 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2158 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2159 (amp_caps & AC_AMPCAP_MIN_MUTE))
2160 ; /* set the zero value as a fake mute */
2161 else
2162 parm |= val;
2163 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2164 }
2165
2166 /**
2167 * snd_hda_codec_amp_read - Read AMP value
2168 * @codec: HD-audio codec
2169 * @nid: NID to read the AMP value
2170 * @ch: channel (left=0 or right=1)
2171 * @direction: #HDA_INPUT or #HDA_OUTPUT
2172 * @index: the index value (only for input direction)
2173 *
2174 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2175 */
2176 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2177 int direction, int index)
2178 {
2179 struct hda_amp_info *info;
2180 unsigned int val = 0;
2181
2182 mutex_lock(&codec->hash_mutex);
2183 info = update_amp_hash(codec, nid, ch, direction, index, false);
2184 if (info)
2185 val = info->vol[ch];
2186 mutex_unlock(&codec->hash_mutex);
2187 return val;
2188 }
2189 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
2190
2191 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2192 int direction, int idx, int mask, int val,
2193 bool init_only)
2194 {
2195 struct hda_amp_info *info;
2196 unsigned int caps;
2197 unsigned int cache_only;
2198
2199 if (snd_BUG_ON(mask & ~0xff))
2200 mask &= 0xff;
2201 val &= mask;
2202
2203 mutex_lock(&codec->hash_mutex);
2204 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2205 if (!info) {
2206 mutex_unlock(&codec->hash_mutex);
2207 return 0;
2208 }
2209 val |= info->vol[ch] & ~mask;
2210 if (info->vol[ch] == val) {
2211 mutex_unlock(&codec->hash_mutex);
2212 return 0;
2213 }
2214 info->vol[ch] = val;
2215 cache_only = info->head.dirty = codec->cached_write;
2216 caps = info->amp_caps;
2217 mutex_unlock(&codec->hash_mutex);
2218 if (!cache_only)
2219 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2220 return 1;
2221 }
2222
2223 /**
2224 * snd_hda_codec_amp_update - update the AMP value
2225 * @codec: HD-audio codec
2226 * @nid: NID to read the AMP value
2227 * @ch: channel (left=0 or right=1)
2228 * @direction: #HDA_INPUT or #HDA_OUTPUT
2229 * @idx: the index value (only for input direction)
2230 * @mask: bit mask to set
2231 * @val: the bits value to set
2232 *
2233 * Update the AMP value with a bit mask.
2234 * Returns 0 if the value is unchanged, 1 if changed.
2235 */
2236 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2237 int direction, int idx, int mask, int val)
2238 {
2239 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2240 }
2241 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
2242
2243 /**
2244 * snd_hda_codec_amp_stereo - update the AMP stereo values
2245 * @codec: HD-audio codec
2246 * @nid: NID to read the AMP value
2247 * @direction: #HDA_INPUT or #HDA_OUTPUT
2248 * @idx: the index value (only for input direction)
2249 * @mask: bit mask to set
2250 * @val: the bits value to set
2251 *
2252 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2253 * stereo widget with the same mask and value.
2254 */
2255 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2256 int direction, int idx, int mask, int val)
2257 {
2258 int ch, ret = 0;
2259
2260 if (snd_BUG_ON(mask & ~0xff))
2261 mask &= 0xff;
2262 for (ch = 0; ch < 2; ch++)
2263 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2264 idx, mask, val);
2265 return ret;
2266 }
2267 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2268
2269 /**
2270 * snd_hda_codec_amp_init - initialize the AMP value
2271 * @codec: the HDA codec
2272 * @nid: NID to read the AMP value
2273 * @ch: channel (left=0 or right=1)
2274 * @dir: #HDA_INPUT or #HDA_OUTPUT
2275 * @idx: the index value (only for input direction)
2276 * @mask: bit mask to set
2277 * @val: the bits value to set
2278 *
2279 * Works like snd_hda_codec_amp_update() but it writes the value only at
2280 * the first access. If the amp was already initialized / updated beforehand,
2281 * this does nothing.
2282 */
2283 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2284 int dir, int idx, int mask, int val)
2285 {
2286 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2287 }
2288 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2289
2290 /**
2291 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2292 * @codec: the HDA codec
2293 * @nid: NID to read the AMP value
2294 * @dir: #HDA_INPUT or #HDA_OUTPUT
2295 * @idx: the index value (only for input direction)
2296 * @mask: bit mask to set
2297 * @val: the bits value to set
2298 *
2299 * Call snd_hda_codec_amp_init() for both stereo channels.
2300 */
2301 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2302 int dir, int idx, int mask, int val)
2303 {
2304 int ch, ret = 0;
2305
2306 if (snd_BUG_ON(mask & ~0xff))
2307 mask &= 0xff;
2308 for (ch = 0; ch < 2; ch++)
2309 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2310 idx, mask, val);
2311 return ret;
2312 }
2313 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2314
2315 /**
2316 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2317 * @codec: HD-audio codec
2318 *
2319 * Resume the all amp commands from the cache.
2320 */
2321 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2322 {
2323 int i;
2324
2325 mutex_lock(&codec->hash_mutex);
2326 codec->cached_write = 0;
2327 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2328 struct hda_amp_info *buffer;
2329 u32 key;
2330 hda_nid_t nid;
2331 unsigned int idx, dir, ch;
2332 struct hda_amp_info info;
2333
2334 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2335 if (!buffer->head.dirty)
2336 continue;
2337 buffer->head.dirty = 0;
2338 info = *buffer;
2339 key = info.head.key;
2340 if (!key)
2341 continue;
2342 nid = key & 0xff;
2343 idx = (key >> 16) & 0xff;
2344 dir = (key >> 24) & 0xff;
2345 for (ch = 0; ch < 2; ch++) {
2346 if (!(info.head.val & INFO_AMP_VOL(ch)))
2347 continue;
2348 mutex_unlock(&codec->hash_mutex);
2349 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2350 info.vol[ch]);
2351 mutex_lock(&codec->hash_mutex);
2352 }
2353 }
2354 mutex_unlock(&codec->hash_mutex);
2355 }
2356 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2357
2358 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2359 unsigned int ofs)
2360 {
2361 u32 caps = query_amp_caps(codec, nid, dir);
2362 /* get num steps */
2363 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2364 if (ofs < caps)
2365 caps -= ofs;
2366 return caps;
2367 }
2368
2369 /**
2370 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2371 * @kcontrol: referred ctl element
2372 * @uinfo: pointer to get/store the data
2373 *
2374 * The control element is supposed to have the private_value field
2375 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2376 */
2377 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_info *uinfo)
2379 {
2380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2381 u16 nid = get_amp_nid(kcontrol);
2382 u8 chs = get_amp_channels(kcontrol);
2383 int dir = get_amp_direction(kcontrol);
2384 unsigned int ofs = get_amp_offset(kcontrol);
2385
2386 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2387 uinfo->count = chs == 3 ? 2 : 1;
2388 uinfo->value.integer.min = 0;
2389 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2390 if (!uinfo->value.integer.max) {
2391 codec_warn(codec,
2392 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2393 nid, kcontrol->id.name);
2394 return -EINVAL;
2395 }
2396 return 0;
2397 }
2398 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2399
2400
2401 static inline unsigned int
2402 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2403 int ch, int dir, int idx, unsigned int ofs)
2404 {
2405 unsigned int val;
2406 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2407 val &= HDA_AMP_VOLMASK;
2408 if (val >= ofs)
2409 val -= ofs;
2410 else
2411 val = 0;
2412 return val;
2413 }
2414
2415 static inline int
2416 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2417 int ch, int dir, int idx, unsigned int ofs,
2418 unsigned int val)
2419 {
2420 unsigned int maxval;
2421
2422 if (val > 0)
2423 val += ofs;
2424 /* ofs = 0: raw max value */
2425 maxval = get_amp_max_value(codec, nid, dir, 0);
2426 if (val > maxval)
2427 val = maxval;
2428 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2429 HDA_AMP_VOLMASK, val);
2430 }
2431
2432 /**
2433 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2434 * @kcontrol: ctl element
2435 * @ucontrol: pointer to get/store the data
2436 *
2437 * The control element is supposed to have the private_value field
2438 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2439 */
2440 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2442 {
2443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2444 hda_nid_t nid = get_amp_nid(kcontrol);
2445 int chs = get_amp_channels(kcontrol);
2446 int dir = get_amp_direction(kcontrol);
2447 int idx = get_amp_index(kcontrol);
2448 unsigned int ofs = get_amp_offset(kcontrol);
2449 long *valp = ucontrol->value.integer.value;
2450
2451 if (chs & 1)
2452 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2453 if (chs & 2)
2454 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2455 return 0;
2456 }
2457 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2458
2459 /**
2460 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2461 * @kcontrol: ctl element
2462 * @ucontrol: pointer to get/store the data
2463 *
2464 * The control element is supposed to have the private_value field
2465 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2466 */
2467 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2469 {
2470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2471 hda_nid_t nid = get_amp_nid(kcontrol);
2472 int chs = get_amp_channels(kcontrol);
2473 int dir = get_amp_direction(kcontrol);
2474 int idx = get_amp_index(kcontrol);
2475 unsigned int ofs = get_amp_offset(kcontrol);
2476 long *valp = ucontrol->value.integer.value;
2477 int change = 0;
2478
2479 snd_hda_power_up(codec);
2480 if (chs & 1) {
2481 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2482 valp++;
2483 }
2484 if (chs & 2)
2485 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2486 snd_hda_power_down(codec);
2487 return change;
2488 }
2489 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2490
2491 /**
2492 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2493 * @kcontrol: ctl element
2494 * @op_flag: operation flag
2495 * @size: byte size of input TLV
2496 * @_tlv: TLV data
2497 *
2498 * The control element is supposed to have the private_value field
2499 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2500 */
2501 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2502 unsigned int size, unsigned int __user *_tlv)
2503 {
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 hda_nid_t nid = get_amp_nid(kcontrol);
2506 int dir = get_amp_direction(kcontrol);
2507 unsigned int ofs = get_amp_offset(kcontrol);
2508 bool min_mute = get_amp_min_mute(kcontrol);
2509 u32 caps, val1, val2;
2510
2511 if (size < 4 * sizeof(unsigned int))
2512 return -ENOMEM;
2513 caps = query_amp_caps(codec, nid, dir);
2514 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2515 val2 = (val2 + 1) * 25;
2516 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2517 val1 += ofs;
2518 val1 = ((int)val1) * ((int)val2);
2519 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2520 val2 |= TLV_DB_SCALE_MUTE;
2521 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2522 return -EFAULT;
2523 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2524 return -EFAULT;
2525 if (put_user(val1, _tlv + 2))
2526 return -EFAULT;
2527 if (put_user(val2, _tlv + 3))
2528 return -EFAULT;
2529 return 0;
2530 }
2531 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2532
2533 /**
2534 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2535 * @codec: HD-audio codec
2536 * @nid: NID of a reference widget
2537 * @dir: #HDA_INPUT or #HDA_OUTPUT
2538 * @tlv: TLV data to be stored, at least 4 elements
2539 *
2540 * Set (static) TLV data for a virtual master volume using the AMP caps
2541 * obtained from the reference NID.
2542 * The volume range is recalculated as if the max volume is 0dB.
2543 */
2544 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2545 unsigned int *tlv)
2546 {
2547 u32 caps;
2548 int nums, step;
2549
2550 caps = query_amp_caps(codec, nid, dir);
2551 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2552 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2553 step = (step + 1) * 25;
2554 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2555 tlv[1] = 2 * sizeof(unsigned int);
2556 tlv[2] = -nums * step;
2557 tlv[3] = step;
2558 }
2559 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2560
2561 /* find a mixer control element with the given name */
2562 static struct snd_kcontrol *
2563 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2564 {
2565 struct snd_ctl_elem_id id;
2566 memset(&id, 0, sizeof(id));
2567 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2568 id.device = dev;
2569 id.index = idx;
2570 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2571 return NULL;
2572 strcpy(id.name, name);
2573 return snd_ctl_find_id(codec->bus->card, &id);
2574 }
2575
2576 /**
2577 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2578 * @codec: HD-audio codec
2579 * @name: ctl id name string
2580 *
2581 * Get the control element with the given id string and IFACE_MIXER.
2582 */
2583 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2584 const char *name)
2585 {
2586 return find_mixer_ctl(codec, name, 0, 0);
2587 }
2588 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2589
2590 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2591 int start_idx)
2592 {
2593 int i, idx;
2594 /* 16 ctlrs should be large enough */
2595 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2596 if (!find_mixer_ctl(codec, name, 0, idx))
2597 return idx;
2598 }
2599 return -EBUSY;
2600 }
2601
2602 /**
2603 * snd_hda_ctl_add - Add a control element and assign to the codec
2604 * @codec: HD-audio codec
2605 * @nid: corresponding NID (optional)
2606 * @kctl: the control element to assign
2607 *
2608 * Add the given control element to an array inside the codec instance.
2609 * All control elements belonging to a codec are supposed to be added
2610 * by this function so that a proper clean-up works at the free or
2611 * reconfiguration time.
2612 *
2613 * If non-zero @nid is passed, the NID is assigned to the control element.
2614 * The assignment is shown in the codec proc file.
2615 *
2616 * snd_hda_ctl_add() checks the control subdev id field whether
2617 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2618 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2619 * specifies if kctl->private_value is a HDA amplifier value.
2620 */
2621 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2622 struct snd_kcontrol *kctl)
2623 {
2624 int err;
2625 unsigned short flags = 0;
2626 struct hda_nid_item *item;
2627
2628 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2629 flags |= HDA_NID_ITEM_AMP;
2630 if (nid == 0)
2631 nid = get_amp_nid_(kctl->private_value);
2632 }
2633 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2634 nid = kctl->id.subdevice & 0xffff;
2635 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2636 kctl->id.subdevice = 0;
2637 err = snd_ctl_add(codec->bus->card, kctl);
2638 if (err < 0)
2639 return err;
2640 item = snd_array_new(&codec->mixers);
2641 if (!item)
2642 return -ENOMEM;
2643 item->kctl = kctl;
2644 item->nid = nid;
2645 item->flags = flags;
2646 return 0;
2647 }
2648 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2649
2650 /**
2651 * snd_hda_add_nid - Assign a NID to a control element
2652 * @codec: HD-audio codec
2653 * @nid: corresponding NID (optional)
2654 * @kctl: the control element to assign
2655 * @index: index to kctl
2656 *
2657 * Add the given control element to an array inside the codec instance.
2658 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2659 * NID:KCTL mapping - for example "Capture Source" selector.
2660 */
2661 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2662 unsigned int index, hda_nid_t nid)
2663 {
2664 struct hda_nid_item *item;
2665
2666 if (nid > 0) {
2667 item = snd_array_new(&codec->nids);
2668 if (!item)
2669 return -ENOMEM;
2670 item->kctl = kctl;
2671 item->index = index;
2672 item->nid = nid;
2673 return 0;
2674 }
2675 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2676 kctl->id.name, kctl->id.index, index);
2677 return -EINVAL;
2678 }
2679 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2680
2681 /**
2682 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2683 * @codec: HD-audio codec
2684 */
2685 void snd_hda_ctls_clear(struct hda_codec *codec)
2686 {
2687 int i;
2688 struct hda_nid_item *items = codec->mixers.list;
2689 for (i = 0; i < codec->mixers.used; i++)
2690 snd_ctl_remove(codec->bus->card, items[i].kctl);
2691 snd_array_free(&codec->mixers);
2692 snd_array_free(&codec->nids);
2693 }
2694
2695 /**
2696 * snd_hda_lock_devices - pseudo device locking
2697 * @bus: the BUS
2698 *
2699 * toggle card->shutdown to allow/disallow the device access (as a hack)
2700 */
2701 int snd_hda_lock_devices(struct hda_bus *bus)
2702 {
2703 struct snd_card *card = bus->card;
2704 struct hda_codec *codec;
2705
2706 spin_lock(&card->files_lock);
2707 if (card->shutdown)
2708 goto err_unlock;
2709 card->shutdown = 1;
2710 if (!list_empty(&card->ctl_files))
2711 goto err_clear;
2712
2713 list_for_each_entry(codec, &bus->codec_list, list) {
2714 int pcm;
2715 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2716 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2717 if (!cpcm->pcm)
2718 continue;
2719 if (cpcm->pcm->streams[0].substream_opened ||
2720 cpcm->pcm->streams[1].substream_opened)
2721 goto err_clear;
2722 }
2723 }
2724 spin_unlock(&card->files_lock);
2725 return 0;
2726
2727 err_clear:
2728 card->shutdown = 0;
2729 err_unlock:
2730 spin_unlock(&card->files_lock);
2731 return -EINVAL;
2732 }
2733 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2734
2735 /**
2736 * snd_hda_unlock_devices - pseudo device unlocking
2737 * @bus: the BUS
2738 */
2739 void snd_hda_unlock_devices(struct hda_bus *bus)
2740 {
2741 struct snd_card *card = bus->card;
2742
2743 card = bus->card;
2744 spin_lock(&card->files_lock);
2745 card->shutdown = 0;
2746 spin_unlock(&card->files_lock);
2747 }
2748 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2749
2750 /**
2751 * snd_hda_codec_reset - Clear all objects assigned to the codec
2752 * @codec: HD-audio codec
2753 *
2754 * This frees the all PCM and control elements assigned to the codec, and
2755 * clears the caches and restores the pin default configurations.
2756 *
2757 * When a device is being used, it returns -EBSY. If successfully freed,
2758 * returns zero.
2759 */
2760 int snd_hda_codec_reset(struct hda_codec *codec)
2761 {
2762 struct hda_bus *bus = codec->bus;
2763 struct snd_card *card = bus->card;
2764 int i;
2765
2766 if (snd_hda_lock_devices(bus) < 0)
2767 return -EBUSY;
2768
2769 /* OK, let it free */
2770 cancel_delayed_work_sync(&codec->jackpoll_work);
2771 #ifdef CONFIG_PM
2772 cancel_delayed_work_sync(&codec->power_work);
2773 flush_workqueue(bus->workq);
2774 #endif
2775 snd_hda_ctls_clear(codec);
2776 /* release PCMs */
2777 for (i = 0; i < codec->num_pcms; i++) {
2778 if (codec->pcm_info[i].pcm) {
2779 snd_device_free(card, codec->pcm_info[i].pcm);
2780 clear_bit(codec->pcm_info[i].device,
2781 bus->pcm_dev_bits);
2782 }
2783 }
2784 snd_hda_detach_beep_device(codec);
2785 if (codec->patch_ops.free)
2786 codec->patch_ops.free(codec);
2787 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2788 snd_hda_jack_tbl_clear(codec);
2789 codec->proc_widget_hook = NULL;
2790 codec->spec = NULL;
2791 free_hda_cache(&codec->amp_cache);
2792 free_hda_cache(&codec->cmd_cache);
2793 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2794 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2795 /* free only driver_pins so that init_pins + user_pins are restored */
2796 snd_array_free(&codec->driver_pins);
2797 snd_array_free(&codec->cvt_setups);
2798 snd_array_free(&codec->spdif_out);
2799 snd_array_free(&codec->verbs);
2800 codec->num_pcms = 0;
2801 codec->pcm_info = NULL;
2802 codec->preset = NULL;
2803 codec->slave_dig_outs = NULL;
2804 codec->spdif_status_reset = 0;
2805 unload_parser(codec);
2806 module_put(codec->owner);
2807 codec->owner = NULL;
2808
2809 /* allow device access again */
2810 snd_hda_unlock_devices(bus);
2811 return 0;
2812 }
2813
2814 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2815
2816 /* apply the function to all matching slave ctls in the mixer list */
2817 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2818 const char *suffix, map_slave_func_t func, void *data)
2819 {
2820 struct hda_nid_item *items;
2821 const char * const *s;
2822 int i, err;
2823
2824 items = codec->mixers.list;
2825 for (i = 0; i < codec->mixers.used; i++) {
2826 struct snd_kcontrol *sctl = items[i].kctl;
2827 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2828 continue;
2829 for (s = slaves; *s; s++) {
2830 char tmpname[sizeof(sctl->id.name)];
2831 const char *name = *s;
2832 if (suffix) {
2833 snprintf(tmpname, sizeof(tmpname), "%s %s",
2834 name, suffix);
2835 name = tmpname;
2836 }
2837 if (!strcmp(sctl->id.name, name)) {
2838 err = func(codec, data, sctl);
2839 if (err)
2840 return err;
2841 break;
2842 }
2843 }
2844 }
2845 return 0;
2846 }
2847
2848 static int check_slave_present(struct hda_codec *codec,
2849 void *data, struct snd_kcontrol *sctl)
2850 {
2851 return 1;
2852 }
2853
2854 /* guess the value corresponding to 0dB */
2855 static int get_kctl_0dB_offset(struct hda_codec *codec,
2856 struct snd_kcontrol *kctl, int *step_to_check)
2857 {
2858 int _tlv[4];
2859 const int *tlv = NULL;
2860 int val = -1;
2861
2862 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2863 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2864 mm_segment_t fs = get_fs();
2865 set_fs(get_ds());
2866 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2867 tlv = _tlv;
2868 set_fs(fs);
2869 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2870 tlv = kctl->tlv.p;
2871 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2872 int step = tlv[3];
2873 step &= ~TLV_DB_SCALE_MUTE;
2874 if (!step)
2875 return -1;
2876 if (*step_to_check && *step_to_check != step) {
2877 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2878 - *step_to_check, step);
2879 return -1;
2880 }
2881 *step_to_check = step;
2882 val = -tlv[2] / step;
2883 }
2884 return val;
2885 }
2886
2887 /* call kctl->put with the given value(s) */
2888 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2889 {
2890 struct snd_ctl_elem_value *ucontrol;
2891 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2892 if (!ucontrol)
2893 return -ENOMEM;
2894 ucontrol->value.integer.value[0] = val;
2895 ucontrol->value.integer.value[1] = val;
2896 kctl->put(kctl, ucontrol);
2897 kfree(ucontrol);
2898 return 0;
2899 }
2900
2901 /* initialize the slave volume with 0dB */
2902 static int init_slave_0dB(struct hda_codec *codec,
2903 void *data, struct snd_kcontrol *slave)
2904 {
2905 int offset = get_kctl_0dB_offset(codec, slave, data);
2906 if (offset > 0)
2907 put_kctl_with_value(slave, offset);
2908 return 0;
2909 }
2910
2911 /* unmute the slave */
2912 static int init_slave_unmute(struct hda_codec *codec,
2913 void *data, struct snd_kcontrol *slave)
2914 {
2915 return put_kctl_with_value(slave, 1);
2916 }
2917
2918 static int add_slave(struct hda_codec *codec,
2919 void *data, struct snd_kcontrol *slave)
2920 {
2921 return snd_ctl_add_slave(data, slave);
2922 }
2923
2924 /**
2925 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2926 * @codec: HD-audio codec
2927 * @name: vmaster control name
2928 * @tlv: TLV data (optional)
2929 * @slaves: slave control names (optional)
2930 * @suffix: suffix string to each slave name (optional)
2931 * @init_slave_vol: initialize slaves to unmute/0dB
2932 * @ctl_ret: store the vmaster kcontrol in return
2933 *
2934 * Create a virtual master control with the given name. The TLV data
2935 * must be either NULL or a valid data.
2936 *
2937 * @slaves is a NULL-terminated array of strings, each of which is a
2938 * slave control name. All controls with these names are assigned to
2939 * the new virtual master control.
2940 *
2941 * This function returns zero if successful or a negative error code.
2942 */
2943 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2944 unsigned int *tlv, const char * const *slaves,
2945 const char *suffix, bool init_slave_vol,
2946 struct snd_kcontrol **ctl_ret)
2947 {
2948 struct snd_kcontrol *kctl;
2949 int err;
2950
2951 if (ctl_ret)
2952 *ctl_ret = NULL;
2953
2954 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2955 if (err != 1) {
2956 codec_dbg(codec, "No slave found for %s\n", name);
2957 return 0;
2958 }
2959 kctl = snd_ctl_make_virtual_master(name, tlv);
2960 if (!kctl)
2961 return -ENOMEM;
2962 err = snd_hda_ctl_add(codec, 0, kctl);
2963 if (err < 0)
2964 return err;
2965
2966 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2967 if (err < 0)
2968 return err;
2969
2970 /* init with master mute & zero volume */
2971 put_kctl_with_value(kctl, 0);
2972 if (init_slave_vol) {
2973 int step = 0;
2974 map_slaves(codec, slaves, suffix,
2975 tlv ? init_slave_0dB : init_slave_unmute, &step);
2976 }
2977
2978 if (ctl_ret)
2979 *ctl_ret = kctl;
2980 return 0;
2981 }
2982 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2983
2984 /*
2985 * mute-LED control using vmaster
2986 */
2987 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_info *uinfo)
2989 {
2990 static const char * const texts[] = {
2991 "On", "Off", "Follow Master"
2992 };
2993
2994 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2995 }
2996
2997 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
2999 {
3000 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3001 ucontrol->value.enumerated.item[0] = hook->mute_mode;
3002 return 0;
3003 }
3004
3005 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
3007 {
3008 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3009 unsigned int old_mode = hook->mute_mode;
3010
3011 hook->mute_mode = ucontrol->value.enumerated.item[0];
3012 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
3013 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3014 if (old_mode == hook->mute_mode)
3015 return 0;
3016 snd_hda_sync_vmaster_hook(hook);
3017 return 1;
3018 }
3019
3020 static struct snd_kcontrol_new vmaster_mute_mode = {
3021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3022 .name = "Mute-LED Mode",
3023 .info = vmaster_mute_mode_info,
3024 .get = vmaster_mute_mode_get,
3025 .put = vmaster_mute_mode_put,
3026 };
3027
3028 /**
3029 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3030 * @codec: the HDA codec
3031 * @hook: the vmaster hook object
3032 * @expose_enum_ctl: flag to create an enum ctl
3033 *
3034 * Add a mute-LED hook with the given vmaster switch kctl.
3035 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3036 * created and associated with the given hook.
3037 */
3038 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
3039 struct hda_vmaster_mute_hook *hook,
3040 bool expose_enum_ctl)
3041 {
3042 struct snd_kcontrol *kctl;
3043
3044 if (!hook->hook || !hook->sw_kctl)
3045 return 0;
3046 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3047 hook->codec = codec;
3048 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3049 if (!expose_enum_ctl)
3050 return 0;
3051 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3052 if (!kctl)
3053 return -ENOMEM;
3054 return snd_hda_ctl_add(codec, 0, kctl);
3055 }
3056 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
3057
3058 /**
3059 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3060 * @hook: the vmaster hook
3061 *
3062 * Call the hook with the current value for synchronization.
3063 * Should be called in init callback.
3064 */
3065 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3066 {
3067 if (!hook->hook || !hook->codec)
3068 return;
3069 /* don't call vmaster hook in the destructor since it might have
3070 * been already destroyed
3071 */
3072 if (hook->codec->bus->shutdown)
3073 return;
3074 switch (hook->mute_mode) {
3075 case HDA_VMUTE_FOLLOW_MASTER:
3076 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3077 break;
3078 default:
3079 hook->hook(hook->codec, hook->mute_mode);
3080 break;
3081 }
3082 }
3083 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
3084
3085
3086 /**
3087 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3088 * @kcontrol: referred ctl element
3089 * @uinfo: pointer to get/store the data
3090 *
3091 * The control element is supposed to have the private_value field
3092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3093 */
3094 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_info *uinfo)
3096 {
3097 int chs = get_amp_channels(kcontrol);
3098
3099 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3100 uinfo->count = chs == 3 ? 2 : 1;
3101 uinfo->value.integer.min = 0;
3102 uinfo->value.integer.max = 1;
3103 return 0;
3104 }
3105 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
3106
3107 /**
3108 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3109 * @kcontrol: ctl element
3110 * @ucontrol: pointer to get/store the data
3111 *
3112 * The control element is supposed to have the private_value field
3113 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3114 */
3115 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3116 struct snd_ctl_elem_value *ucontrol)
3117 {
3118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3119 hda_nid_t nid = get_amp_nid(kcontrol);
3120 int chs = get_amp_channels(kcontrol);
3121 int dir = get_amp_direction(kcontrol);
3122 int idx = get_amp_index(kcontrol);
3123 long *valp = ucontrol->value.integer.value;
3124
3125 if (chs & 1)
3126 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3127 HDA_AMP_MUTE) ? 0 : 1;
3128 if (chs & 2)
3129 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3130 HDA_AMP_MUTE) ? 0 : 1;
3131 return 0;
3132 }
3133 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
3134
3135 /**
3136 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3137 * @kcontrol: ctl element
3138 * @ucontrol: pointer to get/store the data
3139 *
3140 * The control element is supposed to have the private_value field
3141 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3142 */
3143 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3144 struct snd_ctl_elem_value *ucontrol)
3145 {
3146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3147 hda_nid_t nid = get_amp_nid(kcontrol);
3148 int chs = get_amp_channels(kcontrol);
3149 int dir = get_amp_direction(kcontrol);
3150 int idx = get_amp_index(kcontrol);
3151 long *valp = ucontrol->value.integer.value;
3152 int change = 0;
3153
3154 snd_hda_power_up(codec);
3155 if (chs & 1) {
3156 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3157 HDA_AMP_MUTE,
3158 *valp ? 0 : HDA_AMP_MUTE);
3159 valp++;
3160 }
3161 if (chs & 2)
3162 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3163 HDA_AMP_MUTE,
3164 *valp ? 0 : HDA_AMP_MUTE);
3165 hda_call_check_power_status(codec, nid);
3166 snd_hda_power_down(codec);
3167 return change;
3168 }
3169 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
3170
3171 /*
3172 * bound volume controls
3173 *
3174 * bind multiple volumes (# indices, from 0)
3175 */
3176
3177 #define AMP_VAL_IDX_SHIFT 19
3178 #define AMP_VAL_IDX_MASK (0x0f<<19)
3179
3180 /**
3181 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3182 * @kcontrol: ctl element
3183 * @ucontrol: pointer to get/store the data
3184 *
3185 * The control element is supposed to have the private_value field
3186 * set up via HDA_BIND_MUTE*() macros.
3187 */
3188 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
3190 {
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 unsigned long pval;
3193 int err;
3194
3195 mutex_lock(&codec->control_mutex);
3196 pval = kcontrol->private_value;
3197 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3198 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3199 kcontrol->private_value = pval;
3200 mutex_unlock(&codec->control_mutex);
3201 return err;
3202 }
3203 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
3204
3205 /**
3206 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3207 * @kcontrol: ctl element
3208 * @ucontrol: pointer to get/store the data
3209 *
3210 * The control element is supposed to have the private_value field
3211 * set up via HDA_BIND_MUTE*() macros.
3212 */
3213 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3215 {
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3217 unsigned long pval;
3218 int i, indices, err = 0, change = 0;
3219
3220 mutex_lock(&codec->control_mutex);
3221 pval = kcontrol->private_value;
3222 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3223 for (i = 0; i < indices; i++) {
3224 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3225 (i << AMP_VAL_IDX_SHIFT);
3226 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3227 if (err < 0)
3228 break;
3229 change |= err;
3230 }
3231 kcontrol->private_value = pval;
3232 mutex_unlock(&codec->control_mutex);
3233 return err < 0 ? err : change;
3234 }
3235 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
3236
3237 /**
3238 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3239 * @kcontrol: referred ctl element
3240 * @uinfo: pointer to get/store the data
3241 *
3242 * The control element is supposed to have the private_value field
3243 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3244 */
3245 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3246 struct snd_ctl_elem_info *uinfo)
3247 {
3248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3249 struct hda_bind_ctls *c;
3250 int err;
3251
3252 mutex_lock(&codec->control_mutex);
3253 c = (struct hda_bind_ctls *)kcontrol->private_value;
3254 kcontrol->private_value = *c->values;
3255 err = c->ops->info(kcontrol, uinfo);
3256 kcontrol->private_value = (long)c;
3257 mutex_unlock(&codec->control_mutex);
3258 return err;
3259 }
3260 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
3261
3262 /**
3263 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3264 * @kcontrol: ctl element
3265 * @ucontrol: pointer to get/store the data
3266 *
3267 * The control element is supposed to have the private_value field
3268 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3269 */
3270 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3271 struct snd_ctl_elem_value *ucontrol)
3272 {
3273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3274 struct hda_bind_ctls *c;
3275 int err;
3276
3277 mutex_lock(&codec->control_mutex);
3278 c = (struct hda_bind_ctls *)kcontrol->private_value;
3279 kcontrol->private_value = *c->values;
3280 err = c->ops->get(kcontrol, ucontrol);
3281 kcontrol->private_value = (long)c;
3282 mutex_unlock(&codec->control_mutex);
3283 return err;
3284 }
3285 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
3286
3287 /**
3288 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3289 * @kcontrol: ctl element
3290 * @ucontrol: pointer to get/store the data
3291 *
3292 * The control element is supposed to have the private_value field
3293 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3294 */
3295 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3296 struct snd_ctl_elem_value *ucontrol)
3297 {
3298 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3299 struct hda_bind_ctls *c;
3300 unsigned long *vals;
3301 int err = 0, change = 0;
3302
3303 mutex_lock(&codec->control_mutex);
3304 c = (struct hda_bind_ctls *)kcontrol->private_value;
3305 for (vals = c->values; *vals; vals++) {
3306 kcontrol->private_value = *vals;
3307 err = c->ops->put(kcontrol, ucontrol);
3308 if (err < 0)
3309 break;
3310 change |= err;
3311 }
3312 kcontrol->private_value = (long)c;
3313 mutex_unlock(&codec->control_mutex);
3314 return err < 0 ? err : change;
3315 }
3316 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3317
3318 /**
3319 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3320 * @kcontrol: ctl element
3321 * @op_flag: operation flag
3322 * @size: byte size of input TLV
3323 * @tlv: TLV data
3324 *
3325 * The control element is supposed to have the private_value field
3326 * set up via HDA_BIND_VOL() macro.
3327 */
3328 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3329 unsigned int size, unsigned int __user *tlv)
3330 {
3331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3332 struct hda_bind_ctls *c;
3333 int err;
3334
3335 mutex_lock(&codec->control_mutex);
3336 c = (struct hda_bind_ctls *)kcontrol->private_value;
3337 kcontrol->private_value = *c->values;
3338 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3339 kcontrol->private_value = (long)c;
3340 mutex_unlock(&codec->control_mutex);
3341 return err;
3342 }
3343 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3344
3345 struct hda_ctl_ops snd_hda_bind_vol = {
3346 .info = snd_hda_mixer_amp_volume_info,
3347 .get = snd_hda_mixer_amp_volume_get,
3348 .put = snd_hda_mixer_amp_volume_put,
3349 .tlv = snd_hda_mixer_amp_tlv
3350 };
3351 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3352
3353 struct hda_ctl_ops snd_hda_bind_sw = {
3354 .info = snd_hda_mixer_amp_switch_info,
3355 .get = snd_hda_mixer_amp_switch_get,
3356 .put = snd_hda_mixer_amp_switch_put,
3357 .tlv = snd_hda_mixer_amp_tlv
3358 };
3359 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3360
3361 /*
3362 * SPDIF out controls
3363 */
3364
3365 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_info *uinfo)
3367 {
3368 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3369 uinfo->count = 1;
3370 return 0;
3371 }
3372
3373 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
3375 {
3376 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3377 IEC958_AES0_NONAUDIO |
3378 IEC958_AES0_CON_EMPHASIS_5015 |
3379 IEC958_AES0_CON_NOT_COPYRIGHT;
3380 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3381 IEC958_AES1_CON_ORIGINAL;
3382 return 0;
3383 }
3384
3385 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_value *ucontrol)
3387 {
3388 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3389 IEC958_AES0_NONAUDIO |
3390 IEC958_AES0_PRO_EMPHASIS_5015;
3391 return 0;
3392 }
3393
3394 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3395 struct snd_ctl_elem_value *ucontrol)
3396 {
3397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3398 int idx = kcontrol->private_value;
3399 struct hda_spdif_out *spdif;
3400
3401 mutex_lock(&codec->spdif_mutex);
3402 spdif = snd_array_elem(&codec->spdif_out, idx);
3403 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3404 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3405 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3406 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3407 mutex_unlock(&codec->spdif_mutex);
3408
3409 return 0;
3410 }
3411
3412 /* convert from SPDIF status bits to HDA SPDIF bits
3413 * bit 0 (DigEn) is always set zero (to be filled later)
3414 */
3415 static unsigned short convert_from_spdif_status(unsigned int sbits)
3416 {
3417 unsigned short val = 0;
3418
3419 if (sbits & IEC958_AES0_PROFESSIONAL)
3420 val |= AC_DIG1_PROFESSIONAL;
3421 if (sbits & IEC958_AES0_NONAUDIO)
3422 val |= AC_DIG1_NONAUDIO;
3423 if (sbits & IEC958_AES0_PROFESSIONAL) {
3424 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3425 IEC958_AES0_PRO_EMPHASIS_5015)
3426 val |= AC_DIG1_EMPHASIS;
3427 } else {
3428 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3429 IEC958_AES0_CON_EMPHASIS_5015)
3430 val |= AC_DIG1_EMPHASIS;
3431 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3432 val |= AC_DIG1_COPYRIGHT;
3433 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3434 val |= AC_DIG1_LEVEL;
3435 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3436 }
3437 return val;
3438 }
3439
3440 /* convert to SPDIF status bits from HDA SPDIF bits
3441 */
3442 static unsigned int convert_to_spdif_status(unsigned short val)
3443 {
3444 unsigned int sbits = 0;
3445
3446 if (val & AC_DIG1_NONAUDIO)
3447 sbits |= IEC958_AES0_NONAUDIO;
3448 if (val & AC_DIG1_PROFESSIONAL)
3449 sbits |= IEC958_AES0_PROFESSIONAL;
3450 if (sbits & IEC958_AES0_PROFESSIONAL) {
3451 if (val & AC_DIG1_EMPHASIS)
3452 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3453 } else {
3454 if (val & AC_DIG1_EMPHASIS)
3455 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3456 if (!(val & AC_DIG1_COPYRIGHT))
3457 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3458 if (val & AC_DIG1_LEVEL)
3459 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3460 sbits |= val & (0x7f << 8);
3461 }
3462 return sbits;
3463 }
3464
3465 /* set digital convert verbs both for the given NID and its slaves */
3466 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3467 int verb, int val)
3468 {
3469 const hda_nid_t *d;
3470
3471 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3472 d = codec->slave_dig_outs;
3473 if (!d)
3474 return;
3475 for (; *d; d++)
3476 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3477 }
3478
3479 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3480 int dig1, int dig2)
3481 {
3482 if (dig1 != -1)
3483 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3484 if (dig2 != -1)
3485 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3486 }
3487
3488 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3489 struct snd_ctl_elem_value *ucontrol)
3490 {
3491 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3492 int idx = kcontrol->private_value;
3493 struct hda_spdif_out *spdif;
3494 hda_nid_t nid;
3495 unsigned short val;
3496 int change;
3497
3498 mutex_lock(&codec->spdif_mutex);
3499 spdif = snd_array_elem(&codec->spdif_out, idx);
3500 nid = spdif->nid;
3501 spdif->status = ucontrol->value.iec958.status[0] |
3502 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3503 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3504 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3505 val = convert_from_spdif_status(spdif->status);
3506 val |= spdif->ctls & 1;
3507 change = spdif->ctls != val;
3508 spdif->ctls = val;
3509 if (change && nid != (u16)-1)
3510 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3511 mutex_unlock(&codec->spdif_mutex);
3512 return change;
3513 }
3514
3515 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3516
3517 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3518 struct snd_ctl_elem_value *ucontrol)
3519 {
3520 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3521 int idx = kcontrol->private_value;
3522 struct hda_spdif_out *spdif;
3523
3524 mutex_lock(&codec->spdif_mutex);
3525 spdif = snd_array_elem(&codec->spdif_out, idx);
3526 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3527 mutex_unlock(&codec->spdif_mutex);
3528 return 0;
3529 }
3530
3531 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3532 int dig1, int dig2)
3533 {
3534 set_dig_out_convert(codec, nid, dig1, dig2);
3535 /* unmute amp switch (if any) */
3536 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3537 (dig1 & AC_DIG1_ENABLE))
3538 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3539 HDA_AMP_MUTE, 0);
3540 }
3541
3542 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3544 {
3545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3546 int idx = kcontrol->private_value;
3547 struct hda_spdif_out *spdif;
3548 hda_nid_t nid;
3549 unsigned short val;
3550 int change;
3551
3552 mutex_lock(&codec->spdif_mutex);
3553 spdif = snd_array_elem(&codec->spdif_out, idx);
3554 nid = spdif->nid;
3555 val = spdif->ctls & ~AC_DIG1_ENABLE;
3556 if (ucontrol->value.integer.value[0])
3557 val |= AC_DIG1_ENABLE;
3558 change = spdif->ctls != val;
3559 spdif->ctls = val;
3560 if (change && nid != (u16)-1)
3561 set_spdif_ctls(codec, nid, val & 0xff, -1);
3562 mutex_unlock(&codec->spdif_mutex);
3563 return change;
3564 }
3565
3566 static struct snd_kcontrol_new dig_mixes[] = {
3567 {
3568 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3570 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3571 .info = snd_hda_spdif_mask_info,
3572 .get = snd_hda_spdif_cmask_get,
3573 },
3574 {
3575 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3576 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3577 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3578 .info = snd_hda_spdif_mask_info,
3579 .get = snd_hda_spdif_pmask_get,
3580 },
3581 {
3582 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3583 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3584 .info = snd_hda_spdif_mask_info,
3585 .get = snd_hda_spdif_default_get,
3586 .put = snd_hda_spdif_default_put,
3587 },
3588 {
3589 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3590 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3591 .info = snd_hda_spdif_out_switch_info,
3592 .get = snd_hda_spdif_out_switch_get,
3593 .put = snd_hda_spdif_out_switch_put,
3594 },
3595 { } /* end */
3596 };
3597
3598 /**
3599 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3600 * @codec: the HDA codec
3601 * @associated_nid: NID that new ctls associated with
3602 * @cvt_nid: converter NID
3603 * @type: HDA_PCM_TYPE_*
3604 * Creates controls related with the digital output.
3605 * Called from each patch supporting the digital out.
3606 *
3607 * Returns 0 if successful, or a negative error code.
3608 */
3609 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3610 hda_nid_t associated_nid,
3611 hda_nid_t cvt_nid,
3612 int type)
3613 {
3614 int err;
3615 struct snd_kcontrol *kctl;
3616 struct snd_kcontrol_new *dig_mix;
3617 int idx = 0;
3618 const int spdif_index = 16;
3619 struct hda_spdif_out *spdif;
3620 struct hda_bus *bus = codec->bus;
3621
3622 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3623 type == HDA_PCM_TYPE_SPDIF) {
3624 idx = spdif_index;
3625 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3626 type == HDA_PCM_TYPE_HDMI) {
3627 /* suppose a single SPDIF device */
3628 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3629 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3630 if (!kctl)
3631 break;
3632 kctl->id.index = spdif_index;
3633 }
3634 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3635 }
3636 if (!bus->primary_dig_out_type)
3637 bus->primary_dig_out_type = type;
3638
3639 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3640 if (idx < 0) {
3641 codec_err(codec, "too many IEC958 outputs\n");
3642 return -EBUSY;
3643 }
3644 spdif = snd_array_new(&codec->spdif_out);
3645 if (!spdif)
3646 return -ENOMEM;
3647 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3648 kctl = snd_ctl_new1(dig_mix, codec);
3649 if (!kctl)
3650 return -ENOMEM;
3651 kctl->id.index = idx;
3652 kctl->private_value = codec->spdif_out.used - 1;
3653 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3654 if (err < 0)
3655 return err;
3656 }
3657 spdif->nid = cvt_nid;
3658 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3659 AC_VERB_GET_DIGI_CONVERT_1, 0);
3660 spdif->status = convert_to_spdif_status(spdif->ctls);
3661 return 0;
3662 }
3663 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3664
3665 /**
3666 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3667 * @codec: the HDA codec
3668 * @nid: widget NID
3669 *
3670 * call within spdif_mutex lock
3671 */
3672 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3673 hda_nid_t nid)
3674 {
3675 int i;
3676 for (i = 0; i < codec->spdif_out.used; i++) {
3677 struct hda_spdif_out *spdif =
3678 snd_array_elem(&codec->spdif_out, i);
3679 if (spdif->nid == nid)
3680 return spdif;
3681 }
3682 return NULL;
3683 }
3684 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3685
3686 /**
3687 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3688 * @codec: the HDA codec
3689 * @idx: the SPDIF ctl index
3690 *
3691 * Unassign the widget from the given SPDIF control.
3692 */
3693 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3694 {
3695 struct hda_spdif_out *spdif;
3696
3697 mutex_lock(&codec->spdif_mutex);
3698 spdif = snd_array_elem(&codec->spdif_out, idx);
3699 spdif->nid = (u16)-1;
3700 mutex_unlock(&codec->spdif_mutex);
3701 }
3702 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3703
3704 /**
3705 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3706 * @codec: the HDA codec
3707 * @idx: the SPDIF ctl idx
3708 * @nid: widget NID
3709 *
3710 * Assign the widget to the SPDIF control with the given index.
3711 */
3712 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3713 {
3714 struct hda_spdif_out *spdif;
3715 unsigned short val;
3716
3717 mutex_lock(&codec->spdif_mutex);
3718 spdif = snd_array_elem(&codec->spdif_out, idx);
3719 if (spdif->nid != nid) {
3720 spdif->nid = nid;
3721 val = spdif->ctls;
3722 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3723 }
3724 mutex_unlock(&codec->spdif_mutex);
3725 }
3726 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3727
3728 /*
3729 * SPDIF sharing with analog output
3730 */
3731 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3732 struct snd_ctl_elem_value *ucontrol)
3733 {
3734 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3735 ucontrol->value.integer.value[0] = mout->share_spdif;
3736 return 0;
3737 }
3738
3739 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3740 struct snd_ctl_elem_value *ucontrol)
3741 {
3742 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3743 mout->share_spdif = !!ucontrol->value.integer.value[0];
3744 return 0;
3745 }
3746
3747 static struct snd_kcontrol_new spdif_share_sw = {
3748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3749 .name = "IEC958 Default PCM Playback Switch",
3750 .info = snd_ctl_boolean_mono_info,
3751 .get = spdif_share_sw_get,
3752 .put = spdif_share_sw_put,
3753 };
3754
3755 /**
3756 * snd_hda_create_spdif_share_sw - create Default PCM switch
3757 * @codec: the HDA codec
3758 * @mout: multi-out instance
3759 */
3760 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3761 struct hda_multi_out *mout)
3762 {
3763 struct snd_kcontrol *kctl;
3764
3765 if (!mout->dig_out_nid)
3766 return 0;
3767
3768 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3769 if (!kctl)
3770 return -ENOMEM;
3771 /* ATTENTION: here mout is passed as private_data, instead of codec */
3772 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3773 }
3774 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3775
3776 /*
3777 * SPDIF input
3778 */
3779
3780 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3781
3782 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3783 struct snd_ctl_elem_value *ucontrol)
3784 {
3785 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3786
3787 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3788 return 0;
3789 }
3790
3791 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3792 struct snd_ctl_elem_value *ucontrol)
3793 {
3794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3795 hda_nid_t nid = kcontrol->private_value;
3796 unsigned int val = !!ucontrol->value.integer.value[0];
3797 int change;
3798
3799 mutex_lock(&codec->spdif_mutex);
3800 change = codec->spdif_in_enable != val;
3801 if (change) {
3802 codec->spdif_in_enable = val;
3803 snd_hda_codec_write_cache(codec, nid, 0,
3804 AC_VERB_SET_DIGI_CONVERT_1, val);
3805 }
3806 mutex_unlock(&codec->spdif_mutex);
3807 return change;
3808 }
3809
3810 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3811 struct snd_ctl_elem_value *ucontrol)
3812 {
3813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3814 hda_nid_t nid = kcontrol->private_value;
3815 unsigned short val;
3816 unsigned int sbits;
3817
3818 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3819 sbits = convert_to_spdif_status(val);
3820 ucontrol->value.iec958.status[0] = sbits;
3821 ucontrol->value.iec958.status[1] = sbits >> 8;
3822 ucontrol->value.iec958.status[2] = sbits >> 16;
3823 ucontrol->value.iec958.status[3] = sbits >> 24;
3824 return 0;
3825 }
3826
3827 static struct snd_kcontrol_new dig_in_ctls[] = {
3828 {
3829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3830 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3831 .info = snd_hda_spdif_in_switch_info,
3832 .get = snd_hda_spdif_in_switch_get,
3833 .put = snd_hda_spdif_in_switch_put,
3834 },
3835 {
3836 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3837 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3838 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3839 .info = snd_hda_spdif_mask_info,
3840 .get = snd_hda_spdif_in_status_get,
3841 },
3842 { } /* end */
3843 };
3844
3845 /**
3846 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3847 * @codec: the HDA codec
3848 * @nid: audio in widget NID
3849 *
3850 * Creates controls related with the SPDIF input.
3851 * Called from each patch supporting the SPDIF in.
3852 *
3853 * Returns 0 if successful, or a negative error code.
3854 */
3855 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3856 {
3857 int err;
3858 struct snd_kcontrol *kctl;
3859 struct snd_kcontrol_new *dig_mix;
3860 int idx;
3861
3862 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3863 if (idx < 0) {
3864 codec_err(codec, "too many IEC958 inputs\n");
3865 return -EBUSY;
3866 }
3867 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3868 kctl = snd_ctl_new1(dig_mix, codec);
3869 if (!kctl)
3870 return -ENOMEM;
3871 kctl->private_value = nid;
3872 err = snd_hda_ctl_add(codec, nid, kctl);
3873 if (err < 0)
3874 return err;
3875 }
3876 codec->spdif_in_enable =
3877 snd_hda_codec_read(codec, nid, 0,
3878 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3879 AC_DIG1_ENABLE;
3880 return 0;
3881 }
3882 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3883
3884 /*
3885 * command cache
3886 */
3887
3888 /* build a 31bit cache key with the widget id and the command parameter */
3889 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3890 #define get_cmd_cache_nid(key) ((key) & 0xff)
3891 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3892
3893 /**
3894 * snd_hda_codec_write_cache - send a single command with caching
3895 * @codec: the HDA codec
3896 * @nid: NID to send the command
3897 * @flags: optional bit flags
3898 * @verb: the verb to send
3899 * @parm: the parameter for the verb
3900 *
3901 * Send a single command without waiting for response.
3902 *
3903 * Returns 0 if successful, or a negative error code.
3904 */
3905 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3906 int flags, unsigned int verb, unsigned int parm)
3907 {
3908 int err;
3909 struct hda_cache_head *c;
3910 u32 key;
3911 unsigned int cache_only;
3912
3913 cache_only = codec->cached_write;
3914 if (!cache_only) {
3915 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3916 if (err < 0)
3917 return err;
3918 }
3919
3920 /* parm may contain the verb stuff for get/set amp */
3921 verb = verb | (parm >> 8);
3922 parm &= 0xff;
3923 key = build_cmd_cache_key(nid, verb);
3924 mutex_lock(&codec->bus->cmd_mutex);
3925 c = get_alloc_hash(&codec->cmd_cache, key);
3926 if (c) {
3927 c->val = parm;
3928 c->dirty = cache_only;
3929 }
3930 mutex_unlock(&codec->bus->cmd_mutex);
3931 return 0;
3932 }
3933 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3934
3935 /**
3936 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3937 * @codec: the HDA codec
3938 * @nid: NID to send the command
3939 * @flags: optional bit flags
3940 * @verb: the verb to send
3941 * @parm: the parameter for the verb
3942 *
3943 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3944 * command if the parameter is already identical with the cached value.
3945 * If not, it sends the command and refreshes the cache.
3946 *
3947 * Returns 0 if successful, or a negative error code.
3948 */
3949 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3950 int flags, unsigned int verb, unsigned int parm)
3951 {
3952 struct hda_cache_head *c;
3953 u32 key;
3954
3955 /* parm may contain the verb stuff for get/set amp */
3956 verb = verb | (parm >> 8);
3957 parm &= 0xff;
3958 key = build_cmd_cache_key(nid, verb);
3959 mutex_lock(&codec->bus->cmd_mutex);
3960 c = get_hash(&codec->cmd_cache, key);
3961 if (c && c->val == parm) {
3962 mutex_unlock(&codec->bus->cmd_mutex);
3963 return 0;
3964 }
3965 mutex_unlock(&codec->bus->cmd_mutex);
3966 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3967 }
3968 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3969
3970 /**
3971 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3972 * @codec: HD-audio codec
3973 *
3974 * Execute all verbs recorded in the command caches to resume.
3975 */
3976 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3977 {
3978 int i;
3979
3980 mutex_lock(&codec->hash_mutex);
3981 codec->cached_write = 0;
3982 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3983 struct hda_cache_head *buffer;
3984 u32 key;
3985
3986 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3987 key = buffer->key;
3988 if (!key)
3989 continue;
3990 if (!buffer->dirty)
3991 continue;
3992 buffer->dirty = 0;
3993 mutex_unlock(&codec->hash_mutex);
3994 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3995 get_cmd_cache_cmd(key), buffer->val);
3996 mutex_lock(&codec->hash_mutex);
3997 }
3998 mutex_unlock(&codec->hash_mutex);
3999 }
4000 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
4001
4002 /**
4003 * snd_hda_sequence_write_cache - sequence writes with caching
4004 * @codec: the HDA codec
4005 * @seq: VERB array to send
4006 *
4007 * Send the commands sequentially from the given array.
4008 * Thte commands are recorded on cache for power-save and resume.
4009 * The array must be terminated with NID=0.
4010 */
4011 void snd_hda_sequence_write_cache(struct hda_codec *codec,
4012 const struct hda_verb *seq)
4013 {
4014 for (; seq->nid; seq++)
4015 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
4016 seq->param);
4017 }
4018 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
4019
4020 /**
4021 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4022 * @codec: HD-audio codec
4023 */
4024 void snd_hda_codec_flush_cache(struct hda_codec *codec)
4025 {
4026 snd_hda_codec_resume_amp(codec);
4027 snd_hda_codec_resume_cache(codec);
4028 }
4029 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
4030
4031 /**
4032 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4033 * @codec: the HDA codec
4034 * @fg: function group (not used now)
4035 * @power_state: the power state to set (AC_PWRST_*)
4036 *
4037 * Set the given power state to all widgets that have the power control.
4038 * If the codec has power_filter set, it evaluates the power state and
4039 * filter out if it's unchanged as D3.
4040 */
4041 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
4042 unsigned int power_state)
4043 {
4044 hda_nid_t nid = codec->start_nid;
4045 int i;
4046
4047 for (i = 0; i < codec->num_nodes; i++, nid++) {
4048 unsigned int wcaps = get_wcaps(codec, nid);
4049 unsigned int state = power_state;
4050 if (!(wcaps & AC_WCAP_POWER))
4051 continue;
4052 if (codec->power_filter) {
4053 state = codec->power_filter(codec, nid, power_state);
4054 if (state != power_state && power_state == AC_PWRST_D3)
4055 continue;
4056 }
4057 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
4058 state);
4059 }
4060 }
4061 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
4062
4063 /*
4064 * supported power states check
4065 */
4066 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4067 unsigned int power_state)
4068 {
4069 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4070
4071 if (sup == -1)
4072 return false;
4073 if (sup & power_state)
4074 return true;
4075 else
4076 return false;
4077 }
4078
4079 /*
4080 * wait until the state is reached, returns the current state
4081 */
4082 static unsigned int hda_sync_power_state(struct hda_codec *codec,
4083 hda_nid_t fg,
4084 unsigned int power_state)
4085 {
4086 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4087 unsigned int state, actual_state;
4088
4089 for (;;) {
4090 state = snd_hda_codec_read(codec, fg, 0,
4091 AC_VERB_GET_POWER_STATE, 0);
4092 if (state & AC_PWRST_ERROR)
4093 break;
4094 actual_state = (state >> 4) & 0x0f;
4095 if (actual_state == power_state)
4096 break;
4097 if (time_after_eq(jiffies, end_time))
4098 break;
4099 /* wait until the codec reachs to the target state */
4100 msleep(1);
4101 }
4102 return state;
4103 }
4104
4105 /**
4106 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4107 * @codec: the HDA codec
4108 * @nid: widget NID
4109 * @power_state: power state to evalue
4110 *
4111 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4112 * This can be used a codec power_filter callback.
4113 */
4114 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4115 hda_nid_t nid,
4116 unsigned int power_state)
4117 {
4118 if (nid == codec->afg || nid == codec->mfg)
4119 return power_state;
4120 if (power_state == AC_PWRST_D3 &&
4121 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4122 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4123 int eapd = snd_hda_codec_read(codec, nid, 0,
4124 AC_VERB_GET_EAPD_BTLENABLE, 0);
4125 if (eapd & 0x02)
4126 return AC_PWRST_D0;
4127 }
4128 return power_state;
4129 }
4130 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
4131
4132 /*
4133 * set power state of the codec, and return the power state
4134 */
4135 static unsigned int hda_set_power_state(struct hda_codec *codec,
4136 unsigned int power_state)
4137 {
4138 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
4139 int count;
4140 unsigned int state;
4141 int flags = 0;
4142
4143 /* this delay seems necessary to avoid click noise at power-down */
4144 if (power_state == AC_PWRST_D3) {
4145 if (codec->depop_delay < 0)
4146 msleep(codec->epss ? 10 : 100);
4147 else if (codec->depop_delay > 0)
4148 msleep(codec->depop_delay);
4149 flags = HDA_RW_NO_RESPONSE_FALLBACK;
4150 }
4151
4152 /* repeat power states setting at most 10 times*/
4153 for (count = 0; count < 10; count++) {
4154 if (codec->patch_ops.set_power_state)
4155 codec->patch_ops.set_power_state(codec, fg,
4156 power_state);
4157 else {
4158 state = power_state;
4159 if (codec->power_filter)
4160 state = codec->power_filter(codec, fg, state);
4161 if (state == power_state || power_state != AC_PWRST_D3)
4162 snd_hda_codec_read(codec, fg, flags,
4163 AC_VERB_SET_POWER_STATE,
4164 state);
4165 snd_hda_codec_set_power_to_all(codec, fg, power_state);
4166 }
4167 state = hda_sync_power_state(codec, fg, power_state);
4168 if (!(state & AC_PWRST_ERROR))
4169 break;
4170 }
4171
4172 return state;
4173 }
4174
4175 /* sync power states of all widgets;
4176 * this is called at the end of codec parsing
4177 */
4178 static void sync_power_up_states(struct hda_codec *codec)
4179 {
4180 hda_nid_t nid = codec->start_nid;
4181 int i;
4182
4183 /* don't care if no filter is used */
4184 if (!codec->power_filter)
4185 return;
4186
4187 for (i = 0; i < codec->num_nodes; i++, nid++) {
4188 unsigned int wcaps = get_wcaps(codec, nid);
4189 unsigned int target;
4190 if (!(wcaps & AC_WCAP_POWER))
4191 continue;
4192 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4193 if (target == AC_PWRST_D0)
4194 continue;
4195 if (!snd_hda_check_power_state(codec, nid, target))
4196 snd_hda_codec_write(codec, nid, 0,
4197 AC_VERB_SET_POWER_STATE, target);
4198 }
4199 }
4200
4201 #ifdef CONFIG_SND_HDA_RECONFIG
4202 /* execute additional init verbs */
4203 static void hda_exec_init_verbs(struct hda_codec *codec)
4204 {
4205 if (codec->init_verbs.list)
4206 snd_hda_sequence_write(codec, codec->init_verbs.list);
4207 }
4208 #else
4209 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4210 #endif
4211
4212 #ifdef CONFIG_PM
4213 /*
4214 * call suspend and power-down; used both from PM and power-save
4215 * this function returns the power state in the end
4216 */
4217 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4218 {
4219 unsigned int state;
4220
4221 codec->in_pm = 1;
4222
4223 if (codec->patch_ops.suspend)
4224 codec->patch_ops.suspend(codec);
4225 hda_cleanup_all_streams(codec);
4226 state = hda_set_power_state(codec, AC_PWRST_D3);
4227 /* Cancel delayed work if we aren't currently running from it. */
4228 if (!in_wq)
4229 cancel_delayed_work_sync(&codec->power_work);
4230 spin_lock(&codec->power_lock);
4231 snd_hda_update_power_acct(codec);
4232 trace_hda_power_down(codec);
4233 codec->power_on = 0;
4234 codec->power_transition = 0;
4235 codec->power_jiffies = jiffies;
4236 spin_unlock(&codec->power_lock);
4237 codec->in_pm = 0;
4238 return state;
4239 }
4240
4241 /* mark all entries of cmd and amp caches dirty */
4242 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4243 {
4244 int i;
4245 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4246 struct hda_cache_head *cmd;
4247 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4248 cmd->dirty = 1;
4249 }
4250 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4251 struct hda_amp_info *amp;
4252 amp = snd_array_elem(&codec->amp_cache.buf, i);
4253 amp->head.dirty = 1;
4254 }
4255 }
4256
4257 /*
4258 * kick up codec; used both from PM and power-save
4259 */
4260 static void hda_call_codec_resume(struct hda_codec *codec)
4261 {
4262 codec->in_pm = 1;
4263
4264 hda_mark_cmd_cache_dirty(codec);
4265
4266 /* set as if powered on for avoiding re-entering the resume
4267 * in the resume / power-save sequence
4268 */
4269 hda_keep_power_on(codec);
4270 hda_set_power_state(codec, AC_PWRST_D0);
4271 restore_shutup_pins(codec);
4272 hda_exec_init_verbs(codec);
4273 snd_hda_jack_set_dirty_all(codec);
4274 if (codec->patch_ops.resume)
4275 codec->patch_ops.resume(codec);
4276 else {
4277 if (codec->patch_ops.init)
4278 codec->patch_ops.init(codec);
4279 snd_hda_codec_resume_amp(codec);
4280 snd_hda_codec_resume_cache(codec);
4281 }
4282
4283 if (codec->jackpoll_interval)
4284 hda_jackpoll_work(&codec->jackpoll_work.work);
4285 else
4286 snd_hda_jack_report_sync(codec);
4287
4288 codec->in_pm = 0;
4289 snd_hda_power_down(codec); /* flag down before returning */
4290 }
4291 #endif /* CONFIG_PM */
4292
4293
4294 /**
4295 * snd_hda_build_controls - build mixer controls
4296 * @bus: the BUS
4297 *
4298 * Creates mixer controls for each codec included in the bus.
4299 *
4300 * Returns 0 if successful, otherwise a negative error code.
4301 */
4302 int snd_hda_build_controls(struct hda_bus *bus)
4303 {
4304 struct hda_codec *codec;
4305
4306 list_for_each_entry(codec, &bus->codec_list, list) {
4307 int err = snd_hda_codec_build_controls(codec);
4308 if (err < 0) {
4309 codec_err(codec,
4310 "cannot build controls for #%d (error %d)\n",
4311 codec->addr, err);
4312 err = snd_hda_codec_reset(codec);
4313 if (err < 0) {
4314 codec_err(codec,
4315 "cannot revert codec\n");
4316 return err;
4317 }
4318 }
4319 }
4320 return 0;
4321 }
4322 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4323
4324 /*
4325 * add standard channel maps if not specified
4326 */
4327 static int add_std_chmaps(struct hda_codec *codec)
4328 {
4329 int i, str, err;
4330
4331 for (i = 0; i < codec->num_pcms; i++) {
4332 for (str = 0; str < 2; str++) {
4333 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4334 struct hda_pcm_stream *hinfo =
4335 &codec->pcm_info[i].stream[str];
4336 struct snd_pcm_chmap *chmap;
4337 const struct snd_pcm_chmap_elem *elem;
4338
4339 if (codec->pcm_info[i].own_chmap)
4340 continue;
4341 if (!pcm || !hinfo->substreams)
4342 continue;
4343 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4344 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4345 hinfo->channels_max,
4346 0, &chmap);
4347 if (err < 0)
4348 return err;
4349 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4350 }
4351 }
4352 return 0;
4353 }
4354
4355 /* default channel maps for 2.1 speakers;
4356 * since HD-audio supports only stereo, odd number channels are omitted
4357 */
4358 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4359 { .channels = 2,
4360 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4361 { .channels = 4,
4362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4363 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4364 { }
4365 };
4366 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4367
4368 int snd_hda_codec_build_controls(struct hda_codec *codec)
4369 {
4370 int err = 0;
4371 hda_exec_init_verbs(codec);
4372 /* continue to initialize... */
4373 if (codec->patch_ops.init)
4374 err = codec->patch_ops.init(codec);
4375 if (!err && codec->patch_ops.build_controls)
4376 err = codec->patch_ops.build_controls(codec);
4377 if (err < 0)
4378 return err;
4379
4380 /* we create chmaps here instead of build_pcms */
4381 err = add_std_chmaps(codec);
4382 if (err < 0)
4383 return err;
4384
4385 if (codec->jackpoll_interval)
4386 hda_jackpoll_work(&codec->jackpoll_work.work);
4387 else
4388 snd_hda_jack_report_sync(codec); /* call at the last init point */
4389 sync_power_up_states(codec);
4390 return 0;
4391 }
4392
4393 /*
4394 * stream formats
4395 */
4396 struct hda_rate_tbl {
4397 unsigned int hz;
4398 unsigned int alsa_bits;
4399 unsigned int hda_fmt;
4400 };
4401
4402 /* rate = base * mult / div */
4403 #define HDA_RATE(base, mult, div) \
4404 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4405 (((div) - 1) << AC_FMT_DIV_SHIFT))
4406
4407 static struct hda_rate_tbl rate_bits[] = {
4408 /* rate in Hz, ALSA rate bitmask, HDA format value */
4409
4410 /* autodetected value used in snd_hda_query_supported_pcm */
4411 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4412 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4413 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4414 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4415 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4416 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4417 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4418 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4419 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4420 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4421 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4422 #define AC_PAR_PCM_RATE_BITS 11
4423 /* up to bits 10, 384kHZ isn't supported properly */
4424
4425 /* not autodetected value */
4426 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4427
4428 { 0 } /* terminator */
4429 };
4430
4431 /**
4432 * snd_hda_calc_stream_format - calculate format bitset
4433 * @codec: HD-audio codec
4434 * @rate: the sample rate
4435 * @channels: the number of channels
4436 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4437 * @maxbps: the max. bps
4438 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4439 *
4440 * Calculate the format bitset from the given rate, channels and th PCM format.
4441 *
4442 * Return zero if invalid.
4443 */
4444 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4445 unsigned int rate,
4446 unsigned int channels,
4447 unsigned int format,
4448 unsigned int maxbps,
4449 unsigned short spdif_ctls)
4450 {
4451 int i;
4452 unsigned int val = 0;
4453
4454 for (i = 0; rate_bits[i].hz; i++)
4455 if (rate_bits[i].hz == rate) {
4456 val = rate_bits[i].hda_fmt;
4457 break;
4458 }
4459 if (!rate_bits[i].hz) {
4460 codec_dbg(codec, "invalid rate %d\n", rate);
4461 return 0;
4462 }
4463
4464 if (channels == 0 || channels > 8) {
4465 codec_dbg(codec, "invalid channels %d\n", channels);
4466 return 0;
4467 }
4468 val |= channels - 1;
4469
4470 switch (snd_pcm_format_width(format)) {
4471 case 8:
4472 val |= AC_FMT_BITS_8;
4473 break;
4474 case 16:
4475 val |= AC_FMT_BITS_16;
4476 break;
4477 case 20:
4478 case 24:
4479 case 32:
4480 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4481 val |= AC_FMT_BITS_32;
4482 else if (maxbps >= 24)
4483 val |= AC_FMT_BITS_24;
4484 else
4485 val |= AC_FMT_BITS_20;
4486 break;
4487 default:
4488 codec_dbg(codec, "invalid format width %d\n",
4489 snd_pcm_format_width(format));
4490 return 0;
4491 }
4492
4493 if (spdif_ctls & AC_DIG1_NONAUDIO)
4494 val |= AC_FMT_TYPE_NON_PCM;
4495
4496 return val;
4497 }
4498 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4499
4500 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4501 int dir)
4502 {
4503 unsigned int val = 0;
4504 if (nid != codec->afg &&
4505 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4506 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4507 if (!val || val == -1)
4508 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4509 if (!val || val == -1)
4510 return 0;
4511 return val;
4512 }
4513
4514 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4515 {
4516 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4517 get_pcm_param);
4518 }
4519
4520 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4521 int dir)
4522 {
4523 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4524 if (!streams || streams == -1)
4525 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4526 if (!streams || streams == -1)
4527 return 0;
4528 return streams;
4529 }
4530
4531 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4532 {
4533 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4534 get_stream_param);
4535 }
4536
4537 /**
4538 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4539 * @codec: the HDA codec
4540 * @nid: NID to query
4541 * @ratesp: the pointer to store the detected rate bitflags
4542 * @formatsp: the pointer to store the detected formats
4543 * @bpsp: the pointer to store the detected format widths
4544 *
4545 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4546 * or @bsps argument is ignored.
4547 *
4548 * Returns 0 if successful, otherwise a negative error code.
4549 */
4550 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4551 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4552 {
4553 unsigned int i, val, wcaps;
4554
4555 wcaps = get_wcaps(codec, nid);
4556 val = query_pcm_param(codec, nid);
4557
4558 if (ratesp) {
4559 u32 rates = 0;
4560 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4561 if (val & (1 << i))
4562 rates |= rate_bits[i].alsa_bits;
4563 }
4564 if (rates == 0) {
4565 codec_err(codec,
4566 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4567 nid, val,
4568 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4569 return -EIO;
4570 }
4571 *ratesp = rates;
4572 }
4573
4574 if (formatsp || bpsp) {
4575 u64 formats = 0;
4576 unsigned int streams, bps;
4577
4578 streams = query_stream_param(codec, nid);
4579 if (!streams)
4580 return -EIO;
4581
4582 bps = 0;
4583 if (streams & AC_SUPFMT_PCM) {
4584 if (val & AC_SUPPCM_BITS_8) {
4585 formats |= SNDRV_PCM_FMTBIT_U8;
4586 bps = 8;
4587 }
4588 if (val & AC_SUPPCM_BITS_16) {
4589 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4590 bps = 16;
4591 }
4592 if (wcaps & AC_WCAP_DIGITAL) {
4593 if (val & AC_SUPPCM_BITS_32)
4594 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4595 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4596 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4597 if (val & AC_SUPPCM_BITS_24)
4598 bps = 24;
4599 else if (val & AC_SUPPCM_BITS_20)
4600 bps = 20;
4601 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4602 AC_SUPPCM_BITS_32)) {
4603 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4604 if (val & AC_SUPPCM_BITS_32)
4605 bps = 32;
4606 else if (val & AC_SUPPCM_BITS_24)
4607 bps = 24;
4608 else if (val & AC_SUPPCM_BITS_20)
4609 bps = 20;
4610 }
4611 }
4612 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4613 if (streams & AC_SUPFMT_FLOAT32) {
4614 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4615 if (!bps)
4616 bps = 32;
4617 }
4618 #endif
4619 if (streams == AC_SUPFMT_AC3) {
4620 /* should be exclusive */
4621 /* temporary hack: we have still no proper support
4622 * for the direct AC3 stream...
4623 */
4624 formats |= SNDRV_PCM_FMTBIT_U8;
4625 bps = 8;
4626 }
4627 if (formats == 0) {
4628 codec_err(codec,
4629 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4630 nid, val,
4631 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4632 streams);
4633 return -EIO;
4634 }
4635 if (formatsp)
4636 *formatsp = formats;
4637 if (bpsp)
4638 *bpsp = bps;
4639 }
4640
4641 return 0;
4642 }
4643 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4644
4645 /**
4646 * snd_hda_is_supported_format - Check the validity of the format
4647 * @codec: HD-audio codec
4648 * @nid: NID to check
4649 * @format: the HD-audio format value to check
4650 *
4651 * Check whether the given node supports the format value.
4652 *
4653 * Returns 1 if supported, 0 if not.
4654 */
4655 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4656 unsigned int format)
4657 {
4658 int i;
4659 unsigned int val = 0, rate, stream;
4660
4661 val = query_pcm_param(codec, nid);
4662 if (!val)
4663 return 0;
4664
4665 rate = format & 0xff00;
4666 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4667 if (rate_bits[i].hda_fmt == rate) {
4668 if (val & (1 << i))
4669 break;
4670 return 0;
4671 }
4672 if (i >= AC_PAR_PCM_RATE_BITS)
4673 return 0;
4674
4675 stream = query_stream_param(codec, nid);
4676 if (!stream)
4677 return 0;
4678
4679 if (stream & AC_SUPFMT_PCM) {
4680 switch (format & 0xf0) {
4681 case 0x00:
4682 if (!(val & AC_SUPPCM_BITS_8))
4683 return 0;
4684 break;
4685 case 0x10:
4686 if (!(val & AC_SUPPCM_BITS_16))
4687 return 0;
4688 break;
4689 case 0x20:
4690 if (!(val & AC_SUPPCM_BITS_20))
4691 return 0;
4692 break;
4693 case 0x30:
4694 if (!(val & AC_SUPPCM_BITS_24))
4695 return 0;
4696 break;
4697 case 0x40:
4698 if (!(val & AC_SUPPCM_BITS_32))
4699 return 0;
4700 break;
4701 default:
4702 return 0;
4703 }
4704 } else {
4705 /* FIXME: check for float32 and AC3? */
4706 }
4707
4708 return 1;
4709 }
4710 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4711
4712 /*
4713 * PCM stuff
4714 */
4715 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4716 struct hda_codec *codec,
4717 struct snd_pcm_substream *substream)
4718 {
4719 return 0;
4720 }
4721
4722 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4723 struct hda_codec *codec,
4724 unsigned int stream_tag,
4725 unsigned int format,
4726 struct snd_pcm_substream *substream)
4727 {
4728 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4729 return 0;
4730 }
4731
4732 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4733 struct hda_codec *codec,
4734 struct snd_pcm_substream *substream)
4735 {
4736 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4737 return 0;
4738 }
4739
4740 static int set_pcm_default_values(struct hda_codec *codec,
4741 struct hda_pcm_stream *info)
4742 {
4743 int err;
4744
4745 /* query support PCM information from the given NID */
4746 if (info->nid && (!info->rates || !info->formats)) {
4747 err = snd_hda_query_supported_pcm(codec, info->nid,
4748 info->rates ? NULL : &info->rates,
4749 info->formats ? NULL : &info->formats,
4750 info->maxbps ? NULL : &info->maxbps);
4751 if (err < 0)
4752 return err;
4753 }
4754 if (info->ops.open == NULL)
4755 info->ops.open = hda_pcm_default_open_close;
4756 if (info->ops.close == NULL)
4757 info->ops.close = hda_pcm_default_open_close;
4758 if (info->ops.prepare == NULL) {
4759 if (snd_BUG_ON(!info->nid))
4760 return -EINVAL;
4761 info->ops.prepare = hda_pcm_default_prepare;
4762 }
4763 if (info->ops.cleanup == NULL) {
4764 if (snd_BUG_ON(!info->nid))
4765 return -EINVAL;
4766 info->ops.cleanup = hda_pcm_default_cleanup;
4767 }
4768 return 0;
4769 }
4770
4771 /*
4772 * codec prepare/cleanup entries
4773 */
4774 /**
4775 * snd_hda_codec_prepare - Prepare a stream
4776 * @codec: the HDA codec
4777 * @hinfo: PCM information
4778 * @stream: stream tag to assign
4779 * @format: format id to assign
4780 * @substream: PCM substream to assign
4781 *
4782 * Calls the prepare callback set by the codec with the given arguments.
4783 * Clean up the inactive streams when successful.
4784 */
4785 int snd_hda_codec_prepare(struct hda_codec *codec,
4786 struct hda_pcm_stream *hinfo,
4787 unsigned int stream,
4788 unsigned int format,
4789 struct snd_pcm_substream *substream)
4790 {
4791 int ret;
4792 mutex_lock(&codec->bus->prepare_mutex);
4793 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4794 if (ret >= 0)
4795 purify_inactive_streams(codec);
4796 mutex_unlock(&codec->bus->prepare_mutex);
4797 return ret;
4798 }
4799 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4800
4801 /**
4802 * snd_hda_codec_cleanup - Prepare a stream
4803 * @codec: the HDA codec
4804 * @hinfo: PCM information
4805 * @substream: PCM substream
4806 *
4807 * Calls the cleanup callback set by the codec with the given arguments.
4808 */
4809 void snd_hda_codec_cleanup(struct hda_codec *codec,
4810 struct hda_pcm_stream *hinfo,
4811 struct snd_pcm_substream *substream)
4812 {
4813 mutex_lock(&codec->bus->prepare_mutex);
4814 hinfo->ops.cleanup(hinfo, codec, substream);
4815 mutex_unlock(&codec->bus->prepare_mutex);
4816 }
4817 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4818
4819 /* global */
4820 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4821 "Audio", "SPDIF", "HDMI", "Modem"
4822 };
4823
4824 /*
4825 * get the empty PCM device number to assign
4826 */
4827 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4828 {
4829 /* audio device indices; not linear to keep compatibility */
4830 /* assigned to static slots up to dev#10; if more needed, assign
4831 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4832 */
4833 static int audio_idx[HDA_PCM_NTYPES][5] = {
4834 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4835 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4836 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4837 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4838 };
4839 int i;
4840
4841 if (type >= HDA_PCM_NTYPES) {
4842 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4843 return -EINVAL;
4844 }
4845
4846 for (i = 0; audio_idx[type][i] >= 0; i++) {
4847 #ifndef CONFIG_SND_DYNAMIC_MINORS
4848 if (audio_idx[type][i] >= 8)
4849 break;
4850 #endif
4851 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4852 return audio_idx[type][i];
4853 }
4854
4855 #ifdef CONFIG_SND_DYNAMIC_MINORS
4856 /* non-fixed slots starting from 10 */
4857 for (i = 10; i < 32; i++) {
4858 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4859 return i;
4860 }
4861 #endif
4862
4863 dev_warn(bus->card->dev, "Too many %s devices\n",
4864 snd_hda_pcm_type_name[type]);
4865 #ifndef CONFIG_SND_DYNAMIC_MINORS
4866 dev_warn(bus->card->dev,
4867 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4868 #endif
4869 return -EAGAIN;
4870 }
4871
4872 /*
4873 * attach a new PCM stream
4874 */
4875 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4876 {
4877 struct hda_bus *bus = codec->bus;
4878 struct hda_pcm_stream *info;
4879 int stream, err;
4880
4881 if (snd_BUG_ON(!pcm->name))
4882 return -EINVAL;
4883 for (stream = 0; stream < 2; stream++) {
4884 info = &pcm->stream[stream];
4885 if (info->substreams) {
4886 err = set_pcm_default_values(codec, info);
4887 if (err < 0)
4888 return err;
4889 }
4890 }
4891 return bus->ops.attach_pcm(bus, codec, pcm);
4892 }
4893
4894 /* assign all PCMs of the given codec */
4895 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4896 {
4897 unsigned int pcm;
4898 int err;
4899
4900 if (!codec->num_pcms) {
4901 if (!codec->patch_ops.build_pcms)
4902 return 0;
4903 err = codec->patch_ops.build_pcms(codec);
4904 if (err < 0) {
4905 codec_err(codec,
4906 "cannot build PCMs for #%d (error %d)\n",
4907 codec->addr, err);
4908 err = snd_hda_codec_reset(codec);
4909 if (err < 0) {
4910 codec_err(codec,
4911 "cannot revert codec\n");
4912 return err;
4913 }
4914 }
4915 }
4916 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4917 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4918 int dev;
4919
4920 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4921 continue; /* no substreams assigned */
4922
4923 if (!cpcm->pcm) {
4924 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4925 if (dev < 0)
4926 continue; /* no fatal error */
4927 cpcm->device = dev;
4928 err = snd_hda_attach_pcm(codec, cpcm);
4929 if (err < 0) {
4930 codec_err(codec,
4931 "cannot attach PCM stream %d for codec #%d\n",
4932 dev, codec->addr);
4933 continue; /* no fatal error */
4934 }
4935 }
4936 }
4937 return 0;
4938 }
4939
4940 /**
4941 * snd_hda_build_pcms - build PCM information
4942 * @bus: the BUS
4943 *
4944 * Create PCM information for each codec included in the bus.
4945 *
4946 * The build_pcms codec patch is requested to set up codec->num_pcms and
4947 * codec->pcm_info properly. The array is referred by the top-level driver
4948 * to create its PCM instances.
4949 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4950 * callback.
4951 *
4952 * At least, substreams, channels_min and channels_max must be filled for
4953 * each stream. substreams = 0 indicates that the stream doesn't exist.
4954 * When rates and/or formats are zero, the supported values are queried
4955 * from the given nid. The nid is used also by the default ops.prepare
4956 * and ops.cleanup callbacks.
4957 *
4958 * The driver needs to call ops.open in its open callback. Similarly,
4959 * ops.close is supposed to be called in the close callback.
4960 * ops.prepare should be called in the prepare or hw_params callback
4961 * with the proper parameters for set up.
4962 * ops.cleanup should be called in hw_free for clean up of streams.
4963 *
4964 * This function returns 0 if successful, or a negative error code.
4965 */
4966 int snd_hda_build_pcms(struct hda_bus *bus)
4967 {
4968 struct hda_codec *codec;
4969
4970 list_for_each_entry(codec, &bus->codec_list, list) {
4971 int err = snd_hda_codec_build_pcms(codec);
4972 if (err < 0)
4973 return err;
4974 }
4975 return 0;
4976 }
4977 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4978
4979 /**
4980 * snd_hda_add_new_ctls - create controls from the array
4981 * @codec: the HDA codec
4982 * @knew: the array of struct snd_kcontrol_new
4983 *
4984 * This helper function creates and add new controls in the given array.
4985 * The array must be terminated with an empty entry as terminator.
4986 *
4987 * Returns 0 if successful, or a negative error code.
4988 */
4989 int snd_hda_add_new_ctls(struct hda_codec *codec,
4990 const struct snd_kcontrol_new *knew)
4991 {
4992 int err;
4993
4994 for (; knew->name; knew++) {
4995 struct snd_kcontrol *kctl;
4996 int addr = 0, idx = 0;
4997 if (knew->iface == -1) /* skip this codec private value */
4998 continue;
4999 for (;;) {
5000 kctl = snd_ctl_new1(knew, codec);
5001 if (!kctl)
5002 return -ENOMEM;
5003 if (addr > 0)
5004 kctl->id.device = addr;
5005 if (idx > 0)
5006 kctl->id.index = idx;
5007 err = snd_hda_ctl_add(codec, 0, kctl);
5008 if (!err)
5009 break;
5010 /* try first with another device index corresponding to
5011 * the codec addr; if it still fails (or it's the
5012 * primary codec), then try another control index
5013 */
5014 if (!addr && codec->addr)
5015 addr = codec->addr;
5016 else if (!idx && !knew->index) {
5017 idx = find_empty_mixer_ctl_idx(codec,
5018 knew->name, 0);
5019 if (idx <= 0)
5020 return err;
5021 } else
5022 return err;
5023 }
5024 }
5025 return 0;
5026 }
5027 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
5028
5029 #ifdef CONFIG_PM
5030 static void hda_power_work(struct work_struct *work)
5031 {
5032 struct hda_codec *codec =
5033 container_of(work, struct hda_codec, power_work.work);
5034 struct hda_bus *bus = codec->bus;
5035 unsigned int state;
5036
5037 spin_lock(&codec->power_lock);
5038 if (codec->power_transition > 0) { /* during power-up sequence? */
5039 spin_unlock(&codec->power_lock);
5040 return;
5041 }
5042 if (!codec->power_on || codec->power_count) {
5043 codec->power_transition = 0;
5044 spin_unlock(&codec->power_lock);
5045 return;
5046 }
5047 spin_unlock(&codec->power_lock);
5048
5049 state = hda_call_codec_suspend(codec, true);
5050 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5051 hda_call_pm_notify(codec, false);
5052 }
5053
5054 static void hda_keep_power_on(struct hda_codec *codec)
5055 {
5056 spin_lock(&codec->power_lock);
5057 codec->power_count++;
5058 codec->power_on = 1;
5059 codec->power_jiffies = jiffies;
5060 spin_unlock(&codec->power_lock);
5061 hda_call_pm_notify(codec, true);
5062 }
5063
5064 /* update the power on/off account with the current jiffies */
5065 void snd_hda_update_power_acct(struct hda_codec *codec)
5066 {
5067 unsigned long delta = jiffies - codec->power_jiffies;
5068 if (codec->power_on)
5069 codec->power_on_acct += delta;
5070 else
5071 codec->power_off_acct += delta;
5072 codec->power_jiffies += delta;
5073 }
5074
5075 /* Transition to powered up, if wait_power_down then wait for a pending
5076 * transition to D3 to complete. A pending D3 transition is indicated
5077 * with power_transition == -1. */
5078 /* call this with codec->power_lock held! */
5079 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
5080 {
5081 /* Return if power_on or transitioning to power_on, unless currently
5082 * powering down. */
5083 if ((codec->power_on || codec->power_transition > 0) &&
5084 !(wait_power_down && codec->power_transition < 0))
5085 return;
5086 spin_unlock(&codec->power_lock);
5087
5088 cancel_delayed_work_sync(&codec->power_work);
5089
5090 spin_lock(&codec->power_lock);
5091 /* If the power down delayed work was cancelled above before starting,
5092 * then there is no need to go through power up here.
5093 */
5094 if (codec->power_on) {
5095 if (codec->power_transition < 0)
5096 codec->power_transition = 0;
5097 return;
5098 }
5099
5100 trace_hda_power_up(codec);
5101 snd_hda_update_power_acct(codec);
5102 codec->power_on = 1;
5103 codec->power_jiffies = jiffies;
5104 codec->power_transition = 1; /* avoid reentrance */
5105 spin_unlock(&codec->power_lock);
5106
5107 hda_call_codec_resume(codec);
5108
5109 spin_lock(&codec->power_lock);
5110 codec->power_transition = 0;
5111 }
5112
5113 #define power_save(codec) \
5114 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
5115
5116 /* Transition to powered down */
5117 static void __snd_hda_power_down(struct hda_codec *codec)
5118 {
5119 if (!codec->power_on || codec->power_count || codec->power_transition)
5120 return;
5121
5122 if (power_save(codec)) {
5123 codec->power_transition = -1; /* avoid reentrance */
5124 queue_delayed_work(codec->bus->workq, &codec->power_work,
5125 msecs_to_jiffies(power_save(codec) * 1000));
5126 }
5127 }
5128
5129 /**
5130 * snd_hda_power_save - Power-up/down/sync the codec
5131 * @codec: HD-audio codec
5132 * @delta: the counter delta to change
5133 * @d3wait: sync for D3 transition complete
5134 *
5135 * Change the power-up counter via @delta, and power up or down the hardware
5136 * appropriately. For the power-down, queue to the delayed action.
5137 * Passing zero to @delta means to synchronize the power state.
5138 */
5139 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5140 {
5141 spin_lock(&codec->power_lock);
5142 codec->power_count += delta;
5143 trace_hda_power_count(codec);
5144 if (delta > 0)
5145 __snd_hda_power_up(codec, d3wait);
5146 else
5147 __snd_hda_power_down(codec);
5148 spin_unlock(&codec->power_lock);
5149 }
5150 EXPORT_SYMBOL_GPL(snd_hda_power_save);
5151
5152 /**
5153 * snd_hda_check_amp_list_power - Check the amp list and update the power
5154 * @codec: HD-audio codec
5155 * @check: the object containing an AMP list and the status
5156 * @nid: NID to check / update
5157 *
5158 * Check whether the given NID is in the amp list. If it's in the list,
5159 * check the current AMP status, and update the the power-status according
5160 * to the mute status.
5161 *
5162 * This function is supposed to be set or called from the check_power_status
5163 * patch ops.
5164 */
5165 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5166 struct hda_loopback_check *check,
5167 hda_nid_t nid)
5168 {
5169 const struct hda_amp_list *p;
5170 int ch, v;
5171
5172 if (!check->amplist)
5173 return 0;
5174 for (p = check->amplist; p->nid; p++) {
5175 if (p->nid == nid)
5176 break;
5177 }
5178 if (!p->nid)
5179 return 0; /* nothing changed */
5180
5181 for (p = check->amplist; p->nid; p++) {
5182 for (ch = 0; ch < 2; ch++) {
5183 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5184 p->idx);
5185 if (!(v & HDA_AMP_MUTE) && v > 0) {
5186 if (!check->power_on) {
5187 check->power_on = 1;
5188 snd_hda_power_up(codec);
5189 }
5190 return 1;
5191 }
5192 }
5193 }
5194 if (check->power_on) {
5195 check->power_on = 0;
5196 snd_hda_power_down(codec);
5197 }
5198 return 0;
5199 }
5200 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
5201 #endif
5202
5203 /*
5204 * Channel mode helper
5205 */
5206
5207 /**
5208 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5209 * @codec: the HDA codec
5210 * @uinfo: pointer to get/store the data
5211 * @chmode: channel mode array
5212 * @num_chmodes: channel mode array size
5213 */
5214 int snd_hda_ch_mode_info(struct hda_codec *codec,
5215 struct snd_ctl_elem_info *uinfo,
5216 const struct hda_channel_mode *chmode,
5217 int num_chmodes)
5218 {
5219 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5220 uinfo->count = 1;
5221 uinfo->value.enumerated.items = num_chmodes;
5222 if (uinfo->value.enumerated.item >= num_chmodes)
5223 uinfo->value.enumerated.item = num_chmodes - 1;
5224 sprintf(uinfo->value.enumerated.name, "%dch",
5225 chmode[uinfo->value.enumerated.item].channels);
5226 return 0;
5227 }
5228 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
5229
5230 /**
5231 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5232 * @codec: the HDA codec
5233 * @ucontrol: pointer to get/store the data
5234 * @chmode: channel mode array
5235 * @num_chmodes: channel mode array size
5236 * @max_channels: max number of channels
5237 */
5238 int snd_hda_ch_mode_get(struct hda_codec *codec,
5239 struct snd_ctl_elem_value *ucontrol,
5240 const struct hda_channel_mode *chmode,
5241 int num_chmodes,
5242 int max_channels)
5243 {
5244 int i;
5245
5246 for (i = 0; i < num_chmodes; i++) {
5247 if (max_channels == chmode[i].channels) {
5248 ucontrol->value.enumerated.item[0] = i;
5249 break;
5250 }
5251 }
5252 return 0;
5253 }
5254 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
5255
5256 /**
5257 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5258 * @codec: the HDA codec
5259 * @ucontrol: pointer to get/store the data
5260 * @chmode: channel mode array
5261 * @num_chmodes: channel mode array size
5262 * @max_channelsp: pointer to store the max channels
5263 */
5264 int snd_hda_ch_mode_put(struct hda_codec *codec,
5265 struct snd_ctl_elem_value *ucontrol,
5266 const struct hda_channel_mode *chmode,
5267 int num_chmodes,
5268 int *max_channelsp)
5269 {
5270 unsigned int mode;
5271
5272 mode = ucontrol->value.enumerated.item[0];
5273 if (mode >= num_chmodes)
5274 return -EINVAL;
5275 if (*max_channelsp == chmode[mode].channels)
5276 return 0;
5277 /* change the current channel setting */
5278 *max_channelsp = chmode[mode].channels;
5279 if (chmode[mode].sequence)
5280 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5281 return 1;
5282 }
5283 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
5284
5285 /*
5286 * input MUX helper
5287 */
5288
5289 /**
5290 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5291 * @imux: imux helper object
5292 * @uinfo: pointer to get/store the data
5293 */
5294 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5295 struct snd_ctl_elem_info *uinfo)
5296 {
5297 unsigned int index;
5298
5299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5300 uinfo->count = 1;
5301 uinfo->value.enumerated.items = imux->num_items;
5302 if (!imux->num_items)
5303 return 0;
5304 index = uinfo->value.enumerated.item;
5305 if (index >= imux->num_items)
5306 index = imux->num_items - 1;
5307 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5308 return 0;
5309 }
5310 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
5311
5312 /**
5313 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5314 * @codec: the HDA codec
5315 * @imux: imux helper object
5316 * @ucontrol: pointer to get/store the data
5317 * @nid: input mux NID
5318 * @cur_val: pointer to get/store the current imux value
5319 */
5320 int snd_hda_input_mux_put(struct hda_codec *codec,
5321 const struct hda_input_mux *imux,
5322 struct snd_ctl_elem_value *ucontrol,
5323 hda_nid_t nid,
5324 unsigned int *cur_val)
5325 {
5326 unsigned int idx;
5327
5328 if (!imux->num_items)
5329 return 0;
5330 idx = ucontrol->value.enumerated.item[0];
5331 if (idx >= imux->num_items)
5332 idx = imux->num_items - 1;
5333 if (*cur_val == idx)
5334 return 0;
5335 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5336 imux->items[idx].index);
5337 *cur_val = idx;
5338 return 1;
5339 }
5340 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5341
5342
5343 /**
5344 * snd_hda_enum_helper_info - Helper for simple enum ctls
5345 * @kcontrol: ctl element
5346 * @uinfo: pointer to get/store the data
5347 * @num_items: number of enum items
5348 * @texts: enum item string array
5349 *
5350 * process kcontrol info callback of a simple string enum array
5351 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5352 */
5353 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5354 struct snd_ctl_elem_info *uinfo,
5355 int num_items, const char * const *texts)
5356 {
5357 static const char * const texts_default[] = {
5358 "Disabled", "Enabled"
5359 };
5360
5361 if (!texts || !num_items) {
5362 num_items = 2;
5363 texts = texts_default;
5364 }
5365
5366 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
5367 }
5368 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5369
5370 /*
5371 * Multi-channel / digital-out PCM helper functions
5372 */
5373
5374 /* setup SPDIF output stream */
5375 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5376 unsigned int stream_tag, unsigned int format)
5377 {
5378 struct hda_spdif_out *spdif;
5379 unsigned int curr_fmt;
5380 bool reset;
5381
5382 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5383 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5384 AC_VERB_GET_STREAM_FORMAT, 0);
5385 reset = codec->spdif_status_reset &&
5386 (spdif->ctls & AC_DIG1_ENABLE) &&
5387 curr_fmt != format;
5388
5389 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5390 updated */
5391 if (reset)
5392 set_dig_out_convert(codec, nid,
5393 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5394 -1);
5395 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5396 if (codec->slave_dig_outs) {
5397 const hda_nid_t *d;
5398 for (d = codec->slave_dig_outs; *d; d++)
5399 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5400 format);
5401 }
5402 /* turn on again (if needed) */
5403 if (reset)
5404 set_dig_out_convert(codec, nid,
5405 spdif->ctls & 0xff, -1);
5406 }
5407
5408 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5409 {
5410 snd_hda_codec_cleanup_stream(codec, nid);
5411 if (codec->slave_dig_outs) {
5412 const hda_nid_t *d;
5413 for (d = codec->slave_dig_outs; *d; d++)
5414 snd_hda_codec_cleanup_stream(codec, *d);
5415 }
5416 }
5417
5418 /**
5419 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5420 * @bus: HD-audio bus
5421 */
5422 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5423 {
5424 struct hda_codec *codec;
5425
5426 if (!bus)
5427 return;
5428 list_for_each_entry(codec, &bus->codec_list, list) {
5429 if (hda_codec_is_power_on(codec) &&
5430 codec->patch_ops.reboot_notify)
5431 codec->patch_ops.reboot_notify(codec);
5432 }
5433 }
5434 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5435
5436 /**
5437 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5438 * @codec: the HDA codec
5439 * @mout: hda_multi_out object
5440 */
5441 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5442 struct hda_multi_out *mout)
5443 {
5444 mutex_lock(&codec->spdif_mutex);
5445 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5446 /* already opened as analog dup; reset it once */
5447 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5448 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5449 mutex_unlock(&codec->spdif_mutex);
5450 return 0;
5451 }
5452 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5453
5454 /**
5455 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5456 * @codec: the HDA codec
5457 * @mout: hda_multi_out object
5458 * @stream_tag: stream tag to assign
5459 * @format: format id to assign
5460 * @substream: PCM substream to assign
5461 */
5462 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5463 struct hda_multi_out *mout,
5464 unsigned int stream_tag,
5465 unsigned int format,
5466 struct snd_pcm_substream *substream)
5467 {
5468 mutex_lock(&codec->spdif_mutex);
5469 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5470 mutex_unlock(&codec->spdif_mutex);
5471 return 0;
5472 }
5473 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5474
5475 /**
5476 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5477 * @codec: the HDA codec
5478 * @mout: hda_multi_out object
5479 */
5480 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5481 struct hda_multi_out *mout)
5482 {
5483 mutex_lock(&codec->spdif_mutex);
5484 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5485 mutex_unlock(&codec->spdif_mutex);
5486 return 0;
5487 }
5488 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5489
5490 /**
5491 * snd_hda_multi_out_dig_close - release the digital out stream
5492 * @codec: the HDA codec
5493 * @mout: hda_multi_out object
5494 */
5495 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5496 struct hda_multi_out *mout)
5497 {
5498 mutex_lock(&codec->spdif_mutex);
5499 mout->dig_out_used = 0;
5500 mutex_unlock(&codec->spdif_mutex);
5501 return 0;
5502 }
5503 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5504
5505 /**
5506 * snd_hda_multi_out_analog_open - open analog outputs
5507 * @codec: the HDA codec
5508 * @mout: hda_multi_out object
5509 * @substream: PCM substream to assign
5510 * @hinfo: PCM information to assign
5511 *
5512 * Open analog outputs and set up the hw-constraints.
5513 * If the digital outputs can be opened as slave, open the digital
5514 * outputs, too.
5515 */
5516 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5517 struct hda_multi_out *mout,
5518 struct snd_pcm_substream *substream,
5519 struct hda_pcm_stream *hinfo)
5520 {
5521 struct snd_pcm_runtime *runtime = substream->runtime;
5522 runtime->hw.channels_max = mout->max_channels;
5523 if (mout->dig_out_nid) {
5524 if (!mout->analog_rates) {
5525 mout->analog_rates = hinfo->rates;
5526 mout->analog_formats = hinfo->formats;
5527 mout->analog_maxbps = hinfo->maxbps;
5528 } else {
5529 runtime->hw.rates = mout->analog_rates;
5530 runtime->hw.formats = mout->analog_formats;
5531 hinfo->maxbps = mout->analog_maxbps;
5532 }
5533 if (!mout->spdif_rates) {
5534 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5535 &mout->spdif_rates,
5536 &mout->spdif_formats,
5537 &mout->spdif_maxbps);
5538 }
5539 mutex_lock(&codec->spdif_mutex);
5540 if (mout->share_spdif) {
5541 if ((runtime->hw.rates & mout->spdif_rates) &&
5542 (runtime->hw.formats & mout->spdif_formats)) {
5543 runtime->hw.rates &= mout->spdif_rates;
5544 runtime->hw.formats &= mout->spdif_formats;
5545 if (mout->spdif_maxbps < hinfo->maxbps)
5546 hinfo->maxbps = mout->spdif_maxbps;
5547 } else {
5548 mout->share_spdif = 0;
5549 /* FIXME: need notify? */
5550 }
5551 }
5552 mutex_unlock(&codec->spdif_mutex);
5553 }
5554 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5555 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5556 }
5557 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5558
5559 /**
5560 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5561 * @codec: the HDA codec
5562 * @mout: hda_multi_out object
5563 * @stream_tag: stream tag to assign
5564 * @format: format id to assign
5565 * @substream: PCM substream to assign
5566 *
5567 * Set up the i/o for analog out.
5568 * When the digital out is available, copy the front out to digital out, too.
5569 */
5570 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5571 struct hda_multi_out *mout,
5572 unsigned int stream_tag,
5573 unsigned int format,
5574 struct snd_pcm_substream *substream)
5575 {
5576 const hda_nid_t *nids = mout->dac_nids;
5577 int chs = substream->runtime->channels;
5578 struct hda_spdif_out *spdif;
5579 int i;
5580
5581 mutex_lock(&codec->spdif_mutex);
5582 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5583 if (mout->dig_out_nid && mout->share_spdif &&
5584 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5585 if (chs == 2 &&
5586 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5587 format) &&
5588 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5589 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5590 setup_dig_out_stream(codec, mout->dig_out_nid,
5591 stream_tag, format);
5592 } else {
5593 mout->dig_out_used = 0;
5594 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5595 }
5596 }
5597 mutex_unlock(&codec->spdif_mutex);
5598
5599 /* front */
5600 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5601 0, format);
5602 if (!mout->no_share_stream &&
5603 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5604 /* headphone out will just decode front left/right (stereo) */
5605 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5606 0, format);
5607 /* extra outputs copied from front */
5608 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5609 if (!mout->no_share_stream && mout->hp_out_nid[i])
5610 snd_hda_codec_setup_stream(codec,
5611 mout->hp_out_nid[i],
5612 stream_tag, 0, format);
5613
5614 /* surrounds */
5615 for (i = 1; i < mout->num_dacs; i++) {
5616 if (chs >= (i + 1) * 2) /* independent out */
5617 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5618 i * 2, format);
5619 else if (!mout->no_share_stream) /* copy front */
5620 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5621 0, format);
5622 }
5623
5624 /* extra surrounds */
5625 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5626 int ch = 0;
5627 if (!mout->extra_out_nid[i])
5628 break;
5629 if (chs >= (i + 1) * 2)
5630 ch = i * 2;
5631 else if (!mout->no_share_stream)
5632 break;
5633 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5634 stream_tag, ch, format);
5635 }
5636
5637 return 0;
5638 }
5639 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5640
5641 /**
5642 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5643 * @codec: the HDA codec
5644 * @mout: hda_multi_out object
5645 */
5646 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5647 struct hda_multi_out *mout)
5648 {
5649 const hda_nid_t *nids = mout->dac_nids;
5650 int i;
5651
5652 for (i = 0; i < mout->num_dacs; i++)
5653 snd_hda_codec_cleanup_stream(codec, nids[i]);
5654 if (mout->hp_nid)
5655 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5656 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5657 if (mout->hp_out_nid[i])
5658 snd_hda_codec_cleanup_stream(codec,
5659 mout->hp_out_nid[i]);
5660 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5661 if (mout->extra_out_nid[i])
5662 snd_hda_codec_cleanup_stream(codec,
5663 mout->extra_out_nid[i]);
5664 mutex_lock(&codec->spdif_mutex);
5665 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5666 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5667 mout->dig_out_used = 0;
5668 }
5669 mutex_unlock(&codec->spdif_mutex);
5670 return 0;
5671 }
5672 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5673
5674 /**
5675 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5676 * @codec: the HDA codec
5677 * @pin: referred pin NID
5678 *
5679 * Guess the suitable VREF pin bits to be set as the pin-control value.
5680 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5681 */
5682 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5683 {
5684 unsigned int pincap;
5685 unsigned int oldval;
5686 oldval = snd_hda_codec_read(codec, pin, 0,
5687 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5688 pincap = snd_hda_query_pin_caps(codec, pin);
5689 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5690 /* Exception: if the default pin setup is vref50, we give it priority */
5691 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5692 return AC_PINCTL_VREF_80;
5693 else if (pincap & AC_PINCAP_VREF_50)
5694 return AC_PINCTL_VREF_50;
5695 else if (pincap & AC_PINCAP_VREF_100)
5696 return AC_PINCTL_VREF_100;
5697 else if (pincap & AC_PINCAP_VREF_GRD)
5698 return AC_PINCTL_VREF_GRD;
5699 return AC_PINCTL_VREF_HIZ;
5700 }
5701 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5702
5703 /**
5704 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5705 * @codec: the HDA codec
5706 * @pin: referred pin NID
5707 * @val: pin ctl value to audit
5708 */
5709 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5710 hda_nid_t pin, unsigned int val)
5711 {
5712 static unsigned int cap_lists[][2] = {
5713 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5714 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5715 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5716 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5717 };
5718 unsigned int cap;
5719
5720 if (!val)
5721 return 0;
5722 cap = snd_hda_query_pin_caps(codec, pin);
5723 if (!cap)
5724 return val; /* don't know what to do... */
5725
5726 if (val & AC_PINCTL_OUT_EN) {
5727 if (!(cap & AC_PINCAP_OUT))
5728 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5729 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5730 val &= ~AC_PINCTL_HP_EN;
5731 }
5732
5733 if (val & AC_PINCTL_IN_EN) {
5734 if (!(cap & AC_PINCAP_IN))
5735 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5736 else {
5737 unsigned int vcap, vref;
5738 int i;
5739 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5740 vref = val & AC_PINCTL_VREFEN;
5741 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5742 if (vref == cap_lists[i][0] &&
5743 !(vcap & cap_lists[i][1])) {
5744 if (i == ARRAY_SIZE(cap_lists) - 1)
5745 vref = AC_PINCTL_VREF_HIZ;
5746 else
5747 vref = cap_lists[i + 1][0];
5748 }
5749 }
5750 val &= ~AC_PINCTL_VREFEN;
5751 val |= vref;
5752 }
5753 }
5754
5755 return val;
5756 }
5757 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5758
5759 /**
5760 * _snd_hda_pin_ctl - Helper to set pin ctl value
5761 * @codec: the HDA codec
5762 * @pin: referred pin NID
5763 * @val: pin control value to set
5764 * @cached: access over codec pinctl cache or direct write
5765 *
5766 * This function is a helper to set a pin ctl value more safely.
5767 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5768 * value in pin target array via snd_hda_codec_set_pin_target(), then
5769 * actually writes the value via either snd_hda_codec_update_cache() or
5770 * snd_hda_codec_write() depending on @cached flag.
5771 */
5772 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5773 unsigned int val, bool cached)
5774 {
5775 val = snd_hda_correct_pin_ctl(codec, pin, val);
5776 snd_hda_codec_set_pin_target(codec, pin, val);
5777 if (cached)
5778 return snd_hda_codec_update_cache(codec, pin, 0,
5779 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5780 else
5781 return snd_hda_codec_write(codec, pin, 0,
5782 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5783 }
5784 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5785
5786 /**
5787 * snd_hda_add_imux_item - Add an item to input_mux
5788 * @codec: the HDA codec
5789 * @imux: imux helper object
5790 * @label: the name of imux item to assign
5791 * @index: index number of imux item to assign
5792 * @type_idx: pointer to store the resultant label index
5793 *
5794 * When the same label is used already in the existing items, the number
5795 * suffix is appended to the label. This label index number is stored
5796 * to type_idx when non-NULL pointer is given.
5797 */
5798 int snd_hda_add_imux_item(struct hda_codec *codec,
5799 struct hda_input_mux *imux, const char *label,
5800 int index, int *type_idx)
5801 {
5802 int i, label_idx = 0;
5803 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5804 codec_err(codec, "hda_codec: Too many imux items!\n");
5805 return -EINVAL;
5806 }
5807 for (i = 0; i < imux->num_items; i++) {
5808 if (!strncmp(label, imux->items[i].label, strlen(label)))
5809 label_idx++;
5810 }
5811 if (type_idx)
5812 *type_idx = label_idx;
5813 if (label_idx > 0)
5814 snprintf(imux->items[imux->num_items].label,
5815 sizeof(imux->items[imux->num_items].label),
5816 "%s %d", label, label_idx);
5817 else
5818 strlcpy(imux->items[imux->num_items].label, label,
5819 sizeof(imux->items[imux->num_items].label));
5820 imux->items[imux->num_items].index = index;
5821 imux->num_items++;
5822 return 0;
5823 }
5824 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5825
5826
5827 #ifdef CONFIG_PM
5828 /*
5829 * power management
5830 */
5831
5832
5833 static void hda_async_suspend(void *data, async_cookie_t cookie)
5834 {
5835 hda_call_codec_suspend(data, false);
5836 }
5837
5838 static void hda_async_resume(void *data, async_cookie_t cookie)
5839 {
5840 hda_call_codec_resume(data);
5841 }
5842
5843 /**
5844 * snd_hda_suspend - suspend the codecs
5845 * @bus: the HDA bus
5846 *
5847 * Returns 0 if successful.
5848 */
5849 int snd_hda_suspend(struct hda_bus *bus)
5850 {
5851 struct hda_codec *codec;
5852 ASYNC_DOMAIN_EXCLUSIVE(domain);
5853
5854 list_for_each_entry(codec, &bus->codec_list, list) {
5855 cancel_delayed_work_sync(&codec->jackpoll_work);
5856 if (hda_codec_is_power_on(codec)) {
5857 if (bus->num_codecs > 1)
5858 async_schedule_domain(hda_async_suspend, codec,
5859 &domain);
5860 else
5861 hda_call_codec_suspend(codec, false);
5862 }
5863 }
5864
5865 if (bus->num_codecs > 1)
5866 async_synchronize_full_domain(&domain);
5867
5868 return 0;
5869 }
5870 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5871
5872 /**
5873 * snd_hda_resume - resume the codecs
5874 * @bus: the HDA bus
5875 *
5876 * Returns 0 if successful.
5877 */
5878 int snd_hda_resume(struct hda_bus *bus)
5879 {
5880 struct hda_codec *codec;
5881 ASYNC_DOMAIN_EXCLUSIVE(domain);
5882
5883 list_for_each_entry(codec, &bus->codec_list, list) {
5884 if (bus->num_codecs > 1)
5885 async_schedule_domain(hda_async_resume, codec, &domain);
5886 else
5887 hda_call_codec_resume(codec);
5888 }
5889
5890 if (bus->num_codecs > 1)
5891 async_synchronize_full_domain(&domain);
5892
5893 return 0;
5894 }
5895 EXPORT_SYMBOL_GPL(snd_hda_resume);
5896 #endif /* CONFIG_PM */
5897
5898 /*
5899 * generic arrays
5900 */
5901
5902 /**
5903 * snd_array_new - get a new element from the given array
5904 * @array: the array object
5905 *
5906 * Get a new element from the given array. If it exceeds the
5907 * pre-allocated array size, re-allocate the array.
5908 *
5909 * Returns NULL if allocation failed.
5910 */
5911 void *snd_array_new(struct snd_array *array)
5912 {
5913 if (snd_BUG_ON(!array->elem_size))
5914 return NULL;
5915 if (array->used >= array->alloced) {
5916 int num = array->alloced + array->alloc_align;
5917 int size = (num + 1) * array->elem_size;
5918 void *nlist;
5919 if (snd_BUG_ON(num >= 4096))
5920 return NULL;
5921 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5922 if (!nlist)
5923 return NULL;
5924 array->list = nlist;
5925 array->alloced = num;
5926 }
5927 return snd_array_elem(array, array->used++);
5928 }
5929 EXPORT_SYMBOL_GPL(snd_array_new);
5930
5931 /**
5932 * snd_array_free - free the given array elements
5933 * @array: the array object
5934 */
5935 void snd_array_free(struct snd_array *array)
5936 {
5937 kfree(array->list);
5938 array->used = 0;
5939 array->alloced = 0;
5940 array->list = NULL;
5941 }
5942 EXPORT_SYMBOL_GPL(snd_array_free);
5943
5944 /**
5945 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5946 * @pcm: PCM caps bits
5947 * @buf: the string buffer to write
5948 * @buflen: the max buffer length
5949 *
5950 * used by hda_proc.c and hda_eld.c
5951 */
5952 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5953 {
5954 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5955 int i, j;
5956
5957 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5958 if (pcm & (AC_SUPPCM_BITS_8 << i))
5959 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5960
5961 buf[j] = '\0'; /* necessary when j == 0 */
5962 }
5963 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5964
5965 MODULE_DESCRIPTION("HDA codec core");
5966 MODULE_LICENSE("GPL");
This page took 0.141309 seconds and 4 git commands to generate.