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