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