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