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