2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/module.h>
28 #include <linux/pm_runtime.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define codec_in_pm(codec) snd_hdac_is_in_pm(&codec->core)
41 #define hda_codec_is_power_on(codec) snd_hdac_is_power_on(&codec->core)
42 #define codec_has_epss(codec) \
43 ((codec)->core.power_caps & AC_PWRST_EPSS)
44 #define codec_has_clkstop(codec) \
45 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
48 * Send and receive a verb - passed to exec_verb override for hdac_device
50 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
51 unsigned int flags, unsigned int *res)
53 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
54 struct hda_bus *bus = codec->bus;
61 snd_hda_power_up_pm(codec);
62 mutex_lock(&bus->core.cmd_mutex);
63 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
64 bus->no_response_fallback = 1;
65 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
67 bus->no_response_fallback = 0;
68 mutex_unlock(&bus->core.cmd_mutex);
69 snd_hda_power_down_pm(codec);
70 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
71 if (bus->response_reset) {
73 "resetting BUS due to fatal communication error\n");
74 snd_hda_bus_reset(bus);
78 /* clear reset-flag when the communication gets recovered */
79 if (!err || codec_in_pm(codec))
80 bus->response_reset = 0;
85 * snd_hda_sequence_write - sequence writes
86 * @codec: the HDA codec
87 * @seq: VERB array to send
89 * Send the commands sequentially from the given array.
90 * The array must be terminated with NID=0.
92 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
94 for (; seq->nid; seq++)
95 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
97 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
99 /* connection list element */
100 struct hda_conn_list {
101 struct list_head list;
107 /* look up the cached results */
108 static struct hda_conn_list *
109 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
111 struct hda_conn_list *p;
112 list_for_each_entry(p, &codec->conn_list, list) {
119 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
120 const hda_nid_t *list)
122 struct hda_conn_list *p;
124 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
129 memcpy(p->conns, list, len * sizeof(hda_nid_t));
130 list_add(&p->list, &codec->conn_list);
134 static void remove_conn_list(struct hda_codec *codec)
136 while (!list_empty(&codec->conn_list)) {
137 struct hda_conn_list *p;
138 p = list_first_entry(&codec->conn_list, typeof(*p), list);
144 /* read the connection and add to the cache */
145 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
148 hda_nid_t *result = list;
151 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
152 if (len == -ENOSPC) {
153 len = snd_hda_get_num_raw_conns(codec, nid);
154 result = kmalloc_array(len, sizeof(hda_nid_t), GFP_KERNEL);
157 len = snd_hda_get_raw_connections(codec, nid, result, len);
160 len = snd_hda_override_conn_list(codec, nid, len, result);
167 * snd_hda_get_conn_list - get connection list
168 * @codec: the HDA codec
170 * @listp: the pointer to store NID list
172 * Parses the connection list of the given widget and stores the pointer
173 * to the list of NIDs.
175 * Returns the number of connections, or a negative error code.
177 * Note that the returned pointer isn't protected against the list
178 * modification. If snd_hda_override_conn_list() might be called
179 * concurrently, protect with a mutex appropriately.
181 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
182 const hda_nid_t **listp)
188 const struct hda_conn_list *p;
190 /* if the connection-list is already cached, read it */
191 p = lookup_conn_list(codec, nid);
197 if (snd_BUG_ON(added))
200 err = read_and_add_raw_conns(codec, nid);
206 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
209 * snd_hda_get_connections - copy connection list
210 * @codec: the HDA codec
212 * @conn_list: connection list array; when NULL, checks only the size
213 * @max_conns: max. number of connections to store
215 * Parses the connection list of the given widget and stores the list
218 * Returns the number of connections, or a negative error code.
220 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
221 hda_nid_t *conn_list, int max_conns)
223 const hda_nid_t *list;
224 int len = snd_hda_get_conn_list(codec, nid, &list);
226 if (len > 0 && conn_list) {
227 if (len > max_conns) {
228 codec_err(codec, "Too many connections %d for NID 0x%x\n",
232 memcpy(conn_list, list, len * sizeof(hda_nid_t));
237 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
240 * snd_hda_override_conn_list - add/modify the connection-list to cache
241 * @codec: the HDA codec
243 * @len: number of connection list entries
244 * @list: the list of connection entries
246 * Add or modify the given connection-list to the cache. If the corresponding
247 * cache already exists, invalidate it and append a new one.
249 * Returns zero or a negative error code.
251 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
252 const hda_nid_t *list)
254 struct hda_conn_list *p;
256 p = lookup_conn_list(codec, nid);
262 return add_conn_list(codec, nid, len, list);
264 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
267 * snd_hda_get_conn_index - get the connection index of the given NID
268 * @codec: the HDA codec
269 * @mux: NID containing the list
270 * @nid: NID to select
271 * @recursive: 1 when searching NID recursively, otherwise 0
273 * Parses the connection list of the widget @mux and checks whether the
274 * widget @nid is present. If it is, return the connection index.
275 * Otherwise it returns -1.
277 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
278 hda_nid_t nid, int recursive)
280 const hda_nid_t *conn;
283 nums = snd_hda_get_conn_list(codec, mux, &conn);
284 for (i = 0; i < nums; i++)
289 if (recursive > 10) {
290 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
294 for (i = 0; i < nums; i++) {
295 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
296 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
298 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
303 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
306 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
307 * @codec: the HDA codec
308 * @nid: NID of the pin to parse
310 * Get the device entry number on the given widget. This is a feature of
311 * DP MST audio. Each pin can have several device entries in it.
313 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
315 unsigned int wcaps = get_wcaps(codec, nid);
318 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
319 get_wcaps_type(wcaps) != AC_WID_PIN)
322 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
325 return parm & AC_DEV_LIST_LEN_MASK;
327 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
330 * snd_hda_get_devices - copy device list without cache
331 * @codec: the HDA codec
332 * @nid: NID of the pin to parse
333 * @dev_list: device list array
334 * @max_devices: max. number of devices to store
336 * Copy the device list. This info is dynamic and so not cached.
337 * Currently called only from hda_proc.c, so not exported.
339 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
340 u8 *dev_list, int max_devices)
343 int i, dev_len, devices;
345 parm = snd_hda_get_num_devices(codec, nid);
346 if (!parm) /* not multi-stream capable */
350 dev_len = dev_len < max_devices ? dev_len : max_devices;
353 while (devices < dev_len) {
354 if (snd_hdac_read(&codec->core, nid,
355 AC_VERB_GET_DEVICE_LIST, devices, &parm))
358 for (i = 0; i < 8; i++) {
359 dev_list[devices] = (u8)parm;
362 if (devices >= dev_len)
370 * snd_hda_get_dev_select - get device entry select on the pin
371 * @codec: the HDA codec
372 * @nid: NID of the pin to get device entry select
374 * Get the devcie entry select on the pin. Return the device entry
375 * id selected on the pin. Return 0 means the first device entry
376 * is selected or MST is not supported.
378 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
380 /* not support dp_mst will always return 0, using first dev_entry */
384 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
386 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
389 * snd_hda_set_dev_select - set device entry select on the pin
390 * @codec: the HDA codec
391 * @nid: NID of the pin to set device entry select
392 * @dev_id: device entry id to be set
394 * Set the device entry select on the pin nid.
396 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
398 int ret, num_devices;
400 /* not support dp_mst will always return 0, using first dev_entry */
404 /* AC_PAR_DEVLIST_LEN is 0 based. */
405 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
406 /* If Device List Length is 0 (num_device = 1),
407 * the pin is not multi stream capable.
408 * Do nothing in this case.
410 if (num_devices == 1)
413 /* Behavior of setting index being equal to or greater than
414 * Device List Length is not predictable
416 if (num_devices <= dev_id)
419 ret = snd_hda_codec_write(codec, nid, 0,
420 AC_VERB_SET_DEVICE_SEL, dev_id);
424 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
427 * read widget caps for each widget and store in cache
429 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
434 codec->wcaps = kmalloc_array(codec->core.num_nodes, 4, GFP_KERNEL);
437 nid = codec->core.start_nid;
438 for (i = 0; i < codec->core.num_nodes; i++, nid++)
439 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
440 nid, AC_PAR_AUDIO_WIDGET_CAP);
444 /* read all pin default configurations and save codec->init_pins */
445 static int read_pin_defaults(struct hda_codec *codec)
449 for_each_hda_codec_node(nid, codec) {
450 struct hda_pincfg *pin;
451 unsigned int wcaps = get_wcaps(codec, nid);
452 unsigned int wid_type = get_wcaps_type(wcaps);
453 if (wid_type != AC_WID_PIN)
455 pin = snd_array_new(&codec->init_pins);
459 pin->cfg = snd_hda_codec_read(codec, nid, 0,
460 AC_VERB_GET_CONFIG_DEFAULT, 0);
462 * all device entries are the same widget control so far
463 * fixme: if any codec is different, need fix here
465 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
466 AC_VERB_GET_PIN_WIDGET_CONTROL,
472 /* look up the given pin config list and return the item matching with NID */
473 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
474 struct snd_array *array,
477 struct hda_pincfg *pin;
480 snd_array_for_each(array, i, pin) {
487 /* set the current pin config value for the given NID.
488 * the value is cached, and read via snd_hda_codec_get_pincfg()
490 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
491 hda_nid_t nid, unsigned int cfg)
493 struct hda_pincfg *pin;
495 /* the check below may be invalid when pins are added by a fixup
496 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
500 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
504 pin = look_up_pincfg(codec, list, nid);
506 pin = snd_array_new(list);
516 * snd_hda_codec_set_pincfg - Override a pin default configuration
517 * @codec: the HDA codec
518 * @nid: NID to set the pin config
519 * @cfg: the pin default config value
521 * Override a pin default configuration value in the cache.
522 * This value can be read by snd_hda_codec_get_pincfg() in a higher
523 * priority than the real hardware value.
525 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
526 hda_nid_t nid, unsigned int cfg)
528 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
530 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
533 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
534 * @codec: the HDA codec
535 * @nid: NID to get the pin config
537 * Get the current pin config value of the given pin NID.
538 * If the pincfg value is cached or overridden via sysfs or driver,
539 * returns the cached value.
541 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
543 struct hda_pincfg *pin;
545 #ifdef CONFIG_SND_HDA_RECONFIG
547 unsigned int cfg = 0;
548 mutex_lock(&codec->user_mutex);
549 pin = look_up_pincfg(codec, &codec->user_pins, nid);
552 mutex_unlock(&codec->user_mutex);
557 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
560 pin = look_up_pincfg(codec, &codec->init_pins, nid);
565 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
568 * snd_hda_codec_set_pin_target - remember the current pinctl target value
569 * @codec: the HDA codec
571 * @val: assigned pinctl value
573 * This function stores the given value to a pinctl target value in the
574 * pincfg table. This isn't always as same as the actually written value
575 * but can be referred at any time via snd_hda_codec_get_pin_target().
577 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
580 struct hda_pincfg *pin;
582 pin = look_up_pincfg(codec, &codec->init_pins, nid);
588 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
591 * snd_hda_codec_get_pin_target - return the current pinctl target value
592 * @codec: the HDA codec
595 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
597 struct hda_pincfg *pin;
599 pin = look_up_pincfg(codec, &codec->init_pins, nid);
604 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
607 * snd_hda_shutup_pins - Shut up all pins
608 * @codec: the HDA codec
610 * Clear all pin controls to shup up before suspend for avoiding click noise.
611 * The controls aren't cached so that they can be resumed properly.
613 void snd_hda_shutup_pins(struct hda_codec *codec)
615 const struct hda_pincfg *pin;
618 /* don't shut up pins when unloading the driver; otherwise it breaks
619 * the default pin setup at the next load of the driver
621 if (codec->bus->shutdown)
623 snd_array_for_each(&codec->init_pins, i, pin) {
624 /* use read here for syncing after issuing each verb */
625 snd_hda_codec_read(codec, pin->nid, 0,
626 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
628 codec->pins_shutup = 1;
630 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
633 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
634 static void restore_shutup_pins(struct hda_codec *codec)
636 const struct hda_pincfg *pin;
639 if (!codec->pins_shutup)
641 if (codec->bus->shutdown)
643 snd_array_for_each(&codec->init_pins, i, pin) {
644 snd_hda_codec_write(codec, pin->nid, 0,
645 AC_VERB_SET_PIN_WIDGET_CONTROL,
648 codec->pins_shutup = 0;
652 static void hda_jackpoll_work(struct work_struct *work)
654 struct hda_codec *codec =
655 container_of(work, struct hda_codec, jackpoll_work.work);
657 snd_hda_jack_set_dirty_all(codec);
658 snd_hda_jack_poll_all(codec);
660 if (!codec->jackpoll_interval)
663 schedule_delayed_work(&codec->jackpoll_work,
664 codec->jackpoll_interval);
667 /* release all pincfg lists */
668 static void free_init_pincfgs(struct hda_codec *codec)
670 snd_array_free(&codec->driver_pins);
671 #ifdef CONFIG_SND_HDA_RECONFIG
672 snd_array_free(&codec->user_pins);
674 snd_array_free(&codec->init_pins);
678 * audio-converter setup caches
680 struct hda_cvt_setup {
685 unsigned char active; /* cvt is currently used */
686 unsigned char dirty; /* setups should be cleared */
689 /* get or create a cache entry for the given audio converter NID */
690 static struct hda_cvt_setup *
691 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
693 struct hda_cvt_setup *p;
696 snd_array_for_each(&codec->cvt_setups, i, p) {
700 p = snd_array_new(&codec->cvt_setups);
709 static void release_pcm(struct kref *kref)
711 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
714 snd_device_free(pcm->codec->card, pcm->pcm);
715 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
720 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
722 kref_put(&pcm->kref, release_pcm);
724 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
726 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
727 const char *fmt, ...)
732 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
737 kref_init(&pcm->kref);
739 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
746 list_add_tail(&pcm->list, &codec->pcm_list_head);
749 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
754 static void codec_release_pcms(struct hda_codec *codec)
756 struct hda_pcm *pcm, *n;
758 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
759 list_del_init(&pcm->list);
761 snd_device_disconnect(codec->card, pcm->pcm);
762 snd_hda_codec_pcm_put(pcm);
766 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
768 if (codec->registered) {
769 /* pm_runtime_put() is called in snd_hdac_device_exit() */
770 pm_runtime_get_noresume(hda_codec_dev(codec));
771 pm_runtime_disable(hda_codec_dev(codec));
772 codec->registered = 0;
775 cancel_delayed_work_sync(&codec->jackpoll_work);
776 if (!codec->in_freeing)
777 snd_hda_ctls_clear(codec);
778 codec_release_pcms(codec);
779 snd_hda_detach_beep_device(codec);
780 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
781 snd_hda_jack_tbl_clear(codec);
782 codec->proc_widget_hook = NULL;
785 /* free only driver_pins so that init_pins + user_pins are restored */
786 snd_array_free(&codec->driver_pins);
787 snd_array_free(&codec->cvt_setups);
788 snd_array_free(&codec->spdif_out);
789 snd_array_free(&codec->verbs);
790 codec->preset = NULL;
791 codec->slave_dig_outs = NULL;
792 codec->spdif_status_reset = 0;
793 snd_array_free(&codec->mixers);
794 snd_array_free(&codec->nids);
795 remove_conn_list(codec);
796 snd_hdac_regmap_exit(&codec->core);
799 static unsigned int hda_set_power_state(struct hda_codec *codec,
800 unsigned int power_state);
802 /* also called from hda_bind.c */
803 void snd_hda_codec_register(struct hda_codec *codec)
805 if (codec->registered)
807 if (device_is_registered(hda_codec_dev(codec))) {
808 snd_hda_register_beep_device(codec);
809 snd_hdac_link_power(&codec->core, true);
810 pm_runtime_enable(hda_codec_dev(codec));
811 /* it was powered up in snd_hda_codec_new(), now all done */
812 snd_hda_power_down(codec);
813 codec->registered = 1;
817 static int snd_hda_codec_dev_register(struct snd_device *device)
819 snd_hda_codec_register(device->device_data);
823 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
825 struct hda_codec *codec = device->device_data;
827 snd_hda_detach_beep_device(codec);
831 static int snd_hda_codec_dev_free(struct snd_device *device)
833 struct hda_codec *codec = device->device_data;
835 codec->in_freeing = 1;
836 snd_hdac_device_unregister(&codec->core);
837 snd_hdac_link_power(&codec->core, false);
838 put_device(hda_codec_dev(codec));
842 static void snd_hda_codec_dev_release(struct device *dev)
844 struct hda_codec *codec = dev_to_hda_codec(dev);
846 free_init_pincfgs(codec);
847 snd_hdac_device_exit(&codec->core);
848 snd_hda_sysfs_clear(codec);
849 kfree(codec->modelname);
854 #define DEV_NAME_LEN 31
856 static int snd_hda_codec_device_init(struct hda_bus *bus, struct snd_card *card,
857 unsigned int codec_addr, struct hda_codec **codecp)
859 char name[DEV_NAME_LEN];
860 struct hda_codec *codec;
863 dev_dbg(card->dev, "%s: entry\n", __func__);
865 if (snd_BUG_ON(!bus))
867 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
870 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
874 sprintf(name, "hdaudioC%dD%d", card->number, codec_addr);
875 err = snd_hdac_device_init(&codec->core, &bus->core, name, codec_addr);
881 codec->core.type = HDA_DEV_LEGACY;
888 * snd_hda_codec_new - create a HDA codec
889 * @bus: the bus to assign
890 * @codec_addr: the codec address
891 * @codecp: the pointer to store the generated codec
893 * Returns 0 if successful, or a negative error code.
895 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
896 unsigned int codec_addr, struct hda_codec **codecp)
900 ret = snd_hda_codec_device_init(bus, card, codec_addr, codecp);
904 return snd_hda_codec_device_new(bus, card, codec_addr, *codecp);
906 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
908 int snd_hda_codec_device_new(struct hda_bus *bus, struct snd_card *card,
909 unsigned int codec_addr, struct hda_codec *codec)
914 static struct snd_device_ops dev_ops = {
915 .dev_register = snd_hda_codec_dev_register,
916 .dev_disconnect = snd_hda_codec_dev_disconnect,
917 .dev_free = snd_hda_codec_dev_free,
920 dev_dbg(card->dev, "%s: entry\n", __func__);
922 if (snd_BUG_ON(!bus))
924 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
927 codec->core.dev.release = snd_hda_codec_dev_release;
928 codec->core.exec_verb = codec_exec_verb;
932 codec->addr = codec_addr;
933 mutex_init(&codec->spdif_mutex);
934 mutex_init(&codec->control_mutex);
935 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
936 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
937 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
938 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
939 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
940 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
941 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
942 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
943 INIT_LIST_HEAD(&codec->conn_list);
944 INIT_LIST_HEAD(&codec->pcm_list_head);
946 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
947 codec->depop_delay = -1;
948 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
951 codec->power_jiffies = jiffies;
954 snd_hda_sysfs_init(codec);
956 if (codec->bus->modelname) {
957 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
958 if (!codec->modelname) {
964 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
965 err = read_widget_caps(codec, fg);
968 err = read_pin_defaults(codec);
972 /* power-up all before initialization */
973 hda_set_power_state(codec, AC_PWRST_D0);
975 snd_hda_codec_proc_new(codec);
977 snd_hda_create_hwdep(codec);
979 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
980 codec->core.subsystem_id, codec->core.revision_id);
981 snd_component_add(card, component);
983 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
990 put_device(hda_codec_dev(codec));
993 EXPORT_SYMBOL_GPL(snd_hda_codec_device_new);
996 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
997 * @codec: the HDA codec
999 * Forcibly refresh the all widget caps and the init pin configurations of
1002 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1007 err = snd_hdac_refresh_widgets(&codec->core, true);
1011 /* Assume the function group node does not change,
1012 * only the widget nodes may change.
1014 kfree(codec->wcaps);
1015 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1016 err = read_widget_caps(codec, fg);
1020 snd_array_free(&codec->init_pins);
1021 err = read_pin_defaults(codec);
1025 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1027 /* update the stream-id if changed */
1028 static void update_pcm_stream_id(struct hda_codec *codec,
1029 struct hda_cvt_setup *p, hda_nid_t nid,
1030 u32 stream_tag, int channel_id)
1032 unsigned int oldval, newval;
1034 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1035 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1036 newval = (stream_tag << 4) | channel_id;
1037 if (oldval != newval)
1038 snd_hda_codec_write(codec, nid, 0,
1039 AC_VERB_SET_CHANNEL_STREAMID,
1041 p->stream_tag = stream_tag;
1042 p->channel_id = channel_id;
1046 /* update the format-id if changed */
1047 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1048 hda_nid_t nid, int format)
1050 unsigned int oldval;
1052 if (p->format_id != format) {
1053 oldval = snd_hda_codec_read(codec, nid, 0,
1054 AC_VERB_GET_STREAM_FORMAT, 0);
1055 if (oldval != format) {
1057 snd_hda_codec_write(codec, nid, 0,
1058 AC_VERB_SET_STREAM_FORMAT,
1061 p->format_id = format;
1066 * snd_hda_codec_setup_stream - set up the codec for streaming
1067 * @codec: the CODEC to set up
1068 * @nid: the NID to set up
1069 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1070 * @channel_id: channel id to pass, zero based.
1071 * @format: stream format.
1073 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1075 int channel_id, int format)
1077 struct hda_codec *c;
1078 struct hda_cvt_setup *p;
1086 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1087 nid, stream_tag, channel_id, format);
1088 p = get_hda_cvt_setup(codec, nid);
1092 if (codec->patch_ops.stream_pm)
1093 codec->patch_ops.stream_pm(codec, nid, true);
1094 if (codec->pcm_format_first)
1095 update_pcm_format(codec, p, nid, format);
1096 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1097 if (!codec->pcm_format_first)
1098 update_pcm_format(codec, p, nid, format);
1103 /* make other inactive cvts with the same stream-tag dirty */
1104 type = get_wcaps_type(get_wcaps(codec, nid));
1105 list_for_each_codec(c, codec->bus) {
1106 snd_array_for_each(&c->cvt_setups, i, p) {
1107 if (!p->active && p->stream_tag == stream_tag &&
1108 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1113 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1115 static void really_cleanup_stream(struct hda_codec *codec,
1116 struct hda_cvt_setup *q);
1119 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1120 * @codec: the CODEC to clean up
1121 * @nid: the NID to clean up
1122 * @do_now: really clean up the stream instead of clearing the active flag
1124 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1127 struct hda_cvt_setup *p;
1132 if (codec->no_sticky_stream)
1135 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1136 p = get_hda_cvt_setup(codec, nid);
1138 /* here we just clear the active flag when do_now isn't set;
1139 * actual clean-ups will be done later in
1140 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1143 really_cleanup_stream(codec, p);
1148 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1150 static void really_cleanup_stream(struct hda_codec *codec,
1151 struct hda_cvt_setup *q)
1153 hda_nid_t nid = q->nid;
1154 if (q->stream_tag || q->channel_id)
1155 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1157 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1159 memset(q, 0, sizeof(*q));
1161 if (codec->patch_ops.stream_pm)
1162 codec->patch_ops.stream_pm(codec, nid, false);
1165 /* clean up the all conflicting obsolete streams */
1166 static void purify_inactive_streams(struct hda_codec *codec)
1168 struct hda_codec *c;
1169 struct hda_cvt_setup *p;
1172 list_for_each_codec(c, codec->bus) {
1173 snd_array_for_each(&c->cvt_setups, i, p) {
1175 really_cleanup_stream(c, p);
1181 /* clean up all streams; called from suspend */
1182 static void hda_cleanup_all_streams(struct hda_codec *codec)
1184 struct hda_cvt_setup *p;
1187 snd_array_for_each(&codec->cvt_setups, i, p) {
1189 really_cleanup_stream(codec, p);
1195 * amp access functions
1199 * query_amp_caps - query AMP capabilities
1200 * @codec: the HD-auio codec
1201 * @nid: the NID to query
1202 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1204 * Query AMP capabilities for the given widget and direction.
1205 * Returns the obtained capability bits.
1207 * When cap bits have been already read, this doesn't read again but
1208 * returns the cached value.
1210 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1212 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1213 nid = codec->core.afg;
1214 return snd_hda_param_read(codec, nid,
1215 direction == HDA_OUTPUT ?
1216 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1218 EXPORT_SYMBOL_GPL(query_amp_caps);
1221 * snd_hda_check_amp_caps - query AMP capabilities
1222 * @codec: the HD-audio codec
1223 * @nid: the NID to query
1224 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1225 * @bits: bit mask to check the result
1227 * Check whether the widget has the given amp capability for the direction.
1229 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1230 int dir, unsigned int bits)
1234 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1235 if (query_amp_caps(codec, nid, dir) & bits)
1239 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1242 * snd_hda_override_amp_caps - Override the AMP capabilities
1243 * @codec: the CODEC to clean up
1244 * @nid: the NID to clean up
1245 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1246 * @caps: the capability bits to set
1248 * Override the cached AMP caps bits value by the given one.
1249 * This function is useful if the driver needs to adjust the AMP ranges,
1250 * e.g. limit to 0dB, etc.
1252 * Returns zero if successful or a negative error code.
1254 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1259 snd_hda_override_wcaps(codec, nid,
1260 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1261 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1262 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1264 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1267 * snd_hda_codec_amp_update - update the AMP mono value
1268 * @codec: HD-audio codec
1269 * @nid: NID to read the AMP value
1270 * @ch: channel to update (0 or 1)
1271 * @dir: #HDA_INPUT or #HDA_OUTPUT
1272 * @idx: the index value (only for input direction)
1273 * @mask: bit mask to set
1274 * @val: the bits value to set
1276 * Update the AMP values for the given channel, direction and index.
1278 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1279 int ch, int dir, int idx, int mask, int val)
1281 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1283 /* enable fake mute if no h/w mute but min=mute */
1284 if ((query_amp_caps(codec, nid, dir) &
1285 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1286 cmd |= AC_AMP_FAKE_MUTE;
1287 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1289 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1292 * snd_hda_codec_amp_stereo - update the AMP stereo values
1293 * @codec: HD-audio codec
1294 * @nid: NID to read the AMP value
1295 * @direction: #HDA_INPUT or #HDA_OUTPUT
1296 * @idx: the index value (only for input direction)
1297 * @mask: bit mask to set
1298 * @val: the bits value to set
1300 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1301 * stereo widget with the same mask and value.
1303 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1304 int direction, int idx, int mask, int val)
1308 if (snd_BUG_ON(mask & ~0xff))
1310 for (ch = 0; ch < 2; ch++)
1311 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1315 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1318 * snd_hda_codec_amp_init - initialize the AMP value
1319 * @codec: the HDA codec
1320 * @nid: NID to read the AMP value
1321 * @ch: channel (left=0 or right=1)
1322 * @dir: #HDA_INPUT or #HDA_OUTPUT
1323 * @idx: the index value (only for input direction)
1324 * @mask: bit mask to set
1325 * @val: the bits value to set
1327 * Works like snd_hda_codec_amp_update() but it writes the value only at
1328 * the first access. If the amp was already initialized / updated beforehand,
1329 * this does nothing.
1331 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1332 int dir, int idx, int mask, int val)
1336 if (!codec->core.regmap)
1338 regcache_cache_only(codec->core.regmap, true);
1339 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1340 regcache_cache_only(codec->core.regmap, false);
1343 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1345 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1348 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1349 * @codec: the HDA codec
1350 * @nid: NID to read the AMP value
1351 * @dir: #HDA_INPUT or #HDA_OUTPUT
1352 * @idx: the index value (only for input direction)
1353 * @mask: bit mask to set
1354 * @val: the bits value to set
1356 * Call snd_hda_codec_amp_init() for both stereo channels.
1358 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1359 int dir, int idx, int mask, int val)
1363 if (snd_BUG_ON(mask & ~0xff))
1365 for (ch = 0; ch < 2; ch++)
1366 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1370 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1372 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1375 u32 caps = query_amp_caps(codec, nid, dir);
1377 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1384 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1385 * @kcontrol: referred ctl element
1386 * @uinfo: pointer to get/store the data
1388 * The control element is supposed to have the private_value field
1389 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1391 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1392 struct snd_ctl_elem_info *uinfo)
1394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1395 u16 nid = get_amp_nid(kcontrol);
1396 u8 chs = get_amp_channels(kcontrol);
1397 int dir = get_amp_direction(kcontrol);
1398 unsigned int ofs = get_amp_offset(kcontrol);
1400 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1401 uinfo->count = chs == 3 ? 2 : 1;
1402 uinfo->value.integer.min = 0;
1403 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1404 if (!uinfo->value.integer.max) {
1406 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1407 nid, kcontrol->id.name);
1412 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1415 static inline unsigned int
1416 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1417 int ch, int dir, int idx, unsigned int ofs)
1420 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1421 val &= HDA_AMP_VOLMASK;
1430 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1431 int ch, int dir, int idx, unsigned int ofs,
1434 unsigned int maxval;
1438 /* ofs = 0: raw max value */
1439 maxval = get_amp_max_value(codec, nid, dir, 0);
1442 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1443 HDA_AMP_VOLMASK, val);
1447 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1448 * @kcontrol: ctl element
1449 * @ucontrol: pointer to get/store the data
1451 * The control element is supposed to have the private_value field
1452 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1454 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1455 struct snd_ctl_elem_value *ucontrol)
1457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1458 hda_nid_t nid = get_amp_nid(kcontrol);
1459 int chs = get_amp_channels(kcontrol);
1460 int dir = get_amp_direction(kcontrol);
1461 int idx = get_amp_index(kcontrol);
1462 unsigned int ofs = get_amp_offset(kcontrol);
1463 long *valp = ucontrol->value.integer.value;
1466 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1468 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1471 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1474 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1475 * @kcontrol: ctl element
1476 * @ucontrol: pointer to get/store the data
1478 * The control element is supposed to have the private_value field
1479 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1481 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_value *ucontrol)
1484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1485 hda_nid_t nid = get_amp_nid(kcontrol);
1486 int chs = get_amp_channels(kcontrol);
1487 int dir = get_amp_direction(kcontrol);
1488 int idx = get_amp_index(kcontrol);
1489 unsigned int ofs = get_amp_offset(kcontrol);
1490 long *valp = ucontrol->value.integer.value;
1494 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1498 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1501 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1503 /* inquiry the amp caps and convert to TLV */
1504 static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
1506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1507 hda_nid_t nid = get_amp_nid(kcontrol);
1508 int dir = get_amp_direction(kcontrol);
1509 unsigned int ofs = get_amp_offset(kcontrol);
1510 bool min_mute = get_amp_min_mute(kcontrol);
1511 u32 caps, val1, val2;
1513 caps = query_amp_caps(codec, nid, dir);
1514 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1515 val2 = (val2 + 1) * 25;
1516 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1518 val1 = ((int)val1) * ((int)val2);
1519 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1520 val2 |= TLV_DB_SCALE_MUTE;
1521 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
1522 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
1523 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = val1;
1524 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = val2;
1528 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
1529 * @kcontrol: ctl element
1530 * @op_flag: operation flag
1531 * @size: byte size of input TLV
1534 * The control element is supposed to have the private_value field
1535 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1537 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1538 unsigned int size, unsigned int __user *_tlv)
1540 unsigned int tlv[4];
1542 if (size < 4 * sizeof(unsigned int))
1544 get_ctl_amp_tlv(kcontrol, tlv);
1545 if (copy_to_user(_tlv, tlv, sizeof(tlv)))
1549 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1552 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1553 * @codec: HD-audio codec
1554 * @nid: NID of a reference widget
1555 * @dir: #HDA_INPUT or #HDA_OUTPUT
1556 * @tlv: TLV data to be stored, at least 4 elements
1558 * Set (static) TLV data for a virtual master volume using the AMP caps
1559 * obtained from the reference NID.
1560 * The volume range is recalculated as if the max volume is 0dB.
1562 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1568 caps = query_amp_caps(codec, nid, dir);
1569 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1570 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1571 step = (step + 1) * 25;
1572 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
1573 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
1574 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = -nums * step;
1575 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = step;
1577 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1579 /* find a mixer control element with the given name */
1580 static struct snd_kcontrol *
1581 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1583 struct snd_ctl_elem_id id;
1584 memset(&id, 0, sizeof(id));
1585 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1588 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1590 strcpy(id.name, name);
1591 return snd_ctl_find_id(codec->card, &id);
1595 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1596 * @codec: HD-audio codec
1597 * @name: ctl id name string
1599 * Get the control element with the given id string and IFACE_MIXER.
1601 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1604 return find_mixer_ctl(codec, name, 0, 0);
1606 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1608 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1612 /* 16 ctlrs should be large enough */
1613 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1614 if (!find_mixer_ctl(codec, name, 0, idx))
1621 * snd_hda_ctl_add - Add a control element and assign to the codec
1622 * @codec: HD-audio codec
1623 * @nid: corresponding NID (optional)
1624 * @kctl: the control element to assign
1626 * Add the given control element to an array inside the codec instance.
1627 * All control elements belonging to a codec are supposed to be added
1628 * by this function so that a proper clean-up works at the free or
1629 * reconfiguration time.
1631 * If non-zero @nid is passed, the NID is assigned to the control element.
1632 * The assignment is shown in the codec proc file.
1634 * snd_hda_ctl_add() checks the control subdev id field whether
1635 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1636 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1637 * specifies if kctl->private_value is a HDA amplifier value.
1639 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1640 struct snd_kcontrol *kctl)
1643 unsigned short flags = 0;
1644 struct hda_nid_item *item;
1646 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1647 flags |= HDA_NID_ITEM_AMP;
1649 nid = get_amp_nid_(kctl->private_value);
1651 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1652 nid = kctl->id.subdevice & 0xffff;
1653 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1654 kctl->id.subdevice = 0;
1655 err = snd_ctl_add(codec->card, kctl);
1658 item = snd_array_new(&codec->mixers);
1663 item->flags = flags;
1666 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1669 * snd_hda_add_nid - Assign a NID to a control element
1670 * @codec: HD-audio codec
1671 * @nid: corresponding NID (optional)
1672 * @kctl: the control element to assign
1673 * @index: index to kctl
1675 * Add the given control element to an array inside the codec instance.
1676 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1677 * NID:KCTL mapping - for example "Capture Source" selector.
1679 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1680 unsigned int index, hda_nid_t nid)
1682 struct hda_nid_item *item;
1685 item = snd_array_new(&codec->nids);
1689 item->index = index;
1693 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1694 kctl->id.name, kctl->id.index, index);
1697 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1700 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1701 * @codec: HD-audio codec
1703 void snd_hda_ctls_clear(struct hda_codec *codec)
1706 struct hda_nid_item *items = codec->mixers.list;
1707 for (i = 0; i < codec->mixers.used; i++)
1708 snd_ctl_remove(codec->card, items[i].kctl);
1709 snd_array_free(&codec->mixers);
1710 snd_array_free(&codec->nids);
1714 * snd_hda_lock_devices - pseudo device locking
1717 * toggle card->shutdown to allow/disallow the device access (as a hack)
1719 int snd_hda_lock_devices(struct hda_bus *bus)
1721 struct snd_card *card = bus->card;
1722 struct hda_codec *codec;
1724 spin_lock(&card->files_lock);
1728 if (!list_empty(&card->ctl_files))
1731 list_for_each_codec(codec, bus) {
1732 struct hda_pcm *cpcm;
1733 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1736 if (cpcm->pcm->streams[0].substream_opened ||
1737 cpcm->pcm->streams[1].substream_opened)
1741 spin_unlock(&card->files_lock);
1747 spin_unlock(&card->files_lock);
1750 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1753 * snd_hda_unlock_devices - pseudo device unlocking
1756 void snd_hda_unlock_devices(struct hda_bus *bus)
1758 struct snd_card *card = bus->card;
1760 spin_lock(&card->files_lock);
1762 spin_unlock(&card->files_lock);
1764 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1767 * snd_hda_codec_reset - Clear all objects assigned to the codec
1768 * @codec: HD-audio codec
1770 * This frees the all PCM and control elements assigned to the codec, and
1771 * clears the caches and restores the pin default configurations.
1773 * When a device is being used, it returns -EBSY. If successfully freed,
1776 int snd_hda_codec_reset(struct hda_codec *codec)
1778 struct hda_bus *bus = codec->bus;
1780 if (snd_hda_lock_devices(bus) < 0)
1783 /* OK, let it free */
1784 snd_hdac_device_unregister(&codec->core);
1786 /* allow device access again */
1787 snd_hda_unlock_devices(bus);
1791 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1793 /* apply the function to all matching slave ctls in the mixer list */
1794 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1795 const char *suffix, map_slave_func_t func, void *data)
1797 struct hda_nid_item *items;
1798 const char * const *s;
1801 items = codec->mixers.list;
1802 for (i = 0; i < codec->mixers.used; i++) {
1803 struct snd_kcontrol *sctl = items[i].kctl;
1804 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1806 for (s = slaves; *s; s++) {
1807 char tmpname[sizeof(sctl->id.name)];
1808 const char *name = *s;
1810 snprintf(tmpname, sizeof(tmpname), "%s %s",
1814 if (!strcmp(sctl->id.name, name)) {
1815 err = func(codec, data, sctl);
1825 static int check_slave_present(struct hda_codec *codec,
1826 void *data, struct snd_kcontrol *sctl)
1831 /* call kctl->put with the given value(s) */
1832 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1834 struct snd_ctl_elem_value *ucontrol;
1835 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1838 ucontrol->value.integer.value[0] = val;
1839 ucontrol->value.integer.value[1] = val;
1840 kctl->put(kctl, ucontrol);
1845 struct slave_init_arg {
1846 struct hda_codec *codec;
1850 /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */
1851 static int init_slave_0dB(struct snd_kcontrol *slave,
1852 struct snd_kcontrol *kctl,
1855 struct slave_init_arg *arg = _arg;
1857 const int *tlv = NULL;
1861 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1862 if (kctl->tlv.c != snd_hda_mixer_amp_tlv) {
1863 codec_err(arg->codec,
1864 "Unexpected TLV callback for slave %s:%d\n",
1865 kctl->id.name, kctl->id.index);
1866 return 0; /* ignore */
1868 get_ctl_amp_tlv(kctl, _tlv);
1870 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1873 if (!tlv || tlv[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
1876 step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP];
1877 step &= ~TLV_DB_SCALE_MUTE;
1880 if (arg->step && arg->step != step) {
1881 codec_err(arg->codec,
1882 "Mismatching dB step for vmaster slave (%d!=%d)\n",
1888 val = -tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] / step;
1890 put_kctl_with_value(slave, val);
1897 /* unmute the slave via snd_ctl_apply_vmaster_slaves() */
1898 static int init_slave_unmute(struct snd_kcontrol *slave,
1899 struct snd_kcontrol *kctl,
1902 return put_kctl_with_value(slave, 1);
1905 static int add_slave(struct hda_codec *codec,
1906 void *data, struct snd_kcontrol *slave)
1908 return snd_ctl_add_slave(data, slave);
1912 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1913 * @codec: HD-audio codec
1914 * @name: vmaster control name
1915 * @tlv: TLV data (optional)
1916 * @slaves: slave control names (optional)
1917 * @suffix: suffix string to each slave name (optional)
1918 * @init_slave_vol: initialize slaves to unmute/0dB
1919 * @ctl_ret: store the vmaster kcontrol in return
1921 * Create a virtual master control with the given name. The TLV data
1922 * must be either NULL or a valid data.
1924 * @slaves is a NULL-terminated array of strings, each of which is a
1925 * slave control name. All controls with these names are assigned to
1926 * the new virtual master control.
1928 * This function returns zero if successful or a negative error code.
1930 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1931 unsigned int *tlv, const char * const *slaves,
1932 const char *suffix, bool init_slave_vol,
1933 struct snd_kcontrol **ctl_ret)
1935 struct snd_kcontrol *kctl;
1941 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1943 codec_dbg(codec, "No slave found for %s\n", name);
1946 kctl = snd_ctl_make_virtual_master(name, tlv);
1949 err = snd_hda_ctl_add(codec, 0, kctl);
1953 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1957 /* init with master mute & zero volume */
1958 put_kctl_with_value(kctl, 0);
1959 if (init_slave_vol) {
1960 struct slave_init_arg arg = {
1964 snd_ctl_apply_vmaster_slaves(kctl,
1965 tlv ? init_slave_0dB : init_slave_unmute,
1973 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1976 * mute-LED control using vmaster
1978 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_info *uinfo)
1981 static const char * const texts[] = {
1982 "On", "Off", "Follow Master"
1985 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1988 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
1991 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1992 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1996 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1997 struct snd_ctl_elem_value *ucontrol)
1999 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2000 unsigned int old_mode = hook->mute_mode;
2002 hook->mute_mode = ucontrol->value.enumerated.item[0];
2003 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2004 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2005 if (old_mode == hook->mute_mode)
2007 snd_hda_sync_vmaster_hook(hook);
2011 static const struct snd_kcontrol_new vmaster_mute_mode = {
2012 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2013 .name = "Mute-LED Mode",
2014 .info = vmaster_mute_mode_info,
2015 .get = vmaster_mute_mode_get,
2016 .put = vmaster_mute_mode_put,
2019 /* meta hook to call each driver's vmaster hook */
2020 static void vmaster_hook(void *private_data, int enabled)
2022 struct hda_vmaster_mute_hook *hook = private_data;
2024 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2025 enabled = hook->mute_mode;
2026 hook->hook(hook->codec, enabled);
2030 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2031 * @codec: the HDA codec
2032 * @hook: the vmaster hook object
2033 * @expose_enum_ctl: flag to create an enum ctl
2035 * Add a mute-LED hook with the given vmaster switch kctl.
2036 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2037 * created and associated with the given hook.
2039 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2040 struct hda_vmaster_mute_hook *hook,
2041 bool expose_enum_ctl)
2043 struct snd_kcontrol *kctl;
2045 if (!hook->hook || !hook->sw_kctl)
2047 hook->codec = codec;
2048 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2049 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2050 if (!expose_enum_ctl)
2052 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2055 return snd_hda_ctl_add(codec, 0, kctl);
2057 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2060 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2061 * @hook: the vmaster hook
2063 * Call the hook with the current value for synchronization.
2064 * Should be called in init callback.
2066 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2068 if (!hook->hook || !hook->codec)
2070 /* don't call vmaster hook in the destructor since it might have
2071 * been already destroyed
2073 if (hook->codec->bus->shutdown)
2075 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2077 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2081 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2082 * @kcontrol: referred ctl element
2083 * @uinfo: pointer to get/store the data
2085 * The control element is supposed to have the private_value field
2086 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2088 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *uinfo)
2091 int chs = get_amp_channels(kcontrol);
2093 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2094 uinfo->count = chs == 3 ? 2 : 1;
2095 uinfo->value.integer.min = 0;
2096 uinfo->value.integer.max = 1;
2099 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2102 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2103 * @kcontrol: ctl element
2104 * @ucontrol: pointer to get/store the data
2106 * The control element is supposed to have the private_value field
2107 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2109 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_value *ucontrol)
2112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2113 hda_nid_t nid = get_amp_nid(kcontrol);
2114 int chs = get_amp_channels(kcontrol);
2115 int dir = get_amp_direction(kcontrol);
2116 int idx = get_amp_index(kcontrol);
2117 long *valp = ucontrol->value.integer.value;
2120 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2121 HDA_AMP_MUTE) ? 0 : 1;
2123 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2124 HDA_AMP_MUTE) ? 0 : 1;
2127 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2130 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2131 * @kcontrol: ctl element
2132 * @ucontrol: pointer to get/store the data
2134 * The control element is supposed to have the private_value field
2135 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2137 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2141 hda_nid_t nid = get_amp_nid(kcontrol);
2142 int chs = get_amp_channels(kcontrol);
2143 int dir = get_amp_direction(kcontrol);
2144 int idx = get_amp_index(kcontrol);
2145 long *valp = ucontrol->value.integer.value;
2149 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2151 *valp ? 0 : HDA_AMP_MUTE);
2155 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2157 *valp ? 0 : HDA_AMP_MUTE);
2158 hda_call_check_power_status(codec, nid);
2161 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2164 * SPDIF out controls
2167 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_info *uinfo)
2170 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2175 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2178 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2179 IEC958_AES0_NONAUDIO |
2180 IEC958_AES0_CON_EMPHASIS_5015 |
2181 IEC958_AES0_CON_NOT_COPYRIGHT;
2182 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2183 IEC958_AES1_CON_ORIGINAL;
2187 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
2190 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2191 IEC958_AES0_NONAUDIO |
2192 IEC958_AES0_PRO_EMPHASIS_5015;
2196 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_value *ucontrol)
2199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2200 int idx = kcontrol->private_value;
2201 struct hda_spdif_out *spdif;
2203 if (WARN_ON(codec->spdif_out.used <= idx))
2205 mutex_lock(&codec->spdif_mutex);
2206 spdif = snd_array_elem(&codec->spdif_out, idx);
2207 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2208 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2209 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2210 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2211 mutex_unlock(&codec->spdif_mutex);
2216 /* convert from SPDIF status bits to HDA SPDIF bits
2217 * bit 0 (DigEn) is always set zero (to be filled later)
2219 static unsigned short convert_from_spdif_status(unsigned int sbits)
2221 unsigned short val = 0;
2223 if (sbits & IEC958_AES0_PROFESSIONAL)
2224 val |= AC_DIG1_PROFESSIONAL;
2225 if (sbits & IEC958_AES0_NONAUDIO)
2226 val |= AC_DIG1_NONAUDIO;
2227 if (sbits & IEC958_AES0_PROFESSIONAL) {
2228 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2229 IEC958_AES0_PRO_EMPHASIS_5015)
2230 val |= AC_DIG1_EMPHASIS;
2232 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2233 IEC958_AES0_CON_EMPHASIS_5015)
2234 val |= AC_DIG1_EMPHASIS;
2235 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2236 val |= AC_DIG1_COPYRIGHT;
2237 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2238 val |= AC_DIG1_LEVEL;
2239 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2244 /* convert to SPDIF status bits from HDA SPDIF bits
2246 static unsigned int convert_to_spdif_status(unsigned short val)
2248 unsigned int sbits = 0;
2250 if (val & AC_DIG1_NONAUDIO)
2251 sbits |= IEC958_AES0_NONAUDIO;
2252 if (val & AC_DIG1_PROFESSIONAL)
2253 sbits |= IEC958_AES0_PROFESSIONAL;
2254 if (sbits & IEC958_AES0_PROFESSIONAL) {
2255 if (val & AC_DIG1_EMPHASIS)
2256 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2258 if (val & AC_DIG1_EMPHASIS)
2259 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2260 if (!(val & AC_DIG1_COPYRIGHT))
2261 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2262 if (val & AC_DIG1_LEVEL)
2263 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2264 sbits |= val & (0x7f << 8);
2269 /* set digital convert verbs both for the given NID and its slaves */
2270 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2275 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2277 d = codec->slave_dig_outs;
2281 snd_hdac_regmap_update(&codec->core, *d,
2282 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2285 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2288 unsigned int mask = 0;
2289 unsigned int val = 0;
2299 set_dig_out(codec, nid, mask, val);
2302 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2303 struct snd_ctl_elem_value *ucontrol)
2305 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2306 int idx = kcontrol->private_value;
2307 struct hda_spdif_out *spdif;
2312 if (WARN_ON(codec->spdif_out.used <= idx))
2314 mutex_lock(&codec->spdif_mutex);
2315 spdif = snd_array_elem(&codec->spdif_out, idx);
2317 spdif->status = ucontrol->value.iec958.status[0] |
2318 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2319 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2320 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2321 val = convert_from_spdif_status(spdif->status);
2322 val |= spdif->ctls & 1;
2323 change = spdif->ctls != val;
2325 if (change && nid != (u16)-1)
2326 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2327 mutex_unlock(&codec->spdif_mutex);
2331 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2333 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 int idx = kcontrol->private_value;
2338 struct hda_spdif_out *spdif;
2340 if (WARN_ON(codec->spdif_out.used <= idx))
2342 mutex_lock(&codec->spdif_mutex);
2343 spdif = snd_array_elem(&codec->spdif_out, idx);
2344 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2345 mutex_unlock(&codec->spdif_mutex);
2349 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2352 set_dig_out_convert(codec, nid, dig1, dig2);
2353 /* unmute amp switch (if any) */
2354 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2355 (dig1 & AC_DIG1_ENABLE))
2356 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2360 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
2363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2364 int idx = kcontrol->private_value;
2365 struct hda_spdif_out *spdif;
2370 if (WARN_ON(codec->spdif_out.used <= idx))
2372 mutex_lock(&codec->spdif_mutex);
2373 spdif = snd_array_elem(&codec->spdif_out, idx);
2375 val = spdif->ctls & ~AC_DIG1_ENABLE;
2376 if (ucontrol->value.integer.value[0])
2377 val |= AC_DIG1_ENABLE;
2378 change = spdif->ctls != val;
2380 if (change && nid != (u16)-1)
2381 set_spdif_ctls(codec, nid, val & 0xff, -1);
2382 mutex_unlock(&codec->spdif_mutex);
2386 static struct snd_kcontrol_new dig_mixes[] = {
2388 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2389 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2390 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2391 .info = snd_hda_spdif_mask_info,
2392 .get = snd_hda_spdif_cmask_get,
2395 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2396 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2397 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2398 .info = snd_hda_spdif_mask_info,
2399 .get = snd_hda_spdif_pmask_get,
2402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2403 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2404 .info = snd_hda_spdif_mask_info,
2405 .get = snd_hda_spdif_default_get,
2406 .put = snd_hda_spdif_default_put,
2409 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2410 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2411 .info = snd_hda_spdif_out_switch_info,
2412 .get = snd_hda_spdif_out_switch_get,
2413 .put = snd_hda_spdif_out_switch_put,
2419 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2420 * @codec: the HDA codec
2421 * @associated_nid: NID that new ctls associated with
2422 * @cvt_nid: converter NID
2423 * @type: HDA_PCM_TYPE_*
2424 * Creates controls related with the digital output.
2425 * Called from each patch supporting the digital out.
2427 * Returns 0 if successful, or a negative error code.
2429 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2430 hda_nid_t associated_nid,
2435 struct snd_kcontrol *kctl;
2436 struct snd_kcontrol_new *dig_mix;
2439 const int spdif_index = 16;
2440 struct hda_spdif_out *spdif;
2441 struct hda_bus *bus = codec->bus;
2443 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2444 type == HDA_PCM_TYPE_SPDIF) {
2446 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2447 type == HDA_PCM_TYPE_HDMI) {
2448 /* suppose a single SPDIF device */
2449 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2450 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2453 kctl->id.index = spdif_index;
2455 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2457 if (!bus->primary_dig_out_type)
2458 bus->primary_dig_out_type = type;
2460 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2462 codec_err(codec, "too many IEC958 outputs\n");
2465 spdif = snd_array_new(&codec->spdif_out);
2468 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2469 kctl = snd_ctl_new1(dig_mix, codec);
2472 kctl->id.index = idx;
2473 kctl->private_value = codec->spdif_out.used - 1;
2474 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2478 spdif->nid = cvt_nid;
2479 snd_hdac_regmap_read(&codec->core, cvt_nid,
2480 AC_VERB_GET_DIGI_CONVERT_1, &val);
2482 spdif->status = convert_to_spdif_status(spdif->ctls);
2485 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2488 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2489 * @codec: the HDA codec
2492 * call within spdif_mutex lock
2494 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2497 struct hda_spdif_out *spdif;
2500 snd_array_for_each(&codec->spdif_out, i, spdif) {
2501 if (spdif->nid == nid)
2506 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2509 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2510 * @codec: the HDA codec
2511 * @idx: the SPDIF ctl index
2513 * Unassign the widget from the given SPDIF control.
2515 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2517 struct hda_spdif_out *spdif;
2519 if (WARN_ON(codec->spdif_out.used <= idx))
2521 mutex_lock(&codec->spdif_mutex);
2522 spdif = snd_array_elem(&codec->spdif_out, idx);
2523 spdif->nid = (u16)-1;
2524 mutex_unlock(&codec->spdif_mutex);
2526 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2529 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2530 * @codec: the HDA codec
2531 * @idx: the SPDIF ctl idx
2534 * Assign the widget to the SPDIF control with the given index.
2536 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2538 struct hda_spdif_out *spdif;
2541 if (WARN_ON(codec->spdif_out.used <= idx))
2543 mutex_lock(&codec->spdif_mutex);
2544 spdif = snd_array_elem(&codec->spdif_out, idx);
2545 if (spdif->nid != nid) {
2548 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2550 mutex_unlock(&codec->spdif_mutex);
2552 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2555 * SPDIF sharing with analog output
2557 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2561 ucontrol->value.integer.value[0] = mout->share_spdif;
2565 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2568 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2569 mout->share_spdif = !!ucontrol->value.integer.value[0];
2573 static const struct snd_kcontrol_new spdif_share_sw = {
2574 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2575 .name = "IEC958 Default PCM Playback Switch",
2576 .info = snd_ctl_boolean_mono_info,
2577 .get = spdif_share_sw_get,
2578 .put = spdif_share_sw_put,
2582 * snd_hda_create_spdif_share_sw - create Default PCM switch
2583 * @codec: the HDA codec
2584 * @mout: multi-out instance
2586 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2587 struct hda_multi_out *mout)
2589 struct snd_kcontrol *kctl;
2591 if (!mout->dig_out_nid)
2594 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2597 /* ATTENTION: here mout is passed as private_data, instead of codec */
2598 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2600 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2606 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2608 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2617 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 hda_nid_t nid = kcontrol->private_value;
2622 unsigned int val = !!ucontrol->value.integer.value[0];
2625 mutex_lock(&codec->spdif_mutex);
2626 change = codec->spdif_in_enable != val;
2628 codec->spdif_in_enable = val;
2629 snd_hdac_regmap_write(&codec->core, nid,
2630 AC_VERB_SET_DIGI_CONVERT_1, val);
2632 mutex_unlock(&codec->spdif_mutex);
2636 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2639 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2640 hda_nid_t nid = kcontrol->private_value;
2644 snd_hdac_regmap_read(&codec->core, nid,
2645 AC_VERB_GET_DIGI_CONVERT_1, &val);
2646 sbits = convert_to_spdif_status(val);
2647 ucontrol->value.iec958.status[0] = sbits;
2648 ucontrol->value.iec958.status[1] = sbits >> 8;
2649 ucontrol->value.iec958.status[2] = sbits >> 16;
2650 ucontrol->value.iec958.status[3] = sbits >> 24;
2654 static struct snd_kcontrol_new dig_in_ctls[] = {
2656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2657 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2658 .info = snd_hda_spdif_in_switch_info,
2659 .get = snd_hda_spdif_in_switch_get,
2660 .put = snd_hda_spdif_in_switch_put,
2663 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2664 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2665 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2666 .info = snd_hda_spdif_mask_info,
2667 .get = snd_hda_spdif_in_status_get,
2673 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2674 * @codec: the HDA codec
2675 * @nid: audio in widget NID
2677 * Creates controls related with the SPDIF input.
2678 * Called from each patch supporting the SPDIF in.
2680 * Returns 0 if successful, or a negative error code.
2682 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2685 struct snd_kcontrol *kctl;
2686 struct snd_kcontrol_new *dig_mix;
2689 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2691 codec_err(codec, "too many IEC958 inputs\n");
2694 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2695 kctl = snd_ctl_new1(dig_mix, codec);
2698 kctl->private_value = nid;
2699 err = snd_hda_ctl_add(codec, nid, kctl);
2703 codec->spdif_in_enable =
2704 snd_hda_codec_read(codec, nid, 0,
2705 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2709 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2712 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2713 * @codec: the HDA codec
2714 * @fg: function group (not used now)
2715 * @power_state: the power state to set (AC_PWRST_*)
2717 * Set the given power state to all widgets that have the power control.
2718 * If the codec has power_filter set, it evaluates the power state and
2719 * filter out if it's unchanged as D3.
2721 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2722 unsigned int power_state)
2726 for_each_hda_codec_node(nid, codec) {
2727 unsigned int wcaps = get_wcaps(codec, nid);
2728 unsigned int state = power_state;
2729 if (!(wcaps & AC_WCAP_POWER))
2731 if (codec->power_filter) {
2732 state = codec->power_filter(codec, nid, power_state);
2733 if (state != power_state && power_state == AC_PWRST_D3)
2736 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2740 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2743 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2744 * @codec: the HDA codec
2746 * @power_state: power state to evalue
2748 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2749 * This can be used a codec power_filter callback.
2751 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2753 unsigned int power_state)
2755 if (nid == codec->core.afg || nid == codec->core.mfg)
2757 if (power_state == AC_PWRST_D3 &&
2758 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2759 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2760 int eapd = snd_hda_codec_read(codec, nid, 0,
2761 AC_VERB_GET_EAPD_BTLENABLE, 0);
2767 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2770 * set power state of the codec, and return the power state
2772 static unsigned int hda_set_power_state(struct hda_codec *codec,
2773 unsigned int power_state)
2775 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2780 /* this delay seems necessary to avoid click noise at power-down */
2781 if (power_state == AC_PWRST_D3) {
2782 if (codec->depop_delay < 0)
2783 msleep(codec_has_epss(codec) ? 10 : 100);
2784 else if (codec->depop_delay > 0)
2785 msleep(codec->depop_delay);
2786 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2789 /* repeat power states setting at most 10 times*/
2790 for (count = 0; count < 10; count++) {
2791 if (codec->patch_ops.set_power_state)
2792 codec->patch_ops.set_power_state(codec, fg,
2795 state = power_state;
2796 if (codec->power_filter)
2797 state = codec->power_filter(codec, fg, state);
2798 if (state == power_state || power_state != AC_PWRST_D3)
2799 snd_hda_codec_read(codec, fg, flags,
2800 AC_VERB_SET_POWER_STATE,
2802 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2804 state = snd_hda_sync_power_state(codec, fg, power_state);
2805 if (!(state & AC_PWRST_ERROR))
2812 /* sync power states of all widgets;
2813 * this is called at the end of codec parsing
2815 static void sync_power_up_states(struct hda_codec *codec)
2819 /* don't care if no filter is used */
2820 if (!codec->power_filter)
2823 for_each_hda_codec_node(nid, codec) {
2824 unsigned int wcaps = get_wcaps(codec, nid);
2825 unsigned int target;
2826 if (!(wcaps & AC_WCAP_POWER))
2828 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2829 if (target == AC_PWRST_D0)
2831 if (!snd_hda_check_power_state(codec, nid, target))
2832 snd_hda_codec_write(codec, nid, 0,
2833 AC_VERB_SET_POWER_STATE, target);
2837 #ifdef CONFIG_SND_HDA_RECONFIG
2838 /* execute additional init verbs */
2839 static void hda_exec_init_verbs(struct hda_codec *codec)
2841 if (codec->init_verbs.list)
2842 snd_hda_sequence_write(codec, codec->init_verbs.list);
2845 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2849 /* update the power on/off account with the current jiffies */
2850 static void update_power_acct(struct hda_codec *codec, bool on)
2852 unsigned long delta = jiffies - codec->power_jiffies;
2855 codec->power_on_acct += delta;
2857 codec->power_off_acct += delta;
2858 codec->power_jiffies += delta;
2861 void snd_hda_update_power_acct(struct hda_codec *codec)
2863 update_power_acct(codec, hda_codec_is_power_on(codec));
2867 * call suspend and power-down; used both from PM and power-save
2868 * this function returns the power state in the end
2870 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
2874 snd_hdac_enter_pm(&codec->core);
2875 if (codec->patch_ops.suspend)
2876 codec->patch_ops.suspend(codec);
2877 hda_cleanup_all_streams(codec);
2878 state = hda_set_power_state(codec, AC_PWRST_D3);
2879 update_power_acct(codec, true);
2880 snd_hdac_leave_pm(&codec->core);
2885 * kick up codec; used both from PM and power-save
2887 static void hda_call_codec_resume(struct hda_codec *codec)
2889 snd_hdac_enter_pm(&codec->core);
2890 if (codec->core.regmap)
2891 regcache_mark_dirty(codec->core.regmap);
2893 codec->power_jiffies = jiffies;
2895 hda_set_power_state(codec, AC_PWRST_D0);
2896 restore_shutup_pins(codec);
2897 hda_exec_init_verbs(codec);
2898 snd_hda_jack_set_dirty_all(codec);
2899 if (codec->patch_ops.resume)
2900 codec->patch_ops.resume(codec);
2902 if (codec->patch_ops.init)
2903 codec->patch_ops.init(codec);
2904 if (codec->core.regmap)
2905 regcache_sync(codec->core.regmap);
2908 if (codec->jackpoll_interval)
2909 hda_jackpoll_work(&codec->jackpoll_work.work);
2911 snd_hda_jack_report_sync(codec);
2912 snd_hdac_leave_pm(&codec->core);
2915 static int hda_codec_runtime_suspend(struct device *dev)
2917 struct hda_codec *codec = dev_to_hda_codec(dev);
2918 struct hda_pcm *pcm;
2921 cancel_delayed_work_sync(&codec->jackpoll_work);
2922 list_for_each_entry(pcm, &codec->pcm_list_head, list)
2923 snd_pcm_suspend_all(pcm->pcm);
2924 state = hda_call_codec_suspend(codec);
2925 if (codec->link_down_at_suspend ||
2926 (codec_has_clkstop(codec) && codec_has_epss(codec) &&
2927 (state & AC_PWRST_CLK_STOP_OK)))
2928 snd_hdac_codec_link_down(&codec->core);
2929 snd_hdac_link_power(&codec->core, false);
2933 static int hda_codec_runtime_resume(struct device *dev)
2935 struct hda_codec *codec = dev_to_hda_codec(dev);
2937 snd_hdac_link_power(&codec->core, true);
2938 snd_hdac_codec_link_up(&codec->core);
2939 hda_call_codec_resume(codec);
2940 pm_runtime_mark_last_busy(dev);
2943 #endif /* CONFIG_PM */
2945 /* referred in hda_bind.c */
2946 const struct dev_pm_ops hda_codec_driver_pm = {
2947 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2948 pm_runtime_force_resume)
2949 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
2954 * add standard channel maps if not specified
2956 static int add_std_chmaps(struct hda_codec *codec)
2958 struct hda_pcm *pcm;
2961 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
2962 for (str = 0; str < 2; str++) {
2963 struct hda_pcm_stream *hinfo = &pcm->stream[str];
2964 struct snd_pcm_chmap *chmap;
2965 const struct snd_pcm_chmap_elem *elem;
2967 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
2969 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
2970 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
2971 hinfo->channels_max,
2975 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
2981 /* default channel maps for 2.1 speakers;
2982 * since HD-audio supports only stereo, odd number channels are omitted
2984 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
2986 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2988 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2989 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
2992 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
2994 int snd_hda_codec_build_controls(struct hda_codec *codec)
2997 hda_exec_init_verbs(codec);
2998 /* continue to initialize... */
2999 if (codec->patch_ops.init)
3000 err = codec->patch_ops.init(codec);
3001 if (!err && codec->patch_ops.build_controls)
3002 err = codec->patch_ops.build_controls(codec);
3006 /* we create chmaps here instead of build_pcms */
3007 err = add_std_chmaps(codec);
3011 if (codec->jackpoll_interval)
3012 hda_jackpoll_work(&codec->jackpoll_work.work);
3014 snd_hda_jack_report_sync(codec); /* call at the last init point */
3015 sync_power_up_states(codec);
3018 EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls);
3023 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3024 struct hda_codec *codec,
3025 struct snd_pcm_substream *substream)
3030 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3031 struct hda_codec *codec,
3032 unsigned int stream_tag,
3033 unsigned int format,
3034 struct snd_pcm_substream *substream)
3036 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3040 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3041 struct hda_codec *codec,
3042 struct snd_pcm_substream *substream)
3044 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3048 static int set_pcm_default_values(struct hda_codec *codec,
3049 struct hda_pcm_stream *info)
3053 /* query support PCM information from the given NID */
3054 if (info->nid && (!info->rates || !info->formats)) {
3055 err = snd_hda_query_supported_pcm(codec, info->nid,
3056 info->rates ? NULL : &info->rates,
3057 info->formats ? NULL : &info->formats,
3058 info->maxbps ? NULL : &info->maxbps);
3062 if (info->ops.open == NULL)
3063 info->ops.open = hda_pcm_default_open_close;
3064 if (info->ops.close == NULL)
3065 info->ops.close = hda_pcm_default_open_close;
3066 if (info->ops.prepare == NULL) {
3067 if (snd_BUG_ON(!info->nid))
3069 info->ops.prepare = hda_pcm_default_prepare;
3071 if (info->ops.cleanup == NULL) {
3072 if (snd_BUG_ON(!info->nid))
3074 info->ops.cleanup = hda_pcm_default_cleanup;
3080 * codec prepare/cleanup entries
3083 * snd_hda_codec_prepare - Prepare a stream
3084 * @codec: the HDA codec
3085 * @hinfo: PCM information
3086 * @stream: stream tag to assign
3087 * @format: format id to assign
3088 * @substream: PCM substream to assign
3090 * Calls the prepare callback set by the codec with the given arguments.
3091 * Clean up the inactive streams when successful.
3093 int snd_hda_codec_prepare(struct hda_codec *codec,
3094 struct hda_pcm_stream *hinfo,
3095 unsigned int stream,
3096 unsigned int format,
3097 struct snd_pcm_substream *substream)
3100 mutex_lock(&codec->bus->prepare_mutex);
3101 if (hinfo->ops.prepare)
3102 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3107 purify_inactive_streams(codec);
3108 mutex_unlock(&codec->bus->prepare_mutex);
3111 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3114 * snd_hda_codec_cleanup - Prepare a stream
3115 * @codec: the HDA codec
3116 * @hinfo: PCM information
3117 * @substream: PCM substream
3119 * Calls the cleanup callback set by the codec with the given arguments.
3121 void snd_hda_codec_cleanup(struct hda_codec *codec,
3122 struct hda_pcm_stream *hinfo,
3123 struct snd_pcm_substream *substream)
3125 mutex_lock(&codec->bus->prepare_mutex);
3126 if (hinfo->ops.cleanup)
3127 hinfo->ops.cleanup(hinfo, codec, substream);
3128 mutex_unlock(&codec->bus->prepare_mutex);
3130 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3133 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3134 "Audio", "SPDIF", "HDMI", "Modem"
3138 * get the empty PCM device number to assign
3140 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3142 /* audio device indices; not linear to keep compatibility */
3143 /* assigned to static slots up to dev#10; if more needed, assign
3144 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3146 static int audio_idx[HDA_PCM_NTYPES][5] = {
3147 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3148 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3149 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3150 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3154 if (type >= HDA_PCM_NTYPES) {
3155 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3159 for (i = 0; audio_idx[type][i] >= 0; i++) {
3160 #ifndef CONFIG_SND_DYNAMIC_MINORS
3161 if (audio_idx[type][i] >= 8)
3164 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3165 return audio_idx[type][i];
3168 #ifdef CONFIG_SND_DYNAMIC_MINORS
3169 /* non-fixed slots starting from 10 */
3170 for (i = 10; i < 32; i++) {
3171 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3176 dev_warn(bus->card->dev, "Too many %s devices\n",
3177 snd_hda_pcm_type_name[type]);
3178 #ifndef CONFIG_SND_DYNAMIC_MINORS
3179 dev_warn(bus->card->dev,
3180 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3185 /* call build_pcms ops of the given codec and set up the default parameters */
3186 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3188 struct hda_pcm *cpcm;
3191 if (!list_empty(&codec->pcm_list_head))
3192 return 0; /* already parsed */
3194 if (!codec->patch_ops.build_pcms)
3197 err = codec->patch_ops.build_pcms(codec);
3199 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3200 codec->core.addr, err);
3204 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3207 for (stream = 0; stream < 2; stream++) {
3208 struct hda_pcm_stream *info = &cpcm->stream[stream];
3210 if (!info->substreams)
3212 err = set_pcm_default_values(codec, info);
3215 "fail to setup default for PCM %s\n",
3224 EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms);
3226 /* assign all PCMs of the given codec */
3227 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3229 struct hda_bus *bus = codec->bus;
3230 struct hda_pcm *cpcm;
3233 err = snd_hda_codec_parse_pcms(codec);
3237 /* attach a new PCM streams */
3238 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3240 continue; /* already attached */
3241 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3242 continue; /* no substreams assigned */
3244 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3246 cpcm->device = SNDRV_PCM_INVALID_DEVICE;
3247 continue; /* no fatal error */
3250 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3253 "cannot attach PCM stream %d for codec #%d\n",
3254 dev, codec->core.addr);
3255 continue; /* no fatal error */
3263 * snd_hda_add_new_ctls - create controls from the array
3264 * @codec: the HDA codec
3265 * @knew: the array of struct snd_kcontrol_new
3267 * This helper function creates and add new controls in the given array.
3268 * The array must be terminated with an empty entry as terminator.
3270 * Returns 0 if successful, or a negative error code.
3272 int snd_hda_add_new_ctls(struct hda_codec *codec,
3273 const struct snd_kcontrol_new *knew)
3277 for (; knew->name; knew++) {
3278 struct snd_kcontrol *kctl;
3279 int addr = 0, idx = 0;
3280 if (knew->iface == (__force snd_ctl_elem_iface_t)-1)
3281 continue; /* skip this codec private value */
3283 kctl = snd_ctl_new1(knew, codec);
3287 kctl->id.device = addr;
3289 kctl->id.index = idx;
3290 err = snd_hda_ctl_add(codec, 0, kctl);
3293 /* try first with another device index corresponding to
3294 * the codec addr; if it still fails (or it's the
3295 * primary codec), then try another control index
3297 if (!addr && codec->core.addr)
3298 addr = codec->core.addr;
3299 else if (!idx && !knew->index) {
3300 idx = find_empty_mixer_ctl_idx(codec,
3310 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3313 static void codec_set_power_save(struct hda_codec *codec, int delay)
3315 struct device *dev = hda_codec_dev(codec);
3317 if (delay == 0 && codec->auto_runtime_pm)
3321 pm_runtime_set_autosuspend_delay(dev, delay);
3322 pm_runtime_use_autosuspend(dev);
3323 pm_runtime_allow(dev);
3324 if (!pm_runtime_suspended(dev))
3325 pm_runtime_mark_last_busy(dev);
3327 pm_runtime_dont_use_autosuspend(dev);
3328 pm_runtime_forbid(dev);
3333 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3334 * @bus: HD-audio bus
3335 * @delay: autosuspend delay in msec, 0 = off
3337 * Synchronize the runtime PM autosuspend state from the power_save option.
3339 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3341 struct hda_codec *c;
3343 list_for_each_codec(c, bus)
3344 codec_set_power_save(c, delay);
3346 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3349 * snd_hda_check_amp_list_power - Check the amp list and update the power
3350 * @codec: HD-audio codec
3351 * @check: the object containing an AMP list and the status
3352 * @nid: NID to check / update
3354 * Check whether the given NID is in the amp list. If it's in the list,
3355 * check the current AMP status, and update the the power-status according
3356 * to the mute status.
3358 * This function is supposed to be set or called from the check_power_status
3361 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3362 struct hda_loopback_check *check,
3365 const struct hda_amp_list *p;
3368 if (!check->amplist)
3370 for (p = check->amplist; p->nid; p++) {
3375 return 0; /* nothing changed */
3377 for (p = check->amplist; p->nid; p++) {
3378 for (ch = 0; ch < 2; ch++) {
3379 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3381 if (!(v & HDA_AMP_MUTE) && v > 0) {
3382 if (!check->power_on) {
3383 check->power_on = 1;
3384 snd_hda_power_up_pm(codec);
3390 if (check->power_on) {
3391 check->power_on = 0;
3392 snd_hda_power_down_pm(codec);
3396 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3404 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3405 * @imux: imux helper object
3406 * @uinfo: pointer to get/store the data
3408 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3409 struct snd_ctl_elem_info *uinfo)
3413 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3415 uinfo->value.enumerated.items = imux->num_items;
3416 if (!imux->num_items)
3418 index = uinfo->value.enumerated.item;
3419 if (index >= imux->num_items)
3420 index = imux->num_items - 1;
3421 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3424 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3427 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3428 * @codec: the HDA codec
3429 * @imux: imux helper object
3430 * @ucontrol: pointer to get/store the data
3431 * @nid: input mux NID
3432 * @cur_val: pointer to get/store the current imux value
3434 int snd_hda_input_mux_put(struct hda_codec *codec,
3435 const struct hda_input_mux *imux,
3436 struct snd_ctl_elem_value *ucontrol,
3438 unsigned int *cur_val)
3442 if (!imux->num_items)
3444 idx = ucontrol->value.enumerated.item[0];
3445 if (idx >= imux->num_items)
3446 idx = imux->num_items - 1;
3447 if (*cur_val == idx)
3449 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3450 imux->items[idx].index);
3454 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3458 * snd_hda_enum_helper_info - Helper for simple enum ctls
3459 * @kcontrol: ctl element
3460 * @uinfo: pointer to get/store the data
3461 * @num_items: number of enum items
3462 * @texts: enum item string array
3464 * process kcontrol info callback of a simple string enum array
3465 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3467 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_info *uinfo,
3469 int num_items, const char * const *texts)
3471 static const char * const texts_default[] = {
3472 "Disabled", "Enabled"
3475 if (!texts || !num_items) {
3477 texts = texts_default;
3480 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3482 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3485 * Multi-channel / digital-out PCM helper functions
3488 /* setup SPDIF output stream */
3489 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3490 unsigned int stream_tag, unsigned int format)
3492 struct hda_spdif_out *spdif;
3493 unsigned int curr_fmt;
3496 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3497 /* Add sanity check to pass klockwork check.
3498 * This should never happen.
3500 if (WARN_ON(spdif == NULL))
3503 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3504 AC_VERB_GET_STREAM_FORMAT, 0);
3505 reset = codec->spdif_status_reset &&
3506 (spdif->ctls & AC_DIG1_ENABLE) &&
3509 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3512 set_dig_out_convert(codec, nid,
3513 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3515 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3516 if (codec->slave_dig_outs) {
3518 for (d = codec->slave_dig_outs; *d; d++)
3519 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3522 /* turn on again (if needed) */
3524 set_dig_out_convert(codec, nid,
3525 spdif->ctls & 0xff, -1);
3528 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3530 snd_hda_codec_cleanup_stream(codec, nid);
3531 if (codec->slave_dig_outs) {
3533 for (d = codec->slave_dig_outs; *d; d++)
3534 snd_hda_codec_cleanup_stream(codec, *d);
3539 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3540 * @codec: the HDA codec
3541 * @mout: hda_multi_out object
3543 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3544 struct hda_multi_out *mout)
3546 mutex_lock(&codec->spdif_mutex);
3547 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3548 /* already opened as analog dup; reset it once */
3549 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3550 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3551 mutex_unlock(&codec->spdif_mutex);
3554 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3557 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3558 * @codec: the HDA codec
3559 * @mout: hda_multi_out object
3560 * @stream_tag: stream tag to assign
3561 * @format: format id to assign
3562 * @substream: PCM substream to assign
3564 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3565 struct hda_multi_out *mout,
3566 unsigned int stream_tag,
3567 unsigned int format,
3568 struct snd_pcm_substream *substream)
3570 mutex_lock(&codec->spdif_mutex);
3571 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3572 mutex_unlock(&codec->spdif_mutex);
3575 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3578 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3579 * @codec: the HDA codec
3580 * @mout: hda_multi_out object
3582 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3583 struct hda_multi_out *mout)
3585 mutex_lock(&codec->spdif_mutex);
3586 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3587 mutex_unlock(&codec->spdif_mutex);
3590 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3593 * snd_hda_multi_out_dig_close - release the digital out stream
3594 * @codec: the HDA codec
3595 * @mout: hda_multi_out object
3597 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3598 struct hda_multi_out *mout)
3600 mutex_lock(&codec->spdif_mutex);
3601 mout->dig_out_used = 0;
3602 mutex_unlock(&codec->spdif_mutex);
3605 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3608 * snd_hda_multi_out_analog_open - open analog outputs
3609 * @codec: the HDA codec
3610 * @mout: hda_multi_out object
3611 * @substream: PCM substream to assign
3612 * @hinfo: PCM information to assign
3614 * Open analog outputs and set up the hw-constraints.
3615 * If the digital outputs can be opened as slave, open the digital
3618 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3619 struct hda_multi_out *mout,
3620 struct snd_pcm_substream *substream,
3621 struct hda_pcm_stream *hinfo)
3623 struct snd_pcm_runtime *runtime = substream->runtime;
3624 runtime->hw.channels_max = mout->max_channels;
3625 if (mout->dig_out_nid) {
3626 if (!mout->analog_rates) {
3627 mout->analog_rates = hinfo->rates;
3628 mout->analog_formats = hinfo->formats;
3629 mout->analog_maxbps = hinfo->maxbps;
3631 runtime->hw.rates = mout->analog_rates;
3632 runtime->hw.formats = mout->analog_formats;
3633 hinfo->maxbps = mout->analog_maxbps;
3635 if (!mout->spdif_rates) {
3636 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3638 &mout->spdif_formats,
3639 &mout->spdif_maxbps);
3641 mutex_lock(&codec->spdif_mutex);
3642 if (mout->share_spdif) {
3643 if ((runtime->hw.rates & mout->spdif_rates) &&
3644 (runtime->hw.formats & mout->spdif_formats)) {
3645 runtime->hw.rates &= mout->spdif_rates;
3646 runtime->hw.formats &= mout->spdif_formats;
3647 if (mout->spdif_maxbps < hinfo->maxbps)
3648 hinfo->maxbps = mout->spdif_maxbps;
3650 mout->share_spdif = 0;
3651 /* FIXME: need notify? */
3654 mutex_unlock(&codec->spdif_mutex);
3656 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3657 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3659 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3662 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3663 * @codec: the HDA codec
3664 * @mout: hda_multi_out object
3665 * @stream_tag: stream tag to assign
3666 * @format: format id to assign
3667 * @substream: PCM substream to assign
3669 * Set up the i/o for analog out.
3670 * When the digital out is available, copy the front out to digital out, too.
3672 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3673 struct hda_multi_out *mout,
3674 unsigned int stream_tag,
3675 unsigned int format,
3676 struct snd_pcm_substream *substream)
3678 const hda_nid_t *nids = mout->dac_nids;
3679 int chs = substream->runtime->channels;
3680 struct hda_spdif_out *spdif;
3683 mutex_lock(&codec->spdif_mutex);
3684 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3685 if (mout->dig_out_nid && mout->share_spdif &&
3686 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3687 if (chs == 2 && spdif != NULL &&
3688 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3690 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3691 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3692 setup_dig_out_stream(codec, mout->dig_out_nid,
3693 stream_tag, format);
3695 mout->dig_out_used = 0;
3696 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3699 mutex_unlock(&codec->spdif_mutex);
3702 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3704 if (!mout->no_share_stream &&
3705 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3706 /* headphone out will just decode front left/right (stereo) */
3707 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3709 /* extra outputs copied from front */
3710 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3711 if (!mout->no_share_stream && mout->hp_out_nid[i])
3712 snd_hda_codec_setup_stream(codec,
3713 mout->hp_out_nid[i],
3714 stream_tag, 0, format);
3717 for (i = 1; i < mout->num_dacs; i++) {
3718 if (chs >= (i + 1) * 2) /* independent out */
3719 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3721 else if (!mout->no_share_stream) /* copy front */
3722 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3726 /* extra surrounds */
3727 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3729 if (!mout->extra_out_nid[i])
3731 if (chs >= (i + 1) * 2)
3733 else if (!mout->no_share_stream)
3735 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3736 stream_tag, ch, format);
3741 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3744 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3745 * @codec: the HDA codec
3746 * @mout: hda_multi_out object
3748 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3749 struct hda_multi_out *mout)
3751 const hda_nid_t *nids = mout->dac_nids;
3754 for (i = 0; i < mout->num_dacs; i++)
3755 snd_hda_codec_cleanup_stream(codec, nids[i]);
3757 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3758 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3759 if (mout->hp_out_nid[i])
3760 snd_hda_codec_cleanup_stream(codec,
3761 mout->hp_out_nid[i]);
3762 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3763 if (mout->extra_out_nid[i])
3764 snd_hda_codec_cleanup_stream(codec,
3765 mout->extra_out_nid[i]);
3766 mutex_lock(&codec->spdif_mutex);
3767 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3768 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3769 mout->dig_out_used = 0;
3771 mutex_unlock(&codec->spdif_mutex);
3774 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3777 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3778 * @codec: the HDA codec
3779 * @pin: referred pin NID
3781 * Guess the suitable VREF pin bits to be set as the pin-control value.
3782 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3784 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3786 unsigned int pincap;
3787 unsigned int oldval;
3788 oldval = snd_hda_codec_read(codec, pin, 0,
3789 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3790 pincap = snd_hda_query_pin_caps(codec, pin);
3791 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3792 /* Exception: if the default pin setup is vref50, we give it priority */
3793 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3794 return AC_PINCTL_VREF_80;
3795 else if (pincap & AC_PINCAP_VREF_50)
3796 return AC_PINCTL_VREF_50;
3797 else if (pincap & AC_PINCAP_VREF_100)
3798 return AC_PINCTL_VREF_100;
3799 else if (pincap & AC_PINCAP_VREF_GRD)
3800 return AC_PINCTL_VREF_GRD;
3801 return AC_PINCTL_VREF_HIZ;
3803 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3806 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3807 * @codec: the HDA codec
3808 * @pin: referred pin NID
3809 * @val: pin ctl value to audit
3811 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3812 hda_nid_t pin, unsigned int val)
3814 static unsigned int cap_lists[][2] = {
3815 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3816 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3817 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3818 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3824 cap = snd_hda_query_pin_caps(codec, pin);
3826 return val; /* don't know what to do... */
3828 if (val & AC_PINCTL_OUT_EN) {
3829 if (!(cap & AC_PINCAP_OUT))
3830 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3831 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3832 val &= ~AC_PINCTL_HP_EN;
3835 if (val & AC_PINCTL_IN_EN) {
3836 if (!(cap & AC_PINCAP_IN))
3837 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3839 unsigned int vcap, vref;
3841 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3842 vref = val & AC_PINCTL_VREFEN;
3843 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3844 if (vref == cap_lists[i][0] &&
3845 !(vcap & cap_lists[i][1])) {
3846 if (i == ARRAY_SIZE(cap_lists) - 1)
3847 vref = AC_PINCTL_VREF_HIZ;
3849 vref = cap_lists[i + 1][0];
3852 val &= ~AC_PINCTL_VREFEN;
3859 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
3862 * _snd_hda_pin_ctl - Helper to set pin ctl value
3863 * @codec: the HDA codec
3864 * @pin: referred pin NID
3865 * @val: pin control value to set
3866 * @cached: access over codec pinctl cache or direct write
3868 * This function is a helper to set a pin ctl value more safely.
3869 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3870 * value in pin target array via snd_hda_codec_set_pin_target(), then
3871 * actually writes the value via either snd_hda_codec_write_cache() or
3872 * snd_hda_codec_write() depending on @cached flag.
3874 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3875 unsigned int val, bool cached)
3877 val = snd_hda_correct_pin_ctl(codec, pin, val);
3878 snd_hda_codec_set_pin_target(codec, pin, val);
3880 return snd_hda_codec_write_cache(codec, pin, 0,
3881 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3883 return snd_hda_codec_write(codec, pin, 0,
3884 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3886 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
3889 * snd_hda_add_imux_item - Add an item to input_mux
3890 * @codec: the HDA codec
3891 * @imux: imux helper object
3892 * @label: the name of imux item to assign
3893 * @index: index number of imux item to assign
3894 * @type_idx: pointer to store the resultant label index
3896 * When the same label is used already in the existing items, the number
3897 * suffix is appended to the label. This label index number is stored
3898 * to type_idx when non-NULL pointer is given.
3900 int snd_hda_add_imux_item(struct hda_codec *codec,
3901 struct hda_input_mux *imux, const char *label,
3902 int index, int *type_idx)
3904 int i, label_idx = 0;
3905 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
3906 codec_err(codec, "hda_codec: Too many imux items!\n");
3909 for (i = 0; i < imux->num_items; i++) {
3910 if (!strncmp(label, imux->items[i].label, strlen(label)))
3914 *type_idx = label_idx;
3916 snprintf(imux->items[imux->num_items].label,
3917 sizeof(imux->items[imux->num_items].label),
3918 "%s %d", label, label_idx);
3920 strlcpy(imux->items[imux->num_items].label, label,
3921 sizeof(imux->items[imux->num_items].label));
3922 imux->items[imux->num_items].index = index;
3926 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
3929 * snd_hda_bus_reset_codecs - Reset the bus
3930 * @bus: HD-audio bus
3932 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
3934 struct hda_codec *codec;
3936 list_for_each_codec(codec, bus) {
3937 /* FIXME: maybe a better way needed for forced reset */
3938 if (current_work() != &codec->jackpoll_work.work)
3939 cancel_delayed_work_sync(&codec->jackpoll_work);
3941 if (hda_codec_is_power_on(codec)) {
3942 hda_call_codec_suspend(codec);
3943 hda_call_codec_resume(codec);
3950 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
3951 * @pcm: PCM caps bits
3952 * @buf: the string buffer to write
3953 * @buflen: the max buffer length
3955 * used by hda_proc.c and hda_eld.c
3957 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3959 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3962 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3963 if (pcm & (AC_SUPPCM_BITS_8 << i))
3964 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3966 buf[j] = '\0'; /* necessary when j == 0 */
3968 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
3970 MODULE_DESCRIPTION("HDA codec core");
3971 MODULE_LICENSE("GPL");