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