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
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * Send and receive a verb - passed to exec_verb override for hdac_device
59 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
60 unsigned int flags, unsigned int *res)
62 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
63 struct hda_bus *bus = codec->bus;
70 snd_hda_power_up_pm(codec);
71 mutex_lock(&bus->core.cmd_mutex);
72 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
73 bus->no_response_fallback = 1;
74 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
76 bus->no_response_fallback = 0;
77 mutex_unlock(&bus->core.cmd_mutex);
78 snd_hda_power_down_pm(codec);
79 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
80 if (bus->response_reset) {
82 "resetting BUS due to fatal communication error\n");
83 snd_hda_bus_reset(bus);
87 /* clear reset-flag when the communication gets recovered */
88 if (!err || codec_in_pm(codec))
89 bus->response_reset = 0;
94 * snd_hda_sequence_write - sequence writes
95 * @codec: the HDA codec
96 * @seq: VERB array to send
98 * Send the commands sequentially from the given array.
99 * The array must be terminated with NID=0.
101 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
103 for (; seq->nid; seq++)
104 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
106 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
108 /* connection list element */
109 struct hda_conn_list {
110 struct list_head list;
116 /* look up the cached results */
117 static struct hda_conn_list *
118 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
120 struct hda_conn_list *p;
121 list_for_each_entry(p, &codec->conn_list, list) {
128 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
129 const hda_nid_t *list)
131 struct hda_conn_list *p;
133 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
138 memcpy(p->conns, list, len * sizeof(hda_nid_t));
139 list_add(&p->list, &codec->conn_list);
143 static void remove_conn_list(struct hda_codec *codec)
145 while (!list_empty(&codec->conn_list)) {
146 struct hda_conn_list *p;
147 p = list_first_entry(&codec->conn_list, typeof(*p), list);
153 /* read the connection and add to the cache */
154 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
157 hda_nid_t *result = list;
160 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
161 if (len == -ENOSPC) {
162 len = snd_hda_get_num_raw_conns(codec, nid);
163 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
166 len = snd_hda_get_raw_connections(codec, nid, result, len);
169 len = snd_hda_override_conn_list(codec, nid, len, result);
176 * snd_hda_get_conn_list - get connection list
177 * @codec: the HDA codec
179 * @listp: the pointer to store NID list
181 * Parses the connection list of the given widget and stores the pointer
182 * to the list of NIDs.
184 * Returns the number of connections, or a negative error code.
186 * Note that the returned pointer isn't protected against the list
187 * modification. If snd_hda_override_conn_list() might be called
188 * concurrently, protect with a mutex appropriately.
190 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
191 const hda_nid_t **listp)
197 const struct hda_conn_list *p;
199 /* if the connection-list is already cached, read it */
200 p = lookup_conn_list(codec, nid);
206 if (snd_BUG_ON(added))
209 err = read_and_add_raw_conns(codec, nid);
215 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
218 * snd_hda_get_connections - copy connection list
219 * @codec: the HDA codec
221 * @conn_list: connection list array; when NULL, checks only the size
222 * @max_conns: max. number of connections to store
224 * Parses the connection list of the given widget and stores the list
227 * Returns the number of connections, or a negative error code.
229 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
230 hda_nid_t *conn_list, int max_conns)
232 const hda_nid_t *list;
233 int len = snd_hda_get_conn_list(codec, nid, &list);
235 if (len > 0 && conn_list) {
236 if (len > max_conns) {
237 codec_err(codec, "Too many connections %d for NID 0x%x\n",
241 memcpy(conn_list, list, len * sizeof(hda_nid_t));
246 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
249 * snd_hda_override_conn_list - add/modify the connection-list to cache
250 * @codec: the HDA codec
252 * @len: number of connection list entries
253 * @list: the list of connection entries
255 * Add or modify the given connection-list to the cache. If the corresponding
256 * cache already exists, invalidate it and append a new one.
258 * Returns zero or a negative error code.
260 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
261 const hda_nid_t *list)
263 struct hda_conn_list *p;
265 p = lookup_conn_list(codec, nid);
271 return add_conn_list(codec, nid, len, list);
273 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
276 * snd_hda_get_conn_index - get the connection index of the given NID
277 * @codec: the HDA codec
278 * @mux: NID containing the list
279 * @nid: NID to select
280 * @recursive: 1 when searching NID recursively, otherwise 0
282 * Parses the connection list of the widget @mux and checks whether the
283 * widget @nid is present. If it is, return the connection index.
284 * Otherwise it returns -1.
286 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
287 hda_nid_t nid, int recursive)
289 const hda_nid_t *conn;
292 nums = snd_hda_get_conn_list(codec, mux, &conn);
293 for (i = 0; i < nums; i++)
298 if (recursive > 10) {
299 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
303 for (i = 0; i < nums; i++) {
304 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
305 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
307 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
312 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
315 /* return DEVLIST_LEN parameter of the given widget */
316 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
318 unsigned int wcaps = get_wcaps(codec, nid);
321 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
322 get_wcaps_type(wcaps) != AC_WID_PIN)
325 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
328 return parm & AC_DEV_LIST_LEN_MASK;
332 * snd_hda_get_devices - copy device list without cache
333 * @codec: the HDA codec
334 * @nid: NID of the pin to parse
335 * @dev_list: device list array
336 * @max_devices: max. number of devices to store
338 * Copy the device list. This info is dynamic and so not cached.
339 * Currently called only from hda_proc.c, so not exported.
341 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
342 u8 *dev_list, int max_devices)
345 int i, dev_len, devices;
347 parm = get_num_devices(codec, nid);
348 if (!parm) /* not multi-stream capable */
352 dev_len = dev_len < max_devices ? dev_len : max_devices;
355 while (devices < dev_len) {
356 if (snd_hdac_read(&codec->core, nid,
357 AC_VERB_GET_DEVICE_LIST, devices, &parm))
360 for (i = 0; i < 8; i++) {
361 dev_list[devices] = (u8)parm;
364 if (devices >= dev_len)
372 * read widget caps for each widget and store in cache
374 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
379 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
382 nid = codec->core.start_nid;
383 for (i = 0; i < codec->core.num_nodes; i++, nid++)
384 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
385 nid, AC_PAR_AUDIO_WIDGET_CAP);
389 /* read all pin default configurations and save codec->init_pins */
390 static int read_pin_defaults(struct hda_codec *codec)
394 for_each_hda_codec_node(nid, codec) {
395 struct hda_pincfg *pin;
396 unsigned int wcaps = get_wcaps(codec, nid);
397 unsigned int wid_type = get_wcaps_type(wcaps);
398 if (wid_type != AC_WID_PIN)
400 pin = snd_array_new(&codec->init_pins);
404 pin->cfg = snd_hda_codec_read(codec, nid, 0,
405 AC_VERB_GET_CONFIG_DEFAULT, 0);
406 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
407 AC_VERB_GET_PIN_WIDGET_CONTROL,
413 /* look up the given pin config list and return the item matching with NID */
414 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
415 struct snd_array *array,
419 for (i = 0; i < array->used; i++) {
420 struct hda_pincfg *pin = snd_array_elem(array, i);
427 /* set the current pin config value for the given NID.
428 * the value is cached, and read via snd_hda_codec_get_pincfg()
430 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
431 hda_nid_t nid, unsigned int cfg)
433 struct hda_pincfg *pin;
435 /* the check below may be invalid when pins are added by a fixup
436 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
440 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
444 pin = look_up_pincfg(codec, list, nid);
446 pin = snd_array_new(list);
456 * snd_hda_codec_set_pincfg - Override a pin default configuration
457 * @codec: the HDA codec
458 * @nid: NID to set the pin config
459 * @cfg: the pin default config value
461 * Override a pin default configuration value in the cache.
462 * This value can be read by snd_hda_codec_get_pincfg() in a higher
463 * priority than the real hardware value.
465 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
466 hda_nid_t nid, unsigned int cfg)
468 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
470 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
473 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
474 * @codec: the HDA codec
475 * @nid: NID to get the pin config
477 * Get the current pin config value of the given pin NID.
478 * If the pincfg value is cached or overridden via sysfs or driver,
479 * returns the cached value.
481 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
483 struct hda_pincfg *pin;
485 #ifdef CONFIG_SND_HDA_RECONFIG
487 unsigned int cfg = 0;
488 mutex_lock(&codec->user_mutex);
489 pin = look_up_pincfg(codec, &codec->user_pins, nid);
492 mutex_unlock(&codec->user_mutex);
497 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
500 pin = look_up_pincfg(codec, &codec->init_pins, nid);
505 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
508 * snd_hda_codec_set_pin_target - remember the current pinctl target value
509 * @codec: the HDA codec
511 * @val: assigned pinctl value
513 * This function stores the given value to a pinctl target value in the
514 * pincfg table. This isn't always as same as the actually written value
515 * but can be referred at any time via snd_hda_codec_get_pin_target().
517 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
520 struct hda_pincfg *pin;
522 pin = look_up_pincfg(codec, &codec->init_pins, nid);
528 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
531 * snd_hda_codec_get_pin_target - return the current pinctl target value
532 * @codec: the HDA codec
535 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
537 struct hda_pincfg *pin;
539 pin = look_up_pincfg(codec, &codec->init_pins, nid);
544 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
547 * snd_hda_shutup_pins - Shut up all pins
548 * @codec: the HDA codec
550 * Clear all pin controls to shup up before suspend for avoiding click noise.
551 * The controls aren't cached so that they can be resumed properly.
553 void snd_hda_shutup_pins(struct hda_codec *codec)
556 /* don't shut up pins when unloading the driver; otherwise it breaks
557 * the default pin setup at the next load of the driver
559 if (codec->bus->shutdown)
561 for (i = 0; i < codec->init_pins.used; i++) {
562 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
563 /* use read here for syncing after issuing each verb */
564 snd_hda_codec_read(codec, pin->nid, 0,
565 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
567 codec->pins_shutup = 1;
569 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
572 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
573 static void restore_shutup_pins(struct hda_codec *codec)
576 if (!codec->pins_shutup)
578 if (codec->bus->shutdown)
580 for (i = 0; i < codec->init_pins.used; i++) {
581 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
582 snd_hda_codec_write(codec, pin->nid, 0,
583 AC_VERB_SET_PIN_WIDGET_CONTROL,
586 codec->pins_shutup = 0;
590 static void hda_jackpoll_work(struct work_struct *work)
592 struct hda_codec *codec =
593 container_of(work, struct hda_codec, jackpoll_work.work);
595 snd_hda_jack_set_dirty_all(codec);
596 snd_hda_jack_poll_all(codec);
598 if (!codec->jackpoll_interval)
601 schedule_delayed_work(&codec->jackpoll_work,
602 codec->jackpoll_interval);
605 /* release all pincfg lists */
606 static void free_init_pincfgs(struct hda_codec *codec)
608 snd_array_free(&codec->driver_pins);
609 #ifdef CONFIG_SND_HDA_RECONFIG
610 snd_array_free(&codec->user_pins);
612 snd_array_free(&codec->init_pins);
616 * audio-converter setup caches
618 struct hda_cvt_setup {
623 unsigned char active; /* cvt is currently used */
624 unsigned char dirty; /* setups should be cleared */
627 /* get or create a cache entry for the given audio converter NID */
628 static struct hda_cvt_setup *
629 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
631 struct hda_cvt_setup *p;
634 for (i = 0; i < codec->cvt_setups.used; i++) {
635 p = snd_array_elem(&codec->cvt_setups, i);
639 p = snd_array_new(&codec->cvt_setups);
648 static void release_pcm(struct kref *kref)
650 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
653 snd_device_free(pcm->codec->card, pcm->pcm);
654 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
659 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
661 kref_put(&pcm->kref, release_pcm);
663 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
665 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
666 const char *fmt, ...)
671 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
676 kref_init(&pcm->kref);
678 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
685 list_add_tail(&pcm->list, &codec->pcm_list_head);
688 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
693 static void codec_release_pcms(struct hda_codec *codec)
695 struct hda_pcm *pcm, *n;
697 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
698 list_del_init(&pcm->list);
700 snd_device_disconnect(codec->card, pcm->pcm);
701 snd_hda_codec_pcm_put(pcm);
705 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
707 if (codec->registered) {
708 /* pm_runtime_put() is called in snd_hdac_device_exit() */
709 pm_runtime_get_noresume(hda_codec_dev(codec));
710 pm_runtime_disable(hda_codec_dev(codec));
711 codec->registered = 0;
714 cancel_delayed_work_sync(&codec->jackpoll_work);
715 if (!codec->in_freeing)
716 snd_hda_ctls_clear(codec);
717 codec_release_pcms(codec);
718 snd_hda_detach_beep_device(codec);
719 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
720 snd_hda_jack_tbl_clear(codec);
721 codec->proc_widget_hook = NULL;
724 /* free only driver_pins so that init_pins + user_pins are restored */
725 snd_array_free(&codec->driver_pins);
726 snd_array_free(&codec->cvt_setups);
727 snd_array_free(&codec->spdif_out);
728 snd_array_free(&codec->verbs);
729 codec->preset = NULL;
730 codec->slave_dig_outs = NULL;
731 codec->spdif_status_reset = 0;
732 snd_array_free(&codec->mixers);
733 snd_array_free(&codec->nids);
734 remove_conn_list(codec);
735 snd_hdac_regmap_exit(&codec->core);
738 static unsigned int hda_set_power_state(struct hda_codec *codec,
739 unsigned int power_state);
741 /* also called from hda_bind.c */
742 void snd_hda_codec_register(struct hda_codec *codec)
744 if (codec->registered)
746 if (device_is_registered(hda_codec_dev(codec))) {
747 snd_hda_register_beep_device(codec);
748 snd_hdac_link_power(&codec->core, true);
749 pm_runtime_enable(hda_codec_dev(codec));
750 /* it was powered up in snd_hda_codec_new(), now all done */
751 snd_hda_power_down(codec);
752 codec->registered = 1;
756 static int snd_hda_codec_dev_register(struct snd_device *device)
758 snd_hda_codec_register(device->device_data);
762 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
764 struct hda_codec *codec = device->device_data;
766 snd_hda_detach_beep_device(codec);
770 static int snd_hda_codec_dev_free(struct snd_device *device)
772 struct hda_codec *codec = device->device_data;
774 codec->in_freeing = 1;
775 snd_hdac_device_unregister(&codec->core);
776 snd_hdac_link_power(&codec->core, false);
777 put_device(hda_codec_dev(codec));
781 static void snd_hda_codec_dev_release(struct device *dev)
783 struct hda_codec *codec = dev_to_hda_codec(dev);
785 free_init_pincfgs(codec);
786 snd_hdac_device_exit(&codec->core);
787 snd_hda_sysfs_clear(codec);
788 kfree(codec->modelname);
794 * snd_hda_codec_new - create a HDA codec
795 * @bus: the bus to assign
796 * @codec_addr: the codec address
797 * @codecp: the pointer to store the generated codec
799 * Returns 0 if successful, or a negative error code.
801 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
802 unsigned int codec_addr, struct hda_codec **codecp)
804 struct hda_codec *codec;
808 static struct snd_device_ops dev_ops = {
809 .dev_register = snd_hda_codec_dev_register,
810 .dev_disconnect = snd_hda_codec_dev_disconnect,
811 .dev_free = snd_hda_codec_dev_free,
814 if (snd_BUG_ON(!bus))
816 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
819 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
823 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
824 err = snd_hdac_device_init(&codec->core, &bus->core, component,
831 codec->core.dev.release = snd_hda_codec_dev_release;
832 codec->core.type = HDA_DEV_LEGACY;
833 codec->core.exec_verb = codec_exec_verb;
837 codec->addr = codec_addr;
838 mutex_init(&codec->spdif_mutex);
839 mutex_init(&codec->control_mutex);
840 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
841 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
842 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
843 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
844 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
845 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
846 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
847 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
848 INIT_LIST_HEAD(&codec->conn_list);
849 INIT_LIST_HEAD(&codec->pcm_list_head);
851 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
852 codec->depop_delay = -1;
853 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
856 codec->power_jiffies = jiffies;
859 snd_hda_sysfs_init(codec);
861 if (codec->bus->modelname) {
862 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
863 if (!codec->modelname) {
869 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
870 err = read_widget_caps(codec, fg);
873 err = read_pin_defaults(codec);
877 /* power-up all before initialization */
878 hda_set_power_state(codec, AC_PWRST_D0);
879 codec->core.dev.power.power_state = PMSG_ON;
881 snd_hda_codec_proc_new(codec);
883 snd_hda_create_hwdep(codec);
885 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
886 codec->core.subsystem_id, codec->core.revision_id);
887 snd_component_add(card, component);
889 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
898 put_device(hda_codec_dev(codec));
901 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
904 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
905 * @codec: the HDA codec
907 * Forcibly refresh the all widget caps and the init pin configurations of
910 int snd_hda_codec_update_widgets(struct hda_codec *codec)
915 err = snd_hdac_refresh_widget_sysfs(&codec->core);
919 /* Assume the function group node does not change,
920 * only the widget nodes may change.
923 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
924 err = read_widget_caps(codec, fg);
928 snd_array_free(&codec->init_pins);
929 err = read_pin_defaults(codec);
933 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
935 /* update the stream-id if changed */
936 static void update_pcm_stream_id(struct hda_codec *codec,
937 struct hda_cvt_setup *p, hda_nid_t nid,
938 u32 stream_tag, int channel_id)
940 unsigned int oldval, newval;
942 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
943 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
944 newval = (stream_tag << 4) | channel_id;
945 if (oldval != newval)
946 snd_hda_codec_write(codec, nid, 0,
947 AC_VERB_SET_CHANNEL_STREAMID,
949 p->stream_tag = stream_tag;
950 p->channel_id = channel_id;
954 /* update the format-id if changed */
955 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
956 hda_nid_t nid, int format)
960 if (p->format_id != format) {
961 oldval = snd_hda_codec_read(codec, nid, 0,
962 AC_VERB_GET_STREAM_FORMAT, 0);
963 if (oldval != format) {
965 snd_hda_codec_write(codec, nid, 0,
966 AC_VERB_SET_STREAM_FORMAT,
969 p->format_id = format;
974 * snd_hda_codec_setup_stream - set up the codec for streaming
975 * @codec: the CODEC to set up
976 * @nid: the NID to set up
977 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
978 * @channel_id: channel id to pass, zero based.
979 * @format: stream format.
981 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
983 int channel_id, int format)
986 struct hda_cvt_setup *p;
994 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
995 nid, stream_tag, channel_id, format);
996 p = get_hda_cvt_setup(codec, nid);
1000 if (codec->patch_ops.stream_pm)
1001 codec->patch_ops.stream_pm(codec, nid, true);
1002 if (codec->pcm_format_first)
1003 update_pcm_format(codec, p, nid, format);
1004 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1005 if (!codec->pcm_format_first)
1006 update_pcm_format(codec, p, nid, format);
1011 /* make other inactive cvts with the same stream-tag dirty */
1012 type = get_wcaps_type(get_wcaps(codec, nid));
1013 list_for_each_codec(c, codec->bus) {
1014 for (i = 0; i < c->cvt_setups.used; i++) {
1015 p = snd_array_elem(&c->cvt_setups, i);
1016 if (!p->active && p->stream_tag == stream_tag &&
1017 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1022 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1024 static void really_cleanup_stream(struct hda_codec *codec,
1025 struct hda_cvt_setup *q);
1028 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1029 * @codec: the CODEC to clean up
1030 * @nid: the NID to clean up
1031 * @do_now: really clean up the stream instead of clearing the active flag
1033 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1036 struct hda_cvt_setup *p;
1041 if (codec->no_sticky_stream)
1044 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1045 p = get_hda_cvt_setup(codec, nid);
1047 /* here we just clear the active flag when do_now isn't set;
1048 * actual clean-ups will be done later in
1049 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1052 really_cleanup_stream(codec, p);
1057 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1059 static void really_cleanup_stream(struct hda_codec *codec,
1060 struct hda_cvt_setup *q)
1062 hda_nid_t nid = q->nid;
1063 if (q->stream_tag || q->channel_id)
1064 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1066 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1068 memset(q, 0, sizeof(*q));
1070 if (codec->patch_ops.stream_pm)
1071 codec->patch_ops.stream_pm(codec, nid, false);
1074 /* clean up the all conflicting obsolete streams */
1075 static void purify_inactive_streams(struct hda_codec *codec)
1077 struct hda_codec *c;
1080 list_for_each_codec(c, codec->bus) {
1081 for (i = 0; i < c->cvt_setups.used; i++) {
1082 struct hda_cvt_setup *p;
1083 p = snd_array_elem(&c->cvt_setups, i);
1085 really_cleanup_stream(c, p);
1091 /* clean up all streams; called from suspend */
1092 static void hda_cleanup_all_streams(struct hda_codec *codec)
1096 for (i = 0; i < codec->cvt_setups.used; i++) {
1097 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1099 really_cleanup_stream(codec, p);
1105 * amp access functions
1109 * query_amp_caps - query AMP capabilities
1110 * @codec: the HD-auio codec
1111 * @nid: the NID to query
1112 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1114 * Query AMP capabilities for the given widget and direction.
1115 * Returns the obtained capability bits.
1117 * When cap bits have been already read, this doesn't read again but
1118 * returns the cached value.
1120 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1122 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1123 nid = codec->core.afg;
1124 return snd_hda_param_read(codec, nid,
1125 direction == HDA_OUTPUT ?
1126 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1128 EXPORT_SYMBOL_GPL(query_amp_caps);
1131 * snd_hda_check_amp_caps - query AMP capabilities
1132 * @codec: the HD-audio codec
1133 * @nid: the NID to query
1134 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1135 * @bits: bit mask to check the result
1137 * Check whether the widget has the given amp capability for the direction.
1139 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1140 int dir, unsigned int bits)
1144 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1145 if (query_amp_caps(codec, nid, dir) & bits)
1149 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1152 * snd_hda_override_amp_caps - Override the AMP capabilities
1153 * @codec: the CODEC to clean up
1154 * @nid: the NID to clean up
1155 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1156 * @caps: the capability bits to set
1158 * Override the cached AMP caps bits value by the given one.
1159 * This function is useful if the driver needs to adjust the AMP ranges,
1160 * e.g. limit to 0dB, etc.
1162 * Returns zero if successful or a negative error code.
1164 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1169 snd_hda_override_wcaps(codec, nid,
1170 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1171 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1172 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1174 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1177 * snd_hda_codec_amp_update - update the AMP mono value
1178 * @codec: HD-audio codec
1179 * @nid: NID to read the AMP value
1180 * @ch: channel to update (0 or 1)
1181 * @dir: #HDA_INPUT or #HDA_OUTPUT
1182 * @idx: the index value (only for input direction)
1183 * @mask: bit mask to set
1184 * @val: the bits value to set
1186 * Update the AMP values for the given channel, direction and index.
1188 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1189 int ch, int dir, int idx, int mask, int val)
1191 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1193 /* enable fake mute if no h/w mute but min=mute */
1194 if ((query_amp_caps(codec, nid, dir) &
1195 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1196 cmd |= AC_AMP_FAKE_MUTE;
1197 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1199 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1202 * snd_hda_codec_amp_stereo - update the AMP stereo values
1203 * @codec: HD-audio codec
1204 * @nid: NID to read the AMP value
1205 * @direction: #HDA_INPUT or #HDA_OUTPUT
1206 * @idx: the index value (only for input direction)
1207 * @mask: bit mask to set
1208 * @val: the bits value to set
1210 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1211 * stereo widget with the same mask and value.
1213 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1214 int direction, int idx, int mask, int val)
1218 if (snd_BUG_ON(mask & ~0xff))
1220 for (ch = 0; ch < 2; ch++)
1221 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1225 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1228 * snd_hda_codec_amp_init - initialize the AMP value
1229 * @codec: the HDA codec
1230 * @nid: NID to read the AMP value
1231 * @ch: channel (left=0 or right=1)
1232 * @dir: #HDA_INPUT or #HDA_OUTPUT
1233 * @idx: the index value (only for input direction)
1234 * @mask: bit mask to set
1235 * @val: the bits value to set
1237 * Works like snd_hda_codec_amp_update() but it writes the value only at
1238 * the first access. If the amp was already initialized / updated beforehand,
1239 * this does nothing.
1241 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1242 int dir, int idx, int mask, int val)
1246 if (!codec->core.regmap)
1248 regcache_cache_only(codec->core.regmap, true);
1249 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1250 regcache_cache_only(codec->core.regmap, false);
1253 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1255 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1258 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1259 * @codec: the HDA codec
1260 * @nid: NID to read the AMP value
1261 * @dir: #HDA_INPUT or #HDA_OUTPUT
1262 * @idx: the index value (only for input direction)
1263 * @mask: bit mask to set
1264 * @val: the bits value to set
1266 * Call snd_hda_codec_amp_init() for both stereo channels.
1268 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1269 int dir, int idx, int mask, int val)
1273 if (snd_BUG_ON(mask & ~0xff))
1275 for (ch = 0; ch < 2; ch++)
1276 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1280 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1282 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1285 u32 caps = query_amp_caps(codec, nid, dir);
1287 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1294 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1295 * @kcontrol: referred ctl element
1296 * @uinfo: pointer to get/store the data
1298 * The control element is supposed to have the private_value field
1299 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1301 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_info *uinfo)
1304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1305 u16 nid = get_amp_nid(kcontrol);
1306 u8 chs = get_amp_channels(kcontrol);
1307 int dir = get_amp_direction(kcontrol);
1308 unsigned int ofs = get_amp_offset(kcontrol);
1310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1311 uinfo->count = chs == 3 ? 2 : 1;
1312 uinfo->value.integer.min = 0;
1313 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1314 if (!uinfo->value.integer.max) {
1316 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1317 nid, kcontrol->id.name);
1322 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1325 static inline unsigned int
1326 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1327 int ch, int dir, int idx, unsigned int ofs)
1330 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1331 val &= HDA_AMP_VOLMASK;
1340 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1341 int ch, int dir, int idx, unsigned int ofs,
1344 unsigned int maxval;
1348 /* ofs = 0: raw max value */
1349 maxval = get_amp_max_value(codec, nid, dir, 0);
1352 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1353 HDA_AMP_VOLMASK, val);
1357 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1358 * @kcontrol: ctl element
1359 * @ucontrol: pointer to get/store the data
1361 * The control element is supposed to have the private_value field
1362 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1364 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1365 struct snd_ctl_elem_value *ucontrol)
1367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1368 hda_nid_t nid = get_amp_nid(kcontrol);
1369 int chs = get_amp_channels(kcontrol);
1370 int dir = get_amp_direction(kcontrol);
1371 int idx = get_amp_index(kcontrol);
1372 unsigned int ofs = get_amp_offset(kcontrol);
1373 long *valp = ucontrol->value.integer.value;
1376 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1378 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1381 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1384 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1385 * @kcontrol: ctl element
1386 * @ucontrol: pointer to get/store the data
1388 * The control element is supposed to have the private_value field
1389 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1391 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1392 struct snd_ctl_elem_value *ucontrol)
1394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1395 hda_nid_t nid = get_amp_nid(kcontrol);
1396 int chs = get_amp_channels(kcontrol);
1397 int dir = get_amp_direction(kcontrol);
1398 int idx = get_amp_index(kcontrol);
1399 unsigned int ofs = get_amp_offset(kcontrol);
1400 long *valp = ucontrol->value.integer.value;
1404 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1408 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1411 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1414 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1415 * @kcontrol: ctl element
1416 * @op_flag: operation flag
1417 * @size: byte size of input TLV
1420 * The control element is supposed to have the private_value field
1421 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1423 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1424 unsigned int size, unsigned int __user *_tlv)
1426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1427 hda_nid_t nid = get_amp_nid(kcontrol);
1428 int dir = get_amp_direction(kcontrol);
1429 unsigned int ofs = get_amp_offset(kcontrol);
1430 bool min_mute = get_amp_min_mute(kcontrol);
1431 u32 caps, val1, val2;
1433 if (size < 4 * sizeof(unsigned int))
1435 caps = query_amp_caps(codec, nid, dir);
1436 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1437 val2 = (val2 + 1) * 25;
1438 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1440 val1 = ((int)val1) * ((int)val2);
1441 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1442 val2 |= TLV_DB_SCALE_MUTE;
1443 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1445 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1447 if (put_user(val1, _tlv + 2))
1449 if (put_user(val2, _tlv + 3))
1453 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1456 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1457 * @codec: HD-audio codec
1458 * @nid: NID of a reference widget
1459 * @dir: #HDA_INPUT or #HDA_OUTPUT
1460 * @tlv: TLV data to be stored, at least 4 elements
1462 * Set (static) TLV data for a virtual master volume using the AMP caps
1463 * obtained from the reference NID.
1464 * The volume range is recalculated as if the max volume is 0dB.
1466 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1472 caps = query_amp_caps(codec, nid, dir);
1473 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1474 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1475 step = (step + 1) * 25;
1476 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1477 tlv[1] = 2 * sizeof(unsigned int);
1478 tlv[2] = -nums * step;
1481 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1483 /* find a mixer control element with the given name */
1484 static struct snd_kcontrol *
1485 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1487 struct snd_ctl_elem_id id;
1488 memset(&id, 0, sizeof(id));
1489 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1492 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1494 strcpy(id.name, name);
1495 return snd_ctl_find_id(codec->card, &id);
1499 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1500 * @codec: HD-audio codec
1501 * @name: ctl id name string
1503 * Get the control element with the given id string and IFACE_MIXER.
1505 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1508 return find_mixer_ctl(codec, name, 0, 0);
1510 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1512 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1516 /* 16 ctlrs should be large enough */
1517 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1518 if (!find_mixer_ctl(codec, name, 0, idx))
1525 * snd_hda_ctl_add - Add a control element and assign to the codec
1526 * @codec: HD-audio codec
1527 * @nid: corresponding NID (optional)
1528 * @kctl: the control element to assign
1530 * Add the given control element to an array inside the codec instance.
1531 * All control elements belonging to a codec are supposed to be added
1532 * by this function so that a proper clean-up works at the free or
1533 * reconfiguration time.
1535 * If non-zero @nid is passed, the NID is assigned to the control element.
1536 * The assignment is shown in the codec proc file.
1538 * snd_hda_ctl_add() checks the control subdev id field whether
1539 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1540 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1541 * specifies if kctl->private_value is a HDA amplifier value.
1543 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1544 struct snd_kcontrol *kctl)
1547 unsigned short flags = 0;
1548 struct hda_nid_item *item;
1550 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1551 flags |= HDA_NID_ITEM_AMP;
1553 nid = get_amp_nid_(kctl->private_value);
1555 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1556 nid = kctl->id.subdevice & 0xffff;
1557 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1558 kctl->id.subdevice = 0;
1559 err = snd_ctl_add(codec->card, kctl);
1562 item = snd_array_new(&codec->mixers);
1567 item->flags = flags;
1570 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1573 * snd_hda_add_nid - Assign a NID to a control element
1574 * @codec: HD-audio codec
1575 * @nid: corresponding NID (optional)
1576 * @kctl: the control element to assign
1577 * @index: index to kctl
1579 * Add the given control element to an array inside the codec instance.
1580 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1581 * NID:KCTL mapping - for example "Capture Source" selector.
1583 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1584 unsigned int index, hda_nid_t nid)
1586 struct hda_nid_item *item;
1589 item = snd_array_new(&codec->nids);
1593 item->index = index;
1597 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1598 kctl->id.name, kctl->id.index, index);
1601 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1604 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1605 * @codec: HD-audio codec
1607 void snd_hda_ctls_clear(struct hda_codec *codec)
1610 struct hda_nid_item *items = codec->mixers.list;
1612 down_write(&codec->card->controls_rwsem);
1613 for (i = 0; i < codec->mixers.used; i++)
1614 snd_ctl_remove(codec->card, items[i].kctl);
1615 up_write(&codec->card->controls_rwsem);
1616 snd_array_free(&codec->mixers);
1617 snd_array_free(&codec->nids);
1621 * snd_hda_lock_devices - pseudo device locking
1624 * toggle card->shutdown to allow/disallow the device access (as a hack)
1626 int snd_hda_lock_devices(struct hda_bus *bus)
1628 struct snd_card *card = bus->card;
1629 struct hda_codec *codec;
1631 spin_lock(&card->files_lock);
1635 if (!list_empty(&card->ctl_files))
1638 list_for_each_codec(codec, bus) {
1639 struct hda_pcm *cpcm;
1640 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1643 if (cpcm->pcm->streams[0].substream_opened ||
1644 cpcm->pcm->streams[1].substream_opened)
1648 spin_unlock(&card->files_lock);
1654 spin_unlock(&card->files_lock);
1657 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1660 * snd_hda_unlock_devices - pseudo device unlocking
1663 void snd_hda_unlock_devices(struct hda_bus *bus)
1665 struct snd_card *card = bus->card;
1667 spin_lock(&card->files_lock);
1669 spin_unlock(&card->files_lock);
1671 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1674 * snd_hda_codec_reset - Clear all objects assigned to the codec
1675 * @codec: HD-audio codec
1677 * This frees the all PCM and control elements assigned to the codec, and
1678 * clears the caches and restores the pin default configurations.
1680 * When a device is being used, it returns -EBSY. If successfully freed,
1683 int snd_hda_codec_reset(struct hda_codec *codec)
1685 struct hda_bus *bus = codec->bus;
1687 if (snd_hda_lock_devices(bus) < 0)
1690 /* OK, let it free */
1691 snd_hdac_device_unregister(&codec->core);
1693 /* allow device access again */
1694 snd_hda_unlock_devices(bus);
1698 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1700 /* apply the function to all matching slave ctls in the mixer list */
1701 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1702 const char *suffix, map_slave_func_t func, void *data)
1704 struct hda_nid_item *items;
1705 const char * const *s;
1708 items = codec->mixers.list;
1709 for (i = 0; i < codec->mixers.used; i++) {
1710 struct snd_kcontrol *sctl = items[i].kctl;
1711 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1713 for (s = slaves; *s; s++) {
1714 char tmpname[sizeof(sctl->id.name)];
1715 const char *name = *s;
1717 snprintf(tmpname, sizeof(tmpname), "%s %s",
1721 if (!strcmp(sctl->id.name, name)) {
1722 err = func(codec, data, sctl);
1732 static int check_slave_present(struct hda_codec *codec,
1733 void *data, struct snd_kcontrol *sctl)
1738 /* guess the value corresponding to 0dB */
1739 static int get_kctl_0dB_offset(struct hda_codec *codec,
1740 struct snd_kcontrol *kctl, int *step_to_check)
1743 const int *tlv = NULL;
1746 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1747 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1748 mm_segment_t fs = get_fs();
1750 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1753 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1755 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1757 step &= ~TLV_DB_SCALE_MUTE;
1760 if (*step_to_check && *step_to_check != step) {
1761 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1762 *step_to_check, step);
1765 *step_to_check = step;
1766 val = -tlv[2] / step;
1771 /* call kctl->put with the given value(s) */
1772 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1774 struct snd_ctl_elem_value *ucontrol;
1775 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1778 ucontrol->value.integer.value[0] = val;
1779 ucontrol->value.integer.value[1] = val;
1780 kctl->put(kctl, ucontrol);
1785 /* initialize the slave volume with 0dB */
1786 static int init_slave_0dB(struct hda_codec *codec,
1787 void *data, struct snd_kcontrol *slave)
1789 int offset = get_kctl_0dB_offset(codec, slave, data);
1791 put_kctl_with_value(slave, offset);
1795 /* unmute the slave */
1796 static int init_slave_unmute(struct hda_codec *codec,
1797 void *data, struct snd_kcontrol *slave)
1799 return put_kctl_with_value(slave, 1);
1802 static int add_slave(struct hda_codec *codec,
1803 void *data, struct snd_kcontrol *slave)
1805 return snd_ctl_add_slave(data, slave);
1809 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1810 * @codec: HD-audio codec
1811 * @name: vmaster control name
1812 * @tlv: TLV data (optional)
1813 * @slaves: slave control names (optional)
1814 * @suffix: suffix string to each slave name (optional)
1815 * @init_slave_vol: initialize slaves to unmute/0dB
1816 * @ctl_ret: store the vmaster kcontrol in return
1818 * Create a virtual master control with the given name. The TLV data
1819 * must be either NULL or a valid data.
1821 * @slaves is a NULL-terminated array of strings, each of which is a
1822 * slave control name. All controls with these names are assigned to
1823 * the new virtual master control.
1825 * This function returns zero if successful or a negative error code.
1827 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1828 unsigned int *tlv, const char * const *slaves,
1829 const char *suffix, bool init_slave_vol,
1830 struct snd_kcontrol **ctl_ret)
1832 struct snd_kcontrol *kctl;
1838 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1840 codec_dbg(codec, "No slave found for %s\n", name);
1843 kctl = snd_ctl_make_virtual_master(name, tlv);
1846 err = snd_hda_ctl_add(codec, 0, kctl);
1850 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1854 /* init with master mute & zero volume */
1855 put_kctl_with_value(kctl, 0);
1856 if (init_slave_vol) {
1858 map_slaves(codec, slaves, suffix,
1859 tlv ? init_slave_0dB : init_slave_unmute, &step);
1866 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1869 * mute-LED control using vmaster
1871 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1872 struct snd_ctl_elem_info *uinfo)
1874 static const char * const texts[] = {
1875 "On", "Off", "Follow Master"
1878 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1881 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *ucontrol)
1884 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1885 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1889 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_value *ucontrol)
1892 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1893 unsigned int old_mode = hook->mute_mode;
1895 hook->mute_mode = ucontrol->value.enumerated.item[0];
1896 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1897 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1898 if (old_mode == hook->mute_mode)
1900 snd_hda_sync_vmaster_hook(hook);
1904 static struct snd_kcontrol_new vmaster_mute_mode = {
1905 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1906 .name = "Mute-LED Mode",
1907 .info = vmaster_mute_mode_info,
1908 .get = vmaster_mute_mode_get,
1909 .put = vmaster_mute_mode_put,
1912 /* meta hook to call each driver's vmaster hook */
1913 static void vmaster_hook(void *private_data, int enabled)
1915 struct hda_vmaster_mute_hook *hook = private_data;
1917 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1918 enabled = hook->mute_mode;
1919 hook->hook(hook->codec, enabled);
1923 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1924 * @codec: the HDA codec
1925 * @hook: the vmaster hook object
1926 * @expose_enum_ctl: flag to create an enum ctl
1928 * Add a mute-LED hook with the given vmaster switch kctl.
1929 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1930 * created and associated with the given hook.
1932 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1933 struct hda_vmaster_mute_hook *hook,
1934 bool expose_enum_ctl)
1936 struct snd_kcontrol *kctl;
1938 if (!hook->hook || !hook->sw_kctl)
1940 hook->codec = codec;
1941 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1942 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
1943 if (!expose_enum_ctl)
1945 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1948 return snd_hda_ctl_add(codec, 0, kctl);
1950 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
1953 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1954 * @hook: the vmaster hook
1956 * Call the hook with the current value for synchronization.
1957 * Should be called in init callback.
1959 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
1961 if (!hook->hook || !hook->codec)
1963 /* don't call vmaster hook in the destructor since it might have
1964 * been already destroyed
1966 if (hook->codec->bus->shutdown)
1968 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
1970 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
1974 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
1975 * @kcontrol: referred ctl element
1976 * @uinfo: pointer to get/store the data
1978 * The control element is supposed to have the private_value field
1979 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1981 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_info *uinfo)
1984 int chs = get_amp_channels(kcontrol);
1986 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1987 uinfo->count = chs == 3 ? 2 : 1;
1988 uinfo->value.integer.min = 0;
1989 uinfo->value.integer.max = 1;
1992 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
1995 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
1996 * @kcontrol: ctl element
1997 * @ucontrol: pointer to get/store the data
1999 * The control element is supposed to have the private_value field
2000 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2002 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_value *ucontrol)
2005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006 hda_nid_t nid = get_amp_nid(kcontrol);
2007 int chs = get_amp_channels(kcontrol);
2008 int dir = get_amp_direction(kcontrol);
2009 int idx = get_amp_index(kcontrol);
2010 long *valp = ucontrol->value.integer.value;
2013 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2014 HDA_AMP_MUTE) ? 0 : 1;
2016 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2017 HDA_AMP_MUTE) ? 0 : 1;
2020 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2023 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2024 * @kcontrol: ctl element
2025 * @ucontrol: pointer to get/store the data
2027 * The control element is supposed to have the private_value field
2028 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2030 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2031 struct snd_ctl_elem_value *ucontrol)
2033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2034 hda_nid_t nid = get_amp_nid(kcontrol);
2035 int chs = get_amp_channels(kcontrol);
2036 int dir = get_amp_direction(kcontrol);
2037 int idx = get_amp_index(kcontrol);
2038 long *valp = ucontrol->value.integer.value;
2042 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2044 *valp ? 0 : HDA_AMP_MUTE);
2048 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2050 *valp ? 0 : HDA_AMP_MUTE);
2051 hda_call_check_power_status(codec, nid);
2054 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2057 * bound volume controls
2059 * bind multiple volumes (# indices, from 0)
2062 #define AMP_VAL_IDX_SHIFT 19
2063 #define AMP_VAL_IDX_MASK (0x0f<<19)
2066 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2067 * @kcontrol: ctl element
2068 * @ucontrol: pointer to get/store the data
2070 * The control element is supposed to have the private_value field
2071 * set up via HDA_BIND_MUTE*() macros.
2073 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *ucontrol)
2076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2080 mutex_lock(&codec->control_mutex);
2081 pval = kcontrol->private_value;
2082 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2083 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2084 kcontrol->private_value = pval;
2085 mutex_unlock(&codec->control_mutex);
2088 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2091 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2092 * @kcontrol: ctl element
2093 * @ucontrol: pointer to get/store the data
2095 * The control element is supposed to have the private_value field
2096 * set up via HDA_BIND_MUTE*() macros.
2098 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2099 struct snd_ctl_elem_value *ucontrol)
2101 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2103 int i, indices, err = 0, change = 0;
2105 mutex_lock(&codec->control_mutex);
2106 pval = kcontrol->private_value;
2107 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2108 for (i = 0; i < indices; i++) {
2109 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2110 (i << AMP_VAL_IDX_SHIFT);
2111 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2116 kcontrol->private_value = pval;
2117 mutex_unlock(&codec->control_mutex);
2118 return err < 0 ? err : change;
2120 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2123 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2124 * @kcontrol: referred ctl element
2125 * @uinfo: pointer to get/store the data
2127 * The control element is supposed to have the private_value field
2128 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2130 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2131 struct snd_ctl_elem_info *uinfo)
2133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2134 struct hda_bind_ctls *c;
2137 mutex_lock(&codec->control_mutex);
2138 c = (struct hda_bind_ctls *)kcontrol->private_value;
2139 kcontrol->private_value = *c->values;
2140 err = c->ops->info(kcontrol, uinfo);
2141 kcontrol->private_value = (long)c;
2142 mutex_unlock(&codec->control_mutex);
2145 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2148 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2149 * @kcontrol: ctl element
2150 * @ucontrol: pointer to get/store the data
2152 * The control element is supposed to have the private_value field
2153 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2155 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2159 struct hda_bind_ctls *c;
2162 mutex_lock(&codec->control_mutex);
2163 c = (struct hda_bind_ctls *)kcontrol->private_value;
2164 kcontrol->private_value = *c->values;
2165 err = c->ops->get(kcontrol, ucontrol);
2166 kcontrol->private_value = (long)c;
2167 mutex_unlock(&codec->control_mutex);
2170 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2173 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2174 * @kcontrol: ctl element
2175 * @ucontrol: pointer to get/store the data
2177 * The control element is supposed to have the private_value field
2178 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2180 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2184 struct hda_bind_ctls *c;
2185 unsigned long *vals;
2186 int err = 0, change = 0;
2188 mutex_lock(&codec->control_mutex);
2189 c = (struct hda_bind_ctls *)kcontrol->private_value;
2190 for (vals = c->values; *vals; vals++) {
2191 kcontrol->private_value = *vals;
2192 err = c->ops->put(kcontrol, ucontrol);
2197 kcontrol->private_value = (long)c;
2198 mutex_unlock(&codec->control_mutex);
2199 return err < 0 ? err : change;
2201 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2204 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2205 * @kcontrol: ctl element
2206 * @op_flag: operation flag
2207 * @size: byte size of input TLV
2210 * The control element is supposed to have the private_value field
2211 * set up via HDA_BIND_VOL() macro.
2213 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2214 unsigned int size, unsigned int __user *tlv)
2216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2217 struct hda_bind_ctls *c;
2220 mutex_lock(&codec->control_mutex);
2221 c = (struct hda_bind_ctls *)kcontrol->private_value;
2222 kcontrol->private_value = *c->values;
2223 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2224 kcontrol->private_value = (long)c;
2225 mutex_unlock(&codec->control_mutex);
2228 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2230 struct hda_ctl_ops snd_hda_bind_vol = {
2231 .info = snd_hda_mixer_amp_volume_info,
2232 .get = snd_hda_mixer_amp_volume_get,
2233 .put = snd_hda_mixer_amp_volume_put,
2234 .tlv = snd_hda_mixer_amp_tlv
2236 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2238 struct hda_ctl_ops snd_hda_bind_sw = {
2239 .info = snd_hda_mixer_amp_switch_info,
2240 .get = snd_hda_mixer_amp_switch_get,
2241 .put = snd_hda_mixer_amp_switch_put,
2242 .tlv = snd_hda_mixer_amp_tlv
2244 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2247 * SPDIF out controls
2250 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2251 struct snd_ctl_elem_info *uinfo)
2253 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2258 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
2261 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2262 IEC958_AES0_NONAUDIO |
2263 IEC958_AES0_CON_EMPHASIS_5015 |
2264 IEC958_AES0_CON_NOT_COPYRIGHT;
2265 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2266 IEC958_AES1_CON_ORIGINAL;
2270 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2273 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2274 IEC958_AES0_NONAUDIO |
2275 IEC958_AES0_PRO_EMPHASIS_5015;
2279 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2280 struct snd_ctl_elem_value *ucontrol)
2282 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2283 int idx = kcontrol->private_value;
2284 struct hda_spdif_out *spdif;
2286 mutex_lock(&codec->spdif_mutex);
2287 spdif = snd_array_elem(&codec->spdif_out, idx);
2288 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2289 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2290 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2291 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2292 mutex_unlock(&codec->spdif_mutex);
2297 /* convert from SPDIF status bits to HDA SPDIF bits
2298 * bit 0 (DigEn) is always set zero (to be filled later)
2300 static unsigned short convert_from_spdif_status(unsigned int sbits)
2302 unsigned short val = 0;
2304 if (sbits & IEC958_AES0_PROFESSIONAL)
2305 val |= AC_DIG1_PROFESSIONAL;
2306 if (sbits & IEC958_AES0_NONAUDIO)
2307 val |= AC_DIG1_NONAUDIO;
2308 if (sbits & IEC958_AES0_PROFESSIONAL) {
2309 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2310 IEC958_AES0_PRO_EMPHASIS_5015)
2311 val |= AC_DIG1_EMPHASIS;
2313 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2314 IEC958_AES0_CON_EMPHASIS_5015)
2315 val |= AC_DIG1_EMPHASIS;
2316 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2317 val |= AC_DIG1_COPYRIGHT;
2318 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2319 val |= AC_DIG1_LEVEL;
2320 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2325 /* convert to SPDIF status bits from HDA SPDIF bits
2327 static unsigned int convert_to_spdif_status(unsigned short val)
2329 unsigned int sbits = 0;
2331 if (val & AC_DIG1_NONAUDIO)
2332 sbits |= IEC958_AES0_NONAUDIO;
2333 if (val & AC_DIG1_PROFESSIONAL)
2334 sbits |= IEC958_AES0_PROFESSIONAL;
2335 if (sbits & IEC958_AES0_PROFESSIONAL) {
2336 if (val & AC_DIG1_EMPHASIS)
2337 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2339 if (val & AC_DIG1_EMPHASIS)
2340 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2341 if (!(val & AC_DIG1_COPYRIGHT))
2342 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2343 if (val & AC_DIG1_LEVEL)
2344 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2345 sbits |= val & (0x7f << 8);
2350 /* set digital convert verbs both for the given NID and its slaves */
2351 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2356 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2358 d = codec->slave_dig_outs;
2362 snd_hdac_regmap_update(&codec->core, *d,
2363 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2366 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2369 unsigned int mask = 0;
2370 unsigned int val = 0;
2380 set_dig_out(codec, nid, mask, val);
2383 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2384 struct snd_ctl_elem_value *ucontrol)
2386 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2387 int idx = kcontrol->private_value;
2388 struct hda_spdif_out *spdif;
2393 mutex_lock(&codec->spdif_mutex);
2394 spdif = snd_array_elem(&codec->spdif_out, idx);
2396 spdif->status = ucontrol->value.iec958.status[0] |
2397 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2398 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2399 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2400 val = convert_from_spdif_status(spdif->status);
2401 val |= spdif->ctls & 1;
2402 change = spdif->ctls != val;
2404 if (change && nid != (u16)-1)
2405 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2406 mutex_unlock(&codec->spdif_mutex);
2410 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2412 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
2415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2416 int idx = kcontrol->private_value;
2417 struct hda_spdif_out *spdif;
2419 mutex_lock(&codec->spdif_mutex);
2420 spdif = snd_array_elem(&codec->spdif_out, idx);
2421 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2422 mutex_unlock(&codec->spdif_mutex);
2426 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2429 set_dig_out_convert(codec, nid, dig1, dig2);
2430 /* unmute amp switch (if any) */
2431 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2432 (dig1 & AC_DIG1_ENABLE))
2433 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2437 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_value *ucontrol)
2440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2441 int idx = kcontrol->private_value;
2442 struct hda_spdif_out *spdif;
2447 mutex_lock(&codec->spdif_mutex);
2448 spdif = snd_array_elem(&codec->spdif_out, idx);
2450 val = spdif->ctls & ~AC_DIG1_ENABLE;
2451 if (ucontrol->value.integer.value[0])
2452 val |= AC_DIG1_ENABLE;
2453 change = spdif->ctls != val;
2455 if (change && nid != (u16)-1)
2456 set_spdif_ctls(codec, nid, val & 0xff, -1);
2457 mutex_unlock(&codec->spdif_mutex);
2461 static struct snd_kcontrol_new dig_mixes[] = {
2463 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2464 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2465 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2466 .info = snd_hda_spdif_mask_info,
2467 .get = snd_hda_spdif_cmask_get,
2470 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2471 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2472 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2473 .info = snd_hda_spdif_mask_info,
2474 .get = snd_hda_spdif_pmask_get,
2477 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2478 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2479 .info = snd_hda_spdif_mask_info,
2480 .get = snd_hda_spdif_default_get,
2481 .put = snd_hda_spdif_default_put,
2484 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2485 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2486 .info = snd_hda_spdif_out_switch_info,
2487 .get = snd_hda_spdif_out_switch_get,
2488 .put = snd_hda_spdif_out_switch_put,
2494 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2495 * @codec: the HDA codec
2496 * @associated_nid: NID that new ctls associated with
2497 * @cvt_nid: converter NID
2498 * @type: HDA_PCM_TYPE_*
2499 * Creates controls related with the digital output.
2500 * Called from each patch supporting the digital out.
2502 * Returns 0 if successful, or a negative error code.
2504 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2505 hda_nid_t associated_nid,
2510 struct snd_kcontrol *kctl;
2511 struct snd_kcontrol_new *dig_mix;
2514 const int spdif_index = 16;
2515 struct hda_spdif_out *spdif;
2516 struct hda_bus *bus = codec->bus;
2518 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2519 type == HDA_PCM_TYPE_SPDIF) {
2521 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2522 type == HDA_PCM_TYPE_HDMI) {
2523 /* suppose a single SPDIF device */
2524 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2525 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2528 kctl->id.index = spdif_index;
2530 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2532 if (!bus->primary_dig_out_type)
2533 bus->primary_dig_out_type = type;
2535 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2537 codec_err(codec, "too many IEC958 outputs\n");
2540 spdif = snd_array_new(&codec->spdif_out);
2543 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2544 kctl = snd_ctl_new1(dig_mix, codec);
2547 kctl->id.index = idx;
2548 kctl->private_value = codec->spdif_out.used - 1;
2549 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2553 spdif->nid = cvt_nid;
2554 snd_hdac_regmap_read(&codec->core, cvt_nid,
2555 AC_VERB_GET_DIGI_CONVERT_1, &val);
2557 spdif->status = convert_to_spdif_status(spdif->ctls);
2560 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2563 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2564 * @codec: the HDA codec
2567 * call within spdif_mutex lock
2569 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2573 for (i = 0; i < codec->spdif_out.used; i++) {
2574 struct hda_spdif_out *spdif =
2575 snd_array_elem(&codec->spdif_out, i);
2576 if (spdif->nid == nid)
2581 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2584 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2585 * @codec: the HDA codec
2586 * @idx: the SPDIF ctl index
2588 * Unassign the widget from the given SPDIF control.
2590 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2592 struct hda_spdif_out *spdif;
2594 mutex_lock(&codec->spdif_mutex);
2595 spdif = snd_array_elem(&codec->spdif_out, idx);
2596 spdif->nid = (u16)-1;
2597 mutex_unlock(&codec->spdif_mutex);
2599 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2602 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2603 * @codec: the HDA codec
2604 * @idx: the SPDIF ctl idx
2607 * Assign the widget to the SPDIF control with the given index.
2609 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2611 struct hda_spdif_out *spdif;
2614 mutex_lock(&codec->spdif_mutex);
2615 spdif = snd_array_elem(&codec->spdif_out, idx);
2616 if (spdif->nid != nid) {
2619 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2621 mutex_unlock(&codec->spdif_mutex);
2623 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2626 * SPDIF sharing with analog output
2628 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2631 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2632 ucontrol->value.integer.value[0] = mout->share_spdif;
2636 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2639 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2640 mout->share_spdif = !!ucontrol->value.integer.value[0];
2644 static struct snd_kcontrol_new spdif_share_sw = {
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646 .name = "IEC958 Default PCM Playback Switch",
2647 .info = snd_ctl_boolean_mono_info,
2648 .get = spdif_share_sw_get,
2649 .put = spdif_share_sw_put,
2653 * snd_hda_create_spdif_share_sw - create Default PCM switch
2654 * @codec: the HDA codec
2655 * @mout: multi-out instance
2657 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2658 struct hda_multi_out *mout)
2660 struct snd_kcontrol *kctl;
2662 if (!mout->dig_out_nid)
2665 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2668 /* ATTENTION: here mout is passed as private_data, instead of codec */
2669 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2671 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2677 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2679 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2682 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2684 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2688 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
2691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692 hda_nid_t nid = kcontrol->private_value;
2693 unsigned int val = !!ucontrol->value.integer.value[0];
2696 mutex_lock(&codec->spdif_mutex);
2697 change = codec->spdif_in_enable != val;
2699 codec->spdif_in_enable = val;
2700 snd_hdac_regmap_write(&codec->core, nid,
2701 AC_VERB_SET_DIGI_CONVERT_1, val);
2703 mutex_unlock(&codec->spdif_mutex);
2707 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2711 hda_nid_t nid = kcontrol->private_value;
2715 snd_hdac_regmap_read(&codec->core, nid,
2716 AC_VERB_GET_DIGI_CONVERT_1, &val);
2717 sbits = convert_to_spdif_status(val);
2718 ucontrol->value.iec958.status[0] = sbits;
2719 ucontrol->value.iec958.status[1] = sbits >> 8;
2720 ucontrol->value.iec958.status[2] = sbits >> 16;
2721 ucontrol->value.iec958.status[3] = sbits >> 24;
2725 static struct snd_kcontrol_new dig_in_ctls[] = {
2727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2728 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2729 .info = snd_hda_spdif_in_switch_info,
2730 .get = snd_hda_spdif_in_switch_get,
2731 .put = snd_hda_spdif_in_switch_put,
2734 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2736 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2737 .info = snd_hda_spdif_mask_info,
2738 .get = snd_hda_spdif_in_status_get,
2744 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2745 * @codec: the HDA codec
2746 * @nid: audio in widget NID
2748 * Creates controls related with the SPDIF input.
2749 * Called from each patch supporting the SPDIF in.
2751 * Returns 0 if successful, or a negative error code.
2753 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2756 struct snd_kcontrol *kctl;
2757 struct snd_kcontrol_new *dig_mix;
2760 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2762 codec_err(codec, "too many IEC958 inputs\n");
2765 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2766 kctl = snd_ctl_new1(dig_mix, codec);
2769 kctl->private_value = nid;
2770 err = snd_hda_ctl_add(codec, nid, kctl);
2774 codec->spdif_in_enable =
2775 snd_hda_codec_read(codec, nid, 0,
2776 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2780 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2783 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2784 * @codec: the HDA codec
2785 * @fg: function group (not used now)
2786 * @power_state: the power state to set (AC_PWRST_*)
2788 * Set the given power state to all widgets that have the power control.
2789 * If the codec has power_filter set, it evaluates the power state and
2790 * filter out if it's unchanged as D3.
2792 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2793 unsigned int power_state)
2797 for_each_hda_codec_node(nid, codec) {
2798 unsigned int wcaps = get_wcaps(codec, nid);
2799 unsigned int state = power_state;
2800 if (!(wcaps & AC_WCAP_POWER))
2802 if (codec->power_filter) {
2803 state = codec->power_filter(codec, nid, power_state);
2804 if (state != power_state && power_state == AC_PWRST_D3)
2807 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2811 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2814 * wait until the state is reached, returns the current state
2816 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2818 unsigned int power_state)
2820 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2821 unsigned int state, actual_state;
2824 state = snd_hda_codec_read(codec, fg, 0,
2825 AC_VERB_GET_POWER_STATE, 0);
2826 if (state & AC_PWRST_ERROR)
2828 actual_state = (state >> 4) & 0x0f;
2829 if (actual_state == power_state)
2831 if (time_after_eq(jiffies, end_time))
2833 /* wait until the codec reachs to the target state */
2840 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2841 * @codec: the HDA codec
2843 * @power_state: power state to evalue
2845 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2846 * This can be used a codec power_filter callback.
2848 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2850 unsigned int power_state)
2852 if (nid == codec->core.afg || nid == codec->core.mfg)
2854 if (power_state == AC_PWRST_D3 &&
2855 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2856 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2857 int eapd = snd_hda_codec_read(codec, nid, 0,
2858 AC_VERB_GET_EAPD_BTLENABLE, 0);
2864 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2867 * set power state of the codec, and return the power state
2869 static unsigned int hda_set_power_state(struct hda_codec *codec,
2870 unsigned int power_state)
2872 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2877 /* this delay seems necessary to avoid click noise at power-down */
2878 if (power_state == AC_PWRST_D3) {
2879 if (codec->depop_delay < 0)
2880 msleep(codec_has_epss(codec) ? 10 : 100);
2881 else if (codec->depop_delay > 0)
2882 msleep(codec->depop_delay);
2883 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2886 /* repeat power states setting at most 10 times*/
2887 for (count = 0; count < 10; count++) {
2888 if (codec->patch_ops.set_power_state)
2889 codec->patch_ops.set_power_state(codec, fg,
2892 state = power_state;
2893 if (codec->power_filter)
2894 state = codec->power_filter(codec, fg, state);
2895 if (state == power_state || power_state != AC_PWRST_D3)
2896 snd_hda_codec_read(codec, fg, flags,
2897 AC_VERB_SET_POWER_STATE,
2899 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2901 state = hda_sync_power_state(codec, fg, power_state);
2902 if (!(state & AC_PWRST_ERROR))
2909 /* sync power states of all widgets;
2910 * this is called at the end of codec parsing
2912 static void sync_power_up_states(struct hda_codec *codec)
2916 /* don't care if no filter is used */
2917 if (!codec->power_filter)
2920 for_each_hda_codec_node(nid, codec) {
2921 unsigned int wcaps = get_wcaps(codec, nid);
2922 unsigned int target;
2923 if (!(wcaps & AC_WCAP_POWER))
2925 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2926 if (target == AC_PWRST_D0)
2928 if (!snd_hda_check_power_state(codec, nid, target))
2929 snd_hda_codec_write(codec, nid, 0,
2930 AC_VERB_SET_POWER_STATE, target);
2934 #ifdef CONFIG_SND_HDA_RECONFIG
2935 /* execute additional init verbs */
2936 static void hda_exec_init_verbs(struct hda_codec *codec)
2938 if (codec->init_verbs.list)
2939 snd_hda_sequence_write(codec, codec->init_verbs.list);
2942 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2946 /* update the power on/off account with the current jiffies */
2947 static void update_power_acct(struct hda_codec *codec, bool on)
2949 unsigned long delta = jiffies - codec->power_jiffies;
2952 codec->power_on_acct += delta;
2954 codec->power_off_acct += delta;
2955 codec->power_jiffies += delta;
2958 void snd_hda_update_power_acct(struct hda_codec *codec)
2960 update_power_acct(codec, hda_codec_is_power_on(codec));
2964 * call suspend and power-down; used both from PM and power-save
2965 * this function returns the power state in the end
2967 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2971 atomic_inc(&codec->core.in_pm);
2973 if (codec->patch_ops.suspend)
2974 codec->patch_ops.suspend(codec);
2975 hda_cleanup_all_streams(codec);
2976 state = hda_set_power_state(codec, AC_PWRST_D3);
2977 update_power_acct(codec, true);
2978 atomic_dec(&codec->core.in_pm);
2983 * kick up codec; used both from PM and power-save
2985 static void hda_call_codec_resume(struct hda_codec *codec)
2987 atomic_inc(&codec->core.in_pm);
2989 if (codec->core.regmap)
2990 regcache_mark_dirty(codec->core.regmap);
2992 codec->power_jiffies = jiffies;
2994 hda_set_power_state(codec, AC_PWRST_D0);
2995 restore_shutup_pins(codec);
2996 hda_exec_init_verbs(codec);
2997 snd_hda_jack_set_dirty_all(codec);
2998 if (codec->patch_ops.resume)
2999 codec->patch_ops.resume(codec);
3001 if (codec->patch_ops.init)
3002 codec->patch_ops.init(codec);
3003 if (codec->core.regmap)
3004 regcache_sync(codec->core.regmap);
3007 if (codec->jackpoll_interval)
3008 hda_jackpoll_work(&codec->jackpoll_work.work);
3010 snd_hda_jack_report_sync(codec);
3011 codec->core.dev.power.power_state = PMSG_ON;
3012 atomic_dec(&codec->core.in_pm);
3015 static int hda_codec_runtime_suspend(struct device *dev)
3017 struct hda_codec *codec = dev_to_hda_codec(dev);
3018 struct hda_pcm *pcm;
3021 cancel_delayed_work_sync(&codec->jackpoll_work);
3022 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3023 snd_pcm_suspend_all(pcm->pcm);
3024 state = hda_call_codec_suspend(codec);
3025 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3026 (state & AC_PWRST_CLK_STOP_OK))
3027 snd_hdac_codec_link_down(&codec->core);
3028 snd_hdac_link_power(&codec->core, false);
3032 static int hda_codec_runtime_resume(struct device *dev)
3034 struct hda_codec *codec = dev_to_hda_codec(dev);
3036 snd_hdac_link_power(&codec->core, true);
3037 snd_hdac_codec_link_up(&codec->core);
3038 hda_call_codec_resume(codec);
3039 pm_runtime_mark_last_busy(dev);
3042 #endif /* CONFIG_PM */
3044 #ifdef CONFIG_PM_SLEEP
3045 static int hda_codec_force_resume(struct device *dev)
3049 /* The get/put pair below enforces the runtime resume even if the
3050 * device hasn't been used at suspend time. This trick is needed to
3051 * update the jack state change during the sleep.
3053 pm_runtime_get_noresume(dev);
3054 ret = pm_runtime_force_resume(dev);
3055 pm_runtime_put(dev);
3059 static int hda_codec_pm_suspend(struct device *dev)
3061 dev->power.power_state = PMSG_SUSPEND;
3062 return pm_runtime_force_suspend(dev);
3065 static int hda_codec_pm_resume(struct device *dev)
3067 dev->power.power_state = PMSG_RESUME;
3068 return hda_codec_force_resume(dev);
3071 static int hda_codec_pm_freeze(struct device *dev)
3073 dev->power.power_state = PMSG_FREEZE;
3074 return pm_runtime_force_suspend(dev);
3077 static int hda_codec_pm_thaw(struct device *dev)
3079 dev->power.power_state = PMSG_THAW;
3080 return hda_codec_force_resume(dev);
3083 static int hda_codec_pm_restore(struct device *dev)
3085 dev->power.power_state = PMSG_RESTORE;
3086 return hda_codec_force_resume(dev);
3088 #endif /* CONFIG_PM_SLEEP */
3090 /* referred in hda_bind.c */
3091 const struct dev_pm_ops hda_codec_driver_pm = {
3092 #ifdef CONFIG_PM_SLEEP
3093 .suspend = hda_codec_pm_suspend,
3094 .resume = hda_codec_pm_resume,
3095 .freeze = hda_codec_pm_freeze,
3096 .thaw = hda_codec_pm_thaw,
3097 .poweroff = hda_codec_pm_suspend,
3098 .restore = hda_codec_pm_restore,
3099 #endif /* CONFIG_PM_SLEEP */
3100 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3105 * add standard channel maps if not specified
3107 static int add_std_chmaps(struct hda_codec *codec)
3109 struct hda_pcm *pcm;
3112 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3113 for (str = 0; str < 2; str++) {
3114 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3115 struct snd_pcm_chmap *chmap;
3116 const struct snd_pcm_chmap_elem *elem;
3118 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3120 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3121 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3122 hinfo->channels_max,
3126 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3132 /* default channel maps for 2.1 speakers;
3133 * since HD-audio supports only stereo, odd number channels are omitted
3135 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3137 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3139 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3140 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3143 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3145 int snd_hda_codec_build_controls(struct hda_codec *codec)
3148 hda_exec_init_verbs(codec);
3149 /* continue to initialize... */
3150 if (codec->patch_ops.init)
3151 err = codec->patch_ops.init(codec);
3152 if (!err && codec->patch_ops.build_controls)
3153 err = codec->patch_ops.build_controls(codec);
3157 /* we create chmaps here instead of build_pcms */
3158 err = add_std_chmaps(codec);
3162 if (codec->jackpoll_interval)
3163 hda_jackpoll_work(&codec->jackpoll_work.work);
3165 snd_hda_jack_report_sync(codec); /* call at the last init point */
3166 sync_power_up_states(codec);
3173 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3174 struct hda_codec *codec,
3175 struct snd_pcm_substream *substream)
3180 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 unsigned int stream_tag,
3183 unsigned int format,
3184 struct snd_pcm_substream *substream)
3186 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3190 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3191 struct hda_codec *codec,
3192 struct snd_pcm_substream *substream)
3194 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3198 static int set_pcm_default_values(struct hda_codec *codec,
3199 struct hda_pcm_stream *info)
3203 /* query support PCM information from the given NID */
3204 if (info->nid && (!info->rates || !info->formats)) {
3205 err = snd_hda_query_supported_pcm(codec, info->nid,
3206 info->rates ? NULL : &info->rates,
3207 info->formats ? NULL : &info->formats,
3208 info->maxbps ? NULL : &info->maxbps);
3212 if (info->ops.open == NULL)
3213 info->ops.open = hda_pcm_default_open_close;
3214 if (info->ops.close == NULL)
3215 info->ops.close = hda_pcm_default_open_close;
3216 if (info->ops.prepare == NULL) {
3217 if (snd_BUG_ON(!info->nid))
3219 info->ops.prepare = hda_pcm_default_prepare;
3221 if (info->ops.cleanup == NULL) {
3222 if (snd_BUG_ON(!info->nid))
3224 info->ops.cleanup = hda_pcm_default_cleanup;
3230 * codec prepare/cleanup entries
3233 * snd_hda_codec_prepare - Prepare a stream
3234 * @codec: the HDA codec
3235 * @hinfo: PCM information
3236 * @stream: stream tag to assign
3237 * @format: format id to assign
3238 * @substream: PCM substream to assign
3240 * Calls the prepare callback set by the codec with the given arguments.
3241 * Clean up the inactive streams when successful.
3243 int snd_hda_codec_prepare(struct hda_codec *codec,
3244 struct hda_pcm_stream *hinfo,
3245 unsigned int stream,
3246 unsigned int format,
3247 struct snd_pcm_substream *substream)
3250 mutex_lock(&codec->bus->prepare_mutex);
3251 if (hinfo->ops.prepare)
3252 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3257 purify_inactive_streams(codec);
3258 mutex_unlock(&codec->bus->prepare_mutex);
3261 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3264 * snd_hda_codec_cleanup - Prepare a stream
3265 * @codec: the HDA codec
3266 * @hinfo: PCM information
3267 * @substream: PCM substream
3269 * Calls the cleanup callback set by the codec with the given arguments.
3271 void snd_hda_codec_cleanup(struct hda_codec *codec,
3272 struct hda_pcm_stream *hinfo,
3273 struct snd_pcm_substream *substream)
3275 mutex_lock(&codec->bus->prepare_mutex);
3276 if (hinfo->ops.cleanup)
3277 hinfo->ops.cleanup(hinfo, codec, substream);
3278 mutex_unlock(&codec->bus->prepare_mutex);
3280 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3283 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3284 "Audio", "SPDIF", "HDMI", "Modem"
3288 * get the empty PCM device number to assign
3290 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3292 /* audio device indices; not linear to keep compatibility */
3293 /* assigned to static slots up to dev#10; if more needed, assign
3294 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3296 static int audio_idx[HDA_PCM_NTYPES][5] = {
3297 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3298 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3299 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3300 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3304 if (type >= HDA_PCM_NTYPES) {
3305 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3309 for (i = 0; audio_idx[type][i] >= 0; i++) {
3310 #ifndef CONFIG_SND_DYNAMIC_MINORS
3311 if (audio_idx[type][i] >= 8)
3314 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3315 return audio_idx[type][i];
3318 #ifdef CONFIG_SND_DYNAMIC_MINORS
3319 /* non-fixed slots starting from 10 */
3320 for (i = 10; i < 32; i++) {
3321 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3326 dev_warn(bus->card->dev, "Too many %s devices\n",
3327 snd_hda_pcm_type_name[type]);
3328 #ifndef CONFIG_SND_DYNAMIC_MINORS
3329 dev_warn(bus->card->dev,
3330 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3335 /* call build_pcms ops of the given codec and set up the default parameters */
3336 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3338 struct hda_pcm *cpcm;
3341 if (!list_empty(&codec->pcm_list_head))
3342 return 0; /* already parsed */
3344 if (!codec->patch_ops.build_pcms)
3347 err = codec->patch_ops.build_pcms(codec);
3349 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3350 codec->core.addr, err);
3354 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3357 for (stream = 0; stream < 2; stream++) {
3358 struct hda_pcm_stream *info = &cpcm->stream[stream];
3360 if (!info->substreams)
3362 err = set_pcm_default_values(codec, info);
3365 "fail to setup default for PCM %s\n",
3375 /* assign all PCMs of the given codec */
3376 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3378 struct hda_bus *bus = codec->bus;
3379 struct hda_pcm *cpcm;
3382 err = snd_hda_codec_parse_pcms(codec);
3386 /* attach a new PCM streams */
3387 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3389 continue; /* already attached */
3390 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3391 continue; /* no substreams assigned */
3393 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3395 continue; /* no fatal error */
3397 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3400 "cannot attach PCM stream %d for codec #%d\n",
3401 dev, codec->core.addr);
3402 continue; /* no fatal error */
3410 * snd_hda_add_new_ctls - create controls from the array
3411 * @codec: the HDA codec
3412 * @knew: the array of struct snd_kcontrol_new
3414 * This helper function creates and add new controls in the given array.
3415 * The array must be terminated with an empty entry as terminator.
3417 * Returns 0 if successful, or a negative error code.
3419 int snd_hda_add_new_ctls(struct hda_codec *codec,
3420 const struct snd_kcontrol_new *knew)
3424 for (; knew->name; knew++) {
3425 struct snd_kcontrol *kctl;
3426 int addr = 0, idx = 0;
3427 if (knew->iface == -1) /* skip this codec private value */
3430 kctl = snd_ctl_new1(knew, codec);
3434 kctl->id.device = addr;
3436 kctl->id.index = idx;
3437 err = snd_hda_ctl_add(codec, 0, kctl);
3440 /* try first with another device index corresponding to
3441 * the codec addr; if it still fails (or it's the
3442 * primary codec), then try another control index
3444 if (!addr && codec->core.addr)
3445 addr = codec->core.addr;
3446 else if (!idx && !knew->index) {
3447 idx = find_empty_mixer_ctl_idx(codec,
3457 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3460 static void codec_set_power_save(struct hda_codec *codec, int delay)
3462 struct device *dev = hda_codec_dev(codec);
3464 if (delay == 0 && codec->auto_runtime_pm)
3468 pm_runtime_set_autosuspend_delay(dev, delay);
3469 pm_runtime_use_autosuspend(dev);
3470 pm_runtime_allow(dev);
3471 if (!pm_runtime_suspended(dev))
3472 pm_runtime_mark_last_busy(dev);
3474 pm_runtime_dont_use_autosuspend(dev);
3475 pm_runtime_forbid(dev);
3480 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3481 * @bus: HD-audio bus
3482 * @delay: autosuspend delay in msec, 0 = off
3484 * Synchronize the runtime PM autosuspend state from the power_save option.
3486 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3488 struct hda_codec *c;
3490 list_for_each_codec(c, bus)
3491 codec_set_power_save(c, delay);
3493 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3496 * snd_hda_check_amp_list_power - Check the amp list and update the power
3497 * @codec: HD-audio codec
3498 * @check: the object containing an AMP list and the status
3499 * @nid: NID to check / update
3501 * Check whether the given NID is in the amp list. If it's in the list,
3502 * check the current AMP status, and update the power-status according
3503 * to the mute status.
3505 * This function is supposed to be set or called from the check_power_status
3508 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3509 struct hda_loopback_check *check,
3512 const struct hda_amp_list *p;
3515 if (!check->amplist)
3517 for (p = check->amplist; p->nid; p++) {
3522 return 0; /* nothing changed */
3524 for (p = check->amplist; p->nid; p++) {
3525 for (ch = 0; ch < 2; ch++) {
3526 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3528 if (!(v & HDA_AMP_MUTE) && v > 0) {
3529 if (!check->power_on) {
3530 check->power_on = 1;
3531 snd_hda_power_up_pm(codec);
3537 if (check->power_on) {
3538 check->power_on = 0;
3539 snd_hda_power_down_pm(codec);
3543 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3551 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3552 * @imux: imux helper object
3553 * @uinfo: pointer to get/store the data
3555 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3556 struct snd_ctl_elem_info *uinfo)
3560 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3562 uinfo->value.enumerated.items = imux->num_items;
3563 if (!imux->num_items)
3565 index = uinfo->value.enumerated.item;
3566 if (index >= imux->num_items)
3567 index = imux->num_items - 1;
3568 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3571 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3574 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3575 * @codec: the HDA codec
3576 * @imux: imux helper object
3577 * @ucontrol: pointer to get/store the data
3578 * @nid: input mux NID
3579 * @cur_val: pointer to get/store the current imux value
3581 int snd_hda_input_mux_put(struct hda_codec *codec,
3582 const struct hda_input_mux *imux,
3583 struct snd_ctl_elem_value *ucontrol,
3585 unsigned int *cur_val)
3589 if (!imux->num_items)
3591 idx = ucontrol->value.enumerated.item[0];
3592 if (idx >= imux->num_items)
3593 idx = imux->num_items - 1;
3594 if (*cur_val == idx)
3596 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3597 imux->items[idx].index);
3601 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3605 * snd_hda_enum_helper_info - Helper for simple enum ctls
3606 * @kcontrol: ctl element
3607 * @uinfo: pointer to get/store the data
3608 * @num_items: number of enum items
3609 * @texts: enum item string array
3611 * process kcontrol info callback of a simple string enum array
3612 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3614 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3615 struct snd_ctl_elem_info *uinfo,
3616 int num_items, const char * const *texts)
3618 static const char * const texts_default[] = {
3619 "Disabled", "Enabled"
3622 if (!texts || !num_items) {
3624 texts = texts_default;
3627 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3629 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3632 * Multi-channel / digital-out PCM helper functions
3635 /* setup SPDIF output stream */
3636 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3637 unsigned int stream_tag, unsigned int format)
3639 struct hda_spdif_out *spdif;
3640 unsigned int curr_fmt;
3643 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3644 /* Add sanity check to pass klockwork check.
3645 * This should never happen.
3647 if (WARN_ON(spdif == NULL))
3650 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3651 AC_VERB_GET_STREAM_FORMAT, 0);
3652 reset = codec->spdif_status_reset &&
3653 (spdif->ctls & AC_DIG1_ENABLE) &&
3656 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3659 set_dig_out_convert(codec, nid,
3660 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3662 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3663 if (codec->slave_dig_outs) {
3665 for (d = codec->slave_dig_outs; *d; d++)
3666 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3669 /* turn on again (if needed) */
3671 set_dig_out_convert(codec, nid,
3672 spdif->ctls & 0xff, -1);
3675 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3677 snd_hda_codec_cleanup_stream(codec, nid);
3678 if (codec->slave_dig_outs) {
3680 for (d = codec->slave_dig_outs; *d; d++)
3681 snd_hda_codec_cleanup_stream(codec, *d);
3686 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3687 * @codec: the HDA codec
3688 * @mout: hda_multi_out object
3690 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3691 struct hda_multi_out *mout)
3693 mutex_lock(&codec->spdif_mutex);
3694 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3695 /* already opened as analog dup; reset it once */
3696 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3697 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3698 mutex_unlock(&codec->spdif_mutex);
3701 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3704 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3705 * @codec: the HDA codec
3706 * @mout: hda_multi_out object
3707 * @stream_tag: stream tag to assign
3708 * @format: format id to assign
3709 * @substream: PCM substream to assign
3711 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3712 struct hda_multi_out *mout,
3713 unsigned int stream_tag,
3714 unsigned int format,
3715 struct snd_pcm_substream *substream)
3717 mutex_lock(&codec->spdif_mutex);
3718 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3719 mutex_unlock(&codec->spdif_mutex);
3722 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3725 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3726 * @codec: the HDA codec
3727 * @mout: hda_multi_out object
3729 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3730 struct hda_multi_out *mout)
3732 mutex_lock(&codec->spdif_mutex);
3733 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3734 mutex_unlock(&codec->spdif_mutex);
3737 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3740 * snd_hda_multi_out_dig_close - release the digital out stream
3741 * @codec: the HDA codec
3742 * @mout: hda_multi_out object
3744 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3745 struct hda_multi_out *mout)
3747 mutex_lock(&codec->spdif_mutex);
3748 mout->dig_out_used = 0;
3749 mutex_unlock(&codec->spdif_mutex);
3752 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3755 * snd_hda_multi_out_analog_open - open analog outputs
3756 * @codec: the HDA codec
3757 * @mout: hda_multi_out object
3758 * @substream: PCM substream to assign
3759 * @hinfo: PCM information to assign
3761 * Open analog outputs and set up the hw-constraints.
3762 * If the digital outputs can be opened as slave, open the digital
3765 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3766 struct hda_multi_out *mout,
3767 struct snd_pcm_substream *substream,
3768 struct hda_pcm_stream *hinfo)
3770 struct snd_pcm_runtime *runtime = substream->runtime;
3771 runtime->hw.channels_max = mout->max_channels;
3772 if (mout->dig_out_nid) {
3773 if (!mout->analog_rates) {
3774 mout->analog_rates = hinfo->rates;
3775 mout->analog_formats = hinfo->formats;
3776 mout->analog_maxbps = hinfo->maxbps;
3778 runtime->hw.rates = mout->analog_rates;
3779 runtime->hw.formats = mout->analog_formats;
3780 hinfo->maxbps = mout->analog_maxbps;
3782 if (!mout->spdif_rates) {
3783 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3785 &mout->spdif_formats,
3786 &mout->spdif_maxbps);
3788 mutex_lock(&codec->spdif_mutex);
3789 if (mout->share_spdif) {
3790 if ((runtime->hw.rates & mout->spdif_rates) &&
3791 (runtime->hw.formats & mout->spdif_formats)) {
3792 runtime->hw.rates &= mout->spdif_rates;
3793 runtime->hw.formats &= mout->spdif_formats;
3794 if (mout->spdif_maxbps < hinfo->maxbps)
3795 hinfo->maxbps = mout->spdif_maxbps;
3797 mout->share_spdif = 0;
3798 /* FIXME: need notify? */
3801 mutex_unlock(&codec->spdif_mutex);
3803 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3804 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3806 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3809 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3810 * @codec: the HDA codec
3811 * @mout: hda_multi_out object
3812 * @stream_tag: stream tag to assign
3813 * @format: format id to assign
3814 * @substream: PCM substream to assign
3816 * Set up the i/o for analog out.
3817 * When the digital out is available, copy the front out to digital out, too.
3819 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3820 struct hda_multi_out *mout,
3821 unsigned int stream_tag,
3822 unsigned int format,
3823 struct snd_pcm_substream *substream)
3825 const hda_nid_t *nids = mout->dac_nids;
3826 int chs = substream->runtime->channels;
3827 struct hda_spdif_out *spdif;
3830 mutex_lock(&codec->spdif_mutex);
3831 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3832 if (mout->dig_out_nid && mout->share_spdif &&
3833 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3834 if (chs == 2 && spdif != NULL &&
3835 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3837 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3838 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3839 setup_dig_out_stream(codec, mout->dig_out_nid,
3840 stream_tag, format);
3842 mout->dig_out_used = 0;
3843 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3846 mutex_unlock(&codec->spdif_mutex);
3849 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3851 if (!mout->no_share_stream &&
3852 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3853 /* headphone out will just decode front left/right (stereo) */
3854 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3856 /* extra outputs copied from front */
3857 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3858 if (!mout->no_share_stream && mout->hp_out_nid[i])
3859 snd_hda_codec_setup_stream(codec,
3860 mout->hp_out_nid[i],
3861 stream_tag, 0, format);
3864 for (i = 1; i < mout->num_dacs; i++) {
3865 if (chs >= (i + 1) * 2) /* independent out */
3866 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3868 else if (!mout->no_share_stream) /* copy front */
3869 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3873 /* extra surrounds */
3874 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3876 if (!mout->extra_out_nid[i])
3878 if (chs >= (i + 1) * 2)
3880 else if (!mout->no_share_stream)
3882 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3883 stream_tag, ch, format);
3888 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3891 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3892 * @codec: the HDA codec
3893 * @mout: hda_multi_out object
3895 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3896 struct hda_multi_out *mout)
3898 const hda_nid_t *nids = mout->dac_nids;
3901 for (i = 0; i < mout->num_dacs; i++)
3902 snd_hda_codec_cleanup_stream(codec, nids[i]);
3904 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3905 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3906 if (mout->hp_out_nid[i])
3907 snd_hda_codec_cleanup_stream(codec,
3908 mout->hp_out_nid[i]);
3909 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3910 if (mout->extra_out_nid[i])
3911 snd_hda_codec_cleanup_stream(codec,
3912 mout->extra_out_nid[i]);
3913 mutex_lock(&codec->spdif_mutex);
3914 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3915 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3916 mout->dig_out_used = 0;
3918 mutex_unlock(&codec->spdif_mutex);
3921 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3924 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3925 * @codec: the HDA codec
3926 * @pin: referred pin NID
3928 * Guess the suitable VREF pin bits to be set as the pin-control value.
3929 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3931 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3933 unsigned int pincap;
3934 unsigned int oldval;
3935 oldval = snd_hda_codec_read(codec, pin, 0,
3936 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3937 pincap = snd_hda_query_pin_caps(codec, pin);
3938 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3939 /* Exception: if the default pin setup is vref50, we give it priority */
3940 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3941 return AC_PINCTL_VREF_80;
3942 else if (pincap & AC_PINCAP_VREF_50)
3943 return AC_PINCTL_VREF_50;
3944 else if (pincap & AC_PINCAP_VREF_100)
3945 return AC_PINCTL_VREF_100;
3946 else if (pincap & AC_PINCAP_VREF_GRD)
3947 return AC_PINCTL_VREF_GRD;
3948 return AC_PINCTL_VREF_HIZ;
3950 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3953 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3954 * @codec: the HDA codec
3955 * @pin: referred pin NID
3956 * @val: pin ctl value to audit
3958 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3959 hda_nid_t pin, unsigned int val)
3961 static unsigned int cap_lists[][2] = {
3962 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3963 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3964 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3965 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3971 cap = snd_hda_query_pin_caps(codec, pin);
3973 return val; /* don't know what to do... */
3975 if (val & AC_PINCTL_OUT_EN) {
3976 if (!(cap & AC_PINCAP_OUT))
3977 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3978 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3979 val &= ~AC_PINCTL_HP_EN;
3982 if (val & AC_PINCTL_IN_EN) {
3983 if (!(cap & AC_PINCAP_IN))
3984 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3986 unsigned int vcap, vref;
3988 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3989 vref = val & AC_PINCTL_VREFEN;
3990 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3991 if (vref == cap_lists[i][0] &&
3992 !(vcap & cap_lists[i][1])) {
3993 if (i == ARRAY_SIZE(cap_lists) - 1)
3994 vref = AC_PINCTL_VREF_HIZ;
3996 vref = cap_lists[i + 1][0];
3999 val &= ~AC_PINCTL_VREFEN;
4006 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4009 * _snd_hda_pin_ctl - Helper to set pin ctl value
4010 * @codec: the HDA codec
4011 * @pin: referred pin NID
4012 * @val: pin control value to set
4013 * @cached: access over codec pinctl cache or direct write
4015 * This function is a helper to set a pin ctl value more safely.
4016 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4017 * value in pin target array via snd_hda_codec_set_pin_target(), then
4018 * actually writes the value via either snd_hda_codec_update_cache() or
4019 * snd_hda_codec_write() depending on @cached flag.
4021 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4022 unsigned int val, bool cached)
4024 val = snd_hda_correct_pin_ctl(codec, pin, val);
4025 snd_hda_codec_set_pin_target(codec, pin, val);
4027 return snd_hda_codec_update_cache(codec, pin, 0,
4028 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4030 return snd_hda_codec_write(codec, pin, 0,
4031 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4033 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4036 * snd_hda_add_imux_item - Add an item to input_mux
4037 * @codec: the HDA codec
4038 * @imux: imux helper object
4039 * @label: the name of imux item to assign
4040 * @index: index number of imux item to assign
4041 * @type_idx: pointer to store the resultant label index
4043 * When the same label is used already in the existing items, the number
4044 * suffix is appended to the label. This label index number is stored
4045 * to type_idx when non-NULL pointer is given.
4047 int snd_hda_add_imux_item(struct hda_codec *codec,
4048 struct hda_input_mux *imux, const char *label,
4049 int index, int *type_idx)
4051 int i, label_idx = 0;
4052 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4053 codec_err(codec, "hda_codec: Too many imux items!\n");
4056 for (i = 0; i < imux->num_items; i++) {
4057 if (!strncmp(label, imux->items[i].label, strlen(label)))
4061 *type_idx = label_idx;
4063 snprintf(imux->items[imux->num_items].label,
4064 sizeof(imux->items[imux->num_items].label),
4065 "%s %d", label, label_idx);
4067 strlcpy(imux->items[imux->num_items].label, label,
4068 sizeof(imux->items[imux->num_items].label));
4069 imux->items[imux->num_items].index = index;
4073 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4076 * snd_hda_bus_reset_codecs - Reset the bus
4077 * @bus: HD-audio bus
4079 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4081 struct hda_codec *codec;
4083 list_for_each_codec(codec, bus) {
4084 /* FIXME: maybe a better way needed for forced reset */
4085 if (current_work() != &codec->jackpoll_work.work)
4086 cancel_delayed_work_sync(&codec->jackpoll_work);
4088 if (hda_codec_is_power_on(codec)) {
4089 hda_call_codec_suspend(codec);
4090 hda_call_codec_resume(codec);
4097 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4098 * @pcm: PCM caps bits
4099 * @buf: the string buffer to write
4100 * @buflen: the max buffer length
4102 * used by hda_proc.c and hda_eld.c
4104 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4106 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4109 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4110 if (pcm & (AC_SUPPCM_BITS_8 << i))
4111 j += scnprintf(buf + j, buflen - j, " %d", bits[i]);
4113 buf[j] = '\0'; /* necessary when j == 0 */
4115 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4117 MODULE_DESCRIPTION("HDA codec core");
4118 MODULE_LICENSE("GPL");