ALSA: hda - Remove shutup calls in free callbacks
[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
18478e8b 22#include <linux/mm.h>
1da177e4
LT
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
62932df8 27#include <linux/mutex.h>
da155d5b 28#include <linux/module.h>
1da177e4
LT
29#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
302e9c5a 32#include <sound/tlv.h>
1da177e4 33#include <sound/initval.h>
cd372fb3 34#include <sound/jack.h>
1da177e4 35#include "hda_local.h"
123c07ae 36#include "hda_beep.h"
1835a0f9 37#include "hda_jack.h"
2807314d 38#include <sound/hda_hwdep.h>
1da177e4 39
d66fee5d
TI
40#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
1da177e4
LT
43/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
c8cd1281 54 { 0x1002, "ATI" },
e5f14248 55 { 0x1013, "Cirrus Logic" },
a9226251 56 { 0x1057, "Motorola" },
c8cd1281 57 { 0x1095, "Silicon Image" },
31117b78 58 { 0x10de, "Nvidia" },
c8cd1281 59 { 0x10ec, "Realtek" },
4e01f54b 60 { 0x1102, "Creative" },
c577b8a1 61 { 0x1106, "VIA" },
7f16859a 62 { 0x111d, "IDT" },
c8cd1281 63 { 0x11c1, "LSI" },
54b903ec 64 { 0x11d4, "Analog Devices" },
1da177e4 65 { 0x13f6, "C-Media" },
a9226251 66 { 0x14f1, "Conexant" },
c8cd1281
TI
67 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
8199de3b 69 { 0x1aec, "Wolfson Microelectronics" },
1da177e4 70 { 0x434d, "C-Media" },
74c61133 71 { 0x8086, "Intel" },
2f2f4251 72 { 0x8384, "SigmaTel" },
1da177e4
LT
73 {} /* terminator */
74};
75
1289e9e8
TI
76static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
ff7a3267 86EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
1289e9e8
TI
87
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
ff7a3267 95EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
1da177e4 96
83012a7c 97#ifdef CONFIG_PM
cb53c626
TI
98static void hda_power_work(struct work_struct *work);
99static void hda_keep_power_on(struct hda_codec *codec);
e581f3db 100#define hda_codec_is_power_on(codec) ((codec)->power_on)
68467f51
TI
101static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
102{
103 if (bus->ops.pm_notify)
104 bus->ops.pm_notify(bus, power_up);
105}
cb53c626
TI
106#else
107static inline void hda_keep_power_on(struct hda_codec *codec) {}
e581f3db 108#define hda_codec_is_power_on(codec) 1
68467f51 109#define hda_call_pm_notify(bus, state) {}
cb53c626
TI
110#endif
111
d5191e50
TI
112/**
113 * snd_hda_get_jack_location - Give a location string of the jack
114 * @cfg: pin default config value
115 *
116 * Parse the pin default config value and returns the string of the
117 * jack location, e.g. "Rear", "Front", etc.
118 */
50a9f790
MR
119const char *snd_hda_get_jack_location(u32 cfg)
120{
121 static char *bases[7] = {
122 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
123 };
124 static unsigned char specials_idx[] = {
125 0x07, 0x08,
126 0x17, 0x18, 0x19,
127 0x37, 0x38
128 };
129 static char *specials[] = {
130 "Rear Panel", "Drive Bar",
131 "Riser", "HDMI", "ATAPI",
132 "Mobile-In", "Mobile-Out"
133 };
134 int i;
135 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
136 if ((cfg & 0x0f) < 7)
137 return bases[cfg & 0x0f];
138 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
139 if (cfg == specials_idx[i])
140 return specials[i];
141 }
142 return "UNKNOWN";
143}
ff7a3267 144EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
50a9f790 145
d5191e50
TI
146/**
147 * snd_hda_get_jack_connectivity - Give a connectivity 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 connectivity, i.e. external or internal connection.
152 */
50a9f790
MR
153const char *snd_hda_get_jack_connectivity(u32 cfg)
154{
155 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
156
157 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
158}
ff7a3267 159EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
50a9f790 160
d5191e50
TI
161/**
162 * snd_hda_get_jack_type - Give a type string of the jack
163 * @cfg: pin default config value
164 *
165 * Parse the pin default config value and returns the string of the
166 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
167 */
50a9f790
MR
168const char *snd_hda_get_jack_type(u32 cfg)
169{
170 static char *jack_types[16] = {
171 "Line Out", "Speaker", "HP Out", "CD",
172 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
173 "Line In", "Aux", "Mic", "Telephony",
174 "SPDIF In", "Digitial In", "Reserved", "Other"
175 };
176
177 return jack_types[(cfg & AC_DEFCFG_DEVICE)
178 >> AC_DEFCFG_DEVICE_SHIFT];
179}
ff7a3267 180EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
50a9f790 181
33fa35ed
TI
182/*
183 * Compose a 32bit command word to be sent to the HD-audio controller
184 */
185static inline unsigned int
186make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
187 unsigned int verb, unsigned int parm)
188{
189 u32 val;
190
82e1b804
TI
191 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
192 (verb & ~0xfff) || (parm & ~0xffff)) {
6430aeeb
WF
193 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
194 codec->addr, direct, nid, verb, parm);
195 return ~0;
196 }
197
198 val = (u32)codec->addr << 28;
33fa35ed
TI
199 val |= (u32)direct << 27;
200 val |= (u32)nid << 20;
201 val |= verb << 8;
202 val |= parm;
203 return val;
204}
205
aa2936f5
TI
206/*
207 * Send and receive a verb
208 */
209static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
210 unsigned int *res)
211{
212 struct hda_bus *bus = codec->bus;
8dd78330 213 int err;
aa2936f5 214
6430aeeb
WF
215 if (cmd == ~0)
216 return -1;
217
aa2936f5
TI
218 if (res)
219 *res = -1;
8dd78330 220 again:
aa2936f5
TI
221 snd_hda_power_up(codec);
222 mutex_lock(&bus->cmd_mutex);
d66fee5d 223 trace_hda_send_cmd(codec, cmd);
aa2936f5 224 err = bus->ops.command(bus, cmd);
d66fee5d 225 if (!err && res) {
deadff16 226 *res = bus->ops.get_response(bus, codec->addr);
d66fee5d
TI
227 trace_hda_get_response(codec, *res);
228 }
aa2936f5
TI
229 mutex_unlock(&bus->cmd_mutex);
230 snd_hda_power_down(codec);
989c3187 231 if (!codec->in_pm && res && *res == -1 && bus->rirb_error) {
8dd78330
TI
232 if (bus->response_reset) {
233 snd_printd("hda_codec: resetting BUS due to "
234 "fatal communication error\n");
d66fee5d 235 trace_hda_bus_reset(bus);
8dd78330
TI
236 bus->ops.bus_reset(bus);
237 }
238 goto again;
239 }
240 /* clear reset-flag when the communication gets recovered */
989c3187 241 if (!err || codec->in_pm)
8dd78330 242 bus->response_reset = 0;
aa2936f5
TI
243 return err;
244}
245
1da177e4
LT
246/**
247 * snd_hda_codec_read - send a command and get the response
248 * @codec: the HDA codec
249 * @nid: NID to send the command
250 * @direct: direct flag
251 * @verb: the verb to send
252 * @parm: the parameter for the verb
253 *
254 * Send a single command and read the corresponding response.
255 *
256 * Returns the obtained response value, or -1 for an error.
257 */
0ba21762
TI
258unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
259 int direct,
1da177e4
LT
260 unsigned int verb, unsigned int parm)
261{
aa2936f5
TI
262 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
263 unsigned int res;
9857edfd
GT
264 if (codec_exec_verb(codec, cmd, &res))
265 return -1;
1da177e4
LT
266 return res;
267}
ff7a3267 268EXPORT_SYMBOL_HDA(snd_hda_codec_read);
1da177e4
LT
269
270/**
271 * snd_hda_codec_write - send a single command without waiting for response
272 * @codec: the HDA codec
273 * @nid: NID to send the command
274 * @direct: direct flag
275 * @verb: the verb to send
276 * @parm: the parameter for the verb
277 *
278 * Send a single command without waiting for response.
279 *
280 * Returns 0 if successful, or a negative error code.
281 */
282int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
283 unsigned int verb, unsigned int parm)
284{
aa2936f5 285 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
33fa35ed 286 unsigned int res;
b20f3b83
TI
287 return codec_exec_verb(codec, cmd,
288 codec->bus->sync_write ? &res : NULL);
1da177e4 289}
ff7a3267 290EXPORT_SYMBOL_HDA(snd_hda_codec_write);
1da177e4
LT
291
292/**
293 * snd_hda_sequence_write - sequence writes
294 * @codec: the HDA codec
295 * @seq: VERB array to send
296 *
297 * Send the commands sequentially from the given array.
298 * The array must be terminated with NID=0.
299 */
300void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
301{
302 for (; seq->nid; seq++)
303 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
304}
ff7a3267 305EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
1da177e4
LT
306
307/**
308 * snd_hda_get_sub_nodes - get the range of sub nodes
309 * @codec: the HDA codec
310 * @nid: NID to parse
311 * @start_id: the pointer to store the start NID
312 *
313 * Parse the NID and store the start NID of its sub-nodes.
314 * Returns the number of sub-nodes.
315 */
0ba21762
TI
316int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
317 hda_nid_t *start_id)
1da177e4
LT
318{
319 unsigned int parm;
320
321 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
e8a7f136
DT
322 if (parm == -1)
323 return 0;
1da177e4
LT
324 *start_id = (parm >> 16) & 0x7fff;
325 return (int)(parm & 0x7fff);
326}
ff7a3267 327EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
1da177e4 328
b2f934a0
TI
329/* look up the cached results */
330static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
331{
332 int i, len;
333 for (i = 0; i < array->used; ) {
334 hda_nid_t *p = snd_array_elem(array, i);
335 if (nid == *p)
336 return p;
337 len = p[1];
338 i += len + 2;
339 }
340 return NULL;
341}
a12d3e1e 342
09cf03b8
TI
343/* read the connection and add to the cache */
344static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
345{
346 hda_nid_t list[HDA_MAX_CONNECTIONS];
347 int len;
348
349 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
350 if (len < 0)
351 return len;
352 return snd_hda_override_conn_list(codec, nid, len, list);
353}
354
1da177e4 355/**
09cf03b8 356 * snd_hda_get_connections - copy connection list
1da177e4
LT
357 * @codec: the HDA codec
358 * @nid: NID to parse
09cf03b8
TI
359 * @conn_list: connection list array; when NULL, checks only the size
360 * @max_conns: max. number of connections to store
1da177e4
LT
361 *
362 * Parses the connection list of the given widget and stores the list
363 * of NIDs.
364 *
365 * Returns the number of connections, or a negative error code.
366 */
09cf03b8
TI
367int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
368 hda_nid_t *conn_list, int max_conns)
a12d3e1e
TI
369{
370 struct snd_array *array = &codec->conn_lists;
09cf03b8 371 int len;
dce2079b 372 hda_nid_t *p;
b2f934a0 373 bool added = false;
a12d3e1e 374
b2f934a0 375 again:
09cf03b8
TI
376 mutex_lock(&codec->hash_mutex);
377 len = -1;
b2f934a0
TI
378 /* if the connection-list is already cached, read it */
379 p = lookup_conn_list(array, nid);
380 if (p) {
09cf03b8
TI
381 len = p[1];
382 if (conn_list && len > max_conns) {
383 snd_printk(KERN_ERR "hda_codec: "
384 "Too many connections %d for NID 0x%x\n",
385 len, nid);
386 mutex_unlock(&codec->hash_mutex);
387 return -EINVAL;
388 }
389 if (conn_list && len)
390 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
a12d3e1e 391 }
09cf03b8
TI
392 mutex_unlock(&codec->hash_mutex);
393 if (len >= 0)
394 return len;
b2f934a0
TI
395 if (snd_BUG_ON(added))
396 return -EINVAL;
a12d3e1e 397
09cf03b8 398 len = read_and_add_raw_conns(codec, nid);
a12d3e1e
TI
399 if (len < 0)
400 return len;
b2f934a0
TI
401 added = true;
402 goto again;
a12d3e1e
TI
403}
404EXPORT_SYMBOL_HDA(snd_hda_get_connections);
405
9e7717c9
TI
406/**
407 * snd_hda_get_raw_connections - copy connection list without cache
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @conn_list: connection list array
411 * @max_conns: max. number of connections to store
412 *
413 * Like snd_hda_get_connections(), copy the connection list but without
414 * checking through the connection-list cache.
415 * Currently called only from hda_proc.c, so not exported.
416 */
417int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
418 hda_nid_t *conn_list, int max_conns)
1da177e4
LT
419{
420 unsigned int parm;
54d17403 421 int i, conn_len, conns;
1da177e4 422 unsigned int shift, num_elems, mask;
1ba7a7c6 423 unsigned int wcaps;
54d17403 424 hda_nid_t prev_nid;
1da177e4 425
da3cec35
TI
426 if (snd_BUG_ON(!conn_list || max_conns <= 0))
427 return -EINVAL;
1da177e4 428
1ba7a7c6
TI
429 wcaps = get_wcaps(codec, nid);
430 if (!(wcaps & AC_WCAP_CONN_LIST) &&
8d087c76
TI
431 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
432 return 0;
16a433d8 433
1da177e4
LT
434 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
435 if (parm & AC_CLIST_LONG) {
436 /* long form */
437 shift = 16;
438 num_elems = 2;
439 } else {
440 /* short form */
441 shift = 8;
442 num_elems = 4;
443 }
444 conn_len = parm & AC_CLIST_LENGTH;
1da177e4
LT
445 mask = (1 << (shift-1)) - 1;
446
0ba21762 447 if (!conn_len)
1da177e4
LT
448 return 0; /* no connection */
449
450 if (conn_len == 1) {
451 /* single connection */
0ba21762
TI
452 parm = snd_hda_codec_read(codec, nid, 0,
453 AC_VERB_GET_CONNECT_LIST, 0);
3c6aae44
TI
454 if (parm == -1 && codec->bus->rirb_error)
455 return -EIO;
1da177e4
LT
456 conn_list[0] = parm & mask;
457 return 1;
458 }
459
460 /* multi connection */
461 conns = 0;
54d17403
TI
462 prev_nid = 0;
463 for (i = 0; i < conn_len; i++) {
464 int range_val;
465 hda_nid_t val, n;
466
3c6aae44 467 if (i % num_elems == 0) {
54d17403
TI
468 parm = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONNECT_LIST, i);
3c6aae44
TI
470 if (parm == -1 && codec->bus->rirb_error)
471 return -EIO;
472 }
0ba21762 473 range_val = !!(parm & (1 << (shift-1))); /* ranges */
54d17403 474 val = parm & mask;
2e9bf247
JK
475 if (val == 0) {
476 snd_printk(KERN_WARNING "hda_codec: "
477 "invalid CONNECT_LIST verb %x[%i]:%x\n",
478 nid, i, parm);
479 return 0;
480 }
54d17403
TI
481 parm >>= shift;
482 if (range_val) {
483 /* ranges between the previous and this one */
0ba21762
TI
484 if (!prev_nid || prev_nid >= val) {
485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid dep_range_val %x:%x\n",
487 prev_nid, val);
54d17403
TI
488 continue;
489 }
490 for (n = prev_nid + 1; n <= val; n++) {
491 if (conns >= max_conns) {
5aacc218
TI
492 snd_printk(KERN_ERR "hda_codec: "
493 "Too many connections %d for NID 0x%x\n",
494 conns, nid);
1da177e4 495 return -EINVAL;
54d17403
TI
496 }
497 conn_list[conns++] = n;
1da177e4 498 }
54d17403
TI
499 } else {
500 if (conns >= max_conns) {
5aacc218
TI
501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
503 conns, nid);
54d17403
TI
504 return -EINVAL;
505 }
506 conn_list[conns++] = val;
1da177e4 507 }
54d17403 508 prev_nid = val;
1da177e4
LT
509 }
510 return conns;
511}
512
a12d3e1e
TI
513static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
514{
515 hda_nid_t *p = snd_array_new(array);
516 if (!p)
517 return false;
518 *p = nid;
519 return true;
520}
521
b2f934a0
TI
522/**
523 * snd_hda_override_conn_list - add/modify the connection-list to cache
524 * @codec: the HDA codec
525 * @nid: NID to parse
526 * @len: number of connection list entries
527 * @list: the list of connection entries
528 *
529 * Add or modify the given connection-list to the cache. If the corresponding
530 * cache already exists, invalidate it and append a new one.
531 *
532 * Returns zero or a negative error code.
533 */
534int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
535 const hda_nid_t *list)
536{
537 struct snd_array *array = &codec->conn_lists;
538 hda_nid_t *p;
539 int i, old_used;
540
09cf03b8 541 mutex_lock(&codec->hash_mutex);
b2f934a0
TI
542 p = lookup_conn_list(array, nid);
543 if (p)
544 *p = -1; /* invalidate the old entry */
545
546 old_used = array->used;
547 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
548 goto error_add;
549 for (i = 0; i < len; i++)
550 if (!add_conn_list(array, list[i]))
551 goto error_add;
09cf03b8 552 mutex_unlock(&codec->hash_mutex);
b2f934a0
TI
553 return 0;
554
555 error_add:
556 array->used = old_used;
09cf03b8 557 mutex_unlock(&codec->hash_mutex);
b2f934a0
TI
558 return -ENOMEM;
559}
560EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
561
8d087c76
TI
562/**
563 * snd_hda_get_conn_index - get the connection index of the given NID
564 * @codec: the HDA codec
565 * @mux: NID containing the list
566 * @nid: NID to select
567 * @recursive: 1 when searching NID recursively, otherwise 0
568 *
569 * Parses the connection list of the widget @mux and checks whether the
570 * widget @nid is present. If it is, return the connection index.
571 * Otherwise it returns -1.
572 */
573int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
574 hda_nid_t nid, int recursive)
a12d3e1e 575{
8d087c76
TI
576 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
577 int i, nums;
578
579 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
580 for (i = 0; i < nums; i++)
581 if (conn[i] == nid)
582 return i;
583 if (!recursive)
584 return -1;
585 if (recursive > 5) {
586 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
587 return -1;
a12d3e1e 588 }
8d087c76 589 recursive++;
99e14c9d
TI
590 for (i = 0; i < nums; i++) {
591 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
592 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
593 continue;
8d087c76
TI
594 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
595 return i;
99e14c9d 596 }
8d087c76 597 return -1;
a12d3e1e 598}
8d087c76 599EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
1da177e4
LT
600
601/**
602 * snd_hda_queue_unsol_event - add an unsolicited event to queue
603 * @bus: the BUS
604 * @res: unsolicited event (lower 32bit of RIRB entry)
605 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
606 *
607 * Adds the given event to the queue. The events are processed in
608 * the workqueue asynchronously. Call this function in the interrupt
609 * hanlder when RIRB receives an unsolicited event.
610 *
611 * Returns 0 if successful, or a negative error code.
612 */
613int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
614{
615 struct hda_bus_unsolicited *unsol;
616 unsigned int wp;
617
ecf726f5 618 trace_hda_unsol_event(bus, res, res_ex);
0ba21762
TI
619 unsol = bus->unsol;
620 if (!unsol)
1da177e4
LT
621 return 0;
622
623 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
624 unsol->wp = wp;
625
626 wp <<= 1;
627 unsol->queue[wp] = res;
628 unsol->queue[wp + 1] = res_ex;
629
6acaed38 630 queue_work(bus->workq, &unsol->work);
1da177e4
LT
631
632 return 0;
633}
ff7a3267 634EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
1da177e4
LT
635
636/*
5c1d1a98 637 * process queued unsolicited events
1da177e4 638 */
c4028958 639static void process_unsol_events(struct work_struct *work)
1da177e4 640{
c4028958
DH
641 struct hda_bus_unsolicited *unsol =
642 container_of(work, struct hda_bus_unsolicited, work);
643 struct hda_bus *bus = unsol->bus;
1da177e4
LT
644 struct hda_codec *codec;
645 unsigned int rp, caddr, res;
646
647 while (unsol->rp != unsol->wp) {
648 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
649 unsol->rp = rp;
650 rp <<= 1;
651 res = unsol->queue[rp];
652 caddr = unsol->queue[rp + 1];
0ba21762 653 if (!(caddr & (1 << 4))) /* no unsolicited event? */
1da177e4
LT
654 continue;
655 codec = bus->caddr_tbl[caddr & 0x0f];
656 if (codec && codec->patch_ops.unsol_event)
657 codec->patch_ops.unsol_event(codec, res);
658 }
659}
660
661/*
662 * initialize unsolicited queue
663 */
6c1f45ea 664static int init_unsol_queue(struct hda_bus *bus)
1da177e4
LT
665{
666 struct hda_bus_unsolicited *unsol;
667
9f146bb6
TI
668 if (bus->unsol) /* already initialized */
669 return 0;
670
e560d8d8 671 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
0ba21762
TI
672 if (!unsol) {
673 snd_printk(KERN_ERR "hda_codec: "
674 "can't allocate unsolicited queue\n");
1da177e4
LT
675 return -ENOMEM;
676 }
c4028958
DH
677 INIT_WORK(&unsol->work, process_unsol_events);
678 unsol->bus = bus;
1da177e4
LT
679 bus->unsol = unsol;
680 return 0;
681}
682
683/*
684 * destructor
685 */
686static void snd_hda_codec_free(struct hda_codec *codec);
687
688static int snd_hda_bus_free(struct hda_bus *bus)
689{
0ba21762 690 struct hda_codec *codec, *n;
1da177e4 691
0ba21762 692 if (!bus)
1da177e4 693 return 0;
6acaed38
TI
694 if (bus->workq)
695 flush_workqueue(bus->workq);
696 if (bus->unsol)
1da177e4 697 kfree(bus->unsol);
0ba21762 698 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
1da177e4
LT
699 snd_hda_codec_free(codec);
700 }
701 if (bus->ops.private_free)
702 bus->ops.private_free(bus);
6acaed38
TI
703 if (bus->workq)
704 destroy_workqueue(bus->workq);
1da177e4
LT
705 kfree(bus);
706 return 0;
707}
708
c8b6bf9b 709static int snd_hda_bus_dev_free(struct snd_device *device)
1da177e4
LT
710{
711 struct hda_bus *bus = device->device_data;
b94d3539 712 bus->shutdown = 1;
1da177e4
LT
713 return snd_hda_bus_free(bus);
714}
715
d7ffba19
TI
716#ifdef CONFIG_SND_HDA_HWDEP
717static int snd_hda_bus_dev_register(struct snd_device *device)
718{
719 struct hda_bus *bus = device->device_data;
720 struct hda_codec *codec;
721 list_for_each_entry(codec, &bus->codec_list, list) {
722 snd_hda_hwdep_add_sysfs(codec);
a2f6309e 723 snd_hda_hwdep_add_power_sysfs(codec);
d7ffba19
TI
724 }
725 return 0;
726}
727#else
728#define snd_hda_bus_dev_register NULL
729#endif
730
1da177e4
LT
731/**
732 * snd_hda_bus_new - create a HDA bus
733 * @card: the card entry
734 * @temp: the template for hda_bus information
735 * @busp: the pointer to store the created bus instance
736 *
737 * Returns 0 if successful, or a negative error code.
738 */
1289e9e8 739int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
756e2b01
TI
740 const struct hda_bus_template *temp,
741 struct hda_bus **busp)
1da177e4
LT
742{
743 struct hda_bus *bus;
744 int err;
c8b6bf9b 745 static struct snd_device_ops dev_ops = {
d7ffba19 746 .dev_register = snd_hda_bus_dev_register,
1da177e4
LT
747 .dev_free = snd_hda_bus_dev_free,
748 };
749
da3cec35
TI
750 if (snd_BUG_ON(!temp))
751 return -EINVAL;
752 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
753 return -EINVAL;
1da177e4
LT
754
755 if (busp)
756 *busp = NULL;
757
e560d8d8 758 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
759 if (bus == NULL) {
760 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
761 return -ENOMEM;
762 }
763
764 bus->card = card;
765 bus->private_data = temp->private_data;
766 bus->pci = temp->pci;
767 bus->modelname = temp->modelname;
fee2fba3 768 bus->power_save = temp->power_save;
1da177e4
LT
769 bus->ops = temp->ops;
770
62932df8 771 mutex_init(&bus->cmd_mutex);
3f50ac6a 772 mutex_init(&bus->prepare_mutex);
1da177e4
LT
773 INIT_LIST_HEAD(&bus->codec_list);
774
e8c0ee5d
TI
775 snprintf(bus->workq_name, sizeof(bus->workq_name),
776 "hd-audio%d", card->number);
777 bus->workq = create_singlethread_workqueue(bus->workq_name);
6acaed38 778 if (!bus->workq) {
e8c0ee5d
TI
779 snd_printk(KERN_ERR "cannot create workqueue %s\n",
780 bus->workq_name);
6acaed38
TI
781 kfree(bus);
782 return -ENOMEM;
783 }
784
0ba21762
TI
785 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
786 if (err < 0) {
1da177e4
LT
787 snd_hda_bus_free(bus);
788 return err;
789 }
790 if (busp)
791 *busp = bus;
792 return 0;
793}
ff7a3267 794EXPORT_SYMBOL_HDA(snd_hda_bus_new);
1da177e4 795
82467611
TI
796#ifdef CONFIG_SND_HDA_GENERIC
797#define is_generic_config(codec) \
f44ac837 798 (codec->modelname && !strcmp(codec->modelname, "generic"))
82467611
TI
799#else
800#define is_generic_config(codec) 0
801#endif
802
645f10c1 803#ifdef MODULE
1289e9e8
TI
804#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
805#else
645f10c1 806#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
1289e9e8
TI
807#endif
808
1da177e4
LT
809/*
810 * find a matching codec preset
811 */
6c1f45ea 812static const struct hda_codec_preset *
756e2b01 813find_codec_preset(struct hda_codec *codec)
1da177e4 814{
1289e9e8
TI
815 struct hda_codec_preset_list *tbl;
816 const struct hda_codec_preset *preset;
5d908ab9 817 unsigned int mod_requested = 0;
1da177e4 818
82467611 819 if (is_generic_config(codec))
d5ad630b
TI
820 return NULL; /* use the generic parser */
821
1289e9e8
TI
822 again:
823 mutex_lock(&preset_mutex);
824 list_for_each_entry(tbl, &hda_preset_tables, list) {
825 if (!try_module_get(tbl->owner)) {
826 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
827 continue;
828 }
829 for (preset = tbl->preset; preset->id; preset++) {
1da177e4 830 u32 mask = preset->mask;
ca7cfae9
MB
831 if (preset->afg && preset->afg != codec->afg)
832 continue;
833 if (preset->mfg && preset->mfg != codec->mfg)
834 continue;
0ba21762 835 if (!mask)
1da177e4 836 mask = ~0;
9c7f852e 837 if (preset->id == (codec->vendor_id & mask) &&
0ba21762 838 (!preset->rev ||
1289e9e8
TI
839 preset->rev == codec->revision_id)) {
840 mutex_unlock(&preset_mutex);
841 codec->owner = tbl->owner;
1da177e4 842 return preset;
1289e9e8 843 }
1da177e4 844 }
1289e9e8
TI
845 module_put(tbl->owner);
846 }
847 mutex_unlock(&preset_mutex);
848
849 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
850 char name[32];
851 if (!mod_requested)
852 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
853 codec->vendor_id);
854 else
855 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
856 (codec->vendor_id >> 16) & 0xffff);
857 request_module(name);
858 mod_requested++;
859 goto again;
1da177e4
LT
860 }
861 return NULL;
862}
863
864/*
f44ac837 865 * get_codec_name - store the codec name
1da177e4 866 */
f44ac837 867static int get_codec_name(struct hda_codec *codec)
1da177e4
LT
868{
869 const struct hda_vendor_id *c;
870 const char *vendor = NULL;
871 u16 vendor_id = codec->vendor_id >> 16;
812a2cca
TI
872 char tmp[16];
873
874 if (codec->vendor_name)
875 goto get_chip_name;
1da177e4
LT
876
877 for (c = hda_vendor_ids; c->id; c++) {
878 if (c->id == vendor_id) {
879 vendor = c->name;
880 break;
881 }
882 }
0ba21762 883 if (!vendor) {
1da177e4
LT
884 sprintf(tmp, "Generic %04x", vendor_id);
885 vendor = tmp;
886 }
812a2cca
TI
887 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
888 if (!codec->vendor_name)
889 return -ENOMEM;
890
891 get_chip_name:
892 if (codec->chip_name)
893 return 0;
894
1da177e4 895 if (codec->preset && codec->preset->name)
812a2cca
TI
896 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
897 else {
898 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
899 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
900 }
901 if (!codec->chip_name)
f44ac837
TI
902 return -ENOMEM;
903 return 0;
1da177e4
LT
904}
905
906/*
673b683a 907 * look for an AFG and MFG nodes
1da177e4 908 */
1289e9e8 909static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
1da177e4 910{
93e82ae7 911 int i, total_nodes, function_id;
1da177e4
LT
912 hda_nid_t nid;
913
914 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
915 for (i = 0; i < total_nodes; i++, nid++) {
93e82ae7 916 function_id = snd_hda_param_read(codec, nid,
79c944ad 917 AC_PAR_FUNCTION_TYPE);
cd7643bf 918 switch (function_id & 0xff) {
673b683a
SK
919 case AC_GRP_AUDIO_FUNCTION:
920 codec->afg = nid;
79c944ad
JK
921 codec->afg_function_id = function_id & 0xff;
922 codec->afg_unsol = (function_id >> 8) & 1;
673b683a
SK
923 break;
924 case AC_GRP_MODEM_FUNCTION:
925 codec->mfg = nid;
79c944ad
JK
926 codec->mfg_function_id = function_id & 0xff;
927 codec->mfg_unsol = (function_id >> 8) & 1;
673b683a
SK
928 break;
929 default:
930 break;
931 }
1da177e4 932 }
1da177e4
LT
933}
934
54d17403
TI
935/*
936 * read widget caps for each widget and store in cache
937 */
938static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
939{
940 int i;
941 hda_nid_t nid;
942
943 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
944 &codec->start_nid);
945 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
0ba21762 946 if (!codec->wcaps)
54d17403
TI
947 return -ENOMEM;
948 nid = codec->start_nid;
949 for (i = 0; i < codec->num_nodes; i++, nid++)
950 codec->wcaps[i] = snd_hda_param_read(codec, nid,
951 AC_PAR_AUDIO_WIDGET_CAP);
952 return 0;
953}
954
3be14149
TI
955/* read all pin default configurations and save codec->init_pins */
956static int read_pin_defaults(struct hda_codec *codec)
957{
958 int i;
959 hda_nid_t nid = codec->start_nid;
960
961 for (i = 0; i < codec->num_nodes; i++, nid++) {
962 struct hda_pincfg *pin;
963 unsigned int wcaps = get_wcaps(codec, nid);
a22d543a 964 unsigned int wid_type = get_wcaps_type(wcaps);
3be14149
TI
965 if (wid_type != AC_WID_PIN)
966 continue;
967 pin = snd_array_new(&codec->init_pins);
968 if (!pin)
969 return -ENOMEM;
970 pin->nid = nid;
971 pin->cfg = snd_hda_codec_read(codec, nid, 0,
972 AC_VERB_GET_CONFIG_DEFAULT, 0);
ac0547dc
TI
973 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_PIN_WIDGET_CONTROL,
975 0);
3be14149
TI
976 }
977 return 0;
978}
979
980/* look up the given pin config list and return the item matching with NID */
981static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
982 struct snd_array *array,
983 hda_nid_t nid)
984{
985 int i;
986 for (i = 0; i < array->used; i++) {
987 struct hda_pincfg *pin = snd_array_elem(array, i);
988 if (pin->nid == nid)
989 return pin;
990 }
991 return NULL;
992}
993
994/* write a config value for the given NID */
995static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
996 unsigned int cfg)
997{
998 int i;
999 for (i = 0; i < 4; i++) {
1000 snd_hda_codec_write(codec, nid, 0,
1001 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1002 cfg & 0xff);
1003 cfg >>= 8;
1004 }
1005}
1006
1007/* set the current pin config value for the given NID.
1008 * the value is cached, and read via snd_hda_codec_get_pincfg()
1009 */
1010int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1011 hda_nid_t nid, unsigned int cfg)
1012{
1013 struct hda_pincfg *pin;
5e7b8e0d 1014 unsigned int oldcfg;
3be14149 1015
b82855a0
TI
1016 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1017 return -EINVAL;
1018
5e7b8e0d 1019 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
3be14149
TI
1020 pin = look_up_pincfg(codec, list, nid);
1021 if (!pin) {
1022 pin = snd_array_new(list);
1023 if (!pin)
1024 return -ENOMEM;
1025 pin->nid = nid;
1026 }
1027 pin->cfg = cfg;
5e7b8e0d
TI
1028
1029 /* change only when needed; e.g. if the pincfg is already present
1030 * in user_pins[], don't write it
1031 */
1032 cfg = snd_hda_codec_get_pincfg(codec, nid);
1033 if (oldcfg != cfg)
1034 set_pincfg(codec, nid, cfg);
3be14149
TI
1035 return 0;
1036}
1037
d5191e50
TI
1038/**
1039 * snd_hda_codec_set_pincfg - Override a pin default configuration
1040 * @codec: the HDA codec
1041 * @nid: NID to set the pin config
1042 * @cfg: the pin default config value
1043 *
1044 * Override a pin default configuration value in the cache.
1045 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1046 * priority than the real hardware value.
1047 */
3be14149
TI
1048int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1049 hda_nid_t nid, unsigned int cfg)
1050{
346ff70f 1051 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
3be14149
TI
1052}
1053EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1054
d5191e50
TI
1055/**
1056 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1057 * @codec: the HDA codec
1058 * @nid: NID to get the pin config
1059 *
1060 * Get the current pin config value of the given pin NID.
1061 * If the pincfg value is cached or overridden via sysfs or driver,
1062 * returns the cached value.
1063 */
3be14149
TI
1064unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1065{
1066 struct hda_pincfg *pin;
1067
3be14149 1068#ifdef CONFIG_SND_HDA_HWDEP
346ff70f 1069 pin = look_up_pincfg(codec, &codec->user_pins, nid);
3be14149
TI
1070 if (pin)
1071 return pin->cfg;
1072#endif
5e7b8e0d
TI
1073 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1074 if (pin)
1075 return pin->cfg;
3be14149
TI
1076 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1077 if (pin)
1078 return pin->cfg;
1079 return 0;
1080}
1081EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1082
1083/* restore all current pin configs */
1084static void restore_pincfgs(struct hda_codec *codec)
1085{
1086 int i;
1087 for (i = 0; i < codec->init_pins.used; i++) {
1088 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1089 set_pincfg(codec, pin->nid,
1090 snd_hda_codec_get_pincfg(codec, pin->nid));
1091 }
1092}
54d17403 1093
92ee6162
TI
1094/**
1095 * snd_hda_shutup_pins - Shut up all pins
1096 * @codec: the HDA codec
1097 *
1098 * Clear all pin controls to shup up before suspend for avoiding click noise.
1099 * The controls aren't cached so that they can be resumed properly.
1100 */
1101void snd_hda_shutup_pins(struct hda_codec *codec)
1102{
1103 int i;
ac0547dc
TI
1104 /* don't shut up pins when unloading the driver; otherwise it breaks
1105 * the default pin setup at the next load of the driver
1106 */
1107 if (codec->bus->shutdown)
1108 return;
92ee6162
TI
1109 for (i = 0; i < codec->init_pins.used; i++) {
1110 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1111 /* use read here for syncing after issuing each verb */
1112 snd_hda_codec_read(codec, pin->nid, 0,
1113 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1114 }
ac0547dc 1115 codec->pins_shutup = 1;
92ee6162
TI
1116}
1117EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1118
2a43952a 1119#ifdef CONFIG_PM
ac0547dc
TI
1120/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1121static void restore_shutup_pins(struct hda_codec *codec)
1122{
1123 int i;
1124 if (!codec->pins_shutup)
1125 return;
1126 if (codec->bus->shutdown)
1127 return;
1128 for (i = 0; i < codec->init_pins.used; i++) {
1129 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1130 snd_hda_codec_write(codec, pin->nid, 0,
1131 AC_VERB_SET_PIN_WIDGET_CONTROL,
1132 pin->ctrl);
1133 }
1134 codec->pins_shutup = 0;
1135}
1c7276cf 1136#endif
ac0547dc 1137
26a6cb6c
DH
1138static void hda_jackpoll_work(struct work_struct *work)
1139{
1140 struct hda_codec *codec =
1141 container_of(work, struct hda_codec, jackpoll_work.work);
1142 if (!codec->jackpoll_interval)
1143 return;
1144
1145 snd_hda_jack_set_dirty_all(codec);
1146 snd_hda_jack_poll_all(codec);
1147 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1148 codec->jackpoll_interval);
1149}
1150
01751f54
TI
1151static void init_hda_cache(struct hda_cache_rec *cache,
1152 unsigned int record_size);
1fcaee6e 1153static void free_hda_cache(struct hda_cache_rec *cache);
01751f54 1154
3be14149
TI
1155/* restore the initial pin cfgs and release all pincfg lists */
1156static void restore_init_pincfgs(struct hda_codec *codec)
1157{
346ff70f 1158 /* first free driver_pins and user_pins, then call restore_pincfg
3be14149
TI
1159 * so that only the values in init_pins are restored
1160 */
346ff70f 1161 snd_array_free(&codec->driver_pins);
3be14149 1162#ifdef CONFIG_SND_HDA_HWDEP
346ff70f 1163 snd_array_free(&codec->user_pins);
3be14149
TI
1164#endif
1165 restore_pincfgs(codec);
1166 snd_array_free(&codec->init_pins);
1167}
1168
eb541337
TI
1169/*
1170 * audio-converter setup caches
1171 */
1172struct hda_cvt_setup {
1173 hda_nid_t nid;
1174 u8 stream_tag;
1175 u8 channel_id;
1176 u16 format_id;
1177 unsigned char active; /* cvt is currently used */
1178 unsigned char dirty; /* setups should be cleared */
1179};
1180
1181/* get or create a cache entry for the given audio converter NID */
1182static struct hda_cvt_setup *
1183get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1184{
1185 struct hda_cvt_setup *p;
1186 int i;
1187
1188 for (i = 0; i < codec->cvt_setups.used; i++) {
1189 p = snd_array_elem(&codec->cvt_setups, i);
1190 if (p->nid == nid)
1191 return p;
1192 }
1193 p = snd_array_new(&codec->cvt_setups);
1194 if (p)
1195 p->nid = nid;
1196 return p;
1197}
1198
1da177e4
LT
1199/*
1200 * codec destructor
1201 */
1202static void snd_hda_codec_free(struct hda_codec *codec)
1203{
0ba21762 1204 if (!codec)
1da177e4 1205 return;
26a6cb6c 1206 cancel_delayed_work_sync(&codec->jackpoll_work);
59cad16b 1207 snd_hda_jack_tbl_clear(codec);
3be14149 1208 restore_init_pincfgs(codec);
83012a7c 1209#ifdef CONFIG_PM
cb53c626 1210 cancel_delayed_work(&codec->power_work);
6acaed38 1211 flush_workqueue(codec->bus->workq);
cb53c626 1212#endif
1da177e4 1213 list_del(&codec->list);
d13bd412 1214 snd_array_free(&codec->mixers);
5b0cb1d8 1215 snd_array_free(&codec->nids);
59cad16b 1216 snd_array_free(&codec->cvt_setups);
a12d3e1e 1217 snd_array_free(&codec->conn_lists);
7c935976 1218 snd_array_free(&codec->spdif_out);
1da177e4
LT
1219 codec->bus->caddr_tbl[codec->addr] = NULL;
1220 if (codec->patch_ops.free)
1221 codec->patch_ops.free(codec);
83012a7c 1222#ifdef CONFIG_PM
08fa20ae 1223 if (!codec->pm_down_notified) /* cancel leftover refcounts */
68467f51
TI
1224 hda_call_pm_notify(codec->bus, false);
1225#endif
1289e9e8 1226 module_put(codec->owner);
01751f54 1227 free_hda_cache(&codec->amp_cache);
b3ac5636 1228 free_hda_cache(&codec->cmd_cache);
812a2cca
TI
1229 kfree(codec->vendor_name);
1230 kfree(codec->chip_name);
f44ac837 1231 kfree(codec->modelname);
54d17403 1232 kfree(codec->wcaps);
1da177e4
LT
1233 kfree(codec);
1234}
1235
b8dfc462
ML
1236static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1237 hda_nid_t fg, unsigned int power_state);
1238
d819387e 1239static unsigned int hda_set_power_state(struct hda_codec *codec,
bb6ac72f
TI
1240 unsigned int power_state);
1241
1da177e4
LT
1242/**
1243 * snd_hda_codec_new - create a HDA codec
1244 * @bus: the bus to assign
1245 * @codec_addr: the codec address
1246 * @codecp: the pointer to store the generated codec
1247 *
1248 * Returns 0 if successful, or a negative error code.
1249 */
28aedaf7
NL
1250int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1251 unsigned int codec_addr,
1252 struct hda_codec **codecp)
1da177e4
LT
1253{
1254 struct hda_codec *codec;
ba443687 1255 char component[31];
d819387e 1256 hda_nid_t fg;
1da177e4
LT
1257 int err;
1258
da3cec35
TI
1259 if (snd_BUG_ON(!bus))
1260 return -EINVAL;
1261 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1262 return -EINVAL;
1da177e4
LT
1263
1264 if (bus->caddr_tbl[codec_addr]) {
0ba21762
TI
1265 snd_printk(KERN_ERR "hda_codec: "
1266 "address 0x%x is already occupied\n", codec_addr);
1da177e4
LT
1267 return -EBUSY;
1268 }
1269
e560d8d8 1270 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1da177e4
LT
1271 if (codec == NULL) {
1272 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1273 return -ENOMEM;
1274 }
1275
1276 codec->bus = bus;
1277 codec->addr = codec_addr;
62932df8 1278 mutex_init(&codec->spdif_mutex);
5a9e02e9 1279 mutex_init(&codec->control_mutex);
c3b6bcc2 1280 mutex_init(&codec->hash_mutex);
01751f54 1281 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
b3ac5636 1282 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
5b0cb1d8
JK
1283 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1284 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
3be14149 1285 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
346ff70f 1286 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
eb541337 1287 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
a12d3e1e 1288 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
7c935976 1289 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
26a6cb6c 1290 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1da177e4 1291
83012a7c 1292#ifdef CONFIG_PM
5536c6d6 1293 spin_lock_init(&codec->power_lock);
cb53c626
TI
1294 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1295 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1296 * the caller has to power down appropriatley after initialization
1297 * phase.
1298 */
1299 hda_keep_power_on(codec);
68467f51 1300 hda_call_pm_notify(bus, true);
cb53c626
TI
1301#endif
1302
c382a9f0
TI
1303 if (codec->bus->modelname) {
1304 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1305 if (!codec->modelname) {
1306 snd_hda_codec_free(codec);
1307 return -ENODEV;
1308 }
1309 }
1310
1da177e4
LT
1311 list_add_tail(&codec->list, &bus->codec_list);
1312 bus->caddr_tbl[codec_addr] = codec;
1313
0ba21762
TI
1314 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1315 AC_PAR_VENDOR_ID);
111d3af5
TI
1316 if (codec->vendor_id == -1)
1317 /* read again, hopefully the access method was corrected
1318 * in the last read...
1319 */
1320 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1321 AC_PAR_VENDOR_ID);
0ba21762
TI
1322 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1323 AC_PAR_SUBSYSTEM_ID);
1324 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1325 AC_PAR_REV_ID);
1da177e4 1326
673b683a 1327 setup_fg_nodes(codec);
0ba21762 1328 if (!codec->afg && !codec->mfg) {
673b683a 1329 snd_printdd("hda_codec: no AFG or MFG node found\n");
3be14149
TI
1330 err = -ENODEV;
1331 goto error;
1da177e4
LT
1332 }
1333
d819387e
TI
1334 fg = codec->afg ? codec->afg : codec->mfg;
1335 err = read_widget_caps(codec, fg);
3be14149 1336 if (err < 0) {
54d17403 1337 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
3be14149 1338 goto error;
54d17403 1339 }
3be14149
TI
1340 err = read_pin_defaults(codec);
1341 if (err < 0)
1342 goto error;
54d17403 1343
0ba21762 1344 if (!codec->subsystem_id) {
0ba21762 1345 codec->subsystem_id =
d819387e 1346 snd_hda_codec_read(codec, fg, 0,
0ba21762 1347 AC_VERB_GET_SUBSYSTEM_ID, 0);
86284e45
TI
1348 }
1349
83012a7c 1350#ifdef CONFIG_PM
d819387e 1351 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
b8dfc462
ML
1352 AC_PWRST_CLKSTOP);
1353 if (!codec->d3_stop_clk)
1354 bus->power_keep_link_on = 1;
5d6147f1 1355#endif
d819387e 1356 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
983f6b93 1357 AC_PWRST_EPSS);
b8dfc462 1358
bb6ac72f 1359 /* power-up all before initialization */
d819387e 1360 hda_set_power_state(codec, AC_PWRST_D0);
bb6ac72f 1361
6c1f45ea
TI
1362 snd_hda_codec_proc_new(codec);
1363
6c1f45ea 1364 snd_hda_create_hwdep(codec);
6c1f45ea
TI
1365
1366 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1367 codec->subsystem_id, codec->revision_id);
1368 snd_component_add(codec->bus->card, component);
1369
1370 if (codecp)
1371 *codecp = codec;
1372 return 0;
3be14149
TI
1373
1374 error:
1375 snd_hda_codec_free(codec);
1376 return err;
6c1f45ea 1377}
ff7a3267 1378EXPORT_SYMBOL_HDA(snd_hda_codec_new);
6c1f45ea 1379
d5191e50
TI
1380/**
1381 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1382 * @codec: the HDA codec
1383 *
1384 * Start parsing of the given codec tree and (re-)initialize the whole
1385 * patch instance.
1386 *
1387 * Returns 0 if successful or a negative error code.
1388 */
6c1f45ea
TI
1389int snd_hda_codec_configure(struct hda_codec *codec)
1390{
1391 int err;
1392
d5ad630b 1393 codec->preset = find_codec_preset(codec);
812a2cca 1394 if (!codec->vendor_name || !codec->chip_name) {
f44ac837
TI
1395 err = get_codec_name(codec);
1396 if (err < 0)
1397 return err;
1398 }
1da177e4 1399
82467611 1400 if (is_generic_config(codec)) {
1da177e4 1401 err = snd_hda_parse_generic_codec(codec);
82467611
TI
1402 goto patched;
1403 }
82467611
TI
1404 if (codec->preset && codec->preset->patch) {
1405 err = codec->preset->patch(codec);
1406 goto patched;
1407 }
1408
1409 /* call the default parser */
82467611 1410 err = snd_hda_parse_generic_codec(codec);
35a1e0cc
TI
1411 if (err < 0)
1412 printk(KERN_ERR "hda-codec: No codec parser is available\n");
82467611
TI
1413
1414 patched:
6c1f45ea
TI
1415 if (!err && codec->patch_ops.unsol_event)
1416 err = init_unsol_queue(codec->bus);
f62faedb
TI
1417 /* audio codec should override the mixer name */
1418 if (!err && (codec->afg || !*codec->bus->card->mixername))
1419 snprintf(codec->bus->card->mixername,
1420 sizeof(codec->bus->card->mixername),
1421 "%s %s", codec->vendor_name, codec->chip_name);
6c1f45ea 1422 return err;
1da177e4 1423}
a1e21c90 1424EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1da177e4 1425
ed360813
TI
1426/* update the stream-id if changed */
1427static void update_pcm_stream_id(struct hda_codec *codec,
1428 struct hda_cvt_setup *p, hda_nid_t nid,
1429 u32 stream_tag, int channel_id)
1430{
1431 unsigned int oldval, newval;
1432
1433 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1434 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1435 newval = (stream_tag << 4) | channel_id;
1436 if (oldval != newval)
1437 snd_hda_codec_write(codec, nid, 0,
1438 AC_VERB_SET_CHANNEL_STREAMID,
1439 newval);
1440 p->stream_tag = stream_tag;
1441 p->channel_id = channel_id;
1442 }
1443}
1444
1445/* update the format-id if changed */
1446static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1447 hda_nid_t nid, int format)
1448{
1449 unsigned int oldval;
1450
1451 if (p->format_id != format) {
1452 oldval = snd_hda_codec_read(codec, nid, 0,
1453 AC_VERB_GET_STREAM_FORMAT, 0);
1454 if (oldval != format) {
1455 msleep(1);
1456 snd_hda_codec_write(codec, nid, 0,
1457 AC_VERB_SET_STREAM_FORMAT,
1458 format);
1459 }
1460 p->format_id = format;
1461 }
1462}
1463
1da177e4
LT
1464/**
1465 * snd_hda_codec_setup_stream - set up the codec for streaming
1466 * @codec: the CODEC to set up
1467 * @nid: the NID to set up
1468 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1469 * @channel_id: channel id to pass, zero based.
1470 * @format: stream format.
1471 */
0ba21762
TI
1472void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1473 u32 stream_tag,
1da177e4
LT
1474 int channel_id, int format)
1475{
3f50ac6a 1476 struct hda_codec *c;
eb541337 1477 struct hda_cvt_setup *p;
62b7e5e0 1478 int type;
eb541337
TI
1479 int i;
1480
0ba21762 1481 if (!nid)
d21b37ea
TI
1482 return;
1483
0ba21762
TI
1484 snd_printdd("hda_codec_setup_stream: "
1485 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1da177e4 1486 nid, stream_tag, channel_id, format);
eb541337
TI
1487 p = get_hda_cvt_setup(codec, nid);
1488 if (!p)
1489 return;
ed360813
TI
1490
1491 if (codec->pcm_format_first)
1492 update_pcm_format(codec, p, nid, format);
1493 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1494 if (!codec->pcm_format_first)
1495 update_pcm_format(codec, p, nid, format);
1496
eb541337
TI
1497 p->active = 1;
1498 p->dirty = 0;
1499
1500 /* make other inactive cvts with the same stream-tag dirty */
62b7e5e0 1501 type = get_wcaps_type(get_wcaps(codec, nid));
3f50ac6a
TI
1502 list_for_each_entry(c, &codec->bus->codec_list, list) {
1503 for (i = 0; i < c->cvt_setups.used; i++) {
1504 p = snd_array_elem(&c->cvt_setups, i);
62b7e5e0 1505 if (!p->active && p->stream_tag == stream_tag &&
54c2a89f 1506 get_wcaps_type(get_wcaps(c, p->nid)) == type)
3f50ac6a
TI
1507 p->dirty = 1;
1508 }
eb541337 1509 }
1da177e4 1510}
ff7a3267 1511EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1da177e4 1512
f0cea797
TI
1513static void really_cleanup_stream(struct hda_codec *codec,
1514 struct hda_cvt_setup *q);
1515
d5191e50 1516/**
f0cea797 1517 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
d5191e50
TI
1518 * @codec: the CODEC to clean up
1519 * @nid: the NID to clean up
f0cea797 1520 * @do_now: really clean up the stream instead of clearing the active flag
d5191e50 1521 */
f0cea797
TI
1522void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1523 int do_now)
888afa15 1524{
eb541337
TI
1525 struct hda_cvt_setup *p;
1526
888afa15
TI
1527 if (!nid)
1528 return;
1529
0e7adbe2
TI
1530 if (codec->no_sticky_stream)
1531 do_now = 1;
1532
888afa15 1533 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
eb541337 1534 p = get_hda_cvt_setup(codec, nid);
f0cea797
TI
1535 if (p) {
1536 /* here we just clear the active flag when do_now isn't set;
1537 * actual clean-ups will be done later in
1538 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1539 */
1540 if (do_now)
1541 really_cleanup_stream(codec, p);
1542 else
1543 p->active = 0;
1544 }
eb541337 1545}
f0cea797 1546EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
eb541337
TI
1547
1548static void really_cleanup_stream(struct hda_codec *codec,
1549 struct hda_cvt_setup *q)
1550{
1551 hda_nid_t nid = q->nid;
218264ae
TI
1552 if (q->stream_tag || q->channel_id)
1553 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1554 if (q->format_id)
1555 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1556);
eb541337
TI
1557 memset(q, 0, sizeof(*q));
1558 q->nid = nid;
1559}
1560
1561/* clean up the all conflicting obsolete streams */
1562static void purify_inactive_streams(struct hda_codec *codec)
1563{
3f50ac6a 1564 struct hda_codec *c;
eb541337
TI
1565 int i;
1566
3f50ac6a
TI
1567 list_for_each_entry(c, &codec->bus->codec_list, list) {
1568 for (i = 0; i < c->cvt_setups.used; i++) {
1569 struct hda_cvt_setup *p;
1570 p = snd_array_elem(&c->cvt_setups, i);
1571 if (p->dirty)
1572 really_cleanup_stream(c, p);
1573 }
eb541337
TI
1574 }
1575}
1576
2a43952a 1577#ifdef CONFIG_PM
eb541337
TI
1578/* clean up all streams; called from suspend */
1579static void hda_cleanup_all_streams(struct hda_codec *codec)
1580{
1581 int i;
1582
1583 for (i = 0; i < codec->cvt_setups.used; i++) {
1584 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1585 if (p->stream_tag)
1586 really_cleanup_stream(codec, p);
1587 }
888afa15 1588}
1c7276cf 1589#endif
888afa15 1590
1da177e4
LT
1591/*
1592 * amp access functions
1593 */
1594
4a19faee 1595/* FIXME: more better hash key? */
28aedaf7 1596#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1327a32b 1597#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
92c7c8a7
TI
1598#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1599#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1da177e4 1600#define INFO_AMP_CAPS (1<<0)
4a19faee 1601#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1da177e4
LT
1602
1603/* initialize the hash table */
1289e9e8 1604static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
01751f54
TI
1605 unsigned int record_size)
1606{
1607 memset(cache, 0, sizeof(*cache));
1608 memset(cache->hash, 0xff, sizeof(cache->hash));
603c4019 1609 snd_array_init(&cache->buf, record_size, 64);
01751f54
TI
1610}
1611
1fcaee6e 1612static void free_hda_cache(struct hda_cache_rec *cache)
1da177e4 1613{
603c4019 1614 snd_array_free(&cache->buf);
1da177e4
LT
1615}
1616
1617/* query the hash. allocate an entry if not found. */
a68d5a54 1618static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1da177e4 1619{
01751f54
TI
1620 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1621 u16 cur = cache->hash[idx];
1622 struct hda_cache_head *info;
1da177e4
LT
1623
1624 while (cur != 0xffff) {
f43aa025 1625 info = snd_array_elem(&cache->buf, cur);
1da177e4
LT
1626 if (info->key == key)
1627 return info;
1628 cur = info->next;
1629 }
a68d5a54
TI
1630 return NULL;
1631}
1da177e4 1632
a68d5a54
TI
1633/* query the hash. allocate an entry if not found. */
1634static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1635 u32 key)
1636{
1637 struct hda_cache_head *info = get_hash(cache, key);
1638 if (!info) {
1639 u16 idx, cur;
1640 /* add a new hash entry */
1641 info = snd_array_new(&cache->buf);
1642 if (!info)
1643 return NULL;
1644 cur = snd_array_index(&cache->buf, info);
1645 info->key = key;
1646 info->val = 0;
1647 idx = key % (u16)ARRAY_SIZE(cache->hash);
1648 info->next = cache->hash[idx];
1649 cache->hash[idx] = cur;
1650 }
1da177e4
LT
1651 return info;
1652}
1653
01751f54
TI
1654/* query and allocate an amp hash entry */
1655static inline struct hda_amp_info *
1656get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1657{
1658 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1659}
1660
c3b6bcc2
TI
1661/* overwrite the value with the key in the caps hash */
1662static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1663{
1664 struct hda_amp_info *info;
1665
1666 mutex_lock(&codec->hash_mutex);
1667 info = get_alloc_amp_hash(codec, key);
1668 if (!info) {
1669 mutex_unlock(&codec->hash_mutex);
1670 return -EINVAL;
1671 }
1672 info->amp_caps = val;
1673 info->head.val |= INFO_AMP_CAPS;
1674 mutex_unlock(&codec->hash_mutex);
1675 return 0;
1676}
1677
1678/* query the value from the caps hash; if not found, fetch the current
1679 * value from the given function and store in the hash
1680 */
1681static unsigned int
1682query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1683 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1684{
1685 struct hda_amp_info *info;
1686 unsigned int val;
1687
1688 mutex_lock(&codec->hash_mutex);
1689 info = get_alloc_amp_hash(codec, key);
1690 if (!info) {
1691 mutex_unlock(&codec->hash_mutex);
1692 return 0;
1693 }
1694 if (!(info->head.val & INFO_AMP_CAPS)) {
1695 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1696 val = func(codec, nid, dir);
1697 write_caps_hash(codec, key, val);
1698 } else {
1699 val = info->amp_caps;
1700 mutex_unlock(&codec->hash_mutex);
1701 }
1702 return val;
1703}
1704
1705static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1706 int direction)
1707{
1708 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1709 nid = codec->afg;
1710 return snd_hda_param_read(codec, nid,
1711 direction == HDA_OUTPUT ?
1712 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1713}
1714
d5191e50
TI
1715/**
1716 * query_amp_caps - query AMP capabilities
1717 * @codec: the HD-auio codec
1718 * @nid: the NID to query
1719 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1720 *
1721 * Query AMP capabilities for the given widget and direction.
1722 * Returns the obtained capability bits.
1723 *
1724 * When cap bits have been already read, this doesn't read again but
1725 * returns the cached value.
1da177e4 1726 */
09a99959 1727u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1da177e4 1728{
c3b6bcc2
TI
1729 return query_caps_hash(codec, nid, direction,
1730 HDA_HASH_KEY(nid, direction, 0),
1731 read_amp_cap);
1da177e4 1732}
ff7a3267 1733EXPORT_SYMBOL_HDA(query_amp_caps);
1da177e4 1734
d5191e50
TI
1735/**
1736 * snd_hda_override_amp_caps - Override the AMP capabilities
1737 * @codec: the CODEC to clean up
1738 * @nid: the NID to clean up
1739 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1740 * @caps: the capability bits to set
1741 *
1742 * Override the cached AMP caps bits value by the given one.
1743 * This function is useful if the driver needs to adjust the AMP ranges,
1744 * e.g. limit to 0dB, etc.
1745 *
1746 * Returns zero if successful or a negative error code.
1747 */
897cc188
TI
1748int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1749 unsigned int caps)
1750{
c3b6bcc2 1751 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
897cc188 1752}
ff7a3267 1753EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1327a32b 1754
c3b6bcc2
TI
1755static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1756 int dir)
92c7c8a7
TI
1757{
1758 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1759}
1760
d5191e50
TI
1761/**
1762 * snd_hda_query_pin_caps - Query PIN capabilities
1763 * @codec: the HD-auio codec
1764 * @nid: the NID to query
1765 *
1766 * Query PIN capabilities for the given widget.
1767 * Returns the obtained capability bits.
1768 *
1769 * When cap bits have been already read, this doesn't read again but
1770 * returns the cached value.
1771 */
92c7c8a7
TI
1772u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1773{
c3b6bcc2 1774 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
92c7c8a7
TI
1775 read_pin_cap);
1776}
1327a32b 1777EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
897cc188 1778
f57c2565
TI
1779/**
1780 * snd_hda_override_pin_caps - Override the pin capabilities
1781 * @codec: the CODEC
1782 * @nid: the NID to override
1783 * @caps: the capability bits to set
1784 *
1785 * Override the cached PIN capabilitiy bits value by the given one.
1786 *
1787 * Returns zero if successful or a negative error code.
1788 */
1789int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1790 unsigned int caps)
1791{
c3b6bcc2 1792 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
f57c2565
TI
1793}
1794EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1795
c3b6bcc2
TI
1796/* read or sync the hash value with the current value;
1797 * call within hash_mutex
1da177e4 1798 */
c3b6bcc2
TI
1799static struct hda_amp_info *
1800update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1801 int direction, int index)
1da177e4 1802{
c3b6bcc2
TI
1803 struct hda_amp_info *info;
1804 unsigned int parm, val = 0;
1805 bool val_read = false;
1da177e4 1806
c3b6bcc2
TI
1807 retry:
1808 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1809 if (!info)
1810 return NULL;
1811 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1812 if (!val_read) {
1813 mutex_unlock(&codec->hash_mutex);
1814 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1815 parm |= direction == HDA_OUTPUT ?
1816 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1817 parm |= index;
1818 val = snd_hda_codec_read(codec, nid, 0,
0ba21762 1819 AC_VERB_GET_AMP_GAIN_MUTE, parm);
c3b6bcc2
TI
1820 val &= 0xff;
1821 val_read = true;
1822 mutex_lock(&codec->hash_mutex);
1823 goto retry;
1824 }
1825 info->vol[ch] = val;
1826 info->head.val |= INFO_AMP_VOL(ch);
1827 }
1828 return info;
1da177e4
LT
1829}
1830
1831/*
c3b6bcc2 1832 * write the current volume in info to the h/w
1da177e4 1833 */
4a19faee 1834static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
0ba21762
TI
1835 hda_nid_t nid, int ch, int direction, int index,
1836 int val)
1da177e4
LT
1837{
1838 u32 parm;
1839
1840 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1841 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1842 parm |= index << AC_AMP_SET_INDEX_SHIFT;
3868137e
TI
1843 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1844 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1845 ; /* set the zero value as a fake mute */
1846 else
1847 parm |= val;
1da177e4
LT
1848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1849}
1850
d5191e50
TI
1851/**
1852 * snd_hda_codec_amp_read - Read AMP value
1853 * @codec: HD-audio codec
1854 * @nid: NID to read the AMP value
1855 * @ch: channel (left=0 or right=1)
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @index: the index value (only for input direction)
1858 *
1859 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1da177e4 1860 */
834be88d
TI
1861int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1862 int direction, int index)
1da177e4 1863{
0ba21762 1864 struct hda_amp_info *info;
c3b6bcc2
TI
1865 unsigned int val = 0;
1866
1867 mutex_lock(&codec->hash_mutex);
1868 info = update_amp_hash(codec, nid, ch, direction, index);
1869 if (info)
1870 val = info->vol[ch];
1871 mutex_unlock(&codec->hash_mutex);
1872 return val;
1da177e4 1873}
ff7a3267 1874EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1da177e4 1875
d5191e50
TI
1876/**
1877 * snd_hda_codec_amp_update - update the AMP value
1878 * @codec: HD-audio codec
1879 * @nid: NID to read the AMP value
1880 * @ch: channel (left=0 or right=1)
1881 * @direction: #HDA_INPUT or #HDA_OUTPUT
1882 * @idx: the index value (only for input direction)
1883 * @mask: bit mask to set
1884 * @val: the bits value to set
1885 *
1886 * Update the AMP value with a bit mask.
1887 * Returns 0 if the value is unchanged, 1 if changed.
4a19faee 1888 */
834be88d
TI
1889int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1890 int direction, int idx, int mask, int val)
1da177e4 1891{
0ba21762 1892 struct hda_amp_info *info;
4a19faee 1893
46712646
TI
1894 if (snd_BUG_ON(mask & ~0xff))
1895 mask &= 0xff;
4a19faee 1896 val &= mask;
c3b6bcc2
TI
1897
1898 mutex_lock(&codec->hash_mutex);
1899 info = update_amp_hash(codec, nid, ch, direction, idx);
1900 if (!info) {
1901 mutex_unlock(&codec->hash_mutex);
1902 return 0;
1903 }
1904 val |= info->vol[ch] & ~mask;
1905 if (info->vol[ch] == val) {
1906 mutex_unlock(&codec->hash_mutex);
1da177e4 1907 return 0;
c3b6bcc2
TI
1908 }
1909 info->vol[ch] = val;
1910 mutex_unlock(&codec->hash_mutex);
4a19faee 1911 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1da177e4
LT
1912 return 1;
1913}
ff7a3267 1914EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1da177e4 1915
d5191e50
TI
1916/**
1917 * snd_hda_codec_amp_stereo - update the AMP stereo values
1918 * @codec: HD-audio codec
1919 * @nid: NID to read the AMP value
1920 * @direction: #HDA_INPUT or #HDA_OUTPUT
1921 * @idx: the index value (only for input direction)
1922 * @mask: bit mask to set
1923 * @val: the bits value to set
1924 *
1925 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1926 * stereo widget with the same mask and value.
47fd830a
TI
1927 */
1928int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1929 int direction, int idx, int mask, int val)
1930{
1931 int ch, ret = 0;
46712646
TI
1932
1933 if (snd_BUG_ON(mask & ~0xff))
1934 mask &= 0xff;
47fd830a
TI
1935 for (ch = 0; ch < 2; ch++)
1936 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1937 idx, mask, val);
1938 return ret;
1939}
ff7a3267 1940EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
47fd830a 1941
2a43952a 1942#ifdef CONFIG_PM
d5191e50
TI
1943/**
1944 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1945 * @codec: HD-audio codec
1946 *
1947 * Resume the all amp commands from the cache.
1948 */
b3ac5636
TI
1949void snd_hda_codec_resume_amp(struct hda_codec *codec)
1950{
603c4019 1951 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
b3ac5636
TI
1952 int i;
1953
603c4019 1954 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
b3ac5636
TI
1955 u32 key = buffer->head.key;
1956 hda_nid_t nid;
1957 unsigned int idx, dir, ch;
1958 if (!key)
1959 continue;
1960 nid = key & 0xff;
1961 idx = (key >> 16) & 0xff;
1962 dir = (key >> 24) & 0xff;
1963 for (ch = 0; ch < 2; ch++) {
1964 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1965 continue;
1966 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1967 buffer->vol[ch]);
1968 }
1969 }
1970}
ff7a3267 1971EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2a43952a 1972#endif /* CONFIG_PM */
1da177e4 1973
afbd9b84
TI
1974static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1975 unsigned int ofs)
1976{
1977 u32 caps = query_amp_caps(codec, nid, dir);
1978 /* get num steps */
1979 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1980 if (ofs < caps)
1981 caps -= ofs;
1982 return caps;
1983}
1984
d5191e50
TI
1985/**
1986 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1987 *
1988 * The control element is supposed to have the private_value field
1989 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1990 */
0ba21762
TI
1991int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1992 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1993{
1994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1995 u16 nid = get_amp_nid(kcontrol);
1996 u8 chs = get_amp_channels(kcontrol);
1997 int dir = get_amp_direction(kcontrol);
29fdbec2 1998 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4 1999
afbd9b84
TI
2000 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2001 uinfo->count = chs == 3 ? 2 : 1;
2002 uinfo->value.integer.min = 0;
2003 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2004 if (!uinfo->value.integer.max) {
0ba21762 2005 printk(KERN_WARNING "hda_codec: "
9c8f2abd
TI
2006 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2007 kcontrol->id.name);
1da177e4
LT
2008 return -EINVAL;
2009 }
1da177e4
LT
2010 return 0;
2011}
ff7a3267 2012EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1da177e4 2013
29fdbec2
TI
2014
2015static inline unsigned int
2016read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2017 int ch, int dir, int idx, unsigned int ofs)
2018{
2019 unsigned int val;
2020 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2021 val &= HDA_AMP_VOLMASK;
2022 if (val >= ofs)
2023 val -= ofs;
2024 else
2025 val = 0;
2026 return val;
2027}
2028
2029static inline int
2030update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2031 int ch, int dir, int idx, unsigned int ofs,
2032 unsigned int val)
2033{
afbd9b84
TI
2034 unsigned int maxval;
2035
29fdbec2
TI
2036 if (val > 0)
2037 val += ofs;
7ccc3efa
TI
2038 /* ofs = 0: raw max value */
2039 maxval = get_amp_max_value(codec, nid, dir, 0);
afbd9b84
TI
2040 if (val > maxval)
2041 val = maxval;
29fdbec2
TI
2042 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2043 HDA_AMP_VOLMASK, val);
2044}
2045
d5191e50
TI
2046/**
2047 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2048 *
2049 * The control element is supposed to have the private_value field
2050 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2051 */
0ba21762
TI
2052int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2054{
2055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2056 hda_nid_t nid = get_amp_nid(kcontrol);
2057 int chs = get_amp_channels(kcontrol);
2058 int dir = get_amp_direction(kcontrol);
2059 int idx = get_amp_index(kcontrol);
29fdbec2 2060 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
2061 long *valp = ucontrol->value.integer.value;
2062
2063 if (chs & 1)
29fdbec2 2064 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1da177e4 2065 if (chs & 2)
29fdbec2 2066 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1da177e4
LT
2067 return 0;
2068}
ff7a3267 2069EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1da177e4 2070
d5191e50
TI
2071/**
2072 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2073 *
2074 * The control element is supposed to have the private_value field
2075 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2076 */
0ba21762
TI
2077int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2079{
2080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2081 hda_nid_t nid = get_amp_nid(kcontrol);
2082 int chs = get_amp_channels(kcontrol);
2083 int dir = get_amp_direction(kcontrol);
2084 int idx = get_amp_index(kcontrol);
29fdbec2 2085 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
2086 long *valp = ucontrol->value.integer.value;
2087 int change = 0;
2088
cb53c626 2089 snd_hda_power_up(codec);
b9f5a89c 2090 if (chs & 1) {
29fdbec2 2091 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
b9f5a89c
NG
2092 valp++;
2093 }
4a19faee 2094 if (chs & 2)
29fdbec2 2095 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
cb53c626 2096 snd_hda_power_down(codec);
1da177e4
LT
2097 return change;
2098}
ff7a3267 2099EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1da177e4 2100
d5191e50
TI
2101/**
2102 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2103 *
2104 * The control element is supposed to have the private_value field
2105 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2106 */
302e9c5a
JK
2107int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2108 unsigned int size, unsigned int __user *_tlv)
2109{
2110 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2111 hda_nid_t nid = get_amp_nid(kcontrol);
2112 int dir = get_amp_direction(kcontrol);
29fdbec2 2113 unsigned int ofs = get_amp_offset(kcontrol);
de8c85f7 2114 bool min_mute = get_amp_min_mute(kcontrol);
302e9c5a
JK
2115 u32 caps, val1, val2;
2116
2117 if (size < 4 * sizeof(unsigned int))
2118 return -ENOMEM;
2119 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
2120 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2121 val2 = (val2 + 1) * 25;
302e9c5a 2122 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
29fdbec2 2123 val1 += ofs;
302e9c5a 2124 val1 = ((int)val1) * ((int)val2);
3868137e 2125 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
c08d9169 2126 val2 |= TLV_DB_SCALE_MUTE;
302e9c5a
JK
2127 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2128 return -EFAULT;
2129 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2130 return -EFAULT;
2131 if (put_user(val1, _tlv + 2))
2132 return -EFAULT;
2133 if (put_user(val2, _tlv + 3))
2134 return -EFAULT;
2135 return 0;
2136}
ff7a3267 2137EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
302e9c5a 2138
d5191e50
TI
2139/**
2140 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2141 * @codec: HD-audio codec
2142 * @nid: NID of a reference widget
2143 * @dir: #HDA_INPUT or #HDA_OUTPUT
2144 * @tlv: TLV data to be stored, at least 4 elements
2145 *
2146 * Set (static) TLV data for a virtual master volume using the AMP caps
2147 * obtained from the reference NID.
2148 * The volume range is recalculated as if the max volume is 0dB.
2134ea4f
TI
2149 */
2150void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2151 unsigned int *tlv)
2152{
2153 u32 caps;
2154 int nums, step;
2155
2156 caps = query_amp_caps(codec, nid, dir);
2157 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2158 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2159 step = (step + 1) * 25;
2160 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2161 tlv[1] = 2 * sizeof(unsigned int);
2162 tlv[2] = -nums * step;
2163 tlv[3] = step;
2164}
ff7a3267 2165EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2134ea4f
TI
2166
2167/* find a mixer control element with the given name */
09f99701 2168static struct snd_kcontrol *
dcda5806 2169find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2134ea4f
TI
2170{
2171 struct snd_ctl_elem_id id;
2172 memset(&id, 0, sizeof(id));
2173 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
dcda5806 2174 id.device = dev;
09f99701 2175 id.index = idx;
18cb7109
TI
2176 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2177 return NULL;
2134ea4f
TI
2178 strcpy(id.name, name);
2179 return snd_ctl_find_id(codec->bus->card, &id);
2180}
2181
d5191e50
TI
2182/**
2183 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2184 * @codec: HD-audio codec
2185 * @name: ctl id name string
2186 *
2187 * Get the control element with the given id string and IFACE_MIXER.
2188 */
09f99701
TI
2189struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2190 const char *name)
2191{
dcda5806 2192 return find_mixer_ctl(codec, name, 0, 0);
09f99701 2193}
ff7a3267 2194EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
09f99701 2195
dcda5806
TI
2196static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2197 int dev)
1afe206a
TI
2198{
2199 int idx;
2200 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
dcda5806 2201 if (!find_mixer_ctl(codec, name, dev, idx))
1afe206a
TI
2202 return idx;
2203 }
2204 return -EBUSY;
2205}
2206
d5191e50 2207/**
5b0cb1d8 2208 * snd_hda_ctl_add - Add a control element and assign to the codec
d5191e50
TI
2209 * @codec: HD-audio codec
2210 * @nid: corresponding NID (optional)
2211 * @kctl: the control element to assign
2212 *
2213 * Add the given control element to an array inside the codec instance.
2214 * All control elements belonging to a codec are supposed to be added
2215 * by this function so that a proper clean-up works at the free or
2216 * reconfiguration time.
2217 *
2218 * If non-zero @nid is passed, the NID is assigned to the control element.
2219 * The assignment is shown in the codec proc file.
2220 *
2221 * snd_hda_ctl_add() checks the control subdev id field whether
2222 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
9e3fd871
JK
2223 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2224 * specifies if kctl->private_value is a HDA amplifier value.
d5191e50 2225 */
3911a4c1
JK
2226int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2227 struct snd_kcontrol *kctl)
d13bd412
TI
2228{
2229 int err;
9e3fd871 2230 unsigned short flags = 0;
3911a4c1 2231 struct hda_nid_item *item;
d13bd412 2232
5e26dfd0 2233 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
9e3fd871 2234 flags |= HDA_NID_ITEM_AMP;
5e26dfd0
JK
2235 if (nid == 0)
2236 nid = get_amp_nid_(kctl->private_value);
2237 }
9e3fd871
JK
2238 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2239 nid = kctl->id.subdevice & 0xffff;
5e26dfd0 2240 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
4d02d1b6 2241 kctl->id.subdevice = 0;
d13bd412
TI
2242 err = snd_ctl_add(codec->bus->card, kctl);
2243 if (err < 0)
2244 return err;
3911a4c1
JK
2245 item = snd_array_new(&codec->mixers);
2246 if (!item)
d13bd412 2247 return -ENOMEM;
3911a4c1
JK
2248 item->kctl = kctl;
2249 item->nid = nid;
9e3fd871 2250 item->flags = flags;
d13bd412
TI
2251 return 0;
2252}
ff7a3267 2253EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
d13bd412 2254
5b0cb1d8
JK
2255/**
2256 * snd_hda_add_nid - Assign a NID to a control element
2257 * @codec: HD-audio codec
2258 * @nid: corresponding NID (optional)
2259 * @kctl: the control element to assign
2260 * @index: index to kctl
2261 *
2262 * Add the given control element to an array inside the codec instance.
2263 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2264 * NID:KCTL mapping - for example "Capture Source" selector.
2265 */
2266int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2267 unsigned int index, hda_nid_t nid)
2268{
2269 struct hda_nid_item *item;
2270
2271 if (nid > 0) {
2272 item = snd_array_new(&codec->nids);
2273 if (!item)
2274 return -ENOMEM;
2275 item->kctl = kctl;
2276 item->index = index;
2277 item->nid = nid;
2278 return 0;
2279 }
28d1a85e
TI
2280 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2281 kctl->id.name, kctl->id.index, index);
5b0cb1d8
JK
2282 return -EINVAL;
2283}
2284EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2285
d5191e50
TI
2286/**
2287 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2288 * @codec: HD-audio codec
2289 */
d13bd412
TI
2290void snd_hda_ctls_clear(struct hda_codec *codec)
2291{
2292 int i;
3911a4c1 2293 struct hda_nid_item *items = codec->mixers.list;
d13bd412 2294 for (i = 0; i < codec->mixers.used; i++)
3911a4c1 2295 snd_ctl_remove(codec->bus->card, items[i].kctl);
d13bd412 2296 snd_array_free(&codec->mixers);
5b0cb1d8 2297 snd_array_free(&codec->nids);
d13bd412
TI
2298}
2299
a65d629c
TI
2300/* pseudo device locking
2301 * toggle card->shutdown to allow/disallow the device access (as a hack)
2302 */
d3d020bd 2303int snd_hda_lock_devices(struct hda_bus *bus)
6c1f45ea 2304{
d3d020bd
TI
2305 struct snd_card *card = bus->card;
2306 struct hda_codec *codec;
2307
a65d629c 2308 spin_lock(&card->files_lock);
d3d020bd
TI
2309 if (card->shutdown)
2310 goto err_unlock;
a65d629c 2311 card->shutdown = 1;
d3d020bd
TI
2312 if (!list_empty(&card->ctl_files))
2313 goto err_clear;
2314
2315 list_for_each_entry(codec, &bus->codec_list, list) {
2316 int pcm;
2317 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2318 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2319 if (!cpcm->pcm)
2320 continue;
2321 if (cpcm->pcm->streams[0].substream_opened ||
2322 cpcm->pcm->streams[1].substream_opened)
2323 goto err_clear;
2324 }
2325 }
a65d629c
TI
2326 spin_unlock(&card->files_lock);
2327 return 0;
d3d020bd
TI
2328
2329 err_clear:
2330 card->shutdown = 0;
2331 err_unlock:
2332 spin_unlock(&card->files_lock);
2333 return -EINVAL;
a65d629c 2334}
d3d020bd 2335EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
a65d629c 2336
d3d020bd 2337void snd_hda_unlock_devices(struct hda_bus *bus)
a65d629c 2338{
d3d020bd
TI
2339 struct snd_card *card = bus->card;
2340
2341 card = bus->card;
a65d629c
TI
2342 spin_lock(&card->files_lock);
2343 card->shutdown = 0;
2344 spin_unlock(&card->files_lock);
2345}
d3d020bd 2346EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
a65d629c 2347
d5191e50
TI
2348/**
2349 * snd_hda_codec_reset - Clear all objects assigned to the codec
2350 * @codec: HD-audio codec
2351 *
2352 * This frees the all PCM and control elements assigned to the codec, and
2353 * clears the caches and restores the pin default configurations.
2354 *
2355 * When a device is being used, it returns -EBSY. If successfully freed,
2356 * returns zero.
2357 */
a65d629c
TI
2358int snd_hda_codec_reset(struct hda_codec *codec)
2359{
d3d020bd
TI
2360 struct hda_bus *bus = codec->bus;
2361 struct snd_card *card = bus->card;
2362 int i;
a65d629c 2363
d3d020bd 2364 if (snd_hda_lock_devices(bus) < 0)
a65d629c 2365 return -EBUSY;
a65d629c
TI
2366
2367 /* OK, let it free */
26a6cb6c 2368 cancel_delayed_work_sync(&codec->jackpoll_work);
83012a7c 2369#ifdef CONFIG_PM
a2d96e77 2370 cancel_delayed_work_sync(&codec->power_work);
339876d7
TI
2371 codec->power_on = 0;
2372 codec->power_transition = 0;
2373 codec->power_jiffies = jiffies;
d3d020bd 2374 flush_workqueue(bus->workq);
6c1f45ea
TI
2375#endif
2376 snd_hda_ctls_clear(codec);
2377 /* relase PCMs */
2378 for (i = 0; i < codec->num_pcms; i++) {
529bd6c4 2379 if (codec->pcm_info[i].pcm) {
a65d629c 2380 snd_device_free(card, codec->pcm_info[i].pcm);
529bd6c4 2381 clear_bit(codec->pcm_info[i].device,
d3d020bd 2382 bus->pcm_dev_bits);
529bd6c4 2383 }
6c1f45ea
TI
2384 }
2385 if (codec->patch_ops.free)
2386 codec->patch_ops.free(codec);
07dc59f0 2387 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1835a0f9 2388 snd_hda_jack_tbl_clear(codec);
56d17712 2389 codec->proc_widget_hook = NULL;
6c1f45ea
TI
2390 codec->spec = NULL;
2391 free_hda_cache(&codec->amp_cache);
2392 free_hda_cache(&codec->cmd_cache);
827057f5
TI
2393 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2394 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
346ff70f
TI
2395 /* free only driver_pins so that init_pins + user_pins are restored */
2396 snd_array_free(&codec->driver_pins);
3be14149 2397 restore_pincfgs(codec);
09a6071b
TI
2398 snd_array_free(&codec->cvt_setups);
2399 snd_array_free(&codec->spdif_out);
6c1f45ea
TI
2400 codec->num_pcms = 0;
2401 codec->pcm_info = NULL;
2402 codec->preset = NULL;
d1f1af2d
TI
2403 codec->slave_dig_outs = NULL;
2404 codec->spdif_status_reset = 0;
1289e9e8
TI
2405 module_put(codec->owner);
2406 codec->owner = NULL;
a65d629c
TI
2407
2408 /* allow device access again */
d3d020bd 2409 snd_hda_unlock_devices(bus);
a65d629c 2410 return 0;
6c1f45ea
TI
2411}
2412
aeb4b88e
TI
2413typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2414
2415/* apply the function to all matching slave ctls in the mixer list */
2416static int map_slaves(struct hda_codec *codec, const char * const *slaves,
9322ca54 2417 const char *suffix, map_slave_func_t func, void *data)
aeb4b88e
TI
2418{
2419 struct hda_nid_item *items;
2420 const char * const *s;
2421 int i, err;
2422
2423 items = codec->mixers.list;
2424 for (i = 0; i < codec->mixers.used; i++) {
2425 struct snd_kcontrol *sctl = items[i].kctl;
2426 if (!sctl || !sctl->id.name ||
2427 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2428 continue;
2429 for (s = slaves; *s; s++) {
9322ca54
TI
2430 char tmpname[sizeof(sctl->id.name)];
2431 const char *name = *s;
2432 if (suffix) {
2433 snprintf(tmpname, sizeof(tmpname), "%s %s",
2434 name, suffix);
2435 name = tmpname;
2436 }
9322ca54 2437 if (!strcmp(sctl->id.name, name)) {
aeb4b88e
TI
2438 err = func(data, sctl);
2439 if (err)
2440 return err;
2441 break;
2442 }
2443 }
2444 }
2445 return 0;
2446}
2447
2448static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2449{
2450 return 1;
2451}
2452
18478e8b
TI
2453/* guess the value corresponding to 0dB */
2454static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2455{
2456 int _tlv[4];
2457 const int *tlv = NULL;
2458 int val = -1;
2459
2460 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2461 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2462 mm_segment_t fs = get_fs();
2463 set_fs(get_ds());
2464 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2465 tlv = _tlv;
2466 set_fs(fs);
2467 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2468 tlv = kctl->tlv.p;
2469 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2470 val = -tlv[2] / tlv[3];
2471 return val;
2472}
2473
2474/* call kctl->put with the given value(s) */
2475static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2476{
2477 struct snd_ctl_elem_value *ucontrol;
2478 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2479 if (!ucontrol)
2480 return -ENOMEM;
2481 ucontrol->value.integer.value[0] = val;
2482 ucontrol->value.integer.value[1] = val;
2483 kctl->put(kctl, ucontrol);
2484 kfree(ucontrol);
2485 return 0;
2486}
2487
2488/* initialize the slave volume with 0dB */
2489static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2490{
2491 int offset = get_kctl_0dB_offset(slave);
2492 if (offset > 0)
2493 put_kctl_with_value(slave, offset);
2494 return 0;
2495}
2496
2497/* unmute the slave */
2498static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2499{
2500 return put_kctl_with_value(slave, 1);
2501}
2502
d5191e50
TI
2503/**
2504 * snd_hda_add_vmaster - create a virtual master control and add slaves
2505 * @codec: HD-audio codec
2506 * @name: vmaster control name
2507 * @tlv: TLV data (optional)
2508 * @slaves: slave control names (optional)
9322ca54 2509 * @suffix: suffix string to each slave name (optional)
18478e8b 2510 * @init_slave_vol: initialize slaves to unmute/0dB
29e5853d 2511 * @ctl_ret: store the vmaster kcontrol in return
d5191e50
TI
2512 *
2513 * Create a virtual master control with the given name. The TLV data
2514 * must be either NULL or a valid data.
2515 *
2516 * @slaves is a NULL-terminated array of strings, each of which is a
2517 * slave control name. All controls with these names are assigned to
2518 * the new virtual master control.
2519 *
2520 * This function returns zero if successful or a negative error code.
2521 */
18478e8b 2522int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
9322ca54 2523 unsigned int *tlv, const char * const *slaves,
29e5853d
TI
2524 const char *suffix, bool init_slave_vol,
2525 struct snd_kcontrol **ctl_ret)
2134ea4f
TI
2526{
2527 struct snd_kcontrol *kctl;
2134ea4f
TI
2528 int err;
2529
29e5853d
TI
2530 if (ctl_ret)
2531 *ctl_ret = NULL;
2532
9322ca54 2533 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
aeb4b88e 2534 if (err != 1) {
2f085549
TI
2535 snd_printdd("No slave found for %s\n", name);
2536 return 0;
2537 }
2134ea4f
TI
2538 kctl = snd_ctl_make_virtual_master(name, tlv);
2539 if (!kctl)
2540 return -ENOMEM;
3911a4c1 2541 err = snd_hda_ctl_add(codec, 0, kctl);
2134ea4f
TI
2542 if (err < 0)
2543 return err;
28aedaf7 2544
9322ca54
TI
2545 err = map_slaves(codec, slaves, suffix,
2546 (map_slave_func_t)snd_ctl_add_slave, kctl);
aeb4b88e
TI
2547 if (err < 0)
2548 return err;
18478e8b
TI
2549
2550 /* init with master mute & zero volume */
2551 put_kctl_with_value(kctl, 0);
2552 if (init_slave_vol)
2553 map_slaves(codec, slaves, suffix,
2554 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2555
29e5853d
TI
2556 if (ctl_ret)
2557 *ctl_ret = kctl;
2134ea4f
TI
2558 return 0;
2559}
18478e8b 2560EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2134ea4f 2561
d2f344b5
TI
2562/*
2563 * mute-LED control using vmaster
2564 */
2565static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_info *uinfo)
2567{
2568 static const char * const texts[] = {
2569 "Off", "On", "Follow Master"
2570 };
2571 unsigned int index;
2572
2573 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2574 uinfo->count = 1;
2575 uinfo->value.enumerated.items = 3;
2576 index = uinfo->value.enumerated.item;
2577 if (index >= 3)
2578 index = 2;
2579 strcpy(uinfo->value.enumerated.name, texts[index]);
2580 return 0;
2581}
2582
2583static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
2585{
2586 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2587 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2588 return 0;
2589}
2590
2591static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2595 unsigned int old_mode = hook->mute_mode;
2596
2597 hook->mute_mode = ucontrol->value.enumerated.item[0];
2598 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2599 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2600 if (old_mode == hook->mute_mode)
2601 return 0;
2602 snd_hda_sync_vmaster_hook(hook);
2603 return 1;
2604}
2605
2606static struct snd_kcontrol_new vmaster_mute_mode = {
2607 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2608 .name = "Mute-LED Mode",
2609 .info = vmaster_mute_mode_info,
2610 .get = vmaster_mute_mode_get,
2611 .put = vmaster_mute_mode_put,
2612};
2613
2614/*
2615 * Add a mute-LED hook with the given vmaster switch kctl
2616 * "Mute-LED Mode" control is automatically created and associated with
2617 * the given hook.
2618 */
2619int snd_hda_add_vmaster_hook(struct hda_codec *codec,
f29735cb
TI
2620 struct hda_vmaster_mute_hook *hook,
2621 bool expose_enum_ctl)
d2f344b5
TI
2622{
2623 struct snd_kcontrol *kctl;
2624
2625 if (!hook->hook || !hook->sw_kctl)
2626 return 0;
2627 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2628 hook->codec = codec;
2629 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
f29735cb
TI
2630 if (!expose_enum_ctl)
2631 return 0;
d2f344b5
TI
2632 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2633 if (!kctl)
2634 return -ENOMEM;
2635 return snd_hda_ctl_add(codec, 0, kctl);
2636}
2637EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2638
2639/*
2640 * Call the hook with the current value for synchronization
2641 * Should be called in init callback
2642 */
2643void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2644{
2645 if (!hook->hook || !hook->codec)
2646 return;
2647 switch (hook->mute_mode) {
2648 case HDA_VMUTE_FOLLOW_MASTER:
2649 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2650 break;
2651 default:
2652 hook->hook(hook->codec, hook->mute_mode);
2653 break;
2654 }
2655}
2656EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2657
2658
d5191e50
TI
2659/**
2660 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2661 *
2662 * The control element is supposed to have the private_value field
2663 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2664 */
0ba21762
TI
2665int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2667{
2668 int chs = get_amp_channels(kcontrol);
2669
2670 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2671 uinfo->count = chs == 3 ? 2 : 1;
2672 uinfo->value.integer.min = 0;
2673 uinfo->value.integer.max = 1;
2674 return 0;
2675}
ff7a3267 2676EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1da177e4 2677
d5191e50
TI
2678/**
2679 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2680 *
2681 * The control element is supposed to have the private_value field
2682 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2683 */
0ba21762
TI
2684int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2686{
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 hda_nid_t nid = get_amp_nid(kcontrol);
2689 int chs = get_amp_channels(kcontrol);
2690 int dir = get_amp_direction(kcontrol);
2691 int idx = get_amp_index(kcontrol);
2692 long *valp = ucontrol->value.integer.value;
2693
2694 if (chs & 1)
0ba21762 2695 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
47fd830a 2696 HDA_AMP_MUTE) ? 0 : 1;
1da177e4 2697 if (chs & 2)
0ba21762 2698 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
47fd830a 2699 HDA_AMP_MUTE) ? 0 : 1;
1da177e4
LT
2700 return 0;
2701}
ff7a3267 2702EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1da177e4 2703
d5191e50
TI
2704/**
2705 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2706 *
2707 * The control element is supposed to have the private_value field
2708 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2709 */
0ba21762
TI
2710int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2712{
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 hda_nid_t nid = get_amp_nid(kcontrol);
2715 int chs = get_amp_channels(kcontrol);
2716 int dir = get_amp_direction(kcontrol);
2717 int idx = get_amp_index(kcontrol);
1da177e4
LT
2718 long *valp = ucontrol->value.integer.value;
2719 int change = 0;
2720
cb53c626 2721 snd_hda_power_up(codec);
b9f5a89c 2722 if (chs & 1) {
4a19faee 2723 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
47fd830a
TI
2724 HDA_AMP_MUTE,
2725 *valp ? 0 : HDA_AMP_MUTE);
b9f5a89c
NG
2726 valp++;
2727 }
4a19faee
TI
2728 if (chs & 2)
2729 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
47fd830a
TI
2730 HDA_AMP_MUTE,
2731 *valp ? 0 : HDA_AMP_MUTE);
9e5341b9 2732 hda_call_check_power_status(codec, nid);
cb53c626 2733 snd_hda_power_down(codec);
1da177e4
LT
2734 return change;
2735}
ff7a3267 2736EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1da177e4 2737
985be54b
TI
2738/*
2739 * bound volume controls
2740 *
2741 * bind multiple volumes (# indices, from 0)
2742 */
2743
2744#define AMP_VAL_IDX_SHIFT 19
2745#define AMP_VAL_IDX_MASK (0x0f<<19)
2746
d5191e50
TI
2747/**
2748 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2749 *
2750 * The control element is supposed to have the private_value field
2751 * set up via HDA_BIND_MUTE*() macros.
2752 */
0ba21762
TI
2753int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2755{
2756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2757 unsigned long pval;
2758 int err;
2759
5a9e02e9 2760 mutex_lock(&codec->control_mutex);
985be54b
TI
2761 pval = kcontrol->private_value;
2762 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2763 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2764 kcontrol->private_value = pval;
5a9e02e9 2765 mutex_unlock(&codec->control_mutex);
985be54b
TI
2766 return err;
2767}
ff7a3267 2768EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
985be54b 2769
d5191e50
TI
2770/**
2771 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2772 *
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_BIND_MUTE*() macros.
2775 */
0ba21762
TI
2776int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2778{
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 unsigned long pval;
2781 int i, indices, err = 0, change = 0;
2782
5a9e02e9 2783 mutex_lock(&codec->control_mutex);
985be54b
TI
2784 pval = kcontrol->private_value;
2785 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2786 for (i = 0; i < indices; i++) {
0ba21762
TI
2787 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2788 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
2789 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2790 if (err < 0)
2791 break;
2792 change |= err;
2793 }
2794 kcontrol->private_value = pval;
5a9e02e9 2795 mutex_unlock(&codec->control_mutex);
985be54b
TI
2796 return err < 0 ? err : change;
2797}
ff7a3267 2798EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
985be54b 2799
d5191e50
TI
2800/**
2801 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2802 *
2803 * The control element is supposed to have the private_value field
2804 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
532d5381
TI
2805 */
2806int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_info *uinfo)
2808{
2809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2810 struct hda_bind_ctls *c;
2811 int err;
2812
5a9e02e9 2813 mutex_lock(&codec->control_mutex);
14c65f98 2814 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2815 kcontrol->private_value = *c->values;
2816 err = c->ops->info(kcontrol, uinfo);
2817 kcontrol->private_value = (long)c;
5a9e02e9 2818 mutex_unlock(&codec->control_mutex);
532d5381
TI
2819 return err;
2820}
ff7a3267 2821EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
532d5381 2822
d5191e50
TI
2823/**
2824 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2825 *
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2828 */
532d5381
TI
2829int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 struct hda_bind_ctls *c;
2834 int err;
2835
5a9e02e9 2836 mutex_lock(&codec->control_mutex);
14c65f98 2837 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2838 kcontrol->private_value = *c->values;
2839 err = c->ops->get(kcontrol, ucontrol);
2840 kcontrol->private_value = (long)c;
5a9e02e9 2841 mutex_unlock(&codec->control_mutex);
532d5381
TI
2842 return err;
2843}
ff7a3267 2844EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
532d5381 2845
d5191e50
TI
2846/**
2847 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2848 *
2849 * The control element is supposed to have the private_value field
2850 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2851 */
532d5381
TI
2852int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
2854{
2855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2856 struct hda_bind_ctls *c;
2857 unsigned long *vals;
2858 int err = 0, change = 0;
2859
5a9e02e9 2860 mutex_lock(&codec->control_mutex);
14c65f98 2861 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2862 for (vals = c->values; *vals; vals++) {
2863 kcontrol->private_value = *vals;
2864 err = c->ops->put(kcontrol, ucontrol);
2865 if (err < 0)
2866 break;
2867 change |= err;
2868 }
2869 kcontrol->private_value = (long)c;
5a9e02e9 2870 mutex_unlock(&codec->control_mutex);
532d5381
TI
2871 return err < 0 ? err : change;
2872}
ff7a3267 2873EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
532d5381 2874
d5191e50
TI
2875/**
2876 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2877 *
2878 * The control element is supposed to have the private_value field
2879 * set up via HDA_BIND_VOL() macro.
2880 */
532d5381
TI
2881int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2882 unsigned int size, unsigned int __user *tlv)
2883{
2884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2885 struct hda_bind_ctls *c;
2886 int err;
2887
5a9e02e9 2888 mutex_lock(&codec->control_mutex);
14c65f98 2889 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2890 kcontrol->private_value = *c->values;
2891 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2892 kcontrol->private_value = (long)c;
5a9e02e9 2893 mutex_unlock(&codec->control_mutex);
532d5381
TI
2894 return err;
2895}
ff7a3267 2896EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
532d5381
TI
2897
2898struct hda_ctl_ops snd_hda_bind_vol = {
2899 .info = snd_hda_mixer_amp_volume_info,
2900 .get = snd_hda_mixer_amp_volume_get,
2901 .put = snd_hda_mixer_amp_volume_put,
2902 .tlv = snd_hda_mixer_amp_tlv
2903};
ff7a3267 2904EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
532d5381
TI
2905
2906struct hda_ctl_ops snd_hda_bind_sw = {
2907 .info = snd_hda_mixer_amp_switch_info,
2908 .get = snd_hda_mixer_amp_switch_get,
2909 .put = snd_hda_mixer_amp_switch_put,
2910 .tlv = snd_hda_mixer_amp_tlv
2911};
ff7a3267 2912EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
532d5381 2913
1da177e4
LT
2914/*
2915 * SPDIF out controls
2916 */
2917
0ba21762
TI
2918static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2920{
2921 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2922 uinfo->count = 1;
2923 return 0;
2924}
2925
0ba21762
TI
2926static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2927 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2928{
2929 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2930 IEC958_AES0_NONAUDIO |
2931 IEC958_AES0_CON_EMPHASIS_5015 |
2932 IEC958_AES0_CON_NOT_COPYRIGHT;
2933 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2934 IEC958_AES1_CON_ORIGINAL;
2935 return 0;
2936}
2937
0ba21762
TI
2938static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2940{
2941 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2942 IEC958_AES0_NONAUDIO |
2943 IEC958_AES0_PRO_EMPHASIS_5015;
2944 return 0;
2945}
2946
0ba21762
TI
2947static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2949{
2950 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 2951 int idx = kcontrol->private_value;
e3245cdd 2952 struct hda_spdif_out *spdif;
1da177e4 2953
e3245cdd
TI
2954 mutex_lock(&codec->spdif_mutex);
2955 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976
SW
2956 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2957 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2958 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2959 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
e3245cdd 2960 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2961
2962 return 0;
2963}
2964
2965/* convert from SPDIF status bits to HDA SPDIF bits
2966 * bit 0 (DigEn) is always set zero (to be filled later)
2967 */
2968static unsigned short convert_from_spdif_status(unsigned int sbits)
2969{
2970 unsigned short val = 0;
2971
2972 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 2973 val |= AC_DIG1_PROFESSIONAL;
1da177e4 2974 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 2975 val |= AC_DIG1_NONAUDIO;
1da177e4 2976 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
2977 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2978 IEC958_AES0_PRO_EMPHASIS_5015)
2979 val |= AC_DIG1_EMPHASIS;
1da177e4 2980 } else {
0ba21762
TI
2981 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2982 IEC958_AES0_CON_EMPHASIS_5015)
2983 val |= AC_DIG1_EMPHASIS;
2984 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2985 val |= AC_DIG1_COPYRIGHT;
1da177e4 2986 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 2987 val |= AC_DIG1_LEVEL;
1da177e4
LT
2988 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2989 }
2990 return val;
2991}
2992
2993/* convert to SPDIF status bits from HDA SPDIF bits
2994 */
2995static unsigned int convert_to_spdif_status(unsigned short val)
2996{
2997 unsigned int sbits = 0;
2998
0ba21762 2999 if (val & AC_DIG1_NONAUDIO)
1da177e4 3000 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 3001 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
3002 sbits |= IEC958_AES0_PROFESSIONAL;
3003 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762 3004 if (sbits & AC_DIG1_EMPHASIS)
1da177e4
LT
3005 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3006 } else {
0ba21762 3007 if (val & AC_DIG1_EMPHASIS)
1da177e4 3008 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 3009 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 3010 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 3011 if (val & AC_DIG1_LEVEL)
1da177e4
LT
3012 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3013 sbits |= val & (0x7f << 8);
3014 }
3015 return sbits;
3016}
3017
2f72853c
TI
3018/* set digital convert verbs both for the given NID and its slaves */
3019static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3020 int verb, int val)
3021{
dda14410 3022 const hda_nid_t *d;
2f72853c 3023
9e976976 3024 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2f72853c
TI
3025 d = codec->slave_dig_outs;
3026 if (!d)
3027 return;
3028 for (; *d; d++)
9e976976 3029 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2f72853c
TI
3030}
3031
3032static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3033 int dig1, int dig2)
3034{
3035 if (dig1 != -1)
3036 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3037 if (dig2 != -1)
3038 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3039}
3040
0ba21762
TI
3041static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3042 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3043{
3044 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3045 int idx = kcontrol->private_value;
e3245cdd
TI
3046 struct hda_spdif_out *spdif;
3047 hda_nid_t nid;
1da177e4
LT
3048 unsigned short val;
3049 int change;
3050
62932df8 3051 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
3052 spdif = snd_array_elem(&codec->spdif_out, idx);
3053 nid = spdif->nid;
7c935976 3054 spdif->status = ucontrol->value.iec958.status[0] |
1da177e4
LT
3055 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3056 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3057 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
7c935976
SW
3058 val = convert_from_spdif_status(spdif->status);
3059 val |= spdif->ctls & 1;
3060 change = spdif->ctls != val;
3061 spdif->ctls = val;
74b654c9 3062 if (change && nid != (u16)-1)
2f72853c 3063 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
62932df8 3064 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3065 return change;
3066}
3067
a5ce8890 3068#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1da177e4 3069
0ba21762
TI
3070static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3072{
3073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3074 int idx = kcontrol->private_value;
e3245cdd 3075 struct hda_spdif_out *spdif;
1da177e4 3076
e3245cdd
TI
3077 mutex_lock(&codec->spdif_mutex);
3078 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976 3079 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
e3245cdd 3080 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3081 return 0;
3082}
3083
74b654c9
SW
3084static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3085 int dig1, int dig2)
3086{
3087 set_dig_out_convert(codec, nid, dig1, dig2);
3088 /* unmute amp switch (if any) */
3089 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3090 (dig1 & AC_DIG1_ENABLE))
3091 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3092 HDA_AMP_MUTE, 0);
3093}
3094
0ba21762
TI
3095static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3096 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3097{
3098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3099 int idx = kcontrol->private_value;
e3245cdd
TI
3100 struct hda_spdif_out *spdif;
3101 hda_nid_t nid;
1da177e4
LT
3102 unsigned short val;
3103 int change;
3104
62932df8 3105 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
3106 spdif = snd_array_elem(&codec->spdif_out, idx);
3107 nid = spdif->nid;
7c935976 3108 val = spdif->ctls & ~AC_DIG1_ENABLE;
1da177e4 3109 if (ucontrol->value.integer.value[0])
0ba21762 3110 val |= AC_DIG1_ENABLE;
7c935976 3111 change = spdif->ctls != val;
74b654c9
SW
3112 spdif->ctls = val;
3113 if (change && nid != (u16)-1)
3114 set_spdif_ctls(codec, nid, val & 0xff, -1);
62932df8 3115 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3116 return change;
3117}
3118
c8b6bf9b 3119static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
3120 {
3121 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3123 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1da177e4
LT
3124 .info = snd_hda_spdif_mask_info,
3125 .get = snd_hda_spdif_cmask_get,
3126 },
3127 {
3128 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3129 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3130 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1da177e4
LT
3131 .info = snd_hda_spdif_mask_info,
3132 .get = snd_hda_spdif_pmask_get,
3133 },
3134 {
3135 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3136 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1da177e4
LT
3137 .info = snd_hda_spdif_mask_info,
3138 .get = snd_hda_spdif_default_get,
3139 .put = snd_hda_spdif_default_put,
3140 },
3141 {
3142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3143 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1da177e4
LT
3144 .info = snd_hda_spdif_out_switch_info,
3145 .get = snd_hda_spdif_out_switch_get,
3146 .put = snd_hda_spdif_out_switch_put,
3147 },
3148 { } /* end */
3149};
3150
3151/**
dcda5806 3152 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
1da177e4 3153 * @codec: the HDA codec
dcda5806
TI
3154 * @associated_nid: NID that new ctls associated with
3155 * @cvt_nid: converter NID
3156 * @type: HDA_PCM_TYPE_*
3157 * Creates controls related with the digital output.
3158 * Called from each patch supporting the digital out.
1da177e4
LT
3159 *
3160 * Returns 0 if successful, or a negative error code.
3161 */
dcda5806
TI
3162int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3163 hda_nid_t associated_nid,
3164 hda_nid_t cvt_nid,
3165 int type)
1da177e4
LT
3166{
3167 int err;
c8b6bf9b
TI
3168 struct snd_kcontrol *kctl;
3169 struct snd_kcontrol_new *dig_mix;
dcda5806
TI
3170 int idx, dev = 0;
3171 const int spdif_pcm_dev = 1;
7c935976 3172 struct hda_spdif_out *spdif;
1da177e4 3173
dcda5806
TI
3174 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3175 type == HDA_PCM_TYPE_SPDIF) {
3176 dev = spdif_pcm_dev;
3177 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3178 type == HDA_PCM_TYPE_HDMI) {
3179 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3180 spdif = snd_array_elem(&codec->spdif_out, idx);
3181 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3182 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3183 if (!kctl)
3184 break;
3185 kctl->id.device = spdif_pcm_dev;
3186 }
3187 }
3188 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3189 }
3190 if (!codec->primary_dig_out_type)
3191 codec->primary_dig_out_type = type;
3192
3193 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
1afe206a 3194 if (idx < 0) {
09f99701
TI
3195 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3196 return -EBUSY;
3197 }
7c935976 3198 spdif = snd_array_new(&codec->spdif_out);
1da177e4
LT
3199 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3200 kctl = snd_ctl_new1(dig_mix, codec);
b91f080f
TI
3201 if (!kctl)
3202 return -ENOMEM;
dcda5806 3203 kctl->id.device = dev;
09f99701 3204 kctl->id.index = idx;
7c935976 3205 kctl->private_value = codec->spdif_out.used - 1;
74b654c9 3206 err = snd_hda_ctl_add(codec, associated_nid, kctl);
0ba21762 3207 if (err < 0)
1da177e4
LT
3208 return err;
3209 }
74b654c9
SW
3210 spdif->nid = cvt_nid;
3211 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
7c935976
SW
3212 AC_VERB_GET_DIGI_CONVERT_1, 0);
3213 spdif->status = convert_to_spdif_status(spdif->ctls);
1da177e4
LT
3214 return 0;
3215}
dcda5806 3216EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
1da177e4 3217
e3245cdd
TI
3218/* get the hda_spdif_out entry from the given NID
3219 * call within spdif_mutex lock
3220 */
7c935976
SW
3221struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3222 hda_nid_t nid)
3223{
3224 int i;
3225 for (i = 0; i < codec->spdif_out.used; i++) {
3226 struct hda_spdif_out *spdif =
3227 snd_array_elem(&codec->spdif_out, i);
3228 if (spdif->nid == nid)
3229 return spdif;
3230 }
3231 return NULL;
3232}
3233EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3234
74b654c9
SW
3235void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3236{
e3245cdd 3237 struct hda_spdif_out *spdif;
74b654c9
SW
3238
3239 mutex_lock(&codec->spdif_mutex);
e3245cdd 3240 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
3241 spdif->nid = (u16)-1;
3242 mutex_unlock(&codec->spdif_mutex);
3243}
3244EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3245
3246void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3247{
e3245cdd 3248 struct hda_spdif_out *spdif;
74b654c9
SW
3249 unsigned short val;
3250
3251 mutex_lock(&codec->spdif_mutex);
e3245cdd 3252 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
3253 if (spdif->nid != nid) {
3254 spdif->nid = nid;
3255 val = spdif->ctls;
3256 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3257 }
3258 mutex_unlock(&codec->spdif_mutex);
3259}
3260EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3261
9a08160b
TI
3262/*
3263 * SPDIF sharing with analog output
3264 */
3265static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3266 struct snd_ctl_elem_value *ucontrol)
3267{
3268 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3269 ucontrol->value.integer.value[0] = mout->share_spdif;
3270 return 0;
3271}
3272
3273static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3275{
3276 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3277 mout->share_spdif = !!ucontrol->value.integer.value[0];
3278 return 0;
3279}
3280
3281static struct snd_kcontrol_new spdif_share_sw = {
3282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3283 .name = "IEC958 Default PCM Playback Switch",
3284 .info = snd_ctl_boolean_mono_info,
3285 .get = spdif_share_sw_get,
3286 .put = spdif_share_sw_put,
3287};
3288
d5191e50
TI
3289/**
3290 * snd_hda_create_spdif_share_sw - create Default PCM switch
3291 * @codec: the HDA codec
3292 * @mout: multi-out instance
3293 */
9a08160b
TI
3294int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3295 struct hda_multi_out *mout)
3296{
3297 if (!mout->dig_out_nid)
3298 return 0;
3299 /* ATTENTION: here mout is passed as private_data, instead of codec */
3911a4c1
JK
3300 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3301 snd_ctl_new1(&spdif_share_sw, mout));
9a08160b 3302}
ff7a3267 3303EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
9a08160b 3304
1da177e4
LT
3305/*
3306 * SPDIF input
3307 */
3308
3309#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3310
0ba21762
TI
3311static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3313{
3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3315
3316 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3317 return 0;
3318}
3319
0ba21762
TI
3320static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3322{
3323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3324 hda_nid_t nid = kcontrol->private_value;
3325 unsigned int val = !!ucontrol->value.integer.value[0];
3326 int change;
3327
62932df8 3328 mutex_lock(&codec->spdif_mutex);
1da177e4 3329 change = codec->spdif_in_enable != val;
82beb8fd 3330 if (change) {
1da177e4 3331 codec->spdif_in_enable = val;
82beb8fd
TI
3332 snd_hda_codec_write_cache(codec, nid, 0,
3333 AC_VERB_SET_DIGI_CONVERT_1, val);
1da177e4 3334 }
62932df8 3335 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3336 return change;
3337}
3338
0ba21762
TI
3339static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3340 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3341{
3342 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3343 hda_nid_t nid = kcontrol->private_value;
3344 unsigned short val;
3345 unsigned int sbits;
3346
3982d17e 3347 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1da177e4
LT
3348 sbits = convert_to_spdif_status(val);
3349 ucontrol->value.iec958.status[0] = sbits;
3350 ucontrol->value.iec958.status[1] = sbits >> 8;
3351 ucontrol->value.iec958.status[2] = sbits >> 16;
3352 ucontrol->value.iec958.status[3] = sbits >> 24;
3353 return 0;
3354}
3355
c8b6bf9b 3356static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
3357 {
3358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3359 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
1da177e4
LT
3360 .info = snd_hda_spdif_in_switch_info,
3361 .get = snd_hda_spdif_in_switch_get,
3362 .put = snd_hda_spdif_in_switch_put,
3363 },
3364 {
3365 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3367 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
1da177e4
LT
3368 .info = snd_hda_spdif_mask_info,
3369 .get = snd_hda_spdif_in_status_get,
3370 },
3371 { } /* end */
3372};
3373
3374/**
3375 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3376 * @codec: the HDA codec
3377 * @nid: audio in widget NID
3378 *
3379 * Creates controls related with the SPDIF input.
3380 * Called from each patch supporting the SPDIF in.
3381 *
3382 * Returns 0 if successful, or a negative error code.
3383 */
12f288bf 3384int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
3385{
3386 int err;
c8b6bf9b
TI
3387 struct snd_kcontrol *kctl;
3388 struct snd_kcontrol_new *dig_mix;
09f99701 3389 int idx;
1da177e4 3390
dcda5806 3391 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
1afe206a 3392 if (idx < 0) {
09f99701
TI
3393 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3394 return -EBUSY;
3395 }
1da177e4
LT
3396 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3397 kctl = snd_ctl_new1(dig_mix, codec);
c8dcdf82
TI
3398 if (!kctl)
3399 return -ENOMEM;
1da177e4 3400 kctl->private_value = nid;
3911a4c1 3401 err = snd_hda_ctl_add(codec, nid, kctl);
0ba21762 3402 if (err < 0)
1da177e4
LT
3403 return err;
3404 }
0ba21762 3405 codec->spdif_in_enable =
3982d17e
AP
3406 snd_hda_codec_read(codec, nid, 0,
3407 AC_VERB_GET_DIGI_CONVERT_1, 0) &
0ba21762 3408 AC_DIG1_ENABLE;
1da177e4
LT
3409 return 0;
3410}
ff7a3267 3411EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
1da177e4 3412
2a43952a 3413#ifdef CONFIG_PM
82beb8fd
TI
3414/*
3415 * command cache
3416 */
1da177e4 3417
b3ac5636
TI
3418/* build a 32bit cache key with the widget id and the command parameter */
3419#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3420#define get_cmd_cache_nid(key) ((key) & 0xff)
3421#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3422
3423/**
3424 * snd_hda_codec_write_cache - send a single command with caching
3425 * @codec: the HDA codec
3426 * @nid: NID to send the command
3427 * @direct: direct flag
3428 * @verb: the verb to send
3429 * @parm: the parameter for the verb
3430 *
3431 * Send a single command without waiting for response.
3432 *
3433 * Returns 0 if successful, or a negative error code.
3434 */
3435int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3436 int direct, unsigned int verb, unsigned int parm)
3437{
aa2936f5
TI
3438 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3439 struct hda_cache_head *c;
3440 u32 key;
33fa35ed 3441
aa2936f5
TI
3442 if (err < 0)
3443 return err;
3444 /* parm may contain the verb stuff for get/set amp */
3445 verb = verb | (parm >> 8);
3446 parm &= 0xff;
3447 key = build_cmd_cache_key(nid, verb);
3448 mutex_lock(&codec->bus->cmd_mutex);
3449 c = get_alloc_hash(&codec->cmd_cache, key);
3450 if (c)
3451 c->val = parm;
3452 mutex_unlock(&codec->bus->cmd_mutex);
3453 return 0;
b3ac5636 3454}
ff7a3267 3455EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
b3ac5636 3456
a68d5a54
TI
3457/**
3458 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3459 * @codec: the HDA codec
3460 * @nid: NID to send the command
3461 * @direct: direct flag
3462 * @verb: the verb to send
3463 * @parm: the parameter for the verb
3464 *
3465 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3466 * command if the parameter is already identical with the cached value.
3467 * If not, it sends the command and refreshes the cache.
3468 *
3469 * Returns 0 if successful, or a negative error code.
3470 */
3471int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3472 int direct, unsigned int verb, unsigned int parm)
3473{
3474 struct hda_cache_head *c;
3475 u32 key;
3476
3477 /* parm may contain the verb stuff for get/set amp */
3478 verb = verb | (parm >> 8);
3479 parm &= 0xff;
3480 key = build_cmd_cache_key(nid, verb);
3481 mutex_lock(&codec->bus->cmd_mutex);
3482 c = get_hash(&codec->cmd_cache, key);
3483 if (c && c->val == parm) {
3484 mutex_unlock(&codec->bus->cmd_mutex);
3485 return 0;
3486 }
3487 mutex_unlock(&codec->bus->cmd_mutex);
3488 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3489}
3490EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3491
d5191e50
TI
3492/**
3493 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3494 * @codec: HD-audio codec
3495 *
3496 * Execute all verbs recorded in the command caches to resume.
3497 */
b3ac5636
TI
3498void snd_hda_codec_resume_cache(struct hda_codec *codec)
3499{
603c4019 3500 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
b3ac5636
TI
3501 int i;
3502
603c4019 3503 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
b3ac5636
TI
3504 u32 key = buffer->key;
3505 if (!key)
3506 continue;
3507 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3508 get_cmd_cache_cmd(key), buffer->val);
3509 }
3510}
ff7a3267 3511EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
b3ac5636
TI
3512
3513/**
3514 * snd_hda_sequence_write_cache - sequence writes with caching
3515 * @codec: the HDA codec
3516 * @seq: VERB array to send
3517 *
3518 * Send the commands sequentially from the given array.
3519 * Thte commands are recorded on cache for power-save and resume.
3520 * The array must be terminated with NID=0.
3521 */
3522void snd_hda_sequence_write_cache(struct hda_codec *codec,
3523 const struct hda_verb *seq)
3524{
3525 for (; seq->nid; seq++)
3526 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3527 seq->param);
3528}
ff7a3267 3529EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2a43952a 3530#endif /* CONFIG_PM */
b3ac5636 3531
4d7fbdbc
TI
3532void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3533 unsigned int power_state,
3534 bool eapd_workaround)
54d17403 3535{
4d7fbdbc 3536 hda_nid_t nid = codec->start_nid;
cb53c626 3537 int i;
54d17403 3538
cb53c626 3539 for (i = 0; i < codec->num_nodes; i++, nid++) {
7eba5c9d 3540 unsigned int wcaps = get_wcaps(codec, nid);
4d7fbdbc
TI
3541 if (!(wcaps & AC_WCAP_POWER))
3542 continue;
3543 /* don't power down the widget if it controls eapd and
3544 * EAPD_BTLENABLE is set.
3545 */
3546 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3547 get_wcaps_type(wcaps) == AC_WID_PIN &&
3548 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3549 int eapd = snd_hda_codec_read(codec, nid, 0,
7eba5c9d 3550 AC_VERB_GET_EAPD_BTLENABLE, 0);
4d7fbdbc
TI
3551 if (eapd & 0x02)
3552 continue;
1194b5b7 3553 }
4d7fbdbc
TI
3554 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3555 power_state);
54d17403 3556 }
cb53c626 3557}
4d7fbdbc
TI
3558EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3559
0c7f46ad
WX
3560/*
3561 * supported power states check
3562 */
3563static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3564 unsigned int power_state)
3565{
3566 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3567
e037cb4a 3568 if (sup == -1)
0c7f46ad
WX
3569 return false;
3570 if (sup & power_state)
3571 return true;
3572 else
3573 return false;
3574}
3575
432c641e
TI
3576/*
3577 * wait until the state is reached, returns the current state
3578 */
3579static unsigned int hda_sync_power_state(struct hda_codec *codec,
3580 hda_nid_t fg,
3581 unsigned int power_state)
3582{
3583 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3584 unsigned int state, actual_state;
3585
3586 for (;;) {
3587 state = snd_hda_codec_read(codec, fg, 0,
3588 AC_VERB_GET_POWER_STATE, 0);
3589 if (state & AC_PWRST_ERROR)
3590 break;
3591 actual_state = (state >> 4) & 0x0f;
3592 if (actual_state == power_state)
3593 break;
3594 if (time_after_eq(jiffies, end_time))
3595 break;
3596 /* wait until the codec reachs to the target state */
3597 msleep(1);
3598 }
3599 return state;
3600}
3601
4d7fbdbc 3602/*
08fa20ae 3603 * set power state of the codec, and return the power state
4d7fbdbc 3604 */
d819387e 3605static unsigned int hda_set_power_state(struct hda_codec *codec,
08fa20ae 3606 unsigned int power_state)
4d7fbdbc 3607{
d819387e 3608 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
09617ce4
WX
3609 int count;
3610 unsigned int state;
3611
4d7fbdbc 3612 /* this delay seems necessary to avoid click noise at power-down */
0f4ccbb0
WX
3613 if (power_state == AC_PWRST_D3) {
3614 /* transition time less than 10ms for power down */
983f6b93 3615 msleep(codec->epss ? 10 : 100);
0f4ccbb0 3616 }
09617ce4
WX
3617
3618 /* repeat power states setting at most 10 times*/
3619 for (count = 0; count < 10; count++) {
432c641e
TI
3620 if (codec->patch_ops.set_power_state)
3621 codec->patch_ops.set_power_state(codec, fg,
3622 power_state);
3623 else {
3624 snd_hda_codec_read(codec, fg, 0,
3625 AC_VERB_SET_POWER_STATE,
3626 power_state);
3627 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3628 true);
3629 }
3630 state = hda_sync_power_state(codec, fg, power_state);
09617ce4
WX
3631 if (!(state & AC_PWRST_ERROR))
3632 break;
3633 }
b8dfc462 3634
08fa20ae 3635 return state;
4d7fbdbc 3636}
cb53c626 3637
11aeff08
TI
3638#ifdef CONFIG_SND_HDA_HWDEP
3639/* execute additional init verbs */
3640static void hda_exec_init_verbs(struct hda_codec *codec)
3641{
3642 if (codec->init_verbs.list)
3643 snd_hda_sequence_write(codec, codec->init_verbs.list);
3644}
3645#else
3646static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3647#endif
3648
2a43952a 3649#ifdef CONFIG_PM
cb53c626
TI
3650/*
3651 * call suspend and power-down; used both from PM and power-save
08fa20ae 3652 * this function returns the power state in the end
cb53c626 3653 */
d17344b3 3654static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
cb53c626 3655{
08fa20ae
TI
3656 unsigned int state;
3657
989c3187
TI
3658 codec->in_pm = 1;
3659
cb53c626 3660 if (codec->patch_ops.suspend)
68cb2b55 3661 codec->patch_ops.suspend(codec);
eb541337 3662 hda_cleanup_all_streams(codec);
d819387e 3663 state = hda_set_power_state(codec, AC_PWRST_D3);
d17344b3
DR
3664 /* Cancel delayed work if we aren't currently running from it. */
3665 if (!in_wq)
3666 cancel_delayed_work_sync(&codec->power_work);
a2d96e77
TI
3667 spin_lock(&codec->power_lock);
3668 snd_hda_update_power_acct(codec);
3669 trace_hda_power_down(codec);
95e99fda 3670 codec->power_on = 0;
a221e287 3671 codec->power_transition = 0;
a2f6309e 3672 codec->power_jiffies = jiffies;
a2d96e77 3673 spin_unlock(&codec->power_lock);
989c3187 3674 codec->in_pm = 0;
08fa20ae 3675 return state;
54d17403
TI
3676}
3677
cb53c626
TI
3678/*
3679 * kick up codec; used both from PM and power-save
3680 */
3681static void hda_call_codec_resume(struct hda_codec *codec)
3682{
989c3187
TI
3683 codec->in_pm = 1;
3684
7f30830b
TI
3685 /* set as if powered on for avoiding re-entering the resume
3686 * in the resume / power-save sequence
3687 */
3688 hda_keep_power_on(codec);
d819387e 3689 hda_set_power_state(codec, AC_PWRST_D0);
3be14149 3690 restore_pincfgs(codec); /* restore all current pin configs */
ac0547dc 3691 restore_shutup_pins(codec);
11aeff08 3692 hda_exec_init_verbs(codec);
cb53c626
TI
3693 if (codec->patch_ops.resume)
3694 codec->patch_ops.resume(codec);
3695 else {
9d99f312
TI
3696 if (codec->patch_ops.init)
3697 codec->patch_ops.init(codec);
cb53c626
TI
3698 snd_hda_codec_resume_amp(codec);
3699 snd_hda_codec_resume_cache(codec);
3700 }
26a6cb6c
DH
3701
3702 if (codec->jackpoll_interval)
3703 hda_jackpoll_work(&codec->jackpoll_work.work);
3704 else {
3705 snd_hda_jack_set_dirty_all(codec);
3706 snd_hda_jack_report_sync(codec);
3707 }
989c3187
TI
3708
3709 codec->in_pm = 0;
7f30830b 3710 snd_hda_power_down(codec); /* flag down before returning */
cb53c626 3711}
2a43952a 3712#endif /* CONFIG_PM */
cb53c626 3713
54d17403 3714
1da177e4
LT
3715/**
3716 * snd_hda_build_controls - build mixer controls
3717 * @bus: the BUS
3718 *
3719 * Creates mixer controls for each codec included in the bus.
3720 *
3721 * Returns 0 if successful, otherwise a negative error code.
3722 */
1289e9e8 3723int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
1da177e4 3724{
0ba21762 3725 struct hda_codec *codec;
1da177e4 3726
0ba21762 3727 list_for_each_entry(codec, &bus->codec_list, list) {
6c1f45ea 3728 int err = snd_hda_codec_build_controls(codec);
f93d461b 3729 if (err < 0) {
28d1a85e 3730 printk(KERN_ERR "hda_codec: cannot build controls "
28aedaf7 3731 "for #%d (error %d)\n", codec->addr, err);
f93d461b
TI
3732 err = snd_hda_codec_reset(codec);
3733 if (err < 0) {
3734 printk(KERN_ERR
3735 "hda_codec: cannot revert codec\n");
3736 return err;
3737 }
3738 }
1da177e4 3739 }
6c1f45ea
TI
3740 return 0;
3741}
ff7a3267 3742EXPORT_SYMBOL_HDA(snd_hda_build_controls);
cb53c626 3743
9c9a5175
TI
3744/*
3745 * add standard channel maps if not specified
3746 */
3747static int add_std_chmaps(struct hda_codec *codec)
3748{
3749 int i, str, err;
3750
3751 for (i = 0; i < codec->num_pcms; i++) {
3752 for (str = 0; str < 2; str++) {
3753 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3754 struct hda_pcm_stream *hinfo =
3755 &codec->pcm_info[i].stream[str];
3756 struct snd_pcm_chmap *chmap;
ee81abb6 3757 const struct snd_pcm_chmap_elem *elem;
9c9a5175
TI
3758
3759 if (codec->pcm_info[i].own_chmap)
3760 continue;
3761 if (!pcm || !hinfo->substreams)
3762 continue;
ee81abb6
TI
3763 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3764 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
9c9a5175
TI
3765 hinfo->channels_max,
3766 0, &chmap);
3767 if (err < 0)
3768 return err;
3769 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3770 }
3771 }
3772 return 0;
3773}
3774
ee81abb6
TI
3775/* default channel maps for 2.1 speakers;
3776 * since HD-audio supports only stereo, odd number channels are omitted
3777 */
3778const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3779 { .channels = 2,
3780 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3781 { .channels = 4,
3782 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3783 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3784 { }
3785};
3786EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3787
6c1f45ea
TI
3788int snd_hda_codec_build_controls(struct hda_codec *codec)
3789{
3790 int err = 0;
11aeff08 3791 hda_exec_init_verbs(codec);
6c1f45ea
TI
3792 /* continue to initialize... */
3793 if (codec->patch_ops.init)
3794 err = codec->patch_ops.init(codec);
3795 if (!err && codec->patch_ops.build_controls)
3796 err = codec->patch_ops.build_controls(codec);
6c1f45ea
TI
3797 if (err < 0)
3798 return err;
9c9a5175
TI
3799
3800 /* we create chmaps here instead of build_pcms */
3801 err = add_std_chmaps(codec);
3802 if (err < 0)
3803 return err;
3804
26a6cb6c
DH
3805 if (codec->jackpoll_interval)
3806 hda_jackpoll_work(&codec->jackpoll_work.work);
3807 else
3808 snd_hda_jack_report_sync(codec); /* call at the last init point */
1da177e4
LT
3809 return 0;
3810}
3811
1da177e4
LT
3812/*
3813 * stream formats
3814 */
befdf316
TI
3815struct hda_rate_tbl {
3816 unsigned int hz;
3817 unsigned int alsa_bits;
3818 unsigned int hda_fmt;
3819};
3820
92f10b3f
TI
3821/* rate = base * mult / div */
3822#define HDA_RATE(base, mult, div) \
3823 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3824 (((div) - 1) << AC_FMT_DIV_SHIFT))
3825
befdf316 3826static struct hda_rate_tbl rate_bits[] = {
1da177e4 3827 /* rate in Hz, ALSA rate bitmask, HDA format value */
9d8f53f2
NG
3828
3829 /* autodetected value used in snd_hda_query_supported_pcm */
92f10b3f
TI
3830 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3831 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3832 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3833 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3834 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3835 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3836 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3837 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3838 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3839 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3840 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
a961f9fe
TI
3841#define AC_PAR_PCM_RATE_BITS 11
3842 /* up to bits 10, 384kHZ isn't supported properly */
3843
3844 /* not autodetected value */
92f10b3f 3845 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
9d8f53f2 3846
befdf316 3847 { 0 } /* terminator */
1da177e4
LT
3848};
3849
3850/**
3851 * snd_hda_calc_stream_format - calculate format bitset
3852 * @rate: the sample rate
3853 * @channels: the number of channels
3854 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3855 * @maxbps: the max. bps
3856 *
3857 * Calculate the format bitset from the given rate, channels and th PCM format.
3858 *
3859 * Return zero if invalid.
3860 */
3861unsigned int snd_hda_calc_stream_format(unsigned int rate,
3862 unsigned int channels,
3863 unsigned int format,
32c168c8
AH
3864 unsigned int maxbps,
3865 unsigned short spdif_ctls)
1da177e4
LT
3866{
3867 int i;
3868 unsigned int val = 0;
3869
befdf316
TI
3870 for (i = 0; rate_bits[i].hz; i++)
3871 if (rate_bits[i].hz == rate) {
3872 val = rate_bits[i].hda_fmt;
1da177e4
LT
3873 break;
3874 }
0ba21762 3875 if (!rate_bits[i].hz) {
1da177e4
LT
3876 snd_printdd("invalid rate %d\n", rate);
3877 return 0;
3878 }
3879
3880 if (channels == 0 || channels > 8) {
3881 snd_printdd("invalid channels %d\n", channels);
3882 return 0;
3883 }
3884 val |= channels - 1;
3885
3886 switch (snd_pcm_format_width(format)) {
28aedaf7 3887 case 8:
92f10b3f 3888 val |= AC_FMT_BITS_8;
28aedaf7
NL
3889 break;
3890 case 16:
92f10b3f 3891 val |= AC_FMT_BITS_16;
28aedaf7 3892 break;
1da177e4
LT
3893 case 20:
3894 case 24:
3895 case 32:
b0bb3aa6 3896 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
92f10b3f 3897 val |= AC_FMT_BITS_32;
1da177e4 3898 else if (maxbps >= 24)
92f10b3f 3899 val |= AC_FMT_BITS_24;
1da177e4 3900 else
92f10b3f 3901 val |= AC_FMT_BITS_20;
1da177e4
LT
3902 break;
3903 default:
0ba21762
TI
3904 snd_printdd("invalid format width %d\n",
3905 snd_pcm_format_width(format));
1da177e4
LT
3906 return 0;
3907 }
3908
32c168c8 3909 if (spdif_ctls & AC_DIG1_NONAUDIO)
92f10b3f 3910 val |= AC_FMT_TYPE_NON_PCM;
32c168c8 3911
1da177e4
LT
3912 return val;
3913}
ff7a3267 3914EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
1da177e4 3915
c3b6bcc2
TI
3916static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3917 int dir)
92c7c8a7
TI
3918{
3919 unsigned int val = 0;
3920 if (nid != codec->afg &&
3921 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3922 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3923 if (!val || val == -1)
3924 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3925 if (!val || val == -1)
3926 return 0;
3927 return val;
3928}
3929
3930static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3931{
c3b6bcc2 3932 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
92c7c8a7
TI
3933 get_pcm_param);
3934}
3935
c3b6bcc2
TI
3936static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3937 int dir)
92c7c8a7
TI
3938{
3939 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3940 if (!streams || streams == -1)
3941 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3942 if (!streams || streams == -1)
3943 return 0;
3944 return streams;
3945}
3946
3947static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3948{
c3b6bcc2 3949 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
92c7c8a7
TI
3950 get_stream_param);
3951}
3952
1da177e4
LT
3953/**
3954 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3955 * @codec: the HDA codec
3956 * @nid: NID to query
3957 * @ratesp: the pointer to store the detected rate bitflags
3958 * @formatsp: the pointer to store the detected formats
3959 * @bpsp: the pointer to store the detected format widths
3960 *
3961 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3962 * or @bsps argument is ignored.
3963 *
3964 * Returns 0 if successful, otherwise a negative error code.
3965 */
384a48d7 3966int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1da177e4
LT
3967 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3968{
ee504710 3969 unsigned int i, val, wcaps;
1da177e4 3970
ee504710 3971 wcaps = get_wcaps(codec, nid);
92c7c8a7 3972 val = query_pcm_param(codec, nid);
1da177e4
LT
3973
3974 if (ratesp) {
3975 u32 rates = 0;
a961f9fe 3976 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1da177e4 3977 if (val & (1 << i))
befdf316 3978 rates |= rate_bits[i].alsa_bits;
1da177e4 3979 }
ee504710
JK
3980 if (rates == 0) {
3981 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3982 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3983 nid, val,
3984 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3985 return -EIO;
3986 }
1da177e4
LT
3987 *ratesp = rates;
3988 }
3989
3990 if (formatsp || bpsp) {
3991 u64 formats = 0;
ee504710 3992 unsigned int streams, bps;
1da177e4 3993
92c7c8a7
TI
3994 streams = query_stream_param(codec, nid);
3995 if (!streams)
1da177e4 3996 return -EIO;
1da177e4
LT
3997
3998 bps = 0;
3999 if (streams & AC_SUPFMT_PCM) {
4000 if (val & AC_SUPPCM_BITS_8) {
4001 formats |= SNDRV_PCM_FMTBIT_U8;
4002 bps = 8;
4003 }
4004 if (val & AC_SUPPCM_BITS_16) {
4005 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4006 bps = 16;
4007 }
4008 if (wcaps & AC_WCAP_DIGITAL) {
4009 if (val & AC_SUPPCM_BITS_32)
4010 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4011 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4012 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4013 if (val & AC_SUPPCM_BITS_24)
4014 bps = 24;
4015 else if (val & AC_SUPPCM_BITS_20)
4016 bps = 20;
0ba21762
TI
4017 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4018 AC_SUPPCM_BITS_32)) {
1da177e4
LT
4019 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4020 if (val & AC_SUPPCM_BITS_32)
4021 bps = 32;
1da177e4
LT
4022 else if (val & AC_SUPPCM_BITS_24)
4023 bps = 24;
33ef7651
NG
4024 else if (val & AC_SUPPCM_BITS_20)
4025 bps = 20;
1da177e4
LT
4026 }
4027 }
8c7dd890 4028#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
b5025c50 4029 if (streams & AC_SUPFMT_FLOAT32) {
1da177e4 4030 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
b0bb3aa6
TI
4031 if (!bps)
4032 bps = 32;
b5025c50 4033 }
8c7dd890 4034#endif
b5025c50 4035 if (streams == AC_SUPFMT_AC3) {
0ba21762 4036 /* should be exclusive */
1da177e4
LT
4037 /* temporary hack: we have still no proper support
4038 * for the direct AC3 stream...
4039 */
4040 formats |= SNDRV_PCM_FMTBIT_U8;
4041 bps = 8;
4042 }
ee504710
JK
4043 if (formats == 0) {
4044 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4045 "(nid=0x%x, val=0x%x, ovrd=%i, "
4046 "streams=0x%x)\n",
4047 nid, val,
4048 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4049 streams);
4050 return -EIO;
4051 }
1da177e4
LT
4052 if (formatsp)
4053 *formatsp = formats;
4054 if (bpsp)
4055 *bpsp = bps;
4056 }
4057
4058 return 0;
4059}
384a48d7 4060EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
1da177e4
LT
4061
4062/**
d5191e50
TI
4063 * snd_hda_is_supported_format - Check the validity of the format
4064 * @codec: HD-audio codec
4065 * @nid: NID to check
4066 * @format: the HD-audio format value to check
4067 *
4068 * Check whether the given node supports the format value.
1da177e4
LT
4069 *
4070 * Returns 1 if supported, 0 if not.
4071 */
4072int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4073 unsigned int format)
4074{
4075 int i;
4076 unsigned int val = 0, rate, stream;
4077
92c7c8a7
TI
4078 val = query_pcm_param(codec, nid);
4079 if (!val)
4080 return 0;
1da177e4
LT
4081
4082 rate = format & 0xff00;
a961f9fe 4083 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
befdf316 4084 if (rate_bits[i].hda_fmt == rate) {
1da177e4
LT
4085 if (val & (1 << i))
4086 break;
4087 return 0;
4088 }
a961f9fe 4089 if (i >= AC_PAR_PCM_RATE_BITS)
1da177e4
LT
4090 return 0;
4091
92c7c8a7
TI
4092 stream = query_stream_param(codec, nid);
4093 if (!stream)
1da177e4
LT
4094 return 0;
4095
4096 if (stream & AC_SUPFMT_PCM) {
4097 switch (format & 0xf0) {
4098 case 0x00:
0ba21762 4099 if (!(val & AC_SUPPCM_BITS_8))
1da177e4
LT
4100 return 0;
4101 break;
4102 case 0x10:
0ba21762 4103 if (!(val & AC_SUPPCM_BITS_16))
1da177e4
LT
4104 return 0;
4105 break;
4106 case 0x20:
0ba21762 4107 if (!(val & AC_SUPPCM_BITS_20))
1da177e4
LT
4108 return 0;
4109 break;
4110 case 0x30:
0ba21762 4111 if (!(val & AC_SUPPCM_BITS_24))
1da177e4
LT
4112 return 0;
4113 break;
4114 case 0x40:
0ba21762 4115 if (!(val & AC_SUPPCM_BITS_32))
1da177e4
LT
4116 return 0;
4117 break;
4118 default:
4119 return 0;
4120 }
4121 } else {
4122 /* FIXME: check for float32 and AC3? */
4123 }
4124
4125 return 1;
4126}
ff7a3267 4127EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
1da177e4
LT
4128
4129/*
4130 * PCM stuff
4131 */
4132static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4133 struct hda_codec *codec,
c8b6bf9b 4134 struct snd_pcm_substream *substream)
1da177e4
LT
4135{
4136 return 0;
4137}
4138
4139static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4140 struct hda_codec *codec,
4141 unsigned int stream_tag,
4142 unsigned int format,
c8b6bf9b 4143 struct snd_pcm_substream *substream)
1da177e4
LT
4144{
4145 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4146 return 0;
4147}
4148
4149static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4150 struct hda_codec *codec,
c8b6bf9b 4151 struct snd_pcm_substream *substream)
1da177e4 4152{
888afa15 4153 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1da177e4
LT
4154 return 0;
4155}
4156
6c1f45ea
TI
4157static int set_pcm_default_values(struct hda_codec *codec,
4158 struct hda_pcm_stream *info)
1da177e4 4159{
ee504710
JK
4160 int err;
4161
0ba21762
TI
4162 /* query support PCM information from the given NID */
4163 if (info->nid && (!info->rates || !info->formats)) {
ee504710 4164 err = snd_hda_query_supported_pcm(codec, info->nid,
0ba21762
TI
4165 info->rates ? NULL : &info->rates,
4166 info->formats ? NULL : &info->formats,
4167 info->maxbps ? NULL : &info->maxbps);
ee504710
JK
4168 if (err < 0)
4169 return err;
1da177e4
LT
4170 }
4171 if (info->ops.open == NULL)
4172 info->ops.open = hda_pcm_default_open_close;
4173 if (info->ops.close == NULL)
4174 info->ops.close = hda_pcm_default_open_close;
4175 if (info->ops.prepare == NULL) {
da3cec35
TI
4176 if (snd_BUG_ON(!info->nid))
4177 return -EINVAL;
1da177e4
LT
4178 info->ops.prepare = hda_pcm_default_prepare;
4179 }
1da177e4 4180 if (info->ops.cleanup == NULL) {
da3cec35
TI
4181 if (snd_BUG_ON(!info->nid))
4182 return -EINVAL;
1da177e4
LT
4183 info->ops.cleanup = hda_pcm_default_cleanup;
4184 }
4185 return 0;
4186}
4187
eb541337
TI
4188/*
4189 * codec prepare/cleanup entries
4190 */
4191int snd_hda_codec_prepare(struct hda_codec *codec,
4192 struct hda_pcm_stream *hinfo,
4193 unsigned int stream,
4194 unsigned int format,
4195 struct snd_pcm_substream *substream)
4196{
4197 int ret;
3f50ac6a 4198 mutex_lock(&codec->bus->prepare_mutex);
eb541337
TI
4199 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4200 if (ret >= 0)
4201 purify_inactive_streams(codec);
3f50ac6a 4202 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
4203 return ret;
4204}
4205EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4206
4207void snd_hda_codec_cleanup(struct hda_codec *codec,
4208 struct hda_pcm_stream *hinfo,
4209 struct snd_pcm_substream *substream)
4210{
3f50ac6a 4211 mutex_lock(&codec->bus->prepare_mutex);
eb541337 4212 hinfo->ops.cleanup(hinfo, codec, substream);
3f50ac6a 4213 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
4214}
4215EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4216
d5191e50 4217/* global */
e3303235
JK
4218const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4219 "Audio", "SPDIF", "HDMI", "Modem"
4220};
4221
529bd6c4
TI
4222/*
4223 * get the empty PCM device number to assign
c8936222
TI
4224 *
4225 * note the max device number is limited by HDA_MAX_PCMS, currently 10
529bd6c4
TI
4226 */
4227static int get_empty_pcm_device(struct hda_bus *bus, int type)
4228{
f5d6def5
WF
4229 /* audio device indices; not linear to keep compatibility */
4230 static int audio_idx[HDA_PCM_NTYPES][5] = {
4231 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4232 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
92608bad 4233 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
f5d6def5 4234 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
529bd6c4 4235 };
f5d6def5
WF
4236 int i;
4237
4238 if (type >= HDA_PCM_NTYPES) {
529bd6c4
TI
4239 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4240 return -EINVAL;
4241 }
f5d6def5
WF
4242
4243 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4244 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4245 return audio_idx[type][i];
4246
01b65bfb
TI
4247 /* non-fixed slots starting from 10 */
4248 for (i = 10; i < 32; i++) {
4249 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4250 return i;
4251 }
4252
28aedaf7
NL
4253 snd_printk(KERN_WARNING "Too many %s devices\n",
4254 snd_hda_pcm_type_name[type]);
f5d6def5 4255 return -EAGAIN;
529bd6c4
TI
4256}
4257
176d5335
TI
4258/*
4259 * attach a new PCM stream
4260 */
529bd6c4 4261static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
176d5335 4262{
33fa35ed 4263 struct hda_bus *bus = codec->bus;
176d5335
TI
4264 struct hda_pcm_stream *info;
4265 int stream, err;
4266
b91f080f 4267 if (snd_BUG_ON(!pcm->name))
176d5335
TI
4268 return -EINVAL;
4269 for (stream = 0; stream < 2; stream++) {
4270 info = &pcm->stream[stream];
4271 if (info->substreams) {
4272 err = set_pcm_default_values(codec, info);
4273 if (err < 0)
4274 return err;
4275 }
4276 }
33fa35ed 4277 return bus->ops.attach_pcm(bus, codec, pcm);
176d5335
TI
4278}
4279
529bd6c4
TI
4280/* assign all PCMs of the given codec */
4281int snd_hda_codec_build_pcms(struct hda_codec *codec)
4282{
4283 unsigned int pcm;
4284 int err;
4285
4286 if (!codec->num_pcms) {
4287 if (!codec->patch_ops.build_pcms)
4288 return 0;
4289 err = codec->patch_ops.build_pcms(codec);
6e655bf2
TI
4290 if (err < 0) {
4291 printk(KERN_ERR "hda_codec: cannot build PCMs"
28aedaf7 4292 "for #%d (error %d)\n", codec->addr, err);
6e655bf2
TI
4293 err = snd_hda_codec_reset(codec);
4294 if (err < 0) {
4295 printk(KERN_ERR
4296 "hda_codec: cannot revert codec\n");
4297 return err;
4298 }
4299 }
529bd6c4
TI
4300 }
4301 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4302 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4303 int dev;
4304
4305 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
41b5b01a 4306 continue; /* no substreams assigned */
529bd6c4
TI
4307
4308 if (!cpcm->pcm) {
4309 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4310 if (dev < 0)
6e655bf2 4311 continue; /* no fatal error */
529bd6c4
TI
4312 cpcm->device = dev;
4313 err = snd_hda_attach_pcm(codec, cpcm);
6e655bf2
TI
4314 if (err < 0) {
4315 printk(KERN_ERR "hda_codec: cannot attach "
4316 "PCM stream %d for codec #%d\n",
4317 dev, codec->addr);
4318 continue; /* no fatal error */
4319 }
529bd6c4
TI
4320 }
4321 }
4322 return 0;
4323}
4324
1da177e4
LT
4325/**
4326 * snd_hda_build_pcms - build PCM information
4327 * @bus: the BUS
4328 *
4329 * Create PCM information for each codec included in the bus.
4330 *
4331 * The build_pcms codec patch is requested to set up codec->num_pcms and
4332 * codec->pcm_info properly. The array is referred by the top-level driver
4333 * to create its PCM instances.
4334 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4335 * callback.
4336 *
4337 * At least, substreams, channels_min and channels_max must be filled for
4338 * each stream. substreams = 0 indicates that the stream doesn't exist.
4339 * When rates and/or formats are zero, the supported values are queried
4340 * from the given nid. The nid is used also by the default ops.prepare
4341 * and ops.cleanup callbacks.
4342 *
4343 * The driver needs to call ops.open in its open callback. Similarly,
4344 * ops.close is supposed to be called in the close callback.
4345 * ops.prepare should be called in the prepare or hw_params callback
4346 * with the proper parameters for set up.
4347 * ops.cleanup should be called in hw_free for clean up of streams.
4348 *
25985edc 4349 * This function returns 0 if successful, or a negative error code.
1da177e4 4350 */
5cb543db 4351int snd_hda_build_pcms(struct hda_bus *bus)
1da177e4 4352{
0ba21762 4353 struct hda_codec *codec;
1da177e4 4354
0ba21762 4355 list_for_each_entry(codec, &bus->codec_list, list) {
529bd6c4
TI
4356 int err = snd_hda_codec_build_pcms(codec);
4357 if (err < 0)
4358 return err;
1da177e4
LT
4359 }
4360 return 0;
4361}
ff7a3267 4362EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
1da177e4 4363
1da177e4
LT
4364/**
4365 * snd_hda_check_board_config - compare the current codec with the config table
4366 * @codec: the HDA codec
f5fcc13c
TI
4367 * @num_configs: number of config enums
4368 * @models: array of model name strings
1da177e4
LT
4369 * @tbl: configuration table, terminated by null entries
4370 *
4371 * Compares the modelname or PCI subsystem id of the current codec with the
4372 * given configuration table. If a matching entry is found, returns its
4373 * config value (supposed to be 0 or positive).
4374 *
4375 * If no entries are matching, the function returns a negative value.
4376 */
12f288bf 4377int snd_hda_check_board_config(struct hda_codec *codec,
ea734963 4378 int num_configs, const char * const *models,
12f288bf 4379 const struct snd_pci_quirk *tbl)
1da177e4 4380{
f44ac837 4381 if (codec->modelname && models) {
f5fcc13c
TI
4382 int i;
4383 for (i = 0; i < num_configs; i++) {
4384 if (models[i] &&
f44ac837 4385 !strcmp(codec->modelname, models[i])) {
f5fcc13c
TI
4386 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4387 "selected\n", models[i]);
4388 return i;
1da177e4
LT
4389 }
4390 }
4391 }
4392
f5fcc13c
TI
4393 if (!codec->bus->pci || !tbl)
4394 return -1;
4395
4396 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4397 if (!tbl)
4398 return -1;
4399 if (tbl->value >= 0 && tbl->value < num_configs) {
62cf872a 4400#ifdef CONFIG_SND_DEBUG_VERBOSE
f5fcc13c
TI
4401 char tmp[10];
4402 const char *model = NULL;
4403 if (models)
4404 model = models[tbl->value];
4405 if (!model) {
4406 sprintf(tmp, "#%d", tbl->value);
4407 model = tmp;
1da177e4 4408 }
f5fcc13c
TI
4409 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4410 "for config %x:%x (%s)\n",
4411 model, tbl->subvendor, tbl->subdevice,
4412 (tbl->name ? tbl->name : "Unknown device"));
4413#endif
4414 return tbl->value;
1da177e4
LT
4415 }
4416 return -1;
4417}
ff7a3267 4418EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
1da177e4 4419
2eda3445
MCC
4420/**
4421 * snd_hda_check_board_codec_sid_config - compare the current codec
28aedaf7
NL
4422 subsystem ID with the
4423 config table
2eda3445
MCC
4424
4425 This is important for Gateway notebooks with SB450 HDA Audio
4426 where the vendor ID of the PCI device is:
4427 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4428 and the vendor/subvendor are found only at the codec.
4429
4430 * @codec: the HDA codec
4431 * @num_configs: number of config enums
4432 * @models: array of model name strings
4433 * @tbl: configuration table, terminated by null entries
4434 *
4435 * Compares the modelname or PCI subsystem id of the current codec with the
4436 * given configuration table. If a matching entry is found, returns its
4437 * config value (supposed to be 0 or positive).
4438 *
4439 * If no entries are matching, the function returns a negative value.
4440 */
4441int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
ea734963 4442 int num_configs, const char * const *models,
2eda3445
MCC
4443 const struct snd_pci_quirk *tbl)
4444{
4445 const struct snd_pci_quirk *q;
4446
4447 /* Search for codec ID */
4448 for (q = tbl; q->subvendor; q++) {
e2301a4d
TI
4449 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4450 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4451 if ((codec->subsystem_id & mask) == id)
2eda3445
MCC
4452 break;
4453 }
4454
4455 if (!q->subvendor)
4456 return -1;
4457
4458 tbl = q;
4459
4460 if (tbl->value >= 0 && tbl->value < num_configs) {
d94ff6b7 4461#ifdef CONFIG_SND_DEBUG_VERBOSE
2eda3445
MCC
4462 char tmp[10];
4463 const char *model = NULL;
4464 if (models)
4465 model = models[tbl->value];
4466 if (!model) {
4467 sprintf(tmp, "#%d", tbl->value);
4468 model = tmp;
4469 }
4470 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4471 "for config %x:%x (%s)\n",
4472 model, tbl->subvendor, tbl->subdevice,
4473 (tbl->name ? tbl->name : "Unknown device"));
4474#endif
4475 return tbl->value;
4476 }
4477 return -1;
4478}
4479EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4480
1da177e4
LT
4481/**
4482 * snd_hda_add_new_ctls - create controls from the array
4483 * @codec: the HDA codec
c8b6bf9b 4484 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
4485 *
4486 * This helper function creates and add new controls in the given array.
4487 * The array must be terminated with an empty entry as terminator.
4488 *
4489 * Returns 0 if successful, or a negative error code.
4490 */
031024ee
TI
4491int snd_hda_add_new_ctls(struct hda_codec *codec,
4492 const struct snd_kcontrol_new *knew)
1da177e4 4493{
4d02d1b6 4494 int err;
1da177e4
LT
4495
4496 for (; knew->name; knew++) {
54d17403 4497 struct snd_kcontrol *kctl;
1afe206a 4498 int addr = 0, idx = 0;
5b0cb1d8
JK
4499 if (knew->iface == -1) /* skip this codec private value */
4500 continue;
1afe206a 4501 for (;;) {
54d17403 4502 kctl = snd_ctl_new1(knew, codec);
0ba21762 4503 if (!kctl)
54d17403 4504 return -ENOMEM;
1afe206a
TI
4505 if (addr > 0)
4506 kctl->id.device = addr;
4507 if (idx > 0)
4508 kctl->id.index = idx;
3911a4c1 4509 err = snd_hda_ctl_add(codec, 0, kctl);
1afe206a
TI
4510 if (!err)
4511 break;
4512 /* try first with another device index corresponding to
4513 * the codec addr; if it still fails (or it's the
4514 * primary codec), then try another control index
4515 */
4516 if (!addr && codec->addr)
4517 addr = codec->addr;
4518 else if (!idx && !knew->index) {
4519 idx = find_empty_mixer_ctl_idx(codec,
dcda5806 4520 knew->name, 0);
1afe206a
TI
4521 if (idx <= 0)
4522 return err;
4523 } else
54d17403
TI
4524 return err;
4525 }
1da177e4
LT
4526 }
4527 return 0;
4528}
ff7a3267 4529EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
1da177e4 4530
83012a7c 4531#ifdef CONFIG_PM
cb53c626
TI
4532static void hda_power_work(struct work_struct *work)
4533{
4534 struct hda_codec *codec =
4535 container_of(work, struct hda_codec, power_work.work);
33fa35ed 4536 struct hda_bus *bus = codec->bus;
08fa20ae 4537 unsigned int state;
cb53c626 4538
5536c6d6 4539 spin_lock(&codec->power_lock);
a2d96e77
TI
4540 if (codec->power_transition > 0) { /* during power-up sequence? */
4541 spin_unlock(&codec->power_lock);
4542 return;
4543 }
2e492462
ML
4544 if (!codec->power_on || codec->power_count) {
4545 codec->power_transition = 0;
5536c6d6 4546 spin_unlock(&codec->power_lock);
cb53c626 4547 return;
2e492462 4548 }
5536c6d6
TI
4549 spin_unlock(&codec->power_lock);
4550
d17344b3 4551 state = hda_call_codec_suspend(codec, true);
08fa20ae
TI
4552 codec->pm_down_notified = 0;
4553 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4554 codec->pm_down_notified = 1;
68467f51 4555 hda_call_pm_notify(bus, false);
08fa20ae 4556 }
cb53c626
TI
4557}
4558
4559static void hda_keep_power_on(struct hda_codec *codec)
4560{
5536c6d6 4561 spin_lock(&codec->power_lock);
cb53c626
TI
4562 codec->power_count++;
4563 codec->power_on = 1;
a2f6309e 4564 codec->power_jiffies = jiffies;
5536c6d6 4565 spin_unlock(&codec->power_lock);
a2f6309e
TI
4566}
4567
d5191e50 4568/* update the power on/off account with the current jiffies */
a2f6309e
TI
4569void snd_hda_update_power_acct(struct hda_codec *codec)
4570{
4571 unsigned long delta = jiffies - codec->power_jiffies;
4572 if (codec->power_on)
4573 codec->power_on_acct += delta;
4574 else
4575 codec->power_off_acct += delta;
4576 codec->power_jiffies += delta;
cb53c626
TI
4577}
4578
b4a91cf0
DR
4579/* Transition to powered up, if wait_power_down then wait for a pending
4580 * transition to D3 to complete. A pending D3 transition is indicated
4581 * with power_transition == -1. */
c376e2c7 4582/* call this with codec->power_lock held! */
b4a91cf0 4583static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
cb53c626 4584{
33fa35ed
TI
4585 struct hda_bus *bus = codec->bus;
4586
b4a91cf0
DR
4587 /* Return if power_on or transitioning to power_on, unless currently
4588 * powering down. */
4589 if ((codec->power_on || codec->power_transition > 0) &&
c376e2c7 4590 !(wait_power_down && codec->power_transition < 0))
cb53c626 4591 return;
a2d96e77 4592 spin_unlock(&codec->power_lock);
cb53c626 4593
a2d96e77
TI
4594 cancel_delayed_work_sync(&codec->power_work);
4595
4596 spin_lock(&codec->power_lock);
b43d2247
DR
4597 /* If the power down delayed work was cancelled above before starting,
4598 * then there is no need to go through power up here.
4599 */
4600 if (codec->power_on) {
535b6c51
TI
4601 if (codec->power_transition < 0)
4602 codec->power_transition = 0;
b43d2247
DR
4603 return;
4604 }
c376e2c7 4605
d66fee5d 4606 trace_hda_power_up(codec);
a2f6309e 4607 snd_hda_update_power_acct(codec);
cb53c626 4608 codec->power_on = 1;
a2f6309e 4609 codec->power_jiffies = jiffies;
7f30830b 4610 codec->power_transition = 1; /* avoid reentrance */
5536c6d6
TI
4611 spin_unlock(&codec->power_lock);
4612
08fa20ae
TI
4613 if (codec->pm_down_notified) {
4614 codec->pm_down_notified = 0;
68467f51 4615 hda_call_pm_notify(bus, true);
08fa20ae
TI
4616 }
4617
cb53c626 4618 hda_call_codec_resume(codec);
5536c6d6
TI
4619
4620 spin_lock(&codec->power_lock);
a221e287 4621 codec->power_transition = 0;
cb53c626 4622}
b4a91cf0 4623
c376e2c7
TI
4624#define power_save(codec) \
4625 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
1289e9e8 4626
c376e2c7
TI
4627/* Transition to powered down */
4628static void __snd_hda_power_down(struct hda_codec *codec)
b4a91cf0 4629{
c376e2c7
TI
4630 if (!codec->power_on || codec->power_count || codec->power_transition)
4631 return;
b4a91cf0 4632
c376e2c7
TI
4633 if (power_save(codec)) {
4634 codec->power_transition = -1; /* avoid reentrance */
4635 queue_delayed_work(codec->bus->workq, &codec->power_work,
4636 msecs_to_jiffies(power_save(codec) * 1000));
4637 }
4638}
cb53c626 4639
d5191e50 4640/**
c376e2c7 4641 * snd_hda_power_save - Power-up/down/sync the codec
d5191e50 4642 * @codec: HD-audio codec
c376e2c7 4643 * @delta: the counter delta to change
d5191e50 4644 *
c376e2c7
TI
4645 * Change the power-up counter via @delta, and power up or down the hardware
4646 * appropriately. For the power-down, queue to the delayed action.
4647 * Passing zero to @delta means to synchronize the power state.
28aedaf7 4648 */
c376e2c7 4649void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
cb53c626 4650{
5536c6d6 4651 spin_lock(&codec->power_lock);
c376e2c7 4652 codec->power_count += delta;
b244d335 4653 trace_hda_power_count(codec);
c376e2c7
TI
4654 if (delta > 0)
4655 __snd_hda_power_up(codec, d3wait);
4656 else
4657 __snd_hda_power_down(codec);
5536c6d6 4658 spin_unlock(&codec->power_lock);
cb53c626 4659}
c376e2c7 4660EXPORT_SYMBOL_HDA(snd_hda_power_save);
cb53c626 4661
d5191e50
TI
4662/**
4663 * snd_hda_check_amp_list_power - Check the amp list and update the power
4664 * @codec: HD-audio codec
4665 * @check: the object containing an AMP list and the status
4666 * @nid: NID to check / update
4667 *
4668 * Check whether the given NID is in the amp list. If it's in the list,
4669 * check the current AMP status, and update the the power-status according
4670 * to the mute status.
4671 *
4672 * This function is supposed to be set or called from the check_power_status
4673 * patch ops.
28aedaf7 4674 */
cb53c626
TI
4675int snd_hda_check_amp_list_power(struct hda_codec *codec,
4676 struct hda_loopback_check *check,
4677 hda_nid_t nid)
4678{
031024ee 4679 const struct hda_amp_list *p;
cb53c626
TI
4680 int ch, v;
4681
4682 if (!check->amplist)
4683 return 0;
4684 for (p = check->amplist; p->nid; p++) {
4685 if (p->nid == nid)
4686 break;
4687 }
4688 if (!p->nid)
4689 return 0; /* nothing changed */
4690
4691 for (p = check->amplist; p->nid; p++) {
4692 for (ch = 0; ch < 2; ch++) {
4693 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4694 p->idx);
4695 if (!(v & HDA_AMP_MUTE) && v > 0) {
4696 if (!check->power_on) {
4697 check->power_on = 1;
4698 snd_hda_power_up(codec);
4699 }
4700 return 1;
4701 }
4702 }
4703 }
4704 if (check->power_on) {
4705 check->power_on = 0;
4706 snd_hda_power_down(codec);
4707 }
4708 return 0;
4709}
ff7a3267 4710EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
cb53c626 4711#endif
1da177e4 4712
c8b6bf9b 4713/*
d2a6d7dc
TI
4714 * Channel mode helper
4715 */
d5191e50
TI
4716
4717/**
4718 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4719 */
0ba21762
TI
4720int snd_hda_ch_mode_info(struct hda_codec *codec,
4721 struct snd_ctl_elem_info *uinfo,
4722 const struct hda_channel_mode *chmode,
4723 int num_chmodes)
d2a6d7dc
TI
4724{
4725 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4726 uinfo->count = 1;
4727 uinfo->value.enumerated.items = num_chmodes;
4728 if (uinfo->value.enumerated.item >= num_chmodes)
4729 uinfo->value.enumerated.item = num_chmodes - 1;
4730 sprintf(uinfo->value.enumerated.name, "%dch",
4731 chmode[uinfo->value.enumerated.item].channels);
4732 return 0;
4733}
ff7a3267 4734EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
d2a6d7dc 4735
d5191e50
TI
4736/**
4737 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4738 */
0ba21762
TI
4739int snd_hda_ch_mode_get(struct hda_codec *codec,
4740 struct snd_ctl_elem_value *ucontrol,
4741 const struct hda_channel_mode *chmode,
4742 int num_chmodes,
d2a6d7dc
TI
4743 int max_channels)
4744{
4745 int i;
4746
4747 for (i = 0; i < num_chmodes; i++) {
4748 if (max_channels == chmode[i].channels) {
4749 ucontrol->value.enumerated.item[0] = i;
4750 break;
4751 }
4752 }
4753 return 0;
4754}
ff7a3267 4755EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
d2a6d7dc 4756
d5191e50
TI
4757/**
4758 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4759 */
0ba21762
TI
4760int snd_hda_ch_mode_put(struct hda_codec *codec,
4761 struct snd_ctl_elem_value *ucontrol,
4762 const struct hda_channel_mode *chmode,
4763 int num_chmodes,
d2a6d7dc
TI
4764 int *max_channelsp)
4765{
4766 unsigned int mode;
4767
4768 mode = ucontrol->value.enumerated.item[0];
68ea7b2f
TI
4769 if (mode >= num_chmodes)
4770 return -EINVAL;
82beb8fd 4771 if (*max_channelsp == chmode[mode].channels)
d2a6d7dc
TI
4772 return 0;
4773 /* change the current channel setting */
4774 *max_channelsp = chmode[mode].channels;
4775 if (chmode[mode].sequence)
82beb8fd 4776 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
d2a6d7dc
TI
4777 return 1;
4778}
ff7a3267 4779EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
d2a6d7dc 4780
1da177e4
LT
4781/*
4782 * input MUX helper
4783 */
d5191e50
TI
4784
4785/**
4786 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4787 */
0ba21762
TI
4788int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4789 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
4790{
4791 unsigned int index;
4792
4793 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4794 uinfo->count = 1;
4795 uinfo->value.enumerated.items = imux->num_items;
5513b0c5
TI
4796 if (!imux->num_items)
4797 return 0;
1da177e4
LT
4798 index = uinfo->value.enumerated.item;
4799 if (index >= imux->num_items)
4800 index = imux->num_items - 1;
4801 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4802 return 0;
4803}
ff7a3267 4804EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
1da177e4 4805
d5191e50
TI
4806/**
4807 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4808 */
0ba21762
TI
4809int snd_hda_input_mux_put(struct hda_codec *codec,
4810 const struct hda_input_mux *imux,
4811 struct snd_ctl_elem_value *ucontrol,
4812 hda_nid_t nid,
1da177e4
LT
4813 unsigned int *cur_val)
4814{
4815 unsigned int idx;
4816
5513b0c5
TI
4817 if (!imux->num_items)
4818 return 0;
1da177e4
LT
4819 idx = ucontrol->value.enumerated.item[0];
4820 if (idx >= imux->num_items)
4821 idx = imux->num_items - 1;
82beb8fd 4822 if (*cur_val == idx)
1da177e4 4823 return 0;
82beb8fd
TI
4824 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4825 imux->items[idx].index);
1da177e4
LT
4826 *cur_val = idx;
4827 return 1;
4828}
ff7a3267 4829EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
1da177e4
LT
4830
4831
4832/*
4833 * Multi-channel / digital-out PCM helper functions
4834 */
4835
6b97eb45
TI
4836/* setup SPDIF output stream */
4837static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4838 unsigned int stream_tag, unsigned int format)
4839{
3bef1c37
LD
4840 struct hda_spdif_out *spdif;
4841 unsigned int curr_fmt;
4842 bool reset;
4843
4844 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4845 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4846 AC_VERB_GET_STREAM_FORMAT, 0);
4847 reset = codec->spdif_status_reset &&
4848 (spdif->ctls & AC_DIG1_ENABLE) &&
4849 curr_fmt != format;
4850
4851 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4852 updated */
4853 if (reset)
28aedaf7 4854 set_dig_out_convert(codec, nid,
7c935976 4855 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
2f72853c 4856 -1);
6b97eb45 4857 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2f72853c 4858 if (codec->slave_dig_outs) {
dda14410 4859 const hda_nid_t *d;
2f72853c
TI
4860 for (d = codec->slave_dig_outs; *d; d++)
4861 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4862 format);
4863 }
6b97eb45 4864 /* turn on again (if needed) */
3bef1c37 4865 if (reset)
2f72853c 4866 set_dig_out_convert(codec, nid,
7c935976 4867 spdif->ctls & 0xff, -1);
2f72853c 4868}
de51ca12 4869
2f72853c
TI
4870static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4871{
4872 snd_hda_codec_cleanup_stream(codec, nid);
4873 if (codec->slave_dig_outs) {
dda14410 4874 const hda_nid_t *d;
2f72853c
TI
4875 for (d = codec->slave_dig_outs; *d; d++)
4876 snd_hda_codec_cleanup_stream(codec, *d);
de51ca12 4877 }
6b97eb45
TI
4878}
4879
d5191e50
TI
4880/**
4881 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4882 * @bus: HD-audio bus
4883 */
fb8d1a34
TI
4884void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4885{
4886 struct hda_codec *codec;
4887
4888 if (!bus)
4889 return;
4890 list_for_each_entry(codec, &bus->codec_list, list) {
e581f3db
TI
4891 if (hda_codec_is_power_on(codec) &&
4892 codec->patch_ops.reboot_notify)
fb8d1a34
TI
4893 codec->patch_ops.reboot_notify(codec);
4894 }
4895}
8f217a22 4896EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
fb8d1a34 4897
d5191e50
TI
4898/**
4899 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
1da177e4 4900 */
0ba21762
TI
4901int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4902 struct hda_multi_out *mout)
1da177e4 4903{
62932df8 4904 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
4905 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4906 /* already opened as analog dup; reset it once */
2f72853c 4907 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4 4908 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 4909 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4910 return 0;
4911}
ff7a3267 4912EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
1da177e4 4913
d5191e50
TI
4914/**
4915 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4916 */
6b97eb45
TI
4917int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4918 struct hda_multi_out *mout,
4919 unsigned int stream_tag,
4920 unsigned int format,
4921 struct snd_pcm_substream *substream)
4922{
4923 mutex_lock(&codec->spdif_mutex);
4924 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4925 mutex_unlock(&codec->spdif_mutex);
4926 return 0;
4927}
ff7a3267 4928EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
6b97eb45 4929
d5191e50
TI
4930/**
4931 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4932 */
9411e21c
TI
4933int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4934 struct hda_multi_out *mout)
4935{
4936 mutex_lock(&codec->spdif_mutex);
4937 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4938 mutex_unlock(&codec->spdif_mutex);
4939 return 0;
4940}
4941EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4942
d5191e50
TI
4943/**
4944 * snd_hda_multi_out_dig_close - release the digital out stream
1da177e4 4945 */
0ba21762
TI
4946int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4947 struct hda_multi_out *mout)
1da177e4 4948{
62932df8 4949 mutex_lock(&codec->spdif_mutex);
1da177e4 4950 mout->dig_out_used = 0;
62932df8 4951 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4952 return 0;
4953}
ff7a3267 4954EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
1da177e4 4955
d5191e50
TI
4956/**
4957 * snd_hda_multi_out_analog_open - open analog outputs
4958 *
4959 * Open analog outputs and set up the hw-constraints.
4960 * If the digital outputs can be opened as slave, open the digital
4961 * outputs, too.
1da177e4 4962 */
0ba21762
TI
4963int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4964 struct hda_multi_out *mout,
9a08160b
TI
4965 struct snd_pcm_substream *substream,
4966 struct hda_pcm_stream *hinfo)
4967{
4968 struct snd_pcm_runtime *runtime = substream->runtime;
4969 runtime->hw.channels_max = mout->max_channels;
4970 if (mout->dig_out_nid) {
4971 if (!mout->analog_rates) {
4972 mout->analog_rates = hinfo->rates;
4973 mout->analog_formats = hinfo->formats;
4974 mout->analog_maxbps = hinfo->maxbps;
4975 } else {
4976 runtime->hw.rates = mout->analog_rates;
4977 runtime->hw.formats = mout->analog_formats;
4978 hinfo->maxbps = mout->analog_maxbps;
4979 }
4980 if (!mout->spdif_rates) {
4981 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4982 &mout->spdif_rates,
4983 &mout->spdif_formats,
4984 &mout->spdif_maxbps);
4985 }
4986 mutex_lock(&codec->spdif_mutex);
4987 if (mout->share_spdif) {
022b466f
TI
4988 if ((runtime->hw.rates & mout->spdif_rates) &&
4989 (runtime->hw.formats & mout->spdif_formats)) {
4990 runtime->hw.rates &= mout->spdif_rates;
4991 runtime->hw.formats &= mout->spdif_formats;
4992 if (mout->spdif_maxbps < hinfo->maxbps)
4993 hinfo->maxbps = mout->spdif_maxbps;
4994 } else {
4995 mout->share_spdif = 0;
4996 /* FIXME: need notify? */
4997 }
9a08160b 4998 }
eaa9985b 4999 mutex_unlock(&codec->spdif_mutex);
9a08160b 5000 }
1da177e4
LT
5001 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5002 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5003}
ff7a3267 5004EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
1da177e4 5005
d5191e50
TI
5006/**
5007 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5008 *
5009 * Set up the i/o for analog out.
5010 * When the digital out is available, copy the front out to digital out, too.
1da177e4 5011 */
0ba21762
TI
5012int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5013 struct hda_multi_out *mout,
1da177e4
LT
5014 unsigned int stream_tag,
5015 unsigned int format,
c8b6bf9b 5016 struct snd_pcm_substream *substream)
1da177e4 5017{
dda14410 5018 const hda_nid_t *nids = mout->dac_nids;
1da177e4 5019 int chs = substream->runtime->channels;
e3245cdd 5020 struct hda_spdif_out *spdif;
1da177e4
LT
5021 int i;
5022
62932df8 5023 mutex_lock(&codec->spdif_mutex);
e3245cdd 5024 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
9a08160b
TI
5025 if (mout->dig_out_nid && mout->share_spdif &&
5026 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1da177e4 5027 if (chs == 2 &&
0ba21762
TI
5028 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5029 format) &&
7c935976 5030 !(spdif->status & IEC958_AES0_NONAUDIO)) {
1da177e4 5031 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
5032 setup_dig_out_stream(codec, mout->dig_out_nid,
5033 stream_tag, format);
1da177e4
LT
5034 } else {
5035 mout->dig_out_used = 0;
2f72853c 5036 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
5037 }
5038 }
62932df8 5039 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5040
5041 /* front */
0ba21762
TI
5042 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5043 0, format);
d29240ce
TI
5044 if (!mout->no_share_stream &&
5045 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 5046 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
5047 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5048 0, format);
82bc955f 5049 /* extra outputs copied from front */
a06dbfc2
TI
5050 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5051 if (!mout->no_share_stream && mout->hp_out_nid[i])
5052 snd_hda_codec_setup_stream(codec,
5053 mout->hp_out_nid[i],
5054 stream_tag, 0, format);
82bc955f 5055 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
d29240ce 5056 if (!mout->no_share_stream && mout->extra_out_nid[i])
82bc955f
TI
5057 snd_hda_codec_setup_stream(codec,
5058 mout->extra_out_nid[i],
5059 stream_tag, 0, format);
5060
1da177e4
LT
5061 /* surrounds */
5062 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 5063 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
5064 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5065 i * 2, format);
d29240ce 5066 else if (!mout->no_share_stream) /* copy front */
0ba21762
TI
5067 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5068 0, format);
1da177e4
LT
5069 }
5070 return 0;
5071}
ff7a3267 5072EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
1da177e4 5073
d5191e50
TI
5074/**
5075 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
1da177e4 5076 */
0ba21762
TI
5077int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5078 struct hda_multi_out *mout)
1da177e4 5079{
dda14410 5080 const hda_nid_t *nids = mout->dac_nids;
1da177e4
LT
5081 int i;
5082
5083 for (i = 0; i < mout->num_dacs; i++)
888afa15 5084 snd_hda_codec_cleanup_stream(codec, nids[i]);
1da177e4 5085 if (mout->hp_nid)
888afa15 5086 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
a06dbfc2
TI
5087 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5088 if (mout->hp_out_nid[i])
5089 snd_hda_codec_cleanup_stream(codec,
5090 mout->hp_out_nid[i]);
82bc955f
TI
5091 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5092 if (mout->extra_out_nid[i])
888afa15
TI
5093 snd_hda_codec_cleanup_stream(codec,
5094 mout->extra_out_nid[i]);
62932df8 5095 mutex_lock(&codec->spdif_mutex);
1da177e4 5096 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2f72853c 5097 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
5098 mout->dig_out_used = 0;
5099 }
62932df8 5100 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5101 return 0;
5102}
ff7a3267 5103EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
1da177e4 5104
4740860b
TI
5105/**
5106 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5107 *
5108 * Guess the suitable VREF pin bits to be set as the pin-control value.
5109 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5110 */
5111unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5112{
5113 unsigned int pincap;
5114 unsigned int oldval;
5115 oldval = snd_hda_codec_read(codec, pin, 0,
5116 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5117 pincap = snd_hda_query_pin_caps(codec, pin);
5118 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5119 /* Exception: if the default pin setup is vref50, we give it priority */
5120 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5121 return AC_PINCTL_VREF_80;
5122 else if (pincap & AC_PINCAP_VREF_50)
5123 return AC_PINCTL_VREF_50;
5124 else if (pincap & AC_PINCAP_VREF_100)
5125 return AC_PINCTL_VREF_100;
5126 else if (pincap & AC_PINCAP_VREF_GRD)
5127 return AC_PINCTL_VREF_GRD;
5128 return AC_PINCTL_VREF_HIZ;
5129}
5130EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5131
cdd03ced
TI
5132int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5133 unsigned int val, bool cached)
5134{
5135 if (val) {
5136 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
6942c103 5137 if (cap && (val & AC_PINCTL_OUT_EN)) {
cdd03ced
TI
5138 if (!(cap & AC_PINCAP_OUT))
5139 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5140 else if ((val & AC_PINCTL_HP_EN) &&
5141 !(cap & AC_PINCAP_HP_DRV))
5142 val &= ~AC_PINCTL_HP_EN;
5143 }
6942c103 5144 if (cap && (val & AC_PINCTL_IN_EN)) {
cdd03ced
TI
5145 if (!(cap & AC_PINCAP_IN))
5146 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5147 }
5148 }
5149 if (cached)
5150 return snd_hda_codec_update_cache(codec, pin, 0,
5151 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5152 else
5153 return snd_hda_codec_write(codec, pin, 0,
5154 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5155}
5156EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5157
990061c2
TI
5158/**
5159 * snd_hda_add_imux_item - Add an item to input_mux
5160 *
5161 * When the same label is used already in the existing items, the number
5162 * suffix is appended to the label. This label index number is stored
5163 * to type_idx when non-NULL pointer is given.
5164 */
10a20af7
TI
5165int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5166 int index, int *type_idx)
5167{
5168 int i, label_idx = 0;
5169 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5170 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5171 return -EINVAL;
5172 }
5173 for (i = 0; i < imux->num_items; i++) {
5174 if (!strncmp(label, imux->items[i].label, strlen(label)))
5175 label_idx++;
d7b1ae9d 5176 }
10a20af7
TI
5177 if (type_idx)
5178 *type_idx = label_idx;
5179 if (label_idx > 0)
5180 snprintf(imux->items[imux->num_items].label,
5181 sizeof(imux->items[imux->num_items].label),
5182 "%s %d", label, label_idx);
b5786e85 5183 else
10a20af7
TI
5184 strlcpy(imux->items[imux->num_items].label, label,
5185 sizeof(imux->items[imux->num_items].label));
5186 imux->items[imux->num_items].index = index;
5187 imux->num_items++;
5188 return 0;
d7b1ae9d 5189}
10a20af7 5190EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
d7b1ae9d 5191
4a471b7d 5192
1da177e4
LT
5193#ifdef CONFIG_PM
5194/*
5195 * power management
5196 */
5197
5198/**
5199 * snd_hda_suspend - suspend the codecs
5200 * @bus: the HDA bus
1da177e4
LT
5201 *
5202 * Returns 0 if successful.
5203 */
8dd78330 5204int snd_hda_suspend(struct hda_bus *bus)
1da177e4 5205{
0ba21762 5206 struct hda_codec *codec;
1da177e4 5207
0ba21762 5208 list_for_each_entry(codec, &bus->codec_list, list) {
26a6cb6c 5209 cancel_delayed_work_sync(&codec->jackpoll_work);
e581f3db 5210 if (hda_codec_is_power_on(codec))
d17344b3 5211 hda_call_codec_suspend(codec, false);
1da177e4
LT
5212 }
5213 return 0;
5214}
ff7a3267 5215EXPORT_SYMBOL_HDA(snd_hda_suspend);
1da177e4
LT
5216
5217/**
5218 * snd_hda_resume - resume the codecs
5219 * @bus: the HDA bus
1da177e4
LT
5220 *
5221 * Returns 0 if successful.
5222 */
5223int snd_hda_resume(struct hda_bus *bus)
5224{
0ba21762 5225 struct hda_codec *codec;
1da177e4 5226
0ba21762 5227 list_for_each_entry(codec, &bus->codec_list, list) {
7f30830b 5228 hda_call_codec_resume(codec);
1da177e4 5229 }
1da177e4
LT
5230 return 0;
5231}
ff7a3267 5232EXPORT_SYMBOL_HDA(snd_hda_resume);
1289e9e8 5233#endif /* CONFIG_PM */
b2e18597
TI
5234
5235/*
5236 * generic arrays
5237 */
5238
d5191e50
TI
5239/**
5240 * snd_array_new - get a new element from the given array
5241 * @array: the array object
28aedaf7 5242 *
d5191e50
TI
5243 * Get a new element from the given array. If it exceeds the
5244 * pre-allocated array size, re-allocate the array.
5245 *
5246 * Returns NULL if allocation failed.
b2e18597
TI
5247 */
5248void *snd_array_new(struct snd_array *array)
5249{
12f17717
TI
5250 if (snd_BUG_ON(!array->elem_size))
5251 return NULL;
b2e18597
TI
5252 if (array->used >= array->alloced) {
5253 int num = array->alloced + array->alloc_align;
3101ba03 5254 int size = (num + 1) * array->elem_size;
00ef9610 5255 int oldsize = array->alloced * array->elem_size;
b910d9ae
TI
5256 void *nlist;
5257 if (snd_BUG_ON(num >= 4096))
5258 return NULL;
3101ba03 5259 nlist = krealloc(array->list, size, GFP_KERNEL);
b2e18597
TI
5260 if (!nlist)
5261 return NULL;
00ef9610 5262 memset(nlist + oldsize, 0, size - oldsize);
b2e18597
TI
5263 array->list = nlist;
5264 array->alloced = num;
5265 }
f43aa025 5266 return snd_array_elem(array, array->used++);
b2e18597 5267}
ff7a3267 5268EXPORT_SYMBOL_HDA(snd_array_new);
b2e18597 5269
d5191e50
TI
5270/**
5271 * snd_array_free - free the given array elements
5272 * @array: the array object
5273 */
b2e18597
TI
5274void snd_array_free(struct snd_array *array)
5275{
5276 kfree(array->list);
5277 array->used = 0;
5278 array->alloced = 0;
5279 array->list = NULL;
5280}
ff7a3267 5281EXPORT_SYMBOL_HDA(snd_array_free);
b2022266 5282
d5191e50
TI
5283/**
5284 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5285 * @pcm: PCM caps bits
5286 * @buf: the string buffer to write
5287 * @buflen: the max buffer length
5288 *
5289 * used by hda_proc.c and hda_eld.c
5290 */
b2022266
TI
5291void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5292{
5293 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5294 int i, j;
5295
5296 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5297 if (pcm & (AC_SUPPCM_BITS_8 << i))
5298 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5299
5300 buf[j] = '\0'; /* necessary when j == 0 */
5301}
ff7a3267 5302EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
1289e9e8
TI
5303
5304MODULE_DESCRIPTION("HDA codec core");
5305MODULE_LICENSE("GPL");
This page took 0.980437 seconds and 5 git commands to generate.