2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
63 const char *snd_hda_get_jack_location(u32 cfg)
65 static char *bases[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
68 static unsigned char specials_idx[] = {
73 static char *specials[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
79 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
81 return bases[cfg & 0x0f];
82 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
83 if (cfg == specials_idx[i])
88 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
97 const char *snd_hda_get_jack_connectivity(u32 cfg)
99 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
101 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
103 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
112 const char *snd_hda_get_jack_type(u32 cfg)
114 static char *jack_types[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
118 "SPDIF In", "Digital In", "Reserved", "Other"
121 return jack_types[(cfg & AC_DEFCFG_DEVICE)
122 >> AC_DEFCFG_DEVICE_SHIFT];
124 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
127 * Send and receive a verb - passed to exec_verb override for hdac_device
129 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
130 unsigned int flags, unsigned int *res)
132 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
133 struct hda_bus *bus = codec->bus;
140 snd_hda_power_up_pm(codec);
141 mutex_lock(&bus->core.cmd_mutex);
142 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
143 bus->no_response_fallback = 1;
144 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
146 bus->no_response_fallback = 0;
147 mutex_unlock(&bus->core.cmd_mutex);
148 snd_hda_power_down_pm(codec);
149 if (!codec_in_pm(codec) && res && err < 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_hdac_read_parm_uncached(&codec->core,
590 nid, 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 /* release all pincfg lists */
811 static void free_init_pincfgs(struct hda_codec *codec)
813 snd_array_free(&codec->driver_pins);
814 #ifdef CONFIG_SND_HDA_RECONFIG
815 snd_array_free(&codec->user_pins);
817 snd_array_free(&codec->init_pins);
821 * audio-converter setup caches
823 struct hda_cvt_setup {
828 unsigned char active; /* cvt is currently used */
829 unsigned char dirty; /* setups should be cleared */
832 /* get or create a cache entry for the given audio converter NID */
833 static struct hda_cvt_setup *
834 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
836 struct hda_cvt_setup *p;
839 for (i = 0; i < codec->cvt_setups.used; i++) {
840 p = snd_array_elem(&codec->cvt_setups, i);
844 p = snd_array_new(&codec->cvt_setups);
853 static void release_pcm(struct kref *kref)
855 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
858 snd_device_free(pcm->codec->card, pcm->pcm);
859 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
864 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
866 kref_put(&pcm->kref, release_pcm);
868 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
870 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
871 const char *fmt, ...)
876 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
881 kref_init(&pcm->kref);
883 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
890 list_add_tail(&pcm->list, &codec->pcm_list_head);
893 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
898 static void codec_release_pcms(struct hda_codec *codec)
900 struct hda_pcm *pcm, *n;
902 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
903 list_del_init(&pcm->list);
905 snd_device_disconnect(codec->card, pcm->pcm);
906 snd_hda_codec_pcm_put(pcm);
910 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
912 if (codec->registered) {
913 /* pm_runtime_put() is called in snd_hdac_device_exit() */
914 pm_runtime_get_noresume(hda_codec_dev(codec));
915 pm_runtime_disable(hda_codec_dev(codec));
916 codec->registered = 0;
919 cancel_delayed_work_sync(&codec->jackpoll_work);
920 if (!codec->in_freeing)
921 snd_hda_ctls_clear(codec);
922 codec_release_pcms(codec);
923 snd_hda_detach_beep_device(codec);
924 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
925 snd_hda_jack_tbl_clear(codec);
926 codec->proc_widget_hook = NULL;
929 /* free only driver_pins so that init_pins + user_pins are restored */
930 snd_array_free(&codec->driver_pins);
931 snd_array_free(&codec->cvt_setups);
932 snd_array_free(&codec->spdif_out);
933 snd_array_free(&codec->verbs);
934 codec->preset = NULL;
935 codec->slave_dig_outs = NULL;
936 codec->spdif_status_reset = 0;
937 snd_array_free(&codec->mixers);
938 snd_array_free(&codec->nids);
939 remove_conn_list(codec);
940 snd_hdac_regmap_exit(&codec->core);
943 static unsigned int hda_set_power_state(struct hda_codec *codec,
944 unsigned int power_state);
946 /* also called from hda_bind.c */
947 void snd_hda_codec_register(struct hda_codec *codec)
949 if (codec->registered)
951 if (device_is_registered(hda_codec_dev(codec))) {
952 snd_hda_register_beep_device(codec);
953 pm_runtime_enable(hda_codec_dev(codec));
954 /* it was powered up in snd_hda_codec_new(), now all done */
955 snd_hda_power_down(codec);
956 codec->registered = 1;
960 static int snd_hda_codec_dev_register(struct snd_device *device)
962 snd_hda_codec_register(device->device_data);
966 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
968 struct hda_codec *codec = device->device_data;
970 snd_hda_detach_beep_device(codec);
974 static int snd_hda_codec_dev_free(struct snd_device *device)
976 struct hda_codec *codec = device->device_data;
978 codec->in_freeing = 1;
979 snd_hdac_device_unregister(&codec->core);
980 put_device(hda_codec_dev(codec));
984 static void snd_hda_codec_dev_release(struct device *dev)
986 struct hda_codec *codec = dev_to_hda_codec(dev);
988 free_init_pincfgs(codec);
989 snd_hdac_device_exit(&codec->core);
990 snd_hda_sysfs_clear(codec);
991 kfree(codec->modelname);
997 * snd_hda_codec_new - create a HDA codec
998 * @bus: the bus to assign
999 * @codec_addr: the codec address
1000 * @codecp: the pointer to store the generated codec
1002 * Returns 0 if successful, or a negative error code.
1004 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1005 unsigned int codec_addr, struct hda_codec **codecp)
1007 struct hda_codec *codec;
1011 static struct snd_device_ops dev_ops = {
1012 .dev_register = snd_hda_codec_dev_register,
1013 .dev_disconnect = snd_hda_codec_dev_disconnect,
1014 .dev_free = snd_hda_codec_dev_free,
1017 if (snd_BUG_ON(!bus))
1019 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1022 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1026 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
1027 err = snd_hdac_device_init(&codec->core, &bus->core, component,
1034 codec->core.dev.release = snd_hda_codec_dev_release;
1035 codec->core.type = HDA_DEV_LEGACY;
1036 codec->core.exec_verb = codec_exec_verb;
1040 codec->addr = codec_addr;
1041 mutex_init(&codec->spdif_mutex);
1042 mutex_init(&codec->control_mutex);
1043 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1044 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1045 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1046 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1047 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1048 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1049 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1050 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1051 INIT_LIST_HEAD(&codec->conn_list);
1052 INIT_LIST_HEAD(&codec->pcm_list_head);
1054 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1055 codec->depop_delay = -1;
1056 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1059 codec->power_jiffies = jiffies;
1062 snd_hda_sysfs_init(codec);
1064 if (codec->bus->modelname) {
1065 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1066 if (!codec->modelname) {
1072 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1073 err = read_widget_caps(codec, fg);
1076 err = read_pin_defaults(codec);
1080 /* power-up all before initialization */
1081 hda_set_power_state(codec, AC_PWRST_D0);
1083 snd_hda_codec_proc_new(codec);
1085 snd_hda_create_hwdep(codec);
1087 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
1088 codec->core.subsystem_id, codec->core.revision_id);
1089 snd_component_add(card, component);
1091 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
1100 put_device(hda_codec_dev(codec));
1103 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1106 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1107 * @codec: the HDA codec
1109 * Forcibly refresh the all widget caps and the init pin configurations of
1112 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1117 err = snd_hdac_refresh_widgets(&codec->core);
1121 /* Assume the function group node does not change,
1122 * only the widget nodes may change.
1124 kfree(codec->wcaps);
1125 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
1126 err = read_widget_caps(codec, fg);
1130 snd_array_free(&codec->init_pins);
1131 err = read_pin_defaults(codec);
1135 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1137 /* update the stream-id if changed */
1138 static void update_pcm_stream_id(struct hda_codec *codec,
1139 struct hda_cvt_setup *p, hda_nid_t nid,
1140 u32 stream_tag, int channel_id)
1142 unsigned int oldval, newval;
1144 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1145 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1146 newval = (stream_tag << 4) | channel_id;
1147 if (oldval != newval)
1148 snd_hda_codec_write(codec, nid, 0,
1149 AC_VERB_SET_CHANNEL_STREAMID,
1151 p->stream_tag = stream_tag;
1152 p->channel_id = channel_id;
1156 /* update the format-id if changed */
1157 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1158 hda_nid_t nid, int format)
1160 unsigned int oldval;
1162 if (p->format_id != format) {
1163 oldval = snd_hda_codec_read(codec, nid, 0,
1164 AC_VERB_GET_STREAM_FORMAT, 0);
1165 if (oldval != format) {
1167 snd_hda_codec_write(codec, nid, 0,
1168 AC_VERB_SET_STREAM_FORMAT,
1171 p->format_id = format;
1176 * snd_hda_codec_setup_stream - set up the codec for streaming
1177 * @codec: the CODEC to set up
1178 * @nid: the NID to set up
1179 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1180 * @channel_id: channel id to pass, zero based.
1181 * @format: stream format.
1183 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1185 int channel_id, int format)
1187 struct hda_codec *c;
1188 struct hda_cvt_setup *p;
1196 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1197 nid, stream_tag, channel_id, format);
1198 p = get_hda_cvt_setup(codec, nid);
1202 if (codec->patch_ops.stream_pm)
1203 codec->patch_ops.stream_pm(codec, nid, true);
1204 if (codec->pcm_format_first)
1205 update_pcm_format(codec, p, nid, format);
1206 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1207 if (!codec->pcm_format_first)
1208 update_pcm_format(codec, p, nid, format);
1213 /* make other inactive cvts with the same stream-tag dirty */
1214 type = get_wcaps_type(get_wcaps(codec, nid));
1215 list_for_each_codec(c, codec->bus) {
1216 for (i = 0; i < c->cvt_setups.used; i++) {
1217 p = snd_array_elem(&c->cvt_setups, i);
1218 if (!p->active && p->stream_tag == stream_tag &&
1219 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1224 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1226 static void really_cleanup_stream(struct hda_codec *codec,
1227 struct hda_cvt_setup *q);
1230 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1231 * @codec: the CODEC to clean up
1232 * @nid: the NID to clean up
1233 * @do_now: really clean up the stream instead of clearing the active flag
1235 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1238 struct hda_cvt_setup *p;
1243 if (codec->no_sticky_stream)
1246 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1247 p = get_hda_cvt_setup(codec, nid);
1249 /* here we just clear the active flag when do_now isn't set;
1250 * actual clean-ups will be done later in
1251 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1254 really_cleanup_stream(codec, p);
1259 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1261 static void really_cleanup_stream(struct hda_codec *codec,
1262 struct hda_cvt_setup *q)
1264 hda_nid_t nid = q->nid;
1265 if (q->stream_tag || q->channel_id)
1266 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1268 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1270 memset(q, 0, sizeof(*q));
1272 if (codec->patch_ops.stream_pm)
1273 codec->patch_ops.stream_pm(codec, nid, false);
1276 /* clean up the all conflicting obsolete streams */
1277 static void purify_inactive_streams(struct hda_codec *codec)
1279 struct hda_codec *c;
1282 list_for_each_codec(c, codec->bus) {
1283 for (i = 0; i < c->cvt_setups.used; i++) {
1284 struct hda_cvt_setup *p;
1285 p = snd_array_elem(&c->cvt_setups, i);
1287 really_cleanup_stream(c, p);
1293 /* clean up all streams; called from suspend */
1294 static void hda_cleanup_all_streams(struct hda_codec *codec)
1298 for (i = 0; i < codec->cvt_setups.used; i++) {
1299 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1301 really_cleanup_stream(codec, p);
1307 * amp access functions
1311 * query_amp_caps - query AMP capabilities
1312 * @codec: the HD-auio codec
1313 * @nid: the NID to query
1314 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1316 * Query AMP capabilities for the given widget and direction.
1317 * Returns the obtained capability bits.
1319 * When cap bits have been already read, this doesn't read again but
1320 * returns the cached value.
1322 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1324 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1325 nid = codec->core.afg;
1326 return snd_hda_param_read(codec, nid,
1327 direction == HDA_OUTPUT ?
1328 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1330 EXPORT_SYMBOL_GPL(query_amp_caps);
1333 * snd_hda_check_amp_caps - query AMP capabilities
1334 * @codec: the HD-audio codec
1335 * @nid: the NID to query
1336 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1337 * @bits: bit mask to check the result
1339 * Check whether the widget has the given amp capability for the direction.
1341 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1342 int dir, unsigned int bits)
1346 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1347 if (query_amp_caps(codec, nid, dir) & bits)
1351 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1354 * snd_hda_override_amp_caps - Override the AMP capabilities
1355 * @codec: the CODEC to clean up
1356 * @nid: the NID to clean up
1357 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1358 * @caps: the capability bits to set
1360 * Override the cached AMP caps bits value by the given one.
1361 * This function is useful if the driver needs to adjust the AMP ranges,
1362 * e.g. limit to 0dB, etc.
1364 * Returns zero if successful or a negative error code.
1366 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1371 snd_hda_override_wcaps(codec, nid,
1372 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1373 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1374 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
1376 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1379 * snd_hda_codec_amp_stereo - update the AMP stereo values
1380 * @codec: HD-audio codec
1381 * @nid: NID to read the AMP value
1382 * @direction: #HDA_INPUT or #HDA_OUTPUT
1383 * @idx: the index value (only for input direction)
1384 * @mask: bit mask to set
1385 * @val: the bits value to set
1387 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1388 * stereo widget with the same mask and value.
1390 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1391 int direction, int idx, int mask, int val)
1395 if (snd_BUG_ON(mask & ~0xff))
1397 for (ch = 0; ch < 2; ch++)
1398 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1402 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1405 * snd_hda_codec_amp_init - initialize the AMP value
1406 * @codec: the HDA codec
1407 * @nid: NID to read the AMP value
1408 * @ch: channel (left=0 or right=1)
1409 * @dir: #HDA_INPUT or #HDA_OUTPUT
1410 * @idx: the index value (only for input direction)
1411 * @mask: bit mask to set
1412 * @val: the bits value to set
1414 * Works like snd_hda_codec_amp_update() but it writes the value only at
1415 * the first access. If the amp was already initialized / updated beforehand,
1416 * this does nothing.
1418 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1419 int dir, int idx, int mask, int val)
1423 if (!codec->core.regmap)
1425 regcache_cache_only(codec->core.regmap, true);
1426 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1427 regcache_cache_only(codec->core.regmap, false);
1430 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
1432 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1435 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1436 * @codec: the HDA codec
1437 * @nid: NID to read the AMP value
1438 * @dir: #HDA_INPUT or #HDA_OUTPUT
1439 * @idx: the index value (only for input direction)
1440 * @mask: bit mask to set
1441 * @val: the bits value to set
1443 * Call snd_hda_codec_amp_init() for both stereo channels.
1445 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1446 int dir, int idx, int mask, int val)
1450 if (snd_BUG_ON(mask & ~0xff))
1452 for (ch = 0; ch < 2; ch++)
1453 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1457 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
1459 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1462 u32 caps = query_amp_caps(codec, nid, dir);
1464 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1471 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1472 * @kcontrol: referred ctl element
1473 * @uinfo: pointer to get/store the data
1475 * The control element is supposed to have the private_value field
1476 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1478 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_info *uinfo)
1481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1482 u16 nid = get_amp_nid(kcontrol);
1483 u8 chs = get_amp_channels(kcontrol);
1484 int dir = get_amp_direction(kcontrol);
1485 unsigned int ofs = get_amp_offset(kcontrol);
1487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1488 uinfo->count = chs == 3 ? 2 : 1;
1489 uinfo->value.integer.min = 0;
1490 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1491 if (!uinfo->value.integer.max) {
1493 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1494 nid, kcontrol->id.name);
1499 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
1502 static inline unsigned int
1503 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1504 int ch, int dir, int idx, unsigned int ofs)
1507 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1508 val &= HDA_AMP_VOLMASK;
1517 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1518 int ch, int dir, int idx, unsigned int ofs,
1521 unsigned int maxval;
1525 /* ofs = 0: raw max value */
1526 maxval = get_amp_max_value(codec, nid, dir, 0);
1529 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1530 HDA_AMP_VOLMASK, val);
1534 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1535 * @kcontrol: ctl element
1536 * @ucontrol: pointer to get/store the data
1538 * The control element is supposed to have the private_value field
1539 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1541 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
1544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1545 hda_nid_t nid = get_amp_nid(kcontrol);
1546 int chs = get_amp_channels(kcontrol);
1547 int dir = get_amp_direction(kcontrol);
1548 int idx = get_amp_index(kcontrol);
1549 unsigned int ofs = get_amp_offset(kcontrol);
1550 long *valp = ucontrol->value.integer.value;
1553 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1555 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1558 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
1561 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1562 * @kcontrol: ctl element
1563 * @ucontrol: pointer to get/store the data
1565 * The control element is supposed to have the private_value field
1566 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1568 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
1571 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1572 hda_nid_t nid = get_amp_nid(kcontrol);
1573 int chs = get_amp_channels(kcontrol);
1574 int dir = get_amp_direction(kcontrol);
1575 int idx = get_amp_index(kcontrol);
1576 unsigned int ofs = get_amp_offset(kcontrol);
1577 long *valp = ucontrol->value.integer.value;
1581 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1585 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1588 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
1591 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1592 * @kcontrol: ctl element
1593 * @op_flag: operation flag
1594 * @size: byte size of input TLV
1597 * The control element is supposed to have the private_value field
1598 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1600 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1601 unsigned int size, unsigned int __user *_tlv)
1603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1604 hda_nid_t nid = get_amp_nid(kcontrol);
1605 int dir = get_amp_direction(kcontrol);
1606 unsigned int ofs = get_amp_offset(kcontrol);
1607 bool min_mute = get_amp_min_mute(kcontrol);
1608 u32 caps, val1, val2;
1610 if (size < 4 * sizeof(unsigned int))
1612 caps = query_amp_caps(codec, nid, dir);
1613 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1614 val2 = (val2 + 1) * 25;
1615 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1617 val1 = ((int)val1) * ((int)val2);
1618 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1619 val2 |= TLV_DB_SCALE_MUTE;
1620 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1622 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1624 if (put_user(val1, _tlv + 2))
1626 if (put_user(val2, _tlv + 3))
1630 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
1633 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1634 * @codec: HD-audio codec
1635 * @nid: NID of a reference widget
1636 * @dir: #HDA_INPUT or #HDA_OUTPUT
1637 * @tlv: TLV data to be stored, at least 4 elements
1639 * Set (static) TLV data for a virtual master volume using the AMP caps
1640 * obtained from the reference NID.
1641 * The volume range is recalculated as if the max volume is 0dB.
1643 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1649 caps = query_amp_caps(codec, nid, dir);
1650 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1651 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1652 step = (step + 1) * 25;
1653 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1654 tlv[1] = 2 * sizeof(unsigned int);
1655 tlv[2] = -nums * step;
1658 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
1660 /* find a mixer control element with the given name */
1661 static struct snd_kcontrol *
1662 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
1664 struct snd_ctl_elem_id id;
1665 memset(&id, 0, sizeof(id));
1666 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1669 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1671 strcpy(id.name, name);
1672 return snd_ctl_find_id(codec->card, &id);
1676 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1677 * @codec: HD-audio codec
1678 * @name: ctl id name string
1680 * Get the control element with the given id string and IFACE_MIXER.
1682 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1685 return find_mixer_ctl(codec, name, 0, 0);
1687 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
1689 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
1693 /* 16 ctlrs should be large enough */
1694 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1695 if (!find_mixer_ctl(codec, name, 0, idx))
1702 * snd_hda_ctl_add - Add a control element and assign to the codec
1703 * @codec: HD-audio codec
1704 * @nid: corresponding NID (optional)
1705 * @kctl: the control element to assign
1707 * Add the given control element to an array inside the codec instance.
1708 * All control elements belonging to a codec are supposed to be added
1709 * by this function so that a proper clean-up works at the free or
1710 * reconfiguration time.
1712 * If non-zero @nid is passed, the NID is assigned to the control element.
1713 * The assignment is shown in the codec proc file.
1715 * snd_hda_ctl_add() checks the control subdev id field whether
1716 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1717 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1718 * specifies if kctl->private_value is a HDA amplifier value.
1720 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1721 struct snd_kcontrol *kctl)
1724 unsigned short flags = 0;
1725 struct hda_nid_item *item;
1727 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1728 flags |= HDA_NID_ITEM_AMP;
1730 nid = get_amp_nid_(kctl->private_value);
1732 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1733 nid = kctl->id.subdevice & 0xffff;
1734 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1735 kctl->id.subdevice = 0;
1736 err = snd_ctl_add(codec->card, kctl);
1739 item = snd_array_new(&codec->mixers);
1744 item->flags = flags;
1747 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
1750 * snd_hda_add_nid - Assign a NID to a control element
1751 * @codec: HD-audio codec
1752 * @nid: corresponding NID (optional)
1753 * @kctl: the control element to assign
1754 * @index: index to kctl
1756 * Add the given control element to an array inside the codec instance.
1757 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1758 * NID:KCTL mapping - for example "Capture Source" selector.
1760 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1761 unsigned int index, hda_nid_t nid)
1763 struct hda_nid_item *item;
1766 item = snd_array_new(&codec->nids);
1770 item->index = index;
1774 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1775 kctl->id.name, kctl->id.index, index);
1778 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
1781 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1782 * @codec: HD-audio codec
1784 void snd_hda_ctls_clear(struct hda_codec *codec)
1787 struct hda_nid_item *items = codec->mixers.list;
1788 for (i = 0; i < codec->mixers.used; i++)
1789 snd_ctl_remove(codec->card, items[i].kctl);
1790 snd_array_free(&codec->mixers);
1791 snd_array_free(&codec->nids);
1795 * snd_hda_lock_devices - pseudo device locking
1798 * toggle card->shutdown to allow/disallow the device access (as a hack)
1800 int snd_hda_lock_devices(struct hda_bus *bus)
1802 struct snd_card *card = bus->card;
1803 struct hda_codec *codec;
1805 spin_lock(&card->files_lock);
1809 if (!list_empty(&card->ctl_files))
1812 list_for_each_codec(codec, bus) {
1813 struct hda_pcm *cpcm;
1814 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
1817 if (cpcm->pcm->streams[0].substream_opened ||
1818 cpcm->pcm->streams[1].substream_opened)
1822 spin_unlock(&card->files_lock);
1828 spin_unlock(&card->files_lock);
1831 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
1834 * snd_hda_unlock_devices - pseudo device unlocking
1837 void snd_hda_unlock_devices(struct hda_bus *bus)
1839 struct snd_card *card = bus->card;
1841 spin_lock(&card->files_lock);
1843 spin_unlock(&card->files_lock);
1845 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
1848 * snd_hda_codec_reset - Clear all objects assigned to the codec
1849 * @codec: HD-audio codec
1851 * This frees the all PCM and control elements assigned to the codec, and
1852 * clears the caches and restores the pin default configurations.
1854 * When a device is being used, it returns -EBSY. If successfully freed,
1857 int snd_hda_codec_reset(struct hda_codec *codec)
1859 struct hda_bus *bus = codec->bus;
1861 if (snd_hda_lock_devices(bus) < 0)
1864 /* OK, let it free */
1865 snd_hdac_device_unregister(&codec->core);
1867 /* allow device access again */
1868 snd_hda_unlock_devices(bus);
1872 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
1874 /* apply the function to all matching slave ctls in the mixer list */
1875 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
1876 const char *suffix, map_slave_func_t func, void *data)
1878 struct hda_nid_item *items;
1879 const char * const *s;
1882 items = codec->mixers.list;
1883 for (i = 0; i < codec->mixers.used; i++) {
1884 struct snd_kcontrol *sctl = items[i].kctl;
1885 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
1887 for (s = slaves; *s; s++) {
1888 char tmpname[sizeof(sctl->id.name)];
1889 const char *name = *s;
1891 snprintf(tmpname, sizeof(tmpname), "%s %s",
1895 if (!strcmp(sctl->id.name, name)) {
1896 err = func(codec, data, sctl);
1906 static int check_slave_present(struct hda_codec *codec,
1907 void *data, struct snd_kcontrol *sctl)
1912 /* guess the value corresponding to 0dB */
1913 static int get_kctl_0dB_offset(struct hda_codec *codec,
1914 struct snd_kcontrol *kctl, int *step_to_check)
1917 const int *tlv = NULL;
1920 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1921 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1922 mm_segment_t fs = get_fs();
1924 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1927 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1929 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1931 step &= ~TLV_DB_SCALE_MUTE;
1934 if (*step_to_check && *step_to_check != step) {
1935 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
1936 - *step_to_check, step);
1939 *step_to_check = step;
1940 val = -tlv[2] / step;
1945 /* call kctl->put with the given value(s) */
1946 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1948 struct snd_ctl_elem_value *ucontrol;
1949 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1952 ucontrol->value.integer.value[0] = val;
1953 ucontrol->value.integer.value[1] = val;
1954 kctl->put(kctl, ucontrol);
1959 /* initialize the slave volume with 0dB */
1960 static int init_slave_0dB(struct hda_codec *codec,
1961 void *data, struct snd_kcontrol *slave)
1963 int offset = get_kctl_0dB_offset(codec, slave, data);
1965 put_kctl_with_value(slave, offset);
1969 /* unmute the slave */
1970 static int init_slave_unmute(struct hda_codec *codec,
1971 void *data, struct snd_kcontrol *slave)
1973 return put_kctl_with_value(slave, 1);
1976 static int add_slave(struct hda_codec *codec,
1977 void *data, struct snd_kcontrol *slave)
1979 return snd_ctl_add_slave(data, slave);
1983 * __snd_hda_add_vmaster - create a virtual master control and add slaves
1984 * @codec: HD-audio codec
1985 * @name: vmaster control name
1986 * @tlv: TLV data (optional)
1987 * @slaves: slave control names (optional)
1988 * @suffix: suffix string to each slave name (optional)
1989 * @init_slave_vol: initialize slaves to unmute/0dB
1990 * @ctl_ret: store the vmaster kcontrol in return
1992 * Create a virtual master control with the given name. The TLV data
1993 * must be either NULL or a valid data.
1995 * @slaves is a NULL-terminated array of strings, each of which is a
1996 * slave control name. All controls with these names are assigned to
1997 * the new virtual master control.
1999 * This function returns zero if successful or a negative error code.
2001 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2002 unsigned int *tlv, const char * const *slaves,
2003 const char *suffix, bool init_slave_vol,
2004 struct snd_kcontrol **ctl_ret)
2006 struct snd_kcontrol *kctl;
2012 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2014 codec_dbg(codec, "No slave found for %s\n", name);
2017 kctl = snd_ctl_make_virtual_master(name, tlv);
2020 err = snd_hda_ctl_add(codec, 0, kctl);
2024 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2028 /* init with master mute & zero volume */
2029 put_kctl_with_value(kctl, 0);
2030 if (init_slave_vol) {
2032 map_slaves(codec, slaves, suffix,
2033 tlv ? init_slave_0dB : init_slave_unmute, &step);
2040 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2043 * mute-LED control using vmaster
2045 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
2048 static const char * const texts[] = {
2049 "On", "Off", "Follow Master"
2052 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2055 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2058 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2059 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2063 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2066 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2067 unsigned int old_mode = hook->mute_mode;
2069 hook->mute_mode = ucontrol->value.enumerated.item[0];
2070 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2071 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2072 if (old_mode == hook->mute_mode)
2074 snd_hda_sync_vmaster_hook(hook);
2078 static struct snd_kcontrol_new vmaster_mute_mode = {
2079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2080 .name = "Mute-LED Mode",
2081 .info = vmaster_mute_mode_info,
2082 .get = vmaster_mute_mode_get,
2083 .put = vmaster_mute_mode_put,
2086 /* meta hook to call each driver's vmaster hook */
2087 static void vmaster_hook(void *private_data, int enabled)
2089 struct hda_vmaster_mute_hook *hook = private_data;
2091 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2092 enabled = hook->mute_mode;
2093 hook->hook(hook->codec, enabled);
2097 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2098 * @codec: the HDA codec
2099 * @hook: the vmaster hook object
2100 * @expose_enum_ctl: flag to create an enum ctl
2102 * Add a mute-LED hook with the given vmaster switch kctl.
2103 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2104 * created and associated with the given hook.
2106 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2107 struct hda_vmaster_mute_hook *hook,
2108 bool expose_enum_ctl)
2110 struct snd_kcontrol *kctl;
2112 if (!hook->hook || !hook->sw_kctl)
2114 hook->codec = codec;
2115 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2116 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
2117 if (!expose_enum_ctl)
2119 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2122 return snd_hda_ctl_add(codec, 0, kctl);
2124 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2127 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2128 * @hook: the vmaster hook
2130 * Call the hook with the current value for synchronization.
2131 * Should be called in init callback.
2133 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2135 if (!hook->hook || !hook->codec)
2137 /* don't call vmaster hook in the destructor since it might have
2138 * been already destroyed
2140 if (hook->codec->bus->shutdown)
2142 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2144 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2148 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2149 * @kcontrol: referred ctl element
2150 * @uinfo: pointer to get/store the data
2152 * The control element is supposed to have the private_value field
2153 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2155 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_info *uinfo)
2158 int chs = get_amp_channels(kcontrol);
2160 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2161 uinfo->count = chs == 3 ? 2 : 1;
2162 uinfo->value.integer.min = 0;
2163 uinfo->value.integer.max = 1;
2166 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2169 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2170 * @kcontrol: ctl element
2171 * @ucontrol: pointer to get/store the data
2173 * The control element is supposed to have the private_value field
2174 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2176 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 hda_nid_t nid = get_amp_nid(kcontrol);
2181 int chs = get_amp_channels(kcontrol);
2182 int dir = get_amp_direction(kcontrol);
2183 int idx = get_amp_index(kcontrol);
2184 long *valp = ucontrol->value.integer.value;
2187 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2188 HDA_AMP_MUTE) ? 0 : 1;
2190 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2191 HDA_AMP_MUTE) ? 0 : 1;
2194 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2197 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2198 * @kcontrol: ctl element
2199 * @ucontrol: pointer to get/store the data
2201 * The control element is supposed to have the private_value field
2202 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2204 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2205 struct snd_ctl_elem_value *ucontrol)
2207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2208 hda_nid_t nid = get_amp_nid(kcontrol);
2209 int chs = get_amp_channels(kcontrol);
2210 int dir = get_amp_direction(kcontrol);
2211 int idx = get_amp_index(kcontrol);
2212 long *valp = ucontrol->value.integer.value;
2216 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2218 *valp ? 0 : HDA_AMP_MUTE);
2222 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2224 *valp ? 0 : HDA_AMP_MUTE);
2225 hda_call_check_power_status(codec, nid);
2228 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2231 * bound volume controls
2233 * bind multiple volumes (# indices, from 0)
2236 #define AMP_VAL_IDX_SHIFT 19
2237 #define AMP_VAL_IDX_MASK (0x0f<<19)
2240 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2241 * @kcontrol: ctl element
2242 * @ucontrol: pointer to get/store the data
2244 * The control element is supposed to have the private_value field
2245 * set up via HDA_BIND_MUTE*() macros.
2247 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2254 mutex_lock(&codec->control_mutex);
2255 pval = kcontrol->private_value;
2256 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2257 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2258 kcontrol->private_value = pval;
2259 mutex_unlock(&codec->control_mutex);
2262 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2265 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2266 * @kcontrol: ctl element
2267 * @ucontrol: pointer to get/store the data
2269 * The control element is supposed to have the private_value field
2270 * set up via HDA_BIND_MUTE*() macros.
2272 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2277 int i, indices, err = 0, change = 0;
2279 mutex_lock(&codec->control_mutex);
2280 pval = kcontrol->private_value;
2281 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2282 for (i = 0; i < indices; i++) {
2283 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2284 (i << AMP_VAL_IDX_SHIFT);
2285 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2290 kcontrol->private_value = pval;
2291 mutex_unlock(&codec->control_mutex);
2292 return err < 0 ? err : change;
2294 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2297 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2298 * @kcontrol: referred ctl element
2299 * @uinfo: pointer to get/store the data
2301 * The control element is supposed to have the private_value field
2302 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2304 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2305 struct snd_ctl_elem_info *uinfo)
2307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2308 struct hda_bind_ctls *c;
2311 mutex_lock(&codec->control_mutex);
2312 c = (struct hda_bind_ctls *)kcontrol->private_value;
2313 kcontrol->private_value = *c->values;
2314 err = c->ops->info(kcontrol, uinfo);
2315 kcontrol->private_value = (long)c;
2316 mutex_unlock(&codec->control_mutex);
2319 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2322 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2323 * @kcontrol: ctl element
2324 * @ucontrol: pointer to get/store the data
2326 * The control element is supposed to have the private_value field
2327 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2329 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 struct hda_bind_ctls *c;
2336 mutex_lock(&codec->control_mutex);
2337 c = (struct hda_bind_ctls *)kcontrol->private_value;
2338 kcontrol->private_value = *c->values;
2339 err = c->ops->get(kcontrol, ucontrol);
2340 kcontrol->private_value = (long)c;
2341 mutex_unlock(&codec->control_mutex);
2344 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2347 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2348 * @kcontrol: ctl element
2349 * @ucontrol: pointer to get/store the data
2351 * The control element is supposed to have the private_value field
2352 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2354 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 struct hda_bind_ctls *c;
2359 unsigned long *vals;
2360 int err = 0, change = 0;
2362 mutex_lock(&codec->control_mutex);
2363 c = (struct hda_bind_ctls *)kcontrol->private_value;
2364 for (vals = c->values; *vals; vals++) {
2365 kcontrol->private_value = *vals;
2366 err = c->ops->put(kcontrol, ucontrol);
2371 kcontrol->private_value = (long)c;
2372 mutex_unlock(&codec->control_mutex);
2373 return err < 0 ? err : change;
2375 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
2378 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2379 * @kcontrol: ctl element
2380 * @op_flag: operation flag
2381 * @size: byte size of input TLV
2384 * The control element is supposed to have the private_value field
2385 * set up via HDA_BIND_VOL() macro.
2387 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2388 unsigned int size, unsigned int __user *tlv)
2390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2391 struct hda_bind_ctls *c;
2394 mutex_lock(&codec->control_mutex);
2395 c = (struct hda_bind_ctls *)kcontrol->private_value;
2396 kcontrol->private_value = *c->values;
2397 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2398 kcontrol->private_value = (long)c;
2399 mutex_unlock(&codec->control_mutex);
2402 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
2404 struct hda_ctl_ops snd_hda_bind_vol = {
2405 .info = snd_hda_mixer_amp_volume_info,
2406 .get = snd_hda_mixer_amp_volume_get,
2407 .put = snd_hda_mixer_amp_volume_put,
2408 .tlv = snd_hda_mixer_amp_tlv
2410 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
2412 struct hda_ctl_ops snd_hda_bind_sw = {
2413 .info = snd_hda_mixer_amp_switch_info,
2414 .get = snd_hda_mixer_amp_switch_get,
2415 .put = snd_hda_mixer_amp_switch_put,
2416 .tlv = snd_hda_mixer_amp_tlv
2418 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
2421 * SPDIF out controls
2424 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_info *uinfo)
2427 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2432 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2435 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2436 IEC958_AES0_NONAUDIO |
2437 IEC958_AES0_CON_EMPHASIS_5015 |
2438 IEC958_AES0_CON_NOT_COPYRIGHT;
2439 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2440 IEC958_AES1_CON_ORIGINAL;
2444 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2447 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2448 IEC958_AES0_NONAUDIO |
2449 IEC958_AES0_PRO_EMPHASIS_5015;
2453 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2457 int idx = kcontrol->private_value;
2458 struct hda_spdif_out *spdif;
2460 mutex_lock(&codec->spdif_mutex);
2461 spdif = snd_array_elem(&codec->spdif_out, idx);
2462 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2463 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2464 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2465 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2466 mutex_unlock(&codec->spdif_mutex);
2471 /* convert from SPDIF status bits to HDA SPDIF bits
2472 * bit 0 (DigEn) is always set zero (to be filled later)
2474 static unsigned short convert_from_spdif_status(unsigned int sbits)
2476 unsigned short val = 0;
2478 if (sbits & IEC958_AES0_PROFESSIONAL)
2479 val |= AC_DIG1_PROFESSIONAL;
2480 if (sbits & IEC958_AES0_NONAUDIO)
2481 val |= AC_DIG1_NONAUDIO;
2482 if (sbits & IEC958_AES0_PROFESSIONAL) {
2483 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2484 IEC958_AES0_PRO_EMPHASIS_5015)
2485 val |= AC_DIG1_EMPHASIS;
2487 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2488 IEC958_AES0_CON_EMPHASIS_5015)
2489 val |= AC_DIG1_EMPHASIS;
2490 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2491 val |= AC_DIG1_COPYRIGHT;
2492 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2493 val |= AC_DIG1_LEVEL;
2494 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2499 /* convert to SPDIF status bits from HDA SPDIF bits
2501 static unsigned int convert_to_spdif_status(unsigned short val)
2503 unsigned int sbits = 0;
2505 if (val & AC_DIG1_NONAUDIO)
2506 sbits |= IEC958_AES0_NONAUDIO;
2507 if (val & AC_DIG1_PROFESSIONAL)
2508 sbits |= IEC958_AES0_PROFESSIONAL;
2509 if (sbits & IEC958_AES0_PROFESSIONAL) {
2510 if (val & AC_DIG1_EMPHASIS)
2511 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2513 if (val & AC_DIG1_EMPHASIS)
2514 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2515 if (!(val & AC_DIG1_COPYRIGHT))
2516 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2517 if (val & AC_DIG1_LEVEL)
2518 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2519 sbits |= val & (0x7f << 8);
2524 /* set digital convert verbs both for the given NID and its slaves */
2525 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2530 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2532 d = codec->slave_dig_outs;
2536 snd_hdac_regmap_update(&codec->core, *d,
2537 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
2540 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2543 unsigned int mask = 0;
2544 unsigned int val = 0;
2554 set_dig_out(codec, nid, mask, val);
2557 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2561 int idx = kcontrol->private_value;
2562 struct hda_spdif_out *spdif;
2567 mutex_lock(&codec->spdif_mutex);
2568 spdif = snd_array_elem(&codec->spdif_out, idx);
2570 spdif->status = ucontrol->value.iec958.status[0] |
2571 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2572 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2573 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2574 val = convert_from_spdif_status(spdif->status);
2575 val |= spdif->ctls & 1;
2576 change = spdif->ctls != val;
2578 if (change && nid != (u16)-1)
2579 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2580 mutex_unlock(&codec->spdif_mutex);
2584 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2586 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2590 int idx = kcontrol->private_value;
2591 struct hda_spdif_out *spdif;
2593 mutex_lock(&codec->spdif_mutex);
2594 spdif = snd_array_elem(&codec->spdif_out, idx);
2595 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2596 mutex_unlock(&codec->spdif_mutex);
2600 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2603 set_dig_out_convert(codec, nid, dig1, dig2);
2604 /* unmute amp switch (if any) */
2605 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2606 (dig1 & AC_DIG1_ENABLE))
2607 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2611 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_value *ucontrol)
2614 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2615 int idx = kcontrol->private_value;
2616 struct hda_spdif_out *spdif;
2621 mutex_lock(&codec->spdif_mutex);
2622 spdif = snd_array_elem(&codec->spdif_out, idx);
2624 val = spdif->ctls & ~AC_DIG1_ENABLE;
2625 if (ucontrol->value.integer.value[0])
2626 val |= AC_DIG1_ENABLE;
2627 change = spdif->ctls != val;
2629 if (change && nid != (u16)-1)
2630 set_spdif_ctls(codec, nid, val & 0xff, -1);
2631 mutex_unlock(&codec->spdif_mutex);
2635 static struct snd_kcontrol_new dig_mixes[] = {
2637 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2639 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_cmask_get,
2644 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2647 .info = snd_hda_spdif_mask_info,
2648 .get = snd_hda_spdif_pmask_get,
2651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2652 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2653 .info = snd_hda_spdif_mask_info,
2654 .get = snd_hda_spdif_default_get,
2655 .put = snd_hda_spdif_default_put,
2658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2659 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2660 .info = snd_hda_spdif_out_switch_info,
2661 .get = snd_hda_spdif_out_switch_get,
2662 .put = snd_hda_spdif_out_switch_put,
2668 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2669 * @codec: the HDA codec
2670 * @associated_nid: NID that new ctls associated with
2671 * @cvt_nid: converter NID
2672 * @type: HDA_PCM_TYPE_*
2673 * Creates controls related with the digital output.
2674 * Called from each patch supporting the digital out.
2676 * Returns 0 if successful, or a negative error code.
2678 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2679 hda_nid_t associated_nid,
2684 struct snd_kcontrol *kctl;
2685 struct snd_kcontrol_new *dig_mix;
2688 const int spdif_index = 16;
2689 struct hda_spdif_out *spdif;
2690 struct hda_bus *bus = codec->bus;
2692 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
2693 type == HDA_PCM_TYPE_SPDIF) {
2695 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
2696 type == HDA_PCM_TYPE_HDMI) {
2697 /* suppose a single SPDIF device */
2698 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2699 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2702 kctl->id.index = spdif_index;
2704 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
2706 if (!bus->primary_dig_out_type)
2707 bus->primary_dig_out_type = type;
2709 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
2711 codec_err(codec, "too many IEC958 outputs\n");
2714 spdif = snd_array_new(&codec->spdif_out);
2717 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2718 kctl = snd_ctl_new1(dig_mix, codec);
2721 kctl->id.index = idx;
2722 kctl->private_value = codec->spdif_out.used - 1;
2723 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2727 spdif->nid = cvt_nid;
2728 snd_hdac_regmap_read(&codec->core, cvt_nid,
2729 AC_VERB_GET_DIGI_CONVERT_1, &val);
2731 spdif->status = convert_to_spdif_status(spdif->ctls);
2734 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
2737 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2738 * @codec: the HDA codec
2741 * call within spdif_mutex lock
2743 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2747 for (i = 0; i < codec->spdif_out.used; i++) {
2748 struct hda_spdif_out *spdif =
2749 snd_array_elem(&codec->spdif_out, i);
2750 if (spdif->nid == nid)
2755 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
2758 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2759 * @codec: the HDA codec
2760 * @idx: the SPDIF ctl index
2762 * Unassign the widget from the given SPDIF control.
2764 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2766 struct hda_spdif_out *spdif;
2768 mutex_lock(&codec->spdif_mutex);
2769 spdif = snd_array_elem(&codec->spdif_out, idx);
2770 spdif->nid = (u16)-1;
2771 mutex_unlock(&codec->spdif_mutex);
2773 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
2776 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2777 * @codec: the HDA codec
2778 * @idx: the SPDIF ctl idx
2781 * Assign the widget to the SPDIF control with the given index.
2783 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2785 struct hda_spdif_out *spdif;
2788 mutex_lock(&codec->spdif_mutex);
2789 spdif = snd_array_elem(&codec->spdif_out, idx);
2790 if (spdif->nid != nid) {
2793 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2795 mutex_unlock(&codec->spdif_mutex);
2797 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
2800 * SPDIF sharing with analog output
2802 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2805 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2806 ucontrol->value.integer.value[0] = mout->share_spdif;
2810 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2813 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2814 mout->share_spdif = !!ucontrol->value.integer.value[0];
2818 static struct snd_kcontrol_new spdif_share_sw = {
2819 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2820 .name = "IEC958 Default PCM Playback Switch",
2821 .info = snd_ctl_boolean_mono_info,
2822 .get = spdif_share_sw_get,
2823 .put = spdif_share_sw_put,
2827 * snd_hda_create_spdif_share_sw - create Default PCM switch
2828 * @codec: the HDA codec
2829 * @mout: multi-out instance
2831 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2832 struct hda_multi_out *mout)
2834 struct snd_kcontrol *kctl;
2836 if (!mout->dig_out_nid)
2839 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2842 /* ATTENTION: here mout is passed as private_data, instead of codec */
2843 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
2845 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
2851 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2853 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2858 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2862 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
2865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2866 hda_nid_t nid = kcontrol->private_value;
2867 unsigned int val = !!ucontrol->value.integer.value[0];
2870 mutex_lock(&codec->spdif_mutex);
2871 change = codec->spdif_in_enable != val;
2873 codec->spdif_in_enable = val;
2874 snd_hdac_regmap_write(&codec->core, nid,
2875 AC_VERB_SET_DIGI_CONVERT_1, val);
2877 mutex_unlock(&codec->spdif_mutex);
2881 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2882 struct snd_ctl_elem_value *ucontrol)
2884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2885 hda_nid_t nid = kcontrol->private_value;
2889 snd_hdac_regmap_read(&codec->core, nid,
2890 AC_VERB_GET_DIGI_CONVERT_1, &val);
2891 sbits = convert_to_spdif_status(val);
2892 ucontrol->value.iec958.status[0] = sbits;
2893 ucontrol->value.iec958.status[1] = sbits >> 8;
2894 ucontrol->value.iec958.status[2] = sbits >> 16;
2895 ucontrol->value.iec958.status[3] = sbits >> 24;
2899 static struct snd_kcontrol_new dig_in_ctls[] = {
2901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2902 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2903 .info = snd_hda_spdif_in_switch_info,
2904 .get = snd_hda_spdif_in_switch_get,
2905 .put = snd_hda_spdif_in_switch_put,
2908 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2910 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2911 .info = snd_hda_spdif_mask_info,
2912 .get = snd_hda_spdif_in_status_get,
2918 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2919 * @codec: the HDA codec
2920 * @nid: audio in widget NID
2922 * Creates controls related with the SPDIF input.
2923 * Called from each patch supporting the SPDIF in.
2925 * Returns 0 if successful, or a negative error code.
2927 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2930 struct snd_kcontrol *kctl;
2931 struct snd_kcontrol_new *dig_mix;
2934 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
2936 codec_err(codec, "too many IEC958 inputs\n");
2939 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2940 kctl = snd_ctl_new1(dig_mix, codec);
2943 kctl->private_value = nid;
2944 err = snd_hda_ctl_add(codec, nid, kctl);
2948 codec->spdif_in_enable =
2949 snd_hda_codec_read(codec, nid, 0,
2950 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2954 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
2957 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2958 * @codec: the HDA codec
2959 * @fg: function group (not used now)
2960 * @power_state: the power state to set (AC_PWRST_*)
2962 * Set the given power state to all widgets that have the power control.
2963 * If the codec has power_filter set, it evaluates the power state and
2964 * filter out if it's unchanged as D3.
2966 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
2967 unsigned int power_state)
2971 for_each_hda_codec_node(nid, codec) {
2972 unsigned int wcaps = get_wcaps(codec, nid);
2973 unsigned int state = power_state;
2974 if (!(wcaps & AC_WCAP_POWER))
2976 if (codec->power_filter) {
2977 state = codec->power_filter(codec, nid, power_state);
2978 if (state != power_state && power_state == AC_PWRST_D3)
2981 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
2985 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
2988 * wait until the state is reached, returns the current state
2990 static unsigned int hda_sync_power_state(struct hda_codec *codec,
2992 unsigned int power_state)
2994 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2995 unsigned int state, actual_state;
2998 state = snd_hda_codec_read(codec, fg, 0,
2999 AC_VERB_GET_POWER_STATE, 0);
3000 if (state & AC_PWRST_ERROR)
3002 actual_state = (state >> 4) & 0x0f;
3003 if (actual_state == power_state)
3005 if (time_after_eq(jiffies, end_time))
3007 /* wait until the codec reachs to the target state */
3014 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3015 * @codec: the HDA codec
3017 * @power_state: power state to evalue
3019 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3020 * This can be used a codec power_filter callback.
3022 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3024 unsigned int power_state)
3026 if (nid == codec->core.afg || nid == codec->core.mfg)
3028 if (power_state == AC_PWRST_D3 &&
3029 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3030 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3031 int eapd = snd_hda_codec_read(codec, nid, 0,
3032 AC_VERB_GET_EAPD_BTLENABLE, 0);
3038 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
3041 * set power state of the codec, and return the power state
3043 static unsigned int hda_set_power_state(struct hda_codec *codec,
3044 unsigned int power_state)
3046 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
3051 /* this delay seems necessary to avoid click noise at power-down */
3052 if (power_state == AC_PWRST_D3) {
3053 if (codec->depop_delay < 0)
3054 msleep(codec_has_epss(codec) ? 10 : 100);
3055 else if (codec->depop_delay > 0)
3056 msleep(codec->depop_delay);
3057 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3060 /* repeat power states setting at most 10 times*/
3061 for (count = 0; count < 10; count++) {
3062 if (codec->patch_ops.set_power_state)
3063 codec->patch_ops.set_power_state(codec, fg,
3066 state = power_state;
3067 if (codec->power_filter)
3068 state = codec->power_filter(codec, fg, state);
3069 if (state == power_state || power_state != AC_PWRST_D3)
3070 snd_hda_codec_read(codec, fg, flags,
3071 AC_VERB_SET_POWER_STATE,
3073 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3075 state = hda_sync_power_state(codec, fg, power_state);
3076 if (!(state & AC_PWRST_ERROR))
3083 /* sync power states of all widgets;
3084 * this is called at the end of codec parsing
3086 static void sync_power_up_states(struct hda_codec *codec)
3090 /* don't care if no filter is used */
3091 if (!codec->power_filter)
3094 for_each_hda_codec_node(nid, codec) {
3095 unsigned int wcaps = get_wcaps(codec, nid);
3096 unsigned int target;
3097 if (!(wcaps & AC_WCAP_POWER))
3099 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3100 if (target == AC_PWRST_D0)
3102 if (!snd_hda_check_power_state(codec, nid, target))
3103 snd_hda_codec_write(codec, nid, 0,
3104 AC_VERB_SET_POWER_STATE, target);
3108 #ifdef CONFIG_SND_HDA_RECONFIG
3109 /* execute additional init verbs */
3110 static void hda_exec_init_verbs(struct hda_codec *codec)
3112 if (codec->init_verbs.list)
3113 snd_hda_sequence_write(codec, codec->init_verbs.list);
3116 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3120 /* update the power on/off account with the current jiffies */
3121 static void update_power_acct(struct hda_codec *codec, bool on)
3123 unsigned long delta = jiffies - codec->power_jiffies;
3126 codec->power_on_acct += delta;
3128 codec->power_off_acct += delta;
3129 codec->power_jiffies += delta;
3132 void snd_hda_update_power_acct(struct hda_codec *codec)
3134 update_power_acct(codec, hda_codec_is_power_on(codec));
3138 * call suspend and power-down; used both from PM and power-save
3139 * this function returns the power state in the end
3141 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3145 atomic_inc(&codec->core.in_pm);
3147 if (codec->patch_ops.suspend)
3148 codec->patch_ops.suspend(codec);
3149 hda_cleanup_all_streams(codec);
3150 state = hda_set_power_state(codec, AC_PWRST_D3);
3151 update_power_acct(codec, true);
3152 atomic_dec(&codec->core.in_pm);
3157 * kick up codec; used both from PM and power-save
3159 static void hda_call_codec_resume(struct hda_codec *codec)
3161 atomic_inc(&codec->core.in_pm);
3163 if (codec->core.regmap)
3164 regcache_mark_dirty(codec->core.regmap);
3166 codec->power_jiffies = jiffies;
3168 hda_set_power_state(codec, AC_PWRST_D0);
3169 restore_shutup_pins(codec);
3170 hda_exec_init_verbs(codec);
3171 snd_hda_jack_set_dirty_all(codec);
3172 if (codec->patch_ops.resume)
3173 codec->patch_ops.resume(codec);
3175 if (codec->patch_ops.init)
3176 codec->patch_ops.init(codec);
3177 if (codec->core.regmap)
3178 regcache_sync(codec->core.regmap);
3181 if (codec->jackpoll_interval)
3182 hda_jackpoll_work(&codec->jackpoll_work.work);
3184 snd_hda_jack_report_sync(codec);
3185 atomic_dec(&codec->core.in_pm);
3188 static int hda_codec_runtime_suspend(struct device *dev)
3190 struct hda_codec *codec = dev_to_hda_codec(dev);
3191 struct hda_pcm *pcm;
3194 cancel_delayed_work_sync(&codec->jackpoll_work);
3195 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3196 snd_pcm_suspend_all(pcm->pcm);
3197 state = hda_call_codec_suspend(codec);
3198 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3199 (state & AC_PWRST_CLK_STOP_OK))
3200 snd_hdac_codec_link_down(&codec->core);
3204 static int hda_codec_runtime_resume(struct device *dev)
3206 struct hda_codec *codec = dev_to_hda_codec(dev);
3208 snd_hdac_codec_link_up(&codec->core);
3209 hda_call_codec_resume(codec);
3210 pm_runtime_mark_last_busy(dev);
3213 #endif /* CONFIG_PM */
3215 /* referred in hda_bind.c */
3216 const struct dev_pm_ops hda_codec_driver_pm = {
3217 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3218 pm_runtime_force_resume)
3219 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3224 * add standard channel maps if not specified
3226 static int add_std_chmaps(struct hda_codec *codec)
3228 struct hda_pcm *pcm;
3231 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
3232 for (str = 0; str < 2; str++) {
3233 struct hda_pcm_stream *hinfo = &pcm->stream[str];
3234 struct snd_pcm_chmap *chmap;
3235 const struct snd_pcm_chmap_elem *elem;
3237 if (!pcm || pcm->own_chmap ||
3240 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3241 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
3242 hinfo->channels_max,
3246 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3252 /* default channel maps for 2.1 speakers;
3253 * since HD-audio supports only stereo, odd number channels are omitted
3255 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3257 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3259 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3260 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3263 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3265 int snd_hda_codec_build_controls(struct hda_codec *codec)
3268 hda_exec_init_verbs(codec);
3269 /* continue to initialize... */
3270 if (codec->patch_ops.init)
3271 err = codec->patch_ops.init(codec);
3272 if (!err && codec->patch_ops.build_controls)
3273 err = codec->patch_ops.build_controls(codec);
3277 /* we create chmaps here instead of build_pcms */
3278 err = add_std_chmaps(codec);
3282 if (codec->jackpoll_interval)
3283 hda_jackpoll_work(&codec->jackpoll_work.work);
3285 snd_hda_jack_report_sync(codec); /* call at the last init point */
3286 sync_power_up_states(codec);
3293 struct hda_rate_tbl {
3295 unsigned int alsa_bits;
3296 unsigned int hda_fmt;
3299 /* rate = base * mult / div */
3300 #define HDA_RATE(base, mult, div) \
3301 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3302 (((div) - 1) << AC_FMT_DIV_SHIFT))
3304 static struct hda_rate_tbl rate_bits[] = {
3305 /* rate in Hz, ALSA rate bitmask, HDA format value */
3307 /* autodetected value used in snd_hda_query_supported_pcm */
3308 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3309 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3310 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3311 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3312 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3313 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3314 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3315 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3316 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3317 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3318 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3319 #define AC_PAR_PCM_RATE_BITS 11
3320 /* up to bits 10, 384kHZ isn't supported properly */
3322 /* not autodetected value */
3323 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3325 { 0 } /* terminator */
3329 * snd_hda_calc_stream_format - calculate format bitset
3330 * @codec: HD-audio codec
3331 * @rate: the sample rate
3332 * @channels: the number of channels
3333 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3334 * @maxbps: the max. bps
3335 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
3337 * Calculate the format bitset from the given rate, channels and th PCM format.
3339 * Return zero if invalid.
3341 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
3343 unsigned int channels,
3344 unsigned int format,
3345 unsigned int maxbps,
3346 unsigned short spdif_ctls)
3349 unsigned int val = 0;
3351 for (i = 0; rate_bits[i].hz; i++)
3352 if (rate_bits[i].hz == rate) {
3353 val = rate_bits[i].hda_fmt;
3356 if (!rate_bits[i].hz) {
3357 codec_dbg(codec, "invalid rate %d\n", rate);
3361 if (channels == 0 || channels > 8) {
3362 codec_dbg(codec, "invalid channels %d\n", channels);
3365 val |= channels - 1;
3367 switch (snd_pcm_format_width(format)) {
3369 val |= AC_FMT_BITS_8;
3372 val |= AC_FMT_BITS_16;
3377 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3378 val |= AC_FMT_BITS_32;
3379 else if (maxbps >= 24)
3380 val |= AC_FMT_BITS_24;
3382 val |= AC_FMT_BITS_20;
3385 codec_dbg(codec, "invalid format width %d\n",
3386 snd_pcm_format_width(format));
3390 if (spdif_ctls & AC_DIG1_NONAUDIO)
3391 val |= AC_FMT_TYPE_NON_PCM;
3395 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
3397 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3399 unsigned int val = 0;
3400 if (nid != codec->core.afg &&
3401 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3402 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3403 if (!val || val == -1)
3404 val = snd_hda_param_read(codec, codec->core.afg, AC_PAR_PCM);
3405 if (!val || val == -1)
3410 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3412 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3413 if (!streams || streams == -1)
3414 streams = snd_hda_param_read(codec, codec->core.afg, AC_PAR_STREAM);
3415 if (!streams || streams == -1)
3421 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3422 * @codec: the HDA codec
3423 * @nid: NID to query
3424 * @ratesp: the pointer to store the detected rate bitflags
3425 * @formatsp: the pointer to store the detected formats
3426 * @bpsp: the pointer to store the detected format widths
3428 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3429 * or @bsps argument is ignored.
3431 * Returns 0 if successful, otherwise a negative error code.
3433 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3434 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3436 unsigned int i, val, wcaps;
3438 wcaps = get_wcaps(codec, nid);
3439 val = query_pcm_param(codec, nid);
3443 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3445 rates |= rate_bits[i].alsa_bits;
3449 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3451 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3457 if (formatsp || bpsp) {
3459 unsigned int streams, bps;
3461 streams = query_stream_param(codec, nid);
3466 if (streams & AC_SUPFMT_PCM) {
3467 if (val & AC_SUPPCM_BITS_8) {
3468 formats |= SNDRV_PCM_FMTBIT_U8;
3471 if (val & AC_SUPPCM_BITS_16) {
3472 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3475 if (wcaps & AC_WCAP_DIGITAL) {
3476 if (val & AC_SUPPCM_BITS_32)
3477 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3478 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3479 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3480 if (val & AC_SUPPCM_BITS_24)
3482 else if (val & AC_SUPPCM_BITS_20)
3484 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3485 AC_SUPPCM_BITS_32)) {
3486 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3487 if (val & AC_SUPPCM_BITS_32)
3489 else if (val & AC_SUPPCM_BITS_24)
3491 else if (val & AC_SUPPCM_BITS_20)
3495 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
3496 if (streams & AC_SUPFMT_FLOAT32) {
3497 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3502 if (streams == AC_SUPFMT_AC3) {
3503 /* should be exclusive */
3504 /* temporary hack: we have still no proper support
3505 * for the direct AC3 stream...
3507 formats |= SNDRV_PCM_FMTBIT_U8;
3512 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3514 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3519 *formatsp = formats;
3526 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
3529 * snd_hda_is_supported_format - Check the validity of the format
3530 * @codec: HD-audio codec
3531 * @nid: NID to check
3532 * @format: the HD-audio format value to check
3534 * Check whether the given node supports the format value.
3536 * Returns 1 if supported, 0 if not.
3538 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3539 unsigned int format)
3542 unsigned int val = 0, rate, stream;
3544 val = query_pcm_param(codec, nid);
3548 rate = format & 0xff00;
3549 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3550 if (rate_bits[i].hda_fmt == rate) {
3555 if (i >= AC_PAR_PCM_RATE_BITS)
3558 stream = query_stream_param(codec, nid);
3562 if (stream & AC_SUPFMT_PCM) {
3563 switch (format & 0xf0) {
3565 if (!(val & AC_SUPPCM_BITS_8))
3569 if (!(val & AC_SUPPCM_BITS_16))
3573 if (!(val & AC_SUPPCM_BITS_20))
3577 if (!(val & AC_SUPPCM_BITS_24))
3581 if (!(val & AC_SUPPCM_BITS_32))
3588 /* FIXME: check for float32 and AC3? */
3593 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
3598 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3599 struct hda_codec *codec,
3600 struct snd_pcm_substream *substream)
3605 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3606 struct hda_codec *codec,
3607 unsigned int stream_tag,
3608 unsigned int format,
3609 struct snd_pcm_substream *substream)
3611 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3615 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3616 struct hda_codec *codec,
3617 struct snd_pcm_substream *substream)
3619 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3623 static int set_pcm_default_values(struct hda_codec *codec,
3624 struct hda_pcm_stream *info)
3628 /* query support PCM information from the given NID */
3629 if (info->nid && (!info->rates || !info->formats)) {
3630 err = snd_hda_query_supported_pcm(codec, info->nid,
3631 info->rates ? NULL : &info->rates,
3632 info->formats ? NULL : &info->formats,
3633 info->maxbps ? NULL : &info->maxbps);
3637 if (info->ops.open == NULL)
3638 info->ops.open = hda_pcm_default_open_close;
3639 if (info->ops.close == NULL)
3640 info->ops.close = hda_pcm_default_open_close;
3641 if (info->ops.prepare == NULL) {
3642 if (snd_BUG_ON(!info->nid))
3644 info->ops.prepare = hda_pcm_default_prepare;
3646 if (info->ops.cleanup == NULL) {
3647 if (snd_BUG_ON(!info->nid))
3649 info->ops.cleanup = hda_pcm_default_cleanup;
3655 * codec prepare/cleanup entries
3658 * snd_hda_codec_prepare - Prepare a stream
3659 * @codec: the HDA codec
3660 * @hinfo: PCM information
3661 * @stream: stream tag to assign
3662 * @format: format id to assign
3663 * @substream: PCM substream to assign
3665 * Calls the prepare callback set by the codec with the given arguments.
3666 * Clean up the inactive streams when successful.
3668 int snd_hda_codec_prepare(struct hda_codec *codec,
3669 struct hda_pcm_stream *hinfo,
3670 unsigned int stream,
3671 unsigned int format,
3672 struct snd_pcm_substream *substream)
3675 mutex_lock(&codec->bus->prepare_mutex);
3676 if (hinfo->ops.prepare)
3677 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3682 purify_inactive_streams(codec);
3683 mutex_unlock(&codec->bus->prepare_mutex);
3686 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
3689 * snd_hda_codec_cleanup - Prepare a stream
3690 * @codec: the HDA codec
3691 * @hinfo: PCM information
3692 * @substream: PCM substream
3694 * Calls the cleanup callback set by the codec with the given arguments.
3696 void snd_hda_codec_cleanup(struct hda_codec *codec,
3697 struct hda_pcm_stream *hinfo,
3698 struct snd_pcm_substream *substream)
3700 mutex_lock(&codec->bus->prepare_mutex);
3701 if (hinfo->ops.cleanup)
3702 hinfo->ops.cleanup(hinfo, codec, substream);
3703 mutex_unlock(&codec->bus->prepare_mutex);
3705 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
3708 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3709 "Audio", "SPDIF", "HDMI", "Modem"
3713 * get the empty PCM device number to assign
3715 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
3717 /* audio device indices; not linear to keep compatibility */
3718 /* assigned to static slots up to dev#10; if more needed, assign
3719 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3721 static int audio_idx[HDA_PCM_NTYPES][5] = {
3722 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3723 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3724 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3725 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3729 if (type >= HDA_PCM_NTYPES) {
3730 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
3734 for (i = 0; audio_idx[type][i] >= 0; i++) {
3735 #ifndef CONFIG_SND_DYNAMIC_MINORS
3736 if (audio_idx[type][i] >= 8)
3739 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3740 return audio_idx[type][i];
3743 #ifdef CONFIG_SND_DYNAMIC_MINORS
3744 /* non-fixed slots starting from 10 */
3745 for (i = 10; i < 32; i++) {
3746 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3751 dev_warn(bus->card->dev, "Too many %s devices\n",
3752 snd_hda_pcm_type_name[type]);
3753 #ifndef CONFIG_SND_DYNAMIC_MINORS
3754 dev_warn(bus->card->dev,
3755 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
3760 /* call build_pcms ops of the given codec and set up the default parameters */
3761 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
3763 struct hda_pcm *cpcm;
3766 if (!list_empty(&codec->pcm_list_head))
3767 return 0; /* already parsed */
3769 if (!codec->patch_ops.build_pcms)
3772 err = codec->patch_ops.build_pcms(codec);
3774 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
3775 codec->core.addr, err);
3779 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3782 for (stream = 0; stream < 2; stream++) {
3783 struct hda_pcm_stream *info = &cpcm->stream[stream];
3785 if (!info->substreams)
3787 err = set_pcm_default_values(codec, info);
3790 "fail to setup default for PCM %s\n",
3800 /* assign all PCMs of the given codec */
3801 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3803 struct hda_bus *bus = codec->bus;
3804 struct hda_pcm *cpcm;
3807 if (snd_BUG_ON(!bus->ops.attach_pcm))
3810 err = snd_hda_codec_parse_pcms(codec);
3812 snd_hda_codec_reset(codec);
3816 /* attach a new PCM streams */
3817 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
3819 continue; /* already attached */
3820 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3821 continue; /* no substreams assigned */
3823 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3825 continue; /* no fatal error */
3827 err = bus->ops.attach_pcm(bus, codec, cpcm);
3830 "cannot attach PCM stream %d for codec #%d\n",
3831 dev, codec->core.addr);
3832 continue; /* no fatal error */
3840 * snd_hda_add_new_ctls - create controls from the array
3841 * @codec: the HDA codec
3842 * @knew: the array of struct snd_kcontrol_new
3844 * This helper function creates and add new controls in the given array.
3845 * The array must be terminated with an empty entry as terminator.
3847 * Returns 0 if successful, or a negative error code.
3849 int snd_hda_add_new_ctls(struct hda_codec *codec,
3850 const struct snd_kcontrol_new *knew)
3854 for (; knew->name; knew++) {
3855 struct snd_kcontrol *kctl;
3856 int addr = 0, idx = 0;
3857 if (knew->iface == -1) /* skip this codec private value */
3860 kctl = snd_ctl_new1(knew, codec);
3864 kctl->id.device = addr;
3866 kctl->id.index = idx;
3867 err = snd_hda_ctl_add(codec, 0, kctl);
3870 /* try first with another device index corresponding to
3871 * the codec addr; if it still fails (or it's the
3872 * primary codec), then try another control index
3874 if (!addr && codec->core.addr)
3875 addr = codec->core.addr;
3876 else if (!idx && !knew->index) {
3877 idx = find_empty_mixer_ctl_idx(codec,
3887 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
3890 static void codec_set_power_save(struct hda_codec *codec, int delay)
3892 struct device *dev = hda_codec_dev(codec);
3895 pm_runtime_set_autosuspend_delay(dev, delay);
3896 pm_runtime_use_autosuspend(dev);
3897 pm_runtime_allow(dev);
3898 if (!pm_runtime_suspended(dev))
3899 pm_runtime_mark_last_busy(dev);
3901 pm_runtime_dont_use_autosuspend(dev);
3902 pm_runtime_forbid(dev);
3907 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3908 * @bus: HD-audio bus
3909 * @delay: autosuspend delay in msec, 0 = off
3911 * Synchronize the runtime PM autosuspend state from the power_save option.
3913 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3915 struct hda_codec *c;
3917 list_for_each_codec(c, bus)
3918 codec_set_power_save(c, delay);
3920 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
3923 * snd_hda_check_amp_list_power - Check the amp list and update the power
3924 * @codec: HD-audio codec
3925 * @check: the object containing an AMP list and the status
3926 * @nid: NID to check / update
3928 * Check whether the given NID is in the amp list. If it's in the list,
3929 * check the current AMP status, and update the the power-status according
3930 * to the mute status.
3932 * This function is supposed to be set or called from the check_power_status
3935 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3936 struct hda_loopback_check *check,
3939 const struct hda_amp_list *p;
3942 if (!check->amplist)
3944 for (p = check->amplist; p->nid; p++) {
3949 return 0; /* nothing changed */
3951 for (p = check->amplist; p->nid; p++) {
3952 for (ch = 0; ch < 2; ch++) {
3953 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3955 if (!(v & HDA_AMP_MUTE) && v > 0) {
3956 if (!check->power_on) {
3957 check->power_on = 1;
3958 snd_hda_power_up_pm(codec);
3964 if (check->power_on) {
3965 check->power_on = 0;
3966 snd_hda_power_down_pm(codec);
3970 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
3978 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3979 * @imux: imux helper object
3980 * @uinfo: pointer to get/store the data
3982 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3983 struct snd_ctl_elem_info *uinfo)
3987 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3989 uinfo->value.enumerated.items = imux->num_items;
3990 if (!imux->num_items)
3992 index = uinfo->value.enumerated.item;
3993 if (index >= imux->num_items)
3994 index = imux->num_items - 1;
3995 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3998 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
4001 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4002 * @codec: the HDA codec
4003 * @imux: imux helper object
4004 * @ucontrol: pointer to get/store the data
4005 * @nid: input mux NID
4006 * @cur_val: pointer to get/store the current imux value
4008 int snd_hda_input_mux_put(struct hda_codec *codec,
4009 const struct hda_input_mux *imux,
4010 struct snd_ctl_elem_value *ucontrol,
4012 unsigned int *cur_val)
4016 if (!imux->num_items)
4018 idx = ucontrol->value.enumerated.item[0];
4019 if (idx >= imux->num_items)
4020 idx = imux->num_items - 1;
4021 if (*cur_val == idx)
4023 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4024 imux->items[idx].index);
4028 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
4032 * snd_hda_enum_helper_info - Helper for simple enum ctls
4033 * @kcontrol: ctl element
4034 * @uinfo: pointer to get/store the data
4035 * @num_items: number of enum items
4036 * @texts: enum item string array
4038 * process kcontrol info callback of a simple string enum array
4039 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4041 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4042 struct snd_ctl_elem_info *uinfo,
4043 int num_items, const char * const *texts)
4045 static const char * const texts_default[] = {
4046 "Disabled", "Enabled"
4049 if (!texts || !num_items) {
4051 texts = texts_default;
4054 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
4056 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
4059 * Multi-channel / digital-out PCM helper functions
4062 /* setup SPDIF output stream */
4063 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4064 unsigned int stream_tag, unsigned int format)
4066 struct hda_spdif_out *spdif;
4067 unsigned int curr_fmt;
4070 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4071 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4072 AC_VERB_GET_STREAM_FORMAT, 0);
4073 reset = codec->spdif_status_reset &&
4074 (spdif->ctls & AC_DIG1_ENABLE) &&
4077 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4080 set_dig_out_convert(codec, nid,
4081 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4083 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4084 if (codec->slave_dig_outs) {
4086 for (d = codec->slave_dig_outs; *d; d++)
4087 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4090 /* turn on again (if needed) */
4092 set_dig_out_convert(codec, nid,
4093 spdif->ctls & 0xff, -1);
4096 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4098 snd_hda_codec_cleanup_stream(codec, nid);
4099 if (codec->slave_dig_outs) {
4101 for (d = codec->slave_dig_outs; *d; d++)
4102 snd_hda_codec_cleanup_stream(codec, *d);
4107 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4108 * @codec: the HDA codec
4109 * @mout: hda_multi_out object
4111 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4112 struct hda_multi_out *mout)
4114 mutex_lock(&codec->spdif_mutex);
4115 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4116 /* already opened as analog dup; reset it once */
4117 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4118 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4119 mutex_unlock(&codec->spdif_mutex);
4122 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
4125 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4126 * @codec: the HDA codec
4127 * @mout: hda_multi_out object
4128 * @stream_tag: stream tag to assign
4129 * @format: format id to assign
4130 * @substream: PCM substream to assign
4132 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4133 struct hda_multi_out *mout,
4134 unsigned int stream_tag,
4135 unsigned int format,
4136 struct snd_pcm_substream *substream)
4138 mutex_lock(&codec->spdif_mutex);
4139 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4140 mutex_unlock(&codec->spdif_mutex);
4143 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
4146 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4147 * @codec: the HDA codec
4148 * @mout: hda_multi_out object
4150 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4151 struct hda_multi_out *mout)
4153 mutex_lock(&codec->spdif_mutex);
4154 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4155 mutex_unlock(&codec->spdif_mutex);
4158 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
4161 * snd_hda_multi_out_dig_close - release the digital out stream
4162 * @codec: the HDA codec
4163 * @mout: hda_multi_out object
4165 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4166 struct hda_multi_out *mout)
4168 mutex_lock(&codec->spdif_mutex);
4169 mout->dig_out_used = 0;
4170 mutex_unlock(&codec->spdif_mutex);
4173 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
4176 * snd_hda_multi_out_analog_open - open analog outputs
4177 * @codec: the HDA codec
4178 * @mout: hda_multi_out object
4179 * @substream: PCM substream to assign
4180 * @hinfo: PCM information to assign
4182 * Open analog outputs and set up the hw-constraints.
4183 * If the digital outputs can be opened as slave, open the digital
4186 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4187 struct hda_multi_out *mout,
4188 struct snd_pcm_substream *substream,
4189 struct hda_pcm_stream *hinfo)
4191 struct snd_pcm_runtime *runtime = substream->runtime;
4192 runtime->hw.channels_max = mout->max_channels;
4193 if (mout->dig_out_nid) {
4194 if (!mout->analog_rates) {
4195 mout->analog_rates = hinfo->rates;
4196 mout->analog_formats = hinfo->formats;
4197 mout->analog_maxbps = hinfo->maxbps;
4199 runtime->hw.rates = mout->analog_rates;
4200 runtime->hw.formats = mout->analog_formats;
4201 hinfo->maxbps = mout->analog_maxbps;
4203 if (!mout->spdif_rates) {
4204 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4206 &mout->spdif_formats,
4207 &mout->spdif_maxbps);
4209 mutex_lock(&codec->spdif_mutex);
4210 if (mout->share_spdif) {
4211 if ((runtime->hw.rates & mout->spdif_rates) &&
4212 (runtime->hw.formats & mout->spdif_formats)) {
4213 runtime->hw.rates &= mout->spdif_rates;
4214 runtime->hw.formats &= mout->spdif_formats;
4215 if (mout->spdif_maxbps < hinfo->maxbps)
4216 hinfo->maxbps = mout->spdif_maxbps;
4218 mout->share_spdif = 0;
4219 /* FIXME: need notify? */
4222 mutex_unlock(&codec->spdif_mutex);
4224 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4225 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4227 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
4230 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4231 * @codec: the HDA codec
4232 * @mout: hda_multi_out object
4233 * @stream_tag: stream tag to assign
4234 * @format: format id to assign
4235 * @substream: PCM substream to assign
4237 * Set up the i/o for analog out.
4238 * When the digital out is available, copy the front out to digital out, too.
4240 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4241 struct hda_multi_out *mout,
4242 unsigned int stream_tag,
4243 unsigned int format,
4244 struct snd_pcm_substream *substream)
4246 const hda_nid_t *nids = mout->dac_nids;
4247 int chs = substream->runtime->channels;
4248 struct hda_spdif_out *spdif;
4251 mutex_lock(&codec->spdif_mutex);
4252 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
4253 if (mout->dig_out_nid && mout->share_spdif &&
4254 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4256 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4258 !(spdif->status & IEC958_AES0_NONAUDIO)) {
4259 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4260 setup_dig_out_stream(codec, mout->dig_out_nid,
4261 stream_tag, format);
4263 mout->dig_out_used = 0;
4264 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4267 mutex_unlock(&codec->spdif_mutex);
4270 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4272 if (!mout->no_share_stream &&
4273 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4274 /* headphone out will just decode front left/right (stereo) */
4275 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4277 /* extra outputs copied from front */
4278 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4279 if (!mout->no_share_stream && mout->hp_out_nid[i])
4280 snd_hda_codec_setup_stream(codec,
4281 mout->hp_out_nid[i],
4282 stream_tag, 0, format);
4285 for (i = 1; i < mout->num_dacs; i++) {
4286 if (chs >= (i + 1) * 2) /* independent out */
4287 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4289 else if (!mout->no_share_stream) /* copy front */
4290 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4294 /* extra surrounds */
4295 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
4297 if (!mout->extra_out_nid[i])
4299 if (chs >= (i + 1) * 2)
4301 else if (!mout->no_share_stream)
4303 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
4304 stream_tag, ch, format);
4309 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
4312 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4313 * @codec: the HDA codec
4314 * @mout: hda_multi_out object
4316 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4317 struct hda_multi_out *mout)
4319 const hda_nid_t *nids = mout->dac_nids;
4322 for (i = 0; i < mout->num_dacs; i++)
4323 snd_hda_codec_cleanup_stream(codec, nids[i]);
4325 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4326 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4327 if (mout->hp_out_nid[i])
4328 snd_hda_codec_cleanup_stream(codec,
4329 mout->hp_out_nid[i]);
4330 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4331 if (mout->extra_out_nid[i])
4332 snd_hda_codec_cleanup_stream(codec,
4333 mout->extra_out_nid[i]);
4334 mutex_lock(&codec->spdif_mutex);
4335 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4336 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4337 mout->dig_out_used = 0;
4339 mutex_unlock(&codec->spdif_mutex);
4342 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
4345 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4346 * @codec: the HDA codec
4347 * @pin: referred pin NID
4349 * Guess the suitable VREF pin bits to be set as the pin-control value.
4350 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4352 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
4354 unsigned int pincap;
4355 unsigned int oldval;
4356 oldval = snd_hda_codec_read(codec, pin, 0,
4357 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4358 pincap = snd_hda_query_pin_caps(codec, pin);
4359 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4360 /* Exception: if the default pin setup is vref50, we give it priority */
4361 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
4362 return AC_PINCTL_VREF_80;
4363 else if (pincap & AC_PINCAP_VREF_50)
4364 return AC_PINCTL_VREF_50;
4365 else if (pincap & AC_PINCAP_VREF_100)
4366 return AC_PINCTL_VREF_100;
4367 else if (pincap & AC_PINCAP_VREF_GRD)
4368 return AC_PINCTL_VREF_GRD;
4369 return AC_PINCTL_VREF_HIZ;
4371 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
4374 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4375 * @codec: the HDA codec
4376 * @pin: referred pin NID
4377 * @val: pin ctl value to audit
4379 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
4380 hda_nid_t pin, unsigned int val)
4382 static unsigned int cap_lists[][2] = {
4383 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
4384 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
4385 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
4386 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
4392 cap = snd_hda_query_pin_caps(codec, pin);
4394 return val; /* don't know what to do... */
4396 if (val & AC_PINCTL_OUT_EN) {
4397 if (!(cap & AC_PINCAP_OUT))
4398 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
4399 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
4400 val &= ~AC_PINCTL_HP_EN;
4403 if (val & AC_PINCTL_IN_EN) {
4404 if (!(cap & AC_PINCAP_IN))
4405 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
4407 unsigned int vcap, vref;
4409 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4410 vref = val & AC_PINCTL_VREFEN;
4411 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4412 if (vref == cap_lists[i][0] &&
4413 !(vcap & cap_lists[i][1])) {
4414 if (i == ARRAY_SIZE(cap_lists) - 1)
4415 vref = AC_PINCTL_VREF_HIZ;
4417 vref = cap_lists[i + 1][0];
4420 val &= ~AC_PINCTL_VREFEN;
4427 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
4430 * _snd_hda_pin_ctl - Helper to set pin ctl value
4431 * @codec: the HDA codec
4432 * @pin: referred pin NID
4433 * @val: pin control value to set
4434 * @cached: access over codec pinctl cache or direct write
4436 * This function is a helper to set a pin ctl value more safely.
4437 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4438 * value in pin target array via snd_hda_codec_set_pin_target(), then
4439 * actually writes the value via either snd_hda_codec_update_cache() or
4440 * snd_hda_codec_write() depending on @cached flag.
4442 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4443 unsigned int val, bool cached)
4445 val = snd_hda_correct_pin_ctl(codec, pin, val);
4446 snd_hda_codec_set_pin_target(codec, pin, val);
4448 return snd_hda_codec_update_cache(codec, pin, 0,
4449 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4451 return snd_hda_codec_write(codec, pin, 0,
4452 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4454 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
4457 * snd_hda_add_imux_item - Add an item to input_mux
4458 * @codec: the HDA codec
4459 * @imux: imux helper object
4460 * @label: the name of imux item to assign
4461 * @index: index number of imux item to assign
4462 * @type_idx: pointer to store the resultant label index
4464 * When the same label is used already in the existing items, the number
4465 * suffix is appended to the label. This label index number is stored
4466 * to type_idx when non-NULL pointer is given.
4468 int snd_hda_add_imux_item(struct hda_codec *codec,
4469 struct hda_input_mux *imux, const char *label,
4470 int index, int *type_idx)
4472 int i, label_idx = 0;
4473 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4474 codec_err(codec, "hda_codec: Too many imux items!\n");
4477 for (i = 0; i < imux->num_items; i++) {
4478 if (!strncmp(label, imux->items[i].label, strlen(label)))
4482 *type_idx = label_idx;
4484 snprintf(imux->items[imux->num_items].label,
4485 sizeof(imux->items[imux->num_items].label),
4486 "%s %d", label, label_idx);
4488 strlcpy(imux->items[imux->num_items].label, label,
4489 sizeof(imux->items[imux->num_items].label));
4490 imux->items[imux->num_items].index = index;
4494 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
4497 * snd_hda_bus_reset - Reset the bus
4498 * @bus: HD-audio bus
4500 void snd_hda_bus_reset(struct hda_bus *bus)
4502 struct hda_codec *codec;
4504 list_for_each_codec(codec, bus) {
4505 /* FIXME: maybe a better way needed for forced reset */
4506 cancel_delayed_work_sync(&codec->jackpoll_work);
4508 if (hda_codec_is_power_on(codec)) {
4509 hda_call_codec_suspend(codec);
4510 hda_call_codec_resume(codec);
4515 EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
4518 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4519 * @pcm: PCM caps bits
4520 * @buf: the string buffer to write
4521 * @buflen: the max buffer length
4523 * used by hda_proc.c and hda_eld.c
4525 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4527 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4530 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4531 if (pcm & (AC_SUPPCM_BITS_8 << i))
4532 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4534 buf[j] = '\0'; /* necessary when j == 0 */
4536 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
4538 MODULE_DESCRIPTION("HDA codec core");
4539 MODULE_LICENSE("GPL");