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