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