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