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