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