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