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(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(codec);
149 if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) {
150 if (bus->response_reset) {
152 "resetting BUS due to fatal communication error\n");
153 bus->ops.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 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
440 if (parm == -1 && codec->bus->rirb_error)
442 return parm & AC_DEV_LIST_LEN_MASK;
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
455 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
456 u8 *dev_list, int max_devices)
459 int i, dev_len, devices;
461 parm = get_num_devices(codec, nid);
462 if (!parm) /* not multi-stream capable */
466 dev_len = dev_len < max_devices ? dev_len : max_devices;
469 while (devices < dev_len) {
470 parm = snd_hda_codec_read(codec, nid, 0,
471 AC_VERB_GET_DEVICE_LIST, devices);
472 if (parm == -1 && codec->bus->rirb_error)
475 for (i = 0; i < 8; i++) {
476 dev_list[devices] = (u8)parm;
479 if (devices >= dev_len)
489 static void snd_hda_bus_free(struct hda_bus *bus)
493 if (bus->ops.private_free)
494 bus->ops.private_free(bus);
495 snd_hdac_bus_exit(&bus->core);
499 static int snd_hda_bus_dev_free(struct snd_device *device)
501 snd_hda_bus_free(device->device_data);
505 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
507 struct hda_bus *bus = device->device_data;
512 /* hdac_bus_ops translations */
513 static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd)
515 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
516 return bus->ops.command(bus, cmd);
519 static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr,
522 struct hda_bus *bus = container_of(_bus, struct hda_bus, core);
523 *res = bus->ops.get_response(bus, addr);
524 return bus->rirb_error ? -EIO : 0;
527 static const struct hdac_bus_ops bus_ops = {
528 .command = _hda_bus_command,
529 .get_response = _hda_bus_get_response,
533 * snd_hda_bus_new - create a HDA bus
534 * @card: the card entry
535 * @busp: the pointer to store the created bus instance
537 * Returns 0 if successful, or a negative error code.
539 int snd_hda_bus_new(struct snd_card *card,
540 struct hda_bus **busp)
544 static struct snd_device_ops dev_ops = {
545 .dev_disconnect = snd_hda_bus_dev_disconnect,
546 .dev_free = snd_hda_bus_dev_free,
552 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
556 err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops);
563 mutex_init(&bus->prepare_mutex);
565 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
567 snd_hda_bus_free(bus);
574 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
577 * read widget caps for each widget and store in cache
579 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
584 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
587 nid = codec->core.start_nid;
588 for (i = 0; i < codec->core.num_nodes; i++, nid++)
589 codec->wcaps[i] = snd_hda_param_read(codec, nid,
590 AC_PAR_AUDIO_WIDGET_CAP);
594 /* read all pin default configurations and save codec->init_pins */
595 static int read_pin_defaults(struct hda_codec *codec)
599 for_each_hda_codec_node(nid, codec) {
600 struct hda_pincfg *pin;
601 unsigned int wcaps = get_wcaps(codec, nid);
602 unsigned int wid_type = get_wcaps_type(wcaps);
603 if (wid_type != AC_WID_PIN)
605 pin = snd_array_new(&codec->init_pins);
609 pin->cfg = snd_hda_codec_read(codec, nid, 0,
610 AC_VERB_GET_CONFIG_DEFAULT, 0);
611 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
612 AC_VERB_GET_PIN_WIDGET_CONTROL,
618 /* look up the given pin config list and return the item matching with NID */
619 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
620 struct snd_array *array,
624 for (i = 0; i < array->used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(array, i);
632 /* set the current pin config value for the given NID.
633 * the value is cached, and read via snd_hda_codec_get_pincfg()
635 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
636 hda_nid_t nid, unsigned int cfg)
638 struct hda_pincfg *pin;
640 /* the check below may be invalid when pins are added by a fixup
641 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
645 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
649 pin = look_up_pincfg(codec, list, nid);
651 pin = snd_array_new(list);
661 * snd_hda_codec_set_pincfg - Override a pin default configuration
662 * @codec: the HDA codec
663 * @nid: NID to set the pin config
664 * @cfg: the pin default config value
666 * Override a pin default configuration value in the cache.
667 * This value can be read by snd_hda_codec_get_pincfg() in a higher
668 * priority than the real hardware value.
670 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
671 hda_nid_t nid, unsigned int cfg)
673 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
675 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
678 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
679 * @codec: the HDA codec
680 * @nid: NID to get the pin config
682 * Get the current pin config value of the given pin NID.
683 * If the pincfg value is cached or overridden via sysfs or driver,
684 * returns the cached value.
686 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
688 struct hda_pincfg *pin;
690 #ifdef CONFIG_SND_HDA_RECONFIG
692 unsigned int cfg = 0;
693 mutex_lock(&codec->user_mutex);
694 pin = look_up_pincfg(codec, &codec->user_pins, nid);
697 mutex_unlock(&codec->user_mutex);
702 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
705 pin = look_up_pincfg(codec, &codec->init_pins, nid);
710 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
713 * snd_hda_codec_set_pin_target - remember the current pinctl target value
714 * @codec: the HDA codec
716 * @val: assigned pinctl value
718 * This function stores the given value to a pinctl target value in the
719 * pincfg table. This isn't always as same as the actually written value
720 * but can be referred at any time via snd_hda_codec_get_pin_target().
722 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
725 struct hda_pincfg *pin;
727 pin = look_up_pincfg(codec, &codec->init_pins, nid);
733 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
736 * snd_hda_codec_get_pin_target - return the current pinctl target value
737 * @codec: the HDA codec
740 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
742 struct hda_pincfg *pin;
744 pin = look_up_pincfg(codec, &codec->init_pins, nid);
749 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
752 * snd_hda_shutup_pins - Shut up all pins
753 * @codec: the HDA codec
755 * Clear all pin controls to shup up before suspend for avoiding click noise.
756 * The controls aren't cached so that they can be resumed properly.
758 void snd_hda_shutup_pins(struct hda_codec *codec)
761 /* don't shut up pins when unloading the driver; otherwise it breaks
762 * the default pin setup at the next load of the driver
764 if (codec->bus->shutdown)
766 for (i = 0; i < codec->init_pins.used; i++) {
767 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
768 /* use read here for syncing after issuing each verb */
769 snd_hda_codec_read(codec, pin->nid, 0,
770 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
772 codec->pins_shutup = 1;
774 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
777 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
778 static void restore_shutup_pins(struct hda_codec *codec)
781 if (!codec->pins_shutup)
783 if (codec->bus->shutdown)
785 for (i = 0; i < codec->init_pins.used; i++) {
786 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
787 snd_hda_codec_write(codec, pin->nid, 0,
788 AC_VERB_SET_PIN_WIDGET_CONTROL,
791 codec->pins_shutup = 0;
795 static void hda_jackpoll_work(struct work_struct *work)
797 struct hda_codec *codec =
798 container_of(work, struct hda_codec, jackpoll_work.work);
800 snd_hda_jack_set_dirty_all(codec);
801 snd_hda_jack_poll_all(codec);
803 if (!codec->jackpoll_interval)
806 schedule_delayed_work(&codec->jackpoll_work,
807 codec->jackpoll_interval);
810 static void init_hda_cache(struct hda_cache_rec *cache,
811 unsigned int record_size);
812 static void free_hda_cache(struct hda_cache_rec *cache);
814 /* release all pincfg lists */
815 static void free_init_pincfgs(struct hda_codec *codec)
817 snd_array_free(&codec->driver_pins);
818 #ifdef CONFIG_SND_HDA_RECONFIG
819 snd_array_free(&codec->user_pins);
821 snd_array_free(&codec->init_pins);
825 * audio-converter setup caches
827 struct hda_cvt_setup {
832 unsigned char active; /* cvt is currently used */
833 unsigned char dirty; /* setups should be cleared */
836 /* get or create a cache entry for the given audio converter NID */
837 static struct hda_cvt_setup *
838 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
840 struct hda_cvt_setup *p;
843 for (i = 0; i < codec->cvt_setups.used; i++) {
844 p = snd_array_elem(&codec->cvt_setups, i);
848 p = snd_array_new(&codec->cvt_setups);
857 static void release_pcm(struct kref *kref)
859 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
862 snd_device_free(pcm->codec->card, pcm->pcm);
863 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
868 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
870 kref_put(&pcm->kref, release_pcm);
872 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
874 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
875 const char *fmt, ...)
881 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
886 kref_init(&pcm->kref);
887 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
893 list_add_tail(&pcm->list, &codec->pcm_list_head);
896 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
901 static void codec_release_pcms(struct hda_codec *codec)
903 struct hda_pcm *pcm, *n;
905 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
906 list_del_init(&pcm->list);
908 snd_device_disconnect(codec->card, pcm->pcm);
909 snd_hda_codec_pcm_put(pcm);
913 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
915 cancel_delayed_work_sync(&codec->jackpoll_work);
916 if (!codec->in_freeing)
917 snd_hda_ctls_clear(codec);
918 codec_release_pcms(codec);
919 snd_hda_detach_beep_device(codec);
920 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
921 snd_hda_jack_tbl_clear(codec);
922 codec->proc_widget_hook = NULL;
925 free_hda_cache(&codec->amp_cache);
926 free_hda_cache(&codec->cmd_cache);
927 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
928 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
930 /* free only driver_pins so that init_pins + user_pins are restored */
931 snd_array_free(&codec->driver_pins);
932 snd_array_free(&codec->cvt_setups);
933 snd_array_free(&codec->spdif_out);
934 snd_array_free(&codec->verbs);
935 codec->preset = NULL;
936 codec->slave_dig_outs = NULL;
937 codec->spdif_status_reset = 0;
938 snd_array_free(&codec->mixers);
939 snd_array_free(&codec->nids);
940 remove_conn_list(codec);
943 static unsigned int hda_set_power_state(struct hda_codec *codec,
944 unsigned int power_state);
946 static int snd_hda_codec_dev_register(struct snd_device *device)
948 struct hda_codec *codec = device->device_data;
950 snd_hda_register_beep_device(codec);
951 if (device_is_registered(hda_codec_dev(codec)))
952 pm_runtime_enable(hda_codec_dev(codec));
953 /* it was powered up in snd_hda_codec_new(), now all done */
954 snd_hda_power_down(codec);
958 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
960 struct hda_codec *codec = device->device_data;
962 snd_hda_detach_beep_device(codec);
966 static int snd_hda_codec_dev_free(struct snd_device *device)
968 struct hda_codec *codec = device->device_data;
970 codec->in_freeing = 1;
971 snd_hdac_device_unregister(&codec->core);
972 put_device(hda_codec_dev(codec));
976 static void snd_hda_codec_dev_release(struct device *dev)
978 struct hda_codec *codec = dev_to_hda_codec(dev);
980 free_init_pincfgs(codec);
981 snd_hdac_device_exit(&codec->core);
982 snd_hda_sysfs_clear(codec);
983 free_hda_cache(&codec->amp_cache);
984 free_hda_cache(&codec->cmd_cache);
985 kfree(codec->modelname);
991 * snd_hda_codec_new - create a HDA codec
992 * @bus: the bus to assign
993 * @codec_addr: the codec address
994 * @codecp: the pointer to store the generated codec
996 * Returns 0 if successful, or a negative error code.
998 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
999 unsigned int codec_addr, struct hda_codec **codecp)
1001 struct hda_codec *codec;
1005 static struct snd_device_ops dev_ops = {
1006 .dev_register = snd_hda_codec_dev_register,
1007 .dev_disconnect = snd_hda_codec_dev_disconnect,
1008 .dev_free = snd_hda_codec_dev_free,
1011 if (snd_BUG_ON(!bus))
1013 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1016 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1020 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1021 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1028 codec->core.dev.release = snd_hda_codec_dev_release;
1029 codec->core.type = HDA_DEV_LEGACY;
1030 codec->core.exec_verb = codec_exec_verb;
1034 codec->addr = codec_addr;
1035 mutex_init(&codec->spdif_mutex);
1036 mutex_init(&codec->control_mutex);
1037 mutex_init(&codec->hash_mutex);
1038 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1039 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1040 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1041 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1042 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1043 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1044 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1045 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1046 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1047 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1048 INIT_LIST_HEAD(&codec->conn_list);
1049 INIT_LIST_HEAD(&codec->pcm_list_head);
1051 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1052 codec->depop_delay = -1;
1053 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1056 codec->power_jiffies = jiffies;
1059 snd_hda_sysfs_init(codec);
1061 if (codec->bus->modelname) {
1062 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1063 if (!codec->modelname) {
1069 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1070 err = read_widget_caps(codec, fg);
1073 err = read_pin_defaults(codec);
1077 /* power-up all before initialization */
1078 hda_set_power_state(codec, AC_PWRST_D0);
1080 snd_hda_codec_proc_new(codec);
1082 snd_hda_create_hwdep(codec);
1084 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1085 codec->core.subsystem_id, codec->core.revision_id);
1086 snd_component_add(card, component);
1088 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
1097 pm_runtime_put_noidle(hda_codec_dev(codec));
1098 put_device(hda_codec_dev(codec));
1101 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1104 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1105 * @codec: the HDA codec
1107 * Forcibly refresh the all widget caps and the init pin configurations of
1110 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1115 err = snd_hdac_refresh_widgets(&codec->core);
1119 /* Assume the function group node does not change,
1120 * only the widget nodes may change.
1122 kfree(codec->wcaps);
1123 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1124 err = read_widget_caps(codec, fg);
1128 snd_array_free(&codec->init_pins);
1129 err = read_pin_defaults(codec);
1133 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1135 /* update the stream-id if changed */
1136 static void update_pcm_stream_id(struct hda_codec *codec,
1137 struct hda_cvt_setup *p, hda_nid_t nid,
1138 u32 stream_tag, int channel_id)
1140 unsigned int oldval, newval;
1142 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1143 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1144 newval = (stream_tag << 4) | channel_id;
1145 if (oldval != newval)
1146 snd_hda_codec_write(codec, nid, 0,
1147 AC_VERB_SET_CHANNEL_STREAMID,
1149 p->stream_tag = stream_tag;
1150 p->channel_id = channel_id;
1154 /* update the format-id if changed */
1155 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1156 hda_nid_t nid, int format)
1158 unsigned int oldval;
1160 if (p->format_id != format) {
1161 oldval = snd_hda_codec_read(codec, nid, 0,
1162 AC_VERB_GET_STREAM_FORMAT, 0);
1163 if (oldval != format) {
1165 snd_hda_codec_write(codec, nid, 0,
1166 AC_VERB_SET_STREAM_FORMAT,
1169 p->format_id = format;
1174 * snd_hda_codec_setup_stream - set up the codec for streaming
1175 * @codec: the CODEC to set up
1176 * @nid: the NID to set up
1177 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1178 * @channel_id: channel id to pass, zero based.
1179 * @format: stream format.
1181 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1183 int channel_id, int format)
1185 struct hda_codec *c;
1186 struct hda_cvt_setup *p;
1194 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1195 nid, stream_tag, channel_id, format);
1196 p = get_hda_cvt_setup(codec, nid);
1200 if (codec->patch_ops.stream_pm)
1201 codec->patch_ops.stream_pm(codec, nid, true);
1202 if (codec->pcm_format_first)
1203 update_pcm_format(codec, p, nid, format);
1204 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1205 if (!codec->pcm_format_first)
1206 update_pcm_format(codec, p, nid, format);
1211 /* make other inactive cvts with the same stream-tag dirty */
1212 type = get_wcaps_type(get_wcaps(codec, nid));
1213 list_for_each_codec(c, codec->bus) {
1214 for (i = 0; i < c->cvt_setups.used; i++) {
1215 p = snd_array_elem(&c->cvt_setups, i);
1216 if (!p->active && p->stream_tag == stream_tag &&
1217 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1222 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1224 static void really_cleanup_stream(struct hda_codec *codec,
1225 struct hda_cvt_setup *q);
1228 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1229 * @codec: the CODEC to clean up
1230 * @nid: the NID to clean up
1231 * @do_now: really clean up the stream instead of clearing the active flag
1233 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1236 struct hda_cvt_setup *p;
1241 if (codec->no_sticky_stream)
1244 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1245 p = get_hda_cvt_setup(codec, nid);
1247 /* here we just clear the active flag when do_now isn't set;
1248 * actual clean-ups will be done later in
1249 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1252 really_cleanup_stream(codec, p);
1257 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1259 static void really_cleanup_stream(struct hda_codec *codec,
1260 struct hda_cvt_setup *q)
1262 hda_nid_t nid = q->nid;
1263 if (q->stream_tag || q->channel_id)
1264 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1266 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1268 memset(q, 0, sizeof(*q));
1270 if (codec->patch_ops.stream_pm)
1271 codec->patch_ops.stream_pm(codec, nid, false);
1274 /* clean up the all conflicting obsolete streams */
1275 static void purify_inactive_streams(struct hda_codec *codec)
1277 struct hda_codec *c;
1280 list_for_each_codec(c, codec->bus) {
1281 for (i = 0; i < c->cvt_setups.used; i++) {
1282 struct hda_cvt_setup *p;
1283 p = snd_array_elem(&c->cvt_setups, i);
1285 really_cleanup_stream(c, p);
1291 /* clean up all streams; called from suspend */
1292 static void hda_cleanup_all_streams(struct hda_codec *codec)
1296 for (i = 0; i < codec->cvt_setups.used; i++) {
1297 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1299 really_cleanup_stream(codec, p);
1305 * amp access functions
1308 /* FIXME: more better hash key? */
1309 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1310 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1311 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1312 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1313 #define INFO_AMP_CAPS (1<<0)
1314 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1316 /* initialize the hash table */
1317 static void init_hda_cache(struct hda_cache_rec *cache,
1318 unsigned int record_size)
1320 memset(cache, 0, sizeof(*cache));
1321 memset(cache->hash, 0xff, sizeof(cache->hash));
1322 snd_array_init(&cache->buf, record_size, 64);
1325 static void free_hda_cache(struct hda_cache_rec *cache)
1327 snd_array_free(&cache->buf);
1330 /* query the hash. allocate an entry if not found. */
1331 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1333 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1334 u16 cur = cache->hash[idx];
1335 struct hda_cache_head *info;
1337 while (cur != 0xffff) {
1338 info = snd_array_elem(&cache->buf, cur);
1339 if (info->key == key)
1346 /* query the hash. allocate an entry if not found. */
1347 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1350 struct hda_cache_head *info = get_hash(cache, key);
1353 /* add a new hash entry */
1354 info = snd_array_new(&cache->buf);
1357 cur = snd_array_index(&cache->buf, info);
1361 idx = key % (u16)ARRAY_SIZE(cache->hash);
1362 info->next = cache->hash[idx];
1363 cache->hash[idx] = cur;
1368 /* query and allocate an amp hash entry */
1369 static inline struct hda_amp_info *
1370 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1372 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1375 /* overwrite the value with the key in the caps hash */
1376 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1378 struct hda_amp_info *info;
1380 mutex_lock(&codec->hash_mutex);
1381 info = get_alloc_amp_hash(codec, key);
1383 mutex_unlock(&codec->hash_mutex);
1386 info->amp_caps = val;
1387 info->head.val |= INFO_AMP_CAPS;
1388 mutex_unlock(&codec->hash_mutex);
1392 /* query the value from the caps hash; if not found, fetch the current
1393 * value from the given function and store in the hash
1396 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1397 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1399 struct hda_amp_info *info;
1402 mutex_lock(&codec->hash_mutex);
1403 info = get_alloc_amp_hash(codec, key);
1405 mutex_unlock(&codec->hash_mutex);
1408 if (!(info->head.val & INFO_AMP_CAPS)) {
1409 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1410 val = func(codec, nid, dir);
1411 write_caps_hash(codec, key, val);
1413 val = info->amp_caps;
1414 mutex_unlock(&codec->hash_mutex);
1419 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1422 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1423 nid = codec->core.afg;
1424 return snd_hda_param_read(codec, nid,
1425 direction == HDA_OUTPUT ?
1426 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1430 * query_amp_caps - query AMP capabilities
1431 * @codec: the HD-auio codec
1432 * @nid: the NID to query
1433 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1435 * Query AMP capabilities for the given widget and direction.
1436 * Returns the obtained capability bits.
1438 * When cap bits have been already read, this doesn't read again but
1439 * returns the cached value.
1441 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1443 return query_caps_hash(codec, nid, direction,
1444 HDA_HASH_KEY(nid, direction, 0),
1447 EXPORT_SYMBOL_GPL(query_amp_caps);
1450 * snd_hda_check_amp_caps - query AMP capabilities
1451 * @codec: the HD-audio codec
1452 * @nid: the NID to query
1453 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1454 * @bits: bit mask to check the result
1456 * Check whether the widget has the given amp capability for the direction.
1458 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1459 int dir, unsigned int bits)
1463 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1464 if (query_amp_caps(codec, nid, dir) & bits)
1468 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1471 * snd_hda_override_amp_caps - Override the AMP capabilities
1472 * @codec: the CODEC to clean up
1473 * @nid: the NID to clean up
1474 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1475 * @caps: the capability bits to set
1477 * Override the cached AMP caps bits value by the given one.
1478 * This function is useful if the driver needs to adjust the AMP ranges,
1479 * e.g. limit to 0dB, etc.
1481 * Returns zero if successful or a negative error code.
1483 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1486 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1488 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1490 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1493 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1497 * snd_hda_query_pin_caps - Query PIN capabilities
1498 * @codec: the HD-auio codec
1499 * @nid: the NID to query
1501 * Query PIN capabilities for the given widget.
1502 * Returns the obtained capability bits.
1504 * When cap bits have been already read, this doesn't read again but
1505 * returns the cached value.
1507 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1509 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1512 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
1515 * snd_hda_override_pin_caps - Override the pin capabilities
1517 * @nid: the NID to override
1518 * @caps: the capability bits to set
1520 * Override the cached PIN capabilitiy bits value by the given one.
1522 * Returns zero if successful or a negative error code.
1524 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1527 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1529 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
1531 /* read or sync the hash value with the current value;
1532 * call within hash_mutex
1534 static struct hda_amp_info *
1535 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1536 int direction, int index, bool init_only)
1538 struct hda_amp_info *info;
1539 unsigned int parm, val = 0;
1540 bool val_read = false;
1543 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1546 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1548 mutex_unlock(&codec->hash_mutex);
1549 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1550 parm |= direction == HDA_OUTPUT ?
1551 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1553 val = snd_hda_codec_read(codec, nid, 0,
1554 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1557 mutex_lock(&codec->hash_mutex);
1560 info->vol[ch] = val;
1561 info->head.val |= INFO_AMP_VOL(ch);
1562 } else if (init_only)
1568 * write the current volume in info to the h/w
1570 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1571 hda_nid_t nid, int ch, int direction, int index,
1576 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1577 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1578 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1579 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1580 (amp_caps & AC_AMPCAP_MIN_MUTE))
1581 ; /* set the zero value as a fake mute */
1584 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1588 * snd_hda_codec_amp_read - Read AMP value
1589 * @codec: HD-audio codec
1590 * @nid: NID to read the AMP value
1591 * @ch: channel (left=0 or right=1)
1592 * @direction: #HDA_INPUT or #HDA_OUTPUT
1593 * @index: the index value (only for input direction)
1595 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1597 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1598 int direction, int index)
1600 struct hda_amp_info *info;
1601 unsigned int val = 0;
1603 mutex_lock(&codec->hash_mutex);
1604 info = update_amp_hash(codec, nid, ch, direction, index, false);
1606 val = info->vol[ch];
1607 mutex_unlock(&codec->hash_mutex);
1610 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
1612 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1613 int direction, int idx, int mask, int val,
1614 bool init_only, bool cache_only)
1616 struct hda_amp_info *info;
1619 if (snd_BUG_ON(mask & ~0xff))
1623 mutex_lock(&codec->hash_mutex);
1624 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1626 mutex_unlock(&codec->hash_mutex);
1629 val |= info->vol[ch] & ~mask;
1630 if (info->vol[ch] == val) {
1631 mutex_unlock(&codec->hash_mutex);
1634 info->vol[ch] = val;
1635 info->head.dirty |= cache_only;
1636 caps = info->amp_caps;
1637 mutex_unlock(&codec->hash_mutex);
1639 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
1644 * snd_hda_codec_amp_update - update the AMP value
1645 * @codec: HD-audio codec
1646 * @nid: NID to read the AMP value
1647 * @ch: channel (left=0 or right=1)
1648 * @direction: #HDA_INPUT or #HDA_OUTPUT
1649 * @idx: the index value (only for input direction)
1650 * @mask: bit mask to set
1651 * @val: the bits value to set
1653 * Update the AMP value with a bit mask.
1654 * Returns 0 if the value is unchanged, 1 if changed.
1656 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1657 int direction, int idx, int mask, int val)
1659 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1660 false, codec->cached_write);
1662 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1665 * snd_hda_codec_amp_stereo - update the AMP stereo values
1666 * @codec: HD-audio codec
1667 * @nid: NID to read the AMP value
1668 * @direction: #HDA_INPUT or #HDA_OUTPUT
1669 * @idx: the index value (only for input direction)
1670 * @mask: bit mask to set
1671 * @val: the bits value to set
1673 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1674 * stereo widget with the same mask and value.
1676 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1677 int direction, int idx, int mask, int val)
1681 if (snd_BUG_ON(mask & ~0xff))
1683 for (ch = 0; ch < 2; ch++)
1684 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1688 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1691 * snd_hda_codec_amp_init - initialize the AMP value
1692 * @codec: the HDA codec
1693 * @nid: NID to read the AMP value
1694 * @ch: channel (left=0 or right=1)
1695 * @dir: #HDA_INPUT or #HDA_OUTPUT
1696 * @idx: the index value (only for input direction)
1697 * @mask: bit mask to set
1698 * @val: the bits value to set
1700 * Works like snd_hda_codec_amp_update() but it writes the value only at
1701 * the first access. If the amp was already initialized / updated beforehand,
1702 * this does nothing.
1704 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1705 int dir, int idx, int mask, int val)
1707 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1708 codec->cached_write);
1710 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1713 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1714 * @codec: the HDA codec
1715 * @nid: NID to read the AMP value
1716 * @dir: #HDA_INPUT or #HDA_OUTPUT
1717 * @idx: the index value (only for input direction)
1718 * @mask: bit mask to set
1719 * @val: the bits value to set
1721 * Call snd_hda_codec_amp_init() for both stereo channels.
1723 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1724 int dir, int idx, int mask, int val)
1728 if (snd_BUG_ON(mask & ~0xff))
1730 for (ch = 0; ch < 2; ch++)
1731 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1735 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1738 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1739 * @codec: HD-audio codec
1741 * Resume the all amp commands from the cache.
1743 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1747 mutex_lock(&codec->hash_mutex);
1748 codec->cached_write = 0;
1749 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1750 struct hda_amp_info *buffer;
1753 unsigned int idx, dir, ch;
1754 struct hda_amp_info info;
1756 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1757 if (!buffer->head.dirty)
1759 buffer->head.dirty = 0;
1761 key = info.head.key;
1765 idx = (key >> 16) & 0xff;
1766 dir = (key >> 24) & 0xff;
1767 for (ch = 0; ch < 2; ch++) {
1768 if (!(info.head.val & INFO_AMP_VOL(ch)))
1770 mutex_unlock(&codec->hash_mutex);
1771 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
1773 mutex_lock(&codec->hash_mutex);
1776 mutex_unlock(&codec->hash_mutex);
1778 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
1780 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1783 u32 caps = query_amp_caps(codec, nid, dir);
1785 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1792 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1793 * @kcontrol: referred ctl element
1794 * @uinfo: pointer to get/store the data
1796 * The control element is supposed to have the private_value field
1797 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1799 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_info *uinfo)
1802 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1803 u16 nid = get_amp_nid(kcontrol);
1804 u8 chs = get_amp_channels(kcontrol);
1805 int dir = get_amp_direction(kcontrol);
1806 unsigned int ofs = get_amp_offset(kcontrol);
1808 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1809 uinfo->count = chs == 3 ? 2 : 1;
1810 uinfo->value.integer.min = 0;
1811 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1812 if (!uinfo->value.integer.max) {
1814 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1815 nid, kcontrol->id.name);
1820 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1823 static inline unsigned int
1824 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1825 int ch, int dir, int idx, unsigned int ofs)
1828 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1829 val &= HDA_AMP_VOLMASK;
1838 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1839 int ch, int dir, int idx, unsigned int ofs,
1842 unsigned int maxval;
1846 /* ofs = 0: raw max value */
1847 maxval = get_amp_max_value(codec, nid, dir, 0);
1850 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
1851 false, !hda_codec_is_power_on(codec));
1855 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1856 * @kcontrol: ctl element
1857 * @ucontrol: pointer to get/store the data
1859 * The control element is supposed to have the private_value field
1860 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1862 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1863 struct snd_ctl_elem_value *ucontrol)
1865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1866 hda_nid_t nid = get_amp_nid(kcontrol);
1867 int chs = get_amp_channels(kcontrol);
1868 int dir = get_amp_direction(kcontrol);
1869 int idx = get_amp_index(kcontrol);
1870 unsigned int ofs = get_amp_offset(kcontrol);
1871 long *valp = ucontrol->value.integer.value;
1874 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1876 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1879 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1882 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1883 * @kcontrol: ctl element
1884 * @ucontrol: pointer to get/store the data
1886 * The control element is supposed to have the private_value field
1887 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1889 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_value *ucontrol)
1892 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1893 hda_nid_t nid = get_amp_nid(kcontrol);
1894 int chs = get_amp_channels(kcontrol);
1895 int dir = get_amp_direction(kcontrol);
1896 int idx = get_amp_index(kcontrol);
1897 unsigned int ofs = get_amp_offset(kcontrol);
1898 long *valp = ucontrol->value.integer.value;
1902 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1906 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1909 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1912 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1913 * @kcontrol: ctl element
1914 * @op_flag: operation flag
1915 * @size: byte size of input TLV
1918 * The control element is supposed to have the private_value field
1919 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1921 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1922 unsigned int size, unsigned int __user *_tlv)
1924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1925 hda_nid_t nid = get_amp_nid(kcontrol);
1926 int dir = get_amp_direction(kcontrol);
1927 unsigned int ofs = get_amp_offset(kcontrol);
1928 bool min_mute = get_amp_min_mute(kcontrol);
1929 u32 caps, val1, val2;
1931 if (size < 4 * sizeof(unsigned int))
1933 caps = query_amp_caps(codec, nid, dir);
1934 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1935 val2 = (val2 + 1) * 25;
1936 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1938 val1 = ((int)val1) * ((int)val2);
1939 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1940 val2 |= TLV_DB_SCALE_MUTE;
1941 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1943 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1945 if (put_user(val1, _tlv + 2))
1947 if (put_user(val2, _tlv + 3))
1951 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1954 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1955 * @codec: HD-audio codec
1956 * @nid: NID of a reference widget
1957 * @dir: #HDA_INPUT or #HDA_OUTPUT
1958 * @tlv: TLV data to be stored, at least 4 elements
1960 * Set (static) TLV data for a virtual master volume using the AMP caps
1961 * obtained from the reference NID.
1962 * The volume range is recalculated as if the max volume is 0dB.
1964 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1970 caps = query_amp_caps(codec, nid, dir);
1971 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1972 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1973 step = (step + 1) * 25;
1974 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1975 tlv[1] = 2 * sizeof(unsigned int);
1976 tlv[2] = -nums * step;
1979 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1981 /* find a mixer control element with the given name */
1982 static struct snd_kcontrol *
1983 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1985 struct snd_ctl_elem_id id;
1986 memset(&id, 0, sizeof(id));
1987 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1990 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1992 strcpy(id.name, name);
1993 return snd_ctl_find_id(codec->card, &id);
1997 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1998 * @codec: HD-audio codec
1999 * @name: ctl id name string
2001 * Get the control element with the given id string and IFACE_MIXER.
2003 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2006 return find_mixer_ctl(codec, name, 0, 0);
2008 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2010 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2014 /* 16 ctlrs should be large enough */
2015 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2016 if (!find_mixer_ctl(codec, name, 0, idx))
2023 * snd_hda_ctl_add - Add a control element and assign to the codec
2024 * @codec: HD-audio codec
2025 * @nid: corresponding NID (optional)
2026 * @kctl: the control element to assign
2028 * Add the given control element to an array inside the codec instance.
2029 * All control elements belonging to a codec are supposed to be added
2030 * by this function so that a proper clean-up works at the free or
2031 * reconfiguration time.
2033 * If non-zero @nid is passed, the NID is assigned to the control element.
2034 * The assignment is shown in the codec proc file.
2036 * snd_hda_ctl_add() checks the control subdev id field whether
2037 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2038 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2039 * specifies if kctl->private_value is a HDA amplifier value.
2041 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2042 struct snd_kcontrol *kctl)
2045 unsigned short flags = 0;
2046 struct hda_nid_item *item;
2048 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2049 flags |= HDA_NID_ITEM_AMP;
2051 nid = get_amp_nid_(kctl->private_value);
2053 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2054 nid = kctl->id.subdevice & 0xffff;
2055 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2056 kctl->id.subdevice = 0;
2057 err = snd_ctl_add(codec->card, kctl);
2060 item = snd_array_new(&codec->mixers);
2065 item->flags = flags;
2068 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2071 * snd_hda_add_nid - Assign a NID to a control element
2072 * @codec: HD-audio codec
2073 * @nid: corresponding NID (optional)
2074 * @kctl: the control element to assign
2075 * @index: index to kctl
2077 * Add the given control element to an array inside the codec instance.
2078 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2079 * NID:KCTL mapping - for example "Capture Source" selector.
2081 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2082 unsigned int index, hda_nid_t nid)
2084 struct hda_nid_item *item;
2087 item = snd_array_new(&codec->nids);
2091 item->index = index;
2095 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2096 kctl->id.name, kctl->id.index, index);
2099 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2102 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2103 * @codec: HD-audio codec
2105 void snd_hda_ctls_clear(struct hda_codec *codec)
2108 struct hda_nid_item *items = codec->mixers.list;
2109 for (i = 0; i < codec->mixers.used; i++)
2110 snd_ctl_remove(codec->card, items[i].kctl);
2111 snd_array_free(&codec->mixers);
2112 snd_array_free(&codec->nids);
2116 * snd_hda_lock_devices - pseudo device locking
2119 * toggle card->shutdown to allow/disallow the device access (as a hack)
2121 int snd_hda_lock_devices(struct hda_bus *bus)
2123 struct snd_card *card = bus->card;
2124 struct hda_codec *codec;
2126 spin_lock(&card->files_lock);
2130 if (!list_empty(&card->ctl_files))
2133 list_for_each_codec(codec, bus) {
2134 struct hda_pcm *cpcm;
2135 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
2138 if (cpcm->pcm->streams[0].substream_opened ||
2139 cpcm->pcm->streams[1].substream_opened)
2143 spin_unlock(&card->files_lock);
2149 spin_unlock(&card->files_lock);
2152 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2155 * snd_hda_unlock_devices - pseudo device unlocking
2158 void snd_hda_unlock_devices(struct hda_bus *bus)
2160 struct snd_card *card = bus->card;
2162 spin_lock(&card->files_lock);
2164 spin_unlock(&card->files_lock);
2166 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2169 * snd_hda_codec_reset - Clear all objects assigned to the codec
2170 * @codec: HD-audio codec
2172 * This frees the all PCM and control elements assigned to the codec, and
2173 * clears the caches and restores the pin default configurations.
2175 * When a device is being used, it returns -EBSY. If successfully freed,
2178 int snd_hda_codec_reset(struct hda_codec *codec)
2180 struct hda_bus *bus = codec->bus;
2182 if (snd_hda_lock_devices(bus) < 0)
2185 /* OK, let it free */
2186 snd_hdac_device_unregister(&codec->core);
2188 /* allow device access again */
2189 snd_hda_unlock_devices(bus);
2193 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2195 /* apply the function to all matching slave ctls in the mixer list */
2196 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2197 const char *suffix, map_slave_func_t func, void *data)
2199 struct hda_nid_item *items;
2200 const char * const *s;
2203 items = codec->mixers.list;
2204 for (i = 0; i < codec->mixers.used; i++) {
2205 struct snd_kcontrol *sctl = items[i].kctl;
2206 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2208 for (s = slaves; *s; s++) {
2209 char tmpname[sizeof(sctl->id.name)];
2210 const char *name = *s;
2212 snprintf(tmpname, sizeof(tmpname), "%s %s",
2216 if (!strcmp(sctl->id.name, name)) {
2217 err = func(codec, data, sctl);
2227 static int check_slave_present(struct hda_codec *codec,
2228 void *data, struct snd_kcontrol *sctl)
2233 /* guess the value corresponding to 0dB */
2234 static int get_kctl_0dB_offset(struct hda_codec *codec,
2235 struct snd_kcontrol *kctl, int *step_to_check)
2238 const int *tlv = NULL;
2241 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2242 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2243 mm_segment_t fs = get_fs();
2245 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2248 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2250 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2252 step &= ~TLV_DB_SCALE_MUTE;
2255 if (*step_to_check && *step_to_check != step) {
2256 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2257 - *step_to_check, step);
2260 *step_to_check = step;
2261 val = -tlv[2] / step;
2266 /* call kctl->put with the given value(s) */
2267 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2269 struct snd_ctl_elem_value *ucontrol;
2270 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2273 ucontrol->value.integer.value[0] = val;
2274 ucontrol->value.integer.value[1] = val;
2275 kctl->put(kctl, ucontrol);
2280 /* initialize the slave volume with 0dB */
2281 static int init_slave_0dB(struct hda_codec *codec,
2282 void *data, struct snd_kcontrol *slave)
2284 int offset = get_kctl_0dB_offset(codec, slave, data);
2286 put_kctl_with_value(slave, offset);
2290 /* unmute the slave */
2291 static int init_slave_unmute(struct hda_codec *codec,
2292 void *data, struct snd_kcontrol *slave)
2294 return put_kctl_with_value(slave, 1);
2297 static int add_slave(struct hda_codec *codec,
2298 void *data, struct snd_kcontrol *slave)
2300 return snd_ctl_add_slave(data, slave);
2304 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2305 * @codec: HD-audio codec
2306 * @name: vmaster control name
2307 * @tlv: TLV data (optional)
2308 * @slaves: slave control names (optional)
2309 * @suffix: suffix string to each slave name (optional)
2310 * @init_slave_vol: initialize slaves to unmute/0dB
2311 * @ctl_ret: store the vmaster kcontrol in return
2313 * Create a virtual master control with the given name. The TLV data
2314 * must be either NULL or a valid data.
2316 * @slaves is a NULL-terminated array of strings, each of which is a
2317 * slave control name. All controls with these names are assigned to
2318 * the new virtual master control.
2320 * This function returns zero if successful or a negative error code.
2322 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2323 unsigned int *tlv, const char * const *slaves,
2324 const char *suffix, bool init_slave_vol,
2325 struct snd_kcontrol **ctl_ret)
2327 struct snd_kcontrol *kctl;
2333 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2335 codec_dbg(codec, "No slave found for %s\n", name);
2338 kctl = snd_ctl_make_virtual_master(name, tlv);
2341 err = snd_hda_ctl_add(codec, 0, kctl);
2345 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2349 /* init with master mute & zero volume */
2350 put_kctl_with_value(kctl, 0);
2351 if (init_slave_vol) {
2353 map_slaves(codec, slaves, suffix,
2354 tlv ? init_slave_0dB : init_slave_unmute, &step);
2361 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2364 * mute-LED control using vmaster
2366 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2367 struct snd_ctl_elem_info *uinfo)
2369 static const char * const texts[] = {
2370 "On", "Off", "Follow Master"
2373 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2376 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2379 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2380 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2384 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2387 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2388 unsigned int old_mode = hook->mute_mode;
2390 hook->mute_mode = ucontrol->value.enumerated.item[0];
2391 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2392 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2393 if (old_mode == hook->mute_mode)
2395 snd_hda_sync_vmaster_hook(hook);
2399 static struct snd_kcontrol_new vmaster_mute_mode = {
2400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2401 .name = "Mute-LED Mode",
2402 .info = vmaster_mute_mode_info,
2403 .get = vmaster_mute_mode_get,
2404 .put = vmaster_mute_mode_put,
2408 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2409 * @codec: the HDA codec
2410 * @hook: the vmaster hook object
2411 * @expose_enum_ctl: flag to create an enum ctl
2413 * Add a mute-LED hook with the given vmaster switch kctl.
2414 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2415 * created and associated with the given hook.
2417 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2418 struct hda_vmaster_mute_hook *hook,
2419 bool expose_enum_ctl)
2421 struct snd_kcontrol *kctl;
2423 if (!hook->hook || !hook->sw_kctl)
2425 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2426 hook->codec = codec;
2427 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2428 if (!expose_enum_ctl)
2430 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2433 return snd_hda_ctl_add(codec, 0, kctl);
2435 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2438 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2439 * @hook: the vmaster hook
2441 * Call the hook with the current value for synchronization.
2442 * Should be called in init callback.
2444 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2446 if (!hook->hook || !hook->codec)
2448 /* don't call vmaster hook in the destructor since it might have
2449 * been already destroyed
2451 if (hook->codec->bus->shutdown)
2453 switch (hook->mute_mode) {
2454 case HDA_VMUTE_FOLLOW_MASTER:
2455 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2458 hook->hook(hook->codec, hook->mute_mode);
2462 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2466 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2467 * @kcontrol: referred ctl element
2468 * @uinfo: pointer to get/store the data
2470 * The control element is supposed to have the private_value field
2471 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2473 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_info *uinfo)
2476 int chs = get_amp_channels(kcontrol);
2478 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2479 uinfo->count = chs == 3 ? 2 : 1;
2480 uinfo->value.integer.min = 0;
2481 uinfo->value.integer.max = 1;
2484 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2487 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2488 * @kcontrol: ctl element
2489 * @ucontrol: pointer to get/store the data
2491 * The control element is supposed to have the private_value field
2492 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2494 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2498 hda_nid_t nid = get_amp_nid(kcontrol);
2499 int chs = get_amp_channels(kcontrol);
2500 int dir = get_amp_direction(kcontrol);
2501 int idx = get_amp_index(kcontrol);
2502 long *valp = ucontrol->value.integer.value;
2505 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2506 HDA_AMP_MUTE) ? 0 : 1;
2508 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2509 HDA_AMP_MUTE) ? 0 : 1;
2512 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2515 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2516 * @kcontrol: ctl element
2517 * @ucontrol: pointer to get/store the data
2519 * The control element is supposed to have the private_value field
2520 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2522 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2525 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2526 hda_nid_t nid = get_amp_nid(kcontrol);
2527 int chs = get_amp_channels(kcontrol);
2528 int dir = get_amp_direction(kcontrol);
2529 int idx = get_amp_index(kcontrol);
2530 long *valp = ucontrol->value.integer.value;
2534 change = codec_amp_update(codec, nid, 0, dir, idx,
2536 *valp ? 0 : HDA_AMP_MUTE, false,
2537 !hda_codec_is_power_on(codec));
2541 change |= codec_amp_update(codec, nid, 1, dir, idx,
2543 *valp ? 0 : HDA_AMP_MUTE, false,
2544 !hda_codec_is_power_on(codec));
2545 hda_call_check_power_status(codec, nid);
2548 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2551 * bound volume controls
2553 * bind multiple volumes (# indices, from 0)
2556 #define AMP_VAL_IDX_SHIFT 19
2557 #define AMP_VAL_IDX_MASK (0x0f<<19)
2560 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2561 * @kcontrol: ctl element
2562 * @ucontrol: pointer to get/store the data
2564 * The control element is supposed to have the private_value field
2565 * set up via HDA_BIND_MUTE*() macros.
2567 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2574 mutex_lock(&codec->control_mutex);
2575 pval = kcontrol->private_value;
2576 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2577 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2578 kcontrol->private_value = pval;
2579 mutex_unlock(&codec->control_mutex);
2582 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2585 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2586 * @kcontrol: ctl element
2587 * @ucontrol: pointer to get/store the data
2589 * The control element is supposed to have the private_value field
2590 * set up via HDA_BIND_MUTE*() macros.
2592 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
2595 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 int i, indices, err = 0, change = 0;
2599 mutex_lock(&codec->control_mutex);
2600 pval = kcontrol->private_value;
2601 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2602 for (i = 0; i < indices; i++) {
2603 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2604 (i << AMP_VAL_IDX_SHIFT);
2605 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2610 kcontrol->private_value = pval;
2611 mutex_unlock(&codec->control_mutex);
2612 return err < 0 ? err : change;
2614 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2617 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2618 * @kcontrol: referred ctl element
2619 * @uinfo: pointer to get/store the data
2621 * The control element is supposed to have the private_value field
2622 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2624 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_info *uinfo)
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2628 struct hda_bind_ctls *c;
2631 mutex_lock(&codec->control_mutex);
2632 c = (struct hda_bind_ctls *)kcontrol->private_value;
2633 kcontrol->private_value = *c->values;
2634 err = c->ops->info(kcontrol, uinfo);
2635 kcontrol->private_value = (long)c;
2636 mutex_unlock(&codec->control_mutex);
2639 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2642 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2643 * @kcontrol: ctl element
2644 * @ucontrol: pointer to get/store the data
2646 * The control element is supposed to have the private_value field
2647 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2649 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2653 struct hda_bind_ctls *c;
2656 mutex_lock(&codec->control_mutex);
2657 c = (struct hda_bind_ctls *)kcontrol->private_value;
2658 kcontrol->private_value = *c->values;
2659 err = c->ops->get(kcontrol, ucontrol);
2660 kcontrol->private_value = (long)c;
2661 mutex_unlock(&codec->control_mutex);
2664 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2667 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2668 * @kcontrol: ctl element
2669 * @ucontrol: pointer to get/store the data
2671 * The control element is supposed to have the private_value field
2672 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2674 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_value *ucontrol)
2677 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2678 struct hda_bind_ctls *c;
2679 unsigned long *vals;
2680 int err = 0, change = 0;
2682 mutex_lock(&codec->control_mutex);
2683 c = (struct hda_bind_ctls *)kcontrol->private_value;
2684 for (vals = c->values; *vals; vals++) {
2685 kcontrol->private_value = *vals;
2686 err = c->ops->put(kcontrol, ucontrol);
2691 kcontrol->private_value = (long)c;
2692 mutex_unlock(&codec->control_mutex);
2693 return err < 0 ? err : change;
2695 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2698 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2699 * @kcontrol: ctl element
2700 * @op_flag: operation flag
2701 * @size: byte size of input TLV
2704 * The control element is supposed to have the private_value field
2705 * set up via HDA_BIND_VOL() macro.
2707 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2708 unsigned int size, unsigned int __user *tlv)
2710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2711 struct hda_bind_ctls *c;
2714 mutex_lock(&codec->control_mutex);
2715 c = (struct hda_bind_ctls *)kcontrol->private_value;
2716 kcontrol->private_value = *c->values;
2717 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2718 kcontrol->private_value = (long)c;
2719 mutex_unlock(&codec->control_mutex);
2722 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2724 struct hda_ctl_ops snd_hda_bind_vol = {
2725 .info = snd_hda_mixer_amp_volume_info,
2726 .get = snd_hda_mixer_amp_volume_get,
2727 .put = snd_hda_mixer_amp_volume_put,
2728 .tlv = snd_hda_mixer_amp_tlv
2730 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2732 struct hda_ctl_ops snd_hda_bind_sw = {
2733 .info = snd_hda_mixer_amp_switch_info,
2734 .get = snd_hda_mixer_amp_switch_get,
2735 .put = snd_hda_mixer_amp_switch_put,
2736 .tlv = snd_hda_mixer_amp_tlv
2738 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2741 * SPDIF out controls
2744 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_info *uinfo)
2747 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2752 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2755 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2756 IEC958_AES0_NONAUDIO |
2757 IEC958_AES0_CON_EMPHASIS_5015 |
2758 IEC958_AES0_CON_NOT_COPYRIGHT;
2759 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2760 IEC958_AES1_CON_ORIGINAL;
2764 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2767 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2768 IEC958_AES0_NONAUDIO |
2769 IEC958_AES0_PRO_EMPHASIS_5015;
2773 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 int idx = kcontrol->private_value;
2778 struct hda_spdif_out *spdif;
2780 mutex_lock(&codec->spdif_mutex);
2781 spdif = snd_array_elem(&codec->spdif_out, idx);
2782 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2783 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2784 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2785 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2786 mutex_unlock(&codec->spdif_mutex);
2791 /* convert from SPDIF status bits to HDA SPDIF bits
2792 * bit 0 (DigEn) is always set zero (to be filled later)
2794 static unsigned short convert_from_spdif_status(unsigned int sbits)
2796 unsigned short val = 0;
2798 if (sbits & IEC958_AES0_PROFESSIONAL)
2799 val |= AC_DIG1_PROFESSIONAL;
2800 if (sbits & IEC958_AES0_NONAUDIO)
2801 val |= AC_DIG1_NONAUDIO;
2802 if (sbits & IEC958_AES0_PROFESSIONAL) {
2803 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2804 IEC958_AES0_PRO_EMPHASIS_5015)
2805 val |= AC_DIG1_EMPHASIS;
2807 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2808 IEC958_AES0_CON_EMPHASIS_5015)
2809 val |= AC_DIG1_EMPHASIS;
2810 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2811 val |= AC_DIG1_COPYRIGHT;
2812 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2813 val |= AC_DIG1_LEVEL;
2814 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2819 /* convert to SPDIF status bits from HDA SPDIF bits
2821 static unsigned int convert_to_spdif_status(unsigned short val)
2823 unsigned int sbits = 0;
2825 if (val & AC_DIG1_NONAUDIO)
2826 sbits |= IEC958_AES0_NONAUDIO;
2827 if (val & AC_DIG1_PROFESSIONAL)
2828 sbits |= IEC958_AES0_PROFESSIONAL;
2829 if (sbits & IEC958_AES0_PROFESSIONAL) {
2830 if (val & AC_DIG1_EMPHASIS)
2831 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2833 if (val & AC_DIG1_EMPHASIS)
2834 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2835 if (!(val & AC_DIG1_COPYRIGHT))
2836 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2837 if (val & AC_DIG1_LEVEL)
2838 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2839 sbits |= val & (0x7f << 8);
2844 /* set digital convert verbs both for the given NID and its slaves */
2845 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2850 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2851 d = codec->slave_dig_outs;
2855 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2858 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2862 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2864 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2867 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2871 int idx = kcontrol->private_value;
2872 struct hda_spdif_out *spdif;
2877 mutex_lock(&codec->spdif_mutex);
2878 spdif = snd_array_elem(&codec->spdif_out, idx);
2880 spdif->status = ucontrol->value.iec958.status[0] |
2881 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2882 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2883 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2884 val = convert_from_spdif_status(spdif->status);
2885 val |= spdif->ctls & 1;
2886 change = spdif->ctls != val;
2888 if (change && nid != (u16)-1)
2889 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2890 mutex_unlock(&codec->spdif_mutex);
2894 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2896 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
2899 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2900 int idx = kcontrol->private_value;
2901 struct hda_spdif_out *spdif;
2903 mutex_lock(&codec->spdif_mutex);
2904 spdif = snd_array_elem(&codec->spdif_out, idx);
2905 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2906 mutex_unlock(&codec->spdif_mutex);
2910 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2913 set_dig_out_convert(codec, nid, dig1, dig2);
2914 /* unmute amp switch (if any) */
2915 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2916 (dig1 & AC_DIG1_ENABLE))
2917 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2921 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_value *ucontrol)
2924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2925 int idx = kcontrol->private_value;
2926 struct hda_spdif_out *spdif;
2931 mutex_lock(&codec->spdif_mutex);
2932 spdif = snd_array_elem(&codec->spdif_out, idx);
2934 val = spdif->ctls & ~AC_DIG1_ENABLE;
2935 if (ucontrol->value.integer.value[0])
2936 val |= AC_DIG1_ENABLE;
2937 change = spdif->ctls != val;
2939 if (change && nid != (u16)-1)
2940 set_spdif_ctls(codec, nid, val & 0xff, -1);
2941 mutex_unlock(&codec->spdif_mutex);
2945 static struct snd_kcontrol_new dig_mixes[] = {
2947 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2948 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2949 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2950 .info = snd_hda_spdif_mask_info,
2951 .get = snd_hda_spdif_cmask_get,
2954 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2955 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2956 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2957 .info = snd_hda_spdif_mask_info,
2958 .get = snd_hda_spdif_pmask_get,
2961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2962 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2963 .info = snd_hda_spdif_mask_info,
2964 .get = snd_hda_spdif_default_get,
2965 .put = snd_hda_spdif_default_put,
2968 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2969 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2970 .info = snd_hda_spdif_out_switch_info,
2971 .get = snd_hda_spdif_out_switch_get,
2972 .put = snd_hda_spdif_out_switch_put,
2978 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2979 * @codec: the HDA codec
2980 * @associated_nid: NID that new ctls associated with
2981 * @cvt_nid: converter NID
2982 * @type: HDA_PCM_TYPE_*
2983 * Creates controls related with the digital output.
2984 * Called from each patch supporting the digital out.
2986 * Returns 0 if successful, or a negative error code.
2988 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2989 hda_nid_t associated_nid,
2994 struct snd_kcontrol *kctl;
2995 struct snd_kcontrol_new *dig_mix;
2997 const int spdif_index = 16;
2998 struct hda_spdif_out *spdif;
2999 struct hda_bus *bus = codec->bus;
3001 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3002 type == HDA_PCM_TYPE_SPDIF) {
3004 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3005 type == HDA_PCM_TYPE_HDMI) {
3006 /* suppose a single SPDIF device */
3007 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3008 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3011 kctl->id.index = spdif_index;
3013 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3015 if (!bus->primary_dig_out_type)
3016 bus->primary_dig_out_type = type;
3018 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3020 codec_err(codec, "too many IEC958 outputs\n");
3023 spdif = snd_array_new(&codec->spdif_out);
3026 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3027 kctl = snd_ctl_new1(dig_mix, codec);
3030 kctl->id.index = idx;
3031 kctl->private_value = codec->spdif_out.used - 1;
3032 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3036 spdif->nid = cvt_nid;
3037 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3038 AC_VERB_GET_DIGI_CONVERT_1, 0);
3039 spdif->status = convert_to_spdif_status(spdif->ctls);
3042 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3045 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3046 * @codec: the HDA codec
3049 * call within spdif_mutex lock
3051 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3055 for (i = 0; i < codec->spdif_out.used; i++) {
3056 struct hda_spdif_out *spdif =
3057 snd_array_elem(&codec->spdif_out, i);
3058 if (spdif->nid == nid)
3063 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3066 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3067 * @codec: the HDA codec
3068 * @idx: the SPDIF ctl index
3070 * Unassign the widget from the given SPDIF control.
3072 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3074 struct hda_spdif_out *spdif;
3076 mutex_lock(&codec->spdif_mutex);
3077 spdif = snd_array_elem(&codec->spdif_out, idx);
3078 spdif->nid = (u16)-1;
3079 mutex_unlock(&codec->spdif_mutex);
3081 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3084 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3085 * @codec: the HDA codec
3086 * @idx: the SPDIF ctl idx
3089 * Assign the widget to the SPDIF control with the given index.
3091 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3093 struct hda_spdif_out *spdif;
3096 mutex_lock(&codec->spdif_mutex);
3097 spdif = snd_array_elem(&codec->spdif_out, idx);
3098 if (spdif->nid != nid) {
3101 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3103 mutex_unlock(&codec->spdif_mutex);
3105 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3108 * SPDIF sharing with analog output
3110 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3113 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3114 ucontrol->value.integer.value[0] = mout->share_spdif;
3118 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3119 struct snd_ctl_elem_value *ucontrol)
3121 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3122 mout->share_spdif = !!ucontrol->value.integer.value[0];
3126 static struct snd_kcontrol_new spdif_share_sw = {
3127 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3128 .name = "IEC958 Default PCM Playback Switch",
3129 .info = snd_ctl_boolean_mono_info,
3130 .get = spdif_share_sw_get,
3131 .put = spdif_share_sw_put,
3135 * snd_hda_create_spdif_share_sw - create Default PCM switch
3136 * @codec: the HDA codec
3137 * @mout: multi-out instance
3139 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3140 struct hda_multi_out *mout)
3142 struct snd_kcontrol *kctl;
3144 if (!mout->dig_out_nid)
3147 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3150 /* ATTENTION: here mout is passed as private_data, instead of codec */
3151 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3153 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3159 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3161 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_value *ucontrol)
3164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3166 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3170 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
3173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3174 hda_nid_t nid = kcontrol->private_value;
3175 unsigned int val = !!ucontrol->value.integer.value[0];
3178 mutex_lock(&codec->spdif_mutex);
3179 change = codec->spdif_in_enable != val;
3181 codec->spdif_in_enable = val;
3182 snd_hda_codec_write_cache(codec, nid, 0,
3183 AC_VERB_SET_DIGI_CONVERT_1, val);
3185 mutex_unlock(&codec->spdif_mutex);
3189 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3190 struct snd_ctl_elem_value *ucontrol)
3192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3193 hda_nid_t nid = kcontrol->private_value;
3197 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3198 sbits = convert_to_spdif_status(val);
3199 ucontrol->value.iec958.status[0] = sbits;
3200 ucontrol->value.iec958.status[1] = sbits >> 8;
3201 ucontrol->value.iec958.status[2] = sbits >> 16;
3202 ucontrol->value.iec958.status[3] = sbits >> 24;
3206 static struct snd_kcontrol_new dig_in_ctls[] = {
3208 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3209 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3210 .info = snd_hda_spdif_in_switch_info,
3211 .get = snd_hda_spdif_in_switch_get,
3212 .put = snd_hda_spdif_in_switch_put,
3215 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3217 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3218 .info = snd_hda_spdif_mask_info,
3219 .get = snd_hda_spdif_in_status_get,
3225 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3226 * @codec: the HDA codec
3227 * @nid: audio in widget NID
3229 * Creates controls related with the SPDIF input.
3230 * Called from each patch supporting the SPDIF in.
3232 * Returns 0 if successful, or a negative error code.
3234 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3237 struct snd_kcontrol *kctl;
3238 struct snd_kcontrol_new *dig_mix;
3241 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3243 codec_err(codec, "too many IEC958 inputs\n");
3246 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3247 kctl = snd_ctl_new1(dig_mix, codec);
3250 kctl->private_value = nid;
3251 err = snd_hda_ctl_add(codec, nid, kctl);
3255 codec->spdif_in_enable =
3256 snd_hda_codec_read(codec, nid, 0,
3257 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3261 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3267 /* build a 31bit cache key with the widget id and the command parameter */
3268 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3269 #define get_cmd_cache_nid(key) ((key) & 0xff)
3270 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3273 * snd_hda_codec_write_cache - send a single command with caching
3274 * @codec: the HDA codec
3275 * @nid: NID to send the command
3276 * @flags: optional bit flags
3277 * @verb: the verb to send
3278 * @parm: the parameter for the verb
3280 * Send a single command without waiting for response.
3282 * Returns 0 if successful, or a negative error code.
3284 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3285 int flags, unsigned int verb, unsigned int parm)
3288 struct hda_cache_head *c;
3290 unsigned int cache_only;
3292 cache_only = codec->cached_write;
3294 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3299 /* parm may contain the verb stuff for get/set amp */
3300 verb = verb | (parm >> 8);
3302 key = build_cmd_cache_key(nid, verb);
3303 mutex_lock(&codec->bus->core.cmd_mutex);
3304 c = get_alloc_hash(&codec->cmd_cache, key);
3307 c->dirty = cache_only;
3309 mutex_unlock(&codec->bus->core.cmd_mutex);
3312 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3315 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3316 * @codec: the HDA codec
3317 * @nid: NID to send the command
3318 * @flags: optional bit flags
3319 * @verb: the verb to send
3320 * @parm: the parameter for the verb
3322 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3323 * command if the parameter is already identical with the cached value.
3324 * If not, it sends the command and refreshes the cache.
3326 * Returns 0 if successful, or a negative error code.
3328 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3329 int flags, unsigned int verb, unsigned int parm)
3331 struct hda_cache_head *c;
3334 /* parm may contain the verb stuff for get/set amp */
3335 verb = verb | (parm >> 8);
3337 key = build_cmd_cache_key(nid, verb);
3338 mutex_lock(&codec->bus->core.cmd_mutex);
3339 c = get_hash(&codec->cmd_cache, key);
3340 if (c && c->val == parm) {
3341 mutex_unlock(&codec->bus->core.cmd_mutex);
3344 mutex_unlock(&codec->bus->core.cmd_mutex);
3345 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3347 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3350 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3351 * @codec: HD-audio codec
3353 * Execute all verbs recorded in the command caches to resume.
3355 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3359 mutex_lock(&codec->hash_mutex);
3360 codec->cached_write = 0;
3361 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3362 struct hda_cache_head *buffer;
3365 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3372 mutex_unlock(&codec->hash_mutex);
3373 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3374 get_cmd_cache_cmd(key), buffer->val);
3375 mutex_lock(&codec->hash_mutex);
3377 mutex_unlock(&codec->hash_mutex);
3379 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3382 * snd_hda_sequence_write_cache - sequence writes with caching
3383 * @codec: the HDA codec
3384 * @seq: VERB array to send
3386 * Send the commands sequentially from the given array.
3387 * Thte commands are recorded on cache for power-save and resume.
3388 * The array must be terminated with NID=0.
3390 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3391 const struct hda_verb *seq)
3393 for (; seq->nid; seq++)
3394 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3397 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3400 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3401 * @codec: HD-audio codec
3403 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3405 snd_hda_codec_resume_amp(codec);
3406 snd_hda_codec_resume_cache(codec);
3408 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3411 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3412 * @codec: the HDA codec
3413 * @fg: function group (not used now)
3414 * @power_state: the power state to set (AC_PWRST_*)
3416 * Set the given power state to all widgets that have the power control.
3417 * If the codec has power_filter set, it evaluates the power state and
3418 * filter out if it's unchanged as D3.
3420 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3421 unsigned int power_state)
3425 for_each_hda_codec_node(nid, codec) {
3426 unsigned int wcaps = get_wcaps(codec, nid);
3427 unsigned int state = power_state;
3428 if (!(wcaps & AC_WCAP_POWER))
3430 if (codec->power_filter) {
3431 state = codec->power_filter(codec, nid, power_state);
3432 if (state != power_state && power_state == AC_PWRST_D3)
3435 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3439 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3442 * wait until the state is reached, returns the current state
3444 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3446 unsigned int power_state)
3448 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3449 unsigned int state, actual_state;
3452 state = snd_hda_codec_read(codec, fg, 0,
3453 AC_VERB_GET_POWER_STATE, 0);
3454 if (state & AC_PWRST_ERROR)
3456 actual_state = (state >> 4) & 0x0f;
3457 if (actual_state == power_state)
3459 if (time_after_eq(jiffies, end_time))
3461 /* wait until the codec reachs to the target state */
3468 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3469 * @codec: the HDA codec
3471 * @power_state: power state to evalue
3473 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3474 * This can be used a codec power_filter callback.
3476 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3478 unsigned int power_state)
3480 if (nid == codec->core.afg || nid == codec->core.mfg)
3482 if (power_state == AC_PWRST_D3 &&
3483 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3484 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3485 int eapd = snd_hda_codec_read(codec, nid, 0,
3486 AC_VERB_GET_EAPD_BTLENABLE, 0);
3492 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
3495 * set power state of the codec, and return the power state
3497 static unsigned int hda_set_power_state(struct hda_codec *codec,
3498 unsigned int power_state)
3500 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
3505 /* this delay seems necessary to avoid click noise at power-down */
3506 if (power_state == AC_PWRST_D3) {
3507 if (codec->depop_delay < 0)
3508 msleep(codec_has_epss(codec) ? 10 : 100);
3509 else if (codec->depop_delay > 0)
3510 msleep(codec->depop_delay);
3511 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3514 /* repeat power states setting at most 10 times*/
3515 for (count = 0; count < 10; count++) {
3516 if (codec->patch_ops.set_power_state)
3517 codec->patch_ops.set_power_state(codec, fg,
3520 state = power_state;
3521 if (codec->power_filter)
3522 state = codec->power_filter(codec, fg, state);
3523 if (state == power_state || power_state != AC_PWRST_D3)
3524 snd_hda_codec_read(codec, fg, flags,
3525 AC_VERB_SET_POWER_STATE,
3527 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3529 state = hda_sync_power_state(codec, fg, power_state);
3530 if (!(state & AC_PWRST_ERROR))
3537 /* sync power states of all widgets;
3538 * this is called at the end of codec parsing
3540 static void sync_power_up_states(struct hda_codec *codec)
3544 /* don't care if no filter is used */
3545 if (!codec->power_filter)
3548 for_each_hda_codec_node(nid, codec) {
3549 unsigned int wcaps = get_wcaps(codec, nid);
3550 unsigned int target;
3551 if (!(wcaps & AC_WCAP_POWER))
3553 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3554 if (target == AC_PWRST_D0)
3556 if (!snd_hda_check_power_state(codec, nid, target))
3557 snd_hda_codec_write(codec, nid, 0,
3558 AC_VERB_SET_POWER_STATE, target);
3562 #ifdef CONFIG_SND_HDA_RECONFIG
3563 /* execute additional init verbs */
3564 static void hda_exec_init_verbs(struct hda_codec *codec)
3566 if (codec->init_verbs.list)
3567 snd_hda_sequence_write(codec, codec->init_verbs.list);
3570 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3574 /* update the power on/off account with the current jiffies */
3575 static void update_power_acct(struct hda_codec *codec, bool on)
3577 unsigned long delta = jiffies - codec->power_jiffies;
3580 codec->power_on_acct += delta;
3582 codec->power_off_acct += delta;
3583 codec->power_jiffies += delta;
3586 void snd_hda_update_power_acct(struct hda_codec *codec)
3588 update_power_acct(codec, hda_codec_is_power_on(codec));
3592 * call suspend and power-down; used both from PM and power-save
3593 * this function returns the power state in the end
3595 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3599 atomic_inc(&codec->core.in_pm);
3601 if (codec->patch_ops.suspend)
3602 codec->patch_ops.suspend(codec);
3603 hda_cleanup_all_streams(codec);
3604 state = hda_set_power_state(codec, AC_PWRST_D3);
3605 update_power_acct(codec, true);
3606 atomic_dec(&codec->core.in_pm);
3610 /* mark all entries of cmd and amp caches dirty */
3611 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3614 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3615 struct hda_cache_head *cmd;
3616 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3619 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3620 struct hda_amp_info *amp;
3621 amp = snd_array_elem(&codec->amp_cache.buf, i);
3622 amp->head.dirty = 1;
3627 * kick up codec; used both from PM and power-save
3629 static void hda_call_codec_resume(struct hda_codec *codec)
3631 atomic_inc(&codec->core.in_pm);
3633 hda_mark_cmd_cache_dirty(codec);
3635 codec->power_jiffies = jiffies;
3637 hda_set_power_state(codec, AC_PWRST_D0);
3638 restore_shutup_pins(codec);
3639 hda_exec_init_verbs(codec);
3640 snd_hda_jack_set_dirty_all(codec);
3641 if (codec->patch_ops.resume)
3642 codec->patch_ops.resume(codec);
3644 if (codec->patch_ops.init)
3645 codec->patch_ops.init(codec);
3646 snd_hda_codec_resume_amp(codec);
3647 snd_hda_codec_resume_cache(codec);
3650 if (codec->jackpoll_interval)
3651 hda_jackpoll_work(&codec->jackpoll_work.work);
3653 snd_hda_jack_report_sync(codec);
3654 atomic_dec(&codec->core.in_pm);
3657 static int hda_codec_runtime_suspend(struct device *dev)
3659 struct hda_codec *codec = dev_to_hda_codec(dev);
3660 struct hda_pcm *pcm;
3663 cancel_delayed_work_sync(&codec->jackpoll_work);
3664 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3665 snd_pcm_suspend_all(pcm->pcm);
3666 state = hda_call_codec_suspend(codec);
3667 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3668 (state & AC_PWRST_CLK_STOP_OK))
3669 snd_hdac_codec_link_down(&codec->core);
3673 static int hda_codec_runtime_resume(struct device *dev)
3675 struct hda_codec *codec = dev_to_hda_codec(dev);
3677 snd_hdac_codec_link_up(&codec->core);
3678 hda_call_codec_resume(codec);
3679 pm_runtime_mark_last_busy(dev);
3682 #endif /* CONFIG_PM */
3684 /* referred in hda_bind.c */
3685 const struct dev_pm_ops hda_codec_driver_pm = {
3686 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3687 pm_runtime_force_resume)
3688 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3693 * add standard channel maps if not specified
3695 static int add_std_chmaps(struct hda_codec *codec)
3697 struct hda_pcm *pcm;
3700 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3701 for (str = 0; str < 2; str++) {
3702 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3703 struct snd_pcm_chmap *chmap;
3704 const struct snd_pcm_chmap_elem *elem;
3708 if (!pcm || !hinfo->substreams)
3710 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3711 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3712 hinfo->channels_max,
3716 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3722 /* default channel maps for 2.1 speakers;
3723 * since HD-audio supports only stereo, odd number channels are omitted
3725 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3727 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3729 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3730 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3733 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3735 int snd_hda_codec_build_controls(struct hda_codec *codec)
3738 hda_exec_init_verbs(codec);
3739 /* continue to initialize... */
3740 if (codec->patch_ops.init)
3741 err = codec->patch_ops.init(codec);
3742 if (!err && codec->patch_ops.build_controls)
3743 err = codec->patch_ops.build_controls(codec);
3747 /* we create chmaps here instead of build_pcms */
3748 err = add_std_chmaps(codec);
3752 if (codec->jackpoll_interval)
3753 hda_jackpoll_work(&codec->jackpoll_work.work);
3755 snd_hda_jack_report_sync(codec); /* call at the last init point */
3756 sync_power_up_states(codec);
3763 struct hda_rate_tbl {
3765 unsigned int alsa_bits;
3766 unsigned int hda_fmt;
3769 /* rate = base * mult / div */
3770 #define HDA_RATE(base, mult, div) \
3771 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3772 (((div) - 1) << AC_FMT_DIV_SHIFT))
3774 static struct hda_rate_tbl rate_bits[] = {
3775 /* rate in Hz, ALSA rate bitmask, HDA format value */
3777 /* autodetected value used in snd_hda_query_supported_pcm */
3778 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3779 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3780 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3781 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3782 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3783 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3784 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3785 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3786 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3787 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3788 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3789 #define AC_PAR_PCM_RATE_BITS 11
3790 /* up to bits 10, 384kHZ isn't supported properly */
3792 /* not autodetected value */
3793 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3795 { 0 } /* terminator */
3799 * snd_hda_calc_stream_format - calculate format bitset
3800 * @codec: HD-audio codec
3801 * @rate: the sample rate
3802 * @channels: the number of channels
3803 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3804 * @maxbps: the max. bps
3805 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
3807 * Calculate the format bitset from the given rate, channels and th PCM format.
3809 * Return zero if invalid.
3811 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3813 unsigned int channels,
3814 unsigned int format,
3815 unsigned int maxbps,
3816 unsigned short spdif_ctls)
3819 unsigned int val = 0;
3821 for (i = 0; rate_bits[i].hz; i++)
3822 if (rate_bits[i].hz == rate) {
3823 val = rate_bits[i].hda_fmt;
3826 if (!rate_bits[i].hz) {
3827 codec_dbg(codec, "invalid rate %d\n", rate);
3831 if (channels == 0 || channels > 8) {
3832 codec_dbg(codec, "invalid channels %d\n", channels);
3835 val |= channels - 1;
3837 switch (snd_pcm_format_width(format)) {
3839 val |= AC_FMT_BITS_8;
3842 val |= AC_FMT_BITS_16;
3847 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3848 val |= AC_FMT_BITS_32;
3849 else if (maxbps >= 24)
3850 val |= AC_FMT_BITS_24;
3852 val |= AC_FMT_BITS_20;
3855 codec_dbg(codec, "invalid format width %d\n",
3856 snd_pcm_format_width(format));
3860 if (spdif_ctls & AC_DIG1_NONAUDIO)
3861 val |= AC_FMT_TYPE_NON_PCM;
3865 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
3867 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3870 unsigned int val = 0;
3871 if (nid != codec->core.afg &&
3872 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3873 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3874 if (!val || val == -1)
3875 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
3876 if (!val || val == -1)
3881 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3883 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
3887 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3890 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3891 if (!streams || streams == -1)
3892 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
3893 if (!streams || streams == -1)
3898 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3900 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
3905 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3906 * @codec: the HDA codec
3907 * @nid: NID to query
3908 * @ratesp: the pointer to store the detected rate bitflags
3909 * @formatsp: the pointer to store the detected formats
3910 * @bpsp: the pointer to store the detected format widths
3912 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3913 * or @bsps argument is ignored.
3915 * Returns 0 if successful, otherwise a negative error code.
3917 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3918 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3920 unsigned int i, val, wcaps;
3922 wcaps = get_wcaps(codec, nid);
3923 val = query_pcm_param(codec, nid);
3927 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3929 rates |= rate_bits[i].alsa_bits;
3933 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3935 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3941 if (formatsp || bpsp) {
3943 unsigned int streams, bps;
3945 streams = query_stream_param(codec, nid);
3950 if (streams & AC_SUPFMT_PCM) {
3951 if (val & AC_SUPPCM_BITS_8) {
3952 formats |= SNDRV_PCM_FMTBIT_U8;
3955 if (val & AC_SUPPCM_BITS_16) {
3956 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3959 if (wcaps & AC_WCAP_DIGITAL) {
3960 if (val & AC_SUPPCM_BITS_32)
3961 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3962 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3963 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3964 if (val & AC_SUPPCM_BITS_24)
3966 else if (val & AC_SUPPCM_BITS_20)
3968 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3969 AC_SUPPCM_BITS_32)) {
3970 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3971 if (val & AC_SUPPCM_BITS_32)
3973 else if (val & AC_SUPPCM_BITS_24)
3975 else if (val & AC_SUPPCM_BITS_20)
3979 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
3980 if (streams & AC_SUPFMT_FLOAT32) {
3981 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3986 if (streams == AC_SUPFMT_AC3) {
3987 /* should be exclusive */
3988 /* temporary hack: we have still no proper support
3989 * for the direct AC3 stream...
3991 formats |= SNDRV_PCM_FMTBIT_U8;
3996 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3998 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4003 *formatsp = formats;
4010 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4013 * snd_hda_is_supported_format - Check the validity of the format
4014 * @codec: HD-audio codec
4015 * @nid: NID to check
4016 * @format: the HD-audio format value to check
4018 * Check whether the given node supports the format value.
4020 * Returns 1 if supported, 0 if not.
4022 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4023 unsigned int format)
4026 unsigned int val = 0, rate, stream;
4028 val = query_pcm_param(codec, nid);
4032 rate = format & 0xff00;
4033 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4034 if (rate_bits[i].hda_fmt == rate) {
4039 if (i >= AC_PAR_PCM_RATE_BITS)
4042 stream = query_stream_param(codec, nid);
4046 if (stream & AC_SUPFMT_PCM) {
4047 switch (format & 0xf0) {
4049 if (!(val & AC_SUPPCM_BITS_8))
4053 if (!(val & AC_SUPPCM_BITS_16))
4057 if (!(val & AC_SUPPCM_BITS_20))
4061 if (!(val & AC_SUPPCM_BITS_24))
4065 if (!(val & AC_SUPPCM_BITS_32))
4072 /* FIXME: check for float32 and AC3? */
4077 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4082 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4083 struct hda_codec *codec,
4084 struct snd_pcm_substream *substream)
4089 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4090 struct hda_codec *codec,
4091 unsigned int stream_tag,
4092 unsigned int format,
4093 struct snd_pcm_substream *substream)
4095 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4099 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4100 struct hda_codec *codec,
4101 struct snd_pcm_substream *substream)
4103 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4107 static int set_pcm_default_values(struct hda_codec *codec,
4108 struct hda_pcm_stream *info)
4112 /* query support PCM information from the given NID */
4113 if (info->nid && (!info->rates || !info->formats)) {
4114 err = snd_hda_query_supported_pcm(codec, info->nid,
4115 info->rates ? NULL : &info->rates,
4116 info->formats ? NULL : &info->formats,
4117 info->maxbps ? NULL : &info->maxbps);
4121 if (info->ops.open == NULL)
4122 info->ops.open = hda_pcm_default_open_close;
4123 if (info->ops.close == NULL)
4124 info->ops.close = hda_pcm_default_open_close;
4125 if (info->ops.prepare == NULL) {
4126 if (snd_BUG_ON(!info->nid))
4128 info->ops.prepare = hda_pcm_default_prepare;
4130 if (info->ops.cleanup == NULL) {
4131 if (snd_BUG_ON(!info->nid))
4133 info->ops.cleanup = hda_pcm_default_cleanup;
4139 * codec prepare/cleanup entries
4142 * snd_hda_codec_prepare - Prepare a stream
4143 * @codec: the HDA codec
4144 * @hinfo: PCM information
4145 * @stream: stream tag to assign
4146 * @format: format id to assign
4147 * @substream: PCM substream to assign
4149 * Calls the prepare callback set by the codec with the given arguments.
4150 * Clean up the inactive streams when successful.
4152 int snd_hda_codec_prepare(struct hda_codec *codec,
4153 struct hda_pcm_stream *hinfo,
4154 unsigned int stream,
4155 unsigned int format,
4156 struct snd_pcm_substream *substream)
4159 mutex_lock(&codec->bus->prepare_mutex);
4160 if (hinfo->ops.prepare)
4161 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4166 purify_inactive_streams(codec);
4167 mutex_unlock(&codec->bus->prepare_mutex);
4170 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4173 * snd_hda_codec_cleanup - Prepare a stream
4174 * @codec: the HDA codec
4175 * @hinfo: PCM information
4176 * @substream: PCM substream
4178 * Calls the cleanup callback set by the codec with the given arguments.
4180 void snd_hda_codec_cleanup(struct hda_codec *codec,
4181 struct hda_pcm_stream *hinfo,
4182 struct snd_pcm_substream *substream)
4184 mutex_lock(&codec->bus->prepare_mutex);
4185 if (hinfo->ops.cleanup)
4186 hinfo->ops.cleanup(hinfo, codec, substream);
4187 mutex_unlock(&codec->bus->prepare_mutex);
4189 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4192 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4193 "Audio", "SPDIF", "HDMI", "Modem"
4197 * get the empty PCM device number to assign
4199 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4201 /* audio device indices; not linear to keep compatibility */
4202 /* assigned to static slots up to dev#10; if more needed, assign
4203 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4205 static int audio_idx[HDA_PCM_NTYPES][5] = {
4206 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4207 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4208 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4209 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4213 if (type >= HDA_PCM_NTYPES) {
4214 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4218 for (i = 0; audio_idx[type][i] >= 0; i++) {
4219 #ifndef CONFIG_SND_DYNAMIC_MINORS
4220 if (audio_idx[type][i] >= 8)
4223 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4224 return audio_idx[type][i];
4227 #ifdef CONFIG_SND_DYNAMIC_MINORS
4228 /* non-fixed slots starting from 10 */
4229 for (i = 10; i < 32; i++) {
4230 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4235 dev_warn(bus->card->dev, "Too many %s devices\n",
4236 snd_hda_pcm_type_name[type]);
4237 #ifndef CONFIG_SND_DYNAMIC_MINORS
4238 dev_warn(bus->card->dev,
4239 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4244 /* call build_pcms ops of the given codec and set up the default parameters */
4245 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
4247 struct hda_pcm *cpcm;
4250 if (!list_empty(&codec->pcm_list_head))
4251 return 0; /* already parsed */
4253 if (!codec->patch_ops.build_pcms)
4256 err = codec->patch_ops.build_pcms(codec);
4258 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4259 codec->core.addr, err);
4263 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
4266 for (stream = 0; stream < 2; stream++) {
4267 struct hda_pcm_stream *info = &cpcm->stream[stream];
4269 if (!info->substreams)
4271 err = set_pcm_default_values(codec, info);
4274 "fail to setup default for PCM %s\n",
4284 /* assign all PCMs of the given codec */
4285 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4287 struct hda_bus *bus = codec->bus;
4288 struct hda_pcm *cpcm;
4291 if (snd_BUG_ON(!bus->ops.attach_pcm))
4294 err = snd_hda_codec_parse_pcms(codec);
4296 snd_hda_codec_reset(codec);
4300 /* attach a new PCM streams */
4301 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
4303 continue; /* already attached */
4304 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4305 continue; /* no substreams assigned */
4307 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4309 continue; /* no fatal error */
4311 err = bus->ops.attach_pcm(bus, codec, cpcm);
4314 "cannot attach PCM stream %d for codec #%d\n",
4315 dev, codec->core.addr);
4316 continue; /* no fatal error */
4324 * snd_hda_add_new_ctls - create controls from the array
4325 * @codec: the HDA codec
4326 * @knew: the array of struct snd_kcontrol_new
4328 * This helper function creates and add new controls in the given array.
4329 * The array must be terminated with an empty entry as terminator.
4331 * Returns 0 if successful, or a negative error code.
4333 int snd_hda_add_new_ctls(struct hda_codec *codec,
4334 const struct snd_kcontrol_new *knew)
4338 for (; knew->name; knew++) {
4339 struct snd_kcontrol *kctl;
4340 int addr = 0, idx = 0;
4341 if (knew->iface == -1) /* skip this codec private value */
4344 kctl = snd_ctl_new1(knew, codec);
4348 kctl->id.device = addr;
4350 kctl->id.index = idx;
4351 err = snd_hda_ctl_add(codec, 0, kctl);
4354 /* try first with another device index corresponding to
4355 * the codec addr; if it still fails (or it's the
4356 * primary codec), then try another control index
4358 if (!addr && codec->core.addr)
4359 addr = codec->core.addr;
4360 else if (!idx && !knew->index) {
4361 idx = find_empty_mixer_ctl_idx(codec,
4371 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4374 static void codec_set_power_save(struct hda_codec *codec, int delay)
4376 struct device *dev = hda_codec_dev(codec);
4379 pm_runtime_set_autosuspend_delay(dev, delay);
4380 pm_runtime_use_autosuspend(dev);
4381 pm_runtime_allow(dev);
4382 if (!pm_runtime_suspended(dev))
4383 pm_runtime_mark_last_busy(dev);
4385 pm_runtime_dont_use_autosuspend(dev);
4386 pm_runtime_forbid(dev);
4391 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4392 * @bus: HD-audio bus
4393 * @delay: autosuspend delay in msec, 0 = off
4395 * Synchronize the runtime PM autosuspend state from the power_save option.
4397 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4399 struct hda_codec *c;
4401 list_for_each_codec(c, bus)
4402 codec_set_power_save(c, delay);
4404 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
4407 * snd_hda_check_amp_list_power - Check the amp list and update the power
4408 * @codec: HD-audio codec
4409 * @check: the object containing an AMP list and the status
4410 * @nid: NID to check / update
4412 * Check whether the given NID is in the amp list. If it's in the list,
4413 * check the current AMP status, and update the the power-status according
4414 * to the mute status.
4416 * This function is supposed to be set or called from the check_power_status
4419 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4420 struct hda_loopback_check *check,
4423 const struct hda_amp_list *p;
4426 if (!check->amplist)
4428 for (p = check->amplist; p->nid; p++) {
4433 return 0; /* nothing changed */
4435 for (p = check->amplist; p->nid; p++) {
4436 for (ch = 0; ch < 2; ch++) {
4437 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4439 if (!(v & HDA_AMP_MUTE) && v > 0) {
4440 if (!check->power_on) {
4441 check->power_on = 1;
4442 snd_hda_power_up(codec);
4448 if (check->power_on) {
4449 check->power_on = 0;
4450 snd_hda_power_down(codec);
4454 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
4462 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4463 * @imux: imux helper object
4464 * @uinfo: pointer to get/store the data
4466 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4467 struct snd_ctl_elem_info *uinfo)
4471 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4473 uinfo->value.enumerated.items = imux->num_items;
4474 if (!imux->num_items)
4476 index = uinfo->value.enumerated.item;
4477 if (index >= imux->num_items)
4478 index = imux->num_items - 1;
4479 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4482 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
4485 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4486 * @codec: the HDA codec
4487 * @imux: imux helper object
4488 * @ucontrol: pointer to get/store the data
4489 * @nid: input mux NID
4490 * @cur_val: pointer to get/store the current imux value
4492 int snd_hda_input_mux_put(struct hda_codec *codec,
4493 const struct hda_input_mux *imux,
4494 struct snd_ctl_elem_value *ucontrol,
4496 unsigned int *cur_val)
4500 if (!imux->num_items)
4502 idx = ucontrol->value.enumerated.item[0];
4503 if (idx >= imux->num_items)
4504 idx = imux->num_items - 1;
4505 if (*cur_val == idx)
4507 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4508 imux->items[idx].index);
4512 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
4516 * snd_hda_enum_helper_info - Helper for simple enum ctls
4517 * @kcontrol: ctl element
4518 * @uinfo: pointer to get/store the data
4519 * @num_items: number of enum items
4520 * @texts: enum item string array
4522 * process kcontrol info callback of a simple string enum array
4523 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4525 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4526 struct snd_ctl_elem_info *uinfo,
4527 int num_items, const char * const *texts)
4529 static const char * const texts_default[] = {
4530 "Disabled", "Enabled"
4533 if (!texts || !num_items) {
4535 texts = texts_default;
4538 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
4540 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
4543 * Multi-channel / digital-out PCM helper functions
4546 /* setup SPDIF output stream */
4547 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4548 unsigned int stream_tag, unsigned int format)
4550 struct hda_spdif_out *spdif;
4551 unsigned int curr_fmt;
4554 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4555 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4556 AC_VERB_GET_STREAM_FORMAT, 0);
4557 reset = codec->spdif_status_reset &&
4558 (spdif->ctls & AC_DIG1_ENABLE) &&
4561 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4564 set_dig_out_convert(codec, nid,
4565 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4567 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4568 if (codec->slave_dig_outs) {
4570 for (d = codec->slave_dig_outs; *d; d++)
4571 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4574 /* turn on again (if needed) */
4576 set_dig_out_convert(codec, nid,
4577 spdif->ctls & 0xff, -1);
4580 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4582 snd_hda_codec_cleanup_stream(codec, nid);
4583 if (codec->slave_dig_outs) {
4585 for (d = codec->slave_dig_outs; *d; d++)
4586 snd_hda_codec_cleanup_stream(codec, *d);
4591 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4592 * @codec: the HDA codec
4593 * @mout: hda_multi_out object
4595 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4596 struct hda_multi_out *mout)
4598 mutex_lock(&codec->spdif_mutex);
4599 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4600 /* already opened as analog dup; reset it once */
4601 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4602 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4603 mutex_unlock(&codec->spdif_mutex);
4606 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
4609 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4610 * @codec: the HDA codec
4611 * @mout: hda_multi_out object
4612 * @stream_tag: stream tag to assign
4613 * @format: format id to assign
4614 * @substream: PCM substream to assign
4616 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4617 struct hda_multi_out *mout,
4618 unsigned int stream_tag,
4619 unsigned int format,
4620 struct snd_pcm_substream *substream)
4622 mutex_lock(&codec->spdif_mutex);
4623 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4624 mutex_unlock(&codec->spdif_mutex);
4627 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
4630 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4631 * @codec: the HDA codec
4632 * @mout: hda_multi_out object
4634 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4635 struct hda_multi_out *mout)
4637 mutex_lock(&codec->spdif_mutex);
4638 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4639 mutex_unlock(&codec->spdif_mutex);
4642 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
4645 * snd_hda_multi_out_dig_close - release the digital out stream
4646 * @codec: the HDA codec
4647 * @mout: hda_multi_out object
4649 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4650 struct hda_multi_out *mout)
4652 mutex_lock(&codec->spdif_mutex);
4653 mout->dig_out_used = 0;
4654 mutex_unlock(&codec->spdif_mutex);
4657 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
4660 * snd_hda_multi_out_analog_open - open analog outputs
4661 * @codec: the HDA codec
4662 * @mout: hda_multi_out object
4663 * @substream: PCM substream to assign
4664 * @hinfo: PCM information to assign
4666 * Open analog outputs and set up the hw-constraints.
4667 * If the digital outputs can be opened as slave, open the digital
4670 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4671 struct hda_multi_out *mout,
4672 struct snd_pcm_substream *substream,
4673 struct hda_pcm_stream *hinfo)
4675 struct snd_pcm_runtime *runtime = substream->runtime;
4676 runtime->hw.channels_max = mout->max_channels;
4677 if (mout->dig_out_nid) {
4678 if (!mout->analog_rates) {
4679 mout->analog_rates = hinfo->rates;
4680 mout->analog_formats = hinfo->formats;
4681 mout->analog_maxbps = hinfo->maxbps;
4683 runtime->hw.rates = mout->analog_rates;
4684 runtime->hw.formats = mout->analog_formats;
4685 hinfo->maxbps = mout->analog_maxbps;
4687 if (!mout->spdif_rates) {
4688 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4690 &mout->spdif_formats,
4691 &mout->spdif_maxbps);
4693 mutex_lock(&codec->spdif_mutex);
4694 if (mout->share_spdif) {
4695 if ((runtime->hw.rates & mout->spdif_rates) &&
4696 (runtime->hw.formats & mout->spdif_formats)) {
4697 runtime->hw.rates &= mout->spdif_rates;
4698 runtime->hw.formats &= mout->spdif_formats;
4699 if (mout->spdif_maxbps < hinfo->maxbps)
4700 hinfo->maxbps = mout->spdif_maxbps;
4702 mout->share_spdif = 0;
4703 /* FIXME: need notify? */
4706 mutex_unlock(&codec->spdif_mutex);
4708 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4709 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4711 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
4714 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4715 * @codec: the HDA codec
4716 * @mout: hda_multi_out object
4717 * @stream_tag: stream tag to assign
4718 * @format: format id to assign
4719 * @substream: PCM substream to assign
4721 * Set up the i/o for analog out.
4722 * When the digital out is available, copy the front out to digital out, too.
4724 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4725 struct hda_multi_out *mout,
4726 unsigned int stream_tag,
4727 unsigned int format,
4728 struct snd_pcm_substream *substream)
4730 const hda_nid_t *nids = mout->dac_nids;
4731 int chs = substream->runtime->channels;
4732 struct hda_spdif_out *spdif;
4735 mutex_lock(&codec->spdif_mutex);
4736 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
4737 if (mout->dig_out_nid && mout->share_spdif &&
4738 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4740 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4742 !(spdif->status & IEC958_AES0_NONAUDIO)) {
4743 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4744 setup_dig_out_stream(codec, mout->dig_out_nid,
4745 stream_tag, format);
4747 mout->dig_out_used = 0;
4748 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4751 mutex_unlock(&codec->spdif_mutex);
4754 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4756 if (!mout->no_share_stream &&
4757 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4758 /* headphone out will just decode front left/right (stereo) */
4759 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4761 /* extra outputs copied from front */
4762 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4763 if (!mout->no_share_stream && mout->hp_out_nid[i])
4764 snd_hda_codec_setup_stream(codec,
4765 mout->hp_out_nid[i],
4766 stream_tag, 0, format);
4769 for (i = 1; i < mout->num_dacs; i++) {
4770 if (chs >= (i + 1) * 2) /* independent out */
4771 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4773 else if (!mout->no_share_stream) /* copy front */
4774 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4778 /* extra surrounds */
4779 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4781 if (!mout->extra_out_nid[i])
4783 if (chs >= (i + 1) * 2)
4785 else if (!mout->no_share_stream)
4787 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4788 stream_tag, ch, format);
4793 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
4796 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4797 * @codec: the HDA codec
4798 * @mout: hda_multi_out object
4800 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4801 struct hda_multi_out *mout)
4803 const hda_nid_t *nids = mout->dac_nids;
4806 for (i = 0; i < mout->num_dacs; i++)
4807 snd_hda_codec_cleanup_stream(codec, nids[i]);
4809 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4810 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4811 if (mout->hp_out_nid[i])
4812 snd_hda_codec_cleanup_stream(codec,
4813 mout->hp_out_nid[i]);
4814 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4815 if (mout->extra_out_nid[i])
4816 snd_hda_codec_cleanup_stream(codec,
4817 mout->extra_out_nid[i]);
4818 mutex_lock(&codec->spdif_mutex);
4819 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4820 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4821 mout->dig_out_used = 0;
4823 mutex_unlock(&codec->spdif_mutex);
4826 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
4829 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4830 * @codec: the HDA codec
4831 * @pin: referred pin NID
4833 * Guess the suitable VREF pin bits to be set as the pin-control value.
4834 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4836 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4838 unsigned int pincap;
4839 unsigned int oldval;
4840 oldval = snd_hda_codec_read(codec, pin, 0,
4841 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4842 pincap = snd_hda_query_pin_caps(codec, pin);
4843 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4844 /* Exception: if the default pin setup is vref50, we give it priority */
4845 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4846 return AC_PINCTL_VREF_80;
4847 else if (pincap & AC_PINCAP_VREF_50)
4848 return AC_PINCTL_VREF_50;
4849 else if (pincap & AC_PINCAP_VREF_100)
4850 return AC_PINCTL_VREF_100;
4851 else if (pincap & AC_PINCAP_VREF_GRD)
4852 return AC_PINCTL_VREF_GRD;
4853 return AC_PINCTL_VREF_HIZ;
4855 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
4858 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4859 * @codec: the HDA codec
4860 * @pin: referred pin NID
4861 * @val: pin ctl value to audit
4863 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4864 hda_nid_t pin, unsigned int val)
4866 static unsigned int cap_lists[][2] = {
4867 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4868 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4869 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4870 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4876 cap = snd_hda_query_pin_caps(codec, pin);
4878 return val; /* don't know what to do... */
4880 if (val & AC_PINCTL_OUT_EN) {
4881 if (!(cap & AC_PINCAP_OUT))
4882 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4883 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4884 val &= ~AC_PINCTL_HP_EN;
4887 if (val & AC_PINCTL_IN_EN) {
4888 if (!(cap & AC_PINCAP_IN))
4889 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4891 unsigned int vcap, vref;
4893 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4894 vref = val & AC_PINCTL_VREFEN;
4895 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4896 if (vref == cap_lists[i][0] &&
4897 !(vcap & cap_lists[i][1])) {
4898 if (i == ARRAY_SIZE(cap_lists) - 1)
4899 vref = AC_PINCTL_VREF_HIZ;
4901 vref = cap_lists[i + 1][0];
4904 val &= ~AC_PINCTL_VREFEN;
4911 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4914 * _snd_hda_pin_ctl - Helper to set pin ctl value
4915 * @codec: the HDA codec
4916 * @pin: referred pin NID
4917 * @val: pin control value to set
4918 * @cached: access over codec pinctl cache or direct write
4920 * This function is a helper to set a pin ctl value more safely.
4921 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4922 * value in pin target array via snd_hda_codec_set_pin_target(), then
4923 * actually writes the value via either snd_hda_codec_update_cache() or
4924 * snd_hda_codec_write() depending on @cached flag.
4926 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4927 unsigned int val, bool cached)
4929 val = snd_hda_correct_pin_ctl(codec, pin, val);
4930 snd_hda_codec_set_pin_target(codec, pin, val);
4932 return snd_hda_codec_update_cache(codec, pin, 0,
4933 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4935 return snd_hda_codec_write(codec, pin, 0,
4936 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4938 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4941 * snd_hda_add_imux_item - Add an item to input_mux
4942 * @codec: the HDA codec
4943 * @imux: imux helper object
4944 * @label: the name of imux item to assign
4945 * @index: index number of imux item to assign
4946 * @type_idx: pointer to store the resultant label index
4948 * When the same label is used already in the existing items, the number
4949 * suffix is appended to the label. This label index number is stored
4950 * to type_idx when non-NULL pointer is given.
4952 int snd_hda_add_imux_item(struct hda_codec *codec,
4953 struct hda_input_mux *imux, const char *label,
4954 int index, int *type_idx)
4956 int i, label_idx = 0;
4957 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4958 codec_err(codec, "hda_codec: Too many imux items!\n");
4961 for (i = 0; i < imux->num_items; i++) {
4962 if (!strncmp(label, imux->items[i].label, strlen(label)))
4966 *type_idx = label_idx;
4968 snprintf(imux->items[imux->num_items].label,
4969 sizeof(imux->items[imux->num_items].label),
4970 "%s %d", label, label_idx);
4972 strlcpy(imux->items[imux->num_items].label, label,
4973 sizeof(imux->items[imux->num_items].label));
4974 imux->items[imux->num_items].index = index;
4978 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4981 * snd_hda_bus_reset - Reset the bus
4982 * @bus: HD-audio bus
4984 void snd_hda_bus_reset(struct hda_bus *bus)
4986 struct hda_codec *codec;
4988 list_for_each_codec(codec, bus) {
4989 /* FIXME: maybe a better way needed for forced reset */
4990 cancel_delayed_work_sync(&codec->jackpoll_work);
4992 if (hda_codec_is_power_on(codec)) {
4993 hda_call_codec_suspend(codec);
4994 hda_call_codec_resume(codec);
4999 EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
5006 * snd_array_new - get a new element from the given array
5007 * @array: the array object
5009 * Get a new element from the given array. If it exceeds the
5010 * pre-allocated array size, re-allocate the array.
5012 * Returns NULL if allocation failed.
5014 void *snd_array_new(struct snd_array *array)
5016 if (snd_BUG_ON(!array->elem_size))
5018 if (array->used >= array->alloced) {
5019 int num = array->alloced + array->alloc_align;
5020 int size = (num + 1) * array->elem_size;
5022 if (snd_BUG_ON(num >= 4096))
5024 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5027 array->list = nlist;
5028 array->alloced = num;
5030 return snd_array_elem(array, array->used++);
5032 EXPORT_SYMBOL_GPL(snd_array_new);
5035 * snd_array_free - free the given array elements
5036 * @array: the array object
5038 void snd_array_free(struct snd_array *array)
5045 EXPORT_SYMBOL_GPL(snd_array_free);
5048 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5049 * @pcm: PCM caps bits
5050 * @buf: the string buffer to write
5051 * @buflen: the max buffer length
5053 * used by hda_proc.c and hda_eld.c
5055 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5057 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5060 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5061 if (pcm & (AC_SUPPCM_BITS_8 << i))
5062 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5064 buf[j] = '\0'; /* necessary when j == 0 */
5066 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5068 MODULE_DESCRIPTION("HDA codec core");
5069 MODULE_LICENSE("GPL");