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