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