]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - sound/pci/hda/hda_codec.c
Merge branch 'for-linus' into for-next
[mirror_ubuntu-focal-kernel.git] / sound / pci / hda / hda_codec.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
18478e8b 22#include <linux/mm.h>
1da177e4
LT
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
62932df8 26#include <linux/mutex.h>
da155d5b 27#include <linux/module.h>
f4d6a55d 28#include <linux/async.h>
cc72da7d
TI
29#include <linux/pm.h>
30#include <linux/pm_runtime.h>
1da177e4
LT
31#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
302e9c5a 34#include <sound/tlv.h>
1da177e4 35#include <sound/initval.h>
cd372fb3 36#include <sound/jack.h>
1da177e4 37#include "hda_local.h"
123c07ae 38#include "hda_beep.h"
1835a0f9 39#include "hda_jack.h"
2807314d 40#include <sound/hda_hwdep.h>
1da177e4 41
83012a7c 42#ifdef CONFIG_PM
7639a06c 43#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
cc72da7d
TI
44#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
cb53c626 46#else
d846b174 47#define codec_in_pm(codec) 0
e581f3db 48#define hda_codec_is_power_on(codec) 1
cb53c626
TI
49#endif
50
7639a06c
TI
51#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
d5191e50
TI
56/**
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
59 *
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
62 */
50a9f790
MR
63const char *snd_hda_get_jack_location(u32 cfg)
64{
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
67 };
68 static unsigned char specials_idx[] = {
69 0x07, 0x08,
70 0x17, 0x18, 0x19,
71 0x37, 0x38
72 };
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
77 };
78 int i;
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
80 if ((cfg & 0x0f) < 7)
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
84 return specials[i];
85 }
86 return "UNKNOWN";
87}
2698ea98 88EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
50a9f790 89
d5191e50
TI
90/**
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
93 *
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
96 */
50a9f790
MR
97const char *snd_hda_get_jack_connectivity(u32 cfg)
98{
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
100
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
102}
2698ea98 103EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
50a9f790 104
d5191e50
TI
105/**
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
108 *
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
111 */
50a9f790
MR
112const char *snd_hda_get_jack_type(u32 cfg)
113{
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
aeb3a972 118 "SPDIF In", "Digital In", "Reserved", "Other"
50a9f790
MR
119 };
120
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
123}
2698ea98 124EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
50a9f790 125
aa2936f5 126/*
05852448 127 * Send and receive a verb - passed to exec_verb override for hdac_device
aa2936f5 128 */
05852448
TI
129static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
aa2936f5 131{
05852448 132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
aa2936f5 133 struct hda_bus *bus = codec->bus;
8dd78330 134 int err;
aa2936f5 135
6430aeeb
WF
136 if (cmd == ~0)
137 return -1;
138
8dd78330 139 again:
664c7155 140 snd_hda_power_up_pm(codec);
d068ebc2 141 mutex_lock(&bus->core.cmd_mutex);
63e51fd7
TI
142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
d068ebc2
TI
144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
145 cmd, res);
63e51fd7 146 bus->no_response_fallback = 0;
d068ebc2 147 mutex_unlock(&bus->core.cmd_mutex);
664c7155 148 snd_hda_power_down_pm(codec);
cad372f1 149 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
8dd78330 150 if (bus->response_reset) {
4e76a883
TI
151 codec_dbg(codec,
152 "resetting BUS due to fatal communication error\n");
0a50575b 153 snd_hda_bus_reset(bus);
8dd78330
TI
154 }
155 goto again;
156 }
157 /* clear reset-flag when the communication gets recovered */
d846b174 158 if (!err || codec_in_pm(codec))
8dd78330 159 bus->response_reset = 0;
aa2936f5
TI
160 return err;
161}
162
1da177e4
LT
163/**
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
e7ecc27e 167 * @flags: optional bit flags
1da177e4
LT
168 * @verb: the verb to send
169 * @parm: the parameter for the verb
170 *
171 * Send a single command and read the corresponding response.
172 *
173 * Returns the obtained response value, or -1 for an error.
174 */
0ba21762 175unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
e7ecc27e 176 int flags,
1da177e4
LT
177 unsigned int verb, unsigned int parm)
178{
7639a06c 179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
aa2936f5 180 unsigned int res;
05852448 181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
9857edfd 182 return -1;
1da177e4
LT
183 return res;
184}
2698ea98 185EXPORT_SYMBOL_GPL(snd_hda_codec_read);
1da177e4
LT
186
187/**
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
e7ecc27e 191 * @flags: optional bit flags
1da177e4
LT
192 * @verb: the verb to send
193 * @parm: the parameter for the verb
194 *
195 * Send a single command without waiting for response.
196 *
197 * Returns 0 if successful, or a negative error code.
198 */
e7ecc27e
TI
199int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
1da177e4 201{
7639a06c 202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
05852448 203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
1da177e4 204}
2698ea98 205EXPORT_SYMBOL_GPL(snd_hda_codec_write);
1da177e4
LT
206
207/**
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
211 *
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
214 */
215void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
216{
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
219}
2698ea98 220EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
1da177e4 221
ee8e765b
TI
222/* connection list element */
223struct hda_conn_list {
224 struct list_head list;
225 int len;
226 hda_nid_t nid;
227 hda_nid_t conns[0];
228};
229
b2f934a0 230/* look up the cached results */
ee8e765b
TI
231static struct hda_conn_list *
232lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
b2f934a0 233{
ee8e765b
TI
234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
236 if (p->nid == nid)
b2f934a0 237 return p;
b2f934a0
TI
238 }
239 return NULL;
240}
a12d3e1e 241
ee8e765b
TI
242static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
244{
245 struct hda_conn_list *p;
246
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
248 if (!p)
249 return -ENOMEM;
250 p->len = len;
251 p->nid = nid;
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
254 return 0;
255}
256
257static void remove_conn_list(struct hda_codec *codec)
258{
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
262 list_del(&p->list);
263 kfree(p);
264 }
265}
266
09cf03b8
TI
267/* read the connection and add to the cache */
268static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
269{
4eea3091
TI
270 hda_nid_t list[32];
271 hda_nid_t *result = list;
09cf03b8
TI
272 int len;
273
274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
4eea3091
TI
275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
278 if (!result)
279 return -ENOMEM;
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
281 }
282 if (len >= 0)
283 len = snd_hda_override_conn_list(codec, nid, len, result);
284 if (result != list)
285 kfree(result);
286 return len;
09cf03b8
TI
287}
288
ee8e765b
TI
289/**
290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
292 * @nid: NID to parse
ee8e765b
TI
293 * @listp: the pointer to store NID list
294 *
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
297 *
298 * Returns the number of connections, or a negative error code.
299 *
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
303 */
304int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
306{
307 bool added = false;
308
309 for (;;) {
310 int err;
311 const struct hda_conn_list *p;
312
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
315 if (p) {
316 if (listp)
317 *listp = p->conns;
318 return p->len;
319 }
320 if (snd_BUG_ON(added))
321 return -EINVAL;
322
323 err = read_and_add_raw_conns(codec, nid);
324 if (err < 0)
325 return err;
326 added = true;
327 }
328}
2698ea98 329EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
ee8e765b 330
1da177e4 331/**
09cf03b8 332 * snd_hda_get_connections - copy connection list
1da177e4
LT
333 * @codec: the HDA codec
334 * @nid: NID to parse
09cf03b8
TI
335 * @conn_list: connection list array; when NULL, checks only the size
336 * @max_conns: max. number of connections to store
1da177e4
LT
337 *
338 * Parses the connection list of the given widget and stores the list
339 * of NIDs.
340 *
341 * Returns the number of connections, or a negative error code.
342 */
09cf03b8
TI
343int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *conn_list, int max_conns)
a12d3e1e 345{
ee8e765b
TI
346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
a12d3e1e 348
ee8e765b
TI
349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
4e76a883 351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
09cf03b8 352 len, nid);
09cf03b8
TI
353 return -EINVAL;
354 }
ee8e765b 355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
a12d3e1e
TI
356 }
357
ee8e765b 358 return len;
a12d3e1e 359}
2698ea98 360EXPORT_SYMBOL_GPL(snd_hda_get_connections);
a12d3e1e 361
b2f934a0
TI
362/**
363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
365 * @nid: NID to parse
366 * @len: number of connection list entries
367 * @list: the list of connection entries
368 *
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
371 *
372 * Returns zero or a negative error code.
373 */
374int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
376{
ee8e765b 377 struct hda_conn_list *p;
b2f934a0 378
ee8e765b
TI
379 p = lookup_conn_list(codec, nid);
380 if (p) {
381 list_del(&p->list);
382 kfree(p);
383 }
b2f934a0 384
ee8e765b 385 return add_conn_list(codec, nid, len, list);
b2f934a0 386}
2698ea98 387EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
b2f934a0 388
8d087c76
TI
389/**
390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
395 *
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
399 */
400int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
a12d3e1e 402{
ee8e765b 403 const hda_nid_t *conn;
8d087c76
TI
404 int i, nums;
405
ee8e765b 406 nums = snd_hda_get_conn_list(codec, mux, &conn);
8d087c76
TI
407 for (i = 0; i < nums; i++)
408 if (conn[i] == nid)
409 return i;
410 if (!recursive)
411 return -1;
d94ddd85 412 if (recursive > 10) {
4e76a883 413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
8d087c76 414 return -1;
a12d3e1e 415 }
8d087c76 416 recursive++;
99e14c9d
TI
417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
420 continue;
8d087c76
TI
421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
422 return i;
99e14c9d 423 }
8d087c76 424 return -1;
a12d3e1e 425}
2698ea98 426EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
1da177e4 427
f1aa0684
ML
428
429/* return DEVLIST_LEN parameter of the given widget */
430static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
431{
432 unsigned int wcaps = get_wcaps(codec, nid);
433 unsigned int parm;
434
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
437 return 0;
438
132bd96b 439 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
8654844c 440 if (parm == -1)
f1aa0684
ML
441 parm = 0;
442 return parm & AC_DEV_LIST_LEN_MASK;
443}
444
445/**
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
451 *
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
454 */
455int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
457{
458 unsigned int parm;
459 int i, dev_len, devices;
460
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
463 return 0;
464
465 dev_len = parm + 1;
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
467
468 devices = 0;
469 while (devices < dev_len) {
cad372f1
TI
470 if (snd_hdac_read(&codec->core, nid,
471 AC_VERB_GET_DEVICE_LIST, devices, &parm))
472 break; /* error */
f1aa0684
ML
473
474 for (i = 0; i < 8; i++) {
475 dev_list[devices] = (u8)parm;
476 parm >>= 4;
477 devices++;
478 if (devices >= dev_len)
479 break;
480 }
481 }
482 return devices;
483}
484
54d17403
TI
485/*
486 * read widget caps for each widget and store in cache
487 */
488static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
489{
490 int i;
491 hda_nid_t nid;
492
7639a06c 493 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
0ba21762 494 if (!codec->wcaps)
54d17403 495 return -ENOMEM;
7639a06c
TI
496 nid = codec->core.start_nid;
497 for (i = 0; i < codec->core.num_nodes; i++, nid++)
9ba17b4d
TI
498 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
499 nid, AC_PAR_AUDIO_WIDGET_CAP);
54d17403
TI
500 return 0;
501}
502
3be14149
TI
503/* read all pin default configurations and save codec->init_pins */
504static int read_pin_defaults(struct hda_codec *codec)
505{
7639a06c 506 hda_nid_t nid;
3be14149 507
7639a06c 508 for_each_hda_codec_node(nid, codec) {
3be14149
TI
509 struct hda_pincfg *pin;
510 unsigned int wcaps = get_wcaps(codec, nid);
a22d543a 511 unsigned int wid_type = get_wcaps_type(wcaps);
3be14149
TI
512 if (wid_type != AC_WID_PIN)
513 continue;
514 pin = snd_array_new(&codec->init_pins);
515 if (!pin)
516 return -ENOMEM;
517 pin->nid = nid;
518 pin->cfg = snd_hda_codec_read(codec, nid, 0,
519 AC_VERB_GET_CONFIG_DEFAULT, 0);
ac0547dc
TI
520 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
521 AC_VERB_GET_PIN_WIDGET_CONTROL,
522 0);
3be14149
TI
523 }
524 return 0;
525}
526
527/* look up the given pin config list and return the item matching with NID */
528static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
529 struct snd_array *array,
530 hda_nid_t nid)
531{
532 int i;
533 for (i = 0; i < array->used; i++) {
534 struct hda_pincfg *pin = snd_array_elem(array, i);
535 if (pin->nid == nid)
536 return pin;
537 }
538 return NULL;
539}
540
3be14149
TI
541/* set the current pin config value for the given NID.
542 * the value is cached, and read via snd_hda_codec_get_pincfg()
543 */
544int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
545 hda_nid_t nid, unsigned int cfg)
546{
547 struct hda_pincfg *pin;
548
d5657ec9
TI
549 /* the check below may be invalid when pins are added by a fixup
550 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
551 * for now
552 */
553 /*
b82855a0
TI
554 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
555 return -EINVAL;
d5657ec9 556 */
b82855a0 557
3be14149
TI
558 pin = look_up_pincfg(codec, list, nid);
559 if (!pin) {
560 pin = snd_array_new(list);
561 if (!pin)
562 return -ENOMEM;
563 pin->nid = nid;
564 }
565 pin->cfg = cfg;
3be14149
TI
566 return 0;
567}
568
d5191e50
TI
569/**
570 * snd_hda_codec_set_pincfg - Override a pin default configuration
571 * @codec: the HDA codec
572 * @nid: NID to set the pin config
573 * @cfg: the pin default config value
574 *
575 * Override a pin default configuration value in the cache.
576 * This value can be read by snd_hda_codec_get_pincfg() in a higher
577 * priority than the real hardware value.
578 */
3be14149
TI
579int snd_hda_codec_set_pincfg(struct hda_codec *codec,
580 hda_nid_t nid, unsigned int cfg)
581{
346ff70f 582 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
3be14149 583}
2698ea98 584EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
3be14149 585
d5191e50
TI
586/**
587 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
588 * @codec: the HDA codec
589 * @nid: NID to get the pin config
590 *
591 * Get the current pin config value of the given pin NID.
592 * If the pincfg value is cached or overridden via sysfs or driver,
593 * returns the cached value.
594 */
3be14149
TI
595unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
596{
597 struct hda_pincfg *pin;
598
648a8d27 599#ifdef CONFIG_SND_HDA_RECONFIG
09b70e85
TI
600 {
601 unsigned int cfg = 0;
602 mutex_lock(&codec->user_mutex);
603 pin = look_up_pincfg(codec, &codec->user_pins, nid);
604 if (pin)
605 cfg = pin->cfg;
606 mutex_unlock(&codec->user_mutex);
607 if (cfg)
608 return cfg;
609 }
3be14149 610#endif
5e7b8e0d
TI
611 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
612 if (pin)
613 return pin->cfg;
3be14149
TI
614 pin = look_up_pincfg(codec, &codec->init_pins, nid);
615 if (pin)
616 return pin->cfg;
617 return 0;
618}
2698ea98 619EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
3be14149 620
95a962c3
TI
621/**
622 * snd_hda_codec_set_pin_target - remember the current pinctl target value
623 * @codec: the HDA codec
624 * @nid: pin NID
625 * @val: assigned pinctl value
626 *
627 * This function stores the given value to a pinctl target value in the
628 * pincfg table. This isn't always as same as the actually written value
629 * but can be referred at any time via snd_hda_codec_get_pin_target().
630 */
d7fdc00a
TI
631int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
632 unsigned int val)
633{
634 struct hda_pincfg *pin;
635
636 pin = look_up_pincfg(codec, &codec->init_pins, nid);
637 if (!pin)
638 return -EINVAL;
639 pin->target = val;
640 return 0;
641}
2698ea98 642EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
d7fdc00a 643
95a962c3
TI
644/**
645 * snd_hda_codec_get_pin_target - return the current pinctl target value
646 * @codec: the HDA codec
647 * @nid: pin NID
648 */
d7fdc00a
TI
649int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
650{
651 struct hda_pincfg *pin;
652
653 pin = look_up_pincfg(codec, &codec->init_pins, nid);
654 if (!pin)
655 return 0;
656 return pin->target;
657}
2698ea98 658EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
d7fdc00a 659
92ee6162
TI
660/**
661 * snd_hda_shutup_pins - Shut up all pins
662 * @codec: the HDA codec
663 *
664 * Clear all pin controls to shup up before suspend for avoiding click noise.
665 * The controls aren't cached so that they can be resumed properly.
666 */
667void snd_hda_shutup_pins(struct hda_codec *codec)
668{
669 int i;
ac0547dc
TI
670 /* don't shut up pins when unloading the driver; otherwise it breaks
671 * the default pin setup at the next load of the driver
672 */
673 if (codec->bus->shutdown)
674 return;
92ee6162
TI
675 for (i = 0; i < codec->init_pins.used; i++) {
676 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
677 /* use read here for syncing after issuing each verb */
678 snd_hda_codec_read(codec, pin->nid, 0,
679 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
680 }
ac0547dc 681 codec->pins_shutup = 1;
92ee6162 682}
2698ea98 683EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
92ee6162 684
2a43952a 685#ifdef CONFIG_PM
ac0547dc
TI
686/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
687static void restore_shutup_pins(struct hda_codec *codec)
688{
689 int i;
690 if (!codec->pins_shutup)
691 return;
692 if (codec->bus->shutdown)
693 return;
694 for (i = 0; i < codec->init_pins.used; i++) {
695 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
696 snd_hda_codec_write(codec, pin->nid, 0,
697 AC_VERB_SET_PIN_WIDGET_CONTROL,
698 pin->ctrl);
699 }
700 codec->pins_shutup = 0;
701}
1c7276cf 702#endif
ac0547dc 703
26a6cb6c
DH
704static void hda_jackpoll_work(struct work_struct *work)
705{
706 struct hda_codec *codec =
707 container_of(work, struct hda_codec, jackpoll_work.work);
26a6cb6c
DH
708
709 snd_hda_jack_set_dirty_all(codec);
710 snd_hda_jack_poll_all(codec);
18e60627
WX
711
712 if (!codec->jackpoll_interval)
713 return;
714
2f35c630
TI
715 schedule_delayed_work(&codec->jackpoll_work,
716 codec->jackpoll_interval);
26a6cb6c
DH
717}
718
3fdf1469
TI
719/* release all pincfg lists */
720static void free_init_pincfgs(struct hda_codec *codec)
3be14149 721{
346ff70f 722 snd_array_free(&codec->driver_pins);
648a8d27 723#ifdef CONFIG_SND_HDA_RECONFIG
346ff70f 724 snd_array_free(&codec->user_pins);
3be14149 725#endif
3be14149
TI
726 snd_array_free(&codec->init_pins);
727}
728
eb541337
TI
729/*
730 * audio-converter setup caches
731 */
732struct hda_cvt_setup {
733 hda_nid_t nid;
734 u8 stream_tag;
735 u8 channel_id;
736 u16 format_id;
737 unsigned char active; /* cvt is currently used */
738 unsigned char dirty; /* setups should be cleared */
739};
740
741/* get or create a cache entry for the given audio converter NID */
742static struct hda_cvt_setup *
743get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
744{
745 struct hda_cvt_setup *p;
746 int i;
747
748 for (i = 0; i < codec->cvt_setups.used; i++) {
749 p = snd_array_elem(&codec->cvt_setups, i);
750 if (p->nid == nid)
751 return p;
752 }
753 p = snd_array_new(&codec->cvt_setups);
754 if (p)
755 p->nid = nid;
756 return p;
757}
758
bbbc7e85
TI
759/*
760 * PCM device
761 */
762static void release_pcm(struct kref *kref)
763{
764 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
765
766 if (pcm->pcm)
767 snd_device_free(pcm->codec->card, pcm->pcm);
768 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
769 kfree(pcm->name);
770 kfree(pcm);
771}
772
773void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
774{
775 kref_put(&pcm->kref, release_pcm);
776}
777EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
778
779struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
780 const char *fmt, ...)
781{
782 struct hda_pcm *pcm;
783 va_list args;
784
bbbc7e85
TI
785 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
786 if (!pcm)
787 return NULL;
788
789 pcm->codec = codec;
790 kref_init(&pcm->kref);
30e5f003 791 va_start(args, fmt);
bbbc7e85 792 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
30e5f003 793 va_end(args);
bbbc7e85
TI
794 if (!pcm->name) {
795 kfree(pcm);
796 return NULL;
797 }
798
799 list_add_tail(&pcm->list, &codec->pcm_list_head);
800 return pcm;
801}
802EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
803
9a6246ff
TI
804/*
805 * codec destructor
806 */
bbbc7e85
TI
807static void codec_release_pcms(struct hda_codec *codec)
808{
809 struct hda_pcm *pcm, *n;
810
811 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
812 list_del_init(&pcm->list);
9a6246ff
TI
813 if (pcm->pcm)
814 snd_device_disconnect(codec->card, pcm->pcm);
bbbc7e85
TI
815 snd_hda_codec_pcm_put(pcm);
816 }
817}
818
9a6246ff
TI
819void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
820{
c4c2533f
TI
821 if (codec->registered) {
822 /* pm_runtime_put() is called in snd_hdac_device_exit() */
823 pm_runtime_get_noresume(hda_codec_dev(codec));
824 pm_runtime_disable(hda_codec_dev(codec));
825 codec->registered = 0;
826 }
827
9a6246ff 828 cancel_delayed_work_sync(&codec->jackpoll_work);
9a6246ff
TI
829 if (!codec->in_freeing)
830 snd_hda_ctls_clear(codec);
831 codec_release_pcms(codec);
832 snd_hda_detach_beep_device(codec);
833 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
834 snd_hda_jack_tbl_clear(codec);
835 codec->proc_widget_hook = NULL;
836 codec->spec = NULL;
837
9a6246ff
TI
838 /* free only driver_pins so that init_pins + user_pins are restored */
839 snd_array_free(&codec->driver_pins);
840 snd_array_free(&codec->cvt_setups);
841 snd_array_free(&codec->spdif_out);
842 snd_array_free(&codec->verbs);
843 codec->preset = NULL;
844 codec->slave_dig_outs = NULL;
845 codec->spdif_status_reset = 0;
846 snd_array_free(&codec->mixers);
847 snd_array_free(&codec->nids);
848 remove_conn_list(codec);
4d75faa0 849 snd_hdac_regmap_exit(&codec->core);
9a6246ff
TI
850}
851
d819387e 852static unsigned int hda_set_power_state(struct hda_codec *codec,
bb6ac72f
TI
853 unsigned int power_state);
854
c4c2533f
TI
855/* also called from hda_bind.c */
856void snd_hda_codec_register(struct hda_codec *codec)
13aeaf68 857{
c4c2533f
TI
858 if (codec->registered)
859 return;
860 if (device_is_registered(hda_codec_dev(codec))) {
861 snd_hda_register_beep_device(codec);
a5e7e07c 862 snd_hdac_link_power(&codec->core, true);
cc72da7d 863 pm_runtime_enable(hda_codec_dev(codec));
c4c2533f
TI
864 /* it was powered up in snd_hda_codec_new(), now all done */
865 snd_hda_power_down(codec);
866 codec->registered = 1;
867 }
868}
869
870static int snd_hda_codec_dev_register(struct snd_device *device)
871{
872 snd_hda_codec_register(device->device_data);
d604b399 873 return 0;
13aeaf68
TI
874}
875
876static int snd_hda_codec_dev_disconnect(struct snd_device *device)
877{
878 struct hda_codec *codec = device->device_data;
879
d604b399 880 snd_hda_detach_beep_device(codec);
13aeaf68
TI
881 return 0;
882}
883
2565c899
TI
884static int snd_hda_codec_dev_free(struct snd_device *device)
885{
d56db741
TI
886 struct hda_codec *codec = device->device_data;
887
888 codec->in_freeing = 1;
a5e7e07c 889 snd_hdac_link_power(&codec->core, false);
3256be65 890 snd_hdac_device_unregister(&codec->core);
d56db741 891 put_device(hda_codec_dev(codec));
2565c899
TI
892 return 0;
893}
894
13aeaf68
TI
895static void snd_hda_codec_dev_release(struct device *dev)
896{
d56db741
TI
897 struct hda_codec *codec = dev_to_hda_codec(dev);
898
899 free_init_pincfgs(codec);
7639a06c 900 snd_hdac_device_exit(&codec->core);
d56db741 901 snd_hda_sysfs_clear(codec);
d56db741
TI
902 kfree(codec->modelname);
903 kfree(codec->wcaps);
d56db741 904 kfree(codec);
13aeaf68
TI
905}
906
1da177e4
LT
907/**
908 * snd_hda_codec_new - create a HDA codec
909 * @bus: the bus to assign
910 * @codec_addr: the codec address
911 * @codecp: the pointer to store the generated codec
912 *
913 * Returns 0 if successful, or a negative error code.
914 */
6efdd851
TI
915int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
916 unsigned int codec_addr, struct hda_codec **codecp)
1da177e4
LT
917{
918 struct hda_codec *codec;
ba443687 919 char component[31];
d819387e 920 hda_nid_t fg;
1da177e4 921 int err;
2565c899 922 static struct snd_device_ops dev_ops = {
13aeaf68
TI
923 .dev_register = snd_hda_codec_dev_register,
924 .dev_disconnect = snd_hda_codec_dev_disconnect,
2565c899
TI
925 .dev_free = snd_hda_codec_dev_free,
926 };
1da177e4 927
da3cec35
TI
928 if (snd_BUG_ON(!bus))
929 return -EINVAL;
930 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
931 return -EINVAL;
1da177e4 932
e560d8d8 933 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
f4de8fe6 934 if (!codec)
1da177e4 935 return -ENOMEM;
1da177e4 936
7639a06c
TI
937 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
938 err = snd_hdac_device_init(&codec->core, &bus->core, component,
939 codec_addr);
940 if (err < 0) {
941 kfree(codec);
942 return err;
943 }
d068ebc2 944
7639a06c
TI
945 codec->core.dev.release = snd_hda_codec_dev_release;
946 codec->core.type = HDA_DEV_LEGACY;
05852448 947 codec->core.exec_verb = codec_exec_verb;
13aeaf68 948
1da177e4 949 codec->bus = bus;
6efdd851 950 codec->card = card;
1da177e4 951 codec->addr = codec_addr;
62932df8 952 mutex_init(&codec->spdif_mutex);
5a9e02e9 953 mutex_init(&codec->control_mutex);
5b0cb1d8
JK
954 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
955 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
3be14149 956 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
346ff70f 957 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
eb541337 958 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
7c935976 959 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
361dab3e 960 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
c9ce6b26 961 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
ee8e765b 962 INIT_LIST_HEAD(&codec->conn_list);
bbbc7e85 963 INIT_LIST_HEAD(&codec->pcm_list_head);
ee8e765b 964
26a6cb6c 965 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
7f132927 966 codec->depop_delay = -1;
f5662e1c 967 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1da177e4 968
83012a7c 969#ifdef CONFIG_PM
cc72da7d 970 codec->power_jiffies = jiffies;
cb53c626
TI
971#endif
972
648a8d27
TI
973 snd_hda_sysfs_init(codec);
974
c382a9f0
TI
975 if (codec->bus->modelname) {
976 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
977 if (!codec->modelname) {
13aeaf68
TI
978 err = -ENODEV;
979 goto error;
c382a9f0
TI
980 }
981 }
982
7639a06c 983 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
d819387e 984 err = read_widget_caps(codec, fg);
f4de8fe6 985 if (err < 0)
3be14149 986 goto error;
3be14149
TI
987 err = read_pin_defaults(codec);
988 if (err < 0)
989 goto error;
54d17403 990
bb6ac72f 991 /* power-up all before initialization */
d819387e 992 hda_set_power_state(codec, AC_PWRST_D0);
bb6ac72f 993
6c1f45ea
TI
994 snd_hda_codec_proc_new(codec);
995
6c1f45ea 996 snd_hda_create_hwdep(codec);
6c1f45ea 997
7639a06c
TI
998 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
999 codec->core.subsystem_id, codec->core.revision_id);
6efdd851 1000 snd_component_add(card, component);
6c1f45ea 1001
6efdd851 1002 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
2565c899
TI
1003 if (err < 0)
1004 goto error;
1005
6c1f45ea
TI
1006 if (codecp)
1007 *codecp = codec;
1008 return 0;
3be14149
TI
1009
1010 error:
d56db741 1011 put_device(hda_codec_dev(codec));
3be14149 1012 return err;
6c1f45ea 1013}
2698ea98 1014EXPORT_SYMBOL_GPL(snd_hda_codec_new);
6c1f45ea 1015
95a962c3
TI
1016/**
1017 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1018 * @codec: the HDA codec
1019 *
1020 * Forcibly refresh the all widget caps and the init pin configurations of
1021 * the given codec.
1022 */
a15d05db
ML
1023int snd_hda_codec_update_widgets(struct hda_codec *codec)
1024{
1025 hda_nid_t fg;
1026 int err;
1027
7639a06c
TI
1028 err = snd_hdac_refresh_widgets(&codec->core);
1029 if (err < 0)
1030 return err;
1031
a15d05db
ML
1032 /* Assume the function group node does not change,
1033 * only the widget nodes may change.
1034 */
1035 kfree(codec->wcaps);
7639a06c 1036 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
a15d05db 1037 err = read_widget_caps(codec, fg);
f4de8fe6 1038 if (err < 0)
a15d05db 1039 return err;
a15d05db
ML
1040
1041 snd_array_free(&codec->init_pins);
1042 err = read_pin_defaults(codec);
1043
1044 return err;
1045}
2698ea98 1046EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
a15d05db 1047
ed360813
TI
1048/* update the stream-id if changed */
1049static void update_pcm_stream_id(struct hda_codec *codec,
1050 struct hda_cvt_setup *p, hda_nid_t nid,
1051 u32 stream_tag, int channel_id)
1052{
1053 unsigned int oldval, newval;
1054
1055 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1056 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1057 newval = (stream_tag << 4) | channel_id;
1058 if (oldval != newval)
1059 snd_hda_codec_write(codec, nid, 0,
1060 AC_VERB_SET_CHANNEL_STREAMID,
1061 newval);
1062 p->stream_tag = stream_tag;
1063 p->channel_id = channel_id;
1064 }
1065}
1066
1067/* update the format-id if changed */
1068static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1069 hda_nid_t nid, int format)
1070{
1071 unsigned int oldval;
1072
1073 if (p->format_id != format) {
1074 oldval = snd_hda_codec_read(codec, nid, 0,
1075 AC_VERB_GET_STREAM_FORMAT, 0);
1076 if (oldval != format) {
1077 msleep(1);
1078 snd_hda_codec_write(codec, nid, 0,
1079 AC_VERB_SET_STREAM_FORMAT,
1080 format);
1081 }
1082 p->format_id = format;
1083 }
1084}
1085
1da177e4
LT
1086/**
1087 * snd_hda_codec_setup_stream - set up the codec for streaming
1088 * @codec: the CODEC to set up
1089 * @nid: the NID to set up
1090 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1091 * @channel_id: channel id to pass, zero based.
1092 * @format: stream format.
1093 */
0ba21762
TI
1094void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1095 u32 stream_tag,
1da177e4
LT
1096 int channel_id, int format)
1097{
3f50ac6a 1098 struct hda_codec *c;
eb541337 1099 struct hda_cvt_setup *p;
62b7e5e0 1100 int type;
eb541337
TI
1101 int i;
1102
0ba21762 1103 if (!nid)
d21b37ea
TI
1104 return;
1105
4e76a883
TI
1106 codec_dbg(codec,
1107 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1108 nid, stream_tag, channel_id, format);
eb541337 1109 p = get_hda_cvt_setup(codec, nid);
6c35ae3c 1110 if (!p)
eb541337 1111 return;
ed360813 1112
e6feb5d0
TI
1113 if (codec->patch_ops.stream_pm)
1114 codec->patch_ops.stream_pm(codec, nid, true);
ed360813
TI
1115 if (codec->pcm_format_first)
1116 update_pcm_format(codec, p, nid, format);
1117 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1118 if (!codec->pcm_format_first)
1119 update_pcm_format(codec, p, nid, format);
1120
eb541337
TI
1121 p->active = 1;
1122 p->dirty = 0;
1123
1124 /* make other inactive cvts with the same stream-tag dirty */
62b7e5e0 1125 type = get_wcaps_type(get_wcaps(codec, nid));
d068ebc2 1126 list_for_each_codec(c, codec->bus) {
3f50ac6a
TI
1127 for (i = 0; i < c->cvt_setups.used; i++) {
1128 p = snd_array_elem(&c->cvt_setups, i);
62b7e5e0 1129 if (!p->active && p->stream_tag == stream_tag &&
54c2a89f 1130 get_wcaps_type(get_wcaps(c, p->nid)) == type)
3f50ac6a
TI
1131 p->dirty = 1;
1132 }
eb541337 1133 }
1da177e4 1134}
2698ea98 1135EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1da177e4 1136
f0cea797
TI
1137static void really_cleanup_stream(struct hda_codec *codec,
1138 struct hda_cvt_setup *q);
1139
d5191e50 1140/**
f0cea797 1141 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
d5191e50
TI
1142 * @codec: the CODEC to clean up
1143 * @nid: the NID to clean up
f0cea797 1144 * @do_now: really clean up the stream instead of clearing the active flag
d5191e50 1145 */
f0cea797
TI
1146void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1147 int do_now)
888afa15 1148{
eb541337
TI
1149 struct hda_cvt_setup *p;
1150
888afa15
TI
1151 if (!nid)
1152 return;
1153
0e7adbe2
TI
1154 if (codec->no_sticky_stream)
1155 do_now = 1;
1156
4e76a883 1157 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
eb541337 1158 p = get_hda_cvt_setup(codec, nid);
6c35ae3c 1159 if (p) {
f0cea797
TI
1160 /* here we just clear the active flag when do_now isn't set;
1161 * actual clean-ups will be done later in
1162 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1163 */
1164 if (do_now)
1165 really_cleanup_stream(codec, p);
1166 else
1167 p->active = 0;
1168 }
eb541337 1169}
2698ea98 1170EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
eb541337
TI
1171
1172static void really_cleanup_stream(struct hda_codec *codec,
1173 struct hda_cvt_setup *q)
1174{
1175 hda_nid_t nid = q->nid;
218264ae
TI
1176 if (q->stream_tag || q->channel_id)
1177 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1178 if (q->format_id)
1179 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1180);
eb541337
TI
1181 memset(q, 0, sizeof(*q));
1182 q->nid = nid;
e6feb5d0
TI
1183 if (codec->patch_ops.stream_pm)
1184 codec->patch_ops.stream_pm(codec, nid, false);
eb541337
TI
1185}
1186
1187/* clean up the all conflicting obsolete streams */
1188static void purify_inactive_streams(struct hda_codec *codec)
1189{
3f50ac6a 1190 struct hda_codec *c;
eb541337
TI
1191 int i;
1192
d068ebc2 1193 list_for_each_codec(c, codec->bus) {
3f50ac6a
TI
1194 for (i = 0; i < c->cvt_setups.used; i++) {
1195 struct hda_cvt_setup *p;
1196 p = snd_array_elem(&c->cvt_setups, i);
1197 if (p->dirty)
1198 really_cleanup_stream(c, p);
1199 }
eb541337
TI
1200 }
1201}
1202
2a43952a 1203#ifdef CONFIG_PM
eb541337
TI
1204/* clean up all streams; called from suspend */
1205static void hda_cleanup_all_streams(struct hda_codec *codec)
1206{
1207 int i;
1208
1209 for (i = 0; i < codec->cvt_setups.used; i++) {
1210 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1211 if (p->stream_tag)
1212 really_cleanup_stream(codec, p);
1213 }
888afa15 1214}
1c7276cf 1215#endif
888afa15 1216
1da177e4
LT
1217/*
1218 * amp access functions
1219 */
1220
d5191e50
TI
1221/**
1222 * query_amp_caps - query AMP capabilities
1223 * @codec: the HD-auio codec
1224 * @nid: the NID to query
1225 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1226 *
1227 * Query AMP capabilities for the given widget and direction.
1228 * Returns the obtained capability bits.
1229 *
1230 * When cap bits have been already read, this doesn't read again but
1231 * returns the cached value.
1da177e4 1232 */
09a99959 1233u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1da177e4 1234{
faa75f8a
TI
1235 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1236 nid = codec->core.afg;
1237 return snd_hda_param_read(codec, nid,
1238 direction == HDA_OUTPUT ?
1239 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1da177e4 1240}
2698ea98 1241EXPORT_SYMBOL_GPL(query_amp_caps);
1da177e4 1242
861a04ed
DH
1243/**
1244 * snd_hda_check_amp_caps - query AMP capabilities
1245 * @codec: the HD-audio codec
1246 * @nid: the NID to query
1247 * @dir: either #HDA_INPUT or #HDA_OUTPUT
a11e9b16 1248 * @bits: bit mask to check the result
861a04ed
DH
1249 *
1250 * Check whether the widget has the given amp capability for the direction.
1251 */
1252bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1253 int dir, unsigned int bits)
1254{
1255 if (!nid)
1256 return false;
1257 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1258 if (query_amp_caps(codec, nid, dir) & bits)
1259 return true;
1260 return false;
1261}
1262EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1263
d5191e50
TI
1264/**
1265 * snd_hda_override_amp_caps - Override the AMP capabilities
1266 * @codec: the CODEC to clean up
1267 * @nid: the NID to clean up
a11e9b16 1268 * @dir: either #HDA_INPUT or #HDA_OUTPUT
d5191e50
TI
1269 * @caps: the capability bits to set
1270 *
1271 * Override the cached AMP caps bits value by the given one.
1272 * This function is useful if the driver needs to adjust the AMP ranges,
1273 * e.g. limit to 0dB, etc.
1274 *
1275 * Returns zero if successful or a negative error code.
1276 */
897cc188
TI
1277int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1278 unsigned int caps)
1279{
faa75f8a 1280 unsigned int parm;
897cc188 1281
faa75f8a
TI
1282 snd_hda_override_wcaps(codec, nid,
1283 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1284 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1285 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
f57c2565 1286}
faa75f8a 1287EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
f57c2565 1288
d5191e50
TI
1289/**
1290 * snd_hda_codec_amp_stereo - update the AMP stereo values
1291 * @codec: HD-audio codec
1292 * @nid: NID to read the AMP value
1293 * @direction: #HDA_INPUT or #HDA_OUTPUT
1294 * @idx: the index value (only for input direction)
1295 * @mask: bit mask to set
1296 * @val: the bits value to set
1297 *
1298 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1299 * stereo widget with the same mask and value.
47fd830a
TI
1300 */
1301int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1302 int direction, int idx, int mask, int val)
1303{
1304 int ch, ret = 0;
46712646
TI
1305
1306 if (snd_BUG_ON(mask & ~0xff))
1307 mask &= 0xff;
47fd830a
TI
1308 for (ch = 0; ch < 2; ch++)
1309 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1310 idx, mask, val);
1311 return ret;
1312}
2698ea98 1313EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
47fd830a 1314
95a962c3
TI
1315/**
1316 * snd_hda_codec_amp_init - initialize the AMP value
1317 * @codec: the HDA codec
1318 * @nid: NID to read the AMP value
1319 * @ch: channel (left=0 or right=1)
1320 * @dir: #HDA_INPUT or #HDA_OUTPUT
1321 * @idx: the index value (only for input direction)
1322 * @mask: bit mask to set
1323 * @val: the bits value to set
1324 *
1325 * Works like snd_hda_codec_amp_update() but it writes the value only at
280e57d5
TI
1326 * the first access. If the amp was already initialized / updated beforehand,
1327 * this does nothing.
1328 */
1329int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1330 int dir, int idx, int mask, int val)
1331{
eeecd9d1
TI
1332 int orig;
1333
1334 if (!codec->core.regmap)
1335 return -EINVAL;
1336 regcache_cache_only(codec->core.regmap, true);
1337 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1338 regcache_cache_only(codec->core.regmap, false);
1339 if (orig >= 0)
1340 return 0;
1341 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
280e57d5 1342}
2698ea98 1343EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
280e57d5 1344
95a962c3
TI
1345/**
1346 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1347 * @codec: the HDA codec
1348 * @nid: NID to read the AMP value
1349 * @dir: #HDA_INPUT or #HDA_OUTPUT
1350 * @idx: the index value (only for input direction)
1351 * @mask: bit mask to set
1352 * @val: the bits value to set
1353 *
1354 * Call snd_hda_codec_amp_init() for both stereo channels.
1355 */
280e57d5
TI
1356int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1357 int dir, int idx, int mask, int val)
1358{
1359 int ch, ret = 0;
1360
1361 if (snd_BUG_ON(mask & ~0xff))
1362 mask &= 0xff;
1363 for (ch = 0; ch < 2; ch++)
1364 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1365 idx, mask, val);
1366 return ret;
1367}
2698ea98 1368EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
280e57d5 1369
afbd9b84
TI
1370static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1371 unsigned int ofs)
1372{
1373 u32 caps = query_amp_caps(codec, nid, dir);
1374 /* get num steps */
1375 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1376 if (ofs < caps)
1377 caps -= ofs;
1378 return caps;
1379}
1380
d5191e50
TI
1381/**
1382 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
a11e9b16
TI
1383 * @kcontrol: referred ctl element
1384 * @uinfo: pointer to get/store the data
d5191e50
TI
1385 *
1386 * The control element is supposed to have the private_value field
1387 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1388 */
0ba21762
TI
1389int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1390 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1391{
1392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1393 u16 nid = get_amp_nid(kcontrol);
1394 u8 chs = get_amp_channels(kcontrol);
1395 int dir = get_amp_direction(kcontrol);
29fdbec2 1396 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4 1397
afbd9b84
TI
1398 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1399 uinfo->count = chs == 3 ? 2 : 1;
1400 uinfo->value.integer.min = 0;
1401 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1402 if (!uinfo->value.integer.max) {
4e76a883
TI
1403 codec_warn(codec,
1404 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1405 nid, kcontrol->id.name);
1da177e4
LT
1406 return -EINVAL;
1407 }
1da177e4
LT
1408 return 0;
1409}
2698ea98 1410EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1da177e4 1411
29fdbec2
TI
1412
1413static inline unsigned int
1414read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1415 int ch, int dir, int idx, unsigned int ofs)
1416{
1417 unsigned int val;
1418 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1419 val &= HDA_AMP_VOLMASK;
1420 if (val >= ofs)
1421 val -= ofs;
1422 else
1423 val = 0;
1424 return val;
1425}
1426
1427static inline int
1428update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1429 int ch, int dir, int idx, unsigned int ofs,
1430 unsigned int val)
1431{
afbd9b84
TI
1432 unsigned int maxval;
1433
29fdbec2
TI
1434 if (val > 0)
1435 val += ofs;
7ccc3efa
TI
1436 /* ofs = 0: raw max value */
1437 maxval = get_amp_max_value(codec, nid, dir, 0);
afbd9b84
TI
1438 if (val > maxval)
1439 val = maxval;
eeecd9d1
TI
1440 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1441 HDA_AMP_VOLMASK, val);
29fdbec2
TI
1442}
1443
d5191e50
TI
1444/**
1445 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
a11e9b16
TI
1446 * @kcontrol: ctl element
1447 * @ucontrol: pointer to get/store the data
d5191e50
TI
1448 *
1449 * The control element is supposed to have the private_value field
1450 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1451 */
0ba21762
TI
1452int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1454{
1455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1456 hda_nid_t nid = get_amp_nid(kcontrol);
1457 int chs = get_amp_channels(kcontrol);
1458 int dir = get_amp_direction(kcontrol);
1459 int idx = get_amp_index(kcontrol);
29fdbec2 1460 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
1461 long *valp = ucontrol->value.integer.value;
1462
1463 if (chs & 1)
29fdbec2 1464 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1da177e4 1465 if (chs & 2)
29fdbec2 1466 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1da177e4
LT
1467 return 0;
1468}
2698ea98 1469EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1da177e4 1470
d5191e50
TI
1471/**
1472 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
a11e9b16
TI
1473 * @kcontrol: ctl element
1474 * @ucontrol: pointer to get/store the data
d5191e50
TI
1475 *
1476 * The control element is supposed to have the private_value field
1477 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1478 */
0ba21762
TI
1479int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1480 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1481{
1482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1483 hda_nid_t nid = get_amp_nid(kcontrol);
1484 int chs = get_amp_channels(kcontrol);
1485 int dir = get_amp_direction(kcontrol);
1486 int idx = get_amp_index(kcontrol);
29fdbec2 1487 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
1488 long *valp = ucontrol->value.integer.value;
1489 int change = 0;
1490
b9f5a89c 1491 if (chs & 1) {
29fdbec2 1492 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
b9f5a89c
NG
1493 valp++;
1494 }
4a19faee 1495 if (chs & 2)
29fdbec2 1496 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1da177e4
LT
1497 return change;
1498}
2698ea98 1499EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1da177e4 1500
d5191e50
TI
1501/**
1502 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
a11e9b16
TI
1503 * @kcontrol: ctl element
1504 * @op_flag: operation flag
1505 * @size: byte size of input TLV
1506 * @_tlv: TLV data
d5191e50
TI
1507 *
1508 * The control element is supposed to have the private_value field
1509 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1510 */
302e9c5a
JK
1511int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1512 unsigned int size, unsigned int __user *_tlv)
1513{
1514 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1515 hda_nid_t nid = get_amp_nid(kcontrol);
1516 int dir = get_amp_direction(kcontrol);
29fdbec2 1517 unsigned int ofs = get_amp_offset(kcontrol);
de8c85f7 1518 bool min_mute = get_amp_min_mute(kcontrol);
302e9c5a
JK
1519 u32 caps, val1, val2;
1520
1521 if (size < 4 * sizeof(unsigned int))
1522 return -ENOMEM;
1523 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
1524 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1525 val2 = (val2 + 1) * 25;
302e9c5a 1526 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
29fdbec2 1527 val1 += ofs;
302e9c5a 1528 val1 = ((int)val1) * ((int)val2);
3868137e 1529 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
c08d9169 1530 val2 |= TLV_DB_SCALE_MUTE;
302e9c5a
JK
1531 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1532 return -EFAULT;
1533 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1534 return -EFAULT;
1535 if (put_user(val1, _tlv + 2))
1536 return -EFAULT;
1537 if (put_user(val2, _tlv + 3))
1538 return -EFAULT;
1539 return 0;
1540}
2698ea98 1541EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
302e9c5a 1542
d5191e50
TI
1543/**
1544 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1545 * @codec: HD-audio codec
1546 * @nid: NID of a reference widget
1547 * @dir: #HDA_INPUT or #HDA_OUTPUT
1548 * @tlv: TLV data to be stored, at least 4 elements
1549 *
1550 * Set (static) TLV data for a virtual master volume using the AMP caps
1551 * obtained from the reference NID.
1552 * The volume range is recalculated as if the max volume is 0dB.
2134ea4f
TI
1553 */
1554void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1555 unsigned int *tlv)
1556{
1557 u32 caps;
1558 int nums, step;
1559
1560 caps = query_amp_caps(codec, nid, dir);
1561 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1562 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1563 step = (step + 1) * 25;
1564 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1565 tlv[1] = 2 * sizeof(unsigned int);
1566 tlv[2] = -nums * step;
1567 tlv[3] = step;
1568}
2698ea98 1569EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2134ea4f
TI
1570
1571/* find a mixer control element with the given name */
09f99701 1572static struct snd_kcontrol *
dcda5806 1573find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2134ea4f
TI
1574{
1575 struct snd_ctl_elem_id id;
1576 memset(&id, 0, sizeof(id));
1577 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
dcda5806 1578 id.device = dev;
09f99701 1579 id.index = idx;
18cb7109
TI
1580 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1581 return NULL;
2134ea4f 1582 strcpy(id.name, name);
6efdd851 1583 return snd_ctl_find_id(codec->card, &id);
2134ea4f
TI
1584}
1585
d5191e50
TI
1586/**
1587 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1588 * @codec: HD-audio codec
1589 * @name: ctl id name string
1590 *
1591 * Get the control element with the given id string and IFACE_MIXER.
1592 */
09f99701
TI
1593struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1594 const char *name)
1595{
dcda5806 1596 return find_mixer_ctl(codec, name, 0, 0);
09f99701 1597}
2698ea98 1598EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
09f99701 1599
dcda5806 1600static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
ea9b43ad 1601 int start_idx)
1afe206a 1602{
ea9b43ad
TI
1603 int i, idx;
1604 /* 16 ctlrs should be large enough */
1605 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1606 if (!find_mixer_ctl(codec, name, 0, idx))
1afe206a
TI
1607 return idx;
1608 }
1609 return -EBUSY;
1610}
1611
d5191e50 1612/**
5b0cb1d8 1613 * snd_hda_ctl_add - Add a control element and assign to the codec
d5191e50
TI
1614 * @codec: HD-audio codec
1615 * @nid: corresponding NID (optional)
1616 * @kctl: the control element to assign
1617 *
1618 * Add the given control element to an array inside the codec instance.
1619 * All control elements belonging to a codec are supposed to be added
1620 * by this function so that a proper clean-up works at the free or
1621 * reconfiguration time.
1622 *
1623 * If non-zero @nid is passed, the NID is assigned to the control element.
1624 * The assignment is shown in the codec proc file.
1625 *
1626 * snd_hda_ctl_add() checks the control subdev id field whether
1627 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
9e3fd871
JK
1628 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1629 * specifies if kctl->private_value is a HDA amplifier value.
d5191e50 1630 */
3911a4c1
JK
1631int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1632 struct snd_kcontrol *kctl)
d13bd412
TI
1633{
1634 int err;
9e3fd871 1635 unsigned short flags = 0;
3911a4c1 1636 struct hda_nid_item *item;
d13bd412 1637
5e26dfd0 1638 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
9e3fd871 1639 flags |= HDA_NID_ITEM_AMP;
5e26dfd0
JK
1640 if (nid == 0)
1641 nid = get_amp_nid_(kctl->private_value);
1642 }
9e3fd871
JK
1643 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1644 nid = kctl->id.subdevice & 0xffff;
5e26dfd0 1645 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
4d02d1b6 1646 kctl->id.subdevice = 0;
6efdd851 1647 err = snd_ctl_add(codec->card, kctl);
d13bd412
TI
1648 if (err < 0)
1649 return err;
3911a4c1
JK
1650 item = snd_array_new(&codec->mixers);
1651 if (!item)
d13bd412 1652 return -ENOMEM;
3911a4c1
JK
1653 item->kctl = kctl;
1654 item->nid = nid;
9e3fd871 1655 item->flags = flags;
d13bd412
TI
1656 return 0;
1657}
2698ea98 1658EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
d13bd412 1659
5b0cb1d8
JK
1660/**
1661 * snd_hda_add_nid - Assign a NID to a control element
1662 * @codec: HD-audio codec
1663 * @nid: corresponding NID (optional)
1664 * @kctl: the control element to assign
1665 * @index: index to kctl
1666 *
1667 * Add the given control element to an array inside the codec instance.
1668 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1669 * NID:KCTL mapping - for example "Capture Source" selector.
1670 */
1671int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1672 unsigned int index, hda_nid_t nid)
1673{
1674 struct hda_nid_item *item;
1675
1676 if (nid > 0) {
1677 item = snd_array_new(&codec->nids);
1678 if (!item)
1679 return -ENOMEM;
1680 item->kctl = kctl;
1681 item->index = index;
1682 item->nid = nid;
1683 return 0;
1684 }
4e76a883
TI
1685 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1686 kctl->id.name, kctl->id.index, index);
5b0cb1d8
JK
1687 return -EINVAL;
1688}
2698ea98 1689EXPORT_SYMBOL_GPL(snd_hda_add_nid);
5b0cb1d8 1690
d5191e50
TI
1691/**
1692 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1693 * @codec: HD-audio codec
1694 */
d13bd412
TI
1695void snd_hda_ctls_clear(struct hda_codec *codec)
1696{
1697 int i;
3911a4c1 1698 struct hda_nid_item *items = codec->mixers.list;
d13bd412 1699 for (i = 0; i < codec->mixers.used; i++)
6efdd851 1700 snd_ctl_remove(codec->card, items[i].kctl);
d13bd412 1701 snd_array_free(&codec->mixers);
5b0cb1d8 1702 snd_array_free(&codec->nids);
d13bd412
TI
1703}
1704
95a962c3
TI
1705/**
1706 * snd_hda_lock_devices - pseudo device locking
1707 * @bus: the BUS
1708 *
a65d629c
TI
1709 * toggle card->shutdown to allow/disallow the device access (as a hack)
1710 */
d3d020bd 1711int snd_hda_lock_devices(struct hda_bus *bus)
6c1f45ea 1712{
d3d020bd
TI
1713 struct snd_card *card = bus->card;
1714 struct hda_codec *codec;
1715
a65d629c 1716 spin_lock(&card->files_lock);
d3d020bd
TI
1717 if (card->shutdown)
1718 goto err_unlock;
a65d629c 1719 card->shutdown = 1;
d3d020bd
TI
1720 if (!list_empty(&card->ctl_files))
1721 goto err_clear;
1722
d068ebc2 1723 list_for_each_codec(codec, bus) {
bbbc7e85
TI
1724 struct hda_pcm *cpcm;
1725 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
d3d020bd
TI
1726 if (!cpcm->pcm)
1727 continue;
1728 if (cpcm->pcm->streams[0].substream_opened ||
1729 cpcm->pcm->streams[1].substream_opened)
1730 goto err_clear;
1731 }
1732 }
a65d629c
TI
1733 spin_unlock(&card->files_lock);
1734 return 0;
d3d020bd
TI
1735
1736 err_clear:
1737 card->shutdown = 0;
1738 err_unlock:
1739 spin_unlock(&card->files_lock);
1740 return -EINVAL;
a65d629c 1741}
2698ea98 1742EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
a65d629c 1743
95a962c3
TI
1744/**
1745 * snd_hda_unlock_devices - pseudo device unlocking
1746 * @bus: the BUS
1747 */
d3d020bd 1748void snd_hda_unlock_devices(struct hda_bus *bus)
a65d629c 1749{
d3d020bd
TI
1750 struct snd_card *card = bus->card;
1751
a65d629c
TI
1752 spin_lock(&card->files_lock);
1753 card->shutdown = 0;
1754 spin_unlock(&card->files_lock);
1755}
2698ea98 1756EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
a65d629c 1757
d5191e50
TI
1758/**
1759 * snd_hda_codec_reset - Clear all objects assigned to the codec
1760 * @codec: HD-audio codec
1761 *
1762 * This frees the all PCM and control elements assigned to the codec, and
1763 * clears the caches and restores the pin default configurations.
1764 *
1765 * When a device is being used, it returns -EBSY. If successfully freed,
1766 * returns zero.
1767 */
a65d629c
TI
1768int snd_hda_codec_reset(struct hda_codec *codec)
1769{
d3d020bd 1770 struct hda_bus *bus = codec->bus;
a65d629c 1771
d3d020bd 1772 if (snd_hda_lock_devices(bus) < 0)
a65d629c 1773 return -EBUSY;
a65d629c
TI
1774
1775 /* OK, let it free */
3256be65 1776 snd_hdac_device_unregister(&codec->core);
d8a766a1 1777
a65d629c 1778 /* allow device access again */
d3d020bd 1779 snd_hda_unlock_devices(bus);
a65d629c 1780 return 0;
6c1f45ea
TI
1781}
1782
6194b99d 1783typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
aeb4b88e
TI
1784
1785/* apply the function to all matching slave ctls in the mixer list */
1786static int map_slaves(struct hda_codec *codec, const char * const *slaves,
9322ca54 1787 const char *suffix, map_slave_func_t func, void *data)
aeb4b88e
TI
1788{
1789 struct hda_nid_item *items;
1790 const char * const *s;
1791 int i, err;
1792
1793 items = codec->mixers.list;
1794 for (i = 0; i < codec->mixers.used; i++) {
1795 struct snd_kcontrol *sctl = items[i].kctl;
ca16ec02 1796 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
aeb4b88e
TI
1797 continue;
1798 for (s = slaves; *s; s++) {
9322ca54
TI
1799 char tmpname[sizeof(sctl->id.name)];
1800 const char *name = *s;
1801 if (suffix) {
1802 snprintf(tmpname, sizeof(tmpname), "%s %s",
1803 name, suffix);
1804 name = tmpname;
1805 }
9322ca54 1806 if (!strcmp(sctl->id.name, name)) {
6194b99d 1807 err = func(codec, data, sctl);
aeb4b88e
TI
1808 if (err)
1809 return err;
1810 break;
1811 }
1812 }
1813 }
1814 return 0;
1815}
1816
6194b99d
TI
1817static int check_slave_present(struct hda_codec *codec,
1818 void *data, struct snd_kcontrol *sctl)
aeb4b88e
TI
1819{
1820 return 1;
1821}
1822
18478e8b 1823/* guess the value corresponding to 0dB */
6194b99d
TI
1824static int get_kctl_0dB_offset(struct hda_codec *codec,
1825 struct snd_kcontrol *kctl, int *step_to_check)
18478e8b
TI
1826{
1827 int _tlv[4];
1828 const int *tlv = NULL;
1829 int val = -1;
1830
1831 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1832 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1833 mm_segment_t fs = get_fs();
1834 set_fs(get_ds());
1835 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1836 tlv = _tlv;
1837 set_fs(fs);
1838 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1839 tlv = kctl->tlv.p;
a4e7a121
TI
1840 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1841 int step = tlv[3];
1842 step &= ~TLV_DB_SCALE_MUTE;
1843 if (!step)
1844 return -1;
485e3e0c 1845 if (*step_to_check && *step_to_check != step) {
6194b99d 1846 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
4e76a883 1847- *step_to_check, step);
485e3e0c
TI
1848 return -1;
1849 }
1850 *step_to_check = step;
a4e7a121
TI
1851 val = -tlv[2] / step;
1852 }
18478e8b
TI
1853 return val;
1854}
1855
1856/* call kctl->put with the given value(s) */
1857static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1858{
1859 struct snd_ctl_elem_value *ucontrol;
1860 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1861 if (!ucontrol)
1862 return -ENOMEM;
1863 ucontrol->value.integer.value[0] = val;
1864 ucontrol->value.integer.value[1] = val;
1865 kctl->put(kctl, ucontrol);
1866 kfree(ucontrol);
1867 return 0;
1868}
1869
1870/* initialize the slave volume with 0dB */
6194b99d
TI
1871static int init_slave_0dB(struct hda_codec *codec,
1872 void *data, struct snd_kcontrol *slave)
18478e8b 1873{
6194b99d 1874 int offset = get_kctl_0dB_offset(codec, slave, data);
18478e8b
TI
1875 if (offset > 0)
1876 put_kctl_with_value(slave, offset);
1877 return 0;
1878}
1879
1880/* unmute the slave */
6194b99d
TI
1881static int init_slave_unmute(struct hda_codec *codec,
1882 void *data, struct snd_kcontrol *slave)
18478e8b
TI
1883{
1884 return put_kctl_with_value(slave, 1);
1885}
1886
e8750940
TI
1887static int add_slave(struct hda_codec *codec,
1888 void *data, struct snd_kcontrol *slave)
1889{
1890 return snd_ctl_add_slave(data, slave);
1891}
1892
d5191e50 1893/**
95a962c3 1894 * __snd_hda_add_vmaster - create a virtual master control and add slaves
d5191e50
TI
1895 * @codec: HD-audio codec
1896 * @name: vmaster control name
1897 * @tlv: TLV data (optional)
1898 * @slaves: slave control names (optional)
9322ca54 1899 * @suffix: suffix string to each slave name (optional)
18478e8b 1900 * @init_slave_vol: initialize slaves to unmute/0dB
29e5853d 1901 * @ctl_ret: store the vmaster kcontrol in return
d5191e50
TI
1902 *
1903 * Create a virtual master control with the given name. The TLV data
1904 * must be either NULL or a valid data.
1905 *
1906 * @slaves is a NULL-terminated array of strings, each of which is a
1907 * slave control name. All controls with these names are assigned to
1908 * the new virtual master control.
1909 *
1910 * This function returns zero if successful or a negative error code.
1911 */
18478e8b 1912int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
9322ca54 1913 unsigned int *tlv, const char * const *slaves,
29e5853d
TI
1914 const char *suffix, bool init_slave_vol,
1915 struct snd_kcontrol **ctl_ret)
2134ea4f
TI
1916{
1917 struct snd_kcontrol *kctl;
2134ea4f
TI
1918 int err;
1919
29e5853d
TI
1920 if (ctl_ret)
1921 *ctl_ret = NULL;
1922
9322ca54 1923 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
aeb4b88e 1924 if (err != 1) {
4e76a883 1925 codec_dbg(codec, "No slave found for %s\n", name);
2f085549
TI
1926 return 0;
1927 }
2134ea4f
TI
1928 kctl = snd_ctl_make_virtual_master(name, tlv);
1929 if (!kctl)
1930 return -ENOMEM;
3911a4c1 1931 err = snd_hda_ctl_add(codec, 0, kctl);
2134ea4f
TI
1932 if (err < 0)
1933 return err;
28aedaf7 1934
e8750940 1935 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
aeb4b88e
TI
1936 if (err < 0)
1937 return err;
18478e8b
TI
1938
1939 /* init with master mute & zero volume */
1940 put_kctl_with_value(kctl, 0);
485e3e0c
TI
1941 if (init_slave_vol) {
1942 int step = 0;
18478e8b 1943 map_slaves(codec, slaves, suffix,
485e3e0c
TI
1944 tlv ? init_slave_0dB : init_slave_unmute, &step);
1945 }
18478e8b 1946
29e5853d
TI
1947 if (ctl_ret)
1948 *ctl_ret = kctl;
2134ea4f
TI
1949 return 0;
1950}
2698ea98 1951EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2134ea4f 1952
d2f344b5
TI
1953/*
1954 * mute-LED control using vmaster
1955 */
1956static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1958{
1959 static const char * const texts[] = {
c86c2d44 1960 "On", "Off", "Follow Master"
d2f344b5 1961 };
d2f344b5 1962
3ff72219 1963 return snd_ctl_enum_info(uinfo, 1, 3, texts);
d2f344b5
TI
1964}
1965
1966static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
1968{
1969 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1970 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1971 return 0;
1972}
1973
1974static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_value *ucontrol)
1976{
1977 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1978 unsigned int old_mode = hook->mute_mode;
1979
1980 hook->mute_mode = ucontrol->value.enumerated.item[0];
1981 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1982 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1983 if (old_mode == hook->mute_mode)
1984 return 0;
1985 snd_hda_sync_vmaster_hook(hook);
1986 return 1;
1987}
1988
1989static struct snd_kcontrol_new vmaster_mute_mode = {
1990 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1991 .name = "Mute-LED Mode",
1992 .info = vmaster_mute_mode_info,
1993 .get = vmaster_mute_mode_get,
1994 .put = vmaster_mute_mode_put,
1995};
1996
ee52e56e
TI
1997/* meta hook to call each driver's vmaster hook */
1998static void vmaster_hook(void *private_data, int enabled)
1999{
2000 struct hda_vmaster_mute_hook *hook = private_data;
2001
2002 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2003 enabled = hook->mute_mode;
2004 hook->hook(hook->codec, enabled);
2005}
2006
95a962c3
TI
2007/**
2008 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2009 * @codec: the HDA codec
2010 * @hook: the vmaster hook object
2011 * @expose_enum_ctl: flag to create an enum ctl
2012 *
2013 * Add a mute-LED hook with the given vmaster switch kctl.
2014 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2015 * created and associated with the given hook.
d2f344b5
TI
2016 */
2017int snd_hda_add_vmaster_hook(struct hda_codec *codec,
f29735cb
TI
2018 struct hda_vmaster_mute_hook *hook,
2019 bool expose_enum_ctl)
d2f344b5
TI
2020{
2021 struct snd_kcontrol *kctl;
2022
2023 if (!hook->hook || !hook->sw_kctl)
2024 return 0;
d2f344b5
TI
2025 hook->codec = codec;
2026 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
ee52e56e 2027 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
f29735cb
TI
2028 if (!expose_enum_ctl)
2029 return 0;
d2f344b5
TI
2030 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2031 if (!kctl)
2032 return -ENOMEM;
2033 return snd_hda_ctl_add(codec, 0, kctl);
2034}
2698ea98 2035EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
d2f344b5 2036
95a962c3
TI
2037/**
2038 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2039 * @hook: the vmaster hook
2040 *
2041 * Call the hook with the current value for synchronization.
2042 * Should be called in init callback.
d2f344b5
TI
2043 */
2044void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2045{
2046 if (!hook->hook || !hook->codec)
2047 return;
594813ff
TI
2048 /* don't call vmaster hook in the destructor since it might have
2049 * been already destroyed
2050 */
2051 if (hook->codec->bus->shutdown)
2052 return;
ee52e56e 2053 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
d2f344b5 2054}
2698ea98 2055EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
d2f344b5
TI
2056
2057
d5191e50
TI
2058/**
2059 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
a11e9b16
TI
2060 * @kcontrol: referred ctl element
2061 * @uinfo: pointer to get/store the data
d5191e50
TI
2062 *
2063 * The control element is supposed to have the private_value field
2064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2065 */
0ba21762
TI
2066int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2068{
2069 int chs = get_amp_channels(kcontrol);
2070
2071 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2072 uinfo->count = chs == 3 ? 2 : 1;
2073 uinfo->value.integer.min = 0;
2074 uinfo->value.integer.max = 1;
2075 return 0;
2076}
2698ea98 2077EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
1da177e4 2078
d5191e50
TI
2079/**
2080 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
a11e9b16
TI
2081 * @kcontrol: ctl element
2082 * @ucontrol: pointer to get/store the data
d5191e50
TI
2083 *
2084 * The control element is supposed to have the private_value field
2085 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2086 */
0ba21762
TI
2087int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2088 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2089{
2090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2091 hda_nid_t nid = get_amp_nid(kcontrol);
2092 int chs = get_amp_channels(kcontrol);
2093 int dir = get_amp_direction(kcontrol);
2094 int idx = get_amp_index(kcontrol);
2095 long *valp = ucontrol->value.integer.value;
2096
2097 if (chs & 1)
0ba21762 2098 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
47fd830a 2099 HDA_AMP_MUTE) ? 0 : 1;
1da177e4 2100 if (chs & 2)
0ba21762 2101 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
47fd830a 2102 HDA_AMP_MUTE) ? 0 : 1;
1da177e4
LT
2103 return 0;
2104}
2698ea98 2105EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
1da177e4 2106
d5191e50
TI
2107/**
2108 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
a11e9b16
TI
2109 * @kcontrol: ctl element
2110 * @ucontrol: pointer to get/store the data
d5191e50
TI
2111 *
2112 * The control element is supposed to have the private_value field
2113 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2114 */
0ba21762
TI
2115int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2117{
2118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2119 hda_nid_t nid = get_amp_nid(kcontrol);
2120 int chs = get_amp_channels(kcontrol);
2121 int dir = get_amp_direction(kcontrol);
2122 int idx = get_amp_index(kcontrol);
1da177e4
LT
2123 long *valp = ucontrol->value.integer.value;
2124 int change = 0;
2125
b9f5a89c 2126 if (chs & 1) {
eeecd9d1
TI
2127 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2128 HDA_AMP_MUTE,
2129 *valp ? 0 : HDA_AMP_MUTE);
b9f5a89c
NG
2130 valp++;
2131 }
4a19faee 2132 if (chs & 2)
eeecd9d1
TI
2133 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2134 HDA_AMP_MUTE,
2135 *valp ? 0 : HDA_AMP_MUTE);
9e5341b9 2136 hda_call_check_power_status(codec, nid);
1da177e4
LT
2137 return change;
2138}
2698ea98 2139EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
1da177e4 2140
985be54b
TI
2141/*
2142 * bound volume controls
2143 *
2144 * bind multiple volumes (# indices, from 0)
2145 */
2146
2147#define AMP_VAL_IDX_SHIFT 19
2148#define AMP_VAL_IDX_MASK (0x0f<<19)
2149
d5191e50
TI
2150/**
2151 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
a11e9b16
TI
2152 * @kcontrol: ctl element
2153 * @ucontrol: pointer to get/store the data
d5191e50
TI
2154 *
2155 * The control element is supposed to have the private_value field
2156 * set up via HDA_BIND_MUTE*() macros.
2157 */
0ba21762
TI
2158int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2160{
2161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2162 unsigned long pval;
2163 int err;
2164
5a9e02e9 2165 mutex_lock(&codec->control_mutex);
985be54b
TI
2166 pval = kcontrol->private_value;
2167 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2168 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2169 kcontrol->private_value = pval;
5a9e02e9 2170 mutex_unlock(&codec->control_mutex);
985be54b
TI
2171 return err;
2172}
2698ea98 2173EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
985be54b 2174
d5191e50
TI
2175/**
2176 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
a11e9b16
TI
2177 * @kcontrol: ctl element
2178 * @ucontrol: pointer to get/store the data
d5191e50
TI
2179 *
2180 * The control element is supposed to have the private_value field
2181 * set up via HDA_BIND_MUTE*() macros.
2182 */
0ba21762
TI
2183int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 unsigned long pval;
2188 int i, indices, err = 0, change = 0;
2189
5a9e02e9 2190 mutex_lock(&codec->control_mutex);
985be54b
TI
2191 pval = kcontrol->private_value;
2192 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2193 for (i = 0; i < indices; i++) {
0ba21762
TI
2194 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2195 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
2196 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2197 if (err < 0)
2198 break;
2199 change |= err;
2200 }
2201 kcontrol->private_value = pval;
5a9e02e9 2202 mutex_unlock(&codec->control_mutex);
985be54b
TI
2203 return err < 0 ? err : change;
2204}
2698ea98 2205EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
985be54b 2206
d5191e50
TI
2207/**
2208 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
a11e9b16
TI
2209 * @kcontrol: referred ctl element
2210 * @uinfo: pointer to get/store the data
d5191e50
TI
2211 *
2212 * The control element is supposed to have the private_value field
2213 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
532d5381
TI
2214 */
2215int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_info *uinfo)
2217{
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 struct hda_bind_ctls *c;
2220 int err;
2221
5a9e02e9 2222 mutex_lock(&codec->control_mutex);
14c65f98 2223 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2224 kcontrol->private_value = *c->values;
2225 err = c->ops->info(kcontrol, uinfo);
2226 kcontrol->private_value = (long)c;
5a9e02e9 2227 mutex_unlock(&codec->control_mutex);
532d5381
TI
2228 return err;
2229}
2698ea98 2230EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
532d5381 2231
d5191e50
TI
2232/**
2233 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
a11e9b16
TI
2234 * @kcontrol: ctl element
2235 * @ucontrol: pointer to get/store the data
d5191e50
TI
2236 *
2237 * The control element is supposed to have the private_value field
2238 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2239 */
532d5381
TI
2240int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct hda_bind_ctls *c;
2245 int err;
2246
5a9e02e9 2247 mutex_lock(&codec->control_mutex);
14c65f98 2248 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2249 kcontrol->private_value = *c->values;
2250 err = c->ops->get(kcontrol, ucontrol);
2251 kcontrol->private_value = (long)c;
5a9e02e9 2252 mutex_unlock(&codec->control_mutex);
532d5381
TI
2253 return err;
2254}
2698ea98 2255EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
532d5381 2256
d5191e50
TI
2257/**
2258 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
a11e9b16
TI
2259 * @kcontrol: ctl element
2260 * @ucontrol: pointer to get/store the data
d5191e50
TI
2261 *
2262 * The control element is supposed to have the private_value field
2263 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2264 */
532d5381
TI
2265int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
2267{
2268 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2269 struct hda_bind_ctls *c;
2270 unsigned long *vals;
2271 int err = 0, change = 0;
2272
5a9e02e9 2273 mutex_lock(&codec->control_mutex);
14c65f98 2274 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2275 for (vals = c->values; *vals; vals++) {
2276 kcontrol->private_value = *vals;
2277 err = c->ops->put(kcontrol, ucontrol);
2278 if (err < 0)
2279 break;
2280 change |= err;
2281 }
2282 kcontrol->private_value = (long)c;
5a9e02e9 2283 mutex_unlock(&codec->control_mutex);
532d5381
TI
2284 return err < 0 ? err : change;
2285}
2698ea98 2286EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
532d5381 2287
d5191e50
TI
2288/**
2289 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
a11e9b16
TI
2290 * @kcontrol: ctl element
2291 * @op_flag: operation flag
2292 * @size: byte size of input TLV
2293 * @tlv: TLV data
d5191e50
TI
2294 *
2295 * The control element is supposed to have the private_value field
2296 * set up via HDA_BIND_VOL() macro.
2297 */
532d5381
TI
2298int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2299 unsigned int size, unsigned int __user *tlv)
2300{
2301 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2302 struct hda_bind_ctls *c;
2303 int err;
2304
5a9e02e9 2305 mutex_lock(&codec->control_mutex);
14c65f98 2306 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2307 kcontrol->private_value = *c->values;
2308 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2309 kcontrol->private_value = (long)c;
5a9e02e9 2310 mutex_unlock(&codec->control_mutex);
532d5381
TI
2311 return err;
2312}
2698ea98 2313EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
532d5381
TI
2314
2315struct hda_ctl_ops snd_hda_bind_vol = {
2316 .info = snd_hda_mixer_amp_volume_info,
2317 .get = snd_hda_mixer_amp_volume_get,
2318 .put = snd_hda_mixer_amp_volume_put,
2319 .tlv = snd_hda_mixer_amp_tlv
2320};
2698ea98 2321EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
532d5381
TI
2322
2323struct hda_ctl_ops snd_hda_bind_sw = {
2324 .info = snd_hda_mixer_amp_switch_info,
2325 .get = snd_hda_mixer_amp_switch_get,
2326 .put = snd_hda_mixer_amp_switch_put,
2327 .tlv = snd_hda_mixer_amp_tlv
2328};
2698ea98 2329EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
532d5381 2330
1da177e4
LT
2331/*
2332 * SPDIF out controls
2333 */
2334
0ba21762
TI
2335static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2337{
2338 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2339 uinfo->count = 1;
2340 return 0;
2341}
2342
0ba21762
TI
2343static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2345{
2346 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2347 IEC958_AES0_NONAUDIO |
2348 IEC958_AES0_CON_EMPHASIS_5015 |
2349 IEC958_AES0_CON_NOT_COPYRIGHT;
2350 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2351 IEC958_AES1_CON_ORIGINAL;
2352 return 0;
2353}
2354
0ba21762
TI
2355static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2356 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2357{
2358 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2359 IEC958_AES0_NONAUDIO |
2360 IEC958_AES0_PRO_EMPHASIS_5015;
2361 return 0;
2362}
2363
0ba21762
TI
2364static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2366{
2367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 2368 int idx = kcontrol->private_value;
e3245cdd 2369 struct hda_spdif_out *spdif;
1da177e4 2370
e3245cdd
TI
2371 mutex_lock(&codec->spdif_mutex);
2372 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976
SW
2373 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2374 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2375 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2376 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
e3245cdd 2377 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2378
2379 return 0;
2380}
2381
2382/* convert from SPDIF status bits to HDA SPDIF bits
2383 * bit 0 (DigEn) is always set zero (to be filled later)
2384 */
2385static unsigned short convert_from_spdif_status(unsigned int sbits)
2386{
2387 unsigned short val = 0;
2388
2389 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 2390 val |= AC_DIG1_PROFESSIONAL;
1da177e4 2391 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 2392 val |= AC_DIG1_NONAUDIO;
1da177e4 2393 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
2394 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2395 IEC958_AES0_PRO_EMPHASIS_5015)
2396 val |= AC_DIG1_EMPHASIS;
1da177e4 2397 } else {
0ba21762
TI
2398 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2399 IEC958_AES0_CON_EMPHASIS_5015)
2400 val |= AC_DIG1_EMPHASIS;
2401 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2402 val |= AC_DIG1_COPYRIGHT;
1da177e4 2403 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 2404 val |= AC_DIG1_LEVEL;
1da177e4
LT
2405 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2406 }
2407 return val;
2408}
2409
2410/* convert to SPDIF status bits from HDA SPDIF bits
2411 */
2412static unsigned int convert_to_spdif_status(unsigned short val)
2413{
2414 unsigned int sbits = 0;
2415
0ba21762 2416 if (val & AC_DIG1_NONAUDIO)
1da177e4 2417 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 2418 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
2419 sbits |= IEC958_AES0_PROFESSIONAL;
2420 if (sbits & IEC958_AES0_PROFESSIONAL) {
a686fd14 2421 if (val & AC_DIG1_EMPHASIS)
1da177e4
LT
2422 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2423 } else {
0ba21762 2424 if (val & AC_DIG1_EMPHASIS)
1da177e4 2425 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 2426 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 2427 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 2428 if (val & AC_DIG1_LEVEL)
1da177e4
LT
2429 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2430 sbits |= val & (0x7f << 8);
2431 }
2432 return sbits;
2433}
2434
2f72853c
TI
2435/* set digital convert verbs both for the given NID and its slaves */
2436static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
a551d914 2437 int mask, int val)
2f72853c 2438{
dda14410 2439 const hda_nid_t *d;
2f72853c 2440
a551d914
TI
2441 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2442 mask, val);
2f72853c
TI
2443 d = codec->slave_dig_outs;
2444 if (!d)
2445 return;
2446 for (; *d; d++)
7d4b5e97 2447 snd_hdac_regmap_update(&codec->core, *d,
a551d914 2448 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2f72853c
TI
2449}
2450
2451static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2452 int dig1, int dig2)
2453{
a551d914
TI
2454 unsigned int mask = 0;
2455 unsigned int val = 0;
2456
2457 if (dig1 != -1) {
2458 mask |= 0xff;
2459 val = dig1;
2460 }
2461 if (dig2 != -1) {
2462 mask |= 0xff00;
2463 val |= dig2 << 8;
2464 }
2465 set_dig_out(codec, nid, mask, val);
2f72853c
TI
2466}
2467
0ba21762
TI
2468static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2470{
2471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 2472 int idx = kcontrol->private_value;
e3245cdd
TI
2473 struct hda_spdif_out *spdif;
2474 hda_nid_t nid;
1da177e4
LT
2475 unsigned short val;
2476 int change;
2477
62932df8 2478 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
2479 spdif = snd_array_elem(&codec->spdif_out, idx);
2480 nid = spdif->nid;
7c935976 2481 spdif->status = ucontrol->value.iec958.status[0] |
1da177e4
LT
2482 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2483 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2484 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
7c935976
SW
2485 val = convert_from_spdif_status(spdif->status);
2486 val |= spdif->ctls & 1;
2487 change = spdif->ctls != val;
2488 spdif->ctls = val;
74b654c9 2489 if (change && nid != (u16)-1)
2f72853c 2490 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
62932df8 2491 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2492 return change;
2493}
2494
a5ce8890 2495#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1da177e4 2496
0ba21762
TI
2497static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2499{
2500 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 2501 int idx = kcontrol->private_value;
e3245cdd 2502 struct hda_spdif_out *spdif;
1da177e4 2503
e3245cdd
TI
2504 mutex_lock(&codec->spdif_mutex);
2505 spdif = snd_array_elem(&codec->spdif_out, idx);
7c935976 2506 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
e3245cdd 2507 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2508 return 0;
2509}
2510
74b654c9
SW
2511static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2512 int dig1, int dig2)
2513{
2514 set_dig_out_convert(codec, nid, dig1, dig2);
2515 /* unmute amp switch (if any) */
2516 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2517 (dig1 & AC_DIG1_ENABLE))
2518 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2519 HDA_AMP_MUTE, 0);
2520}
2521
0ba21762
TI
2522static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2524{
2525 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976 2526 int idx = kcontrol->private_value;
e3245cdd
TI
2527 struct hda_spdif_out *spdif;
2528 hda_nid_t nid;
1da177e4
LT
2529 unsigned short val;
2530 int change;
2531
62932df8 2532 mutex_lock(&codec->spdif_mutex);
e3245cdd
TI
2533 spdif = snd_array_elem(&codec->spdif_out, idx);
2534 nid = spdif->nid;
7c935976 2535 val = spdif->ctls & ~AC_DIG1_ENABLE;
1da177e4 2536 if (ucontrol->value.integer.value[0])
0ba21762 2537 val |= AC_DIG1_ENABLE;
7c935976 2538 change = spdif->ctls != val;
74b654c9
SW
2539 spdif->ctls = val;
2540 if (change && nid != (u16)-1)
2541 set_spdif_ctls(codec, nid, val & 0xff, -1);
62932df8 2542 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2543 return change;
2544}
2545
c8b6bf9b 2546static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
2547 {
2548 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2549 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2550 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1da177e4
LT
2551 .info = snd_hda_spdif_mask_info,
2552 .get = snd_hda_spdif_cmask_get,
2553 },
2554 {
2555 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2557 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1da177e4
LT
2558 .info = snd_hda_spdif_mask_info,
2559 .get = snd_hda_spdif_pmask_get,
2560 },
2561 {
2562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2563 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1da177e4
LT
2564 .info = snd_hda_spdif_mask_info,
2565 .get = snd_hda_spdif_default_get,
2566 .put = snd_hda_spdif_default_put,
2567 },
2568 {
2569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2570 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1da177e4
LT
2571 .info = snd_hda_spdif_out_switch_info,
2572 .get = snd_hda_spdif_out_switch_get,
2573 .put = snd_hda_spdif_out_switch_put,
2574 },
2575 { } /* end */
2576};
2577
2578/**
dcda5806 2579 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
1da177e4 2580 * @codec: the HDA codec
dcda5806
TI
2581 * @associated_nid: NID that new ctls associated with
2582 * @cvt_nid: converter NID
2583 * @type: HDA_PCM_TYPE_*
2584 * Creates controls related with the digital output.
2585 * Called from each patch supporting the digital out.
1da177e4
LT
2586 *
2587 * Returns 0 if successful, or a negative error code.
2588 */
dcda5806
TI
2589int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2590 hda_nid_t associated_nid,
2591 hda_nid_t cvt_nid,
2592 int type)
1da177e4
LT
2593{
2594 int err;
c8b6bf9b
TI
2595 struct snd_kcontrol *kctl;
2596 struct snd_kcontrol_new *dig_mix;
ea9b43ad 2597 int idx = 0;
a551d914 2598 int val = 0;
ea9b43ad 2599 const int spdif_index = 16;
7c935976 2600 struct hda_spdif_out *spdif;
ea9b43ad 2601 struct hda_bus *bus = codec->bus;
1da177e4 2602
ea9b43ad 2603 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
dcda5806 2604 type == HDA_PCM_TYPE_SPDIF) {
ea9b43ad
TI
2605 idx = spdif_index;
2606 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
dcda5806 2607 type == HDA_PCM_TYPE_HDMI) {
ea9b43ad
TI
2608 /* suppose a single SPDIF device */
2609 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2610 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2611 if (!kctl)
2612 break;
2613 kctl->id.index = spdif_index;
dcda5806 2614 }
ea9b43ad 2615 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
dcda5806 2616 }
ea9b43ad
TI
2617 if (!bus->primary_dig_out_type)
2618 bus->primary_dig_out_type = type;
dcda5806 2619
ea9b43ad 2620 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
1afe206a 2621 if (idx < 0) {
4e76a883 2622 codec_err(codec, "too many IEC958 outputs\n");
09f99701
TI
2623 return -EBUSY;
2624 }
7c935976 2625 spdif = snd_array_new(&codec->spdif_out);
25336e8a
ML
2626 if (!spdif)
2627 return -ENOMEM;
1da177e4
LT
2628 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2629 kctl = snd_ctl_new1(dig_mix, codec);
b91f080f
TI
2630 if (!kctl)
2631 return -ENOMEM;
09f99701 2632 kctl->id.index = idx;
7c935976 2633 kctl->private_value = codec->spdif_out.used - 1;
74b654c9 2634 err = snd_hda_ctl_add(codec, associated_nid, kctl);
0ba21762 2635 if (err < 0)
1da177e4
LT
2636 return err;
2637 }
74b654c9 2638 spdif->nid = cvt_nid;
a551d914
TI
2639 snd_hdac_regmap_read(&codec->core, cvt_nid,
2640 AC_VERB_GET_DIGI_CONVERT_1, &val);
2641 spdif->ctls = val;
7c935976 2642 spdif->status = convert_to_spdif_status(spdif->ctls);
1da177e4
LT
2643 return 0;
2644}
2698ea98 2645EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
1da177e4 2646
95a962c3
TI
2647/**
2648 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2649 * @codec: the HDA codec
2650 * @nid: widget NID
2651 *
e3245cdd
TI
2652 * call within spdif_mutex lock
2653 */
7c935976
SW
2654struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2655 hda_nid_t nid)
2656{
2657 int i;
2658 for (i = 0; i < codec->spdif_out.used; i++) {
2659 struct hda_spdif_out *spdif =
2660 snd_array_elem(&codec->spdif_out, i);
2661 if (spdif->nid == nid)
2662 return spdif;
2663 }
2664 return NULL;
2665}
2698ea98 2666EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
7c935976 2667
95a962c3
TI
2668/**
2669 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2670 * @codec: the HDA codec
2671 * @idx: the SPDIF ctl index
2672 *
2673 * Unassign the widget from the given SPDIF control.
2674 */
74b654c9
SW
2675void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2676{
e3245cdd 2677 struct hda_spdif_out *spdif;
74b654c9
SW
2678
2679 mutex_lock(&codec->spdif_mutex);
e3245cdd 2680 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
2681 spdif->nid = (u16)-1;
2682 mutex_unlock(&codec->spdif_mutex);
2683}
2698ea98 2684EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
74b654c9 2685
95a962c3
TI
2686/**
2687 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2688 * @codec: the HDA codec
2689 * @idx: the SPDIF ctl idx
2690 * @nid: widget NID
2691 *
2692 * Assign the widget to the SPDIF control with the given index.
2693 */
74b654c9
SW
2694void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2695{
e3245cdd 2696 struct hda_spdif_out *spdif;
74b654c9
SW
2697 unsigned short val;
2698
2699 mutex_lock(&codec->spdif_mutex);
e3245cdd 2700 spdif = snd_array_elem(&codec->spdif_out, idx);
74b654c9
SW
2701 if (spdif->nid != nid) {
2702 spdif->nid = nid;
2703 val = spdif->ctls;
2704 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2705 }
2706 mutex_unlock(&codec->spdif_mutex);
2707}
2698ea98 2708EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
74b654c9 2709
9a08160b
TI
2710/*
2711 * SPDIF sharing with analog output
2712 */
2713static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_value *ucontrol)
2715{
2716 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2717 ucontrol->value.integer.value[0] = mout->share_spdif;
2718 return 0;
2719}
2720
2721static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_value *ucontrol)
2723{
2724 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2725 mout->share_spdif = !!ucontrol->value.integer.value[0];
2726 return 0;
2727}
2728
2729static struct snd_kcontrol_new spdif_share_sw = {
2730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2731 .name = "IEC958 Default PCM Playback Switch",
2732 .info = snd_ctl_boolean_mono_info,
2733 .get = spdif_share_sw_get,
2734 .put = spdif_share_sw_put,
2735};
2736
d5191e50
TI
2737/**
2738 * snd_hda_create_spdif_share_sw - create Default PCM switch
2739 * @codec: the HDA codec
2740 * @mout: multi-out instance
2741 */
9a08160b
TI
2742int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2743 struct hda_multi_out *mout)
2744{
4c7a548a
ML
2745 struct snd_kcontrol *kctl;
2746
9a08160b
TI
2747 if (!mout->dig_out_nid)
2748 return 0;
4c7a548a
ML
2749
2750 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2751 if (!kctl)
2752 return -ENOMEM;
9a08160b 2753 /* ATTENTION: here mout is passed as private_data, instead of codec */
4c7a548a 2754 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
9a08160b 2755}
2698ea98 2756EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
9a08160b 2757
1da177e4
LT
2758/*
2759 * SPDIF input
2760 */
2761
2762#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2763
0ba21762
TI
2764static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768
2769 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2770 return 0;
2771}
2772
0ba21762
TI
2773static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2775{
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 hda_nid_t nid = kcontrol->private_value;
2778 unsigned int val = !!ucontrol->value.integer.value[0];
2779 int change;
2780
62932df8 2781 mutex_lock(&codec->spdif_mutex);
1da177e4 2782 change = codec->spdif_in_enable != val;
82beb8fd 2783 if (change) {
1da177e4 2784 codec->spdif_in_enable = val;
a551d914
TI
2785 snd_hdac_regmap_write(&codec->core, nid,
2786 AC_VERB_SET_DIGI_CONVERT_1, val);
1da177e4 2787 }
62932df8 2788 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2789 return change;
2790}
2791
0ba21762
TI
2792static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2794{
2795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2796 hda_nid_t nid = kcontrol->private_value;
a551d914 2797 unsigned int val;
1da177e4
LT
2798 unsigned int sbits;
2799
a551d914
TI
2800 snd_hdac_regmap_read(&codec->core, nid,
2801 AC_VERB_GET_DIGI_CONVERT_1, &val);
1da177e4
LT
2802 sbits = convert_to_spdif_status(val);
2803 ucontrol->value.iec958.status[0] = sbits;
2804 ucontrol->value.iec958.status[1] = sbits >> 8;
2805 ucontrol->value.iec958.status[2] = sbits >> 16;
2806 ucontrol->value.iec958.status[3] = sbits >> 24;
2807 return 0;
2808}
2809
c8b6bf9b 2810static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
2811 {
2812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2813 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
1da177e4
LT
2814 .info = snd_hda_spdif_in_switch_info,
2815 .get = snd_hda_spdif_in_switch_get,
2816 .put = snd_hda_spdif_in_switch_put,
2817 },
2818 {
2819 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2820 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2821 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
1da177e4
LT
2822 .info = snd_hda_spdif_mask_info,
2823 .get = snd_hda_spdif_in_status_get,
2824 },
2825 { } /* end */
2826};
2827
2828/**
2829 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2830 * @codec: the HDA codec
2831 * @nid: audio in widget NID
2832 *
2833 * Creates controls related with the SPDIF input.
2834 * Called from each patch supporting the SPDIF in.
2835 *
2836 * Returns 0 if successful, or a negative error code.
2837 */
12f288bf 2838int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
2839{
2840 int err;
c8b6bf9b
TI
2841 struct snd_kcontrol *kctl;
2842 struct snd_kcontrol_new *dig_mix;
09f99701 2843 int idx;
1da177e4 2844
dcda5806 2845 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
1afe206a 2846 if (idx < 0) {
4e76a883 2847 codec_err(codec, "too many IEC958 inputs\n");
09f99701
TI
2848 return -EBUSY;
2849 }
1da177e4
LT
2850 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2851 kctl = snd_ctl_new1(dig_mix, codec);
c8dcdf82
TI
2852 if (!kctl)
2853 return -ENOMEM;
1da177e4 2854 kctl->private_value = nid;
3911a4c1 2855 err = snd_hda_ctl_add(codec, nid, kctl);
0ba21762 2856 if (err < 0)
1da177e4
LT
2857 return err;
2858 }
0ba21762 2859 codec->spdif_in_enable =
3982d17e
AP
2860 snd_hda_codec_read(codec, nid, 0,
2861 AC_VERB_GET_DIGI_CONVERT_1, 0) &
0ba21762 2862 AC_DIG1_ENABLE;
1da177e4
LT
2863 return 0;
2864}
2698ea98 2865EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
1da177e4 2866
95a962c3
TI
2867/**
2868 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2869 * @codec: the HDA codec
2870 * @fg: function group (not used now)
2871 * @power_state: the power state to set (AC_PWRST_*)
2872 *
2873 * Set the given power state to all widgets that have the power control.
2874 * If the codec has power_filter set, it evaluates the power state and
2875 * filter out if it's unchanged as D3.
2876 */
4d7fbdbc 2877void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
9419ab6b 2878 unsigned int power_state)
54d17403 2879{
7639a06c 2880 hda_nid_t nid;
54d17403 2881
7639a06c 2882 for_each_hda_codec_node(nid, codec) {
7eba5c9d 2883 unsigned int wcaps = get_wcaps(codec, nid);
9419ab6b 2884 unsigned int state = power_state;
4d7fbdbc
TI
2885 if (!(wcaps & AC_WCAP_POWER))
2886 continue;
9419ab6b
TI
2887 if (codec->power_filter) {
2888 state = codec->power_filter(codec, nid, power_state);
2889 if (state != power_state && power_state == AC_PWRST_D3)
4d7fbdbc 2890 continue;
1194b5b7 2891 }
4d7fbdbc 2892 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
9419ab6b 2893 state);
54d17403 2894 }
cb53c626 2895}
2698ea98 2896EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
4d7fbdbc 2897
432c641e
TI
2898/*
2899 * wait until the state is reached, returns the current state
2900 */
2901static unsigned int hda_sync_power_state(struct hda_codec *codec,
2902 hda_nid_t fg,
2903 unsigned int power_state)
2904{
2905 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2906 unsigned int state, actual_state;
2907
2908 for (;;) {
2909 state = snd_hda_codec_read(codec, fg, 0,
2910 AC_VERB_GET_POWER_STATE, 0);
2911 if (state & AC_PWRST_ERROR)
2912 break;
2913 actual_state = (state >> 4) & 0x0f;
2914 if (actual_state == power_state)
2915 break;
2916 if (time_after_eq(jiffies, end_time))
2917 break;
2918 /* wait until the codec reachs to the target state */
2919 msleep(1);
2920 }
2921 return state;
2922}
2923
95a962c3
TI
2924/**
2925 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2926 * @codec: the HDA codec
2927 * @nid: widget NID
2928 * @power_state: power state to evalue
2929 *
2930 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2931 * This can be used a codec power_filter callback.
2932 */
ba615b86
TI
2933unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2934 hda_nid_t nid,
2935 unsigned int power_state)
9419ab6b 2936{
7639a06c 2937 if (nid == codec->core.afg || nid == codec->core.mfg)
dfc6e469 2938 return power_state;
9419ab6b
TI
2939 if (power_state == AC_PWRST_D3 &&
2940 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2941 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2942 int eapd = snd_hda_codec_read(codec, nid, 0,
2943 AC_VERB_GET_EAPD_BTLENABLE, 0);
2944 if (eapd & 0x02)
2945 return AC_PWRST_D0;
2946 }
2947 return power_state;
2948}
2698ea98 2949EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
9419ab6b 2950
4d7fbdbc 2951/*
08fa20ae 2952 * set power state of the codec, and return the power state
4d7fbdbc 2953 */
d819387e 2954static unsigned int hda_set_power_state(struct hda_codec *codec,
08fa20ae 2955 unsigned int power_state)
4d7fbdbc 2956{
7639a06c 2957 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
09617ce4
WX
2958 int count;
2959 unsigned int state;
63e51fd7 2960 int flags = 0;
09617ce4 2961
4d7fbdbc 2962 /* this delay seems necessary to avoid click noise at power-down */
0f4ccbb0 2963 if (power_state == AC_PWRST_D3) {
7f132927 2964 if (codec->depop_delay < 0)
7639a06c 2965 msleep(codec_has_epss(codec) ? 10 : 100);
7f132927
ML
2966 else if (codec->depop_delay > 0)
2967 msleep(codec->depop_delay);
63e51fd7 2968 flags = HDA_RW_NO_RESPONSE_FALLBACK;
0f4ccbb0 2969 }
09617ce4
WX
2970
2971 /* repeat power states setting at most 10 times*/
2972 for (count = 0; count < 10; count++) {
432c641e
TI
2973 if (codec->patch_ops.set_power_state)
2974 codec->patch_ops.set_power_state(codec, fg,
2975 power_state);
2976 else {
dfc6e469
TI
2977 state = power_state;
2978 if (codec->power_filter)
2979 state = codec->power_filter(codec, fg, state);
2980 if (state == power_state || power_state != AC_PWRST_D3)
2981 snd_hda_codec_read(codec, fg, flags,
2982 AC_VERB_SET_POWER_STATE,
2983 state);
9419ab6b 2984 snd_hda_codec_set_power_to_all(codec, fg, power_state);
432c641e
TI
2985 }
2986 state = hda_sync_power_state(codec, fg, power_state);
09617ce4
WX
2987 if (!(state & AC_PWRST_ERROR))
2988 break;
2989 }
b8dfc462 2990
08fa20ae 2991 return state;
4d7fbdbc 2992}
cb53c626 2993
b9c590bb
TI
2994/* sync power states of all widgets;
2995 * this is called at the end of codec parsing
2996 */
2997static void sync_power_up_states(struct hda_codec *codec)
2998{
7639a06c 2999 hda_nid_t nid;
b9c590bb 3000
ba615b86
TI
3001 /* don't care if no filter is used */
3002 if (!codec->power_filter)
b9c590bb
TI
3003 return;
3004
7639a06c 3005 for_each_hda_codec_node(nid, codec) {
b9c590bb 3006 unsigned int wcaps = get_wcaps(codec, nid);
9040d102 3007 unsigned int target;
b9c590bb
TI
3008 if (!(wcaps & AC_WCAP_POWER))
3009 continue;
3010 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3011 if (target == AC_PWRST_D0)
3012 continue;
9040d102 3013 if (!snd_hda_check_power_state(codec, nid, target))
b9c590bb
TI
3014 snd_hda_codec_write(codec, nid, 0,
3015 AC_VERB_SET_POWER_STATE, target);
3016 }
3017}
3018
648a8d27 3019#ifdef CONFIG_SND_HDA_RECONFIG
11aeff08
TI
3020/* execute additional init verbs */
3021static void hda_exec_init_verbs(struct hda_codec *codec)
3022{
3023 if (codec->init_verbs.list)
3024 snd_hda_sequence_write(codec, codec->init_verbs.list);
3025}
3026#else
3027static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3028#endif
3029
2a43952a 3030#ifdef CONFIG_PM
cc72da7d
TI
3031/* update the power on/off account with the current jiffies */
3032static void update_power_acct(struct hda_codec *codec, bool on)
3033{
3034 unsigned long delta = jiffies - codec->power_jiffies;
3035
3036 if (on)
3037 codec->power_on_acct += delta;
3038 else
3039 codec->power_off_acct += delta;
3040 codec->power_jiffies += delta;
3041}
3042
3043void snd_hda_update_power_acct(struct hda_codec *codec)
3044{
3045 update_power_acct(codec, hda_codec_is_power_on(codec));
3046}
3047
cb53c626
TI
3048/*
3049 * call suspend and power-down; used both from PM and power-save
08fa20ae 3050 * this function returns the power state in the end
cb53c626 3051 */
cc72da7d 3052static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
cb53c626 3053{
08fa20ae
TI
3054 unsigned int state;
3055
7639a06c 3056 atomic_inc(&codec->core.in_pm);
989c3187 3057
cb53c626 3058 if (codec->patch_ops.suspend)
68cb2b55 3059 codec->patch_ops.suspend(codec);
eb541337 3060 hda_cleanup_all_streams(codec);
d819387e 3061 state = hda_set_power_state(codec, AC_PWRST_D3);
cc72da7d 3062 update_power_acct(codec, true);
7639a06c 3063 atomic_dec(&codec->core.in_pm);
08fa20ae 3064 return state;
54d17403
TI
3065}
3066
cb53c626
TI
3067/*
3068 * kick up codec; used both from PM and power-save
3069 */
3070static void hda_call_codec_resume(struct hda_codec *codec)
3071{
7639a06c 3072 atomic_inc(&codec->core.in_pm);
989c3187 3073
eeecd9d1
TI
3074 if (codec->core.regmap)
3075 regcache_mark_dirty(codec->core.regmap);
3076
cc72da7d 3077 codec->power_jiffies = jiffies;
cc72da7d 3078
d819387e 3079 hda_set_power_state(codec, AC_PWRST_D0);
ac0547dc 3080 restore_shutup_pins(codec);
11aeff08 3081 hda_exec_init_verbs(codec);
31614bb8 3082 snd_hda_jack_set_dirty_all(codec);
cb53c626
TI
3083 if (codec->patch_ops.resume)
3084 codec->patch_ops.resume(codec);
3085 else {
9d99f312
TI
3086 if (codec->patch_ops.init)
3087 codec->patch_ops.init(codec);
eeecd9d1
TI
3088 if (codec->core.regmap)
3089 regcache_sync(codec->core.regmap);
cb53c626 3090 }
26a6cb6c
DH
3091
3092 if (codec->jackpoll_interval)
3093 hda_jackpoll_work(&codec->jackpoll_work.work);
31614bb8 3094 else
26a6cb6c 3095 snd_hda_jack_report_sync(codec);
7639a06c 3096 atomic_dec(&codec->core.in_pm);
cb53c626 3097}
59ed1ead 3098
cc72da7d 3099static int hda_codec_runtime_suspend(struct device *dev)
59ed1ead
TI
3100{
3101 struct hda_codec *codec = dev_to_hda_codec(dev);
bbbc7e85 3102 struct hda_pcm *pcm;
cc72da7d 3103 unsigned int state;
59ed1ead
TI
3104
3105 cancel_delayed_work_sync(&codec->jackpoll_work);
bbbc7e85
TI
3106 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3107 snd_pcm_suspend_all(pcm->pcm);
cc72da7d 3108 state = hda_call_codec_suspend(codec);
7639a06c
TI
3109 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3110 (state & AC_PWRST_CLK_STOP_OK))
3111 snd_hdac_codec_link_down(&codec->core);
a5e7e07c 3112 snd_hdac_link_power(&codec->core, false);
59ed1ead
TI
3113 return 0;
3114}
3115
cc72da7d 3116static int hda_codec_runtime_resume(struct device *dev)
59ed1ead 3117{
55ed9cd1
TI
3118 struct hda_codec *codec = dev_to_hda_codec(dev);
3119
a5e7e07c 3120 snd_hdac_link_power(&codec->core, true);
7639a06c 3121 snd_hdac_codec_link_up(&codec->core);
55ed9cd1 3122 hda_call_codec_resume(codec);
cc72da7d 3123 pm_runtime_mark_last_busy(dev);
59ed1ead
TI
3124 return 0;
3125}
2a43952a 3126#endif /* CONFIG_PM */
cb53c626 3127
59ed1ead
TI
3128/* referred in hda_bind.c */
3129const struct dev_pm_ops hda_codec_driver_pm = {
cc72da7d
TI
3130 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3131 pm_runtime_force_resume)
3132 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3133 NULL)
59ed1ead 3134};
54d17403 3135
9c9a5175
TI
3136/*
3137 * add standard channel maps if not specified
3138 */
3139static int add_std_chmaps(struct hda_codec *codec)
3140{
bbbc7e85
TI
3141 struct hda_pcm *pcm;
3142 int str, err;
9c9a5175 3143
bbbc7e85 3144 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
9c9a5175 3145 for (str = 0; str < 2; str++) {
bbbc7e85 3146 struct hda_pcm_stream *hinfo = &pcm->stream[str];
9c9a5175 3147 struct snd_pcm_chmap *chmap;
ee81abb6 3148 const struct snd_pcm_chmap_elem *elem;
9c9a5175 3149
751e2216
SM
3150 if (!pcm || pcm->own_chmap ||
3151 !hinfo->substreams)
9c9a5175 3152 continue;
ee81abb6 3153 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
bbbc7e85 3154 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
9c9a5175
TI
3155 hinfo->channels_max,
3156 0, &chmap);
3157 if (err < 0)
3158 return err;
3159 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3160 }
3161 }
3162 return 0;
3163}
3164
ee81abb6
TI
3165/* default channel maps for 2.1 speakers;
3166 * since HD-audio supports only stereo, odd number channels are omitted
3167 */
3168const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3169 { .channels = 2,
3170 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3171 { .channels = 4,
3172 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3173 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3174 { }
3175};
3176EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3177
6c1f45ea
TI
3178int snd_hda_codec_build_controls(struct hda_codec *codec)
3179{
3180 int err = 0;
11aeff08 3181 hda_exec_init_verbs(codec);
6c1f45ea
TI
3182 /* continue to initialize... */
3183 if (codec->patch_ops.init)
3184 err = codec->patch_ops.init(codec);
3185 if (!err && codec->patch_ops.build_controls)
3186 err = codec->patch_ops.build_controls(codec);
6c1f45ea
TI
3187 if (err < 0)
3188 return err;
9c9a5175
TI
3189
3190 /* we create chmaps here instead of build_pcms */
3191 err = add_std_chmaps(codec);
3192 if (err < 0)
3193 return err;
3194
26a6cb6c
DH
3195 if (codec->jackpoll_interval)
3196 hda_jackpoll_work(&codec->jackpoll_work.work);
3197 else
3198 snd_hda_jack_report_sync(codec); /* call at the last init point */
b9c590bb 3199 sync_power_up_states(codec);
1da177e4
LT
3200 return 0;
3201}
3202
1da177e4
LT
3203/*
3204 * PCM stuff
3205 */
3206static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3207 struct hda_codec *codec,
c8b6bf9b 3208 struct snd_pcm_substream *substream)
1da177e4
LT
3209{
3210 return 0;
3211}
3212
3213static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3214 struct hda_codec *codec,
3215 unsigned int stream_tag,
3216 unsigned int format,
c8b6bf9b 3217 struct snd_pcm_substream *substream)
1da177e4
LT
3218{
3219 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3220 return 0;
3221}
3222
3223static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3224 struct hda_codec *codec,
c8b6bf9b 3225 struct snd_pcm_substream *substream)
1da177e4 3226{
888afa15 3227 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1da177e4
LT
3228 return 0;
3229}
3230
6c1f45ea
TI
3231static int set_pcm_default_values(struct hda_codec *codec,
3232 struct hda_pcm_stream *info)
1da177e4 3233{
ee504710
JK
3234 int err;
3235
0ba21762
TI
3236 /* query support PCM information from the given NID */
3237 if (info->nid && (!info->rates || !info->formats)) {
ee504710 3238 err = snd_hda_query_supported_pcm(codec, info->nid,
0ba21762
TI
3239 info->rates ? NULL : &info->rates,
3240 info->formats ? NULL : &info->formats,
3241 info->maxbps ? NULL : &info->maxbps);
ee504710
JK
3242 if (err < 0)
3243 return err;
1da177e4
LT
3244 }
3245 if (info->ops.open == NULL)
3246 info->ops.open = hda_pcm_default_open_close;
3247 if (info->ops.close == NULL)
3248 info->ops.close = hda_pcm_default_open_close;
3249 if (info->ops.prepare == NULL) {
da3cec35
TI
3250 if (snd_BUG_ON(!info->nid))
3251 return -EINVAL;
1da177e4
LT
3252 info->ops.prepare = hda_pcm_default_prepare;
3253 }
1da177e4 3254 if (info->ops.cleanup == NULL) {
da3cec35
TI
3255 if (snd_BUG_ON(!info->nid))
3256 return -EINVAL;
1da177e4
LT
3257 info->ops.cleanup = hda_pcm_default_cleanup;
3258 }
3259 return 0;
3260}
3261
eb541337
TI
3262/*
3263 * codec prepare/cleanup entries
3264 */
95a962c3
TI
3265/**
3266 * snd_hda_codec_prepare - Prepare a stream
3267 * @codec: the HDA codec
3268 * @hinfo: PCM information
3269 * @stream: stream tag to assign
3270 * @format: format id to assign
3271 * @substream: PCM substream to assign
3272 *
3273 * Calls the prepare callback set by the codec with the given arguments.
3274 * Clean up the inactive streams when successful.
3275 */
eb541337
TI
3276int snd_hda_codec_prepare(struct hda_codec *codec,
3277 struct hda_pcm_stream *hinfo,
3278 unsigned int stream,
3279 unsigned int format,
3280 struct snd_pcm_substream *substream)
3281{
3282 int ret;
3f50ac6a 3283 mutex_lock(&codec->bus->prepare_mutex);
61ca4107
TI
3284 if (hinfo->ops.prepare)
3285 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3286 substream);
3287 else
3288 ret = -ENODEV;
eb541337
TI
3289 if (ret >= 0)
3290 purify_inactive_streams(codec);
3f50ac6a 3291 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
3292 return ret;
3293}
2698ea98 3294EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
eb541337 3295
95a962c3
TI
3296/**
3297 * snd_hda_codec_cleanup - Prepare a stream
3298 * @codec: the HDA codec
3299 * @hinfo: PCM information
3300 * @substream: PCM substream
3301 *
3302 * Calls the cleanup callback set by the codec with the given arguments.
3303 */
eb541337
TI
3304void snd_hda_codec_cleanup(struct hda_codec *codec,
3305 struct hda_pcm_stream *hinfo,
3306 struct snd_pcm_substream *substream)
3307{
3f50ac6a 3308 mutex_lock(&codec->bus->prepare_mutex);
61ca4107
TI
3309 if (hinfo->ops.cleanup)
3310 hinfo->ops.cleanup(hinfo, codec, substream);
3f50ac6a 3311 mutex_unlock(&codec->bus->prepare_mutex);
eb541337 3312}
2698ea98 3313EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
eb541337 3314
d5191e50 3315/* global */
e3303235
JK
3316const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3317 "Audio", "SPDIF", "HDMI", "Modem"
3318};
3319
529bd6c4
TI
3320/*
3321 * get the empty PCM device number to assign
3322 */
36bb00d4 3323static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
529bd6c4 3324{
f5d6def5 3325 /* audio device indices; not linear to keep compatibility */
36bb00d4
TI
3326 /* assigned to static slots up to dev#10; if more needed, assign
3327 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3328 */
f5d6def5
WF
3329 static int audio_idx[HDA_PCM_NTYPES][5] = {
3330 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3331 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
92608bad 3332 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
f5d6def5 3333 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
529bd6c4 3334 };
f5d6def5
WF
3335 int i;
3336
3337 if (type >= HDA_PCM_NTYPES) {
4e76a883 3338 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
529bd6c4
TI
3339 return -EINVAL;
3340 }
f5d6def5 3341
36bb00d4
TI
3342 for (i = 0; audio_idx[type][i] >= 0; i++) {
3343#ifndef CONFIG_SND_DYNAMIC_MINORS
3344 if (audio_idx[type][i] >= 8)
3345 break;
3346#endif
f5d6def5
WF
3347 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3348 return audio_idx[type][i];
36bb00d4 3349 }
f5d6def5 3350
36bb00d4 3351#ifdef CONFIG_SND_DYNAMIC_MINORS
01b65bfb
TI
3352 /* non-fixed slots starting from 10 */
3353 for (i = 10; i < 32; i++) {
3354 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3355 return i;
3356 }
36bb00d4 3357#endif
01b65bfb 3358
4e76a883 3359 dev_warn(bus->card->dev, "Too many %s devices\n",
28aedaf7 3360 snd_hda_pcm_type_name[type]);
36bb00d4 3361#ifndef CONFIG_SND_DYNAMIC_MINORS
4e76a883
TI
3362 dev_warn(bus->card->dev,
3363 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
36bb00d4 3364#endif
f5d6def5 3365 return -EAGAIN;
529bd6c4
TI
3366}
3367
1a4ba30c
TI
3368/* call build_pcms ops of the given codec and set up the default parameters */
3369int snd_hda_codec_parse_pcms(struct hda_codec *codec)
176d5335 3370{
bbbc7e85 3371 struct hda_pcm *cpcm;
1a4ba30c 3372 int err;
176d5335 3373
bbbc7e85 3374 if (!list_empty(&codec->pcm_list_head))
1a4ba30c
TI
3375 return 0; /* already parsed */
3376
3377 if (!codec->patch_ops.build_pcms)
3378 return 0;
3379
3380 err = codec->patch_ops.build_pcms(codec);
3381 if (err < 0) {
3382 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
d068ebc2 3383 codec->core.addr, err);
1a4ba30c
TI
3384 return err;
3385 }
3386
bbbc7e85 3387 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1a4ba30c
TI
3388 int stream;
3389
3390 for (stream = 0; stream < 2; stream++) {
3391 struct hda_pcm_stream *info = &cpcm->stream[stream];
3392
3393 if (!info->substreams)
3394 continue;
176d5335 3395 err = set_pcm_default_values(codec, info);
1a4ba30c
TI
3396 if (err < 0) {
3397 codec_warn(codec,
3398 "fail to setup default for PCM %s\n",
3399 cpcm->name);
176d5335 3400 return err;
1a4ba30c 3401 }
176d5335
TI
3402 }
3403 }
1a4ba30c
TI
3404
3405 return 0;
176d5335
TI
3406}
3407
529bd6c4
TI
3408/* assign all PCMs of the given codec */
3409int snd_hda_codec_build_pcms(struct hda_codec *codec)
3410{
1a4ba30c 3411 struct hda_bus *bus = codec->bus;
bbbc7e85 3412 struct hda_pcm *cpcm;
1a4ba30c 3413 int dev, err;
529bd6c4 3414
1a4ba30c
TI
3415 err = snd_hda_codec_parse_pcms(codec);
3416 if (err < 0) {
3417 snd_hda_codec_reset(codec);
3418 return err;
529bd6c4 3419 }
1a4ba30c
TI
3420
3421 /* attach a new PCM streams */
bbbc7e85 3422 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1a4ba30c
TI
3423 if (cpcm->pcm)
3424 continue; /* already attached */
529bd6c4 3425 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
41b5b01a 3426 continue; /* no substreams assigned */
529bd6c4 3427
1a4ba30c
TI
3428 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3429 if (dev < 0)
3430 continue; /* no fatal error */
3431 cpcm->device = dev;
0a50575b 3432 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
1a4ba30c
TI
3433 if (err < 0) {
3434 codec_err(codec,
3435 "cannot attach PCM stream %d for codec #%d\n",
d068ebc2 3436 dev, codec->core.addr);
1a4ba30c 3437 continue; /* no fatal error */
529bd6c4
TI
3438 }
3439 }
1a4ba30c 3440
529bd6c4
TI
3441 return 0;
3442}
3443
1da177e4
LT
3444/**
3445 * snd_hda_add_new_ctls - create controls from the array
3446 * @codec: the HDA codec
c8b6bf9b 3447 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
3448 *
3449 * This helper function creates and add new controls in the given array.
3450 * The array must be terminated with an empty entry as terminator.
3451 *
3452 * Returns 0 if successful, or a negative error code.
3453 */
031024ee
TI
3454int snd_hda_add_new_ctls(struct hda_codec *codec,
3455 const struct snd_kcontrol_new *knew)
1da177e4 3456{
4d02d1b6 3457 int err;
1da177e4
LT
3458
3459 for (; knew->name; knew++) {
54d17403 3460 struct snd_kcontrol *kctl;
1afe206a 3461 int addr = 0, idx = 0;
5b0cb1d8
JK
3462 if (knew->iface == -1) /* skip this codec private value */
3463 continue;
1afe206a 3464 for (;;) {
54d17403 3465 kctl = snd_ctl_new1(knew, codec);
0ba21762 3466 if (!kctl)
54d17403 3467 return -ENOMEM;
1afe206a
TI
3468 if (addr > 0)
3469 kctl->id.device = addr;
3470 if (idx > 0)
3471 kctl->id.index = idx;
3911a4c1 3472 err = snd_hda_ctl_add(codec, 0, kctl);
1afe206a
TI
3473 if (!err)
3474 break;
3475 /* try first with another device index corresponding to
3476 * the codec addr; if it still fails (or it's the
3477 * primary codec), then try another control index
3478 */
d068ebc2
TI
3479 if (!addr && codec->core.addr)
3480 addr = codec->core.addr;
1afe206a
TI
3481 else if (!idx && !knew->index) {
3482 idx = find_empty_mixer_ctl_idx(codec,
dcda5806 3483 knew->name, 0);
1afe206a
TI
3484 if (idx <= 0)
3485 return err;
3486 } else
54d17403
TI
3487 return err;
3488 }
1da177e4
LT
3489 }
3490 return 0;
3491}
2698ea98 3492EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
1da177e4 3493
83012a7c 3494#ifdef CONFIG_PM
bb573928 3495static void codec_set_power_save(struct hda_codec *codec, int delay)
cb53c626 3496{
cc72da7d 3497 struct device *dev = hda_codec_dev(codec);
cc72da7d 3498
cc72da7d
TI
3499 if (delay > 0) {
3500 pm_runtime_set_autosuspend_delay(dev, delay);
3501 pm_runtime_use_autosuspend(dev);
3502 pm_runtime_allow(dev);
3503 if (!pm_runtime_suspended(dev))
3504 pm_runtime_mark_last_busy(dev);
3505 } else {
3506 pm_runtime_dont_use_autosuspend(dev);
3507 pm_runtime_forbid(dev);
3508 }
cb53c626 3509}
bb573928
TI
3510
3511/**
3512 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3513 * @bus: HD-audio bus
3514 * @delay: autosuspend delay in msec, 0 = off
3515 *
3516 * Synchronize the runtime PM autosuspend state from the power_save option.
3517 */
3518void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3519{
3520 struct hda_codec *c;
3521
d068ebc2 3522 list_for_each_codec(c, bus)
bb573928
TI
3523 codec_set_power_save(c, delay);
3524}
3525EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
cb53c626 3526
d5191e50
TI
3527/**
3528 * snd_hda_check_amp_list_power - Check the amp list and update the power
3529 * @codec: HD-audio codec
3530 * @check: the object containing an AMP list and the status
3531 * @nid: NID to check / update
3532 *
3533 * Check whether the given NID is in the amp list. If it's in the list,
3534 * check the current AMP status, and update the the power-status according
3535 * to the mute status.
3536 *
3537 * This function is supposed to be set or called from the check_power_status
3538 * patch ops.
28aedaf7 3539 */
cb53c626
TI
3540int snd_hda_check_amp_list_power(struct hda_codec *codec,
3541 struct hda_loopback_check *check,
3542 hda_nid_t nid)
3543{
031024ee 3544 const struct hda_amp_list *p;
cb53c626
TI
3545 int ch, v;
3546
3547 if (!check->amplist)
3548 return 0;
3549 for (p = check->amplist; p->nid; p++) {
3550 if (p->nid == nid)
3551 break;
3552 }
3553 if (!p->nid)
3554 return 0; /* nothing changed */
3555
3556 for (p = check->amplist; p->nid; p++) {
3557 for (ch = 0; ch < 2; ch++) {
3558 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3559 p->idx);
3560 if (!(v & HDA_AMP_MUTE) && v > 0) {
3561 if (!check->power_on) {
3562 check->power_on = 1;
664c7155 3563 snd_hda_power_up_pm(codec);
cb53c626
TI
3564 }
3565 return 1;
3566 }
3567 }
3568 }
3569 if (check->power_on) {
3570 check->power_on = 0;
664c7155 3571 snd_hda_power_down_pm(codec);
cb53c626
TI
3572 }
3573 return 0;
3574}
2698ea98 3575EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
cb53c626 3576#endif
1da177e4
LT
3577
3578/*
3579 * input MUX helper
3580 */
d5191e50
TI
3581
3582/**
3583 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
a11e9b16
TI
3584 * @imux: imux helper object
3585 * @uinfo: pointer to get/store the data
d5191e50 3586 */
0ba21762
TI
3587int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3588 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
3589{
3590 unsigned int index;
3591
3592 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3593 uinfo->count = 1;
3594 uinfo->value.enumerated.items = imux->num_items;
5513b0c5
TI
3595 if (!imux->num_items)
3596 return 0;
1da177e4
LT
3597 index = uinfo->value.enumerated.item;
3598 if (index >= imux->num_items)
3599 index = imux->num_items - 1;
3600 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3601 return 0;
3602}
2698ea98 3603EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
1da177e4 3604
d5191e50
TI
3605/**
3606 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
a11e9b16
TI
3607 * @codec: the HDA codec
3608 * @imux: imux helper object
3609 * @ucontrol: pointer to get/store the data
3610 * @nid: input mux NID
3611 * @cur_val: pointer to get/store the current imux value
d5191e50 3612 */
0ba21762
TI
3613int snd_hda_input_mux_put(struct hda_codec *codec,
3614 const struct hda_input_mux *imux,
3615 struct snd_ctl_elem_value *ucontrol,
3616 hda_nid_t nid,
1da177e4
LT
3617 unsigned int *cur_val)
3618{
3619 unsigned int idx;
3620
5513b0c5
TI
3621 if (!imux->num_items)
3622 return 0;
1da177e4
LT
3623 idx = ucontrol->value.enumerated.item[0];
3624 if (idx >= imux->num_items)
3625 idx = imux->num_items - 1;
82beb8fd 3626 if (*cur_val == idx)
1da177e4 3627 return 0;
82beb8fd
TI
3628 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3629 imux->items[idx].index);
1da177e4
LT
3630 *cur_val = idx;
3631 return 1;
3632}
2698ea98 3633EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
1da177e4
LT
3634
3635
a11e9b16
TI
3636/**
3637 * snd_hda_enum_helper_info - Helper for simple enum ctls
3638 * @kcontrol: ctl element
3639 * @uinfo: pointer to get/store the data
3640 * @num_items: number of enum items
3641 * @texts: enum item string array
3642 *
dda415d4
TI
3643 * process kcontrol info callback of a simple string enum array
3644 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3645 */
3646int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3647 struct snd_ctl_elem_info *uinfo,
3648 int num_items, const char * const *texts)
3649{
3650 static const char * const texts_default[] = {
3651 "Disabled", "Enabled"
3652 };
3653
3654 if (!texts || !num_items) {
3655 num_items = 2;
3656 texts = texts_default;
3657 }
3658
3ff72219 3659 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
dda415d4 3660}
2698ea98 3661EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
dda415d4 3662
1da177e4
LT
3663/*
3664 * Multi-channel / digital-out PCM helper functions
3665 */
3666
6b97eb45
TI
3667/* setup SPDIF output stream */
3668static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3669 unsigned int stream_tag, unsigned int format)
3670{
3bef1c37
LD
3671 struct hda_spdif_out *spdif;
3672 unsigned int curr_fmt;
3673 bool reset;
3674
3675 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3676 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3677 AC_VERB_GET_STREAM_FORMAT, 0);
3678 reset = codec->spdif_status_reset &&
3679 (spdif->ctls & AC_DIG1_ENABLE) &&
3680 curr_fmt != format;
3681
3682 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3683 updated */
3684 if (reset)
28aedaf7 3685 set_dig_out_convert(codec, nid,
7c935976 3686 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
2f72853c 3687 -1);
6b97eb45 3688 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2f72853c 3689 if (codec->slave_dig_outs) {
dda14410 3690 const hda_nid_t *d;
2f72853c
TI
3691 for (d = codec->slave_dig_outs; *d; d++)
3692 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3693 format);
3694 }
6b97eb45 3695 /* turn on again (if needed) */
3bef1c37 3696 if (reset)
2f72853c 3697 set_dig_out_convert(codec, nid,
7c935976 3698 spdif->ctls & 0xff, -1);
2f72853c 3699}
de51ca12 3700
2f72853c
TI
3701static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3702{
3703 snd_hda_codec_cleanup_stream(codec, nid);
3704 if (codec->slave_dig_outs) {
dda14410 3705 const hda_nid_t *d;
2f72853c
TI
3706 for (d = codec->slave_dig_outs; *d; d++)
3707 snd_hda_codec_cleanup_stream(codec, *d);
de51ca12 3708 }
6b97eb45
TI
3709}
3710
d5191e50
TI
3711/**
3712 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
a11e9b16
TI
3713 * @codec: the HDA codec
3714 * @mout: hda_multi_out object
1da177e4 3715 */
0ba21762
TI
3716int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3717 struct hda_multi_out *mout)
1da177e4 3718{
62932df8 3719 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
3720 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3721 /* already opened as analog dup; reset it once */
2f72853c 3722 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4 3723 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 3724 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3725 return 0;
3726}
2698ea98 3727EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
1da177e4 3728
d5191e50
TI
3729/**
3730 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
a11e9b16
TI
3731 * @codec: the HDA codec
3732 * @mout: hda_multi_out object
3733 * @stream_tag: stream tag to assign
3734 * @format: format id to assign
3735 * @substream: PCM substream to assign
d5191e50 3736 */
6b97eb45
TI
3737int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3738 struct hda_multi_out *mout,
3739 unsigned int stream_tag,
3740 unsigned int format,
3741 struct snd_pcm_substream *substream)
3742{
3743 mutex_lock(&codec->spdif_mutex);
3744 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3745 mutex_unlock(&codec->spdif_mutex);
3746 return 0;
3747}
2698ea98 3748EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
6b97eb45 3749
d5191e50
TI
3750/**
3751 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
a11e9b16
TI
3752 * @codec: the HDA codec
3753 * @mout: hda_multi_out object
d5191e50 3754 */
9411e21c
TI
3755int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3756 struct hda_multi_out *mout)
3757{
3758 mutex_lock(&codec->spdif_mutex);
3759 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3760 mutex_unlock(&codec->spdif_mutex);
3761 return 0;
3762}
2698ea98 3763EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
9411e21c 3764
d5191e50
TI
3765/**
3766 * snd_hda_multi_out_dig_close - release the digital out stream
a11e9b16
TI
3767 * @codec: the HDA codec
3768 * @mout: hda_multi_out object
1da177e4 3769 */
0ba21762
TI
3770int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3771 struct hda_multi_out *mout)
1da177e4 3772{
62932df8 3773 mutex_lock(&codec->spdif_mutex);
1da177e4 3774 mout->dig_out_used = 0;
62932df8 3775 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3776 return 0;
3777}
2698ea98 3778EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
1da177e4 3779
d5191e50
TI
3780/**
3781 * snd_hda_multi_out_analog_open - open analog outputs
a11e9b16
TI
3782 * @codec: the HDA codec
3783 * @mout: hda_multi_out object
3784 * @substream: PCM substream to assign
3785 * @hinfo: PCM information to assign
d5191e50
TI
3786 *
3787 * Open analog outputs and set up the hw-constraints.
3788 * If the digital outputs can be opened as slave, open the digital
3789 * outputs, too.
1da177e4 3790 */
0ba21762
TI
3791int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3792 struct hda_multi_out *mout,
9a08160b
TI
3793 struct snd_pcm_substream *substream,
3794 struct hda_pcm_stream *hinfo)
3795{
3796 struct snd_pcm_runtime *runtime = substream->runtime;
3797 runtime->hw.channels_max = mout->max_channels;
3798 if (mout->dig_out_nid) {
3799 if (!mout->analog_rates) {
3800 mout->analog_rates = hinfo->rates;
3801 mout->analog_formats = hinfo->formats;
3802 mout->analog_maxbps = hinfo->maxbps;
3803 } else {
3804 runtime->hw.rates = mout->analog_rates;
3805 runtime->hw.formats = mout->analog_formats;
3806 hinfo->maxbps = mout->analog_maxbps;
3807 }
3808 if (!mout->spdif_rates) {
3809 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3810 &mout->spdif_rates,
3811 &mout->spdif_formats,
3812 &mout->spdif_maxbps);
3813 }
3814 mutex_lock(&codec->spdif_mutex);
3815 if (mout->share_spdif) {
022b466f
TI
3816 if ((runtime->hw.rates & mout->spdif_rates) &&
3817 (runtime->hw.formats & mout->spdif_formats)) {
3818 runtime->hw.rates &= mout->spdif_rates;
3819 runtime->hw.formats &= mout->spdif_formats;
3820 if (mout->spdif_maxbps < hinfo->maxbps)
3821 hinfo->maxbps = mout->spdif_maxbps;
3822 } else {
3823 mout->share_spdif = 0;
3824 /* FIXME: need notify? */
3825 }
9a08160b 3826 }
eaa9985b 3827 mutex_unlock(&codec->spdif_mutex);
9a08160b 3828 }
1da177e4
LT
3829 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3830 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3831}
2698ea98 3832EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
1da177e4 3833
d5191e50
TI
3834/**
3835 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
a11e9b16
TI
3836 * @codec: the HDA codec
3837 * @mout: hda_multi_out object
3838 * @stream_tag: stream tag to assign
3839 * @format: format id to assign
3840 * @substream: PCM substream to assign
d5191e50
TI
3841 *
3842 * Set up the i/o for analog out.
3843 * When the digital out is available, copy the front out to digital out, too.
1da177e4 3844 */
0ba21762
TI
3845int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3846 struct hda_multi_out *mout,
1da177e4
LT
3847 unsigned int stream_tag,
3848 unsigned int format,
c8b6bf9b 3849 struct snd_pcm_substream *substream)
1da177e4 3850{
dda14410 3851 const hda_nid_t *nids = mout->dac_nids;
1da177e4 3852 int chs = substream->runtime->channels;
e3245cdd 3853 struct hda_spdif_out *spdif;
1da177e4
LT
3854 int i;
3855
62932df8 3856 mutex_lock(&codec->spdif_mutex);
e3245cdd 3857 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
9a08160b
TI
3858 if (mout->dig_out_nid && mout->share_spdif &&
3859 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1da177e4 3860 if (chs == 2 &&
0ba21762
TI
3861 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3862 format) &&
7c935976 3863 !(spdif->status & IEC958_AES0_NONAUDIO)) {
1da177e4 3864 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
3865 setup_dig_out_stream(codec, mout->dig_out_nid,
3866 stream_tag, format);
1da177e4
LT
3867 } else {
3868 mout->dig_out_used = 0;
2f72853c 3869 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
3870 }
3871 }
62932df8 3872 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3873
3874 /* front */
0ba21762
TI
3875 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3876 0, format);
d29240ce
TI
3877 if (!mout->no_share_stream &&
3878 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 3879 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
3880 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3881 0, format);
82bc955f 3882 /* extra outputs copied from front */
a06dbfc2
TI
3883 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3884 if (!mout->no_share_stream && mout->hp_out_nid[i])
3885 snd_hda_codec_setup_stream(codec,
3886 mout->hp_out_nid[i],
3887 stream_tag, 0, format);
82bc955f 3888
1da177e4
LT
3889 /* surrounds */
3890 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 3891 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
3892 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3893 i * 2, format);
d29240ce 3894 else if (!mout->no_share_stream) /* copy front */
0ba21762
TI
3895 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3896 0, format);
1da177e4 3897 }
cd4035e8
DH
3898
3899 /* extra surrounds */
3900 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3901 int ch = 0;
3902 if (!mout->extra_out_nid[i])
3903 break;
3904 if (chs >= (i + 1) * 2)
3905 ch = i * 2;
3906 else if (!mout->no_share_stream)
3907 break;
3908 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3909 stream_tag, ch, format);
3910 }
3911
1da177e4
LT
3912 return 0;
3913}
2698ea98 3914EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
1da177e4 3915
d5191e50
TI
3916/**
3917 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
a11e9b16
TI
3918 * @codec: the HDA codec
3919 * @mout: hda_multi_out object
1da177e4 3920 */
0ba21762
TI
3921int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3922 struct hda_multi_out *mout)
1da177e4 3923{
dda14410 3924 const hda_nid_t *nids = mout->dac_nids;
1da177e4
LT
3925 int i;
3926
3927 for (i = 0; i < mout->num_dacs; i++)
888afa15 3928 snd_hda_codec_cleanup_stream(codec, nids[i]);
1da177e4 3929 if (mout->hp_nid)
888afa15 3930 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
a06dbfc2
TI
3931 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3932 if (mout->hp_out_nid[i])
3933 snd_hda_codec_cleanup_stream(codec,
3934 mout->hp_out_nid[i]);
82bc955f
TI
3935 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3936 if (mout->extra_out_nid[i])
888afa15
TI
3937 snd_hda_codec_cleanup_stream(codec,
3938 mout->extra_out_nid[i]);
62932df8 3939 mutex_lock(&codec->spdif_mutex);
1da177e4 3940 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2f72853c 3941 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
3942 mout->dig_out_used = 0;
3943 }
62932df8 3944 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3945 return 0;
3946}
2698ea98 3947EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
1da177e4 3948
4740860b
TI
3949/**
3950 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
a11e9b16
TI
3951 * @codec: the HDA codec
3952 * @pin: referred pin NID
4740860b
TI
3953 *
3954 * Guess the suitable VREF pin bits to be set as the pin-control value.
3955 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3956 */
3957unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3958{
3959 unsigned int pincap;
3960 unsigned int oldval;
3961 oldval = snd_hda_codec_read(codec, pin, 0,
3962 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3963 pincap = snd_hda_query_pin_caps(codec, pin);
3964 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3965 /* Exception: if the default pin setup is vref50, we give it priority */
3966 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3967 return AC_PINCTL_VREF_80;
3968 else if (pincap & AC_PINCAP_VREF_50)
3969 return AC_PINCTL_VREF_50;
3970 else if (pincap & AC_PINCAP_VREF_100)
3971 return AC_PINCTL_VREF_100;
3972 else if (pincap & AC_PINCAP_VREF_GRD)
3973 return AC_PINCTL_VREF_GRD;
3974 return AC_PINCTL_VREF_HIZ;
3975}
2698ea98 3976EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
4740860b 3977
a11e9b16
TI
3978/**
3979 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3980 * @codec: the HDA codec
3981 * @pin: referred pin NID
3982 * @val: pin ctl value to audit
3983 */
62f3a2f7
TI
3984unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3985 hda_nid_t pin, unsigned int val)
3986{
3987 static unsigned int cap_lists[][2] = {
3988 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3989 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3990 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3991 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3992 };
3993 unsigned int cap;
3994
3995 if (!val)
3996 return 0;
3997 cap = snd_hda_query_pin_caps(codec, pin);
3998 if (!cap)
3999 return val; /* don't know what to do... */
4000
4001 if (val & AC_PINCTL_OUT_EN) {
4002 if (!(cap & AC_PINCAP_OUT))
4003 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4004 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4005 val &= ~AC_PINCTL_HP_EN;
4006 }
4007
4008 if (val & AC_PINCTL_IN_EN) {
4009 if (!(cap & AC_PINCAP_IN))
4010 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4011 else {
4012 unsigned int vcap, vref;
4013 int i;
4014 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4015 vref = val & AC_PINCTL_VREFEN;
4016 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4017 if (vref == cap_lists[i][0] &&
4018 !(vcap & cap_lists[i][1])) {
4019 if (i == ARRAY_SIZE(cap_lists) - 1)
4020 vref = AC_PINCTL_VREF_HIZ;
4021 else
4022 vref = cap_lists[i + 1][0];
4023 }
4024 }
4025 val &= ~AC_PINCTL_VREFEN;
4026 val |= vref;
4027 }
4028 }
4029
4030 return val;
4031}
2698ea98 4032EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
62f3a2f7 4033
a11e9b16
TI
4034/**
4035 * _snd_hda_pin_ctl - Helper to set pin ctl value
4036 * @codec: the HDA codec
4037 * @pin: referred pin NID
4038 * @val: pin control value to set
4039 * @cached: access over codec pinctl cache or direct write
4040 *
4041 * This function is a helper to set a pin ctl value more safely.
4042 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4043 * value in pin target array via snd_hda_codec_set_pin_target(), then
4044 * actually writes the value via either snd_hda_codec_update_cache() or
4045 * snd_hda_codec_write() depending on @cached flag.
4046 */
cdd03ced
TI
4047int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4048 unsigned int val, bool cached)
4049{
62f3a2f7 4050 val = snd_hda_correct_pin_ctl(codec, pin, val);
d7fdc00a 4051 snd_hda_codec_set_pin_target(codec, pin, val);
cdd03ced
TI
4052 if (cached)
4053 return snd_hda_codec_update_cache(codec, pin, 0,
4054 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4055 else
4056 return snd_hda_codec_write(codec, pin, 0,
4057 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4058}
2698ea98 4059EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
cdd03ced 4060
990061c2
TI
4061/**
4062 * snd_hda_add_imux_item - Add an item to input_mux
a11e9b16
TI
4063 * @codec: the HDA codec
4064 * @imux: imux helper object
4065 * @label: the name of imux item to assign
4066 * @index: index number of imux item to assign
4067 * @type_idx: pointer to store the resultant label index
990061c2
TI
4068 *
4069 * When the same label is used already in the existing items, the number
4070 * suffix is appended to the label. This label index number is stored
4071 * to type_idx when non-NULL pointer is given.
4072 */
6194b99d
TI
4073int snd_hda_add_imux_item(struct hda_codec *codec,
4074 struct hda_input_mux *imux, const char *label,
10a20af7
TI
4075 int index, int *type_idx)
4076{
4077 int i, label_idx = 0;
4078 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
6194b99d 4079 codec_err(codec, "hda_codec: Too many imux items!\n");
10a20af7
TI
4080 return -EINVAL;
4081 }
4082 for (i = 0; i < imux->num_items; i++) {
4083 if (!strncmp(label, imux->items[i].label, strlen(label)))
4084 label_idx++;
d7b1ae9d 4085 }
10a20af7
TI
4086 if (type_idx)
4087 *type_idx = label_idx;
4088 if (label_idx > 0)
4089 snprintf(imux->items[imux->num_items].label,
4090 sizeof(imux->items[imux->num_items].label),
4091 "%s %d", label, label_idx);
b5786e85 4092 else
10a20af7
TI
4093 strlcpy(imux->items[imux->num_items].label, label,
4094 sizeof(imux->items[imux->num_items].label));
4095 imux->items[imux->num_items].index = index;
4096 imux->num_items++;
4097 return 0;
d7b1ae9d 4098}
2698ea98 4099EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
d7b1ae9d 4100
1da177e4 4101/**
0a50575b 4102 * snd_hda_bus_reset_codecs - Reset the bus
59ed1ead 4103 * @bus: HD-audio bus
1da177e4 4104 */
0a50575b 4105void snd_hda_bus_reset_codecs(struct hda_bus *bus)
1da177e4 4106{
0ba21762 4107 struct hda_codec *codec;
1da177e4 4108
d068ebc2 4109 list_for_each_codec(codec, bus) {
59ed1ead 4110 /* FIXME: maybe a better way needed for forced reset */
26a6cb6c 4111 cancel_delayed_work_sync(&codec->jackpoll_work);
59ed1ead 4112#ifdef CONFIG_PM
0e24dbb7 4113 if (hda_codec_is_power_on(codec)) {
cc72da7d 4114 hda_call_codec_suspend(codec);
12edb893 4115 hda_call_codec_resume(codec);
59ed1ead
TI
4116 }
4117#endif
1da177e4 4118 }
1da177e4 4119}
b2e18597 4120
d5191e50
TI
4121/**
4122 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4123 * @pcm: PCM caps bits
4124 * @buf: the string buffer to write
4125 * @buflen: the max buffer length
4126 *
4127 * used by hda_proc.c and hda_eld.c
4128 */
b2022266
TI
4129void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4130{
4131 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4132 int i, j;
4133
4134 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4135 if (pcm & (AC_SUPPCM_BITS_8 << i))
4136 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4137
4138 buf[j] = '\0'; /* necessary when j == 0 */
4139}
2698ea98 4140EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
1289e9e8
TI
4141
4142MODULE_DESCRIPTION("HDA codec core");
4143MODULE_LICENSE("GPL");