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