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