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