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