2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
63 const char *snd_hda_get_jack_location(u32 cfg)
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
68 static unsigned char specials_idx[] = {
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
88 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
97 const char *snd_hda_get_jack_connectivity(u32 cfg)
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
103 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
112 const char *snd_hda_get_jack_type(u32 cfg)
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
118 "SPDIF In", "Digital In", "Reserved", "Other"
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
124 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
127 * Send and receive a verb - passed to exec_verb override for hdac_device
129 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
133 struct hda_bus *bus = codec->bus;
140 snd_hda_power_up_pm(codec);
141 mutex_lock(&bus->core.cmd_mutex);
142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
146 bus->no_response_fallback = 0;
147 mutex_unlock(&bus->core.cmd_mutex);
148 snd_hda_power_down_pm(codec);
149 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
150 if (bus->response_reset) {
152 "resetting BUS due to fatal communication error\n");
153 snd_hda_bus_reset(bus);
157 /* clear reset-flag when the communication gets recovered */
158 if (!err || codec_in_pm(codec))
159 bus->response_reset = 0;
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
167 * @flags: optional bit flags
168 * @verb: the verb to send
169 * @parm: the parameter for the verb
171 * Send a single command and read the corresponding response.
173 * Returns the obtained response value, or -1 for an error.
175 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
177 unsigned int verb, unsigned int parm)
179 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
181 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
185 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
191 * @flags: optional bit flags
192 * @verb: the verb to send
193 * @parm: the parameter for the verb
195 * Send a single command without waiting for response.
197 * Returns 0 if successful, or a negative error code.
199 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
200 unsigned int verb, unsigned int parm)
202 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
203 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
205 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
215 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
217 for (; seq->nid; seq++)
218 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
220 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
222 /* connection list element */
223 struct hda_conn_list {
224 struct list_head list;
230 /* look up the cached results */
231 static struct hda_conn_list *
232 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
234 struct hda_conn_list *p;
235 list_for_each_entry(p, &codec->conn_list, list) {
242 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
243 const hda_nid_t *list)
245 struct hda_conn_list *p;
247 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
252 memcpy(p->conns, list, len * sizeof(hda_nid_t));
253 list_add(&p->list, &codec->conn_list);
257 static void remove_conn_list(struct hda_codec *codec)
259 while (!list_empty(&codec->conn_list)) {
260 struct hda_conn_list *p;
261 p = list_first_entry(&codec->conn_list, typeof(*p), list);
267 /* read the connection and add to the cache */
268 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
271 hda_nid_t *result = list;
274 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
275 if (len == -ENOSPC) {
276 len = snd_hda_get_num_raw_conns(codec, nid);
277 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
280 len = snd_hda_get_raw_connections(codec, nid, result, len);
283 len = snd_hda_override_conn_list(codec, nid, len, result);
290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
293 * @listp: the pointer to store NID list
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
298 * Returns the number of connections, or a negative error code.
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
304 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
305 const hda_nid_t **listp)
311 const struct hda_conn_list *p;
313 /* if the connection-list is already cached, read it */
314 p = lookup_conn_list(codec, nid);
320 if (snd_BUG_ON(added))
323 err = read_and_add_raw_conns(codec, nid);
329 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
332 * snd_hda_get_connections - copy connection list
333 * @codec: the HDA codec
335 * @conn_list: connection list array; when NULL, checks only the size
336 * @max_conns: max. number of connections to store
338 * Parses the connection list of the given widget and stores the list
341 * Returns the number of connections, or a negative error code.
343 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
344 hda_nid_t *conn_list, int max_conns)
346 const hda_nid_t *list;
347 int len = snd_hda_get_conn_list(codec, nid, &list);
349 if (len > 0 && conn_list) {
350 if (len > max_conns) {
351 codec_err(codec, "Too many connections %d for NID 0x%x\n",
355 memcpy(conn_list, list, len * sizeof(hda_nid_t));
360 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
366 * @len: number of connection list entries
367 * @list: the list of connection entries
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
372 * Returns zero or a negative error code.
374 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
375 const hda_nid_t *list)
377 struct hda_conn_list *p;
379 p = lookup_conn_list(codec, nid);
385 return add_conn_list(codec, nid, len, list);
387 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
400 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
401 hda_nid_t nid, int recursive)
403 const hda_nid_t *conn;
406 nums = snd_hda_get_conn_list(codec, mux, &conn);
407 for (i = 0; i < nums; i++)
412 if (recursive > 10) {
413 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
417 for (i = 0; i < nums; i++) {
418 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
419 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
421 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
426 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
429 /* return DEVLIST_LEN parameter of the given widget */
430 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
432 unsigned int wcaps = get_wcaps(codec, nid);
435 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
436 get_wcaps_type(wcaps) != AC_WID_PIN)
439 if (_snd_hdac_read_parm(&codec->core, nid, AC_PAR_DEVLIST_LEN, &parm))
440 return 0; /* error */
441 return parm & AC_DEV_LIST_LEN_MASK;
445 * snd_hda_get_devices - copy device list without cache
446 * @codec: the HDA codec
447 * @nid: NID of the pin to parse
448 * @dev_list: device list array
449 * @max_devices: max. number of devices to store
451 * Copy the device list. This info is dynamic and so not cached.
452 * Currently called only from hda_proc.c, so not exported.
454 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
455 u8 *dev_list, int max_devices)
458 int i, dev_len, devices;
460 parm = get_num_devices(codec, nid);
461 if (!parm) /* not multi-stream capable */
465 dev_len = dev_len < max_devices ? dev_len : max_devices;
468 while (devices < dev_len) {
469 if (snd_hdac_read(&codec->core, nid,
470 AC_VERB_GET_DEVICE_LIST, devices, &parm))
473 for (i = 0; i < 8; i++) {
474 dev_list[devices] = (u8)parm;
477 if (devices >= dev_len)
485 * read widget caps for each widget and store in cache
487 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
492 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
495 nid = codec->core.start_nid;
496 for (i = 0; i < codec->core.num_nodes; i++, nid++)
497 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
498 nid, AC_PAR_AUDIO_WIDGET_CAP);
502 /* read all pin default configurations and save codec->init_pins */
503 static int read_pin_defaults(struct hda_codec *codec)
507 for_each_hda_codec_node(nid, codec) {
508 struct hda_pincfg *pin;
509 unsigned int wcaps = get_wcaps(codec, nid);
510 unsigned int wid_type = get_wcaps_type(wcaps);
511 if (wid_type != AC_WID_PIN)
513 pin = snd_array_new(&codec->init_pins);
517 pin->cfg = snd_hda_codec_read(codec, nid, 0,
518 AC_VERB_GET_CONFIG_DEFAULT, 0);
519 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
520 AC_VERB_GET_PIN_WIDGET_CONTROL,
526 /* look up the given pin config list and return the item matching with NID */
527 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
528 struct snd_array *array,
532 for (i = 0; i < array->used; i++) {
533 struct hda_pincfg *pin = snd_array_elem(array, i);
540 /* set the current pin config value for the given NID.
541 * the value is cached, and read via snd_hda_codec_get_pincfg()
543 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
544 hda_nid_t nid, unsigned int cfg)
546 struct hda_pincfg *pin;
548 /* the check below may be invalid when pins are added by a fixup
549 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
553 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
557 pin = look_up_pincfg(codec, list, nid);
559 pin = snd_array_new(list);
569 * snd_hda_codec_set_pincfg - Override a pin default configuration
570 * @codec: the HDA codec
571 * @nid: NID to set the pin config
572 * @cfg: the pin default config value
574 * Override a pin default configuration value in the cache.
575 * This value can be read by snd_hda_codec_get_pincfg() in a higher
576 * priority than the real hardware value.
578 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
579 hda_nid_t nid, unsigned int cfg)
581 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
583 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
586 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
587 * @codec: the HDA codec
588 * @nid: NID to get the pin config
590 * Get the current pin config value of the given pin NID.
591 * If the pincfg value is cached or overridden via sysfs or driver,
592 * returns the cached value.
594 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
596 struct hda_pincfg *pin;
598 #ifdef CONFIG_SND_HDA_RECONFIG
600 unsigned int cfg = 0;
601 mutex_lock(&codec->user_mutex);
602 pin = look_up_pincfg(codec, &codec->user_pins, nid);
605 mutex_unlock(&codec->user_mutex);
610 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
613 pin = look_up_pincfg(codec, &codec->init_pins, nid);
618 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
621 * snd_hda_codec_set_pin_target - remember the current pinctl target value
622 * @codec: the HDA codec
624 * @val: assigned pinctl value
626 * This function stores the given value to a pinctl target value in the
627 * pincfg table. This isn't always as same as the actually written value
628 * but can be referred at any time via snd_hda_codec_get_pin_target().
630 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
633 struct hda_pincfg *pin;
635 pin = look_up_pincfg(codec, &codec->init_pins, nid);
641 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
644 * snd_hda_codec_get_pin_target - return the current pinctl target value
645 * @codec: the HDA codec
648 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
650 struct hda_pincfg *pin;
652 pin = look_up_pincfg(codec, &codec->init_pins, nid);
657 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
660 * snd_hda_shutup_pins - Shut up all pins
661 * @codec: the HDA codec
663 * Clear all pin controls to shup up before suspend for avoiding click noise.
664 * The controls aren't cached so that they can be resumed properly.
666 void snd_hda_shutup_pins(struct hda_codec *codec)
669 /* don't shut up pins when unloading the driver; otherwise it breaks
670 * the default pin setup at the next load of the driver
672 if (codec->bus->shutdown)
674 for (i = 0; i < codec->init_pins.used; i++) {
675 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
676 /* use read here for syncing after issuing each verb */
677 snd_hda_codec_read(codec, pin->nid, 0,
678 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
680 codec->pins_shutup = 1;
682 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
685 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
686 static void restore_shutup_pins(struct hda_codec *codec)
689 if (!codec->pins_shutup)
691 if (codec->bus->shutdown)
693 for (i = 0; i < codec->init_pins.used; i++) {
694 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
695 snd_hda_codec_write(codec, pin->nid, 0,
696 AC_VERB_SET_PIN_WIDGET_CONTROL,
699 codec->pins_shutup = 0;
703 static void hda_jackpoll_work(struct work_struct *work)
705 struct hda_codec *codec =
706 container_of(work, struct hda_codec, jackpoll_work.work);
708 snd_hda_jack_set_dirty_all(codec);
709 snd_hda_jack_poll_all(codec);
711 if (!codec->jackpoll_interval)
714 schedule_delayed_work(&codec->jackpoll_work,
715 codec->jackpoll_interval);
718 /* release all pincfg lists */
719 static void free_init_pincfgs(struct hda_codec *codec)
721 snd_array_free(&codec->driver_pins);
722 #ifdef CONFIG_SND_HDA_RECONFIG
723 snd_array_free(&codec->user_pins);
725 snd_array_free(&codec->init_pins);
729 * audio-converter setup caches
731 struct hda_cvt_setup {
736 unsigned char active; /* cvt is currently used */
737 unsigned char dirty; /* setups should be cleared */
740 /* get or create a cache entry for the given audio converter NID */
741 static struct hda_cvt_setup *
742 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
744 struct hda_cvt_setup *p;
747 for (i = 0; i < codec->cvt_setups.used; i++) {
748 p = snd_array_elem(&codec->cvt_setups, i);
752 p = snd_array_new(&codec->cvt_setups);
761 static void release_pcm(struct kref *kref)
763 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
766 snd_device_free(pcm->codec->card, pcm->pcm);
767 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
772 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
774 kref_put(&pcm->kref, release_pcm);
776 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
778 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
779 const char *fmt, ...)
784 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
789 kref_init(&pcm->kref);
791 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
798 list_add_tail(&pcm->list, &codec->pcm_list_head);
801 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
806 static void codec_release_pcms(struct hda_codec *codec)
808 struct hda_pcm *pcm, *n;
810 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
811 list_del_init(&pcm->list);
813 snd_device_disconnect(codec->card, pcm->pcm);
814 snd_hda_codec_pcm_put(pcm);
818 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
820 if (codec->registered) {
821 /* pm_runtime_put() is called in snd_hdac_device_exit() */
822 pm_runtime_get_noresume(hda_codec_dev(codec));
823 pm_runtime_disable(hda_codec_dev(codec));
824 codec->registered = 0;
827 cancel_delayed_work_sync(&codec->jackpoll_work);
828 if (!codec->in_freeing)
829 snd_hda_ctls_clear(codec);
830 codec_release_pcms(codec);
831 snd_hda_detach_beep_device(codec);
832 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
833 snd_hda_jack_tbl_clear(codec);
834 codec->proc_widget_hook = NULL;
837 /* free only driver_pins so that init_pins + user_pins are restored */
838 snd_array_free(&codec->driver_pins);
839 snd_array_free(&codec->cvt_setups);
840 snd_array_free(&codec->spdif_out);
841 snd_array_free(&codec->verbs);
842 codec->preset = NULL;
843 codec->slave_dig_outs = NULL;
844 codec->spdif_status_reset = 0;
845 snd_array_free(&codec->mixers);
846 snd_array_free(&codec->nids);
847 remove_conn_list(codec);
848 snd_hdac_regmap_exit(&codec->core);
851 static unsigned int hda_set_power_state(struct hda_codec *codec,
852 unsigned int power_state);
854 /* also called from hda_bind.c */
855 void snd_hda_codec_register(struct hda_codec *codec)
857 if (codec->registered)
859 if (device_is_registered(hda_codec_dev(codec))) {
860 snd_hda_register_beep_device(codec);
861 pm_runtime_enable(hda_codec_dev(codec));
862 /* it was powered up in snd_hda_codec_new(), now all done */
863 snd_hda_power_down(codec);
864 codec->registered = 1;
868 static int snd_hda_codec_dev_register(struct snd_device *device)
870 snd_hda_codec_register(device->device_data);
874 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
876 struct hda_codec *codec = device->device_data;
878 snd_hda_detach_beep_device(codec);
882 static int snd_hda_codec_dev_free(struct snd_device *device)
884 struct hda_codec *codec = device->device_data;
886 codec->in_freeing = 1;
887 snd_hdac_device_unregister(&codec->core);
888 put_device(hda_codec_dev(codec));
892 static void snd_hda_codec_dev_release(struct device *dev)
894 struct hda_codec *codec = dev_to_hda_codec(dev);
896 free_init_pincfgs(codec);
897 snd_hdac_device_exit(&codec->core);
898 snd_hda_sysfs_clear(codec);
899 kfree(codec->modelname);
905 * snd_hda_codec_new - create a HDA codec
906 * @bus: the bus to assign
907 * @codec_addr: the codec address
908 * @codecp: the pointer to store the generated codec
910 * Returns 0 if successful, or a negative error code.
912 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
913 unsigned int codec_addr, struct hda_codec **codecp)
915 struct hda_codec *codec;
919 static struct snd_device_ops dev_ops = {
920 .dev_register = snd_hda_codec_dev_register,
921 .dev_disconnect = snd_hda_codec_dev_disconnect,
922 .dev_free = snd_hda_codec_dev_free,
925 if (snd_BUG_ON(!bus))
927 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
930 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
934 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
935 err = snd_hdac_device_init(&codec->core, &bus->core, component,
942 codec->core.dev.release = snd_hda_codec_dev_release;
943 codec->core.type = HDA_DEV_LEGACY;
944 codec->core.exec_verb = codec_exec_verb;
948 codec->addr = codec_addr;
949 mutex_init(&codec->spdif_mutex);
950 mutex_init(&codec->control_mutex);
951 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
952 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
953 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
954 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
955 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
956 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
957 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
958 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
959 INIT_LIST_HEAD(&codec->conn_list);
960 INIT_LIST_HEAD(&codec->pcm_list_head);
962 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
963 codec->depop_delay = -1;
964 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
967 codec->power_jiffies = jiffies;
970 snd_hda_sysfs_init(codec);
972 if (codec->bus->modelname) {
973 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
974 if (!codec->modelname) {
980 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
981 err = read_widget_caps(codec, fg);
984 err = read_pin_defaults(codec);
988 /* power-up all before initialization */
989 hda_set_power_state(codec, AC_PWRST_D0);
991 snd_hda_codec_proc_new(codec);
993 snd_hda_create_hwdep(codec);
995 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
996 codec->core.subsystem_id, codec->core.revision_id);
997 snd_component_add(card, component);
999 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
1008 put_device(hda_codec_dev(codec));
1011 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1014 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1015 * @codec: the HDA codec
1017 * Forcibly refresh the all widget caps and the init pin configurations of
1020 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1025 err = snd_hdac_refresh_widgets(&codec->core);
1029 /* Assume the function group node does not change,
1030 * only the widget nodes may change.
1032 kfree(codec->wcaps);
1033 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1034 err = read_widget_caps(codec, fg);
1038 snd_array_free(&codec->init_pins);
1039 err = read_pin_defaults(codec);
1043 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1045 /* update the stream-id if changed */
1046 static void update_pcm_stream_id(struct hda_codec *codec,
1047 struct hda_cvt_setup *p, hda_nid_t nid,
1048 u32 stream_tag, int channel_id)
1050 unsigned int oldval, newval;
1052 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1053 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1054 newval = (stream_tag << 4) | channel_id;
1055 if (oldval != newval)
1056 snd_hda_codec_write(codec, nid, 0,
1057 AC_VERB_SET_CHANNEL_STREAMID,
1059 p->stream_tag = stream_tag;
1060 p->channel_id = channel_id;
1064 /* update the format-id if changed */
1065 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1066 hda_nid_t nid, int format)
1068 unsigned int oldval;
1070 if (p->format_id != format) {
1071 oldval = snd_hda_codec_read(codec, nid, 0,
1072 AC_VERB_GET_STREAM_FORMAT, 0);
1073 if (oldval != format) {
1075 snd_hda_codec_write(codec, nid, 0,
1076 AC_VERB_SET_STREAM_FORMAT,
1079 p->format_id = format;
1084 * snd_hda_codec_setup_stream - set up the codec for streaming
1085 * @codec: the CODEC to set up
1086 * @nid: the NID to set up
1087 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1088 * @channel_id: channel id to pass, zero based.
1089 * @format: stream format.
1091 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1093 int channel_id, int format)
1095 struct hda_codec *c;
1096 struct hda_cvt_setup *p;
1104 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1105 nid, stream_tag, channel_id, format);
1106 p = get_hda_cvt_setup(codec, nid);
1110 if (codec->patch_ops.stream_pm)
1111 codec->patch_ops.stream_pm(codec, nid, true);
1112 if (codec->pcm_format_first)
1113 update_pcm_format(codec, p, nid, format);
1114 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1115 if (!codec->pcm_format_first)
1116 update_pcm_format(codec, p, nid, format);
1121 /* make other inactive cvts with the same stream-tag dirty */
1122 type = get_wcaps_type(get_wcaps(codec, nid));
1123 list_for_each_codec(c, codec->bus) {
1124 for (i = 0; i < c->cvt_setups.used; i++) {
1125 p = snd_array_elem(&c->cvt_setups, i);
1126 if (!p->active && p->stream_tag == stream_tag &&
1127 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1132 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1134 static void really_cleanup_stream(struct hda_codec *codec,
1135 struct hda_cvt_setup *q);
1138 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1139 * @codec: the CODEC to clean up
1140 * @nid: the NID to clean up
1141 * @do_now: really clean up the stream instead of clearing the active flag
1143 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1146 struct hda_cvt_setup *p;
1151 if (codec->no_sticky_stream)
1154 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1155 p = get_hda_cvt_setup(codec, nid);
1157 /* here we just clear the active flag when do_now isn't set;
1158 * actual clean-ups will be done later in
1159 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1162 really_cleanup_stream(codec, p);
1167 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1169 static void really_cleanup_stream(struct hda_codec *codec,
1170 struct hda_cvt_setup *q)
1172 hda_nid_t nid = q->nid;
1173 if (q->stream_tag || q->channel_id)
1174 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1176 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1178 memset(q, 0, sizeof(*q));
1180 if (codec->patch_ops.stream_pm)
1181 codec->patch_ops.stream_pm(codec, nid, false);
1184 /* clean up the all conflicting obsolete streams */
1185 static void purify_inactive_streams(struct hda_codec *codec)
1187 struct hda_codec *c;
1190 list_for_each_codec(c, codec->bus) {
1191 for (i = 0; i < c->cvt_setups.used; i++) {
1192 struct hda_cvt_setup *p;
1193 p = snd_array_elem(&c->cvt_setups, i);
1195 really_cleanup_stream(c, p);
1201 /* clean up all streams; called from suspend */
1202 static void hda_cleanup_all_streams(struct hda_codec *codec)
1206 for (i = 0; i < codec->cvt_setups.used; i++) {
1207 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1209 really_cleanup_stream(codec, p);
1215 * amp access functions
1219 * query_amp_caps - query AMP capabilities
1220 * @codec: the HD-auio codec
1221 * @nid: the NID to query
1222 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1224 * Query AMP capabilities for the given widget and direction.
1225 * Returns the obtained capability bits.
1227 * When cap bits have been already read, this doesn't read again but
1228 * returns the cached value.
1230 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1232 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1233 nid = codec->core.afg;
1234 return snd_hda_param_read(codec, nid,
1235 direction == HDA_OUTPUT ?
1236 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1238 EXPORT_SYMBOL_GPL(query_amp_caps);
1241 * snd_hda_check_amp_caps - query AMP capabilities
1242 * @codec: the HD-audio codec
1243 * @nid: the NID to query
1244 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1245 * @bits: bit mask to check the result
1247 * Check whether the widget has the given amp capability for the direction.
1249 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1250 int dir, unsigned int bits)
1254 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1255 if (query_amp_caps(codec, nid, dir) & bits)
1259 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1262 * snd_hda_override_amp_caps - Override the AMP capabilities
1263 * @codec: the CODEC to clean up
1264 * @nid: the NID to clean up
1265 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1266 * @caps: the capability bits to set
1268 * Override the cached AMP caps bits value by the given one.
1269 * This function is useful if the driver needs to adjust the AMP ranges,
1270 * e.g. limit to 0dB, etc.
1272 * Returns zero if successful or a negative error code.
1274 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1279 snd_hda_override_wcaps(codec, nid,
1280 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1281 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1282 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1284 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1287 * snd_hda_codec_amp_stereo - update the AMP stereo values
1288 * @codec: HD-audio codec
1289 * @nid: NID to read the AMP value
1290 * @direction: #HDA_INPUT or #HDA_OUTPUT
1291 * @idx: the index value (only for input direction)
1292 * @mask: bit mask to set
1293 * @val: the bits value to set
1295 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1296 * stereo widget with the same mask and value.
1298 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1299 int direction, int idx, int mask, int val)
1303 if (snd_BUG_ON(mask & ~0xff))
1305 for (ch = 0; ch < 2; ch++)
1306 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1310 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1313 * snd_hda_codec_amp_init - initialize the AMP value
1314 * @codec: the HDA codec
1315 * @nid: NID to read the AMP value
1316 * @ch: channel (left=0 or right=1)
1317 * @dir: #HDA_INPUT or #HDA_OUTPUT
1318 * @idx: the index value (only for input direction)
1319 * @mask: bit mask to set
1320 * @val: the bits value to set
1322 * Works like snd_hda_codec_amp_update() but it writes the value only at
1323 * the first access. If the amp was already initialized / updated beforehand,
1324 * this does nothing.
1326 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1327 int dir, int idx, int mask, int val)
1331 if (!codec->core.regmap)
1333 regcache_cache_only(codec->core.regmap, true);
1334 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1335 regcache_cache_only(codec->core.regmap, false);
1338 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1340 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1343 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1344 * @codec: the HDA codec
1345 * @nid: NID to read the AMP value
1346 * @dir: #HDA_INPUT or #HDA_OUTPUT
1347 * @idx: the index value (only for input direction)
1348 * @mask: bit mask to set
1349 * @val: the bits value to set
1351 * Call snd_hda_codec_amp_init() for both stereo channels.
1353 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1354 int dir, int idx, int mask, int val)
1358 if (snd_BUG_ON(mask & ~0xff))
1360 for (ch = 0; ch < 2; ch++)
1361 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1365 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1367 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1370 u32 caps = query_amp_caps(codec, nid, dir);
1372 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1379 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1380 * @kcontrol: referred ctl element
1381 * @uinfo: pointer to get/store the data
1383 * The control element is supposed to have the private_value field
1384 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1386 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_info *uinfo)
1389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1390 u16 nid = get_amp_nid(kcontrol);
1391 u8 chs = get_amp_channels(kcontrol);
1392 int dir = get_amp_direction(kcontrol);
1393 unsigned int ofs = get_amp_offset(kcontrol);
1395 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1396 uinfo->count = chs == 3 ? 2 : 1;
1397 uinfo->value.integer.min = 0;
1398 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1399 if (!uinfo->value.integer.max) {
1401 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1402 nid, kcontrol->id.name);
1407 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1410 static inline unsigned int
1411 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1412 int ch, int dir, int idx, unsigned int ofs)
1415 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1416 val &= HDA_AMP_VOLMASK;
1425 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1426 int ch, int dir, int idx, unsigned int ofs,
1429 unsigned int maxval;
1433 /* ofs = 0: raw max value */
1434 maxval = get_amp_max_value(codec, nid, dir, 0);
1437 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1438 HDA_AMP_VOLMASK, val);
1442 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1443 * @kcontrol: ctl element
1444 * @ucontrol: pointer to get/store the data
1446 * The control element is supposed to have the private_value field
1447 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1449 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1450 struct snd_ctl_elem_value *ucontrol)
1452 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1453 hda_nid_t nid = get_amp_nid(kcontrol);
1454 int chs = get_amp_channels(kcontrol);
1455 int dir = get_amp_direction(kcontrol);
1456 int idx = get_amp_index(kcontrol);
1457 unsigned int ofs = get_amp_offset(kcontrol);
1458 long *valp = ucontrol->value.integer.value;
1461 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1463 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1466 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1469 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1470 * @kcontrol: ctl element
1471 * @ucontrol: pointer to get/store the data
1473 * The control element is supposed to have the private_value field
1474 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1476 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1477 struct snd_ctl_elem_value *ucontrol)
1479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1480 hda_nid_t nid = get_amp_nid(kcontrol);
1481 int chs = get_amp_channels(kcontrol);
1482 int dir = get_amp_direction(kcontrol);
1483 int idx = get_amp_index(kcontrol);
1484 unsigned int ofs = get_amp_offset(kcontrol);
1485 long *valp = ucontrol->value.integer.value;
1489 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1493 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1496 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1499 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1500 * @kcontrol: ctl element
1501 * @op_flag: operation flag
1502 * @size: byte size of input TLV
1505 * The control element is supposed to have the private_value field
1506 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1508 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1509 unsigned int size, unsigned int __user *_tlv)
1511 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1512 hda_nid_t nid = get_amp_nid(kcontrol);
1513 int dir = get_amp_direction(kcontrol);
1514 unsigned int ofs = get_amp_offset(kcontrol);
1515 bool min_mute = get_amp_min_mute(kcontrol);
1516 u32 caps, val1, val2;
1518 if (size < 4 * sizeof(unsigned int))
1520 caps = query_amp_caps(codec, nid, dir);
1521 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1522 val2 = (val2 + 1) * 25;
1523 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1525 val1 = ((int)val1) * ((int)val2);
1526 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1527 val2 |= TLV_DB_SCALE_MUTE;
1528 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1530 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1532 if (put_user(val1, _tlv + 2))
1534 if (put_user(val2, _tlv + 3))
1538 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1541 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1542 * @codec: HD-audio codec
1543 * @nid: NID of a reference widget
1544 * @dir: #HDA_INPUT or #HDA_OUTPUT
1545 * @tlv: TLV data to be stored, at least 4 elements
1547 * Set (static) TLV data for a virtual master volume using the AMP caps
1548 * obtained from the reference NID.
1549 * The volume range is recalculated as if the max volume is 0dB.
1551 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1557 caps = query_amp_caps(codec, nid, dir);
1558 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1559 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1560 step = (step + 1) * 25;
1561 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1562 tlv[1] = 2 * sizeof(unsigned int);
1563 tlv[2] = -nums * step;
1566 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1568 /* find a mixer control element with the given name */
1569 static struct snd_kcontrol *
1570 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1572 struct snd_ctl_elem_id id;
1573 memset(&id, 0, sizeof(id));
1574 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1577 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1579 strcpy(id.name, name);
1580 return snd_ctl_find_id(codec->card, &id);
1584 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1585 * @codec: HD-audio codec
1586 * @name: ctl id name string
1588 * Get the control element with the given id string and IFACE_MIXER.
1590 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1593 return find_mixer_ctl(codec, name, 0, 0);
1595 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1597 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1601 /* 16 ctlrs should be large enough */
1602 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1603 if (!find_mixer_ctl(codec, name, 0, idx))
1610 * snd_hda_ctl_add - Add a control element and assign to the codec
1611 * @codec: HD-audio codec
1612 * @nid: corresponding NID (optional)
1613 * @kctl: the control element to assign
1615 * Add the given control element to an array inside the codec instance.
1616 * All control elements belonging to a codec are supposed to be added
1617 * by this function so that a proper clean-up works at the free or
1618 * reconfiguration time.
1620 * If non-zero @nid is passed, the NID is assigned to the control element.
1621 * The assignment is shown in the codec proc file.
1623 * snd_hda_ctl_add() checks the control subdev id field whether
1624 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1625 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1626 * specifies if kctl->private_value is a HDA amplifier value.
1628 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1629 struct snd_kcontrol *kctl)
1632 unsigned short flags = 0;
1633 struct hda_nid_item *item;
1635 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1636 flags |= HDA_NID_ITEM_AMP;
1638 nid = get_amp_nid_(kctl->private_value);
1640 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1641 nid = kctl->id.subdevice & 0xffff;
1642 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1643 kctl->id.subdevice = 0;
1644 err = snd_ctl_add(codec->card, kctl);
1647 item = snd_array_new(&codec->mixers);
1652 item->flags = flags;
1655 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1658 * snd_hda_add_nid - Assign a NID to a control element
1659 * @codec: HD-audio codec
1660 * @nid: corresponding NID (optional)
1661 * @kctl: the control element to assign
1662 * @index: index to kctl
1664 * Add the given control element to an array inside the codec instance.
1665 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1666 * NID:KCTL mapping - for example "Capture Source" selector.
1668 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1669 unsigned int index, hda_nid_t nid)
1671 struct hda_nid_item *item;
1674 item = snd_array_new(&codec->nids);
1678 item->index = index;
1682 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1683 kctl->id.name, kctl->id.index, index);
1686 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1689 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1690 * @codec: HD-audio codec
1692 void snd_hda_ctls_clear(struct hda_codec *codec)
1695 struct hda_nid_item *items = codec->mixers.list;
1696 for (i = 0; i < codec->mixers.used; i++)
1697 snd_ctl_remove(codec->card, items[i].kctl);
1698 snd_array_free(&codec->mixers);
1699 snd_array_free(&codec->nids);
1703 * snd_hda_lock_devices - pseudo device locking
1706 * toggle card->shutdown to allow/disallow the device access (as a hack)
1708 int snd_hda_lock_devices(struct hda_bus *bus)
1710 struct snd_card *card = bus->card;
1711 struct hda_codec *codec;
1713 spin_lock(&card->files_lock);
1717 if (!list_empty(&card->ctl_files))
1720 list_for_each_codec(codec, bus) {
1721 struct hda_pcm *cpcm;
1722 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1725 if (cpcm->pcm->streams[0].substream_opened ||
1726 cpcm->pcm->streams[1].substream_opened)
1730 spin_unlock(&card->files_lock);
1736 spin_unlock(&card->files_lock);
1739 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1742 * snd_hda_unlock_devices - pseudo device unlocking
1745 void snd_hda_unlock_devices(struct hda_bus *bus)
1747 struct snd_card *card = bus->card;
1749 spin_lock(&card->files_lock);
1751 spin_unlock(&card->files_lock);
1753 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1756 * snd_hda_codec_reset - Clear all objects assigned to the codec
1757 * @codec: HD-audio codec
1759 * This frees the all PCM and control elements assigned to the codec, and
1760 * clears the caches and restores the pin default configurations.
1762 * When a device is being used, it returns -EBSY. If successfully freed,
1765 int snd_hda_codec_reset(struct hda_codec *codec)
1767 struct hda_bus *bus = codec->bus;
1769 if (snd_hda_lock_devices(bus) < 0)
1772 /* OK, let it free */
1773 snd_hdac_device_unregister(&codec->core);
1775 /* allow device access again */
1776 snd_hda_unlock_devices(bus);
1780 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1782 /* apply the function to all matching slave ctls in the mixer list */
1783 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1784 const char *suffix, map_slave_func_t func, void *data)
1786 struct hda_nid_item *items;
1787 const char * const *s;
1790 items = codec->mixers.list;
1791 for (i = 0; i < codec->mixers.used; i++) {
1792 struct snd_kcontrol *sctl = items[i].kctl;
1793 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1795 for (s = slaves; *s; s++) {
1796 char tmpname[sizeof(sctl->id.name)];
1797 const char *name = *s;
1799 snprintf(tmpname, sizeof(tmpname), "%s %s",
1803 if (!strcmp(sctl->id.name, name)) {
1804 err = func(codec, data, sctl);
1814 static int check_slave_present(struct hda_codec *codec,
1815 void *data, struct snd_kcontrol *sctl)
1820 /* guess the value corresponding to 0dB */
1821 static int get_kctl_0dB_offset(struct hda_codec *codec,
1822 struct snd_kcontrol *kctl, int *step_to_check)
1825 const int *tlv = NULL;
1828 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1829 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1830 mm_segment_t fs = get_fs();
1832 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1835 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1837 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1839 step &= ~TLV_DB_SCALE_MUTE;
1842 if (*step_to_check && *step_to_check != step) {
1843 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1844 - *step_to_check, step);
1847 *step_to_check = step;
1848 val = -tlv[2] / step;
1853 /* call kctl->put with the given value(s) */
1854 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1856 struct snd_ctl_elem_value *ucontrol;
1857 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1860 ucontrol->value.integer.value[0] = val;
1861 ucontrol->value.integer.value[1] = val;
1862 kctl->put(kctl, ucontrol);
1867 /* initialize the slave volume with 0dB */
1868 static int init_slave_0dB(struct hda_codec *codec,
1869 void *data, struct snd_kcontrol *slave)
1871 int offset = get_kctl_0dB_offset(codec, slave, data);
1873 put_kctl_with_value(slave, offset);
1877 /* unmute the slave */
1878 static int init_slave_unmute(struct hda_codec *codec,
1879 void *data, struct snd_kcontrol *slave)
1881 return put_kctl_with_value(slave, 1);
1884 static int add_slave(struct hda_codec *codec,
1885 void *data, struct snd_kcontrol *slave)
1887 return snd_ctl_add_slave(data, slave);
1891 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1892 * @codec: HD-audio codec
1893 * @name: vmaster control name
1894 * @tlv: TLV data (optional)
1895 * @slaves: slave control names (optional)
1896 * @suffix: suffix string to each slave name (optional)
1897 * @init_slave_vol: initialize slaves to unmute/0dB
1898 * @ctl_ret: store the vmaster kcontrol in return
1900 * Create a virtual master control with the given name. The TLV data
1901 * must be either NULL or a valid data.
1903 * @slaves is a NULL-terminated array of strings, each of which is a
1904 * slave control name. All controls with these names are assigned to
1905 * the new virtual master control.
1907 * This function returns zero if successful or a negative error code.
1909 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1910 unsigned int *tlv, const char * const *slaves,
1911 const char *suffix, bool init_slave_vol,
1912 struct snd_kcontrol **ctl_ret)
1914 struct snd_kcontrol *kctl;
1920 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
1922 codec_dbg(codec, "No slave found for %s\n", name);
1925 kctl = snd_ctl_make_virtual_master(name, tlv);
1928 err = snd_hda_ctl_add(codec, 0, kctl);
1932 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
1936 /* init with master mute & zero volume */
1937 put_kctl_with_value(kctl, 0);
1938 if (init_slave_vol) {
1940 map_slaves(codec, slaves, suffix,
1941 tlv ? init_slave_0dB : init_slave_unmute, &step);
1948 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
1951 * mute-LED control using vmaster
1953 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_info *uinfo)
1956 static const char * const texts[] = {
1957 "On", "Off", "Follow Master"
1960 return snd_ctl_enum_info(uinfo, 1, 3, texts);
1963 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
1966 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1967 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1971 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
1974 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1975 unsigned int old_mode = hook->mute_mode;
1977 hook->mute_mode = ucontrol->value.enumerated.item[0];
1978 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1979 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1980 if (old_mode == hook->mute_mode)
1982 snd_hda_sync_vmaster_hook(hook);
1986 static struct snd_kcontrol_new vmaster_mute_mode = {
1987 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1988 .name = "Mute-LED Mode",
1989 .info = vmaster_mute_mode_info,
1990 .get = vmaster_mute_mode_get,
1991 .put = vmaster_mute_mode_put,
1994 /* meta hook to call each driver's vmaster hook */
1995 static void vmaster_hook(void *private_data, int enabled)
1997 struct hda_vmaster_mute_hook *hook = private_data;
1999 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2000 enabled = hook->mute_mode;
2001 hook->hook(hook->codec, enabled);
2005 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2006 * @codec: the HDA codec
2007 * @hook: the vmaster hook object
2008 * @expose_enum_ctl: flag to create an enum ctl
2010 * Add a mute-LED hook with the given vmaster switch kctl.
2011 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2012 * created and associated with the given hook.
2014 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2015 struct hda_vmaster_mute_hook *hook,
2016 bool expose_enum_ctl)
2018 struct snd_kcontrol *kctl;
2020 if (!hook->hook || !hook->sw_kctl)
2022 hook->codec = codec;
2023 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2024 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2025 if (!expose_enum_ctl)
2027 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2030 return snd_hda_ctl_add(codec, 0, kctl);
2032 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2035 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2036 * @hook: the vmaster hook
2038 * Call the hook with the current value for synchronization.
2039 * Should be called in init callback.
2041 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2043 if (!hook->hook || !hook->codec)
2045 /* don't call vmaster hook in the destructor since it might have
2046 * been already destroyed
2048 if (hook->codec->bus->shutdown)
2050 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2052 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2056 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2057 * @kcontrol: referred ctl element
2058 * @uinfo: pointer to get/store the data
2060 * The control element is supposed to have the private_value field
2061 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2063 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
2066 int chs = get_amp_channels(kcontrol);
2068 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2069 uinfo->count = chs == 3 ? 2 : 1;
2070 uinfo->value.integer.min = 0;
2071 uinfo->value.integer.max = 1;
2074 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2077 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2078 * @kcontrol: ctl element
2079 * @ucontrol: pointer to get/store the data
2081 * The control element is supposed to have the private_value field
2082 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2084 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2085 struct snd_ctl_elem_value *ucontrol)
2087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2088 hda_nid_t nid = get_amp_nid(kcontrol);
2089 int chs = get_amp_channels(kcontrol);
2090 int dir = get_amp_direction(kcontrol);
2091 int idx = get_amp_index(kcontrol);
2092 long *valp = ucontrol->value.integer.value;
2095 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2096 HDA_AMP_MUTE) ? 0 : 1;
2098 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2099 HDA_AMP_MUTE) ? 0 : 1;
2102 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2105 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2106 * @kcontrol: ctl element
2107 * @ucontrol: pointer to get/store the data
2109 * The control element is supposed to have the private_value field
2110 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2112 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_value *ucontrol)
2115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2116 hda_nid_t nid = get_amp_nid(kcontrol);
2117 int chs = get_amp_channels(kcontrol);
2118 int dir = get_amp_direction(kcontrol);
2119 int idx = get_amp_index(kcontrol);
2120 long *valp = ucontrol->value.integer.value;
2124 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2126 *valp ? 0 : HDA_AMP_MUTE);
2130 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2132 *valp ? 0 : HDA_AMP_MUTE);
2133 hda_call_check_power_status(codec, nid);
2136 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2139 * bound volume controls
2141 * bind multiple volumes (# indices, from 0)
2144 #define AMP_VAL_IDX_SHIFT 19
2145 #define AMP_VAL_IDX_MASK (0x0f<<19)
2148 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2149 * @kcontrol: ctl element
2150 * @ucontrol: pointer to get/store the data
2152 * The control element is supposed to have the private_value field
2153 * set up via HDA_BIND_MUTE*() macros.
2155 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2162 mutex_lock(&codec->control_mutex);
2163 pval = kcontrol->private_value;
2164 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2165 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2166 kcontrol->private_value = pval;
2167 mutex_unlock(&codec->control_mutex);
2170 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2173 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2174 * @kcontrol: ctl element
2175 * @ucontrol: pointer to get/store the data
2177 * The control element is supposed to have the private_value field
2178 * set up via HDA_BIND_MUTE*() macros.
2180 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2185 int i, indices, err = 0, change = 0;
2187 mutex_lock(&codec->control_mutex);
2188 pval = kcontrol->private_value;
2189 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2190 for (i = 0; i < indices; i++) {
2191 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2192 (i << AMP_VAL_IDX_SHIFT);
2193 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2198 kcontrol->private_value = pval;
2199 mutex_unlock(&codec->control_mutex);
2200 return err < 0 ? err : change;
2202 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2205 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2206 * @kcontrol: referred ctl element
2207 * @uinfo: pointer to get/store the data
2209 * The control element is supposed to have the private_value field
2210 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2212 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_info *uinfo)
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 struct hda_bind_ctls *c;
2219 mutex_lock(&codec->control_mutex);
2220 c = (struct hda_bind_ctls *)kcontrol->private_value;
2221 kcontrol->private_value = *c->values;
2222 err = c->ops->info(kcontrol, uinfo);
2223 kcontrol->private_value = (long)c;
2224 mutex_unlock(&codec->control_mutex);
2227 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2230 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2231 * @kcontrol: ctl element
2232 * @ucontrol: pointer to get/store the data
2234 * The control element is supposed to have the private_value field
2235 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2237 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2238 struct snd_ctl_elem_value *ucontrol)
2240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2241 struct hda_bind_ctls *c;
2244 mutex_lock(&codec->control_mutex);
2245 c = (struct hda_bind_ctls *)kcontrol->private_value;
2246 kcontrol->private_value = *c->values;
2247 err = c->ops->get(kcontrol, ucontrol);
2248 kcontrol->private_value = (long)c;
2249 mutex_unlock(&codec->control_mutex);
2252 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2255 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2256 * @kcontrol: ctl element
2257 * @ucontrol: pointer to get/store the data
2259 * The control element is supposed to have the private_value field
2260 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2262 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
2265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2266 struct hda_bind_ctls *c;
2267 unsigned long *vals;
2268 int err = 0, change = 0;
2270 mutex_lock(&codec->control_mutex);
2271 c = (struct hda_bind_ctls *)kcontrol->private_value;
2272 for (vals = c->values; *vals; vals++) {
2273 kcontrol->private_value = *vals;
2274 err = c->ops->put(kcontrol, ucontrol);
2279 kcontrol->private_value = (long)c;
2280 mutex_unlock(&codec->control_mutex);
2281 return err < 0 ? err : change;
2283 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2286 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2287 * @kcontrol: ctl element
2288 * @op_flag: operation flag
2289 * @size: byte size of input TLV
2292 * The control element is supposed to have the private_value field
2293 * set up via HDA_BIND_VOL() macro.
2295 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2296 unsigned int size, unsigned int __user *tlv)
2298 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2299 struct hda_bind_ctls *c;
2302 mutex_lock(&codec->control_mutex);
2303 c = (struct hda_bind_ctls *)kcontrol->private_value;
2304 kcontrol->private_value = *c->values;
2305 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2306 kcontrol->private_value = (long)c;
2307 mutex_unlock(&codec->control_mutex);
2310 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2312 struct hda_ctl_ops snd_hda_bind_vol = {
2313 .info = snd_hda_mixer_amp_volume_info,
2314 .get = snd_hda_mixer_amp_volume_get,
2315 .put = snd_hda_mixer_amp_volume_put,
2316 .tlv = snd_hda_mixer_amp_tlv
2318 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2320 struct hda_ctl_ops snd_hda_bind_sw = {
2321 .info = snd_hda_mixer_amp_switch_info,
2322 .get = snd_hda_mixer_amp_switch_get,
2323 .put = snd_hda_mixer_amp_switch_put,
2324 .tlv = snd_hda_mixer_amp_tlv
2326 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2329 * SPDIF out controls
2332 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_info *uinfo)
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2340 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *ucontrol)
2343 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2344 IEC958_AES0_NONAUDIO |
2345 IEC958_AES0_CON_EMPHASIS_5015 |
2346 IEC958_AES0_CON_NOT_COPYRIGHT;
2347 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2348 IEC958_AES1_CON_ORIGINAL;
2352 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_value *ucontrol)
2355 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2356 IEC958_AES0_NONAUDIO |
2357 IEC958_AES0_PRO_EMPHASIS_5015;
2361 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2364 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2365 int idx = kcontrol->private_value;
2366 struct hda_spdif_out *spdif;
2368 mutex_lock(&codec->spdif_mutex);
2369 spdif = snd_array_elem(&codec->spdif_out, idx);
2370 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2371 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2372 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2373 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2374 mutex_unlock(&codec->spdif_mutex);
2379 /* convert from SPDIF status bits to HDA SPDIF bits
2380 * bit 0 (DigEn) is always set zero (to be filled later)
2382 static unsigned short convert_from_spdif_status(unsigned int sbits)
2384 unsigned short val = 0;
2386 if (sbits & IEC958_AES0_PROFESSIONAL)
2387 val |= AC_DIG1_PROFESSIONAL;
2388 if (sbits & IEC958_AES0_NONAUDIO)
2389 val |= AC_DIG1_NONAUDIO;
2390 if (sbits & IEC958_AES0_PROFESSIONAL) {
2391 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2392 IEC958_AES0_PRO_EMPHASIS_5015)
2393 val |= AC_DIG1_EMPHASIS;
2395 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2396 IEC958_AES0_CON_EMPHASIS_5015)
2397 val |= AC_DIG1_EMPHASIS;
2398 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2399 val |= AC_DIG1_COPYRIGHT;
2400 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2401 val |= AC_DIG1_LEVEL;
2402 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2407 /* convert to SPDIF status bits from HDA SPDIF bits
2409 static unsigned int convert_to_spdif_status(unsigned short val)
2411 unsigned int sbits = 0;
2413 if (val & AC_DIG1_NONAUDIO)
2414 sbits |= IEC958_AES0_NONAUDIO;
2415 if (val & AC_DIG1_PROFESSIONAL)
2416 sbits |= IEC958_AES0_PROFESSIONAL;
2417 if (sbits & IEC958_AES0_PROFESSIONAL) {
2418 if (val & AC_DIG1_EMPHASIS)
2419 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2421 if (val & AC_DIG1_EMPHASIS)
2422 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2423 if (!(val & AC_DIG1_COPYRIGHT))
2424 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2425 if (val & AC_DIG1_LEVEL)
2426 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2427 sbits |= val & (0x7f << 8);
2432 /* set digital convert verbs both for the given NID and its slaves */
2433 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2438 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2440 d = codec->slave_dig_outs;
2444 snd_hdac_regmap_update(&codec->core, *d,
2445 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2448 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2451 unsigned int mask = 0;
2452 unsigned int val = 0;
2462 set_dig_out(codec, nid, mask, val);
2465 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2469 int idx = kcontrol->private_value;
2470 struct hda_spdif_out *spdif;
2475 mutex_lock(&codec->spdif_mutex);
2476 spdif = snd_array_elem(&codec->spdif_out, idx);
2478 spdif->status = ucontrol->value.iec958.status[0] |
2479 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2480 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2481 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2482 val = convert_from_spdif_status(spdif->status);
2483 val |= spdif->ctls & 1;
2484 change = spdif->ctls != val;
2486 if (change && nid != (u16)-1)
2487 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2488 mutex_unlock(&codec->spdif_mutex);
2492 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2494 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2498 int idx = kcontrol->private_value;
2499 struct hda_spdif_out *spdif;
2501 mutex_lock(&codec->spdif_mutex);
2502 spdif = snd_array_elem(&codec->spdif_out, idx);
2503 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2504 mutex_unlock(&codec->spdif_mutex);
2508 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2511 set_dig_out_convert(codec, nid, dig1, dig2);
2512 /* unmute amp switch (if any) */
2513 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2514 (dig1 & AC_DIG1_ENABLE))
2515 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2519 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2520 struct snd_ctl_elem_value *ucontrol)
2522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2523 int idx = kcontrol->private_value;
2524 struct hda_spdif_out *spdif;
2529 mutex_lock(&codec->spdif_mutex);
2530 spdif = snd_array_elem(&codec->spdif_out, idx);
2532 val = spdif->ctls & ~AC_DIG1_ENABLE;
2533 if (ucontrol->value.integer.value[0])
2534 val |= AC_DIG1_ENABLE;
2535 change = spdif->ctls != val;
2537 if (change && nid != (u16)-1)
2538 set_spdif_ctls(codec, nid, val & 0xff, -1);
2539 mutex_unlock(&codec->spdif_mutex);
2543 static struct snd_kcontrol_new dig_mixes[] = {
2545 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2547 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2548 .info = snd_hda_spdif_mask_info,
2549 .get = snd_hda_spdif_cmask_get,
2552 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2553 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2554 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2555 .info = snd_hda_spdif_mask_info,
2556 .get = snd_hda_spdif_pmask_get,
2559 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2560 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2561 .info = snd_hda_spdif_mask_info,
2562 .get = snd_hda_spdif_default_get,
2563 .put = snd_hda_spdif_default_put,
2566 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2567 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2568 .info = snd_hda_spdif_out_switch_info,
2569 .get = snd_hda_spdif_out_switch_get,
2570 .put = snd_hda_spdif_out_switch_put,
2576 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2577 * @codec: the HDA codec
2578 * @associated_nid: NID that new ctls associated with
2579 * @cvt_nid: converter NID
2580 * @type: HDA_PCM_TYPE_*
2581 * Creates controls related with the digital output.
2582 * Called from each patch supporting the digital out.
2584 * Returns 0 if successful, or a negative error code.
2586 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2587 hda_nid_t associated_nid,
2592 struct snd_kcontrol *kctl;
2593 struct snd_kcontrol_new *dig_mix;
2596 const int spdif_index = 16;
2597 struct hda_spdif_out *spdif;
2598 struct hda_bus *bus = codec->bus;
2600 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2601 type == HDA_PCM_TYPE_SPDIF) {
2603 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2604 type == HDA_PCM_TYPE_HDMI) {
2605 /* suppose a single SPDIF device */
2606 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2607 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2610 kctl->id.index = spdif_index;
2612 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2614 if (!bus->primary_dig_out_type)
2615 bus->primary_dig_out_type = type;
2617 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2619 codec_err(codec, "too many IEC958 outputs\n");
2622 spdif = snd_array_new(&codec->spdif_out);
2625 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2626 kctl = snd_ctl_new1(dig_mix, codec);
2629 kctl->id.index = idx;
2630 kctl->private_value = codec->spdif_out.used - 1;
2631 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2635 spdif->nid = cvt_nid;
2636 snd_hdac_regmap_read(&codec->core, cvt_nid,
2637 AC_VERB_GET_DIGI_CONVERT_1, &val);
2639 spdif->status = convert_to_spdif_status(spdif->ctls);
2642 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2645 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2646 * @codec: the HDA codec
2649 * call within spdif_mutex lock
2651 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2655 for (i = 0; i < codec->spdif_out.used; i++) {
2656 struct hda_spdif_out *spdif =
2657 snd_array_elem(&codec->spdif_out, i);
2658 if (spdif->nid == nid)
2663 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2666 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2667 * @codec: the HDA codec
2668 * @idx: the SPDIF ctl index
2670 * Unassign the widget from the given SPDIF control.
2672 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2674 struct hda_spdif_out *spdif;
2676 mutex_lock(&codec->spdif_mutex);
2677 spdif = snd_array_elem(&codec->spdif_out, idx);
2678 spdif->nid = (u16)-1;
2679 mutex_unlock(&codec->spdif_mutex);
2681 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2684 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2685 * @codec: the HDA codec
2686 * @idx: the SPDIF ctl idx
2689 * Assign the widget to the SPDIF control with the given index.
2691 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2693 struct hda_spdif_out *spdif;
2696 mutex_lock(&codec->spdif_mutex);
2697 spdif = snd_array_elem(&codec->spdif_out, idx);
2698 if (spdif->nid != nid) {
2701 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2703 mutex_unlock(&codec->spdif_mutex);
2705 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2708 * SPDIF sharing with analog output
2710 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2713 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2714 ucontrol->value.integer.value[0] = mout->share_spdif;
2718 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2719 struct snd_ctl_elem_value *ucontrol)
2721 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2722 mout->share_spdif = !!ucontrol->value.integer.value[0];
2726 static struct snd_kcontrol_new spdif_share_sw = {
2727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2728 .name = "IEC958 Default PCM Playback Switch",
2729 .info = snd_ctl_boolean_mono_info,
2730 .get = spdif_share_sw_get,
2731 .put = spdif_share_sw_put,
2735 * snd_hda_create_spdif_share_sw - create Default PCM switch
2736 * @codec: the HDA codec
2737 * @mout: multi-out instance
2739 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2740 struct hda_multi_out *mout)
2742 struct snd_kcontrol *kctl;
2744 if (!mout->dig_out_nid)
2747 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2750 /* ATTENTION: here mout is passed as private_data, instead of codec */
2751 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2753 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2759 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2761 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2766 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2770 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2771 struct snd_ctl_elem_value *ucontrol)
2773 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2774 hda_nid_t nid = kcontrol->private_value;
2775 unsigned int val = !!ucontrol->value.integer.value[0];
2778 mutex_lock(&codec->spdif_mutex);
2779 change = codec->spdif_in_enable != val;
2781 codec->spdif_in_enable = val;
2782 snd_hdac_regmap_write(&codec->core, nid,
2783 AC_VERB_SET_DIGI_CONVERT_1, val);
2785 mutex_unlock(&codec->spdif_mutex);
2789 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2793 hda_nid_t nid = kcontrol->private_value;
2797 snd_hdac_regmap_read(&codec->core, nid,
2798 AC_VERB_GET_DIGI_CONVERT_1, &val);
2799 sbits = convert_to_spdif_status(val);
2800 ucontrol->value.iec958.status[0] = sbits;
2801 ucontrol->value.iec958.status[1] = sbits >> 8;
2802 ucontrol->value.iec958.status[2] = sbits >> 16;
2803 ucontrol->value.iec958.status[3] = sbits >> 24;
2807 static struct snd_kcontrol_new dig_in_ctls[] = {
2809 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2810 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2811 .info = snd_hda_spdif_in_switch_info,
2812 .get = snd_hda_spdif_in_switch_get,
2813 .put = snd_hda_spdif_in_switch_put,
2816 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2817 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2818 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2819 .info = snd_hda_spdif_mask_info,
2820 .get = snd_hda_spdif_in_status_get,
2826 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2827 * @codec: the HDA codec
2828 * @nid: audio in widget NID
2830 * Creates controls related with the SPDIF input.
2831 * Called from each patch supporting the SPDIF in.
2833 * Returns 0 if successful, or a negative error code.
2835 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2838 struct snd_kcontrol *kctl;
2839 struct snd_kcontrol_new *dig_mix;
2842 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2844 codec_err(codec, "too many IEC958 inputs\n");
2847 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2848 kctl = snd_ctl_new1(dig_mix, codec);
2851 kctl->private_value = nid;
2852 err = snd_hda_ctl_add(codec, nid, kctl);
2856 codec->spdif_in_enable =
2857 snd_hda_codec_read(codec, nid, 0,
2858 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2862 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2865 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2866 * @codec: the HDA codec
2867 * @fg: function group (not used now)
2868 * @power_state: the power state to set (AC_PWRST_*)
2870 * Set the given power state to all widgets that have the power control.
2871 * If the codec has power_filter set, it evaluates the power state and
2872 * filter out if it's unchanged as D3.
2874 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2875 unsigned int power_state)
2879 for_each_hda_codec_node(nid, codec) {
2880 unsigned int wcaps = get_wcaps(codec, nid);
2881 unsigned int state = power_state;
2882 if (!(wcaps & AC_WCAP_POWER))
2884 if (codec->power_filter) {
2885 state = codec->power_filter(codec, nid, power_state);
2886 if (state != power_state && power_state == AC_PWRST_D3)
2889 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2893 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2896 * wait until the state is reached, returns the current state
2898 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2900 unsigned int power_state)
2902 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2903 unsigned int state, actual_state;
2906 state = snd_hda_codec_read(codec, fg, 0,
2907 AC_VERB_GET_POWER_STATE, 0);
2908 if (state & AC_PWRST_ERROR)
2910 actual_state = (state >> 4) & 0x0f;
2911 if (actual_state == power_state)
2913 if (time_after_eq(jiffies, end_time))
2915 /* wait until the codec reachs to the target state */
2922 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2923 * @codec: the HDA codec
2925 * @power_state: power state to evalue
2927 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2928 * This can be used a codec power_filter callback.
2930 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2932 unsigned int power_state)
2934 if (nid == codec->core.afg || nid == codec->core.mfg)
2936 if (power_state == AC_PWRST_D3 &&
2937 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2938 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2939 int eapd = snd_hda_codec_read(codec, nid, 0,
2940 AC_VERB_GET_EAPD_BTLENABLE, 0);
2946 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
2949 * set power state of the codec, and return the power state
2951 static unsigned int hda_set_power_state(struct hda_codec *codec,
2952 unsigned int power_state)
2954 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
2959 /* this delay seems necessary to avoid click noise at power-down */
2960 if (power_state == AC_PWRST_D3) {
2961 if (codec->depop_delay < 0)
2962 msleep(codec_has_epss(codec) ? 10 : 100);
2963 else if (codec->depop_delay > 0)
2964 msleep(codec->depop_delay);
2965 flags = HDA_RW_NO_RESPONSE_FALLBACK;
2968 /* repeat power states setting at most 10 times*/
2969 for (count = 0; count < 10; count++) {
2970 if (codec->patch_ops.set_power_state)
2971 codec->patch_ops.set_power_state(codec, fg,
2974 state = power_state;
2975 if (codec->power_filter)
2976 state = codec->power_filter(codec, fg, state);
2977 if (state == power_state || power_state != AC_PWRST_D3)
2978 snd_hda_codec_read(codec, fg, flags,
2979 AC_VERB_SET_POWER_STATE,
2981 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2983 state = hda_sync_power_state(codec, fg, power_state);
2984 if (!(state & AC_PWRST_ERROR))
2991 /* sync power states of all widgets;
2992 * this is called at the end of codec parsing
2994 static void sync_power_up_states(struct hda_codec *codec)
2998 /* don't care if no filter is used */
2999 if (!codec->power_filter)
3002 for_each_hda_codec_node(nid, codec) {
3003 unsigned int wcaps = get_wcaps(codec, nid);
3004 unsigned int target;
3005 if (!(wcaps & AC_WCAP_POWER))
3007 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3008 if (target == AC_PWRST_D0)
3010 if (!snd_hda_check_power_state(codec, nid, target))
3011 snd_hda_codec_write(codec, nid, 0,
3012 AC_VERB_SET_POWER_STATE, target);
3016 #ifdef CONFIG_SND_HDA_RECONFIG
3017 /* execute additional init verbs */
3018 static void hda_exec_init_verbs(struct hda_codec *codec)
3020 if (codec->init_verbs.list)
3021 snd_hda_sequence_write(codec, codec->init_verbs.list);
3024 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3028 /* update the power on/off account with the current jiffies */
3029 static void update_power_acct(struct hda_codec *codec, bool on)
3031 unsigned long delta = jiffies - codec->power_jiffies;
3034 codec->power_on_acct += delta;
3036 codec->power_off_acct += delta;
3037 codec->power_jiffies += delta;
3040 void snd_hda_update_power_acct(struct hda_codec *codec)
3042 update_power_acct(codec, hda_codec_is_power_on(codec));
3046 * call suspend and power-down; used both from PM and power-save
3047 * this function returns the power state in the end
3049 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3053 atomic_inc(&codec->core.in_pm);
3055 if (codec->patch_ops.suspend)
3056 codec->patch_ops.suspend(codec);
3057 hda_cleanup_all_streams(codec);
3058 state = hda_set_power_state(codec, AC_PWRST_D3);
3059 update_power_acct(codec, true);
3060 atomic_dec(&codec->core.in_pm);
3065 * kick up codec; used both from PM and power-save
3067 static void hda_call_codec_resume(struct hda_codec *codec)
3069 atomic_inc(&codec->core.in_pm);
3071 if (codec->core.regmap)
3072 regcache_mark_dirty(codec->core.regmap);
3074 codec->power_jiffies = jiffies;
3076 hda_set_power_state(codec, AC_PWRST_D0);
3077 restore_shutup_pins(codec);
3078 hda_exec_init_verbs(codec);
3079 snd_hda_jack_set_dirty_all(codec);
3080 if (codec->patch_ops.resume)
3081 codec->patch_ops.resume(codec);
3083 if (codec->patch_ops.init)
3084 codec->patch_ops.init(codec);
3085 if (codec->core.regmap)
3086 regcache_sync(codec->core.regmap);
3089 if (codec->jackpoll_interval)
3090 hda_jackpoll_work(&codec->jackpoll_work.work);
3092 snd_hda_jack_report_sync(codec);
3093 atomic_dec(&codec->core.in_pm);
3096 static int hda_codec_runtime_suspend(struct device *dev)
3098 struct hda_codec *codec = dev_to_hda_codec(dev);
3099 struct hda_pcm *pcm;
3102 cancel_delayed_work_sync(&codec->jackpoll_work);
3103 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3104 snd_pcm_suspend_all(pcm->pcm);
3105 state = hda_call_codec_suspend(codec);
3106 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3107 (state & AC_PWRST_CLK_STOP_OK))
3108 snd_hdac_codec_link_down(&codec->core);
3112 static int hda_codec_runtime_resume(struct device *dev)
3114 struct hda_codec *codec = dev_to_hda_codec(dev);
3116 snd_hdac_codec_link_up(&codec->core);
3117 hda_call_codec_resume(codec);
3118 pm_runtime_mark_last_busy(dev);
3121 #endif /* CONFIG_PM */
3123 /* referred in hda_bind.c */
3124 const struct dev_pm_ops hda_codec_driver_pm = {
3125 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3126 pm_runtime_force_resume)
3127 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3132 * add standard channel maps if not specified
3134 static int add_std_chmaps(struct hda_codec *codec)
3136 struct hda_pcm *pcm;
3139 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3140 for (str = 0; str < 2; str++) {
3141 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3142 struct snd_pcm_chmap *chmap;
3143 const struct snd_pcm_chmap_elem *elem;
3145 if (!pcm || pcm->own_chmap ||
3148 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3149 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3150 hinfo->channels_max,
3154 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3160 /* default channel maps for 2.1 speakers;
3161 * since HD-audio supports only stereo, odd number channels are omitted
3163 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3165 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3167 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3168 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3171 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3173 int snd_hda_codec_build_controls(struct hda_codec *codec)
3176 hda_exec_init_verbs(codec);
3177 /* continue to initialize... */
3178 if (codec->patch_ops.init)
3179 err = codec->patch_ops.init(codec);
3180 if (!err && codec->patch_ops.build_controls)
3181 err = codec->patch_ops.build_controls(codec);
3185 /* we create chmaps here instead of build_pcms */
3186 err = add_std_chmaps(codec);
3190 if (codec->jackpoll_interval)
3191 hda_jackpoll_work(&codec->jackpoll_work.work);
3193 snd_hda_jack_report_sync(codec); /* call at the last init point */
3194 sync_power_up_states(codec);
3201 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3202 struct hda_codec *codec,
3203 struct snd_pcm_substream *substream)
3208 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3209 struct hda_codec *codec,
3210 unsigned int stream_tag,
3211 unsigned int format,
3212 struct snd_pcm_substream *substream)
3214 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3218 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3219 struct hda_codec *codec,
3220 struct snd_pcm_substream *substream)
3222 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3226 static int set_pcm_default_values(struct hda_codec *codec,
3227 struct hda_pcm_stream *info)
3231 /* query support PCM information from the given NID */
3232 if (info->nid && (!info->rates || !info->formats)) {
3233 err = snd_hda_query_supported_pcm(codec, info->nid,
3234 info->rates ? NULL : &info->rates,
3235 info->formats ? NULL : &info->formats,
3236 info->maxbps ? NULL : &info->maxbps);
3240 if (info->ops.open == NULL)
3241 info->ops.open = hda_pcm_default_open_close;
3242 if (info->ops.close == NULL)
3243 info->ops.close = hda_pcm_default_open_close;
3244 if (info->ops.prepare == NULL) {
3245 if (snd_BUG_ON(!info->nid))
3247 info->ops.prepare = hda_pcm_default_prepare;
3249 if (info->ops.cleanup == NULL) {
3250 if (snd_BUG_ON(!info->nid))
3252 info->ops.cleanup = hda_pcm_default_cleanup;
3258 * codec prepare/cleanup entries
3261 * snd_hda_codec_prepare - Prepare a stream
3262 * @codec: the HDA codec
3263 * @hinfo: PCM information
3264 * @stream: stream tag to assign
3265 * @format: format id to assign
3266 * @substream: PCM substream to assign
3268 * Calls the prepare callback set by the codec with the given arguments.
3269 * Clean up the inactive streams when successful.
3271 int snd_hda_codec_prepare(struct hda_codec *codec,
3272 struct hda_pcm_stream *hinfo,
3273 unsigned int stream,
3274 unsigned int format,
3275 struct snd_pcm_substream *substream)
3278 mutex_lock(&codec->bus->prepare_mutex);
3279 if (hinfo->ops.prepare)
3280 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3285 purify_inactive_streams(codec);
3286 mutex_unlock(&codec->bus->prepare_mutex);
3289 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3292 * snd_hda_codec_cleanup - Prepare a stream
3293 * @codec: the HDA codec
3294 * @hinfo: PCM information
3295 * @substream: PCM substream
3297 * Calls the cleanup callback set by the codec with the given arguments.
3299 void snd_hda_codec_cleanup(struct hda_codec *codec,
3300 struct hda_pcm_stream *hinfo,
3301 struct snd_pcm_substream *substream)
3303 mutex_lock(&codec->bus->prepare_mutex);
3304 if (hinfo->ops.cleanup)
3305 hinfo->ops.cleanup(hinfo, codec, substream);
3306 mutex_unlock(&codec->bus->prepare_mutex);
3308 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3311 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3312 "Audio", "SPDIF", "HDMI", "Modem"
3316 * get the empty PCM device number to assign
3318 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3320 /* audio device indices; not linear to keep compatibility */
3321 /* assigned to static slots up to dev#10; if more needed, assign
3322 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3324 static int audio_idx[HDA_PCM_NTYPES][5] = {
3325 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3326 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3327 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3328 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3332 if (type >= HDA_PCM_NTYPES) {
3333 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3337 for (i = 0; audio_idx[type][i] >= 0; i++) {
3338 #ifndef CONFIG_SND_DYNAMIC_MINORS
3339 if (audio_idx[type][i] >= 8)
3342 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3343 return audio_idx[type][i];
3346 #ifdef CONFIG_SND_DYNAMIC_MINORS
3347 /* non-fixed slots starting from 10 */
3348 for (i = 10; i < 32; i++) {
3349 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3354 dev_warn(bus->card->dev, "Too many %s devices\n",
3355 snd_hda_pcm_type_name[type]);
3356 #ifndef CONFIG_SND_DYNAMIC_MINORS
3357 dev_warn(bus->card->dev,
3358 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3363 /* call build_pcms ops of the given codec and set up the default parameters */
3364 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3366 struct hda_pcm *cpcm;
3369 if (!list_empty(&codec->pcm_list_head))
3370 return 0; /* already parsed */
3372 if (!codec->patch_ops.build_pcms)
3375 err = codec->patch_ops.build_pcms(codec);
3377 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3378 codec->core.addr, err);
3382 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3385 for (stream = 0; stream < 2; stream++) {
3386 struct hda_pcm_stream *info = &cpcm->stream[stream];
3388 if (!info->substreams)
3390 err = set_pcm_default_values(codec, info);
3393 "fail to setup default for PCM %s\n",
3403 /* assign all PCMs of the given codec */
3404 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3406 struct hda_bus *bus = codec->bus;
3407 struct hda_pcm *cpcm;
3410 err = snd_hda_codec_parse_pcms(codec);
3412 snd_hda_codec_reset(codec);
3416 /* attach a new PCM streams */
3417 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3419 continue; /* already attached */
3420 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3421 continue; /* no substreams assigned */
3423 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3425 continue; /* no fatal error */
3427 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
3430 "cannot attach PCM stream %d for codec #%d\n",
3431 dev, codec->core.addr);
3432 continue; /* no fatal error */
3440 * snd_hda_add_new_ctls - create controls from the array
3441 * @codec: the HDA codec
3442 * @knew: the array of struct snd_kcontrol_new
3444 * This helper function creates and add new controls in the given array.
3445 * The array must be terminated with an empty entry as terminator.
3447 * Returns 0 if successful, or a negative error code.
3449 int snd_hda_add_new_ctls(struct hda_codec *codec,
3450 const struct snd_kcontrol_new *knew)
3454 for (; knew->name; knew++) {
3455 struct snd_kcontrol *kctl;
3456 int addr = 0, idx = 0;
3457 if (knew->iface == -1) /* skip this codec private value */
3460 kctl = snd_ctl_new1(knew, codec);
3464 kctl->id.device = addr;
3466 kctl->id.index = idx;
3467 err = snd_hda_ctl_add(codec, 0, kctl);
3470 /* try first with another device index corresponding to
3471 * the codec addr; if it still fails (or it's the
3472 * primary codec), then try another control index
3474 if (!addr && codec->core.addr)
3475 addr = codec->core.addr;
3476 else if (!idx && !knew->index) {
3477 idx = find_empty_mixer_ctl_idx(codec,
3487 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3490 static void codec_set_power_save(struct hda_codec *codec, int delay)
3492 struct device *dev = hda_codec_dev(codec);
3495 pm_runtime_set_autosuspend_delay(dev, delay);
3496 pm_runtime_use_autosuspend(dev);
3497 pm_runtime_allow(dev);
3498 if (!pm_runtime_suspended(dev))
3499 pm_runtime_mark_last_busy(dev);
3501 pm_runtime_dont_use_autosuspend(dev);
3502 pm_runtime_forbid(dev);
3507 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3508 * @bus: HD-audio bus
3509 * @delay: autosuspend delay in msec, 0 = off
3511 * Synchronize the runtime PM autosuspend state from the power_save option.
3513 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3515 struct hda_codec *c;
3517 list_for_each_codec(c, bus)
3518 codec_set_power_save(c, delay);
3520 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3523 * snd_hda_check_amp_list_power - Check the amp list and update the power
3524 * @codec: HD-audio codec
3525 * @check: the object containing an AMP list and the status
3526 * @nid: NID to check / update
3528 * Check whether the given NID is in the amp list. If it's in the list,
3529 * check the current AMP status, and update the the power-status according
3530 * to the mute status.
3532 * This function is supposed to be set or called from the check_power_status
3535 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3536 struct hda_loopback_check *check,
3539 const struct hda_amp_list *p;
3542 if (!check->amplist)
3544 for (p = check->amplist; p->nid; p++) {
3549 return 0; /* nothing changed */
3551 for (p = check->amplist; p->nid; p++) {
3552 for (ch = 0; ch < 2; ch++) {
3553 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3555 if (!(v & HDA_AMP_MUTE) && v > 0) {
3556 if (!check->power_on) {
3557 check->power_on = 1;
3558 snd_hda_power_up_pm(codec);
3564 if (check->power_on) {
3565 check->power_on = 0;
3566 snd_hda_power_down_pm(codec);
3570 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3578 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3579 * @imux: imux helper object
3580 * @uinfo: pointer to get/store the data
3582 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3583 struct snd_ctl_elem_info *uinfo)
3587 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3589 uinfo->value.enumerated.items = imux->num_items;
3590 if (!imux->num_items)
3592 index = uinfo->value.enumerated.item;
3593 if (index >= imux->num_items)
3594 index = imux->num_items - 1;
3595 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3598 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
3601 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3602 * @codec: the HDA codec
3603 * @imux: imux helper object
3604 * @ucontrol: pointer to get/store the data
3605 * @nid: input mux NID
3606 * @cur_val: pointer to get/store the current imux value
3608 int snd_hda_input_mux_put(struct hda_codec *codec,
3609 const struct hda_input_mux *imux,
3610 struct snd_ctl_elem_value *ucontrol,
3612 unsigned int *cur_val)
3616 if (!imux->num_items)
3618 idx = ucontrol->value.enumerated.item[0];
3619 if (idx >= imux->num_items)
3620 idx = imux->num_items - 1;
3621 if (*cur_val == idx)
3623 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3624 imux->items[idx].index);
3628 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
3632 * snd_hda_enum_helper_info - Helper for simple enum ctls
3633 * @kcontrol: ctl element
3634 * @uinfo: pointer to get/store the data
3635 * @num_items: number of enum items
3636 * @texts: enum item string array
3638 * process kcontrol info callback of a simple string enum array
3639 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3641 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3642 struct snd_ctl_elem_info *uinfo,
3643 int num_items, const char * const *texts)
3645 static const char * const texts_default[] = {
3646 "Disabled", "Enabled"
3649 if (!texts || !num_items) {
3651 texts = texts_default;
3654 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
3656 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
3659 * Multi-channel / digital-out PCM helper functions
3662 /* setup SPDIF output stream */
3663 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3664 unsigned int stream_tag, unsigned int format)
3666 struct hda_spdif_out *spdif;
3667 unsigned int curr_fmt;
3670 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3671 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3672 AC_VERB_GET_STREAM_FORMAT, 0);
3673 reset = codec->spdif_status_reset &&
3674 (spdif->ctls & AC_DIG1_ENABLE) &&
3677 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3680 set_dig_out_convert(codec, nid,
3681 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
3683 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3684 if (codec->slave_dig_outs) {
3686 for (d = codec->slave_dig_outs; *d; d++)
3687 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3690 /* turn on again (if needed) */
3692 set_dig_out_convert(codec, nid,
3693 spdif->ctls & 0xff, -1);
3696 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3698 snd_hda_codec_cleanup_stream(codec, nid);
3699 if (codec->slave_dig_outs) {
3701 for (d = codec->slave_dig_outs; *d; d++)
3702 snd_hda_codec_cleanup_stream(codec, *d);
3707 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3708 * @codec: the HDA codec
3709 * @mout: hda_multi_out object
3711 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3712 struct hda_multi_out *mout)
3714 mutex_lock(&codec->spdif_mutex);
3715 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3716 /* already opened as analog dup; reset it once */
3717 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3718 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3719 mutex_unlock(&codec->spdif_mutex);
3722 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
3725 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3726 * @codec: the HDA codec
3727 * @mout: hda_multi_out object
3728 * @stream_tag: stream tag to assign
3729 * @format: format id to assign
3730 * @substream: PCM substream to assign
3732 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3733 struct hda_multi_out *mout,
3734 unsigned int stream_tag,
3735 unsigned int format,
3736 struct snd_pcm_substream *substream)
3738 mutex_lock(&codec->spdif_mutex);
3739 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3740 mutex_unlock(&codec->spdif_mutex);
3743 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
3746 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3747 * @codec: the HDA codec
3748 * @mout: hda_multi_out object
3750 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3751 struct hda_multi_out *mout)
3753 mutex_lock(&codec->spdif_mutex);
3754 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3755 mutex_unlock(&codec->spdif_mutex);
3758 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
3761 * snd_hda_multi_out_dig_close - release the digital out stream
3762 * @codec: the HDA codec
3763 * @mout: hda_multi_out object
3765 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3766 struct hda_multi_out *mout)
3768 mutex_lock(&codec->spdif_mutex);
3769 mout->dig_out_used = 0;
3770 mutex_unlock(&codec->spdif_mutex);
3773 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
3776 * snd_hda_multi_out_analog_open - open analog outputs
3777 * @codec: the HDA codec
3778 * @mout: hda_multi_out object
3779 * @substream: PCM substream to assign
3780 * @hinfo: PCM information to assign
3782 * Open analog outputs and set up the hw-constraints.
3783 * If the digital outputs can be opened as slave, open the digital
3786 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3787 struct hda_multi_out *mout,
3788 struct snd_pcm_substream *substream,
3789 struct hda_pcm_stream *hinfo)
3791 struct snd_pcm_runtime *runtime = substream->runtime;
3792 runtime->hw.channels_max = mout->max_channels;
3793 if (mout->dig_out_nid) {
3794 if (!mout->analog_rates) {
3795 mout->analog_rates = hinfo->rates;
3796 mout->analog_formats = hinfo->formats;
3797 mout->analog_maxbps = hinfo->maxbps;
3799 runtime->hw.rates = mout->analog_rates;
3800 runtime->hw.formats = mout->analog_formats;
3801 hinfo->maxbps = mout->analog_maxbps;
3803 if (!mout->spdif_rates) {
3804 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3806 &mout->spdif_formats,
3807 &mout->spdif_maxbps);
3809 mutex_lock(&codec->spdif_mutex);
3810 if (mout->share_spdif) {
3811 if ((runtime->hw.rates & mout->spdif_rates) &&
3812 (runtime->hw.formats & mout->spdif_formats)) {
3813 runtime->hw.rates &= mout->spdif_rates;
3814 runtime->hw.formats &= mout->spdif_formats;
3815 if (mout->spdif_maxbps < hinfo->maxbps)
3816 hinfo->maxbps = mout->spdif_maxbps;
3818 mout->share_spdif = 0;
3819 /* FIXME: need notify? */
3822 mutex_unlock(&codec->spdif_mutex);
3824 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3825 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3827 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
3830 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
3831 * @codec: the HDA codec
3832 * @mout: hda_multi_out object
3833 * @stream_tag: stream tag to assign
3834 * @format: format id to assign
3835 * @substream: PCM substream to assign
3837 * Set up the i/o for analog out.
3838 * When the digital out is available, copy the front out to digital out, too.
3840 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3841 struct hda_multi_out *mout,
3842 unsigned int stream_tag,
3843 unsigned int format,
3844 struct snd_pcm_substream *substream)
3846 const hda_nid_t *nids = mout->dac_nids;
3847 int chs = substream->runtime->channels;
3848 struct hda_spdif_out *spdif;
3851 mutex_lock(&codec->spdif_mutex);
3852 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
3853 if (mout->dig_out_nid && mout->share_spdif &&
3854 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3856 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3858 !(spdif->status & IEC958_AES0_NONAUDIO)) {
3859 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3860 setup_dig_out_stream(codec, mout->dig_out_nid,
3861 stream_tag, format);
3863 mout->dig_out_used = 0;
3864 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3867 mutex_unlock(&codec->spdif_mutex);
3870 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3872 if (!mout->no_share_stream &&
3873 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3874 /* headphone out will just decode front left/right (stereo) */
3875 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3877 /* extra outputs copied from front */
3878 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3879 if (!mout->no_share_stream && mout->hp_out_nid[i])
3880 snd_hda_codec_setup_stream(codec,
3881 mout->hp_out_nid[i],
3882 stream_tag, 0, format);
3885 for (i = 1; i < mout->num_dacs; i++) {
3886 if (chs >= (i + 1) * 2) /* independent out */
3887 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3889 else if (!mout->no_share_stream) /* copy front */
3890 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3894 /* extra surrounds */
3895 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3897 if (!mout->extra_out_nid[i])
3899 if (chs >= (i + 1) * 2)
3901 else if (!mout->no_share_stream)
3903 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3904 stream_tag, ch, format);
3909 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
3912 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
3913 * @codec: the HDA codec
3914 * @mout: hda_multi_out object
3916 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3917 struct hda_multi_out *mout)
3919 const hda_nid_t *nids = mout->dac_nids;
3922 for (i = 0; i < mout->num_dacs; i++)
3923 snd_hda_codec_cleanup_stream(codec, nids[i]);
3925 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3926 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3927 if (mout->hp_out_nid[i])
3928 snd_hda_codec_cleanup_stream(codec,
3929 mout->hp_out_nid[i]);
3930 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3931 if (mout->extra_out_nid[i])
3932 snd_hda_codec_cleanup_stream(codec,
3933 mout->extra_out_nid[i]);
3934 mutex_lock(&codec->spdif_mutex);
3935 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3936 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3937 mout->dig_out_used = 0;
3939 mutex_unlock(&codec->spdif_mutex);
3942 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
3945 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
3946 * @codec: the HDA codec
3947 * @pin: referred pin NID
3949 * Guess the suitable VREF pin bits to be set as the pin-control value.
3950 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3952 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3954 unsigned int pincap;
3955 unsigned int oldval;
3956 oldval = snd_hda_codec_read(codec, pin, 0,
3957 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3958 pincap = snd_hda_query_pin_caps(codec, pin);
3959 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3960 /* Exception: if the default pin setup is vref50, we give it priority */
3961 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3962 return AC_PINCTL_VREF_80;
3963 else if (pincap & AC_PINCAP_VREF_50)
3964 return AC_PINCTL_VREF_50;
3965 else if (pincap & AC_PINCAP_VREF_100)
3966 return AC_PINCTL_VREF_100;
3967 else if (pincap & AC_PINCAP_VREF_GRD)
3968 return AC_PINCTL_VREF_GRD;
3969 return AC_PINCTL_VREF_HIZ;
3971 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
3974 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3975 * @codec: the HDA codec
3976 * @pin: referred pin NID
3977 * @val: pin ctl value to audit
3979 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3980 hda_nid_t pin, unsigned int val)
3982 static unsigned int cap_lists[][2] = {
3983 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3984 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3985 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3986 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3992 cap = snd_hda_query_pin_caps(codec, pin);
3994 return val; /* don't know what to do... */
3996 if (val & AC_PINCTL_OUT_EN) {
3997 if (!(cap & AC_PINCAP_OUT))
3998 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3999 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4000 val &= ~AC_PINCTL_HP_EN;
4003 if (val & AC_PINCTL_IN_EN) {
4004 if (!(cap & AC_PINCAP_IN))
4005 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4007 unsigned int vcap, vref;
4009 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4010 vref = val & AC_PINCTL_VREFEN;
4011 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4012 if (vref == cap_lists[i][0] &&
4013 !(vcap & cap_lists[i][1])) {
4014 if (i == ARRAY_SIZE(cap_lists) - 1)
4015 vref = AC_PINCTL_VREF_HIZ;
4017 vref = cap_lists[i + 1][0];
4020 val &= ~AC_PINCTL_VREFEN;
4027 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4030 * _snd_hda_pin_ctl - Helper to set pin ctl value
4031 * @codec: the HDA codec
4032 * @pin: referred pin NID
4033 * @val: pin control value to set
4034 * @cached: access over codec pinctl cache or direct write
4036 * This function is a helper to set a pin ctl value more safely.
4037 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4038 * value in pin target array via snd_hda_codec_set_pin_target(), then
4039 * actually writes the value via either snd_hda_codec_update_cache() or
4040 * snd_hda_codec_write() depending on @cached flag.
4042 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4043 unsigned int val, bool cached)
4045 val = snd_hda_correct_pin_ctl(codec, pin, val);
4046 snd_hda_codec_set_pin_target(codec, pin, val);
4048 return snd_hda_codec_update_cache(codec, pin, 0,
4049 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4051 return snd_hda_codec_write(codec, pin, 0,
4052 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4054 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4057 * snd_hda_add_imux_item - Add an item to input_mux
4058 * @codec: the HDA codec
4059 * @imux: imux helper object
4060 * @label: the name of imux item to assign
4061 * @index: index number of imux item to assign
4062 * @type_idx: pointer to store the resultant label index
4064 * When the same label is used already in the existing items, the number
4065 * suffix is appended to the label. This label index number is stored
4066 * to type_idx when non-NULL pointer is given.
4068 int snd_hda_add_imux_item(struct hda_codec *codec,
4069 struct hda_input_mux *imux, const char *label,
4070 int index, int *type_idx)
4072 int i, label_idx = 0;
4073 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4074 codec_err(codec, "hda_codec: Too many imux items!\n");
4077 for (i = 0; i < imux->num_items; i++) {
4078 if (!strncmp(label, imux->items[i].label, strlen(label)))
4082 *type_idx = label_idx;
4084 snprintf(imux->items[imux->num_items].label,
4085 sizeof(imux->items[imux->num_items].label),
4086 "%s %d", label, label_idx);
4088 strlcpy(imux->items[imux->num_items].label, label,
4089 sizeof(imux->items[imux->num_items].label));
4090 imux->items[imux->num_items].index = index;
4094 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4097 * snd_hda_bus_reset_codecs - Reset the bus
4098 * @bus: HD-audio bus
4100 void snd_hda_bus_reset_codecs(struct hda_bus *bus)
4102 struct hda_codec *codec;
4104 list_for_each_codec(codec, bus) {
4105 /* FIXME: maybe a better way needed for forced reset */
4106 cancel_delayed_work_sync(&codec->jackpoll_work);
4108 if (hda_codec_is_power_on(codec)) {
4109 hda_call_codec_suspend(codec);
4110 hda_call_codec_resume(codec);
4117 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4118 * @pcm: PCM caps bits
4119 * @buf: the string buffer to write
4120 * @buflen: the max buffer length
4122 * used by hda_proc.c and hda_eld.c
4124 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4126 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4129 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4130 if (pcm & (AC_SUPPCM_BITS_8 << i))
4131 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4133 buf[j] = '\0'; /* necessary when j == 0 */
4135 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4137 MODULE_DESCRIPTION("HDA codec core");
4138 MODULE_LICENSE("GPL");