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