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