ALSA: hda - check NULL pointer when creating SPDIF controls
[linux-2.6-block.git] / sound / pci / hda / hda_codec.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
18478e8b 22#include <linux/mm.h>
1da177e4
LT
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
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 2334static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
ea9b43ad 2335 int start_idx)
1afe206a 2336{
ea9b43ad
TI
2337 int i, idx;
2338 /* 16 ctlrs should be large enough */
2339 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2340 if (!find_mixer_ctl(codec, name, 0, idx))
1afe206a
TI
2341 return idx;
2342 }
2343 return -EBUSY;
2344}
2345
d5191e50 2346/**
5b0cb1d8 2347 * snd_hda_ctl_add - Add a control element and assign to the codec
d5191e50
TI
2348 * @codec: HD-audio codec
2349 * @nid: corresponding NID (optional)
2350 * @kctl: the control element to assign
2351 *
2352 * Add the given control element to an array inside the codec instance.
2353 * All control elements belonging to a codec are supposed to be added
2354 * by this function so that a proper clean-up works at the free or
2355 * reconfiguration time.
2356 *
2357 * If non-zero @nid is passed, the NID is assigned to the control element.
2358 * The assignment is shown in the codec proc file.
2359 *
2360 * snd_hda_ctl_add() checks the control subdev id field whether
2361 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
9e3fd871
JK
2362 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2363 * specifies if kctl->private_value is a HDA amplifier value.
d5191e50 2364 */
3911a4c1
JK
2365int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2366 struct snd_kcontrol *kctl)
d13bd412
TI
2367{
2368 int err;
9e3fd871 2369 unsigned short flags = 0;
3911a4c1 2370 struct hda_nid_item *item;
d13bd412 2371
5e26dfd0 2372 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
9e3fd871 2373 flags |= HDA_NID_ITEM_AMP;
5e26dfd0
JK
2374 if (nid == 0)
2375 nid = get_amp_nid_(kctl->private_value);
2376 }
9e3fd871
JK
2377 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2378 nid = kctl->id.subdevice & 0xffff;
5e26dfd0 2379 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
4d02d1b6 2380 kctl->id.subdevice = 0;
d13bd412
TI
2381 err = snd_ctl_add(codec->bus->card, kctl);
2382 if (err < 0)
2383 return err;
3911a4c1
JK
2384 item = snd_array_new(&codec->mixers);
2385 if (!item)
d13bd412 2386 return -ENOMEM;
3911a4c1
JK
2387 item->kctl = kctl;
2388 item->nid = nid;
9e3fd871 2389 item->flags = flags;
d13bd412
TI
2390 return 0;
2391}
ff7a3267 2392EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
d13bd412 2393
5b0cb1d8
JK
2394/**
2395 * snd_hda_add_nid - Assign a NID to a control element
2396 * @codec: HD-audio codec
2397 * @nid: corresponding NID (optional)
2398 * @kctl: the control element to assign
2399 * @index: index to kctl
2400 *
2401 * Add the given control element to an array inside the codec instance.
2402 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2403 * NID:KCTL mapping - for example "Capture Source" selector.
2404 */
2405int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2406 unsigned int index, hda_nid_t nid)
2407{
2408 struct hda_nid_item *item;
2409
2410 if (nid > 0) {
2411 item = snd_array_new(&codec->nids);
2412 if (!item)
2413 return -ENOMEM;
2414 item->kctl = kctl;
2415 item->index = index;
2416 item->nid = nid;
2417 return 0;
2418 }
28d1a85e
TI
2419 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2420 kctl->id.name, kctl->id.index, index);
5b0cb1d8
JK
2421 return -EINVAL;
2422}
2423EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2424
d5191e50
TI
2425/**
2426 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2427 * @codec: HD-audio codec
2428 */
d13bd412
TI
2429void snd_hda_ctls_clear(struct hda_codec *codec)
2430{
2431 int i;
3911a4c1 2432 struct hda_nid_item *items = codec->mixers.list;
d13bd412 2433 for (i = 0; i < codec->mixers.used; i++)
3911a4c1 2434 snd_ctl_remove(codec->bus->card, items[i].kctl);
d13bd412 2435 snd_array_free(&codec->mixers);
5b0cb1d8 2436 snd_array_free(&codec->nids);
d13bd412
TI
2437}
2438
a65d629c
TI
2439/* pseudo device locking
2440 * toggle card->shutdown to allow/disallow the device access (as a hack)
2441 */
d3d020bd 2442int snd_hda_lock_devices(struct hda_bus *bus)
6c1f45ea 2443{
d3d020bd
TI
2444 struct snd_card *card = bus->card;
2445 struct hda_codec *codec;
2446
a65d629c 2447 spin_lock(&card->files_lock);
d3d020bd
TI
2448 if (card->shutdown)
2449 goto err_unlock;
a65d629c 2450 card->shutdown = 1;
d3d020bd
TI
2451 if (!list_empty(&card->ctl_files))
2452 goto err_clear;
2453
2454 list_for_each_entry(codec, &bus->codec_list, list) {
2455 int pcm;
2456 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2457 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2458 if (!cpcm->pcm)
2459 continue;
2460 if (cpcm->pcm->streams[0].substream_opened ||
2461 cpcm->pcm->streams[1].substream_opened)
2462 goto err_clear;
2463 }
2464 }
a65d629c
TI
2465 spin_unlock(&card->files_lock);
2466 return 0;
d3d020bd
TI
2467
2468 err_clear:
2469 card->shutdown = 0;
2470 err_unlock:
2471 spin_unlock(&card->files_lock);
2472 return -EINVAL;
a65d629c 2473}
d3d020bd 2474EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
a65d629c 2475
d3d020bd 2476void snd_hda_unlock_devices(struct hda_bus *bus)
a65d629c 2477{
d3d020bd
TI
2478 struct snd_card *card = bus->card;
2479
2480 card = bus->card;
a65d629c
TI
2481 spin_lock(&card->files_lock);
2482 card->shutdown = 0;
2483 spin_unlock(&card->files_lock);
2484}
d3d020bd 2485EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
a65d629c 2486
d5191e50
TI
2487/**
2488 * snd_hda_codec_reset - Clear all objects assigned to the codec
2489 * @codec: HD-audio codec
2490 *
2491 * This frees the all PCM and control elements assigned to the codec, and
2492 * clears the caches and restores the pin default configurations.
2493 *
2494 * When a device is being used, it returns -EBSY. If successfully freed,
2495 * returns zero.
2496 */
a65d629c
TI
2497int snd_hda_codec_reset(struct hda_codec *codec)
2498{
d3d020bd
TI
2499 struct hda_bus *bus = codec->bus;
2500 struct snd_card *card = bus->card;
2501 int i;
a65d629c 2502
d3d020bd 2503 if (snd_hda_lock_devices(bus) < 0)
a65d629c 2504 return -EBUSY;
a65d629c
TI
2505
2506 /* OK, let it free */
26a6cb6c 2507 cancel_delayed_work_sync(&codec->jackpoll_work);
83012a7c 2508#ifdef CONFIG_PM
a2d96e77 2509 cancel_delayed_work_sync(&codec->power_work);
339876d7
TI
2510 codec->power_on = 0;
2511 codec->power_transition = 0;
2512 codec->power_jiffies = jiffies;
d3d020bd 2513 flush_workqueue(bus->workq);
6c1f45ea
TI
2514#endif
2515 snd_hda_ctls_clear(codec);
2516 /* relase PCMs */
2517 for (i = 0; i < codec->num_pcms; i++) {
529bd6c4 2518 if (codec->pcm_info[i].pcm) {
a65d629c 2519 snd_device_free(card, codec->pcm_info[i].pcm);
529bd6c4 2520 clear_bit(codec->pcm_info[i].device,
d3d020bd 2521 bus->pcm_dev_bits);
529bd6c4 2522 }
6c1f45ea
TI
2523 }
2524 if (codec->patch_ops.free)
2525 codec->patch_ops.free(codec);
07dc59f0 2526 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1835a0f9 2527 snd_hda_jack_tbl_clear(codec);
56d17712 2528 codec->proc_widget_hook = NULL;
6c1f45ea
TI
2529 codec->spec = NULL;
2530 free_hda_cache(&codec->amp_cache);
2531 free_hda_cache(&codec->cmd_cache);
827057f5
TI
2532 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2533 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
346ff70f
TI
2534 /* free only driver_pins so that init_pins + user_pins are restored */
2535 snd_array_free(&codec->driver_pins);
09a6071b
TI
2536 snd_array_free(&codec->cvt_setups);
2537 snd_array_free(&codec->spdif_out);
c9ce6b26 2538 snd_array_free(&codec->verbs);
6c1f45ea
TI
2539 codec->num_pcms = 0;
2540 codec->pcm_info = NULL;
2541 codec->preset = NULL;
d1f1af2d
TI
2542 codec->slave_dig_outs = NULL;
2543 codec->spdif_status_reset = 0;
1289e9e8
TI
2544 module_put(codec->owner);
2545 codec->owner = NULL;
a65d629c
TI
2546
2547 /* allow device access again */
d3d020bd 2548 snd_hda_unlock_devices(bus);
a65d629c 2549 return 0;
6c1f45ea
TI
2550}
2551
aeb4b88e
TI
2552typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2553
2554/* apply the function to all matching slave ctls in the mixer list */
2555static int map_slaves(struct hda_codec *codec, const char * const *slaves,
9322ca54 2556 const char *suffix, map_slave_func_t func, void *data)
aeb4b88e
TI
2557{
2558 struct hda_nid_item *items;
2559 const char * const *s;
2560 int i, err;
2561
2562 items = codec->mixers.list;
2563 for (i = 0; i < codec->mixers.used; i++) {
2564 struct snd_kcontrol *sctl = items[i].kctl;
2565 if (!sctl || !sctl->id.name ||
2566 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2567 continue;
2568 for (s = slaves; *s; s++) {
9322ca54
TI
2569 char tmpname[sizeof(sctl->id.name)];
2570 const char *name = *s;
2571 if (suffix) {
2572 snprintf(tmpname, sizeof(tmpname), "%s %s",
2573 name, suffix);
2574 name = tmpname;
2575 }
9322ca54 2576 if (!strcmp(sctl->id.name, name)) {
aeb4b88e
TI
2577 err = func(data, sctl);
2578 if (err)
2579 return err;
2580 break;
2581 }
2582 }
2583 }
2584 return 0;
2585}
2586
2587static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2588{
2589 return 1;
2590}
2591
18478e8b
TI
2592/* guess the value corresponding to 0dB */
2593static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2594{
2595 int _tlv[4];
2596 const int *tlv = NULL;
2597 int val = -1;
2598
2599 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2600 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2601 mm_segment_t fs = get_fs();
2602 set_fs(get_ds());
2603 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2604 tlv = _tlv;
2605 set_fs(fs);
2606 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2607 tlv = kctl->tlv.p;
2608 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2609 val = -tlv[2] / tlv[3];
2610 return val;
2611}
2612
2613/* call kctl->put with the given value(s) */
2614static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2615{
2616 struct snd_ctl_elem_value *ucontrol;
2617 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2618 if (!ucontrol)
2619 return -ENOMEM;
2620 ucontrol->value.integer.value[0] = val;
2621 ucontrol->value.integer.value[1] = val;
2622 kctl->put(kctl, ucontrol);
2623 kfree(ucontrol);
2624 return 0;
2625}
2626
2627/* initialize the slave volume with 0dB */
2628static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2629{
2630 int offset = get_kctl_0dB_offset(slave);
2631 if (offset > 0)
2632 put_kctl_with_value(slave, offset);
2633 return 0;
2634}
2635
2636/* unmute the slave */
2637static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2638{
2639 return put_kctl_with_value(slave, 1);
2640}
2641
d5191e50
TI
2642/**
2643 * snd_hda_add_vmaster - create a virtual master control and add slaves
2644 * @codec: HD-audio codec
2645 * @name: vmaster control name
2646 * @tlv: TLV data (optional)
2647 * @slaves: slave control names (optional)
9322ca54 2648 * @suffix: suffix string to each slave name (optional)
18478e8b 2649 * @init_slave_vol: initialize slaves to unmute/0dB
29e5853d 2650 * @ctl_ret: store the vmaster kcontrol in return
d5191e50
TI
2651 *
2652 * Create a virtual master control with the given name. The TLV data
2653 * must be either NULL or a valid data.
2654 *
2655 * @slaves is a NULL-terminated array of strings, each of which is a
2656 * slave control name. All controls with these names are assigned to
2657 * the new virtual master control.
2658 *
2659 * This function returns zero if successful or a negative error code.
2660 */
18478e8b 2661int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
9322ca54 2662 unsigned int *tlv, const char * const *slaves,
29e5853d
TI
2663 const char *suffix, bool init_slave_vol,
2664 struct snd_kcontrol **ctl_ret)
2134ea4f
TI
2665{
2666 struct snd_kcontrol *kctl;
2134ea4f
TI
2667 int err;
2668
29e5853d
TI
2669 if (ctl_ret)
2670 *ctl_ret = NULL;
2671
9322ca54 2672 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
aeb4b88e 2673 if (err != 1) {
2f085549
TI
2674 snd_printdd("No slave found for %s\n", name);
2675 return 0;
2676 }
2134ea4f
TI
2677 kctl = snd_ctl_make_virtual_master(name, tlv);
2678 if (!kctl)
2679 return -ENOMEM;
3911a4c1 2680 err = snd_hda_ctl_add(codec, 0, kctl);
2134ea4f
TI
2681 if (err < 0)
2682 return err;
28aedaf7 2683
9322ca54
TI
2684 err = map_slaves(codec, slaves, suffix,
2685 (map_slave_func_t)snd_ctl_add_slave, kctl);
aeb4b88e
TI
2686 if (err < 0)
2687 return err;
18478e8b
TI
2688
2689 /* init with master mute & zero volume */
2690 put_kctl_with_value(kctl, 0);
2691 if (init_slave_vol)
2692 map_slaves(codec, slaves, suffix,
2693 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2694
29e5853d
TI
2695 if (ctl_ret)
2696 *ctl_ret = kctl;
2134ea4f
TI
2697 return 0;
2698}
18478e8b 2699EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2134ea4f 2700
d2f344b5
TI
2701/*
2702 * mute-LED control using vmaster
2703 */
2704static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2705 struct snd_ctl_elem_info *uinfo)
2706{
2707 static const char * const texts[] = {
c86c2d44 2708 "On", "Off", "Follow Master"
d2f344b5
TI
2709 };
2710 unsigned int index;
2711
2712 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2713 uinfo->count = 1;
2714 uinfo->value.enumerated.items = 3;
2715 index = uinfo->value.enumerated.item;
2716 if (index >= 3)
2717 index = 2;
2718 strcpy(uinfo->value.enumerated.name, texts[index]);
2719 return 0;
2720}
2721
2722static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2723 struct snd_ctl_elem_value *ucontrol)
2724{
2725 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2726 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2727 return 0;
2728}
2729
2730static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2731 struct snd_ctl_elem_value *ucontrol)
2732{
2733 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2734 unsigned int old_mode = hook->mute_mode;
2735
2736 hook->mute_mode = ucontrol->value.enumerated.item[0];
2737 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2738 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2739 if (old_mode == hook->mute_mode)
2740 return 0;
2741 snd_hda_sync_vmaster_hook(hook);
2742 return 1;
2743}
2744
2745static struct snd_kcontrol_new vmaster_mute_mode = {
2746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2747 .name = "Mute-LED Mode",
2748 .info = vmaster_mute_mode_info,
2749 .get = vmaster_mute_mode_get,
2750 .put = vmaster_mute_mode_put,
2751};
2752
2753/*
2754 * Add a mute-LED hook with the given vmaster switch kctl
2755 * "Mute-LED Mode" control is automatically created and associated with
2756 * the given hook.
2757 */
2758int snd_hda_add_vmaster_hook(struct hda_codec *codec,
f29735cb
TI
2759 struct hda_vmaster_mute_hook *hook,
2760 bool expose_enum_ctl)
d2f344b5
TI
2761{
2762 struct snd_kcontrol *kctl;
2763
2764 if (!hook->hook || !hook->sw_kctl)
2765 return 0;
2766 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2767 hook->codec = codec;
2768 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
f29735cb
TI
2769 if (!expose_enum_ctl)
2770 return 0;
d2f344b5
TI
2771 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2772 if (!kctl)
2773 return -ENOMEM;
2774 return snd_hda_ctl_add(codec, 0, kctl);
2775}
2776EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2777
2778/*
2779 * Call the hook with the current value for synchronization
2780 * Should be called in init callback
2781 */
2782void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2783{
2784 if (!hook->hook || !hook->codec)
2785 return;
2786 switch (hook->mute_mode) {
2787 case HDA_VMUTE_FOLLOW_MASTER:
2788 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2789 break;
2790 default:
2791 hook->hook(hook->codec, hook->mute_mode);
2792 break;
2793 }
2794}
2795EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2796
2797
d5191e50
TI
2798/**
2799 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2800 *
2801 * The control element is supposed to have the private_value field
2802 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2803 */
0ba21762
TI
2804int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2806{
2807 int chs = get_amp_channels(kcontrol);
2808
2809 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2810 uinfo->count = chs == 3 ? 2 : 1;
2811 uinfo->value.integer.min = 0;
2812 uinfo->value.integer.max = 1;
2813 return 0;
2814}
ff7a3267 2815EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1da177e4 2816
d5191e50
TI
2817/**
2818 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2819 *
2820 * The control element is supposed to have the private_value field
2821 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2822 */
0ba21762
TI
2823int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2825{
2826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 hda_nid_t nid = get_amp_nid(kcontrol);
2828 int chs = get_amp_channels(kcontrol);
2829 int dir = get_amp_direction(kcontrol);
2830 int idx = get_amp_index(kcontrol);
2831 long *valp = ucontrol->value.integer.value;
2832
2833 if (chs & 1)
0ba21762 2834 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
47fd830a 2835 HDA_AMP_MUTE) ? 0 : 1;
1da177e4 2836 if (chs & 2)
0ba21762 2837 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
47fd830a 2838 HDA_AMP_MUTE) ? 0 : 1;
1da177e4
LT
2839 return 0;
2840}
ff7a3267 2841EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1da177e4 2842
d5191e50
TI
2843/**
2844 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2845 *
2846 * The control element is supposed to have the private_value field
2847 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2848 */
0ba21762
TI
2849int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2851{
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2853 hda_nid_t nid = get_amp_nid(kcontrol);
2854 int chs = get_amp_channels(kcontrol);
2855 int dir = get_amp_direction(kcontrol);
2856 int idx = get_amp_index(kcontrol);
1da177e4
LT
2857 long *valp = ucontrol->value.integer.value;
2858 int change = 0;
2859
cb53c626 2860 snd_hda_power_up(codec);
b9f5a89c 2861 if (chs & 1) {
4a19faee 2862 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
47fd830a
TI
2863 HDA_AMP_MUTE,
2864 *valp ? 0 : HDA_AMP_MUTE);
b9f5a89c
NG
2865 valp++;
2866 }
4a19faee
TI
2867 if (chs & 2)
2868 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
47fd830a
TI
2869 HDA_AMP_MUTE,
2870 *valp ? 0 : HDA_AMP_MUTE);
9e5341b9 2871 hda_call_check_power_status(codec, nid);
cb53c626 2872 snd_hda_power_down(codec);
1da177e4
LT
2873 return change;
2874}
ff7a3267 2875EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1da177e4 2876
985be54b
TI
2877/*
2878 * bound volume controls
2879 *
2880 * bind multiple volumes (# indices, from 0)
2881 */
2882
2883#define AMP_VAL_IDX_SHIFT 19
2884#define AMP_VAL_IDX_MASK (0x0f<<19)
2885
d5191e50
TI
2886/**
2887 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2888 *
2889 * The control element is supposed to have the private_value field
2890 * set up via HDA_BIND_MUTE*() macros.
2891 */
0ba21762
TI
2892int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2894{
2895 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2896 unsigned long pval;
2897 int err;
2898
5a9e02e9 2899 mutex_lock(&codec->control_mutex);
985be54b
TI
2900 pval = kcontrol->private_value;
2901 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2902 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2903 kcontrol->private_value = pval;
5a9e02e9 2904 mutex_unlock(&codec->control_mutex);
985be54b
TI
2905 return err;
2906}
ff7a3267 2907EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
985be54b 2908
d5191e50
TI
2909/**
2910 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2911 *
2912 * The control element is supposed to have the private_value field
2913 * set up via HDA_BIND_MUTE*() macros.
2914 */
0ba21762
TI
2915int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2917{
2918 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2919 unsigned long pval;
2920 int i, indices, err = 0, change = 0;
2921
5a9e02e9 2922 mutex_lock(&codec->control_mutex);
985be54b
TI
2923 pval = kcontrol->private_value;
2924 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2925 for (i = 0; i < indices; i++) {
0ba21762
TI
2926 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2927 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
2928 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2929 if (err < 0)
2930 break;
2931 change |= err;
2932 }
2933 kcontrol->private_value = pval;
5a9e02e9 2934 mutex_unlock(&codec->control_mutex);
985be54b
TI
2935 return err < 0 ? err : change;
2936}
ff7a3267 2937EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
985be54b 2938
d5191e50
TI
2939/**
2940 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2941 *
2942 * The control element is supposed to have the private_value field
2943 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
532d5381
TI
2944 */
2945int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_info *uinfo)
2947{
2948 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2949 struct hda_bind_ctls *c;
2950 int err;
2951
5a9e02e9 2952 mutex_lock(&codec->control_mutex);
14c65f98 2953 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2954 kcontrol->private_value = *c->values;
2955 err = c->ops->info(kcontrol, uinfo);
2956 kcontrol->private_value = (long)c;
5a9e02e9 2957 mutex_unlock(&codec->control_mutex);
532d5381
TI
2958 return err;
2959}
ff7a3267 2960EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
532d5381 2961
d5191e50
TI
2962/**
2963 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2964 *
2965 * The control element is supposed to have the private_value field
2966 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2967 */
532d5381
TI
2968int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
2970{
2971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2972 struct hda_bind_ctls *c;
2973 int err;
2974
5a9e02e9 2975 mutex_lock(&codec->control_mutex);
14c65f98 2976 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2977 kcontrol->private_value = *c->values;
2978 err = c->ops->get(kcontrol, ucontrol);
2979 kcontrol->private_value = (long)c;
5a9e02e9 2980 mutex_unlock(&codec->control_mutex);
532d5381
TI
2981 return err;
2982}
ff7a3267 2983EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
532d5381 2984
d5191e50
TI
2985/**
2986 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2987 *
2988 * The control element is supposed to have the private_value field
2989 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2990 */
532d5381
TI
2991int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2992 struct snd_ctl_elem_value *ucontrol)
2993{
2994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2995 struct hda_bind_ctls *c;
2996 unsigned long *vals;
2997 int err = 0, change = 0;
2998
5a9e02e9 2999 mutex_lock(&codec->control_mutex);
14c65f98 3000 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
3001 for (vals = c->values; *vals; vals++) {
3002 kcontrol->private_value = *vals;
3003 err = c->ops->put(kcontrol, ucontrol);
3004 if (err < 0)
3005 break;
3006 change |= err;
3007 }
3008 kcontrol->private_value = (long)c;
5a9e02e9 3009 mutex_unlock(&codec->control_mutex);
532d5381
TI
3010 return err < 0 ? err : change;
3011}
ff7a3267 3012EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
532d5381 3013
d5191e50
TI
3014/**
3015 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3016 *
3017 * The control element is supposed to have the private_value field
3018 * set up via HDA_BIND_VOL() macro.
3019 */
532d5381
TI
3020int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3021 unsigned int size, unsigned int __user *tlv)
3022{
3023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3024 struct hda_bind_ctls *c;
3025 int err;
3026
5a9e02e9 3027 mutex_lock(&codec->control_mutex);
14c65f98 3028 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
3029 kcontrol->private_value = *c->values;
3030 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3031 kcontrol->private_value = (long)c;
5a9e02e9 3032 mutex_unlock(&codec->control_mutex);
532d5381
TI
3033 return err;
3034}
ff7a3267 3035EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
532d5381
TI
3036
3037struct hda_ctl_ops snd_hda_bind_vol = {
3038 .info = snd_hda_mixer_amp_volume_info,
3039 .get = snd_hda_mixer_amp_volume_get,
3040 .put = snd_hda_mixer_amp_volume_put,
3041 .tlv = snd_hda_mixer_amp_tlv
3042};
ff7a3267 3043EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
532d5381
TI
3044
3045struct hda_ctl_ops snd_hda_bind_sw = {
3046 .info = snd_hda_mixer_amp_switch_info,
3047 .get = snd_hda_mixer_amp_switch_get,
3048 .put = snd_hda_mixer_amp_switch_put,
3049 .tlv = snd_hda_mixer_amp_tlv
3050};
ff7a3267 3051EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
532d5381 3052
1da177e4
LT
3053/*
3054 * SPDIF out controls
3055 */
3056
0ba21762
TI
3057static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
3059{
3060 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3061 uinfo->count = 1;
3062 return 0;
3063}
3064
0ba21762
TI
3065static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3067{
3068 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3069 IEC958_AES0_NONAUDIO |
3070 IEC958_AES0_CON_EMPHASIS_5015 |
3071 IEC958_AES0_CON_NOT_COPYRIGHT;
3072 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3073 IEC958_AES1_CON_ORIGINAL;
3074 return 0;
3075}
3076
0ba21762
TI
3077static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3078 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3079{
3080 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3081 IEC958_AES0_NONAUDIO |
3082 IEC958_AES0_PRO_EMPHASIS_5015;
3083 return 0;
3084}
3085
0ba21762
TI
3086static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3087 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3088{
3089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3090 int idx = kcontrol->private_value;
e3245cdd 3091 struct hda_spdif_out *spdif;
1da177e4 3092
e3245cdd
TI
3093 mutex_lock(&codec->spdif_mutex);
3094 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976
SW
3095 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3096 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3097 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3098 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
e3245cdd 3099 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3100
3101 return 0;
3102}
3103
3104/* convert from SPDIF status bits to HDA SPDIF bits
3105 * bit 0 (DigEn) is always set zero (to be filled later)
3106 */
3107static unsigned short convert_from_spdif_status(unsigned int sbits)
3108{
3109 unsigned short val = 0;
3110
3111 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 3112 val |= AC_DIG1_PROFESSIONAL;
1da177e4 3113 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 3114 val |= AC_DIG1_NONAUDIO;
1da177e4 3115 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
3116 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3117 IEC958_AES0_PRO_EMPHASIS_5015)
3118 val |= AC_DIG1_EMPHASIS;
1da177e4 3119 } else {
0ba21762
TI
3120 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3121 IEC958_AES0_CON_EMPHASIS_5015)
3122 val |= AC_DIG1_EMPHASIS;
3123 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3124 val |= AC_DIG1_COPYRIGHT;
1da177e4 3125 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 3126 val |= AC_DIG1_LEVEL;
1da177e4
LT
3127 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3128 }
3129 return val;
3130}
3131
3132/* convert to SPDIF status bits from HDA SPDIF bits
3133 */
3134static unsigned int convert_to_spdif_status(unsigned short val)
3135{
3136 unsigned int sbits = 0;
3137
0ba21762 3138 if (val & AC_DIG1_NONAUDIO)
1da177e4 3139 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 3140 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
3141 sbits |= IEC958_AES0_PROFESSIONAL;
3142 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762 3143 if (sbits & AC_DIG1_EMPHASIS)
1da177e4
LT
3144 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3145 } else {
0ba21762 3146 if (val & AC_DIG1_EMPHASIS)
1da177e4 3147 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 3148 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 3149 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 3150 if (val & AC_DIG1_LEVEL)
1da177e4
LT
3151 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3152 sbits |= val & (0x7f << 8);
3153 }
3154 return sbits;
3155}
3156
2f72853c
TI
3157/* set digital convert verbs both for the given NID and its slaves */
3158static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3159 int verb, int val)
3160{
dda14410 3161 const hda_nid_t *d;
2f72853c 3162
9e976976 3163 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2f72853c
TI
3164 d = codec->slave_dig_outs;
3165 if (!d)
3166 return;
3167 for (; *d; d++)
9e976976 3168 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2f72853c
TI
3169}
3170
3171static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3172 int dig1, int dig2)
3173{
3174 if (dig1 != -1)
3175 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3176 if (dig2 != -1)
3177 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3178}
3179
0ba21762
TI
3180static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3181 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3182{
3183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3184 int idx = kcontrol->private_value;
e3245cdd
TI
3185 struct hda_spdif_out *spdif;
3186 hda_nid_t nid;
1da177e4
LT
3187 unsigned short val;
3188 int change;
3189
62932df8 3190 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
3191 spdif = snd_array_elem(&codec->spdif_out, idx);
3192 nid = spdif->nid;
7c935976 3193 spdif->status = ucontrol->value.iec958.status[0] |
1da177e4
LT
3194 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3195 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3196 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
7c935976
SW
3197 val = convert_from_spdif_status(spdif->status);
3198 val |= spdif->ctls & 1;
3199 change = spdif->ctls != val;
3200 spdif->ctls = val;
74b654c9 3201 if (change && nid != (u16)-1)
2f72853c 3202 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
62932df8 3203 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3204 return change;
3205}
3206
a5ce8890 3207#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1da177e4 3208
0ba21762
TI
3209static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3211{
3212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3213 int idx = kcontrol->private_value;
e3245cdd 3214 struct hda_spdif_out *spdif;
1da177e4 3215
e3245cdd
TI
3216 mutex_lock(&codec->spdif_mutex);
3217 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976 3218 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
e3245cdd 3219 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3220 return 0;
3221}
3222
74b654c9
SW
3223static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3224 int dig1, int dig2)
3225{
3226 set_dig_out_convert(codec, nid, dig1, dig2);
3227 /* unmute amp switch (if any) */
3228 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3229 (dig1 & AC_DIG1_ENABLE))
3230 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3231 HDA_AMP_MUTE, 0);
3232}
3233
0ba21762
TI
3234static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3235 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3236{
3237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 3238 int idx = kcontrol->private_value;
e3245cdd
TI
3239 struct hda_spdif_out *spdif;
3240 hda_nid_t nid;
1da177e4
LT
3241 unsigned short val;
3242 int change;
3243
62932df8 3244 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
3245 spdif = snd_array_elem(&codec->spdif_out, idx);
3246 nid = spdif->nid;
7c935976 3247 val = spdif->ctls & ~AC_DIG1_ENABLE;
1da177e4 3248 if (ucontrol->value.integer.value[0])
0ba21762 3249 val |= AC_DIG1_ENABLE;
7c935976 3250 change = spdif->ctls != val;
74b654c9
SW
3251 spdif->ctls = val;
3252 if (change && nid != (u16)-1)
3253 set_spdif_ctls(codec, nid, val & 0xff, -1);
62932df8 3254 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3255 return change;
3256}
3257
c8b6bf9b 3258static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
3259 {
3260 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3261 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3262 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1da177e4
LT
3263 .info = snd_hda_spdif_mask_info,
3264 .get = snd_hda_spdif_cmask_get,
3265 },
3266 {
3267 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3268 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3269 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1da177e4
LT
3270 .info = snd_hda_spdif_mask_info,
3271 .get = snd_hda_spdif_pmask_get,
3272 },
3273 {
3274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3275 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1da177e4
LT
3276 .info = snd_hda_spdif_mask_info,
3277 .get = snd_hda_spdif_default_get,
3278 .put = snd_hda_spdif_default_put,
3279 },
3280 {
3281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3282 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1da177e4
LT
3283 .info = snd_hda_spdif_out_switch_info,
3284 .get = snd_hda_spdif_out_switch_get,
3285 .put = snd_hda_spdif_out_switch_put,
3286 },
3287 { } /* end */
3288};
3289
3290/**
dcda5806 3291 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
1da177e4 3292 * @codec: the HDA codec
dcda5806
TI
3293 * @associated_nid: NID that new ctls associated with
3294 * @cvt_nid: converter NID
3295 * @type: HDA_PCM_TYPE_*
3296 * Creates controls related with the digital output.
3297 * Called from each patch supporting the digital out.
1da177e4
LT
3298 *
3299 * Returns 0 if successful, or a negative error code.
3300 */
dcda5806
TI
3301int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3302 hda_nid_t associated_nid,
3303 hda_nid_t cvt_nid,
3304 int type)
1da177e4
LT
3305{
3306 int err;
c8b6bf9b
TI
3307 struct snd_kcontrol *kctl;
3308 struct snd_kcontrol_new *dig_mix;
ea9b43ad
TI
3309 int idx = 0;
3310 const int spdif_index = 16;
7c935976 3311 struct hda_spdif_out *spdif;
ea9b43ad 3312 struct hda_bus *bus = codec->bus;
1da177e4 3313
ea9b43ad 3314 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
dcda5806 3315 type == HDA_PCM_TYPE_SPDIF) {
ea9b43ad
TI
3316 idx = spdif_index;
3317 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
dcda5806 3318 type == HDA_PCM_TYPE_HDMI) {
ea9b43ad
TI
3319 /* suppose a single SPDIF device */
3320 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3321 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3322 if (!kctl)
3323 break;
3324 kctl->id.index = spdif_index;
dcda5806 3325 }
ea9b43ad 3326 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
dcda5806 3327 }
ea9b43ad
TI
3328 if (!bus->primary_dig_out_type)
3329 bus->primary_dig_out_type = type;
dcda5806 3330
ea9b43ad 3331 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
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);
25336e8a
ML
3337 if (!spdif)
3338 return -ENOMEM;
1da177e4
LT
3339 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3340 kctl = snd_ctl_new1(dig_mix, codec);
b91f080f
TI
3341 if (!kctl)
3342 return -ENOMEM;
09f99701 3343 kctl->id.index = idx;
7c935976 3344 kctl->private_value = codec->spdif_out.used - 1;
74b654c9 3345 err = snd_hda_ctl_add(codec, associated_nid, kctl);
0ba21762 3346 if (err < 0)
1da177e4
LT
3347 return err;
3348 }
74b654c9
SW
3349 spdif->nid = cvt_nid;
3350 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
7c935976
SW
3351 AC_VERB_GET_DIGI_CONVERT_1, 0);
3352 spdif->status = convert_to_spdif_status(spdif->ctls);
1da177e4
LT
3353 return 0;
3354}
dcda5806 3355EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
1da177e4 3356
e3245cdd
TI
3357/* get the hda_spdif_out entry from the given NID
3358 * call within spdif_mutex lock
3359 */
7c935976
SW
3360struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3361 hda_nid_t nid)
3362{
3363 int i;
3364 for (i = 0; i < codec->spdif_out.used; i++) {
3365 struct hda_spdif_out *spdif =
3366 snd_array_elem(&codec->spdif_out, i);
3367 if (spdif->nid == nid)
3368 return spdif;
3369 }
3370 return NULL;
3371}
3372EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3373
74b654c9
SW
3374void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3375{
e3245cdd 3376 struct hda_spdif_out *spdif;
74b654c9
SW
3377
3378 mutex_lock(&codec->spdif_mutex);
e3245cdd 3379 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
3380 spdif->nid = (u16)-1;
3381 mutex_unlock(&codec->spdif_mutex);
3382}
3383EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3384
3385void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3386{
e3245cdd 3387 struct hda_spdif_out *spdif;
74b654c9
SW
3388 unsigned short val;
3389
3390 mutex_lock(&codec->spdif_mutex);
e3245cdd 3391 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
3392 if (spdif->nid != nid) {
3393 spdif->nid = nid;
3394 val = spdif->ctls;
3395 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3396 }
3397 mutex_unlock(&codec->spdif_mutex);
3398}
3399EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3400
9a08160b
TI
3401/*
3402 * SPDIF sharing with analog output
3403 */
3404static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3406{
3407 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3408 ucontrol->value.integer.value[0] = mout->share_spdif;
3409 return 0;
3410}
3411
3412static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
3415 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3416 mout->share_spdif = !!ucontrol->value.integer.value[0];
3417 return 0;
3418}
3419
3420static struct snd_kcontrol_new spdif_share_sw = {
3421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3422 .name = "IEC958 Default PCM Playback Switch",
3423 .info = snd_ctl_boolean_mono_info,
3424 .get = spdif_share_sw_get,
3425 .put = spdif_share_sw_put,
3426};
3427
d5191e50
TI
3428/**
3429 * snd_hda_create_spdif_share_sw - create Default PCM switch
3430 * @codec: the HDA codec
3431 * @mout: multi-out instance
3432 */
9a08160b
TI
3433int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3434 struct hda_multi_out *mout)
3435{
3436 if (!mout->dig_out_nid)
3437 return 0;
3438 /* ATTENTION: here mout is passed as private_data, instead of codec */
3911a4c1
JK
3439 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3440 snd_ctl_new1(&spdif_share_sw, mout));
9a08160b 3441}
ff7a3267 3442EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
9a08160b 3443
1da177e4
LT
3444/*
3445 * SPDIF input
3446 */
3447
3448#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3449
0ba21762
TI
3450static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3451 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3452{
3453 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3454
3455 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3456 return 0;
3457}
3458
0ba21762
TI
3459static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3460 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3461{
3462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3463 hda_nid_t nid = kcontrol->private_value;
3464 unsigned int val = !!ucontrol->value.integer.value[0];
3465 int change;
3466
62932df8 3467 mutex_lock(&codec->spdif_mutex);
1da177e4 3468 change = codec->spdif_in_enable != val;
82beb8fd 3469 if (change) {
1da177e4 3470 codec->spdif_in_enable = val;
82beb8fd
TI
3471 snd_hda_codec_write_cache(codec, nid, 0,
3472 AC_VERB_SET_DIGI_CONVERT_1, val);
1da177e4 3473 }
62932df8 3474 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3475 return change;
3476}
3477
0ba21762
TI
3478static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3479 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3480{
3481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3482 hda_nid_t nid = kcontrol->private_value;
3483 unsigned short val;
3484 unsigned int sbits;
3485
3982d17e 3486 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1da177e4
LT
3487 sbits = convert_to_spdif_status(val);
3488 ucontrol->value.iec958.status[0] = sbits;
3489 ucontrol->value.iec958.status[1] = sbits >> 8;
3490 ucontrol->value.iec958.status[2] = sbits >> 16;
3491 ucontrol->value.iec958.status[3] = sbits >> 24;
3492 return 0;
3493}
3494
c8b6bf9b 3495static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
3496 {
3497 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3498 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
1da177e4
LT
3499 .info = snd_hda_spdif_in_switch_info,
3500 .get = snd_hda_spdif_in_switch_get,
3501 .put = snd_hda_spdif_in_switch_put,
3502 },
3503 {
3504 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3505 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3506 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
1da177e4
LT
3507 .info = snd_hda_spdif_mask_info,
3508 .get = snd_hda_spdif_in_status_get,
3509 },
3510 { } /* end */
3511};
3512
3513/**
3514 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3515 * @codec: the HDA codec
3516 * @nid: audio in widget NID
3517 *
3518 * Creates controls related with the SPDIF input.
3519 * Called from each patch supporting the SPDIF in.
3520 *
3521 * Returns 0 if successful, or a negative error code.
3522 */
12f288bf 3523int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
3524{
3525 int err;
c8b6bf9b
TI
3526 struct snd_kcontrol *kctl;
3527 struct snd_kcontrol_new *dig_mix;
09f99701 3528 int idx;
1da177e4 3529
dcda5806 3530 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
1afe206a 3531 if (idx < 0) {
09f99701
TI
3532 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3533 return -EBUSY;
3534 }
1da177e4
LT
3535 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3536 kctl = snd_ctl_new1(dig_mix, codec);
c8dcdf82
TI
3537 if (!kctl)
3538 return -ENOMEM;
1da177e4 3539 kctl->private_value = nid;
3911a4c1 3540 err = snd_hda_ctl_add(codec, nid, kctl);
0ba21762 3541 if (err < 0)
1da177e4
LT
3542 return err;
3543 }
0ba21762 3544 codec->spdif_in_enable =
3982d17e
AP
3545 snd_hda_codec_read(codec, nid, 0,
3546 AC_VERB_GET_DIGI_CONVERT_1, 0) &
0ba21762 3547 AC_DIG1_ENABLE;
1da177e4
LT
3548 return 0;
3549}
ff7a3267 3550EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
1da177e4 3551
82beb8fd
TI
3552/*
3553 * command cache
3554 */
1da177e4 3555
c370dd6e 3556/* build a 31bit cache key with the widget id and the command parameter */
b3ac5636
TI
3557#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3558#define get_cmd_cache_nid(key) ((key) & 0xff)
3559#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3560
3561/**
3562 * snd_hda_codec_write_cache - send a single command with caching
3563 * @codec: the HDA codec
3564 * @nid: NID to send the command
3565 * @direct: direct flag
3566 * @verb: the verb to send
3567 * @parm: the parameter for the verb
3568 *
3569 * Send a single command without waiting for response.
3570 *
3571 * Returns 0 if successful, or a negative error code.
3572 */
3573int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3574 int direct, unsigned int verb, unsigned int parm)
3575{
c370dd6e 3576 int err;
aa2936f5
TI
3577 struct hda_cache_head *c;
3578 u32 key;
de1e37b7 3579 unsigned int cache_only;
33fa35ed 3580
de1e37b7
TI
3581 cache_only = codec->cached_write;
3582 if (!cache_only) {
c370dd6e
TI
3583 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3584 if (err < 0)
3585 return err;
3586 }
3587
aa2936f5
TI
3588 /* parm may contain the verb stuff for get/set amp */
3589 verb = verb | (parm >> 8);
3590 parm &= 0xff;
3591 key = build_cmd_cache_key(nid, verb);
3592 mutex_lock(&codec->bus->cmd_mutex);
3593 c = get_alloc_hash(&codec->cmd_cache, key);
c370dd6e 3594 if (c) {
aa2936f5 3595 c->val = parm;
de1e37b7 3596 c->dirty = cache_only;
c370dd6e 3597 }
aa2936f5
TI
3598 mutex_unlock(&codec->bus->cmd_mutex);
3599 return 0;
b3ac5636 3600}
ff7a3267 3601EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
b3ac5636 3602
a68d5a54
TI
3603/**
3604 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3605 * @codec: the HDA codec
3606 * @nid: NID to send the command
3607 * @direct: direct flag
3608 * @verb: the verb to send
3609 * @parm: the parameter for the verb
3610 *
3611 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3612 * command if the parameter is already identical with the cached value.
3613 * If not, it sends the command and refreshes the cache.
3614 *
3615 * Returns 0 if successful, or a negative error code.
3616 */
3617int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3618 int direct, unsigned int verb, unsigned int parm)
3619{
3620 struct hda_cache_head *c;
3621 u32 key;
3622
3623 /* parm may contain the verb stuff for get/set amp */
3624 verb = verb | (parm >> 8);
3625 parm &= 0xff;
3626 key = build_cmd_cache_key(nid, verb);
3627 mutex_lock(&codec->bus->cmd_mutex);
3628 c = get_hash(&codec->cmd_cache, key);
3629 if (c && c->val == parm) {
3630 mutex_unlock(&codec->bus->cmd_mutex);
3631 return 0;
3632 }
3633 mutex_unlock(&codec->bus->cmd_mutex);
3634 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3635}
3636EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3637
d5191e50
TI
3638/**
3639 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3640 * @codec: HD-audio codec
3641 *
3642 * Execute all verbs recorded in the command caches to resume.
3643 */
b3ac5636
TI
3644void snd_hda_codec_resume_cache(struct hda_codec *codec)
3645{
b3ac5636
TI
3646 int i;
3647
c370dd6e 3648 mutex_lock(&codec->hash_mutex);
aa88a355 3649 codec->cached_write = 0;
c370dd6e
TI
3650 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3651 struct hda_cache_head *buffer;
3652 u32 key;
3653
3654 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3655 key = buffer->key;
b3ac5636
TI
3656 if (!key)
3657 continue;
c370dd6e
TI
3658 if (!buffer->dirty)
3659 continue;
3660 buffer->dirty = 0;
3661 mutex_unlock(&codec->hash_mutex);
b3ac5636
TI
3662 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3663 get_cmd_cache_cmd(key), buffer->val);
c370dd6e 3664 mutex_lock(&codec->hash_mutex);
b3ac5636 3665 }
c370dd6e 3666 mutex_unlock(&codec->hash_mutex);
b3ac5636 3667}
ff7a3267 3668EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
b3ac5636
TI
3669
3670/**
3671 * snd_hda_sequence_write_cache - sequence writes with caching
3672 * @codec: the HDA codec
3673 * @seq: VERB array to send
3674 *
3675 * Send the commands sequentially from the given array.
3676 * Thte commands are recorded on cache for power-save and resume.
3677 * The array must be terminated with NID=0.
3678 */
3679void snd_hda_sequence_write_cache(struct hda_codec *codec,
3680 const struct hda_verb *seq)
3681{
3682 for (; seq->nid; seq++)
3683 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3684 seq->param);
3685}
ff7a3267 3686EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
b3ac5636 3687
dc870f38
TI
3688/**
3689 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3690 * @codec: HD-audio codec
3691 */
3692void snd_hda_codec_flush_cache(struct hda_codec *codec)
3693{
3694 snd_hda_codec_resume_amp(codec);
3695 snd_hda_codec_resume_cache(codec);
3696}
3697EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3698
4d7fbdbc 3699void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
9419ab6b 3700 unsigned int power_state)
54d17403 3701{
4d7fbdbc 3702 hda_nid_t nid = codec->start_nid;
cb53c626 3703 int i;
54d17403 3704
cb53c626 3705 for (i = 0; i < codec->num_nodes; i++, nid++) {
7eba5c9d 3706 unsigned int wcaps = get_wcaps(codec, nid);
9419ab6b 3707 unsigned int state = power_state;
4d7fbdbc
TI
3708 if (!(wcaps & AC_WCAP_POWER))
3709 continue;
9419ab6b
TI
3710 if (codec->power_filter) {
3711 state = codec->power_filter(codec, nid, power_state);
3712 if (state != power_state && power_state == AC_PWRST_D3)
4d7fbdbc 3713 continue;
1194b5b7 3714 }
4d7fbdbc 3715 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
9419ab6b 3716 state);
54d17403 3717 }
cb53c626 3718}
4d7fbdbc
TI
3719EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3720
0c7f46ad
WX
3721/*
3722 * supported power states check
3723 */
3724static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3725 unsigned int power_state)
3726{
3727 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3728
e037cb4a 3729 if (sup == -1)
0c7f46ad
WX
3730 return false;
3731 if (sup & power_state)
3732 return true;
3733 else
3734 return false;
3735}
3736
432c641e
TI
3737/*
3738 * wait until the state is reached, returns the current state
3739 */
3740static unsigned int hda_sync_power_state(struct hda_codec *codec,
3741 hda_nid_t fg,
3742 unsigned int power_state)
3743{
3744 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3745 unsigned int state, actual_state;
3746
3747 for (;;) {
3748 state = snd_hda_codec_read(codec, fg, 0,
3749 AC_VERB_GET_POWER_STATE, 0);
3750 if (state & AC_PWRST_ERROR)
3751 break;
3752 actual_state = (state >> 4) & 0x0f;
3753 if (actual_state == power_state)
3754 break;
3755 if (time_after_eq(jiffies, end_time))
3756 break;
3757 /* wait until the codec reachs to the target state */
3758 msleep(1);
3759 }
3760 return state;
3761}
3762
9419ab6b
TI
3763/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3764static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
3765 unsigned int power_state)
3766{
3767 if (power_state == AC_PWRST_D3 &&
3768 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3769 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3770 int eapd = snd_hda_codec_read(codec, nid, 0,
3771 AC_VERB_GET_EAPD_BTLENABLE, 0);
3772 if (eapd & 0x02)
3773 return AC_PWRST_D0;
3774 }
3775 return power_state;
3776}
3777
4d7fbdbc 3778/*
08fa20ae 3779 * set power state of the codec, and return the power state
4d7fbdbc 3780 */
d819387e 3781static unsigned int hda_set_power_state(struct hda_codec *codec,
08fa20ae 3782 unsigned int power_state)
4d7fbdbc 3783{
d819387e 3784 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
09617ce4
WX
3785 int count;
3786 unsigned int state;
3787
4d7fbdbc 3788 /* this delay seems necessary to avoid click noise at power-down */
0f4ccbb0
WX
3789 if (power_state == AC_PWRST_D3) {
3790 /* transition time less than 10ms for power down */
983f6b93 3791 msleep(codec->epss ? 10 : 100);
0f4ccbb0 3792 }
09617ce4
WX
3793
3794 /* repeat power states setting at most 10 times*/
3795 for (count = 0; count < 10; count++) {
432c641e
TI
3796 if (codec->patch_ops.set_power_state)
3797 codec->patch_ops.set_power_state(codec, fg,
3798 power_state);
3799 else {
3800 snd_hda_codec_read(codec, fg, 0,
3801 AC_VERB_SET_POWER_STATE,
3802 power_state);
9419ab6b 3803 snd_hda_codec_set_power_to_all(codec, fg, power_state);
432c641e
TI
3804 }
3805 state = hda_sync_power_state(codec, fg, power_state);
09617ce4
WX
3806 if (!(state & AC_PWRST_ERROR))
3807 break;
3808 }
b8dfc462 3809
08fa20ae 3810 return state;
4d7fbdbc 3811}
cb53c626 3812
b9c590bb
TI
3813/* sync power states of all widgets;
3814 * this is called at the end of codec parsing
3815 */
3816static void sync_power_up_states(struct hda_codec *codec)
3817{
3818 hda_nid_t nid = codec->start_nid;
3819 int i;
3820
3821 /* don't care if no or standard filter is used */
3822 if (!codec->power_filter || codec->power_filter == default_power_filter)
3823 return;
3824
3825 for (i = 0; i < codec->num_nodes; i++, nid++) {
3826 unsigned int wcaps = get_wcaps(codec, nid);
9040d102 3827 unsigned int target;
b9c590bb
TI
3828 if (!(wcaps & AC_WCAP_POWER))
3829 continue;
3830 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3831 if (target == AC_PWRST_D0)
3832 continue;
9040d102 3833 if (!snd_hda_check_power_state(codec, nid, target))
b9c590bb
TI
3834 snd_hda_codec_write(codec, nid, 0,
3835 AC_VERB_SET_POWER_STATE, target);
3836 }
3837}
3838
11aeff08
TI
3839#ifdef CONFIG_SND_HDA_HWDEP
3840/* execute additional init verbs */
3841static void hda_exec_init_verbs(struct hda_codec *codec)
3842{
3843 if (codec->init_verbs.list)
3844 snd_hda_sequence_write(codec, codec->init_verbs.list);
3845}
3846#else
3847static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3848#endif
3849
2a43952a 3850#ifdef CONFIG_PM
cb53c626
TI
3851/*
3852 * call suspend and power-down; used both from PM and power-save
08fa20ae 3853 * this function returns the power state in the end
cb53c626 3854 */
d17344b3 3855static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
cb53c626 3856{
08fa20ae
TI
3857 unsigned int state;
3858
989c3187
TI
3859 codec->in_pm = 1;
3860
cb53c626 3861 if (codec->patch_ops.suspend)
68cb2b55 3862 codec->patch_ops.suspend(codec);
eb541337 3863 hda_cleanup_all_streams(codec);
d819387e 3864 state = hda_set_power_state(codec, AC_PWRST_D3);
d17344b3
DR
3865 /* Cancel delayed work if we aren't currently running from it. */
3866 if (!in_wq)
3867 cancel_delayed_work_sync(&codec->power_work);
a2d96e77
TI
3868 spin_lock(&codec->power_lock);
3869 snd_hda_update_power_acct(codec);
3870 trace_hda_power_down(codec);
95e99fda 3871 codec->power_on = 0;
a221e287 3872 codec->power_transition = 0;
a2f6309e 3873 codec->power_jiffies = jiffies;
a2d96e77 3874 spin_unlock(&codec->power_lock);
989c3187 3875 codec->in_pm = 0;
08fa20ae 3876 return state;
54d17403
TI
3877}
3878
c370dd6e
TI
3879/* mark all entries of cmd and amp caches dirty */
3880static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3881{
3882 int i;
3883 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3884 struct hda_cache_head *cmd;
3885 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3886 cmd->dirty = 1;
3887 }
3888 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3889 struct hda_amp_info *amp;
f038fcac 3890 amp = snd_array_elem(&codec->amp_cache.buf, i);
c370dd6e
TI
3891 amp->head.dirty = 1;
3892 }
3893}
3894
cb53c626
TI
3895/*
3896 * kick up codec; used both from PM and power-save
3897 */
3898static void hda_call_codec_resume(struct hda_codec *codec)
3899{
989c3187
TI
3900 codec->in_pm = 1;
3901
c370dd6e
TI
3902 hda_mark_cmd_cache_dirty(codec);
3903
7f30830b
TI
3904 /* set as if powered on for avoiding re-entering the resume
3905 * in the resume / power-save sequence
3906 */
3907 hda_keep_power_on(codec);
d819387e 3908 hda_set_power_state(codec, AC_PWRST_D0);
ac0547dc 3909 restore_shutup_pins(codec);
11aeff08 3910 hda_exec_init_verbs(codec);
31614bb8 3911 snd_hda_jack_set_dirty_all(codec);
cb53c626
TI
3912 if (codec->patch_ops.resume)
3913 codec->patch_ops.resume(codec);
3914 else {
9d99f312
TI
3915 if (codec->patch_ops.init)
3916 codec->patch_ops.init(codec);
cb53c626
TI
3917 snd_hda_codec_resume_amp(codec);
3918 snd_hda_codec_resume_cache(codec);
3919 }
26a6cb6c
DH
3920
3921 if (codec->jackpoll_interval)
3922 hda_jackpoll_work(&codec->jackpoll_work.work);
31614bb8 3923 else
26a6cb6c 3924 snd_hda_jack_report_sync(codec);
989c3187
TI
3925
3926 codec->in_pm = 0;
7f30830b 3927 snd_hda_power_down(codec); /* flag down before returning */
cb53c626 3928}
2a43952a 3929#endif /* CONFIG_PM */
cb53c626 3930
54d17403 3931
1da177e4
LT
3932/**
3933 * snd_hda_build_controls - build mixer controls
3934 * @bus: the BUS
3935 *
3936 * Creates mixer controls for each codec included in the bus.
3937 *
3938 * Returns 0 if successful, otherwise a negative error code.
3939 */
6a0f56a7 3940int snd_hda_build_controls(struct hda_bus *bus)
1da177e4 3941{
0ba21762 3942 struct hda_codec *codec;
1da177e4 3943
0ba21762 3944 list_for_each_entry(codec, &bus->codec_list, list) {
6c1f45ea 3945 int err = snd_hda_codec_build_controls(codec);
f93d461b 3946 if (err < 0) {
28d1a85e 3947 printk(KERN_ERR "hda_codec: cannot build controls "
28aedaf7 3948 "for #%d (error %d)\n", codec->addr, err);
f93d461b
TI
3949 err = snd_hda_codec_reset(codec);
3950 if (err < 0) {
3951 printk(KERN_ERR
3952 "hda_codec: cannot revert codec\n");
3953 return err;
3954 }
3955 }
1da177e4 3956 }
6c1f45ea
TI
3957 return 0;
3958}
ff7a3267 3959EXPORT_SYMBOL_HDA(snd_hda_build_controls);
cb53c626 3960
9c9a5175
TI
3961/*
3962 * add standard channel maps if not specified
3963 */
3964static int add_std_chmaps(struct hda_codec *codec)
3965{
3966 int i, str, err;
3967
3968 for (i = 0; i < codec->num_pcms; i++) {
3969 for (str = 0; str < 2; str++) {
3970 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3971 struct hda_pcm_stream *hinfo =
3972 &codec->pcm_info[i].stream[str];
3973 struct snd_pcm_chmap *chmap;
ee81abb6 3974 const struct snd_pcm_chmap_elem *elem;
9c9a5175
TI
3975
3976 if (codec->pcm_info[i].own_chmap)
3977 continue;
3978 if (!pcm || !hinfo->substreams)
3979 continue;
ee81abb6
TI
3980 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3981 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
9c9a5175
TI
3982 hinfo->channels_max,
3983 0, &chmap);
3984 if (err < 0)
3985 return err;
3986 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3987 }
3988 }
3989 return 0;
3990}
3991
ee81abb6
TI
3992/* default channel maps for 2.1 speakers;
3993 * since HD-audio supports only stereo, odd number channels are omitted
3994 */
3995const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3996 { .channels = 2,
3997 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3998 { .channels = 4,
3999 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4000 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4001 { }
4002};
4003EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4004
6c1f45ea
TI
4005int snd_hda_codec_build_controls(struct hda_codec *codec)
4006{
4007 int err = 0;
11aeff08 4008 hda_exec_init_verbs(codec);
6c1f45ea
TI
4009 /* continue to initialize... */
4010 if (codec->patch_ops.init)
4011 err = codec->patch_ops.init(codec);
4012 if (!err && codec->patch_ops.build_controls)
4013 err = codec->patch_ops.build_controls(codec);
6c1f45ea
TI
4014 if (err < 0)
4015 return err;
9c9a5175
TI
4016
4017 /* we create chmaps here instead of build_pcms */
4018 err = add_std_chmaps(codec);
4019 if (err < 0)
4020 return err;
4021
26a6cb6c
DH
4022 if (codec->jackpoll_interval)
4023 hda_jackpoll_work(&codec->jackpoll_work.work);
4024 else
4025 snd_hda_jack_report_sync(codec); /* call at the last init point */
b9c590bb 4026 sync_power_up_states(codec);
1da177e4
LT
4027 return 0;
4028}
4029
1da177e4
LT
4030/*
4031 * stream formats
4032 */
befdf316
TI
4033struct hda_rate_tbl {
4034 unsigned int hz;
4035 unsigned int alsa_bits;
4036 unsigned int hda_fmt;
4037};
4038
92f10b3f
TI
4039/* rate = base * mult / div */
4040#define HDA_RATE(base, mult, div) \
4041 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4042 (((div) - 1) << AC_FMT_DIV_SHIFT))
4043
befdf316 4044static struct hda_rate_tbl rate_bits[] = {
1da177e4 4045 /* rate in Hz, ALSA rate bitmask, HDA format value */
9d8f53f2
NG
4046
4047 /* autodetected value used in snd_hda_query_supported_pcm */
92f10b3f
TI
4048 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4049 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4050 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4051 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4052 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4053 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4054 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4055 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4056 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4057 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4058 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
a961f9fe
TI
4059#define AC_PAR_PCM_RATE_BITS 11
4060 /* up to bits 10, 384kHZ isn't supported properly */
4061
4062 /* not autodetected value */
92f10b3f 4063 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
9d8f53f2 4064
befdf316 4065 { 0 } /* terminator */
1da177e4
LT
4066};
4067
4068/**
4069 * snd_hda_calc_stream_format - calculate format bitset
4070 * @rate: the sample rate
4071 * @channels: the number of channels
4072 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4073 * @maxbps: the max. bps
4074 *
4075 * Calculate the format bitset from the given rate, channels and th PCM format.
4076 *
4077 * Return zero if invalid.
4078 */
4079unsigned int snd_hda_calc_stream_format(unsigned int rate,
4080 unsigned int channels,
4081 unsigned int format,
32c168c8
AH
4082 unsigned int maxbps,
4083 unsigned short spdif_ctls)
1da177e4
LT
4084{
4085 int i;
4086 unsigned int val = 0;
4087
befdf316
TI
4088 for (i = 0; rate_bits[i].hz; i++)
4089 if (rate_bits[i].hz == rate) {
4090 val = rate_bits[i].hda_fmt;
1da177e4
LT
4091 break;
4092 }
0ba21762 4093 if (!rate_bits[i].hz) {
1da177e4
LT
4094 snd_printdd("invalid rate %d\n", rate);
4095 return 0;
4096 }
4097
4098 if (channels == 0 || channels > 8) {
4099 snd_printdd("invalid channels %d\n", channels);
4100 return 0;
4101 }
4102 val |= channels - 1;
4103
4104 switch (snd_pcm_format_width(format)) {
28aedaf7 4105 case 8:
92f10b3f 4106 val |= AC_FMT_BITS_8;
28aedaf7
NL
4107 break;
4108 case 16:
92f10b3f 4109 val |= AC_FMT_BITS_16;
28aedaf7 4110 break;
1da177e4
LT
4111 case 20:
4112 case 24:
4113 case 32:
b0bb3aa6 4114 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
92f10b3f 4115 val |= AC_FMT_BITS_32;
1da177e4 4116 else if (maxbps >= 24)
92f10b3f 4117 val |= AC_FMT_BITS_24;
1da177e4 4118 else
92f10b3f 4119 val |= AC_FMT_BITS_20;
1da177e4
LT
4120 break;
4121 default:
0ba21762
TI
4122 snd_printdd("invalid format width %d\n",
4123 snd_pcm_format_width(format));
1da177e4
LT
4124 return 0;
4125 }
4126
32c168c8 4127 if (spdif_ctls & AC_DIG1_NONAUDIO)
92f10b3f 4128 val |= AC_FMT_TYPE_NON_PCM;
32c168c8 4129
1da177e4
LT
4130 return val;
4131}
ff7a3267 4132EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
1da177e4 4133
c3b6bcc2
TI
4134static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4135 int dir)
92c7c8a7
TI
4136{
4137 unsigned int val = 0;
4138 if (nid != codec->afg &&
4139 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4140 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4141 if (!val || val == -1)
4142 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4143 if (!val || val == -1)
4144 return 0;
4145 return val;
4146}
4147
4148static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4149{
c3b6bcc2 4150 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
92c7c8a7
TI
4151 get_pcm_param);
4152}
4153
c3b6bcc2
TI
4154static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4155 int dir)
92c7c8a7
TI
4156{
4157 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4158 if (!streams || streams == -1)
4159 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4160 if (!streams || streams == -1)
4161 return 0;
4162 return streams;
4163}
4164
4165static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4166{
c3b6bcc2 4167 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
92c7c8a7
TI
4168 get_stream_param);
4169}
4170
1da177e4
LT
4171/**
4172 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4173 * @codec: the HDA codec
4174 * @nid: NID to query
4175 * @ratesp: the pointer to store the detected rate bitflags
4176 * @formatsp: the pointer to store the detected formats
4177 * @bpsp: the pointer to store the detected format widths
4178 *
4179 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4180 * or @bsps argument is ignored.
4181 *
4182 * Returns 0 if successful, otherwise a negative error code.
4183 */
384a48d7 4184int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1da177e4
LT
4185 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4186{
ee504710 4187 unsigned int i, val, wcaps;
1da177e4 4188
ee504710 4189 wcaps = get_wcaps(codec, nid);
92c7c8a7 4190 val = query_pcm_param(codec, nid);
1da177e4
LT
4191
4192 if (ratesp) {
4193 u32 rates = 0;
a961f9fe 4194 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1da177e4 4195 if (val & (1 << i))
befdf316 4196 rates |= rate_bits[i].alsa_bits;
1da177e4 4197 }
ee504710
JK
4198 if (rates == 0) {
4199 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4200 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4201 nid, val,
4202 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4203 return -EIO;
4204 }
1da177e4
LT
4205 *ratesp = rates;
4206 }
4207
4208 if (formatsp || bpsp) {
4209 u64 formats = 0;
ee504710 4210 unsigned int streams, bps;
1da177e4 4211
92c7c8a7
TI
4212 streams = query_stream_param(codec, nid);
4213 if (!streams)
1da177e4 4214 return -EIO;
1da177e4
LT
4215
4216 bps = 0;
4217 if (streams & AC_SUPFMT_PCM) {
4218 if (val & AC_SUPPCM_BITS_8) {
4219 formats |= SNDRV_PCM_FMTBIT_U8;
4220 bps = 8;
4221 }
4222 if (val & AC_SUPPCM_BITS_16) {
4223 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4224 bps = 16;
4225 }
4226 if (wcaps & AC_WCAP_DIGITAL) {
4227 if (val & AC_SUPPCM_BITS_32)
4228 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4229 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4230 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4231 if (val & AC_SUPPCM_BITS_24)
4232 bps = 24;
4233 else if (val & AC_SUPPCM_BITS_20)
4234 bps = 20;
0ba21762
TI
4235 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4236 AC_SUPPCM_BITS_32)) {
1da177e4
LT
4237 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4238 if (val & AC_SUPPCM_BITS_32)
4239 bps = 32;
1da177e4
LT
4240 else if (val & AC_SUPPCM_BITS_24)
4241 bps = 24;
33ef7651
NG
4242 else if (val & AC_SUPPCM_BITS_20)
4243 bps = 20;
1da177e4
LT
4244 }
4245 }
8c7dd890 4246#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
b5025c50 4247 if (streams & AC_SUPFMT_FLOAT32) {
1da177e4 4248 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
b0bb3aa6
TI
4249 if (!bps)
4250 bps = 32;
b5025c50 4251 }
8c7dd890 4252#endif
b5025c50 4253 if (streams == AC_SUPFMT_AC3) {
0ba21762 4254 /* should be exclusive */
1da177e4
LT
4255 /* temporary hack: we have still no proper support
4256 * for the direct AC3 stream...
4257 */
4258 formats |= SNDRV_PCM_FMTBIT_U8;
4259 bps = 8;
4260 }
ee504710
JK
4261 if (formats == 0) {
4262 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4263 "(nid=0x%x, val=0x%x, ovrd=%i, "
4264 "streams=0x%x)\n",
4265 nid, val,
4266 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4267 streams);
4268 return -EIO;
4269 }
1da177e4
LT
4270 if (formatsp)
4271 *formatsp = formats;
4272 if (bpsp)
4273 *bpsp = bps;
4274 }
4275
4276 return 0;
4277}
384a48d7 4278EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
1da177e4
LT
4279
4280/**
d5191e50
TI
4281 * snd_hda_is_supported_format - Check the validity of the format
4282 * @codec: HD-audio codec
4283 * @nid: NID to check
4284 * @format: the HD-audio format value to check
4285 *
4286 * Check whether the given node supports the format value.
1da177e4
LT
4287 *
4288 * Returns 1 if supported, 0 if not.
4289 */
4290int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4291 unsigned int format)
4292{
4293 int i;
4294 unsigned int val = 0, rate, stream;
4295
92c7c8a7
TI
4296 val = query_pcm_param(codec, nid);
4297 if (!val)
4298 return 0;
1da177e4
LT
4299
4300 rate = format & 0xff00;
a961f9fe 4301 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
befdf316 4302 if (rate_bits[i].hda_fmt == rate) {
1da177e4
LT
4303 if (val & (1 << i))
4304 break;
4305 return 0;
4306 }
a961f9fe 4307 if (i >= AC_PAR_PCM_RATE_BITS)
1da177e4
LT
4308 return 0;
4309
92c7c8a7
TI
4310 stream = query_stream_param(codec, nid);
4311 if (!stream)
1da177e4
LT
4312 return 0;
4313
4314 if (stream & AC_SUPFMT_PCM) {
4315 switch (format & 0xf0) {
4316 case 0x00:
0ba21762 4317 if (!(val & AC_SUPPCM_BITS_8))
1da177e4
LT
4318 return 0;
4319 break;
4320 case 0x10:
0ba21762 4321 if (!(val & AC_SUPPCM_BITS_16))
1da177e4
LT
4322 return 0;
4323 break;
4324 case 0x20:
0ba21762 4325 if (!(val & AC_SUPPCM_BITS_20))
1da177e4
LT
4326 return 0;
4327 break;
4328 case 0x30:
0ba21762 4329 if (!(val & AC_SUPPCM_BITS_24))
1da177e4
LT
4330 return 0;
4331 break;
4332 case 0x40:
0ba21762 4333 if (!(val & AC_SUPPCM_BITS_32))
1da177e4
LT
4334 return 0;
4335 break;
4336 default:
4337 return 0;
4338 }
4339 } else {
4340 /* FIXME: check for float32 and AC3? */
4341 }
4342
4343 return 1;
4344}
ff7a3267 4345EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
1da177e4
LT
4346
4347/*
4348 * PCM stuff
4349 */
4350static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4351 struct hda_codec *codec,
c8b6bf9b 4352 struct snd_pcm_substream *substream)
1da177e4
LT
4353{
4354 return 0;
4355}
4356
4357static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4358 struct hda_codec *codec,
4359 unsigned int stream_tag,
4360 unsigned int format,
c8b6bf9b 4361 struct snd_pcm_substream *substream)
1da177e4
LT
4362{
4363 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4364 return 0;
4365}
4366
4367static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4368 struct hda_codec *codec,
c8b6bf9b 4369 struct snd_pcm_substream *substream)
1da177e4 4370{
888afa15 4371 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1da177e4
LT
4372 return 0;
4373}
4374
6c1f45ea
TI
4375static int set_pcm_default_values(struct hda_codec *codec,
4376 struct hda_pcm_stream *info)
1da177e4 4377{
ee504710
JK
4378 int err;
4379
0ba21762
TI
4380 /* query support PCM information from the given NID */
4381 if (info->nid && (!info->rates || !info->formats)) {
ee504710 4382 err = snd_hda_query_supported_pcm(codec, info->nid,
0ba21762
TI
4383 info->rates ? NULL : &info->rates,
4384 info->formats ? NULL : &info->formats,
4385 info->maxbps ? NULL : &info->maxbps);
ee504710
JK
4386 if (err < 0)
4387 return err;
1da177e4
LT
4388 }
4389 if (info->ops.open == NULL)
4390 info->ops.open = hda_pcm_default_open_close;
4391 if (info->ops.close == NULL)
4392 info->ops.close = hda_pcm_default_open_close;
4393 if (info->ops.prepare == NULL) {
da3cec35
TI
4394 if (snd_BUG_ON(!info->nid))
4395 return -EINVAL;
1da177e4
LT
4396 info->ops.prepare = hda_pcm_default_prepare;
4397 }
1da177e4 4398 if (info->ops.cleanup == NULL) {
da3cec35
TI
4399 if (snd_BUG_ON(!info->nid))
4400 return -EINVAL;
1da177e4
LT
4401 info->ops.cleanup = hda_pcm_default_cleanup;
4402 }
4403 return 0;
4404}
4405
eb541337
TI
4406/*
4407 * codec prepare/cleanup entries
4408 */
4409int snd_hda_codec_prepare(struct hda_codec *codec,
4410 struct hda_pcm_stream *hinfo,
4411 unsigned int stream,
4412 unsigned int format,
4413 struct snd_pcm_substream *substream)
4414{
4415 int ret;
3f50ac6a 4416 mutex_lock(&codec->bus->prepare_mutex);
eb541337
TI
4417 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4418 if (ret >= 0)
4419 purify_inactive_streams(codec);
3f50ac6a 4420 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
4421 return ret;
4422}
4423EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4424
4425void snd_hda_codec_cleanup(struct hda_codec *codec,
4426 struct hda_pcm_stream *hinfo,
4427 struct snd_pcm_substream *substream)
4428{
3f50ac6a 4429 mutex_lock(&codec->bus->prepare_mutex);
eb541337 4430 hinfo->ops.cleanup(hinfo, codec, substream);
3f50ac6a 4431 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
4432}
4433EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4434
d5191e50 4435/* global */
e3303235
JK
4436const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4437 "Audio", "SPDIF", "HDMI", "Modem"
4438};
4439
529bd6c4
TI
4440/*
4441 * get the empty PCM device number to assign
c8936222
TI
4442 *
4443 * note the max device number is limited by HDA_MAX_PCMS, currently 10
529bd6c4
TI
4444 */
4445static int get_empty_pcm_device(struct hda_bus *bus, int type)
4446{
f5d6def5
WF
4447 /* audio device indices; not linear to keep compatibility */
4448 static int audio_idx[HDA_PCM_NTYPES][5] = {
4449 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4450 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
92608bad 4451 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
f5d6def5 4452 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
529bd6c4 4453 };
f5d6def5
WF
4454 int i;
4455
4456 if (type >= HDA_PCM_NTYPES) {
529bd6c4
TI
4457 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4458 return -EINVAL;
4459 }
f5d6def5
WF
4460
4461 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4462 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4463 return audio_idx[type][i];
4464
01b65bfb
TI
4465 /* non-fixed slots starting from 10 */
4466 for (i = 10; i < 32; i++) {
4467 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4468 return i;
4469 }
4470
28aedaf7
NL
4471 snd_printk(KERN_WARNING "Too many %s devices\n",
4472 snd_hda_pcm_type_name[type]);
f5d6def5 4473 return -EAGAIN;
529bd6c4
TI
4474}
4475
176d5335
TI
4476/*
4477 * attach a new PCM stream
4478 */
529bd6c4 4479static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
176d5335 4480{
33fa35ed 4481 struct hda_bus *bus = codec->bus;
176d5335
TI
4482 struct hda_pcm_stream *info;
4483 int stream, err;
4484
b91f080f 4485 if (snd_BUG_ON(!pcm->name))
176d5335
TI
4486 return -EINVAL;
4487 for (stream = 0; stream < 2; stream++) {
4488 info = &pcm->stream[stream];
4489 if (info->substreams) {
4490 err = set_pcm_default_values(codec, info);
4491 if (err < 0)
4492 return err;
4493 }
4494 }
33fa35ed 4495 return bus->ops.attach_pcm(bus, codec, pcm);
176d5335
TI
4496}
4497
529bd6c4
TI
4498/* assign all PCMs of the given codec */
4499int snd_hda_codec_build_pcms(struct hda_codec *codec)
4500{
4501 unsigned int pcm;
4502 int err;
4503
4504 if (!codec->num_pcms) {
4505 if (!codec->patch_ops.build_pcms)
4506 return 0;
4507 err = codec->patch_ops.build_pcms(codec);
6e655bf2
TI
4508 if (err < 0) {
4509 printk(KERN_ERR "hda_codec: cannot build PCMs"
28aedaf7 4510 "for #%d (error %d)\n", codec->addr, err);
6e655bf2
TI
4511 err = snd_hda_codec_reset(codec);
4512 if (err < 0) {
4513 printk(KERN_ERR
4514 "hda_codec: cannot revert codec\n");
4515 return err;
4516 }
4517 }
529bd6c4
TI
4518 }
4519 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4520 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4521 int dev;
4522
4523 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
41b5b01a 4524 continue; /* no substreams assigned */
529bd6c4
TI
4525
4526 if (!cpcm->pcm) {
4527 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4528 if (dev < 0)
6e655bf2 4529 continue; /* no fatal error */
529bd6c4
TI
4530 cpcm->device = dev;
4531 err = snd_hda_attach_pcm(codec, cpcm);
6e655bf2
TI
4532 if (err < 0) {
4533 printk(KERN_ERR "hda_codec: cannot attach "
4534 "PCM stream %d for codec #%d\n",
4535 dev, codec->addr);
4536 continue; /* no fatal error */
4537 }
529bd6c4
TI
4538 }
4539 }
4540 return 0;
4541}
4542
1da177e4
LT
4543/**
4544 * snd_hda_build_pcms - build PCM information
4545 * @bus: the BUS
4546 *
4547 * Create PCM information for each codec included in the bus.
4548 *
4549 * The build_pcms codec patch is requested to set up codec->num_pcms and
4550 * codec->pcm_info properly. The array is referred by the top-level driver
4551 * to create its PCM instances.
4552 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4553 * callback.
4554 *
4555 * At least, substreams, channels_min and channels_max must be filled for
4556 * each stream. substreams = 0 indicates that the stream doesn't exist.
4557 * When rates and/or formats are zero, the supported values are queried
4558 * from the given nid. The nid is used also by the default ops.prepare
4559 * and ops.cleanup callbacks.
4560 *
4561 * The driver needs to call ops.open in its open callback. Similarly,
4562 * ops.close is supposed to be called in the close callback.
4563 * ops.prepare should be called in the prepare or hw_params callback
4564 * with the proper parameters for set up.
4565 * ops.cleanup should be called in hw_free for clean up of streams.
4566 *
25985edc 4567 * This function returns 0 if successful, or a negative error code.
1da177e4 4568 */
5cb543db 4569int snd_hda_build_pcms(struct hda_bus *bus)
1da177e4 4570{
0ba21762 4571 struct hda_codec *codec;
1da177e4 4572
0ba21762 4573 list_for_each_entry(codec, &bus->codec_list, list) {
529bd6c4
TI
4574 int err = snd_hda_codec_build_pcms(codec);
4575 if (err < 0)
4576 return err;
1da177e4
LT
4577 }
4578 return 0;
4579}
ff7a3267 4580EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
1da177e4 4581
1da177e4
LT
4582/**
4583 * snd_hda_check_board_config - compare the current codec with the config table
4584 * @codec: the HDA codec
f5fcc13c
TI
4585 * @num_configs: number of config enums
4586 * @models: array of model name strings
1da177e4
LT
4587 * @tbl: configuration table, terminated by null entries
4588 *
4589 * Compares the modelname or PCI subsystem id of the current codec with the
4590 * given configuration table. If a matching entry is found, returns its
4591 * config value (supposed to be 0 or positive).
4592 *
4593 * If no entries are matching, the function returns a negative value.
4594 */
12f288bf 4595int snd_hda_check_board_config(struct hda_codec *codec,
ea734963 4596 int num_configs, const char * const *models,
12f288bf 4597 const struct snd_pci_quirk *tbl)
1da177e4 4598{
f44ac837 4599 if (codec->modelname && models) {
f5fcc13c
TI
4600 int i;
4601 for (i = 0; i < num_configs; i++) {
4602 if (models[i] &&
f44ac837 4603 !strcmp(codec->modelname, models[i])) {
f5fcc13c
TI
4604 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4605 "selected\n", models[i]);
4606 return i;
1da177e4
LT
4607 }
4608 }
4609 }
4610
f5fcc13c
TI
4611 if (!codec->bus->pci || !tbl)
4612 return -1;
4613
4614 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4615 if (!tbl)
4616 return -1;
4617 if (tbl->value >= 0 && tbl->value < num_configs) {
62cf872a 4618#ifdef CONFIG_SND_DEBUG_VERBOSE
f5fcc13c
TI
4619 char tmp[10];
4620 const char *model = NULL;
4621 if (models)
4622 model = models[tbl->value];
4623 if (!model) {
4624 sprintf(tmp, "#%d", tbl->value);
4625 model = tmp;
1da177e4 4626 }
f5fcc13c
TI
4627 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4628 "for config %x:%x (%s)\n",
4629 model, tbl->subvendor, tbl->subdevice,
4630 (tbl->name ? tbl->name : "Unknown device"));
4631#endif
4632 return tbl->value;
1da177e4
LT
4633 }
4634 return -1;
4635}
ff7a3267 4636EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
1da177e4 4637
2eda3445
MCC
4638/**
4639 * snd_hda_check_board_codec_sid_config - compare the current codec
28aedaf7
NL
4640 subsystem ID with the
4641 config table
2eda3445
MCC
4642
4643 This is important for Gateway notebooks with SB450 HDA Audio
4644 where the vendor ID of the PCI device is:
4645 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4646 and the vendor/subvendor are found only at the codec.
4647
4648 * @codec: the HDA codec
4649 * @num_configs: number of config enums
4650 * @models: array of model name strings
4651 * @tbl: configuration table, terminated by null entries
4652 *
4653 * Compares the modelname or PCI subsystem id of the current codec with the
4654 * given configuration table. If a matching entry is found, returns its
4655 * config value (supposed to be 0 or positive).
4656 *
4657 * If no entries are matching, the function returns a negative value.
4658 */
4659int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
ea734963 4660 int num_configs, const char * const *models,
2eda3445
MCC
4661 const struct snd_pci_quirk *tbl)
4662{
4663 const struct snd_pci_quirk *q;
4664
4665 /* Search for codec ID */
4666 for (q = tbl; q->subvendor; q++) {
e2301a4d
TI
4667 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4668 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4669 if ((codec->subsystem_id & mask) == id)
2eda3445
MCC
4670 break;
4671 }
4672
4673 if (!q->subvendor)
4674 return -1;
4675
4676 tbl = q;
4677
4678 if (tbl->value >= 0 && tbl->value < num_configs) {
d94ff6b7 4679#ifdef CONFIG_SND_DEBUG_VERBOSE
2eda3445
MCC
4680 char tmp[10];
4681 const char *model = NULL;
4682 if (models)
4683 model = models[tbl->value];
4684 if (!model) {
4685 sprintf(tmp, "#%d", tbl->value);
4686 model = tmp;
4687 }
4688 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4689 "for config %x:%x (%s)\n",
4690 model, tbl->subvendor, tbl->subdevice,
4691 (tbl->name ? tbl->name : "Unknown device"));
4692#endif
4693 return tbl->value;
4694 }
4695 return -1;
4696}
4697EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4698
1da177e4
LT
4699/**
4700 * snd_hda_add_new_ctls - create controls from the array
4701 * @codec: the HDA codec
c8b6bf9b 4702 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
4703 *
4704 * This helper function creates and add new controls in the given array.
4705 * The array must be terminated with an empty entry as terminator.
4706 *
4707 * Returns 0 if successful, or a negative error code.
4708 */
031024ee
TI
4709int snd_hda_add_new_ctls(struct hda_codec *codec,
4710 const struct snd_kcontrol_new *knew)
1da177e4 4711{
4d02d1b6 4712 int err;
1da177e4
LT
4713
4714 for (; knew->name; knew++) {
54d17403 4715 struct snd_kcontrol *kctl;
1afe206a 4716 int addr = 0, idx = 0;
5b0cb1d8
JK
4717 if (knew->iface == -1) /* skip this codec private value */
4718 continue;
1afe206a 4719 for (;;) {
54d17403 4720 kctl = snd_ctl_new1(knew, codec);
0ba21762 4721 if (!kctl)
54d17403 4722 return -ENOMEM;
1afe206a
TI
4723 if (addr > 0)
4724 kctl->id.device = addr;
4725 if (idx > 0)
4726 kctl->id.index = idx;
3911a4c1 4727 err = snd_hda_ctl_add(codec, 0, kctl);
1afe206a
TI
4728 if (!err)
4729 break;
4730 /* try first with another device index corresponding to
4731 * the codec addr; if it still fails (or it's the
4732 * primary codec), then try another control index
4733 */
4734 if (!addr && codec->addr)
4735 addr = codec->addr;
4736 else if (!idx && !knew->index) {
4737 idx = find_empty_mixer_ctl_idx(codec,
dcda5806 4738 knew->name, 0);
1afe206a
TI
4739 if (idx <= 0)
4740 return err;
4741 } else
54d17403
TI
4742 return err;
4743 }
1da177e4
LT
4744 }
4745 return 0;
4746}
ff7a3267 4747EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
1da177e4 4748
83012a7c 4749#ifdef CONFIG_PM
cb53c626
TI
4750static void hda_power_work(struct work_struct *work)
4751{
4752 struct hda_codec *codec =
4753 container_of(work, struct hda_codec, power_work.work);
33fa35ed 4754 struct hda_bus *bus = codec->bus;
08fa20ae 4755 unsigned int state;
cb53c626 4756
5536c6d6 4757 spin_lock(&codec->power_lock);
a2d96e77
TI
4758 if (codec->power_transition > 0) { /* during power-up sequence? */
4759 spin_unlock(&codec->power_lock);
4760 return;
4761 }
2e492462
ML
4762 if (!codec->power_on || codec->power_count) {
4763 codec->power_transition = 0;
5536c6d6 4764 spin_unlock(&codec->power_lock);
cb53c626 4765 return;
2e492462 4766 }
5536c6d6
TI
4767 spin_unlock(&codec->power_lock);
4768
d17344b3 4769 state = hda_call_codec_suspend(codec, true);
08fa20ae
TI
4770 codec->pm_down_notified = 0;
4771 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4772 codec->pm_down_notified = 1;
68467f51 4773 hda_call_pm_notify(bus, false);
08fa20ae 4774 }
cb53c626
TI
4775}
4776
4777static void hda_keep_power_on(struct hda_codec *codec)
4778{
5536c6d6 4779 spin_lock(&codec->power_lock);
cb53c626
TI
4780 codec->power_count++;
4781 codec->power_on = 1;
a2f6309e 4782 codec->power_jiffies = jiffies;
5536c6d6 4783 spin_unlock(&codec->power_lock);
a2f6309e
TI
4784}
4785
d5191e50 4786/* update the power on/off account with the current jiffies */
a2f6309e
TI
4787void snd_hda_update_power_acct(struct hda_codec *codec)
4788{
4789 unsigned long delta = jiffies - codec->power_jiffies;
4790 if (codec->power_on)
4791 codec->power_on_acct += delta;
4792 else
4793 codec->power_off_acct += delta;
4794 codec->power_jiffies += delta;
cb53c626
TI
4795}
4796
b4a91cf0
DR
4797/* Transition to powered up, if wait_power_down then wait for a pending
4798 * transition to D3 to complete. A pending D3 transition is indicated
4799 * with power_transition == -1. */
c376e2c7 4800/* call this with codec->power_lock held! */
b4a91cf0 4801static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
cb53c626 4802{
33fa35ed
TI
4803 struct hda_bus *bus = codec->bus;
4804
b4a91cf0
DR
4805 /* Return if power_on or transitioning to power_on, unless currently
4806 * powering down. */
4807 if ((codec->power_on || codec->power_transition > 0) &&
c376e2c7 4808 !(wait_power_down && codec->power_transition < 0))
cb53c626 4809 return;
a2d96e77 4810 spin_unlock(&codec->power_lock);
cb53c626 4811
a2d96e77
TI
4812 cancel_delayed_work_sync(&codec->power_work);
4813
4814 spin_lock(&codec->power_lock);
b43d2247
DR
4815 /* If the power down delayed work was cancelled above before starting,
4816 * then there is no need to go through power up here.
4817 */
4818 if (codec->power_on) {
535b6c51
TI
4819 if (codec->power_transition < 0)
4820 codec->power_transition = 0;
b43d2247
DR
4821 return;
4822 }
c376e2c7 4823
d66fee5d 4824 trace_hda_power_up(codec);
a2f6309e 4825 snd_hda_update_power_acct(codec);
cb53c626 4826 codec->power_on = 1;
a2f6309e 4827 codec->power_jiffies = jiffies;
7f30830b 4828 codec->power_transition = 1; /* avoid reentrance */
5536c6d6
TI
4829 spin_unlock(&codec->power_lock);
4830
08fa20ae
TI
4831 if (codec->pm_down_notified) {
4832 codec->pm_down_notified = 0;
68467f51 4833 hda_call_pm_notify(bus, true);
08fa20ae
TI
4834 }
4835
cb53c626 4836 hda_call_codec_resume(codec);
5536c6d6
TI
4837
4838 spin_lock(&codec->power_lock);
a221e287 4839 codec->power_transition = 0;
cb53c626 4840}
b4a91cf0 4841
c376e2c7
TI
4842#define power_save(codec) \
4843 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
1289e9e8 4844
c376e2c7
TI
4845/* Transition to powered down */
4846static void __snd_hda_power_down(struct hda_codec *codec)
b4a91cf0 4847{
c376e2c7
TI
4848 if (!codec->power_on || codec->power_count || codec->power_transition)
4849 return;
b4a91cf0 4850
c376e2c7
TI
4851 if (power_save(codec)) {
4852 codec->power_transition = -1; /* avoid reentrance */
4853 queue_delayed_work(codec->bus->workq, &codec->power_work,
4854 msecs_to_jiffies(power_save(codec) * 1000));
4855 }
4856}
cb53c626 4857
d5191e50 4858/**
c376e2c7 4859 * snd_hda_power_save - Power-up/down/sync the codec
d5191e50 4860 * @codec: HD-audio codec
c376e2c7 4861 * @delta: the counter delta to change
d5191e50 4862 *
c376e2c7
TI
4863 * Change the power-up counter via @delta, and power up or down the hardware
4864 * appropriately. For the power-down, queue to the delayed action.
4865 * Passing zero to @delta means to synchronize the power state.
28aedaf7 4866 */
c376e2c7 4867void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
cb53c626 4868{
5536c6d6 4869 spin_lock(&codec->power_lock);
c376e2c7 4870 codec->power_count += delta;
b244d335 4871 trace_hda_power_count(codec);
c376e2c7
TI
4872 if (delta > 0)
4873 __snd_hda_power_up(codec, d3wait);
4874 else
4875 __snd_hda_power_down(codec);
5536c6d6 4876 spin_unlock(&codec->power_lock);
cb53c626 4877}
c376e2c7 4878EXPORT_SYMBOL_HDA(snd_hda_power_save);
cb53c626 4879
d5191e50
TI
4880/**
4881 * snd_hda_check_amp_list_power - Check the amp list and update the power
4882 * @codec: HD-audio codec
4883 * @check: the object containing an AMP list and the status
4884 * @nid: NID to check / update
4885 *
4886 * Check whether the given NID is in the amp list. If it's in the list,
4887 * check the current AMP status, and update the the power-status according
4888 * to the mute status.
4889 *
4890 * This function is supposed to be set or called from the check_power_status
4891 * patch ops.
28aedaf7 4892 */
cb53c626
TI
4893int snd_hda_check_amp_list_power(struct hda_codec *codec,
4894 struct hda_loopback_check *check,
4895 hda_nid_t nid)
4896{
031024ee 4897 const struct hda_amp_list *p;
cb53c626
TI
4898 int ch, v;
4899
4900 if (!check->amplist)
4901 return 0;
4902 for (p = check->amplist; p->nid; p++) {
4903 if (p->nid == nid)
4904 break;
4905 }
4906 if (!p->nid)
4907 return 0; /* nothing changed */
4908
4909 for (p = check->amplist; p->nid; p++) {
4910 for (ch = 0; ch < 2; ch++) {
4911 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4912 p->idx);
4913 if (!(v & HDA_AMP_MUTE) && v > 0) {
4914 if (!check->power_on) {
4915 check->power_on = 1;
4916 snd_hda_power_up(codec);
4917 }
4918 return 1;
4919 }
4920 }
4921 }
4922 if (check->power_on) {
4923 check->power_on = 0;
4924 snd_hda_power_down(codec);
4925 }
4926 return 0;
4927}
ff7a3267 4928EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
cb53c626 4929#endif
1da177e4 4930
c8b6bf9b 4931/*
d2a6d7dc
TI
4932 * Channel mode helper
4933 */
d5191e50
TI
4934
4935/**
4936 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4937 */
0ba21762
TI
4938int snd_hda_ch_mode_info(struct hda_codec *codec,
4939 struct snd_ctl_elem_info *uinfo,
4940 const struct hda_channel_mode *chmode,
4941 int num_chmodes)
d2a6d7dc
TI
4942{
4943 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4944 uinfo->count = 1;
4945 uinfo->value.enumerated.items = num_chmodes;
4946 if (uinfo->value.enumerated.item >= num_chmodes)
4947 uinfo->value.enumerated.item = num_chmodes - 1;
4948 sprintf(uinfo->value.enumerated.name, "%dch",
4949 chmode[uinfo->value.enumerated.item].channels);
4950 return 0;
4951}
ff7a3267 4952EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
d2a6d7dc 4953
d5191e50
TI
4954/**
4955 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4956 */
0ba21762
TI
4957int snd_hda_ch_mode_get(struct hda_codec *codec,
4958 struct snd_ctl_elem_value *ucontrol,
4959 const struct hda_channel_mode *chmode,
4960 int num_chmodes,
d2a6d7dc
TI
4961 int max_channels)
4962{
4963 int i;
4964
4965 for (i = 0; i < num_chmodes; i++) {
4966 if (max_channels == chmode[i].channels) {
4967 ucontrol->value.enumerated.item[0] = i;
4968 break;
4969 }
4970 }
4971 return 0;
4972}
ff7a3267 4973EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
d2a6d7dc 4974
d5191e50
TI
4975/**
4976 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4977 */
0ba21762
TI
4978int snd_hda_ch_mode_put(struct hda_codec *codec,
4979 struct snd_ctl_elem_value *ucontrol,
4980 const struct hda_channel_mode *chmode,
4981 int num_chmodes,
d2a6d7dc
TI
4982 int *max_channelsp)
4983{
4984 unsigned int mode;
4985
4986 mode = ucontrol->value.enumerated.item[0];
68ea7b2f
TI
4987 if (mode >= num_chmodes)
4988 return -EINVAL;
82beb8fd 4989 if (*max_channelsp == chmode[mode].channels)
d2a6d7dc
TI
4990 return 0;
4991 /* change the current channel setting */
4992 *max_channelsp = chmode[mode].channels;
4993 if (chmode[mode].sequence)
82beb8fd 4994 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
d2a6d7dc
TI
4995 return 1;
4996}
ff7a3267 4997EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
d2a6d7dc 4998
1da177e4
LT
4999/*
5000 * input MUX helper
5001 */
d5191e50
TI
5002
5003/**
5004 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5005 */
0ba21762
TI
5006int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5007 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
5008{
5009 unsigned int index;
5010
5011 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5012 uinfo->count = 1;
5013 uinfo->value.enumerated.items = imux->num_items;
5513b0c5
TI
5014 if (!imux->num_items)
5015 return 0;
1da177e4
LT
5016 index = uinfo->value.enumerated.item;
5017 if (index >= imux->num_items)
5018 index = imux->num_items - 1;
5019 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5020 return 0;
5021}
ff7a3267 5022EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
1da177e4 5023
d5191e50
TI
5024/**
5025 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5026 */
0ba21762
TI
5027int snd_hda_input_mux_put(struct hda_codec *codec,
5028 const struct hda_input_mux *imux,
5029 struct snd_ctl_elem_value *ucontrol,
5030 hda_nid_t nid,
1da177e4
LT
5031 unsigned int *cur_val)
5032{
5033 unsigned int idx;
5034
5513b0c5
TI
5035 if (!imux->num_items)
5036 return 0;
1da177e4
LT
5037 idx = ucontrol->value.enumerated.item[0];
5038 if (idx >= imux->num_items)
5039 idx = imux->num_items - 1;
82beb8fd 5040 if (*cur_val == idx)
1da177e4 5041 return 0;
82beb8fd
TI
5042 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5043 imux->items[idx].index);
1da177e4
LT
5044 *cur_val = idx;
5045 return 1;
5046}
ff7a3267 5047EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
1da177e4
LT
5048
5049
dda415d4
TI
5050/*
5051 * process kcontrol info callback of a simple string enum array
5052 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5053 */
5054int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5055 struct snd_ctl_elem_info *uinfo,
5056 int num_items, const char * const *texts)
5057{
5058 static const char * const texts_default[] = {
5059 "Disabled", "Enabled"
5060 };
5061
5062 if (!texts || !num_items) {
5063 num_items = 2;
5064 texts = texts_default;
5065 }
5066
5067 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5068 uinfo->count = 1;
5069 uinfo->value.enumerated.items = num_items;
5070 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5071 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5072 strcpy(uinfo->value.enumerated.name,
5073 texts[uinfo->value.enumerated.item]);
5074 return 0;
5075}
5076EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5077
1da177e4
LT
5078/*
5079 * Multi-channel / digital-out PCM helper functions
5080 */
5081
6b97eb45
TI
5082/* setup SPDIF output stream */
5083static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5084 unsigned int stream_tag, unsigned int format)
5085{
3bef1c37
LD
5086 struct hda_spdif_out *spdif;
5087 unsigned int curr_fmt;
5088 bool reset;
5089
5090 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5091 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5092 AC_VERB_GET_STREAM_FORMAT, 0);
5093 reset = codec->spdif_status_reset &&
5094 (spdif->ctls & AC_DIG1_ENABLE) &&
5095 curr_fmt != format;
5096
5097 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5098 updated */
5099 if (reset)
28aedaf7 5100 set_dig_out_convert(codec, nid,
7c935976 5101 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
2f72853c 5102 -1);
6b97eb45 5103 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2f72853c 5104 if (codec->slave_dig_outs) {
dda14410 5105 const hda_nid_t *d;
2f72853c
TI
5106 for (d = codec->slave_dig_outs; *d; d++)
5107 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5108 format);
5109 }
6b97eb45 5110 /* turn on again (if needed) */
3bef1c37 5111 if (reset)
2f72853c 5112 set_dig_out_convert(codec, nid,
7c935976 5113 spdif->ctls & 0xff, -1);
2f72853c 5114}
de51ca12 5115
2f72853c
TI
5116static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5117{
5118 snd_hda_codec_cleanup_stream(codec, nid);
5119 if (codec->slave_dig_outs) {
dda14410 5120 const hda_nid_t *d;
2f72853c
TI
5121 for (d = codec->slave_dig_outs; *d; d++)
5122 snd_hda_codec_cleanup_stream(codec, *d);
de51ca12 5123 }
6b97eb45
TI
5124}
5125
d5191e50
TI
5126/**
5127 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5128 * @bus: HD-audio bus
5129 */
fb8d1a34
TI
5130void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5131{
5132 struct hda_codec *codec;
5133
5134 if (!bus)
5135 return;
5136 list_for_each_entry(codec, &bus->codec_list, list) {
e581f3db
TI
5137 if (hda_codec_is_power_on(codec) &&
5138 codec->patch_ops.reboot_notify)
fb8d1a34
TI
5139 codec->patch_ops.reboot_notify(codec);
5140 }
5141}
8f217a22 5142EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
fb8d1a34 5143
d5191e50
TI
5144/**
5145 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
1da177e4 5146 */
0ba21762
TI
5147int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5148 struct hda_multi_out *mout)
1da177e4 5149{
62932df8 5150 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
5151 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5152 /* already opened as analog dup; reset it once */
2f72853c 5153 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4 5154 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 5155 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5156 return 0;
5157}
ff7a3267 5158EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
1da177e4 5159
d5191e50
TI
5160/**
5161 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5162 */
6b97eb45
TI
5163int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5164 struct hda_multi_out *mout,
5165 unsigned int stream_tag,
5166 unsigned int format,
5167 struct snd_pcm_substream *substream)
5168{
5169 mutex_lock(&codec->spdif_mutex);
5170 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5171 mutex_unlock(&codec->spdif_mutex);
5172 return 0;
5173}
ff7a3267 5174EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
6b97eb45 5175
d5191e50
TI
5176/**
5177 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5178 */
9411e21c
TI
5179int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5180 struct hda_multi_out *mout)
5181{
5182 mutex_lock(&codec->spdif_mutex);
5183 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5184 mutex_unlock(&codec->spdif_mutex);
5185 return 0;
5186}
5187EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5188
d5191e50
TI
5189/**
5190 * snd_hda_multi_out_dig_close - release the digital out stream
1da177e4 5191 */
0ba21762
TI
5192int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5193 struct hda_multi_out *mout)
1da177e4 5194{
62932df8 5195 mutex_lock(&codec->spdif_mutex);
1da177e4 5196 mout->dig_out_used = 0;
62932df8 5197 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5198 return 0;
5199}
ff7a3267 5200EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
1da177e4 5201
d5191e50
TI
5202/**
5203 * snd_hda_multi_out_analog_open - open analog outputs
5204 *
5205 * Open analog outputs and set up the hw-constraints.
5206 * If the digital outputs can be opened as slave, open the digital
5207 * outputs, too.
1da177e4 5208 */
0ba21762
TI
5209int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5210 struct hda_multi_out *mout,
9a08160b
TI
5211 struct snd_pcm_substream *substream,
5212 struct hda_pcm_stream *hinfo)
5213{
5214 struct snd_pcm_runtime *runtime = substream->runtime;
5215 runtime->hw.channels_max = mout->max_channels;
5216 if (mout->dig_out_nid) {
5217 if (!mout->analog_rates) {
5218 mout->analog_rates = hinfo->rates;
5219 mout->analog_formats = hinfo->formats;
5220 mout->analog_maxbps = hinfo->maxbps;
5221 } else {
5222 runtime->hw.rates = mout->analog_rates;
5223 runtime->hw.formats = mout->analog_formats;
5224 hinfo->maxbps = mout->analog_maxbps;
5225 }
5226 if (!mout->spdif_rates) {
5227 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5228 &mout->spdif_rates,
5229 &mout->spdif_formats,
5230 &mout->spdif_maxbps);
5231 }
5232 mutex_lock(&codec->spdif_mutex);
5233 if (mout->share_spdif) {
022b466f
TI
5234 if ((runtime->hw.rates & mout->spdif_rates) &&
5235 (runtime->hw.formats & mout->spdif_formats)) {
5236 runtime->hw.rates &= mout->spdif_rates;
5237 runtime->hw.formats &= mout->spdif_formats;
5238 if (mout->spdif_maxbps < hinfo->maxbps)
5239 hinfo->maxbps = mout->spdif_maxbps;
5240 } else {
5241 mout->share_spdif = 0;
5242 /* FIXME: need notify? */
5243 }
9a08160b 5244 }
eaa9985b 5245 mutex_unlock(&codec->spdif_mutex);
9a08160b 5246 }
1da177e4
LT
5247 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5248 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5249}
ff7a3267 5250EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
1da177e4 5251
d5191e50
TI
5252/**
5253 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5254 *
5255 * Set up the i/o for analog out.
5256 * When the digital out is available, copy the front out to digital out, too.
1da177e4 5257 */
0ba21762
TI
5258int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5259 struct hda_multi_out *mout,
1da177e4
LT
5260 unsigned int stream_tag,
5261 unsigned int format,
c8b6bf9b 5262 struct snd_pcm_substream *substream)
1da177e4 5263{
dda14410 5264 const hda_nid_t *nids = mout->dac_nids;
1da177e4 5265 int chs = substream->runtime->channels;
e3245cdd 5266 struct hda_spdif_out *spdif;
1da177e4
LT
5267 int i;
5268
62932df8 5269 mutex_lock(&codec->spdif_mutex);
e3245cdd 5270 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
9a08160b
TI
5271 if (mout->dig_out_nid && mout->share_spdif &&
5272 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1da177e4 5273 if (chs == 2 &&
0ba21762
TI
5274 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5275 format) &&
7c935976 5276 !(spdif->status & IEC958_AES0_NONAUDIO)) {
1da177e4 5277 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
5278 setup_dig_out_stream(codec, mout->dig_out_nid,
5279 stream_tag, format);
1da177e4
LT
5280 } else {
5281 mout->dig_out_used = 0;
2f72853c 5282 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
5283 }
5284 }
62932df8 5285 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5286
5287 /* front */
0ba21762
TI
5288 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5289 0, format);
d29240ce
TI
5290 if (!mout->no_share_stream &&
5291 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 5292 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
5293 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5294 0, format);
82bc955f 5295 /* extra outputs copied from front */
a06dbfc2
TI
5296 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5297 if (!mout->no_share_stream && mout->hp_out_nid[i])
5298 snd_hda_codec_setup_stream(codec,
5299 mout->hp_out_nid[i],
5300 stream_tag, 0, format);
82bc955f 5301 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
d29240ce 5302 if (!mout->no_share_stream && mout->extra_out_nid[i])
82bc955f
TI
5303 snd_hda_codec_setup_stream(codec,
5304 mout->extra_out_nid[i],
5305 stream_tag, 0, format);
5306
1da177e4
LT
5307 /* surrounds */
5308 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 5309 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
5310 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5311 i * 2, format);
d29240ce 5312 else if (!mout->no_share_stream) /* copy front */
0ba21762
TI
5313 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5314 0, format);
1da177e4
LT
5315 }
5316 return 0;
5317}
ff7a3267 5318EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
1da177e4 5319
d5191e50
TI
5320/**
5321 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
1da177e4 5322 */
0ba21762
TI
5323int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5324 struct hda_multi_out *mout)
1da177e4 5325{
dda14410 5326 const hda_nid_t *nids = mout->dac_nids;
1da177e4
LT
5327 int i;
5328
5329 for (i = 0; i < mout->num_dacs; i++)
888afa15 5330 snd_hda_codec_cleanup_stream(codec, nids[i]);
1da177e4 5331 if (mout->hp_nid)
888afa15 5332 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
a06dbfc2
TI
5333 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5334 if (mout->hp_out_nid[i])
5335 snd_hda_codec_cleanup_stream(codec,
5336 mout->hp_out_nid[i]);
82bc955f
TI
5337 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5338 if (mout->extra_out_nid[i])
888afa15
TI
5339 snd_hda_codec_cleanup_stream(codec,
5340 mout->extra_out_nid[i]);
62932df8 5341 mutex_lock(&codec->spdif_mutex);
1da177e4 5342 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2f72853c 5343 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
5344 mout->dig_out_used = 0;
5345 }
62932df8 5346 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
5347 return 0;
5348}
ff7a3267 5349EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
1da177e4 5350
4740860b
TI
5351/**
5352 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5353 *
5354 * Guess the suitable VREF pin bits to be set as the pin-control value.
5355 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5356 */
5357unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5358{
5359 unsigned int pincap;
5360 unsigned int oldval;
5361 oldval = snd_hda_codec_read(codec, pin, 0,
5362 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5363 pincap = snd_hda_query_pin_caps(codec, pin);
5364 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5365 /* Exception: if the default pin setup is vref50, we give it priority */
5366 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5367 return AC_PINCTL_VREF_80;
5368 else if (pincap & AC_PINCAP_VREF_50)
5369 return AC_PINCTL_VREF_50;
5370 else if (pincap & AC_PINCAP_VREF_100)
5371 return AC_PINCTL_VREF_100;
5372 else if (pincap & AC_PINCAP_VREF_GRD)
5373 return AC_PINCTL_VREF_GRD;
5374 return AC_PINCTL_VREF_HIZ;
5375}
5376EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5377
62f3a2f7
TI
5378/* correct the pin ctl value for matching with the pin cap */
5379unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5380 hda_nid_t pin, unsigned int val)
5381{
5382 static unsigned int cap_lists[][2] = {
5383 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5384 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5385 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5386 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5387 };
5388 unsigned int cap;
5389
5390 if (!val)
5391 return 0;
5392 cap = snd_hda_query_pin_caps(codec, pin);
5393 if (!cap)
5394 return val; /* don't know what to do... */
5395
5396 if (val & AC_PINCTL_OUT_EN) {
5397 if (!(cap & AC_PINCAP_OUT))
5398 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5399 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5400 val &= ~AC_PINCTL_HP_EN;
5401 }
5402
5403 if (val & AC_PINCTL_IN_EN) {
5404 if (!(cap & AC_PINCAP_IN))
5405 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5406 else {
5407 unsigned int vcap, vref;
5408 int i;
5409 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5410 vref = val & AC_PINCTL_VREFEN;
5411 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5412 if (vref == cap_lists[i][0] &&
5413 !(vcap & cap_lists[i][1])) {
5414 if (i == ARRAY_SIZE(cap_lists) - 1)
5415 vref = AC_PINCTL_VREF_HIZ;
5416 else
5417 vref = cap_lists[i + 1][0];
5418 }
5419 }
5420 val &= ~AC_PINCTL_VREFEN;
5421 val |= vref;
5422 }
5423 }
5424
5425 return val;
5426}
5427EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5428
cdd03ced
TI
5429int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5430 unsigned int val, bool cached)
5431{
62f3a2f7 5432 val = snd_hda_correct_pin_ctl(codec, pin, val);
d7fdc00a 5433 snd_hda_codec_set_pin_target(codec, pin, val);
cdd03ced
TI
5434 if (cached)
5435 return snd_hda_codec_update_cache(codec, pin, 0,
5436 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5437 else
5438 return snd_hda_codec_write(codec, pin, 0,
5439 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5440}
5441EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5442
990061c2
TI
5443/**
5444 * snd_hda_add_imux_item - Add an item to input_mux
5445 *
5446 * When the same label is used already in the existing items, the number
5447 * suffix is appended to the label. This label index number is stored
5448 * to type_idx when non-NULL pointer is given.
5449 */
10a20af7
TI
5450int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5451 int index, int *type_idx)
5452{
5453 int i, label_idx = 0;
5454 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5455 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5456 return -EINVAL;
5457 }
5458 for (i = 0; i < imux->num_items; i++) {
5459 if (!strncmp(label, imux->items[i].label, strlen(label)))
5460 label_idx++;
d7b1ae9d 5461 }
10a20af7
TI
5462 if (type_idx)
5463 *type_idx = label_idx;
5464 if (label_idx > 0)
5465 snprintf(imux->items[imux->num_items].label,
5466 sizeof(imux->items[imux->num_items].label),
5467 "%s %d", label, label_idx);
b5786e85 5468 else
10a20af7
TI
5469 strlcpy(imux->items[imux->num_items].label, label,
5470 sizeof(imux->items[imux->num_items].label));
5471 imux->items[imux->num_items].index = index;
5472 imux->num_items++;
5473 return 0;
d7b1ae9d 5474}
10a20af7 5475EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
d7b1ae9d 5476
4a471b7d 5477
1da177e4
LT
5478#ifdef CONFIG_PM
5479/*
5480 * power management
5481 */
5482
5483/**
5484 * snd_hda_suspend - suspend the codecs
5485 * @bus: the HDA bus
1da177e4
LT
5486 *
5487 * Returns 0 if successful.
5488 */
8dd78330 5489int snd_hda_suspend(struct hda_bus *bus)
1da177e4 5490{
0ba21762 5491 struct hda_codec *codec;
1da177e4 5492
0ba21762 5493 list_for_each_entry(codec, &bus->codec_list, list) {
26a6cb6c 5494 cancel_delayed_work_sync(&codec->jackpoll_work);
e581f3db 5495 if (hda_codec_is_power_on(codec))
d17344b3 5496 hda_call_codec_suspend(codec, false);
1da177e4
LT
5497 }
5498 return 0;
5499}
ff7a3267 5500EXPORT_SYMBOL_HDA(snd_hda_suspend);
1da177e4
LT
5501
5502/**
5503 * snd_hda_resume - resume the codecs
5504 * @bus: the HDA bus
1da177e4
LT
5505 *
5506 * Returns 0 if successful.
5507 */
5508int snd_hda_resume(struct hda_bus *bus)
5509{
0ba21762 5510 struct hda_codec *codec;
1da177e4 5511
0ba21762 5512 list_for_each_entry(codec, &bus->codec_list, list) {
7f30830b 5513 hda_call_codec_resume(codec);
1da177e4 5514 }
1da177e4
LT
5515 return 0;
5516}
ff7a3267 5517EXPORT_SYMBOL_HDA(snd_hda_resume);
1289e9e8 5518#endif /* CONFIG_PM */
b2e18597
TI
5519
5520/*
5521 * generic arrays
5522 */
5523
d5191e50
TI
5524/**
5525 * snd_array_new - get a new element from the given array
5526 * @array: the array object
28aedaf7 5527 *
d5191e50
TI
5528 * Get a new element from the given array. If it exceeds the
5529 * pre-allocated array size, re-allocate the array.
5530 *
5531 * Returns NULL if allocation failed.
b2e18597
TI
5532 */
5533void *snd_array_new(struct snd_array *array)
5534{
12f17717
TI
5535 if (snd_BUG_ON(!array->elem_size))
5536 return NULL;
b2e18597
TI
5537 if (array->used >= array->alloced) {
5538 int num = array->alloced + array->alloc_align;
3101ba03 5539 int size = (num + 1) * array->elem_size;
00ef9610 5540 int oldsize = array->alloced * array->elem_size;
b910d9ae
TI
5541 void *nlist;
5542 if (snd_BUG_ON(num >= 4096))
5543 return NULL;
3101ba03 5544 nlist = krealloc(array->list, size, GFP_KERNEL);
b2e18597
TI
5545 if (!nlist)
5546 return NULL;
00ef9610 5547 memset(nlist + oldsize, 0, size - oldsize);
b2e18597
TI
5548 array->list = nlist;
5549 array->alloced = num;
5550 }
f43aa025 5551 return snd_array_elem(array, array->used++);
b2e18597 5552}
ff7a3267 5553EXPORT_SYMBOL_HDA(snd_array_new);
b2e18597 5554
d5191e50
TI
5555/**
5556 * snd_array_free - free the given array elements
5557 * @array: the array object
5558 */
b2e18597
TI
5559void snd_array_free(struct snd_array *array)
5560{
5561 kfree(array->list);
5562 array->used = 0;
5563 array->alloced = 0;
5564 array->list = NULL;
5565}
ff7a3267 5566EXPORT_SYMBOL_HDA(snd_array_free);
b2022266 5567
d5191e50
TI
5568/**
5569 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5570 * @pcm: PCM caps bits
5571 * @buf: the string buffer to write
5572 * @buflen: the max buffer length
5573 *
5574 * used by hda_proc.c and hda_eld.c
5575 */
b2022266
TI
5576void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5577{
5578 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5579 int i, j;
5580
5581 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5582 if (pcm & (AC_SUPPCM_BITS_8 << i))
5583 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5584
5585 buf[j] = '\0'; /* necessary when j == 0 */
5586}
ff7a3267 5587EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
1289e9e8
TI
5588
5589MODULE_DESCRIPTION("HDA codec core");
5590MODULE_LICENSE("GPL");