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