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