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