2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
43 #define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
44 #define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
47 #define codec_in_pm(codec) 0
48 #define hda_codec_is_power_on(codec) 1
51 #define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53 #define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
57 * snd_hda_get_jack_location - Give a location string of the jack
58 * @cfg: pin default config value
60 * Parse the pin default config value and returns the string of the
61 * jack location, e.g. "Rear", "Front", etc.
63 const char *snd_hda_get_jack_location(u32 cfg
)
65 static char *bases
[7] = {
66 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
68 static unsigned char specials_idx
[] = {
73 static char *specials
[] = {
74 "Rear Panel", "Drive Bar",
75 "Riser", "HDMI", "ATAPI",
76 "Mobile-In", "Mobile-Out"
79 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
81 return bases
[cfg
& 0x0f];
82 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
83 if (cfg
== specials_idx
[i
])
88 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location
);
91 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
92 * @cfg: pin default config value
94 * Parse the pin default config value and returns the string of the
95 * jack connectivity, i.e. external or internal connection.
97 const char *snd_hda_get_jack_connectivity(u32 cfg
)
99 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
101 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
103 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity
);
106 * snd_hda_get_jack_type - Give a type string of the jack
107 * @cfg: pin default config value
109 * Parse the pin default config value and returns the string of the
110 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
112 const char *snd_hda_get_jack_type(u32 cfg
)
114 static char *jack_types
[16] = {
115 "Line Out", "Speaker", "HP Out", "CD",
116 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
117 "Line In", "Aux", "Mic", "Telephony",
118 "SPDIF In", "Digital In", "Reserved", "Other"
121 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
122 >> AC_DEFCFG_DEVICE_SHIFT
];
124 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type
);
127 * Send and receive a verb - passed to exec_verb override for hdac_device
129 static int codec_exec_verb(struct hdac_device
*dev
, unsigned int cmd
,
130 unsigned int flags
, unsigned int *res
)
132 struct hda_codec
*codec
= container_of(dev
, struct hda_codec
, core
);
133 struct hda_bus
*bus
= codec
->bus
;
140 snd_hda_power_up(codec
);
141 mutex_lock(&bus
->core
.cmd_mutex
);
142 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
143 bus
->no_response_fallback
= 1;
144 err
= snd_hdac_bus_exec_verb_unlocked(&bus
->core
, codec
->core
.addr
,
146 bus
->no_response_fallback
= 0;
147 mutex_unlock(&bus
->core
.cmd_mutex
);
148 snd_hda_power_down(codec
);
149 if (!codec_in_pm(codec
) && res
&& err
< 0 && bus
->rirb_error
) {
150 if (bus
->response_reset
) {
152 "resetting BUS due to fatal communication error\n");
153 bus
->ops
.bus_reset(bus
);
157 /* clear reset-flag when the communication gets recovered */
158 if (!err
|| codec_in_pm(codec
))
159 bus
->response_reset
= 0;
164 * snd_hda_codec_read - send a command and get the response
165 * @codec: the HDA codec
166 * @nid: NID to send the command
167 * @flags: optional bit flags
168 * @verb: the verb to send
169 * @parm: the parameter for the verb
171 * Send a single command and read the corresponding response.
173 * Returns the obtained response value, or -1 for an error.
175 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
177 unsigned int verb
, unsigned int parm
)
179 unsigned int cmd
= snd_hdac_make_cmd(&codec
->core
, nid
, verb
, parm
);
181 if (snd_hdac_exec_verb(&codec
->core
, cmd
, flags
, &res
))
185 EXPORT_SYMBOL_GPL(snd_hda_codec_read
);
188 * snd_hda_codec_write - send a single command without waiting for response
189 * @codec: the HDA codec
190 * @nid: NID to send the command
191 * @flags: optional bit flags
192 * @verb: the verb to send
193 * @parm: the parameter for the verb
195 * Send a single command without waiting for response.
197 * Returns 0 if successful, or a negative error code.
199 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
200 unsigned int verb
, unsigned int parm
)
202 unsigned int cmd
= snd_hdac_make_cmd(&codec
->core
, nid
, verb
, parm
);
203 return snd_hdac_exec_verb(&codec
->core
, cmd
, flags
, NULL
);
205 EXPORT_SYMBOL_GPL(snd_hda_codec_write
);
208 * snd_hda_sequence_write - sequence writes
209 * @codec: the HDA codec
210 * @seq: VERB array to send
212 * Send the commands sequentially from the given array.
213 * The array must be terminated with NID=0.
215 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
217 for (; seq
->nid
; seq
++)
218 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
220 EXPORT_SYMBOL_GPL(snd_hda_sequence_write
);
222 /* connection list element */
223 struct hda_conn_list
{
224 struct list_head list
;
230 /* look up the cached results */
231 static struct hda_conn_list
*
232 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
234 struct hda_conn_list
*p
;
235 list_for_each_entry(p
, &codec
->conn_list
, list
) {
242 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
243 const hda_nid_t
*list
)
245 struct hda_conn_list
*p
;
247 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
252 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
253 list_add(&p
->list
, &codec
->conn_list
);
257 static void remove_conn_list(struct hda_codec
*codec
)
259 while (!list_empty(&codec
->conn_list
)) {
260 struct hda_conn_list
*p
;
261 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
267 /* read the connection and add to the cache */
268 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
271 hda_nid_t
*result
= list
;
274 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
275 if (len
== -ENOSPC
) {
276 len
= snd_hda_get_num_raw_conns(codec
, nid
);
277 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
280 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
283 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
290 * snd_hda_get_conn_list - get connection list
291 * @codec: the HDA codec
293 * @listp: the pointer to store NID list
295 * Parses the connection list of the given widget and stores the pointer
296 * to the list of NIDs.
298 * Returns the number of connections, or a negative error code.
300 * Note that the returned pointer isn't protected against the list
301 * modification. If snd_hda_override_conn_list() might be called
302 * concurrently, protect with a mutex appropriately.
304 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
305 const hda_nid_t
**listp
)
311 const struct hda_conn_list
*p
;
313 /* if the connection-list is already cached, read it */
314 p
= lookup_conn_list(codec
, nid
);
320 if (snd_BUG_ON(added
))
323 err
= read_and_add_raw_conns(codec
, nid
);
329 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list
);
332 * snd_hda_get_connections - copy connection list
333 * @codec: the HDA codec
335 * @conn_list: connection list array; when NULL, checks only the size
336 * @max_conns: max. number of connections to store
338 * Parses the connection list of the given widget and stores the list
341 * Returns the number of connections, or a negative error code.
343 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
344 hda_nid_t
*conn_list
, int max_conns
)
346 const hda_nid_t
*list
;
347 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
349 if (len
> 0 && conn_list
) {
350 if (len
> max_conns
) {
351 codec_err(codec
, "Too many connections %d for NID 0x%x\n",
355 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
360 EXPORT_SYMBOL_GPL(snd_hda_get_connections
);
363 * snd_hda_override_conn_list - add/modify the connection-list to cache
364 * @codec: the HDA codec
366 * @len: number of connection list entries
367 * @list: the list of connection entries
369 * Add or modify the given connection-list to the cache. If the corresponding
370 * cache already exists, invalidate it and append a new one.
372 * Returns zero or a negative error code.
374 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
375 const hda_nid_t
*list
)
377 struct hda_conn_list
*p
;
379 p
= lookup_conn_list(codec
, nid
);
385 return add_conn_list(codec
, nid
, len
, list
);
387 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list
);
390 * snd_hda_get_conn_index - get the connection index of the given NID
391 * @codec: the HDA codec
392 * @mux: NID containing the list
393 * @nid: NID to select
394 * @recursive: 1 when searching NID recursively, otherwise 0
396 * Parses the connection list of the widget @mux and checks whether the
397 * widget @nid is present. If it is, return the connection index.
398 * Otherwise it returns -1.
400 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
401 hda_nid_t nid
, int recursive
)
403 const hda_nid_t
*conn
;
406 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
407 for (i
= 0; i
< nums
; i
++)
412 if (recursive
> 10) {
413 codec_dbg(codec
, "too deep connection for 0x%x\n", nid
);
417 for (i
= 0; i
< nums
; i
++) {
418 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
419 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
421 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
426 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index
);
429 /* return DEVLIST_LEN parameter of the given widget */
430 static unsigned int get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
432 unsigned int wcaps
= get_wcaps(codec
, nid
);
435 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
436 get_wcaps_type(wcaps
) != AC_WID_PIN
)
439 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_DEVLIST_LEN
);
440 if (parm
== -1 && codec
->bus
->rirb_error
)
442 return parm
& AC_DEV_LIST_LEN_MASK
;
446 * snd_hda_get_devices - copy device list without cache
447 * @codec: the HDA codec
448 * @nid: NID of the pin to parse
449 * @dev_list: device list array
450 * @max_devices: max. number of devices to store
452 * Copy the device list. This info is dynamic and so not cached.
453 * Currently called only from hda_proc.c, so not exported.
455 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
456 u8
*dev_list
, int max_devices
)
459 int i
, dev_len
, devices
;
461 parm
= get_num_devices(codec
, nid
);
462 if (!parm
) /* not multi-stream capable */
466 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
469 while (devices
< dev_len
) {
470 parm
= snd_hda_codec_read(codec
, nid
, 0,
471 AC_VERB_GET_DEVICE_LIST
, devices
);
472 if (parm
== -1 && codec
->bus
->rirb_error
)
475 for (i
= 0; i
< 8; i
++) {
476 dev_list
[devices
] = (u8
)parm
;
479 if (devices
>= dev_len
)
489 static void snd_hda_bus_free(struct hda_bus
*bus
)
493 if (bus
->ops
.private_free
)
494 bus
->ops
.private_free(bus
);
495 snd_hdac_bus_exit(&bus
->core
);
499 static int snd_hda_bus_dev_free(struct snd_device
*device
)
501 snd_hda_bus_free(device
->device_data
);
505 static int snd_hda_bus_dev_disconnect(struct snd_device
*device
)
507 struct hda_bus
*bus
= device
->device_data
;
512 /* hdac_bus_ops translations */
513 static int _hda_bus_command(struct hdac_bus
*_bus
, unsigned int cmd
)
515 struct hda_bus
*bus
= container_of(_bus
, struct hda_bus
, core
);
516 return bus
->ops
.command(bus
, cmd
);
519 static int _hda_bus_get_response(struct hdac_bus
*_bus
, unsigned int addr
,
522 struct hda_bus
*bus
= container_of(_bus
, struct hda_bus
, core
);
523 *res
= bus
->ops
.get_response(bus
, addr
);
524 return bus
->rirb_error
? -EIO
: 0;
527 static const struct hdac_bus_ops bus_ops
= {
528 .command
= _hda_bus_command
,
529 .get_response
= _hda_bus_get_response
,
533 * snd_hda_bus_new - create a HDA bus
534 * @card: the card entry
535 * @busp: the pointer to store the created bus instance
537 * Returns 0 if successful, or a negative error code.
539 int snd_hda_bus_new(struct snd_card
*card
,
540 struct hda_bus
**busp
)
544 static struct snd_device_ops dev_ops
= {
545 .dev_disconnect
= snd_hda_bus_dev_disconnect
,
546 .dev_free
= snd_hda_bus_dev_free
,
552 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
556 err
= snd_hdac_bus_init(&bus
->core
, card
->dev
, &bus_ops
);
563 mutex_init(&bus
->prepare_mutex
);
565 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
567 snd_hda_bus_free(bus
);
574 EXPORT_SYMBOL_GPL(snd_hda_bus_new
);
577 * read widget caps for each widget and store in cache
579 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
584 codec
->wcaps
= kmalloc(codec
->core
.num_nodes
* 4, GFP_KERNEL
);
587 nid
= codec
->core
.start_nid
;
588 for (i
= 0; i
< codec
->core
.num_nodes
; i
++, nid
++)
589 codec
->wcaps
[i
] = snd_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 static void init_hda_cache(struct hda_cache_rec
*cache
,
811 unsigned int record_size
);
812 static void free_hda_cache(struct hda_cache_rec
*cache
);
814 /* release all pincfg lists */
815 static void free_init_pincfgs(struct hda_codec
*codec
)
817 snd_array_free(&codec
->driver_pins
);
818 #ifdef CONFIG_SND_HDA_RECONFIG
819 snd_array_free(&codec
->user_pins
);
821 snd_array_free(&codec
->init_pins
);
825 * audio-converter setup caches
827 struct hda_cvt_setup
{
832 unsigned char active
; /* cvt is currently used */
833 unsigned char dirty
; /* setups should be cleared */
836 /* get or create a cache entry for the given audio converter NID */
837 static struct hda_cvt_setup
*
838 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
840 struct hda_cvt_setup
*p
;
843 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
844 p
= snd_array_elem(&codec
->cvt_setups
, i
);
848 p
= snd_array_new(&codec
->cvt_setups
);
857 static void release_pcm(struct kref
*kref
)
859 struct hda_pcm
*pcm
= container_of(kref
, struct hda_pcm
, kref
);
862 snd_device_free(pcm
->codec
->card
, pcm
->pcm
);
863 clear_bit(pcm
->device
, pcm
->codec
->bus
->pcm_dev_bits
);
868 void snd_hda_codec_pcm_put(struct hda_pcm
*pcm
)
870 kref_put(&pcm
->kref
, release_pcm
);
872 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put
);
874 struct hda_pcm
*snd_hda_codec_pcm_new(struct hda_codec
*codec
,
875 const char *fmt
, ...)
881 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
886 kref_init(&pcm
->kref
);
887 pcm
->name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
893 list_add_tail(&pcm
->list
, &codec
->pcm_list_head
);
896 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new
);
901 static void codec_release_pcms(struct hda_codec
*codec
)
903 struct hda_pcm
*pcm
, *n
;
905 list_for_each_entry_safe(pcm
, n
, &codec
->pcm_list_head
, list
) {
906 list_del_init(&pcm
->list
);
908 snd_device_disconnect(codec
->card
, pcm
->pcm
);
909 snd_hda_codec_pcm_put(pcm
);
913 void snd_hda_codec_cleanup_for_unbind(struct hda_codec
*codec
)
915 if (codec
->registered
) {
916 /* pm_runtime_put() is called in snd_hdac_device_exit() */
917 pm_runtime_get_noresume(hda_codec_dev(codec
));
918 pm_runtime_disable(hda_codec_dev(codec
));
919 codec
->registered
= 0;
922 cancel_delayed_work_sync(&codec
->jackpoll_work
);
923 if (!codec
->in_freeing
)
924 snd_hda_ctls_clear(codec
);
925 codec_release_pcms(codec
);
926 snd_hda_detach_beep_device(codec
);
927 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
928 snd_hda_jack_tbl_clear(codec
);
929 codec
->proc_widget_hook
= NULL
;
932 free_hda_cache(&codec
->amp_cache
);
933 free_hda_cache(&codec
->cmd_cache
);
934 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
935 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
937 /* free only driver_pins so that init_pins + user_pins are restored */
938 snd_array_free(&codec
->driver_pins
);
939 snd_array_free(&codec
->cvt_setups
);
940 snd_array_free(&codec
->spdif_out
);
941 snd_array_free(&codec
->verbs
);
942 codec
->preset
= NULL
;
943 codec
->slave_dig_outs
= NULL
;
944 codec
->spdif_status_reset
= 0;
945 snd_array_free(&codec
->mixers
);
946 snd_array_free(&codec
->nids
);
947 remove_conn_list(codec
);
948 snd_hdac_regmap_exit(&codec
->core
);
951 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
952 unsigned int power_state
);
954 /* also called from hda_bind.c */
955 void snd_hda_codec_register(struct hda_codec
*codec
)
957 if (codec
->registered
)
959 if (device_is_registered(hda_codec_dev(codec
))) {
960 snd_hda_register_beep_device(codec
);
961 pm_runtime_enable(hda_codec_dev(codec
));
962 /* it was powered up in snd_hda_codec_new(), now all done */
963 snd_hda_power_down(codec
);
964 codec
->registered
= 1;
968 static int snd_hda_codec_dev_register(struct snd_device
*device
)
970 snd_hda_codec_register(device
->device_data
);
974 static int snd_hda_codec_dev_disconnect(struct snd_device
*device
)
976 struct hda_codec
*codec
= device
->device_data
;
978 snd_hda_detach_beep_device(codec
);
982 static int snd_hda_codec_dev_free(struct snd_device
*device
)
984 struct hda_codec
*codec
= device
->device_data
;
986 codec
->in_freeing
= 1;
987 snd_hdac_device_unregister(&codec
->core
);
988 put_device(hda_codec_dev(codec
));
992 static void snd_hda_codec_dev_release(struct device
*dev
)
994 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
996 free_init_pincfgs(codec
);
997 snd_hdac_device_exit(&codec
->core
);
998 snd_hda_sysfs_clear(codec
);
999 free_hda_cache(&codec
->amp_cache
);
1000 free_hda_cache(&codec
->cmd_cache
);
1001 kfree(codec
->modelname
);
1002 kfree(codec
->wcaps
);
1007 * snd_hda_codec_new - create a HDA codec
1008 * @bus: the bus to assign
1009 * @codec_addr: the codec address
1010 * @codecp: the pointer to store the generated codec
1012 * Returns 0 if successful, or a negative error code.
1014 int snd_hda_codec_new(struct hda_bus
*bus
, struct snd_card
*card
,
1015 unsigned int codec_addr
, struct hda_codec
**codecp
)
1017 struct hda_codec
*codec
;
1021 static struct snd_device_ops dev_ops
= {
1022 .dev_register
= snd_hda_codec_dev_register
,
1023 .dev_disconnect
= snd_hda_codec_dev_disconnect
,
1024 .dev_free
= snd_hda_codec_dev_free
,
1027 if (snd_BUG_ON(!bus
))
1029 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1032 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1036 sprintf(component
, "hdaudioC%dD%d", card
->number
, codec_addr
);
1037 err
= snd_hdac_device_init(&codec
->core
, &bus
->core
, component
,
1044 codec
->core
.dev
.release
= snd_hda_codec_dev_release
;
1045 codec
->core
.type
= HDA_DEV_LEGACY
;
1046 codec
->core
.exec_verb
= codec_exec_verb
;
1050 codec
->addr
= codec_addr
;
1051 mutex_init(&codec
->spdif_mutex
);
1052 mutex_init(&codec
->control_mutex
);
1053 mutex_init(&codec
->hash_mutex
);
1054 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1055 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1056 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1057 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1058 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1059 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1060 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1061 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1062 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1063 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1064 INIT_LIST_HEAD(&codec
->conn_list
);
1065 INIT_LIST_HEAD(&codec
->pcm_list_head
);
1067 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1068 codec
->depop_delay
= -1;
1069 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
1072 codec
->power_jiffies
= jiffies
;
1075 snd_hda_sysfs_init(codec
);
1077 if (codec
->bus
->modelname
) {
1078 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1079 if (!codec
->modelname
) {
1085 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
1086 err
= read_widget_caps(codec
, fg
);
1089 err
= read_pin_defaults(codec
);
1093 /* power-up all before initialization */
1094 hda_set_power_state(codec
, AC_PWRST_D0
);
1096 snd_hda_codec_proc_new(codec
);
1098 snd_hda_create_hwdep(codec
);
1100 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->core
.vendor_id
,
1101 codec
->core
.subsystem_id
, codec
->core
.revision_id
);
1102 snd_component_add(card
, component
);
1104 err
= snd_device_new(card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
1113 put_device(hda_codec_dev(codec
));
1116 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
1119 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1120 * @codec: the HDA codec
1122 * Forcibly refresh the all widget caps and the init pin configurations of
1125 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1130 err
= snd_hdac_refresh_widgets(&codec
->core
);
1134 /* Assume the function group node does not change,
1135 * only the widget nodes may change.
1137 kfree(codec
->wcaps
);
1138 fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
1139 err
= read_widget_caps(codec
, fg
);
1143 snd_array_free(&codec
->init_pins
);
1144 err
= read_pin_defaults(codec
);
1148 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
1150 /* update the stream-id if changed */
1151 static void update_pcm_stream_id(struct hda_codec
*codec
,
1152 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1153 u32 stream_tag
, int channel_id
)
1155 unsigned int oldval
, newval
;
1157 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1158 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1159 newval
= (stream_tag
<< 4) | channel_id
;
1160 if (oldval
!= newval
)
1161 snd_hda_codec_write(codec
, nid
, 0,
1162 AC_VERB_SET_CHANNEL_STREAMID
,
1164 p
->stream_tag
= stream_tag
;
1165 p
->channel_id
= channel_id
;
1169 /* update the format-id if changed */
1170 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1171 hda_nid_t nid
, int format
)
1173 unsigned int oldval
;
1175 if (p
->format_id
!= format
) {
1176 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1177 AC_VERB_GET_STREAM_FORMAT
, 0);
1178 if (oldval
!= format
) {
1180 snd_hda_codec_write(codec
, nid
, 0,
1181 AC_VERB_SET_STREAM_FORMAT
,
1184 p
->format_id
= format
;
1189 * snd_hda_codec_setup_stream - set up the codec for streaming
1190 * @codec: the CODEC to set up
1191 * @nid: the NID to set up
1192 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1193 * @channel_id: channel id to pass, zero based.
1194 * @format: stream format.
1196 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1198 int channel_id
, int format
)
1200 struct hda_codec
*c
;
1201 struct hda_cvt_setup
*p
;
1209 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1210 nid
, stream_tag
, channel_id
, format
);
1211 p
= get_hda_cvt_setup(codec
, nid
);
1215 if (codec
->patch_ops
.stream_pm
)
1216 codec
->patch_ops
.stream_pm(codec
, nid
, true);
1217 if (codec
->pcm_format_first
)
1218 update_pcm_format(codec
, p
, nid
, format
);
1219 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1220 if (!codec
->pcm_format_first
)
1221 update_pcm_format(codec
, p
, nid
, format
);
1226 /* make other inactive cvts with the same stream-tag dirty */
1227 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1228 list_for_each_codec(c
, codec
->bus
) {
1229 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1230 p
= snd_array_elem(&c
->cvt_setups
, i
);
1231 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1232 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1237 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1239 static void really_cleanup_stream(struct hda_codec
*codec
,
1240 struct hda_cvt_setup
*q
);
1243 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1244 * @codec: the CODEC to clean up
1245 * @nid: the NID to clean up
1246 * @do_now: really clean up the stream instead of clearing the active flag
1248 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1251 struct hda_cvt_setup
*p
;
1256 if (codec
->no_sticky_stream
)
1259 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1260 p
= get_hda_cvt_setup(codec
, nid
);
1262 /* here we just clear the active flag when do_now isn't set;
1263 * actual clean-ups will be done later in
1264 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1267 really_cleanup_stream(codec
, p
);
1272 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1274 static void really_cleanup_stream(struct hda_codec
*codec
,
1275 struct hda_cvt_setup
*q
)
1277 hda_nid_t nid
= q
->nid
;
1278 if (q
->stream_tag
|| q
->channel_id
)
1279 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1281 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1283 memset(q
, 0, sizeof(*q
));
1285 if (codec
->patch_ops
.stream_pm
)
1286 codec
->patch_ops
.stream_pm(codec
, nid
, false);
1289 /* clean up the all conflicting obsolete streams */
1290 static void purify_inactive_streams(struct hda_codec
*codec
)
1292 struct hda_codec
*c
;
1295 list_for_each_codec(c
, codec
->bus
) {
1296 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1297 struct hda_cvt_setup
*p
;
1298 p
= snd_array_elem(&c
->cvt_setups
, i
);
1300 really_cleanup_stream(c
, p
);
1306 /* clean up all streams; called from suspend */
1307 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1311 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1312 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1314 really_cleanup_stream(codec
, p
);
1320 * amp access functions
1323 /* FIXME: more better hash key? */
1324 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1325 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1326 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1327 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1328 #define INFO_AMP_CAPS (1<<0)
1329 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1331 /* initialize the hash table */
1332 static void init_hda_cache(struct hda_cache_rec
*cache
,
1333 unsigned int record_size
)
1335 memset(cache
, 0, sizeof(*cache
));
1336 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1337 snd_array_init(&cache
->buf
, record_size
, 64);
1340 static void free_hda_cache(struct hda_cache_rec
*cache
)
1342 snd_array_free(&cache
->buf
);
1345 /* query the hash. allocate an entry if not found. */
1346 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1348 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1349 u16 cur
= cache
->hash
[idx
];
1350 struct hda_cache_head
*info
;
1352 while (cur
!= 0xffff) {
1353 info
= snd_array_elem(&cache
->buf
, cur
);
1354 if (info
->key
== key
)
1361 /* query the hash. allocate an entry if not found. */
1362 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1365 struct hda_cache_head
*info
= get_hash(cache
, key
);
1368 /* add a new hash entry */
1369 info
= snd_array_new(&cache
->buf
);
1372 cur
= snd_array_index(&cache
->buf
, info
);
1376 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1377 info
->next
= cache
->hash
[idx
];
1378 cache
->hash
[idx
] = cur
;
1383 /* query and allocate an amp hash entry */
1384 static inline struct hda_amp_info
*
1385 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1387 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1390 /* overwrite the value with the key in the caps hash */
1391 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1393 struct hda_amp_info
*info
;
1395 mutex_lock(&codec
->hash_mutex
);
1396 info
= get_alloc_amp_hash(codec
, key
);
1398 mutex_unlock(&codec
->hash_mutex
);
1401 info
->amp_caps
= val
;
1402 info
->head
.val
|= INFO_AMP_CAPS
;
1403 mutex_unlock(&codec
->hash_mutex
);
1407 /* query the value from the caps hash; if not found, fetch the current
1408 * value from the given function and store in the hash
1411 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1412 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1414 struct hda_amp_info
*info
;
1417 mutex_lock(&codec
->hash_mutex
);
1418 info
= get_alloc_amp_hash(codec
, key
);
1420 mutex_unlock(&codec
->hash_mutex
);
1423 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1424 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1425 val
= func(codec
, nid
, dir
);
1426 write_caps_hash(codec
, key
, val
);
1428 val
= info
->amp_caps
;
1429 mutex_unlock(&codec
->hash_mutex
);
1434 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1437 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1438 nid
= codec
->core
.afg
;
1439 return snd_hda_param_read(codec
, nid
,
1440 direction
== HDA_OUTPUT
?
1441 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1445 * query_amp_caps - query AMP capabilities
1446 * @codec: the HD-auio codec
1447 * @nid: the NID to query
1448 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1450 * Query AMP capabilities for the given widget and direction.
1451 * Returns the obtained capability bits.
1453 * When cap bits have been already read, this doesn't read again but
1454 * returns the cached value.
1456 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1458 return query_caps_hash(codec
, nid
, direction
,
1459 HDA_HASH_KEY(nid
, direction
, 0),
1462 EXPORT_SYMBOL_GPL(query_amp_caps
);
1465 * snd_hda_check_amp_caps - query AMP capabilities
1466 * @codec: the HD-audio codec
1467 * @nid: the NID to query
1468 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1469 * @bits: bit mask to check the result
1471 * Check whether the widget has the given amp capability for the direction.
1473 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1474 int dir
, unsigned int bits
)
1478 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
1479 if (query_amp_caps(codec
, nid
, dir
) & bits
)
1483 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
1486 * snd_hda_override_amp_caps - Override the AMP capabilities
1487 * @codec: the CODEC to clean up
1488 * @nid: the NID to clean up
1489 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1490 * @caps: the capability bits to set
1492 * Override the cached AMP caps bits value by the given one.
1493 * This function is useful if the driver needs to adjust the AMP ranges,
1494 * e.g. limit to 0dB, etc.
1496 * Returns zero if successful or a negative error code.
1498 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1501 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1503 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
1505 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1508 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1512 * snd_hda_query_pin_caps - Query PIN capabilities
1513 * @codec: the HD-auio codec
1514 * @nid: the NID to query
1516 * Query PIN capabilities for the given widget.
1517 * Returns the obtained capability bits.
1519 * When cap bits have been already read, this doesn't read again but
1520 * returns the cached value.
1522 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1524 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1527 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps
);
1530 * snd_hda_override_pin_caps - Override the pin capabilities
1532 * @nid: the NID to override
1533 * @caps: the capability bits to set
1535 * Override the cached PIN capabilitiy bits value by the given one.
1537 * Returns zero if successful or a negative error code.
1539 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1542 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1544 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps
);
1547 * snd_hda_codec_amp_stereo - update the AMP stereo values
1548 * @codec: HD-audio codec
1549 * @nid: NID to read the AMP value
1550 * @direction: #HDA_INPUT or #HDA_OUTPUT
1551 * @idx: the index value (only for input direction)
1552 * @mask: bit mask to set
1553 * @val: the bits value to set
1555 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1556 * stereo widget with the same mask and value.
1558 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1559 int direction
, int idx
, int mask
, int val
)
1563 if (snd_BUG_ON(mask
& ~0xff))
1565 for (ch
= 0; ch
< 2; ch
++)
1566 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1570 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
1573 * snd_hda_codec_amp_init - initialize the AMP value
1574 * @codec: the HDA codec
1575 * @nid: NID to read the AMP value
1576 * @ch: channel (left=0 or right=1)
1577 * @dir: #HDA_INPUT or #HDA_OUTPUT
1578 * @idx: the index value (only for input direction)
1579 * @mask: bit mask to set
1580 * @val: the bits value to set
1582 * Works like snd_hda_codec_amp_update() but it writes the value only at
1583 * the first access. If the amp was already initialized / updated beforehand,
1584 * this does nothing.
1586 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1587 int dir
, int idx
, int mask
, int val
)
1591 if (!codec
->core
.regmap
)
1593 regcache_cache_only(codec
->core
.regmap
, true);
1594 orig
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1595 regcache_cache_only(codec
->core
.regmap
, false);
1598 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
);
1600 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
1603 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1604 * @codec: the HDA codec
1605 * @nid: NID to read the AMP value
1606 * @dir: #HDA_INPUT or #HDA_OUTPUT
1607 * @idx: the index value (only for input direction)
1608 * @mask: bit mask to set
1609 * @val: the bits value to set
1611 * Call snd_hda_codec_amp_init() for both stereo channels.
1613 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1614 int dir
, int idx
, int mask
, int val
)
1618 if (snd_BUG_ON(mask
& ~0xff))
1620 for (ch
= 0; ch
< 2; ch
++)
1621 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
1625 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
1627 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1630 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1632 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1639 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1640 * @kcontrol: referred ctl element
1641 * @uinfo: pointer to get/store the data
1643 * The control element is supposed to have the private_value field
1644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1646 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1647 struct snd_ctl_elem_info
*uinfo
)
1649 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1650 u16 nid
= get_amp_nid(kcontrol
);
1651 u8 chs
= get_amp_channels(kcontrol
);
1652 int dir
= get_amp_direction(kcontrol
);
1653 unsigned int ofs
= get_amp_offset(kcontrol
);
1655 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1656 uinfo
->count
= chs
== 3 ? 2 : 1;
1657 uinfo
->value
.integer
.min
= 0;
1658 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1659 if (!uinfo
->value
.integer
.max
) {
1661 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1662 nid
, kcontrol
->id
.name
);
1667 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
1670 static inline unsigned int
1671 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1672 int ch
, int dir
, int idx
, unsigned int ofs
)
1675 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1676 val
&= HDA_AMP_VOLMASK
;
1685 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1686 int ch
, int dir
, int idx
, unsigned int ofs
,
1689 unsigned int maxval
;
1693 /* ofs = 0: raw max value */
1694 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1697 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1698 HDA_AMP_VOLMASK
, val
);
1702 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1703 * @kcontrol: ctl element
1704 * @ucontrol: pointer to get/store the data
1706 * The control element is supposed to have the private_value field
1707 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1709 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1710 struct snd_ctl_elem_value
*ucontrol
)
1712 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1713 hda_nid_t nid
= get_amp_nid(kcontrol
);
1714 int chs
= get_amp_channels(kcontrol
);
1715 int dir
= get_amp_direction(kcontrol
);
1716 int idx
= get_amp_index(kcontrol
);
1717 unsigned int ofs
= get_amp_offset(kcontrol
);
1718 long *valp
= ucontrol
->value
.integer
.value
;
1721 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1723 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1726 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
1729 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1730 * @kcontrol: ctl element
1731 * @ucontrol: pointer to get/store the data
1733 * The control element is supposed to have the private_value field
1734 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1736 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1737 struct snd_ctl_elem_value
*ucontrol
)
1739 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1740 hda_nid_t nid
= get_amp_nid(kcontrol
);
1741 int chs
= get_amp_channels(kcontrol
);
1742 int dir
= get_amp_direction(kcontrol
);
1743 int idx
= get_amp_index(kcontrol
);
1744 unsigned int ofs
= get_amp_offset(kcontrol
);
1745 long *valp
= ucontrol
->value
.integer
.value
;
1749 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1753 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1756 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
1759 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1760 * @kcontrol: ctl element
1761 * @op_flag: operation flag
1762 * @size: byte size of input TLV
1765 * The control element is supposed to have the private_value field
1766 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1768 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1769 unsigned int size
, unsigned int __user
*_tlv
)
1771 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1772 hda_nid_t nid
= get_amp_nid(kcontrol
);
1773 int dir
= get_amp_direction(kcontrol
);
1774 unsigned int ofs
= get_amp_offset(kcontrol
);
1775 bool min_mute
= get_amp_min_mute(kcontrol
);
1776 u32 caps
, val1
, val2
;
1778 if (size
< 4 * sizeof(unsigned int))
1780 caps
= query_amp_caps(codec
, nid
, dir
);
1781 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1782 val2
= (val2
+ 1) * 25;
1783 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1785 val1
= ((int)val1
) * ((int)val2
);
1786 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
1787 val2
|= TLV_DB_SCALE_MUTE
;
1788 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
1790 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
1792 if (put_user(val1
, _tlv
+ 2))
1794 if (put_user(val2
, _tlv
+ 3))
1798 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
1801 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1802 * @codec: HD-audio codec
1803 * @nid: NID of a reference widget
1804 * @dir: #HDA_INPUT or #HDA_OUTPUT
1805 * @tlv: TLV data to be stored, at least 4 elements
1807 * Set (static) TLV data for a virtual master volume using the AMP caps
1808 * obtained from the reference NID.
1809 * The volume range is recalculated as if the max volume is 0dB.
1811 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1817 caps
= query_amp_caps(codec
, nid
, dir
);
1818 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1819 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1820 step
= (step
+ 1) * 25;
1821 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
1822 tlv
[1] = 2 * sizeof(unsigned int);
1823 tlv
[2] = -nums
* step
;
1826 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
1828 /* find a mixer control element with the given name */
1829 static struct snd_kcontrol
*
1830 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
1832 struct snd_ctl_elem_id id
;
1833 memset(&id
, 0, sizeof(id
));
1834 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1837 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
1839 strcpy(id
.name
, name
);
1840 return snd_ctl_find_id(codec
->card
, &id
);
1844 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1845 * @codec: HD-audio codec
1846 * @name: ctl id name string
1848 * Get the control element with the given id string and IFACE_MIXER.
1850 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1853 return find_mixer_ctl(codec
, name
, 0, 0);
1855 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
1857 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
1861 /* 16 ctlrs should be large enough */
1862 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
1863 if (!find_mixer_ctl(codec
, name
, 0, idx
))
1870 * snd_hda_ctl_add - Add a control element and assign to the codec
1871 * @codec: HD-audio codec
1872 * @nid: corresponding NID (optional)
1873 * @kctl: the control element to assign
1875 * Add the given control element to an array inside the codec instance.
1876 * All control elements belonging to a codec are supposed to be added
1877 * by this function so that a proper clean-up works at the free or
1878 * reconfiguration time.
1880 * If non-zero @nid is passed, the NID is assigned to the control element.
1881 * The assignment is shown in the codec proc file.
1883 * snd_hda_ctl_add() checks the control subdev id field whether
1884 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1885 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1886 * specifies if kctl->private_value is a HDA amplifier value.
1888 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
1889 struct snd_kcontrol
*kctl
)
1892 unsigned short flags
= 0;
1893 struct hda_nid_item
*item
;
1895 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
1896 flags
|= HDA_NID_ITEM_AMP
;
1898 nid
= get_amp_nid_(kctl
->private_value
);
1900 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
1901 nid
= kctl
->id
.subdevice
& 0xffff;
1902 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
1903 kctl
->id
.subdevice
= 0;
1904 err
= snd_ctl_add(codec
->card
, kctl
);
1907 item
= snd_array_new(&codec
->mixers
);
1912 item
->flags
= flags
;
1915 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
1918 * snd_hda_add_nid - Assign a NID to a control element
1919 * @codec: HD-audio codec
1920 * @nid: corresponding NID (optional)
1921 * @kctl: the control element to assign
1922 * @index: index to kctl
1924 * Add the given control element to an array inside the codec instance.
1925 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1926 * NID:KCTL mapping - for example "Capture Source" selector.
1928 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
1929 unsigned int index
, hda_nid_t nid
)
1931 struct hda_nid_item
*item
;
1934 item
= snd_array_new(&codec
->nids
);
1938 item
->index
= index
;
1942 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
1943 kctl
->id
.name
, kctl
->id
.index
, index
);
1946 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
1949 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1950 * @codec: HD-audio codec
1952 void snd_hda_ctls_clear(struct hda_codec
*codec
)
1955 struct hda_nid_item
*items
= codec
->mixers
.list
;
1956 for (i
= 0; i
< codec
->mixers
.used
; i
++)
1957 snd_ctl_remove(codec
->card
, items
[i
].kctl
);
1958 snd_array_free(&codec
->mixers
);
1959 snd_array_free(&codec
->nids
);
1963 * snd_hda_lock_devices - pseudo device locking
1966 * toggle card->shutdown to allow/disallow the device access (as a hack)
1968 int snd_hda_lock_devices(struct hda_bus
*bus
)
1970 struct snd_card
*card
= bus
->card
;
1971 struct hda_codec
*codec
;
1973 spin_lock(&card
->files_lock
);
1977 if (!list_empty(&card
->ctl_files
))
1980 list_for_each_codec(codec
, bus
) {
1981 struct hda_pcm
*cpcm
;
1982 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
1985 if (cpcm
->pcm
->streams
[0].substream_opened
||
1986 cpcm
->pcm
->streams
[1].substream_opened
)
1990 spin_unlock(&card
->files_lock
);
1996 spin_unlock(&card
->files_lock
);
1999 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
2002 * snd_hda_unlock_devices - pseudo device unlocking
2005 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2007 struct snd_card
*card
= bus
->card
;
2009 spin_lock(&card
->files_lock
);
2011 spin_unlock(&card
->files_lock
);
2013 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
2016 * snd_hda_codec_reset - Clear all objects assigned to the codec
2017 * @codec: HD-audio codec
2019 * This frees the all PCM and control elements assigned to the codec, and
2020 * clears the caches and restores the pin default configurations.
2022 * When a device is being used, it returns -EBSY. If successfully freed,
2025 int snd_hda_codec_reset(struct hda_codec
*codec
)
2027 struct hda_bus
*bus
= codec
->bus
;
2029 if (snd_hda_lock_devices(bus
) < 0)
2032 /* OK, let it free */
2033 snd_hdac_device_unregister(&codec
->core
);
2035 /* allow device access again */
2036 snd_hda_unlock_devices(bus
);
2040 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
2042 /* apply the function to all matching slave ctls in the mixer list */
2043 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2044 const char *suffix
, map_slave_func_t func
, void *data
)
2046 struct hda_nid_item
*items
;
2047 const char * const *s
;
2050 items
= codec
->mixers
.list
;
2051 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2052 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2053 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2055 for (s
= slaves
; *s
; s
++) {
2056 char tmpname
[sizeof(sctl
->id
.name
)];
2057 const char *name
= *s
;
2059 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2063 if (!strcmp(sctl
->id
.name
, name
)) {
2064 err
= func(codec
, data
, sctl
);
2074 static int check_slave_present(struct hda_codec
*codec
,
2075 void *data
, struct snd_kcontrol
*sctl
)
2080 /* guess the value corresponding to 0dB */
2081 static int get_kctl_0dB_offset(struct hda_codec
*codec
,
2082 struct snd_kcontrol
*kctl
, int *step_to_check
)
2085 const int *tlv
= NULL
;
2088 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2089 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2090 mm_segment_t fs
= get_fs();
2092 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2095 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2097 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
) {
2099 step
&= ~TLV_DB_SCALE_MUTE
;
2102 if (*step_to_check
&& *step_to_check
!= step
) {
2103 codec_err(codec
, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2104 - *step_to_check
, step
);
2107 *step_to_check
= step
;
2108 val
= -tlv
[2] / step
;
2113 /* call kctl->put with the given value(s) */
2114 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2116 struct snd_ctl_elem_value
*ucontrol
;
2117 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2120 ucontrol
->value
.integer
.value
[0] = val
;
2121 ucontrol
->value
.integer
.value
[1] = val
;
2122 kctl
->put(kctl
, ucontrol
);
2127 /* initialize the slave volume with 0dB */
2128 static int init_slave_0dB(struct hda_codec
*codec
,
2129 void *data
, struct snd_kcontrol
*slave
)
2131 int offset
= get_kctl_0dB_offset(codec
, slave
, data
);
2133 put_kctl_with_value(slave
, offset
);
2137 /* unmute the slave */
2138 static int init_slave_unmute(struct hda_codec
*codec
,
2139 void *data
, struct snd_kcontrol
*slave
)
2141 return put_kctl_with_value(slave
, 1);
2144 static int add_slave(struct hda_codec
*codec
,
2145 void *data
, struct snd_kcontrol
*slave
)
2147 return snd_ctl_add_slave(data
, slave
);
2151 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2152 * @codec: HD-audio codec
2153 * @name: vmaster control name
2154 * @tlv: TLV data (optional)
2155 * @slaves: slave control names (optional)
2156 * @suffix: suffix string to each slave name (optional)
2157 * @init_slave_vol: initialize slaves to unmute/0dB
2158 * @ctl_ret: store the vmaster kcontrol in return
2160 * Create a virtual master control with the given name. The TLV data
2161 * must be either NULL or a valid data.
2163 * @slaves is a NULL-terminated array of strings, each of which is a
2164 * slave control name. All controls with these names are assigned to
2165 * the new virtual master control.
2167 * This function returns zero if successful or a negative error code.
2169 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2170 unsigned int *tlv
, const char * const *slaves
,
2171 const char *suffix
, bool init_slave_vol
,
2172 struct snd_kcontrol
**ctl_ret
)
2174 struct snd_kcontrol
*kctl
;
2180 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2182 codec_dbg(codec
, "No slave found for %s\n", name
);
2185 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2188 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2192 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
2196 /* init with master mute & zero volume */
2197 put_kctl_with_value(kctl
, 0);
2198 if (init_slave_vol
) {
2200 map_slaves(codec
, slaves
, suffix
,
2201 tlv
? init_slave_0dB
: init_slave_unmute
, &step
);
2208 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
2211 * mute-LED control using vmaster
2213 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2214 struct snd_ctl_elem_info
*uinfo
)
2216 static const char * const texts
[] = {
2217 "On", "Off", "Follow Master"
2220 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
2223 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2224 struct snd_ctl_elem_value
*ucontrol
)
2226 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2227 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2231 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2232 struct snd_ctl_elem_value
*ucontrol
)
2234 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2235 unsigned int old_mode
= hook
->mute_mode
;
2237 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2238 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2239 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2240 if (old_mode
== hook
->mute_mode
)
2242 snd_hda_sync_vmaster_hook(hook
);
2246 static struct snd_kcontrol_new vmaster_mute_mode
= {
2247 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2248 .name
= "Mute-LED Mode",
2249 .info
= vmaster_mute_mode_info
,
2250 .get
= vmaster_mute_mode_get
,
2251 .put
= vmaster_mute_mode_put
,
2255 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2256 * @codec: the HDA codec
2257 * @hook: the vmaster hook object
2258 * @expose_enum_ctl: flag to create an enum ctl
2260 * Add a mute-LED hook with the given vmaster switch kctl.
2261 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2262 * created and associated with the given hook.
2264 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2265 struct hda_vmaster_mute_hook
*hook
,
2266 bool expose_enum_ctl
)
2268 struct snd_kcontrol
*kctl
;
2270 if (!hook
->hook
|| !hook
->sw_kctl
)
2272 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2273 hook
->codec
= codec
;
2274 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2275 if (!expose_enum_ctl
)
2277 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2280 return snd_hda_ctl_add(codec
, 0, kctl
);
2282 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
2285 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2286 * @hook: the vmaster hook
2288 * Call the hook with the current value for synchronization.
2289 * Should be called in init callback.
2291 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2293 if (!hook
->hook
|| !hook
->codec
)
2295 /* don't call vmaster hook in the destructor since it might have
2296 * been already destroyed
2298 if (hook
->codec
->bus
->shutdown
)
2300 switch (hook
->mute_mode
) {
2301 case HDA_VMUTE_FOLLOW_MASTER
:
2302 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2305 hook
->hook(hook
->codec
, hook
->mute_mode
);
2309 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
2313 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2314 * @kcontrol: referred ctl element
2315 * @uinfo: pointer to get/store the data
2317 * The control element is supposed to have the private_value field
2318 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2320 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2321 struct snd_ctl_elem_info
*uinfo
)
2323 int chs
= get_amp_channels(kcontrol
);
2325 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2326 uinfo
->count
= chs
== 3 ? 2 : 1;
2327 uinfo
->value
.integer
.min
= 0;
2328 uinfo
->value
.integer
.max
= 1;
2331 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
2334 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2335 * @kcontrol: ctl element
2336 * @ucontrol: pointer to get/store the data
2338 * The control element is supposed to have the private_value field
2339 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2341 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2342 struct snd_ctl_elem_value
*ucontrol
)
2344 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2345 hda_nid_t nid
= get_amp_nid(kcontrol
);
2346 int chs
= get_amp_channels(kcontrol
);
2347 int dir
= get_amp_direction(kcontrol
);
2348 int idx
= get_amp_index(kcontrol
);
2349 long *valp
= ucontrol
->value
.integer
.value
;
2352 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2353 HDA_AMP_MUTE
) ? 0 : 1;
2355 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2356 HDA_AMP_MUTE
) ? 0 : 1;
2359 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
2362 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2363 * @kcontrol: ctl element
2364 * @ucontrol: pointer to get/store the data
2366 * The control element is supposed to have the private_value field
2367 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2369 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2370 struct snd_ctl_elem_value
*ucontrol
)
2372 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2373 hda_nid_t nid
= get_amp_nid(kcontrol
);
2374 int chs
= get_amp_channels(kcontrol
);
2375 int dir
= get_amp_direction(kcontrol
);
2376 int idx
= get_amp_index(kcontrol
);
2377 long *valp
= ucontrol
->value
.integer
.value
;
2381 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2383 *valp
? 0 : HDA_AMP_MUTE
);
2387 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2389 *valp
? 0 : HDA_AMP_MUTE
);
2390 hda_call_check_power_status(codec
, nid
);
2393 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
2396 * bound volume controls
2398 * bind multiple volumes (# indices, from 0)
2401 #define AMP_VAL_IDX_SHIFT 19
2402 #define AMP_VAL_IDX_MASK (0x0f<<19)
2405 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2406 * @kcontrol: ctl element
2407 * @ucontrol: pointer to get/store the data
2409 * The control element is supposed to have the private_value field
2410 * set up via HDA_BIND_MUTE*() macros.
2412 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2413 struct snd_ctl_elem_value
*ucontrol
)
2415 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2419 mutex_lock(&codec
->control_mutex
);
2420 pval
= kcontrol
->private_value
;
2421 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2422 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2423 kcontrol
->private_value
= pval
;
2424 mutex_unlock(&codec
->control_mutex
);
2427 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get
);
2430 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2431 * @kcontrol: ctl element
2432 * @ucontrol: pointer to get/store the data
2434 * The control element is supposed to have the private_value field
2435 * set up via HDA_BIND_MUTE*() macros.
2437 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2438 struct snd_ctl_elem_value
*ucontrol
)
2440 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2442 int i
, indices
, err
= 0, change
= 0;
2444 mutex_lock(&codec
->control_mutex
);
2445 pval
= kcontrol
->private_value
;
2446 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2447 for (i
= 0; i
< indices
; i
++) {
2448 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2449 (i
<< AMP_VAL_IDX_SHIFT
);
2450 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2455 kcontrol
->private_value
= pval
;
2456 mutex_unlock(&codec
->control_mutex
);
2457 return err
< 0 ? err
: change
;
2459 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put
);
2462 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2463 * @kcontrol: referred ctl element
2464 * @uinfo: pointer to get/store the data
2466 * The control element is supposed to have the private_value field
2467 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2469 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2470 struct snd_ctl_elem_info
*uinfo
)
2472 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2473 struct hda_bind_ctls
*c
;
2476 mutex_lock(&codec
->control_mutex
);
2477 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2478 kcontrol
->private_value
= *c
->values
;
2479 err
= c
->ops
->info(kcontrol
, uinfo
);
2480 kcontrol
->private_value
= (long)c
;
2481 mutex_unlock(&codec
->control_mutex
);
2484 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info
);
2487 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2488 * @kcontrol: ctl element
2489 * @ucontrol: pointer to get/store the data
2491 * The control element is supposed to have the private_value field
2492 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2494 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2495 struct snd_ctl_elem_value
*ucontrol
)
2497 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2498 struct hda_bind_ctls
*c
;
2501 mutex_lock(&codec
->control_mutex
);
2502 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2503 kcontrol
->private_value
= *c
->values
;
2504 err
= c
->ops
->get(kcontrol
, ucontrol
);
2505 kcontrol
->private_value
= (long)c
;
2506 mutex_unlock(&codec
->control_mutex
);
2509 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get
);
2512 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2513 * @kcontrol: ctl element
2514 * @ucontrol: pointer to get/store the data
2516 * The control element is supposed to have the private_value field
2517 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2519 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2520 struct snd_ctl_elem_value
*ucontrol
)
2522 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2523 struct hda_bind_ctls
*c
;
2524 unsigned long *vals
;
2525 int err
= 0, change
= 0;
2527 mutex_lock(&codec
->control_mutex
);
2528 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2529 for (vals
= c
->values
; *vals
; vals
++) {
2530 kcontrol
->private_value
= *vals
;
2531 err
= c
->ops
->put(kcontrol
, ucontrol
);
2536 kcontrol
->private_value
= (long)c
;
2537 mutex_unlock(&codec
->control_mutex
);
2538 return err
< 0 ? err
: change
;
2540 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put
);
2543 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2544 * @kcontrol: ctl element
2545 * @op_flag: operation flag
2546 * @size: byte size of input TLV
2549 * The control element is supposed to have the private_value field
2550 * set up via HDA_BIND_VOL() macro.
2552 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2553 unsigned int size
, unsigned int __user
*tlv
)
2555 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2556 struct hda_bind_ctls
*c
;
2559 mutex_lock(&codec
->control_mutex
);
2560 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2561 kcontrol
->private_value
= *c
->values
;
2562 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2563 kcontrol
->private_value
= (long)c
;
2564 mutex_unlock(&codec
->control_mutex
);
2567 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv
);
2569 struct hda_ctl_ops snd_hda_bind_vol
= {
2570 .info
= snd_hda_mixer_amp_volume_info
,
2571 .get
= snd_hda_mixer_amp_volume_get
,
2572 .put
= snd_hda_mixer_amp_volume_put
,
2573 .tlv
= snd_hda_mixer_amp_tlv
2575 EXPORT_SYMBOL_GPL(snd_hda_bind_vol
);
2577 struct hda_ctl_ops snd_hda_bind_sw
= {
2578 .info
= snd_hda_mixer_amp_switch_info
,
2579 .get
= snd_hda_mixer_amp_switch_get
,
2580 .put
= snd_hda_mixer_amp_switch_put
,
2581 .tlv
= snd_hda_mixer_amp_tlv
2583 EXPORT_SYMBOL_GPL(snd_hda_bind_sw
);
2586 * SPDIF out controls
2589 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2590 struct snd_ctl_elem_info
*uinfo
)
2592 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2597 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2598 struct snd_ctl_elem_value
*ucontrol
)
2600 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2601 IEC958_AES0_NONAUDIO
|
2602 IEC958_AES0_CON_EMPHASIS_5015
|
2603 IEC958_AES0_CON_NOT_COPYRIGHT
;
2604 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2605 IEC958_AES1_CON_ORIGINAL
;
2609 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2610 struct snd_ctl_elem_value
*ucontrol
)
2612 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2613 IEC958_AES0_NONAUDIO
|
2614 IEC958_AES0_PRO_EMPHASIS_5015
;
2618 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2619 struct snd_ctl_elem_value
*ucontrol
)
2621 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2622 int idx
= kcontrol
->private_value
;
2623 struct hda_spdif_out
*spdif
;
2625 mutex_lock(&codec
->spdif_mutex
);
2626 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2627 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2628 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2629 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2630 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2631 mutex_unlock(&codec
->spdif_mutex
);
2636 /* convert from SPDIF status bits to HDA SPDIF bits
2637 * bit 0 (DigEn) is always set zero (to be filled later)
2639 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2641 unsigned short val
= 0;
2643 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2644 val
|= AC_DIG1_PROFESSIONAL
;
2645 if (sbits
& IEC958_AES0_NONAUDIO
)
2646 val
|= AC_DIG1_NONAUDIO
;
2647 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2648 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2649 IEC958_AES0_PRO_EMPHASIS_5015
)
2650 val
|= AC_DIG1_EMPHASIS
;
2652 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2653 IEC958_AES0_CON_EMPHASIS_5015
)
2654 val
|= AC_DIG1_EMPHASIS
;
2655 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2656 val
|= AC_DIG1_COPYRIGHT
;
2657 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2658 val
|= AC_DIG1_LEVEL
;
2659 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2664 /* convert to SPDIF status bits from HDA SPDIF bits
2666 static unsigned int convert_to_spdif_status(unsigned short val
)
2668 unsigned int sbits
= 0;
2670 if (val
& AC_DIG1_NONAUDIO
)
2671 sbits
|= IEC958_AES0_NONAUDIO
;
2672 if (val
& AC_DIG1_PROFESSIONAL
)
2673 sbits
|= IEC958_AES0_PROFESSIONAL
;
2674 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2675 if (val
& AC_DIG1_EMPHASIS
)
2676 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2678 if (val
& AC_DIG1_EMPHASIS
)
2679 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2680 if (!(val
& AC_DIG1_COPYRIGHT
))
2681 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2682 if (val
& AC_DIG1_LEVEL
)
2683 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2684 sbits
|= val
& (0x7f << 8);
2689 /* set digital convert verbs both for the given NID and its slaves */
2690 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2695 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2696 d
= codec
->slave_dig_outs
;
2700 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2703 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2707 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2709 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2712 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2713 struct snd_ctl_elem_value
*ucontrol
)
2715 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2716 int idx
= kcontrol
->private_value
;
2717 struct hda_spdif_out
*spdif
;
2722 mutex_lock(&codec
->spdif_mutex
);
2723 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2725 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
2726 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
2727 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
2728 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
2729 val
= convert_from_spdif_status(spdif
->status
);
2730 val
|= spdif
->ctls
& 1;
2731 change
= spdif
->ctls
!= val
;
2733 if (change
&& nid
!= (u16
)-1)
2734 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2735 mutex_unlock(&codec
->spdif_mutex
);
2739 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2741 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
2742 struct snd_ctl_elem_value
*ucontrol
)
2744 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2745 int idx
= kcontrol
->private_value
;
2746 struct hda_spdif_out
*spdif
;
2748 mutex_lock(&codec
->spdif_mutex
);
2749 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2750 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
2751 mutex_unlock(&codec
->spdif_mutex
);
2755 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
2758 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
2759 /* unmute amp switch (if any) */
2760 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2761 (dig1
& AC_DIG1_ENABLE
))
2762 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2766 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
2767 struct snd_ctl_elem_value
*ucontrol
)
2769 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2770 int idx
= kcontrol
->private_value
;
2771 struct hda_spdif_out
*spdif
;
2776 mutex_lock(&codec
->spdif_mutex
);
2777 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2779 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
2780 if (ucontrol
->value
.integer
.value
[0])
2781 val
|= AC_DIG1_ENABLE
;
2782 change
= spdif
->ctls
!= val
;
2784 if (change
&& nid
!= (u16
)-1)
2785 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
2786 mutex_unlock(&codec
->spdif_mutex
);
2790 static struct snd_kcontrol_new dig_mixes
[] = {
2792 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2793 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2794 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
2795 .info
= snd_hda_spdif_mask_info
,
2796 .get
= snd_hda_spdif_cmask_get
,
2799 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2800 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2801 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
2802 .info
= snd_hda_spdif_mask_info
,
2803 .get
= snd_hda_spdif_pmask_get
,
2806 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2807 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
2808 .info
= snd_hda_spdif_mask_info
,
2809 .get
= snd_hda_spdif_default_get
,
2810 .put
= snd_hda_spdif_default_put
,
2813 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2814 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
2815 .info
= snd_hda_spdif_out_switch_info
,
2816 .get
= snd_hda_spdif_out_switch_get
,
2817 .put
= snd_hda_spdif_out_switch_put
,
2823 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
2824 * @codec: the HDA codec
2825 * @associated_nid: NID that new ctls associated with
2826 * @cvt_nid: converter NID
2827 * @type: HDA_PCM_TYPE_*
2828 * Creates controls related with the digital output.
2829 * Called from each patch supporting the digital out.
2831 * Returns 0 if successful, or a negative error code.
2833 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
2834 hda_nid_t associated_nid
,
2839 struct snd_kcontrol
*kctl
;
2840 struct snd_kcontrol_new
*dig_mix
;
2842 const int spdif_index
= 16;
2843 struct hda_spdif_out
*spdif
;
2844 struct hda_bus
*bus
= codec
->bus
;
2846 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
2847 type
== HDA_PCM_TYPE_SPDIF
) {
2849 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
2850 type
== HDA_PCM_TYPE_HDMI
) {
2851 /* suppose a single SPDIF device */
2852 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2853 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
2856 kctl
->id
.index
= spdif_index
;
2858 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
2860 if (!bus
->primary_dig_out_type
)
2861 bus
->primary_dig_out_type
= type
;
2863 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
2865 codec_err(codec
, "too many IEC958 outputs\n");
2868 spdif
= snd_array_new(&codec
->spdif_out
);
2871 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2872 kctl
= snd_ctl_new1(dig_mix
, codec
);
2875 kctl
->id
.index
= idx
;
2876 kctl
->private_value
= codec
->spdif_out
.used
- 1;
2877 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
2881 spdif
->nid
= cvt_nid
;
2882 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
2883 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2884 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
2887 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
2890 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2891 * @codec: the HDA codec
2894 * call within spdif_mutex lock
2896 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
2900 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
2901 struct hda_spdif_out
*spdif
=
2902 snd_array_elem(&codec
->spdif_out
, i
);
2903 if (spdif
->nid
== nid
)
2908 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
2911 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2912 * @codec: the HDA codec
2913 * @idx: the SPDIF ctl index
2915 * Unassign the widget from the given SPDIF control.
2917 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
2919 struct hda_spdif_out
*spdif
;
2921 mutex_lock(&codec
->spdif_mutex
);
2922 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2923 spdif
->nid
= (u16
)-1;
2924 mutex_unlock(&codec
->spdif_mutex
);
2926 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
2929 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2930 * @codec: the HDA codec
2931 * @idx: the SPDIF ctl idx
2934 * Assign the widget to the SPDIF control with the given index.
2936 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
2938 struct hda_spdif_out
*spdif
;
2941 mutex_lock(&codec
->spdif_mutex
);
2942 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2943 if (spdif
->nid
!= nid
) {
2946 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
2948 mutex_unlock(&codec
->spdif_mutex
);
2950 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
2953 * SPDIF sharing with analog output
2955 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2956 struct snd_ctl_elem_value
*ucontrol
)
2958 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2959 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2963 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2964 struct snd_ctl_elem_value
*ucontrol
)
2966 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2967 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2971 static struct snd_kcontrol_new spdif_share_sw
= {
2972 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2973 .name
= "IEC958 Default PCM Playback Switch",
2974 .info
= snd_ctl_boolean_mono_info
,
2975 .get
= spdif_share_sw_get
,
2976 .put
= spdif_share_sw_put
,
2980 * snd_hda_create_spdif_share_sw - create Default PCM switch
2981 * @codec: the HDA codec
2982 * @mout: multi-out instance
2984 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2985 struct hda_multi_out
*mout
)
2987 struct snd_kcontrol
*kctl
;
2989 if (!mout
->dig_out_nid
)
2992 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
2995 /* ATTENTION: here mout is passed as private_data, instead of codec */
2996 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
2998 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
3004 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3006 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3007 struct snd_ctl_elem_value
*ucontrol
)
3009 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3011 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3015 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3016 struct snd_ctl_elem_value
*ucontrol
)
3018 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3019 hda_nid_t nid
= kcontrol
->private_value
;
3020 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3023 mutex_lock(&codec
->spdif_mutex
);
3024 change
= codec
->spdif_in_enable
!= val
;
3026 codec
->spdif_in_enable
= val
;
3027 snd_hda_codec_write_cache(codec
, nid
, 0,
3028 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3030 mutex_unlock(&codec
->spdif_mutex
);
3034 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3035 struct snd_ctl_elem_value
*ucontrol
)
3037 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3038 hda_nid_t nid
= kcontrol
->private_value
;
3042 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3043 sbits
= convert_to_spdif_status(val
);
3044 ucontrol
->value
.iec958
.status
[0] = sbits
;
3045 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3046 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3047 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3051 static struct snd_kcontrol_new dig_in_ctls
[] = {
3053 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3054 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3055 .info
= snd_hda_spdif_in_switch_info
,
3056 .get
= snd_hda_spdif_in_switch_get
,
3057 .put
= snd_hda_spdif_in_switch_put
,
3060 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3061 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3062 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3063 .info
= snd_hda_spdif_mask_info
,
3064 .get
= snd_hda_spdif_in_status_get
,
3070 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3071 * @codec: the HDA codec
3072 * @nid: audio in widget NID
3074 * Creates controls related with the SPDIF input.
3075 * Called from each patch supporting the SPDIF in.
3077 * Returns 0 if successful, or a negative error code.
3079 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3082 struct snd_kcontrol
*kctl
;
3083 struct snd_kcontrol_new
*dig_mix
;
3086 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3088 codec_err(codec
, "too many IEC958 inputs\n");
3091 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3092 kctl
= snd_ctl_new1(dig_mix
, codec
);
3095 kctl
->private_value
= nid
;
3096 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3100 codec
->spdif_in_enable
=
3101 snd_hda_codec_read(codec
, nid
, 0,
3102 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3106 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
3112 /* build a 31bit cache key with the widget id and the command parameter */
3113 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3114 #define get_cmd_cache_nid(key) ((key) & 0xff)
3115 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3118 * snd_hda_codec_write_cache - send a single command with caching
3119 * @codec: the HDA codec
3120 * @nid: NID to send the command
3121 * @flags: optional bit flags
3122 * @verb: the verb to send
3123 * @parm: the parameter for the verb
3125 * Send a single command without waiting for response.
3127 * Returns 0 if successful, or a negative error code.
3129 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3130 int flags
, unsigned int verb
, unsigned int parm
)
3133 struct hda_cache_head
*c
;
3135 unsigned int cache_only
;
3137 cache_only
= codec
->cached_write
;
3139 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3144 /* parm may contain the verb stuff for get/set amp */
3145 verb
= verb
| (parm
>> 8);
3147 key
= build_cmd_cache_key(nid
, verb
);
3148 mutex_lock(&codec
->bus
->core
.cmd_mutex
);
3149 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3152 c
->dirty
= cache_only
;
3154 mutex_unlock(&codec
->bus
->core
.cmd_mutex
);
3157 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache
);
3160 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3161 * @codec: the HDA codec
3162 * @nid: NID to send the command
3163 * @flags: optional bit flags
3164 * @verb: the verb to send
3165 * @parm: the parameter for the verb
3167 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3168 * command if the parameter is already identical with the cached value.
3169 * If not, it sends the command and refreshes the cache.
3171 * Returns 0 if successful, or a negative error code.
3173 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3174 int flags
, unsigned int verb
, unsigned int parm
)
3176 struct hda_cache_head
*c
;
3179 /* parm may contain the verb stuff for get/set amp */
3180 verb
= verb
| (parm
>> 8);
3182 key
= build_cmd_cache_key(nid
, verb
);
3183 mutex_lock(&codec
->bus
->core
.cmd_mutex
);
3184 c
= get_hash(&codec
->cmd_cache
, key
);
3185 if (c
&& c
->val
== parm
) {
3186 mutex_unlock(&codec
->bus
->core
.cmd_mutex
);
3189 mutex_unlock(&codec
->bus
->core
.cmd_mutex
);
3190 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3192 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache
);
3195 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3196 * @codec: HD-audio codec
3198 * Execute all verbs recorded in the command caches to resume.
3200 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3204 mutex_lock(&codec
->hash_mutex
);
3205 codec
->cached_write
= 0;
3206 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3207 struct hda_cache_head
*buffer
;
3210 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3217 mutex_unlock(&codec
->hash_mutex
);
3218 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3219 get_cmd_cache_cmd(key
), buffer
->val
);
3220 mutex_lock(&codec
->hash_mutex
);
3222 mutex_unlock(&codec
->hash_mutex
);
3224 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache
);
3227 * snd_hda_sequence_write_cache - sequence writes with caching
3228 * @codec: the HDA codec
3229 * @seq: VERB array to send
3231 * Send the commands sequentially from the given array.
3232 * Thte commands are recorded on cache for power-save and resume.
3233 * The array must be terminated with NID=0.
3235 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3236 const struct hda_verb
*seq
)
3238 for (; seq
->nid
; seq
++)
3239 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3242 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache
);
3245 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3246 * @codec: HD-audio codec
3248 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
3250 if (codec
->core
.regmap
)
3251 regcache_sync(codec
->core
.regmap
);
3252 snd_hda_codec_resume_cache(codec
);
3254 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache
);
3257 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3258 * @codec: the HDA codec
3259 * @fg: function group (not used now)
3260 * @power_state: the power state to set (AC_PWRST_*)
3262 * Set the given power state to all widgets that have the power control.
3263 * If the codec has power_filter set, it evaluates the power state and
3264 * filter out if it's unchanged as D3.
3266 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3267 unsigned int power_state
)
3271 for_each_hda_codec_node(nid
, codec
) {
3272 unsigned int wcaps
= get_wcaps(codec
, nid
);
3273 unsigned int state
= power_state
;
3274 if (!(wcaps
& AC_WCAP_POWER
))
3276 if (codec
->power_filter
) {
3277 state
= codec
->power_filter(codec
, nid
, power_state
);
3278 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
3281 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3285 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
3288 * wait until the state is reached, returns the current state
3290 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3292 unsigned int power_state
)
3294 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3295 unsigned int state
, actual_state
;
3298 state
= snd_hda_codec_read(codec
, fg
, 0,
3299 AC_VERB_GET_POWER_STATE
, 0);
3300 if (state
& AC_PWRST_ERROR
)
3302 actual_state
= (state
>> 4) & 0x0f;
3303 if (actual_state
== power_state
)
3305 if (time_after_eq(jiffies
, end_time
))
3307 /* wait until the codec reachs to the target state */
3314 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3315 * @codec: the HDA codec
3317 * @power_state: power state to evalue
3319 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3320 * This can be used a codec power_filter callback.
3322 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
3324 unsigned int power_state
)
3326 if (nid
== codec
->core
.afg
|| nid
== codec
->core
.mfg
)
3328 if (power_state
== AC_PWRST_D3
&&
3329 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
3330 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3331 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3332 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3338 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
3341 * set power state of the codec, and return the power state
3343 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3344 unsigned int power_state
)
3346 hda_nid_t fg
= codec
->core
.afg
? codec
->core
.afg
: codec
->core
.mfg
;
3351 /* this delay seems necessary to avoid click noise at power-down */
3352 if (power_state
== AC_PWRST_D3
) {
3353 if (codec
->depop_delay
< 0)
3354 msleep(codec_has_epss(codec
) ? 10 : 100);
3355 else if (codec
->depop_delay
> 0)
3356 msleep(codec
->depop_delay
);
3357 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
3360 /* repeat power states setting at most 10 times*/
3361 for (count
= 0; count
< 10; count
++) {
3362 if (codec
->patch_ops
.set_power_state
)
3363 codec
->patch_ops
.set_power_state(codec
, fg
,
3366 state
= power_state
;
3367 if (codec
->power_filter
)
3368 state
= codec
->power_filter(codec
, fg
, state
);
3369 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
3370 snd_hda_codec_read(codec
, fg
, flags
,
3371 AC_VERB_SET_POWER_STATE
,
3373 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
3375 state
= hda_sync_power_state(codec
, fg
, power_state
);
3376 if (!(state
& AC_PWRST_ERROR
))
3383 /* sync power states of all widgets;
3384 * this is called at the end of codec parsing
3386 static void sync_power_up_states(struct hda_codec
*codec
)
3390 /* don't care if no filter is used */
3391 if (!codec
->power_filter
)
3394 for_each_hda_codec_node(nid
, codec
) {
3395 unsigned int wcaps
= get_wcaps(codec
, nid
);
3396 unsigned int target
;
3397 if (!(wcaps
& AC_WCAP_POWER
))
3399 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
3400 if (target
== AC_PWRST_D0
)
3402 if (!snd_hda_check_power_state(codec
, nid
, target
))
3403 snd_hda_codec_write(codec
, nid
, 0,
3404 AC_VERB_SET_POWER_STATE
, target
);
3408 #ifdef CONFIG_SND_HDA_RECONFIG
3409 /* execute additional init verbs */
3410 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3412 if (codec
->init_verbs
.list
)
3413 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3416 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3420 /* update the power on/off account with the current jiffies */
3421 static void update_power_acct(struct hda_codec
*codec
, bool on
)
3423 unsigned long delta
= jiffies
- codec
->power_jiffies
;
3426 codec
->power_on_acct
+= delta
;
3428 codec
->power_off_acct
+= delta
;
3429 codec
->power_jiffies
+= delta
;
3432 void snd_hda_update_power_acct(struct hda_codec
*codec
)
3434 update_power_acct(codec
, hda_codec_is_power_on(codec
));
3438 * call suspend and power-down; used both from PM and power-save
3439 * this function returns the power state in the end
3441 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
)
3445 atomic_inc(&codec
->core
.in_pm
);
3447 if (codec
->patch_ops
.suspend
)
3448 codec
->patch_ops
.suspend(codec
);
3449 hda_cleanup_all_streams(codec
);
3450 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3451 update_power_acct(codec
, true);
3452 atomic_dec(&codec
->core
.in_pm
);
3456 /* mark all entries of cmd and amp caches dirty */
3457 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
3460 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3461 struct hda_cache_head
*cmd
;
3462 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3465 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
3466 struct hda_amp_info
*amp
;
3467 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
3468 amp
->head
.dirty
= 1;
3473 * kick up codec; used both from PM and power-save
3475 static void hda_call_codec_resume(struct hda_codec
*codec
)
3477 atomic_inc(&codec
->core
.in_pm
);
3479 if (codec
->core
.regmap
)
3480 regcache_mark_dirty(codec
->core
.regmap
);
3482 hda_mark_cmd_cache_dirty(codec
);
3484 codec
->power_jiffies
= jiffies
;
3486 hda_set_power_state(codec
, AC_PWRST_D0
);
3487 restore_shutup_pins(codec
);
3488 hda_exec_init_verbs(codec
);
3489 snd_hda_jack_set_dirty_all(codec
);
3490 if (codec
->patch_ops
.resume
)
3491 codec
->patch_ops
.resume(codec
);
3493 if (codec
->patch_ops
.init
)
3494 codec
->patch_ops
.init(codec
);
3495 if (codec
->core
.regmap
)
3496 regcache_sync(codec
->core
.regmap
);
3497 snd_hda_codec_resume_cache(codec
);
3500 if (codec
->jackpoll_interval
)
3501 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3503 snd_hda_jack_report_sync(codec
);
3504 atomic_dec(&codec
->core
.in_pm
);
3507 static int hda_codec_runtime_suspend(struct device
*dev
)
3509 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3510 struct hda_pcm
*pcm
;
3513 cancel_delayed_work_sync(&codec
->jackpoll_work
);
3514 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
)
3515 snd_pcm_suspend_all(pcm
->pcm
);
3516 state
= hda_call_codec_suspend(codec
);
3517 if (codec_has_clkstop(codec
) && codec_has_epss(codec
) &&
3518 (state
& AC_PWRST_CLK_STOP_OK
))
3519 snd_hdac_codec_link_down(&codec
->core
);
3523 static int hda_codec_runtime_resume(struct device
*dev
)
3525 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3527 snd_hdac_codec_link_up(&codec
->core
);
3528 hda_call_codec_resume(codec
);
3529 pm_runtime_mark_last_busy(dev
);
3532 #endif /* CONFIG_PM */
3534 /* referred in hda_bind.c */
3535 const struct dev_pm_ops hda_codec_driver_pm
= {
3536 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
3537 pm_runtime_force_resume
)
3538 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend
, hda_codec_runtime_resume
,
3543 * add standard channel maps if not specified
3545 static int add_std_chmaps(struct hda_codec
*codec
)
3547 struct hda_pcm
*pcm
;
3550 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
) {
3551 for (str
= 0; str
< 2; str
++) {
3552 struct hda_pcm_stream
*hinfo
= &pcm
->stream
[str
];
3553 struct snd_pcm_chmap
*chmap
;
3554 const struct snd_pcm_chmap_elem
*elem
;
3558 if (!pcm
|| !hinfo
->substreams
)
3560 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
3561 err
= snd_pcm_add_chmap_ctls(pcm
->pcm
, str
, elem
,
3562 hinfo
->channels_max
,
3566 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
3572 /* default channel maps for 2.1 speakers;
3573 * since HD-audio supports only stereo, odd number channels are omitted
3575 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
3577 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
3579 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
3580 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
3583 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
3585 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3588 hda_exec_init_verbs(codec
);
3589 /* continue to initialize... */
3590 if (codec
->patch_ops
.init
)
3591 err
= codec
->patch_ops
.init(codec
);
3592 if (!err
&& codec
->patch_ops
.build_controls
)
3593 err
= codec
->patch_ops
.build_controls(codec
);
3597 /* we create chmaps here instead of build_pcms */
3598 err
= add_std_chmaps(codec
);
3602 if (codec
->jackpoll_interval
)
3603 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3605 snd_hda_jack_report_sync(codec
); /* call at the last init point */
3606 sync_power_up_states(codec
);
3613 struct hda_rate_tbl
{
3615 unsigned int alsa_bits
;
3616 unsigned int hda_fmt
;
3619 /* rate = base * mult / div */
3620 #define HDA_RATE(base, mult, div) \
3621 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3622 (((div) - 1) << AC_FMT_DIV_SHIFT))
3624 static struct hda_rate_tbl rate_bits
[] = {
3625 /* rate in Hz, ALSA rate bitmask, HDA format value */
3627 /* autodetected value used in snd_hda_query_supported_pcm */
3628 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3629 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3630 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3631 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3632 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3633 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3634 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3635 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3636 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3637 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3638 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3639 #define AC_PAR_PCM_RATE_BITS 11
3640 /* up to bits 10, 384kHZ isn't supported properly */
3642 /* not autodetected value */
3643 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3645 { 0 } /* terminator */
3649 * snd_hda_calc_stream_format - calculate format bitset
3650 * @codec: HD-audio codec
3651 * @rate: the sample rate
3652 * @channels: the number of channels
3653 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3654 * @maxbps: the max. bps
3655 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
3657 * Calculate the format bitset from the given rate, channels and th PCM format.
3659 * Return zero if invalid.
3661 unsigned int snd_hda_calc_stream_format(struct hda_codec
*codec
,
3663 unsigned int channels
,
3664 unsigned int format
,
3665 unsigned int maxbps
,
3666 unsigned short spdif_ctls
)
3669 unsigned int val
= 0;
3671 for (i
= 0; rate_bits
[i
].hz
; i
++)
3672 if (rate_bits
[i
].hz
== rate
) {
3673 val
= rate_bits
[i
].hda_fmt
;
3676 if (!rate_bits
[i
].hz
) {
3677 codec_dbg(codec
, "invalid rate %d\n", rate
);
3681 if (channels
== 0 || channels
> 8) {
3682 codec_dbg(codec
, "invalid channels %d\n", channels
);
3685 val
|= channels
- 1;
3687 switch (snd_pcm_format_width(format
)) {
3689 val
|= AC_FMT_BITS_8
;
3692 val
|= AC_FMT_BITS_16
;
3697 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3698 val
|= AC_FMT_BITS_32
;
3699 else if (maxbps
>= 24)
3700 val
|= AC_FMT_BITS_24
;
3702 val
|= AC_FMT_BITS_20
;
3705 codec_dbg(codec
, "invalid format width %d\n",
3706 snd_pcm_format_width(format
));
3710 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3711 val
|= AC_FMT_TYPE_NON_PCM
;
3715 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format
);
3717 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
3720 unsigned int val
= 0;
3721 if (nid
!= codec
->core
.afg
&&
3722 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3723 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3724 if (!val
|| val
== -1)
3725 val
= snd_hda_param_read(codec
, codec
->core
.afg
, AC_PAR_PCM
);
3726 if (!val
|| val
== -1)
3731 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3733 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
3737 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
3740 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3741 if (!streams
|| streams
== -1)
3742 streams
= snd_hda_param_read(codec
, codec
->core
.afg
, AC_PAR_STREAM
);
3743 if (!streams
|| streams
== -1)
3748 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3750 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
3755 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3756 * @codec: the HDA codec
3757 * @nid: NID to query
3758 * @ratesp: the pointer to store the detected rate bitflags
3759 * @formatsp: the pointer to store the detected formats
3760 * @bpsp: the pointer to store the detected format widths
3762 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3763 * or @bsps argument is ignored.
3765 * Returns 0 if successful, otherwise a negative error code.
3767 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3768 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3770 unsigned int i
, val
, wcaps
;
3772 wcaps
= get_wcaps(codec
, nid
);
3773 val
= query_pcm_param(codec
, nid
);
3777 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3779 rates
|= rate_bits
[i
].alsa_bits
;
3783 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
3785 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3791 if (formatsp
|| bpsp
) {
3793 unsigned int streams
, bps
;
3795 streams
= query_stream_param(codec
, nid
);
3800 if (streams
& AC_SUPFMT_PCM
) {
3801 if (val
& AC_SUPPCM_BITS_8
) {
3802 formats
|= SNDRV_PCM_FMTBIT_U8
;
3805 if (val
& AC_SUPPCM_BITS_16
) {
3806 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3809 if (wcaps
& AC_WCAP_DIGITAL
) {
3810 if (val
& AC_SUPPCM_BITS_32
)
3811 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3812 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3813 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3814 if (val
& AC_SUPPCM_BITS_24
)
3816 else if (val
& AC_SUPPCM_BITS_20
)
3818 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3819 AC_SUPPCM_BITS_32
)) {
3820 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3821 if (val
& AC_SUPPCM_BITS_32
)
3823 else if (val
& AC_SUPPCM_BITS_24
)
3825 else if (val
& AC_SUPPCM_BITS_20
)
3829 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
3830 if (streams
& AC_SUPFMT_FLOAT32
) {
3831 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3836 if (streams
== AC_SUPFMT_AC3
) {
3837 /* should be exclusive */
3838 /* temporary hack: we have still no proper support
3839 * for the direct AC3 stream...
3841 formats
|= SNDRV_PCM_FMTBIT_U8
;
3846 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
3848 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3853 *formatsp
= formats
;
3860 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm
);
3863 * snd_hda_is_supported_format - Check the validity of the format
3864 * @codec: HD-audio codec
3865 * @nid: NID to check
3866 * @format: the HD-audio format value to check
3868 * Check whether the given node supports the format value.
3870 * Returns 1 if supported, 0 if not.
3872 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3873 unsigned int format
)
3876 unsigned int val
= 0, rate
, stream
;
3878 val
= query_pcm_param(codec
, nid
);
3882 rate
= format
& 0xff00;
3883 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3884 if (rate_bits
[i
].hda_fmt
== rate
) {
3889 if (i
>= AC_PAR_PCM_RATE_BITS
)
3892 stream
= query_stream_param(codec
, nid
);
3896 if (stream
& AC_SUPFMT_PCM
) {
3897 switch (format
& 0xf0) {
3899 if (!(val
& AC_SUPPCM_BITS_8
))
3903 if (!(val
& AC_SUPPCM_BITS_16
))
3907 if (!(val
& AC_SUPPCM_BITS_20
))
3911 if (!(val
& AC_SUPPCM_BITS_24
))
3915 if (!(val
& AC_SUPPCM_BITS_32
))
3922 /* FIXME: check for float32 and AC3? */
3927 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format
);
3932 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3933 struct hda_codec
*codec
,
3934 struct snd_pcm_substream
*substream
)
3939 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3940 struct hda_codec
*codec
,
3941 unsigned int stream_tag
,
3942 unsigned int format
,
3943 struct snd_pcm_substream
*substream
)
3945 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3949 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3950 struct hda_codec
*codec
,
3951 struct snd_pcm_substream
*substream
)
3953 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3957 static int set_pcm_default_values(struct hda_codec
*codec
,
3958 struct hda_pcm_stream
*info
)
3962 /* query support PCM information from the given NID */
3963 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3964 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3965 info
->rates
? NULL
: &info
->rates
,
3966 info
->formats
? NULL
: &info
->formats
,
3967 info
->maxbps
? NULL
: &info
->maxbps
);
3971 if (info
->ops
.open
== NULL
)
3972 info
->ops
.open
= hda_pcm_default_open_close
;
3973 if (info
->ops
.close
== NULL
)
3974 info
->ops
.close
= hda_pcm_default_open_close
;
3975 if (info
->ops
.prepare
== NULL
) {
3976 if (snd_BUG_ON(!info
->nid
))
3978 info
->ops
.prepare
= hda_pcm_default_prepare
;
3980 if (info
->ops
.cleanup
== NULL
) {
3981 if (snd_BUG_ON(!info
->nid
))
3983 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
3989 * codec prepare/cleanup entries
3992 * snd_hda_codec_prepare - Prepare a stream
3993 * @codec: the HDA codec
3994 * @hinfo: PCM information
3995 * @stream: stream tag to assign
3996 * @format: format id to assign
3997 * @substream: PCM substream to assign
3999 * Calls the prepare callback set by the codec with the given arguments.
4000 * Clean up the inactive streams when successful.
4002 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4003 struct hda_pcm_stream
*hinfo
,
4004 unsigned int stream
,
4005 unsigned int format
,
4006 struct snd_pcm_substream
*substream
)
4009 mutex_lock(&codec
->bus
->prepare_mutex
);
4010 if (hinfo
->ops
.prepare
)
4011 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
,
4016 purify_inactive_streams(codec
);
4017 mutex_unlock(&codec
->bus
->prepare_mutex
);
4020 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
4023 * snd_hda_codec_cleanup - Prepare a stream
4024 * @codec: the HDA codec
4025 * @hinfo: PCM information
4026 * @substream: PCM substream
4028 * Calls the cleanup callback set by the codec with the given arguments.
4030 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4031 struct hda_pcm_stream
*hinfo
,
4032 struct snd_pcm_substream
*substream
)
4034 mutex_lock(&codec
->bus
->prepare_mutex
);
4035 if (hinfo
->ops
.cleanup
)
4036 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4037 mutex_unlock(&codec
->bus
->prepare_mutex
);
4039 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
4042 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4043 "Audio", "SPDIF", "HDMI", "Modem"
4047 * get the empty PCM device number to assign
4049 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4051 /* audio device indices; not linear to keep compatibility */
4052 /* assigned to static slots up to dev#10; if more needed, assign
4053 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4055 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4056 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4057 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4058 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4059 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4063 if (type
>= HDA_PCM_NTYPES
) {
4064 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
4068 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4069 #ifndef CONFIG_SND_DYNAMIC_MINORS
4070 if (audio_idx
[type
][i
] >= 8)
4073 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4074 return audio_idx
[type
][i
];
4077 #ifdef CONFIG_SND_DYNAMIC_MINORS
4078 /* non-fixed slots starting from 10 */
4079 for (i
= 10; i
< 32; i
++) {
4080 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4085 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
4086 snd_hda_pcm_type_name
[type
]);
4087 #ifndef CONFIG_SND_DYNAMIC_MINORS
4088 dev_warn(bus
->card
->dev
,
4089 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4094 /* call build_pcms ops of the given codec and set up the default parameters */
4095 int snd_hda_codec_parse_pcms(struct hda_codec
*codec
)
4097 struct hda_pcm
*cpcm
;
4100 if (!list_empty(&codec
->pcm_list_head
))
4101 return 0; /* already parsed */
4103 if (!codec
->patch_ops
.build_pcms
)
4106 err
= codec
->patch_ops
.build_pcms(codec
);
4108 codec_err(codec
, "cannot build PCMs for #%d (error %d)\n",
4109 codec
->core
.addr
, err
);
4113 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4116 for (stream
= 0; stream
< 2; stream
++) {
4117 struct hda_pcm_stream
*info
= &cpcm
->stream
[stream
];
4119 if (!info
->substreams
)
4121 err
= set_pcm_default_values(codec
, info
);
4124 "fail to setup default for PCM %s\n",
4134 /* assign all PCMs of the given codec */
4135 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4137 struct hda_bus
*bus
= codec
->bus
;
4138 struct hda_pcm
*cpcm
;
4141 if (snd_BUG_ON(!bus
->ops
.attach_pcm
))
4144 err
= snd_hda_codec_parse_pcms(codec
);
4146 snd_hda_codec_reset(codec
);
4150 /* attach a new PCM streams */
4151 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4153 continue; /* already attached */
4154 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4155 continue; /* no substreams assigned */
4157 dev
= get_empty_pcm_device(bus
, cpcm
->pcm_type
);
4159 continue; /* no fatal error */
4161 err
= bus
->ops
.attach_pcm(bus
, codec
, cpcm
);
4164 "cannot attach PCM stream %d for codec #%d\n",
4165 dev
, codec
->core
.addr
);
4166 continue; /* no fatal error */
4174 * snd_hda_add_new_ctls - create controls from the array
4175 * @codec: the HDA codec
4176 * @knew: the array of struct snd_kcontrol_new
4178 * This helper function creates and add new controls in the given array.
4179 * The array must be terminated with an empty entry as terminator.
4181 * Returns 0 if successful, or a negative error code.
4183 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4184 const struct snd_kcontrol_new
*knew
)
4188 for (; knew
->name
; knew
++) {
4189 struct snd_kcontrol
*kctl
;
4190 int addr
= 0, idx
= 0;
4191 if (knew
->iface
== -1) /* skip this codec private value */
4194 kctl
= snd_ctl_new1(knew
, codec
);
4198 kctl
->id
.device
= addr
;
4200 kctl
->id
.index
= idx
;
4201 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4204 /* try first with another device index corresponding to
4205 * the codec addr; if it still fails (or it's the
4206 * primary codec), then try another control index
4208 if (!addr
&& codec
->core
.addr
)
4209 addr
= codec
->core
.addr
;
4210 else if (!idx
&& !knew
->index
) {
4211 idx
= find_empty_mixer_ctl_idx(codec
,
4221 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
4224 static void codec_set_power_save(struct hda_codec
*codec
, int delay
)
4226 struct device
*dev
= hda_codec_dev(codec
);
4229 pm_runtime_set_autosuspend_delay(dev
, delay
);
4230 pm_runtime_use_autosuspend(dev
);
4231 pm_runtime_allow(dev
);
4232 if (!pm_runtime_suspended(dev
))
4233 pm_runtime_mark_last_busy(dev
);
4235 pm_runtime_dont_use_autosuspend(dev
);
4236 pm_runtime_forbid(dev
);
4241 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4242 * @bus: HD-audio bus
4243 * @delay: autosuspend delay in msec, 0 = off
4245 * Synchronize the runtime PM autosuspend state from the power_save option.
4247 void snd_hda_set_power_save(struct hda_bus
*bus
, int delay
)
4249 struct hda_codec
*c
;
4251 list_for_each_codec(c
, bus
)
4252 codec_set_power_save(c
, delay
);
4254 EXPORT_SYMBOL_GPL(snd_hda_set_power_save
);
4257 * snd_hda_check_amp_list_power - Check the amp list and update the power
4258 * @codec: HD-audio codec
4259 * @check: the object containing an AMP list and the status
4260 * @nid: NID to check / update
4262 * Check whether the given NID is in the amp list. If it's in the list,
4263 * check the current AMP status, and update the the power-status according
4264 * to the mute status.
4266 * This function is supposed to be set or called from the check_power_status
4269 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4270 struct hda_loopback_check
*check
,
4273 const struct hda_amp_list
*p
;
4276 if (!check
->amplist
)
4278 for (p
= check
->amplist
; p
->nid
; p
++) {
4283 return 0; /* nothing changed */
4285 for (p
= check
->amplist
; p
->nid
; p
++) {
4286 for (ch
= 0; ch
< 2; ch
++) {
4287 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4289 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4290 if (!check
->power_on
) {
4291 check
->power_on
= 1;
4292 snd_hda_power_up(codec
);
4298 if (check
->power_on
) {
4299 check
->power_on
= 0;
4300 snd_hda_power_down(codec
);
4304 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
4312 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4313 * @imux: imux helper object
4314 * @uinfo: pointer to get/store the data
4316 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4317 struct snd_ctl_elem_info
*uinfo
)
4321 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4323 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4324 if (!imux
->num_items
)
4326 index
= uinfo
->value
.enumerated
.item
;
4327 if (index
>= imux
->num_items
)
4328 index
= imux
->num_items
- 1;
4329 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4332 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
4335 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4336 * @codec: the HDA codec
4337 * @imux: imux helper object
4338 * @ucontrol: pointer to get/store the data
4339 * @nid: input mux NID
4340 * @cur_val: pointer to get/store the current imux value
4342 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4343 const struct hda_input_mux
*imux
,
4344 struct snd_ctl_elem_value
*ucontrol
,
4346 unsigned int *cur_val
)
4350 if (!imux
->num_items
)
4352 idx
= ucontrol
->value
.enumerated
.item
[0];
4353 if (idx
>= imux
->num_items
)
4354 idx
= imux
->num_items
- 1;
4355 if (*cur_val
== idx
)
4357 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4358 imux
->items
[idx
].index
);
4362 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
4366 * snd_hda_enum_helper_info - Helper for simple enum ctls
4367 * @kcontrol: ctl element
4368 * @uinfo: pointer to get/store the data
4369 * @num_items: number of enum items
4370 * @texts: enum item string array
4372 * process kcontrol info callback of a simple string enum array
4373 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4375 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
4376 struct snd_ctl_elem_info
*uinfo
,
4377 int num_items
, const char * const *texts
)
4379 static const char * const texts_default
[] = {
4380 "Disabled", "Enabled"
4383 if (!texts
|| !num_items
) {
4385 texts
= texts_default
;
4388 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
4390 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
4393 * Multi-channel / digital-out PCM helper functions
4396 /* setup SPDIF output stream */
4397 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4398 unsigned int stream_tag
, unsigned int format
)
4400 struct hda_spdif_out
*spdif
;
4401 unsigned int curr_fmt
;
4404 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4405 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
4406 AC_VERB_GET_STREAM_FORMAT
, 0);
4407 reset
= codec
->spdif_status_reset
&&
4408 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
4411 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4414 set_dig_out_convert(codec
, nid
,
4415 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4417 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4418 if (codec
->slave_dig_outs
) {
4420 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4421 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4424 /* turn on again (if needed) */
4426 set_dig_out_convert(codec
, nid
,
4427 spdif
->ctls
& 0xff, -1);
4430 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4432 snd_hda_codec_cleanup_stream(codec
, nid
);
4433 if (codec
->slave_dig_outs
) {
4435 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4436 snd_hda_codec_cleanup_stream(codec
, *d
);
4441 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4442 * @codec: the HDA codec
4443 * @mout: hda_multi_out object
4445 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4446 struct hda_multi_out
*mout
)
4448 mutex_lock(&codec
->spdif_mutex
);
4449 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4450 /* already opened as analog dup; reset it once */
4451 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4452 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4453 mutex_unlock(&codec
->spdif_mutex
);
4456 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
4459 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4460 * @codec: the HDA codec
4461 * @mout: hda_multi_out object
4462 * @stream_tag: stream tag to assign
4463 * @format: format id to assign
4464 * @substream: PCM substream to assign
4466 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4467 struct hda_multi_out
*mout
,
4468 unsigned int stream_tag
,
4469 unsigned int format
,
4470 struct snd_pcm_substream
*substream
)
4472 mutex_lock(&codec
->spdif_mutex
);
4473 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4474 mutex_unlock(&codec
->spdif_mutex
);
4477 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
4480 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4481 * @codec: the HDA codec
4482 * @mout: hda_multi_out object
4484 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4485 struct hda_multi_out
*mout
)
4487 mutex_lock(&codec
->spdif_mutex
);
4488 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4489 mutex_unlock(&codec
->spdif_mutex
);
4492 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
4495 * snd_hda_multi_out_dig_close - release the digital out stream
4496 * @codec: the HDA codec
4497 * @mout: hda_multi_out object
4499 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4500 struct hda_multi_out
*mout
)
4502 mutex_lock(&codec
->spdif_mutex
);
4503 mout
->dig_out_used
= 0;
4504 mutex_unlock(&codec
->spdif_mutex
);
4507 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
4510 * snd_hda_multi_out_analog_open - open analog outputs
4511 * @codec: the HDA codec
4512 * @mout: hda_multi_out object
4513 * @substream: PCM substream to assign
4514 * @hinfo: PCM information to assign
4516 * Open analog outputs and set up the hw-constraints.
4517 * If the digital outputs can be opened as slave, open the digital
4520 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4521 struct hda_multi_out
*mout
,
4522 struct snd_pcm_substream
*substream
,
4523 struct hda_pcm_stream
*hinfo
)
4525 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4526 runtime
->hw
.channels_max
= mout
->max_channels
;
4527 if (mout
->dig_out_nid
) {
4528 if (!mout
->analog_rates
) {
4529 mout
->analog_rates
= hinfo
->rates
;
4530 mout
->analog_formats
= hinfo
->formats
;
4531 mout
->analog_maxbps
= hinfo
->maxbps
;
4533 runtime
->hw
.rates
= mout
->analog_rates
;
4534 runtime
->hw
.formats
= mout
->analog_formats
;
4535 hinfo
->maxbps
= mout
->analog_maxbps
;
4537 if (!mout
->spdif_rates
) {
4538 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4540 &mout
->spdif_formats
,
4541 &mout
->spdif_maxbps
);
4543 mutex_lock(&codec
->spdif_mutex
);
4544 if (mout
->share_spdif
) {
4545 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4546 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4547 runtime
->hw
.rates
&= mout
->spdif_rates
;
4548 runtime
->hw
.formats
&= mout
->spdif_formats
;
4549 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4550 hinfo
->maxbps
= mout
->spdif_maxbps
;
4552 mout
->share_spdif
= 0;
4553 /* FIXME: need notify? */
4556 mutex_unlock(&codec
->spdif_mutex
);
4558 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4559 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4561 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
4564 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4565 * @codec: the HDA codec
4566 * @mout: hda_multi_out object
4567 * @stream_tag: stream tag to assign
4568 * @format: format id to assign
4569 * @substream: PCM substream to assign
4571 * Set up the i/o for analog out.
4572 * When the digital out is available, copy the front out to digital out, too.
4574 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4575 struct hda_multi_out
*mout
,
4576 unsigned int stream_tag
,
4577 unsigned int format
,
4578 struct snd_pcm_substream
*substream
)
4580 const hda_nid_t
*nids
= mout
->dac_nids
;
4581 int chs
= substream
->runtime
->channels
;
4582 struct hda_spdif_out
*spdif
;
4585 mutex_lock(&codec
->spdif_mutex
);
4586 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
4587 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4588 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4590 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4592 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
4593 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4594 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4595 stream_tag
, format
);
4597 mout
->dig_out_used
= 0;
4598 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4601 mutex_unlock(&codec
->spdif_mutex
);
4604 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4606 if (!mout
->no_share_stream
&&
4607 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4608 /* headphone out will just decode front left/right (stereo) */
4609 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4611 /* extra outputs copied from front */
4612 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4613 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
4614 snd_hda_codec_setup_stream(codec
,
4615 mout
->hp_out_nid
[i
],
4616 stream_tag
, 0, format
);
4619 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4620 if (chs
>= (i
+ 1) * 2) /* independent out */
4621 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4623 else if (!mout
->no_share_stream
) /* copy front */
4624 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4628 /* extra surrounds */
4629 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
4631 if (!mout
->extra_out_nid
[i
])
4633 if (chs
>= (i
+ 1) * 2)
4635 else if (!mout
->no_share_stream
)
4637 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
4638 stream_tag
, ch
, format
);
4643 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
4646 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4647 * @codec: the HDA codec
4648 * @mout: hda_multi_out object
4650 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4651 struct hda_multi_out
*mout
)
4653 const hda_nid_t
*nids
= mout
->dac_nids
;
4656 for (i
= 0; i
< mout
->num_dacs
; i
++)
4657 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4659 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4660 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4661 if (mout
->hp_out_nid
[i
])
4662 snd_hda_codec_cleanup_stream(codec
,
4663 mout
->hp_out_nid
[i
]);
4664 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4665 if (mout
->extra_out_nid
[i
])
4666 snd_hda_codec_cleanup_stream(codec
,
4667 mout
->extra_out_nid
[i
]);
4668 mutex_lock(&codec
->spdif_mutex
);
4669 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4670 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4671 mout
->dig_out_used
= 0;
4673 mutex_unlock(&codec
->spdif_mutex
);
4676 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
4679 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4680 * @codec: the HDA codec
4681 * @pin: referred pin NID
4683 * Guess the suitable VREF pin bits to be set as the pin-control value.
4684 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4686 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
4688 unsigned int pincap
;
4689 unsigned int oldval
;
4690 oldval
= snd_hda_codec_read(codec
, pin
, 0,
4691 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
4692 pincap
= snd_hda_query_pin_caps(codec
, pin
);
4693 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
4694 /* Exception: if the default pin setup is vref50, we give it priority */
4695 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
4696 return AC_PINCTL_VREF_80
;
4697 else if (pincap
& AC_PINCAP_VREF_50
)
4698 return AC_PINCTL_VREF_50
;
4699 else if (pincap
& AC_PINCAP_VREF_100
)
4700 return AC_PINCTL_VREF_100
;
4701 else if (pincap
& AC_PINCAP_VREF_GRD
)
4702 return AC_PINCTL_VREF_GRD
;
4703 return AC_PINCTL_VREF_HIZ
;
4705 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
4708 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
4709 * @codec: the HDA codec
4710 * @pin: referred pin NID
4711 * @val: pin ctl value to audit
4713 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
4714 hda_nid_t pin
, unsigned int val
)
4716 static unsigned int cap_lists
[][2] = {
4717 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
4718 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
4719 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
4720 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
4726 cap
= snd_hda_query_pin_caps(codec
, pin
);
4728 return val
; /* don't know what to do... */
4730 if (val
& AC_PINCTL_OUT_EN
) {
4731 if (!(cap
& AC_PINCAP_OUT
))
4732 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
4733 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
4734 val
&= ~AC_PINCTL_HP_EN
;
4737 if (val
& AC_PINCTL_IN_EN
) {
4738 if (!(cap
& AC_PINCAP_IN
))
4739 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
4741 unsigned int vcap
, vref
;
4743 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
4744 vref
= val
& AC_PINCTL_VREFEN
;
4745 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
4746 if (vref
== cap_lists
[i
][0] &&
4747 !(vcap
& cap_lists
[i
][1])) {
4748 if (i
== ARRAY_SIZE(cap_lists
) - 1)
4749 vref
= AC_PINCTL_VREF_HIZ
;
4751 vref
= cap_lists
[i
+ 1][0];
4754 val
&= ~AC_PINCTL_VREFEN
;
4761 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
4764 * _snd_hda_pin_ctl - Helper to set pin ctl value
4765 * @codec: the HDA codec
4766 * @pin: referred pin NID
4767 * @val: pin control value to set
4768 * @cached: access over codec pinctl cache or direct write
4770 * This function is a helper to set a pin ctl value more safely.
4771 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4772 * value in pin target array via snd_hda_codec_set_pin_target(), then
4773 * actually writes the value via either snd_hda_codec_update_cache() or
4774 * snd_hda_codec_write() depending on @cached flag.
4776 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
4777 unsigned int val
, bool cached
)
4779 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
4780 snd_hda_codec_set_pin_target(codec
, pin
, val
);
4782 return snd_hda_codec_update_cache(codec
, pin
, 0,
4783 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
4785 return snd_hda_codec_write(codec
, pin
, 0,
4786 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
4788 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
4791 * snd_hda_add_imux_item - Add an item to input_mux
4792 * @codec: the HDA codec
4793 * @imux: imux helper object
4794 * @label: the name of imux item to assign
4795 * @index: index number of imux item to assign
4796 * @type_idx: pointer to store the resultant label index
4798 * When the same label is used already in the existing items, the number
4799 * suffix is appended to the label. This label index number is stored
4800 * to type_idx when non-NULL pointer is given.
4802 int snd_hda_add_imux_item(struct hda_codec
*codec
,
4803 struct hda_input_mux
*imux
, const char *label
,
4804 int index
, int *type_idx
)
4806 int i
, label_idx
= 0;
4807 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
4808 codec_err(codec
, "hda_codec: Too many imux items!\n");
4811 for (i
= 0; i
< imux
->num_items
; i
++) {
4812 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
4816 *type_idx
= label_idx
;
4818 snprintf(imux
->items
[imux
->num_items
].label
,
4819 sizeof(imux
->items
[imux
->num_items
].label
),
4820 "%s %d", label
, label_idx
);
4822 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
4823 sizeof(imux
->items
[imux
->num_items
].label
));
4824 imux
->items
[imux
->num_items
].index
= index
;
4828 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
4831 * snd_hda_bus_reset - Reset the bus
4832 * @bus: HD-audio bus
4834 void snd_hda_bus_reset(struct hda_bus
*bus
)
4836 struct hda_codec
*codec
;
4838 list_for_each_codec(codec
, bus
) {
4839 /* FIXME: maybe a better way needed for forced reset */
4840 cancel_delayed_work_sync(&codec
->jackpoll_work
);
4842 if (hda_codec_is_power_on(codec
)) {
4843 hda_call_codec_suspend(codec
);
4844 hda_call_codec_resume(codec
);
4849 EXPORT_SYMBOL_GPL(snd_hda_bus_reset
);
4852 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4853 * @pcm: PCM caps bits
4854 * @buf: the string buffer to write
4855 * @buflen: the max buffer length
4857 * used by hda_proc.c and hda_eld.c
4859 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
4861 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
4864 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
4865 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
4866 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
4868 buf
[j
] = '\0'; /* necessary when j == 0 */
4870 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
4872 MODULE_DESCRIPTION("HDA codec core");
4873 MODULE_LICENSE("GPL");