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