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 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
316 * @codec: the HDA codec
317 * @nid: NID of the pin to parse
319 * Get the device entry number on the given widget. This is a feature of
320 * DP MST audio. Each pin can have several device entries in it.
322 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
324 unsigned int wcaps = get_wcaps(codec, nid);
327 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
328 get_wcaps_type(wcaps) != AC_WID_PIN)
331 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
334 return parm & AC_DEV_LIST_LEN_MASK;
336 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
339 * snd_hda_get_devices - copy device list without cache
340 * @codec: the HDA codec
341 * @nid: NID of the pin to parse
342 * @dev_list: device list array
343 * @max_devices: max. number of devices to store
345 * Copy the device list. This info is dynamic and so not cached.
346 * Currently called only from hda_proc.c, so not exported.
348 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
349 u8 *dev_list, int max_devices)
352 int i, dev_len, devices;
354 parm = snd_hda_get_num_devices(codec, nid);
355 if (!parm) /* not multi-stream capable */
359 dev_len = dev_len < max_devices ? dev_len : max_devices;
362 while (devices < dev_len) {
363 if (snd_hdac_read(&codec->core, nid,
364 AC_VERB_GET_DEVICE_LIST, devices, &parm))
367 for (i = 0; i < 8; i++) {
368 dev_list[devices] = (u8)parm;
371 if (devices >= dev_len)
379 * snd_hda_get_dev_select - get device entry select on the pin
380 * @codec: the HDA codec
381 * @nid: NID of the pin to get device entry select
383 * Get the devcie entry select on the pin. Return the device entry
384 * id selected on the pin. Return 0 means the first device entry
385 * is selected or MST is not supported.
387 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
389 /* not support dp_mst will always return 0, using first dev_entry */
393 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
395 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
398 * snd_hda_set_dev_select - set device entry select on the pin
399 * @codec: the HDA codec
400 * @nid: NID of the pin to set device entry select
401 * @dev_id: device entry id to be set
403 * Set the device entry select on the pin nid.
405 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
407 int ret, num_devices;
409 /* not support dp_mst will always return 0, using first dev_entry */
413 /* AC_PAR_DEVLIST_LEN is 0 based. */
414 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
415 /* If Device List Length is 0 (num_device = 1),
416 * the pin is not multi stream capable.
417 * Do nothing in this case.
419 if (num_devices == 1)
422 /* Behavior of setting index being equal to or greater than
423 * Device List Length is not predictable
425 if (num_devices <= dev_id)
428 ret = snd_hda_codec_write(codec, nid, 0,
429 AC_VERB_SET_DEVICE_SEL, dev_id);
433 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
436 * read widget caps for each widget and store in cache
438 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
443 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
446 nid = codec->core.start_nid;
447 for (i = 0; i < codec->core.num_nodes; i++, nid++)
448 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
449 nid, AC_PAR_AUDIO_WIDGET_CAP);
453 /* read all pin default configurations and save codec->init_pins */
454 static int read_pin_defaults(struct hda_codec *codec)
458 for_each_hda_codec_node(nid, codec) {
459 struct hda_pincfg *pin;
460 unsigned int wcaps = get_wcaps(codec, nid);
461 unsigned int wid_type = get_wcaps_type(wcaps);
462 if (wid_type != AC_WID_PIN)
464 pin = snd_array_new(&codec->init_pins);
468 pin->cfg = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONFIG_DEFAULT, 0);
471 * all device entries are the same widget control so far
472 * fixme: if any codec is different, need fix here
474 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
475 AC_VERB_GET_PIN_WIDGET_CONTROL,
481 /* look up the given pin config list and return the item matching with NID */
482 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
483 struct snd_array *array,
487 for (i = 0; i < array->used; i++) {
488 struct hda_pincfg *pin = snd_array_elem(array, i);
495 /* set the current pin config value for the given NID.
496 * the value is cached, and read via snd_hda_codec_get_pincfg()
498 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
499 hda_nid_t nid, unsigned int cfg)
501 struct hda_pincfg *pin;
503 /* the check below may be invalid when pins are added by a fixup
504 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
508 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
512 pin = look_up_pincfg(codec, list, nid);
514 pin = snd_array_new(list);
524 * snd_hda_codec_set_pincfg - Override a pin default configuration
525 * @codec: the HDA codec
526 * @nid: NID to set the pin config
527 * @cfg: the pin default config value
529 * Override a pin default configuration value in the cache.
530 * This value can be read by snd_hda_codec_get_pincfg() in a higher
531 * priority than the real hardware value.
533 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
534 hda_nid_t nid, unsigned int cfg)
536 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
538 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
541 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
542 * @codec: the HDA codec
543 * @nid: NID to get the pin config
545 * Get the current pin config value of the given pin NID.
546 * If the pincfg value is cached or overridden via sysfs or driver,
547 * returns the cached value.
549 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
551 struct hda_pincfg *pin;
553 #ifdef CONFIG_SND_HDA_RECONFIG
555 unsigned int cfg = 0;
556 mutex_lock(&codec->user_mutex);
557 pin = look_up_pincfg(codec, &codec->user_pins, nid);
560 mutex_unlock(&codec->user_mutex);
565 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
568 pin = look_up_pincfg(codec, &codec->init_pins, nid);
573 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
576 * snd_hda_codec_set_pin_target - remember the current pinctl target value
577 * @codec: the HDA codec
579 * @val: assigned pinctl value
581 * This function stores the given value to a pinctl target value in the
582 * pincfg table. This isn't always as same as the actually written value
583 * but can be referred at any time via snd_hda_codec_get_pin_target().
585 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
588 struct hda_pincfg *pin;
590 pin = look_up_pincfg(codec, &codec->init_pins, nid);
596 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
599 * snd_hda_codec_get_pin_target - return the current pinctl target value
600 * @codec: the HDA codec
603 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
605 struct hda_pincfg *pin;
607 pin = look_up_pincfg(codec, &codec->init_pins, nid);
612 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
615 * snd_hda_shutup_pins - Shut up all pins
616 * @codec: the HDA codec
618 * Clear all pin controls to shup up before suspend for avoiding click noise.
619 * The controls aren't cached so that they can be resumed properly.
621 void snd_hda_shutup_pins(struct hda_codec *codec)
624 /* don't shut up pins when unloading the driver; otherwise it breaks
625 * the default pin setup at the next load of the driver
627 if (codec->bus->shutdown)
629 for (i = 0; i < codec->init_pins.used; i++) {
630 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
631 /* use read here for syncing after issuing each verb */
632 snd_hda_codec_read(codec, pin->nid, 0,
633 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
635 codec->pins_shutup = 1;
637 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
640 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
641 static void restore_shutup_pins(struct hda_codec *codec)
644 if (!codec->pins_shutup)
646 if (codec->bus->shutdown)
648 for (i = 0; i < codec->init_pins.used; i++) {
649 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
650 snd_hda_codec_write(codec, pin->nid, 0,
651 AC_VERB_SET_PIN_WIDGET_CONTROL,
654 codec->pins_shutup = 0;
658 static void hda_jackpoll_work(struct work_struct *work)
660 struct hda_codec *codec =
661 container_of(work, struct hda_codec, jackpoll_work.work);
663 snd_hda_jack_set_dirty_all(codec);
664 snd_hda_jack_poll_all(codec);
666 if (!codec->jackpoll_interval)
669 schedule_delayed_work(&codec->jackpoll_work,
670 codec->jackpoll_interval);
673 /* release all pincfg lists */
674 static void free_init_pincfgs(struct hda_codec *codec)
676 snd_array_free(&codec->driver_pins);
677 #ifdef CONFIG_SND_HDA_RECONFIG
678 snd_array_free(&codec->user_pins);
680 snd_array_free(&codec->init_pins);
684 * audio-converter setup caches
686 struct hda_cvt_setup {
691 unsigned char active; /* cvt is currently used */
692 unsigned char dirty; /* setups should be cleared */
695 /* get or create a cache entry for the given audio converter NID */
696 static struct hda_cvt_setup *
697 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
699 struct hda_cvt_setup *p;
702 for (i = 0; i < codec->cvt_setups.used; i++) {
703 p = snd_array_elem(&codec->cvt_setups, i);
707 p = snd_array_new(&codec->cvt_setups);
716 static void release_pcm(struct kref *kref)
718 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
721 snd_device_free(pcm->codec->card, pcm->pcm);
722 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
727 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
729 kref_put(&pcm->kref, release_pcm);
731 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
733 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
734 const char *fmt, ...)
739 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
744 kref_init(&pcm->kref);
746 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
753 list_add_tail(&pcm->list, &codec->pcm_list_head);
756 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
761 static void codec_release_pcms(struct hda_codec *codec)
763 struct hda_pcm *pcm, *n;
765 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
766 list_del_init(&pcm->list);
768 snd_device_disconnect(codec->card, pcm->pcm);
769 snd_hda_codec_pcm_put(pcm);
773 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
775 if (codec->registered) {
776 /* pm_runtime_put() is called in snd_hdac_device_exit() */
777 pm_runtime_get_noresume(hda_codec_dev(codec));
778 pm_runtime_disable(hda_codec_dev(codec));
779 codec->registered = 0;
782 cancel_delayed_work_sync(&codec->jackpoll_work);
783 if (!codec->in_freeing)
784 snd_hda_ctls_clear(codec);
785 codec_release_pcms(codec);
786 snd_hda_detach_beep_device(codec);
787 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
788 snd_hda_jack_tbl_clear(codec);
789 codec->proc_widget_hook = NULL;
792 /* free only driver_pins so that init_pins + user_pins are restored */
793 snd_array_free(&codec->driver_pins);
794 snd_array_free(&codec->cvt_setups);
795 snd_array_free(&codec->spdif_out);
796 snd_array_free(&codec->verbs);
797 codec->preset = NULL;
798 codec->slave_dig_outs = NULL;
799 codec->spdif_status_reset = 0;
800 snd_array_free(&codec->mixers);
801 snd_array_free(&codec->nids);
802 remove_conn_list(codec);
803 snd_hdac_regmap_exit(&codec->core);
806 static unsigned int hda_set_power_state(struct hda_codec *codec,
807 unsigned int power_state);
809 /* also called from hda_bind.c */
810 void snd_hda_codec_register(struct hda_codec *codec)
812 if (codec->registered)
814 if (device_is_registered(hda_codec_dev(codec))) {
815 snd_hda_register_beep_device(codec);
816 snd_hdac_link_power(&codec->core, true);
817 pm_runtime_enable(hda_codec_dev(codec));
818 /* it was powered up in snd_hda_codec_new(), now all done */
819 snd_hda_power_down(codec);
820 codec->registered = 1;
824 static int snd_hda_codec_dev_register(struct snd_device *device)
826 snd_hda_codec_register(device->device_data);
830 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
832 struct hda_codec *codec = device->device_data;
834 snd_hda_detach_beep_device(codec);
838 static int snd_hda_codec_dev_free(struct snd_device *device)
840 struct hda_codec *codec = device->device_data;
842 codec->in_freeing = 1;
843 snd_hdac_device_unregister(&codec->core);
844 snd_hdac_link_power(&codec->core, false);
845 put_device(hda_codec_dev(codec));
849 static void snd_hda_codec_dev_release(struct device *dev)
851 struct hda_codec *codec = dev_to_hda_codec(dev);
853 free_init_pincfgs(codec);
854 snd_hdac_device_exit(&codec->core);
855 snd_hda_sysfs_clear(codec);
856 kfree(codec->modelname);
862 * snd_hda_codec_new - create a HDA codec
863 * @bus: the bus to assign
864 * @codec_addr: the codec address
865 * @codecp: the pointer to store the generated codec
867 * Returns 0 if successful, or a negative error code.
869 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
870 unsigned int codec_addr, struct hda_codec **codecp)
872 struct hda_codec *codec;
876 static struct snd_device_ops dev_ops = {
877 .dev_register = snd_hda_codec_dev_register,
878 .dev_disconnect = snd_hda_codec_dev_disconnect,
879 .dev_free = snd_hda_codec_dev_free,
882 if (snd_BUG_ON(!bus))
884 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
887 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
891 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
892 err = snd_hdac_device_init(&codec->core, &bus->core, component,
899 codec->core.dev.release = snd_hda_codec_dev_release;
900 codec->core.type = HDA_DEV_LEGACY;
901 codec->core.exec_verb = codec_exec_verb;
905 codec->addr = codec_addr;
906 mutex_init(&codec->spdif_mutex);
907 mutex_init(&codec->control_mutex);
908 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
909 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
910 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
911 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
912 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
913 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
914 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
915 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
916 INIT_LIST_HEAD(&codec->conn_list);
917 INIT_LIST_HEAD(&codec->pcm_list_head);
919 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
920 codec->depop_delay = -1;
921 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
924 codec->power_jiffies = jiffies;
927 snd_hda_sysfs_init(codec);
929 if (codec->bus->modelname) {
930 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
931 if (!codec->modelname) {
937 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
938 err = read_widget_caps(codec, fg);
941 err = read_pin_defaults(codec);
945 /* power-up all before initialization */
946 hda_set_power_state(codec, AC_PWRST_D0);
948 snd_hda_codec_proc_new(codec);
950 snd_hda_create_hwdep(codec);
952 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
953 codec->core.subsystem_id, codec->core.revision_id);
954 snd_component_add(card, component);
956 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
965 put_device(hda_codec_dev(codec));
968 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
971 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
972 * @codec: the HDA codec
974 * Forcibly refresh the all widget caps and the init pin configurations of
977 int snd_hda_codec_update_widgets(struct hda_codec *codec)
982 err = snd_hdac_refresh_widget_sysfs(&codec->core);
986 /* Assume the function group node does not change,
987 * only the widget nodes may change.
990 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
991 err = read_widget_caps(codec, fg);
995 snd_array_free(&codec->init_pins);
996 err = read_pin_defaults(codec);
1000 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1002 /* update the stream-id if changed */
1003 static void update_pcm_stream_id(struct hda_codec *codec,
1004 struct hda_cvt_setup *p, hda_nid_t nid,
1005 u32 stream_tag, int channel_id)
1007 unsigned int oldval, newval;
1009 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1010 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1011 newval = (stream_tag << 4) | channel_id;
1012 if (oldval != newval)
1013 snd_hda_codec_write(codec, nid, 0,
1014 AC_VERB_SET_CHANNEL_STREAMID,
1016 p->stream_tag = stream_tag;
1017 p->channel_id = channel_id;
1021 /* update the format-id if changed */
1022 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1023 hda_nid_t nid, int format)
1025 unsigned int oldval;
1027 if (p->format_id != format) {
1028 oldval = snd_hda_codec_read(codec, nid, 0,
1029 AC_VERB_GET_STREAM_FORMAT, 0);
1030 if (oldval != format) {
1032 snd_hda_codec_write(codec, nid, 0,
1033 AC_VERB_SET_STREAM_FORMAT,
1036 p->format_id = format;
1041 * snd_hda_codec_setup_stream - set up the codec for streaming
1042 * @codec: the CODEC to set up
1043 * @nid: the NID to set up
1044 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1045 * @channel_id: channel id to pass, zero based.
1046 * @format: stream format.
1048 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1050 int channel_id, int format)
1052 struct hda_codec *c;
1053 struct hda_cvt_setup *p;
1061 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1062 nid, stream_tag, channel_id, format);
1063 p = get_hda_cvt_setup(codec, nid);
1067 if (codec->patch_ops.stream_pm)
1068 codec->patch_ops.stream_pm(codec, nid, true);
1069 if (codec->pcm_format_first)
1070 update_pcm_format(codec, p, nid, format);
1071 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1072 if (!codec->pcm_format_first)
1073 update_pcm_format(codec, p, nid, format);
1078 /* make other inactive cvts with the same stream-tag dirty */
1079 type = get_wcaps_type(get_wcaps(codec, nid));
1080 list_for_each_codec(c, codec->bus) {
1081 for (i = 0; i < c->cvt_setups.used; i++) {
1082 p = snd_array_elem(&c->cvt_setups, i);
1083 if (!p->active && p->stream_tag == stream_tag &&
1084 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1089 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1091 static void really_cleanup_stream(struct hda_codec *codec,
1092 struct hda_cvt_setup *q);
1095 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1096 * @codec: the CODEC to clean up
1097 * @nid: the NID to clean up
1098 * @do_now: really clean up the stream instead of clearing the active flag
1100 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1103 struct hda_cvt_setup *p;
1108 if (codec->no_sticky_stream)
1111 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1112 p = get_hda_cvt_setup(codec, nid);
1114 /* here we just clear the active flag when do_now isn't set;
1115 * actual clean-ups will be done later in
1116 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1119 really_cleanup_stream(codec, p);
1124 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1126 static void really_cleanup_stream(struct hda_codec *codec,
1127 struct hda_cvt_setup *q)
1129 hda_nid_t nid = q->nid;
1130 if (q->stream_tag || q->channel_id)
1131 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1133 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1135 memset(q, 0, sizeof(*q));
1137 if (codec->patch_ops.stream_pm)
1138 codec->patch_ops.stream_pm(codec, nid, false);
1141 /* clean up the all conflicting obsolete streams */
1142 static void purify_inactive_streams(struct hda_codec *codec)
1144 struct hda_codec *c;
1147 list_for_each_codec(c, codec->bus) {
1148 for (i = 0; i < c->cvt_setups.used; i++) {
1149 struct hda_cvt_setup *p;
1150 p = snd_array_elem(&c->cvt_setups, i);
1152 really_cleanup_stream(c, p);
1158 /* clean up all streams; called from suspend */
1159 static void hda_cleanup_all_streams(struct hda_codec *codec)
1163 for (i = 0; i < codec->cvt_setups.used; i++) {
1164 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1166 really_cleanup_stream(codec, p);
1172 * amp access functions
1176 * query_amp_caps - query AMP capabilities
1177 * @codec: the HD-auio codec
1178 * @nid: the NID to query
1179 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1181 * Query AMP capabilities for the given widget and direction.
1182 * Returns the obtained capability bits.
1184 * When cap bits have been already read, this doesn't read again but
1185 * returns the cached value.
1187 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1189 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1190 nid = codec->core.afg;
1191 return snd_hda_param_read(codec, nid,
1192 direction == HDA_OUTPUT ?
1193 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1195 EXPORT_SYMBOL_GPL(query_amp_caps);
1198 * snd_hda_check_amp_caps - query AMP capabilities
1199 * @codec: the HD-audio codec
1200 * @nid: the NID to query
1201 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1202 * @bits: bit mask to check the result
1204 * Check whether the widget has the given amp capability for the direction.
1206 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1207 int dir, unsigned int bits)
1211 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1212 if (query_amp_caps(codec, nid, dir) & bits)
1216 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1219 * snd_hda_override_amp_caps - Override the AMP capabilities
1220 * @codec: the CODEC to clean up
1221 * @nid: the NID to clean up
1222 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1223 * @caps: the capability bits to set
1225 * Override the cached AMP caps bits value by the given one.
1226 * This function is useful if the driver needs to adjust the AMP ranges,
1227 * e.g. limit to 0dB, etc.
1229 * Returns zero if successful or a negative error code.
1231 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1236 snd_hda_override_wcaps(codec, nid,
1237 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1238 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1239 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1241 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1244 * snd_hda_codec_amp_update - update the AMP mono value
1245 * @codec: HD-audio codec
1246 * @nid: NID to read the AMP value
1247 * @ch: channel to update (0 or 1)
1248 * @dir: #HDA_INPUT or #HDA_OUTPUT
1249 * @idx: the index value (only for input direction)
1250 * @mask: bit mask to set
1251 * @val: the bits value to set
1253 * Update the AMP values for the given channel, direction and index.
1255 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1256 int ch, int dir, int idx, int mask, int val)
1258 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1260 /* enable fake mute if no h/w mute but min=mute */
1261 if ((query_amp_caps(codec, nid, dir) &
1262 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1263 cmd |= AC_AMP_FAKE_MUTE;
1264 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1266 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1269 * snd_hda_codec_amp_stereo - update the AMP stereo values
1270 * @codec: HD-audio codec
1271 * @nid: NID to read the AMP value
1272 * @direction: #HDA_INPUT or #HDA_OUTPUT
1273 * @idx: the index value (only for input direction)
1274 * @mask: bit mask to set
1275 * @val: the bits value to set
1277 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1278 * stereo widget with the same mask and value.
1280 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1281 int direction, int idx, int mask, int val)
1285 if (snd_BUG_ON(mask & ~0xff))
1287 for (ch = 0; ch < 2; ch++)
1288 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1292 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1295 * snd_hda_codec_amp_init - initialize the AMP value
1296 * @codec: the HDA codec
1297 * @nid: NID to read the AMP value
1298 * @ch: channel (left=0 or right=1)
1299 * @dir: #HDA_INPUT or #HDA_OUTPUT
1300 * @idx: the index value (only for input direction)
1301 * @mask: bit mask to set
1302 * @val: the bits value to set
1304 * Works like snd_hda_codec_amp_update() but it writes the value only at
1305 * the first access. If the amp was already initialized / updated beforehand,
1306 * this does nothing.
1308 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1309 int dir, int idx, int mask, int val)
1313 if (!codec->core.regmap)
1315 regcache_cache_only(codec->core.regmap, true);
1316 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1317 regcache_cache_only(codec->core.regmap, false);
1320 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1322 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1325 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1326 * @codec: the HDA codec
1327 * @nid: NID to read the AMP value
1328 * @dir: #HDA_INPUT or #HDA_OUTPUT
1329 * @idx: the index value (only for input direction)
1330 * @mask: bit mask to set
1331 * @val: the bits value to set
1333 * Call snd_hda_codec_amp_init() for both stereo channels.
1335 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1336 int dir, int idx, int mask, int val)
1340 if (snd_BUG_ON(mask & ~0xff))
1342 for (ch = 0; ch < 2; ch++)
1343 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1347 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1349 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1352 u32 caps = query_amp_caps(codec, nid, dir);
1354 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1361 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1362 * @kcontrol: referred ctl element
1363 * @uinfo: pointer to get/store the data
1365 * The control element is supposed to have the private_value field
1366 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1368 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_info *uinfo)
1371 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1372 u16 nid = get_amp_nid(kcontrol);
1373 u8 chs = get_amp_channels(kcontrol);
1374 int dir = get_amp_direction(kcontrol);
1375 unsigned int ofs = get_amp_offset(kcontrol);
1377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1378 uinfo->count = chs == 3 ? 2 : 1;
1379 uinfo->value.integer.min = 0;
1380 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1381 if (!uinfo->value.integer.max) {
1383 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1384 nid, kcontrol->id.name);
1389 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1392 static inline unsigned int
1393 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1394 int ch, int dir, int idx, unsigned int ofs)
1397 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1398 val &= HDA_AMP_VOLMASK;
1407 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1408 int ch, int dir, int idx, unsigned int ofs,
1411 unsigned int maxval;
1415 /* ofs = 0: raw max value */
1416 maxval = get_amp_max_value(codec, nid, dir, 0);
1419 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1420 HDA_AMP_VOLMASK, val);
1424 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1425 * @kcontrol: ctl element
1426 * @ucontrol: pointer to get/store the data
1428 * The control element is supposed to have the private_value field
1429 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1431 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435 hda_nid_t nid = get_amp_nid(kcontrol);
1436 int chs = get_amp_channels(kcontrol);
1437 int dir = get_amp_direction(kcontrol);
1438 int idx = get_amp_index(kcontrol);
1439 unsigned int ofs = get_amp_offset(kcontrol);
1440 long *valp = ucontrol->value.integer.value;
1443 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1445 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1448 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1451 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1452 * @kcontrol: ctl element
1453 * @ucontrol: pointer to get/store the data
1455 * The control element is supposed to have the private_value field
1456 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1458 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
1461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1462 hda_nid_t nid = get_amp_nid(kcontrol);
1463 int chs = get_amp_channels(kcontrol);
1464 int dir = get_amp_direction(kcontrol);
1465 int idx = get_amp_index(kcontrol);
1466 unsigned int ofs = get_amp_offset(kcontrol);
1467 long *valp = ucontrol->value.integer.value;
1471 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1475 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1478 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1481 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1482 * @kcontrol: ctl element
1483 * @op_flag: operation flag
1484 * @size: byte size of input TLV
1487 * The control element is supposed to have the private_value field
1488 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1490 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1491 unsigned int size, unsigned int __user *_tlv)
1493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494 hda_nid_t nid = get_amp_nid(kcontrol);
1495 int dir = get_amp_direction(kcontrol);
1496 unsigned int ofs = get_amp_offset(kcontrol);
1497 bool min_mute = get_amp_min_mute(kcontrol);
1498 u32 caps, val1, val2;
1500 if (size < 4 * sizeof(unsigned int))
1502 caps = query_amp_caps(codec, nid, dir);
1503 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1504 val2 = (val2 + 1) * 25;
1505 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1507 val1 = ((int)val1) * ((int)val2);
1508 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1509 val2 |= TLV_DB_SCALE_MUTE;
1510 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1512 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1514 if (put_user(val1, _tlv + 2))
1516 if (put_user(val2, _tlv + 3))
1520 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1523 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1524 * @codec: HD-audio codec
1525 * @nid: NID of a reference widget
1526 * @dir: #HDA_INPUT or #HDA_OUTPUT
1527 * @tlv: TLV data to be stored, at least 4 elements
1529 * Set (static) TLV data for a virtual master volume using the AMP caps
1530 * obtained from the reference NID.
1531 * The volume range is recalculated as if the max volume is 0dB.
1533 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1539 caps = query_amp_caps(codec, nid, dir);
1540 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1541 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1542 step = (step + 1) * 25;
1543 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1544 tlv[1] = 2 * sizeof(unsigned int);
1545 tlv[2] = -nums * step;
1548 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1550 /* find a mixer control element with the given name */
1551 static struct snd_kcontrol *
1552 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1554 struct snd_ctl_elem_id id;
1555 memset(&id, 0, sizeof(id));
1556 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1559 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1561 strcpy(id.name, name);
1562 return snd_ctl_find_id(codec->card, &id);
1566 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1567 * @codec: HD-audio codec
1568 * @name: ctl id name string
1570 * Get the control element with the given id string and IFACE_MIXER.
1572 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1575 return find_mixer_ctl(codec, name, 0, 0);
1577 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1579 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1583 /* 16 ctlrs should be large enough */
1584 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1585 if (!find_mixer_ctl(codec, name, 0, idx))
1592 * snd_hda_ctl_add - Add a control element and assign to the codec
1593 * @codec: HD-audio codec
1594 * @nid: corresponding NID (optional)
1595 * @kctl: the control element to assign
1597 * Add the given control element to an array inside the codec instance.
1598 * All control elements belonging to a codec are supposed to be added
1599 * by this function so that a proper clean-up works at the free or
1600 * reconfiguration time.
1602 * If non-zero @nid is passed, the NID is assigned to the control element.
1603 * The assignment is shown in the codec proc file.
1605 * snd_hda_ctl_add() checks the control subdev id field whether
1606 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1607 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1608 * specifies if kctl->private_value is a HDA amplifier value.
1610 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1611 struct snd_kcontrol *kctl)
1614 unsigned short flags = 0;
1615 struct hda_nid_item *item;
1617 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1618 flags |= HDA_NID_ITEM_AMP;
1620 nid = get_amp_nid_(kctl->private_value);
1622 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1623 nid = kctl->id.subdevice & 0xffff;
1624 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1625 kctl->id.subdevice = 0;
1626 err = snd_ctl_add(codec->card, kctl);
1629 item = snd_array_new(&codec->mixers);
1634 item->flags = flags;
1637 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1640 * snd_hda_add_nid - Assign a NID to a control element
1641 * @codec: HD-audio codec
1642 * @nid: corresponding NID (optional)
1643 * @kctl: the control element to assign
1644 * @index: index to kctl
1646 * Add the given control element to an array inside the codec instance.
1647 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1648 * NID:KCTL mapping - for example "Capture Source" selector.
1650 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1651 unsigned int index, hda_nid_t nid)
1653 struct hda_nid_item *item;
1656 item = snd_array_new(&codec->nids);
1660 item->index = index;
1664 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1665 kctl->id.name, kctl->id.index, index);
1668 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1671 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1672 * @codec: HD-audio codec
1674 void snd_hda_ctls_clear(struct hda_codec *codec)
1677 struct hda_nid_item *items = codec->mixers.list;
1678 for (i = 0; i < codec->mixers.used; i++)
1679 snd_ctl_remove(codec->card, items[i].kctl);
1680 snd_array_free(&codec->mixers);
1681 snd_array_free(&codec->nids);
1685 * snd_hda_lock_devices - pseudo device locking
1688 * toggle card->shutdown to allow/disallow the device access (as a hack)
1690 int snd_hda_lock_devices(struct hda_bus *bus)
1692 struct snd_card *card = bus->card;
1693 struct hda_codec *codec;
1695 spin_lock(&card->files_lock);
1699 if (!list_empty(&card->ctl_files))
1702 list_for_each_codec(codec, bus) {
1703 struct hda_pcm *cpcm;
1704 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1707 if (cpcm->pcm->streams[0].substream_opened ||
1708 cpcm->pcm->streams[1].substream_opened)
1712 spin_unlock(&card->files_lock);
1718 spin_unlock(&card->files_lock);
1721 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1724 * snd_hda_unlock_devices - pseudo device unlocking
1727 void snd_hda_unlock_devices(struct hda_bus *bus)
1729 struct snd_card *card = bus->card;
1731 spin_lock(&card->files_lock);
1733 spin_unlock(&card->files_lock);
1735 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1738 * snd_hda_codec_reset - Clear all objects assigned to the codec
1739 * @codec: HD-audio codec
1741 * This frees the all PCM and control elements assigned to the codec, and
1742 * clears the caches and restores the pin default configurations.
1744 * When a device is being used, it returns -EBSY. If successfully freed,
1747 int snd_hda_codec_reset(struct hda_codec *codec)
1749 struct hda_bus *bus = codec->bus;
1751 if (snd_hda_lock_devices(bus) < 0)
1754 /* OK, let it free */
1755 snd_hdac_device_unregister(&codec->core);
1757 /* allow device access again */
1758 snd_hda_unlock_devices(bus);
1762 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1764 /* apply the function to all matching slave ctls in the mixer list */
1765 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1766 const char *suffix, map_slave_func_t func, void *data)
1768 struct hda_nid_item *items;
1769 const char * const *s;
1772 items = codec->mixers.list;
1773 for (i = 0; i < codec->mixers.used; i++) {
1774 struct snd_kcontrol *sctl = items[i].kctl;
1775 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1777 for (s = slaves; *s; s++) {
1778 char tmpname[sizeof(sctl->id.name)];
1779 const char *name = *s;
1781 snprintf(tmpname, sizeof(tmpname), "%s %s",
1785 if (!strcmp(sctl->id.name, name)) {
1786 err = func(codec, data, sctl);
1796 static int check_slave_present(struct hda_codec *codec,
1797 void *data, struct snd_kcontrol *sctl)
1802 /* guess the value corresponding to 0dB */
1803 static int get_kctl_0dB_offset(struct hda_codec *codec,
1804 struct snd_kcontrol *kctl, int *step_to_check)
1807 const int *tlv = NULL;
1810 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1811 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1812 mm_segment_t fs = get_fs();
1814 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1817 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1819 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1821 step &= ~TLV_DB_SCALE_MUTE;
1824 if (*step_to_check && *step_to_check != step) {
1825 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1826 - *step_to_check, step);
1829 *step_to_check = step;
1830 val = -tlv[2] / step;
1835 /* call kctl->put with the given value(s) */
1836 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1838 struct snd_ctl_elem_value *ucontrol;
1839 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1842 ucontrol->value.integer.value[0] = val;
1843 ucontrol->value.integer.value[1] = val;
1844 kctl->put(kctl, ucontrol);
1849 /* initialize the slave volume with 0dB */
1850 static int init_slave_0dB(struct hda_codec *codec,
1851 void *data, struct snd_kcontrol *slave)
1853 int offset = get_kctl_0dB_offset(codec, slave, data);
1855 put_kctl_with_value(slave, offset);
1859 /* unmute the slave */
1860 static int init_slave_unmute(struct hda_codec *codec,
1861 void *data, struct snd_kcontrol *slave)
1863 return put_kctl_with_value(slave, 1);
1866 static int add_slave(struct hda_codec *codec,
1867 void *data, struct snd_kcontrol *slave)
1869 return snd_ctl_add_slave(data, slave);
1873 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1874 * @codec: HD-audio codec
1875 * @name: vmaster control name
1876 * @tlv: TLV data (optional)
1877 * @slaves: slave control names (optional)
1878 * @suffix: suffix string to each slave name (optional)
1879 * @init_slave_vol: initialize slaves to unmute/0dB
1880 * @ctl_ret: store the vmaster kcontrol in return
1882 * Create a virtual master control with the given name. The TLV data
1883 * must be either NULL or a valid data.
1885 * @slaves is a NULL-terminated array of strings, each of which is a
1886 * slave control name. All controls with these names are assigned to
1887 * the new virtual master control.
1889 * This function returns zero if successful or a negative error code.
1891 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1892 unsigned int *tlv, const char * const *slaves,
1893 const char *suffix, bool init_slave_vol,
1894 struct snd_kcontrol **ctl_ret)
1896 struct snd_kcontrol *kctl;
1902 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1904 codec_dbg(codec, "No slave found for %s\n", name);
1907 kctl = snd_ctl_make_virtual_master(name, tlv);
1910 err = snd_hda_ctl_add(codec, 0, kctl);
1914 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1918 /* init with master mute & zero volume */
1919 put_kctl_with_value(kctl, 0);
1920 if (init_slave_vol) {
1922 map_slaves(codec, slaves, suffix,
1923 tlv ? init_slave_0dB : init_slave_unmute, &step);
1930 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1933 * mute-LED control using vmaster
1935 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_info *uinfo)
1938 static const char * const texts[] = {
1939 "On", "Off", "Follow Master"
1942 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1945 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_value *ucontrol)
1948 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1949 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1953 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_value *ucontrol)
1956 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1957 unsigned int old_mode = hook->mute_mode;
1959 hook->mute_mode = ucontrol->value.enumerated.item[0];
1960 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1961 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1962 if (old_mode == hook->mute_mode)
1964 snd_hda_sync_vmaster_hook(hook);
1968 static const struct snd_kcontrol_new vmaster_mute_mode = {
1969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1970 .name = "Mute-LED Mode",
1971 .info = vmaster_mute_mode_info,
1972 .get = vmaster_mute_mode_get,
1973 .put = vmaster_mute_mode_put,
1976 /* meta hook to call each driver's vmaster hook */
1977 static void vmaster_hook(void *private_data, int enabled)
1979 struct hda_vmaster_mute_hook *hook = private_data;
1981 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1982 enabled = hook->mute_mode;
1983 hook->hook(hook->codec, enabled);
1987 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1988 * @codec: the HDA codec
1989 * @hook: the vmaster hook object
1990 * @expose_enum_ctl: flag to create an enum ctl
1992 * Add a mute-LED hook with the given vmaster switch kctl.
1993 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1994 * created and associated with the given hook.
1996 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1997 struct hda_vmaster_mute_hook *hook,
1998 bool expose_enum_ctl)
2000 struct snd_kcontrol *kctl;
2002 if (!hook->hook || !hook->sw_kctl)
2004 hook->codec = codec;
2005 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2006 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2007 if (!expose_enum_ctl)
2009 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2012 return snd_hda_ctl_add(codec, 0, kctl);
2014 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2017 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2018 * @hook: the vmaster hook
2020 * Call the hook with the current value for synchronization.
2021 * Should be called in init callback.
2023 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2025 if (!hook->hook || !hook->codec)
2027 /* don't call vmaster hook in the destructor since it might have
2028 * been already destroyed
2030 if (hook->codec->bus->shutdown)
2032 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2034 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2038 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2039 * @kcontrol: referred ctl element
2040 * @uinfo: pointer to get/store the data
2042 * The control element is supposed to have the private_value field
2043 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2045 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
2048 int chs = get_amp_channels(kcontrol);
2050 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2051 uinfo->count = chs == 3 ? 2 : 1;
2052 uinfo->value.integer.min = 0;
2053 uinfo->value.integer.max = 1;
2056 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2059 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2060 * @kcontrol: ctl element
2061 * @ucontrol: pointer to get/store the data
2063 * The control element is supposed to have the private_value field
2064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2066 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2070 hda_nid_t nid = get_amp_nid(kcontrol);
2071 int chs = get_amp_channels(kcontrol);
2072 int dir = get_amp_direction(kcontrol);
2073 int idx = get_amp_index(kcontrol);
2074 long *valp = ucontrol->value.integer.value;
2077 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2078 HDA_AMP_MUTE) ? 0 : 1;
2080 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2081 HDA_AMP_MUTE) ? 0 : 1;
2084 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2087 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2088 * @kcontrol: ctl element
2089 * @ucontrol: pointer to get/store the data
2091 * The control element is supposed to have the private_value field
2092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2094 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 hda_nid_t nid = get_amp_nid(kcontrol);
2099 int chs = get_amp_channels(kcontrol);
2100 int dir = get_amp_direction(kcontrol);
2101 int idx = get_amp_index(kcontrol);
2102 long *valp = ucontrol->value.integer.value;
2106 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2108 *valp ? 0 : HDA_AMP_MUTE);
2112 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2114 *valp ? 0 : HDA_AMP_MUTE);
2115 hda_call_check_power_status(codec, nid);
2118 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2121 * bound volume controls
2123 * bind multiple volumes (# indices, from 0)
2126 #define AMP_VAL_IDX_SHIFT 19
2127 #define AMP_VAL_IDX_MASK (0x0f<<19)
2130 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2131 * @kcontrol: ctl element
2132 * @ucontrol: pointer to get/store the data
2134 * The control element is supposed to have the private_value field
2135 * set up via HDA_BIND_MUTE*() macros.
2137 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2144 mutex_lock(&codec->control_mutex);
2145 pval = kcontrol->private_value;
2146 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2147 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2148 kcontrol->private_value = pval;
2149 mutex_unlock(&codec->control_mutex);
2152 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2155 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2156 * @kcontrol: ctl element
2157 * @ucontrol: pointer to get/store the data
2159 * The control element is supposed to have the private_value field
2160 * set up via HDA_BIND_MUTE*() macros.
2162 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2167 int i, indices, err = 0, change = 0;
2169 mutex_lock(&codec->control_mutex);
2170 pval = kcontrol->private_value;
2171 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2172 for (i = 0; i < indices; i++) {
2173 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2174 (i << AMP_VAL_IDX_SHIFT);
2175 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2180 kcontrol->private_value = pval;
2181 mutex_unlock(&codec->control_mutex);
2182 return err < 0 ? err : change;
2184 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2187 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2188 * @kcontrol: referred ctl element
2189 * @uinfo: pointer to get/store the data
2191 * The control element is supposed to have the private_value field
2192 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2194 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_info *uinfo)
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 struct hda_bind_ctls *c;
2201 mutex_lock(&codec->control_mutex);
2202 c = (struct hda_bind_ctls *)kcontrol->private_value;
2203 kcontrol->private_value = *c->values;
2204 err = c->ops->info(kcontrol, uinfo);
2205 kcontrol->private_value = (long)c;
2206 mutex_unlock(&codec->control_mutex);
2209 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2212 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2213 * @kcontrol: ctl element
2214 * @ucontrol: pointer to get/store the data
2216 * The control element is supposed to have the private_value field
2217 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2219 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 struct hda_bind_ctls *c;
2226 mutex_lock(&codec->control_mutex);
2227 c = (struct hda_bind_ctls *)kcontrol->private_value;
2228 kcontrol->private_value = *c->values;
2229 err = c->ops->get(kcontrol, ucontrol);
2230 kcontrol->private_value = (long)c;
2231 mutex_unlock(&codec->control_mutex);
2234 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2237 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2238 * @kcontrol: ctl element
2239 * @ucontrol: pointer to get/store the data
2241 * The control element is supposed to have the private_value field
2242 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2244 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248 struct hda_bind_ctls *c;
2249 unsigned long *vals;
2250 int err = 0, change = 0;
2252 mutex_lock(&codec->control_mutex);
2253 c = (struct hda_bind_ctls *)kcontrol->private_value;
2254 for (vals = c->values; *vals; vals++) {
2255 kcontrol->private_value = *vals;
2256 err = c->ops->put(kcontrol, ucontrol);
2261 kcontrol->private_value = (long)c;
2262 mutex_unlock(&codec->control_mutex);
2263 return err < 0 ? err : change;
2265 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2268 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2269 * @kcontrol: ctl element
2270 * @op_flag: operation flag
2271 * @size: byte size of input TLV
2274 * The control element is supposed to have the private_value field
2275 * set up via HDA_BIND_VOL() macro.
2277 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2278 unsigned int size, unsigned int __user *tlv)
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 struct hda_bind_ctls *c;
2284 mutex_lock(&codec->control_mutex);
2285 c = (struct hda_bind_ctls *)kcontrol->private_value;
2286 kcontrol->private_value = *c->values;
2287 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2288 kcontrol->private_value = (long)c;
2289 mutex_unlock(&codec->control_mutex);
2292 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2294 struct hda_ctl_ops snd_hda_bind_vol = {
2295 .info = snd_hda_mixer_amp_volume_info,
2296 .get = snd_hda_mixer_amp_volume_get,
2297 .put = snd_hda_mixer_amp_volume_put,
2298 .tlv = snd_hda_mixer_amp_tlv
2300 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2302 struct hda_ctl_ops snd_hda_bind_sw = {
2303 .info = snd_hda_mixer_amp_switch_info,
2304 .get = snd_hda_mixer_amp_switch_get,
2305 .put = snd_hda_mixer_amp_switch_put,
2306 .tlv = snd_hda_mixer_amp_tlv
2308 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2311 * SPDIF out controls
2314 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_info *uinfo)
2317 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2322 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2325 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2326 IEC958_AES0_NONAUDIO |
2327 IEC958_AES0_CON_EMPHASIS_5015 |
2328 IEC958_AES0_CON_NOT_COPYRIGHT;
2329 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2330 IEC958_AES1_CON_ORIGINAL;
2334 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2337 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2338 IEC958_AES0_NONAUDIO |
2339 IEC958_AES0_PRO_EMPHASIS_5015;
2343 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
2346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2347 int idx = kcontrol->private_value;
2348 struct hda_spdif_out *spdif;
2350 mutex_lock(&codec->spdif_mutex);
2351 spdif = snd_array_elem(&codec->spdif_out, idx);
2352 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2353 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2354 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2355 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2356 mutex_unlock(&codec->spdif_mutex);
2361 /* convert from SPDIF status bits to HDA SPDIF bits
2362 * bit 0 (DigEn) is always set zero (to be filled later)
2364 static unsigned short convert_from_spdif_status(unsigned int sbits)
2366 unsigned short val = 0;
2368 if (sbits & IEC958_AES0_PROFESSIONAL)
2369 val |= AC_DIG1_PROFESSIONAL;
2370 if (sbits & IEC958_AES0_NONAUDIO)
2371 val |= AC_DIG1_NONAUDIO;
2372 if (sbits & IEC958_AES0_PROFESSIONAL) {
2373 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2374 IEC958_AES0_PRO_EMPHASIS_5015)
2375 val |= AC_DIG1_EMPHASIS;
2377 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2378 IEC958_AES0_CON_EMPHASIS_5015)
2379 val |= AC_DIG1_EMPHASIS;
2380 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2381 val |= AC_DIG1_COPYRIGHT;
2382 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2383 val |= AC_DIG1_LEVEL;
2384 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2389 /* convert to SPDIF status bits from HDA SPDIF bits
2391 static unsigned int convert_to_spdif_status(unsigned short val)
2393 unsigned int sbits = 0;
2395 if (val & AC_DIG1_NONAUDIO)
2396 sbits |= IEC958_AES0_NONAUDIO;
2397 if (val & AC_DIG1_PROFESSIONAL)
2398 sbits |= IEC958_AES0_PROFESSIONAL;
2399 if (sbits & IEC958_AES0_PROFESSIONAL) {
2400 if (val & AC_DIG1_EMPHASIS)
2401 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2403 if (val & AC_DIG1_EMPHASIS)
2404 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2405 if (!(val & AC_DIG1_COPYRIGHT))
2406 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2407 if (val & AC_DIG1_LEVEL)
2408 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2409 sbits |= val & (0x7f << 8);
2414 /* set digital convert verbs both for the given NID and its slaves */
2415 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2420 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2422 d = codec->slave_dig_outs;
2426 snd_hdac_regmap_update(&codec->core, *d,
2427 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2430 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2433 unsigned int mask = 0;
2434 unsigned int val = 0;
2444 set_dig_out(codec, nid, mask, val);
2447 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
2450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2451 int idx = kcontrol->private_value;
2452 struct hda_spdif_out *spdif;
2457 mutex_lock(&codec->spdif_mutex);
2458 spdif = snd_array_elem(&codec->spdif_out, idx);
2460 spdif->status = ucontrol->value.iec958.status[0] |
2461 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2462 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2463 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2464 val = convert_from_spdif_status(spdif->status);
2465 val |= spdif->ctls & 1;
2466 change = spdif->ctls != val;
2468 if (change && nid != (u16)-1)
2469 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2470 mutex_unlock(&codec->spdif_mutex);
2474 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2476 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2480 int idx = kcontrol->private_value;
2481 struct hda_spdif_out *spdif;
2483 mutex_lock(&codec->spdif_mutex);
2484 spdif = snd_array_elem(&codec->spdif_out, idx);
2485 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2486 mutex_unlock(&codec->spdif_mutex);
2490 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2493 set_dig_out_convert(codec, nid, dig1, dig2);
2494 /* unmute amp switch (if any) */
2495 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2496 (dig1 & AC_DIG1_ENABLE))
2497 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2501 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 int idx = kcontrol->private_value;
2506 struct hda_spdif_out *spdif;
2511 mutex_lock(&codec->spdif_mutex);
2512 spdif = snd_array_elem(&codec->spdif_out, idx);
2514 val = spdif->ctls & ~AC_DIG1_ENABLE;
2515 if (ucontrol->value.integer.value[0])
2516 val |= AC_DIG1_ENABLE;
2517 change = spdif->ctls != val;
2519 if (change && nid != (u16)-1)
2520 set_spdif_ctls(codec, nid, val & 0xff, -1);
2521 mutex_unlock(&codec->spdif_mutex);
2525 static struct snd_kcontrol_new dig_mixes[] = {
2527 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2528 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2529 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2530 .info = snd_hda_spdif_mask_info,
2531 .get = snd_hda_spdif_cmask_get,
2534 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2536 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2537 .info = snd_hda_spdif_mask_info,
2538 .get = snd_hda_spdif_pmask_get,
2541 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2542 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2543 .info = snd_hda_spdif_mask_info,
2544 .get = snd_hda_spdif_default_get,
2545 .put = snd_hda_spdif_default_put,
2548 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2549 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2550 .info = snd_hda_spdif_out_switch_info,
2551 .get = snd_hda_spdif_out_switch_get,
2552 .put = snd_hda_spdif_out_switch_put,
2558 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2559 * @codec: the HDA codec
2560 * @associated_nid: NID that new ctls associated with
2561 * @cvt_nid: converter NID
2562 * @type: HDA_PCM_TYPE_*
2563 * Creates controls related with the digital output.
2564 * Called from each patch supporting the digital out.
2566 * Returns 0 if successful, or a negative error code.
2568 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2569 hda_nid_t associated_nid,
2574 struct snd_kcontrol *kctl;
2575 struct snd_kcontrol_new *dig_mix;
2578 const int spdif_index = 16;
2579 struct hda_spdif_out *spdif;
2580 struct hda_bus *bus = codec->bus;
2582 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2583 type == HDA_PCM_TYPE_SPDIF) {
2585 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2586 type == HDA_PCM_TYPE_HDMI) {
2587 /* suppose a single SPDIF device */
2588 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2589 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2592 kctl->id.index = spdif_index;
2594 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2596 if (!bus->primary_dig_out_type)
2597 bus->primary_dig_out_type = type;
2599 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2601 codec_err(codec, "too many IEC958 outputs\n");
2604 spdif = snd_array_new(&codec->spdif_out);
2607 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2608 kctl = snd_ctl_new1(dig_mix, codec);
2611 kctl->id.index = idx;
2612 kctl->private_value = codec->spdif_out.used - 1;
2613 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2617 spdif->nid = cvt_nid;
2618 snd_hdac_regmap_read(&codec->core, cvt_nid,
2619 AC_VERB_GET_DIGI_CONVERT_1, &val);
2621 spdif->status = convert_to_spdif_status(spdif->ctls);
2624 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2627 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2628 * @codec: the HDA codec
2631 * call within spdif_mutex lock
2633 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2637 for (i = 0; i < codec->spdif_out.used; i++) {
2638 struct hda_spdif_out *spdif =
2639 snd_array_elem(&codec->spdif_out, i);
2640 if (spdif->nid == nid)
2645 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2648 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2649 * @codec: the HDA codec
2650 * @idx: the SPDIF ctl index
2652 * Unassign the widget from the given SPDIF control.
2654 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2656 struct hda_spdif_out *spdif;
2658 mutex_lock(&codec->spdif_mutex);
2659 spdif = snd_array_elem(&codec->spdif_out, idx);
2660 spdif->nid = (u16)-1;
2661 mutex_unlock(&codec->spdif_mutex);
2663 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2666 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2667 * @codec: the HDA codec
2668 * @idx: the SPDIF ctl idx
2671 * Assign the widget to the SPDIF control with the given index.
2673 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2675 struct hda_spdif_out *spdif;
2678 mutex_lock(&codec->spdif_mutex);
2679 spdif = snd_array_elem(&codec->spdif_out, idx);
2680 if (spdif->nid != nid) {
2683 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2685 mutex_unlock(&codec->spdif_mutex);
2687 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2690 * SPDIF sharing with analog output
2692 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2693 struct snd_ctl_elem_value *ucontrol)
2695 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2696 ucontrol->value.integer.value[0] = mout->share_spdif;
2700 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2703 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2704 mout->share_spdif = !!ucontrol->value.integer.value[0];
2708 static const struct snd_kcontrol_new spdif_share_sw = {
2709 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2710 .name = "IEC958 Default PCM Playback Switch",
2711 .info = snd_ctl_boolean_mono_info,
2712 .get = spdif_share_sw_get,
2713 .put = spdif_share_sw_put,
2717 * snd_hda_create_spdif_share_sw - create Default PCM switch
2718 * @codec: the HDA codec
2719 * @mout: multi-out instance
2721 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2722 struct hda_multi_out *mout)
2724 struct snd_kcontrol *kctl;
2726 if (!mout->dig_out_nid)
2729 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2732 /* ATTENTION: here mout is passed as private_data, instead of codec */
2733 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2735 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2741 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2743 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2752 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2756 hda_nid_t nid = kcontrol->private_value;
2757 unsigned int val = !!ucontrol->value.integer.value[0];
2760 mutex_lock(&codec->spdif_mutex);
2761 change = codec->spdif_in_enable != val;
2763 codec->spdif_in_enable = val;
2764 snd_hdac_regmap_write(&codec->core, nid,
2765 AC_VERB_SET_DIGI_CONVERT_1, val);
2767 mutex_unlock(&codec->spdif_mutex);
2771 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_value *ucontrol)
2774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2775 hda_nid_t nid = kcontrol->private_value;
2779 snd_hdac_regmap_read(&codec->core, nid,
2780 AC_VERB_GET_DIGI_CONVERT_1, &val);
2781 sbits = convert_to_spdif_status(val);
2782 ucontrol->value.iec958.status[0] = sbits;
2783 ucontrol->value.iec958.status[1] = sbits >> 8;
2784 ucontrol->value.iec958.status[2] = sbits >> 16;
2785 ucontrol->value.iec958.status[3] = sbits >> 24;
2789 static struct snd_kcontrol_new dig_in_ctls[] = {
2791 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2792 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2793 .info = snd_hda_spdif_in_switch_info,
2794 .get = snd_hda_spdif_in_switch_get,
2795 .put = snd_hda_spdif_in_switch_put,
2798 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2800 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2801 .info = snd_hda_spdif_mask_info,
2802 .get = snd_hda_spdif_in_status_get,
2808 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2809 * @codec: the HDA codec
2810 * @nid: audio in widget NID
2812 * Creates controls related with the SPDIF input.
2813 * Called from each patch supporting the SPDIF in.
2815 * Returns 0 if successful, or a negative error code.
2817 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2820 struct snd_kcontrol *kctl;
2821 struct snd_kcontrol_new *dig_mix;
2824 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2826 codec_err(codec, "too many IEC958 inputs\n");
2829 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2830 kctl = snd_ctl_new1(dig_mix, codec);
2833 kctl->private_value = nid;
2834 err = snd_hda_ctl_add(codec, nid, kctl);
2838 codec->spdif_in_enable =
2839 snd_hda_codec_read(codec, nid, 0,
2840 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2844 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2847 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2848 * @codec: the HDA codec
2849 * @fg: function group (not used now)
2850 * @power_state: the power state to set (AC_PWRST_*)
2852 * Set the given power state to all widgets that have the power control.
2853 * If the codec has power_filter set, it evaluates the power state and
2854 * filter out if it's unchanged as D3.
2856 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2857 unsigned int power_state)
2861 for_each_hda_codec_node(nid, codec) {
2862 unsigned int wcaps = get_wcaps(codec, nid);
2863 unsigned int state = power_state;
2864 if (!(wcaps & AC_WCAP_POWER))
2866 if (codec->power_filter) {
2867 state = codec->power_filter(codec, nid, power_state);
2868 if (state != power_state && power_state == AC_PWRST_D3)
2871 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2875 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2878 * wait until the state is reached, returns the current state
2880 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2882 unsigned int power_state)
2884 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2885 unsigned int state, actual_state;
2888 state = snd_hda_codec_read(codec, fg, 0,
2889 AC_VERB_GET_POWER_STATE, 0);
2890 if (state & AC_PWRST_ERROR)
2892 actual_state = (state >> 4) & 0x0f;
2893 if (actual_state == power_state)
2895 if (time_after_eq(jiffies, end_time))
2897 /* wait until the codec reachs to the target state */
2904 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2905 * @codec: the HDA codec
2907 * @power_state: power state to evalue
2909 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2910 * This can be used a codec power_filter callback.
2912 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2914 unsigned int power_state)
2916 if (nid == codec->core.afg || nid == codec->core.mfg)
2918 if (power_state == AC_PWRST_D3 &&
2919 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2920 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2921 int eapd = snd_hda_codec_read(codec, nid, 0,
2922 AC_VERB_GET_EAPD_BTLENABLE, 0);
2928 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2931 * set power state of the codec, and return the power state
2933 static unsigned int hda_set_power_state(struct hda_codec *codec,
2934 unsigned int power_state)
2936 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2941 /* this delay seems necessary to avoid click noise at power-down */
2942 if (power_state == AC_PWRST_D3) {
2943 if (codec->depop_delay < 0)
2944 msleep(codec_has_epss(codec) ? 10 : 100);
2945 else if (codec->depop_delay > 0)
2946 msleep(codec->depop_delay);
2947 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2950 /* repeat power states setting at most 10 times*/
2951 for (count = 0; count < 10; count++) {
2952 if (codec->patch_ops.set_power_state)
2953 codec->patch_ops.set_power_state(codec, fg,
2956 state = power_state;
2957 if (codec->power_filter)
2958 state = codec->power_filter(codec, fg, state);
2959 if (state == power_state || power_state != AC_PWRST_D3)
2960 snd_hda_codec_read(codec, fg, flags,
2961 AC_VERB_SET_POWER_STATE,
2963 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2965 state = hda_sync_power_state(codec, fg, power_state);
2966 if (!(state & AC_PWRST_ERROR))
2973 /* sync power states of all widgets;
2974 * this is called at the end of codec parsing
2976 static void sync_power_up_states(struct hda_codec *codec)
2980 /* don't care if no filter is used */
2981 if (!codec->power_filter)
2984 for_each_hda_codec_node(nid, codec) {
2985 unsigned int wcaps = get_wcaps(codec, nid);
2986 unsigned int target;
2987 if (!(wcaps & AC_WCAP_POWER))
2989 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2990 if (target == AC_PWRST_D0)
2992 if (!snd_hda_check_power_state(codec, nid, target))
2993 snd_hda_codec_write(codec, nid, 0,
2994 AC_VERB_SET_POWER_STATE, target);
2998 #ifdef CONFIG_SND_HDA_RECONFIG
2999 /* execute additional init verbs */
3000 static void hda_exec_init_verbs(struct hda_codec *codec)
3002 if (codec->init_verbs.list)
3003 snd_hda_sequence_write(codec, codec->init_verbs.list);
3006 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3010 /* update the power on/off account with the current jiffies */
3011 static void update_power_acct(struct hda_codec *codec, bool on)
3013 unsigned long delta = jiffies - codec->power_jiffies;
3016 codec->power_on_acct += delta;
3018 codec->power_off_acct += delta;
3019 codec->power_jiffies += delta;
3022 void snd_hda_update_power_acct(struct hda_codec *codec)
3024 update_power_acct(codec, hda_codec_is_power_on(codec));
3028 * call suspend and power-down; used both from PM and power-save
3029 * this function returns the power state in the end
3031 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3035 atomic_inc(&codec->core.in_pm);
3037 if (codec->patch_ops.suspend)
3038 codec->patch_ops.suspend(codec);
3039 hda_cleanup_all_streams(codec);
3040 state = hda_set_power_state(codec, AC_PWRST_D3);
3041 update_power_acct(codec, true);
3042 atomic_dec(&codec->core.in_pm);
3047 * kick up codec; used both from PM and power-save
3049 static void hda_call_codec_resume(struct hda_codec *codec)
3051 atomic_inc(&codec->core.in_pm);
3053 if (codec->core.regmap)
3054 regcache_mark_dirty(codec->core.regmap);
3056 codec->power_jiffies = jiffies;
3058 hda_set_power_state(codec, AC_PWRST_D0);
3059 restore_shutup_pins(codec);
3060 hda_exec_init_verbs(codec);
3061 snd_hda_jack_set_dirty_all(codec);
3062 if (codec->patch_ops.resume)
3063 codec->patch_ops.resume(codec);
3065 if (codec->patch_ops.init)
3066 codec->patch_ops.init(codec);
3067 if (codec->core.regmap)
3068 regcache_sync(codec->core.regmap);
3071 if (codec->jackpoll_interval)
3072 hda_jackpoll_work(&codec->jackpoll_work.work);
3074 snd_hda_jack_report_sync(codec);
3075 atomic_dec(&codec->core.in_pm);
3078 static int hda_codec_runtime_suspend(struct device *dev)
3080 struct hda_codec *codec = dev_to_hda_codec(dev);
3081 struct hda_pcm *pcm;
3084 cancel_delayed_work_sync(&codec->jackpoll_work);
3085 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3086 snd_pcm_suspend_all(pcm->pcm);
3087 state = hda_call_codec_suspend(codec);
3088 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3089 (state & AC_PWRST_CLK_STOP_OK))
3090 snd_hdac_codec_link_down(&codec->core);
3091 snd_hdac_link_power(&codec->core, false);
3095 static int hda_codec_runtime_resume(struct device *dev)
3097 struct hda_codec *codec = dev_to_hda_codec(dev);
3099 snd_hdac_link_power(&codec->core, true);
3100 snd_hdac_codec_link_up(&codec->core);
3101 hda_call_codec_resume(codec);
3102 pm_runtime_mark_last_busy(dev);
3105 #endif /* CONFIG_PM */
3107 /* referred in hda_bind.c */
3108 const struct dev_pm_ops hda_codec_driver_pm = {
3109 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3110 pm_runtime_force_resume)
3111 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3116 * add standard channel maps if not specified
3118 static int add_std_chmaps(struct hda_codec *codec)
3120 struct hda_pcm *pcm;
3123 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3124 for (str = 0; str < 2; str++) {
3125 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3126 struct snd_pcm_chmap *chmap;
3127 const struct snd_pcm_chmap_elem *elem;
3129 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3131 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3132 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3133 hinfo->channels_max,
3137 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3143 /* default channel maps for 2.1 speakers;
3144 * since HD-audio supports only stereo, odd number channels are omitted
3146 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3148 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3150 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3151 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3154 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3156 int snd_hda_codec_build_controls(struct hda_codec *codec)
3159 hda_exec_init_verbs(codec);
3160 /* continue to initialize... */
3161 if (codec->patch_ops.init)
3162 err = codec->patch_ops.init(codec);
3163 if (!err && codec->patch_ops.build_controls)
3164 err = codec->patch_ops.build_controls(codec);
3168 /* we create chmaps here instead of build_pcms */
3169 err = add_std_chmaps(codec);
3173 if (codec->jackpoll_interval)
3174 hda_jackpoll_work(&codec->jackpoll_work.work);
3176 snd_hda_jack_report_sync(codec); /* call at the last init point */
3177 sync_power_up_states(codec);
3184 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3185 struct hda_codec *codec,
3186 struct snd_pcm_substream *substream)
3191 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3192 struct hda_codec *codec,
3193 unsigned int stream_tag,
3194 unsigned int format,
3195 struct snd_pcm_substream *substream)
3197 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3201 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3202 struct hda_codec *codec,
3203 struct snd_pcm_substream *substream)
3205 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3209 static int set_pcm_default_values(struct hda_codec *codec,
3210 struct hda_pcm_stream *info)
3214 /* query support PCM information from the given NID */
3215 if (info->nid && (!info->rates || !info->formats)) {
3216 err = snd_hda_query_supported_pcm(codec, info->nid,
3217 info->rates ? NULL : &info->rates,
3218 info->formats ? NULL : &info->formats,
3219 info->maxbps ? NULL : &info->maxbps);
3223 if (info->ops.open == NULL)
3224 info->ops.open = hda_pcm_default_open_close;
3225 if (info->ops.close == NULL)
3226 info->ops.close = hda_pcm_default_open_close;
3227 if (info->ops.prepare == NULL) {
3228 if (snd_BUG_ON(!info->nid))
3230 info->ops.prepare = hda_pcm_default_prepare;
3232 if (info->ops.cleanup == NULL) {
3233 if (snd_BUG_ON(!info->nid))
3235 info->ops.cleanup = hda_pcm_default_cleanup;
3241 * codec prepare/cleanup entries
3244 * snd_hda_codec_prepare - Prepare a stream
3245 * @codec: the HDA codec
3246 * @hinfo: PCM information
3247 * @stream: stream tag to assign
3248 * @format: format id to assign
3249 * @substream: PCM substream to assign
3251 * Calls the prepare callback set by the codec with the given arguments.
3252 * Clean up the inactive streams when successful.
3254 int snd_hda_codec_prepare(struct hda_codec *codec,
3255 struct hda_pcm_stream *hinfo,
3256 unsigned int stream,
3257 unsigned int format,
3258 struct snd_pcm_substream *substream)
3261 mutex_lock(&codec->bus->prepare_mutex);
3262 if (hinfo->ops.prepare)
3263 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3268 purify_inactive_streams(codec);
3269 mutex_unlock(&codec->bus->prepare_mutex);
3272 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3275 * snd_hda_codec_cleanup - Prepare a stream
3276 * @codec: the HDA codec
3277 * @hinfo: PCM information
3278 * @substream: PCM substream
3280 * Calls the cleanup callback set by the codec with the given arguments.
3282 void snd_hda_codec_cleanup(struct hda_codec *codec,
3283 struct hda_pcm_stream *hinfo,
3284 struct snd_pcm_substream *substream)
3286 mutex_lock(&codec->bus->prepare_mutex);
3287 if (hinfo->ops.cleanup)
3288 hinfo->ops.cleanup(hinfo, codec, substream);
3289 mutex_unlock(&codec->bus->prepare_mutex);
3291 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3294 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3295 "Audio", "SPDIF", "HDMI", "Modem"
3299 * get the empty PCM device number to assign
3301 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3303 /* audio device indices; not linear to keep compatibility */
3304 /* assigned to static slots up to dev#10; if more needed, assign
3305 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3307 static int audio_idx[HDA_PCM_NTYPES][5] = {
3308 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3309 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3310 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3311 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3315 if (type >= HDA_PCM_NTYPES) {
3316 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3320 for (i = 0; audio_idx[type][i] >= 0; i++) {
3321 #ifndef CONFIG_SND_DYNAMIC_MINORS
3322 if (audio_idx[type][i] >= 8)
3325 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3326 return audio_idx[type][i];
3329 #ifdef CONFIG_SND_DYNAMIC_MINORS
3330 /* non-fixed slots starting from 10 */
3331 for (i = 10; i < 32; i++) {
3332 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3337 dev_warn(bus->card->dev, "Too many %s devices\n",
3338 snd_hda_pcm_type_name[type]);
3339 #ifndef CONFIG_SND_DYNAMIC_MINORS
3340 dev_warn(bus->card->dev,
3341 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3346 /* call build_pcms ops of the given codec and set up the default parameters */
3347 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3349 struct hda_pcm *cpcm;
3352 if (!list_empty(&codec->pcm_list_head))
3353 return 0; /* already parsed */
3355 if (!codec->patch_ops.build_pcms)
3358 err = codec->patch_ops.build_pcms(codec);
3360 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3361 codec->core.addr, err);
3365 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3368 for (stream = 0; stream < 2; stream++) {
3369 struct hda_pcm_stream *info = &cpcm->stream[stream];
3371 if (!info->substreams)
3373 err = set_pcm_default_values(codec, info);
3376 "fail to setup default for PCM %s\n",
3386 /* assign all PCMs of the given codec */
3387 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3389 struct hda_bus *bus = codec->bus;
3390 struct hda_pcm *cpcm;
3393 err = snd_hda_codec_parse_pcms(codec);
3397 /* attach a new PCM streams */
3398 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3400 continue; /* already attached */
3401 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3402 continue; /* no substreams assigned */
3404 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3406 continue; /* no fatal error */
3408 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3411 "cannot attach PCM stream %d for codec #%d\n",
3412 dev, codec->core.addr);
3413 continue; /* no fatal error */
3421 * snd_hda_add_new_ctls - create controls from the array
3422 * @codec: the HDA codec
3423 * @knew: the array of struct snd_kcontrol_new
3425 * This helper function creates and add new controls in the given array.
3426 * The array must be terminated with an empty entry as terminator.
3428 * Returns 0 if successful, or a negative error code.
3430 int snd_hda_add_new_ctls(struct hda_codec *codec,
3431 const struct snd_kcontrol_new *knew)
3435 for (; knew->name; knew++) {
3436 struct snd_kcontrol *kctl;
3437 int addr = 0, idx = 0;
3438 if (knew->iface == -1) /* skip this codec private value */
3441 kctl = snd_ctl_new1(knew, codec);
3445 kctl->id.device = addr;
3447 kctl->id.index = idx;
3448 err = snd_hda_ctl_add(codec, 0, kctl);
3451 /* try first with another device index corresponding to
3452 * the codec addr; if it still fails (or it's the
3453 * primary codec), then try another control index
3455 if (!addr && codec->core.addr)
3456 addr = codec->core.addr;
3457 else if (!idx && !knew->index) {
3458 idx = find_empty_mixer_ctl_idx(codec,
3468 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3471 static void codec_set_power_save(struct hda_codec *codec, int delay)
3473 struct device *dev = hda_codec_dev(codec);
3475 if (delay == 0 && codec->auto_runtime_pm)
3479 pm_runtime_set_autosuspend_delay(dev, delay);
3480 pm_runtime_use_autosuspend(dev);
3481 pm_runtime_allow(dev);
3482 if (!pm_runtime_suspended(dev))
3483 pm_runtime_mark_last_busy(dev);
3485 pm_runtime_dont_use_autosuspend(dev);
3486 pm_runtime_forbid(dev);
3491 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3492 * @bus: HD-audio bus
3493 * @delay: autosuspend delay in msec, 0 = off
3495 * Synchronize the runtime PM autosuspend state from the power_save option.
3497 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3499 struct hda_codec *c;
3501 list_for_each_codec(c, bus)
3502 codec_set_power_save(c, delay);
3504 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3507 * snd_hda_check_amp_list_power - Check the amp list and update the power
3508 * @codec: HD-audio codec
3509 * @check: the object containing an AMP list and the status
3510 * @nid: NID to check / update
3512 * Check whether the given NID is in the amp list. If it's in the list,
3513 * check the current AMP status, and update the the power-status according
3514 * to the mute status.
3516 * This function is supposed to be set or called from the check_power_status
3519 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3520 struct hda_loopback_check *check,
3523 const struct hda_amp_list *p;
3526 if (!check->amplist)
3528 for (p = check->amplist; p->nid; p++) {
3533 return 0; /* nothing changed */
3535 for (p = check->amplist; p->nid; p++) {
3536 for (ch = 0; ch < 2; ch++) {
3537 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3539 if (!(v & HDA_AMP_MUTE) && v > 0) {
3540 if (!check->power_on) {
3541 check->power_on = 1;
3542 snd_hda_power_up_pm(codec);
3548 if (check->power_on) {
3549 check->power_on = 0;
3550 snd_hda_power_down_pm(codec);
3554 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3562 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3563 * @imux: imux helper object
3564 * @uinfo: pointer to get/store the data
3566 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3567 struct snd_ctl_elem_info *uinfo)
3571 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3573 uinfo->value.enumerated.items = imux->num_items;
3574 if (!imux->num_items)
3576 index = uinfo->value.enumerated.item;
3577 if (index >= imux->num_items)
3578 index = imux->num_items - 1;
3579 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3582 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3585 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3586 * @codec: the HDA codec
3587 * @imux: imux helper object
3588 * @ucontrol: pointer to get/store the data
3589 * @nid: input mux NID
3590 * @cur_val: pointer to get/store the current imux value
3592 int snd_hda_input_mux_put(struct hda_codec *codec,
3593 const struct hda_input_mux *imux,
3594 struct snd_ctl_elem_value *ucontrol,
3596 unsigned int *cur_val)
3600 if (!imux->num_items)
3602 idx = ucontrol->value.enumerated.item[0];
3603 if (idx >= imux->num_items)
3604 idx = imux->num_items - 1;
3605 if (*cur_val == idx)
3607 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3608 imux->items[idx].index);
3612 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3616 * snd_hda_enum_helper_info - Helper for simple enum ctls
3617 * @kcontrol: ctl element
3618 * @uinfo: pointer to get/store the data
3619 * @num_items: number of enum items
3620 * @texts: enum item string array
3622 * process kcontrol info callback of a simple string enum array
3623 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3625 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3626 struct snd_ctl_elem_info *uinfo,
3627 int num_items, const char * const *texts)
3629 static const char * const texts_default[] = {
3630 "Disabled", "Enabled"
3633 if (!texts || !num_items) {
3635 texts = texts_default;
3638 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3640 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3643 * Multi-channel / digital-out PCM helper functions
3646 /* setup SPDIF output stream */
3647 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3648 unsigned int stream_tag, unsigned int format)
3650 struct hda_spdif_out *spdif;
3651 unsigned int curr_fmt;
3654 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3655 /* Add sanity check to pass klockwork check.
3656 * This should never happen.
3658 if (WARN_ON(spdif == NULL))
3661 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3662 AC_VERB_GET_STREAM_FORMAT, 0);
3663 reset = codec->spdif_status_reset &&
3664 (spdif->ctls & AC_DIG1_ENABLE) &&
3667 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3670 set_dig_out_convert(codec, nid,
3671 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3673 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3674 if (codec->slave_dig_outs) {
3676 for (d = codec->slave_dig_outs; *d; d++)
3677 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3680 /* turn on again (if needed) */
3682 set_dig_out_convert(codec, nid,
3683 spdif->ctls & 0xff, -1);
3686 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3688 snd_hda_codec_cleanup_stream(codec, nid);
3689 if (codec->slave_dig_outs) {
3691 for (d = codec->slave_dig_outs; *d; d++)
3692 snd_hda_codec_cleanup_stream(codec, *d);
3697 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3698 * @codec: the HDA codec
3699 * @mout: hda_multi_out object
3701 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3702 struct hda_multi_out *mout)
3704 mutex_lock(&codec->spdif_mutex);
3705 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3706 /* already opened as analog dup; reset it once */
3707 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3708 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3709 mutex_unlock(&codec->spdif_mutex);
3712 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3715 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3716 * @codec: the HDA codec
3717 * @mout: hda_multi_out object
3718 * @stream_tag: stream tag to assign
3719 * @format: format id to assign
3720 * @substream: PCM substream to assign
3722 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3723 struct hda_multi_out *mout,
3724 unsigned int stream_tag,
3725 unsigned int format,
3726 struct snd_pcm_substream *substream)
3728 mutex_lock(&codec->spdif_mutex);
3729 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3730 mutex_unlock(&codec->spdif_mutex);
3733 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3736 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3737 * @codec: the HDA codec
3738 * @mout: hda_multi_out object
3740 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3741 struct hda_multi_out *mout)
3743 mutex_lock(&codec->spdif_mutex);
3744 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3745 mutex_unlock(&codec->spdif_mutex);
3748 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3751 * snd_hda_multi_out_dig_close - release the digital out stream
3752 * @codec: the HDA codec
3753 * @mout: hda_multi_out object
3755 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3756 struct hda_multi_out *mout)
3758 mutex_lock(&codec->spdif_mutex);
3759 mout->dig_out_used = 0;
3760 mutex_unlock(&codec->spdif_mutex);
3763 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3766 * snd_hda_multi_out_analog_open - open analog outputs
3767 * @codec: the HDA codec
3768 * @mout: hda_multi_out object
3769 * @substream: PCM substream to assign
3770 * @hinfo: PCM information to assign
3772 * Open analog outputs and set up the hw-constraints.
3773 * If the digital outputs can be opened as slave, open the digital
3776 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3777 struct hda_multi_out *mout,
3778 struct snd_pcm_substream *substream,
3779 struct hda_pcm_stream *hinfo)
3781 struct snd_pcm_runtime *runtime = substream->runtime;
3782 runtime->hw.channels_max = mout->max_channels;
3783 if (mout->dig_out_nid) {
3784 if (!mout->analog_rates) {
3785 mout->analog_rates = hinfo->rates;
3786 mout->analog_formats = hinfo->formats;
3787 mout->analog_maxbps = hinfo->maxbps;
3789 runtime->hw.rates = mout->analog_rates;
3790 runtime->hw.formats = mout->analog_formats;
3791 hinfo->maxbps = mout->analog_maxbps;
3793 if (!mout->spdif_rates) {
3794 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3796 &mout->spdif_formats,
3797 &mout->spdif_maxbps);
3799 mutex_lock(&codec->spdif_mutex);
3800 if (mout->share_spdif) {
3801 if ((runtime->hw.rates & mout->spdif_rates) &&
3802 (runtime->hw.formats & mout->spdif_formats)) {
3803 runtime->hw.rates &= mout->spdif_rates;
3804 runtime->hw.formats &= mout->spdif_formats;
3805 if (mout->spdif_maxbps < hinfo->maxbps)
3806 hinfo->maxbps = mout->spdif_maxbps;
3808 mout->share_spdif = 0;
3809 /* FIXME: need notify? */
3812 mutex_unlock(&codec->spdif_mutex);
3814 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3815 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3817 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3820 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3821 * @codec: the HDA codec
3822 * @mout: hda_multi_out object
3823 * @stream_tag: stream tag to assign
3824 * @format: format id to assign
3825 * @substream: PCM substream to assign
3827 * Set up the i/o for analog out.
3828 * When the digital out is available, copy the front out to digital out, too.
3830 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3831 struct hda_multi_out *mout,
3832 unsigned int stream_tag,
3833 unsigned int format,
3834 struct snd_pcm_substream *substream)
3836 const hda_nid_t *nids = mout->dac_nids;
3837 int chs = substream->runtime->channels;
3838 struct hda_spdif_out *spdif;
3841 mutex_lock(&codec->spdif_mutex);
3842 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3843 if (mout->dig_out_nid && mout->share_spdif &&
3844 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3845 if (chs == 2 && spdif != NULL &&
3846 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3848 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3849 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3850 setup_dig_out_stream(codec, mout->dig_out_nid,
3851 stream_tag, format);
3853 mout->dig_out_used = 0;
3854 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3857 mutex_unlock(&codec->spdif_mutex);
3860 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3862 if (!mout->no_share_stream &&
3863 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3864 /* headphone out will just decode front left/right (stereo) */
3865 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3867 /* extra outputs copied from front */
3868 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3869 if (!mout->no_share_stream && mout->hp_out_nid[i])
3870 snd_hda_codec_setup_stream(codec,
3871 mout->hp_out_nid[i],
3872 stream_tag, 0, format);
3875 for (i = 1; i < mout->num_dacs; i++) {
3876 if (chs >= (i + 1) * 2) /* independent out */
3877 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3879 else if (!mout->no_share_stream) /* copy front */
3880 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3884 /* extra surrounds */
3885 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3887 if (!mout->extra_out_nid[i])
3889 if (chs >= (i + 1) * 2)
3891 else if (!mout->no_share_stream)
3893 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3894 stream_tag, ch, format);
3899 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3902 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3903 * @codec: the HDA codec
3904 * @mout: hda_multi_out object
3906 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3907 struct hda_multi_out *mout)
3909 const hda_nid_t *nids = mout->dac_nids;
3912 for (i = 0; i < mout->num_dacs; i++)
3913 snd_hda_codec_cleanup_stream(codec, nids[i]);
3915 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3916 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3917 if (mout->hp_out_nid[i])
3918 snd_hda_codec_cleanup_stream(codec,
3919 mout->hp_out_nid[i]);
3920 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3921 if (mout->extra_out_nid[i])
3922 snd_hda_codec_cleanup_stream(codec,
3923 mout->extra_out_nid[i]);
3924 mutex_lock(&codec->spdif_mutex);
3925 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3926 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3927 mout->dig_out_used = 0;
3929 mutex_unlock(&codec->spdif_mutex);
3932 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3935 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3936 * @codec: the HDA codec
3937 * @pin: referred pin NID
3939 * Guess the suitable VREF pin bits to be set as the pin-control value.
3940 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3942 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3944 unsigned int pincap;
3945 unsigned int oldval;
3946 oldval = snd_hda_codec_read(codec, pin, 0,
3947 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3948 pincap = snd_hda_query_pin_caps(codec, pin);
3949 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3950 /* Exception: if the default pin setup is vref50, we give it priority */
3951 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3952 return AC_PINCTL_VREF_80;
3953 else if (pincap & AC_PINCAP_VREF_50)
3954 return AC_PINCTL_VREF_50;
3955 else if (pincap & AC_PINCAP_VREF_100)
3956 return AC_PINCTL_VREF_100;
3957 else if (pincap & AC_PINCAP_VREF_GRD)
3958 return AC_PINCTL_VREF_GRD;
3959 return AC_PINCTL_VREF_HIZ;
3961 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3964 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3965 * @codec: the HDA codec
3966 * @pin: referred pin NID
3967 * @val: pin ctl value to audit
3969 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3970 hda_nid_t pin, unsigned int val)
3972 static unsigned int cap_lists[][2] = {
3973 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3974 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3975 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3976 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3982 cap = snd_hda_query_pin_caps(codec, pin);
3984 return val; /* don't know what to do... */
3986 if (val & AC_PINCTL_OUT_EN) {
3987 if (!(cap & AC_PINCAP_OUT))
3988 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3989 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3990 val &= ~AC_PINCTL_HP_EN;
3993 if (val & AC_PINCTL_IN_EN) {
3994 if (!(cap & AC_PINCAP_IN))
3995 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3997 unsigned int vcap, vref;
3999 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4000 vref = val & AC_PINCTL_VREFEN;
4001 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4002 if (vref == cap_lists[i][0] &&
4003 !(vcap & cap_lists[i][1])) {
4004 if (i == ARRAY_SIZE(cap_lists) - 1)
4005 vref = AC_PINCTL_VREF_HIZ;
4007 vref = cap_lists[i + 1][0];
4010 val &= ~AC_PINCTL_VREFEN;
4017 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4020 * _snd_hda_pin_ctl - Helper to set pin ctl value
4021 * @codec: the HDA codec
4022 * @pin: referred pin NID
4023 * @val: pin control value to set
4024 * @cached: access over codec pinctl cache or direct write
4026 * This function is a helper to set a pin ctl value more safely.
4027 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4028 * value in pin target array via snd_hda_codec_set_pin_target(), then
4029 * actually writes the value via either snd_hda_codec_update_cache() or
4030 * snd_hda_codec_write() depending on @cached flag.
4032 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4033 unsigned int val, bool cached)
4035 val = snd_hda_correct_pin_ctl(codec, pin, val);
4036 snd_hda_codec_set_pin_target(codec, pin, val);
4038 return snd_hda_codec_update_cache(codec, pin, 0,
4039 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4041 return snd_hda_codec_write(codec, pin, 0,
4042 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4044 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4047 * snd_hda_add_imux_item - Add an item to input_mux
4048 * @codec: the HDA codec
4049 * @imux: imux helper object
4050 * @label: the name of imux item to assign
4051 * @index: index number of imux item to assign
4052 * @type_idx: pointer to store the resultant label index
4054 * When the same label is used already in the existing items, the number
4055 * suffix is appended to the label. This label index number is stored
4056 * to type_idx when non-NULL pointer is given.
4058 int snd_hda_add_imux_item(struct hda_codec *codec,
4059 struct hda_input_mux *imux, const char *label,
4060 int index, int *type_idx)
4062 int i, label_idx = 0;
4063 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4064 codec_err(codec, "hda_codec: Too many imux items!\n");
4067 for (i = 0; i < imux->num_items; i++) {
4068 if (!strncmp(label, imux->items[i].label, strlen(label)))
4072 *type_idx = label_idx;
4074 snprintf(imux->items[imux->num_items].label,
4075 sizeof(imux->items[imux->num_items].label),
4076 "%s %d", label, label_idx);
4078 strlcpy(imux->items[imux->num_items].label, label,
4079 sizeof(imux->items[imux->num_items].label));
4080 imux->items[imux->num_items].index = index;
4084 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4087 * snd_hda_bus_reset_codecs - Reset the bus
4088 * @bus: HD-audio bus
4090 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4092 struct hda_codec *codec;
4094 list_for_each_codec(codec, bus) {
4095 /* FIXME: maybe a better way needed for forced reset */
4096 cancel_delayed_work_sync(&codec->jackpoll_work);
4098 if (hda_codec_is_power_on(codec)) {
4099 hda_call_codec_suspend(codec);
4100 hda_call_codec_resume(codec);
4107 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4108 * @pcm: PCM caps bits
4109 * @buf: the string buffer to write
4110 * @buflen: the max buffer length
4112 * used by hda_proc.c and hda_eld.c
4114 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4116 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4119 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4120 if (pcm & (AC_SUPPCM_BITS_8 << i))
4121 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4123 buf[j] = '\0'; /* necessary when j == 0 */
4125 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4127 MODULE_DESCRIPTION("HDA codec core");
4128 MODULE_LICENSE("GPL");