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