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