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