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