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