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