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