2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/module.h>
28 #include <linux/pm_runtime.h>
29 #include <sound/core.h>
30 #include <sound/hda_codec.h>
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
39 #include <sound/hda_component.h>
41 #define codec_in_pm(codec) snd_hdac_is_in_pm(&codec->core)
42 #define hda_codec_is_power_on(codec) snd_hdac_is_power_on(&codec->core)
43 #define codec_has_epss(codec) \
44 ((codec)->core.power_caps & AC_PWRST_EPSS)
45 #define codec_has_clkstop(codec) \
46 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
49 * Send and receive a verb - passed to exec_verb override for hdac_device
51 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
52 unsigned int flags, unsigned int *res)
54 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
55 struct hda_bus *bus = codec->bus;
62 snd_hda_power_up_pm(codec);
63 mutex_lock(&bus->core.cmd_mutex);
64 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
65 bus->no_response_fallback = 1;
66 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
68 bus->no_response_fallback = 0;
69 mutex_unlock(&bus->core.cmd_mutex);
70 snd_hda_power_down_pm(codec);
71 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
72 if (bus->response_reset) {
74 "resetting BUS due to fatal communication error\n");
75 snd_hda_bus_reset(bus);
79 /* clear reset-flag when the communication gets recovered */
80 if (!err || codec_in_pm(codec))
81 bus->response_reset = 0;
86 * snd_hda_sequence_write - sequence writes
87 * @codec: the HDA codec
88 * @seq: VERB array to send
90 * Send the commands sequentially from the given array.
91 * The array must be terminated with NID=0.
93 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
95 for (; seq->nid; seq++)
96 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
98 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
100 /* connection list element */
101 struct hda_conn_list {
102 struct list_head list;
108 /* look up the cached results */
109 static struct hda_conn_list *
110 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
112 struct hda_conn_list *p;
113 list_for_each_entry(p, &codec->conn_list, list) {
120 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
121 const hda_nid_t *list)
123 struct hda_conn_list *p;
125 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
130 memcpy(p->conns, list, len * sizeof(hda_nid_t));
131 list_add(&p->list, &codec->conn_list);
135 static void remove_conn_list(struct hda_codec *codec)
137 while (!list_empty(&codec->conn_list)) {
138 struct hda_conn_list *p;
139 p = list_first_entry(&codec->conn_list, typeof(*p), list);
145 /* read the connection and add to the cache */
146 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
149 hda_nid_t *result = list;
152 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
153 if (len == -ENOSPC) {
154 len = snd_hda_get_num_raw_conns(codec, nid);
155 result = kmalloc_array(len, sizeof(hda_nid_t), GFP_KERNEL);
158 len = snd_hda_get_raw_connections(codec, nid, result, len);
161 len = snd_hda_override_conn_list(codec, nid, len, result);
168 * snd_hda_get_conn_list - get connection list
169 * @codec: the HDA codec
171 * @listp: the pointer to store NID list
173 * Parses the connection list of the given widget and stores the pointer
174 * to the list of NIDs.
176 * Returns the number of connections, or a negative error code.
178 * Note that the returned pointer isn't protected against the list
179 * modification. If snd_hda_override_conn_list() might be called
180 * concurrently, protect with a mutex appropriately.
182 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
183 const hda_nid_t **listp)
189 const struct hda_conn_list *p;
191 /* if the connection-list is already cached, read it */
192 p = lookup_conn_list(codec, nid);
198 if (snd_BUG_ON(added))
201 err = read_and_add_raw_conns(codec, nid);
207 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
210 * snd_hda_get_connections - copy connection list
211 * @codec: the HDA codec
213 * @conn_list: connection list array; when NULL, checks only the size
214 * @max_conns: max. number of connections to store
216 * Parses the connection list of the given widget and stores the list
219 * Returns the number of connections, or a negative error code.
221 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
222 hda_nid_t *conn_list, int max_conns)
224 const hda_nid_t *list;
225 int len = snd_hda_get_conn_list(codec, nid, &list);
227 if (len > 0 && conn_list) {
228 if (len > max_conns) {
229 codec_err(codec, "Too many connections %d for NID 0x%x\n",
233 memcpy(conn_list, list, len * sizeof(hda_nid_t));
238 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
241 * snd_hda_override_conn_list - add/modify the connection-list to cache
242 * @codec: the HDA codec
244 * @len: number of connection list entries
245 * @list: the list of connection entries
247 * Add or modify the given connection-list to the cache. If the corresponding
248 * cache already exists, invalidate it and append a new one.
250 * Returns zero or a negative error code.
252 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
253 const hda_nid_t *list)
255 struct hda_conn_list *p;
257 p = lookup_conn_list(codec, nid);
263 return add_conn_list(codec, nid, len, list);
265 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
268 * snd_hda_get_conn_index - get the connection index of the given NID
269 * @codec: the HDA codec
270 * @mux: NID containing the list
271 * @nid: NID to select
272 * @recursive: 1 when searching NID recursively, otherwise 0
274 * Parses the connection list of the widget @mux and checks whether the
275 * widget @nid is present. If it is, return the connection index.
276 * Otherwise it returns -1.
278 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
279 hda_nid_t nid, int recursive)
281 const hda_nid_t *conn;
284 nums = snd_hda_get_conn_list(codec, mux, &conn);
285 for (i = 0; i < nums; i++)
290 if (recursive > 10) {
291 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
295 for (i = 0; i < nums; i++) {
296 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
297 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
299 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
304 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
307 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
308 * @codec: the HDA codec
309 * @nid: NID of the pin to parse
311 * Get the device entry number on the given widget. This is a feature of
312 * DP MST audio. Each pin can have several device entries in it.
314 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
316 unsigned int wcaps = get_wcaps(codec, nid);
319 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
320 get_wcaps_type(wcaps) != AC_WID_PIN)
323 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
326 return parm & AC_DEV_LIST_LEN_MASK;
328 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
331 * snd_hda_get_devices - copy device list without cache
332 * @codec: the HDA codec
333 * @nid: NID of the pin to parse
334 * @dev_list: device list array
335 * @max_devices: max. number of devices to store
337 * Copy the device list. This info is dynamic and so not cached.
338 * Currently called only from hda_proc.c, so not exported.
340 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
341 u8 *dev_list, int max_devices)
344 int i, dev_len, devices;
346 parm = snd_hda_get_num_devices(codec, nid);
347 if (!parm) /* not multi-stream capable */
351 dev_len = dev_len < max_devices ? dev_len : max_devices;
354 while (devices < dev_len) {
355 if (snd_hdac_read(&codec->core, nid,
356 AC_VERB_GET_DEVICE_LIST, devices, &parm))
359 for (i = 0; i < 8; i++) {
360 dev_list[devices] = (u8)parm;
363 if (devices >= dev_len)
371 * snd_hda_get_dev_select - get device entry select on the pin
372 * @codec: the HDA codec
373 * @nid: NID of the pin to get device entry select
375 * Get the devcie entry select on the pin. Return the device entry
376 * id selected on the pin. Return 0 means the first device entry
377 * is selected or MST is not supported.
379 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
381 /* not support dp_mst will always return 0, using first dev_entry */
385 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
387 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
390 * snd_hda_set_dev_select - set device entry select on the pin
391 * @codec: the HDA codec
392 * @nid: NID of the pin to set device entry select
393 * @dev_id: device entry id to be set
395 * Set the device entry select on the pin nid.
397 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
399 int ret, num_devices;
401 /* not support dp_mst will always return 0, using first dev_entry */
405 /* AC_PAR_DEVLIST_LEN is 0 based. */
406 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
407 /* If Device List Length is 0 (num_device = 1),
408 * the pin is not multi stream capable.
409 * Do nothing in this case.
411 if (num_devices == 1)
414 /* Behavior of setting index being equal to or greater than
415 * Device List Length is not predictable
417 if (num_devices <= dev_id)
420 ret = snd_hda_codec_write(codec, nid, 0,
421 AC_VERB_SET_DEVICE_SEL, dev_id);
425 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
428 * read widget caps for each widget and store in cache
430 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
435 codec->wcaps = kmalloc_array(codec->core.num_nodes, 4, GFP_KERNEL);
438 nid = codec->core.start_nid;
439 for (i = 0; i < codec->core.num_nodes; i++, nid++)
440 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
441 nid, AC_PAR_AUDIO_WIDGET_CAP);
445 /* read all pin default configurations and save codec->init_pins */
446 static int read_pin_defaults(struct hda_codec *codec)
450 for_each_hda_codec_node(nid, codec) {
451 struct hda_pincfg *pin;
452 unsigned int wcaps = get_wcaps(codec, nid);
453 unsigned int wid_type = get_wcaps_type(wcaps);
454 if (wid_type != AC_WID_PIN)
456 pin = snd_array_new(&codec->init_pins);
460 pin->cfg = snd_hda_codec_read(codec, nid, 0,
461 AC_VERB_GET_CONFIG_DEFAULT, 0);
463 * all device entries are the same widget control so far
464 * fixme: if any codec is different, need fix here
466 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
467 AC_VERB_GET_PIN_WIDGET_CONTROL,
473 /* look up the given pin config list and return the item matching with NID */
474 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
475 struct snd_array *array,
478 struct hda_pincfg *pin;
481 snd_array_for_each(array, i, pin) {
488 /* set the current pin config value for the given NID.
489 * the value is cached, and read via snd_hda_codec_get_pincfg()
491 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
492 hda_nid_t nid, unsigned int cfg)
494 struct hda_pincfg *pin;
496 /* the check below may be invalid when pins are added by a fixup
497 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
501 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
505 pin = look_up_pincfg(codec, list, nid);
507 pin = snd_array_new(list);
517 * snd_hda_codec_set_pincfg - Override a pin default configuration
518 * @codec: the HDA codec
519 * @nid: NID to set the pin config
520 * @cfg: the pin default config value
522 * Override a pin default configuration value in the cache.
523 * This value can be read by snd_hda_codec_get_pincfg() in a higher
524 * priority than the real hardware value.
526 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
527 hda_nid_t nid, unsigned int cfg)
529 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
531 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
534 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
535 * @codec: the HDA codec
536 * @nid: NID to get the pin config
538 * Get the current pin config value of the given pin NID.
539 * If the pincfg value is cached or overridden via sysfs or driver,
540 * returns the cached value.
542 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
544 struct hda_pincfg *pin;
546 #ifdef CONFIG_SND_HDA_RECONFIG
548 unsigned int cfg = 0;
549 mutex_lock(&codec->user_mutex);
550 pin = look_up_pincfg(codec, &codec->user_pins, nid);
553 mutex_unlock(&codec->user_mutex);
558 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
561 pin = look_up_pincfg(codec, &codec->init_pins, nid);
566 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
569 * snd_hda_codec_set_pin_target - remember the current pinctl target value
570 * @codec: the HDA codec
572 * @val: assigned pinctl value
574 * This function stores the given value to a pinctl target value in the
575 * pincfg table. This isn't always as same as the actually written value
576 * but can be referred at any time via snd_hda_codec_get_pin_target().
578 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
581 struct hda_pincfg *pin;
583 pin = look_up_pincfg(codec, &codec->init_pins, nid);
589 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
592 * snd_hda_codec_get_pin_target - return the current pinctl target value
593 * @codec: the HDA codec
596 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
598 struct hda_pincfg *pin;
600 pin = look_up_pincfg(codec, &codec->init_pins, nid);
605 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
608 * snd_hda_shutup_pins - Shut up all pins
609 * @codec: the HDA codec
611 * Clear all pin controls to shup up before suspend for avoiding click noise.
612 * The controls aren't cached so that they can be resumed properly.
614 void snd_hda_shutup_pins(struct hda_codec *codec)
616 const struct hda_pincfg *pin;
619 /* don't shut up pins when unloading the driver; otherwise it breaks
620 * the default pin setup at the next load of the driver
622 if (codec->bus->shutdown)
624 snd_array_for_each(&codec->init_pins, i, pin) {
625 /* use read here for syncing after issuing each verb */
626 snd_hda_codec_read(codec, pin->nid, 0,
627 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
629 codec->pins_shutup = 1;
631 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
634 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
635 static void restore_shutup_pins(struct hda_codec *codec)
637 const struct hda_pincfg *pin;
640 if (!codec->pins_shutup)
642 if (codec->bus->shutdown)
644 snd_array_for_each(&codec->init_pins, i, pin) {
645 snd_hda_codec_write(codec, pin->nid, 0,
646 AC_VERB_SET_PIN_WIDGET_CONTROL,
649 codec->pins_shutup = 0;
653 static void hda_jackpoll_work(struct work_struct *work)
655 struct hda_codec *codec =
656 container_of(work, struct hda_codec, jackpoll_work.work);
658 snd_hda_jack_set_dirty_all(codec);
659 snd_hda_jack_poll_all(codec);
661 if (!codec->jackpoll_interval)
664 schedule_delayed_work(&codec->jackpoll_work,
665 codec->jackpoll_interval);
668 /* release all pincfg lists */
669 static void free_init_pincfgs(struct hda_codec *codec)
671 snd_array_free(&codec->driver_pins);
672 #ifdef CONFIG_SND_HDA_RECONFIG
673 snd_array_free(&codec->user_pins);
675 snd_array_free(&codec->init_pins);
679 * audio-converter setup caches
681 struct hda_cvt_setup {
686 unsigned char active; /* cvt is currently used */
687 unsigned char dirty; /* setups should be cleared */
690 /* get or create a cache entry for the given audio converter NID */
691 static struct hda_cvt_setup *
692 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
694 struct hda_cvt_setup *p;
697 snd_array_for_each(&codec->cvt_setups, i, p) {
701 p = snd_array_new(&codec->cvt_setups);
710 static void release_pcm(struct kref *kref)
712 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
715 snd_device_free(pcm->codec->card, pcm->pcm);
716 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
721 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
723 kref_put(&pcm->kref, release_pcm);
725 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
727 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
728 const char *fmt, ...)
733 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
738 kref_init(&pcm->kref);
740 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
747 list_add_tail(&pcm->list, &codec->pcm_list_head);
750 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
755 static void codec_release_pcms(struct hda_codec *codec)
757 struct hda_pcm *pcm, *n;
759 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
760 list_del_init(&pcm->list);
762 snd_device_disconnect(codec->card, pcm->pcm);
763 snd_hda_codec_pcm_put(pcm);
767 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
769 if (codec->registered) {
770 /* pm_runtime_put() is called in snd_hdac_device_exit() */
771 pm_runtime_get_noresume(hda_codec_dev(codec));
772 pm_runtime_disable(hda_codec_dev(codec));
773 codec->registered = 0;
776 cancel_delayed_work_sync(&codec->jackpoll_work);
777 if (!codec->in_freeing)
778 snd_hda_ctls_clear(codec);
779 codec_release_pcms(codec);
780 snd_hda_detach_beep_device(codec);
781 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
782 snd_hda_jack_tbl_clear(codec);
783 codec->proc_widget_hook = NULL;
786 /* free only driver_pins so that init_pins + user_pins are restored */
787 snd_array_free(&codec->driver_pins);
788 snd_array_free(&codec->cvt_setups);
789 snd_array_free(&codec->spdif_out);
790 snd_array_free(&codec->verbs);
791 codec->preset = NULL;
792 codec->slave_dig_outs = NULL;
793 codec->spdif_status_reset = 0;
794 snd_array_free(&codec->mixers);
795 snd_array_free(&codec->nids);
796 remove_conn_list(codec);
797 snd_hdac_regmap_exit(&codec->core);
800 static unsigned int hda_set_power_state(struct hda_codec *codec,
801 unsigned int power_state);
803 /* enable/disable display power per codec */
804 static void codec_display_power(struct hda_codec *codec, bool enable)
806 if (codec->display_power_control)
807 snd_hdac_display_power(&codec->bus->core, codec->addr, enable);
810 /* also called from hda_bind.c */
811 void snd_hda_codec_register(struct hda_codec *codec)
813 if (codec->registered)
815 if (device_is_registered(hda_codec_dev(codec))) {
816 codec_display_power(codec, 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_free(struct snd_device *device)
832 struct hda_codec *codec = device->device_data;
834 codec->in_freeing = 1;
835 snd_hdac_device_unregister(&codec->core);
836 codec_display_power(codec, false);
839 * In the case of ASoC HD-audio bus, the device refcount is released in
840 * snd_hdac_ext_bus_device_remove() explicitly.
842 if (codec->core.type == HDA_DEV_LEGACY)
843 put_device(hda_codec_dev(codec));
848 static void snd_hda_codec_dev_release(struct device *dev)
850 struct hda_codec *codec = dev_to_hda_codec(dev);
852 free_init_pincfgs(codec);
853 snd_hdac_device_exit(&codec->core);
854 snd_hda_sysfs_clear(codec);
855 kfree(codec->modelname);
860 #define DEV_NAME_LEN 31
862 static int snd_hda_codec_device_init(struct hda_bus *bus, struct snd_card *card,
863 unsigned int codec_addr, struct hda_codec **codecp)
865 char name[DEV_NAME_LEN];
866 struct hda_codec *codec;
869 dev_dbg(card->dev, "%s: entry\n", __func__);
871 if (snd_BUG_ON(!bus))
873 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
876 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
880 sprintf(name, "hdaudioC%dD%d", card->number, codec_addr);
881 err = snd_hdac_device_init(&codec->core, &bus->core, name, codec_addr);
887 codec->core.type = HDA_DEV_LEGACY;
894 * snd_hda_codec_new - create a HDA codec
895 * @bus: the bus to assign
896 * @codec_addr: the codec address
897 * @codecp: the pointer to store the generated codec
899 * Returns 0 if successful, or a negative error code.
901 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
902 unsigned int codec_addr, struct hda_codec **codecp)
906 ret = snd_hda_codec_device_init(bus, card, codec_addr, codecp);
910 return snd_hda_codec_device_new(bus, card, codec_addr, *codecp);
912 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
914 int snd_hda_codec_device_new(struct hda_bus *bus, struct snd_card *card,
915 unsigned int codec_addr, struct hda_codec *codec)
920 static struct snd_device_ops dev_ops = {
921 .dev_register = snd_hda_codec_dev_register,
922 .dev_free = snd_hda_codec_dev_free,
925 dev_dbg(card->dev, "%s: entry\n", __func__);
927 if (snd_BUG_ON(!bus))
929 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
932 codec->core.dev.release = snd_hda_codec_dev_release;
933 codec->core.exec_verb = codec_exec_verb;
937 codec->addr = codec_addr;
938 mutex_init(&codec->spdif_mutex);
939 mutex_init(&codec->control_mutex);
940 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
941 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
942 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
943 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
944 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
945 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
946 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
947 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
948 INIT_LIST_HEAD(&codec->conn_list);
949 INIT_LIST_HEAD(&codec->pcm_list_head);
951 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
952 codec->depop_delay = -1;
953 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
956 codec->power_jiffies = jiffies;
959 snd_hda_sysfs_init(codec);
961 if (codec->bus->modelname) {
962 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
963 if (!codec->modelname) {
969 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
970 err = read_widget_caps(codec, fg);
973 err = read_pin_defaults(codec);
977 /* power-up all before initialization */
978 hda_set_power_state(codec, AC_PWRST_D0);
980 snd_hda_codec_proc_new(codec);
982 snd_hda_create_hwdep(codec);
984 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
985 codec->core.subsystem_id, codec->core.revision_id);
986 snd_component_add(card, component);
988 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
995 put_device(hda_codec_dev(codec));
998 EXPORT_SYMBOL_GPL(snd_hda_codec_device_new);
1001 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1002 * @codec: the HDA codec
1004 * Forcibly refresh the all widget caps and the init pin configurations of
1007 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1012 err = snd_hdac_refresh_widgets(&codec->core, true);
1016 /* Assume the function group node does not change,
1017 * only the widget nodes may change.
1019 kfree(codec->wcaps);
1020 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1021 err = read_widget_caps(codec, fg);
1025 snd_array_free(&codec->init_pins);
1026 err = read_pin_defaults(codec);
1030 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1032 /* update the stream-id if changed */
1033 static void update_pcm_stream_id(struct hda_codec *codec,
1034 struct hda_cvt_setup *p, hda_nid_t nid,
1035 u32 stream_tag, int channel_id)
1037 unsigned int oldval, newval;
1039 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1040 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1041 newval = (stream_tag << 4) | channel_id;
1042 if (oldval != newval)
1043 snd_hda_codec_write(codec, nid, 0,
1044 AC_VERB_SET_CHANNEL_STREAMID,
1046 p->stream_tag = stream_tag;
1047 p->channel_id = channel_id;
1051 /* update the format-id if changed */
1052 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1053 hda_nid_t nid, int format)
1055 unsigned int oldval;
1057 if (p->format_id != format) {
1058 oldval = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_STREAM_FORMAT, 0);
1060 if (oldval != format) {
1062 snd_hda_codec_write(codec, nid, 0,
1063 AC_VERB_SET_STREAM_FORMAT,
1066 p->format_id = format;
1071 * snd_hda_codec_setup_stream - set up the codec for streaming
1072 * @codec: the CODEC to set up
1073 * @nid: the NID to set up
1074 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1075 * @channel_id: channel id to pass, zero based.
1076 * @format: stream format.
1078 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1080 int channel_id, int format)
1082 struct hda_codec *c;
1083 struct hda_cvt_setup *p;
1091 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1092 nid, stream_tag, channel_id, format);
1093 p = get_hda_cvt_setup(codec, nid);
1097 if (codec->patch_ops.stream_pm)
1098 codec->patch_ops.stream_pm(codec, nid, true);
1099 if (codec->pcm_format_first)
1100 update_pcm_format(codec, p, nid, format);
1101 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1102 if (!codec->pcm_format_first)
1103 update_pcm_format(codec, p, nid, format);
1108 /* make other inactive cvts with the same stream-tag dirty */
1109 type = get_wcaps_type(get_wcaps(codec, nid));
1110 list_for_each_codec(c, codec->bus) {
1111 snd_array_for_each(&c->cvt_setups, i, p) {
1112 if (!p->active && p->stream_tag == stream_tag &&
1113 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1118 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1120 static void really_cleanup_stream(struct hda_codec *codec,
1121 struct hda_cvt_setup *q);
1124 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1125 * @codec: the CODEC to clean up
1126 * @nid: the NID to clean up
1127 * @do_now: really clean up the stream instead of clearing the active flag
1129 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1132 struct hda_cvt_setup *p;
1137 if (codec->no_sticky_stream)
1140 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1141 p = get_hda_cvt_setup(codec, nid);
1143 /* here we just clear the active flag when do_now isn't set;
1144 * actual clean-ups will be done later in
1145 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1148 really_cleanup_stream(codec, p);
1153 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1155 static void really_cleanup_stream(struct hda_codec *codec,
1156 struct hda_cvt_setup *q)
1158 hda_nid_t nid = q->nid;
1159 if (q->stream_tag || q->channel_id)
1160 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1162 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1164 memset(q, 0, sizeof(*q));
1166 if (codec->patch_ops.stream_pm)
1167 codec->patch_ops.stream_pm(codec, nid, false);
1170 /* clean up the all conflicting obsolete streams */
1171 static void purify_inactive_streams(struct hda_codec *codec)
1173 struct hda_codec *c;
1174 struct hda_cvt_setup *p;
1177 list_for_each_codec(c, codec->bus) {
1178 snd_array_for_each(&c->cvt_setups, i, p) {
1180 really_cleanup_stream(c, p);
1186 /* clean up all streams; called from suspend */
1187 static void hda_cleanup_all_streams(struct hda_codec *codec)
1189 struct hda_cvt_setup *p;
1192 snd_array_for_each(&codec->cvt_setups, i, p) {
1194 really_cleanup_stream(codec, p);
1200 * amp access functions
1204 * query_amp_caps - query AMP capabilities
1205 * @codec: the HD-auio codec
1206 * @nid: the NID to query
1207 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1209 * Query AMP capabilities for the given widget and direction.
1210 * Returns the obtained capability bits.
1212 * When cap bits have been already read, this doesn't read again but
1213 * returns the cached value.
1215 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1217 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1218 nid = codec->core.afg;
1219 return snd_hda_param_read(codec, nid,
1220 direction == HDA_OUTPUT ?
1221 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1223 EXPORT_SYMBOL_GPL(query_amp_caps);
1226 * snd_hda_check_amp_caps - query AMP capabilities
1227 * @codec: the HD-audio codec
1228 * @nid: the NID to query
1229 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1230 * @bits: bit mask to check the result
1232 * Check whether the widget has the given amp capability for the direction.
1234 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1235 int dir, unsigned int bits)
1239 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1240 if (query_amp_caps(codec, nid, dir) & bits)
1244 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1247 * snd_hda_override_amp_caps - Override the AMP capabilities
1248 * @codec: the CODEC to clean up
1249 * @nid: the NID to clean up
1250 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1251 * @caps: the capability bits to set
1253 * Override the cached AMP caps bits value by the given one.
1254 * This function is useful if the driver needs to adjust the AMP ranges,
1255 * e.g. limit to 0dB, etc.
1257 * Returns zero if successful or a negative error code.
1259 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1264 snd_hda_override_wcaps(codec, nid,
1265 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1266 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1267 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1269 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1272 * snd_hda_codec_amp_update - update the AMP mono value
1273 * @codec: HD-audio codec
1274 * @nid: NID to read the AMP value
1275 * @ch: channel to update (0 or 1)
1276 * @dir: #HDA_INPUT or #HDA_OUTPUT
1277 * @idx: the index value (only for input direction)
1278 * @mask: bit mask to set
1279 * @val: the bits value to set
1281 * Update the AMP values for the given channel, direction and index.
1283 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1284 int ch, int dir, int idx, int mask, int val)
1286 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1288 /* enable fake mute if no h/w mute but min=mute */
1289 if ((query_amp_caps(codec, nid, dir) &
1290 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1291 cmd |= AC_AMP_FAKE_MUTE;
1292 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1294 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1297 * snd_hda_codec_amp_stereo - update the AMP stereo values
1298 * @codec: HD-audio codec
1299 * @nid: NID to read the AMP value
1300 * @direction: #HDA_INPUT or #HDA_OUTPUT
1301 * @idx: the index value (only for input direction)
1302 * @mask: bit mask to set
1303 * @val: the bits value to set
1305 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1306 * stereo widget with the same mask and value.
1308 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1309 int direction, int idx, int mask, int val)
1313 if (snd_BUG_ON(mask & ~0xff))
1315 for (ch = 0; ch < 2; ch++)
1316 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1320 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1323 * snd_hda_codec_amp_init - initialize the AMP value
1324 * @codec: the HDA codec
1325 * @nid: NID to read the AMP value
1326 * @ch: channel (left=0 or right=1)
1327 * @dir: #HDA_INPUT or #HDA_OUTPUT
1328 * @idx: the index value (only for input direction)
1329 * @mask: bit mask to set
1330 * @val: the bits value to set
1332 * Works like snd_hda_codec_amp_update() but it writes the value only at
1333 * the first access. If the amp was already initialized / updated beforehand,
1334 * this does nothing.
1336 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1337 int dir, int idx, int mask, int val)
1341 if (!codec->core.regmap)
1343 regcache_cache_only(codec->core.regmap, true);
1344 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1345 regcache_cache_only(codec->core.regmap, false);
1348 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1350 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1353 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1354 * @codec: the HDA codec
1355 * @nid: NID to read the AMP value
1356 * @dir: #HDA_INPUT or #HDA_OUTPUT
1357 * @idx: the index value (only for input direction)
1358 * @mask: bit mask to set
1359 * @val: the bits value to set
1361 * Call snd_hda_codec_amp_init() for both stereo channels.
1363 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1364 int dir, int idx, int mask, int val)
1368 if (snd_BUG_ON(mask & ~0xff))
1370 for (ch = 0; ch < 2; ch++)
1371 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1375 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1377 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1380 u32 caps = query_amp_caps(codec, nid, dir);
1382 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1389 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1390 * @kcontrol: referred ctl element
1391 * @uinfo: pointer to get/store the data
1393 * The control element is supposed to have the private_value field
1394 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1396 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_info *uinfo)
1399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1400 u16 nid = get_amp_nid(kcontrol);
1401 u8 chs = get_amp_channels(kcontrol);
1402 int dir = get_amp_direction(kcontrol);
1403 unsigned int ofs = get_amp_offset(kcontrol);
1405 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1406 uinfo->count = chs == 3 ? 2 : 1;
1407 uinfo->value.integer.min = 0;
1408 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1409 if (!uinfo->value.integer.max) {
1411 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1412 nid, kcontrol->id.name);
1417 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1420 static inline unsigned int
1421 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1422 int ch, int dir, int idx, unsigned int ofs)
1425 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1426 val &= HDA_AMP_VOLMASK;
1435 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1436 int ch, int dir, int idx, unsigned int ofs,
1439 unsigned int maxval;
1443 /* ofs = 0: raw max value */
1444 maxval = get_amp_max_value(codec, nid, dir, 0);
1447 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1448 HDA_AMP_VOLMASK, val);
1452 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1453 * @kcontrol: ctl element
1454 * @ucontrol: pointer to get/store the data
1456 * The control element is supposed to have the private_value field
1457 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1459 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_value *ucontrol)
1462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1463 hda_nid_t nid = get_amp_nid(kcontrol);
1464 int chs = get_amp_channels(kcontrol);
1465 int dir = get_amp_direction(kcontrol);
1466 int idx = get_amp_index(kcontrol);
1467 unsigned int ofs = get_amp_offset(kcontrol);
1468 long *valp = ucontrol->value.integer.value;
1471 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1473 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1476 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1479 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1480 * @kcontrol: ctl element
1481 * @ucontrol: pointer to get/store the data
1483 * The control element is supposed to have the private_value field
1484 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1486 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1487 struct snd_ctl_elem_value *ucontrol)
1489 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1490 hda_nid_t nid = get_amp_nid(kcontrol);
1491 int chs = get_amp_channels(kcontrol);
1492 int dir = get_amp_direction(kcontrol);
1493 int idx = get_amp_index(kcontrol);
1494 unsigned int ofs = get_amp_offset(kcontrol);
1495 long *valp = ucontrol->value.integer.value;
1499 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1503 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1506 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1508 /* inquiry the amp caps and convert to TLV */
1509 static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
1511 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1512 hda_nid_t nid = get_amp_nid(kcontrol);
1513 int dir = get_amp_direction(kcontrol);
1514 unsigned int ofs = get_amp_offset(kcontrol);
1515 bool min_mute = get_amp_min_mute(kcontrol);
1516 u32 caps, val1, val2;
1518 caps = query_amp_caps(codec, nid, dir);
1519 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1520 val2 = (val2 + 1) * 25;
1521 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1523 val1 = ((int)val1) * ((int)val2);
1524 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1525 val2 |= TLV_DB_SCALE_MUTE;
1526 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
1527 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
1528 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = val1;
1529 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = val2;
1533 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
1534 * @kcontrol: ctl element
1535 * @op_flag: operation flag
1536 * @size: byte size of input TLV
1539 * The control element is supposed to have the private_value field
1540 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1542 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1543 unsigned int size, unsigned int __user *_tlv)
1545 unsigned int tlv[4];
1547 if (size < 4 * sizeof(unsigned int))
1549 get_ctl_amp_tlv(kcontrol, tlv);
1550 if (copy_to_user(_tlv, tlv, sizeof(tlv)))
1554 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1557 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1558 * @codec: HD-audio codec
1559 * @nid: NID of a reference widget
1560 * @dir: #HDA_INPUT or #HDA_OUTPUT
1561 * @tlv: TLV data to be stored, at least 4 elements
1563 * Set (static) TLV data for a virtual master volume using the AMP caps
1564 * obtained from the reference NID.
1565 * The volume range is recalculated as if the max volume is 0dB.
1567 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1573 caps = query_amp_caps(codec, nid, dir);
1574 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1575 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1576 step = (step + 1) * 25;
1577 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
1578 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
1579 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = -nums * step;
1580 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = step;
1582 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1584 /* find a mixer control element with the given name */
1585 static struct snd_kcontrol *
1586 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1588 struct snd_ctl_elem_id id;
1589 memset(&id, 0, sizeof(id));
1590 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1593 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1595 strcpy(id.name, name);
1596 return snd_ctl_find_id(codec->card, &id);
1600 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1601 * @codec: HD-audio codec
1602 * @name: ctl id name string
1604 * Get the control element with the given id string and IFACE_MIXER.
1606 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1609 return find_mixer_ctl(codec, name, 0, 0);
1611 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1613 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1617 /* 16 ctlrs should be large enough */
1618 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1619 if (!find_mixer_ctl(codec, name, 0, idx))
1626 * snd_hda_ctl_add - Add a control element and assign to the codec
1627 * @codec: HD-audio codec
1628 * @nid: corresponding NID (optional)
1629 * @kctl: the control element to assign
1631 * Add the given control element to an array inside the codec instance.
1632 * All control elements belonging to a codec are supposed to be added
1633 * by this function so that a proper clean-up works at the free or
1634 * reconfiguration time.
1636 * If non-zero @nid is passed, the NID is assigned to the control element.
1637 * The assignment is shown in the codec proc file.
1639 * snd_hda_ctl_add() checks the control subdev id field whether
1640 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1641 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1642 * specifies if kctl->private_value is a HDA amplifier value.
1644 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1645 struct snd_kcontrol *kctl)
1648 unsigned short flags = 0;
1649 struct hda_nid_item *item;
1651 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1652 flags |= HDA_NID_ITEM_AMP;
1654 nid = get_amp_nid_(kctl->private_value);
1656 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1657 nid = kctl->id.subdevice & 0xffff;
1658 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1659 kctl->id.subdevice = 0;
1660 err = snd_ctl_add(codec->card, kctl);
1663 item = snd_array_new(&codec->mixers);
1668 item->flags = flags;
1671 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1674 * snd_hda_add_nid - Assign a NID to a control element
1675 * @codec: HD-audio codec
1676 * @nid: corresponding NID (optional)
1677 * @kctl: the control element to assign
1678 * @index: index to kctl
1680 * Add the given control element to an array inside the codec instance.
1681 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1682 * NID:KCTL mapping - for example "Capture Source" selector.
1684 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1685 unsigned int index, hda_nid_t nid)
1687 struct hda_nid_item *item;
1690 item = snd_array_new(&codec->nids);
1694 item->index = index;
1698 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1699 kctl->id.name, kctl->id.index, index);
1702 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1705 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1706 * @codec: HD-audio codec
1708 void snd_hda_ctls_clear(struct hda_codec *codec)
1711 struct hda_nid_item *items = codec->mixers.list;
1712 for (i = 0; i < codec->mixers.used; i++)
1713 snd_ctl_remove(codec->card, items[i].kctl);
1714 snd_array_free(&codec->mixers);
1715 snd_array_free(&codec->nids);
1719 * snd_hda_lock_devices - pseudo device locking
1722 * toggle card->shutdown to allow/disallow the device access (as a hack)
1724 int snd_hda_lock_devices(struct hda_bus *bus)
1726 struct snd_card *card = bus->card;
1727 struct hda_codec *codec;
1729 spin_lock(&card->files_lock);
1733 if (!list_empty(&card->ctl_files))
1736 list_for_each_codec(codec, bus) {
1737 struct hda_pcm *cpcm;
1738 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1741 if (cpcm->pcm->streams[0].substream_opened ||
1742 cpcm->pcm->streams[1].substream_opened)
1746 spin_unlock(&card->files_lock);
1752 spin_unlock(&card->files_lock);
1755 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1758 * snd_hda_unlock_devices - pseudo device unlocking
1761 void snd_hda_unlock_devices(struct hda_bus *bus)
1763 struct snd_card *card = bus->card;
1765 spin_lock(&card->files_lock);
1767 spin_unlock(&card->files_lock);
1769 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1772 * snd_hda_codec_reset - Clear all objects assigned to the codec
1773 * @codec: HD-audio codec
1775 * This frees the all PCM and control elements assigned to the codec, and
1776 * clears the caches and restores the pin default configurations.
1778 * When a device is being used, it returns -EBSY. If successfully freed,
1781 int snd_hda_codec_reset(struct hda_codec *codec)
1783 struct hda_bus *bus = codec->bus;
1785 if (snd_hda_lock_devices(bus) < 0)
1788 /* OK, let it free */
1789 snd_hdac_device_unregister(&codec->core);
1791 /* allow device access again */
1792 snd_hda_unlock_devices(bus);
1796 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1798 /* apply the function to all matching slave ctls in the mixer list */
1799 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1800 const char *suffix, map_slave_func_t func, void *data)
1802 struct hda_nid_item *items;
1803 const char * const *s;
1806 items = codec->mixers.list;
1807 for (i = 0; i < codec->mixers.used; i++) {
1808 struct snd_kcontrol *sctl = items[i].kctl;
1809 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1811 for (s = slaves; *s; s++) {
1812 char tmpname[sizeof(sctl->id.name)];
1813 const char *name = *s;
1815 snprintf(tmpname, sizeof(tmpname), "%s %s",
1819 if (!strcmp(sctl->id.name, name)) {
1820 err = func(codec, data, sctl);
1830 static int check_slave_present(struct hda_codec *codec,
1831 void *data, struct snd_kcontrol *sctl)
1836 /* call kctl->put with the given value(s) */
1837 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1839 struct snd_ctl_elem_value *ucontrol;
1840 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1843 ucontrol->value.integer.value[0] = val;
1844 ucontrol->value.integer.value[1] = val;
1845 kctl->put(kctl, ucontrol);
1850 struct slave_init_arg {
1851 struct hda_codec *codec;
1855 /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */
1856 static int init_slave_0dB(struct snd_kcontrol *slave,
1857 struct snd_kcontrol *kctl,
1860 struct slave_init_arg *arg = _arg;
1862 const int *tlv = NULL;
1866 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1867 if (kctl->tlv.c != snd_hda_mixer_amp_tlv) {
1868 codec_err(arg->codec,
1869 "Unexpected TLV callback for slave %s:%d\n",
1870 kctl->id.name, kctl->id.index);
1871 return 0; /* ignore */
1873 get_ctl_amp_tlv(kctl, _tlv);
1875 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1878 if (!tlv || tlv[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
1881 step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP];
1882 step &= ~TLV_DB_SCALE_MUTE;
1885 if (arg->step && arg->step != step) {
1886 codec_err(arg->codec,
1887 "Mismatching dB step for vmaster slave (%d!=%d)\n",
1893 val = -tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] / step;
1895 put_kctl_with_value(slave, val);
1902 /* unmute the slave via snd_ctl_apply_vmaster_slaves() */
1903 static int init_slave_unmute(struct snd_kcontrol *slave,
1904 struct snd_kcontrol *kctl,
1907 return put_kctl_with_value(slave, 1);
1910 static int add_slave(struct hda_codec *codec,
1911 void *data, struct snd_kcontrol *slave)
1913 return snd_ctl_add_slave(data, slave);
1917 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1918 * @codec: HD-audio codec
1919 * @name: vmaster control name
1920 * @tlv: TLV data (optional)
1921 * @slaves: slave control names (optional)
1922 * @suffix: suffix string to each slave name (optional)
1923 * @init_slave_vol: initialize slaves to unmute/0dB
1924 * @ctl_ret: store the vmaster kcontrol in return
1926 * Create a virtual master control with the given name. The TLV data
1927 * must be either NULL or a valid data.
1929 * @slaves is a NULL-terminated array of strings, each of which is a
1930 * slave control name. All controls with these names are assigned to
1931 * the new virtual master control.
1933 * This function returns zero if successful or a negative error code.
1935 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1936 unsigned int *tlv, const char * const *slaves,
1937 const char *suffix, bool init_slave_vol,
1938 struct snd_kcontrol **ctl_ret)
1940 struct snd_kcontrol *kctl;
1946 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1948 codec_dbg(codec, "No slave found for %s\n", name);
1951 kctl = snd_ctl_make_virtual_master(name, tlv);
1954 err = snd_hda_ctl_add(codec, 0, kctl);
1958 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1962 /* init with master mute & zero volume */
1963 put_kctl_with_value(kctl, 0);
1964 if (init_slave_vol) {
1965 struct slave_init_arg arg = {
1969 snd_ctl_apply_vmaster_slaves(kctl,
1970 tlv ? init_slave_0dB : init_slave_unmute,
1978 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1981 * mute-LED control using vmaster
1983 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_info *uinfo)
1986 static const char * const texts[] = {
1987 "On", "Off", "Follow Master"
1990 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1993 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1996 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1997 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2001 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
2004 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2005 unsigned int old_mode = hook->mute_mode;
2007 hook->mute_mode = ucontrol->value.enumerated.item[0];
2008 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2009 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2010 if (old_mode == hook->mute_mode)
2012 snd_hda_sync_vmaster_hook(hook);
2016 static const struct snd_kcontrol_new vmaster_mute_mode = {
2017 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2018 .name = "Mute-LED Mode",
2019 .info = vmaster_mute_mode_info,
2020 .get = vmaster_mute_mode_get,
2021 .put = vmaster_mute_mode_put,
2024 /* meta hook to call each driver's vmaster hook */
2025 static void vmaster_hook(void *private_data, int enabled)
2027 struct hda_vmaster_mute_hook *hook = private_data;
2029 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2030 enabled = hook->mute_mode;
2031 hook->hook(hook->codec, enabled);
2035 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2036 * @codec: the HDA codec
2037 * @hook: the vmaster hook object
2038 * @expose_enum_ctl: flag to create an enum ctl
2040 * Add a mute-LED hook with the given vmaster switch kctl.
2041 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2042 * created and associated with the given hook.
2044 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2045 struct hda_vmaster_mute_hook *hook,
2046 bool expose_enum_ctl)
2048 struct snd_kcontrol *kctl;
2050 if (!hook->hook || !hook->sw_kctl)
2052 hook->codec = codec;
2053 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2054 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2055 if (!expose_enum_ctl)
2057 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2060 return snd_hda_ctl_add(codec, 0, kctl);
2062 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2065 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2066 * @hook: the vmaster hook
2068 * Call the hook with the current value for synchronization.
2069 * Should be called in init callback.
2071 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2073 if (!hook->hook || !hook->codec)
2075 /* don't call vmaster hook in the destructor since it might have
2076 * been already destroyed
2078 if (hook->codec->bus->shutdown)
2080 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2082 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2086 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2087 * @kcontrol: referred ctl element
2088 * @uinfo: pointer to get/store the data
2090 * The control element is supposed to have the private_value field
2091 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2093 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2094 struct snd_ctl_elem_info *uinfo)
2096 int chs = get_amp_channels(kcontrol);
2098 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2099 uinfo->count = chs == 3 ? 2 : 1;
2100 uinfo->value.integer.min = 0;
2101 uinfo->value.integer.max = 1;
2104 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2107 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2108 * @kcontrol: ctl element
2109 * @ucontrol: pointer to get/store the data
2111 * The control element is supposed to have the private_value field
2112 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2114 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2118 hda_nid_t nid = get_amp_nid(kcontrol);
2119 int chs = get_amp_channels(kcontrol);
2120 int dir = get_amp_direction(kcontrol);
2121 int idx = get_amp_index(kcontrol);
2122 long *valp = ucontrol->value.integer.value;
2125 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2126 HDA_AMP_MUTE) ? 0 : 1;
2128 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2129 HDA_AMP_MUTE) ? 0 : 1;
2132 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2135 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2136 * @kcontrol: ctl element
2137 * @ucontrol: pointer to get/store the data
2139 * The control element is supposed to have the private_value field
2140 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2142 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2143 struct snd_ctl_elem_value *ucontrol)
2145 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2146 hda_nid_t nid = get_amp_nid(kcontrol);
2147 int chs = get_amp_channels(kcontrol);
2148 int dir = get_amp_direction(kcontrol);
2149 int idx = get_amp_index(kcontrol);
2150 long *valp = ucontrol->value.integer.value;
2154 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2156 *valp ? 0 : HDA_AMP_MUTE);
2160 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2162 *valp ? 0 : HDA_AMP_MUTE);
2163 hda_call_check_power_status(codec, nid);
2166 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2169 * SPDIF out controls
2172 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_info *uinfo)
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2180 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2184 IEC958_AES0_NONAUDIO |
2185 IEC958_AES0_CON_EMPHASIS_5015 |
2186 IEC958_AES0_CON_NOT_COPYRIGHT;
2187 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2188 IEC958_AES1_CON_ORIGINAL;
2192 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2193 struct snd_ctl_elem_value *ucontrol)
2195 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2196 IEC958_AES0_NONAUDIO |
2197 IEC958_AES0_PRO_EMPHASIS_5015;
2201 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 int idx = kcontrol->private_value;
2206 struct hda_spdif_out *spdif;
2208 if (WARN_ON(codec->spdif_out.used <= idx))
2210 mutex_lock(&codec->spdif_mutex);
2211 spdif = snd_array_elem(&codec->spdif_out, idx);
2212 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2213 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2214 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2215 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2216 mutex_unlock(&codec->spdif_mutex);
2221 /* convert from SPDIF status bits to HDA SPDIF bits
2222 * bit 0 (DigEn) is always set zero (to be filled later)
2224 static unsigned short convert_from_spdif_status(unsigned int sbits)
2226 unsigned short val = 0;
2228 if (sbits & IEC958_AES0_PROFESSIONAL)
2229 val |= AC_DIG1_PROFESSIONAL;
2230 if (sbits & IEC958_AES0_NONAUDIO)
2231 val |= AC_DIG1_NONAUDIO;
2232 if (sbits & IEC958_AES0_PROFESSIONAL) {
2233 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2234 IEC958_AES0_PRO_EMPHASIS_5015)
2235 val |= AC_DIG1_EMPHASIS;
2237 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2238 IEC958_AES0_CON_EMPHASIS_5015)
2239 val |= AC_DIG1_EMPHASIS;
2240 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2241 val |= AC_DIG1_COPYRIGHT;
2242 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2243 val |= AC_DIG1_LEVEL;
2244 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2249 /* convert to SPDIF status bits from HDA SPDIF bits
2251 static unsigned int convert_to_spdif_status(unsigned short val)
2253 unsigned int sbits = 0;
2255 if (val & AC_DIG1_NONAUDIO)
2256 sbits |= IEC958_AES0_NONAUDIO;
2257 if (val & AC_DIG1_PROFESSIONAL)
2258 sbits |= IEC958_AES0_PROFESSIONAL;
2259 if (sbits & IEC958_AES0_PROFESSIONAL) {
2260 if (val & AC_DIG1_EMPHASIS)
2261 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2263 if (val & AC_DIG1_EMPHASIS)
2264 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2265 if (!(val & AC_DIG1_COPYRIGHT))
2266 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2267 if (val & AC_DIG1_LEVEL)
2268 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2269 sbits |= val & (0x7f << 8);
2274 /* set digital convert verbs both for the given NID and its slaves */
2275 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2280 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2282 d = codec->slave_dig_outs;
2286 snd_hdac_regmap_update(&codec->core, *d,
2287 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2290 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2293 unsigned int mask = 0;
2294 unsigned int val = 0;
2304 set_dig_out(codec, nid, mask, val);
2307 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_value *ucontrol)
2310 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2311 int idx = kcontrol->private_value;
2312 struct hda_spdif_out *spdif;
2317 if (WARN_ON(codec->spdif_out.used <= idx))
2319 mutex_lock(&codec->spdif_mutex);
2320 spdif = snd_array_elem(&codec->spdif_out, idx);
2322 spdif->status = ucontrol->value.iec958.status[0] |
2323 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2324 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2325 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2326 val = convert_from_spdif_status(spdif->status);
2327 val |= spdif->ctls & 1;
2328 change = spdif->ctls != val;
2330 if (change && nid != (u16)-1)
2331 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2332 mutex_unlock(&codec->spdif_mutex);
2336 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2338 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2341 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2342 int idx = kcontrol->private_value;
2343 struct hda_spdif_out *spdif;
2345 if (WARN_ON(codec->spdif_out.used <= idx))
2347 mutex_lock(&codec->spdif_mutex);
2348 spdif = snd_array_elem(&codec->spdif_out, idx);
2349 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2350 mutex_unlock(&codec->spdif_mutex);
2354 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2357 set_dig_out_convert(codec, nid, dig1, dig2);
2358 /* unmute amp switch (if any) */
2359 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2360 (dig1 & AC_DIG1_ENABLE))
2361 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2365 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_value *ucontrol)
2368 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2369 int idx = kcontrol->private_value;
2370 struct hda_spdif_out *spdif;
2375 if (WARN_ON(codec->spdif_out.used <= idx))
2377 mutex_lock(&codec->spdif_mutex);
2378 spdif = snd_array_elem(&codec->spdif_out, idx);
2380 val = spdif->ctls & ~AC_DIG1_ENABLE;
2381 if (ucontrol->value.integer.value[0])
2382 val |= AC_DIG1_ENABLE;
2383 change = spdif->ctls != val;
2385 if (change && nid != (u16)-1)
2386 set_spdif_ctls(codec, nid, val & 0xff, -1);
2387 mutex_unlock(&codec->spdif_mutex);
2391 static struct snd_kcontrol_new dig_mixes[] = {
2393 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2395 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2396 .info = snd_hda_spdif_mask_info,
2397 .get = snd_hda_spdif_cmask_get,
2400 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2401 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2402 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2403 .info = snd_hda_spdif_mask_info,
2404 .get = snd_hda_spdif_pmask_get,
2407 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2408 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2409 .info = snd_hda_spdif_mask_info,
2410 .get = snd_hda_spdif_default_get,
2411 .put = snd_hda_spdif_default_put,
2414 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2415 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2416 .info = snd_hda_spdif_out_switch_info,
2417 .get = snd_hda_spdif_out_switch_get,
2418 .put = snd_hda_spdif_out_switch_put,
2424 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2425 * @codec: the HDA codec
2426 * @associated_nid: NID that new ctls associated with
2427 * @cvt_nid: converter NID
2428 * @type: HDA_PCM_TYPE_*
2429 * Creates controls related with the digital output.
2430 * Called from each patch supporting the digital out.
2432 * Returns 0 if successful, or a negative error code.
2434 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2435 hda_nid_t associated_nid,
2440 struct snd_kcontrol *kctl;
2441 struct snd_kcontrol_new *dig_mix;
2444 const int spdif_index = 16;
2445 struct hda_spdif_out *spdif;
2446 struct hda_bus *bus = codec->bus;
2448 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2449 type == HDA_PCM_TYPE_SPDIF) {
2451 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2452 type == HDA_PCM_TYPE_HDMI) {
2453 /* suppose a single SPDIF device */
2454 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2455 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2458 kctl->id.index = spdif_index;
2460 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2462 if (!bus->primary_dig_out_type)
2463 bus->primary_dig_out_type = type;
2465 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2467 codec_err(codec, "too many IEC958 outputs\n");
2470 spdif = snd_array_new(&codec->spdif_out);
2473 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2474 kctl = snd_ctl_new1(dig_mix, codec);
2477 kctl->id.index = idx;
2478 kctl->private_value = codec->spdif_out.used - 1;
2479 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2483 spdif->nid = cvt_nid;
2484 snd_hdac_regmap_read(&codec->core, cvt_nid,
2485 AC_VERB_GET_DIGI_CONVERT_1, &val);
2487 spdif->status = convert_to_spdif_status(spdif->ctls);
2490 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2493 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2494 * @codec: the HDA codec
2497 * call within spdif_mutex lock
2499 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2502 struct hda_spdif_out *spdif;
2505 snd_array_for_each(&codec->spdif_out, i, spdif) {
2506 if (spdif->nid == nid)
2511 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2514 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2515 * @codec: the HDA codec
2516 * @idx: the SPDIF ctl index
2518 * Unassign the widget from the given SPDIF control.
2520 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2522 struct hda_spdif_out *spdif;
2524 if (WARN_ON(codec->spdif_out.used <= idx))
2526 mutex_lock(&codec->spdif_mutex);
2527 spdif = snd_array_elem(&codec->spdif_out, idx);
2528 spdif->nid = (u16)-1;
2529 mutex_unlock(&codec->spdif_mutex);
2531 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2534 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2535 * @codec: the HDA codec
2536 * @idx: the SPDIF ctl idx
2539 * Assign the widget to the SPDIF control with the given index.
2541 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2543 struct hda_spdif_out *spdif;
2546 if (WARN_ON(codec->spdif_out.used <= idx))
2548 mutex_lock(&codec->spdif_mutex);
2549 spdif = snd_array_elem(&codec->spdif_out, idx);
2550 if (spdif->nid != nid) {
2553 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2555 mutex_unlock(&codec->spdif_mutex);
2557 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2560 * SPDIF sharing with analog output
2562 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2563 struct snd_ctl_elem_value *ucontrol)
2565 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2566 ucontrol->value.integer.value[0] = mout->share_spdif;
2570 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2571 struct snd_ctl_elem_value *ucontrol)
2573 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2574 mout->share_spdif = !!ucontrol->value.integer.value[0];
2578 static const struct snd_kcontrol_new spdif_share_sw = {
2579 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2580 .name = "IEC958 Default PCM Playback Switch",
2581 .info = snd_ctl_boolean_mono_info,
2582 .get = spdif_share_sw_get,
2583 .put = spdif_share_sw_put,
2587 * snd_hda_create_spdif_share_sw - create Default PCM switch
2588 * @codec: the HDA codec
2589 * @mout: multi-out instance
2591 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2592 struct hda_multi_out *mout)
2594 struct snd_kcontrol *kctl;
2596 if (!mout->dig_out_nid)
2599 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2602 /* ATTENTION: here mout is passed as private_data, instead of codec */
2603 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2605 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2611 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2613 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
2616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2618 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2622 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_value *ucontrol)
2625 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2626 hda_nid_t nid = kcontrol->private_value;
2627 unsigned int val = !!ucontrol->value.integer.value[0];
2630 mutex_lock(&codec->spdif_mutex);
2631 change = codec->spdif_in_enable != val;
2633 codec->spdif_in_enable = val;
2634 snd_hdac_regmap_write(&codec->core, nid,
2635 AC_VERB_SET_DIGI_CONVERT_1, val);
2637 mutex_unlock(&codec->spdif_mutex);
2641 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2645 hda_nid_t nid = kcontrol->private_value;
2649 snd_hdac_regmap_read(&codec->core, nid,
2650 AC_VERB_GET_DIGI_CONVERT_1, &val);
2651 sbits = convert_to_spdif_status(val);
2652 ucontrol->value.iec958.status[0] = sbits;
2653 ucontrol->value.iec958.status[1] = sbits >> 8;
2654 ucontrol->value.iec958.status[2] = sbits >> 16;
2655 ucontrol->value.iec958.status[3] = sbits >> 24;
2659 static struct snd_kcontrol_new dig_in_ctls[] = {
2661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2662 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2663 .info = snd_hda_spdif_in_switch_info,
2664 .get = snd_hda_spdif_in_switch_get,
2665 .put = snd_hda_spdif_in_switch_put,
2668 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2669 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2670 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2671 .info = snd_hda_spdif_mask_info,
2672 .get = snd_hda_spdif_in_status_get,
2678 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2679 * @codec: the HDA codec
2680 * @nid: audio in widget NID
2682 * Creates controls related with the SPDIF input.
2683 * Called from each patch supporting the SPDIF in.
2685 * Returns 0 if successful, or a negative error code.
2687 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2690 struct snd_kcontrol *kctl;
2691 struct snd_kcontrol_new *dig_mix;
2694 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2696 codec_err(codec, "too many IEC958 inputs\n");
2699 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2700 kctl = snd_ctl_new1(dig_mix, codec);
2703 kctl->private_value = nid;
2704 err = snd_hda_ctl_add(codec, nid, kctl);
2708 codec->spdif_in_enable =
2709 snd_hda_codec_read(codec, nid, 0,
2710 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2714 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2717 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2718 * @codec: the HDA codec
2719 * @fg: function group (not used now)
2720 * @power_state: the power state to set (AC_PWRST_*)
2722 * Set the given power state to all widgets that have the power control.
2723 * If the codec has power_filter set, it evaluates the power state and
2724 * filter out if it's unchanged as D3.
2726 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2727 unsigned int power_state)
2731 for_each_hda_codec_node(nid, codec) {
2732 unsigned int wcaps = get_wcaps(codec, nid);
2733 unsigned int state = power_state;
2734 if (!(wcaps & AC_WCAP_POWER))
2736 if (codec->power_filter) {
2737 state = codec->power_filter(codec, nid, power_state);
2738 if (state != power_state && power_state == AC_PWRST_D3)
2741 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2745 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2748 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2749 * @codec: the HDA codec
2751 * @power_state: power state to evalue
2753 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2754 * This can be used a codec power_filter callback.
2756 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2758 unsigned int power_state)
2760 if (nid == codec->core.afg || nid == codec->core.mfg)
2762 if (power_state == AC_PWRST_D3 &&
2763 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2764 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2765 int eapd = snd_hda_codec_read(codec, nid, 0,
2766 AC_VERB_GET_EAPD_BTLENABLE, 0);
2772 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2775 * set power state of the codec, and return the power state
2777 static unsigned int hda_set_power_state(struct hda_codec *codec,
2778 unsigned int power_state)
2780 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2785 /* this delay seems necessary to avoid click noise at power-down */
2786 if (power_state == AC_PWRST_D3) {
2787 if (codec->depop_delay < 0)
2788 msleep(codec_has_epss(codec) ? 10 : 100);
2789 else if (codec->depop_delay > 0)
2790 msleep(codec->depop_delay);
2791 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2794 /* repeat power states setting at most 10 times*/
2795 for (count = 0; count < 10; count++) {
2796 if (codec->patch_ops.set_power_state)
2797 codec->patch_ops.set_power_state(codec, fg,
2800 state = power_state;
2801 if (codec->power_filter)
2802 state = codec->power_filter(codec, fg, state);
2803 if (state == power_state || power_state != AC_PWRST_D3)
2804 snd_hda_codec_read(codec, fg, flags,
2805 AC_VERB_SET_POWER_STATE,
2807 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2809 state = snd_hda_sync_power_state(codec, fg, power_state);
2810 if (!(state & AC_PWRST_ERROR))
2817 /* sync power states of all widgets;
2818 * this is called at the end of codec parsing
2820 static void sync_power_up_states(struct hda_codec *codec)
2824 /* don't care if no filter is used */
2825 if (!codec->power_filter)
2828 for_each_hda_codec_node(nid, codec) {
2829 unsigned int wcaps = get_wcaps(codec, nid);
2830 unsigned int target;
2831 if (!(wcaps & AC_WCAP_POWER))
2833 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2834 if (target == AC_PWRST_D0)
2836 if (!snd_hda_check_power_state(codec, nid, target))
2837 snd_hda_codec_write(codec, nid, 0,
2838 AC_VERB_SET_POWER_STATE, target);
2842 #ifdef CONFIG_SND_HDA_RECONFIG
2843 /* execute additional init verbs */
2844 static void hda_exec_init_verbs(struct hda_codec *codec)
2846 if (codec->init_verbs.list)
2847 snd_hda_sequence_write(codec, codec->init_verbs.list);
2850 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2854 /* update the power on/off account with the current jiffies */
2855 static void update_power_acct(struct hda_codec *codec, bool on)
2857 unsigned long delta = jiffies - codec->power_jiffies;
2860 codec->power_on_acct += delta;
2862 codec->power_off_acct += delta;
2863 codec->power_jiffies += delta;
2866 void snd_hda_update_power_acct(struct hda_codec *codec)
2868 update_power_acct(codec, hda_codec_is_power_on(codec));
2872 * call suspend and power-down; used both from PM and power-save
2873 * this function returns the power state in the end
2875 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2879 snd_hdac_enter_pm(&codec->core);
2880 if (codec->patch_ops.suspend)
2881 codec->patch_ops.suspend(codec);
2882 hda_cleanup_all_streams(codec);
2883 state = hda_set_power_state(codec, AC_PWRST_D3);
2884 update_power_acct(codec, true);
2885 snd_hdac_leave_pm(&codec->core);
2890 * kick up codec; used both from PM and power-save
2892 static void hda_call_codec_resume(struct hda_codec *codec)
2894 snd_hdac_enter_pm(&codec->core);
2895 if (codec->core.regmap)
2896 regcache_mark_dirty(codec->core.regmap);
2898 codec->power_jiffies = jiffies;
2900 hda_set_power_state(codec, AC_PWRST_D0);
2901 restore_shutup_pins(codec);
2902 hda_exec_init_verbs(codec);
2903 snd_hda_jack_set_dirty_all(codec);
2904 if (codec->patch_ops.resume)
2905 codec->patch_ops.resume(codec);
2907 if (codec->patch_ops.init)
2908 codec->patch_ops.init(codec);
2909 if (codec->core.regmap)
2910 regcache_sync(codec->core.regmap);
2913 if (codec->jackpoll_interval)
2914 hda_jackpoll_work(&codec->jackpoll_work.work);
2916 snd_hda_jack_report_sync(codec);
2917 codec->core.dev.power.power_state = PMSG_ON;
2918 snd_hdac_leave_pm(&codec->core);
2921 static int hda_codec_runtime_suspend(struct device *dev)
2923 struct hda_codec *codec = dev_to_hda_codec(dev);
2926 cancel_delayed_work_sync(&codec->jackpoll_work);
2927 state = hda_call_codec_suspend(codec);
2928 if (codec->link_down_at_suspend ||
2929 (codec_has_clkstop(codec) && codec_has_epss(codec) &&
2930 (state & AC_PWRST_CLK_STOP_OK)))
2931 snd_hdac_codec_link_down(&codec->core);
2932 codec_display_power(codec, false);
2936 static int hda_codec_runtime_resume(struct device *dev)
2938 struct hda_codec *codec = dev_to_hda_codec(dev);
2940 codec_display_power(codec, true);
2941 snd_hdac_codec_link_up(&codec->core);
2942 hda_call_codec_resume(codec);
2943 pm_runtime_mark_last_busy(dev);
2946 #endif /* CONFIG_PM */
2948 #ifdef CONFIG_PM_SLEEP
2949 static int hda_codec_force_resume(struct device *dev)
2953 /* The get/put pair below enforces the runtime resume even if the
2954 * device hasn't been used at suspend time. This trick is needed to
2955 * update the jack state change during the sleep.
2957 pm_runtime_get_noresume(dev);
2958 ret = pm_runtime_force_resume(dev);
2959 pm_runtime_put(dev);
2963 static int hda_codec_pm_suspend(struct device *dev)
2965 dev->power.power_state = PMSG_SUSPEND;
2966 return pm_runtime_force_suspend(dev);
2969 static int hda_codec_pm_resume(struct device *dev)
2971 dev->power.power_state = PMSG_RESUME;
2972 return hda_codec_force_resume(dev);
2975 static int hda_codec_pm_freeze(struct device *dev)
2977 dev->power.power_state = PMSG_FREEZE;
2978 return pm_runtime_force_suspend(dev);
2981 static int hda_codec_pm_thaw(struct device *dev)
2983 dev->power.power_state = PMSG_THAW;
2984 return hda_codec_force_resume(dev);
2987 static int hda_codec_pm_restore(struct device *dev)
2989 dev->power.power_state = PMSG_RESTORE;
2990 return hda_codec_force_resume(dev);
2992 #endif /* CONFIG_PM_SLEEP */
2994 /* referred in hda_bind.c */
2995 const struct dev_pm_ops hda_codec_driver_pm = {
2996 #ifdef CONFIG_PM_SLEEP
2997 .suspend = hda_codec_pm_suspend,
2998 .resume = hda_codec_pm_resume,
2999 .freeze = hda_codec_pm_freeze,
3000 .thaw = hda_codec_pm_thaw,
3001 .poweroff = hda_codec_pm_suspend,
3002 .restore = hda_codec_pm_restore,
3003 #endif /* CONFIG_PM_SLEEP */
3004 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3009 * add standard channel maps if not specified
3011 static int add_std_chmaps(struct hda_codec *codec)
3013 struct hda_pcm *pcm;
3016 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3017 for (str = 0; str < 2; str++) {
3018 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3019 struct snd_pcm_chmap *chmap;
3020 const struct snd_pcm_chmap_elem *elem;
3022 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
3024 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3025 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3026 hinfo->channels_max,
3030 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3036 /* default channel maps for 2.1 speakers;
3037 * since HD-audio supports only stereo, odd number channels are omitted
3039 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3041 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3043 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3044 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3047 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3049 int snd_hda_codec_build_controls(struct hda_codec *codec)
3052 hda_exec_init_verbs(codec);
3053 /* continue to initialize... */
3054 if (codec->patch_ops.init)
3055 err = codec->patch_ops.init(codec);
3056 if (!err && codec->patch_ops.build_controls)
3057 err = codec->patch_ops.build_controls(codec);
3061 /* we create chmaps here instead of build_pcms */
3062 err = add_std_chmaps(codec);
3066 if (codec->jackpoll_interval)
3067 hda_jackpoll_work(&codec->jackpoll_work.work);
3069 snd_hda_jack_report_sync(codec); /* call at the last init point */
3070 sync_power_up_states(codec);
3073 EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls);
3078 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3079 struct hda_codec *codec,
3080 struct snd_pcm_substream *substream)
3085 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3086 struct hda_codec *codec,
3087 unsigned int stream_tag,
3088 unsigned int format,
3089 struct snd_pcm_substream *substream)
3091 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3095 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3096 struct hda_codec *codec,
3097 struct snd_pcm_substream *substream)
3099 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3103 static int set_pcm_default_values(struct hda_codec *codec,
3104 struct hda_pcm_stream *info)
3108 /* query support PCM information from the given NID */
3109 if (info->nid && (!info->rates || !info->formats)) {
3110 err = snd_hda_query_supported_pcm(codec, info->nid,
3111 info->rates ? NULL : &info->rates,
3112 info->formats ? NULL : &info->formats,
3113 info->maxbps ? NULL : &info->maxbps);
3117 if (info->ops.open == NULL)
3118 info->ops.open = hda_pcm_default_open_close;
3119 if (info->ops.close == NULL)
3120 info->ops.close = hda_pcm_default_open_close;
3121 if (info->ops.prepare == NULL) {
3122 if (snd_BUG_ON(!info->nid))
3124 info->ops.prepare = hda_pcm_default_prepare;
3126 if (info->ops.cleanup == NULL) {
3127 if (snd_BUG_ON(!info->nid))
3129 info->ops.cleanup = hda_pcm_default_cleanup;
3135 * codec prepare/cleanup entries
3138 * snd_hda_codec_prepare - Prepare a stream
3139 * @codec: the HDA codec
3140 * @hinfo: PCM information
3141 * @stream: stream tag to assign
3142 * @format: format id to assign
3143 * @substream: PCM substream to assign
3145 * Calls the prepare callback set by the codec with the given arguments.
3146 * Clean up the inactive streams when successful.
3148 int snd_hda_codec_prepare(struct hda_codec *codec,
3149 struct hda_pcm_stream *hinfo,
3150 unsigned int stream,
3151 unsigned int format,
3152 struct snd_pcm_substream *substream)
3155 mutex_lock(&codec->bus->prepare_mutex);
3156 if (hinfo->ops.prepare)
3157 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3162 purify_inactive_streams(codec);
3163 mutex_unlock(&codec->bus->prepare_mutex);
3166 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3169 * snd_hda_codec_cleanup - Prepare a stream
3170 * @codec: the HDA codec
3171 * @hinfo: PCM information
3172 * @substream: PCM substream
3174 * Calls the cleanup callback set by the codec with the given arguments.
3176 void snd_hda_codec_cleanup(struct hda_codec *codec,
3177 struct hda_pcm_stream *hinfo,
3178 struct snd_pcm_substream *substream)
3180 mutex_lock(&codec->bus->prepare_mutex);
3181 if (hinfo->ops.cleanup)
3182 hinfo->ops.cleanup(hinfo, codec, substream);
3183 mutex_unlock(&codec->bus->prepare_mutex);
3185 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3188 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3189 "Audio", "SPDIF", "HDMI", "Modem"
3193 * get the empty PCM device number to assign
3195 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3197 /* audio device indices; not linear to keep compatibility */
3198 /* assigned to static slots up to dev#10; if more needed, assign
3199 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3201 static int audio_idx[HDA_PCM_NTYPES][5] = {
3202 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3203 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3204 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3205 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3209 if (type >= HDA_PCM_NTYPES) {
3210 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3214 for (i = 0; audio_idx[type][i] >= 0; i++) {
3215 #ifndef CONFIG_SND_DYNAMIC_MINORS
3216 if (audio_idx[type][i] >= 8)
3219 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3220 return audio_idx[type][i];
3223 #ifdef CONFIG_SND_DYNAMIC_MINORS
3224 /* non-fixed slots starting from 10 */
3225 for (i = 10; i < 32; i++) {
3226 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3231 dev_warn(bus->card->dev, "Too many %s devices\n",
3232 snd_hda_pcm_type_name[type]);
3233 #ifndef CONFIG_SND_DYNAMIC_MINORS
3234 dev_warn(bus->card->dev,
3235 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3240 /* call build_pcms ops of the given codec and set up the default parameters */
3241 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3243 struct hda_pcm *cpcm;
3246 if (!list_empty(&codec->pcm_list_head))
3247 return 0; /* already parsed */
3249 if (!codec->patch_ops.build_pcms)
3252 err = codec->patch_ops.build_pcms(codec);
3254 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3255 codec->core.addr, err);
3259 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3262 for (stream = 0; stream < 2; stream++) {
3263 struct hda_pcm_stream *info = &cpcm->stream[stream];
3265 if (!info->substreams)
3267 err = set_pcm_default_values(codec, info);
3270 "fail to setup default for PCM %s\n",
3279 EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms);
3281 /* assign all PCMs of the given codec */
3282 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3284 struct hda_bus *bus = codec->bus;
3285 struct hda_pcm *cpcm;
3288 err = snd_hda_codec_parse_pcms(codec);
3292 /* attach a new PCM streams */
3293 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3295 continue; /* already attached */
3296 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3297 continue; /* no substreams assigned */
3299 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3301 cpcm->device = SNDRV_PCM_INVALID_DEVICE;
3302 continue; /* no fatal error */
3305 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3308 "cannot attach PCM stream %d for codec #%d\n",
3309 dev, codec->core.addr);
3310 continue; /* no fatal error */
3318 * snd_hda_add_new_ctls - create controls from the array
3319 * @codec: the HDA codec
3320 * @knew: the array of struct snd_kcontrol_new
3322 * This helper function creates and add new controls in the given array.
3323 * The array must be terminated with an empty entry as terminator.
3325 * Returns 0 if successful, or a negative error code.
3327 int snd_hda_add_new_ctls(struct hda_codec *codec,
3328 const struct snd_kcontrol_new *knew)
3332 for (; knew->name; knew++) {
3333 struct snd_kcontrol *kctl;
3334 int addr = 0, idx = 0;
3335 if (knew->iface == (__force snd_ctl_elem_iface_t)-1)
3336 continue; /* skip this codec private value */
3338 kctl = snd_ctl_new1(knew, codec);
3342 kctl->id.device = addr;
3344 kctl->id.index = idx;
3345 err = snd_hda_ctl_add(codec, 0, kctl);
3348 /* try first with another device index corresponding to
3349 * the codec addr; if it still fails (or it's the
3350 * primary codec), then try another control index
3352 if (!addr && codec->core.addr)
3353 addr = codec->core.addr;
3354 else if (!idx && !knew->index) {
3355 idx = find_empty_mixer_ctl_idx(codec,
3365 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3368 static void codec_set_power_save(struct hda_codec *codec, int delay)
3370 struct device *dev = hda_codec_dev(codec);
3372 if (delay == 0 && codec->auto_runtime_pm)
3376 pm_runtime_set_autosuspend_delay(dev, delay);
3377 pm_runtime_use_autosuspend(dev);
3378 pm_runtime_allow(dev);
3379 if (!pm_runtime_suspended(dev))
3380 pm_runtime_mark_last_busy(dev);
3382 pm_runtime_dont_use_autosuspend(dev);
3383 pm_runtime_forbid(dev);
3388 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3389 * @bus: HD-audio bus
3390 * @delay: autosuspend delay in msec, 0 = off
3392 * Synchronize the runtime PM autosuspend state from the power_save option.
3394 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3396 struct hda_codec *c;
3398 list_for_each_codec(c, bus)
3399 codec_set_power_save(c, delay);
3401 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3404 * snd_hda_check_amp_list_power - Check the amp list and update the power
3405 * @codec: HD-audio codec
3406 * @check: the object containing an AMP list and the status
3407 * @nid: NID to check / update
3409 * Check whether the given NID is in the amp list. If it's in the list,
3410 * check the current AMP status, and update the the power-status according
3411 * to the mute status.
3413 * This function is supposed to be set or called from the check_power_status
3416 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3417 struct hda_loopback_check *check,
3420 const struct hda_amp_list *p;
3423 if (!check->amplist)
3425 for (p = check->amplist; p->nid; p++) {
3430 return 0; /* nothing changed */
3432 for (p = check->amplist; p->nid; p++) {
3433 for (ch = 0; ch < 2; ch++) {
3434 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3436 if (!(v & HDA_AMP_MUTE) && v > 0) {
3437 if (!check->power_on) {
3438 check->power_on = 1;
3439 snd_hda_power_up_pm(codec);
3445 if (check->power_on) {
3446 check->power_on = 0;
3447 snd_hda_power_down_pm(codec);
3451 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3459 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3460 * @imux: imux helper object
3461 * @uinfo: pointer to get/store the data
3463 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3464 struct snd_ctl_elem_info *uinfo)
3468 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3470 uinfo->value.enumerated.items = imux->num_items;
3471 if (!imux->num_items)
3473 index = uinfo->value.enumerated.item;
3474 if (index >= imux->num_items)
3475 index = imux->num_items - 1;
3476 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3479 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3482 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3483 * @codec: the HDA codec
3484 * @imux: imux helper object
3485 * @ucontrol: pointer to get/store the data
3486 * @nid: input mux NID
3487 * @cur_val: pointer to get/store the current imux value
3489 int snd_hda_input_mux_put(struct hda_codec *codec,
3490 const struct hda_input_mux *imux,
3491 struct snd_ctl_elem_value *ucontrol,
3493 unsigned int *cur_val)
3497 if (!imux->num_items)
3499 idx = ucontrol->value.enumerated.item[0];
3500 if (idx >= imux->num_items)
3501 idx = imux->num_items - 1;
3502 if (*cur_val == idx)
3504 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3505 imux->items[idx].index);
3509 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3513 * snd_hda_enum_helper_info - Helper for simple enum ctls
3514 * @kcontrol: ctl element
3515 * @uinfo: pointer to get/store the data
3516 * @num_items: number of enum items
3517 * @texts: enum item string array
3519 * process kcontrol info callback of a simple string enum array
3520 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3522 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3523 struct snd_ctl_elem_info *uinfo,
3524 int num_items, const char * const *texts)
3526 static const char * const texts_default[] = {
3527 "Disabled", "Enabled"
3530 if (!texts || !num_items) {
3532 texts = texts_default;
3535 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3537 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3540 * Multi-channel / digital-out PCM helper functions
3543 /* setup SPDIF output stream */
3544 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3545 unsigned int stream_tag, unsigned int format)
3547 struct hda_spdif_out *spdif;
3548 unsigned int curr_fmt;
3551 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3552 /* Add sanity check to pass klockwork check.
3553 * This should never happen.
3555 if (WARN_ON(spdif == NULL))
3558 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3559 AC_VERB_GET_STREAM_FORMAT, 0);
3560 reset = codec->spdif_status_reset &&
3561 (spdif->ctls & AC_DIG1_ENABLE) &&
3564 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3567 set_dig_out_convert(codec, nid,
3568 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3570 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3571 if (codec->slave_dig_outs) {
3573 for (d = codec->slave_dig_outs; *d; d++)
3574 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3577 /* turn on again (if needed) */
3579 set_dig_out_convert(codec, nid,
3580 spdif->ctls & 0xff, -1);
3583 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3585 snd_hda_codec_cleanup_stream(codec, nid);
3586 if (codec->slave_dig_outs) {
3588 for (d = codec->slave_dig_outs; *d; d++)
3589 snd_hda_codec_cleanup_stream(codec, *d);
3594 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3595 * @codec: the HDA codec
3596 * @mout: hda_multi_out object
3598 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3599 struct hda_multi_out *mout)
3601 mutex_lock(&codec->spdif_mutex);
3602 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3603 /* already opened as analog dup; reset it once */
3604 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3605 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3606 mutex_unlock(&codec->spdif_mutex);
3609 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3612 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3613 * @codec: the HDA codec
3614 * @mout: hda_multi_out object
3615 * @stream_tag: stream tag to assign
3616 * @format: format id to assign
3617 * @substream: PCM substream to assign
3619 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3620 struct hda_multi_out *mout,
3621 unsigned int stream_tag,
3622 unsigned int format,
3623 struct snd_pcm_substream *substream)
3625 mutex_lock(&codec->spdif_mutex);
3626 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3627 mutex_unlock(&codec->spdif_mutex);
3630 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3633 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3634 * @codec: the HDA codec
3635 * @mout: hda_multi_out object
3637 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3638 struct hda_multi_out *mout)
3640 mutex_lock(&codec->spdif_mutex);
3641 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3642 mutex_unlock(&codec->spdif_mutex);
3645 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3648 * snd_hda_multi_out_dig_close - release the digital out stream
3649 * @codec: the HDA codec
3650 * @mout: hda_multi_out object
3652 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3653 struct hda_multi_out *mout)
3655 mutex_lock(&codec->spdif_mutex);
3656 mout->dig_out_used = 0;
3657 mutex_unlock(&codec->spdif_mutex);
3660 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3663 * snd_hda_multi_out_analog_open - open analog outputs
3664 * @codec: the HDA codec
3665 * @mout: hda_multi_out object
3666 * @substream: PCM substream to assign
3667 * @hinfo: PCM information to assign
3669 * Open analog outputs and set up the hw-constraints.
3670 * If the digital outputs can be opened as slave, open the digital
3673 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3674 struct hda_multi_out *mout,
3675 struct snd_pcm_substream *substream,
3676 struct hda_pcm_stream *hinfo)
3678 struct snd_pcm_runtime *runtime = substream->runtime;
3679 runtime->hw.channels_max = mout->max_channels;
3680 if (mout->dig_out_nid) {
3681 if (!mout->analog_rates) {
3682 mout->analog_rates = hinfo->rates;
3683 mout->analog_formats = hinfo->formats;
3684 mout->analog_maxbps = hinfo->maxbps;
3686 runtime->hw.rates = mout->analog_rates;
3687 runtime->hw.formats = mout->analog_formats;
3688 hinfo->maxbps = mout->analog_maxbps;
3690 if (!mout->spdif_rates) {
3691 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3693 &mout->spdif_formats,
3694 &mout->spdif_maxbps);
3696 mutex_lock(&codec->spdif_mutex);
3697 if (mout->share_spdif) {
3698 if ((runtime->hw.rates & mout->spdif_rates) &&
3699 (runtime->hw.formats & mout->spdif_formats)) {
3700 runtime->hw.rates &= mout->spdif_rates;
3701 runtime->hw.formats &= mout->spdif_formats;
3702 if (mout->spdif_maxbps < hinfo->maxbps)
3703 hinfo->maxbps = mout->spdif_maxbps;
3705 mout->share_spdif = 0;
3706 /* FIXME: need notify? */
3709 mutex_unlock(&codec->spdif_mutex);
3711 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3712 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3714 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3717 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3718 * @codec: the HDA codec
3719 * @mout: hda_multi_out object
3720 * @stream_tag: stream tag to assign
3721 * @format: format id to assign
3722 * @substream: PCM substream to assign
3724 * Set up the i/o for analog out.
3725 * When the digital out is available, copy the front out to digital out, too.
3727 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3728 struct hda_multi_out *mout,
3729 unsigned int stream_tag,
3730 unsigned int format,
3731 struct snd_pcm_substream *substream)
3733 const hda_nid_t *nids = mout->dac_nids;
3734 int chs = substream->runtime->channels;
3735 struct hda_spdif_out *spdif;
3738 mutex_lock(&codec->spdif_mutex);
3739 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3740 if (mout->dig_out_nid && mout->share_spdif &&
3741 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3742 if (chs == 2 && spdif != NULL &&
3743 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3745 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3746 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3747 setup_dig_out_stream(codec, mout->dig_out_nid,
3748 stream_tag, format);
3750 mout->dig_out_used = 0;
3751 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3754 mutex_unlock(&codec->spdif_mutex);
3757 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3759 if (!mout->no_share_stream &&
3760 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3761 /* headphone out will just decode front left/right (stereo) */
3762 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3764 /* extra outputs copied from front */
3765 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3766 if (!mout->no_share_stream && mout->hp_out_nid[i])
3767 snd_hda_codec_setup_stream(codec,
3768 mout->hp_out_nid[i],
3769 stream_tag, 0, format);
3772 for (i = 1; i < mout->num_dacs; i++) {
3773 if (chs >= (i + 1) * 2) /* independent out */
3774 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3776 else if (!mout->no_share_stream) /* copy front */
3777 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3781 /* extra surrounds */
3782 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3784 if (!mout->extra_out_nid[i])
3786 if (chs >= (i + 1) * 2)
3788 else if (!mout->no_share_stream)
3790 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3791 stream_tag, ch, format);
3796 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3799 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3800 * @codec: the HDA codec
3801 * @mout: hda_multi_out object
3803 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3804 struct hda_multi_out *mout)
3806 const hda_nid_t *nids = mout->dac_nids;
3809 for (i = 0; i < mout->num_dacs; i++)
3810 snd_hda_codec_cleanup_stream(codec, nids[i]);
3812 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3813 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3814 if (mout->hp_out_nid[i])
3815 snd_hda_codec_cleanup_stream(codec,
3816 mout->hp_out_nid[i]);
3817 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3818 if (mout->extra_out_nid[i])
3819 snd_hda_codec_cleanup_stream(codec,
3820 mout->extra_out_nid[i]);
3821 mutex_lock(&codec->spdif_mutex);
3822 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3823 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3824 mout->dig_out_used = 0;
3826 mutex_unlock(&codec->spdif_mutex);
3829 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3832 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3833 * @codec: the HDA codec
3834 * @pin: referred pin NID
3836 * Guess the suitable VREF pin bits to be set as the pin-control value.
3837 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3839 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3841 unsigned int pincap;
3842 unsigned int oldval;
3843 oldval = snd_hda_codec_read(codec, pin, 0,
3844 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3845 pincap = snd_hda_query_pin_caps(codec, pin);
3846 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3847 /* Exception: if the default pin setup is vref50, we give it priority */
3848 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3849 return AC_PINCTL_VREF_80;
3850 else if (pincap & AC_PINCAP_VREF_50)
3851 return AC_PINCTL_VREF_50;
3852 else if (pincap & AC_PINCAP_VREF_100)
3853 return AC_PINCTL_VREF_100;
3854 else if (pincap & AC_PINCAP_VREF_GRD)
3855 return AC_PINCTL_VREF_GRD;
3856 return AC_PINCTL_VREF_HIZ;
3858 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3861 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3862 * @codec: the HDA codec
3863 * @pin: referred pin NID
3864 * @val: pin ctl value to audit
3866 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3867 hda_nid_t pin, unsigned int val)
3869 static unsigned int cap_lists[][2] = {
3870 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3871 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3872 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3873 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3879 cap = snd_hda_query_pin_caps(codec, pin);
3881 return val; /* don't know what to do... */
3883 if (val & AC_PINCTL_OUT_EN) {
3884 if (!(cap & AC_PINCAP_OUT))
3885 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3886 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3887 val &= ~AC_PINCTL_HP_EN;
3890 if (val & AC_PINCTL_IN_EN) {
3891 if (!(cap & AC_PINCAP_IN))
3892 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3894 unsigned int vcap, vref;
3896 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3897 vref = val & AC_PINCTL_VREFEN;
3898 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3899 if (vref == cap_lists[i][0] &&
3900 !(vcap & cap_lists[i][1])) {
3901 if (i == ARRAY_SIZE(cap_lists) - 1)
3902 vref = AC_PINCTL_VREF_HIZ;
3904 vref = cap_lists[i + 1][0];
3907 val &= ~AC_PINCTL_VREFEN;
3914 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
3917 * _snd_hda_pin_ctl - Helper to set pin ctl value
3918 * @codec: the HDA codec
3919 * @pin: referred pin NID
3920 * @val: pin control value to set
3921 * @cached: access over codec pinctl cache or direct write
3923 * This function is a helper to set a pin ctl value more safely.
3924 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3925 * value in pin target array via snd_hda_codec_set_pin_target(), then
3926 * actually writes the value via either snd_hda_codec_write_cache() or
3927 * snd_hda_codec_write() depending on @cached flag.
3929 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3930 unsigned int val, bool cached)
3932 val = snd_hda_correct_pin_ctl(codec, pin, val);
3933 snd_hda_codec_set_pin_target(codec, pin, val);
3935 return snd_hda_codec_write_cache(codec, pin, 0,
3936 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3938 return snd_hda_codec_write(codec, pin, 0,
3939 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3941 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
3944 * snd_hda_add_imux_item - Add an item to input_mux
3945 * @codec: the HDA codec
3946 * @imux: imux helper object
3947 * @label: the name of imux item to assign
3948 * @index: index number of imux item to assign
3949 * @type_idx: pointer to store the resultant label index
3951 * When the same label is used already in the existing items, the number
3952 * suffix is appended to the label. This label index number is stored
3953 * to type_idx when non-NULL pointer is given.
3955 int snd_hda_add_imux_item(struct hda_codec *codec,
3956 struct hda_input_mux *imux, const char *label,
3957 int index, int *type_idx)
3959 int i, label_idx = 0;
3960 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
3961 codec_err(codec, "hda_codec: Too many imux items!\n");
3964 for (i = 0; i < imux->num_items; i++) {
3965 if (!strncmp(label, imux->items[i].label, strlen(label)))
3969 *type_idx = label_idx;
3971 snprintf(imux->items[imux->num_items].label,
3972 sizeof(imux->items[imux->num_items].label),
3973 "%s %d", label, label_idx);
3975 strlcpy(imux->items[imux->num_items].label, label,
3976 sizeof(imux->items[imux->num_items].label));
3977 imux->items[imux->num_items].index = index;
3981 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
3984 * snd_hda_bus_reset_codecs - Reset the bus
3985 * @bus: HD-audio bus
3987 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
3989 struct hda_codec *codec;
3991 list_for_each_codec(codec, bus) {
3992 /* FIXME: maybe a better way needed for forced reset */
3993 if (current_work() != &codec->jackpoll_work.work)
3994 cancel_delayed_work_sync(&codec->jackpoll_work);
3996 if (hda_codec_is_power_on(codec)) {
3997 hda_call_codec_suspend(codec);
3998 hda_call_codec_resume(codec);
4005 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4006 * @pcm: PCM caps bits
4007 * @buf: the string buffer to write
4008 * @buflen: the max buffer length
4010 * used by hda_proc.c and hda_eld.c
4012 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4014 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4017 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4018 if (pcm & (AC_SUPPCM_BITS_8 << i))
4019 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4021 buf[j] = '\0'; /* necessary when j == 0 */
4023 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4025 MODULE_DESCRIPTION("HDA codec core");
4026 MODULE_LICENSE("GPL");