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