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