2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/module.h>
28 #include <linux/pm_runtime.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
41 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
42 #define hda_codec_is_power_on(codec) \
43 (!pm_runtime_suspended(hda_codec_dev(codec)))
45 #define codec_in_pm(codec) 0
46 #define hda_codec_is_power_on(codec) 1
49 #define codec_has_epss(codec) \
50 ((codec)->core.power_caps & AC_PWRST_EPSS)
51 #define codec_has_clkstop(codec) \
52 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55 * Send and receive a verb - passed to exec_verb override for hdac_device
57 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
58 unsigned int flags, unsigned int *res)
60 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
61 struct hda_bus *bus = codec->bus;
68 snd_hda_power_up_pm(codec);
69 mutex_lock(&bus->core.cmd_mutex);
70 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
71 bus->no_response_fallback = 1;
72 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
74 bus->no_response_fallback = 0;
75 mutex_unlock(&bus->core.cmd_mutex);
76 snd_hda_power_down_pm(codec);
77 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
78 if (bus->response_reset) {
80 "resetting BUS due to fatal communication error\n");
81 snd_hda_bus_reset(bus);
85 /* clear reset-flag when the communication gets recovered */
86 if (!err || codec_in_pm(codec))
87 bus->response_reset = 0;
92 * snd_hda_sequence_write - sequence writes
93 * @codec: the HDA codec
94 * @seq: VERB array to send
96 * Send the commands sequentially from the given array.
97 * The array must be terminated with NID=0.
99 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
101 for (; seq->nid; seq++)
102 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
104 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
106 /* connection list element */
107 struct hda_conn_list {
108 struct list_head list;
114 /* look up the cached results */
115 static struct hda_conn_list *
116 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
118 struct hda_conn_list *p;
119 list_for_each_entry(p, &codec->conn_list, list) {
126 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
127 const hda_nid_t *list)
129 struct hda_conn_list *p;
131 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
136 memcpy(p->conns, list, len * sizeof(hda_nid_t));
137 list_add(&p->list, &codec->conn_list);
141 static void remove_conn_list(struct hda_codec *codec)
143 while (!list_empty(&codec->conn_list)) {
144 struct hda_conn_list *p;
145 p = list_first_entry(&codec->conn_list, typeof(*p), list);
151 /* read the connection and add to the cache */
152 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
155 hda_nid_t *result = list;
158 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
159 if (len == -ENOSPC) {
160 len = snd_hda_get_num_raw_conns(codec, nid);
161 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
164 len = snd_hda_get_raw_connections(codec, nid, result, len);
167 len = snd_hda_override_conn_list(codec, nid, len, result);
174 * snd_hda_get_conn_list - get connection list
175 * @codec: the HDA codec
177 * @listp: the pointer to store NID list
179 * Parses the connection list of the given widget and stores the pointer
180 * to the list of NIDs.
182 * Returns the number of connections, or a negative error code.
184 * Note that the returned pointer isn't protected against the list
185 * modification. If snd_hda_override_conn_list() might be called
186 * concurrently, protect with a mutex appropriately.
188 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
189 const hda_nid_t **listp)
195 const struct hda_conn_list *p;
197 /* if the connection-list is already cached, read it */
198 p = lookup_conn_list(codec, nid);
204 if (snd_BUG_ON(added))
207 err = read_and_add_raw_conns(codec, nid);
213 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
216 * snd_hda_get_connections - copy connection list
217 * @codec: the HDA codec
219 * @conn_list: connection list array; when NULL, checks only the size
220 * @max_conns: max. number of connections to store
222 * Parses the connection list of the given widget and stores the list
225 * Returns the number of connections, or a negative error code.
227 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
228 hda_nid_t *conn_list, int max_conns)
230 const hda_nid_t *list;
231 int len = snd_hda_get_conn_list(codec, nid, &list);
233 if (len > 0 && conn_list) {
234 if (len > max_conns) {
235 codec_err(codec, "Too many connections %d for NID 0x%x\n",
239 memcpy(conn_list, list, len * sizeof(hda_nid_t));
244 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
247 * snd_hda_override_conn_list - add/modify the connection-list to cache
248 * @codec: the HDA codec
250 * @len: number of connection list entries
251 * @list: the list of connection entries
253 * Add or modify the given connection-list to the cache. If the corresponding
254 * cache already exists, invalidate it and append a new one.
256 * Returns zero or a negative error code.
258 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
259 const hda_nid_t *list)
261 struct hda_conn_list *p;
263 p = lookup_conn_list(codec, nid);
269 return add_conn_list(codec, nid, len, list);
271 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
274 * snd_hda_get_conn_index - get the connection index of the given NID
275 * @codec: the HDA codec
276 * @mux: NID containing the list
277 * @nid: NID to select
278 * @recursive: 1 when searching NID recursively, otherwise 0
280 * Parses the connection list of the widget @mux and checks whether the
281 * widget @nid is present. If it is, return the connection index.
282 * Otherwise it returns -1.
284 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
285 hda_nid_t nid, int recursive)
287 const hda_nid_t *conn;
290 nums = snd_hda_get_conn_list(codec, mux, &conn);
291 for (i = 0; i < nums; i++)
296 if (recursive > 10) {
297 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
301 for (i = 0; i < nums; i++) {
302 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
303 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
305 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
310 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
313 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
314 * @codec: the HDA codec
315 * @nid: NID of the pin to parse
317 * Get the device entry number on the given widget. This is a feature of
318 * DP MST audio. Each pin can have several device entries in it.
320 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
322 unsigned int wcaps = get_wcaps(codec, nid);
325 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
326 get_wcaps_type(wcaps) != AC_WID_PIN)
329 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
332 return parm & AC_DEV_LIST_LEN_MASK;
334 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
337 * snd_hda_get_devices - copy device list without cache
338 * @codec: the HDA codec
339 * @nid: NID of the pin to parse
340 * @dev_list: device list array
341 * @max_devices: max. number of devices to store
343 * Copy the device list. This info is dynamic and so not cached.
344 * Currently called only from hda_proc.c, so not exported.
346 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
347 u8 *dev_list, int max_devices)
350 int i, dev_len, devices;
352 parm = snd_hda_get_num_devices(codec, nid);
353 if (!parm) /* not multi-stream capable */
357 dev_len = dev_len < max_devices ? dev_len : max_devices;
360 while (devices < dev_len) {
361 if (snd_hdac_read(&codec->core, nid,
362 AC_VERB_GET_DEVICE_LIST, devices, &parm))
365 for (i = 0; i < 8; i++) {
366 dev_list[devices] = (u8)parm;
369 if (devices >= dev_len)
377 * snd_hda_get_dev_select - get device entry select on the pin
378 * @codec: the HDA codec
379 * @nid: NID of the pin to get device entry select
381 * Get the devcie entry select on the pin. Return the device entry
382 * id selected on the pin. Return 0 means the first device entry
383 * is selected or MST is not supported.
385 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
387 /* not support dp_mst will always return 0, using first dev_entry */
391 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
393 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
396 * snd_hda_set_dev_select - set device entry select on the pin
397 * @codec: the HDA codec
398 * @nid: NID of the pin to set device entry select
399 * @dev_id: device entry id to be set
401 * Set the device entry select on the pin nid.
403 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
405 int ret, num_devices;
407 /* not support dp_mst will always return 0, using first dev_entry */
411 /* AC_PAR_DEVLIST_LEN is 0 based. */
412 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
413 /* If Device List Length is 0 (num_device = 1),
414 * the pin is not multi stream capable.
415 * Do nothing in this case.
417 if (num_devices == 1)
420 /* Behavior of setting index being equal to or greater than
421 * Device List Length is not predictable
423 if (num_devices <= dev_id)
426 ret = snd_hda_codec_write(codec, nid, 0,
427 AC_VERB_SET_DEVICE_SEL, dev_id);
431 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
434 * read widget caps for each widget and store in cache
436 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
441 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
444 nid = codec->core.start_nid;
445 for (i = 0; i < codec->core.num_nodes; i++, nid++)
446 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
447 nid, AC_PAR_AUDIO_WIDGET_CAP);
451 /* read all pin default configurations and save codec->init_pins */
452 static int read_pin_defaults(struct hda_codec *codec)
456 for_each_hda_codec_node(nid, codec) {
457 struct hda_pincfg *pin;
458 unsigned int wcaps = get_wcaps(codec, nid);
459 unsigned int wid_type = get_wcaps_type(wcaps);
460 if (wid_type != AC_WID_PIN)
462 pin = snd_array_new(&codec->init_pins);
466 pin->cfg = snd_hda_codec_read(codec, nid, 0,
467 AC_VERB_GET_CONFIG_DEFAULT, 0);
469 * all device entries are the same widget control so far
470 * fixme: if any codec is different, need fix here
472 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
473 AC_VERB_GET_PIN_WIDGET_CONTROL,
479 /* look up the given pin config list and return the item matching with NID */
480 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
481 struct snd_array *array,
485 for (i = 0; i < array->used; i++) {
486 struct hda_pincfg *pin = snd_array_elem(array, i);
493 /* set the current pin config value for the given NID.
494 * the value is cached, and read via snd_hda_codec_get_pincfg()
496 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
497 hda_nid_t nid, unsigned int cfg)
499 struct hda_pincfg *pin;
501 /* the check below may be invalid when pins are added by a fixup
502 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
506 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
510 pin = look_up_pincfg(codec, list, nid);
512 pin = snd_array_new(list);
522 * snd_hda_codec_set_pincfg - Override a pin default configuration
523 * @codec: the HDA codec
524 * @nid: NID to set the pin config
525 * @cfg: the pin default config value
527 * Override a pin default configuration value in the cache.
528 * This value can be read by snd_hda_codec_get_pincfg() in a higher
529 * priority than the real hardware value.
531 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
532 hda_nid_t nid, unsigned int cfg)
534 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
536 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
539 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
540 * @codec: the HDA codec
541 * @nid: NID to get the pin config
543 * Get the current pin config value of the given pin NID.
544 * If the pincfg value is cached or overridden via sysfs or driver,
545 * returns the cached value.
547 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
549 struct hda_pincfg *pin;
551 #ifdef CONFIG_SND_HDA_RECONFIG
553 unsigned int cfg = 0;
554 mutex_lock(&codec->user_mutex);
555 pin = look_up_pincfg(codec, &codec->user_pins, nid);
558 mutex_unlock(&codec->user_mutex);
563 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
566 pin = look_up_pincfg(codec, &codec->init_pins, nid);
571 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
574 * snd_hda_codec_set_pin_target - remember the current pinctl target value
575 * @codec: the HDA codec
577 * @val: assigned pinctl value
579 * This function stores the given value to a pinctl target value in the
580 * pincfg table. This isn't always as same as the actually written value
581 * but can be referred at any time via snd_hda_codec_get_pin_target().
583 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
586 struct hda_pincfg *pin;
588 pin = look_up_pincfg(codec, &codec->init_pins, nid);
594 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
597 * snd_hda_codec_get_pin_target - return the current pinctl target value
598 * @codec: the HDA codec
601 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
603 struct hda_pincfg *pin;
605 pin = look_up_pincfg(codec, &codec->init_pins, nid);
610 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
613 * snd_hda_shutup_pins - Shut up all pins
614 * @codec: the HDA codec
616 * Clear all pin controls to shup up before suspend for avoiding click noise.
617 * The controls aren't cached so that they can be resumed properly.
619 void snd_hda_shutup_pins(struct hda_codec *codec)
622 /* don't shut up pins when unloading the driver; otherwise it breaks
623 * the default pin setup at the next load of the driver
625 if (codec->bus->shutdown)
627 for (i = 0; i < codec->init_pins.used; i++) {
628 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
629 /* use read here for syncing after issuing each verb */
630 snd_hda_codec_read(codec, pin->nid, 0,
631 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
633 codec->pins_shutup = 1;
635 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
638 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
639 static void restore_shutup_pins(struct hda_codec *codec)
642 if (!codec->pins_shutup)
644 if (codec->bus->shutdown)
646 for (i = 0; i < codec->init_pins.used; i++) {
647 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
648 snd_hda_codec_write(codec, pin->nid, 0,
649 AC_VERB_SET_PIN_WIDGET_CONTROL,
652 codec->pins_shutup = 0;
656 static void hda_jackpoll_work(struct work_struct *work)
658 struct hda_codec *codec =
659 container_of(work, struct hda_codec, jackpoll_work.work);
661 snd_hda_jack_set_dirty_all(codec);
662 snd_hda_jack_poll_all(codec);
664 if (!codec->jackpoll_interval)
667 schedule_delayed_work(&codec->jackpoll_work,
668 codec->jackpoll_interval);
671 /* release all pincfg lists */
672 static void free_init_pincfgs(struct hda_codec *codec)
674 snd_array_free(&codec->driver_pins);
675 #ifdef CONFIG_SND_HDA_RECONFIG
676 snd_array_free(&codec->user_pins);
678 snd_array_free(&codec->init_pins);
682 * audio-converter setup caches
684 struct hda_cvt_setup {
689 unsigned char active; /* cvt is currently used */
690 unsigned char dirty; /* setups should be cleared */
693 /* get or create a cache entry for the given audio converter NID */
694 static struct hda_cvt_setup *
695 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
697 struct hda_cvt_setup *p;
700 for (i = 0; i < codec->cvt_setups.used; i++) {
701 p = snd_array_elem(&codec->cvt_setups, i);
705 p = snd_array_new(&codec->cvt_setups);
714 static void release_pcm(struct kref *kref)
716 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
719 snd_device_free(pcm->codec->card, pcm->pcm);
720 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
725 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
727 kref_put(&pcm->kref, release_pcm);
729 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
731 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
732 const char *fmt, ...)
737 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
742 kref_init(&pcm->kref);
744 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
751 list_add_tail(&pcm->list, &codec->pcm_list_head);
754 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
759 static void codec_release_pcms(struct hda_codec *codec)
761 struct hda_pcm *pcm, *n;
763 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
764 list_del_init(&pcm->list);
766 snd_device_disconnect(codec->card, pcm->pcm);
767 snd_hda_codec_pcm_put(pcm);
771 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
773 if (codec->registered) {
774 /* pm_runtime_put() is called in snd_hdac_device_exit() */
775 pm_runtime_get_noresume(hda_codec_dev(codec));
776 pm_runtime_disable(hda_codec_dev(codec));
777 codec->registered = 0;
780 cancel_delayed_work_sync(&codec->jackpoll_work);
781 if (!codec->in_freeing)
782 snd_hda_ctls_clear(codec);
783 codec_release_pcms(codec);
784 snd_hda_detach_beep_device(codec);
785 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
786 snd_hda_jack_tbl_clear(codec);
787 codec->proc_widget_hook = NULL;
790 /* free only driver_pins so that init_pins + user_pins are restored */
791 snd_array_free(&codec->driver_pins);
792 snd_array_free(&codec->cvt_setups);
793 snd_array_free(&codec->spdif_out);
794 snd_array_free(&codec->verbs);
795 codec->preset = NULL;
796 codec->slave_dig_outs = NULL;
797 codec->spdif_status_reset = 0;
798 snd_array_free(&codec->mixers);
799 snd_array_free(&codec->nids);
800 remove_conn_list(codec);
801 snd_hdac_regmap_exit(&codec->core);
804 static unsigned int hda_set_power_state(struct hda_codec *codec,
805 unsigned int power_state);
807 /* also called from hda_bind.c */
808 void snd_hda_codec_register(struct hda_codec *codec)
810 if (codec->registered)
812 if (device_is_registered(hda_codec_dev(codec))) {
813 snd_hda_register_beep_device(codec);
814 snd_hdac_link_power(&codec->core, true);
815 pm_runtime_enable(hda_codec_dev(codec));
816 /* it was powered up in snd_hda_codec_new(), now all done */
817 snd_hda_power_down(codec);
818 codec->registered = 1;
822 static int snd_hda_codec_dev_register(struct snd_device *device)
824 snd_hda_codec_register(device->device_data);
828 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
830 struct hda_codec *codec = device->device_data;
832 snd_hda_detach_beep_device(codec);
836 static int snd_hda_codec_dev_free(struct snd_device *device)
838 struct hda_codec *codec = device->device_data;
840 codec->in_freeing = 1;
841 snd_hdac_device_unregister(&codec->core);
842 snd_hdac_link_power(&codec->core, false);
843 put_device(hda_codec_dev(codec));
847 static void snd_hda_codec_dev_release(struct device *dev)
849 struct hda_codec *codec = dev_to_hda_codec(dev);
851 free_init_pincfgs(codec);
852 snd_hdac_device_exit(&codec->core);
853 snd_hda_sysfs_clear(codec);
854 kfree(codec->modelname);
860 * snd_hda_codec_new - create a HDA codec
861 * @bus: the bus to assign
862 * @codec_addr: the codec address
863 * @codecp: the pointer to store the generated codec
865 * Returns 0 if successful, or a negative error code.
867 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
868 unsigned int codec_addr, struct hda_codec **codecp)
870 struct hda_codec *codec;
874 static struct snd_device_ops dev_ops = {
875 .dev_register = snd_hda_codec_dev_register,
876 .dev_disconnect = snd_hda_codec_dev_disconnect,
877 .dev_free = snd_hda_codec_dev_free,
880 if (snd_BUG_ON(!bus))
882 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
885 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
889 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
890 err = snd_hdac_device_init(&codec->core, &bus->core, component,
897 codec->core.dev.release = snd_hda_codec_dev_release;
898 codec->core.type = HDA_DEV_LEGACY;
899 codec->core.exec_verb = codec_exec_verb;
903 codec->addr = codec_addr;
904 mutex_init(&codec->spdif_mutex);
905 mutex_init(&codec->control_mutex);
906 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
907 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
908 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
909 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
910 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
911 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
912 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
913 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
914 INIT_LIST_HEAD(&codec->conn_list);
915 INIT_LIST_HEAD(&codec->pcm_list_head);
917 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
918 codec->depop_delay = -1;
919 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
922 codec->power_jiffies = jiffies;
925 snd_hda_sysfs_init(codec);
927 if (codec->bus->modelname) {
928 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
929 if (!codec->modelname) {
935 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
936 err = read_widget_caps(codec, fg);
939 err = read_pin_defaults(codec);
943 /* power-up all before initialization */
944 hda_set_power_state(codec, AC_PWRST_D0);
946 snd_hda_codec_proc_new(codec);
948 snd_hda_create_hwdep(codec);
950 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
951 codec->core.subsystem_id, codec->core.revision_id);
952 snd_component_add(card, component);
954 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
963 put_device(hda_codec_dev(codec));
966 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
969 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
970 * @codec: the HDA codec
972 * Forcibly refresh the all widget caps and the init pin configurations of
975 int snd_hda_codec_update_widgets(struct hda_codec *codec)
980 err = snd_hdac_refresh_widget_sysfs(&codec->core);
984 /* Assume the function group node does not change,
985 * only the widget nodes may change.
988 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
989 err = read_widget_caps(codec, fg);
993 snd_array_free(&codec->init_pins);
994 err = read_pin_defaults(codec);
998 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1000 /* update the stream-id if changed */
1001 static void update_pcm_stream_id(struct hda_codec *codec,
1002 struct hda_cvt_setup *p, hda_nid_t nid,
1003 u32 stream_tag, int channel_id)
1005 unsigned int oldval, newval;
1007 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1008 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1009 newval = (stream_tag << 4) | channel_id;
1010 if (oldval != newval)
1011 snd_hda_codec_write(codec, nid, 0,
1012 AC_VERB_SET_CHANNEL_STREAMID,
1014 p->stream_tag = stream_tag;
1015 p->channel_id = channel_id;
1019 /* update the format-id if changed */
1020 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1021 hda_nid_t nid, int format)
1023 unsigned int oldval;
1025 if (p->format_id != format) {
1026 oldval = snd_hda_codec_read(codec, nid, 0,
1027 AC_VERB_GET_STREAM_FORMAT, 0);
1028 if (oldval != format) {
1030 snd_hda_codec_write(codec, nid, 0,
1031 AC_VERB_SET_STREAM_FORMAT,
1034 p->format_id = format;
1039 * snd_hda_codec_setup_stream - set up the codec for streaming
1040 * @codec: the CODEC to set up
1041 * @nid: the NID to set up
1042 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1043 * @channel_id: channel id to pass, zero based.
1044 * @format: stream format.
1046 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1048 int channel_id, int format)
1050 struct hda_codec *c;
1051 struct hda_cvt_setup *p;
1059 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1060 nid, stream_tag, channel_id, format);
1061 p = get_hda_cvt_setup(codec, nid);
1065 if (codec->patch_ops.stream_pm)
1066 codec->patch_ops.stream_pm(codec, nid, true);
1067 if (codec->pcm_format_first)
1068 update_pcm_format(codec, p, nid, format);
1069 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1070 if (!codec->pcm_format_first)
1071 update_pcm_format(codec, p, nid, format);
1076 /* make other inactive cvts with the same stream-tag dirty */
1077 type = get_wcaps_type(get_wcaps(codec, nid));
1078 list_for_each_codec(c, codec->bus) {
1079 for (i = 0; i < c->cvt_setups.used; i++) {
1080 p = snd_array_elem(&c->cvt_setups, i);
1081 if (!p->active && p->stream_tag == stream_tag &&
1082 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1087 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1089 static void really_cleanup_stream(struct hda_codec *codec,
1090 struct hda_cvt_setup *q);
1093 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1094 * @codec: the CODEC to clean up
1095 * @nid: the NID to clean up
1096 * @do_now: really clean up the stream instead of clearing the active flag
1098 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1101 struct hda_cvt_setup *p;
1106 if (codec->no_sticky_stream)
1109 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1110 p = get_hda_cvt_setup(codec, nid);
1112 /* here we just clear the active flag when do_now isn't set;
1113 * actual clean-ups will be done later in
1114 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1117 really_cleanup_stream(codec, p);
1122 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1124 static void really_cleanup_stream(struct hda_codec *codec,
1125 struct hda_cvt_setup *q)
1127 hda_nid_t nid = q->nid;
1128 if (q->stream_tag || q->channel_id)
1129 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1131 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1133 memset(q, 0, sizeof(*q));
1135 if (codec->patch_ops.stream_pm)
1136 codec->patch_ops.stream_pm(codec, nid, false);
1139 /* clean up the all conflicting obsolete streams */
1140 static void purify_inactive_streams(struct hda_codec *codec)
1142 struct hda_codec *c;
1145 list_for_each_codec(c, codec->bus) {
1146 for (i = 0; i < c->cvt_setups.used; i++) {
1147 struct hda_cvt_setup *p;
1148 p = snd_array_elem(&c->cvt_setups, i);
1150 really_cleanup_stream(c, p);
1156 /* clean up all streams; called from suspend */
1157 static void hda_cleanup_all_streams(struct hda_codec *codec)
1161 for (i = 0; i < codec->cvt_setups.used; i++) {
1162 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1164 really_cleanup_stream(codec, p);
1170 * amp access functions
1174 * query_amp_caps - query AMP capabilities
1175 * @codec: the HD-auio codec
1176 * @nid: the NID to query
1177 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1179 * Query AMP capabilities for the given widget and direction.
1180 * Returns the obtained capability bits.
1182 * When cap bits have been already read, this doesn't read again but
1183 * returns the cached value.
1185 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1187 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1188 nid = codec->core.afg;
1189 return snd_hda_param_read(codec, nid,
1190 direction == HDA_OUTPUT ?
1191 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1193 EXPORT_SYMBOL_GPL(query_amp_caps);
1196 * snd_hda_check_amp_caps - query AMP capabilities
1197 * @codec: the HD-audio codec
1198 * @nid: the NID to query
1199 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1200 * @bits: bit mask to check the result
1202 * Check whether the widget has the given amp capability for the direction.
1204 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1205 int dir, unsigned int bits)
1209 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1210 if (query_amp_caps(codec, nid, dir) & bits)
1214 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1217 * snd_hda_override_amp_caps - Override the AMP capabilities
1218 * @codec: the CODEC to clean up
1219 * @nid: the NID to clean up
1220 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1221 * @caps: the capability bits to set
1223 * Override the cached AMP caps bits value by the given one.
1224 * This function is useful if the driver needs to adjust the AMP ranges,
1225 * e.g. limit to 0dB, etc.
1227 * Returns zero if successful or a negative error code.
1229 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1234 snd_hda_override_wcaps(codec, nid,
1235 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1236 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1237 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1239 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1242 * snd_hda_codec_amp_update - update the AMP mono value
1243 * @codec: HD-audio codec
1244 * @nid: NID to read the AMP value
1245 * @ch: channel to update (0 or 1)
1246 * @dir: #HDA_INPUT or #HDA_OUTPUT
1247 * @idx: the index value (only for input direction)
1248 * @mask: bit mask to set
1249 * @val: the bits value to set
1251 * Update the AMP values for the given channel, direction and index.
1253 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1254 int ch, int dir, int idx, int mask, int val)
1256 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1258 /* enable fake mute if no h/w mute but min=mute */
1259 if ((query_amp_caps(codec, nid, dir) &
1260 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1261 cmd |= AC_AMP_FAKE_MUTE;
1262 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1264 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1267 * snd_hda_codec_amp_stereo - update the AMP stereo values
1268 * @codec: HD-audio codec
1269 * @nid: NID to read the AMP value
1270 * @direction: #HDA_INPUT or #HDA_OUTPUT
1271 * @idx: the index value (only for input direction)
1272 * @mask: bit mask to set
1273 * @val: the bits value to set
1275 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1276 * stereo widget with the same mask and value.
1278 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1279 int direction, int idx, int mask, int val)
1283 if (snd_BUG_ON(mask & ~0xff))
1285 for (ch = 0; ch < 2; ch++)
1286 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1290 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1293 * snd_hda_codec_amp_init - initialize the AMP value
1294 * @codec: the HDA codec
1295 * @nid: NID to read the AMP value
1296 * @ch: channel (left=0 or right=1)
1297 * @dir: #HDA_INPUT or #HDA_OUTPUT
1298 * @idx: the index value (only for input direction)
1299 * @mask: bit mask to set
1300 * @val: the bits value to set
1302 * Works like snd_hda_codec_amp_update() but it writes the value only at
1303 * the first access. If the amp was already initialized / updated beforehand,
1304 * this does nothing.
1306 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1307 int dir, int idx, int mask, int val)
1311 if (!codec->core.regmap)
1313 regcache_cache_only(codec->core.regmap, true);
1314 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1315 regcache_cache_only(codec->core.regmap, false);
1318 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1320 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1323 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1324 * @codec: the HDA codec
1325 * @nid: NID to read the AMP value
1326 * @dir: #HDA_INPUT or #HDA_OUTPUT
1327 * @idx: the index value (only for input direction)
1328 * @mask: bit mask to set
1329 * @val: the bits value to set
1331 * Call snd_hda_codec_amp_init() for both stereo channels.
1333 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1334 int dir, int idx, int mask, int val)
1338 if (snd_BUG_ON(mask & ~0xff))
1340 for (ch = 0; ch < 2; ch++)
1341 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1345 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1347 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1350 u32 caps = query_amp_caps(codec, nid, dir);
1352 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1359 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1360 * @kcontrol: referred ctl element
1361 * @uinfo: pointer to get/store the data
1363 * The control element is supposed to have the private_value field
1364 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1366 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1367 struct snd_ctl_elem_info *uinfo)
1369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1370 u16 nid = get_amp_nid(kcontrol);
1371 u8 chs = get_amp_channels(kcontrol);
1372 int dir = get_amp_direction(kcontrol);
1373 unsigned int ofs = get_amp_offset(kcontrol);
1375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1376 uinfo->count = chs == 3 ? 2 : 1;
1377 uinfo->value.integer.min = 0;
1378 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1379 if (!uinfo->value.integer.max) {
1381 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1382 nid, kcontrol->id.name);
1387 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1390 static inline unsigned int
1391 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1392 int ch, int dir, int idx, unsigned int ofs)
1395 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1396 val &= HDA_AMP_VOLMASK;
1405 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1406 int ch, int dir, int idx, unsigned int ofs,
1409 unsigned int maxval;
1413 /* ofs = 0: raw max value */
1414 maxval = get_amp_max_value(codec, nid, dir, 0);
1417 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1418 HDA_AMP_VOLMASK, val);
1422 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1423 * @kcontrol: ctl element
1424 * @ucontrol: pointer to get/store the data
1426 * The control element is supposed to have the private_value field
1427 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1429 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
1432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1433 hda_nid_t nid = get_amp_nid(kcontrol);
1434 int chs = get_amp_channels(kcontrol);
1435 int dir = get_amp_direction(kcontrol);
1436 int idx = get_amp_index(kcontrol);
1437 unsigned int ofs = get_amp_offset(kcontrol);
1438 long *valp = ucontrol->value.integer.value;
1441 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1443 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1446 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1449 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1450 * @kcontrol: ctl element
1451 * @ucontrol: pointer to get/store the data
1453 * The control element is supposed to have the private_value field
1454 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1456 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
1459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1460 hda_nid_t nid = get_amp_nid(kcontrol);
1461 int chs = get_amp_channels(kcontrol);
1462 int dir = get_amp_direction(kcontrol);
1463 int idx = get_amp_index(kcontrol);
1464 unsigned int ofs = get_amp_offset(kcontrol);
1465 long *valp = ucontrol->value.integer.value;
1469 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1473 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1476 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1478 /* inquiry the amp caps and convert to TLV */
1479 static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
1481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1482 hda_nid_t nid = get_amp_nid(kcontrol);
1483 int dir = get_amp_direction(kcontrol);
1484 unsigned int ofs = get_amp_offset(kcontrol);
1485 bool min_mute = get_amp_min_mute(kcontrol);
1486 u32 caps, val1, val2;
1488 caps = query_amp_caps(codec, nid, dir);
1489 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1490 val2 = (val2 + 1) * 25;
1491 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1493 val1 = ((int)val1) * ((int)val2);
1494 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1495 val2 |= TLV_DB_SCALE_MUTE;
1496 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1497 tlv[1] = 2 * sizeof(unsigned int);
1503 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
1504 * @kcontrol: ctl element
1505 * @op_flag: operation flag
1506 * @size: byte size of input TLV
1509 * The control element is supposed to have the private_value field
1510 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1512 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1513 unsigned int size, unsigned int __user *_tlv)
1515 unsigned int tlv[4];
1517 if (size < 4 * sizeof(unsigned int))
1519 get_ctl_amp_tlv(kcontrol, tlv);
1520 if (copy_to_user(_tlv, tlv, sizeof(tlv)))
1524 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1527 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1528 * @codec: HD-audio codec
1529 * @nid: NID of a reference widget
1530 * @dir: #HDA_INPUT or #HDA_OUTPUT
1531 * @tlv: TLV data to be stored, at least 4 elements
1533 * Set (static) TLV data for a virtual master volume using the AMP caps
1534 * obtained from the reference NID.
1535 * The volume range is recalculated as if the max volume is 0dB.
1537 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1543 caps = query_amp_caps(codec, nid, dir);
1544 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1545 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1546 step = (step + 1) * 25;
1547 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1548 tlv[1] = 2 * sizeof(unsigned int);
1549 tlv[2] = -nums * step;
1552 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1554 /* find a mixer control element with the given name */
1555 static struct snd_kcontrol *
1556 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1558 struct snd_ctl_elem_id id;
1559 memset(&id, 0, sizeof(id));
1560 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1563 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1565 strcpy(id.name, name);
1566 return snd_ctl_find_id(codec->card, &id);
1570 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1571 * @codec: HD-audio codec
1572 * @name: ctl id name string
1574 * Get the control element with the given id string and IFACE_MIXER.
1576 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1579 return find_mixer_ctl(codec, name, 0, 0);
1581 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1583 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1587 /* 16 ctlrs should be large enough */
1588 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1589 if (!find_mixer_ctl(codec, name, 0, idx))
1596 * snd_hda_ctl_add - Add a control element and assign to the codec
1597 * @codec: HD-audio codec
1598 * @nid: corresponding NID (optional)
1599 * @kctl: the control element to assign
1601 * Add the given control element to an array inside the codec instance.
1602 * All control elements belonging to a codec are supposed to be added
1603 * by this function so that a proper clean-up works at the free or
1604 * reconfiguration time.
1606 * If non-zero @nid is passed, the NID is assigned to the control element.
1607 * The assignment is shown in the codec proc file.
1609 * snd_hda_ctl_add() checks the control subdev id field whether
1610 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1611 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1612 * specifies if kctl->private_value is a HDA amplifier value.
1614 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1615 struct snd_kcontrol *kctl)
1618 unsigned short flags = 0;
1619 struct hda_nid_item *item;
1621 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1622 flags |= HDA_NID_ITEM_AMP;
1624 nid = get_amp_nid_(kctl->private_value);
1626 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1627 nid = kctl->id.subdevice & 0xffff;
1628 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1629 kctl->id.subdevice = 0;
1630 err = snd_ctl_add(codec->card, kctl);
1633 item = snd_array_new(&codec->mixers);
1638 item->flags = flags;
1641 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1644 * snd_hda_add_nid - Assign a NID to a control element
1645 * @codec: HD-audio codec
1646 * @nid: corresponding NID (optional)
1647 * @kctl: the control element to assign
1648 * @index: index to kctl
1650 * Add the given control element to an array inside the codec instance.
1651 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1652 * NID:KCTL mapping - for example "Capture Source" selector.
1654 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1655 unsigned int index, hda_nid_t nid)
1657 struct hda_nid_item *item;
1660 item = snd_array_new(&codec->nids);
1664 item->index = index;
1668 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1669 kctl->id.name, kctl->id.index, index);
1672 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1675 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1676 * @codec: HD-audio codec
1678 void snd_hda_ctls_clear(struct hda_codec *codec)
1681 struct hda_nid_item *items = codec->mixers.list;
1682 for (i = 0; i < codec->mixers.used; i++)
1683 snd_ctl_remove(codec->card, items[i].kctl);
1684 snd_array_free(&codec->mixers);
1685 snd_array_free(&codec->nids);
1689 * snd_hda_lock_devices - pseudo device locking
1692 * toggle card->shutdown to allow/disallow the device access (as a hack)
1694 int snd_hda_lock_devices(struct hda_bus *bus)
1696 struct snd_card *card = bus->card;
1697 struct hda_codec *codec;
1699 spin_lock(&card->files_lock);
1703 if (!list_empty(&card->ctl_files))
1706 list_for_each_codec(codec, bus) {
1707 struct hda_pcm *cpcm;
1708 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1711 if (cpcm->pcm->streams[0].substream_opened ||
1712 cpcm->pcm->streams[1].substream_opened)
1716 spin_unlock(&card->files_lock);
1722 spin_unlock(&card->files_lock);
1725 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1728 * snd_hda_unlock_devices - pseudo device unlocking
1731 void snd_hda_unlock_devices(struct hda_bus *bus)
1733 struct snd_card *card = bus->card;
1735 spin_lock(&card->files_lock);
1737 spin_unlock(&card->files_lock);
1739 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1742 * snd_hda_codec_reset - Clear all objects assigned to the codec
1743 * @codec: HD-audio codec
1745 * This frees the all PCM and control elements assigned to the codec, and
1746 * clears the caches and restores the pin default configurations.
1748 * When a device is being used, it returns -EBSY. If successfully freed,
1751 int snd_hda_codec_reset(struct hda_codec *codec)
1753 struct hda_bus *bus = codec->bus;
1755 if (snd_hda_lock_devices(bus) < 0)
1758 /* OK, let it free */
1759 snd_hdac_device_unregister(&codec->core);
1761 /* allow device access again */
1762 snd_hda_unlock_devices(bus);
1766 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1768 /* apply the function to all matching slave ctls in the mixer list */
1769 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1770 const char *suffix, map_slave_func_t func, void *data)
1772 struct hda_nid_item *items;
1773 const char * const *s;
1776 items = codec->mixers.list;
1777 for (i = 0; i < codec->mixers.used; i++) {
1778 struct snd_kcontrol *sctl = items[i].kctl;
1779 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1781 for (s = slaves; *s; s++) {
1782 char tmpname[sizeof(sctl->id.name)];
1783 const char *name = *s;
1785 snprintf(tmpname, sizeof(tmpname), "%s %s",
1789 if (!strcmp(sctl->id.name, name)) {
1790 err = func(codec, data, sctl);
1800 static int check_slave_present(struct hda_codec *codec,
1801 void *data, struct snd_kcontrol *sctl)
1806 /* guess the value corresponding to 0dB */
1807 static int get_kctl_0dB_offset(struct hda_codec *codec,
1808 struct snd_kcontrol *kctl, int *step_to_check)
1811 const int *tlv = NULL;
1814 if ((kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
1815 kctl->tlv.c == snd_hda_mixer_amp_tlv) {
1816 get_ctl_amp_tlv(kctl, _tlv);
1818 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1820 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1822 step &= ~TLV_DB_SCALE_MUTE;
1825 if (*step_to_check && *step_to_check != step) {
1826 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1827 - *step_to_check, step);
1830 *step_to_check = step;
1831 val = -tlv[2] / step;
1836 /* call kctl->put with the given value(s) */
1837 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1839 struct snd_ctl_elem_value *ucontrol;
1840 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1843 ucontrol->value.integer.value[0] = val;
1844 ucontrol->value.integer.value[1] = val;
1845 kctl->put(kctl, ucontrol);
1850 /* initialize the slave volume with 0dB */
1851 static int init_slave_0dB(struct hda_codec *codec,
1852 void *data, struct snd_kcontrol *slave)
1854 int offset = get_kctl_0dB_offset(codec, slave, data);
1856 put_kctl_with_value(slave, offset);
1860 /* unmute the slave */
1861 static int init_slave_unmute(struct hda_codec *codec,
1862 void *data, struct snd_kcontrol *slave)
1864 return put_kctl_with_value(slave, 1);
1867 static int add_slave(struct hda_codec *codec,
1868 void *data, struct snd_kcontrol *slave)
1870 return snd_ctl_add_slave(data, slave);
1874 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1875 * @codec: HD-audio codec
1876 * @name: vmaster control name
1877 * @tlv: TLV data (optional)
1878 * @slaves: slave control names (optional)
1879 * @suffix: suffix string to each slave name (optional)
1880 * @init_slave_vol: initialize slaves to unmute/0dB
1881 * @ctl_ret: store the vmaster kcontrol in return
1883 * Create a virtual master control with the given name. The TLV data
1884 * must be either NULL or a valid data.
1886 * @slaves is a NULL-terminated array of strings, each of which is a
1887 * slave control name. All controls with these names are assigned to
1888 * the new virtual master control.
1890 * This function returns zero if successful or a negative error code.
1892 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1893 unsigned int *tlv, const char * const *slaves,
1894 const char *suffix, bool init_slave_vol,
1895 struct snd_kcontrol **ctl_ret)
1897 struct snd_kcontrol *kctl;
1903 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1905 codec_dbg(codec, "No slave found for %s\n", name);
1908 kctl = snd_ctl_make_virtual_master(name, tlv);
1911 err = snd_hda_ctl_add(codec, 0, kctl);
1915 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1919 /* init with master mute & zero volume */
1920 put_kctl_with_value(kctl, 0);
1921 if (init_slave_vol) {
1923 map_slaves(codec, slaves, suffix,
1924 tlv ? init_slave_0dB : init_slave_unmute, &step);
1931 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1934 * mute-LED control using vmaster
1936 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1937 struct snd_ctl_elem_info *uinfo)
1939 static const char * const texts[] = {
1940 "On", "Off", "Follow Master"
1943 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1946 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1949 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1950 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1954 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_value *ucontrol)
1957 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1958 unsigned int old_mode = hook->mute_mode;
1960 hook->mute_mode = ucontrol->value.enumerated.item[0];
1961 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1962 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1963 if (old_mode == hook->mute_mode)
1965 snd_hda_sync_vmaster_hook(hook);
1969 static const struct snd_kcontrol_new vmaster_mute_mode = {
1970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1971 .name = "Mute-LED Mode",
1972 .info = vmaster_mute_mode_info,
1973 .get = vmaster_mute_mode_get,
1974 .put = vmaster_mute_mode_put,
1977 /* meta hook to call each driver's vmaster hook */
1978 static void vmaster_hook(void *private_data, int enabled)
1980 struct hda_vmaster_mute_hook *hook = private_data;
1982 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1983 enabled = hook->mute_mode;
1984 hook->hook(hook->codec, enabled);
1988 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1989 * @codec: the HDA codec
1990 * @hook: the vmaster hook object
1991 * @expose_enum_ctl: flag to create an enum ctl
1993 * Add a mute-LED hook with the given vmaster switch kctl.
1994 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1995 * created and associated with the given hook.
1997 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1998 struct hda_vmaster_mute_hook *hook,
1999 bool expose_enum_ctl)
2001 struct snd_kcontrol *kctl;
2003 if (!hook->hook || !hook->sw_kctl)
2005 hook->codec = codec;
2006 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2007 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2008 if (!expose_enum_ctl)
2010 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2013 return snd_hda_ctl_add(codec, 0, kctl);
2015 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2018 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2019 * @hook: the vmaster hook
2021 * Call the hook with the current value for synchronization.
2022 * Should be called in init callback.
2024 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2026 if (!hook->hook || !hook->codec)
2028 /* don't call vmaster hook in the destructor since it might have
2029 * been already destroyed
2031 if (hook->codec->bus->shutdown)
2033 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2035 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2039 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2040 * @kcontrol: referred ctl element
2041 * @uinfo: pointer to get/store the data
2043 * The control element is supposed to have the private_value field
2044 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2046 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_info *uinfo)
2049 int chs = get_amp_channels(kcontrol);
2051 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2052 uinfo->count = chs == 3 ? 2 : 1;
2053 uinfo->value.integer.min = 0;
2054 uinfo->value.integer.max = 1;
2057 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2060 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2061 * @kcontrol: ctl element
2062 * @ucontrol: pointer to get/store the data
2064 * The control element is supposed to have the private_value field
2065 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2067 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2068 struct snd_ctl_elem_value *ucontrol)
2070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2071 hda_nid_t nid = get_amp_nid(kcontrol);
2072 int chs = get_amp_channels(kcontrol);
2073 int dir = get_amp_direction(kcontrol);
2074 int idx = get_amp_index(kcontrol);
2075 long *valp = ucontrol->value.integer.value;
2078 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2079 HDA_AMP_MUTE) ? 0 : 1;
2081 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2082 HDA_AMP_MUTE) ? 0 : 1;
2085 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2088 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2089 * @kcontrol: ctl element
2090 * @ucontrol: pointer to get/store the data
2092 * The control element is supposed to have the private_value field
2093 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2095 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol)
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 hda_nid_t nid = get_amp_nid(kcontrol);
2100 int chs = get_amp_channels(kcontrol);
2101 int dir = get_amp_direction(kcontrol);
2102 int idx = get_amp_index(kcontrol);
2103 long *valp = ucontrol->value.integer.value;
2107 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2109 *valp ? 0 : HDA_AMP_MUTE);
2113 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2115 *valp ? 0 : HDA_AMP_MUTE);
2116 hda_call_check_power_status(codec, nid);
2119 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2122 * SPDIF out controls
2125 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_info *uinfo)
2128 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2133 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
2136 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2137 IEC958_AES0_NONAUDIO |
2138 IEC958_AES0_CON_EMPHASIS_5015 |
2139 IEC958_AES0_CON_NOT_COPYRIGHT;
2140 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2141 IEC958_AES1_CON_ORIGINAL;
2145 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_value *ucontrol)
2148 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2149 IEC958_AES0_NONAUDIO |
2150 IEC958_AES0_PRO_EMPHASIS_5015;
2154 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2158 int idx = kcontrol->private_value;
2159 struct hda_spdif_out *spdif;
2161 mutex_lock(&codec->spdif_mutex);
2162 spdif = snd_array_elem(&codec->spdif_out, idx);
2163 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2164 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2165 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2166 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2167 mutex_unlock(&codec->spdif_mutex);
2172 /* convert from SPDIF status bits to HDA SPDIF bits
2173 * bit 0 (DigEn) is always set zero (to be filled later)
2175 static unsigned short convert_from_spdif_status(unsigned int sbits)
2177 unsigned short val = 0;
2179 if (sbits & IEC958_AES0_PROFESSIONAL)
2180 val |= AC_DIG1_PROFESSIONAL;
2181 if (sbits & IEC958_AES0_NONAUDIO)
2182 val |= AC_DIG1_NONAUDIO;
2183 if (sbits & IEC958_AES0_PROFESSIONAL) {
2184 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2185 IEC958_AES0_PRO_EMPHASIS_5015)
2186 val |= AC_DIG1_EMPHASIS;
2188 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2189 IEC958_AES0_CON_EMPHASIS_5015)
2190 val |= AC_DIG1_EMPHASIS;
2191 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2192 val |= AC_DIG1_COPYRIGHT;
2193 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2194 val |= AC_DIG1_LEVEL;
2195 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2200 /* convert to SPDIF status bits from HDA SPDIF bits
2202 static unsigned int convert_to_spdif_status(unsigned short val)
2204 unsigned int sbits = 0;
2206 if (val & AC_DIG1_NONAUDIO)
2207 sbits |= IEC958_AES0_NONAUDIO;
2208 if (val & AC_DIG1_PROFESSIONAL)
2209 sbits |= IEC958_AES0_PROFESSIONAL;
2210 if (sbits & IEC958_AES0_PROFESSIONAL) {
2211 if (val & AC_DIG1_EMPHASIS)
2212 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2214 if (val & AC_DIG1_EMPHASIS)
2215 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2216 if (!(val & AC_DIG1_COPYRIGHT))
2217 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2218 if (val & AC_DIG1_LEVEL)
2219 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2220 sbits |= val & (0x7f << 8);
2225 /* set digital convert verbs both for the given NID and its slaves */
2226 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2231 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2233 d = codec->slave_dig_outs;
2237 snd_hdac_regmap_update(&codec->core, *d,
2238 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2241 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2244 unsigned int mask = 0;
2245 unsigned int val = 0;
2255 set_dig_out(codec, nid, mask, val);
2258 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
2261 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2262 int idx = kcontrol->private_value;
2263 struct hda_spdif_out *spdif;
2268 mutex_lock(&codec->spdif_mutex);
2269 spdif = snd_array_elem(&codec->spdif_out, idx);
2271 spdif->status = ucontrol->value.iec958.status[0] |
2272 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2273 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2274 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2275 val = convert_from_spdif_status(spdif->status);
2276 val |= spdif->ctls & 1;
2277 change = spdif->ctls != val;
2279 if (change && nid != (u16)-1)
2280 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2281 mutex_unlock(&codec->spdif_mutex);
2285 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2287 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
2290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2291 int idx = kcontrol->private_value;
2292 struct hda_spdif_out *spdif;
2294 mutex_lock(&codec->spdif_mutex);
2295 spdif = snd_array_elem(&codec->spdif_out, idx);
2296 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2297 mutex_unlock(&codec->spdif_mutex);
2301 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2304 set_dig_out_convert(codec, nid, dig1, dig2);
2305 /* unmute amp switch (if any) */
2306 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2307 (dig1 & AC_DIG1_ENABLE))
2308 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2312 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
2315 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2316 int idx = kcontrol->private_value;
2317 struct hda_spdif_out *spdif;
2322 mutex_lock(&codec->spdif_mutex);
2323 spdif = snd_array_elem(&codec->spdif_out, idx);
2325 val = spdif->ctls & ~AC_DIG1_ENABLE;
2326 if (ucontrol->value.integer.value[0])
2327 val |= AC_DIG1_ENABLE;
2328 change = spdif->ctls != val;
2330 if (change && nid != (u16)-1)
2331 set_spdif_ctls(codec, nid, val & 0xff, -1);
2332 mutex_unlock(&codec->spdif_mutex);
2336 static struct snd_kcontrol_new dig_mixes[] = {
2338 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2340 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2341 .info = snd_hda_spdif_mask_info,
2342 .get = snd_hda_spdif_cmask_get,
2345 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2346 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2347 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2348 .info = snd_hda_spdif_mask_info,
2349 .get = snd_hda_spdif_pmask_get,
2352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2353 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2354 .info = snd_hda_spdif_mask_info,
2355 .get = snd_hda_spdif_default_get,
2356 .put = snd_hda_spdif_default_put,
2359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2360 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2361 .info = snd_hda_spdif_out_switch_info,
2362 .get = snd_hda_spdif_out_switch_get,
2363 .put = snd_hda_spdif_out_switch_put,
2369 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2370 * @codec: the HDA codec
2371 * @associated_nid: NID that new ctls associated with
2372 * @cvt_nid: converter NID
2373 * @type: HDA_PCM_TYPE_*
2374 * Creates controls related with the digital output.
2375 * Called from each patch supporting the digital out.
2377 * Returns 0 if successful, or a negative error code.
2379 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2380 hda_nid_t associated_nid,
2385 struct snd_kcontrol *kctl;
2386 struct snd_kcontrol_new *dig_mix;
2389 const int spdif_index = 16;
2390 struct hda_spdif_out *spdif;
2391 struct hda_bus *bus = codec->bus;
2393 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2394 type == HDA_PCM_TYPE_SPDIF) {
2396 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2397 type == HDA_PCM_TYPE_HDMI) {
2398 /* suppose a single SPDIF device */
2399 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2400 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2403 kctl->id.index = spdif_index;
2405 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2407 if (!bus->primary_dig_out_type)
2408 bus->primary_dig_out_type = type;
2410 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2412 codec_err(codec, "too many IEC958 outputs\n");
2415 spdif = snd_array_new(&codec->spdif_out);
2418 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2419 kctl = snd_ctl_new1(dig_mix, codec);
2422 kctl->id.index = idx;
2423 kctl->private_value = codec->spdif_out.used - 1;
2424 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2428 spdif->nid = cvt_nid;
2429 snd_hdac_regmap_read(&codec->core, cvt_nid,
2430 AC_VERB_GET_DIGI_CONVERT_1, &val);
2432 spdif->status = convert_to_spdif_status(spdif->ctls);
2435 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2438 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2439 * @codec: the HDA codec
2442 * call within spdif_mutex lock
2444 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2448 for (i = 0; i < codec->spdif_out.used; i++) {
2449 struct hda_spdif_out *spdif =
2450 snd_array_elem(&codec->spdif_out, i);
2451 if (spdif->nid == nid)
2456 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2459 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2460 * @codec: the HDA codec
2461 * @idx: the SPDIF ctl index
2463 * Unassign the widget from the given SPDIF control.
2465 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2467 struct hda_spdif_out *spdif;
2469 mutex_lock(&codec->spdif_mutex);
2470 spdif = snd_array_elem(&codec->spdif_out, idx);
2471 spdif->nid = (u16)-1;
2472 mutex_unlock(&codec->spdif_mutex);
2474 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2477 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2478 * @codec: the HDA codec
2479 * @idx: the SPDIF ctl idx
2482 * Assign the widget to the SPDIF control with the given index.
2484 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2486 struct hda_spdif_out *spdif;
2489 mutex_lock(&codec->spdif_mutex);
2490 spdif = snd_array_elem(&codec->spdif_out, idx);
2491 if (spdif->nid != nid) {
2494 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2496 mutex_unlock(&codec->spdif_mutex);
2498 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2501 * SPDIF sharing with analog output
2503 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2504 struct snd_ctl_elem_value *ucontrol)
2506 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2507 ucontrol->value.integer.value[0] = mout->share_spdif;
2511 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
2514 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2515 mout->share_spdif = !!ucontrol->value.integer.value[0];
2519 static const struct snd_kcontrol_new spdif_share_sw = {
2520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2521 .name = "IEC958 Default PCM Playback Switch",
2522 .info = snd_ctl_boolean_mono_info,
2523 .get = spdif_share_sw_get,
2524 .put = spdif_share_sw_put,
2528 * snd_hda_create_spdif_share_sw - create Default PCM switch
2529 * @codec: the HDA codec
2530 * @mout: multi-out instance
2532 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2533 struct hda_multi_out *mout)
2535 struct snd_kcontrol *kctl;
2537 if (!mout->dig_out_nid)
2540 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2543 /* ATTENTION: here mout is passed as private_data, instead of codec */
2544 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2546 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2552 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2554 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2557 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2559 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2563 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
2566 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2567 hda_nid_t nid = kcontrol->private_value;
2568 unsigned int val = !!ucontrol->value.integer.value[0];
2571 mutex_lock(&codec->spdif_mutex);
2572 change = codec->spdif_in_enable != val;
2574 codec->spdif_in_enable = val;
2575 snd_hdac_regmap_write(&codec->core, nid,
2576 AC_VERB_SET_DIGI_CONVERT_1, val);
2578 mutex_unlock(&codec->spdif_mutex);
2582 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
2585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2586 hda_nid_t nid = kcontrol->private_value;
2590 snd_hdac_regmap_read(&codec->core, nid,
2591 AC_VERB_GET_DIGI_CONVERT_1, &val);
2592 sbits = convert_to_spdif_status(val);
2593 ucontrol->value.iec958.status[0] = sbits;
2594 ucontrol->value.iec958.status[1] = sbits >> 8;
2595 ucontrol->value.iec958.status[2] = sbits >> 16;
2596 ucontrol->value.iec958.status[3] = sbits >> 24;
2600 static struct snd_kcontrol_new dig_in_ctls[] = {
2602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2603 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2604 .info = snd_hda_spdif_in_switch_info,
2605 .get = snd_hda_spdif_in_switch_get,
2606 .put = snd_hda_spdif_in_switch_put,
2609 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2610 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2611 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2612 .info = snd_hda_spdif_mask_info,
2613 .get = snd_hda_spdif_in_status_get,
2619 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2620 * @codec: the HDA codec
2621 * @nid: audio in widget NID
2623 * Creates controls related with the SPDIF input.
2624 * Called from each patch supporting the SPDIF in.
2626 * Returns 0 if successful, or a negative error code.
2628 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2631 struct snd_kcontrol *kctl;
2632 struct snd_kcontrol_new *dig_mix;
2635 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2637 codec_err(codec, "too many IEC958 inputs\n");
2640 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2641 kctl = snd_ctl_new1(dig_mix, codec);
2644 kctl->private_value = nid;
2645 err = snd_hda_ctl_add(codec, nid, kctl);
2649 codec->spdif_in_enable =
2650 snd_hda_codec_read(codec, nid, 0,
2651 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2655 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2658 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2659 * @codec: the HDA codec
2660 * @fg: function group (not used now)
2661 * @power_state: the power state to set (AC_PWRST_*)
2663 * Set the given power state to all widgets that have the power control.
2664 * If the codec has power_filter set, it evaluates the power state and
2665 * filter out if it's unchanged as D3.
2667 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2668 unsigned int power_state)
2672 for_each_hda_codec_node(nid, codec) {
2673 unsigned int wcaps = get_wcaps(codec, nid);
2674 unsigned int state = power_state;
2675 if (!(wcaps & AC_WCAP_POWER))
2677 if (codec->power_filter) {
2678 state = codec->power_filter(codec, nid, power_state);
2679 if (state != power_state && power_state == AC_PWRST_D3)
2682 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2686 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2689 * wait until the state is reached, returns the current state
2691 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2693 unsigned int power_state)
2695 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2696 unsigned int state, actual_state;
2699 state = snd_hda_codec_read(codec, fg, 0,
2700 AC_VERB_GET_POWER_STATE, 0);
2701 if (state & AC_PWRST_ERROR)
2703 actual_state = (state >> 4) & 0x0f;
2704 if (actual_state == power_state)
2706 if (time_after_eq(jiffies, end_time))
2708 /* wait until the codec reachs to the target state */
2715 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2716 * @codec: the HDA codec
2718 * @power_state: power state to evalue
2720 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2721 * This can be used a codec power_filter callback.
2723 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2725 unsigned int power_state)
2727 if (nid == codec->core.afg || nid == codec->core.mfg)
2729 if (power_state == AC_PWRST_D3 &&
2730 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2731 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2732 int eapd = snd_hda_codec_read(codec, nid, 0,
2733 AC_VERB_GET_EAPD_BTLENABLE, 0);
2739 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2742 * set power state of the codec, and return the power state
2744 static unsigned int hda_set_power_state(struct hda_codec *codec,
2745 unsigned int power_state)
2747 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2752 /* this delay seems necessary to avoid click noise at power-down */
2753 if (power_state == AC_PWRST_D3) {
2754 if (codec->depop_delay < 0)
2755 msleep(codec_has_epss(codec) ? 10 : 100);
2756 else if (codec->depop_delay > 0)
2757 msleep(codec->depop_delay);
2758 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2761 /* repeat power states setting at most 10 times*/
2762 for (count = 0; count < 10; count++) {
2763 if (codec->patch_ops.set_power_state)
2764 codec->patch_ops.set_power_state(codec, fg,
2767 state = power_state;
2768 if (codec->power_filter)
2769 state = codec->power_filter(codec, fg, state);
2770 if (state == power_state || power_state != AC_PWRST_D3)
2771 snd_hda_codec_read(codec, fg, flags,
2772 AC_VERB_SET_POWER_STATE,
2774 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2776 state = hda_sync_power_state(codec, fg, power_state);
2777 if (!(state & AC_PWRST_ERROR))
2784 /* sync power states of all widgets;
2785 * this is called at the end of codec parsing
2787 static void sync_power_up_states(struct hda_codec *codec)
2791 /* don't care if no filter is used */
2792 if (!codec->power_filter)
2795 for_each_hda_codec_node(nid, codec) {
2796 unsigned int wcaps = get_wcaps(codec, nid);
2797 unsigned int target;
2798 if (!(wcaps & AC_WCAP_POWER))
2800 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2801 if (target == AC_PWRST_D0)
2803 if (!snd_hda_check_power_state(codec, nid, target))
2804 snd_hda_codec_write(codec, nid, 0,
2805 AC_VERB_SET_POWER_STATE, target);
2809 #ifdef CONFIG_SND_HDA_RECONFIG
2810 /* execute additional init verbs */
2811 static void hda_exec_init_verbs(struct hda_codec *codec)
2813 if (codec->init_verbs.list)
2814 snd_hda_sequence_write(codec, codec->init_verbs.list);
2817 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2821 /* update the power on/off account with the current jiffies */
2822 static void update_power_acct(struct hda_codec *codec, bool on)
2824 unsigned long delta = jiffies - codec->power_jiffies;
2827 codec->power_on_acct += delta;
2829 codec->power_off_acct += delta;
2830 codec->power_jiffies += delta;
2833 void snd_hda_update_power_acct(struct hda_codec *codec)
2835 update_power_acct(codec, hda_codec_is_power_on(codec));
2839 * call suspend and power-down; used both from PM and power-save
2840 * this function returns the power state in the end
2842 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2846 atomic_inc(&codec->core.in_pm);
2848 if (codec->patch_ops.suspend)
2849 codec->patch_ops.suspend(codec);
2850 hda_cleanup_all_streams(codec);
2851 state = hda_set_power_state(codec, AC_PWRST_D3);
2852 update_power_acct(codec, true);
2853 atomic_dec(&codec->core.in_pm);
2858 * kick up codec; used both from PM and power-save
2860 static void hda_call_codec_resume(struct hda_codec *codec)
2862 atomic_inc(&codec->core.in_pm);
2864 if (codec->core.regmap)
2865 regcache_mark_dirty(codec->core.regmap);
2867 codec->power_jiffies = jiffies;
2869 hda_set_power_state(codec, AC_PWRST_D0);
2870 restore_shutup_pins(codec);
2871 hda_exec_init_verbs(codec);
2872 snd_hda_jack_set_dirty_all(codec);
2873 if (codec->patch_ops.resume)
2874 codec->patch_ops.resume(codec);
2876 if (codec->patch_ops.init)
2877 codec->patch_ops.init(codec);
2878 if (codec->core.regmap)
2879 regcache_sync(codec->core.regmap);
2882 if (codec->jackpoll_interval)
2883 hda_jackpoll_work(&codec->jackpoll_work.work);
2885 snd_hda_jack_report_sync(codec);
2886 atomic_dec(&codec->core.in_pm);
2889 static int hda_codec_runtime_suspend(struct device *dev)
2891 struct hda_codec *codec = dev_to_hda_codec(dev);
2892 struct hda_pcm *pcm;
2895 cancel_delayed_work_sync(&codec->jackpoll_work);
2896 list_for_each_entry(pcm, &codec->pcm_list_head, list)
2897 snd_pcm_suspend_all(pcm->pcm);
2898 state = hda_call_codec_suspend(codec);
2899 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
2900 (state & AC_PWRST_CLK_STOP_OK))
2901 snd_hdac_codec_link_down(&codec->core);
2902 snd_hdac_link_power(&codec->core, false);
2906 static int hda_codec_runtime_resume(struct device *dev)
2908 struct hda_codec *codec = dev_to_hda_codec(dev);
2910 snd_hdac_link_power(&codec->core, true);
2911 snd_hdac_codec_link_up(&codec->core);
2912 hda_call_codec_resume(codec);
2913 pm_runtime_mark_last_busy(dev);
2916 #endif /* CONFIG_PM */
2918 /* referred in hda_bind.c */
2919 const struct dev_pm_ops hda_codec_driver_pm = {
2920 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2921 pm_runtime_force_resume)
2922 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
2927 * add standard channel maps if not specified
2929 static int add_std_chmaps(struct hda_codec *codec)
2931 struct hda_pcm *pcm;
2934 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
2935 for (str = 0; str < 2; str++) {
2936 struct hda_pcm_stream *hinfo = &pcm->stream[str];
2937 struct snd_pcm_chmap *chmap;
2938 const struct snd_pcm_chmap_elem *elem;
2940 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
2942 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
2943 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
2944 hinfo->channels_max,
2948 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
2954 /* default channel maps for 2.1 speakers;
2955 * since HD-audio supports only stereo, odd number channels are omitted
2957 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
2959 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2961 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2962 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
2965 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
2967 int snd_hda_codec_build_controls(struct hda_codec *codec)
2970 hda_exec_init_verbs(codec);
2971 /* continue to initialize... */
2972 if (codec->patch_ops.init)
2973 err = codec->patch_ops.init(codec);
2974 if (!err && codec->patch_ops.build_controls)
2975 err = codec->patch_ops.build_controls(codec);
2979 /* we create chmaps here instead of build_pcms */
2980 err = add_std_chmaps(codec);
2984 if (codec->jackpoll_interval)
2985 hda_jackpoll_work(&codec->jackpoll_work.work);
2987 snd_hda_jack_report_sync(codec); /* call at the last init point */
2988 sync_power_up_states(codec);
2995 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2996 struct hda_codec *codec,
2997 struct snd_pcm_substream *substream)
3002 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3003 struct hda_codec *codec,
3004 unsigned int stream_tag,
3005 unsigned int format,
3006 struct snd_pcm_substream *substream)
3008 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3012 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3013 struct hda_codec *codec,
3014 struct snd_pcm_substream *substream)
3016 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3020 static int set_pcm_default_values(struct hda_codec *codec,
3021 struct hda_pcm_stream *info)
3025 /* query support PCM information from the given NID */
3026 if (info->nid && (!info->rates || !info->formats)) {
3027 err = snd_hda_query_supported_pcm(codec, info->nid,
3028 info->rates ? NULL : &info->rates,
3029 info->formats ? NULL : &info->formats,
3030 info->maxbps ? NULL : &info->maxbps);
3034 if (info->ops.open == NULL)
3035 info->ops.open = hda_pcm_default_open_close;
3036 if (info->ops.close == NULL)
3037 info->ops.close = hda_pcm_default_open_close;
3038 if (info->ops.prepare == NULL) {
3039 if (snd_BUG_ON(!info->nid))
3041 info->ops.prepare = hda_pcm_default_prepare;
3043 if (info->ops.cleanup == NULL) {
3044 if (snd_BUG_ON(!info->nid))
3046 info->ops.cleanup = hda_pcm_default_cleanup;
3052 * codec prepare/cleanup entries
3055 * snd_hda_codec_prepare - Prepare a stream
3056 * @codec: the HDA codec
3057 * @hinfo: PCM information
3058 * @stream: stream tag to assign
3059 * @format: format id to assign
3060 * @substream: PCM substream to assign
3062 * Calls the prepare callback set by the codec with the given arguments.
3063 * Clean up the inactive streams when successful.
3065 int snd_hda_codec_prepare(struct hda_codec *codec,
3066 struct hda_pcm_stream *hinfo,
3067 unsigned int stream,
3068 unsigned int format,
3069 struct snd_pcm_substream *substream)
3072 mutex_lock(&codec->bus->prepare_mutex);
3073 if (hinfo->ops.prepare)
3074 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3079 purify_inactive_streams(codec);
3080 mutex_unlock(&codec->bus->prepare_mutex);
3083 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3086 * snd_hda_codec_cleanup - Prepare a stream
3087 * @codec: the HDA codec
3088 * @hinfo: PCM information
3089 * @substream: PCM substream
3091 * Calls the cleanup callback set by the codec with the given arguments.
3093 void snd_hda_codec_cleanup(struct hda_codec *codec,
3094 struct hda_pcm_stream *hinfo,
3095 struct snd_pcm_substream *substream)
3097 mutex_lock(&codec->bus->prepare_mutex);
3098 if (hinfo->ops.cleanup)
3099 hinfo->ops.cleanup(hinfo, codec, substream);
3100 mutex_unlock(&codec->bus->prepare_mutex);
3102 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3105 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3106 "Audio", "SPDIF", "HDMI", "Modem"
3110 * get the empty PCM device number to assign
3112 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3114 /* audio device indices; not linear to keep compatibility */
3115 /* assigned to static slots up to dev#10; if more needed, assign
3116 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3118 static int audio_idx[HDA_PCM_NTYPES][5] = {
3119 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3120 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3121 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3122 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3126 if (type >= HDA_PCM_NTYPES) {
3127 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3131 for (i = 0; audio_idx[type][i] >= 0; i++) {
3132 #ifndef CONFIG_SND_DYNAMIC_MINORS
3133 if (audio_idx[type][i] >= 8)
3136 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3137 return audio_idx[type][i];
3140 #ifdef CONFIG_SND_DYNAMIC_MINORS
3141 /* non-fixed slots starting from 10 */
3142 for (i = 10; i < 32; i++) {
3143 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3148 dev_warn(bus->card->dev, "Too many %s devices\n",
3149 snd_hda_pcm_type_name[type]);
3150 #ifndef CONFIG_SND_DYNAMIC_MINORS
3151 dev_warn(bus->card->dev,
3152 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3157 /* call build_pcms ops of the given codec and set up the default parameters */
3158 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3160 struct hda_pcm *cpcm;
3163 if (!list_empty(&codec->pcm_list_head))
3164 return 0; /* already parsed */
3166 if (!codec->patch_ops.build_pcms)
3169 err = codec->patch_ops.build_pcms(codec);
3171 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3172 codec->core.addr, err);
3176 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3179 for (stream = 0; stream < 2; stream++) {
3180 struct hda_pcm_stream *info = &cpcm->stream[stream];
3182 if (!info->substreams)
3184 err = set_pcm_default_values(codec, info);
3187 "fail to setup default for PCM %s\n",
3197 /* assign all PCMs of the given codec */
3198 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3200 struct hda_bus *bus = codec->bus;
3201 struct hda_pcm *cpcm;
3204 err = snd_hda_codec_parse_pcms(codec);
3208 /* attach a new PCM streams */
3209 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3211 continue; /* already attached */
3212 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3213 continue; /* no substreams assigned */
3215 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3217 continue; /* no fatal error */
3219 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3222 "cannot attach PCM stream %d for codec #%d\n",
3223 dev, codec->core.addr);
3224 continue; /* no fatal error */
3232 * snd_hda_add_new_ctls - create controls from the array
3233 * @codec: the HDA codec
3234 * @knew: the array of struct snd_kcontrol_new
3236 * This helper function creates and add new controls in the given array.
3237 * The array must be terminated with an empty entry as terminator.
3239 * Returns 0 if successful, or a negative error code.
3241 int snd_hda_add_new_ctls(struct hda_codec *codec,
3242 const struct snd_kcontrol_new *knew)
3246 for (; knew->name; knew++) {
3247 struct snd_kcontrol *kctl;
3248 int addr = 0, idx = 0;
3249 if (knew->iface == -1) /* skip this codec private value */
3252 kctl = snd_ctl_new1(knew, codec);
3256 kctl->id.device = addr;
3258 kctl->id.index = idx;
3259 err = snd_hda_ctl_add(codec, 0, kctl);
3262 /* try first with another device index corresponding to
3263 * the codec addr; if it still fails (or it's the
3264 * primary codec), then try another control index
3266 if (!addr && codec->core.addr)
3267 addr = codec->core.addr;
3268 else if (!idx && !knew->index) {
3269 idx = find_empty_mixer_ctl_idx(codec,
3279 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3282 static void codec_set_power_save(struct hda_codec *codec, int delay)
3284 struct device *dev = hda_codec_dev(codec);
3286 if (delay == 0 && codec->auto_runtime_pm)
3290 pm_runtime_set_autosuspend_delay(dev, delay);
3291 pm_runtime_use_autosuspend(dev);
3292 pm_runtime_allow(dev);
3293 if (!pm_runtime_suspended(dev))
3294 pm_runtime_mark_last_busy(dev);
3296 pm_runtime_dont_use_autosuspend(dev);
3297 pm_runtime_forbid(dev);
3302 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3303 * @bus: HD-audio bus
3304 * @delay: autosuspend delay in msec, 0 = off
3306 * Synchronize the runtime PM autosuspend state from the power_save option.
3308 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3310 struct hda_codec *c;
3312 list_for_each_codec(c, bus)
3313 codec_set_power_save(c, delay);
3315 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3318 * snd_hda_check_amp_list_power - Check the amp list and update the power
3319 * @codec: HD-audio codec
3320 * @check: the object containing an AMP list and the status
3321 * @nid: NID to check / update
3323 * Check whether the given NID is in the amp list. If it's in the list,
3324 * check the current AMP status, and update the the power-status according
3325 * to the mute status.
3327 * This function is supposed to be set or called from the check_power_status
3330 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3331 struct hda_loopback_check *check,
3334 const struct hda_amp_list *p;
3337 if (!check->amplist)
3339 for (p = check->amplist; p->nid; p++) {
3344 return 0; /* nothing changed */
3346 for (p = check->amplist; p->nid; p++) {
3347 for (ch = 0; ch < 2; ch++) {
3348 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3350 if (!(v & HDA_AMP_MUTE) && v > 0) {
3351 if (!check->power_on) {
3352 check->power_on = 1;
3353 snd_hda_power_up_pm(codec);
3359 if (check->power_on) {
3360 check->power_on = 0;
3361 snd_hda_power_down_pm(codec);
3365 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3373 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3374 * @imux: imux helper object
3375 * @uinfo: pointer to get/store the data
3377 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3378 struct snd_ctl_elem_info *uinfo)
3382 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3384 uinfo->value.enumerated.items = imux->num_items;
3385 if (!imux->num_items)
3387 index = uinfo->value.enumerated.item;
3388 if (index >= imux->num_items)
3389 index = imux->num_items - 1;
3390 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3393 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3396 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3397 * @codec: the HDA codec
3398 * @imux: imux helper object
3399 * @ucontrol: pointer to get/store the data
3400 * @nid: input mux NID
3401 * @cur_val: pointer to get/store the current imux value
3403 int snd_hda_input_mux_put(struct hda_codec *codec,
3404 const struct hda_input_mux *imux,
3405 struct snd_ctl_elem_value *ucontrol,
3407 unsigned int *cur_val)
3411 if (!imux->num_items)
3413 idx = ucontrol->value.enumerated.item[0];
3414 if (idx >= imux->num_items)
3415 idx = imux->num_items - 1;
3416 if (*cur_val == idx)
3418 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3419 imux->items[idx].index);
3423 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3427 * snd_hda_enum_helper_info - Helper for simple enum ctls
3428 * @kcontrol: ctl element
3429 * @uinfo: pointer to get/store the data
3430 * @num_items: number of enum items
3431 * @texts: enum item string array
3433 * process kcontrol info callback of a simple string enum array
3434 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3436 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_info *uinfo,
3438 int num_items, const char * const *texts)
3440 static const char * const texts_default[] = {
3441 "Disabled", "Enabled"
3444 if (!texts || !num_items) {
3446 texts = texts_default;
3449 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3451 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3454 * Multi-channel / digital-out PCM helper functions
3457 /* setup SPDIF output stream */
3458 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3459 unsigned int stream_tag, unsigned int format)
3461 struct hda_spdif_out *spdif;
3462 unsigned int curr_fmt;
3465 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3466 /* Add sanity check to pass klockwork check.
3467 * This should never happen.
3469 if (WARN_ON(spdif == NULL))
3472 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3473 AC_VERB_GET_STREAM_FORMAT, 0);
3474 reset = codec->spdif_status_reset &&
3475 (spdif->ctls & AC_DIG1_ENABLE) &&
3478 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3481 set_dig_out_convert(codec, nid,
3482 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3484 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3485 if (codec->slave_dig_outs) {
3487 for (d = codec->slave_dig_outs; *d; d++)
3488 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3491 /* turn on again (if needed) */
3493 set_dig_out_convert(codec, nid,
3494 spdif->ctls & 0xff, -1);
3497 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3499 snd_hda_codec_cleanup_stream(codec, nid);
3500 if (codec->slave_dig_outs) {
3502 for (d = codec->slave_dig_outs; *d; d++)
3503 snd_hda_codec_cleanup_stream(codec, *d);
3508 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3509 * @codec: the HDA codec
3510 * @mout: hda_multi_out object
3512 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3513 struct hda_multi_out *mout)
3515 mutex_lock(&codec->spdif_mutex);
3516 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3517 /* already opened as analog dup; reset it once */
3518 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3519 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3520 mutex_unlock(&codec->spdif_mutex);
3523 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3526 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3527 * @codec: the HDA codec
3528 * @mout: hda_multi_out object
3529 * @stream_tag: stream tag to assign
3530 * @format: format id to assign
3531 * @substream: PCM substream to assign
3533 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3534 struct hda_multi_out *mout,
3535 unsigned int stream_tag,
3536 unsigned int format,
3537 struct snd_pcm_substream *substream)
3539 mutex_lock(&codec->spdif_mutex);
3540 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3541 mutex_unlock(&codec->spdif_mutex);
3544 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3547 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3548 * @codec: the HDA codec
3549 * @mout: hda_multi_out object
3551 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3552 struct hda_multi_out *mout)
3554 mutex_lock(&codec->spdif_mutex);
3555 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3556 mutex_unlock(&codec->spdif_mutex);
3559 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3562 * snd_hda_multi_out_dig_close - release the digital out stream
3563 * @codec: the HDA codec
3564 * @mout: hda_multi_out object
3566 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3567 struct hda_multi_out *mout)
3569 mutex_lock(&codec->spdif_mutex);
3570 mout->dig_out_used = 0;
3571 mutex_unlock(&codec->spdif_mutex);
3574 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3577 * snd_hda_multi_out_analog_open - open analog outputs
3578 * @codec: the HDA codec
3579 * @mout: hda_multi_out object
3580 * @substream: PCM substream to assign
3581 * @hinfo: PCM information to assign
3583 * Open analog outputs and set up the hw-constraints.
3584 * If the digital outputs can be opened as slave, open the digital
3587 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3588 struct hda_multi_out *mout,
3589 struct snd_pcm_substream *substream,
3590 struct hda_pcm_stream *hinfo)
3592 struct snd_pcm_runtime *runtime = substream->runtime;
3593 runtime->hw.channels_max = mout->max_channels;
3594 if (mout->dig_out_nid) {
3595 if (!mout->analog_rates) {
3596 mout->analog_rates = hinfo->rates;
3597 mout->analog_formats = hinfo->formats;
3598 mout->analog_maxbps = hinfo->maxbps;
3600 runtime->hw.rates = mout->analog_rates;
3601 runtime->hw.formats = mout->analog_formats;
3602 hinfo->maxbps = mout->analog_maxbps;
3604 if (!mout->spdif_rates) {
3605 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3607 &mout->spdif_formats,
3608 &mout->spdif_maxbps);
3610 mutex_lock(&codec->spdif_mutex);
3611 if (mout->share_spdif) {
3612 if ((runtime->hw.rates & mout->spdif_rates) &&
3613 (runtime->hw.formats & mout->spdif_formats)) {
3614 runtime->hw.rates &= mout->spdif_rates;
3615 runtime->hw.formats &= mout->spdif_formats;
3616 if (mout->spdif_maxbps < hinfo->maxbps)
3617 hinfo->maxbps = mout->spdif_maxbps;
3619 mout->share_spdif = 0;
3620 /* FIXME: need notify? */
3623 mutex_unlock(&codec->spdif_mutex);
3625 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3626 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3628 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3631 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3632 * @codec: the HDA codec
3633 * @mout: hda_multi_out object
3634 * @stream_tag: stream tag to assign
3635 * @format: format id to assign
3636 * @substream: PCM substream to assign
3638 * Set up the i/o for analog out.
3639 * When the digital out is available, copy the front out to digital out, too.
3641 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3642 struct hda_multi_out *mout,
3643 unsigned int stream_tag,
3644 unsigned int format,
3645 struct snd_pcm_substream *substream)
3647 const hda_nid_t *nids = mout->dac_nids;
3648 int chs = substream->runtime->channels;
3649 struct hda_spdif_out *spdif;
3652 mutex_lock(&codec->spdif_mutex);
3653 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3654 if (mout->dig_out_nid && mout->share_spdif &&
3655 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3656 if (chs == 2 && spdif != NULL &&
3657 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3659 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3660 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3661 setup_dig_out_stream(codec, mout->dig_out_nid,
3662 stream_tag, format);
3664 mout->dig_out_used = 0;
3665 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3668 mutex_unlock(&codec->spdif_mutex);
3671 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3673 if (!mout->no_share_stream &&
3674 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3675 /* headphone out will just decode front left/right (stereo) */
3676 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3678 /* extra outputs copied from front */
3679 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3680 if (!mout->no_share_stream && mout->hp_out_nid[i])
3681 snd_hda_codec_setup_stream(codec,
3682 mout->hp_out_nid[i],
3683 stream_tag, 0, format);
3686 for (i = 1; i < mout->num_dacs; i++) {
3687 if (chs >= (i + 1) * 2) /* independent out */
3688 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3690 else if (!mout->no_share_stream) /* copy front */
3691 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3695 /* extra surrounds */
3696 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3698 if (!mout->extra_out_nid[i])
3700 if (chs >= (i + 1) * 2)
3702 else if (!mout->no_share_stream)
3704 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3705 stream_tag, ch, format);
3710 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3713 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3714 * @codec: the HDA codec
3715 * @mout: hda_multi_out object
3717 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3718 struct hda_multi_out *mout)
3720 const hda_nid_t *nids = mout->dac_nids;
3723 for (i = 0; i < mout->num_dacs; i++)
3724 snd_hda_codec_cleanup_stream(codec, nids[i]);
3726 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3727 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3728 if (mout->hp_out_nid[i])
3729 snd_hda_codec_cleanup_stream(codec,
3730 mout->hp_out_nid[i]);
3731 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3732 if (mout->extra_out_nid[i])
3733 snd_hda_codec_cleanup_stream(codec,
3734 mout->extra_out_nid[i]);
3735 mutex_lock(&codec->spdif_mutex);
3736 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3737 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3738 mout->dig_out_used = 0;
3740 mutex_unlock(&codec->spdif_mutex);
3743 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3746 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3747 * @codec: the HDA codec
3748 * @pin: referred pin NID
3750 * Guess the suitable VREF pin bits to be set as the pin-control value.
3751 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3753 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3755 unsigned int pincap;
3756 unsigned int oldval;
3757 oldval = snd_hda_codec_read(codec, pin, 0,
3758 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3759 pincap = snd_hda_query_pin_caps(codec, pin);
3760 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3761 /* Exception: if the default pin setup is vref50, we give it priority */
3762 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3763 return AC_PINCTL_VREF_80;
3764 else if (pincap & AC_PINCAP_VREF_50)
3765 return AC_PINCTL_VREF_50;
3766 else if (pincap & AC_PINCAP_VREF_100)
3767 return AC_PINCTL_VREF_100;
3768 else if (pincap & AC_PINCAP_VREF_GRD)
3769 return AC_PINCTL_VREF_GRD;
3770 return AC_PINCTL_VREF_HIZ;
3772 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3775 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3776 * @codec: the HDA codec
3777 * @pin: referred pin NID
3778 * @val: pin ctl value to audit
3780 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3781 hda_nid_t pin, unsigned int val)
3783 static unsigned int cap_lists[][2] = {
3784 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3785 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3786 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3787 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3793 cap = snd_hda_query_pin_caps(codec, pin);
3795 return val; /* don't know what to do... */
3797 if (val & AC_PINCTL_OUT_EN) {
3798 if (!(cap & AC_PINCAP_OUT))
3799 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3800 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3801 val &= ~AC_PINCTL_HP_EN;
3804 if (val & AC_PINCTL_IN_EN) {
3805 if (!(cap & AC_PINCAP_IN))
3806 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3808 unsigned int vcap, vref;
3810 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3811 vref = val & AC_PINCTL_VREFEN;
3812 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3813 if (vref == cap_lists[i][0] &&
3814 !(vcap & cap_lists[i][1])) {
3815 if (i == ARRAY_SIZE(cap_lists) - 1)
3816 vref = AC_PINCTL_VREF_HIZ;
3818 vref = cap_lists[i + 1][0];
3821 val &= ~AC_PINCTL_VREFEN;
3828 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
3831 * _snd_hda_pin_ctl - Helper to set pin ctl value
3832 * @codec: the HDA codec
3833 * @pin: referred pin NID
3834 * @val: pin control value to set
3835 * @cached: access over codec pinctl cache or direct write
3837 * This function is a helper to set a pin ctl value more safely.
3838 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3839 * value in pin target array via snd_hda_codec_set_pin_target(), then
3840 * actually writes the value via either snd_hda_codec_update_cache() or
3841 * snd_hda_codec_write() depending on @cached flag.
3843 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3844 unsigned int val, bool cached)
3846 val = snd_hda_correct_pin_ctl(codec, pin, val);
3847 snd_hda_codec_set_pin_target(codec, pin, val);
3849 return snd_hda_codec_update_cache(codec, pin, 0,
3850 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3852 return snd_hda_codec_write(codec, pin, 0,
3853 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3855 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
3858 * snd_hda_add_imux_item - Add an item to input_mux
3859 * @codec: the HDA codec
3860 * @imux: imux helper object
3861 * @label: the name of imux item to assign
3862 * @index: index number of imux item to assign
3863 * @type_idx: pointer to store the resultant label index
3865 * When the same label is used already in the existing items, the number
3866 * suffix is appended to the label. This label index number is stored
3867 * to type_idx when non-NULL pointer is given.
3869 int snd_hda_add_imux_item(struct hda_codec *codec,
3870 struct hda_input_mux *imux, const char *label,
3871 int index, int *type_idx)
3873 int i, label_idx = 0;
3874 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
3875 codec_err(codec, "hda_codec: Too many imux items!\n");
3878 for (i = 0; i < imux->num_items; i++) {
3879 if (!strncmp(label, imux->items[i].label, strlen(label)))
3883 *type_idx = label_idx;
3885 snprintf(imux->items[imux->num_items].label,
3886 sizeof(imux->items[imux->num_items].label),
3887 "%s %d", label, label_idx);
3889 strlcpy(imux->items[imux->num_items].label, label,
3890 sizeof(imux->items[imux->num_items].label));
3891 imux->items[imux->num_items].index = index;
3895 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
3898 * snd_hda_bus_reset_codecs - Reset the bus
3899 * @bus: HD-audio bus
3901 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
3903 struct hda_codec *codec;
3905 list_for_each_codec(codec, bus) {
3906 /* FIXME: maybe a better way needed for forced reset */
3907 cancel_delayed_work_sync(&codec->jackpoll_work);
3909 if (hda_codec_is_power_on(codec)) {
3910 hda_call_codec_suspend(codec);
3911 hda_call_codec_resume(codec);
3918 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
3919 * @pcm: PCM caps bits
3920 * @buf: the string buffer to write
3921 * @buflen: the max buffer length
3923 * used by hda_proc.c and hda_eld.c
3925 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3927 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3930 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3931 if (pcm & (AC_SUPPCM_BITS_8 << i))
3932 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3934 buf[j] = '\0'; /* necessary when j == 0 */
3936 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
3938 MODULE_DESCRIPTION("HDA codec core");
3939 MODULE_LICENSE("GPL");