2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * Send and receive a verb - passed to exec_verb override for hdac_device
59 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
60 unsigned int flags, unsigned int *res)
62 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
63 struct hda_bus *bus = codec->bus;
70 snd_hda_power_up_pm(codec);
71 mutex_lock(&bus->core.cmd_mutex);
72 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
73 bus->no_response_fallback = 1;
74 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
76 bus->no_response_fallback = 0;
77 mutex_unlock(&bus->core.cmd_mutex);
78 snd_hda_power_down_pm(codec);
79 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
80 if (bus->response_reset) {
82 "resetting BUS due to fatal communication error\n");
83 snd_hda_bus_reset(bus);
87 /* clear reset-flag when the communication gets recovered */
88 if (!err || codec_in_pm(codec))
89 bus->response_reset = 0;
94 * snd_hda_codec_read - send a command and get the response
95 * @codec: the HDA codec
96 * @nid: NID to send the command
97 * @flags: optional bit flags
98 * @verb: the verb to send
99 * @parm: the parameter for the verb
101 * Send a single command and read the corresponding response.
103 * Returns the obtained response value, or -1 for an error.
105 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
107 unsigned int verb, unsigned int parm)
109 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
111 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
115 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
118 * snd_hda_codec_write - send a single command without waiting for response
119 * @codec: the HDA codec
120 * @nid: NID to send the command
121 * @flags: optional bit flags
122 * @verb: the verb to send
123 * @parm: the parameter for the verb
125 * Send a single command without waiting for response.
127 * Returns 0 if successful, or a negative error code.
129 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
130 unsigned int verb, unsigned int parm)
132 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
133 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
135 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
138 * snd_hda_sequence_write - sequence writes
139 * @codec: the HDA codec
140 * @seq: VERB array to send
142 * Send the commands sequentially from the given array.
143 * The array must be terminated with NID=0.
145 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
147 for (; seq->nid; seq++)
148 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
150 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
152 /* connection list element */
153 struct hda_conn_list {
154 struct list_head list;
160 /* look up the cached results */
161 static struct hda_conn_list *
162 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
164 struct hda_conn_list *p;
165 list_for_each_entry(p, &codec->conn_list, list) {
172 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
173 const hda_nid_t *list)
175 struct hda_conn_list *p;
177 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
182 memcpy(p->conns, list, len * sizeof(hda_nid_t));
183 list_add(&p->list, &codec->conn_list);
187 static void remove_conn_list(struct hda_codec *codec)
189 while (!list_empty(&codec->conn_list)) {
190 struct hda_conn_list *p;
191 p = list_first_entry(&codec->conn_list, typeof(*p), list);
197 /* read the connection and add to the cache */
198 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
201 hda_nid_t *result = list;
204 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
205 if (len == -ENOSPC) {
206 len = snd_hda_get_num_raw_conns(codec, nid);
207 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
210 len = snd_hda_get_raw_connections(codec, nid, result, len);
213 len = snd_hda_override_conn_list(codec, nid, len, result);
220 * snd_hda_get_conn_list - get connection list
221 * @codec: the HDA codec
223 * @listp: the pointer to store NID list
225 * Parses the connection list of the given widget and stores the pointer
226 * to the list of NIDs.
228 * Returns the number of connections, or a negative error code.
230 * Note that the returned pointer isn't protected against the list
231 * modification. If snd_hda_override_conn_list() might be called
232 * concurrently, protect with a mutex appropriately.
234 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
235 const hda_nid_t **listp)
241 const struct hda_conn_list *p;
243 /* if the connection-list is already cached, read it */
244 p = lookup_conn_list(codec, nid);
250 if (snd_BUG_ON(added))
253 err = read_and_add_raw_conns(codec, nid);
259 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
262 * snd_hda_get_connections - copy connection list
263 * @codec: the HDA codec
265 * @conn_list: connection list array; when NULL, checks only the size
266 * @max_conns: max. number of connections to store
268 * Parses the connection list of the given widget and stores the list
271 * Returns the number of connections, or a negative error code.
273 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
274 hda_nid_t *conn_list, int max_conns)
276 const hda_nid_t *list;
277 int len = snd_hda_get_conn_list(codec, nid, &list);
279 if (len > 0 && conn_list) {
280 if (len > max_conns) {
281 codec_err(codec, "Too many connections %d for NID 0x%x\n",
285 memcpy(conn_list, list, len * sizeof(hda_nid_t));
290 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
293 * snd_hda_override_conn_list - add/modify the connection-list to cache
294 * @codec: the HDA codec
296 * @len: number of connection list entries
297 * @list: the list of connection entries
299 * Add or modify the given connection-list to the cache. If the corresponding
300 * cache already exists, invalidate it and append a new one.
302 * Returns zero or a negative error code.
304 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
305 const hda_nid_t *list)
307 struct hda_conn_list *p;
309 p = lookup_conn_list(codec, nid);
315 return add_conn_list(codec, nid, len, list);
317 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
320 * snd_hda_get_conn_index - get the connection index of the given NID
321 * @codec: the HDA codec
322 * @mux: NID containing the list
323 * @nid: NID to select
324 * @recursive: 1 when searching NID recursively, otherwise 0
326 * Parses the connection list of the widget @mux and checks whether the
327 * widget @nid is present. If it is, return the connection index.
328 * Otherwise it returns -1.
330 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
331 hda_nid_t nid, int recursive)
333 const hda_nid_t *conn;
336 nums = snd_hda_get_conn_list(codec, mux, &conn);
337 for (i = 0; i < nums; i++)
342 if (recursive > 10) {
343 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
347 for (i = 0; i < nums; i++) {
348 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
349 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
351 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
356 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
359 /* return DEVLIST_LEN parameter of the given widget */
360 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
362 unsigned int wcaps = get_wcaps(codec, nid);
365 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
366 get_wcaps_type(wcaps) != AC_WID_PIN)
369 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
372 return parm & AC_DEV_LIST_LEN_MASK;
376 * snd_hda_get_devices - copy device list without cache
377 * @codec: the HDA codec
378 * @nid: NID of the pin to parse
379 * @dev_list: device list array
380 * @max_devices: max. number of devices to store
382 * Copy the device list. This info is dynamic and so not cached.
383 * Currently called only from hda_proc.c, so not exported.
385 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
386 u8 *dev_list, int max_devices)
389 int i, dev_len, devices;
391 parm = get_num_devices(codec, nid);
392 if (!parm) /* not multi-stream capable */
396 dev_len = dev_len < max_devices ? dev_len : max_devices;
399 while (devices < dev_len) {
400 if (snd_hdac_read(&codec->core, nid,
401 AC_VERB_GET_DEVICE_LIST, devices, &parm))
404 for (i = 0; i < 8; i++) {
405 dev_list[devices] = (u8)parm;
408 if (devices >= dev_len)
416 * read widget caps for each widget and store in cache
418 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
423 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
426 nid = codec->core.start_nid;
427 for (i = 0; i < codec->core.num_nodes; i++, nid++)
428 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
429 nid, AC_PAR_AUDIO_WIDGET_CAP);
433 /* read all pin default configurations and save codec->init_pins */
434 static int read_pin_defaults(struct hda_codec *codec)
438 for_each_hda_codec_node(nid, codec) {
439 struct hda_pincfg *pin;
440 unsigned int wcaps = get_wcaps(codec, nid);
441 unsigned int wid_type = get_wcaps_type(wcaps);
442 if (wid_type != AC_WID_PIN)
444 pin = snd_array_new(&codec->init_pins);
448 pin->cfg = snd_hda_codec_read(codec, nid, 0,
449 AC_VERB_GET_CONFIG_DEFAULT, 0);
450 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
451 AC_VERB_GET_PIN_WIDGET_CONTROL,
457 /* look up the given pin config list and return the item matching with NID */
458 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
459 struct snd_array *array,
463 for (i = 0; i < array->used; i++) {
464 struct hda_pincfg *pin = snd_array_elem(array, i);
471 /* set the current pin config value for the given NID.
472 * the value is cached, and read via snd_hda_codec_get_pincfg()
474 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
475 hda_nid_t nid, unsigned int cfg)
477 struct hda_pincfg *pin;
479 /* the check below may be invalid when pins are added by a fixup
480 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
484 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
488 pin = look_up_pincfg(codec, list, nid);
490 pin = snd_array_new(list);
500 * snd_hda_codec_set_pincfg - Override a pin default configuration
501 * @codec: the HDA codec
502 * @nid: NID to set the pin config
503 * @cfg: the pin default config value
505 * Override a pin default configuration value in the cache.
506 * This value can be read by snd_hda_codec_get_pincfg() in a higher
507 * priority than the real hardware value.
509 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
510 hda_nid_t nid, unsigned int cfg)
512 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
514 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
517 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
518 * @codec: the HDA codec
519 * @nid: NID to get the pin config
521 * Get the current pin config value of the given pin NID.
522 * If the pincfg value is cached or overridden via sysfs or driver,
523 * returns the cached value.
525 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
527 struct hda_pincfg *pin;
529 #ifdef CONFIG_SND_HDA_RECONFIG
531 unsigned int cfg = 0;
532 mutex_lock(&codec->user_mutex);
533 pin = look_up_pincfg(codec, &codec->user_pins, nid);
536 mutex_unlock(&codec->user_mutex);
541 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
544 pin = look_up_pincfg(codec, &codec->init_pins, nid);
549 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
552 * snd_hda_codec_set_pin_target - remember the current pinctl target value
553 * @codec: the HDA codec
555 * @val: assigned pinctl value
557 * This function stores the given value to a pinctl target value in the
558 * pincfg table. This isn't always as same as the actually written value
559 * but can be referred at any time via snd_hda_codec_get_pin_target().
561 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
564 struct hda_pincfg *pin;
566 pin = look_up_pincfg(codec, &codec->init_pins, nid);
572 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
575 * snd_hda_codec_get_pin_target - return the current pinctl target value
576 * @codec: the HDA codec
579 int snd_hda_codec_get_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);
588 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
591 * snd_hda_shutup_pins - Shut up all pins
592 * @codec: the HDA codec
594 * Clear all pin controls to shup up before suspend for avoiding click noise.
595 * The controls aren't cached so that they can be resumed properly.
597 void snd_hda_shutup_pins(struct hda_codec *codec)
600 /* don't shut up pins when unloading the driver; otherwise it breaks
601 * the default pin setup at the next load of the driver
603 if (codec->bus->shutdown)
605 for (i = 0; i < codec->init_pins.used; i++) {
606 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
607 /* use read here for syncing after issuing each verb */
608 snd_hda_codec_read(codec, pin->nid, 0,
609 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
611 codec->pins_shutup = 1;
613 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
616 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
617 static void restore_shutup_pins(struct hda_codec *codec)
620 if (!codec->pins_shutup)
622 if (codec->bus->shutdown)
624 for (i = 0; i < codec->init_pins.used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
626 snd_hda_codec_write(codec, pin->nid, 0,
627 AC_VERB_SET_PIN_WIDGET_CONTROL,
630 codec->pins_shutup = 0;
634 static void hda_jackpoll_work(struct work_struct *work)
636 struct hda_codec *codec =
637 container_of(work, struct hda_codec, jackpoll_work.work);
639 snd_hda_jack_set_dirty_all(codec);
640 snd_hda_jack_poll_all(codec);
642 if (!codec->jackpoll_interval)
645 schedule_delayed_work(&codec->jackpoll_work,
646 codec->jackpoll_interval);
649 /* release all pincfg lists */
650 static void free_init_pincfgs(struct hda_codec *codec)
652 snd_array_free(&codec->driver_pins);
653 #ifdef CONFIG_SND_HDA_RECONFIG
654 snd_array_free(&codec->user_pins);
656 snd_array_free(&codec->init_pins);
660 * audio-converter setup caches
662 struct hda_cvt_setup {
667 unsigned char active; /* cvt is currently used */
668 unsigned char dirty; /* setups should be cleared */
671 /* get or create a cache entry for the given audio converter NID */
672 static struct hda_cvt_setup *
673 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
675 struct hda_cvt_setup *p;
678 for (i = 0; i < codec->cvt_setups.used; i++) {
679 p = snd_array_elem(&codec->cvt_setups, i);
683 p = snd_array_new(&codec->cvt_setups);
692 static void release_pcm(struct kref *kref)
694 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
697 snd_device_free(pcm->codec->card, pcm->pcm);
698 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
703 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
705 kref_put(&pcm->kref, release_pcm);
707 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
709 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
710 const char *fmt, ...)
715 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
720 kref_init(&pcm->kref);
722 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
729 list_add_tail(&pcm->list, &codec->pcm_list_head);
732 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
737 static void codec_release_pcms(struct hda_codec *codec)
739 struct hda_pcm *pcm, *n;
741 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
742 list_del_init(&pcm->list);
744 snd_device_disconnect(codec->card, pcm->pcm);
745 snd_hda_codec_pcm_put(pcm);
749 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
751 if (codec->registered) {
752 /* pm_runtime_put() is called in snd_hdac_device_exit() */
753 pm_runtime_get_noresume(hda_codec_dev(codec));
754 pm_runtime_disable(hda_codec_dev(codec));
755 codec->registered = 0;
758 cancel_delayed_work_sync(&codec->jackpoll_work);
759 if (!codec->in_freeing)
760 snd_hda_ctls_clear(codec);
761 codec_release_pcms(codec);
762 snd_hda_detach_beep_device(codec);
763 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
764 snd_hda_jack_tbl_clear(codec);
765 codec->proc_widget_hook = NULL;
768 /* free only driver_pins so that init_pins + user_pins are restored */
769 snd_array_free(&codec->driver_pins);
770 snd_array_free(&codec->cvt_setups);
771 snd_array_free(&codec->spdif_out);
772 snd_array_free(&codec->verbs);
773 codec->preset = NULL;
774 codec->slave_dig_outs = NULL;
775 codec->spdif_status_reset = 0;
776 snd_array_free(&codec->mixers);
777 snd_array_free(&codec->nids);
778 remove_conn_list(codec);
779 snd_hdac_regmap_exit(&codec->core);
782 static unsigned int hda_set_power_state(struct hda_codec *codec,
783 unsigned int power_state);
785 /* also called from hda_bind.c */
786 void snd_hda_codec_register(struct hda_codec *codec)
788 if (codec->registered)
790 if (device_is_registered(hda_codec_dev(codec))) {
791 snd_hda_register_beep_device(codec);
792 snd_hdac_link_power(&codec->core, true);
793 pm_runtime_enable(hda_codec_dev(codec));
794 /* it was powered up in snd_hda_codec_new(), now all done */
795 snd_hda_power_down(codec);
796 codec->registered = 1;
800 static int snd_hda_codec_dev_register(struct snd_device *device)
802 snd_hda_codec_register(device->device_data);
806 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
808 struct hda_codec *codec = device->device_data;
810 snd_hda_detach_beep_device(codec);
814 static int snd_hda_codec_dev_free(struct snd_device *device)
816 struct hda_codec *codec = device->device_data;
818 codec->in_freeing = 1;
819 snd_hdac_device_unregister(&codec->core);
820 snd_hdac_link_power(&codec->core, false);
821 put_device(hda_codec_dev(codec));
825 static void snd_hda_codec_dev_release(struct device *dev)
827 struct hda_codec *codec = dev_to_hda_codec(dev);
829 free_init_pincfgs(codec);
830 snd_hdac_device_exit(&codec->core);
831 snd_hda_sysfs_clear(codec);
832 kfree(codec->modelname);
838 * snd_hda_codec_new - create a HDA codec
839 * @bus: the bus to assign
840 * @codec_addr: the codec address
841 * @codecp: the pointer to store the generated codec
843 * Returns 0 if successful, or a negative error code.
845 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
846 unsigned int codec_addr, struct hda_codec **codecp)
848 struct hda_codec *codec;
852 static struct snd_device_ops dev_ops = {
853 .dev_register = snd_hda_codec_dev_register,
854 .dev_disconnect = snd_hda_codec_dev_disconnect,
855 .dev_free = snd_hda_codec_dev_free,
858 if (snd_BUG_ON(!bus))
860 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
863 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
867 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
868 err = snd_hdac_device_init(&codec->core, &bus->core, component,
875 codec->core.dev.release = snd_hda_codec_dev_release;
876 codec->core.type = HDA_DEV_LEGACY;
877 codec->core.exec_verb = codec_exec_verb;
881 codec->addr = codec_addr;
882 mutex_init(&codec->spdif_mutex);
883 mutex_init(&codec->control_mutex);
884 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
885 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
886 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
887 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
888 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
889 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
890 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
891 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
892 INIT_LIST_HEAD(&codec->conn_list);
893 INIT_LIST_HEAD(&codec->pcm_list_head);
895 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
896 codec->depop_delay = -1;
897 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
900 codec->power_jiffies = jiffies;
903 snd_hda_sysfs_init(codec);
905 if (codec->bus->modelname) {
906 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
907 if (!codec->modelname) {
913 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
914 err = read_widget_caps(codec, fg);
917 err = read_pin_defaults(codec);
921 /* power-up all before initialization */
922 hda_set_power_state(codec, AC_PWRST_D0);
924 snd_hda_codec_proc_new(codec);
926 snd_hda_create_hwdep(codec);
928 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
929 codec->core.subsystem_id, codec->core.revision_id);
930 snd_component_add(card, component);
932 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
941 put_device(hda_codec_dev(codec));
944 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
947 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
948 * @codec: the HDA codec
950 * Forcibly refresh the all widget caps and the init pin configurations of
953 int snd_hda_codec_update_widgets(struct hda_codec *codec)
958 err = snd_hdac_refresh_widgets(&codec->core);
962 /* Assume the function group node does not change,
963 * only the widget nodes may change.
966 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
967 err = read_widget_caps(codec, fg);
971 snd_array_free(&codec->init_pins);
972 err = read_pin_defaults(codec);
976 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
978 /* update the stream-id if changed */
979 static void update_pcm_stream_id(struct hda_codec *codec,
980 struct hda_cvt_setup *p, hda_nid_t nid,
981 u32 stream_tag, int channel_id)
983 unsigned int oldval, newval;
985 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
986 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
987 newval = (stream_tag << 4) | channel_id;
988 if (oldval != newval)
989 snd_hda_codec_write(codec, nid, 0,
990 AC_VERB_SET_CHANNEL_STREAMID,
992 p->stream_tag = stream_tag;
993 p->channel_id = channel_id;
997 /* update the format-id if changed */
998 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
999 hda_nid_t nid, int format)
1001 unsigned int oldval;
1003 if (p->format_id != format) {
1004 oldval = snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_STREAM_FORMAT, 0);
1006 if (oldval != format) {
1008 snd_hda_codec_write(codec, nid, 0,
1009 AC_VERB_SET_STREAM_FORMAT,
1012 p->format_id = format;
1017 * snd_hda_codec_setup_stream - set up the codec for streaming
1018 * @codec: the CODEC to set up
1019 * @nid: the NID to set up
1020 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1021 * @channel_id: channel id to pass, zero based.
1022 * @format: stream format.
1024 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1026 int channel_id, int format)
1028 struct hda_codec *c;
1029 struct hda_cvt_setup *p;
1037 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1038 nid, stream_tag, channel_id, format);
1039 p = get_hda_cvt_setup(codec, nid);
1043 if (codec->patch_ops.stream_pm)
1044 codec->patch_ops.stream_pm(codec, nid, true);
1045 if (codec->pcm_format_first)
1046 update_pcm_format(codec, p, nid, format);
1047 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1048 if (!codec->pcm_format_first)
1049 update_pcm_format(codec, p, nid, format);
1054 /* make other inactive cvts with the same stream-tag dirty */
1055 type = get_wcaps_type(get_wcaps(codec, nid));
1056 list_for_each_codec(c, codec->bus) {
1057 for (i = 0; i < c->cvt_setups.used; i++) {
1058 p = snd_array_elem(&c->cvt_setups, i);
1059 if (!p->active && p->stream_tag == stream_tag &&
1060 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1065 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1067 static void really_cleanup_stream(struct hda_codec *codec,
1068 struct hda_cvt_setup *q);
1071 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1072 * @codec: the CODEC to clean up
1073 * @nid: the NID to clean up
1074 * @do_now: really clean up the stream instead of clearing the active flag
1076 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1079 struct hda_cvt_setup *p;
1084 if (codec->no_sticky_stream)
1087 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1088 p = get_hda_cvt_setup(codec, nid);
1090 /* here we just clear the active flag when do_now isn't set;
1091 * actual clean-ups will be done later in
1092 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1095 really_cleanup_stream(codec, p);
1100 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1102 static void really_cleanup_stream(struct hda_codec *codec,
1103 struct hda_cvt_setup *q)
1105 hda_nid_t nid = q->nid;
1106 if (q->stream_tag || q->channel_id)
1107 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1109 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1111 memset(q, 0, sizeof(*q));
1113 if (codec->patch_ops.stream_pm)
1114 codec->patch_ops.stream_pm(codec, nid, false);
1117 /* clean up the all conflicting obsolete streams */
1118 static void purify_inactive_streams(struct hda_codec *codec)
1120 struct hda_codec *c;
1123 list_for_each_codec(c, codec->bus) {
1124 for (i = 0; i < c->cvt_setups.used; i++) {
1125 struct hda_cvt_setup *p;
1126 p = snd_array_elem(&c->cvt_setups, i);
1128 really_cleanup_stream(c, p);
1134 /* clean up all streams; called from suspend */
1135 static void hda_cleanup_all_streams(struct hda_codec *codec)
1139 for (i = 0; i < codec->cvt_setups.used; i++) {
1140 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1142 really_cleanup_stream(codec, p);
1148 * amp access functions
1152 * query_amp_caps - query AMP capabilities
1153 * @codec: the HD-auio codec
1154 * @nid: the NID to query
1155 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1157 * Query AMP capabilities for the given widget and direction.
1158 * Returns the obtained capability bits.
1160 * When cap bits have been already read, this doesn't read again but
1161 * returns the cached value.
1163 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1165 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1166 nid = codec->core.afg;
1167 return snd_hda_param_read(codec, nid,
1168 direction == HDA_OUTPUT ?
1169 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1171 EXPORT_SYMBOL_GPL(query_amp_caps);
1174 * snd_hda_check_amp_caps - query AMP capabilities
1175 * @codec: the HD-audio codec
1176 * @nid: the NID to query
1177 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1178 * @bits: bit mask to check the result
1180 * Check whether the widget has the given amp capability for the direction.
1182 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1183 int dir, unsigned int bits)
1187 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1188 if (query_amp_caps(codec, nid, dir) & bits)
1192 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1195 * snd_hda_override_amp_caps - Override the AMP capabilities
1196 * @codec: the CODEC to clean up
1197 * @nid: the NID to clean up
1198 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1199 * @caps: the capability bits to set
1201 * Override the cached AMP caps bits value by the given one.
1202 * This function is useful if the driver needs to adjust the AMP ranges,
1203 * e.g. limit to 0dB, etc.
1205 * Returns zero if successful or a negative error code.
1207 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1212 snd_hda_override_wcaps(codec, nid,
1213 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1214 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1215 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1217 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1220 * snd_hda_codec_amp_update - update the AMP mono value
1221 * @codec: HD-audio codec
1222 * @nid: NID to read the AMP value
1223 * @ch: channel to update (0 or 1)
1224 * @dir: #HDA_INPUT or #HDA_OUTPUT
1225 * @idx: the index value (only for input direction)
1226 * @mask: bit mask to set
1227 * @val: the bits value to set
1229 * Update the AMP values for the given channel, direction and index.
1231 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1232 int ch, int dir, int idx, int mask, int val)
1234 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1236 /* enable fake mute if no h/w mute but min=mute */
1237 if ((query_amp_caps(codec, nid, dir) &
1238 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1239 cmd |= AC_AMP_FAKE_MUTE;
1240 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1242 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1245 * snd_hda_codec_amp_stereo - update the AMP stereo values
1246 * @codec: HD-audio codec
1247 * @nid: NID to read the AMP value
1248 * @direction: #HDA_INPUT or #HDA_OUTPUT
1249 * @idx: the index value (only for input direction)
1250 * @mask: bit mask to set
1251 * @val: the bits value to set
1253 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1254 * stereo widget with the same mask and value.
1256 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1257 int direction, int idx, int mask, int val)
1261 if (snd_BUG_ON(mask & ~0xff))
1263 for (ch = 0; ch < 2; ch++)
1264 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1268 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1271 * snd_hda_codec_amp_init - initialize the AMP value
1272 * @codec: the HDA codec
1273 * @nid: NID to read the AMP value
1274 * @ch: channel (left=0 or right=1)
1275 * @dir: #HDA_INPUT or #HDA_OUTPUT
1276 * @idx: the index value (only for input direction)
1277 * @mask: bit mask to set
1278 * @val: the bits value to set
1280 * Works like snd_hda_codec_amp_update() but it writes the value only at
1281 * the first access. If the amp was already initialized / updated beforehand,
1282 * this does nothing.
1284 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1285 int dir, int idx, int mask, int val)
1289 if (!codec->core.regmap)
1291 regcache_cache_only(codec->core.regmap, true);
1292 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1293 regcache_cache_only(codec->core.regmap, false);
1296 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1298 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1301 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1302 * @codec: the HDA codec
1303 * @nid: NID to read the AMP value
1304 * @dir: #HDA_INPUT or #HDA_OUTPUT
1305 * @idx: the index value (only for input direction)
1306 * @mask: bit mask to set
1307 * @val: the bits value to set
1309 * Call snd_hda_codec_amp_init() for both stereo channels.
1311 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1312 int dir, int idx, int mask, int val)
1316 if (snd_BUG_ON(mask & ~0xff))
1318 for (ch = 0; ch < 2; ch++)
1319 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1323 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1325 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1328 u32 caps = query_amp_caps(codec, nid, dir);
1330 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1337 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1338 * @kcontrol: referred ctl element
1339 * @uinfo: pointer to get/store the data
1341 * The control element is supposed to have the private_value field
1342 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1344 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1345 struct snd_ctl_elem_info *uinfo)
1347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1348 u16 nid = get_amp_nid(kcontrol);
1349 u8 chs = get_amp_channels(kcontrol);
1350 int dir = get_amp_direction(kcontrol);
1351 unsigned int ofs = get_amp_offset(kcontrol);
1353 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1354 uinfo->count = chs == 3 ? 2 : 1;
1355 uinfo->value.integer.min = 0;
1356 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1357 if (!uinfo->value.integer.max) {
1359 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1360 nid, kcontrol->id.name);
1365 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1368 static inline unsigned int
1369 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1370 int ch, int dir, int idx, unsigned int ofs)
1373 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1374 val &= HDA_AMP_VOLMASK;
1383 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1384 int ch, int dir, int idx, unsigned int ofs,
1387 unsigned int maxval;
1391 /* ofs = 0: raw max value */
1392 maxval = get_amp_max_value(codec, nid, dir, 0);
1395 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1396 HDA_AMP_VOLMASK, val);
1400 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1401 * @kcontrol: ctl element
1402 * @ucontrol: pointer to get/store the data
1404 * The control element is supposed to have the private_value field
1405 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1407 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_value *ucontrol)
1410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1411 hda_nid_t nid = get_amp_nid(kcontrol);
1412 int chs = get_amp_channels(kcontrol);
1413 int dir = get_amp_direction(kcontrol);
1414 int idx = get_amp_index(kcontrol);
1415 unsigned int ofs = get_amp_offset(kcontrol);
1416 long *valp = ucontrol->value.integer.value;
1419 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1421 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1424 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1427 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1428 * @kcontrol: ctl element
1429 * @ucontrol: pointer to get/store the data
1431 * The control element is supposed to have the private_value field
1432 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1434 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_value *ucontrol)
1437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1438 hda_nid_t nid = get_amp_nid(kcontrol);
1439 int chs = get_amp_channels(kcontrol);
1440 int dir = get_amp_direction(kcontrol);
1441 int idx = get_amp_index(kcontrol);
1442 unsigned int ofs = get_amp_offset(kcontrol);
1443 long *valp = ucontrol->value.integer.value;
1447 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1451 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1454 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1457 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1458 * @kcontrol: ctl element
1459 * @op_flag: operation flag
1460 * @size: byte size of input TLV
1463 * The control element is supposed to have the private_value field
1464 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1466 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1467 unsigned int size, unsigned int __user *_tlv)
1469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1470 hda_nid_t nid = get_amp_nid(kcontrol);
1471 int dir = get_amp_direction(kcontrol);
1472 unsigned int ofs = get_amp_offset(kcontrol);
1473 bool min_mute = get_amp_min_mute(kcontrol);
1474 u32 caps, val1, val2;
1476 if (size < 4 * sizeof(unsigned int))
1478 caps = query_amp_caps(codec, nid, dir);
1479 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1480 val2 = (val2 + 1) * 25;
1481 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1483 val1 = ((int)val1) * ((int)val2);
1484 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1485 val2 |= TLV_DB_SCALE_MUTE;
1486 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1488 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1490 if (put_user(val1, _tlv + 2))
1492 if (put_user(val2, _tlv + 3))
1496 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1499 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1500 * @codec: HD-audio codec
1501 * @nid: NID of a reference widget
1502 * @dir: #HDA_INPUT or #HDA_OUTPUT
1503 * @tlv: TLV data to be stored, at least 4 elements
1505 * Set (static) TLV data for a virtual master volume using the AMP caps
1506 * obtained from the reference NID.
1507 * The volume range is recalculated as if the max volume is 0dB.
1509 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1515 caps = query_amp_caps(codec, nid, dir);
1516 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1517 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1518 step = (step + 1) * 25;
1519 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1520 tlv[1] = 2 * sizeof(unsigned int);
1521 tlv[2] = -nums * step;
1524 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1526 /* find a mixer control element with the given name */
1527 static struct snd_kcontrol *
1528 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1530 struct snd_ctl_elem_id id;
1531 memset(&id, 0, sizeof(id));
1532 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1535 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1537 strcpy(id.name, name);
1538 return snd_ctl_find_id(codec->card, &id);
1542 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1543 * @codec: HD-audio codec
1544 * @name: ctl id name string
1546 * Get the control element with the given id string and IFACE_MIXER.
1548 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1551 return find_mixer_ctl(codec, name, 0, 0);
1553 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1555 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1559 /* 16 ctlrs should be large enough */
1560 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1561 if (!find_mixer_ctl(codec, name, 0, idx))
1568 * snd_hda_ctl_add - Add a control element and assign to the codec
1569 * @codec: HD-audio codec
1570 * @nid: corresponding NID (optional)
1571 * @kctl: the control element to assign
1573 * Add the given control element to an array inside the codec instance.
1574 * All control elements belonging to a codec are supposed to be added
1575 * by this function so that a proper clean-up works at the free or
1576 * reconfiguration time.
1578 * If non-zero @nid is passed, the NID is assigned to the control element.
1579 * The assignment is shown in the codec proc file.
1581 * snd_hda_ctl_add() checks the control subdev id field whether
1582 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1583 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1584 * specifies if kctl->private_value is a HDA amplifier value.
1586 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1587 struct snd_kcontrol *kctl)
1590 unsigned short flags = 0;
1591 struct hda_nid_item *item;
1593 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1594 flags |= HDA_NID_ITEM_AMP;
1596 nid = get_amp_nid_(kctl->private_value);
1598 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1599 nid = kctl->id.subdevice & 0xffff;
1600 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1601 kctl->id.subdevice = 0;
1602 err = snd_ctl_add(codec->card, kctl);
1605 item = snd_array_new(&codec->mixers);
1610 item->flags = flags;
1613 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1616 * snd_hda_add_nid - Assign a NID to a control element
1617 * @codec: HD-audio codec
1618 * @nid: corresponding NID (optional)
1619 * @kctl: the control element to assign
1620 * @index: index to kctl
1622 * Add the given control element to an array inside the codec instance.
1623 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1624 * NID:KCTL mapping - for example "Capture Source" selector.
1626 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1627 unsigned int index, hda_nid_t nid)
1629 struct hda_nid_item *item;
1632 item = snd_array_new(&codec->nids);
1636 item->index = index;
1640 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1641 kctl->id.name, kctl->id.index, index);
1644 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1647 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1648 * @codec: HD-audio codec
1650 void snd_hda_ctls_clear(struct hda_codec *codec)
1653 struct hda_nid_item *items = codec->mixers.list;
1654 for (i = 0; i < codec->mixers.used; i++)
1655 snd_ctl_remove(codec->card, items[i].kctl);
1656 snd_array_free(&codec->mixers);
1657 snd_array_free(&codec->nids);
1661 * snd_hda_lock_devices - pseudo device locking
1664 * toggle card->shutdown to allow/disallow the device access (as a hack)
1666 int snd_hda_lock_devices(struct hda_bus *bus)
1668 struct snd_card *card = bus->card;
1669 struct hda_codec *codec;
1671 spin_lock(&card->files_lock);
1675 if (!list_empty(&card->ctl_files))
1678 list_for_each_codec(codec, bus) {
1679 struct hda_pcm *cpcm;
1680 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1683 if (cpcm->pcm->streams[0].substream_opened ||
1684 cpcm->pcm->streams[1].substream_opened)
1688 spin_unlock(&card->files_lock);
1694 spin_unlock(&card->files_lock);
1697 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1700 * snd_hda_unlock_devices - pseudo device unlocking
1703 void snd_hda_unlock_devices(struct hda_bus *bus)
1705 struct snd_card *card = bus->card;
1707 spin_lock(&card->files_lock);
1709 spin_unlock(&card->files_lock);
1711 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1714 * snd_hda_codec_reset - Clear all objects assigned to the codec
1715 * @codec: HD-audio codec
1717 * This frees the all PCM and control elements assigned to the codec, and
1718 * clears the caches and restores the pin default configurations.
1720 * When a device is being used, it returns -EBSY. If successfully freed,
1723 int snd_hda_codec_reset(struct hda_codec *codec)
1725 struct hda_bus *bus = codec->bus;
1727 if (snd_hda_lock_devices(bus) < 0)
1730 /* OK, let it free */
1731 snd_hdac_device_unregister(&codec->core);
1733 /* allow device access again */
1734 snd_hda_unlock_devices(bus);
1738 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1740 /* apply the function to all matching slave ctls in the mixer list */
1741 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1742 const char *suffix, map_slave_func_t func, void *data)
1744 struct hda_nid_item *items;
1745 const char * const *s;
1748 items = codec->mixers.list;
1749 for (i = 0; i < codec->mixers.used; i++) {
1750 struct snd_kcontrol *sctl = items[i].kctl;
1751 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1753 for (s = slaves; *s; s++) {
1754 char tmpname[sizeof(sctl->id.name)];
1755 const char *name = *s;
1757 snprintf(tmpname, sizeof(tmpname), "%s %s",
1761 if (!strcmp(sctl->id.name, name)) {
1762 err = func(codec, data, sctl);
1772 static int check_slave_present(struct hda_codec *codec,
1773 void *data, struct snd_kcontrol *sctl)
1778 /* guess the value corresponding to 0dB */
1779 static int get_kctl_0dB_offset(struct hda_codec *codec,
1780 struct snd_kcontrol *kctl, int *step_to_check)
1783 const int *tlv = NULL;
1786 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1787 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1788 mm_segment_t fs = get_fs();
1790 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1793 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1795 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1797 step &= ~TLV_DB_SCALE_MUTE;
1800 if (*step_to_check && *step_to_check != step) {
1801 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1802 - *step_to_check, step);
1805 *step_to_check = step;
1806 val = -tlv[2] / step;
1811 /* call kctl->put with the given value(s) */
1812 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1814 struct snd_ctl_elem_value *ucontrol;
1815 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1818 ucontrol->value.integer.value[0] = val;
1819 ucontrol->value.integer.value[1] = val;
1820 kctl->put(kctl, ucontrol);
1825 /* initialize the slave volume with 0dB */
1826 static int init_slave_0dB(struct hda_codec *codec,
1827 void *data, struct snd_kcontrol *slave)
1829 int offset = get_kctl_0dB_offset(codec, slave, data);
1831 put_kctl_with_value(slave, offset);
1835 /* unmute the slave */
1836 static int init_slave_unmute(struct hda_codec *codec,
1837 void *data, struct snd_kcontrol *slave)
1839 return put_kctl_with_value(slave, 1);
1842 static int add_slave(struct hda_codec *codec,
1843 void *data, struct snd_kcontrol *slave)
1845 return snd_ctl_add_slave(data, slave);
1849 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1850 * @codec: HD-audio codec
1851 * @name: vmaster control name
1852 * @tlv: TLV data (optional)
1853 * @slaves: slave control names (optional)
1854 * @suffix: suffix string to each slave name (optional)
1855 * @init_slave_vol: initialize slaves to unmute/0dB
1856 * @ctl_ret: store the vmaster kcontrol in return
1858 * Create a virtual master control with the given name. The TLV data
1859 * must be either NULL or a valid data.
1861 * @slaves is a NULL-terminated array of strings, each of which is a
1862 * slave control name. All controls with these names are assigned to
1863 * the new virtual master control.
1865 * This function returns zero if successful or a negative error code.
1867 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1868 unsigned int *tlv, const char * const *slaves,
1869 const char *suffix, bool init_slave_vol,
1870 struct snd_kcontrol **ctl_ret)
1872 struct snd_kcontrol *kctl;
1878 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1880 codec_dbg(codec, "No slave found for %s\n", name);
1883 kctl = snd_ctl_make_virtual_master(name, tlv);
1886 err = snd_hda_ctl_add(codec, 0, kctl);
1890 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1894 /* init with master mute & zero volume */
1895 put_kctl_with_value(kctl, 0);
1896 if (init_slave_vol) {
1898 map_slaves(codec, slaves, suffix,
1899 tlv ? init_slave_0dB : init_slave_unmute, &step);
1906 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1909 * mute-LED control using vmaster
1911 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_info *uinfo)
1914 static const char * const texts[] = {
1915 "On", "Off", "Follow Master"
1918 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1921 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1922 struct snd_ctl_elem_value *ucontrol)
1924 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1925 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1929 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *ucontrol)
1932 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1933 unsigned int old_mode = hook->mute_mode;
1935 hook->mute_mode = ucontrol->value.enumerated.item[0];
1936 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1937 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1938 if (old_mode == hook->mute_mode)
1940 snd_hda_sync_vmaster_hook(hook);
1944 static struct snd_kcontrol_new vmaster_mute_mode = {
1945 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1946 .name = "Mute-LED Mode",
1947 .info = vmaster_mute_mode_info,
1948 .get = vmaster_mute_mode_get,
1949 .put = vmaster_mute_mode_put,
1952 /* meta hook to call each driver's vmaster hook */
1953 static void vmaster_hook(void *private_data, int enabled)
1955 struct hda_vmaster_mute_hook *hook = private_data;
1957 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1958 enabled = hook->mute_mode;
1959 hook->hook(hook->codec, enabled);
1963 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1964 * @codec: the HDA codec
1965 * @hook: the vmaster hook object
1966 * @expose_enum_ctl: flag to create an enum ctl
1968 * Add a mute-LED hook with the given vmaster switch kctl.
1969 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1970 * created and associated with the given hook.
1972 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
1973 struct hda_vmaster_mute_hook *hook,
1974 bool expose_enum_ctl)
1976 struct snd_kcontrol *kctl;
1978 if (!hook->hook || !hook->sw_kctl)
1980 hook->codec = codec;
1981 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1982 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
1983 if (!expose_enum_ctl)
1985 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1988 return snd_hda_ctl_add(codec, 0, kctl);
1990 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
1993 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1994 * @hook: the vmaster hook
1996 * Call the hook with the current value for synchronization.
1997 * Should be called in init callback.
1999 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2001 if (!hook->hook || !hook->codec)
2003 /* don't call vmaster hook in the destructor since it might have
2004 * been already destroyed
2006 if (hook->codec->bus->shutdown)
2008 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2010 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2014 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2015 * @kcontrol: referred ctl element
2016 * @uinfo: pointer to get/store the data
2018 * The control element is supposed to have the private_value field
2019 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2021 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_info *uinfo)
2024 int chs = get_amp_channels(kcontrol);
2026 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2027 uinfo->count = chs == 3 ? 2 : 1;
2028 uinfo->value.integer.min = 0;
2029 uinfo->value.integer.max = 1;
2032 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2035 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2036 * @kcontrol: ctl element
2037 * @ucontrol: pointer to get/store the data
2039 * The control element is supposed to have the private_value field
2040 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2042 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
2045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2046 hda_nid_t nid = get_amp_nid(kcontrol);
2047 int chs = get_amp_channels(kcontrol);
2048 int dir = get_amp_direction(kcontrol);
2049 int idx = get_amp_index(kcontrol);
2050 long *valp = ucontrol->value.integer.value;
2053 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2054 HDA_AMP_MUTE) ? 0 : 1;
2056 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2057 HDA_AMP_MUTE) ? 0 : 1;
2060 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2063 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2064 * @kcontrol: ctl element
2065 * @ucontrol: pointer to get/store the data
2067 * The control element is supposed to have the private_value field
2068 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2070 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 hda_nid_t nid = get_amp_nid(kcontrol);
2075 int chs = get_amp_channels(kcontrol);
2076 int dir = get_amp_direction(kcontrol);
2077 int idx = get_amp_index(kcontrol);
2078 long *valp = ucontrol->value.integer.value;
2082 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2084 *valp ? 0 : HDA_AMP_MUTE);
2088 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2090 *valp ? 0 : HDA_AMP_MUTE);
2091 hda_call_check_power_status(codec, nid);
2094 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2097 * bound volume controls
2099 * bind multiple volumes (# indices, from 0)
2102 #define AMP_VAL_IDX_SHIFT 19
2103 #define AMP_VAL_IDX_MASK (0x0f<<19)
2106 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2107 * @kcontrol: ctl element
2108 * @ucontrol: pointer to get/store the data
2110 * The control element is supposed to have the private_value field
2111 * set up via HDA_BIND_MUTE*() macros.
2113 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2116 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2120 mutex_lock(&codec->control_mutex);
2121 pval = kcontrol->private_value;
2122 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2123 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2124 kcontrol->private_value = pval;
2125 mutex_unlock(&codec->control_mutex);
2128 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2131 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2132 * @kcontrol: ctl element
2133 * @ucontrol: pointer to get/store the data
2135 * The control element is supposed to have the private_value field
2136 * set up via HDA_BIND_MUTE*() macros.
2138 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_value *ucontrol)
2141 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2143 int i, indices, err = 0, change = 0;
2145 mutex_lock(&codec->control_mutex);
2146 pval = kcontrol->private_value;
2147 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2148 for (i = 0; i < indices; i++) {
2149 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2150 (i << AMP_VAL_IDX_SHIFT);
2151 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2156 kcontrol->private_value = pval;
2157 mutex_unlock(&codec->control_mutex);
2158 return err < 0 ? err : change;
2160 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2163 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2164 * @kcontrol: referred ctl element
2165 * @uinfo: pointer to get/store the data
2167 * The control element is supposed to have the private_value field
2168 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2170 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174 struct hda_bind_ctls *c;
2177 mutex_lock(&codec->control_mutex);
2178 c = (struct hda_bind_ctls *)kcontrol->private_value;
2179 kcontrol->private_value = *c->values;
2180 err = c->ops->info(kcontrol, uinfo);
2181 kcontrol->private_value = (long)c;
2182 mutex_unlock(&codec->control_mutex);
2185 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2188 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2189 * @kcontrol: ctl element
2190 * @ucontrol: pointer to get/store the data
2192 * The control element is supposed to have the private_value field
2193 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2195 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_value *ucontrol)
2198 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2199 struct hda_bind_ctls *c;
2202 mutex_lock(&codec->control_mutex);
2203 c = (struct hda_bind_ctls *)kcontrol->private_value;
2204 kcontrol->private_value = *c->values;
2205 err = c->ops->get(kcontrol, ucontrol);
2206 kcontrol->private_value = (long)c;
2207 mutex_unlock(&codec->control_mutex);
2210 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2213 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2214 * @kcontrol: ctl element
2215 * @ucontrol: pointer to get/store the data
2217 * The control element is supposed to have the private_value field
2218 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2220 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_value *ucontrol)
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_bind_ctls *c;
2225 unsigned long *vals;
2226 int err = 0, change = 0;
2228 mutex_lock(&codec->control_mutex);
2229 c = (struct hda_bind_ctls *)kcontrol->private_value;
2230 for (vals = c->values; *vals; vals++) {
2231 kcontrol->private_value = *vals;
2232 err = c->ops->put(kcontrol, ucontrol);
2237 kcontrol->private_value = (long)c;
2238 mutex_unlock(&codec->control_mutex);
2239 return err < 0 ? err : change;
2241 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2244 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2245 * @kcontrol: ctl element
2246 * @op_flag: operation flag
2247 * @size: byte size of input TLV
2250 * The control element is supposed to have the private_value field
2251 * set up via HDA_BIND_VOL() macro.
2253 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2254 unsigned int size, unsigned int __user *tlv)
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 struct hda_bind_ctls *c;
2260 mutex_lock(&codec->control_mutex);
2261 c = (struct hda_bind_ctls *)kcontrol->private_value;
2262 kcontrol->private_value = *c->values;
2263 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2264 kcontrol->private_value = (long)c;
2265 mutex_unlock(&codec->control_mutex);
2268 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2270 struct hda_ctl_ops snd_hda_bind_vol = {
2271 .info = snd_hda_mixer_amp_volume_info,
2272 .get = snd_hda_mixer_amp_volume_get,
2273 .put = snd_hda_mixer_amp_volume_put,
2274 .tlv = snd_hda_mixer_amp_tlv
2276 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2278 struct hda_ctl_ops snd_hda_bind_sw = {
2279 .info = snd_hda_mixer_amp_switch_info,
2280 .get = snd_hda_mixer_amp_switch_get,
2281 .put = snd_hda_mixer_amp_switch_put,
2282 .tlv = snd_hda_mixer_amp_tlv
2284 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2287 * SPDIF out controls
2290 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_info *uinfo)
2293 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2298 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
2301 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2302 IEC958_AES0_NONAUDIO |
2303 IEC958_AES0_CON_EMPHASIS_5015 |
2304 IEC958_AES0_CON_NOT_COPYRIGHT;
2305 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2306 IEC958_AES1_CON_ORIGINAL;
2310 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_value *ucontrol)
2313 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2314 IEC958_AES0_NONAUDIO |
2315 IEC958_AES0_PRO_EMPHASIS_5015;
2319 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
2322 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2323 int idx = kcontrol->private_value;
2324 struct hda_spdif_out *spdif;
2326 mutex_lock(&codec->spdif_mutex);
2327 spdif = snd_array_elem(&codec->spdif_out, idx);
2328 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2329 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2330 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2331 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2332 mutex_unlock(&codec->spdif_mutex);
2337 /* convert from SPDIF status bits to HDA SPDIF bits
2338 * bit 0 (DigEn) is always set zero (to be filled later)
2340 static unsigned short convert_from_spdif_status(unsigned int sbits)
2342 unsigned short val = 0;
2344 if (sbits & IEC958_AES0_PROFESSIONAL)
2345 val |= AC_DIG1_PROFESSIONAL;
2346 if (sbits & IEC958_AES0_NONAUDIO)
2347 val |= AC_DIG1_NONAUDIO;
2348 if (sbits & IEC958_AES0_PROFESSIONAL) {
2349 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2350 IEC958_AES0_PRO_EMPHASIS_5015)
2351 val |= AC_DIG1_EMPHASIS;
2353 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2354 IEC958_AES0_CON_EMPHASIS_5015)
2355 val |= AC_DIG1_EMPHASIS;
2356 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2357 val |= AC_DIG1_COPYRIGHT;
2358 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2359 val |= AC_DIG1_LEVEL;
2360 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2365 /* convert to SPDIF status bits from HDA SPDIF bits
2367 static unsigned int convert_to_spdif_status(unsigned short val)
2369 unsigned int sbits = 0;
2371 if (val & AC_DIG1_NONAUDIO)
2372 sbits |= IEC958_AES0_NONAUDIO;
2373 if (val & AC_DIG1_PROFESSIONAL)
2374 sbits |= IEC958_AES0_PROFESSIONAL;
2375 if (sbits & IEC958_AES0_PROFESSIONAL) {
2376 if (val & AC_DIG1_EMPHASIS)
2377 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2379 if (val & AC_DIG1_EMPHASIS)
2380 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2381 if (!(val & AC_DIG1_COPYRIGHT))
2382 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2383 if (val & AC_DIG1_LEVEL)
2384 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2385 sbits |= val & (0x7f << 8);
2390 /* set digital convert verbs both for the given NID and its slaves */
2391 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2396 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2398 d = codec->slave_dig_outs;
2402 snd_hdac_regmap_update(&codec->core, *d,
2403 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2406 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2409 unsigned int mask = 0;
2410 unsigned int val = 0;
2420 set_dig_out(codec, nid, mask, val);
2423 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2427 int idx = kcontrol->private_value;
2428 struct hda_spdif_out *spdif;
2433 mutex_lock(&codec->spdif_mutex);
2434 spdif = snd_array_elem(&codec->spdif_out, idx);
2436 spdif->status = ucontrol->value.iec958.status[0] |
2437 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2438 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2439 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2440 val = convert_from_spdif_status(spdif->status);
2441 val |= spdif->ctls & 1;
2442 change = spdif->ctls != val;
2444 if (change && nid != (u16)-1)
2445 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2446 mutex_unlock(&codec->spdif_mutex);
2450 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2452 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol)
2455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2456 int idx = kcontrol->private_value;
2457 struct hda_spdif_out *spdif;
2459 mutex_lock(&codec->spdif_mutex);
2460 spdif = snd_array_elem(&codec->spdif_out, idx);
2461 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2462 mutex_unlock(&codec->spdif_mutex);
2466 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2469 set_dig_out_convert(codec, nid, dig1, dig2);
2470 /* unmute amp switch (if any) */
2471 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2472 (dig1 & AC_DIG1_ENABLE))
2473 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2477 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_value *ucontrol)
2480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2481 int idx = kcontrol->private_value;
2482 struct hda_spdif_out *spdif;
2487 mutex_lock(&codec->spdif_mutex);
2488 spdif = snd_array_elem(&codec->spdif_out, idx);
2490 val = spdif->ctls & ~AC_DIG1_ENABLE;
2491 if (ucontrol->value.integer.value[0])
2492 val |= AC_DIG1_ENABLE;
2493 change = spdif->ctls != val;
2495 if (change && nid != (u16)-1)
2496 set_spdif_ctls(codec, nid, val & 0xff, -1);
2497 mutex_unlock(&codec->spdif_mutex);
2501 static struct snd_kcontrol_new dig_mixes[] = {
2503 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2504 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2505 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2506 .info = snd_hda_spdif_mask_info,
2507 .get = snd_hda_spdif_cmask_get,
2510 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2512 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2513 .info = snd_hda_spdif_mask_info,
2514 .get = snd_hda_spdif_pmask_get,
2517 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2518 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2519 .info = snd_hda_spdif_mask_info,
2520 .get = snd_hda_spdif_default_get,
2521 .put = snd_hda_spdif_default_put,
2524 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2525 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2526 .info = snd_hda_spdif_out_switch_info,
2527 .get = snd_hda_spdif_out_switch_get,
2528 .put = snd_hda_spdif_out_switch_put,
2534 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2535 * @codec: the HDA codec
2536 * @associated_nid: NID that new ctls associated with
2537 * @cvt_nid: converter NID
2538 * @type: HDA_PCM_TYPE_*
2539 * Creates controls related with the digital output.
2540 * Called from each patch supporting the digital out.
2542 * Returns 0 if successful, or a negative error code.
2544 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2545 hda_nid_t associated_nid,
2550 struct snd_kcontrol *kctl;
2551 struct snd_kcontrol_new *dig_mix;
2554 const int spdif_index = 16;
2555 struct hda_spdif_out *spdif;
2556 struct hda_bus *bus = codec->bus;
2558 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2559 type == HDA_PCM_TYPE_SPDIF) {
2561 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2562 type == HDA_PCM_TYPE_HDMI) {
2563 /* suppose a single SPDIF device */
2564 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2565 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2568 kctl->id.index = spdif_index;
2570 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2572 if (!bus->primary_dig_out_type)
2573 bus->primary_dig_out_type = type;
2575 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2577 codec_err(codec, "too many IEC958 outputs\n");
2580 spdif = snd_array_new(&codec->spdif_out);
2583 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2584 kctl = snd_ctl_new1(dig_mix, codec);
2587 kctl->id.index = idx;
2588 kctl->private_value = codec->spdif_out.used - 1;
2589 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2593 spdif->nid = cvt_nid;
2594 snd_hdac_regmap_read(&codec->core, cvt_nid,
2595 AC_VERB_GET_DIGI_CONVERT_1, &val);
2597 spdif->status = convert_to_spdif_status(spdif->ctls);
2600 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2603 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2604 * @codec: the HDA codec
2607 * call within spdif_mutex lock
2609 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2613 for (i = 0; i < codec->spdif_out.used; i++) {
2614 struct hda_spdif_out *spdif =
2615 snd_array_elem(&codec->spdif_out, i);
2616 if (spdif->nid == nid)
2621 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2624 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2625 * @codec: the HDA codec
2626 * @idx: the SPDIF ctl index
2628 * Unassign the widget from the given SPDIF control.
2630 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2632 struct hda_spdif_out *spdif;
2634 mutex_lock(&codec->spdif_mutex);
2635 spdif = snd_array_elem(&codec->spdif_out, idx);
2636 spdif->nid = (u16)-1;
2637 mutex_unlock(&codec->spdif_mutex);
2639 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2642 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2643 * @codec: the HDA codec
2644 * @idx: the SPDIF ctl idx
2647 * Assign the widget to the SPDIF control with the given index.
2649 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2651 struct hda_spdif_out *spdif;
2654 mutex_lock(&codec->spdif_mutex);
2655 spdif = snd_array_elem(&codec->spdif_out, idx);
2656 if (spdif->nid != nid) {
2659 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2661 mutex_unlock(&codec->spdif_mutex);
2663 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2666 * SPDIF sharing with analog output
2668 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
2671 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2672 ucontrol->value.integer.value[0] = mout->share_spdif;
2676 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2679 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2680 mout->share_spdif = !!ucontrol->value.integer.value[0];
2684 static struct snd_kcontrol_new spdif_share_sw = {
2685 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2686 .name = "IEC958 Default PCM Playback Switch",
2687 .info = snd_ctl_boolean_mono_info,
2688 .get = spdif_share_sw_get,
2689 .put = spdif_share_sw_put,
2693 * snd_hda_create_spdif_share_sw - create Default PCM switch
2694 * @codec: the HDA codec
2695 * @mout: multi-out instance
2697 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2698 struct hda_multi_out *mout)
2700 struct snd_kcontrol *kctl;
2702 if (!mout->dig_out_nid)
2705 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2708 /* ATTENTION: here mout is passed as private_data, instead of codec */
2709 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2711 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2717 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2719 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
2722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2724 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2728 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2732 hda_nid_t nid = kcontrol->private_value;
2733 unsigned int val = !!ucontrol->value.integer.value[0];
2736 mutex_lock(&codec->spdif_mutex);
2737 change = codec->spdif_in_enable != val;
2739 codec->spdif_in_enable = val;
2740 snd_hdac_regmap_write(&codec->core, nid,
2741 AC_VERB_SET_DIGI_CONVERT_1, val);
2743 mutex_unlock(&codec->spdif_mutex);
2747 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2751 hda_nid_t nid = kcontrol->private_value;
2755 snd_hdac_regmap_read(&codec->core, nid,
2756 AC_VERB_GET_DIGI_CONVERT_1, &val);
2757 sbits = convert_to_spdif_status(val);
2758 ucontrol->value.iec958.status[0] = sbits;
2759 ucontrol->value.iec958.status[1] = sbits >> 8;
2760 ucontrol->value.iec958.status[2] = sbits >> 16;
2761 ucontrol->value.iec958.status[3] = sbits >> 24;
2765 static struct snd_kcontrol_new dig_in_ctls[] = {
2767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2768 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2769 .info = snd_hda_spdif_in_switch_info,
2770 .get = snd_hda_spdif_in_switch_get,
2771 .put = snd_hda_spdif_in_switch_put,
2774 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2776 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2777 .info = snd_hda_spdif_mask_info,
2778 .get = snd_hda_spdif_in_status_get,
2784 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2785 * @codec: the HDA codec
2786 * @nid: audio in widget NID
2788 * Creates controls related with the SPDIF input.
2789 * Called from each patch supporting the SPDIF in.
2791 * Returns 0 if successful, or a negative error code.
2793 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2796 struct snd_kcontrol *kctl;
2797 struct snd_kcontrol_new *dig_mix;
2800 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2802 codec_err(codec, "too many IEC958 inputs\n");
2805 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2806 kctl = snd_ctl_new1(dig_mix, codec);
2809 kctl->private_value = nid;
2810 err = snd_hda_ctl_add(codec, nid, kctl);
2814 codec->spdif_in_enable =
2815 snd_hda_codec_read(codec, nid, 0,
2816 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2820 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2823 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2824 * @codec: the HDA codec
2825 * @fg: function group (not used now)
2826 * @power_state: the power state to set (AC_PWRST_*)
2828 * Set the given power state to all widgets that have the power control.
2829 * If the codec has power_filter set, it evaluates the power state and
2830 * filter out if it's unchanged as D3.
2832 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2833 unsigned int power_state)
2837 for_each_hda_codec_node(nid, codec) {
2838 unsigned int wcaps = get_wcaps(codec, nid);
2839 unsigned int state = power_state;
2840 if (!(wcaps & AC_WCAP_POWER))
2842 if (codec->power_filter) {
2843 state = codec->power_filter(codec, nid, power_state);
2844 if (state != power_state && power_state == AC_PWRST_D3)
2847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2851 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2854 * wait until the state is reached, returns the current state
2856 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2858 unsigned int power_state)
2860 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2861 unsigned int state, actual_state;
2864 state = snd_hda_codec_read(codec, fg, 0,
2865 AC_VERB_GET_POWER_STATE, 0);
2866 if (state & AC_PWRST_ERROR)
2868 actual_state = (state >> 4) & 0x0f;
2869 if (actual_state == power_state)
2871 if (time_after_eq(jiffies, end_time))
2873 /* wait until the codec reachs to the target state */
2880 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2881 * @codec: the HDA codec
2883 * @power_state: power state to evalue
2885 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2886 * This can be used a codec power_filter callback.
2888 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2890 unsigned int power_state)
2892 if (nid == codec->core.afg || nid == codec->core.mfg)
2894 if (power_state == AC_PWRST_D3 &&
2895 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2896 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2897 int eapd = snd_hda_codec_read(codec, nid, 0,
2898 AC_VERB_GET_EAPD_BTLENABLE, 0);
2904 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2907 * set power state of the codec, and return the power state
2909 static unsigned int hda_set_power_state(struct hda_codec *codec,
2910 unsigned int power_state)
2912 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2917 /* this delay seems necessary to avoid click noise at power-down */
2918 if (power_state == AC_PWRST_D3) {
2919 if (codec->depop_delay < 0)
2920 msleep(codec_has_epss(codec) ? 10 : 100);
2921 else if (codec->depop_delay > 0)
2922 msleep(codec->depop_delay);
2923 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2926 /* repeat power states setting at most 10 times*/
2927 for (count = 0; count < 10; count++) {
2928 if (codec->patch_ops.set_power_state)
2929 codec->patch_ops.set_power_state(codec, fg,
2932 state = power_state;
2933 if (codec->power_filter)
2934 state = codec->power_filter(codec, fg, state);
2935 if (state == power_state || power_state != AC_PWRST_D3)
2936 snd_hda_codec_read(codec, fg, flags,
2937 AC_VERB_SET_POWER_STATE,
2939 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2941 state = hda_sync_power_state(codec, fg, power_state);
2942 if (!(state & AC_PWRST_ERROR))
2949 /* sync power states of all widgets;
2950 * this is called at the end of codec parsing
2952 static void sync_power_up_states(struct hda_codec *codec)
2956 /* don't care if no filter is used */
2957 if (!codec->power_filter)
2960 for_each_hda_codec_node(nid, codec) {
2961 unsigned int wcaps = get_wcaps(codec, nid);
2962 unsigned int target;
2963 if (!(wcaps & AC_WCAP_POWER))
2965 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2966 if (target == AC_PWRST_D0)
2968 if (!snd_hda_check_power_state(codec, nid, target))
2969 snd_hda_codec_write(codec, nid, 0,
2970 AC_VERB_SET_POWER_STATE, target);
2974 #ifdef CONFIG_SND_HDA_RECONFIG
2975 /* execute additional init verbs */
2976 static void hda_exec_init_verbs(struct hda_codec *codec)
2978 if (codec->init_verbs.list)
2979 snd_hda_sequence_write(codec, codec->init_verbs.list);
2982 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2986 /* update the power on/off account with the current jiffies */
2987 static void update_power_acct(struct hda_codec *codec, bool on)
2989 unsigned long delta = jiffies - codec->power_jiffies;
2992 codec->power_on_acct += delta;
2994 codec->power_off_acct += delta;
2995 codec->power_jiffies += delta;
2998 void snd_hda_update_power_acct(struct hda_codec *codec)
3000 update_power_acct(codec, hda_codec_is_power_on(codec));
3004 * call suspend and power-down; used both from PM and power-save
3005 * this function returns the power state in the end
3007 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3011 atomic_inc(&codec->core.in_pm);
3013 if (codec->patch_ops.suspend)
3014 codec->patch_ops.suspend(codec);
3015 hda_cleanup_all_streams(codec);
3016 state = hda_set_power_state(codec, AC_PWRST_D3);
3017 update_power_acct(codec, true);
3018 atomic_dec(&codec->core.in_pm);
3023 * kick up codec; used both from PM and power-save
3025 static void hda_call_codec_resume(struct hda_codec *codec)
3027 atomic_inc(&codec->core.in_pm);
3029 if (codec->core.regmap)
3030 regcache_mark_dirty(codec->core.regmap);
3032 codec->power_jiffies = jiffies;
3034 hda_set_power_state(codec, AC_PWRST_D0);
3035 restore_shutup_pins(codec);
3036 hda_exec_init_verbs(codec);
3037 snd_hda_jack_set_dirty_all(codec);
3038 if (codec->patch_ops.resume)
3039 codec->patch_ops.resume(codec);
3041 if (codec->patch_ops.init)
3042 codec->patch_ops.init(codec);
3043 if (codec->core.regmap)
3044 regcache_sync(codec->core.regmap);
3047 if (codec->jackpoll_interval)
3048 hda_jackpoll_work(&codec->jackpoll_work.work);
3050 snd_hda_jack_report_sync(codec);
3051 atomic_dec(&codec->core.in_pm);
3054 static int hda_codec_runtime_suspend(struct device *dev)
3056 struct hda_codec *codec = dev_to_hda_codec(dev);
3057 struct hda_pcm *pcm;
3060 cancel_delayed_work_sync(&codec->jackpoll_work);
3061 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3062 snd_pcm_suspend_all(pcm->pcm);
3063 state = hda_call_codec_suspend(codec);
3064 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3065 (state & AC_PWRST_CLK_STOP_OK))
3066 snd_hdac_codec_link_down(&codec->core);
3067 snd_hdac_link_power(&codec->core, false);
3071 static int hda_codec_runtime_resume(struct device *dev)
3073 struct hda_codec *codec = dev_to_hda_codec(dev);
3075 snd_hdac_link_power(&codec->core, true);
3076 snd_hdac_codec_link_up(&codec->core);
3077 hda_call_codec_resume(codec);
3078 pm_runtime_mark_last_busy(dev);
3081 #endif /* CONFIG_PM */
3083 /* referred in hda_bind.c */
3084 const struct dev_pm_ops hda_codec_driver_pm = {
3085 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3086 pm_runtime_force_resume)
3087 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3092 * add standard channel maps if not specified
3094 static int add_std_chmaps(struct hda_codec *codec)
3096 struct hda_pcm *pcm;
3099 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3100 for (str = 0; str < 2; str++) {
3101 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3102 struct snd_pcm_chmap *chmap;
3103 const struct snd_pcm_chmap_elem *elem;
3105 if (!pcm || !pcm->pcm || pcm->own_chmap ||
3108 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3109 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3110 hinfo->channels_max,
3114 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3120 /* default channel maps for 2.1 speakers;
3121 * since HD-audio supports only stereo, odd number channels are omitted
3123 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3125 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3127 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3128 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3131 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3133 int snd_hda_codec_build_controls(struct hda_codec *codec)
3136 hda_exec_init_verbs(codec);
3137 /* continue to initialize... */
3138 if (codec->patch_ops.init)
3139 err = codec->patch_ops.init(codec);
3140 if (!err && codec->patch_ops.build_controls)
3141 err = codec->patch_ops.build_controls(codec);
3145 /* we create chmaps here instead of build_pcms */
3146 err = add_std_chmaps(codec);
3150 if (codec->jackpoll_interval)
3151 hda_jackpoll_work(&codec->jackpoll_work.work);
3153 snd_hda_jack_report_sync(codec); /* call at the last init point */
3154 sync_power_up_states(codec);
3161 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3162 struct hda_codec *codec,
3163 struct snd_pcm_substream *substream)
3168 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3169 struct hda_codec *codec,
3170 unsigned int stream_tag,
3171 unsigned int format,
3172 struct snd_pcm_substream *substream)
3174 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3178 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3179 struct hda_codec *codec,
3180 struct snd_pcm_substream *substream)
3182 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3186 static int set_pcm_default_values(struct hda_codec *codec,
3187 struct hda_pcm_stream *info)
3191 /* query support PCM information from the given NID */
3192 if (info->nid && (!info->rates || !info->formats)) {
3193 err = snd_hda_query_supported_pcm(codec, info->nid,
3194 info->rates ? NULL : &info->rates,
3195 info->formats ? NULL : &info->formats,
3196 info->maxbps ? NULL : &info->maxbps);
3200 if (info->ops.open == NULL)
3201 info->ops.open = hda_pcm_default_open_close;
3202 if (info->ops.close == NULL)
3203 info->ops.close = hda_pcm_default_open_close;
3204 if (info->ops.prepare == NULL) {
3205 if (snd_BUG_ON(!info->nid))
3207 info->ops.prepare = hda_pcm_default_prepare;
3209 if (info->ops.cleanup == NULL) {
3210 if (snd_BUG_ON(!info->nid))
3212 info->ops.cleanup = hda_pcm_default_cleanup;
3218 * codec prepare/cleanup entries
3221 * snd_hda_codec_prepare - Prepare a stream
3222 * @codec: the HDA codec
3223 * @hinfo: PCM information
3224 * @stream: stream tag to assign
3225 * @format: format id to assign
3226 * @substream: PCM substream to assign
3228 * Calls the prepare callback set by the codec with the given arguments.
3229 * Clean up the inactive streams when successful.
3231 int snd_hda_codec_prepare(struct hda_codec *codec,
3232 struct hda_pcm_stream *hinfo,
3233 unsigned int stream,
3234 unsigned int format,
3235 struct snd_pcm_substream *substream)
3238 mutex_lock(&codec->bus->prepare_mutex);
3239 if (hinfo->ops.prepare)
3240 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3245 purify_inactive_streams(codec);
3246 mutex_unlock(&codec->bus->prepare_mutex);
3249 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3252 * snd_hda_codec_cleanup - Prepare a stream
3253 * @codec: the HDA codec
3254 * @hinfo: PCM information
3255 * @substream: PCM substream
3257 * Calls the cleanup callback set by the codec with the given arguments.
3259 void snd_hda_codec_cleanup(struct hda_codec *codec,
3260 struct hda_pcm_stream *hinfo,
3261 struct snd_pcm_substream *substream)
3263 mutex_lock(&codec->bus->prepare_mutex);
3264 if (hinfo->ops.cleanup)
3265 hinfo->ops.cleanup(hinfo, codec, substream);
3266 mutex_unlock(&codec->bus->prepare_mutex);
3268 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3271 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3272 "Audio", "SPDIF", "HDMI", "Modem"
3276 * get the empty PCM device number to assign
3278 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3280 /* audio device indices; not linear to keep compatibility */
3281 /* assigned to static slots up to dev#10; if more needed, assign
3282 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3284 static int audio_idx[HDA_PCM_NTYPES][5] = {
3285 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3286 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3287 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3288 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3292 if (type >= HDA_PCM_NTYPES) {
3293 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3297 for (i = 0; audio_idx[type][i] >= 0; i++) {
3298 #ifndef CONFIG_SND_DYNAMIC_MINORS
3299 if (audio_idx[type][i] >= 8)
3302 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3303 return audio_idx[type][i];
3306 #ifdef CONFIG_SND_DYNAMIC_MINORS
3307 /* non-fixed slots starting from 10 */
3308 for (i = 10; i < 32; i++) {
3309 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3314 dev_warn(bus->card->dev, "Too many %s devices\n",
3315 snd_hda_pcm_type_name[type]);
3316 #ifndef CONFIG_SND_DYNAMIC_MINORS
3317 dev_warn(bus->card->dev,
3318 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3323 /* call build_pcms ops of the given codec and set up the default parameters */
3324 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3326 struct hda_pcm *cpcm;
3329 if (!list_empty(&codec->pcm_list_head))
3330 return 0; /* already parsed */
3332 if (!codec->patch_ops.build_pcms)
3335 err = codec->patch_ops.build_pcms(codec);
3337 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3338 codec->core.addr, err);
3342 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3345 for (stream = 0; stream < 2; stream++) {
3346 struct hda_pcm_stream *info = &cpcm->stream[stream];
3348 if (!info->substreams)
3350 err = set_pcm_default_values(codec, info);
3353 "fail to setup default for PCM %s\n",
3363 /* assign all PCMs of the given codec */
3364 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3366 struct hda_bus *bus = codec->bus;
3367 struct hda_pcm *cpcm;
3370 err = snd_hda_codec_parse_pcms(codec);
3372 snd_hda_codec_reset(codec);
3376 /* attach a new PCM streams */
3377 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3379 continue; /* already attached */
3380 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3381 continue; /* no substreams assigned */
3383 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3385 continue; /* no fatal error */
3387 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3390 "cannot attach PCM stream %d for codec #%d\n",
3391 dev, codec->core.addr);
3392 continue; /* no fatal error */
3400 * snd_hda_add_new_ctls - create controls from the array
3401 * @codec: the HDA codec
3402 * @knew: the array of struct snd_kcontrol_new
3404 * This helper function creates and add new controls in the given array.
3405 * The array must be terminated with an empty entry as terminator.
3407 * Returns 0 if successful, or a negative error code.
3409 int snd_hda_add_new_ctls(struct hda_codec *codec,
3410 const struct snd_kcontrol_new *knew)
3414 for (; knew->name; knew++) {
3415 struct snd_kcontrol *kctl;
3416 int addr = 0, idx = 0;
3417 if (knew->iface == -1) /* skip this codec private value */
3420 kctl = snd_ctl_new1(knew, codec);
3424 kctl->id.device = addr;
3426 kctl->id.index = idx;
3427 err = snd_hda_ctl_add(codec, 0, kctl);
3430 /* try first with another device index corresponding to
3431 * the codec addr; if it still fails (or it's the
3432 * primary codec), then try another control index
3434 if (!addr && codec->core.addr)
3435 addr = codec->core.addr;
3436 else if (!idx && !knew->index) {
3437 idx = find_empty_mixer_ctl_idx(codec,
3447 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3450 static void codec_set_power_save(struct hda_codec *codec, int delay)
3452 struct device *dev = hda_codec_dev(codec);
3454 if (delay == 0 && codec->auto_runtime_pm)
3458 pm_runtime_set_autosuspend_delay(dev, delay);
3459 pm_runtime_use_autosuspend(dev);
3460 pm_runtime_allow(dev);
3461 if (!pm_runtime_suspended(dev))
3462 pm_runtime_mark_last_busy(dev);
3464 pm_runtime_dont_use_autosuspend(dev);
3465 pm_runtime_forbid(dev);
3470 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3471 * @bus: HD-audio bus
3472 * @delay: autosuspend delay in msec, 0 = off
3474 * Synchronize the runtime PM autosuspend state from the power_save option.
3476 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3478 struct hda_codec *c;
3480 list_for_each_codec(c, bus)
3481 codec_set_power_save(c, delay);
3483 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3486 * snd_hda_check_amp_list_power - Check the amp list and update the power
3487 * @codec: HD-audio codec
3488 * @check: the object containing an AMP list and the status
3489 * @nid: NID to check / update
3491 * Check whether the given NID is in the amp list. If it's in the list,
3492 * check the current AMP status, and update the the power-status according
3493 * to the mute status.
3495 * This function is supposed to be set or called from the check_power_status
3498 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3499 struct hda_loopback_check *check,
3502 const struct hda_amp_list *p;
3505 if (!check->amplist)
3507 for (p = check->amplist; p->nid; p++) {
3512 return 0; /* nothing changed */
3514 for (p = check->amplist; p->nid; p++) {
3515 for (ch = 0; ch < 2; ch++) {
3516 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3518 if (!(v & HDA_AMP_MUTE) && v > 0) {
3519 if (!check->power_on) {
3520 check->power_on = 1;
3521 snd_hda_power_up_pm(codec);
3527 if (check->power_on) {
3528 check->power_on = 0;
3529 snd_hda_power_down_pm(codec);
3533 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3541 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3542 * @imux: imux helper object
3543 * @uinfo: pointer to get/store the data
3545 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3546 struct snd_ctl_elem_info *uinfo)
3550 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3552 uinfo->value.enumerated.items = imux->num_items;
3553 if (!imux->num_items)
3555 index = uinfo->value.enumerated.item;
3556 if (index >= imux->num_items)
3557 index = imux->num_items - 1;
3558 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3561 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3564 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3565 * @codec: the HDA codec
3566 * @imux: imux helper object
3567 * @ucontrol: pointer to get/store the data
3568 * @nid: input mux NID
3569 * @cur_val: pointer to get/store the current imux value
3571 int snd_hda_input_mux_put(struct hda_codec *codec,
3572 const struct hda_input_mux *imux,
3573 struct snd_ctl_elem_value *ucontrol,
3575 unsigned int *cur_val)
3579 if (!imux->num_items)
3581 idx = ucontrol->value.enumerated.item[0];
3582 if (idx >= imux->num_items)
3583 idx = imux->num_items - 1;
3584 if (*cur_val == idx)
3586 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3587 imux->items[idx].index);
3591 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3595 * snd_hda_enum_helper_info - Helper for simple enum ctls
3596 * @kcontrol: ctl element
3597 * @uinfo: pointer to get/store the data
3598 * @num_items: number of enum items
3599 * @texts: enum item string array
3601 * process kcontrol info callback of a simple string enum array
3602 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3604 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3605 struct snd_ctl_elem_info *uinfo,
3606 int num_items, const char * const *texts)
3608 static const char * const texts_default[] = {
3609 "Disabled", "Enabled"
3612 if (!texts || !num_items) {
3614 texts = texts_default;
3617 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3619 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3622 * Multi-channel / digital-out PCM helper functions
3625 /* setup SPDIF output stream */
3626 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3627 unsigned int stream_tag, unsigned int format)
3629 struct hda_spdif_out *spdif;
3630 unsigned int curr_fmt;
3633 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3634 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3635 AC_VERB_GET_STREAM_FORMAT, 0);
3636 reset = codec->spdif_status_reset &&
3637 (spdif->ctls & AC_DIG1_ENABLE) &&
3640 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3643 set_dig_out_convert(codec, nid,
3644 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3646 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3647 if (codec->slave_dig_outs) {
3649 for (d = codec->slave_dig_outs; *d; d++)
3650 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3653 /* turn on again (if needed) */
3655 set_dig_out_convert(codec, nid,
3656 spdif->ctls & 0xff, -1);
3659 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3661 snd_hda_codec_cleanup_stream(codec, nid);
3662 if (codec->slave_dig_outs) {
3664 for (d = codec->slave_dig_outs; *d; d++)
3665 snd_hda_codec_cleanup_stream(codec, *d);
3670 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3671 * @codec: the HDA codec
3672 * @mout: hda_multi_out object
3674 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3675 struct hda_multi_out *mout)
3677 mutex_lock(&codec->spdif_mutex);
3678 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3679 /* already opened as analog dup; reset it once */
3680 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3681 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3682 mutex_unlock(&codec->spdif_mutex);
3685 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3688 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3689 * @codec: the HDA codec
3690 * @mout: hda_multi_out object
3691 * @stream_tag: stream tag to assign
3692 * @format: format id to assign
3693 * @substream: PCM substream to assign
3695 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3696 struct hda_multi_out *mout,
3697 unsigned int stream_tag,
3698 unsigned int format,
3699 struct snd_pcm_substream *substream)
3701 mutex_lock(&codec->spdif_mutex);
3702 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3703 mutex_unlock(&codec->spdif_mutex);
3706 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3709 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3710 * @codec: the HDA codec
3711 * @mout: hda_multi_out object
3713 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3714 struct hda_multi_out *mout)
3716 mutex_lock(&codec->spdif_mutex);
3717 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3718 mutex_unlock(&codec->spdif_mutex);
3721 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3724 * snd_hda_multi_out_dig_close - release the digital out stream
3725 * @codec: the HDA codec
3726 * @mout: hda_multi_out object
3728 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3729 struct hda_multi_out *mout)
3731 mutex_lock(&codec->spdif_mutex);
3732 mout->dig_out_used = 0;
3733 mutex_unlock(&codec->spdif_mutex);
3736 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3739 * snd_hda_multi_out_analog_open - open analog outputs
3740 * @codec: the HDA codec
3741 * @mout: hda_multi_out object
3742 * @substream: PCM substream to assign
3743 * @hinfo: PCM information to assign
3745 * Open analog outputs and set up the hw-constraints.
3746 * If the digital outputs can be opened as slave, open the digital
3749 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3750 struct hda_multi_out *mout,
3751 struct snd_pcm_substream *substream,
3752 struct hda_pcm_stream *hinfo)
3754 struct snd_pcm_runtime *runtime = substream->runtime;
3755 runtime->hw.channels_max = mout->max_channels;
3756 if (mout->dig_out_nid) {
3757 if (!mout->analog_rates) {
3758 mout->analog_rates = hinfo->rates;
3759 mout->analog_formats = hinfo->formats;
3760 mout->analog_maxbps = hinfo->maxbps;
3762 runtime->hw.rates = mout->analog_rates;
3763 runtime->hw.formats = mout->analog_formats;
3764 hinfo->maxbps = mout->analog_maxbps;
3766 if (!mout->spdif_rates) {
3767 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3769 &mout->spdif_formats,
3770 &mout->spdif_maxbps);
3772 mutex_lock(&codec->spdif_mutex);
3773 if (mout->share_spdif) {
3774 if ((runtime->hw.rates & mout->spdif_rates) &&
3775 (runtime->hw.formats & mout->spdif_formats)) {
3776 runtime->hw.rates &= mout->spdif_rates;
3777 runtime->hw.formats &= mout->spdif_formats;
3778 if (mout->spdif_maxbps < hinfo->maxbps)
3779 hinfo->maxbps = mout->spdif_maxbps;
3781 mout->share_spdif = 0;
3782 /* FIXME: need notify? */
3785 mutex_unlock(&codec->spdif_mutex);
3787 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3788 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3790 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3793 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3794 * @codec: the HDA codec
3795 * @mout: hda_multi_out object
3796 * @stream_tag: stream tag to assign
3797 * @format: format id to assign
3798 * @substream: PCM substream to assign
3800 * Set up the i/o for analog out.
3801 * When the digital out is available, copy the front out to digital out, too.
3803 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3804 struct hda_multi_out *mout,
3805 unsigned int stream_tag,
3806 unsigned int format,
3807 struct snd_pcm_substream *substream)
3809 const hda_nid_t *nids = mout->dac_nids;
3810 int chs = substream->runtime->channels;
3811 struct hda_spdif_out *spdif;
3814 mutex_lock(&codec->spdif_mutex);
3815 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3816 if (mout->dig_out_nid && mout->share_spdif &&
3817 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3819 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3821 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3822 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3823 setup_dig_out_stream(codec, mout->dig_out_nid,
3824 stream_tag, format);
3826 mout->dig_out_used = 0;
3827 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3830 mutex_unlock(&codec->spdif_mutex);
3833 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3835 if (!mout->no_share_stream &&
3836 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3837 /* headphone out will just decode front left/right (stereo) */
3838 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3840 /* extra outputs copied from front */
3841 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3842 if (!mout->no_share_stream && mout->hp_out_nid[i])
3843 snd_hda_codec_setup_stream(codec,
3844 mout->hp_out_nid[i],
3845 stream_tag, 0, format);
3848 for (i = 1; i < mout->num_dacs; i++) {
3849 if (chs >= (i + 1) * 2) /* independent out */
3850 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3852 else if (!mout->no_share_stream) /* copy front */
3853 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3857 /* extra surrounds */
3858 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3860 if (!mout->extra_out_nid[i])
3862 if (chs >= (i + 1) * 2)
3864 else if (!mout->no_share_stream)
3866 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3867 stream_tag, ch, format);
3872 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3875 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3876 * @codec: the HDA codec
3877 * @mout: hda_multi_out object
3879 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3880 struct hda_multi_out *mout)
3882 const hda_nid_t *nids = mout->dac_nids;
3885 for (i = 0; i < mout->num_dacs; i++)
3886 snd_hda_codec_cleanup_stream(codec, nids[i]);
3888 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3889 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3890 if (mout->hp_out_nid[i])
3891 snd_hda_codec_cleanup_stream(codec,
3892 mout->hp_out_nid[i]);
3893 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3894 if (mout->extra_out_nid[i])
3895 snd_hda_codec_cleanup_stream(codec,
3896 mout->extra_out_nid[i]);
3897 mutex_lock(&codec->spdif_mutex);
3898 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3899 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3900 mout->dig_out_used = 0;
3902 mutex_unlock(&codec->spdif_mutex);
3905 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3908 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3909 * @codec: the HDA codec
3910 * @pin: referred pin NID
3912 * Guess the suitable VREF pin bits to be set as the pin-control value.
3913 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3915 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3917 unsigned int pincap;
3918 unsigned int oldval;
3919 oldval = snd_hda_codec_read(codec, pin, 0,
3920 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3921 pincap = snd_hda_query_pin_caps(codec, pin);
3922 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3923 /* Exception: if the default pin setup is vref50, we give it priority */
3924 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3925 return AC_PINCTL_VREF_80;
3926 else if (pincap & AC_PINCAP_VREF_50)
3927 return AC_PINCTL_VREF_50;
3928 else if (pincap & AC_PINCAP_VREF_100)
3929 return AC_PINCTL_VREF_100;
3930 else if (pincap & AC_PINCAP_VREF_GRD)
3931 return AC_PINCTL_VREF_GRD;
3932 return AC_PINCTL_VREF_HIZ;
3934 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3937 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3938 * @codec: the HDA codec
3939 * @pin: referred pin NID
3940 * @val: pin ctl value to audit
3942 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3943 hda_nid_t pin, unsigned int val)
3945 static unsigned int cap_lists[][2] = {
3946 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3947 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3948 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3949 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3955 cap = snd_hda_query_pin_caps(codec, pin);
3957 return val; /* don't know what to do... */
3959 if (val & AC_PINCTL_OUT_EN) {
3960 if (!(cap & AC_PINCAP_OUT))
3961 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3962 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3963 val &= ~AC_PINCTL_HP_EN;
3966 if (val & AC_PINCTL_IN_EN) {
3967 if (!(cap & AC_PINCAP_IN))
3968 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3970 unsigned int vcap, vref;
3972 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3973 vref = val & AC_PINCTL_VREFEN;
3974 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3975 if (vref == cap_lists[i][0] &&
3976 !(vcap & cap_lists[i][1])) {
3977 if (i == ARRAY_SIZE(cap_lists) - 1)
3978 vref = AC_PINCTL_VREF_HIZ;
3980 vref = cap_lists[i + 1][0];
3983 val &= ~AC_PINCTL_VREFEN;
3990 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
3993 * _snd_hda_pin_ctl - Helper to set pin ctl value
3994 * @codec: the HDA codec
3995 * @pin: referred pin NID
3996 * @val: pin control value to set
3997 * @cached: access over codec pinctl cache or direct write
3999 * This function is a helper to set a pin ctl value more safely.
4000 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4001 * value in pin target array via snd_hda_codec_set_pin_target(), then
4002 * actually writes the value via either snd_hda_codec_update_cache() or
4003 * snd_hda_codec_write() depending on @cached flag.
4005 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4006 unsigned int val, bool cached)
4008 val = snd_hda_correct_pin_ctl(codec, pin, val);
4009 snd_hda_codec_set_pin_target(codec, pin, val);
4011 return snd_hda_codec_update_cache(codec, pin, 0,
4012 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4014 return snd_hda_codec_write(codec, pin, 0,
4015 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4017 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4020 * snd_hda_add_imux_item - Add an item to input_mux
4021 * @codec: the HDA codec
4022 * @imux: imux helper object
4023 * @label: the name of imux item to assign
4024 * @index: index number of imux item to assign
4025 * @type_idx: pointer to store the resultant label index
4027 * When the same label is used already in the existing items, the number
4028 * suffix is appended to the label. This label index number is stored
4029 * to type_idx when non-NULL pointer is given.
4031 int snd_hda_add_imux_item(struct hda_codec *codec,
4032 struct hda_input_mux *imux, const char *label,
4033 int index, int *type_idx)
4035 int i, label_idx = 0;
4036 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4037 codec_err(codec, "hda_codec: Too many imux items!\n");
4040 for (i = 0; i < imux->num_items; i++) {
4041 if (!strncmp(label, imux->items[i].label, strlen(label)))
4045 *type_idx = label_idx;
4047 snprintf(imux->items[imux->num_items].label,
4048 sizeof(imux->items[imux->num_items].label),
4049 "%s %d", label, label_idx);
4051 strlcpy(imux->items[imux->num_items].label, label,
4052 sizeof(imux->items[imux->num_items].label));
4053 imux->items[imux->num_items].index = index;
4057 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4060 * snd_hda_bus_reset_codecs - Reset the bus
4061 * @bus: HD-audio bus
4063 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4065 struct hda_codec *codec;
4067 list_for_each_codec(codec, bus) {
4068 /* FIXME: maybe a better way needed for forced reset */
4069 cancel_delayed_work_sync(&codec->jackpoll_work);
4071 if (hda_codec_is_power_on(codec)) {
4072 hda_call_codec_suspend(codec);
4073 hda_call_codec_resume(codec);
4080 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4081 * @pcm: PCM caps bits
4082 * @buf: the string buffer to write
4083 * @buflen: the max buffer length
4085 * used by hda_proc.c and hda_eld.c
4087 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4089 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4092 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4093 if (pcm & (AC_SUPPCM_BITS_8 << i))
4094 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4096 buf[j] = '\0'; /* necessary when j == 0 */
4098 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4100 MODULE_DESCRIPTION("HDA codec core");
4101 MODULE_LICENSE("GPL");