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