ALSA: hda: slave_dig_outs code block in wrong location
[deliverable/linux.git] / sound / pci / hda / hda_codec.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
62932df8 26#include <linux/mutex.h>
1da177e4
LT
27#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
302e9c5a 30#include <sound/tlv.h>
1da177e4
LT
31#include <sound/initval.h>
32#include "hda_local.h"
2807314d 33#include <sound/hda_hwdep.h>
3c9a3203 34#include "hda_patch.h" /* codec presets */
1da177e4 35
cb53c626
TI
36#ifdef CONFIG_SND_HDA_POWER_SAVE
37/* define this option here to hide as static */
7a5a27cf 38static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
cb53c626
TI
39module_param(power_save, int, 0644);
40MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41 "(in second, 0 = disable).");
42#endif
1da177e4 43
1da177e4
LT
44/*
45 * vendor / preset table
46 */
47
48struct hda_vendor_id {
49 unsigned int id;
50 const char *name;
51};
52
53/* codec vendor labels */
54static struct hda_vendor_id hda_vendor_ids[] = {
c8cd1281 55 { 0x1002, "ATI" },
a9226251 56 { 0x1057, "Motorola" },
c8cd1281
TI
57 { 0x1095, "Silicon Image" },
58 { 0x10ec, "Realtek" },
c577b8a1 59 { 0x1106, "VIA" },
7f16859a 60 { 0x111d, "IDT" },
c8cd1281 61 { 0x11c1, "LSI" },
54b903ec 62 { 0x11d4, "Analog Devices" },
1da177e4 63 { 0x13f6, "C-Media" },
a9226251 64 { 0x14f1, "Conexant" },
c8cd1281
TI
65 { 0x17e8, "Chrontel" },
66 { 0x1854, "LG" },
1da177e4 67 { 0x434d, "C-Media" },
2f2f4251 68 { 0x8384, "SigmaTel" },
1da177e4
LT
69 {} /* terminator */
70};
71
3c9a3203
HH
72static const struct hda_codec_preset *hda_preset_tables[] = {
73#ifdef CONFIG_SND_HDA_CODEC_REALTEK
74 snd_hda_preset_realtek,
75#endif
76#ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77 snd_hda_preset_cmedia,
78#endif
79#ifdef CONFIG_SND_HDA_CODEC_ANALOG
80 snd_hda_preset_analog,
81#endif
82#ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83 snd_hda_preset_sigmatel,
84#endif
85#ifdef CONFIG_SND_HDA_CODEC_SI3054
86 snd_hda_preset_si3054,
87#endif
88#ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89 snd_hda_preset_atihdmi,
90#endif
91#ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92 snd_hda_preset_conexant,
93#endif
94#ifdef CONFIG_SND_HDA_CODEC_VIA
95 snd_hda_preset_via,
96#endif
97 NULL
98};
1da177e4 99
cb53c626
TI
100#ifdef CONFIG_SND_HDA_POWER_SAVE
101static void hda_power_work(struct work_struct *work);
102static void hda_keep_power_on(struct hda_codec *codec);
103#else
104static inline void hda_keep_power_on(struct hda_codec *codec) {}
105#endif
106
1da177e4
LT
107/**
108 * snd_hda_codec_read - send a command and get the response
109 * @codec: the HDA codec
110 * @nid: NID to send the command
111 * @direct: direct flag
112 * @verb: the verb to send
113 * @parm: the parameter for the verb
114 *
115 * Send a single command and read the corresponding response.
116 *
117 * Returns the obtained response value, or -1 for an error.
118 */
0ba21762
TI
119unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
120 int direct,
1da177e4
LT
121 unsigned int verb, unsigned int parm)
122{
123 unsigned int res;
cb53c626 124 snd_hda_power_up(codec);
62932df8 125 mutex_lock(&codec->bus->cmd_mutex);
0ba21762 126 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
1da177e4
LT
127 res = codec->bus->ops.get_response(codec);
128 else
129 res = (unsigned int)-1;
62932df8 130 mutex_unlock(&codec->bus->cmd_mutex);
cb53c626 131 snd_hda_power_down(codec);
1da177e4
LT
132 return res;
133}
134
135/**
136 * snd_hda_codec_write - send a single command without waiting for response
137 * @codec: the HDA codec
138 * @nid: NID to send the command
139 * @direct: direct flag
140 * @verb: the verb to send
141 * @parm: the parameter for the verb
142 *
143 * Send a single command without waiting for response.
144 *
145 * Returns 0 if successful, or a negative error code.
146 */
147int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
149{
150 int err;
cb53c626 151 snd_hda_power_up(codec);
62932df8 152 mutex_lock(&codec->bus->cmd_mutex);
1da177e4 153 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
62932df8 154 mutex_unlock(&codec->bus->cmd_mutex);
cb53c626 155 snd_hda_power_down(codec);
1da177e4
LT
156 return err;
157}
158
159/**
160 * snd_hda_sequence_write - sequence writes
161 * @codec: the HDA codec
162 * @seq: VERB array to send
163 *
164 * Send the commands sequentially from the given array.
165 * The array must be terminated with NID=0.
166 */
167void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
168{
169 for (; seq->nid; seq++)
170 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
171}
172
173/**
174 * snd_hda_get_sub_nodes - get the range of sub nodes
175 * @codec: the HDA codec
176 * @nid: NID to parse
177 * @start_id: the pointer to store the start NID
178 *
179 * Parse the NID and store the start NID of its sub-nodes.
180 * Returns the number of sub-nodes.
181 */
0ba21762
TI
182int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
183 hda_nid_t *start_id)
1da177e4
LT
184{
185 unsigned int parm;
186
187 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
e8a7f136
DT
188 if (parm == -1)
189 return 0;
1da177e4
LT
190 *start_id = (parm >> 16) & 0x7fff;
191 return (int)(parm & 0x7fff);
192}
193
194/**
195 * snd_hda_get_connections - get connection list
196 * @codec: the HDA codec
197 * @nid: NID to parse
198 * @conn_list: connection list array
199 * @max_conns: max. number of connections to store
200 *
201 * Parses the connection list of the given widget and stores the list
202 * of NIDs.
203 *
204 * Returns the number of connections, or a negative error code.
205 */
206int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
207 hda_nid_t *conn_list, int max_conns)
208{
209 unsigned int parm;
54d17403 210 int i, conn_len, conns;
1da177e4 211 unsigned int shift, num_elems, mask;
54d17403 212 hda_nid_t prev_nid;
1da177e4 213
da3cec35
TI
214 if (snd_BUG_ON(!conn_list || max_conns <= 0))
215 return -EINVAL;
1da177e4
LT
216
217 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
218 if (parm & AC_CLIST_LONG) {
219 /* long form */
220 shift = 16;
221 num_elems = 2;
222 } else {
223 /* short form */
224 shift = 8;
225 num_elems = 4;
226 }
227 conn_len = parm & AC_CLIST_LENGTH;
1da177e4
LT
228 mask = (1 << (shift-1)) - 1;
229
0ba21762 230 if (!conn_len)
1da177e4
LT
231 return 0; /* no connection */
232
233 if (conn_len == 1) {
234 /* single connection */
0ba21762
TI
235 parm = snd_hda_codec_read(codec, nid, 0,
236 AC_VERB_GET_CONNECT_LIST, 0);
1da177e4
LT
237 conn_list[0] = parm & mask;
238 return 1;
239 }
240
241 /* multi connection */
242 conns = 0;
54d17403
TI
243 prev_nid = 0;
244 for (i = 0; i < conn_len; i++) {
245 int range_val;
246 hda_nid_t val, n;
247
248 if (i % num_elems == 0)
249 parm = snd_hda_codec_read(codec, nid, 0,
250 AC_VERB_GET_CONNECT_LIST, i);
0ba21762 251 range_val = !!(parm & (1 << (shift-1))); /* ranges */
54d17403
TI
252 val = parm & mask;
253 parm >>= shift;
254 if (range_val) {
255 /* ranges between the previous and this one */
0ba21762
TI
256 if (!prev_nid || prev_nid >= val) {
257 snd_printk(KERN_WARNING "hda_codec: "
258 "invalid dep_range_val %x:%x\n",
259 prev_nid, val);
54d17403
TI
260 continue;
261 }
262 for (n = prev_nid + 1; n <= val; n++) {
263 if (conns >= max_conns) {
0ba21762
TI
264 snd_printk(KERN_ERR
265 "Too many connections\n");
1da177e4 266 return -EINVAL;
54d17403
TI
267 }
268 conn_list[conns++] = n;
1da177e4 269 }
54d17403
TI
270 } else {
271 if (conns >= max_conns) {
272 snd_printk(KERN_ERR "Too many connections\n");
273 return -EINVAL;
274 }
275 conn_list[conns++] = val;
1da177e4 276 }
54d17403 277 prev_nid = val;
1da177e4
LT
278 }
279 return conns;
280}
281
282
283/**
284 * snd_hda_queue_unsol_event - add an unsolicited event to queue
285 * @bus: the BUS
286 * @res: unsolicited event (lower 32bit of RIRB entry)
287 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
288 *
289 * Adds the given event to the queue. The events are processed in
290 * the workqueue asynchronously. Call this function in the interrupt
291 * hanlder when RIRB receives an unsolicited event.
292 *
293 * Returns 0 if successful, or a negative error code.
294 */
295int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
296{
297 struct hda_bus_unsolicited *unsol;
298 unsigned int wp;
299
0ba21762
TI
300 unsol = bus->unsol;
301 if (!unsol)
1da177e4
LT
302 return 0;
303
304 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
305 unsol->wp = wp;
306
307 wp <<= 1;
308 unsol->queue[wp] = res;
309 unsol->queue[wp + 1] = res_ex;
310
e250af29 311 schedule_work(&unsol->work);
1da177e4
LT
312
313 return 0;
314}
315
316/*
317 * process queueud unsolicited events
318 */
c4028958 319static void process_unsol_events(struct work_struct *work)
1da177e4 320{
c4028958
DH
321 struct hda_bus_unsolicited *unsol =
322 container_of(work, struct hda_bus_unsolicited, work);
323 struct hda_bus *bus = unsol->bus;
1da177e4
LT
324 struct hda_codec *codec;
325 unsigned int rp, caddr, res;
326
327 while (unsol->rp != unsol->wp) {
328 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
329 unsol->rp = rp;
330 rp <<= 1;
331 res = unsol->queue[rp];
332 caddr = unsol->queue[rp + 1];
0ba21762 333 if (!(caddr & (1 << 4))) /* no unsolicited event? */
1da177e4
LT
334 continue;
335 codec = bus->caddr_tbl[caddr & 0x0f];
336 if (codec && codec->patch_ops.unsol_event)
337 codec->patch_ops.unsol_event(codec, res);
338 }
339}
340
341/*
342 * initialize unsolicited queue
343 */
756e2b01 344static int __devinit init_unsol_queue(struct hda_bus *bus)
1da177e4
LT
345{
346 struct hda_bus_unsolicited *unsol;
347
9f146bb6
TI
348 if (bus->unsol) /* already initialized */
349 return 0;
350
e560d8d8 351 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
0ba21762
TI
352 if (!unsol) {
353 snd_printk(KERN_ERR "hda_codec: "
354 "can't allocate unsolicited queue\n");
1da177e4
LT
355 return -ENOMEM;
356 }
c4028958
DH
357 INIT_WORK(&unsol->work, process_unsol_events);
358 unsol->bus = bus;
1da177e4
LT
359 bus->unsol = unsol;
360 return 0;
361}
362
363/*
364 * destructor
365 */
366static void snd_hda_codec_free(struct hda_codec *codec);
367
368static int snd_hda_bus_free(struct hda_bus *bus)
369{
0ba21762 370 struct hda_codec *codec, *n;
1da177e4 371
0ba21762 372 if (!bus)
1da177e4
LT
373 return 0;
374 if (bus->unsol) {
e250af29 375 flush_scheduled_work();
1da177e4
LT
376 kfree(bus->unsol);
377 }
0ba21762 378 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
1da177e4
LT
379 snd_hda_codec_free(codec);
380 }
381 if (bus->ops.private_free)
382 bus->ops.private_free(bus);
383 kfree(bus);
384 return 0;
385}
386
c8b6bf9b 387static int snd_hda_bus_dev_free(struct snd_device *device)
1da177e4
LT
388{
389 struct hda_bus *bus = device->device_data;
390 return snd_hda_bus_free(bus);
391}
392
393/**
394 * snd_hda_bus_new - create a HDA bus
395 * @card: the card entry
396 * @temp: the template for hda_bus information
397 * @busp: the pointer to store the created bus instance
398 *
399 * Returns 0 if successful, or a negative error code.
400 */
756e2b01
TI
401int __devinit snd_hda_bus_new(struct snd_card *card,
402 const struct hda_bus_template *temp,
403 struct hda_bus **busp)
1da177e4
LT
404{
405 struct hda_bus *bus;
406 int err;
c8b6bf9b 407 static struct snd_device_ops dev_ops = {
1da177e4
LT
408 .dev_free = snd_hda_bus_dev_free,
409 };
410
da3cec35
TI
411 if (snd_BUG_ON(!temp))
412 return -EINVAL;
413 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
414 return -EINVAL;
1da177e4
LT
415
416 if (busp)
417 *busp = NULL;
418
e560d8d8 419 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
420 if (bus == NULL) {
421 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
422 return -ENOMEM;
423 }
424
425 bus->card = card;
426 bus->private_data = temp->private_data;
427 bus->pci = temp->pci;
428 bus->modelname = temp->modelname;
429 bus->ops = temp->ops;
430
62932df8 431 mutex_init(&bus->cmd_mutex);
1da177e4
LT
432 INIT_LIST_HEAD(&bus->codec_list);
433
0ba21762
TI
434 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
435 if (err < 0) {
1da177e4
LT
436 snd_hda_bus_free(bus);
437 return err;
438 }
439 if (busp)
440 *busp = bus;
441 return 0;
442}
443
82467611
TI
444#ifdef CONFIG_SND_HDA_GENERIC
445#define is_generic_config(codec) \
446 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
447#else
448#define is_generic_config(codec) 0
449#endif
450
1da177e4
LT
451/*
452 * find a matching codec preset
453 */
756e2b01
TI
454static const struct hda_codec_preset __devinit *
455find_codec_preset(struct hda_codec *codec)
1da177e4
LT
456{
457 const struct hda_codec_preset **tbl, *preset;
458
82467611 459 if (is_generic_config(codec))
d5ad630b
TI
460 return NULL; /* use the generic parser */
461
1da177e4
LT
462 for (tbl = hda_preset_tables; *tbl; tbl++) {
463 for (preset = *tbl; preset->id; preset++) {
464 u32 mask = preset->mask;
ca7cfae9
MB
465 if (preset->afg && preset->afg != codec->afg)
466 continue;
467 if (preset->mfg && preset->mfg != codec->mfg)
468 continue;
0ba21762 469 if (!mask)
1da177e4 470 mask = ~0;
9c7f852e 471 if (preset->id == (codec->vendor_id & mask) &&
0ba21762 472 (!preset->rev ||
9c7f852e 473 preset->rev == codec->revision_id))
1da177e4
LT
474 return preset;
475 }
476 }
477 return NULL;
478}
479
480/*
481 * snd_hda_get_codec_name - store the codec name
482 */
483void snd_hda_get_codec_name(struct hda_codec *codec,
484 char *name, int namelen)
485{
486 const struct hda_vendor_id *c;
487 const char *vendor = NULL;
488 u16 vendor_id = codec->vendor_id >> 16;
489 char tmp[16];
490
491 for (c = hda_vendor_ids; c->id; c++) {
492 if (c->id == vendor_id) {
493 vendor = c->name;
494 break;
495 }
496 }
0ba21762 497 if (!vendor) {
1da177e4
LT
498 sprintf(tmp, "Generic %04x", vendor_id);
499 vendor = tmp;
500 }
501 if (codec->preset && codec->preset->name)
502 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
503 else
0ba21762
TI
504 snprintf(name, namelen, "%s ID %x", vendor,
505 codec->vendor_id & 0xffff);
1da177e4
LT
506}
507
508/*
673b683a 509 * look for an AFG and MFG nodes
1da177e4 510 */
756e2b01 511static void __devinit setup_fg_nodes(struct hda_codec *codec)
1da177e4
LT
512{
513 int i, total_nodes;
514 hda_nid_t nid;
515
516 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
517 for (i = 0; i < total_nodes; i++, nid++) {
0ba21762
TI
518 unsigned int func;
519 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
520 switch (func & 0xff) {
673b683a
SK
521 case AC_GRP_AUDIO_FUNCTION:
522 codec->afg = nid;
523 break;
524 case AC_GRP_MODEM_FUNCTION:
525 codec->mfg = nid;
526 break;
527 default:
528 break;
529 }
1da177e4 530 }
1da177e4
LT
531}
532
54d17403
TI
533/*
534 * read widget caps for each widget and store in cache
535 */
536static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
537{
538 int i;
539 hda_nid_t nid;
540
541 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
542 &codec->start_nid);
543 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
0ba21762 544 if (!codec->wcaps)
54d17403
TI
545 return -ENOMEM;
546 nid = codec->start_nid;
547 for (i = 0; i < codec->num_nodes; i++, nid++)
548 codec->wcaps[i] = snd_hda_param_read(codec, nid,
549 AC_PAR_AUDIO_WIDGET_CAP);
550 return 0;
551}
552
553
01751f54
TI
554static void init_hda_cache(struct hda_cache_rec *cache,
555 unsigned int record_size);
1fcaee6e 556static void free_hda_cache(struct hda_cache_rec *cache);
01751f54 557
1da177e4
LT
558/*
559 * codec destructor
560 */
561static void snd_hda_codec_free(struct hda_codec *codec)
562{
0ba21762 563 if (!codec)
1da177e4 564 return;
cb53c626
TI
565#ifdef CONFIG_SND_HDA_POWER_SAVE
566 cancel_delayed_work(&codec->power_work);
2525fdc4 567 flush_scheduled_work();
cb53c626 568#endif
1da177e4
LT
569 list_del(&codec->list);
570 codec->bus->caddr_tbl[codec->addr] = NULL;
571 if (codec->patch_ops.free)
572 codec->patch_ops.free(codec);
01751f54 573 free_hda_cache(&codec->amp_cache);
b3ac5636 574 free_hda_cache(&codec->cmd_cache);
54d17403 575 kfree(codec->wcaps);
1da177e4
LT
576 kfree(codec);
577}
578
1da177e4
LT
579/**
580 * snd_hda_codec_new - create a HDA codec
581 * @bus: the bus to assign
582 * @codec_addr: the codec address
583 * @codecp: the pointer to store the generated codec
584 *
585 * Returns 0 if successful, or a negative error code.
586 */
756e2b01
TI
587int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
588 struct hda_codec **codecp)
1da177e4
LT
589{
590 struct hda_codec *codec;
ba443687 591 char component[31];
1da177e4
LT
592 int err;
593
da3cec35
TI
594 if (snd_BUG_ON(!bus))
595 return -EINVAL;
596 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
597 return -EINVAL;
1da177e4
LT
598
599 if (bus->caddr_tbl[codec_addr]) {
0ba21762
TI
600 snd_printk(KERN_ERR "hda_codec: "
601 "address 0x%x is already occupied\n", codec_addr);
1da177e4
LT
602 return -EBUSY;
603 }
604
e560d8d8 605 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1da177e4
LT
606 if (codec == NULL) {
607 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
608 return -ENOMEM;
609 }
610
611 codec->bus = bus;
612 codec->addr = codec_addr;
62932df8 613 mutex_init(&codec->spdif_mutex);
01751f54 614 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
b3ac5636 615 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1da177e4 616
cb53c626
TI
617#ifdef CONFIG_SND_HDA_POWER_SAVE
618 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
619 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
620 * the caller has to power down appropriatley after initialization
621 * phase.
622 */
623 hda_keep_power_on(codec);
624#endif
625
1da177e4
LT
626 list_add_tail(&codec->list, &bus->codec_list);
627 bus->caddr_tbl[codec_addr] = codec;
628
0ba21762
TI
629 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
630 AC_PAR_VENDOR_ID);
111d3af5
TI
631 if (codec->vendor_id == -1)
632 /* read again, hopefully the access method was corrected
633 * in the last read...
634 */
635 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
636 AC_PAR_VENDOR_ID);
0ba21762
TI
637 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
638 AC_PAR_SUBSYSTEM_ID);
639 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
640 AC_PAR_REV_ID);
1da177e4 641
673b683a 642 setup_fg_nodes(codec);
0ba21762 643 if (!codec->afg && !codec->mfg) {
673b683a 644 snd_printdd("hda_codec: no AFG or MFG node found\n");
1da177e4
LT
645 snd_hda_codec_free(codec);
646 return -ENODEV;
647 }
648
54d17403
TI
649 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
650 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
651 snd_hda_codec_free(codec);
652 return -ENOMEM;
653 }
654
0ba21762 655 if (!codec->subsystem_id) {
86284e45 656 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
0ba21762
TI
657 codec->subsystem_id =
658 snd_hda_codec_read(codec, nid, 0,
659 AC_VERB_GET_SUBSYSTEM_ID, 0);
86284e45
TI
660 }
661
d5ad630b 662 codec->preset = find_codec_preset(codec);
43ea1d47
TI
663 /* audio codec should override the mixer name */
664 if (codec->afg || !*bus->card->mixername)
1da177e4
LT
665 snd_hda_get_codec_name(codec, bus->card->mixername,
666 sizeof(bus->card->mixername));
667
82467611 668 if (is_generic_config(codec)) {
1da177e4 669 err = snd_hda_parse_generic_codec(codec);
82467611
TI
670 goto patched;
671 }
82467611
TI
672 if (codec->preset && codec->preset->patch) {
673 err = codec->preset->patch(codec);
674 goto patched;
675 }
676
677 /* call the default parser */
82467611 678 err = snd_hda_parse_generic_codec(codec);
35a1e0cc
TI
679 if (err < 0)
680 printk(KERN_ERR "hda-codec: No codec parser is available\n");
82467611
TI
681
682 patched:
1da177e4
LT
683 if (err < 0) {
684 snd_hda_codec_free(codec);
685 return err;
686 }
687
9f146bb6
TI
688 if (codec->patch_ops.unsol_event)
689 init_unsol_queue(bus);
690
1da177e4 691 snd_hda_codec_proc_new(codec);
2807314d
TI
692#ifdef CONFIG_SND_HDA_HWDEP
693 snd_hda_create_hwdep(codec);
694#endif
1da177e4 695
ba443687 696 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
1da177e4
LT
697 snd_component_add(codec->bus->card, component);
698
699 if (codecp)
700 *codecp = codec;
701 return 0;
702}
703
704/**
705 * snd_hda_codec_setup_stream - set up the codec for streaming
706 * @codec: the CODEC to set up
707 * @nid: the NID to set up
708 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
709 * @channel_id: channel id to pass, zero based.
710 * @format: stream format.
711 */
0ba21762
TI
712void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
713 u32 stream_tag,
1da177e4
LT
714 int channel_id, int format)
715{
0ba21762 716 if (!nid)
d21b37ea
TI
717 return;
718
0ba21762
TI
719 snd_printdd("hda_codec_setup_stream: "
720 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1da177e4
LT
721 nid, stream_tag, channel_id, format);
722 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
723 (stream_tag << 4) | channel_id);
724 msleep(1);
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
726}
727
888afa15
TI
728void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
729{
730 if (!nid)
731 return;
732
733 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
734 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
735#if 0 /* keep the format */
736 msleep(1);
737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
738#endif
739}
740
1da177e4
LT
741/*
742 * amp access functions
743 */
744
4a19faee
TI
745/* FIXME: more better hash key? */
746#define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1da177e4 747#define INFO_AMP_CAPS (1<<0)
4a19faee 748#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1da177e4
LT
749
750/* initialize the hash table */
01751f54
TI
751static void __devinit init_hda_cache(struct hda_cache_rec *cache,
752 unsigned int record_size)
753{
754 memset(cache, 0, sizeof(*cache));
755 memset(cache->hash, 0xff, sizeof(cache->hash));
756 cache->record_size = record_size;
757}
758
1fcaee6e 759static void free_hda_cache(struct hda_cache_rec *cache)
1da177e4 760{
01751f54 761 kfree(cache->buffer);
1da177e4
LT
762}
763
764/* query the hash. allocate an entry if not found. */
01751f54
TI
765static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
766 u32 key)
1da177e4 767{
01751f54
TI
768 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
769 u16 cur = cache->hash[idx];
770 struct hda_cache_head *info;
1da177e4
LT
771
772 while (cur != 0xffff) {
01751f54
TI
773 info = (struct hda_cache_head *)(cache->buffer +
774 cur * cache->record_size);
1da177e4
LT
775 if (info->key == key)
776 return info;
777 cur = info->next;
778 }
779
780 /* add a new hash entry */
01751f54 781 if (cache->num_entries >= cache->size) {
d031166f 782 /* reallocate the array */
01751f54
TI
783 unsigned int new_size = cache->size + 64;
784 void *new_buffer;
785 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
786 if (!new_buffer) {
0ba21762
TI
787 snd_printk(KERN_ERR "hda_codec: "
788 "can't malloc amp_info\n");
d031166f
TI
789 return NULL;
790 }
01751f54
TI
791 if (cache->buffer) {
792 memcpy(new_buffer, cache->buffer,
793 cache->size * cache->record_size);
794 kfree(cache->buffer);
d031166f 795 }
01751f54
TI
796 cache->size = new_size;
797 cache->buffer = new_buffer;
1da177e4 798 }
01751f54
TI
799 cur = cache->num_entries++;
800 info = (struct hda_cache_head *)(cache->buffer +
801 cur * cache->record_size);
1da177e4 802 info->key = key;
01751f54
TI
803 info->val = 0;
804 info->next = cache->hash[idx];
805 cache->hash[idx] = cur;
1da177e4
LT
806
807 return info;
808}
809
01751f54
TI
810/* query and allocate an amp hash entry */
811static inline struct hda_amp_info *
812get_alloc_amp_hash(struct hda_codec *codec, u32 key)
813{
814 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
815}
816
1da177e4
LT
817/*
818 * query AMP capabilities for the given widget and direction
819 */
09a99959 820u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1da177e4 821{
0ba21762 822 struct hda_amp_info *info;
1da177e4 823
0ba21762
TI
824 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
825 if (!info)
1da177e4 826 return 0;
01751f54 827 if (!(info->head.val & INFO_AMP_CAPS)) {
0ba21762 828 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1da177e4 829 nid = codec->afg;
0ba21762
TI
830 info->amp_caps = snd_hda_param_read(codec, nid,
831 direction == HDA_OUTPUT ?
832 AC_PAR_AMP_OUT_CAP :
833 AC_PAR_AMP_IN_CAP);
b75e53f0 834 if (info->amp_caps)
01751f54 835 info->head.val |= INFO_AMP_CAPS;
1da177e4
LT
836 }
837 return info->amp_caps;
838}
839
897cc188
TI
840int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
841 unsigned int caps)
842{
843 struct hda_amp_info *info;
844
845 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
846 if (!info)
847 return -EINVAL;
848 info->amp_caps = caps;
01751f54 849 info->head.val |= INFO_AMP_CAPS;
897cc188
TI
850 return 0;
851}
852
1da177e4
LT
853/*
854 * read the current volume to info
4a19faee 855 * if the cache exists, read the cache value.
1da177e4 856 */
0ba21762
TI
857static unsigned int get_vol_mute(struct hda_codec *codec,
858 struct hda_amp_info *info, hda_nid_t nid,
859 int ch, int direction, int index)
1da177e4
LT
860{
861 u32 val, parm;
862
01751f54 863 if (info->head.val & INFO_AMP_VOL(ch))
4a19faee 864 return info->vol[ch];
1da177e4
LT
865
866 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
867 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
868 parm |= index;
0ba21762
TI
869 val = snd_hda_codec_read(codec, nid, 0,
870 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1da177e4 871 info->vol[ch] = val & 0xff;
01751f54 872 info->head.val |= INFO_AMP_VOL(ch);
4a19faee 873 return info->vol[ch];
1da177e4
LT
874}
875
876/*
4a19faee 877 * write the current volume in info to the h/w and update the cache
1da177e4 878 */
4a19faee 879static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
0ba21762
TI
880 hda_nid_t nid, int ch, int direction, int index,
881 int val)
1da177e4
LT
882{
883 u32 parm;
884
885 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
886 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
887 parm |= index << AC_AMP_SET_INDEX_SHIFT;
888 parm |= val;
889 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
4a19faee 890 info->vol[ch] = val;
1da177e4
LT
891}
892
893/*
4a19faee 894 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1da177e4 895 */
834be88d
TI
896int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
897 int direction, int index)
1da177e4 898{
0ba21762
TI
899 struct hda_amp_info *info;
900 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
901 if (!info)
1da177e4 902 return 0;
4a19faee 903 return get_vol_mute(codec, info, nid, ch, direction, index);
1da177e4
LT
904}
905
4a19faee
TI
906/*
907 * update the AMP value, mask = bit mask to set, val = the value
908 */
834be88d
TI
909int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
910 int direction, int idx, int mask, int val)
1da177e4 911{
0ba21762 912 struct hda_amp_info *info;
4a19faee 913
0ba21762
TI
914 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
915 if (!info)
1da177e4 916 return 0;
4a19faee
TI
917 val &= mask;
918 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
82beb8fd 919 if (info->vol[ch] == val)
1da177e4 920 return 0;
4a19faee 921 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1da177e4
LT
922 return 1;
923}
924
47fd830a
TI
925/*
926 * update the AMP stereo with the same mask and value
927 */
928int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
929 int direction, int idx, int mask, int val)
930{
931 int ch, ret = 0;
932 for (ch = 0; ch < 2; ch++)
933 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
934 idx, mask, val);
935 return ret;
936}
937
cb53c626 938#ifdef SND_HDA_NEEDS_RESUME
b3ac5636
TI
939/* resume the all amp commands from the cache */
940void snd_hda_codec_resume_amp(struct hda_codec *codec)
941{
942 struct hda_amp_info *buffer = codec->amp_cache.buffer;
943 int i;
944
945 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
946 u32 key = buffer->head.key;
947 hda_nid_t nid;
948 unsigned int idx, dir, ch;
949 if (!key)
950 continue;
951 nid = key & 0xff;
952 idx = (key >> 16) & 0xff;
953 dir = (key >> 24) & 0xff;
954 for (ch = 0; ch < 2; ch++) {
955 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
956 continue;
957 put_vol_mute(codec, buffer, nid, ch, dir, idx,
958 buffer->vol[ch]);
959 }
960 }
961}
cb53c626 962#endif /* SND_HDA_NEEDS_RESUME */
1da177e4 963
1da177e4 964/* volume */
0ba21762
TI
965int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
967{
968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
969 u16 nid = get_amp_nid(kcontrol);
970 u8 chs = get_amp_channels(kcontrol);
971 int dir = get_amp_direction(kcontrol);
972 u32 caps;
973
974 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
975 /* num steps */
976 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
977 if (!caps) {
978 printk(KERN_WARNING "hda_codec: "
9c8f2abd
TI
979 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
980 kcontrol->id.name);
1da177e4
LT
981 return -EINVAL;
982 }
983 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
984 uinfo->count = chs == 3 ? 2 : 1;
985 uinfo->value.integer.min = 0;
986 uinfo->value.integer.max = caps;
987 return 0;
988}
989
0ba21762
TI
990int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
991 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
992{
993 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
994 hda_nid_t nid = get_amp_nid(kcontrol);
995 int chs = get_amp_channels(kcontrol);
996 int dir = get_amp_direction(kcontrol);
997 int idx = get_amp_index(kcontrol);
998 long *valp = ucontrol->value.integer.value;
999
1000 if (chs & 1)
47fd830a
TI
1001 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1002 & HDA_AMP_VOLMASK;
1da177e4 1003 if (chs & 2)
47fd830a
TI
1004 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1005 & HDA_AMP_VOLMASK;
1da177e4
LT
1006 return 0;
1007}
1008
0ba21762
TI
1009int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1010 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1011{
1012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1013 hda_nid_t nid = get_amp_nid(kcontrol);
1014 int chs = get_amp_channels(kcontrol);
1015 int dir = get_amp_direction(kcontrol);
1016 int idx = get_amp_index(kcontrol);
1da177e4
LT
1017 long *valp = ucontrol->value.integer.value;
1018 int change = 0;
1019
cb53c626 1020 snd_hda_power_up(codec);
b9f5a89c 1021 if (chs & 1) {
4a19faee
TI
1022 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1023 0x7f, *valp);
b9f5a89c
NG
1024 valp++;
1025 }
4a19faee
TI
1026 if (chs & 2)
1027 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
b9f5a89c 1028 0x7f, *valp);
cb53c626 1029 snd_hda_power_down(codec);
1da177e4
LT
1030 return change;
1031}
1032
302e9c5a
JK
1033int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1034 unsigned int size, unsigned int __user *_tlv)
1035{
1036 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1037 hda_nid_t nid = get_amp_nid(kcontrol);
1038 int dir = get_amp_direction(kcontrol);
1039 u32 caps, val1, val2;
1040
1041 if (size < 4 * sizeof(unsigned int))
1042 return -ENOMEM;
1043 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
1044 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1045 val2 = (val2 + 1) * 25;
302e9c5a
JK
1046 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1047 val1 = ((int)val1) * ((int)val2);
302e9c5a
JK
1048 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1049 return -EFAULT;
1050 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1051 return -EFAULT;
1052 if (put_user(val1, _tlv + 2))
1053 return -EFAULT;
1054 if (put_user(val2, _tlv + 3))
1055 return -EFAULT;
1056 return 0;
1057}
1058
2134ea4f
TI
1059/*
1060 * set (static) TLV for virtual master volume; recalculated as max 0dB
1061 */
1062void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1063 unsigned int *tlv)
1064{
1065 u32 caps;
1066 int nums, step;
1067
1068 caps = query_amp_caps(codec, nid, dir);
1069 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1070 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1071 step = (step + 1) * 25;
1072 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1073 tlv[1] = 2 * sizeof(unsigned int);
1074 tlv[2] = -nums * step;
1075 tlv[3] = step;
1076}
1077
1078/* find a mixer control element with the given name */
09f99701
TI
1079static struct snd_kcontrol *
1080_snd_hda_find_mixer_ctl(struct hda_codec *codec,
1081 const char *name, int idx)
2134ea4f
TI
1082{
1083 struct snd_ctl_elem_id id;
1084 memset(&id, 0, sizeof(id));
1085 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
09f99701 1086 id.index = idx;
2134ea4f
TI
1087 strcpy(id.name, name);
1088 return snd_ctl_find_id(codec->bus->card, &id);
1089}
1090
09f99701
TI
1091struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1092 const char *name)
1093{
1094 return _snd_hda_find_mixer_ctl(codec, name, 0);
1095}
1096
2134ea4f
TI
1097/* create a virtual master control and add slaves */
1098int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1099 unsigned int *tlv, const char **slaves)
1100{
1101 struct snd_kcontrol *kctl;
1102 const char **s;
1103 int err;
1104
2f085549
TI
1105 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1106 ;
1107 if (!*s) {
1108 snd_printdd("No slave found for %s\n", name);
1109 return 0;
1110 }
2134ea4f
TI
1111 kctl = snd_ctl_make_virtual_master(name, tlv);
1112 if (!kctl)
1113 return -ENOMEM;
1114 err = snd_ctl_add(codec->bus->card, kctl);
1115 if (err < 0)
1116 return err;
1117
1118 for (s = slaves; *s; s++) {
1119 struct snd_kcontrol *sctl;
1120
1121 sctl = snd_hda_find_mixer_ctl(codec, *s);
1122 if (!sctl) {
1123 snd_printdd("Cannot find slave %s, skipped\n", *s);
1124 continue;
1125 }
1126 err = snd_ctl_add_slave(kctl, sctl);
1127 if (err < 0)
1128 return err;
1129 }
1130 return 0;
1131}
1132
1da177e4 1133/* switch */
0ba21762
TI
1134int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1135 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1136{
1137 int chs = get_amp_channels(kcontrol);
1138
1139 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1140 uinfo->count = chs == 3 ? 2 : 1;
1141 uinfo->value.integer.min = 0;
1142 uinfo->value.integer.max = 1;
1143 return 0;
1144}
1145
0ba21762
TI
1146int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1147 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1148{
1149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1150 hda_nid_t nid = get_amp_nid(kcontrol);
1151 int chs = get_amp_channels(kcontrol);
1152 int dir = get_amp_direction(kcontrol);
1153 int idx = get_amp_index(kcontrol);
1154 long *valp = ucontrol->value.integer.value;
1155
1156 if (chs & 1)
0ba21762 1157 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
47fd830a 1158 HDA_AMP_MUTE) ? 0 : 1;
1da177e4 1159 if (chs & 2)
0ba21762 1160 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
47fd830a 1161 HDA_AMP_MUTE) ? 0 : 1;
1da177e4
LT
1162 return 0;
1163}
1164
0ba21762
TI
1165int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1166 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1167{
1168 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1169 hda_nid_t nid = get_amp_nid(kcontrol);
1170 int chs = get_amp_channels(kcontrol);
1171 int dir = get_amp_direction(kcontrol);
1172 int idx = get_amp_index(kcontrol);
1da177e4
LT
1173 long *valp = ucontrol->value.integer.value;
1174 int change = 0;
1175
cb53c626 1176 snd_hda_power_up(codec);
b9f5a89c 1177 if (chs & 1) {
4a19faee 1178 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
47fd830a
TI
1179 HDA_AMP_MUTE,
1180 *valp ? 0 : HDA_AMP_MUTE);
b9f5a89c
NG
1181 valp++;
1182 }
4a19faee
TI
1183 if (chs & 2)
1184 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
47fd830a
TI
1185 HDA_AMP_MUTE,
1186 *valp ? 0 : HDA_AMP_MUTE);
cb53c626
TI
1187#ifdef CONFIG_SND_HDA_POWER_SAVE
1188 if (codec->patch_ops.check_power_status)
1189 codec->patch_ops.check_power_status(codec, nid);
1190#endif
1191 snd_hda_power_down(codec);
1da177e4
LT
1192 return change;
1193}
1194
985be54b
TI
1195/*
1196 * bound volume controls
1197 *
1198 * bind multiple volumes (# indices, from 0)
1199 */
1200
1201#define AMP_VAL_IDX_SHIFT 19
1202#define AMP_VAL_IDX_MASK (0x0f<<19)
1203
0ba21762
TI
1204int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1205 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
1206{
1207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1208 unsigned long pval;
1209 int err;
1210
62932df8 1211 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
985be54b
TI
1212 pval = kcontrol->private_value;
1213 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1214 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1215 kcontrol->private_value = pval;
62932df8 1216 mutex_unlock(&codec->spdif_mutex);
985be54b
TI
1217 return err;
1218}
1219
0ba21762
TI
1220int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1221 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
1222{
1223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1224 unsigned long pval;
1225 int i, indices, err = 0, change = 0;
1226
62932df8 1227 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
985be54b
TI
1228 pval = kcontrol->private_value;
1229 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1230 for (i = 0; i < indices; i++) {
0ba21762
TI
1231 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1232 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
1233 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1234 if (err < 0)
1235 break;
1236 change |= err;
1237 }
1238 kcontrol->private_value = pval;
62932df8 1239 mutex_unlock(&codec->spdif_mutex);
985be54b
TI
1240 return err < 0 ? err : change;
1241}
1242
532d5381
TI
1243/*
1244 * generic bound volume/swtich controls
1245 */
1246int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1247 struct snd_ctl_elem_info *uinfo)
1248{
1249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1250 struct hda_bind_ctls *c;
1251 int err;
1252
532d5381 1253 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
14c65f98 1254 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
1255 kcontrol->private_value = *c->values;
1256 err = c->ops->info(kcontrol, uinfo);
1257 kcontrol->private_value = (long)c;
1258 mutex_unlock(&codec->spdif_mutex);
1259 return err;
1260}
1261
1262int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1263 struct snd_ctl_elem_value *ucontrol)
1264{
1265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1266 struct hda_bind_ctls *c;
1267 int err;
1268
532d5381 1269 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
14c65f98 1270 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
1271 kcontrol->private_value = *c->values;
1272 err = c->ops->get(kcontrol, ucontrol);
1273 kcontrol->private_value = (long)c;
1274 mutex_unlock(&codec->spdif_mutex);
1275 return err;
1276}
1277
1278int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1279 struct snd_ctl_elem_value *ucontrol)
1280{
1281 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1282 struct hda_bind_ctls *c;
1283 unsigned long *vals;
1284 int err = 0, change = 0;
1285
532d5381 1286 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
14c65f98 1287 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
1288 for (vals = c->values; *vals; vals++) {
1289 kcontrol->private_value = *vals;
1290 err = c->ops->put(kcontrol, ucontrol);
1291 if (err < 0)
1292 break;
1293 change |= err;
1294 }
1295 kcontrol->private_value = (long)c;
1296 mutex_unlock(&codec->spdif_mutex);
1297 return err < 0 ? err : change;
1298}
1299
1300int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1301 unsigned int size, unsigned int __user *tlv)
1302{
1303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1304 struct hda_bind_ctls *c;
1305 int err;
1306
532d5381 1307 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
14c65f98 1308 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
1309 kcontrol->private_value = *c->values;
1310 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1311 kcontrol->private_value = (long)c;
1312 mutex_unlock(&codec->spdif_mutex);
1313 return err;
1314}
1315
1316struct hda_ctl_ops snd_hda_bind_vol = {
1317 .info = snd_hda_mixer_amp_volume_info,
1318 .get = snd_hda_mixer_amp_volume_get,
1319 .put = snd_hda_mixer_amp_volume_put,
1320 .tlv = snd_hda_mixer_amp_tlv
1321};
1322
1323struct hda_ctl_ops snd_hda_bind_sw = {
1324 .info = snd_hda_mixer_amp_switch_info,
1325 .get = snd_hda_mixer_amp_switch_get,
1326 .put = snd_hda_mixer_amp_switch_put,
1327 .tlv = snd_hda_mixer_amp_tlv
1328};
1329
1da177e4
LT
1330/*
1331 * SPDIF out controls
1332 */
1333
0ba21762
TI
1334static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1335 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1336{
1337 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1338 uinfo->count = 1;
1339 return 0;
1340}
1341
0ba21762
TI
1342static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1344{
1345 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1346 IEC958_AES0_NONAUDIO |
1347 IEC958_AES0_CON_EMPHASIS_5015 |
1348 IEC958_AES0_CON_NOT_COPYRIGHT;
1349 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1350 IEC958_AES1_CON_ORIGINAL;
1351 return 0;
1352}
1353
0ba21762
TI
1354static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1356{
1357 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1358 IEC958_AES0_NONAUDIO |
1359 IEC958_AES0_PRO_EMPHASIS_5015;
1360 return 0;
1361}
1362
0ba21762
TI
1363static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1365{
1366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1367
1368 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1369 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1370 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1371 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1372
1373 return 0;
1374}
1375
1376/* convert from SPDIF status bits to HDA SPDIF bits
1377 * bit 0 (DigEn) is always set zero (to be filled later)
1378 */
1379static unsigned short convert_from_spdif_status(unsigned int sbits)
1380{
1381 unsigned short val = 0;
1382
1383 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 1384 val |= AC_DIG1_PROFESSIONAL;
1da177e4 1385 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 1386 val |= AC_DIG1_NONAUDIO;
1da177e4 1387 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
1388 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1389 IEC958_AES0_PRO_EMPHASIS_5015)
1390 val |= AC_DIG1_EMPHASIS;
1da177e4 1391 } else {
0ba21762
TI
1392 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1393 IEC958_AES0_CON_EMPHASIS_5015)
1394 val |= AC_DIG1_EMPHASIS;
1395 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1396 val |= AC_DIG1_COPYRIGHT;
1da177e4 1397 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 1398 val |= AC_DIG1_LEVEL;
1da177e4
LT
1399 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1400 }
1401 return val;
1402}
1403
1404/* convert to SPDIF status bits from HDA SPDIF bits
1405 */
1406static unsigned int convert_to_spdif_status(unsigned short val)
1407{
1408 unsigned int sbits = 0;
1409
0ba21762 1410 if (val & AC_DIG1_NONAUDIO)
1da177e4 1411 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 1412 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
1413 sbits |= IEC958_AES0_PROFESSIONAL;
1414 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762 1415 if (sbits & AC_DIG1_EMPHASIS)
1da177e4
LT
1416 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1417 } else {
0ba21762 1418 if (val & AC_DIG1_EMPHASIS)
1da177e4 1419 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 1420 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 1421 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 1422 if (val & AC_DIG1_LEVEL)
1da177e4
LT
1423 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1424 sbits |= val & (0x7f << 8);
1425 }
1426 return sbits;
1427}
1428
0ba21762
TI
1429static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1431{
1432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1433 hda_nid_t nid = kcontrol->private_value;
1434 unsigned short val;
1435 int change;
1436
62932df8 1437 mutex_lock(&codec->spdif_mutex);
1da177e4
LT
1438 codec->spdif_status = ucontrol->value.iec958.status[0] |
1439 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1440 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1441 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1442 val = convert_from_spdif_status(codec->spdif_status);
1443 val |= codec->spdif_ctls & 1;
1444 change = codec->spdif_ctls != val;
1445 codec->spdif_ctls = val;
1446
82beb8fd 1447 if (change) {
de51ca12 1448 hda_nid_t *d;
82beb8fd
TI
1449 snd_hda_codec_write_cache(codec, nid, 0,
1450 AC_VERB_SET_DIGI_CONVERT_1,
1451 val & 0xff);
1452 snd_hda_codec_write_cache(codec, nid, 0,
1453 AC_VERB_SET_DIGI_CONVERT_2,
1454 val >> 8);
de51ca12 1455
9932fbb0
HRK
1456 if (codec->slave_dig_outs)
1457 for (d = codec->slave_dig_outs; *d; d++) {
1458 snd_hda_codec_write_cache(codec, *d, 0,
de51ca12
MR
1459 AC_VERB_SET_DIGI_CONVERT_1,
1460 val & 0xff);
9932fbb0 1461 snd_hda_codec_write_cache(codec, *d, 0,
de51ca12
MR
1462 AC_VERB_SET_DIGI_CONVERT_2,
1463 val >> 8);
9932fbb0 1464 }
1da177e4
LT
1465 }
1466
62932df8 1467 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1468 return change;
1469}
1470
a5ce8890 1471#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1da177e4 1472
0ba21762
TI
1473static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1475{
1476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1477
0ba21762 1478 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1da177e4
LT
1479 return 0;
1480}
1481
0ba21762
TI
1482static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1484{
1485 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1486 hda_nid_t nid = kcontrol->private_value;
1487 unsigned short val;
1488 int change;
1489
62932df8 1490 mutex_lock(&codec->spdif_mutex);
0ba21762 1491 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1da177e4 1492 if (ucontrol->value.integer.value[0])
0ba21762 1493 val |= AC_DIG1_ENABLE;
1da177e4 1494 change = codec->spdif_ctls != val;
82beb8fd 1495 if (change) {
de51ca12 1496 hda_nid_t *d;
1da177e4 1497 codec->spdif_ctls = val;
82beb8fd
TI
1498 snd_hda_codec_write_cache(codec, nid, 0,
1499 AC_VERB_SET_DIGI_CONVERT_1,
1500 val & 0xff);
de51ca12 1501
9932fbb0
HRK
1502 if (codec->slave_dig_outs)
1503 for (d = codec->slave_dig_outs; *d; d++)
1504 snd_hda_codec_write_cache(codec, *d, 0,
de51ca12
MR
1505 AC_VERB_SET_DIGI_CONVERT_1,
1506 val & 0xff);
0ba21762
TI
1507 /* unmute amp switch (if any) */
1508 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
47fd830a
TI
1509 (val & AC_DIG1_ENABLE))
1510 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1511 HDA_AMP_MUTE, 0);
1da177e4 1512 }
62932df8 1513 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1514 return change;
1515}
1516
c8b6bf9b 1517static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
1518 {
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1522 .info = snd_hda_spdif_mask_info,
1523 .get = snd_hda_spdif_cmask_get,
1524 },
1525 {
1526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1528 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1529 .info = snd_hda_spdif_mask_info,
1530 .get = snd_hda_spdif_pmask_get,
1531 },
1532 {
1533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1534 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1535 .info = snd_hda_spdif_mask_info,
1536 .get = snd_hda_spdif_default_get,
1537 .put = snd_hda_spdif_default_put,
1538 },
1539 {
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1541 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1542 .info = snd_hda_spdif_out_switch_info,
1543 .get = snd_hda_spdif_out_switch_get,
1544 .put = snd_hda_spdif_out_switch_put,
1545 },
1546 { } /* end */
1547};
1548
09f99701
TI
1549#define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1550
1da177e4
LT
1551/**
1552 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1553 * @codec: the HDA codec
1554 * @nid: audio out widget NID
1555 *
1556 * Creates controls related with the SPDIF output.
1557 * Called from each patch supporting the SPDIF out.
1558 *
1559 * Returns 0 if successful, or a negative error code.
1560 */
12f288bf 1561int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
1562{
1563 int err;
c8b6bf9b
TI
1564 struct snd_kcontrol *kctl;
1565 struct snd_kcontrol_new *dig_mix;
09f99701 1566 int idx;
1da177e4 1567
09f99701
TI
1568 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1569 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1570 idx))
1571 break;
1572 }
1573 if (idx >= SPDIF_MAX_IDX) {
1574 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1575 return -EBUSY;
1576 }
1da177e4
LT
1577 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1578 kctl = snd_ctl_new1(dig_mix, codec);
09f99701 1579 kctl->id.index = idx;
1da177e4 1580 kctl->private_value = nid;
0ba21762
TI
1581 err = snd_ctl_add(codec->bus->card, kctl);
1582 if (err < 0)
1da177e4
LT
1583 return err;
1584 }
0ba21762 1585 codec->spdif_ctls =
3982d17e
AP
1586 snd_hda_codec_read(codec, nid, 0,
1587 AC_VERB_GET_DIGI_CONVERT_1, 0);
1da177e4
LT
1588 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1589 return 0;
1590}
1591
9a08160b
TI
1592/*
1593 * SPDIF sharing with analog output
1594 */
1595static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_value *ucontrol)
1597{
1598 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1599 ucontrol->value.integer.value[0] = mout->share_spdif;
1600 return 0;
1601}
1602
1603static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
1605{
1606 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1607 mout->share_spdif = !!ucontrol->value.integer.value[0];
1608 return 0;
1609}
1610
1611static struct snd_kcontrol_new spdif_share_sw = {
1612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1613 .name = "IEC958 Default PCM Playback Switch",
1614 .info = snd_ctl_boolean_mono_info,
1615 .get = spdif_share_sw_get,
1616 .put = spdif_share_sw_put,
1617};
1618
1619int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1620 struct hda_multi_out *mout)
1621{
1622 if (!mout->dig_out_nid)
1623 return 0;
1624 /* ATTENTION: here mout is passed as private_data, instead of codec */
1625 return snd_ctl_add(codec->bus->card,
1626 snd_ctl_new1(&spdif_share_sw, mout));
1627}
1628
1da177e4
LT
1629/*
1630 * SPDIF input
1631 */
1632
1633#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1634
0ba21762
TI
1635static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1636 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1637{
1638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1639
1640 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1641 return 0;
1642}
1643
0ba21762
TI
1644static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1646{
1647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648 hda_nid_t nid = kcontrol->private_value;
1649 unsigned int val = !!ucontrol->value.integer.value[0];
1650 int change;
1651
62932df8 1652 mutex_lock(&codec->spdif_mutex);
1da177e4 1653 change = codec->spdif_in_enable != val;
82beb8fd 1654 if (change) {
1da177e4 1655 codec->spdif_in_enable = val;
82beb8fd
TI
1656 snd_hda_codec_write_cache(codec, nid, 0,
1657 AC_VERB_SET_DIGI_CONVERT_1, val);
1da177e4 1658 }
62932df8 1659 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
1660 return change;
1661}
1662
0ba21762
TI
1663static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1665{
1666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1667 hda_nid_t nid = kcontrol->private_value;
1668 unsigned short val;
1669 unsigned int sbits;
1670
3982d17e 1671 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1da177e4
LT
1672 sbits = convert_to_spdif_status(val);
1673 ucontrol->value.iec958.status[0] = sbits;
1674 ucontrol->value.iec958.status[1] = sbits >> 8;
1675 ucontrol->value.iec958.status[2] = sbits >> 16;
1676 ucontrol->value.iec958.status[3] = sbits >> 24;
1677 return 0;
1678}
1679
c8b6bf9b 1680static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
1681 {
1682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1683 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1684 .info = snd_hda_spdif_in_switch_info,
1685 .get = snd_hda_spdif_in_switch_get,
1686 .put = snd_hda_spdif_in_switch_put,
1687 },
1688 {
1689 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1691 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1692 .info = snd_hda_spdif_mask_info,
1693 .get = snd_hda_spdif_in_status_get,
1694 },
1695 { } /* end */
1696};
1697
1698/**
1699 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1700 * @codec: the HDA codec
1701 * @nid: audio in widget NID
1702 *
1703 * Creates controls related with the SPDIF input.
1704 * Called from each patch supporting the SPDIF in.
1705 *
1706 * Returns 0 if successful, or a negative error code.
1707 */
12f288bf 1708int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
1709{
1710 int err;
c8b6bf9b
TI
1711 struct snd_kcontrol *kctl;
1712 struct snd_kcontrol_new *dig_mix;
09f99701 1713 int idx;
1da177e4 1714
09f99701
TI
1715 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1716 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1717 idx))
1718 break;
1719 }
1720 if (idx >= SPDIF_MAX_IDX) {
1721 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1722 return -EBUSY;
1723 }
1da177e4
LT
1724 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1725 kctl = snd_ctl_new1(dig_mix, codec);
1726 kctl->private_value = nid;
0ba21762
TI
1727 err = snd_ctl_add(codec->bus->card, kctl);
1728 if (err < 0)
1da177e4
LT
1729 return err;
1730 }
0ba21762 1731 codec->spdif_in_enable =
3982d17e
AP
1732 snd_hda_codec_read(codec, nid, 0,
1733 AC_VERB_GET_DIGI_CONVERT_1, 0) &
0ba21762 1734 AC_DIG1_ENABLE;
1da177e4
LT
1735 return 0;
1736}
1737
cb53c626 1738#ifdef SND_HDA_NEEDS_RESUME
82beb8fd
TI
1739/*
1740 * command cache
1741 */
1da177e4 1742
b3ac5636
TI
1743/* build a 32bit cache key with the widget id and the command parameter */
1744#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1745#define get_cmd_cache_nid(key) ((key) & 0xff)
1746#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1747
1748/**
1749 * snd_hda_codec_write_cache - send a single command with caching
1750 * @codec: the HDA codec
1751 * @nid: NID to send the command
1752 * @direct: direct flag
1753 * @verb: the verb to send
1754 * @parm: the parameter for the verb
1755 *
1756 * Send a single command without waiting for response.
1757 *
1758 * Returns 0 if successful, or a negative error code.
1759 */
1760int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1761 int direct, unsigned int verb, unsigned int parm)
1762{
1763 int err;
cb53c626 1764 snd_hda_power_up(codec);
b3ac5636
TI
1765 mutex_lock(&codec->bus->cmd_mutex);
1766 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1767 if (!err) {
1768 struct hda_cache_head *c;
1769 u32 key = build_cmd_cache_key(nid, verb);
1770 c = get_alloc_hash(&codec->cmd_cache, key);
1771 if (c)
1772 c->val = parm;
1773 }
1774 mutex_unlock(&codec->bus->cmd_mutex);
cb53c626 1775 snd_hda_power_down(codec);
b3ac5636
TI
1776 return err;
1777}
1778
1779/* resume the all commands from the cache */
1780void snd_hda_codec_resume_cache(struct hda_codec *codec)
1781{
1782 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1783 int i;
1784
1785 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1786 u32 key = buffer->key;
1787 if (!key)
1788 continue;
1789 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1790 get_cmd_cache_cmd(key), buffer->val);
1791 }
1792}
1793
1794/**
1795 * snd_hda_sequence_write_cache - sequence writes with caching
1796 * @codec: the HDA codec
1797 * @seq: VERB array to send
1798 *
1799 * Send the commands sequentially from the given array.
1800 * Thte commands are recorded on cache for power-save and resume.
1801 * The array must be terminated with NID=0.
1802 */
1803void snd_hda_sequence_write_cache(struct hda_codec *codec,
1804 const struct hda_verb *seq)
1805{
1806 for (; seq->nid; seq++)
1807 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1808 seq->param);
1809}
cb53c626 1810#endif /* SND_HDA_NEEDS_RESUME */
b3ac5636 1811
54d17403
TI
1812/*
1813 * set power state of the codec
1814 */
1815static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1816 unsigned int power_state)
1817{
cb53c626
TI
1818 hda_nid_t nid;
1819 int i;
54d17403
TI
1820
1821 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1822 power_state);
d2595d86 1823 msleep(10); /* partial workaround for "azx_get_response timeout" */
54d17403 1824
cb53c626
TI
1825 nid = codec->start_nid;
1826 for (i = 0; i < codec->num_nodes; i++, nid++) {
7eba5c9d
TI
1827 unsigned int wcaps = get_wcaps(codec, nid);
1828 if (wcaps & AC_WCAP_POWER) {
1829 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1830 AC_WCAP_TYPE_SHIFT;
1831 if (wid_type == AC_WID_PIN) {
1832 unsigned int pincap;
1833 /*
1834 * don't power down the widget if it controls
1835 * eapd and EAPD_BTLENABLE is set.
1836 */
1837 pincap = snd_hda_param_read(codec, nid,
1838 AC_PAR_PIN_CAP);
1839 if (pincap & AC_PINCAP_EAPD) {
1840 int eapd = snd_hda_codec_read(codec,
1841 nid, 0,
1842 AC_VERB_GET_EAPD_BTLENABLE, 0);
1843 eapd &= 0x02;
1844 if (power_state == AC_PWRST_D3 && eapd)
1845 continue;
1846 }
1194b5b7 1847 }
54d17403
TI
1848 snd_hda_codec_write(codec, nid, 0,
1849 AC_VERB_SET_POWER_STATE,
1850 power_state);
1194b5b7 1851 }
54d17403
TI
1852 }
1853
cb53c626
TI
1854 if (power_state == AC_PWRST_D0) {
1855 unsigned long end_time;
1856 int state;
54d17403 1857 msleep(10);
cb53c626
TI
1858 /* wait until the codec reachs to D0 */
1859 end_time = jiffies + msecs_to_jiffies(500);
1860 do {
1861 state = snd_hda_codec_read(codec, fg, 0,
1862 AC_VERB_GET_POWER_STATE, 0);
1863 if (state == power_state)
1864 break;
1865 msleep(1);
1866 } while (time_after_eq(end_time, jiffies));
1867 }
1868}
1869
1870#ifdef SND_HDA_NEEDS_RESUME
1871/*
1872 * call suspend and power-down; used both from PM and power-save
1873 */
1874static void hda_call_codec_suspend(struct hda_codec *codec)
1875{
1876 if (codec->patch_ops.suspend)
1877 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1878 hda_set_power_state(codec,
1879 codec->afg ? codec->afg : codec->mfg,
1880 AC_PWRST_D3);
1881#ifdef CONFIG_SND_HDA_POWER_SAVE
1882 cancel_delayed_work(&codec->power_work);
95e99fda 1883 codec->power_on = 0;
a221e287 1884 codec->power_transition = 0;
cb53c626 1885#endif
54d17403
TI
1886}
1887
cb53c626
TI
1888/*
1889 * kick up codec; used both from PM and power-save
1890 */
1891static void hda_call_codec_resume(struct hda_codec *codec)
1892{
1893 hda_set_power_state(codec,
1894 codec->afg ? codec->afg : codec->mfg,
1895 AC_PWRST_D0);
1896 if (codec->patch_ops.resume)
1897 codec->patch_ops.resume(codec);
1898 else {
9d99f312
TI
1899 if (codec->patch_ops.init)
1900 codec->patch_ops.init(codec);
cb53c626
TI
1901 snd_hda_codec_resume_amp(codec);
1902 snd_hda_codec_resume_cache(codec);
1903 }
1904}
1905#endif /* SND_HDA_NEEDS_RESUME */
1906
54d17403 1907
1da177e4
LT
1908/**
1909 * snd_hda_build_controls - build mixer controls
1910 * @bus: the BUS
1911 *
1912 * Creates mixer controls for each codec included in the bus.
1913 *
1914 * Returns 0 if successful, otherwise a negative error code.
1915 */
756e2b01 1916int __devinit snd_hda_build_controls(struct hda_bus *bus)
1da177e4 1917{
0ba21762 1918 struct hda_codec *codec;
1da177e4 1919
0ba21762 1920 list_for_each_entry(codec, &bus->codec_list, list) {
cb53c626
TI
1921 int err = 0;
1922 /* fake as if already powered-on */
1923 hda_keep_power_on(codec);
1924 /* then fire up */
54d17403
TI
1925 hda_set_power_state(codec,
1926 codec->afg ? codec->afg : codec->mfg,
1927 AC_PWRST_D0);
cb53c626
TI
1928 /* continue to initialize... */
1929 if (codec->patch_ops.init)
1930 err = codec->patch_ops.init(codec);
1931 if (!err && codec->patch_ops.build_controls)
1932 err = codec->patch_ops.build_controls(codec);
1933 snd_hda_power_down(codec);
1da177e4
LT
1934 if (err < 0)
1935 return err;
1936 }
cb53c626 1937
1da177e4
LT
1938 return 0;
1939}
1940
1da177e4
LT
1941/*
1942 * stream formats
1943 */
befdf316
TI
1944struct hda_rate_tbl {
1945 unsigned int hz;
1946 unsigned int alsa_bits;
1947 unsigned int hda_fmt;
1948};
1949
1950static struct hda_rate_tbl rate_bits[] = {
1da177e4 1951 /* rate in Hz, ALSA rate bitmask, HDA format value */
9d8f53f2
NG
1952
1953 /* autodetected value used in snd_hda_query_supported_pcm */
1da177e4
LT
1954 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1955 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1956 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1957 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1958 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1959 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1960 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1961 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1962 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1963 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1964 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
a961f9fe
TI
1965#define AC_PAR_PCM_RATE_BITS 11
1966 /* up to bits 10, 384kHZ isn't supported properly */
1967
1968 /* not autodetected value */
1969 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
9d8f53f2 1970
befdf316 1971 { 0 } /* terminator */
1da177e4
LT
1972};
1973
1974/**
1975 * snd_hda_calc_stream_format - calculate format bitset
1976 * @rate: the sample rate
1977 * @channels: the number of channels
1978 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1979 * @maxbps: the max. bps
1980 *
1981 * Calculate the format bitset from the given rate, channels and th PCM format.
1982 *
1983 * Return zero if invalid.
1984 */
1985unsigned int snd_hda_calc_stream_format(unsigned int rate,
1986 unsigned int channels,
1987 unsigned int format,
1988 unsigned int maxbps)
1989{
1990 int i;
1991 unsigned int val = 0;
1992
befdf316
TI
1993 for (i = 0; rate_bits[i].hz; i++)
1994 if (rate_bits[i].hz == rate) {
1995 val = rate_bits[i].hda_fmt;
1da177e4
LT
1996 break;
1997 }
0ba21762 1998 if (!rate_bits[i].hz) {
1da177e4
LT
1999 snd_printdd("invalid rate %d\n", rate);
2000 return 0;
2001 }
2002
2003 if (channels == 0 || channels > 8) {
2004 snd_printdd("invalid channels %d\n", channels);
2005 return 0;
2006 }
2007 val |= channels - 1;
2008
2009 switch (snd_pcm_format_width(format)) {
2010 case 8: val |= 0x00; break;
2011 case 16: val |= 0x10; break;
2012 case 20:
2013 case 24:
2014 case 32:
2015 if (maxbps >= 32)
2016 val |= 0x40;
2017 else if (maxbps >= 24)
2018 val |= 0x30;
2019 else
2020 val |= 0x20;
2021 break;
2022 default:
0ba21762
TI
2023 snd_printdd("invalid format width %d\n",
2024 snd_pcm_format_width(format));
1da177e4
LT
2025 return 0;
2026 }
2027
2028 return val;
2029}
2030
2031/**
2032 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2033 * @codec: the HDA codec
2034 * @nid: NID to query
2035 * @ratesp: the pointer to store the detected rate bitflags
2036 * @formatsp: the pointer to store the detected formats
2037 * @bpsp: the pointer to store the detected format widths
2038 *
2039 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2040 * or @bsps argument is ignored.
2041 *
2042 * Returns 0 if successful, otherwise a negative error code.
2043 */
2044int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2045 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2046{
2047 int i;
2048 unsigned int val, streams;
2049
2050 val = 0;
2051 if (nid != codec->afg &&
54d17403 2052 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1da177e4
LT
2053 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2054 if (val == -1)
2055 return -EIO;
2056 }
0ba21762 2057 if (!val)
1da177e4
LT
2058 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2059
2060 if (ratesp) {
2061 u32 rates = 0;
a961f9fe 2062 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1da177e4 2063 if (val & (1 << i))
befdf316 2064 rates |= rate_bits[i].alsa_bits;
1da177e4
LT
2065 }
2066 *ratesp = rates;
2067 }
2068
2069 if (formatsp || bpsp) {
2070 u64 formats = 0;
2071 unsigned int bps;
2072 unsigned int wcaps;
2073
54d17403 2074 wcaps = get_wcaps(codec, nid);
1da177e4
LT
2075 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2076 if (streams == -1)
2077 return -EIO;
0ba21762
TI
2078 if (!streams) {
2079 streams = snd_hda_param_read(codec, codec->afg,
2080 AC_PAR_STREAM);
1da177e4
LT
2081 if (streams == -1)
2082 return -EIO;
2083 }
2084
2085 bps = 0;
2086 if (streams & AC_SUPFMT_PCM) {
2087 if (val & AC_SUPPCM_BITS_8) {
2088 formats |= SNDRV_PCM_FMTBIT_U8;
2089 bps = 8;
2090 }
2091 if (val & AC_SUPPCM_BITS_16) {
2092 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2093 bps = 16;
2094 }
2095 if (wcaps & AC_WCAP_DIGITAL) {
2096 if (val & AC_SUPPCM_BITS_32)
2097 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2098 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2099 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2100 if (val & AC_SUPPCM_BITS_24)
2101 bps = 24;
2102 else if (val & AC_SUPPCM_BITS_20)
2103 bps = 20;
0ba21762
TI
2104 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2105 AC_SUPPCM_BITS_32)) {
1da177e4
LT
2106 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2107 if (val & AC_SUPPCM_BITS_32)
2108 bps = 32;
1da177e4
LT
2109 else if (val & AC_SUPPCM_BITS_24)
2110 bps = 24;
33ef7651
NG
2111 else if (val & AC_SUPPCM_BITS_20)
2112 bps = 20;
1da177e4
LT
2113 }
2114 }
0ba21762
TI
2115 else if (streams == AC_SUPFMT_FLOAT32) {
2116 /* should be exclusive */
1da177e4
LT
2117 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2118 bps = 32;
0ba21762
TI
2119 } else if (streams == AC_SUPFMT_AC3) {
2120 /* should be exclusive */
1da177e4
LT
2121 /* temporary hack: we have still no proper support
2122 * for the direct AC3 stream...
2123 */
2124 formats |= SNDRV_PCM_FMTBIT_U8;
2125 bps = 8;
2126 }
2127 if (formatsp)
2128 *formatsp = formats;
2129 if (bpsp)
2130 *bpsp = bps;
2131 }
2132
2133 return 0;
2134}
2135
2136/**
0ba21762
TI
2137 * snd_hda_is_supported_format - check whether the given node supports
2138 * the format val
1da177e4
LT
2139 *
2140 * Returns 1 if supported, 0 if not.
2141 */
2142int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2143 unsigned int format)
2144{
2145 int i;
2146 unsigned int val = 0, rate, stream;
2147
2148 if (nid != codec->afg &&
54d17403 2149 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1da177e4
LT
2150 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2151 if (val == -1)
2152 return 0;
2153 }
0ba21762 2154 if (!val) {
1da177e4
LT
2155 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2156 if (val == -1)
2157 return 0;
2158 }
2159
2160 rate = format & 0xff00;
a961f9fe 2161 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
befdf316 2162 if (rate_bits[i].hda_fmt == rate) {
1da177e4
LT
2163 if (val & (1 << i))
2164 break;
2165 return 0;
2166 }
a961f9fe 2167 if (i >= AC_PAR_PCM_RATE_BITS)
1da177e4
LT
2168 return 0;
2169
2170 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2171 if (stream == -1)
2172 return 0;
0ba21762 2173 if (!stream && nid != codec->afg)
1da177e4 2174 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
0ba21762 2175 if (!stream || stream == -1)
1da177e4
LT
2176 return 0;
2177
2178 if (stream & AC_SUPFMT_PCM) {
2179 switch (format & 0xf0) {
2180 case 0x00:
0ba21762 2181 if (!(val & AC_SUPPCM_BITS_8))
1da177e4
LT
2182 return 0;
2183 break;
2184 case 0x10:
0ba21762 2185 if (!(val & AC_SUPPCM_BITS_16))
1da177e4
LT
2186 return 0;
2187 break;
2188 case 0x20:
0ba21762 2189 if (!(val & AC_SUPPCM_BITS_20))
1da177e4
LT
2190 return 0;
2191 break;
2192 case 0x30:
0ba21762 2193 if (!(val & AC_SUPPCM_BITS_24))
1da177e4
LT
2194 return 0;
2195 break;
2196 case 0x40:
0ba21762 2197 if (!(val & AC_SUPPCM_BITS_32))
1da177e4
LT
2198 return 0;
2199 break;
2200 default:
2201 return 0;
2202 }
2203 } else {
2204 /* FIXME: check for float32 and AC3? */
2205 }
2206
2207 return 1;
2208}
2209
2210/*
2211 * PCM stuff
2212 */
2213static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2214 struct hda_codec *codec,
c8b6bf9b 2215 struct snd_pcm_substream *substream)
1da177e4
LT
2216{
2217 return 0;
2218}
2219
2220static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2221 struct hda_codec *codec,
2222 unsigned int stream_tag,
2223 unsigned int format,
c8b6bf9b 2224 struct snd_pcm_substream *substream)
1da177e4
LT
2225{
2226 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2227 return 0;
2228}
2229
2230static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2231 struct hda_codec *codec,
c8b6bf9b 2232 struct snd_pcm_substream *substream)
1da177e4 2233{
888afa15 2234 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1da177e4
LT
2235 return 0;
2236}
2237
0ba21762
TI
2238static int __devinit set_pcm_default_values(struct hda_codec *codec,
2239 struct hda_pcm_stream *info)
1da177e4 2240{
0ba21762
TI
2241 /* query support PCM information from the given NID */
2242 if (info->nid && (!info->rates || !info->formats)) {
2243 snd_hda_query_supported_pcm(codec, info->nid,
2244 info->rates ? NULL : &info->rates,
2245 info->formats ? NULL : &info->formats,
2246 info->maxbps ? NULL : &info->maxbps);
1da177e4
LT
2247 }
2248 if (info->ops.open == NULL)
2249 info->ops.open = hda_pcm_default_open_close;
2250 if (info->ops.close == NULL)
2251 info->ops.close = hda_pcm_default_open_close;
2252 if (info->ops.prepare == NULL) {
da3cec35
TI
2253 if (snd_BUG_ON(!info->nid))
2254 return -EINVAL;
1da177e4
LT
2255 info->ops.prepare = hda_pcm_default_prepare;
2256 }
1da177e4 2257 if (info->ops.cleanup == NULL) {
da3cec35
TI
2258 if (snd_BUG_ON(!info->nid))
2259 return -EINVAL;
1da177e4
LT
2260 info->ops.cleanup = hda_pcm_default_cleanup;
2261 }
2262 return 0;
2263}
2264
2265/**
2266 * snd_hda_build_pcms - build PCM information
2267 * @bus: the BUS
2268 *
2269 * Create PCM information for each codec included in the bus.
2270 *
2271 * The build_pcms codec patch is requested to set up codec->num_pcms and
2272 * codec->pcm_info properly. The array is referred by the top-level driver
2273 * to create its PCM instances.
2274 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2275 * callback.
2276 *
2277 * At least, substreams, channels_min and channels_max must be filled for
2278 * each stream. substreams = 0 indicates that the stream doesn't exist.
2279 * When rates and/or formats are zero, the supported values are queried
2280 * from the given nid. The nid is used also by the default ops.prepare
2281 * and ops.cleanup callbacks.
2282 *
2283 * The driver needs to call ops.open in its open callback. Similarly,
2284 * ops.close is supposed to be called in the close callback.
2285 * ops.prepare should be called in the prepare or hw_params callback
2286 * with the proper parameters for set up.
2287 * ops.cleanup should be called in hw_free for clean up of streams.
2288 *
2289 * This function returns 0 if successfull, or a negative error code.
2290 */
756e2b01 2291int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1da177e4 2292{
0ba21762 2293 struct hda_codec *codec;
1da177e4 2294
0ba21762 2295 list_for_each_entry(codec, &bus->codec_list, list) {
1da177e4
LT
2296 unsigned int pcm, s;
2297 int err;
0ba21762 2298 if (!codec->patch_ops.build_pcms)
1da177e4
LT
2299 continue;
2300 err = codec->patch_ops.build_pcms(codec);
2301 if (err < 0)
2302 return err;
2303 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2304 for (s = 0; s < 2; s++) {
2305 struct hda_pcm_stream *info;
2306 info = &codec->pcm_info[pcm].stream[s];
0ba21762 2307 if (!info->substreams)
1da177e4
LT
2308 continue;
2309 err = set_pcm_default_values(codec, info);
2310 if (err < 0)
2311 return err;
2312 }
2313 }
2314 }
2315 return 0;
2316}
2317
1da177e4
LT
2318/**
2319 * snd_hda_check_board_config - compare the current codec with the config table
2320 * @codec: the HDA codec
f5fcc13c
TI
2321 * @num_configs: number of config enums
2322 * @models: array of model name strings
1da177e4
LT
2323 * @tbl: configuration table, terminated by null entries
2324 *
2325 * Compares the modelname or PCI subsystem id of the current codec with the
2326 * given configuration table. If a matching entry is found, returns its
2327 * config value (supposed to be 0 or positive).
2328 *
2329 * If no entries are matching, the function returns a negative value.
2330 */
12f288bf
TI
2331int snd_hda_check_board_config(struct hda_codec *codec,
2332 int num_configs, const char **models,
2333 const struct snd_pci_quirk *tbl)
1da177e4 2334{
f5fcc13c
TI
2335 if (codec->bus->modelname && models) {
2336 int i;
2337 for (i = 0; i < num_configs; i++) {
2338 if (models[i] &&
2339 !strcmp(codec->bus->modelname, models[i])) {
2340 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2341 "selected\n", models[i]);
2342 return i;
1da177e4
LT
2343 }
2344 }
2345 }
2346
f5fcc13c
TI
2347 if (!codec->bus->pci || !tbl)
2348 return -1;
2349
2350 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2351 if (!tbl)
2352 return -1;
2353 if (tbl->value >= 0 && tbl->value < num_configs) {
62cf872a 2354#ifdef CONFIG_SND_DEBUG_VERBOSE
f5fcc13c
TI
2355 char tmp[10];
2356 const char *model = NULL;
2357 if (models)
2358 model = models[tbl->value];
2359 if (!model) {
2360 sprintf(tmp, "#%d", tbl->value);
2361 model = tmp;
1da177e4 2362 }
f5fcc13c
TI
2363 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2364 "for config %x:%x (%s)\n",
2365 model, tbl->subvendor, tbl->subdevice,
2366 (tbl->name ? tbl->name : "Unknown device"));
2367#endif
2368 return tbl->value;
1da177e4
LT
2369 }
2370 return -1;
2371}
2372
2373/**
2374 * snd_hda_add_new_ctls - create controls from the array
2375 * @codec: the HDA codec
c8b6bf9b 2376 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
2377 *
2378 * This helper function creates and add new controls in the given array.
2379 * The array must be terminated with an empty entry as terminator.
2380 *
2381 * Returns 0 if successful, or a negative error code.
2382 */
12f288bf 2383int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
1da177e4 2384{
cb53c626 2385 int err;
1da177e4
LT
2386
2387 for (; knew->name; knew++) {
54d17403
TI
2388 struct snd_kcontrol *kctl;
2389 kctl = snd_ctl_new1(knew, codec);
0ba21762 2390 if (!kctl)
54d17403
TI
2391 return -ENOMEM;
2392 err = snd_ctl_add(codec->bus->card, kctl);
2393 if (err < 0) {
0ba21762 2394 if (!codec->addr)
54d17403
TI
2395 return err;
2396 kctl = snd_ctl_new1(knew, codec);
0ba21762 2397 if (!kctl)
54d17403
TI
2398 return -ENOMEM;
2399 kctl->id.device = codec->addr;
0ba21762
TI
2400 err = snd_ctl_add(codec->bus->card, kctl);
2401 if (err < 0)
54d17403
TI
2402 return err;
2403 }
1da177e4
LT
2404 }
2405 return 0;
2406}
2407
cb53c626
TI
2408#ifdef CONFIG_SND_HDA_POWER_SAVE
2409static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2410 unsigned int power_state);
2411
2412static void hda_power_work(struct work_struct *work)
2413{
2414 struct hda_codec *codec =
2415 container_of(work, struct hda_codec, power_work.work);
2416
2e492462
ML
2417 if (!codec->power_on || codec->power_count) {
2418 codec->power_transition = 0;
cb53c626 2419 return;
2e492462 2420 }
cb53c626
TI
2421
2422 hda_call_codec_suspend(codec);
cb53c626
TI
2423 if (codec->bus->ops.pm_notify)
2424 codec->bus->ops.pm_notify(codec);
2425}
2426
2427static void hda_keep_power_on(struct hda_codec *codec)
2428{
2429 codec->power_count++;
2430 codec->power_on = 1;
2431}
2432
2433void snd_hda_power_up(struct hda_codec *codec)
2434{
2435 codec->power_count++;
a221e287 2436 if (codec->power_on || codec->power_transition)
cb53c626
TI
2437 return;
2438
2439 codec->power_on = 1;
2440 if (codec->bus->ops.pm_notify)
2441 codec->bus->ops.pm_notify(codec);
2442 hda_call_codec_resume(codec);
2443 cancel_delayed_work(&codec->power_work);
a221e287 2444 codec->power_transition = 0;
cb53c626
TI
2445}
2446
2447void snd_hda_power_down(struct hda_codec *codec)
2448{
2449 --codec->power_count;
a221e287 2450 if (!codec->power_on || codec->power_count || codec->power_transition)
cb53c626 2451 return;
a221e287
TI
2452 if (power_save) {
2453 codec->power_transition = 1; /* avoid reentrance */
cb53c626
TI
2454 schedule_delayed_work(&codec->power_work,
2455 msecs_to_jiffies(power_save * 1000));
a221e287 2456 }
cb53c626
TI
2457}
2458
2459int snd_hda_check_amp_list_power(struct hda_codec *codec,
2460 struct hda_loopback_check *check,
2461 hda_nid_t nid)
2462{
2463 struct hda_amp_list *p;
2464 int ch, v;
2465
2466 if (!check->amplist)
2467 return 0;
2468 for (p = check->amplist; p->nid; p++) {
2469 if (p->nid == nid)
2470 break;
2471 }
2472 if (!p->nid)
2473 return 0; /* nothing changed */
2474
2475 for (p = check->amplist; p->nid; p++) {
2476 for (ch = 0; ch < 2; ch++) {
2477 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2478 p->idx);
2479 if (!(v & HDA_AMP_MUTE) && v > 0) {
2480 if (!check->power_on) {
2481 check->power_on = 1;
2482 snd_hda_power_up(codec);
2483 }
2484 return 1;
2485 }
2486 }
2487 }
2488 if (check->power_on) {
2489 check->power_on = 0;
2490 snd_hda_power_down(codec);
2491 }
2492 return 0;
2493}
2494#endif
1da177e4 2495
c8b6bf9b 2496/*
d2a6d7dc
TI
2497 * Channel mode helper
2498 */
0ba21762
TI
2499int snd_hda_ch_mode_info(struct hda_codec *codec,
2500 struct snd_ctl_elem_info *uinfo,
2501 const struct hda_channel_mode *chmode,
2502 int num_chmodes)
d2a6d7dc
TI
2503{
2504 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2505 uinfo->count = 1;
2506 uinfo->value.enumerated.items = num_chmodes;
2507 if (uinfo->value.enumerated.item >= num_chmodes)
2508 uinfo->value.enumerated.item = num_chmodes - 1;
2509 sprintf(uinfo->value.enumerated.name, "%dch",
2510 chmode[uinfo->value.enumerated.item].channels);
2511 return 0;
2512}
2513
0ba21762
TI
2514int snd_hda_ch_mode_get(struct hda_codec *codec,
2515 struct snd_ctl_elem_value *ucontrol,
2516 const struct hda_channel_mode *chmode,
2517 int num_chmodes,
d2a6d7dc
TI
2518 int max_channels)
2519{
2520 int i;
2521
2522 for (i = 0; i < num_chmodes; i++) {
2523 if (max_channels == chmode[i].channels) {
2524 ucontrol->value.enumerated.item[0] = i;
2525 break;
2526 }
2527 }
2528 return 0;
2529}
2530
0ba21762
TI
2531int snd_hda_ch_mode_put(struct hda_codec *codec,
2532 struct snd_ctl_elem_value *ucontrol,
2533 const struct hda_channel_mode *chmode,
2534 int num_chmodes,
d2a6d7dc
TI
2535 int *max_channelsp)
2536{
2537 unsigned int mode;
2538
2539 mode = ucontrol->value.enumerated.item[0];
68ea7b2f
TI
2540 if (mode >= num_chmodes)
2541 return -EINVAL;
82beb8fd 2542 if (*max_channelsp == chmode[mode].channels)
d2a6d7dc
TI
2543 return 0;
2544 /* change the current channel setting */
2545 *max_channelsp = chmode[mode].channels;
2546 if (chmode[mode].sequence)
82beb8fd 2547 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
d2a6d7dc
TI
2548 return 1;
2549}
2550
1da177e4
LT
2551/*
2552 * input MUX helper
2553 */
0ba21762
TI
2554int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2555 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2556{
2557 unsigned int index;
2558
2559 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2560 uinfo->count = 1;
2561 uinfo->value.enumerated.items = imux->num_items;
5513b0c5
TI
2562 if (!imux->num_items)
2563 return 0;
1da177e4
LT
2564 index = uinfo->value.enumerated.item;
2565 if (index >= imux->num_items)
2566 index = imux->num_items - 1;
2567 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2568 return 0;
2569}
2570
0ba21762
TI
2571int snd_hda_input_mux_put(struct hda_codec *codec,
2572 const struct hda_input_mux *imux,
2573 struct snd_ctl_elem_value *ucontrol,
2574 hda_nid_t nid,
1da177e4
LT
2575 unsigned int *cur_val)
2576{
2577 unsigned int idx;
2578
5513b0c5
TI
2579 if (!imux->num_items)
2580 return 0;
1da177e4
LT
2581 idx = ucontrol->value.enumerated.item[0];
2582 if (idx >= imux->num_items)
2583 idx = imux->num_items - 1;
82beb8fd 2584 if (*cur_val == idx)
1da177e4 2585 return 0;
82beb8fd
TI
2586 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2587 imux->items[idx].index);
1da177e4
LT
2588 *cur_val = idx;
2589 return 1;
2590}
2591
2592
2593/*
2594 * Multi-channel / digital-out PCM helper functions
2595 */
2596
6b97eb45
TI
2597/* setup SPDIF output stream */
2598static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2599 unsigned int stream_tag, unsigned int format)
2600{
de51ca12
MR
2601 hda_nid_t *d;
2602
6b97eb45 2603 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
de51ca12 2604 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
6b97eb45 2605 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
de51ca12
MR
2606 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2607
9932fbb0
HRK
2608 if (codec->slave_dig_outs)
2609 for (d = codec->slave_dig_outs; *d; d++)
2610 snd_hda_codec_write(codec, *d, 0,
2611 AC_VERB_SET_DIGI_CONVERT_1,
6b97eb45 2612 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
de51ca12 2613 }
6b97eb45
TI
2614 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2615 /* turn on again (if needed) */
de51ca12 2616 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
6b97eb45
TI
2617 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2618 codec->spdif_ctls & 0xff);
de51ca12 2619
9932fbb0
HRK
2620 if (codec->slave_dig_outs)
2621 for (d = codec->slave_dig_outs; *d; d++)
2622 snd_hda_codec_write(codec, *d, 0,
2623 AC_VERB_SET_DIGI_CONVERT_1,
de51ca12
MR
2624 codec->spdif_ctls & 0xff);
2625 }
2626
6b97eb45
TI
2627}
2628
1da177e4
LT
2629/*
2630 * open the digital out in the exclusive mode
2631 */
0ba21762
TI
2632int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2633 struct hda_multi_out *mout)
1da177e4 2634{
62932df8 2635 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
2636 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2637 /* already opened as analog dup; reset it once */
888afa15 2638 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
1da177e4 2639 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 2640 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2641 return 0;
2642}
2643
6b97eb45
TI
2644int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2645 struct hda_multi_out *mout,
2646 unsigned int stream_tag,
2647 unsigned int format,
2648 struct snd_pcm_substream *substream)
2649{
de51ca12 2650 hda_nid_t *nid;
6b97eb45
TI
2651 mutex_lock(&codec->spdif_mutex);
2652 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
de51ca12
MR
2653 if (codec->slave_dig_outs)
2654 for (nid = codec->slave_dig_outs; *nid; nid++)
2655 setup_dig_out_stream(codec, *nid, stream_tag, format);
6b97eb45
TI
2656 mutex_unlock(&codec->spdif_mutex);
2657 return 0;
2658}
2659
1da177e4
LT
2660/*
2661 * release the digital out
2662 */
0ba21762
TI
2663int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2664 struct hda_multi_out *mout)
1da177e4 2665{
62932df8 2666 mutex_lock(&codec->spdif_mutex);
1da177e4 2667 mout->dig_out_used = 0;
62932df8 2668 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2669 return 0;
2670}
2671
2672/*
2673 * set up more restrictions for analog out
2674 */
0ba21762
TI
2675int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2676 struct hda_multi_out *mout,
9a08160b
TI
2677 struct snd_pcm_substream *substream,
2678 struct hda_pcm_stream *hinfo)
2679{
2680 struct snd_pcm_runtime *runtime = substream->runtime;
2681 runtime->hw.channels_max = mout->max_channels;
2682 if (mout->dig_out_nid) {
2683 if (!mout->analog_rates) {
2684 mout->analog_rates = hinfo->rates;
2685 mout->analog_formats = hinfo->formats;
2686 mout->analog_maxbps = hinfo->maxbps;
2687 } else {
2688 runtime->hw.rates = mout->analog_rates;
2689 runtime->hw.formats = mout->analog_formats;
2690 hinfo->maxbps = mout->analog_maxbps;
2691 }
2692 if (!mout->spdif_rates) {
2693 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2694 &mout->spdif_rates,
2695 &mout->spdif_formats,
2696 &mout->spdif_maxbps);
2697 }
2698 mutex_lock(&codec->spdif_mutex);
2699 if (mout->share_spdif) {
2700 runtime->hw.rates &= mout->spdif_rates;
2701 runtime->hw.formats &= mout->spdif_formats;
2702 if (mout->spdif_maxbps < hinfo->maxbps)
2703 hinfo->maxbps = mout->spdif_maxbps;
2704 }
eaa9985b 2705 mutex_unlock(&codec->spdif_mutex);
9a08160b 2706 }
1da177e4
LT
2707 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2708 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2709}
2710
2711/*
2712 * set up the i/o for analog out
2713 * when the digital out is available, copy the front out to digital out, too.
2714 */
0ba21762
TI
2715int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2716 struct hda_multi_out *mout,
1da177e4
LT
2717 unsigned int stream_tag,
2718 unsigned int format,
c8b6bf9b 2719 struct snd_pcm_substream *substream)
1da177e4
LT
2720{
2721 hda_nid_t *nids = mout->dac_nids;
de51ca12 2722 hda_nid_t *d;
1da177e4
LT
2723 int chs = substream->runtime->channels;
2724 int i;
2725
62932df8 2726 mutex_lock(&codec->spdif_mutex);
9a08160b
TI
2727 if (mout->dig_out_nid && mout->share_spdif &&
2728 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1da177e4 2729 if (chs == 2 &&
0ba21762
TI
2730 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2731 format) &&
2732 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1da177e4 2733 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
2734 setup_dig_out_stream(codec, mout->dig_out_nid,
2735 stream_tag, format);
de51ca12
MR
2736 if (codec->slave_dig_outs)
2737 for (d = codec->slave_dig_outs; *d; d++)
2738 setup_dig_out_stream(codec, *d,
2739 stream_tag, format);
1da177e4
LT
2740 } else {
2741 mout->dig_out_used = 0;
888afa15 2742 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
de51ca12
MR
2743 if (codec->slave_dig_outs)
2744 for (d = codec->slave_dig_outs; *d; d++)
2745 snd_hda_codec_cleanup_stream(codec, *d);
1da177e4
LT
2746 }
2747 }
62932df8 2748 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2749
2750 /* front */
0ba21762
TI
2751 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2752 0, format);
d29240ce
TI
2753 if (!mout->no_share_stream &&
2754 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 2755 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
2756 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2757 0, format);
82bc955f
TI
2758 /* extra outputs copied from front */
2759 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
d29240ce 2760 if (!mout->no_share_stream && mout->extra_out_nid[i])
82bc955f
TI
2761 snd_hda_codec_setup_stream(codec,
2762 mout->extra_out_nid[i],
2763 stream_tag, 0, format);
2764
1da177e4
LT
2765 /* surrounds */
2766 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 2767 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
2768 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2769 i * 2, format);
d29240ce 2770 else if (!mout->no_share_stream) /* copy front */
0ba21762
TI
2771 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2772 0, format);
1da177e4
LT
2773 }
2774 return 0;
2775}
2776
2777/*
2778 * clean up the setting for analog out
2779 */
0ba21762
TI
2780int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2781 struct hda_multi_out *mout)
1da177e4
LT
2782{
2783 hda_nid_t *nids = mout->dac_nids;
2784 int i;
2785
2786 for (i = 0; i < mout->num_dacs; i++)
888afa15 2787 snd_hda_codec_cleanup_stream(codec, nids[i]);
1da177e4 2788 if (mout->hp_nid)
888afa15 2789 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
82bc955f
TI
2790 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2791 if (mout->extra_out_nid[i])
888afa15
TI
2792 snd_hda_codec_cleanup_stream(codec,
2793 mout->extra_out_nid[i]);
62932df8 2794 mutex_lock(&codec->spdif_mutex);
1da177e4 2795 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
888afa15 2796 snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
1da177e4
LT
2797 mout->dig_out_used = 0;
2798 }
62932df8 2799 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2800 return 0;
2801}
2802
e9edcee0
TI
2803/*
2804 * Helper for automatic ping configuration
2805 */
df694daa 2806
12f288bf 2807static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
df694daa
KY
2808{
2809 for (; *list; list++)
2810 if (*list == nid)
2811 return 1;
2812 return 0;
2813}
2814
81937d3b
SL
2815
2816/*
2817 * Sort an associated group of pins according to their sequence numbers.
2818 */
2819static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2820 int num_pins)
2821{
2822 int i, j;
2823 short seq;
2824 hda_nid_t nid;
2825
2826 for (i = 0; i < num_pins; i++) {
2827 for (j = i + 1; j < num_pins; j++) {
2828 if (sequences[i] > sequences[j]) {
2829 seq = sequences[i];
2830 sequences[i] = sequences[j];
2831 sequences[j] = seq;
2832 nid = pins[i];
2833 pins[i] = pins[j];
2834 pins[j] = nid;
2835 }
2836 }
2837 }
2838}
2839
2840
82bc955f
TI
2841/*
2842 * Parse all pin widgets and store the useful pin nids to cfg
2843 *
2844 * The number of line-outs or any primary output is stored in line_outs,
2845 * and the corresponding output pins are assigned to line_out_pins[],
2846 * in the order of front, rear, CLFE, side, ...
2847 *
2848 * If more extra outputs (speaker and headphone) are found, the pins are
eb06ed8f 2849 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
82bc955f
TI
2850 * is detected, one of speaker of HP pins is assigned as the primary
2851 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2852 * if any analog output exists.
2853 *
2854 * The analog input pins are assigned to input_pins array.
2855 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2856 * respectively.
2857 */
12f288bf
TI
2858int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2859 struct auto_pin_cfg *cfg,
2860 hda_nid_t *ignore_nids)
e9edcee0 2861{
0ef6ce7b 2862 hda_nid_t nid, end_nid;
81937d3b
SL
2863 short seq, assoc_line_out, assoc_speaker;
2864 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2865 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
f889fa91 2866 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
e9edcee0
TI
2867
2868 memset(cfg, 0, sizeof(*cfg));
2869
81937d3b
SL
2870 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2871 memset(sequences_speaker, 0, sizeof(sequences_speaker));
f889fa91 2872 memset(sequences_hp, 0, sizeof(sequences_hp));
81937d3b 2873 assoc_line_out = assoc_speaker = 0;
e9edcee0 2874
0ef6ce7b
TI
2875 end_nid = codec->start_nid + codec->num_nodes;
2876 for (nid = codec->start_nid; nid < end_nid; nid++) {
54d17403 2877 unsigned int wid_caps = get_wcaps(codec, nid);
0ba21762
TI
2878 unsigned int wid_type =
2879 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
e9edcee0
TI
2880 unsigned int def_conf;
2881 short assoc, loc;
2882
2883 /* read all default configuration for pin complex */
2884 if (wid_type != AC_WID_PIN)
2885 continue;
df694daa
KY
2886 /* ignore the given nids (e.g. pc-beep returns error) */
2887 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2888 continue;
2889
0ba21762
TI
2890 def_conf = snd_hda_codec_read(codec, nid, 0,
2891 AC_VERB_GET_CONFIG_DEFAULT, 0);
e9edcee0
TI
2892 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2893 continue;
2894 loc = get_defcfg_location(def_conf);
2895 switch (get_defcfg_device(def_conf)) {
2896 case AC_JACK_LINE_OUT:
e9edcee0
TI
2897 seq = get_defcfg_sequence(def_conf);
2898 assoc = get_defcfg_association(def_conf);
90da78bf
MR
2899
2900 if (!(wid_caps & AC_WCAP_STEREO))
2901 if (!cfg->mono_out_pin)
2902 cfg->mono_out_pin = nid;
0ba21762 2903 if (!assoc)
e9edcee0 2904 continue;
0ba21762 2905 if (!assoc_line_out)
e9edcee0
TI
2906 assoc_line_out = assoc;
2907 else if (assoc_line_out != assoc)
2908 continue;
2909 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2910 continue;
2911 cfg->line_out_pins[cfg->line_outs] = nid;
81937d3b 2912 sequences_line_out[cfg->line_outs] = seq;
e9edcee0
TI
2913 cfg->line_outs++;
2914 break;
8d88bc3d 2915 case AC_JACK_SPEAKER:
81937d3b
SL
2916 seq = get_defcfg_sequence(def_conf);
2917 assoc = get_defcfg_association(def_conf);
2918 if (! assoc)
2919 continue;
2920 if (! assoc_speaker)
2921 assoc_speaker = assoc;
2922 else if (assoc_speaker != assoc)
2923 continue;
82bc955f
TI
2924 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2925 continue;
2926 cfg->speaker_pins[cfg->speaker_outs] = nid;
81937d3b 2927 sequences_speaker[cfg->speaker_outs] = seq;
82bc955f 2928 cfg->speaker_outs++;
8d88bc3d 2929 break;
e9edcee0 2930 case AC_JACK_HP_OUT:
f889fa91
TI
2931 seq = get_defcfg_sequence(def_conf);
2932 assoc = get_defcfg_association(def_conf);
eb06ed8f
TI
2933 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2934 continue;
2935 cfg->hp_pins[cfg->hp_outs] = nid;
f889fa91 2936 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
eb06ed8f 2937 cfg->hp_outs++;
e9edcee0 2938 break;
314634bc
TI
2939 case AC_JACK_MIC_IN: {
2940 int preferred, alt;
2941 if (loc == AC_JACK_LOC_FRONT) {
2942 preferred = AUTO_PIN_FRONT_MIC;
2943 alt = AUTO_PIN_MIC;
2944 } else {
2945 preferred = AUTO_PIN_MIC;
2946 alt = AUTO_PIN_FRONT_MIC;
2947 }
2948 if (!cfg->input_pins[preferred])
2949 cfg->input_pins[preferred] = nid;
2950 else if (!cfg->input_pins[alt])
2951 cfg->input_pins[alt] = nid;
e9edcee0 2952 break;
314634bc 2953 }
e9edcee0
TI
2954 case AC_JACK_LINE_IN:
2955 if (loc == AC_JACK_LOC_FRONT)
2956 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2957 else
2958 cfg->input_pins[AUTO_PIN_LINE] = nid;
2959 break;
2960 case AC_JACK_CD:
2961 cfg->input_pins[AUTO_PIN_CD] = nid;
2962 break;
2963 case AC_JACK_AUX:
2964 cfg->input_pins[AUTO_PIN_AUX] = nid;
2965 break;
2966 case AC_JACK_SPDIF_OUT:
2967 cfg->dig_out_pin = nid;
2968 break;
2969 case AC_JACK_SPDIF_IN:
2970 cfg->dig_in_pin = nid;
2971 break;
2972 }
2973 }
2974
5832fcf8
TI
2975 /* FIX-UP:
2976 * If no line-out is defined but multiple HPs are found,
2977 * some of them might be the real line-outs.
2978 */
2979 if (!cfg->line_outs && cfg->hp_outs > 1) {
2980 int i = 0;
2981 while (i < cfg->hp_outs) {
2982 /* The real HPs should have the sequence 0x0f */
2983 if ((sequences_hp[i] & 0x0f) == 0x0f) {
2984 i++;
2985 continue;
2986 }
2987 /* Move it to the line-out table */
2988 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
2989 sequences_line_out[cfg->line_outs] = sequences_hp[i];
2990 cfg->line_outs++;
2991 cfg->hp_outs--;
2992 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
2993 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
2994 memmove(sequences_hp + i - 1, sequences_hp + i,
2995 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
2996 }
2997 }
2998
e9edcee0 2999 /* sort by sequence */
81937d3b
SL
3000 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3001 cfg->line_outs);
3002 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3003 cfg->speaker_outs);
f889fa91
TI
3004 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3005 cfg->hp_outs);
81937d3b 3006
f889fa91
TI
3007 /* if we have only one mic, make it AUTO_PIN_MIC */
3008 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3009 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3010 cfg->input_pins[AUTO_PIN_MIC] =
3011 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3012 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3013 }
3014 /* ditto for line-in */
3015 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3016 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3017 cfg->input_pins[AUTO_PIN_LINE] =
3018 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3019 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3020 }
3021
81937d3b
SL
3022 /*
3023 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3024 * as a primary output
3025 */
3026 if (!cfg->line_outs) {
3027 if (cfg->speaker_outs) {
3028 cfg->line_outs = cfg->speaker_outs;
3029 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3030 sizeof(cfg->speaker_pins));
3031 cfg->speaker_outs = 0;
3032 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3033 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3034 } else if (cfg->hp_outs) {
3035 cfg->line_outs = cfg->hp_outs;
3036 memcpy(cfg->line_out_pins, cfg->hp_pins,
3037 sizeof(cfg->hp_pins));
3038 cfg->hp_outs = 0;
3039 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3040 cfg->line_out_type = AUTO_PIN_HP_OUT;
3041 }
3042 }
e9edcee0 3043
cb8e2f83
TI
3044 /* Reorder the surround channels
3045 * ALSA sequence is front/surr/clfe/side
3046 * HDA sequence is:
3047 * 4-ch: front/surr => OK as it is
3048 * 6-ch: front/clfe/surr
9422db40 3049 * 8-ch: front/clfe/rear/side|fc
cb8e2f83
TI
3050 */
3051 switch (cfg->line_outs) {
3052 case 3:
cb8e2f83
TI
3053 case 4:
3054 nid = cfg->line_out_pins[1];
9422db40 3055 cfg->line_out_pins[1] = cfg->line_out_pins[2];
cb8e2f83
TI
3056 cfg->line_out_pins[2] = nid;
3057 break;
e9edcee0
TI
3058 }
3059
82bc955f
TI
3060 /*
3061 * debug prints of the parsed results
3062 */
3063 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3064 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3065 cfg->line_out_pins[2], cfg->line_out_pins[3],
3066 cfg->line_out_pins[4]);
3067 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3068 cfg->speaker_outs, cfg->speaker_pins[0],
3069 cfg->speaker_pins[1], cfg->speaker_pins[2],
3070 cfg->speaker_pins[3], cfg->speaker_pins[4]);
eb06ed8f
TI
3071 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3072 cfg->hp_outs, cfg->hp_pins[0],
3073 cfg->hp_pins[1], cfg->hp_pins[2],
3074 cfg->hp_pins[3], cfg->hp_pins[4]);
90da78bf 3075 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
82bc955f
TI
3076 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3077 " cd=0x%x, aux=0x%x\n",
3078 cfg->input_pins[AUTO_PIN_MIC],
3079 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3080 cfg->input_pins[AUTO_PIN_LINE],
3081 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3082 cfg->input_pins[AUTO_PIN_CD],
3083 cfg->input_pins[AUTO_PIN_AUX]);
3084
e9edcee0
TI
3085 return 0;
3086}
3087
4a471b7d
TI
3088/* labels for input pins */
3089const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3090 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3091};
3092
3093
1da177e4
LT
3094#ifdef CONFIG_PM
3095/*
3096 * power management
3097 */
3098
3099/**
3100 * snd_hda_suspend - suspend the codecs
3101 * @bus: the HDA bus
3102 * @state: suspsend state
3103 *
3104 * Returns 0 if successful.
3105 */
3106int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3107{
0ba21762 3108 struct hda_codec *codec;
1da177e4 3109
0ba21762 3110 list_for_each_entry(codec, &bus->codec_list, list) {
0b7a2e9c
TI
3111#ifdef CONFIG_SND_HDA_POWER_SAVE
3112 if (!codec->power_on)
3113 continue;
3114#endif
cb53c626 3115 hda_call_codec_suspend(codec);
1da177e4
LT
3116 }
3117 return 0;
3118}
3119
3120/**
3121 * snd_hda_resume - resume the codecs
3122 * @bus: the HDA bus
3123 * @state: resume state
3124 *
3125 * Returns 0 if successful.
cb53c626
TI
3126 *
3127 * This fucntion is defined only when POWER_SAVE isn't set.
3128 * In the power-save mode, the codec is resumed dynamically.
1da177e4
LT
3129 */
3130int snd_hda_resume(struct hda_bus *bus)
3131{
0ba21762 3132 struct hda_codec *codec;
1da177e4 3133
0ba21762 3134 list_for_each_entry(codec, &bus->codec_list, list) {
d804ad92
ML
3135 if (snd_hda_codec_needs_resume(codec))
3136 hda_call_codec_resume(codec);
1da177e4 3137 }
1da177e4
LT
3138 return 0;
3139}
d804ad92
ML
3140#ifdef CONFIG_SND_HDA_POWER_SAVE
3141int snd_hda_codecs_inuse(struct hda_bus *bus)
3142{
3143 struct hda_codec *codec;
1da177e4 3144
d804ad92
ML
3145 list_for_each_entry(codec, &bus->codec_list, list) {
3146 if (snd_hda_codec_needs_resume(codec))
3147 return 1;
3148 }
3149 return 0;
3150}
3151#endif
1da177e4 3152#endif
This page took 0.923659 seconds and 5 git commands to generate.