| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Universal Interface for Intel High Definition Audio Codec | 
 | 3 |  * | 
 | 4 |  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> | 
 | 5 |  * | 
 | 6 |  * | 
 | 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. | 
 | 11 |  * | 
 | 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. | 
 | 16 |  * | 
 | 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 | 
 | 20 |  */ | 
 | 21 |  | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 22 | #include <linux/mm.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> | 
 | 24 | #include <linux/delay.h> | 
 | 25 | #include <linux/slab.h> | 
 | 26 | #include <linux/pci.h> | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> | 
| Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 28 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <sound/core.h> | 
 | 30 | #include "hda_codec.h" | 
 | 31 | #include <sound/asoundef.h> | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 32 | #include <sound/tlv.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <sound/initval.h> | 
| Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 34 | #include <sound/jack.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "hda_local.h" | 
| Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 36 | #include "hda_beep.h" | 
| Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 37 | #include "hda_jack.h" | 
| Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 38 | #include <sound/hda_hwdep.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 40 | #define CREATE_TRACE_POINTS | 
 | 41 | #include "hda_trace.h" | 
 | 42 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* | 
 | 44 |  * vendor / preset table | 
 | 45 |  */ | 
 | 46 |  | 
 | 47 | struct hda_vendor_id { | 
 | 48 | 	unsigned int id; | 
 | 49 | 	const char *name; | 
 | 50 | }; | 
 | 51 |  | 
 | 52 | /* codec vendor labels */ | 
 | 53 | static struct hda_vendor_id hda_vendor_ids[] = { | 
| Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 54 | 	{ 0x1002, "ATI" }, | 
| Takashi Iwai | e5f1424 | 2009-07-01 18:11:44 +0200 | [diff] [blame] | 55 | 	{ 0x1013, "Cirrus Logic" }, | 
| Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 56 | 	{ 0x1057, "Motorola" }, | 
| Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 57 | 	{ 0x1095, "Silicon Image" }, | 
| Takashi Iwai | 31117b7 | 2008-12-16 14:43:21 +0100 | [diff] [blame] | 58 | 	{ 0x10de, "Nvidia" }, | 
| Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 59 | 	{ 0x10ec, "Realtek" }, | 
| Takashi Iwai | 4e01f54 | 2009-04-16 08:53:34 +0200 | [diff] [blame] | 60 | 	{ 0x1102, "Creative" }, | 
| Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 61 | 	{ 0x1106, "VIA" }, | 
| Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 62 | 	{ 0x111d, "IDT" }, | 
| Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 63 | 	{ 0x11c1, "LSI" }, | 
| Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 64 | 	{ 0x11d4, "Analog Devices" }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 	{ 0x13f6, "C-Media" }, | 
| Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 66 | 	{ 0x14f1, "Conexant" }, | 
| Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 67 | 	{ 0x17e8, "Chrontel" }, | 
 | 68 | 	{ 0x1854, "LG" }, | 
| Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 69 | 	{ 0x1aec, "Wolfson Microelectronics" }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 	{ 0x434d, "C-Media" }, | 
| Takashi Iwai | 74c6113 | 2008-12-18 09:11:33 +0100 | [diff] [blame] | 71 | 	{ 0x8086, "Intel" }, | 
| Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 72 | 	{ 0x8384, "SigmaTel" }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | 	{} /* terminator */ | 
 | 74 | }; | 
 | 75 |  | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 76 | static DEFINE_MUTEX(preset_mutex); | 
 | 77 | static LIST_HEAD(hda_preset_tables); | 
 | 78 |  | 
 | 79 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) | 
 | 80 | { | 
 | 81 | 	mutex_lock(&preset_mutex); | 
 | 82 | 	list_add_tail(&preset->list, &hda_preset_tables); | 
 | 83 | 	mutex_unlock(&preset_mutex); | 
 | 84 | 	return 0; | 
 | 85 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 86 | EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset); | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 87 |  | 
 | 88 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) | 
 | 89 | { | 
 | 90 | 	mutex_lock(&preset_mutex); | 
 | 91 | 	list_del(&preset->list); | 
 | 92 | 	mutex_unlock(&preset_mutex); | 
 | 93 | 	return 0; | 
 | 94 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 95 | EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 |  | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_PM | 
| Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 98 | #define codec_in_pm(codec)	((codec)->in_pm) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 99 | static void hda_power_work(struct work_struct *work); | 
 | 100 | static void hda_keep_power_on(struct hda_codec *codec); | 
| Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 101 | #define hda_codec_is_power_on(codec)	((codec)->power_on) | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 102 | static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up) | 
 | 103 | { | 
 | 104 | 	if (bus->ops.pm_notify) | 
 | 105 | 		bus->ops.pm_notify(bus, power_up); | 
 | 106 | } | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 107 | #else | 
| Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 108 | #define codec_in_pm(codec)	0 | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 109 | static inline void hda_keep_power_on(struct hda_codec *codec) {} | 
| Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 110 | #define hda_codec_is_power_on(codec)	1 | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 111 | #define hda_call_pm_notify(bus, state) {} | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 112 | #endif | 
 | 113 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 114 | /** | 
 | 115 |  * snd_hda_get_jack_location - Give a location string of the jack | 
 | 116 |  * @cfg: pin default config value | 
 | 117 |  * | 
 | 118 |  * Parse the pin default config value and returns the string of the | 
 | 119 |  * jack location, e.g. "Rear", "Front", etc. | 
 | 120 |  */ | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 121 | const char *snd_hda_get_jack_location(u32 cfg) | 
 | 122 | { | 
 | 123 | 	static char *bases[7] = { | 
 | 124 | 		"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", | 
 | 125 | 	}; | 
 | 126 | 	static unsigned char specials_idx[] = { | 
 | 127 | 		0x07, 0x08, | 
 | 128 | 		0x17, 0x18, 0x19, | 
 | 129 | 		0x37, 0x38 | 
 | 130 | 	}; | 
 | 131 | 	static char *specials[] = { | 
 | 132 | 		"Rear Panel", "Drive Bar", | 
 | 133 | 		"Riser", "HDMI", "ATAPI", | 
 | 134 | 		"Mobile-In", "Mobile-Out" | 
 | 135 | 	}; | 
 | 136 | 	int i; | 
 | 137 | 	cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; | 
 | 138 | 	if ((cfg & 0x0f) < 7) | 
 | 139 | 		return bases[cfg & 0x0f]; | 
 | 140 | 	for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { | 
 | 141 | 		if (cfg == specials_idx[i]) | 
 | 142 | 			return specials[i]; | 
 | 143 | 	} | 
 | 144 | 	return "UNKNOWN"; | 
 | 145 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 146 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 147 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 148 | /** | 
 | 149 |  * snd_hda_get_jack_connectivity - Give a connectivity string of the jack | 
 | 150 |  * @cfg: pin default config value | 
 | 151 |  * | 
 | 152 |  * Parse the pin default config value and returns the string of the | 
 | 153 |  * jack connectivity, i.e. external or internal connection. | 
 | 154 |  */ | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 155 | const char *snd_hda_get_jack_connectivity(u32 cfg) | 
 | 156 | { | 
 | 157 | 	static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; | 
 | 158 |  | 
 | 159 | 	return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; | 
 | 160 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 161 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 162 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 163 | /** | 
 | 164 |  * snd_hda_get_jack_type - Give a type string of the jack | 
 | 165 |  * @cfg: pin default config value | 
 | 166 |  * | 
 | 167 |  * Parse the pin default config value and returns the string of the | 
 | 168 |  * jack type, i.e. the purpose of the jack, such as Line-Out or CD. | 
 | 169 |  */ | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 170 | const char *snd_hda_get_jack_type(u32 cfg) | 
 | 171 | { | 
 | 172 | 	static char *jack_types[16] = { | 
 | 173 | 		"Line Out", "Speaker", "HP Out", "CD", | 
 | 174 | 		"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", | 
 | 175 | 		"Line In", "Aux", "Mic", "Telephony", | 
 | 176 | 		"SPDIF In", "Digitial In", "Reserved", "Other" | 
 | 177 | 	}; | 
 | 178 |  | 
 | 179 | 	return jack_types[(cfg & AC_DEFCFG_DEVICE) | 
 | 180 | 				>> AC_DEFCFG_DEVICE_SHIFT]; | 
 | 181 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 182 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); | 
| Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 183 |  | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 184 | /* | 
 | 185 |  * Compose a 32bit command word to be sent to the HD-audio controller | 
 | 186 |  */ | 
 | 187 | static inline unsigned int | 
 | 188 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, | 
 | 189 | 	       unsigned int verb, unsigned int parm) | 
 | 190 | { | 
 | 191 | 	u32 val; | 
 | 192 |  | 
| Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 193 | 	if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || | 
 | 194 | 	    (verb & ~0xfff) || (parm & ~0xffff)) { | 
| Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 195 | 		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", | 
 | 196 | 		       codec->addr, direct, nid, verb, parm); | 
 | 197 | 		return ~0; | 
 | 198 | 	} | 
 | 199 |  | 
 | 200 | 	val = (u32)codec->addr << 28; | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 201 | 	val |= (u32)direct << 27; | 
 | 202 | 	val |= (u32)nid << 20; | 
 | 203 | 	val |= verb << 8; | 
 | 204 | 	val |= parm; | 
 | 205 | 	return val; | 
 | 206 | } | 
 | 207 |  | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 208 | /* | 
 | 209 |  * Send and receive a verb | 
 | 210 |  */ | 
 | 211 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, | 
 | 212 | 			   unsigned int *res) | 
 | 213 | { | 
 | 214 | 	struct hda_bus *bus = codec->bus; | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 215 | 	int err; | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 216 |  | 
| Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 217 | 	if (cmd == ~0) | 
 | 218 | 		return -1; | 
 | 219 |  | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 220 | 	if (res) | 
 | 221 | 		*res = -1; | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 222 |  again: | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 223 | 	snd_hda_power_up(codec); | 
 | 224 | 	mutex_lock(&bus->cmd_mutex); | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 225 | 	trace_hda_send_cmd(codec, cmd); | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 226 | 	err = bus->ops.command(bus, cmd); | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 227 | 	if (!err && res) { | 
| Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 228 | 		*res = bus->ops.get_response(bus, codec->addr); | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 229 | 		trace_hda_get_response(codec, *res); | 
 | 230 | 	} | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 231 | 	mutex_unlock(&bus->cmd_mutex); | 
 | 232 | 	snd_hda_power_down(codec); | 
| Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 233 | 	if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 234 | 		if (bus->response_reset) { | 
 | 235 | 			snd_printd("hda_codec: resetting BUS due to " | 
 | 236 | 				   "fatal communication error\n"); | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 237 | 			trace_hda_bus_reset(bus); | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 238 | 			bus->ops.bus_reset(bus); | 
 | 239 | 		} | 
 | 240 | 		goto again; | 
 | 241 | 	} | 
 | 242 | 	/* clear reset-flag when the communication gets recovered */ | 
| Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 243 | 	if (!err || codec_in_pm(codec)) | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 244 | 		bus->response_reset = 0; | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 245 | 	return err; | 
 | 246 | } | 
 | 247 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | /** | 
 | 249 |  * snd_hda_codec_read - send a command and get the response | 
 | 250 |  * @codec: the HDA codec | 
 | 251 |  * @nid: NID to send the command | 
 | 252 |  * @direct: direct flag | 
 | 253 |  * @verb: the verb to send | 
 | 254 |  * @parm: the parameter for the verb | 
 | 255 |  * | 
 | 256 |  * Send a single command and read the corresponding response. | 
 | 257 |  * | 
 | 258 |  * Returns the obtained response value, or -1 for an error. | 
 | 259 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 260 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, | 
 | 261 | 				int direct, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | 				unsigned int verb, unsigned int parm) | 
 | 263 | { | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 264 | 	unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); | 
 | 265 | 	unsigned int res; | 
| Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 266 | 	if (codec_exec_verb(codec, cmd, &res)) | 
 | 267 | 		return -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 	return res; | 
 | 269 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 270 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 |  | 
 | 272 | /** | 
 | 273 |  * snd_hda_codec_write - send a single command without waiting for response | 
 | 274 |  * @codec: the HDA codec | 
 | 275 |  * @nid: NID to send the command | 
 | 276 |  * @direct: direct flag | 
 | 277 |  * @verb: the verb to send | 
 | 278 |  * @parm: the parameter for the verb | 
 | 279 |  * | 
 | 280 |  * Send a single command without waiting for response. | 
 | 281 |  * | 
 | 282 |  * Returns 0 if successful, or a negative error code. | 
 | 283 |  */ | 
 | 284 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, | 
 | 285 | 			 unsigned int verb, unsigned int parm) | 
 | 286 | { | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 287 | 	unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 288 | 	unsigned int res; | 
| Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 289 | 	return codec_exec_verb(codec, cmd, | 
 | 290 | 			       codec->bus->sync_write ? &res : NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 292 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 |  | 
 | 294 | /** | 
 | 295 |  * snd_hda_sequence_write - sequence writes | 
 | 296 |  * @codec: the HDA codec | 
 | 297 |  * @seq: VERB array to send | 
 | 298 |  * | 
 | 299 |  * Send the commands sequentially from the given array. | 
 | 300 |  * The array must be terminated with NID=0. | 
 | 301 |  */ | 
 | 302 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) | 
 | 303 | { | 
 | 304 | 	for (; seq->nid; seq++) | 
 | 305 | 		snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); | 
 | 306 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 307 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 |  | 
 | 309 | /** | 
 | 310 |  * snd_hda_get_sub_nodes - get the range of sub nodes | 
 | 311 |  * @codec: the HDA codec | 
 | 312 |  * @nid: NID to parse | 
 | 313 |  * @start_id: the pointer to store the start NID | 
 | 314 |  * | 
 | 315 |  * Parse the NID and store the start NID of its sub-nodes. | 
 | 316 |  * Returns the number of sub-nodes. | 
 | 317 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 318 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, | 
 | 319 | 			  hda_nid_t *start_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { | 
 | 321 | 	unsigned int parm; | 
 | 322 |  | 
 | 323 | 	parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); | 
| Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 324 | 	if (parm == -1) | 
 | 325 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | 	*start_id = (parm >> 16) & 0x7fff; | 
 | 327 | 	return (int)(parm & 0x7fff); | 
 | 328 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 329 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 |  | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 331 | /* look up the cached results */ | 
 | 332 | static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid) | 
 | 333 | { | 
 | 334 | 	int i, len; | 
 | 335 | 	for (i = 0; i < array->used; ) { | 
 | 336 | 		hda_nid_t *p = snd_array_elem(array, i); | 
 | 337 | 		if (nid == *p) | 
 | 338 | 			return p; | 
 | 339 | 		len = p[1]; | 
 | 340 | 		i += len + 2; | 
 | 341 | 	} | 
 | 342 | 	return NULL; | 
 | 343 | } | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 344 |  | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 345 | /* read the connection and add to the cache */ | 
 | 346 | static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 347 | { | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 348 | 	hda_nid_t list[HDA_MAX_CONNECTIONS]; | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 349 | 	int len; | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 350 |  | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 351 | 	len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 352 | 	if (len < 0) | 
 | 353 | 		return len; | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 354 | 	return snd_hda_override_conn_list(codec, nid, len, list); | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 355 | } | 
| Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 356 |  | 
 | 357 | /** | 
 | 358 |  * snd_hda_get_connections - copy connection list | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 |  * @codec: the HDA codec | 
 | 360 |  * @nid: NID to parse | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 361 |  * @conn_list: connection list array; when NULL, checks only the size | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 |  * @max_conns: max. number of connections to store | 
 | 363 |  * | 
 | 364 |  * Parses the connection list of the given widget and stores the list | 
 | 365 |  * of NIDs. | 
 | 366 |  * | 
 | 367 |  * Returns the number of connections, or a negative error code. | 
 | 368 |  */ | 
 | 369 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 370 | 			    hda_nid_t *conn_list, int max_conns) | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 371 | { | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 372 | 	struct snd_array *array = &codec->conn_lists; | 
 | 373 | 	int len; | 
 | 374 | 	hda_nid_t *p; | 
 | 375 | 	bool added = false; | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 376 |  | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 377 |  again: | 
 | 378 | 	mutex_lock(&codec->hash_mutex); | 
 | 379 | 	len = -1; | 
 | 380 | 	/* if the connection-list is already cached, read it */ | 
 | 381 | 	p = lookup_conn_list(array, nid); | 
 | 382 | 	if (p) { | 
 | 383 | 		len = p[1]; | 
 | 384 | 		if (conn_list && len > max_conns) { | 
 | 385 | 			snd_printk(KERN_ERR "hda_codec: " | 
 | 386 | 				   "Too many connections %d for NID 0x%x\n", | 
 | 387 | 				   len, nid); | 
 | 388 | 			mutex_unlock(&codec->hash_mutex); | 
 | 389 | 			return -EINVAL; | 
 | 390 | 		} | 
 | 391 | 		if (conn_list && len) | 
 | 392 | 			memcpy(conn_list, p + 2, len * sizeof(hda_nid_t)); | 
| Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 393 | 	} | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | 	mutex_unlock(&codec->hash_mutex); | 
 | 395 | 	if (len >= 0) | 
 | 396 | 		return len; | 
 | 397 | 	if (snd_BUG_ON(added)) | 
 | 398 | 		return -EINVAL; | 
 | 399 |  | 
 | 400 | 	len = read_and_add_raw_conns(codec, nid); | 
 | 401 | 	if (len < 0) | 
 | 402 | 		return len; | 
 | 403 | 	added = true; | 
 | 404 | 	goto again; | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 405 | } | 
 | 406 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); | 
 | 407 |  | 
| Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 408 | /** | 
 | 409 |  * snd_hda_get_raw_connections - copy connection list without cache | 
 | 410 |  * @codec: the HDA codec | 
 | 411 |  * @nid: NID to parse | 
 | 412 |  * @conn_list: connection list array | 
 | 413 |  * @max_conns: max. number of connections to store | 
 | 414 |  * | 
 | 415 |  * Like snd_hda_get_connections(), copy the connection list but without | 
 | 416 |  * checking through the connection-list cache. | 
 | 417 |  * Currently called only from hda_proc.c, so not exported. | 
 | 418 |  */ | 
 | 419 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, | 
 | 420 | 				hda_nid_t *conn_list, int max_conns) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { | 
 | 422 | 	unsigned int parm; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 423 | 	int i, conn_len, conns; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 	unsigned int shift, num_elems, mask; | 
| Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 425 | 	unsigned int wcaps; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 426 | 	hda_nid_t prev_nid; | 
| Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 427 | 	int null_count = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 |  | 
| Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 429 | 	if (snd_BUG_ON(!conn_list || max_conns <= 0)) | 
 | 430 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 |  | 
| Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 432 | 	wcaps = get_wcaps(codec, nid); | 
 | 433 | 	if (!(wcaps & AC_WCAP_CONN_LIST) && | 
| Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 434 | 	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB) | 
 | 435 | 		return 0; | 
| Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 436 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | 	parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); | 
 | 438 | 	if (parm & AC_CLIST_LONG) { | 
 | 439 | 		/* long form */ | 
 | 440 | 		shift = 16; | 
 | 441 | 		num_elems = 2; | 
 | 442 | 	} else { | 
 | 443 | 		/* short form */ | 
 | 444 | 		shift = 8; | 
 | 445 | 		num_elems = 4; | 
 | 446 | 	} | 
 | 447 | 	conn_len = parm & AC_CLIST_LENGTH; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | 	mask = (1 << (shift-1)) - 1; | 
 | 449 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 450 | 	if (!conn_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | 		return 0; /* no connection */ | 
 | 452 |  | 
 | 453 | 	if (conn_len == 1) { | 
 | 454 | 		/* single connection */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 455 | 		parm = snd_hda_codec_read(codec, nid, 0, | 
 | 456 | 					  AC_VERB_GET_CONNECT_LIST, 0); | 
| Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 457 | 		if (parm == -1 && codec->bus->rirb_error) | 
 | 458 | 			return -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | 		conn_list[0] = parm & mask; | 
 | 460 | 		return 1; | 
 | 461 | 	} | 
 | 462 |  | 
 | 463 | 	/* multi connection */ | 
 | 464 | 	conns = 0; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 465 | 	prev_nid = 0; | 
 | 466 | 	for (i = 0; i < conn_len; i++) { | 
 | 467 | 		int range_val; | 
 | 468 | 		hda_nid_t val, n; | 
 | 469 |  | 
| Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 470 | 		if (i % num_elems == 0) { | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 471 | 			parm = snd_hda_codec_read(codec, nid, 0, | 
 | 472 | 						  AC_VERB_GET_CONNECT_LIST, i); | 
| Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 473 | 			if (parm == -1 && codec->bus->rirb_error) | 
 | 474 | 				return -EIO; | 
 | 475 | 		} | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 476 | 		range_val = !!(parm & (1 << (shift-1))); /* ranges */ | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 477 | 		val = parm & mask; | 
| Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 478 | 		if (val == 0 && null_count++) {  /* no second chance */ | 
| Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 479 | 			snd_printk(KERN_WARNING "hda_codec: " | 
 | 480 | 				   "invalid CONNECT_LIST verb %x[%i]:%x\n", | 
 | 481 | 				    nid, i, parm); | 
 | 482 | 			return 0; | 
 | 483 | 		} | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 484 | 		parm >>= shift; | 
 | 485 | 		if (range_val) { | 
 | 486 | 			/* ranges between the previous and this one */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 487 | 			if (!prev_nid || prev_nid >= val) { | 
 | 488 | 				snd_printk(KERN_WARNING "hda_codec: " | 
 | 489 | 					   "invalid dep_range_val %x:%x\n", | 
 | 490 | 					   prev_nid, val); | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 491 | 				continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | 			} | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 493 | 			for (n = prev_nid + 1; n <= val; n++) { | 
 | 494 | 				if (conns >= max_conns) { | 
| Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 495 | 					snd_printk(KERN_ERR "hda_codec: " | 
 | 496 | 						   "Too many connections %d for NID 0x%x\n", | 
 | 497 | 						   conns, nid); | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 498 | 					return -EINVAL; | 
 | 499 | 				} | 
 | 500 | 				conn_list[conns++] = n; | 
 | 501 | 			} | 
 | 502 | 		} else { | 
 | 503 | 			if (conns >= max_conns) { | 
| Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 504 | 				snd_printk(KERN_ERR "hda_codec: " | 
 | 505 | 					   "Too many connections %d for NID 0x%x\n", | 
 | 506 | 					   conns, nid); | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 507 | 				return -EINVAL; | 
 | 508 | 			} | 
 | 509 | 			conn_list[conns++] = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | 		} | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 511 | 		prev_nid = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | 	} | 
 | 513 | 	return conns; | 
 | 514 | } | 
 | 515 |  | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 516 | static bool add_conn_list(struct snd_array *array, hda_nid_t nid) | 
 | 517 | { | 
 | 518 | 	hda_nid_t *p = snd_array_new(array); | 
 | 519 | 	if (!p) | 
 | 520 | 		return false; | 
 | 521 | 	*p = nid; | 
 | 522 | 	return true; | 
 | 523 | } | 
 | 524 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /** | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 526 |  * snd_hda_override_conn_list - add/modify the connection-list to cache | 
 | 527 |  * @codec: the HDA codec | 
 | 528 |  * @nid: NID to parse | 
 | 529 |  * @len: number of connection list entries | 
 | 530 |  * @list: the list of connection entries | 
 | 531 |  * | 
 | 532 |  * Add or modify the given connection-list to the cache.  If the corresponding | 
 | 533 |  * cache already exists, invalidate it and append a new one. | 
 | 534 |  * | 
 | 535 |  * Returns zero or a negative error code. | 
 | 536 |  */ | 
 | 537 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, | 
 | 538 | 			       const hda_nid_t *list) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 540 | 	struct snd_array *array = &codec->conn_lists; | 
 | 541 | 	hda_nid_t *p; | 
 | 542 | 	int i, old_used; | 
 | 543 |  | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 544 | 	mutex_lock(&codec->hash_mutex); | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 545 | 	p = lookup_conn_list(array, nid); | 
 | 546 | 	if (p) | 
 | 547 | 		*p = -1; /* invalidate the old entry */ | 
 | 548 |  | 
 | 549 | 	old_used = array->used; | 
 | 550 | 	if (!add_conn_list(array, nid) || !add_conn_list(array, len)) | 
 | 551 | 		goto error_add; | 
 | 552 | 	for (i = 0; i < len; i++) | 
 | 553 | 		if (!add_conn_list(array, list[i])) | 
 | 554 | 			goto error_add; | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 555 | 	mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 556 | 	return 0; | 
 | 557 |  | 
 | 558 |  error_add: | 
 | 559 | 	array->used = old_used; | 
| Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 560 | 	mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 561 | 	return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } | 
| Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 563 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); | 
 | 564 |  | 
 | 565 | /** | 
| Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 566 |  * snd_hda_get_conn_index - get the connection index of the given NID | 
 | 567 |  * @codec: the HDA codec | 
 | 568 |  * @mux: NID containing the list | 
 | 569 |  * @nid: NID to select | 
 | 570 |  * @recursive: 1 when searching NID recursively, otherwise 0 | 
 | 571 |  * | 
 | 572 |  * Parses the connection list of the widget @mux and checks whether the | 
 | 573 |  * widget @nid is present.  If it is, return the connection index. | 
 | 574 |  * Otherwise it returns -1. | 
 | 575 |  */ | 
 | 576 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, | 
 | 577 | 			   hda_nid_t nid, int recursive) | 
 | 578 | { | 
 | 579 | 	hda_nid_t conn[HDA_MAX_NUM_INPUTS]; | 
 | 580 | 	int i, nums; | 
 | 581 |  | 
 | 582 | 	nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn)); | 
 | 583 | 	for (i = 0; i < nums; i++) | 
 | 584 | 		if (conn[i] == nid) | 
 | 585 | 			return i; | 
 | 586 | 	if (!recursive) | 
 | 587 | 		return -1; | 
 | 588 | 	if (recursive > 5) { | 
 | 589 | 		snd_printd("hda_codec: too deep connection for 0x%x\n", nid); | 
 | 590 | 		return -1; | 
 | 591 | 	} | 
 | 592 | 	recursive++; | 
| Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 593 | 	for (i = 0; i < nums; i++) { | 
 | 594 | 		unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); | 
 | 595 | 		if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) | 
 | 596 | 			continue; | 
| Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 597 | 		if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) | 
 | 598 | 			return i; | 
| Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 599 | 	} | 
| Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 600 | 	return -1; | 
 | 601 | } | 
 | 602 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 |  | 
 | 604 | /** | 
 | 605 |  * snd_hda_queue_unsol_event - add an unsolicited event to queue | 
 | 606 |  * @bus: the BUS | 
 | 607 |  * @res: unsolicited event (lower 32bit of RIRB entry) | 
 | 608 |  * @res_ex: codec addr and flags (upper 32bit or RIRB entry) | 
 | 609 |  * | 
 | 610 |  * Adds the given event to the queue.  The events are processed in | 
 | 611 |  * the workqueue asynchronously.  Call this function in the interrupt | 
 | 612 |  * hanlder when RIRB receives an unsolicited event. | 
 | 613 |  * | 
 | 614 |  * Returns 0 if successful, or a negative error code. | 
 | 615 |  */ | 
 | 616 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) | 
 | 617 | { | 
 | 618 | 	struct hda_bus_unsolicited *unsol; | 
 | 619 | 	unsigned int wp; | 
 | 620 |  | 
| Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 621 | 	trace_hda_unsol_event(bus, res, res_ex); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 622 | 	unsol = bus->unsol; | 
 | 623 | 	if (!unsol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | 		return 0; | 
 | 625 |  | 
 | 626 | 	wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; | 
 | 627 | 	unsol->wp = wp; | 
 | 628 |  | 
 | 629 | 	wp <<= 1; | 
 | 630 | 	unsol->queue[wp] = res; | 
 | 631 | 	unsol->queue[wp + 1] = res_ex; | 
 | 632 |  | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 633 | 	queue_work(bus->workq, &unsol->work); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 |  | 
 | 635 | 	return 0; | 
 | 636 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 637 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
 | 639 | /* | 
| Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 640 |  * process queued unsolicited events | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 |  */ | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 642 | static void process_unsol_events(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 644 | 	struct hda_bus_unsolicited *unsol = | 
 | 645 | 		container_of(work, struct hda_bus_unsolicited, work); | 
 | 646 | 	struct hda_bus *bus = unsol->bus; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 	struct hda_codec *codec; | 
 | 648 | 	unsigned int rp, caddr, res; | 
 | 649 |  | 
 | 650 | 	while (unsol->rp != unsol->wp) { | 
 | 651 | 		rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; | 
 | 652 | 		unsol->rp = rp; | 
 | 653 | 		rp <<= 1; | 
 | 654 | 		res = unsol->queue[rp]; | 
 | 655 | 		caddr = unsol->queue[rp + 1]; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 656 | 		if (!(caddr & (1 << 4))) /* no unsolicited event? */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | 			continue; | 
 | 658 | 		codec = bus->caddr_tbl[caddr & 0x0f]; | 
 | 659 | 		if (codec && codec->patch_ops.unsol_event) | 
 | 660 | 			codec->patch_ops.unsol_event(codec, res); | 
 | 661 | 	} | 
 | 662 | } | 
 | 663 |  | 
 | 664 | /* | 
 | 665 |  * initialize unsolicited queue | 
 | 666 |  */ | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 667 | static int init_unsol_queue(struct hda_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | { | 
 | 669 | 	struct hda_bus_unsolicited *unsol; | 
 | 670 |  | 
| Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 671 | 	if (bus->unsol) /* already initialized */ | 
 | 672 | 		return 0; | 
 | 673 |  | 
| Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 674 | 	unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 675 | 	if (!unsol) { | 
 | 676 | 		snd_printk(KERN_ERR "hda_codec: " | 
 | 677 | 			   "can't allocate unsolicited queue\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | 		return -ENOMEM; | 
 | 679 | 	} | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 680 | 	INIT_WORK(&unsol->work, process_unsol_events); | 
 | 681 | 	unsol->bus = bus; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | 	bus->unsol = unsol; | 
 | 683 | 	return 0; | 
 | 684 | } | 
 | 685 |  | 
 | 686 | /* | 
 | 687 |  * destructor | 
 | 688 |  */ | 
 | 689 | static void snd_hda_codec_free(struct hda_codec *codec); | 
 | 690 |  | 
 | 691 | static int snd_hda_bus_free(struct hda_bus *bus) | 
 | 692 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 693 | 	struct hda_codec *codec, *n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 695 | 	if (!bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | 		return 0; | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 697 | 	if (bus->workq) | 
 | 698 | 		flush_workqueue(bus->workq); | 
 | 699 | 	if (bus->unsol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | 		kfree(bus->unsol); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 701 | 	list_for_each_entry_safe(codec, n, &bus->codec_list, list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | 		snd_hda_codec_free(codec); | 
 | 703 | 	} | 
 | 704 | 	if (bus->ops.private_free) | 
 | 705 | 		bus->ops.private_free(bus); | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 706 | 	if (bus->workq) | 
 | 707 | 		destroy_workqueue(bus->workq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | 	kfree(bus); | 
 | 709 | 	return 0; | 
 | 710 | } | 
 | 711 |  | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 712 | static int snd_hda_bus_dev_free(struct snd_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { | 
 | 714 | 	struct hda_bus *bus = device->device_data; | 
| Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 715 | 	bus->shutdown = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | 	return snd_hda_bus_free(bus); | 
 | 717 | } | 
 | 718 |  | 
| Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 719 | #ifdef CONFIG_SND_HDA_HWDEP | 
 | 720 | static int snd_hda_bus_dev_register(struct snd_device *device) | 
 | 721 | { | 
 | 722 | 	struct hda_bus *bus = device->device_data; | 
 | 723 | 	struct hda_codec *codec; | 
 | 724 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
 | 725 | 		snd_hda_hwdep_add_sysfs(codec); | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 726 | 		snd_hda_hwdep_add_power_sysfs(codec); | 
| Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 727 | 	} | 
 | 728 | 	return 0; | 
 | 729 | } | 
 | 730 | #else | 
 | 731 | #define snd_hda_bus_dev_register	NULL | 
 | 732 | #endif | 
 | 733 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | /** | 
 | 735 |  * snd_hda_bus_new - create a HDA bus | 
 | 736 |  * @card: the card entry | 
 | 737 |  * @temp: the template for hda_bus information | 
 | 738 |  * @busp: the pointer to store the created bus instance | 
 | 739 |  * | 
 | 740 |  * Returns 0 if successful, or a negative error code. | 
 | 741 |  */ | 
| Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 742 | int snd_hda_bus_new(struct snd_card *card, | 
| Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 743 | 			      const struct hda_bus_template *temp, | 
 | 744 | 			      struct hda_bus **busp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { | 
 | 746 | 	struct hda_bus *bus; | 
 | 747 | 	int err; | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 748 | 	static struct snd_device_ops dev_ops = { | 
| Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 749 | 		.dev_register = snd_hda_bus_dev_register, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | 		.dev_free = snd_hda_bus_dev_free, | 
 | 751 | 	}; | 
 | 752 |  | 
| Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 753 | 	if (snd_BUG_ON(!temp)) | 
 | 754 | 		return -EINVAL; | 
 | 755 | 	if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) | 
 | 756 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 |  | 
 | 758 | 	if (busp) | 
 | 759 | 		*busp = NULL; | 
 | 760 |  | 
| Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 761 | 	bus = kzalloc(sizeof(*bus), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | 	if (bus == NULL) { | 
 | 763 | 		snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); | 
 | 764 | 		return -ENOMEM; | 
 | 765 | 	} | 
 | 766 |  | 
 | 767 | 	bus->card = card; | 
 | 768 | 	bus->private_data = temp->private_data; | 
 | 769 | 	bus->pci = temp->pci; | 
 | 770 | 	bus->modelname = temp->modelname; | 
| Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 771 | 	bus->power_save = temp->power_save; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | 	bus->ops = temp->ops; | 
 | 773 |  | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 774 | 	mutex_init(&bus->cmd_mutex); | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 775 | 	mutex_init(&bus->prepare_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | 	INIT_LIST_HEAD(&bus->codec_list); | 
 | 777 |  | 
| Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 778 | 	snprintf(bus->workq_name, sizeof(bus->workq_name), | 
 | 779 | 		 "hd-audio%d", card->number); | 
 | 780 | 	bus->workq = create_singlethread_workqueue(bus->workq_name); | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 781 | 	if (!bus->workq) { | 
| Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 782 | 		snd_printk(KERN_ERR "cannot create workqueue %s\n", | 
 | 783 | 			   bus->workq_name); | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 784 | 		kfree(bus); | 
 | 785 | 		return -ENOMEM; | 
 | 786 | 	} | 
 | 787 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 788 | 	err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); | 
 | 789 | 	if (err < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | 		snd_hda_bus_free(bus); | 
 | 791 | 		return err; | 
 | 792 | 	} | 
 | 793 | 	if (busp) | 
 | 794 | 		*busp = bus; | 
 | 795 | 	return 0; | 
 | 796 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 797 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 |  | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 799 | #ifdef CONFIG_SND_HDA_GENERIC | 
 | 800 | #define is_generic_config(codec) \ | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 801 | 	(codec->modelname && !strcmp(codec->modelname, "generic")) | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 802 | #else | 
 | 803 | #define is_generic_config(codec)	0 | 
 | 804 | #endif | 
 | 805 |  | 
| Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 806 | #ifdef MODULE | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 807 | #define HDA_MODREQ_MAX_COUNT	2	/* two request_modules()'s */ | 
 | 808 | #else | 
| Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 809 | #define HDA_MODREQ_MAX_COUNT	0	/* all presets are statically linked */ | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 810 | #endif | 
 | 811 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* | 
 | 813 |  * find a matching codec preset | 
 | 814 |  */ | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 815 | static const struct hda_codec_preset * | 
| Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 816 | find_codec_preset(struct hda_codec *codec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 818 | 	struct hda_codec_preset_list *tbl; | 
 | 819 | 	const struct hda_codec_preset *preset; | 
| Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 820 | 	unsigned int mod_requested = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 |  | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 822 | 	if (is_generic_config(codec)) | 
| Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 823 | 		return NULL; /* use the generic parser */ | 
 | 824 |  | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 825 |  again: | 
 | 826 | 	mutex_lock(&preset_mutex); | 
 | 827 | 	list_for_each_entry(tbl, &hda_preset_tables, list) { | 
 | 828 | 		if (!try_module_get(tbl->owner)) { | 
 | 829 | 			snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); | 
 | 830 | 			continue; | 
 | 831 | 		} | 
 | 832 | 		for (preset = tbl->preset; preset->id; preset++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | 			u32 mask = preset->mask; | 
| Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 834 | 			if (preset->afg && preset->afg != codec->afg) | 
 | 835 | 				continue; | 
 | 836 | 			if (preset->mfg && preset->mfg != codec->mfg) | 
 | 837 | 				continue; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 838 | 			if (!mask) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | 				mask = ~0; | 
| Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 840 | 			if (preset->id == (codec->vendor_id & mask) && | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 841 | 			    (!preset->rev || | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 842 | 			     preset->rev == codec->revision_id)) { | 
 | 843 | 				mutex_unlock(&preset_mutex); | 
 | 844 | 				codec->owner = tbl->owner; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | 				return preset; | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 846 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | 		} | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 848 | 		module_put(tbl->owner); | 
 | 849 | 	} | 
 | 850 | 	mutex_unlock(&preset_mutex); | 
 | 851 |  | 
 | 852 | 	if (mod_requested < HDA_MODREQ_MAX_COUNT) { | 
 | 853 | 		char name[32]; | 
 | 854 | 		if (!mod_requested) | 
 | 855 | 			snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", | 
 | 856 | 				 codec->vendor_id); | 
 | 857 | 		else | 
 | 858 | 			snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", | 
 | 859 | 				 (codec->vendor_id >> 16) & 0xffff); | 
 | 860 | 		request_module(name); | 
 | 861 | 		mod_requested++; | 
 | 862 | 		goto again; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | 	} | 
 | 864 | 	return NULL; | 
 | 865 | } | 
 | 866 |  | 
 | 867 | /* | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 868 |  * get_codec_name - store the codec name | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 |  */ | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 870 | static int get_codec_name(struct hda_codec *codec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { | 
 | 872 | 	const struct hda_vendor_id *c; | 
 | 873 | 	const char *vendor = NULL; | 
 | 874 | 	u16 vendor_id = codec->vendor_id >> 16; | 
| Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 875 | 	char tmp[16]; | 
 | 876 |  | 
 | 877 | 	if (codec->vendor_name) | 
 | 878 | 		goto get_chip_name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 |  | 
 | 880 | 	for (c = hda_vendor_ids; c->id; c++) { | 
 | 881 | 		if (c->id == vendor_id) { | 
 | 882 | 			vendor = c->name; | 
 | 883 | 			break; | 
 | 884 | 		} | 
 | 885 | 	} | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 886 | 	if (!vendor) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | 		sprintf(tmp, "Generic %04x", vendor_id); | 
 | 888 | 		vendor = tmp; | 
 | 889 | 	} | 
| Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 890 | 	codec->vendor_name = kstrdup(vendor, GFP_KERNEL); | 
 | 891 | 	if (!codec->vendor_name) | 
 | 892 | 		return -ENOMEM; | 
 | 893 |  | 
 | 894 |  get_chip_name: | 
 | 895 | 	if (codec->chip_name) | 
 | 896 | 		return 0; | 
 | 897 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | 	if (codec->preset && codec->preset->name) | 
| Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 899 | 		codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); | 
 | 900 | 	else { | 
 | 901 | 		sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); | 
 | 902 | 		codec->chip_name = kstrdup(tmp, GFP_KERNEL); | 
 | 903 | 	} | 
 | 904 | 	if (!codec->chip_name) | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 905 | 		return -ENOMEM; | 
 | 906 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } | 
 | 908 |  | 
 | 909 | /* | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 910 |  * look for an AFG and MFG nodes | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 |  */ | 
| Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 912 | static void setup_fg_nodes(struct hda_codec *codec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { | 
| Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 914 | 	int i, total_nodes, function_id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | 	hda_nid_t nid; | 
 | 916 |  | 
 | 917 | 	total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); | 
 | 918 | 	for (i = 0; i < total_nodes; i++, nid++) { | 
| Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 919 | 		function_id = snd_hda_param_read(codec, nid, | 
| Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 920 | 						AC_PAR_FUNCTION_TYPE); | 
| Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 921 | 		switch (function_id & 0xff) { | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 922 | 		case AC_GRP_AUDIO_FUNCTION: | 
 | 923 | 			codec->afg = nid; | 
| Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 924 | 			codec->afg_function_id = function_id & 0xff; | 
 | 925 | 			codec->afg_unsol = (function_id >> 8) & 1; | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 926 | 			break; | 
 | 927 | 		case AC_GRP_MODEM_FUNCTION: | 
 | 928 | 			codec->mfg = nid; | 
| Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 929 | 			codec->mfg_function_id = function_id & 0xff; | 
 | 930 | 			codec->mfg_unsol = (function_id >> 8) & 1; | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 931 | 			break; | 
 | 932 | 		default: | 
 | 933 | 			break; | 
 | 934 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } | 
 | 937 |  | 
 | 938 | /* | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 939 |  * read widget caps for each widget and store in cache | 
 | 940 |  */ | 
 | 941 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) | 
 | 942 | { | 
 | 943 | 	int i; | 
 | 944 | 	hda_nid_t nid; | 
 | 945 |  | 
 | 946 | 	codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, | 
 | 947 | 						 &codec->start_nid); | 
 | 948 | 	codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 949 | 	if (!codec->wcaps) | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 950 | 		return -ENOMEM; | 
 | 951 | 	nid = codec->start_nid; | 
 | 952 | 	for (i = 0; i < codec->num_nodes; i++, nid++) | 
 | 953 | 		codec->wcaps[i] = snd_hda_param_read(codec, nid, | 
 | 954 | 						     AC_PAR_AUDIO_WIDGET_CAP); | 
 | 955 | 	return 0; | 
 | 956 | } | 
 | 957 |  | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 958 | /* read all pin default configurations and save codec->init_pins */ | 
 | 959 | static int read_pin_defaults(struct hda_codec *codec) | 
 | 960 | { | 
 | 961 | 	int i; | 
 | 962 | 	hda_nid_t nid = codec->start_nid; | 
 | 963 |  | 
 | 964 | 	for (i = 0; i < codec->num_nodes; i++, nid++) { | 
 | 965 | 		struct hda_pincfg *pin; | 
 | 966 | 		unsigned int wcaps = get_wcaps(codec, nid); | 
| Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 967 | 		unsigned int wid_type = get_wcaps_type(wcaps); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 968 | 		if (wid_type != AC_WID_PIN) | 
 | 969 | 			continue; | 
 | 970 | 		pin = snd_array_new(&codec->init_pins); | 
 | 971 | 		if (!pin) | 
 | 972 | 			return -ENOMEM; | 
 | 973 | 		pin->nid = nid; | 
 | 974 | 		pin->cfg = snd_hda_codec_read(codec, nid, 0, | 
 | 975 | 					      AC_VERB_GET_CONFIG_DEFAULT, 0); | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 976 | 		pin->ctrl = snd_hda_codec_read(codec, nid, 0, | 
 | 977 | 					       AC_VERB_GET_PIN_WIDGET_CONTROL, | 
 | 978 | 					       0); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 979 | 	} | 
 | 980 | 	return 0; | 
 | 981 | } | 
 | 982 |  | 
 | 983 | /* look up the given pin config list and return the item matching with NID */ | 
 | 984 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, | 
 | 985 | 					 struct snd_array *array, | 
 | 986 | 					 hda_nid_t nid) | 
 | 987 | { | 
 | 988 | 	int i; | 
 | 989 | 	for (i = 0; i < array->used; i++) { | 
 | 990 | 		struct hda_pincfg *pin = snd_array_elem(array, i); | 
 | 991 | 		if (pin->nid == nid) | 
 | 992 | 			return pin; | 
 | 993 | 	} | 
 | 994 | 	return NULL; | 
 | 995 | } | 
 | 996 |  | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 997 | /* set the current pin config value for the given NID. | 
 | 998 |  * the value is cached, and read via snd_hda_codec_get_pincfg() | 
 | 999 |  */ | 
 | 1000 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, | 
 | 1001 | 		       hda_nid_t nid, unsigned int cfg) | 
 | 1002 | { | 
 | 1003 | 	struct hda_pincfg *pin; | 
 | 1004 |  | 
| Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1005 | 	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) | 
 | 1006 | 		return -EINVAL; | 
 | 1007 |  | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1008 | 	pin = look_up_pincfg(codec, list, nid); | 
 | 1009 | 	if (!pin) { | 
 | 1010 | 		pin = snd_array_new(list); | 
 | 1011 | 		if (!pin) | 
 | 1012 | 			return -ENOMEM; | 
 | 1013 | 		pin->nid = nid; | 
 | 1014 | 	} | 
 | 1015 | 	pin->cfg = cfg; | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1016 | 	return 0; | 
 | 1017 | } | 
 | 1018 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1019 | /** | 
 | 1020 |  * snd_hda_codec_set_pincfg - Override a pin default configuration | 
 | 1021 |  * @codec: the HDA codec | 
 | 1022 |  * @nid: NID to set the pin config | 
 | 1023 |  * @cfg: the pin default config value | 
 | 1024 |  * | 
 | 1025 |  * Override a pin default configuration value in the cache. | 
 | 1026 |  * This value can be read by snd_hda_codec_get_pincfg() in a higher | 
 | 1027 |  * priority than the real hardware value. | 
 | 1028 |  */ | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1029 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, | 
 | 1030 | 			     hda_nid_t nid, unsigned int cfg) | 
 | 1031 | { | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1032 | 	return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1033 | } | 
 | 1034 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); | 
 | 1035 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1036 | /** | 
 | 1037 |  * snd_hda_codec_get_pincfg - Obtain a pin-default configuration | 
 | 1038 |  * @codec: the HDA codec | 
 | 1039 |  * @nid: NID to get the pin config | 
 | 1040 |  * | 
 | 1041 |  * Get the current pin config value of the given pin NID. | 
 | 1042 |  * If the pincfg value is cached or overridden via sysfs or driver, | 
 | 1043 |  * returns the cached value. | 
 | 1044 |  */ | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1045 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) | 
 | 1046 | { | 
 | 1047 | 	struct hda_pincfg *pin; | 
 | 1048 |  | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1049 | #ifdef CONFIG_SND_HDA_HWDEP | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1050 | 	pin = look_up_pincfg(codec, &codec->user_pins, nid); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1051 | 	if (pin) | 
 | 1052 | 		return pin->cfg; | 
 | 1053 | #endif | 
| Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1054 | 	pin = look_up_pincfg(codec, &codec->driver_pins, nid); | 
 | 1055 | 	if (pin) | 
 | 1056 | 		return pin->cfg; | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1057 | 	pin = look_up_pincfg(codec, &codec->init_pins, nid); | 
 | 1058 | 	if (pin) | 
 | 1059 | 		return pin->cfg; | 
 | 1060 | 	return 0; | 
 | 1061 | } | 
 | 1062 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); | 
 | 1063 |  | 
| Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1064 | /** | 
 | 1065 |  * snd_hda_shutup_pins - Shut up all pins | 
 | 1066 |  * @codec: the HDA codec | 
 | 1067 |  * | 
 | 1068 |  * Clear all pin controls to shup up before suspend for avoiding click noise. | 
 | 1069 |  * The controls aren't cached so that they can be resumed properly. | 
 | 1070 |  */ | 
 | 1071 | void snd_hda_shutup_pins(struct hda_codec *codec) | 
 | 1072 | { | 
 | 1073 | 	int i; | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1074 | 	/* don't shut up pins when unloading the driver; otherwise it breaks | 
 | 1075 | 	 * the default pin setup at the next load of the driver | 
 | 1076 | 	 */ | 
 | 1077 | 	if (codec->bus->shutdown) | 
 | 1078 | 		return; | 
| Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1079 | 	for (i = 0; i < codec->init_pins.used; i++) { | 
 | 1080 | 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); | 
 | 1081 | 		/* use read here for syncing after issuing each verb */ | 
 | 1082 | 		snd_hda_codec_read(codec, pin->nid, 0, | 
 | 1083 | 				   AC_VERB_SET_PIN_WIDGET_CONTROL, 0); | 
 | 1084 | 	} | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1085 | 	codec->pins_shutup = 1; | 
| Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1086 | } | 
 | 1087 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); | 
 | 1088 |  | 
| Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1089 | #ifdef CONFIG_PM | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1090 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ | 
 | 1091 | static void restore_shutup_pins(struct hda_codec *codec) | 
 | 1092 | { | 
 | 1093 | 	int i; | 
 | 1094 | 	if (!codec->pins_shutup) | 
 | 1095 | 		return; | 
 | 1096 | 	if (codec->bus->shutdown) | 
 | 1097 | 		return; | 
 | 1098 | 	for (i = 0; i < codec->init_pins.used; i++) { | 
 | 1099 | 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); | 
 | 1100 | 		snd_hda_codec_write(codec, pin->nid, 0, | 
 | 1101 | 				    AC_VERB_SET_PIN_WIDGET_CONTROL, | 
 | 1102 | 				    pin->ctrl); | 
 | 1103 | 	} | 
 | 1104 | 	codec->pins_shutup = 0; | 
 | 1105 | } | 
| Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1106 | #endif | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1107 |  | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1108 | static void hda_jackpoll_work(struct work_struct *work) | 
 | 1109 | { | 
 | 1110 | 	struct hda_codec *codec = | 
 | 1111 | 		container_of(work, struct hda_codec, jackpoll_work.work); | 
 | 1112 | 	if (!codec->jackpoll_interval) | 
 | 1113 | 		return; | 
 | 1114 |  | 
 | 1115 | 	snd_hda_jack_set_dirty_all(codec); | 
 | 1116 | 	snd_hda_jack_poll_all(codec); | 
 | 1117 | 	queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, | 
 | 1118 | 			   codec->jackpoll_interval); | 
 | 1119 | } | 
 | 1120 |  | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1121 | static void init_hda_cache(struct hda_cache_rec *cache, | 
 | 1122 | 			   unsigned int record_size); | 
| Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1123 | static void free_hda_cache(struct hda_cache_rec *cache); | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1124 |  | 
| Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1125 | /* release all pincfg lists */ | 
 | 1126 | static void free_init_pincfgs(struct hda_codec *codec) | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1127 | { | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1128 | 	snd_array_free(&codec->driver_pins); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1129 | #ifdef CONFIG_SND_HDA_HWDEP | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1130 | 	snd_array_free(&codec->user_pins); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1131 | #endif | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1132 | 	snd_array_free(&codec->init_pins); | 
 | 1133 | } | 
 | 1134 |  | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1135 | /* | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1136 |  * audio-converter setup caches | 
 | 1137 |  */ | 
 | 1138 | struct hda_cvt_setup { | 
 | 1139 | 	hda_nid_t nid; | 
 | 1140 | 	u8 stream_tag; | 
 | 1141 | 	u8 channel_id; | 
 | 1142 | 	u16 format_id; | 
 | 1143 | 	unsigned char active;	/* cvt is currently used */ | 
 | 1144 | 	unsigned char dirty;	/* setups should be cleared */ | 
 | 1145 | }; | 
 | 1146 |  | 
 | 1147 | /* get or create a cache entry for the given audio converter NID */ | 
 | 1148 | static struct hda_cvt_setup * | 
 | 1149 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) | 
 | 1150 | { | 
 | 1151 | 	struct hda_cvt_setup *p; | 
 | 1152 | 	int i; | 
 | 1153 |  | 
 | 1154 | 	for (i = 0; i < codec->cvt_setups.used; i++) { | 
 | 1155 | 		p = snd_array_elem(&codec->cvt_setups, i); | 
 | 1156 | 		if (p->nid == nid) | 
 | 1157 | 			return p; | 
 | 1158 | 	} | 
 | 1159 | 	p = snd_array_new(&codec->cvt_setups); | 
 | 1160 | 	if (p) | 
 | 1161 | 		p->nid = nid; | 
 | 1162 | 	return p; | 
 | 1163 | } | 
 | 1164 |  | 
 | 1165 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 |  * codec destructor | 
 | 1167 |  */ | 
 | 1168 | static void snd_hda_codec_free(struct hda_codec *codec) | 
 | 1169 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1170 | 	if (!codec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | 		return; | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1172 | 	cancel_delayed_work_sync(&codec->jackpoll_work); | 
| Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1173 | 	snd_hda_jack_tbl_clear(codec); | 
| Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1174 | 	free_init_pincfgs(codec); | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1175 | #ifdef CONFIG_PM | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1176 | 	cancel_delayed_work(&codec->power_work); | 
| Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1177 | 	flush_workqueue(codec->bus->workq); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1178 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | 	list_del(&codec->list); | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1180 | 	snd_array_free(&codec->mixers); | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1181 | 	snd_array_free(&codec->nids); | 
| Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1182 | 	snd_array_free(&codec->cvt_setups); | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1183 | 	snd_array_free(&codec->conn_lists); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1184 | 	snd_array_free(&codec->spdif_out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | 	codec->bus->caddr_tbl[codec->addr] = NULL; | 
 | 1186 | 	if (codec->patch_ops.free) | 
 | 1187 | 		codec->patch_ops.free(codec); | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1188 | #ifdef CONFIG_PM | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 1189 | 	if (!codec->pm_down_notified) /* cancel leftover refcounts */ | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1190 | 		hda_call_pm_notify(codec->bus, false); | 
 | 1191 | #endif | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1192 | 	module_put(codec->owner); | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1193 | 	free_hda_cache(&codec->amp_cache); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1194 | 	free_hda_cache(&codec->cmd_cache); | 
| Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1195 | 	kfree(codec->vendor_name); | 
 | 1196 | 	kfree(codec->chip_name); | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1197 | 	kfree(codec->modelname); | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1198 | 	kfree(codec->wcaps); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | 	kfree(codec); | 
 | 1200 | } | 
 | 1201 |  | 
| Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1202 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, | 
 | 1203 | 				hda_nid_t fg, unsigned int power_state); | 
 | 1204 |  | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1205 | static unsigned int hda_set_power_state(struct hda_codec *codec, | 
| Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1206 | 				unsigned int power_state); | 
 | 1207 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | /** | 
 | 1209 |  * snd_hda_codec_new - create a HDA codec | 
 | 1210 |  * @bus: the bus to assign | 
 | 1211 |  * @codec_addr: the codec address | 
 | 1212 |  * @codecp: the pointer to store the generated codec | 
 | 1213 |  * | 
 | 1214 |  * Returns 0 if successful, or a negative error code. | 
 | 1215 |  */ | 
| Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1216 | int snd_hda_codec_new(struct hda_bus *bus, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1217 | 				unsigned int codec_addr, | 
 | 1218 | 				struct hda_codec **codecp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { | 
 | 1220 | 	struct hda_codec *codec; | 
| Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1221 | 	char component[31]; | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1222 | 	hda_nid_t fg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | 	int err; | 
 | 1224 |  | 
| Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1225 | 	if (snd_BUG_ON(!bus)) | 
 | 1226 | 		return -EINVAL; | 
 | 1227 | 	if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) | 
 | 1228 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 |  | 
 | 1230 | 	if (bus->caddr_tbl[codec_addr]) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1231 | 		snd_printk(KERN_ERR "hda_codec: " | 
 | 1232 | 			   "address 0x%x is already occupied\n", codec_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | 		return -EBUSY; | 
 | 1234 | 	} | 
 | 1235 |  | 
| Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1236 | 	codec = kzalloc(sizeof(*codec), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | 	if (codec == NULL) { | 
 | 1238 | 		snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); | 
 | 1239 | 		return -ENOMEM; | 
 | 1240 | 	} | 
 | 1241 |  | 
 | 1242 | 	codec->bus = bus; | 
 | 1243 | 	codec->addr = codec_addr; | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1244 | 	mutex_init(&codec->spdif_mutex); | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1245 | 	mutex_init(&codec->control_mutex); | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1246 | 	mutex_init(&codec->hash_mutex); | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1247 | 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1248 | 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1249 | 	snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); | 
 | 1250 | 	snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1251 | 	snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1252 | 	snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1253 | 	snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); | 
| Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1254 | 	snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1255 | 	snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); | 
| Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1256 | 	snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); | 
| Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1257 | 	snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1258 | 	INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 |  | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1260 | #ifdef CONFIG_PM | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1261 | 	spin_lock_init(&codec->power_lock); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1262 | 	INIT_DELAYED_WORK(&codec->power_work, hda_power_work); | 
 | 1263 | 	/* snd_hda_codec_new() marks the codec as power-up, and leave it as is. | 
 | 1264 | 	 * the caller has to power down appropriatley after initialization | 
 | 1265 | 	 * phase. | 
 | 1266 | 	 */ | 
 | 1267 | 	hda_keep_power_on(codec); | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1268 | 	hda_call_pm_notify(bus, true); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1269 | #endif | 
 | 1270 |  | 
| Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1271 | 	if (codec->bus->modelname) { | 
 | 1272 | 		codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); | 
 | 1273 | 		if (!codec->modelname) { | 
 | 1274 | 			snd_hda_codec_free(codec); | 
 | 1275 | 			return -ENODEV; | 
 | 1276 | 		} | 
 | 1277 | 	} | 
 | 1278 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | 	list_add_tail(&codec->list, &bus->codec_list); | 
 | 1280 | 	bus->caddr_tbl[codec_addr] = codec; | 
 | 1281 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1282 | 	codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, | 
 | 1283 | 					      AC_PAR_VENDOR_ID); | 
| Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1284 | 	if (codec->vendor_id == -1) | 
 | 1285 | 		/* read again, hopefully the access method was corrected | 
 | 1286 | 		 * in the last read... | 
 | 1287 | 		 */ | 
 | 1288 | 		codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, | 
 | 1289 | 						      AC_PAR_VENDOR_ID); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1290 | 	codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, | 
 | 1291 | 						 AC_PAR_SUBSYSTEM_ID); | 
 | 1292 | 	codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, | 
 | 1293 | 						AC_PAR_REV_ID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 |  | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1295 | 	setup_fg_nodes(codec); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1296 | 	if (!codec->afg && !codec->mfg) { | 
| Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1297 | 		snd_printdd("hda_codec: no AFG or MFG node found\n"); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1298 | 		err = -ENODEV; | 
 | 1299 | 		goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | 	} | 
 | 1301 |  | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1302 | 	fg = codec->afg ? codec->afg : codec->mfg; | 
 | 1303 | 	err = read_widget_caps(codec, fg); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1304 | 	if (err < 0) { | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1305 | 		snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1306 | 		goto error; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1307 | 	} | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1308 | 	err = read_pin_defaults(codec); | 
 | 1309 | 	if (err < 0) | 
 | 1310 | 		goto error; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1311 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1312 | 	if (!codec->subsystem_id) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1313 | 		codec->subsystem_id = | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1314 | 			snd_hda_codec_read(codec, fg, 0, | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1315 | 					   AC_VERB_GET_SUBSYSTEM_ID, 0); | 
| Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1316 | 	} | 
 | 1317 |  | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1318 | #ifdef CONFIG_PM | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1319 | 	codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, | 
| Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1320 | 					AC_PWRST_CLKSTOP); | 
 | 1321 | 	if (!codec->d3_stop_clk) | 
 | 1322 | 		bus->power_keep_link_on = 1; | 
| Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1323 | #endif | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1324 | 	codec->epss = snd_hda_codec_get_supported_ps(codec, fg, | 
| Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1325 | 					AC_PWRST_EPSS); | 
| Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1326 |  | 
| Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1327 | 	/* power-up all before initialization */ | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1328 | 	hda_set_power_state(codec, AC_PWRST_D0); | 
| Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1329 |  | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1330 | 	snd_hda_codec_proc_new(codec); | 
 | 1331 |  | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1332 | 	snd_hda_create_hwdep(codec); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1333 |  | 
 | 1334 | 	sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, | 
 | 1335 | 		codec->subsystem_id, codec->revision_id); | 
 | 1336 | 	snd_component_add(codec->bus->card, component); | 
 | 1337 |  | 
 | 1338 | 	if (codecp) | 
 | 1339 | 		*codecp = codec; | 
 | 1340 | 	return 0; | 
| Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1341 |  | 
 | 1342 |  error: | 
 | 1343 | 	snd_hda_codec_free(codec); | 
 | 1344 | 	return err; | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1345 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1346 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1347 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1348 | /** | 
 | 1349 |  * snd_hda_codec_configure - (Re-)configure the HD-audio codec | 
 | 1350 |  * @codec: the HDA codec | 
 | 1351 |  * | 
 | 1352 |  * Start parsing of the given codec tree and (re-)initialize the whole | 
 | 1353 |  * patch instance. | 
 | 1354 |  * | 
 | 1355 |  * Returns 0 if successful or a negative error code. | 
 | 1356 |  */ | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1357 | int snd_hda_codec_configure(struct hda_codec *codec) | 
 | 1358 | { | 
 | 1359 | 	int err; | 
 | 1360 |  | 
| Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1361 | 	codec->preset = find_codec_preset(codec); | 
| Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1362 | 	if (!codec->vendor_name || !codec->chip_name) { | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1363 | 		err = get_codec_name(codec); | 
 | 1364 | 		if (err < 0) | 
 | 1365 | 			return err; | 
 | 1366 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 |  | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1368 | 	if (is_generic_config(codec)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | 		err = snd_hda_parse_generic_codec(codec); | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1370 | 		goto patched; | 
 | 1371 | 	} | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1372 | 	if (codec->preset && codec->preset->patch) { | 
 | 1373 | 		err = codec->preset->patch(codec); | 
 | 1374 | 		goto patched; | 
 | 1375 | 	} | 
 | 1376 |  | 
 | 1377 | 	/* call the default parser */ | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1378 | 	err = snd_hda_parse_generic_codec(codec); | 
| Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 1379 | 	if (err < 0) | 
 | 1380 | 		printk(KERN_ERR "hda-codec: No codec parser is available\n"); | 
| Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1381 |  | 
 | 1382 |  patched: | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1383 | 	if (!err && codec->patch_ops.unsol_event) | 
 | 1384 | 		err = init_unsol_queue(codec->bus); | 
| Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1385 | 	/* audio codec should override the mixer name */ | 
 | 1386 | 	if (!err && (codec->afg || !*codec->bus->card->mixername)) | 
 | 1387 | 		snprintf(codec->bus->card->mixername, | 
 | 1388 | 			 sizeof(codec->bus->card->mixername), | 
 | 1389 | 			 "%s %s", codec->vendor_name, codec->chip_name); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1390 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } | 
| Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1392 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 |  | 
| Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1394 | /* update the stream-id if changed */ | 
 | 1395 | static void update_pcm_stream_id(struct hda_codec *codec, | 
 | 1396 | 				 struct hda_cvt_setup *p, hda_nid_t nid, | 
 | 1397 | 				 u32 stream_tag, int channel_id) | 
 | 1398 | { | 
 | 1399 | 	unsigned int oldval, newval; | 
 | 1400 |  | 
 | 1401 | 	if (p->stream_tag != stream_tag || p->channel_id != channel_id) { | 
 | 1402 | 		oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); | 
 | 1403 | 		newval = (stream_tag << 4) | channel_id; | 
 | 1404 | 		if (oldval != newval) | 
 | 1405 | 			snd_hda_codec_write(codec, nid, 0, | 
 | 1406 | 					    AC_VERB_SET_CHANNEL_STREAMID, | 
 | 1407 | 					    newval); | 
 | 1408 | 		p->stream_tag = stream_tag; | 
 | 1409 | 		p->channel_id = channel_id; | 
 | 1410 | 	} | 
 | 1411 | } | 
 | 1412 |  | 
 | 1413 | /* update the format-id if changed */ | 
 | 1414 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, | 
 | 1415 | 			      hda_nid_t nid, int format) | 
 | 1416 | { | 
 | 1417 | 	unsigned int oldval; | 
 | 1418 |  | 
 | 1419 | 	if (p->format_id != format) { | 
 | 1420 | 		oldval = snd_hda_codec_read(codec, nid, 0, | 
 | 1421 | 					    AC_VERB_GET_STREAM_FORMAT, 0); | 
 | 1422 | 		if (oldval != format) { | 
 | 1423 | 			msleep(1); | 
 | 1424 | 			snd_hda_codec_write(codec, nid, 0, | 
 | 1425 | 					    AC_VERB_SET_STREAM_FORMAT, | 
 | 1426 | 					    format); | 
 | 1427 | 		} | 
 | 1428 | 		p->format_id = format; | 
 | 1429 | 	} | 
 | 1430 | } | 
 | 1431 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | /** | 
 | 1433 |  * snd_hda_codec_setup_stream - set up the codec for streaming | 
 | 1434 |  * @codec: the CODEC to set up | 
 | 1435 |  * @nid: the NID to set up | 
 | 1436 |  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. | 
 | 1437 |  * @channel_id: channel id to pass, zero based. | 
 | 1438 |  * @format: stream format. | 
 | 1439 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1440 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, | 
 | 1441 | 				u32 stream_tag, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | 				int channel_id, int format) | 
 | 1443 | { | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1444 | 	struct hda_codec *c; | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1445 | 	struct hda_cvt_setup *p; | 
| Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1446 | 	int type; | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1447 | 	int i; | 
 | 1448 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1449 | 	if (!nid) | 
| Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1450 | 		return; | 
 | 1451 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1452 | 	snd_printdd("hda_codec_setup_stream: " | 
 | 1453 | 		    "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | 		    nid, stream_tag, channel_id, format); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1455 | 	p = get_hda_cvt_setup(codec, nid); | 
 | 1456 | 	if (!p) | 
 | 1457 | 		return; | 
| Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1458 |  | 
 | 1459 | 	if (codec->pcm_format_first) | 
 | 1460 | 		update_pcm_format(codec, p, nid, format); | 
 | 1461 | 	update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); | 
 | 1462 | 	if (!codec->pcm_format_first) | 
 | 1463 | 		update_pcm_format(codec, p, nid, format); | 
 | 1464 |  | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1465 | 	p->active = 1; | 
 | 1466 | 	p->dirty = 0; | 
 | 1467 |  | 
 | 1468 | 	/* make other inactive cvts with the same stream-tag dirty */ | 
| Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1469 | 	type = get_wcaps_type(get_wcaps(codec, nid)); | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1470 | 	list_for_each_entry(c, &codec->bus->codec_list, list) { | 
 | 1471 | 		for (i = 0; i < c->cvt_setups.used; i++) { | 
 | 1472 | 			p = snd_array_elem(&c->cvt_setups, i); | 
| Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1473 | 			if (!p->active && p->stream_tag == stream_tag && | 
| David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1474 | 			    get_wcaps_type(get_wcaps(c, p->nid)) == type) | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1475 | 				p->dirty = 1; | 
 | 1476 | 		} | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1477 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1479 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 |  | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1481 | static void really_cleanup_stream(struct hda_codec *codec, | 
 | 1482 | 				  struct hda_cvt_setup *q); | 
 | 1483 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1484 | /** | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1485 |  * __snd_hda_codec_cleanup_stream - clean up the codec for closing | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1486 |  * @codec: the CODEC to clean up | 
 | 1487 |  * @nid: the NID to clean up | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1488 |  * @do_now: really clean up the stream instead of clearing the active flag | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1489 |  */ | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1490 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, | 
 | 1491 | 				    int do_now) | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1492 | { | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1493 | 	struct hda_cvt_setup *p; | 
 | 1494 |  | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1495 | 	if (!nid) | 
 | 1496 | 		return; | 
 | 1497 |  | 
| Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1498 | 	if (codec->no_sticky_stream) | 
 | 1499 | 		do_now = 1; | 
 | 1500 |  | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1501 | 	snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1502 | 	p = get_hda_cvt_setup(codec, nid); | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1503 | 	if (p) { | 
 | 1504 | 		/* here we just clear the active flag when do_now isn't set; | 
 | 1505 | 		 * actual clean-ups will be done later in | 
 | 1506 | 		 * purify_inactive_streams() called from snd_hda_codec_prpapre() | 
 | 1507 | 		 */ | 
 | 1508 | 		if (do_now) | 
 | 1509 | 			really_cleanup_stream(codec, p); | 
 | 1510 | 		else | 
 | 1511 | 			p->active = 0; | 
 | 1512 | 	} | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1513 | } | 
| Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1514 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1515 |  | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1516 | static void really_cleanup_stream(struct hda_codec *codec, | 
 | 1517 | 				  struct hda_cvt_setup *q) | 
 | 1518 | { | 
 | 1519 | 	hda_nid_t nid = q->nid; | 
| Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1520 | 	if (q->stream_tag || q->channel_id) | 
 | 1521 | 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); | 
 | 1522 | 	if (q->format_id) | 
 | 1523 | 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 | 
 | 1524 | ); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1525 | 	memset(q, 0, sizeof(*q)); | 
 | 1526 | 	q->nid = nid; | 
 | 1527 | } | 
 | 1528 |  | 
 | 1529 | /* clean up the all conflicting obsolete streams */ | 
 | 1530 | static void purify_inactive_streams(struct hda_codec *codec) | 
 | 1531 | { | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1532 | 	struct hda_codec *c; | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1533 | 	int i; | 
 | 1534 |  | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1535 | 	list_for_each_entry(c, &codec->bus->codec_list, list) { | 
 | 1536 | 		for (i = 0; i < c->cvt_setups.used; i++) { | 
 | 1537 | 			struct hda_cvt_setup *p; | 
 | 1538 | 			p = snd_array_elem(&c->cvt_setups, i); | 
 | 1539 | 			if (p->dirty) | 
 | 1540 | 				really_cleanup_stream(c, p); | 
 | 1541 | 		} | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1542 | 	} | 
 | 1543 | } | 
 | 1544 |  | 
| Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1545 | #ifdef CONFIG_PM | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1546 | /* clean up all streams; called from suspend */ | 
 | 1547 | static void hda_cleanup_all_streams(struct hda_codec *codec) | 
 | 1548 | { | 
 | 1549 | 	int i; | 
 | 1550 |  | 
 | 1551 | 	for (i = 0; i < codec->cvt_setups.used; i++) { | 
 | 1552 | 		struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); | 
 | 1553 | 		if (p->stream_tag) | 
 | 1554 | 			really_cleanup_stream(codec, p); | 
 | 1555 | 	} | 
 | 1556 | } | 
| Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1557 | #endif | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1558 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | /* | 
 | 1560 |  * amp access functions | 
 | 1561 |  */ | 
 | 1562 |  | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1563 | /* FIXME: more better hash key? */ | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1564 | #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) | 
| Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1565 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1566 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) | 
 | 1567 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | #define INFO_AMP_CAPS	(1<<0) | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1569 | #define INFO_AMP_VOL(ch)	(1 << (1 + (ch))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 |  | 
 | 1571 | /* initialize the hash table */ | 
| Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1572 | static void init_hda_cache(struct hda_cache_rec *cache, | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1573 | 				     unsigned int record_size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | { | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1575 | 	memset(cache, 0, sizeof(*cache)); | 
 | 1576 | 	memset(cache->hash, 0xff, sizeof(cache->hash)); | 
| Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1577 | 	snd_array_init(&cache->buf, record_size, 64); | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1578 | } | 
 | 1579 |  | 
| Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1580 | static void free_hda_cache(struct hda_cache_rec *cache) | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1581 | { | 
| Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1582 | 	snd_array_free(&cache->buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } | 
 | 1584 |  | 
 | 1585 | /* query the hash.  allocate an entry if not found. */ | 
| Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1586 | static struct hda_cache_head  *get_hash(struct hda_cache_rec *cache, u32 key) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | { | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1588 | 	u16 idx = key % (u16)ARRAY_SIZE(cache->hash); | 
 | 1589 | 	u16 cur = cache->hash[idx]; | 
 | 1590 | 	struct hda_cache_head *info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 |  | 
 | 1592 | 	while (cur != 0xffff) { | 
| Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1593 | 		info = snd_array_elem(&cache->buf, cur); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | 		if (info->key == key) | 
 | 1595 | 			return info; | 
 | 1596 | 		cur = info->next; | 
 | 1597 | 	} | 
| Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1598 | 	return NULL; | 
 | 1599 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 |  | 
| Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1601 | /* query the hash.  allocate an entry if not found. */ | 
 | 1602 | static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache, | 
 | 1603 | 					      u32 key) | 
 | 1604 | { | 
 | 1605 | 	struct hda_cache_head *info = get_hash(cache, key); | 
 | 1606 | 	if (!info) { | 
 | 1607 | 		u16 idx, cur; | 
 | 1608 | 		/* add a new hash entry */ | 
 | 1609 | 		info = snd_array_new(&cache->buf); | 
 | 1610 | 		if (!info) | 
 | 1611 | 			return NULL; | 
 | 1612 | 		cur = snd_array_index(&cache->buf, info); | 
 | 1613 | 		info->key = key; | 
 | 1614 | 		info->val = 0; | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1615 | 		info->dirty = 0; | 
| Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1616 | 		idx = key % (u16)ARRAY_SIZE(cache->hash); | 
 | 1617 | 		info->next = cache->hash[idx]; | 
 | 1618 | 		cache->hash[idx] = cur; | 
 | 1619 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | 	return info; | 
 | 1621 | } | 
 | 1622 |  | 
| Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1623 | /* query and allocate an amp hash entry */ | 
 | 1624 | static inline struct hda_amp_info * | 
 | 1625 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) | 
 | 1626 | { | 
 | 1627 | 	return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); | 
 | 1628 | } | 
 | 1629 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1630 | /* overwrite the value with the key in the caps hash */ | 
 | 1631 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) | 
 | 1632 | { | 
 | 1633 | 	struct hda_amp_info *info; | 
 | 1634 |  | 
 | 1635 | 	mutex_lock(&codec->hash_mutex); | 
 | 1636 | 	info = get_alloc_amp_hash(codec, key); | 
 | 1637 | 	if (!info) { | 
 | 1638 | 		mutex_unlock(&codec->hash_mutex); | 
 | 1639 | 		return -EINVAL; | 
 | 1640 | 	} | 
 | 1641 | 	info->amp_caps = val; | 
 | 1642 | 	info->head.val |= INFO_AMP_CAPS; | 
 | 1643 | 	mutex_unlock(&codec->hash_mutex); | 
 | 1644 | 	return 0; | 
 | 1645 | } | 
 | 1646 |  | 
 | 1647 | /* query the value from the caps hash; if not found, fetch the current | 
 | 1648 |  * value from the given function and store in the hash | 
 | 1649 |  */ | 
 | 1650 | static unsigned int | 
 | 1651 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, | 
 | 1652 | 		unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) | 
 | 1653 | { | 
 | 1654 | 	struct hda_amp_info *info; | 
 | 1655 | 	unsigned int val; | 
 | 1656 |  | 
 | 1657 | 	mutex_lock(&codec->hash_mutex); | 
 | 1658 | 	info = get_alloc_amp_hash(codec, key); | 
 | 1659 | 	if (!info) { | 
 | 1660 | 		mutex_unlock(&codec->hash_mutex); | 
 | 1661 | 		return 0; | 
 | 1662 | 	} | 
 | 1663 | 	if (!(info->head.val & INFO_AMP_CAPS)) { | 
 | 1664 | 		mutex_unlock(&codec->hash_mutex); /* for reentrance */ | 
 | 1665 | 		val = func(codec, nid, dir); | 
 | 1666 | 		write_caps_hash(codec, key, val); | 
 | 1667 | 	} else { | 
 | 1668 | 		val = info->amp_caps; | 
 | 1669 | 		mutex_unlock(&codec->hash_mutex); | 
 | 1670 | 	} | 
 | 1671 | 	return val; | 
 | 1672 | } | 
 | 1673 |  | 
 | 1674 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, | 
 | 1675 | 				 int direction) | 
 | 1676 | { | 
 | 1677 | 	if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) | 
 | 1678 | 		nid = codec->afg; | 
 | 1679 | 	return snd_hda_param_read(codec, nid, | 
 | 1680 | 				  direction == HDA_OUTPUT ? | 
 | 1681 | 				  AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); | 
 | 1682 | } | 
 | 1683 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1684 | /** | 
 | 1685 |  * query_amp_caps - query AMP capabilities | 
 | 1686 |  * @codec: the HD-auio codec | 
 | 1687 |  * @nid: the NID to query | 
 | 1688 |  * @direction: either #HDA_INPUT or #HDA_OUTPUT | 
 | 1689 |  * | 
 | 1690 |  * Query AMP capabilities for the given widget and direction. | 
 | 1691 |  * Returns the obtained capability bits. | 
 | 1692 |  * | 
 | 1693 |  * When cap bits have been already read, this doesn't read again but | 
 | 1694 |  * returns the cached value. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 |  */ | 
| Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1696 | u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1698 | 	return query_caps_hash(codec, nid, direction, | 
 | 1699 | 			       HDA_HASH_KEY(nid, direction, 0), | 
 | 1700 | 			       read_amp_cap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1702 | EXPORT_SYMBOL_HDA(query_amp_caps); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1704 | /** | 
 | 1705 |  * snd_hda_override_amp_caps - Override the AMP capabilities | 
 | 1706 |  * @codec: the CODEC to clean up | 
 | 1707 |  * @nid: the NID to clean up | 
 | 1708 |  * @direction: either #HDA_INPUT or #HDA_OUTPUT | 
 | 1709 |  * @caps: the capability bits to set | 
 | 1710 |  * | 
 | 1711 |  * Override the cached AMP caps bits value by the given one. | 
 | 1712 |  * This function is useful if the driver needs to adjust the AMP ranges, | 
 | 1713 |  * e.g. limit to 0dB, etc. | 
 | 1714 |  * | 
 | 1715 |  * Returns zero if successful or a negative error code. | 
 | 1716 |  */ | 
| Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1717 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, | 
 | 1718 | 			      unsigned int caps) | 
 | 1719 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1720 | 	return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); | 
| Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1721 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1722 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); | 
| Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1723 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1724 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, | 
 | 1725 | 				 int dir) | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1726 | { | 
 | 1727 | 	return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); | 
 | 1728 | } | 
 | 1729 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1730 | /** | 
 | 1731 |  * snd_hda_query_pin_caps - Query PIN capabilities | 
 | 1732 |  * @codec: the HD-auio codec | 
 | 1733 |  * @nid: the NID to query | 
 | 1734 |  * | 
 | 1735 |  * Query PIN capabilities for the given widget. | 
 | 1736 |  * Returns the obtained capability bits. | 
 | 1737 |  * | 
 | 1738 |  * When cap bits have been already read, this doesn't read again but | 
 | 1739 |  * returns the cached value. | 
 | 1740 |  */ | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1741 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) | 
 | 1742 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1743 | 	return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1744 | 			       read_pin_cap); | 
 | 1745 | } | 
| Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1746 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); | 
 | 1747 |  | 
| Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1748 | /** | 
| Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1749 |  * snd_hda_override_pin_caps - Override the pin capabilities | 
 | 1750 |  * @codec: the CODEC | 
 | 1751 |  * @nid: the NID to override | 
 | 1752 |  * @caps: the capability bits to set | 
 | 1753 |  * | 
 | 1754 |  * Override the cached PIN capabilitiy bits value by the given one. | 
 | 1755 |  * | 
 | 1756 |  * Returns zero if successful or a negative error code. | 
 | 1757 |  */ | 
 | 1758 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, | 
 | 1759 | 			      unsigned int caps) | 
 | 1760 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1761 | 	return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); | 
| Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1762 | } | 
 | 1763 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); | 
 | 1764 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1765 | /* read or sync the hash value with the current value; | 
 | 1766 |  * call within hash_mutex | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 |  */ | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1768 | static struct hda_amp_info * | 
 | 1769 | update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1770 | 		int direction, int index, bool init_only) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1772 | 	struct hda_amp_info *info; | 
 | 1773 | 	unsigned int parm, val = 0; | 
 | 1774 | 	bool val_read = false; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1776 |  retry: | 
 | 1777 | 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); | 
 | 1778 | 	if (!info) | 
 | 1779 | 		return NULL; | 
 | 1780 | 	if (!(info->head.val & INFO_AMP_VOL(ch))) { | 
 | 1781 | 		if (!val_read) { | 
 | 1782 | 			mutex_unlock(&codec->hash_mutex); | 
 | 1783 | 			parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; | 
 | 1784 | 			parm |= direction == HDA_OUTPUT ? | 
 | 1785 | 				AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; | 
 | 1786 | 			parm |= index; | 
 | 1787 | 			val = snd_hda_codec_read(codec, nid, 0, | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1788 | 				 AC_VERB_GET_AMP_GAIN_MUTE, parm); | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1789 | 			val &= 0xff; | 
 | 1790 | 			val_read = true; | 
 | 1791 | 			mutex_lock(&codec->hash_mutex); | 
 | 1792 | 			goto retry; | 
 | 1793 | 		} | 
 | 1794 | 		info->vol[ch] = val; | 
 | 1795 | 		info->head.val |= INFO_AMP_VOL(ch); | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1796 | 	} else if (init_only) | 
 | 1797 | 		return NULL; | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1798 | 	return info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } | 
 | 1800 |  | 
 | 1801 | /* | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1802 |  * write the current volume in info to the h/w | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 |  */ | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1804 | static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1805 | 			 hda_nid_t nid, int ch, int direction, int index, | 
 | 1806 | 			 int val) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { | 
 | 1808 | 	u32 parm; | 
 | 1809 |  | 
 | 1810 | 	parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; | 
 | 1811 | 	parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; | 
 | 1812 | 	parm |= index << AC_AMP_SET_INDEX_SHIFT; | 
| Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1813 | 	if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) && | 
 | 1814 | 	    (info->amp_caps & AC_AMPCAP_MIN_MUTE)) | 
 | 1815 | 		; /* set the zero value as a fake mute */ | 
 | 1816 | 	else | 
 | 1817 | 		parm |= val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); | 
 | 1819 | } | 
 | 1820 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1821 | /** | 
 | 1822 |  * snd_hda_codec_amp_read - Read AMP value | 
 | 1823 |  * @codec: HD-audio codec | 
 | 1824 |  * @nid: NID to read the AMP value | 
 | 1825 |  * @ch: channel (left=0 or right=1) | 
 | 1826 |  * @direction: #HDA_INPUT or #HDA_OUTPUT | 
 | 1827 |  * @index: the index value (only for input direction) | 
 | 1828 |  * | 
 | 1829 |  * Read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 |  */ | 
| Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1831 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, | 
 | 1832 | 			   int direction, int index) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1834 | 	struct hda_amp_info *info; | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1835 | 	unsigned int val = 0; | 
 | 1836 |  | 
 | 1837 | 	mutex_lock(&codec->hash_mutex); | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1838 | 	info = update_amp_hash(codec, nid, ch, direction, index, false); | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1839 | 	if (info) | 
 | 1840 | 		val = info->vol[ch]; | 
 | 1841 | 	mutex_unlock(&codec->hash_mutex); | 
 | 1842 | 	return val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1844 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 |  | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1846 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, | 
 | 1847 | 			    int direction, int idx, int mask, int val, | 
 | 1848 | 			    bool init_only) | 
 | 1849 | { | 
 | 1850 | 	struct hda_amp_info *info; | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 1851 | 	unsigned int cache_only; | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1852 |  | 
 | 1853 | 	if (snd_BUG_ON(mask & ~0xff)) | 
 | 1854 | 		mask &= 0xff; | 
 | 1855 | 	val &= mask; | 
 | 1856 |  | 
 | 1857 | 	mutex_lock(&codec->hash_mutex); | 
 | 1858 | 	info = update_amp_hash(codec, nid, ch, direction, idx, init_only); | 
 | 1859 | 	if (!info) { | 
 | 1860 | 		mutex_unlock(&codec->hash_mutex); | 
 | 1861 | 		return 0; | 
 | 1862 | 	} | 
 | 1863 | 	val |= info->vol[ch] & ~mask; | 
 | 1864 | 	if (info->vol[ch] == val) { | 
 | 1865 | 		mutex_unlock(&codec->hash_mutex); | 
 | 1866 | 		return 0; | 
 | 1867 | 	} | 
 | 1868 | 	info->vol[ch] = val; | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 1869 | 	cache_only = info->head.dirty = codec->cached_write; | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1870 | 	mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 1871 | 	if (!cache_only) | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1872 | 		put_vol_mute(codec, info, nid, ch, direction, idx, val); | 
 | 1873 | 	return 1; | 
 | 1874 | } | 
 | 1875 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1876 | /** | 
 | 1877 |  * snd_hda_codec_amp_update - update the AMP value | 
 | 1878 |  * @codec: HD-audio codec | 
 | 1879 |  * @nid: NID to read the AMP value | 
 | 1880 |  * @ch: channel (left=0 or right=1) | 
 | 1881 |  * @direction: #HDA_INPUT or #HDA_OUTPUT | 
 | 1882 |  * @idx: the index value (only for input direction) | 
 | 1883 |  * @mask: bit mask to set | 
 | 1884 |  * @val: the bits value to set | 
 | 1885 |  * | 
 | 1886 |  * Update the AMP value with a bit mask. | 
 | 1887 |  * Returns 0 if the value is unchanged, 1 if changed. | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1888 |  */ | 
| Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1889 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, | 
 | 1890 | 			     int direction, int idx, int mask, int val) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | { | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1892 | 	return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1894 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1896 | /** | 
 | 1897 |  * snd_hda_codec_amp_stereo - update the AMP stereo values | 
 | 1898 |  * @codec: HD-audio codec | 
 | 1899 |  * @nid: NID to read the AMP value | 
 | 1900 |  * @direction: #HDA_INPUT or #HDA_OUTPUT | 
 | 1901 |  * @idx: the index value (only for input direction) | 
 | 1902 |  * @mask: bit mask to set | 
 | 1903 |  * @val: the bits value to set | 
 | 1904 |  * | 
 | 1905 |  * Update the AMP values like snd_hda_codec_amp_update(), but for a | 
 | 1906 |  * stereo widget with the same mask and value. | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1907 |  */ | 
 | 1908 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, | 
 | 1909 | 			     int direction, int idx, int mask, int val) | 
 | 1910 | { | 
 | 1911 | 	int ch, ret = 0; | 
| Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1912 |  | 
 | 1913 | 	if (snd_BUG_ON(mask & ~0xff)) | 
 | 1914 | 		mask &= 0xff; | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1915 | 	for (ch = 0; ch < 2; ch++) | 
 | 1916 | 		ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, | 
 | 1917 | 						idx, mask, val); | 
 | 1918 | 	return ret; | 
 | 1919 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1920 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1921 |  | 
| Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1922 | /* Works like snd_hda_codec_amp_update() but it writes the value only at | 
 | 1923 |  * the first access.  If the amp was already initialized / updated beforehand, | 
 | 1924 |  * this does nothing. | 
 | 1925 |  */ | 
 | 1926 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, | 
 | 1927 | 			   int dir, int idx, int mask, int val) | 
 | 1928 | { | 
 | 1929 | 	return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); | 
 | 1930 | } | 
 | 1931 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); | 
 | 1932 |  | 
 | 1933 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, | 
 | 1934 | 				  int dir, int idx, int mask, int val) | 
 | 1935 | { | 
 | 1936 | 	int ch, ret = 0; | 
 | 1937 |  | 
 | 1938 | 	if (snd_BUG_ON(mask & ~0xff)) | 
 | 1939 | 		mask &= 0xff; | 
 | 1940 | 	for (ch = 0; ch < 2; ch++) | 
 | 1941 | 		ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, | 
 | 1942 | 					      idx, mask, val); | 
 | 1943 | 	return ret; | 
 | 1944 | } | 
 | 1945 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); | 
 | 1946 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1947 | /** | 
 | 1948 |  * snd_hda_codec_resume_amp - Resume all AMP commands from the cache | 
 | 1949 |  * @codec: HD-audio codec | 
 | 1950 |  * | 
 | 1951 |  * Resume the all amp commands from the cache. | 
 | 1952 |  */ | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1953 | void snd_hda_codec_resume_amp(struct hda_codec *codec) | 
 | 1954 | { | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1955 | 	int i; | 
 | 1956 |  | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1957 | 	mutex_lock(&codec->hash_mutex); | 
 | 1958 | 	for (i = 0; i < codec->amp_cache.buf.used; i++) { | 
 | 1959 | 		struct hda_amp_info *buffer; | 
 | 1960 | 		u32 key; | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1961 | 		hda_nid_t nid; | 
 | 1962 | 		unsigned int idx, dir, ch; | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1963 |  | 
 | 1964 | 		buffer = snd_array_elem(&codec->amp_cache.buf, i); | 
 | 1965 | 		key = buffer->head.key; | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1966 | 		if (!key) | 
 | 1967 | 			continue; | 
 | 1968 | 		nid = key & 0xff; | 
 | 1969 | 		idx = (key >> 16) & 0xff; | 
 | 1970 | 		dir = (key >> 24) & 0xff; | 
 | 1971 | 		for (ch = 0; ch < 2; ch++) { | 
 | 1972 | 			if (!(buffer->head.val & INFO_AMP_VOL(ch))) | 
 | 1973 | 				continue; | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1974 | 			if (!buffer->head.dirty) | 
 | 1975 | 				continue; | 
 | 1976 | 			buffer->head.dirty = 0; | 
 | 1977 | 			mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1978 | 			put_vol_mute(codec, buffer, nid, ch, dir, idx, | 
 | 1979 | 				     buffer->vol[ch]); | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1980 | 			mutex_lock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1981 | 		} | 
 | 1982 | 	} | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1983 | 	mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1984 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1985 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 |  | 
| Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1987 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, | 
 | 1988 | 			     unsigned int ofs) | 
 | 1989 | { | 
 | 1990 | 	u32 caps = query_amp_caps(codec, nid, dir); | 
 | 1991 | 	/* get num steps */ | 
 | 1992 | 	caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | 
 | 1993 | 	if (ofs < caps) | 
 | 1994 | 		caps -= ofs; | 
 | 1995 | 	return caps; | 
 | 1996 | } | 
 | 1997 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1998 | /** | 
 | 1999 |  * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer | 
 | 2000 |  * | 
 | 2001 |  * The control element is supposed to have the private_value field | 
 | 2002 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2003 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2004 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, | 
 | 2005 | 				  struct snd_ctl_elem_info *uinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | { | 
 | 2007 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2008 | 	u16 nid = get_amp_nid(kcontrol); | 
 | 2009 | 	u8 chs = get_amp_channels(kcontrol); | 
 | 2010 | 	int dir = get_amp_direction(kcontrol); | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2011 | 	unsigned int ofs = get_amp_offset(kcontrol); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 |  | 
| Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2013 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | 
 | 2014 | 	uinfo->count = chs == 3 ? 2 : 1; | 
 | 2015 | 	uinfo->value.integer.min = 0; | 
 | 2016 | 	uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); | 
 | 2017 | 	if (!uinfo->value.integer.max) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2018 | 		printk(KERN_WARNING "hda_codec: " | 
| Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2019 | 		       "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, | 
 | 2020 | 		       kcontrol->id.name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | 		return -EINVAL; | 
 | 2022 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | 	return 0; | 
 | 2024 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2025 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 |  | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2027 |  | 
 | 2028 | static inline unsigned int | 
 | 2029 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, | 
 | 2030 | 	       int ch, int dir, int idx, unsigned int ofs) | 
 | 2031 | { | 
 | 2032 | 	unsigned int val; | 
 | 2033 | 	val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); | 
 | 2034 | 	val &= HDA_AMP_VOLMASK; | 
 | 2035 | 	if (val >= ofs) | 
 | 2036 | 		val -= ofs; | 
 | 2037 | 	else | 
 | 2038 | 		val = 0; | 
 | 2039 | 	return val; | 
 | 2040 | } | 
 | 2041 |  | 
 | 2042 | static inline int | 
 | 2043 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, | 
 | 2044 | 		 int ch, int dir, int idx, unsigned int ofs, | 
 | 2045 | 		 unsigned int val) | 
 | 2046 | { | 
| Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2047 | 	unsigned int maxval; | 
 | 2048 |  | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2049 | 	if (val > 0) | 
 | 2050 | 		val += ofs; | 
| Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2051 | 	/* ofs = 0: raw max value */ | 
 | 2052 | 	maxval = get_amp_max_value(codec, nid, dir, 0); | 
| Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2053 | 	if (val > maxval) | 
 | 2054 | 		val = maxval; | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2055 | 	return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, | 
 | 2056 | 					HDA_AMP_VOLMASK, val); | 
 | 2057 | } | 
 | 2058 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2059 | /** | 
 | 2060 |  * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume | 
 | 2061 |  * | 
 | 2062 |  * The control element is supposed to have the private_value field | 
 | 2063 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2064 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2065 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, | 
 | 2066 | 				 struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | { | 
 | 2068 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2069 | 	hda_nid_t nid = get_amp_nid(kcontrol); | 
 | 2070 | 	int chs = get_amp_channels(kcontrol); | 
 | 2071 | 	int dir = get_amp_direction(kcontrol); | 
 | 2072 | 	int idx = get_amp_index(kcontrol); | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2073 | 	unsigned int ofs = get_amp_offset(kcontrol); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | 	long *valp = ucontrol->value.integer.value; | 
 | 2075 |  | 
 | 2076 | 	if (chs & 1) | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2077 | 		*valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | 	if (chs & 2) | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2079 | 		*valp = read_amp_value(codec, nid, 1, dir, idx, ofs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | 	return 0; | 
 | 2081 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2082 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2084 | /** | 
 | 2085 |  * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume | 
 | 2086 |  * | 
 | 2087 |  * The control element is supposed to have the private_value field | 
 | 2088 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2089 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2090 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, | 
 | 2091 | 				 struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | { | 
 | 2093 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2094 | 	hda_nid_t nid = get_amp_nid(kcontrol); | 
 | 2095 | 	int chs = get_amp_channels(kcontrol); | 
 | 2096 | 	int dir = get_amp_direction(kcontrol); | 
 | 2097 | 	int idx = get_amp_index(kcontrol); | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2098 | 	unsigned int ofs = get_amp_offset(kcontrol); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | 	long *valp = ucontrol->value.integer.value; | 
 | 2100 | 	int change = 0; | 
 | 2101 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2102 | 	snd_hda_power_up(codec); | 
| Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2103 | 	if (chs & 1) { | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2104 | 		change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); | 
| Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2105 | 		valp++; | 
 | 2106 | 	} | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2107 | 	if (chs & 2) | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2108 | 		change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2109 | 	snd_hda_power_down(codec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | 	return change; | 
 | 2111 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2112 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2114 | /** | 
 | 2115 |  * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume | 
 | 2116 |  * | 
 | 2117 |  * The control element is supposed to have the private_value field | 
 | 2118 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2119 |  */ | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2120 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, | 
 | 2121 | 			  unsigned int size, unsigned int __user *_tlv) | 
 | 2122 | { | 
 | 2123 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2124 | 	hda_nid_t nid = get_amp_nid(kcontrol); | 
 | 2125 | 	int dir = get_amp_direction(kcontrol); | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2126 | 	unsigned int ofs = get_amp_offset(kcontrol); | 
| Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2127 | 	bool min_mute = get_amp_min_mute(kcontrol); | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2128 | 	u32 caps, val1, val2; | 
 | 2129 |  | 
 | 2130 | 	if (size < 4 * sizeof(unsigned int)) | 
 | 2131 | 		return -ENOMEM; | 
 | 2132 | 	caps = query_amp_caps(codec, nid, dir); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2133 | 	val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; | 
 | 2134 | 	val2 = (val2 + 1) * 25; | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2135 | 	val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); | 
| Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2136 | 	val1 += ofs; | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2137 | 	val1 = ((int)val1) * ((int)val2); | 
| Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2138 | 	if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) | 
| Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2139 | 		val2 |= TLV_DB_SCALE_MUTE; | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2140 | 	if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) | 
 | 2141 | 		return -EFAULT; | 
 | 2142 | 	if (put_user(2 * sizeof(unsigned int), _tlv + 1)) | 
 | 2143 | 		return -EFAULT; | 
 | 2144 | 	if (put_user(val1, _tlv + 2)) | 
 | 2145 | 		return -EFAULT; | 
 | 2146 | 	if (put_user(val2, _tlv + 3)) | 
 | 2147 | 		return -EFAULT; | 
 | 2148 | 	return 0; | 
 | 2149 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2150 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); | 
| Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2151 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2152 | /** | 
 | 2153 |  * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control | 
 | 2154 |  * @codec: HD-audio codec | 
 | 2155 |  * @nid: NID of a reference widget | 
 | 2156 |  * @dir: #HDA_INPUT or #HDA_OUTPUT | 
 | 2157 |  * @tlv: TLV data to be stored, at least 4 elements | 
 | 2158 |  * | 
 | 2159 |  * Set (static) TLV data for a virtual master volume using the AMP caps | 
 | 2160 |  * obtained from the reference NID. | 
 | 2161 |  * The volume range is recalculated as if the max volume is 0dB. | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2162 |  */ | 
 | 2163 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, | 
 | 2164 | 			     unsigned int *tlv) | 
 | 2165 | { | 
 | 2166 | 	u32 caps; | 
 | 2167 | 	int nums, step; | 
 | 2168 |  | 
 | 2169 | 	caps = query_amp_caps(codec, nid, dir); | 
 | 2170 | 	nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; | 
 | 2171 | 	step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; | 
 | 2172 | 	step = (step + 1) * 25; | 
 | 2173 | 	tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; | 
 | 2174 | 	tlv[1] = 2 * sizeof(unsigned int); | 
 | 2175 | 	tlv[2] = -nums * step; | 
 | 2176 | 	tlv[3] = step; | 
 | 2177 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2178 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2179 |  | 
 | 2180 | /* find a mixer control element with the given name */ | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2181 | static struct snd_kcontrol * | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2182 | find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2183 | { | 
 | 2184 | 	struct snd_ctl_elem_id id; | 
 | 2185 | 	memset(&id, 0, sizeof(id)); | 
 | 2186 | 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2187 | 	id.device = dev; | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2188 | 	id.index = idx; | 
| Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2189 | 	if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) | 
 | 2190 | 		return NULL; | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2191 | 	strcpy(id.name, name); | 
 | 2192 | 	return snd_ctl_find_id(codec->bus->card, &id); | 
 | 2193 | } | 
 | 2194 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2195 | /** | 
 | 2196 |  * snd_hda_find_mixer_ctl - Find a mixer control element with the given name | 
 | 2197 |  * @codec: HD-audio codec | 
 | 2198 |  * @name: ctl id name string | 
 | 2199 |  * | 
 | 2200 |  * Get the control element with the given id string and IFACE_MIXER. | 
 | 2201 |  */ | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2202 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, | 
 | 2203 | 					    const char *name) | 
 | 2204 | { | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2205 | 	return find_mixer_ctl(codec, name, 0, 0); | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2206 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2207 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2208 |  | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2209 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, | 
 | 2210 | 				    int dev) | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2211 | { | 
 | 2212 | 	int idx; | 
 | 2213 | 	for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */ | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2214 | 		if (!find_mixer_ctl(codec, name, dev, idx)) | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2215 | 			return idx; | 
 | 2216 | 	} | 
 | 2217 | 	return -EBUSY; | 
 | 2218 | } | 
 | 2219 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2220 | /** | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2221 |  * snd_hda_ctl_add - Add a control element and assign to the codec | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2222 |  * @codec: HD-audio codec | 
 | 2223 |  * @nid: corresponding NID (optional) | 
 | 2224 |  * @kctl: the control element to assign | 
 | 2225 |  * | 
 | 2226 |  * Add the given control element to an array inside the codec instance. | 
 | 2227 |  * All control elements belonging to a codec are supposed to be added | 
 | 2228 |  * by this function so that a proper clean-up works at the free or | 
 | 2229 |  * reconfiguration time. | 
 | 2230 |  * | 
 | 2231 |  * If non-zero @nid is passed, the NID is assigned to the control element. | 
 | 2232 |  * The assignment is shown in the codec proc file. | 
 | 2233 |  * | 
 | 2234 |  * snd_hda_ctl_add() checks the control subdev id field whether | 
 | 2235 |  * #HDA_SUBDEV_NID_FLAG bit is set.  If set (and @nid is zero), the lower | 
| Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2236 |  * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit | 
 | 2237 |  * specifies if kctl->private_value is a HDA amplifier value. | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2238 |  */ | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2239 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, | 
 | 2240 | 		    struct snd_kcontrol *kctl) | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2241 | { | 
 | 2242 | 	int err; | 
| Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2243 | 	unsigned short flags = 0; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2244 | 	struct hda_nid_item *item; | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2245 |  | 
| Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2246 | 	if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { | 
| Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2247 | 		flags |= HDA_NID_ITEM_AMP; | 
| Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2248 | 		if (nid == 0) | 
 | 2249 | 			nid = get_amp_nid_(kctl->private_value); | 
 | 2250 | 	} | 
| Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2251 | 	if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) | 
 | 2252 | 		nid = kctl->id.subdevice & 0xffff; | 
| Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2253 | 	if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) | 
| Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2254 | 		kctl->id.subdevice = 0; | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2255 | 	err = snd_ctl_add(codec->bus->card, kctl); | 
 | 2256 | 	if (err < 0) | 
 | 2257 | 		return err; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2258 | 	item = snd_array_new(&codec->mixers); | 
 | 2259 | 	if (!item) | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2260 | 		return -ENOMEM; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2261 | 	item->kctl = kctl; | 
 | 2262 | 	item->nid = nid; | 
| Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2263 | 	item->flags = flags; | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2264 | 	return 0; | 
 | 2265 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2266 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2267 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2268 | /** | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2269 |  * snd_hda_add_nid - Assign a NID to a control element | 
 | 2270 |  * @codec: HD-audio codec | 
 | 2271 |  * @nid: corresponding NID (optional) | 
 | 2272 |  * @kctl: the control element to assign | 
 | 2273 |  * @index: index to kctl | 
 | 2274 |  * | 
 | 2275 |  * Add the given control element to an array inside the codec instance. | 
 | 2276 |  * This function is used when #snd_hda_ctl_add cannot be used for 1:1 | 
 | 2277 |  * NID:KCTL mapping - for example "Capture Source" selector. | 
 | 2278 |  */ | 
 | 2279 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, | 
 | 2280 | 		    unsigned int index, hda_nid_t nid) | 
 | 2281 | { | 
 | 2282 | 	struct hda_nid_item *item; | 
 | 2283 |  | 
 | 2284 | 	if (nid > 0) { | 
 | 2285 | 		item = snd_array_new(&codec->nids); | 
 | 2286 | 		if (!item) | 
 | 2287 | 			return -ENOMEM; | 
 | 2288 | 		item->kctl = kctl; | 
 | 2289 | 		item->index = index; | 
 | 2290 | 		item->nid = nid; | 
 | 2291 | 		return 0; | 
 | 2292 | 	} | 
| Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2293 | 	printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", | 
 | 2294 | 	       kctl->id.name, kctl->id.index, index); | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2295 | 	return -EINVAL; | 
 | 2296 | } | 
 | 2297 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); | 
 | 2298 |  | 
 | 2299 | /** | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2300 |  * snd_hda_ctls_clear - Clear all controls assigned to the given codec | 
 | 2301 |  * @codec: HD-audio codec | 
 | 2302 |  */ | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2303 | void snd_hda_ctls_clear(struct hda_codec *codec) | 
 | 2304 | { | 
 | 2305 | 	int i; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2306 | 	struct hda_nid_item *items = codec->mixers.list; | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2307 | 	for (i = 0; i < codec->mixers.used; i++) | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2308 | 		snd_ctl_remove(codec->bus->card, items[i].kctl); | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2309 | 	snd_array_free(&codec->mixers); | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2310 | 	snd_array_free(&codec->nids); | 
| Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2311 | } | 
 | 2312 |  | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2313 | /* pseudo device locking | 
 | 2314 |  * toggle card->shutdown to allow/disallow the device access (as a hack) | 
 | 2315 |  */ | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2316 | int snd_hda_lock_devices(struct hda_bus *bus) | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2317 | { | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2318 | 	struct snd_card *card = bus->card; | 
 | 2319 | 	struct hda_codec *codec; | 
 | 2320 |  | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2321 | 	spin_lock(&card->files_lock); | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2322 | 	if (card->shutdown) | 
 | 2323 | 		goto err_unlock; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2324 | 	card->shutdown = 1; | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2325 | 	if (!list_empty(&card->ctl_files)) | 
 | 2326 | 		goto err_clear; | 
 | 2327 |  | 
 | 2328 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
 | 2329 | 		int pcm; | 
 | 2330 | 		for (pcm = 0; pcm < codec->num_pcms; pcm++) { | 
 | 2331 | 			struct hda_pcm *cpcm = &codec->pcm_info[pcm]; | 
 | 2332 | 			if (!cpcm->pcm) | 
 | 2333 | 				continue; | 
 | 2334 | 			if (cpcm->pcm->streams[0].substream_opened || | 
 | 2335 | 			    cpcm->pcm->streams[1].substream_opened) | 
 | 2336 | 				goto err_clear; | 
 | 2337 | 		} | 
 | 2338 | 	} | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2339 | 	spin_unlock(&card->files_lock); | 
 | 2340 | 	return 0; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2341 |  | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2342 |  err_clear: | 
 | 2343 | 	card->shutdown = 0; | 
 | 2344 |  err_unlock: | 
 | 2345 | 	spin_unlock(&card->files_lock); | 
 | 2346 | 	return -EINVAL; | 
 | 2347 | } | 
 | 2348 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); | 
 | 2349 |  | 
 | 2350 | void snd_hda_unlock_devices(struct hda_bus *bus) | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2351 | { | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2352 | 	struct snd_card *card = bus->card; | 
 | 2353 |  | 
 | 2354 | 	card = bus->card; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2355 | 	spin_lock(&card->files_lock); | 
 | 2356 | 	card->shutdown = 0; | 
 | 2357 | 	spin_unlock(&card->files_lock); | 
 | 2358 | } | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2359 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2360 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2361 | /** | 
 | 2362 |  * snd_hda_codec_reset - Clear all objects assigned to the codec | 
 | 2363 |  * @codec: HD-audio codec | 
 | 2364 |  * | 
 | 2365 |  * This frees the all PCM and control elements assigned to the codec, and | 
 | 2366 |  * clears the caches and restores the pin default configurations. | 
 | 2367 |  * | 
 | 2368 |  * When a device is being used, it returns -EBSY.  If successfully freed, | 
 | 2369 |  * returns zero. | 
 | 2370 |  */ | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2371 | int snd_hda_codec_reset(struct hda_codec *codec) | 
 | 2372 | { | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2373 | 	struct hda_bus *bus = codec->bus; | 
 | 2374 | 	struct snd_card *card = bus->card; | 
 | 2375 | 	int i; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2376 |  | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2377 | 	if (snd_hda_lock_devices(bus) < 0) | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2378 | 		return -EBUSY; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2379 |  | 
 | 2380 | 	/* OK, let it free */ | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2381 | 	cancel_delayed_work_sync(&codec->jackpoll_work); | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2382 | #ifdef CONFIG_PM | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2383 | 	cancel_delayed_work_sync(&codec->power_work); | 
| Takashi Iwai | 339876d | 2012-05-08 16:57:12 +0200 | [diff] [blame] | 2384 | 	codec->power_on = 0; | 
 | 2385 | 	codec->power_transition = 0; | 
 | 2386 | 	codec->power_jiffies = jiffies; | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2387 | 	flush_workqueue(bus->workq); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2388 | #endif | 
 | 2389 | 	snd_hda_ctls_clear(codec); | 
 | 2390 | 	/* relase PCMs */ | 
 | 2391 | 	for (i = 0; i < codec->num_pcms; i++) { | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2392 | 		if (codec->pcm_info[i].pcm) { | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2393 | 			snd_device_free(card, codec->pcm_info[i].pcm); | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2394 | 			clear_bit(codec->pcm_info[i].device, | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2395 | 				  bus->pcm_dev_bits); | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2396 | 		} | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2397 | 	} | 
 | 2398 | 	if (codec->patch_ops.free) | 
 | 2399 | 		codec->patch_ops.free(codec); | 
| Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2400 | 	memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); | 
| Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2401 | 	snd_hda_jack_tbl_clear(codec); | 
| Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2402 | 	codec->proc_widget_hook = NULL; | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2403 | 	codec->spec = NULL; | 
 | 2404 | 	free_hda_cache(&codec->amp_cache); | 
 | 2405 | 	free_hda_cache(&codec->cmd_cache); | 
| Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2406 | 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); | 
 | 2407 | 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); | 
| Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2408 | 	/* free only driver_pins so that init_pins + user_pins are restored */ | 
 | 2409 | 	snd_array_free(&codec->driver_pins); | 
| Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2410 | 	snd_array_free(&codec->cvt_setups); | 
 | 2411 | 	snd_array_free(&codec->spdif_out); | 
| Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2412 | 	snd_array_free(&codec->verbs); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2413 | 	codec->num_pcms = 0; | 
 | 2414 | 	codec->pcm_info = NULL; | 
 | 2415 | 	codec->preset = NULL; | 
| Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2416 | 	codec->slave_dig_outs = NULL; | 
 | 2417 | 	codec->spdif_status_reset = 0; | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2418 | 	module_put(codec->owner); | 
 | 2419 | 	codec->owner = NULL; | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2420 |  | 
 | 2421 | 	/* allow device access again */ | 
| Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2422 | 	snd_hda_unlock_devices(bus); | 
| Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2423 | 	return 0; | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2424 | } | 
 | 2425 |  | 
| Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2426 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); | 
 | 2427 |  | 
 | 2428 | /* apply the function to all matching slave ctls in the mixer list */ | 
 | 2429 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2430 | 		      const char *suffix, map_slave_func_t func, void *data)  | 
| Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2431 | { | 
 | 2432 | 	struct hda_nid_item *items; | 
 | 2433 | 	const char * const *s; | 
 | 2434 | 	int i, err; | 
 | 2435 |  | 
 | 2436 | 	items = codec->mixers.list; | 
 | 2437 | 	for (i = 0; i < codec->mixers.used; i++) { | 
 | 2438 | 		struct snd_kcontrol *sctl = items[i].kctl; | 
 | 2439 | 		if (!sctl || !sctl->id.name || | 
 | 2440 | 		    sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) | 
 | 2441 | 			continue; | 
 | 2442 | 		for (s = slaves; *s; s++) { | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2443 | 			char tmpname[sizeof(sctl->id.name)]; | 
 | 2444 | 			const char *name = *s; | 
 | 2445 | 			if (suffix) { | 
 | 2446 | 				snprintf(tmpname, sizeof(tmpname), "%s %s", | 
 | 2447 | 					 name, suffix); | 
 | 2448 | 				name = tmpname; | 
 | 2449 | 			} | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2450 | 			if (!strcmp(sctl->id.name, name)) { | 
| Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2451 | 				err = func(data, sctl); | 
 | 2452 | 				if (err) | 
 | 2453 | 					return err; | 
 | 2454 | 				break; | 
 | 2455 | 			} | 
 | 2456 | 		} | 
 | 2457 | 	} | 
 | 2458 | 	return 0; | 
 | 2459 | } | 
 | 2460 |  | 
 | 2461 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) | 
 | 2462 | { | 
 | 2463 | 	return 1; | 
 | 2464 | } | 
 | 2465 |  | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2466 | /* guess the value corresponding to 0dB */ | 
 | 2467 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl) | 
 | 2468 | { | 
 | 2469 | 	int _tlv[4]; | 
 | 2470 | 	const int *tlv = NULL; | 
 | 2471 | 	int val = -1; | 
 | 2472 |  | 
 | 2473 | 	if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { | 
 | 2474 | 		/* FIXME: set_fs() hack for obtaining user-space TLV data */ | 
 | 2475 | 		mm_segment_t fs = get_fs(); | 
 | 2476 | 		set_fs(get_ds()); | 
 | 2477 | 		if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) | 
 | 2478 | 			tlv = _tlv; | 
 | 2479 | 		set_fs(fs); | 
 | 2480 | 	} else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) | 
 | 2481 | 		tlv = kctl->tlv.p; | 
 | 2482 | 	if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) | 
 | 2483 | 		val = -tlv[2] / tlv[3]; | 
 | 2484 | 	return val; | 
 | 2485 | } | 
 | 2486 |  | 
 | 2487 | /* call kctl->put with the given value(s) */ | 
 | 2488 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) | 
 | 2489 | { | 
 | 2490 | 	struct snd_ctl_elem_value *ucontrol; | 
 | 2491 | 	ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); | 
 | 2492 | 	if (!ucontrol) | 
 | 2493 | 		return -ENOMEM; | 
 | 2494 | 	ucontrol->value.integer.value[0] = val; | 
 | 2495 | 	ucontrol->value.integer.value[1] = val; | 
 | 2496 | 	kctl->put(kctl, ucontrol); | 
 | 2497 | 	kfree(ucontrol); | 
 | 2498 | 	return 0; | 
 | 2499 | } | 
 | 2500 |  | 
 | 2501 | /* initialize the slave volume with 0dB */ | 
 | 2502 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) | 
 | 2503 | { | 
 | 2504 | 	int offset = get_kctl_0dB_offset(slave); | 
 | 2505 | 	if (offset > 0) | 
 | 2506 | 		put_kctl_with_value(slave, offset); | 
 | 2507 | 	return 0; | 
 | 2508 | } | 
 | 2509 |  | 
 | 2510 | /* unmute the slave */ | 
 | 2511 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) | 
 | 2512 | { | 
 | 2513 | 	return put_kctl_with_value(slave, 1); | 
 | 2514 | } | 
 | 2515 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2516 | /** | 
 | 2517 |  * snd_hda_add_vmaster - create a virtual master control and add slaves | 
 | 2518 |  * @codec: HD-audio codec | 
 | 2519 |  * @name: vmaster control name | 
 | 2520 |  * @tlv: TLV data (optional) | 
 | 2521 |  * @slaves: slave control names (optional) | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2522 |  * @suffix: suffix string to each slave name (optional) | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2523 |  * @init_slave_vol: initialize slaves to unmute/0dB | 
| Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2524 |  * @ctl_ret: store the vmaster kcontrol in return | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2525 |  * | 
 | 2526 |  * Create a virtual master control with the given name.  The TLV data | 
 | 2527 |  * must be either NULL or a valid data. | 
 | 2528 |  * | 
 | 2529 |  * @slaves is a NULL-terminated array of strings, each of which is a | 
 | 2530 |  * slave control name.  All controls with these names are assigned to | 
 | 2531 |  * the new virtual master control. | 
 | 2532 |  * | 
 | 2533 |  * This function returns zero if successful or a negative error code. | 
 | 2534 |  */ | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2535 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2536 | 			unsigned int *tlv, const char * const *slaves, | 
| Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2537 | 			  const char *suffix, bool init_slave_vol, | 
 | 2538 | 			  struct snd_kcontrol **ctl_ret) | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2539 | { | 
 | 2540 | 	struct snd_kcontrol *kctl; | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2541 | 	int err; | 
 | 2542 |  | 
| Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2543 | 	if (ctl_ret) | 
 | 2544 | 		*ctl_ret = NULL; | 
 | 2545 |  | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2546 | 	err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); | 
| Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2547 | 	if (err != 1) { | 
| Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2548 | 		snd_printdd("No slave found for %s\n", name); | 
 | 2549 | 		return 0; | 
 | 2550 | 	} | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2551 | 	kctl = snd_ctl_make_virtual_master(name, tlv); | 
 | 2552 | 	if (!kctl) | 
 | 2553 | 		return -ENOMEM; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2554 | 	err = snd_hda_ctl_add(codec, 0, kctl); | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2555 | 	if (err < 0) | 
 | 2556 | 		return err; | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2557 |  | 
| Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2558 | 	err = map_slaves(codec, slaves, suffix, | 
 | 2559 | 			 (map_slave_func_t)snd_ctl_add_slave, kctl); | 
| Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2560 | 	if (err < 0) | 
 | 2561 | 		return err; | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2562 |  | 
 | 2563 | 	/* init with master mute & zero volume */ | 
 | 2564 | 	put_kctl_with_value(kctl, 0); | 
 | 2565 | 	if (init_slave_vol) | 
 | 2566 | 		map_slaves(codec, slaves, suffix, | 
 | 2567 | 			   tlv ? init_slave_0dB : init_slave_unmute, kctl); | 
 | 2568 |  | 
| Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2569 | 	if (ctl_ret) | 
 | 2570 | 		*ctl_ret = kctl; | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2571 | 	return 0; | 
 | 2572 | } | 
| Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2573 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); | 
| Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2574 |  | 
| Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2575 | /* | 
 | 2576 |  * mute-LED control using vmaster | 
 | 2577 |  */ | 
 | 2578 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, | 
 | 2579 | 				  struct snd_ctl_elem_info *uinfo) | 
 | 2580 | { | 
 | 2581 | 	static const char * const texts[] = { | 
| David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2582 | 		"On", "Off", "Follow Master" | 
| Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2583 | 	}; | 
 | 2584 | 	unsigned int index; | 
 | 2585 |  | 
 | 2586 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | 
 | 2587 | 	uinfo->count = 1; | 
 | 2588 | 	uinfo->value.enumerated.items = 3; | 
 | 2589 | 	index = uinfo->value.enumerated.item; | 
 | 2590 | 	if (index >= 3) | 
 | 2591 | 		index = 2; | 
 | 2592 | 	strcpy(uinfo->value.enumerated.name, texts[index]); | 
 | 2593 | 	return 0; | 
 | 2594 | } | 
 | 2595 |  | 
 | 2596 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, | 
 | 2597 | 				 struct snd_ctl_elem_value *ucontrol) | 
 | 2598 | { | 
 | 2599 | 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); | 
 | 2600 | 	ucontrol->value.enumerated.item[0] = hook->mute_mode; | 
 | 2601 | 	return 0; | 
 | 2602 | } | 
 | 2603 |  | 
 | 2604 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, | 
 | 2605 | 				 struct snd_ctl_elem_value *ucontrol) | 
 | 2606 | { | 
 | 2607 | 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); | 
 | 2608 | 	unsigned int old_mode = hook->mute_mode; | 
 | 2609 |  | 
 | 2610 | 	hook->mute_mode = ucontrol->value.enumerated.item[0]; | 
 | 2611 | 	if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) | 
 | 2612 | 		hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; | 
 | 2613 | 	if (old_mode == hook->mute_mode) | 
 | 2614 | 		return 0; | 
 | 2615 | 	snd_hda_sync_vmaster_hook(hook); | 
 | 2616 | 	return 1; | 
 | 2617 | } | 
 | 2618 |  | 
 | 2619 | static struct snd_kcontrol_new vmaster_mute_mode = { | 
 | 2620 | 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
 | 2621 | 	.name = "Mute-LED Mode", | 
 | 2622 | 	.info = vmaster_mute_mode_info, | 
 | 2623 | 	.get = vmaster_mute_mode_get, | 
 | 2624 | 	.put = vmaster_mute_mode_put, | 
 | 2625 | }; | 
 | 2626 |  | 
 | 2627 | /* | 
 | 2628 |  * Add a mute-LED hook with the given vmaster switch kctl | 
 | 2629 |  * "Mute-LED Mode" control is automatically created and associated with | 
 | 2630 |  * the given hook. | 
 | 2631 |  */ | 
 | 2632 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, | 
| Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2633 | 			     struct hda_vmaster_mute_hook *hook, | 
 | 2634 | 			     bool expose_enum_ctl) | 
| Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2635 | { | 
 | 2636 | 	struct snd_kcontrol *kctl; | 
 | 2637 |  | 
 | 2638 | 	if (!hook->hook || !hook->sw_kctl) | 
 | 2639 | 		return 0; | 
 | 2640 | 	snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); | 
 | 2641 | 	hook->codec = codec; | 
 | 2642 | 	hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; | 
| Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2643 | 	if (!expose_enum_ctl) | 
 | 2644 | 		return 0; | 
| Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2645 | 	kctl = snd_ctl_new1(&vmaster_mute_mode, hook); | 
 | 2646 | 	if (!kctl) | 
 | 2647 | 		return -ENOMEM; | 
 | 2648 | 	return snd_hda_ctl_add(codec, 0, kctl); | 
 | 2649 | } | 
 | 2650 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); | 
 | 2651 |  | 
 | 2652 | /* | 
 | 2653 |  * Call the hook with the current value for synchronization | 
 | 2654 |  * Should be called in init callback | 
 | 2655 |  */ | 
 | 2656 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) | 
 | 2657 | { | 
 | 2658 | 	if (!hook->hook || !hook->codec) | 
 | 2659 | 		return; | 
 | 2660 | 	switch (hook->mute_mode) { | 
 | 2661 | 	case HDA_VMUTE_FOLLOW_MASTER: | 
 | 2662 | 		snd_ctl_sync_vmaster_hook(hook->sw_kctl); | 
 | 2663 | 		break; | 
 | 2664 | 	default: | 
 | 2665 | 		hook->hook(hook->codec, hook->mute_mode); | 
 | 2666 | 		break; | 
 | 2667 | 	} | 
 | 2668 | } | 
 | 2669 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); | 
 | 2670 |  | 
 | 2671 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2672 | /** | 
 | 2673 |  * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch | 
 | 2674 |  * | 
 | 2675 |  * The control element is supposed to have the private_value field | 
 | 2676 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2677 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2678 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, | 
 | 2679 | 				  struct snd_ctl_elem_info *uinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | { | 
 | 2681 | 	int chs = get_amp_channels(kcontrol); | 
 | 2682 |  | 
 | 2683 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | 
 | 2684 | 	uinfo->count = chs == 3 ? 2 : 1; | 
 | 2685 | 	uinfo->value.integer.min = 0; | 
 | 2686 | 	uinfo->value.integer.max = 1; | 
 | 2687 | 	return 0; | 
 | 2688 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2689 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2691 | /** | 
 | 2692 |  * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch | 
 | 2693 |  * | 
 | 2694 |  * The control element is supposed to have the private_value field | 
 | 2695 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2696 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2697 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, | 
 | 2698 | 				 struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | { | 
 | 2700 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2701 | 	hda_nid_t nid = get_amp_nid(kcontrol); | 
 | 2702 | 	int chs = get_amp_channels(kcontrol); | 
 | 2703 | 	int dir = get_amp_direction(kcontrol); | 
 | 2704 | 	int idx = get_amp_index(kcontrol); | 
 | 2705 | 	long *valp = ucontrol->value.integer.value; | 
 | 2706 |  | 
 | 2707 | 	if (chs & 1) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2708 | 		*valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2709 | 			   HDA_AMP_MUTE) ? 0 : 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | 	if (chs & 2) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2711 | 		*valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2712 | 			 HDA_AMP_MUTE) ? 0 : 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | 	return 0; | 
 | 2714 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2715 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2717 | /** | 
 | 2718 |  * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch | 
 | 2719 |  * | 
 | 2720 |  * The control element is supposed to have the private_value field | 
 | 2721 |  * set up via HDA_COMPOSE_AMP_VAL*() or related macros. | 
 | 2722 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2723 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, | 
 | 2724 | 				 struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | { | 
 | 2726 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2727 | 	hda_nid_t nid = get_amp_nid(kcontrol); | 
 | 2728 | 	int chs = get_amp_channels(kcontrol); | 
 | 2729 | 	int dir = get_amp_direction(kcontrol); | 
 | 2730 | 	int idx = get_amp_index(kcontrol); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | 	long *valp = ucontrol->value.integer.value; | 
 | 2732 | 	int change = 0; | 
 | 2733 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2734 | 	snd_hda_power_up(codec); | 
| Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2735 | 	if (chs & 1) { | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2736 | 		change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2737 | 						  HDA_AMP_MUTE, | 
 | 2738 | 						  *valp ? 0 : HDA_AMP_MUTE); | 
| Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2739 | 		valp++; | 
 | 2740 | 	} | 
| Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2741 | 	if (chs & 2) | 
 | 2742 | 		change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, | 
| Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2743 | 						   HDA_AMP_MUTE, | 
 | 2744 | 						   *valp ? 0 : HDA_AMP_MUTE); | 
| Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2745 | 	hda_call_check_power_status(codec, nid); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2746 | 	snd_hda_power_down(codec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | 	return change; | 
 | 2748 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2749 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 |  | 
 | 2751 | /* | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2752 |  * bound volume controls | 
 | 2753 |  * | 
 | 2754 |  * bind multiple volumes (# indices, from 0) | 
 | 2755 |  */ | 
 | 2756 |  | 
 | 2757 | #define AMP_VAL_IDX_SHIFT	19 | 
 | 2758 | #define AMP_VAL_IDX_MASK	(0x0f<<19) | 
 | 2759 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2760 | /** | 
 | 2761 |  * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control | 
 | 2762 |  * | 
 | 2763 |  * The control element is supposed to have the private_value field | 
 | 2764 |  * set up via HDA_BIND_MUTE*() macros. | 
 | 2765 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2766 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, | 
 | 2767 | 				  struct snd_ctl_elem_value *ucontrol) | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2768 | { | 
 | 2769 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2770 | 	unsigned long pval; | 
 | 2771 | 	int err; | 
 | 2772 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2773 | 	mutex_lock(&codec->control_mutex); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2774 | 	pval = kcontrol->private_value; | 
 | 2775 | 	kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ | 
 | 2776 | 	err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); | 
 | 2777 | 	kcontrol->private_value = pval; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2778 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2779 | 	return err; | 
 | 2780 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2781 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2782 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2783 | /** | 
 | 2784 |  * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control | 
 | 2785 |  * | 
 | 2786 |  * The control element is supposed to have the private_value field | 
 | 2787 |  * set up via HDA_BIND_MUTE*() macros. | 
 | 2788 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2789 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, | 
 | 2790 | 				  struct snd_ctl_elem_value *ucontrol) | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2791 | { | 
 | 2792 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2793 | 	unsigned long pval; | 
 | 2794 | 	int i, indices, err = 0, change = 0; | 
 | 2795 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2796 | 	mutex_lock(&codec->control_mutex); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2797 | 	pval = kcontrol->private_value; | 
 | 2798 | 	indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; | 
 | 2799 | 	for (i = 0; i < indices; i++) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2800 | 		kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | | 
 | 2801 | 			(i << AMP_VAL_IDX_SHIFT); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2802 | 		err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); | 
 | 2803 | 		if (err < 0) | 
 | 2804 | 			break; | 
 | 2805 | 		change |= err; | 
 | 2806 | 	} | 
 | 2807 | 	kcontrol->private_value = pval; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2808 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2809 | 	return err < 0 ? err : change; | 
 | 2810 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2811 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); | 
| Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2812 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2813 | /** | 
 | 2814 |  * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control | 
 | 2815 |  * | 
 | 2816 |  * The control element is supposed to have the private_value field | 
 | 2817 |  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2818 |  */ | 
 | 2819 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, | 
 | 2820 | 				 struct snd_ctl_elem_info *uinfo) | 
 | 2821 | { | 
 | 2822 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2823 | 	struct hda_bind_ctls *c; | 
 | 2824 | 	int err; | 
 | 2825 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2826 | 	mutex_lock(&codec->control_mutex); | 
| Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2827 | 	c = (struct hda_bind_ctls *)kcontrol->private_value; | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2828 | 	kcontrol->private_value = *c->values; | 
 | 2829 | 	err = c->ops->info(kcontrol, uinfo); | 
 | 2830 | 	kcontrol->private_value = (long)c; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2831 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2832 | 	return err; | 
 | 2833 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2834 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2835 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2836 | /** | 
 | 2837 |  * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control | 
 | 2838 |  * | 
 | 2839 |  * The control element is supposed to have the private_value field | 
 | 2840 |  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | 
 | 2841 |  */ | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2842 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, | 
 | 2843 | 				struct snd_ctl_elem_value *ucontrol) | 
 | 2844 | { | 
 | 2845 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2846 | 	struct hda_bind_ctls *c; | 
 | 2847 | 	int err; | 
 | 2848 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2849 | 	mutex_lock(&codec->control_mutex); | 
| Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2850 | 	c = (struct hda_bind_ctls *)kcontrol->private_value; | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2851 | 	kcontrol->private_value = *c->values; | 
 | 2852 | 	err = c->ops->get(kcontrol, ucontrol); | 
 | 2853 | 	kcontrol->private_value = (long)c; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2854 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2855 | 	return err; | 
 | 2856 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2857 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2858 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2859 | /** | 
 | 2860 |  * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control | 
 | 2861 |  * | 
 | 2862 |  * The control element is supposed to have the private_value field | 
 | 2863 |  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. | 
 | 2864 |  */ | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2865 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, | 
 | 2866 | 				struct snd_ctl_elem_value *ucontrol) | 
 | 2867 | { | 
 | 2868 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2869 | 	struct hda_bind_ctls *c; | 
 | 2870 | 	unsigned long *vals; | 
 | 2871 | 	int err = 0, change = 0; | 
 | 2872 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2873 | 	mutex_lock(&codec->control_mutex); | 
| Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2874 | 	c = (struct hda_bind_ctls *)kcontrol->private_value; | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2875 | 	for (vals = c->values; *vals; vals++) { | 
 | 2876 | 		kcontrol->private_value = *vals; | 
 | 2877 | 		err = c->ops->put(kcontrol, ucontrol); | 
 | 2878 | 		if (err < 0) | 
 | 2879 | 			break; | 
 | 2880 | 		change |= err; | 
 | 2881 | 	} | 
 | 2882 | 	kcontrol->private_value = (long)c; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2883 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2884 | 	return err < 0 ? err : change; | 
 | 2885 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2886 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2887 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2888 | /** | 
 | 2889 |  * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control | 
 | 2890 |  * | 
 | 2891 |  * The control element is supposed to have the private_value field | 
 | 2892 |  * set up via HDA_BIND_VOL() macro. | 
 | 2893 |  */ | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2894 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, | 
 | 2895 | 			   unsigned int size, unsigned int __user *tlv) | 
 | 2896 | { | 
 | 2897 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 2898 | 	struct hda_bind_ctls *c; | 
 | 2899 | 	int err; | 
 | 2900 |  | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2901 | 	mutex_lock(&codec->control_mutex); | 
| Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2902 | 	c = (struct hda_bind_ctls *)kcontrol->private_value; | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2903 | 	kcontrol->private_value = *c->values; | 
 | 2904 | 	err = c->ops->tlv(kcontrol, op_flag, size, tlv); | 
 | 2905 | 	kcontrol->private_value = (long)c; | 
| Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2906 | 	mutex_unlock(&codec->control_mutex); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2907 | 	return err; | 
 | 2908 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2909 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2910 |  | 
 | 2911 | struct hda_ctl_ops snd_hda_bind_vol = { | 
 | 2912 | 	.info = snd_hda_mixer_amp_volume_info, | 
 | 2913 | 	.get = snd_hda_mixer_amp_volume_get, | 
 | 2914 | 	.put = snd_hda_mixer_amp_volume_put, | 
 | 2915 | 	.tlv = snd_hda_mixer_amp_tlv | 
 | 2916 | }; | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2917 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2918 |  | 
 | 2919 | struct hda_ctl_ops snd_hda_bind_sw = { | 
 | 2920 | 	.info = snd_hda_mixer_amp_switch_info, | 
 | 2921 | 	.get = snd_hda_mixer_amp_switch_get, | 
 | 2922 | 	.put = snd_hda_mixer_amp_switch_put, | 
 | 2923 | 	.tlv = snd_hda_mixer_amp_tlv | 
 | 2924 | }; | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2925 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); | 
| Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2926 |  | 
 | 2927 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 |  * SPDIF out controls | 
 | 2929 |  */ | 
 | 2930 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2931 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, | 
 | 2932 | 				   struct snd_ctl_elem_info *uinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | { | 
 | 2934 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; | 
 | 2935 | 	uinfo->count = 1; | 
 | 2936 | 	return 0; | 
 | 2937 | } | 
 | 2938 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2939 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, | 
 | 2940 | 				   struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | { | 
 | 2942 | 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | 
 | 2943 | 					   IEC958_AES0_NONAUDIO | | 
 | 2944 | 					   IEC958_AES0_CON_EMPHASIS_5015 | | 
 | 2945 | 					   IEC958_AES0_CON_NOT_COPYRIGHT; | 
 | 2946 | 	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | | 
 | 2947 | 					   IEC958_AES1_CON_ORIGINAL; | 
 | 2948 | 	return 0; | 
 | 2949 | } | 
 | 2950 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2951 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, | 
 | 2952 | 				   struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | { | 
 | 2954 | 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | | 
 | 2955 | 					   IEC958_AES0_NONAUDIO | | 
 | 2956 | 					   IEC958_AES0_PRO_EMPHASIS_5015; | 
 | 2957 | 	return 0; | 
 | 2958 | } | 
 | 2959 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2960 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, | 
 | 2961 | 				     struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | { | 
 | 2963 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2964 | 	int idx = kcontrol->private_value; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 2965 | 	struct hda_spdif_out *spdif; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 |  | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 2967 | 	mutex_lock(&codec->spdif_mutex); | 
 | 2968 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2969 | 	ucontrol->value.iec958.status[0] = spdif->status & 0xff; | 
 | 2970 | 	ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; | 
 | 2971 | 	ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; | 
 | 2972 | 	ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 2973 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 |  | 
 | 2975 | 	return 0; | 
 | 2976 | } | 
 | 2977 |  | 
 | 2978 | /* convert from SPDIF status bits to HDA SPDIF bits | 
 | 2979 |  * bit 0 (DigEn) is always set zero (to be filled later) | 
 | 2980 |  */ | 
 | 2981 | static unsigned short convert_from_spdif_status(unsigned int sbits) | 
 | 2982 | { | 
 | 2983 | 	unsigned short val = 0; | 
 | 2984 |  | 
 | 2985 | 	if (sbits & IEC958_AES0_PROFESSIONAL) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2986 | 		val |= AC_DIG1_PROFESSIONAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | 	if (sbits & IEC958_AES0_NONAUDIO) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2988 | 		val |= AC_DIG1_NONAUDIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | 	if (sbits & IEC958_AES0_PROFESSIONAL) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2990 | 		if ((sbits & IEC958_AES0_PRO_EMPHASIS) == | 
 | 2991 | 		    IEC958_AES0_PRO_EMPHASIS_5015) | 
 | 2992 | 			val |= AC_DIG1_EMPHASIS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | 	} else { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2994 | 		if ((sbits & IEC958_AES0_CON_EMPHASIS) == | 
 | 2995 | 		    IEC958_AES0_CON_EMPHASIS_5015) | 
 | 2996 | 			val |= AC_DIG1_EMPHASIS; | 
 | 2997 | 		if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) | 
 | 2998 | 			val |= AC_DIG1_COPYRIGHT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | 		if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3000 | 			val |= AC_DIG1_LEVEL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | 		val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); | 
 | 3002 | 	} | 
 | 3003 | 	return val; | 
 | 3004 | } | 
 | 3005 |  | 
 | 3006 | /* convert to SPDIF status bits from HDA SPDIF bits | 
 | 3007 |  */ | 
 | 3008 | static unsigned int convert_to_spdif_status(unsigned short val) | 
 | 3009 | { | 
 | 3010 | 	unsigned int sbits = 0; | 
 | 3011 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3012 | 	if (val & AC_DIG1_NONAUDIO) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | 		sbits |= IEC958_AES0_NONAUDIO; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3014 | 	if (val & AC_DIG1_PROFESSIONAL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | 		sbits |= IEC958_AES0_PROFESSIONAL; | 
 | 3016 | 	if (sbits & IEC958_AES0_PROFESSIONAL) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3017 | 		if (sbits & AC_DIG1_EMPHASIS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | 			sbits |= IEC958_AES0_PRO_EMPHASIS_5015; | 
 | 3019 | 	} else { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3020 | 		if (val & AC_DIG1_EMPHASIS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | 			sbits |= IEC958_AES0_CON_EMPHASIS_5015; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3022 | 		if (!(val & AC_DIG1_COPYRIGHT)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | 			sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3024 | 		if (val & AC_DIG1_LEVEL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | 			sbits |= (IEC958_AES1_CON_ORIGINAL << 8); | 
 | 3026 | 		sbits |= val & (0x7f << 8); | 
 | 3027 | 	} | 
 | 3028 | 	return sbits; | 
 | 3029 | } | 
 | 3030 |  | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3031 | /* set digital convert verbs both for the given NID and its slaves */ | 
 | 3032 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, | 
 | 3033 | 			int verb, int val) | 
 | 3034 | { | 
| Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3035 | 	const hda_nid_t *d; | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3036 |  | 
| Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3037 | 	snd_hda_codec_write_cache(codec, nid, 0, verb, val); | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3038 | 	d = codec->slave_dig_outs; | 
 | 3039 | 	if (!d) | 
 | 3040 | 		return; | 
 | 3041 | 	for (; *d; d++) | 
| Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3042 | 		snd_hda_codec_write_cache(codec, *d, 0, verb, val); | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3043 | } | 
 | 3044 |  | 
 | 3045 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, | 
 | 3046 | 				       int dig1, int dig2) | 
 | 3047 | { | 
 | 3048 | 	if (dig1 != -1) | 
 | 3049 | 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); | 
 | 3050 | 	if (dig2 != -1) | 
 | 3051 | 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); | 
 | 3052 | } | 
 | 3053 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3054 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, | 
 | 3055 | 				     struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | { | 
 | 3057 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3058 | 	int idx = kcontrol->private_value; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3059 | 	struct hda_spdif_out *spdif; | 
 | 3060 | 	hda_nid_t nid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | 	unsigned short val; | 
 | 3062 | 	int change; | 
 | 3063 |  | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3064 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3065 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
 | 3066 | 	nid = spdif->nid; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3067 | 	spdif->status = ucontrol->value.iec958.status[0] | | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | 		((unsigned int)ucontrol->value.iec958.status[1] << 8) | | 
 | 3069 | 		((unsigned int)ucontrol->value.iec958.status[2] << 16) | | 
 | 3070 | 		((unsigned int)ucontrol->value.iec958.status[3] << 24); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3071 | 	val = convert_from_spdif_status(spdif->status); | 
 | 3072 | 	val |= spdif->ctls & 1; | 
 | 3073 | 	change = spdif->ctls != val; | 
 | 3074 | 	spdif->ctls = val; | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3075 | 	if (change && nid != (u16)-1) | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3076 | 		set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3077 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | 	return change; | 
 | 3079 | } | 
 | 3080 |  | 
| Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3081 | #define snd_hda_spdif_out_switch_info	snd_ctl_boolean_mono_info | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, | 
 | 3084 | 					struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | { | 
 | 3086 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3087 | 	int idx = kcontrol->private_value; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3088 | 	struct hda_spdif_out *spdif; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 |  | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3090 | 	mutex_lock(&codec->spdif_mutex); | 
 | 3091 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3092 | 	ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3093 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | 	return 0; | 
 | 3095 | } | 
 | 3096 |  | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3097 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, | 
 | 3098 | 				  int dig1, int dig2) | 
 | 3099 | { | 
 | 3100 | 	set_dig_out_convert(codec, nid, dig1, dig2); | 
 | 3101 | 	/* unmute amp switch (if any) */ | 
 | 3102 | 	if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && | 
 | 3103 | 	    (dig1 & AC_DIG1_ENABLE)) | 
 | 3104 | 		snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, | 
 | 3105 | 					    HDA_AMP_MUTE, 0); | 
 | 3106 | } | 
 | 3107 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3108 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, | 
 | 3109 | 					struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | { | 
 | 3111 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3112 | 	int idx = kcontrol->private_value; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3113 | 	struct hda_spdif_out *spdif; | 
 | 3114 | 	hda_nid_t nid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | 	unsigned short val; | 
 | 3116 | 	int change; | 
 | 3117 |  | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3118 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3119 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
 | 3120 | 	nid = spdif->nid; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3121 | 	val = spdif->ctls & ~AC_DIG1_ENABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | 	if (ucontrol->value.integer.value[0]) | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3123 | 		val |= AC_DIG1_ENABLE; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3124 | 	change = spdif->ctls != val; | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3125 | 	spdif->ctls = val; | 
 | 3126 | 	if (change && nid != (u16)-1) | 
 | 3127 | 		set_spdif_ctls(codec, nid, val & 0xff, -1); | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3128 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | 	return change; | 
 | 3130 | } | 
 | 3131 |  | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3132 | static struct snd_kcontrol_new dig_mixes[] = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | 	{ | 
 | 3134 | 		.access = SNDRV_CTL_ELEM_ACCESS_READ, | 
 | 3135 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3136 | 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | 		.info = snd_hda_spdif_mask_info, | 
 | 3138 | 		.get = snd_hda_spdif_cmask_get, | 
 | 3139 | 	}, | 
 | 3140 | 	{ | 
 | 3141 | 		.access = SNDRV_CTL_ELEM_ACCESS_READ, | 
 | 3142 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3143 | 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | 		.info = snd_hda_spdif_mask_info, | 
 | 3145 | 		.get = snd_hda_spdif_pmask_get, | 
 | 3146 | 	}, | 
 | 3147 | 	{ | 
 | 3148 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3149 | 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | 		.info = snd_hda_spdif_mask_info, | 
 | 3151 | 		.get = snd_hda_spdif_default_get, | 
 | 3152 | 		.put = snd_hda_spdif_default_put, | 
 | 3153 | 	}, | 
 | 3154 | 	{ | 
 | 3155 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3156 | 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | 		.info = snd_hda_spdif_out_switch_info, | 
 | 3158 | 		.get = snd_hda_spdif_out_switch_get, | 
 | 3159 | 		.put = snd_hda_spdif_out_switch_put, | 
 | 3160 | 	}, | 
 | 3161 | 	{ } /* end */ | 
 | 3162 | }; | 
 | 3163 |  | 
 | 3164 | /** | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3165 |  * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 |  * @codec: the HDA codec | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3167 |  * @associated_nid: NID that new ctls associated with | 
 | 3168 |  * @cvt_nid: converter NID | 
 | 3169 |  * @type: HDA_PCM_TYPE_* | 
 | 3170 |  * Creates controls related with the digital output. | 
 | 3171 |  * Called from each patch supporting the digital out. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 |  * | 
 | 3173 |  * Returns 0 if successful, or a negative error code. | 
 | 3174 |  */ | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3175 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, | 
 | 3176 | 				hda_nid_t associated_nid, | 
 | 3177 | 				hda_nid_t cvt_nid, | 
 | 3178 | 				int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | { | 
 | 3180 | 	int err; | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3181 | 	struct snd_kcontrol *kctl; | 
 | 3182 | 	struct snd_kcontrol_new *dig_mix; | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3183 | 	int idx, dev = 0; | 
 | 3184 | 	const int spdif_pcm_dev = 1; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3185 | 	struct hda_spdif_out *spdif; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 |  | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3187 | 	if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI && | 
 | 3188 | 	    type == HDA_PCM_TYPE_SPDIF) { | 
 | 3189 | 		dev = spdif_pcm_dev; | 
 | 3190 | 	} else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && | 
 | 3191 | 		   type == HDA_PCM_TYPE_HDMI) { | 
 | 3192 | 		for (idx = 0; idx < codec->spdif_out.used; idx++) { | 
 | 3193 | 			spdif = snd_array_elem(&codec->spdif_out, idx); | 
 | 3194 | 			for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { | 
 | 3195 | 				kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx); | 
 | 3196 | 				if (!kctl) | 
 | 3197 | 					break; | 
 | 3198 | 				kctl->id.device = spdif_pcm_dev; | 
 | 3199 | 			} | 
 | 3200 | 		} | 
 | 3201 | 		codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI; | 
 | 3202 | 	} | 
 | 3203 | 	if (!codec->primary_dig_out_type) | 
 | 3204 | 		codec->primary_dig_out_type = type; | 
 | 3205 |  | 
 | 3206 | 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev); | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3207 | 	if (idx < 0) { | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3208 | 		printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); | 
 | 3209 | 		return -EBUSY; | 
 | 3210 | 	} | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3211 | 	spdif = snd_array_new(&codec->spdif_out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | 	for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { | 
 | 3213 | 		kctl = snd_ctl_new1(dig_mix, codec); | 
| Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3214 | 		if (!kctl) | 
 | 3215 | 			return -ENOMEM; | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3216 | 		kctl->id.device = dev; | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3217 | 		kctl->id.index = idx; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3218 | 		kctl->private_value = codec->spdif_out.used - 1; | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3219 | 		err = snd_hda_ctl_add(codec, associated_nid, kctl); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3220 | 		if (err < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | 			return err; | 
 | 3222 | 	} | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3223 | 	spdif->nid = cvt_nid; | 
 | 3224 | 	spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3225 | 					 AC_VERB_GET_DIGI_CONVERT_1, 0); | 
 | 3226 | 	spdif->status = convert_to_spdif_status(spdif->ctls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | 	return 0; | 
 | 3228 | } | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3229 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 |  | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3231 | /* get the hda_spdif_out entry from the given NID | 
 | 3232 |  * call within spdif_mutex lock | 
 | 3233 |  */ | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3234 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, | 
 | 3235 | 					       hda_nid_t nid) | 
 | 3236 | { | 
 | 3237 | 	int i; | 
 | 3238 | 	for (i = 0; i < codec->spdif_out.used; i++) { | 
 | 3239 | 		struct hda_spdif_out *spdif = | 
 | 3240 | 				snd_array_elem(&codec->spdif_out, i); | 
 | 3241 | 		if (spdif->nid == nid) | 
 | 3242 | 			return spdif; | 
 | 3243 | 	} | 
 | 3244 | 	return NULL; | 
 | 3245 | } | 
 | 3246 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); | 
 | 3247 |  | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3248 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) | 
 | 3249 | { | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3250 | 	struct hda_spdif_out *spdif; | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3251 |  | 
 | 3252 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3253 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3254 | 	spdif->nid = (u16)-1; | 
 | 3255 | 	mutex_unlock(&codec->spdif_mutex); | 
 | 3256 | } | 
 | 3257 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); | 
 | 3258 |  | 
 | 3259 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) | 
 | 3260 | { | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3261 | 	struct hda_spdif_out *spdif; | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3262 | 	unsigned short val; | 
 | 3263 |  | 
 | 3264 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3265 | 	spdif = snd_array_elem(&codec->spdif_out, idx); | 
| Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3266 | 	if (spdif->nid != nid) { | 
 | 3267 | 		spdif->nid = nid; | 
 | 3268 | 		val = spdif->ctls; | 
 | 3269 | 		set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); | 
 | 3270 | 	} | 
 | 3271 | 	mutex_unlock(&codec->spdif_mutex); | 
 | 3272 | } | 
 | 3273 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); | 
 | 3274 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | /* | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3276 |  * SPDIF sharing with analog output | 
 | 3277 |  */ | 
 | 3278 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, | 
 | 3279 | 			      struct snd_ctl_elem_value *ucontrol) | 
 | 3280 | { | 
 | 3281 | 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | 
 | 3282 | 	ucontrol->value.integer.value[0] = mout->share_spdif; | 
 | 3283 | 	return 0; | 
 | 3284 | } | 
 | 3285 |  | 
 | 3286 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, | 
 | 3287 | 			      struct snd_ctl_elem_value *ucontrol) | 
 | 3288 | { | 
 | 3289 | 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); | 
 | 3290 | 	mout->share_spdif = !!ucontrol->value.integer.value[0]; | 
 | 3291 | 	return 0; | 
 | 3292 | } | 
 | 3293 |  | 
 | 3294 | static struct snd_kcontrol_new spdif_share_sw = { | 
 | 3295 | 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
 | 3296 | 	.name = "IEC958 Default PCM Playback Switch", | 
 | 3297 | 	.info = snd_ctl_boolean_mono_info, | 
 | 3298 | 	.get = spdif_share_sw_get, | 
 | 3299 | 	.put = spdif_share_sw_put, | 
 | 3300 | }; | 
 | 3301 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3302 | /** | 
 | 3303 |  * snd_hda_create_spdif_share_sw - create Default PCM switch | 
 | 3304 |  * @codec: the HDA codec | 
 | 3305 |  * @mout: multi-out instance | 
 | 3306 |  */ | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3307 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, | 
 | 3308 | 				  struct hda_multi_out *mout) | 
 | 3309 | { | 
 | 3310 | 	if (!mout->dig_out_nid) | 
 | 3311 | 		return 0; | 
 | 3312 | 	/* ATTENTION: here mout is passed as private_data, instead of codec */ | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3313 | 	return snd_hda_ctl_add(codec, mout->dig_out_nid, | 
 | 3314 | 			      snd_ctl_new1(&spdif_share_sw, mout)); | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3315 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3316 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3317 |  | 
 | 3318 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 |  * SPDIF input | 
 | 3320 |  */ | 
 | 3321 |  | 
 | 3322 | #define snd_hda_spdif_in_switch_info	snd_hda_spdif_out_switch_info | 
 | 3323 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3324 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, | 
 | 3325 | 				       struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | { | 
 | 3327 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 3328 |  | 
 | 3329 | 	ucontrol->value.integer.value[0] = codec->spdif_in_enable; | 
 | 3330 | 	return 0; | 
 | 3331 | } | 
 | 3332 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3333 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, | 
 | 3334 | 				       struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | { | 
 | 3336 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 3337 | 	hda_nid_t nid = kcontrol->private_value; | 
 | 3338 | 	unsigned int val = !!ucontrol->value.integer.value[0]; | 
 | 3339 | 	int change; | 
 | 3340 |  | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3341 | 	mutex_lock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | 	change = codec->spdif_in_enable != val; | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3343 | 	if (change) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | 		codec->spdif_in_enable = val; | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3345 | 		snd_hda_codec_write_cache(codec, nid, 0, | 
 | 3346 | 					  AC_VERB_SET_DIGI_CONVERT_1, val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | 	} | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3348 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | 	return change; | 
 | 3350 | } | 
 | 3351 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3352 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, | 
 | 3353 | 				       struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | { | 
 | 3355 | 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | 
 | 3356 | 	hda_nid_t nid = kcontrol->private_value; | 
 | 3357 | 	unsigned short val; | 
 | 3358 | 	unsigned int sbits; | 
 | 3359 |  | 
| Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3360 | 	val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | 	sbits = convert_to_spdif_status(val); | 
 | 3362 | 	ucontrol->value.iec958.status[0] = sbits; | 
 | 3363 | 	ucontrol->value.iec958.status[1] = sbits >> 8; | 
 | 3364 | 	ucontrol->value.iec958.status[2] = sbits >> 16; | 
 | 3365 | 	ucontrol->value.iec958.status[3] = sbits >> 24; | 
 | 3366 | 	return 0; | 
 | 3367 | } | 
 | 3368 |  | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3369 | static struct snd_kcontrol_new dig_in_ctls[] = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | 	{ | 
 | 3371 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3372 | 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | 		.info = snd_hda_spdif_in_switch_info, | 
 | 3374 | 		.get = snd_hda_spdif_in_switch_get, | 
 | 3375 | 		.put = snd_hda_spdif_in_switch_put, | 
 | 3376 | 	}, | 
 | 3377 | 	{ | 
 | 3378 | 		.access = SNDRV_CTL_ELEM_ACCESS_READ, | 
 | 3379 | 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3380 | 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | 		.info = snd_hda_spdif_mask_info, | 
 | 3382 | 		.get = snd_hda_spdif_in_status_get, | 
 | 3383 | 	}, | 
 | 3384 | 	{ } /* end */ | 
 | 3385 | }; | 
 | 3386 |  | 
 | 3387 | /** | 
 | 3388 |  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls | 
 | 3389 |  * @codec: the HDA codec | 
 | 3390 |  * @nid: audio in widget NID | 
 | 3391 |  * | 
 | 3392 |  * Creates controls related with the SPDIF input. | 
 | 3393 |  * Called from each patch supporting the SPDIF in. | 
 | 3394 |  * | 
 | 3395 |  * Returns 0 if successful, or a negative error code. | 
 | 3396 |  */ | 
| Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3397 | int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | { | 
 | 3399 | 	int err; | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3400 | 	struct snd_kcontrol *kctl; | 
 | 3401 | 	struct snd_kcontrol_new *dig_mix; | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3402 | 	int idx; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 |  | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3404 | 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3405 | 	if (idx < 0) { | 
| Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3406 | 		printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); | 
 | 3407 | 		return -EBUSY; | 
 | 3408 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | 	for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { | 
 | 3410 | 		kctl = snd_ctl_new1(dig_mix, codec); | 
| Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3411 | 		if (!kctl) | 
 | 3412 | 			return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | 		kctl->private_value = nid; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3414 | 		err = snd_hda_ctl_add(codec, nid, kctl); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3415 | 		if (err < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | 			return err; | 
 | 3417 | 	} | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3418 | 	codec->spdif_in_enable = | 
| Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3419 | 		snd_hda_codec_read(codec, nid, 0, | 
 | 3420 | 				   AC_VERB_GET_DIGI_CONVERT_1, 0) & | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3421 | 		AC_DIG1_ENABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | 	return 0; | 
 | 3423 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3424 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 |  | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3426 | /* | 
 | 3427 |  * command cache | 
 | 3428 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 |  | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3430 | /* build a 31bit cache key with the widget id and the command parameter */ | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3431 | #define build_cmd_cache_key(nid, verb)	((verb << 8) | nid) | 
 | 3432 | #define get_cmd_cache_nid(key)		((key) & 0xff) | 
 | 3433 | #define get_cmd_cache_cmd(key)		(((key) >> 8) & 0xffff) | 
 | 3434 |  | 
 | 3435 | /** | 
 | 3436 |  * snd_hda_codec_write_cache - send a single command with caching | 
 | 3437 |  * @codec: the HDA codec | 
 | 3438 |  * @nid: NID to send the command | 
 | 3439 |  * @direct: direct flag | 
 | 3440 |  * @verb: the verb to send | 
 | 3441 |  * @parm: the parameter for the verb | 
 | 3442 |  * | 
 | 3443 |  * Send a single command without waiting for response. | 
 | 3444 |  * | 
 | 3445 |  * Returns 0 if successful, or a negative error code. | 
 | 3446 |  */ | 
 | 3447 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, | 
 | 3448 | 			      int direct, unsigned int verb, unsigned int parm) | 
 | 3449 | { | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3450 | 	int err; | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3451 | 	struct hda_cache_head *c; | 
 | 3452 | 	u32 key; | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 3453 | 	unsigned int cache_only; | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3454 |  | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 3455 | 	cache_only = codec->cached_write; | 
 | 3456 | 	if (!cache_only) { | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3457 | 		err = snd_hda_codec_write(codec, nid, direct, verb, parm); | 
 | 3458 | 		if (err < 0) | 
 | 3459 | 			return err; | 
 | 3460 | 	} | 
 | 3461 |  | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3462 | 	/* parm may contain the verb stuff for get/set amp */ | 
 | 3463 | 	verb = verb | (parm >> 8); | 
 | 3464 | 	parm &= 0xff; | 
 | 3465 | 	key = build_cmd_cache_key(nid, verb); | 
 | 3466 | 	mutex_lock(&codec->bus->cmd_mutex); | 
 | 3467 | 	c = get_alloc_hash(&codec->cmd_cache, key); | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3468 | 	if (c) { | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3469 | 		c->val = parm; | 
| Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame^] | 3470 | 		c->dirty = cache_only; | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3471 | 	} | 
| Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3472 | 	mutex_unlock(&codec->bus->cmd_mutex); | 
 | 3473 | 	return 0; | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3474 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3475 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3476 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3477 | /** | 
| Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3478 |  * snd_hda_codec_update_cache - check cache and write the cmd only when needed | 
 | 3479 |  * @codec: the HDA codec | 
 | 3480 |  * @nid: NID to send the command | 
 | 3481 |  * @direct: direct flag | 
 | 3482 |  * @verb: the verb to send | 
 | 3483 |  * @parm: the parameter for the verb | 
 | 3484 |  * | 
 | 3485 |  * This function works like snd_hda_codec_write_cache(), but it doesn't send | 
 | 3486 |  * command if the parameter is already identical with the cached value. | 
 | 3487 |  * If not, it sends the command and refreshes the cache. | 
 | 3488 |  * | 
 | 3489 |  * Returns 0 if successful, or a negative error code. | 
 | 3490 |  */ | 
 | 3491 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, | 
 | 3492 | 			       int direct, unsigned int verb, unsigned int parm) | 
 | 3493 | { | 
 | 3494 | 	struct hda_cache_head *c; | 
 | 3495 | 	u32 key; | 
 | 3496 |  | 
 | 3497 | 	/* parm may contain the verb stuff for get/set amp */ | 
 | 3498 | 	verb = verb | (parm >> 8); | 
 | 3499 | 	parm &= 0xff; | 
 | 3500 | 	key = build_cmd_cache_key(nid, verb); | 
 | 3501 | 	mutex_lock(&codec->bus->cmd_mutex); | 
 | 3502 | 	c = get_hash(&codec->cmd_cache, key); | 
 | 3503 | 	if (c && c->val == parm) { | 
 | 3504 | 		mutex_unlock(&codec->bus->cmd_mutex); | 
 | 3505 | 		return 0; | 
 | 3506 | 	} | 
 | 3507 | 	mutex_unlock(&codec->bus->cmd_mutex); | 
 | 3508 | 	return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); | 
 | 3509 | } | 
 | 3510 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); | 
 | 3511 |  | 
 | 3512 | /** | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3513 |  * snd_hda_codec_resume_cache - Resume the all commands from the cache | 
 | 3514 |  * @codec: HD-audio codec | 
 | 3515 |  * | 
 | 3516 |  * Execute all verbs recorded in the command caches to resume. | 
 | 3517 |  */ | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3518 | void snd_hda_codec_resume_cache(struct hda_codec *codec) | 
 | 3519 | { | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3520 | 	int i; | 
 | 3521 |  | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3522 | 	mutex_lock(&codec->hash_mutex); | 
 | 3523 | 	for (i = 0; i < codec->cmd_cache.buf.used; i++) { | 
 | 3524 | 		struct hda_cache_head *buffer; | 
 | 3525 | 		u32 key; | 
 | 3526 |  | 
 | 3527 | 		buffer = snd_array_elem(&codec->cmd_cache.buf, i); | 
 | 3528 | 		key = buffer->key; | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3529 | 		if (!key) | 
 | 3530 | 			continue; | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3531 | 		if (!buffer->dirty) | 
 | 3532 | 			continue; | 
 | 3533 | 		buffer->dirty = 0; | 
 | 3534 | 		mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3535 | 		snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, | 
 | 3536 | 				    get_cmd_cache_cmd(key), buffer->val); | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3537 | 		mutex_lock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3538 | 	} | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3539 | 	mutex_unlock(&codec->hash_mutex); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3540 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3541 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3542 |  | 
 | 3543 | /** | 
 | 3544 |  * snd_hda_sequence_write_cache - sequence writes with caching | 
 | 3545 |  * @codec: the HDA codec | 
 | 3546 |  * @seq: VERB array to send | 
 | 3547 |  * | 
 | 3548 |  * Send the commands sequentially from the given array. | 
 | 3549 |  * Thte commands are recorded on cache for power-save and resume. | 
 | 3550 |  * The array must be terminated with NID=0. | 
 | 3551 |  */ | 
 | 3552 | void snd_hda_sequence_write_cache(struct hda_codec *codec, | 
 | 3553 | 				  const struct hda_verb *seq) | 
 | 3554 | { | 
 | 3555 | 	for (; seq->nid; seq++) | 
 | 3556 | 		snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, | 
 | 3557 | 					  seq->param); | 
 | 3558 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3559 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); | 
| Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3560 |  | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3561 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, | 
 | 3562 | 				    unsigned int power_state, | 
 | 3563 | 				    bool eapd_workaround) | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3564 | { | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3565 | 	hda_nid_t nid = codec->start_nid; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3566 | 	int i; | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3567 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3568 | 	for (i = 0; i < codec->num_nodes; i++, nid++) { | 
| Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3569 | 		unsigned int wcaps = get_wcaps(codec, nid); | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3570 | 		if (!(wcaps & AC_WCAP_POWER)) | 
 | 3571 | 			continue; | 
 | 3572 | 		/* don't power down the widget if it controls eapd and | 
 | 3573 | 		 * EAPD_BTLENABLE is set. | 
 | 3574 | 		 */ | 
 | 3575 | 		if (eapd_workaround && power_state == AC_PWRST_D3 && | 
 | 3576 | 		    get_wcaps_type(wcaps) == AC_WID_PIN && | 
 | 3577 | 		    (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { | 
 | 3578 | 			int eapd = snd_hda_codec_read(codec, nid, 0, | 
| Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3579 | 						AC_VERB_GET_EAPD_BTLENABLE, 0); | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3580 | 			if (eapd & 0x02) | 
 | 3581 | 				continue; | 
| Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3582 | 		} | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3583 | 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, | 
 | 3584 | 				    power_state); | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3585 | 	} | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3586 | } | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3587 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); | 
 | 3588 |  | 
 | 3589 | /* | 
| Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3590 |  *  supported power states check | 
 | 3591 |  */ | 
 | 3592 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, | 
 | 3593 | 				unsigned int power_state) | 
 | 3594 | { | 
 | 3595 | 	int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); | 
 | 3596 |  | 
| Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3597 | 	if (sup == -1) | 
| Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3598 | 		return false; | 
 | 3599 | 	if (sup & power_state) | 
 | 3600 | 		return true; | 
 | 3601 | 	else | 
 | 3602 | 		return false; | 
 | 3603 | } | 
 | 3604 |  | 
 | 3605 | /* | 
| Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3606 |  * wait until the state is reached, returns the current state | 
 | 3607 |  */ | 
 | 3608 | static unsigned int hda_sync_power_state(struct hda_codec *codec, | 
 | 3609 | 					 hda_nid_t fg, | 
 | 3610 | 					 unsigned int power_state) | 
 | 3611 | { | 
 | 3612 | 	unsigned long end_time = jiffies + msecs_to_jiffies(500); | 
 | 3613 | 	unsigned int state, actual_state; | 
 | 3614 |  | 
 | 3615 | 	for (;;) { | 
 | 3616 | 		state = snd_hda_codec_read(codec, fg, 0, | 
 | 3617 | 					   AC_VERB_GET_POWER_STATE, 0); | 
 | 3618 | 		if (state & AC_PWRST_ERROR) | 
 | 3619 | 			break; | 
 | 3620 | 		actual_state = (state >> 4) & 0x0f; | 
 | 3621 | 		if (actual_state == power_state) | 
 | 3622 | 			break; | 
 | 3623 | 		if (time_after_eq(jiffies, end_time)) | 
 | 3624 | 			break; | 
 | 3625 | 		/* wait until the codec reachs to the target state */ | 
 | 3626 | 		msleep(1); | 
 | 3627 | 	} | 
 | 3628 | 	return state; | 
 | 3629 | } | 
 | 3630 |  | 
 | 3631 | /* | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3632 |  * set power state of the codec, and return the power state | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3633 |  */ | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3634 | static unsigned int hda_set_power_state(struct hda_codec *codec, | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3635 | 					unsigned int power_state) | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3636 | { | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3637 | 	hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; | 
| Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3638 | 	int count; | 
 | 3639 | 	unsigned int state; | 
 | 3640 |  | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3641 | 	/* this delay seems necessary to avoid click noise at power-down */ | 
| Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3642 | 	if (power_state == AC_PWRST_D3) { | 
 | 3643 | 		/* transition time less than 10ms for power down */ | 
| Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 3644 | 		msleep(codec->epss ? 10 : 100); | 
| Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3645 | 	} | 
| Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3646 |  | 
 | 3647 | 	/* repeat power states setting at most 10 times*/ | 
 | 3648 | 	for (count = 0; count < 10; count++) { | 
| Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3649 | 		if (codec->patch_ops.set_power_state) | 
 | 3650 | 			codec->patch_ops.set_power_state(codec, fg, | 
 | 3651 | 							 power_state); | 
 | 3652 | 		else { | 
 | 3653 | 			snd_hda_codec_read(codec, fg, 0, | 
 | 3654 | 					   AC_VERB_SET_POWER_STATE, | 
 | 3655 | 					   power_state); | 
 | 3656 | 			snd_hda_codec_set_power_to_all(codec, fg, power_state, | 
 | 3657 | 						       true); | 
 | 3658 | 		} | 
 | 3659 | 		state = hda_sync_power_state(codec, fg, power_state); | 
| Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3660 | 		if (!(state & AC_PWRST_ERROR)) | 
 | 3661 | 			break; | 
 | 3662 | 	} | 
| Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3663 |  | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3664 | 	return state; | 
| Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3665 | } | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3666 |  | 
| Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3667 | #ifdef CONFIG_SND_HDA_HWDEP | 
 | 3668 | /* execute additional init verbs */ | 
 | 3669 | static void hda_exec_init_verbs(struct hda_codec *codec) | 
 | 3670 | { | 
 | 3671 | 	if (codec->init_verbs.list) | 
 | 3672 | 		snd_hda_sequence_write(codec, codec->init_verbs.list); | 
 | 3673 | } | 
 | 3674 | #else | 
 | 3675 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} | 
 | 3676 | #endif | 
 | 3677 |  | 
| Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3678 | #ifdef CONFIG_PM | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3679 | /* | 
 | 3680 |  * call suspend and power-down; used both from PM and power-save | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3681 |  * this function returns the power state in the end | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3682 |  */ | 
| Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3683 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3684 | { | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3685 | 	unsigned int state; | 
 | 3686 |  | 
| Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3687 | 	codec->in_pm = 1; | 
 | 3688 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3689 | 	if (codec->patch_ops.suspend) | 
| Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3690 | 		codec->patch_ops.suspend(codec); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3691 | 	hda_cleanup_all_streams(codec); | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3692 | 	state = hda_set_power_state(codec, AC_PWRST_D3); | 
| Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3693 | 	/* Cancel delayed work if we aren't currently running from it. */ | 
 | 3694 | 	if (!in_wq) | 
 | 3695 | 		cancel_delayed_work_sync(&codec->power_work); | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3696 | 	spin_lock(&codec->power_lock); | 
 | 3697 | 	snd_hda_update_power_acct(codec); | 
 | 3698 | 	trace_hda_power_down(codec); | 
| Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 3699 | 	codec->power_on = 0; | 
| Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 3700 | 	codec->power_transition = 0; | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3701 | 	codec->power_jiffies = jiffies; | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3702 | 	spin_unlock(&codec->power_lock); | 
| Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3703 | 	codec->in_pm = 0; | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3704 | 	return state; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3705 | } | 
 | 3706 |  | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3707 | /* mark all entries of cmd and amp caches dirty */ | 
 | 3708 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) | 
 | 3709 | { | 
 | 3710 | 	int i; | 
 | 3711 | 	for (i = 0; i < codec->cmd_cache.buf.used; i++) { | 
 | 3712 | 		struct hda_cache_head *cmd; | 
 | 3713 | 		cmd = snd_array_elem(&codec->cmd_cache.buf, i); | 
 | 3714 | 		cmd->dirty = 1; | 
 | 3715 | 	} | 
 | 3716 | 	for (i = 0; i < codec->amp_cache.buf.used; i++) { | 
 | 3717 | 		struct hda_amp_info *amp; | 
 | 3718 | 		amp = snd_array_elem(&codec->cmd_cache.buf, i); | 
 | 3719 | 		amp->head.dirty = 1; | 
 | 3720 | 	} | 
 | 3721 | } | 
 | 3722 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3723 | /* | 
 | 3724 |  * kick up codec; used both from PM and power-save | 
 | 3725 |  */ | 
 | 3726 | static void hda_call_codec_resume(struct hda_codec *codec) | 
 | 3727 | { | 
| Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3728 | 	codec->in_pm = 1; | 
 | 3729 |  | 
| Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3730 | 	hda_mark_cmd_cache_dirty(codec); | 
 | 3731 |  | 
| Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3732 | 	/* set as if powered on for avoiding re-entering the resume | 
 | 3733 | 	 * in the resume / power-save sequence | 
 | 3734 | 	 */ | 
 | 3735 | 	hda_keep_power_on(codec); | 
| Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3736 | 	hda_set_power_state(codec, AC_PWRST_D0); | 
| Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3737 | 	restore_shutup_pins(codec); | 
| Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3738 | 	hda_exec_init_verbs(codec); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3739 | 	if (codec->patch_ops.resume) | 
 | 3740 | 		codec->patch_ops.resume(codec); | 
 | 3741 | 	else { | 
| Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3742 | 		if (codec->patch_ops.init) | 
 | 3743 | 			codec->patch_ops.init(codec); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3744 | 		snd_hda_codec_resume_amp(codec); | 
 | 3745 | 		snd_hda_codec_resume_cache(codec); | 
 | 3746 | 	} | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3747 |  | 
 | 3748 | 	if (codec->jackpoll_interval) | 
 | 3749 | 		hda_jackpoll_work(&codec->jackpoll_work.work); | 
 | 3750 | 	else { | 
 | 3751 | 		snd_hda_jack_set_dirty_all(codec); | 
 | 3752 | 		snd_hda_jack_report_sync(codec); | 
 | 3753 | 	} | 
| Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3754 |  | 
 | 3755 | 	codec->in_pm = 0; | 
| Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3756 | 	snd_hda_power_down(codec); /* flag down before returning */ | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3757 | } | 
| Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3758 | #endif /* CONFIG_PM */ | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3759 |  | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3760 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | /** | 
 | 3762 |  * snd_hda_build_controls - build mixer controls | 
 | 3763 |  * @bus: the BUS | 
 | 3764 |  * | 
 | 3765 |  * Creates mixer controls for each codec included in the bus. | 
 | 3766 |  * | 
 | 3767 |  * Returns 0 if successful, otherwise a negative error code. | 
 | 3768 |  */ | 
| Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 3769 | int snd_hda_build_controls(struct hda_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3771 | 	struct hda_codec *codec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3773 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3774 | 		int err = snd_hda_codec_build_controls(codec); | 
| Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3775 | 		if (err < 0) { | 
| Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 3776 | 			printk(KERN_ERR "hda_codec: cannot build controls " | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3777 | 			       "for #%d (error %d)\n", codec->addr, err); | 
| Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3778 | 			err = snd_hda_codec_reset(codec); | 
 | 3779 | 			if (err < 0) { | 
 | 3780 | 				printk(KERN_ERR | 
 | 3781 | 				       "hda_codec: cannot revert codec\n"); | 
 | 3782 | 				return err; | 
 | 3783 | 			} | 
 | 3784 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | 	} | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3786 | 	return 0; | 
 | 3787 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3788 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 |  | 
| Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3790 | /* | 
 | 3791 |  * add standard channel maps if not specified | 
 | 3792 |  */ | 
 | 3793 | static int add_std_chmaps(struct hda_codec *codec) | 
 | 3794 | { | 
 | 3795 | 	int i, str, err; | 
 | 3796 |  | 
 | 3797 | 	for (i = 0; i < codec->num_pcms; i++) { | 
 | 3798 | 		for (str = 0; str < 2; str++) { | 
 | 3799 | 			struct snd_pcm *pcm = codec->pcm_info[i].pcm; | 
 | 3800 | 			struct hda_pcm_stream *hinfo = | 
 | 3801 | 				&codec->pcm_info[i].stream[str]; | 
 | 3802 | 			struct snd_pcm_chmap *chmap; | 
| Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3803 | 			const struct snd_pcm_chmap_elem *elem; | 
| Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3804 |  | 
 | 3805 | 			if (codec->pcm_info[i].own_chmap) | 
 | 3806 | 				continue; | 
 | 3807 | 			if (!pcm || !hinfo->substreams) | 
 | 3808 | 				continue; | 
| Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3809 | 			elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; | 
 | 3810 | 			err = snd_pcm_add_chmap_ctls(pcm, str, elem, | 
| Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3811 | 						     hinfo->channels_max, | 
 | 3812 | 						     0, &chmap); | 
 | 3813 | 			if (err < 0) | 
 | 3814 | 				return err; | 
 | 3815 | 			chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; | 
 | 3816 | 		} | 
 | 3817 | 	} | 
 | 3818 | 	return 0; | 
 | 3819 | } | 
 | 3820 |  | 
| Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3821 | /* default channel maps for 2.1 speakers; | 
 | 3822 |  * since HD-audio supports only stereo, odd number channels are omitted | 
 | 3823 |  */ | 
 | 3824 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { | 
 | 3825 | 	{ .channels = 2, | 
 | 3826 | 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, | 
 | 3827 | 	{ .channels = 4, | 
 | 3828 | 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, | 
 | 3829 | 		   SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, | 
 | 3830 | 	{ } | 
 | 3831 | }; | 
 | 3832 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); | 
 | 3833 |  | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3834 | int snd_hda_codec_build_controls(struct hda_codec *codec) | 
 | 3835 | { | 
 | 3836 | 	int err = 0; | 
| Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3837 | 	hda_exec_init_verbs(codec); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3838 | 	/* continue to initialize... */ | 
 | 3839 | 	if (codec->patch_ops.init) | 
 | 3840 | 		err = codec->patch_ops.init(codec); | 
 | 3841 | 	if (!err && codec->patch_ops.build_controls) | 
 | 3842 | 		err = codec->patch_ops.build_controls(codec); | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3843 | 	if (err < 0) | 
 | 3844 | 		return err; | 
| Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3845 |  | 
 | 3846 | 	/* we create chmaps here instead of build_pcms */ | 
 | 3847 | 	err = add_std_chmaps(codec); | 
 | 3848 | 	if (err < 0) | 
 | 3849 | 		return err; | 
 | 3850 |  | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3851 | 	if (codec->jackpoll_interval) | 
 | 3852 | 		hda_jackpoll_work(&codec->jackpoll_work.work); | 
 | 3853 | 	else | 
 | 3854 | 		snd_hda_jack_report_sync(codec); /* call at the last init point */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | 	return 0; | 
 | 3856 | } | 
 | 3857 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | /* | 
 | 3859 |  * stream formats | 
 | 3860 |  */ | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3861 | struct hda_rate_tbl { | 
 | 3862 | 	unsigned int hz; | 
 | 3863 | 	unsigned int alsa_bits; | 
 | 3864 | 	unsigned int hda_fmt; | 
 | 3865 | }; | 
 | 3866 |  | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3867 | /* rate = base * mult / div */ | 
 | 3868 | #define HDA_RATE(base, mult, div) \ | 
 | 3869 | 	(AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ | 
 | 3870 | 	 (((div) - 1) << AC_FMT_DIV_SHIFT)) | 
 | 3871 |  | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3872 | static struct hda_rate_tbl rate_bits[] = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | 	/* rate in Hz, ALSA rate bitmask, HDA format value */ | 
| Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3874 |  | 
 | 3875 | 	/* autodetected value used in snd_hda_query_supported_pcm */ | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3876 | 	{ 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, | 
 | 3877 | 	{ 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, | 
 | 3878 | 	{ 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, | 
 | 3879 | 	{ 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, | 
 | 3880 | 	{ 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, | 
 | 3881 | 	{ 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, | 
 | 3882 | 	{ 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, | 
 | 3883 | 	{ 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, | 
 | 3884 | 	{ 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, | 
 | 3885 | 	{ 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, | 
 | 3886 | 	{ 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, | 
| Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3887 | #define AC_PAR_PCM_RATE_BITS	11 | 
 | 3888 | 	/* up to bits 10, 384kHZ isn't supported properly */ | 
 | 3889 |  | 
 | 3890 | 	/* not autodetected value */ | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3891 | 	{ 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, | 
| Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3892 |  | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3893 | 	{ 0 } /* terminator */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | }; | 
 | 3895 |  | 
 | 3896 | /** | 
 | 3897 |  * snd_hda_calc_stream_format - calculate format bitset | 
 | 3898 |  * @rate: the sample rate | 
 | 3899 |  * @channels: the number of channels | 
 | 3900 |  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) | 
 | 3901 |  * @maxbps: the max. bps | 
 | 3902 |  * | 
 | 3903 |  * Calculate the format bitset from the given rate, channels and th PCM format. | 
 | 3904 |  * | 
 | 3905 |  * Return zero if invalid. | 
 | 3906 |  */ | 
 | 3907 | unsigned int snd_hda_calc_stream_format(unsigned int rate, | 
 | 3908 | 					unsigned int channels, | 
 | 3909 | 					unsigned int format, | 
| Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3910 | 					unsigned int maxbps, | 
 | 3911 | 					unsigned short spdif_ctls) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | { | 
 | 3913 | 	int i; | 
 | 3914 | 	unsigned int val = 0; | 
 | 3915 |  | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3916 | 	for (i = 0; rate_bits[i].hz; i++) | 
 | 3917 | 		if (rate_bits[i].hz == rate) { | 
 | 3918 | 			val = rate_bits[i].hda_fmt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | 			break; | 
 | 3920 | 		} | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3921 | 	if (!rate_bits[i].hz) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | 		snd_printdd("invalid rate %d\n", rate); | 
 | 3923 | 		return 0; | 
 | 3924 | 	} | 
 | 3925 |  | 
 | 3926 | 	if (channels == 0 || channels > 8) { | 
 | 3927 | 		snd_printdd("invalid channels %d\n", channels); | 
 | 3928 | 		return 0; | 
 | 3929 | 	} | 
 | 3930 | 	val |= channels - 1; | 
 | 3931 |  | 
 | 3932 | 	switch (snd_pcm_format_width(format)) { | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3933 | 	case 8: | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3934 | 		val |= AC_FMT_BITS_8; | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3935 | 		break; | 
 | 3936 | 	case 16: | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3937 | 		val |= AC_FMT_BITS_16; | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3938 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | 	case 20: | 
 | 3940 | 	case 24: | 
 | 3941 | 	case 32: | 
| Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 3942 | 		if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3943 | 			val |= AC_FMT_BITS_32; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | 		else if (maxbps >= 24) | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3945 | 			val |= AC_FMT_BITS_24; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | 		else | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3947 | 			val |= AC_FMT_BITS_20; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | 		break; | 
 | 3949 | 	default: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3950 | 		snd_printdd("invalid format width %d\n", | 
 | 3951 | 			    snd_pcm_format_width(format)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | 		return 0; | 
 | 3953 | 	} | 
 | 3954 |  | 
| Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3955 | 	if (spdif_ctls & AC_DIG1_NONAUDIO) | 
| Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3956 | 		val |= AC_FMT_TYPE_NON_PCM; | 
| Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3957 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | 	return val; | 
 | 3959 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3960 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 3962 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, | 
 | 3963 | 				  int dir) | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3964 | { | 
 | 3965 | 	unsigned int val = 0; | 
 | 3966 | 	if (nid != codec->afg && | 
 | 3967 | 	    (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) | 
 | 3968 | 		val = snd_hda_param_read(codec, nid, AC_PAR_PCM); | 
 | 3969 | 	if (!val || val == -1) | 
 | 3970 | 		val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); | 
 | 3971 | 	if (!val || val == -1) | 
 | 3972 | 		return 0; | 
 | 3973 | 	return val; | 
 | 3974 | } | 
 | 3975 |  | 
 | 3976 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) | 
 | 3977 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 3978 | 	return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3979 | 			       get_pcm_param); | 
 | 3980 | } | 
 | 3981 |  | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 3982 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, | 
 | 3983 | 				     int dir) | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3984 | { | 
 | 3985 | 	unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); | 
 | 3986 | 	if (!streams || streams == -1) | 
 | 3987 | 		streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); | 
 | 3988 | 	if (!streams || streams == -1) | 
 | 3989 | 		return 0; | 
 | 3990 | 	return streams; | 
 | 3991 | } | 
 | 3992 |  | 
 | 3993 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) | 
 | 3994 | { | 
| Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 3995 | 	return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3996 | 			       get_stream_param); | 
 | 3997 | } | 
 | 3998 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | /** | 
 | 4000 |  * snd_hda_query_supported_pcm - query the supported PCM rates and formats | 
 | 4001 |  * @codec: the HDA codec | 
 | 4002 |  * @nid: NID to query | 
 | 4003 |  * @ratesp: the pointer to store the detected rate bitflags | 
 | 4004 |  * @formatsp: the pointer to store the detected formats | 
 | 4005 |  * @bpsp: the pointer to store the detected format widths | 
 | 4006 |  * | 
 | 4007 |  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp | 
 | 4008 |  * or @bsps argument is ignored. | 
 | 4009 |  * | 
 | 4010 |  * Returns 0 if successful, otherwise a negative error code. | 
 | 4011 |  */ | 
| Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4012 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4013 | 				u32 *ratesp, u64 *formatsp, unsigned int *bpsp) | 
 | 4014 | { | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4015 | 	unsigned int i, val, wcaps; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 |  | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4017 | 	wcaps = get_wcaps(codec, nid); | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4018 | 	val = query_pcm_param(codec, nid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 |  | 
 | 4020 | 	if (ratesp) { | 
 | 4021 | 		u32 rates = 0; | 
| Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4022 | 		for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | 			if (val & (1 << i)) | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4024 | 				rates |= rate_bits[i].alsa_bits; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | 		} | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4026 | 		if (rates == 0) { | 
 | 4027 | 			snd_printk(KERN_ERR "hda_codec: rates == 0 " | 
 | 4028 | 				   "(nid=0x%x, val=0x%x, ovrd=%i)\n", | 
 | 4029 | 					nid, val, | 
 | 4030 | 					(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); | 
 | 4031 | 			return -EIO; | 
 | 4032 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | 		*ratesp = rates; | 
 | 4034 | 	} | 
 | 4035 |  | 
 | 4036 | 	if (formatsp || bpsp) { | 
 | 4037 | 		u64 formats = 0; | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4038 | 		unsigned int streams, bps; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 |  | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4040 | 		streams = query_stream_param(codec, nid); | 
 | 4041 | 		if (!streams) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | 			return -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 |  | 
 | 4044 | 		bps = 0; | 
 | 4045 | 		if (streams & AC_SUPFMT_PCM) { | 
 | 4046 | 			if (val & AC_SUPPCM_BITS_8) { | 
 | 4047 | 				formats |= SNDRV_PCM_FMTBIT_U8; | 
 | 4048 | 				bps = 8; | 
 | 4049 | 			} | 
 | 4050 | 			if (val & AC_SUPPCM_BITS_16) { | 
 | 4051 | 				formats |= SNDRV_PCM_FMTBIT_S16_LE; | 
 | 4052 | 				bps = 16; | 
 | 4053 | 			} | 
 | 4054 | 			if (wcaps & AC_WCAP_DIGITAL) { | 
 | 4055 | 				if (val & AC_SUPPCM_BITS_32) | 
 | 4056 | 					formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; | 
 | 4057 | 				if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) | 
 | 4058 | 					formats |= SNDRV_PCM_FMTBIT_S32_LE; | 
 | 4059 | 				if (val & AC_SUPPCM_BITS_24) | 
 | 4060 | 					bps = 24; | 
 | 4061 | 				else if (val & AC_SUPPCM_BITS_20) | 
 | 4062 | 					bps = 20; | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4063 | 			} else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| | 
 | 4064 | 					  AC_SUPPCM_BITS_32)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | 				formats |= SNDRV_PCM_FMTBIT_S32_LE; | 
 | 4066 | 				if (val & AC_SUPPCM_BITS_32) | 
 | 4067 | 					bps = 32; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | 				else if (val & AC_SUPPCM_BITS_24) | 
 | 4069 | 					bps = 24; | 
| Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4070 | 				else if (val & AC_SUPPCM_BITS_20) | 
 | 4071 | 					bps = 20; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | 			} | 
 | 4073 | 		} | 
| Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4074 | #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ | 
| Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4075 | 		if (streams & AC_SUPFMT_FLOAT32) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | 			formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; | 
| Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4077 | 			if (!bps) | 
 | 4078 | 				bps = 32; | 
| Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4079 | 		} | 
| Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4080 | #endif | 
| Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4081 | 		if (streams == AC_SUPFMT_AC3) { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4082 | 			/* should be exclusive */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | 			/* temporary hack: we have still no proper support | 
 | 4084 | 			 * for the direct AC3 stream... | 
 | 4085 | 			 */ | 
 | 4086 | 			formats |= SNDRV_PCM_FMTBIT_U8; | 
 | 4087 | 			bps = 8; | 
 | 4088 | 		} | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4089 | 		if (formats == 0) { | 
 | 4090 | 			snd_printk(KERN_ERR "hda_codec: formats == 0 " | 
 | 4091 | 				   "(nid=0x%x, val=0x%x, ovrd=%i, " | 
 | 4092 | 				   "streams=0x%x)\n", | 
 | 4093 | 					nid, val, | 
 | 4094 | 					(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, | 
 | 4095 | 					streams); | 
 | 4096 | 			return -EIO; | 
 | 4097 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | 		if (formatsp) | 
 | 4099 | 			*formatsp = formats; | 
 | 4100 | 		if (bpsp) | 
 | 4101 | 			*bpsp = bps; | 
 | 4102 | 	} | 
 | 4103 |  | 
 | 4104 | 	return 0; | 
 | 4105 | } | 
| Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4106 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 |  | 
 | 4108 | /** | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4109 |  * snd_hda_is_supported_format - Check the validity of the format | 
 | 4110 |  * @codec: HD-audio codec | 
 | 4111 |  * @nid: NID to check | 
 | 4112 |  * @format: the HD-audio format value to check | 
 | 4113 |  * | 
 | 4114 |  * Check whether the given node supports the format value. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 |  * | 
 | 4116 |  * Returns 1 if supported, 0 if not. | 
 | 4117 |  */ | 
 | 4118 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, | 
 | 4119 | 				unsigned int format) | 
 | 4120 | { | 
 | 4121 | 	int i; | 
 | 4122 | 	unsigned int val = 0, rate, stream; | 
 | 4123 |  | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4124 | 	val = query_pcm_param(codec, nid); | 
 | 4125 | 	if (!val) | 
 | 4126 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 |  | 
 | 4128 | 	rate = format & 0xff00; | 
| Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4129 | 	for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) | 
| Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4130 | 		if (rate_bits[i].hda_fmt == rate) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | 			if (val & (1 << i)) | 
 | 4132 | 				break; | 
 | 4133 | 			return 0; | 
 | 4134 | 		} | 
| Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4135 | 	if (i >= AC_PAR_PCM_RATE_BITS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | 		return 0; | 
 | 4137 |  | 
| Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4138 | 	stream = query_stream_param(codec, nid); | 
 | 4139 | 	if (!stream) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | 		return 0; | 
 | 4141 |  | 
 | 4142 | 	if (stream & AC_SUPFMT_PCM) { | 
 | 4143 | 		switch (format & 0xf0) { | 
 | 4144 | 		case 0x00: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4145 | 			if (!(val & AC_SUPPCM_BITS_8)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | 				return 0; | 
 | 4147 | 			break; | 
 | 4148 | 		case 0x10: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4149 | 			if (!(val & AC_SUPPCM_BITS_16)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | 				return 0; | 
 | 4151 | 			break; | 
 | 4152 | 		case 0x20: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4153 | 			if (!(val & AC_SUPPCM_BITS_20)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | 				return 0; | 
 | 4155 | 			break; | 
 | 4156 | 		case 0x30: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4157 | 			if (!(val & AC_SUPPCM_BITS_24)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | 				return 0; | 
 | 4159 | 			break; | 
 | 4160 | 		case 0x40: | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4161 | 			if (!(val & AC_SUPPCM_BITS_32)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | 				return 0; | 
 | 4163 | 			break; | 
 | 4164 | 		default: | 
 | 4165 | 			return 0; | 
 | 4166 | 		} | 
 | 4167 | 	} else { | 
 | 4168 | 		/* FIXME: check for float32 and AC3? */ | 
 | 4169 | 	} | 
 | 4170 |  | 
 | 4171 | 	return 1; | 
 | 4172 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4173 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 |  | 
 | 4175 | /* | 
 | 4176 |  * PCM stuff | 
 | 4177 |  */ | 
 | 4178 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, | 
 | 4179 | 				      struct hda_codec *codec, | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4180 | 				      struct snd_pcm_substream *substream) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | { | 
 | 4182 | 	return 0; | 
 | 4183 | } | 
 | 4184 |  | 
 | 4185 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, | 
 | 4186 | 				   struct hda_codec *codec, | 
 | 4187 | 				   unsigned int stream_tag, | 
 | 4188 | 				   unsigned int format, | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4189 | 				   struct snd_pcm_substream *substream) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | { | 
 | 4191 | 	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); | 
 | 4192 | 	return 0; | 
 | 4193 | } | 
 | 4194 |  | 
 | 4195 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, | 
 | 4196 | 				   struct hda_codec *codec, | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4197 | 				   struct snd_pcm_substream *substream) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | { | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4199 | 	snd_hda_codec_cleanup_stream(codec, hinfo->nid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | 	return 0; | 
 | 4201 | } | 
 | 4202 |  | 
| Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4203 | static int set_pcm_default_values(struct hda_codec *codec, | 
 | 4204 | 				  struct hda_pcm_stream *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | { | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4206 | 	int err; | 
 | 4207 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4208 | 	/* query support PCM information from the given NID */ | 
 | 4209 | 	if (info->nid && (!info->rates || !info->formats)) { | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4210 | 		err = snd_hda_query_supported_pcm(codec, info->nid, | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4211 | 				info->rates ? NULL : &info->rates, | 
 | 4212 | 				info->formats ? NULL : &info->formats, | 
 | 4213 | 				info->maxbps ? NULL : &info->maxbps); | 
| Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4214 | 		if (err < 0) | 
 | 4215 | 			return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | 	} | 
 | 4217 | 	if (info->ops.open == NULL) | 
 | 4218 | 		info->ops.open = hda_pcm_default_open_close; | 
 | 4219 | 	if (info->ops.close == NULL) | 
 | 4220 | 		info->ops.close = hda_pcm_default_open_close; | 
 | 4221 | 	if (info->ops.prepare == NULL) { | 
| Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4222 | 		if (snd_BUG_ON(!info->nid)) | 
 | 4223 | 			return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | 		info->ops.prepare = hda_pcm_default_prepare; | 
 | 4225 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | 	if (info->ops.cleanup == NULL) { | 
| Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4227 | 		if (snd_BUG_ON(!info->nid)) | 
 | 4228 | 			return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | 		info->ops.cleanup = hda_pcm_default_cleanup; | 
 | 4230 | 	} | 
 | 4231 | 	return 0; | 
 | 4232 | } | 
 | 4233 |  | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4234 | /* | 
 | 4235 |  * codec prepare/cleanup entries | 
 | 4236 |  */ | 
 | 4237 | int snd_hda_codec_prepare(struct hda_codec *codec, | 
 | 4238 | 			  struct hda_pcm_stream *hinfo, | 
 | 4239 | 			  unsigned int stream, | 
 | 4240 | 			  unsigned int format, | 
 | 4241 | 			  struct snd_pcm_substream *substream) | 
 | 4242 | { | 
 | 4243 | 	int ret; | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4244 | 	mutex_lock(&codec->bus->prepare_mutex); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4245 | 	ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); | 
 | 4246 | 	if (ret >= 0) | 
 | 4247 | 		purify_inactive_streams(codec); | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4248 | 	mutex_unlock(&codec->bus->prepare_mutex); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4249 | 	return ret; | 
 | 4250 | } | 
 | 4251 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); | 
 | 4252 |  | 
 | 4253 | void snd_hda_codec_cleanup(struct hda_codec *codec, | 
 | 4254 | 			   struct hda_pcm_stream *hinfo, | 
 | 4255 | 			   struct snd_pcm_substream *substream) | 
 | 4256 | { | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4257 | 	mutex_lock(&codec->bus->prepare_mutex); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4258 | 	hinfo->ops.cleanup(hinfo, codec, substream); | 
| Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4259 | 	mutex_unlock(&codec->bus->prepare_mutex); | 
| Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4260 | } | 
 | 4261 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); | 
 | 4262 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4263 | /* global */ | 
| Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4264 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { | 
 | 4265 | 	"Audio", "SPDIF", "HDMI", "Modem" | 
 | 4266 | }; | 
 | 4267 |  | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4268 | /* | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4269 |  * get the empty PCM device number to assign | 
| Takashi Iwai | c893622 | 2010-01-28 17:08:53 +0100 | [diff] [blame] | 4270 |  * | 
 | 4271 |  * note the max device number is limited by HDA_MAX_PCMS, currently 10 | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4272 |  */ | 
 | 4273 | static int get_empty_pcm_device(struct hda_bus *bus, int type) | 
 | 4274 | { | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4275 | 	/* audio device indices; not linear to keep compatibility */ | 
 | 4276 | 	static int audio_idx[HDA_PCM_NTYPES][5] = { | 
 | 4277 | 		[HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, | 
 | 4278 | 		[HDA_PCM_TYPE_SPDIF] = { 1, -1 }, | 
| Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4279 | 		[HDA_PCM_TYPE_HDMI]  = { 3, 7, 8, 9, -1 }, | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4280 | 		[HDA_PCM_TYPE_MODEM] = { 6, -1 }, | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4281 | 	}; | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4282 | 	int i; | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4283 |  | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4284 | 	if (type >= HDA_PCM_NTYPES) { | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4285 | 		snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); | 
 | 4286 | 		return -EINVAL; | 
 | 4287 | 	} | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4288 |  | 
 | 4289 | 	for (i = 0; audio_idx[type][i] >= 0 ; i++) | 
 | 4290 | 		if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) | 
 | 4291 | 			return audio_idx[type][i]; | 
 | 4292 |  | 
| Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4293 | 	/* non-fixed slots starting from 10 */ | 
 | 4294 | 	for (i = 10; i < 32; i++) { | 
 | 4295 | 		if (!test_and_set_bit(i, bus->pcm_dev_bits)) | 
 | 4296 | 			return i; | 
 | 4297 | 	} | 
 | 4298 |  | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4299 | 	snd_printk(KERN_WARNING "Too many %s devices\n", | 
 | 4300 | 		snd_hda_pcm_type_name[type]); | 
| Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4301 | 	return -EAGAIN; | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4302 | } | 
 | 4303 |  | 
 | 4304 | /* | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4305 |  * attach a new PCM stream | 
 | 4306 |  */ | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4307 | static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4308 | { | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4309 | 	struct hda_bus *bus = codec->bus; | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4310 | 	struct hda_pcm_stream *info; | 
 | 4311 | 	int stream, err; | 
 | 4312 |  | 
| Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4313 | 	if (snd_BUG_ON(!pcm->name)) | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4314 | 		return -EINVAL; | 
 | 4315 | 	for (stream = 0; stream < 2; stream++) { | 
 | 4316 | 		info = &pcm->stream[stream]; | 
 | 4317 | 		if (info->substreams) { | 
 | 4318 | 			err = set_pcm_default_values(codec, info); | 
 | 4319 | 			if (err < 0) | 
 | 4320 | 				return err; | 
 | 4321 | 		} | 
 | 4322 | 	} | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4323 | 	return bus->ops.attach_pcm(bus, codec, pcm); | 
| Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4324 | } | 
 | 4325 |  | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4326 | /* assign all PCMs of the given codec */ | 
 | 4327 | int snd_hda_codec_build_pcms(struct hda_codec *codec) | 
 | 4328 | { | 
 | 4329 | 	unsigned int pcm; | 
 | 4330 | 	int err; | 
 | 4331 |  | 
 | 4332 | 	if (!codec->num_pcms) { | 
 | 4333 | 		if (!codec->patch_ops.build_pcms) | 
 | 4334 | 			return 0; | 
 | 4335 | 		err = codec->patch_ops.build_pcms(codec); | 
| Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4336 | 		if (err < 0) { | 
 | 4337 | 			printk(KERN_ERR "hda_codec: cannot build PCMs" | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4338 | 			       "for #%d (error %d)\n", codec->addr, err); | 
| Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4339 | 			err = snd_hda_codec_reset(codec); | 
 | 4340 | 			if (err < 0) { | 
 | 4341 | 				printk(KERN_ERR | 
 | 4342 | 				       "hda_codec: cannot revert codec\n"); | 
 | 4343 | 				return err; | 
 | 4344 | 			} | 
 | 4345 | 		} | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4346 | 	} | 
 | 4347 | 	for (pcm = 0; pcm < codec->num_pcms; pcm++) { | 
 | 4348 | 		struct hda_pcm *cpcm = &codec->pcm_info[pcm]; | 
 | 4349 | 		int dev; | 
 | 4350 |  | 
 | 4351 | 		if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) | 
| Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4352 | 			continue; /* no substreams assigned */ | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4353 |  | 
 | 4354 | 		if (!cpcm->pcm) { | 
 | 4355 | 			dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); | 
 | 4356 | 			if (dev < 0) | 
| Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4357 | 				continue; /* no fatal error */ | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4358 | 			cpcm->device = dev; | 
 | 4359 | 			err = snd_hda_attach_pcm(codec, cpcm); | 
| Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4360 | 			if (err < 0) { | 
 | 4361 | 				printk(KERN_ERR "hda_codec: cannot attach " | 
 | 4362 | 				       "PCM stream %d for codec #%d\n", | 
 | 4363 | 				       dev, codec->addr); | 
 | 4364 | 				continue; /* no fatal error */ | 
 | 4365 | 			} | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4366 | 		} | 
 | 4367 | 	} | 
 | 4368 | 	return 0; | 
 | 4369 | } | 
 | 4370 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | /** | 
 | 4372 |  * snd_hda_build_pcms - build PCM information | 
 | 4373 |  * @bus: the BUS | 
 | 4374 |  * | 
 | 4375 |  * Create PCM information for each codec included in the bus. | 
 | 4376 |  * | 
 | 4377 |  * The build_pcms codec patch is requested to set up codec->num_pcms and | 
 | 4378 |  * codec->pcm_info properly.  The array is referred by the top-level driver | 
 | 4379 |  * to create its PCM instances. | 
 | 4380 |  * The allocated codec->pcm_info should be released in codec->patch_ops.free | 
 | 4381 |  * callback. | 
 | 4382 |  * | 
 | 4383 |  * At least, substreams, channels_min and channels_max must be filled for | 
 | 4384 |  * each stream.  substreams = 0 indicates that the stream doesn't exist. | 
 | 4385 |  * When rates and/or formats are zero, the supported values are queried | 
 | 4386 |  * from the given nid.  The nid is used also by the default ops.prepare | 
 | 4387 |  * and ops.cleanup callbacks. | 
 | 4388 |  * | 
 | 4389 |  * The driver needs to call ops.open in its open callback.  Similarly, | 
 | 4390 |  * ops.close is supposed to be called in the close callback. | 
 | 4391 |  * ops.prepare should be called in the prepare or hw_params callback | 
 | 4392 |  * with the proper parameters for set up. | 
 | 4393 |  * ops.cleanup should be called in hw_free for clean up of streams. | 
 | 4394 |  * | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4395 |  * This function returns 0 if successful, or a negative error code. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 |  */ | 
| Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4397 | int snd_hda_build_pcms(struct hda_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4399 | 	struct hda_codec *codec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4401 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
| Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4402 | 		int err = snd_hda_codec_build_pcms(codec); | 
 | 4403 | 		if (err < 0) | 
 | 4404 | 			return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | 	} | 
 | 4406 | 	return 0; | 
 | 4407 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4408 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | /** | 
 | 4411 |  * snd_hda_check_board_config - compare the current codec with the config table | 
 | 4412 |  * @codec: the HDA codec | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4413 |  * @num_configs: number of config enums | 
 | 4414 |  * @models: array of model name strings | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 |  * @tbl: configuration table, terminated by null entries | 
 | 4416 |  * | 
 | 4417 |  * Compares the modelname or PCI subsystem id of the current codec with the | 
 | 4418 |  * given configuration table.  If a matching entry is found, returns its | 
 | 4419 |  * config value (supposed to be 0 or positive). | 
 | 4420 |  * | 
 | 4421 |  * If no entries are matching, the function returns a negative value. | 
 | 4422 |  */ | 
| Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4423 | int snd_hda_check_board_config(struct hda_codec *codec, | 
| Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4424 | 			       int num_configs, const char * const *models, | 
| Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4425 | 			       const struct snd_pci_quirk *tbl) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | { | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4427 | 	if (codec->modelname && models) { | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4428 | 		int i; | 
 | 4429 | 		for (i = 0; i < num_configs; i++) { | 
 | 4430 | 			if (models[i] && | 
| Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4431 | 			    !strcmp(codec->modelname, models[i])) { | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4432 | 				snd_printd(KERN_INFO "hda_codec: model '%s' is " | 
 | 4433 | 					   "selected\n", models[i]); | 
 | 4434 | 				return i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | 			} | 
 | 4436 | 		} | 
 | 4437 | 	} | 
 | 4438 |  | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4439 | 	if (!codec->bus->pci || !tbl) | 
 | 4440 | 		return -1; | 
 | 4441 |  | 
 | 4442 | 	tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); | 
 | 4443 | 	if (!tbl) | 
 | 4444 | 		return -1; | 
 | 4445 | 	if (tbl->value >= 0 && tbl->value < num_configs) { | 
| Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4446 | #ifdef CONFIG_SND_DEBUG_VERBOSE | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4447 | 		char tmp[10]; | 
 | 4448 | 		const char *model = NULL; | 
 | 4449 | 		if (models) | 
 | 4450 | 			model = models[tbl->value]; | 
 | 4451 | 		if (!model) { | 
 | 4452 | 			sprintf(tmp, "#%d", tbl->value); | 
 | 4453 | 			model = tmp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | 		} | 
| Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4455 | 		snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " | 
 | 4456 | 			    "for config %x:%x (%s)\n", | 
 | 4457 | 			    model, tbl->subvendor, tbl->subdevice, | 
 | 4458 | 			    (tbl->name ? tbl->name : "Unknown device")); | 
 | 4459 | #endif | 
 | 4460 | 		return tbl->value; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | 	} | 
 | 4462 | 	return -1; | 
 | 4463 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4464 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 |  | 
 | 4466 | /** | 
| Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4467 |  * snd_hda_check_board_codec_sid_config - compare the current codec | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4468 | 					subsystem ID with the | 
 | 4469 | 					config table | 
| Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4470 |  | 
 | 4471 | 	   This is important for Gateway notebooks with SB450 HDA Audio | 
 | 4472 | 	   where the vendor ID of the PCI device is: | 
 | 4473 | 		ATI Technologies Inc SB450 HDA Audio [1002:437b] | 
 | 4474 | 	   and the vendor/subvendor are found only at the codec. | 
 | 4475 |  | 
 | 4476 |  * @codec: the HDA codec | 
 | 4477 |  * @num_configs: number of config enums | 
 | 4478 |  * @models: array of model name strings | 
 | 4479 |  * @tbl: configuration table, terminated by null entries | 
 | 4480 |  * | 
 | 4481 |  * Compares the modelname or PCI subsystem id of the current codec with the | 
 | 4482 |  * given configuration table.  If a matching entry is found, returns its | 
 | 4483 |  * config value (supposed to be 0 or positive). | 
 | 4484 |  * | 
 | 4485 |  * If no entries are matching, the function returns a negative value. | 
 | 4486 |  */ | 
 | 4487 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, | 
| Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4488 | 			       int num_configs, const char * const *models, | 
| Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4489 | 			       const struct snd_pci_quirk *tbl) | 
 | 4490 | { | 
 | 4491 | 	const struct snd_pci_quirk *q; | 
 | 4492 |  | 
 | 4493 | 	/* Search for codec ID */ | 
 | 4494 | 	for (q = tbl; q->subvendor; q++) { | 
| Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4495 | 		unsigned int mask = 0xffff0000 | q->subdevice_mask; | 
 | 4496 | 		unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; | 
 | 4497 | 		if ((codec->subsystem_id & mask) == id) | 
| Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4498 | 			break; | 
 | 4499 | 	} | 
 | 4500 |  | 
 | 4501 | 	if (!q->subvendor) | 
 | 4502 | 		return -1; | 
 | 4503 |  | 
 | 4504 | 	tbl = q; | 
 | 4505 |  | 
 | 4506 | 	if (tbl->value >= 0 && tbl->value < num_configs) { | 
| Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4507 | #ifdef CONFIG_SND_DEBUG_VERBOSE | 
| Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4508 | 		char tmp[10]; | 
 | 4509 | 		const char *model = NULL; | 
 | 4510 | 		if (models) | 
 | 4511 | 			model = models[tbl->value]; | 
 | 4512 | 		if (!model) { | 
 | 4513 | 			sprintf(tmp, "#%d", tbl->value); | 
 | 4514 | 			model = tmp; | 
 | 4515 | 		} | 
 | 4516 | 		snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " | 
 | 4517 | 			    "for config %x:%x (%s)\n", | 
 | 4518 | 			    model, tbl->subvendor, tbl->subdevice, | 
 | 4519 | 			    (tbl->name ? tbl->name : "Unknown device")); | 
 | 4520 | #endif | 
 | 4521 | 		return tbl->value; | 
 | 4522 | 	} | 
 | 4523 | 	return -1; | 
 | 4524 | } | 
 | 4525 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); | 
 | 4526 |  | 
 | 4527 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 |  * snd_hda_add_new_ctls - create controls from the array | 
 | 4529 |  * @codec: the HDA codec | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4530 |  * @knew: the array of struct snd_kcontrol_new | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 |  * | 
 | 4532 |  * This helper function creates and add new controls in the given array. | 
 | 4533 |  * The array must be terminated with an empty entry as terminator. | 
 | 4534 |  * | 
 | 4535 |  * Returns 0 if successful, or a negative error code. | 
 | 4536 |  */ | 
| Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4537 | int snd_hda_add_new_ctls(struct hda_codec *codec, | 
 | 4538 | 			 const struct snd_kcontrol_new *knew) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { | 
| Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4540 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 |  | 
 | 4542 | 	for (; knew->name; knew++) { | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4543 | 		struct snd_kcontrol *kctl; | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4544 | 		int addr = 0, idx = 0; | 
| Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4545 | 		if (knew->iface == -1)	/* skip this codec private value */ | 
 | 4546 | 			continue; | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4547 | 		for (;;) { | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4548 | 			kctl = snd_ctl_new1(knew, codec); | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4549 | 			if (!kctl) | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4550 | 				return -ENOMEM; | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4551 | 			if (addr > 0) | 
 | 4552 | 				kctl->id.device = addr; | 
 | 4553 | 			if (idx > 0) | 
 | 4554 | 				kctl->id.index = idx; | 
| Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4555 | 			err = snd_hda_ctl_add(codec, 0, kctl); | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4556 | 			if (!err) | 
 | 4557 | 				break; | 
 | 4558 | 			/* try first with another device index corresponding to | 
 | 4559 | 			 * the codec addr; if it still fails (or it's the | 
 | 4560 | 			 * primary codec), then try another control index | 
 | 4561 | 			 */ | 
 | 4562 | 			if (!addr && codec->addr) | 
 | 4563 | 				addr = codec->addr; | 
 | 4564 | 			else if (!idx && !knew->index) { | 
 | 4565 | 				idx = find_empty_mixer_ctl_idx(codec, | 
| Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4566 | 							       knew->name, 0); | 
| Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4567 | 				if (idx <= 0) | 
 | 4568 | 					return err; | 
 | 4569 | 			} else | 
| Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4570 | 				return err; | 
 | 4571 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | 	} | 
 | 4573 | 	return 0; | 
 | 4574 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4575 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 |  | 
| Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4577 | #ifdef CONFIG_PM | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4578 | static void hda_power_work(struct work_struct *work) | 
 | 4579 | { | 
 | 4580 | 	struct hda_codec *codec = | 
 | 4581 | 		container_of(work, struct hda_codec, power_work.work); | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4582 | 	struct hda_bus *bus = codec->bus; | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4583 | 	unsigned int state; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4584 |  | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4585 | 	spin_lock(&codec->power_lock); | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4586 | 	if (codec->power_transition > 0) { /* during power-up sequence? */ | 
 | 4587 | 		spin_unlock(&codec->power_lock); | 
 | 4588 | 		return; | 
 | 4589 | 	} | 
| Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4590 | 	if (!codec->power_on || codec->power_count) { | 
 | 4591 | 		codec->power_transition = 0; | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4592 | 		spin_unlock(&codec->power_lock); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4593 | 		return; | 
| Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4594 | 	} | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4595 | 	spin_unlock(&codec->power_lock); | 
 | 4596 |  | 
| Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4597 | 	state = hda_call_codec_suspend(codec, true); | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4598 | 	codec->pm_down_notified = 0; | 
 | 4599 | 	if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) { | 
 | 4600 | 		codec->pm_down_notified = 1; | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4601 | 		hda_call_pm_notify(bus, false); | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4602 | 	} | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4603 | } | 
 | 4604 |  | 
 | 4605 | static void hda_keep_power_on(struct hda_codec *codec) | 
 | 4606 | { | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4607 | 	spin_lock(&codec->power_lock); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4608 | 	codec->power_count++; | 
 | 4609 | 	codec->power_on = 1; | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4610 | 	codec->power_jiffies = jiffies; | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4611 | 	spin_unlock(&codec->power_lock); | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4612 | } | 
 | 4613 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4614 | /* update the power on/off account with the current jiffies */ | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4615 | void snd_hda_update_power_acct(struct hda_codec *codec) | 
 | 4616 | { | 
 | 4617 | 	unsigned long delta = jiffies - codec->power_jiffies; | 
 | 4618 | 	if (codec->power_on) | 
 | 4619 | 		codec->power_on_acct += delta; | 
 | 4620 | 	else | 
 | 4621 | 		codec->power_off_acct += delta; | 
 | 4622 | 	codec->power_jiffies += delta; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4623 | } | 
 | 4624 |  | 
| Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4625 | /* Transition to powered up, if wait_power_down then wait for a pending | 
 | 4626 |  * transition to D3 to complete. A pending D3 transition is indicated | 
 | 4627 |  * with power_transition == -1. */ | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4628 | /* call this with codec->power_lock held! */ | 
| Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4629 | static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4630 | { | 
| Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4631 | 	struct hda_bus *bus = codec->bus; | 
 | 4632 |  | 
| Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4633 | 	/* Return if power_on or transitioning to power_on, unless currently | 
 | 4634 | 	 * powering down. */ | 
 | 4635 | 	if ((codec->power_on || codec->power_transition > 0) && | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4636 | 	    !(wait_power_down && codec->power_transition < 0)) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4637 | 		return; | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4638 | 	spin_unlock(&codec->power_lock); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4639 |  | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4640 | 	cancel_delayed_work_sync(&codec->power_work); | 
 | 4641 |  | 
 | 4642 | 	spin_lock(&codec->power_lock); | 
| Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4643 | 	/* If the power down delayed work was cancelled above before starting, | 
 | 4644 | 	 * then there is no need to go through power up here. | 
 | 4645 | 	 */ | 
 | 4646 | 	if (codec->power_on) { | 
| Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 4647 | 		if (codec->power_transition < 0) | 
 | 4648 | 			codec->power_transition = 0; | 
| Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4649 | 		return; | 
 | 4650 | 	} | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4651 |  | 
| Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4652 | 	trace_hda_power_up(codec); | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4653 | 	snd_hda_update_power_acct(codec); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4654 | 	codec->power_on = 1; | 
| Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4655 | 	codec->power_jiffies = jiffies; | 
| Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4656 | 	codec->power_transition = 1; /* avoid reentrance */ | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4657 | 	spin_unlock(&codec->power_lock); | 
 | 4658 |  | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4659 | 	if (codec->pm_down_notified) { | 
 | 4660 | 		codec->pm_down_notified = 0; | 
| Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4661 | 		hda_call_pm_notify(bus, true); | 
| Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4662 | 	} | 
 | 4663 |  | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4664 | 	hda_call_codec_resume(codec); | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4665 |  | 
 | 4666 | 	spin_lock(&codec->power_lock); | 
| Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4667 | 	codec->power_transition = 0; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4668 | } | 
| Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4669 |  | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 4670 | #define power_save(codec)	\ | 
 | 4671 | 	((codec)->bus->power_save ? *(codec)->bus->power_save : 0) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4672 |  | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4673 | /* Transition to powered down */ | 
 | 4674 | static void __snd_hda_power_down(struct hda_codec *codec) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4675 | { | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4676 | 	if (!codec->power_on || codec->power_count || codec->power_transition) | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4677 | 		return; | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4678 |  | 
| Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4679 | 	if (power_save(codec)) { | 
| Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4680 | 		codec->power_transition = -1; /* avoid reentrance */ | 
| Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 4681 | 		queue_delayed_work(codec->bus->workq, &codec->power_work, | 
| Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4682 | 				msecs_to_jiffies(power_save(codec) * 1000)); | 
| Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4683 | 	} | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4684 | } | 
 | 4685 |  | 
 | 4686 | /** | 
 | 4687 |  * snd_hda_power_save - Power-up/down/sync the codec | 
 | 4688 |  * @codec: HD-audio codec | 
 | 4689 |  * @delta: the counter delta to change | 
 | 4690 |  * | 
 | 4691 |  * Change the power-up counter via @delta, and power up or down the hardware | 
 | 4692 |  * appropriately.  For the power-down, queue to the delayed action. | 
 | 4693 |  * Passing zero to @delta means to synchronize the power state. | 
 | 4694 |  */ | 
 | 4695 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) | 
 | 4696 | { | 
 | 4697 | 	spin_lock(&codec->power_lock); | 
 | 4698 | 	codec->power_count += delta; | 
 | 4699 | 	trace_hda_power_count(codec); | 
 | 4700 | 	if (delta > 0) | 
 | 4701 | 		__snd_hda_power_up(codec, d3wait); | 
 | 4702 | 	else | 
 | 4703 | 		__snd_hda_power_down(codec); | 
| Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4704 | 	spin_unlock(&codec->power_lock); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4705 | } | 
| Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4706 | EXPORT_SYMBOL_HDA(snd_hda_power_save); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4707 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4708 | /** | 
 | 4709 |  * snd_hda_check_amp_list_power - Check the amp list and update the power | 
 | 4710 |  * @codec: HD-audio codec | 
 | 4711 |  * @check: the object containing an AMP list and the status | 
 | 4712 |  * @nid: NID to check / update | 
 | 4713 |  * | 
 | 4714 |  * Check whether the given NID is in the amp list.  If it's in the list, | 
 | 4715 |  * check the current AMP status, and update the the power-status according | 
 | 4716 |  * to the mute status. | 
 | 4717 |  * | 
 | 4718 |  * This function is supposed to be set or called from the check_power_status | 
 | 4719 |  * patch ops. | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4720 |  */ | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4721 | int snd_hda_check_amp_list_power(struct hda_codec *codec, | 
 | 4722 | 				 struct hda_loopback_check *check, | 
 | 4723 | 				 hda_nid_t nid) | 
 | 4724 | { | 
| Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4725 | 	const struct hda_amp_list *p; | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4726 | 	int ch, v; | 
 | 4727 |  | 
 | 4728 | 	if (!check->amplist) | 
 | 4729 | 		return 0; | 
 | 4730 | 	for (p = check->amplist; p->nid; p++) { | 
 | 4731 | 		if (p->nid == nid) | 
 | 4732 | 			break; | 
 | 4733 | 	} | 
 | 4734 | 	if (!p->nid) | 
 | 4735 | 		return 0; /* nothing changed */ | 
 | 4736 |  | 
 | 4737 | 	for (p = check->amplist; p->nid; p++) { | 
 | 4738 | 		for (ch = 0; ch < 2; ch++) { | 
 | 4739 | 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, | 
 | 4740 | 						   p->idx); | 
 | 4741 | 			if (!(v & HDA_AMP_MUTE) && v > 0) { | 
 | 4742 | 				if (!check->power_on) { | 
 | 4743 | 					check->power_on = 1; | 
 | 4744 | 					snd_hda_power_up(codec); | 
 | 4745 | 				} | 
 | 4746 | 				return 1; | 
 | 4747 | 			} | 
 | 4748 | 		} | 
 | 4749 | 	} | 
 | 4750 | 	if (check->power_on) { | 
 | 4751 | 		check->power_on = 0; | 
 | 4752 | 		snd_hda_power_down(codec); | 
 | 4753 | 	} | 
 | 4754 | 	return 0; | 
 | 4755 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4756 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); | 
| Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4757 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 |  | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4759 | /* | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4760 |  * Channel mode helper | 
 | 4761 |  */ | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4762 |  | 
 | 4763 | /** | 
 | 4764 |  * snd_hda_ch_mode_info - Info callback helper for the channel mode enum | 
 | 4765 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4766 | int snd_hda_ch_mode_info(struct hda_codec *codec, | 
 | 4767 | 			 struct snd_ctl_elem_info *uinfo, | 
 | 4768 | 			 const struct hda_channel_mode *chmode, | 
 | 4769 | 			 int num_chmodes) | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4770 | { | 
 | 4771 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | 
 | 4772 | 	uinfo->count = 1; | 
 | 4773 | 	uinfo->value.enumerated.items = num_chmodes; | 
 | 4774 | 	if (uinfo->value.enumerated.item >= num_chmodes) | 
 | 4775 | 		uinfo->value.enumerated.item = num_chmodes - 1; | 
 | 4776 | 	sprintf(uinfo->value.enumerated.name, "%dch", | 
 | 4777 | 		chmode[uinfo->value.enumerated.item].channels); | 
 | 4778 | 	return 0; | 
 | 4779 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4780 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4781 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4782 | /** | 
 | 4783 |  * snd_hda_ch_mode_get - Get callback helper for the channel mode enum | 
 | 4784 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4785 | int snd_hda_ch_mode_get(struct hda_codec *codec, | 
 | 4786 | 			struct snd_ctl_elem_value *ucontrol, | 
 | 4787 | 			const struct hda_channel_mode *chmode, | 
 | 4788 | 			int num_chmodes, | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4789 | 			int max_channels) | 
 | 4790 | { | 
 | 4791 | 	int i; | 
 | 4792 |  | 
 | 4793 | 	for (i = 0; i < num_chmodes; i++) { | 
 | 4794 | 		if (max_channels == chmode[i].channels) { | 
 | 4795 | 			ucontrol->value.enumerated.item[0] = i; | 
 | 4796 | 			break; | 
 | 4797 | 		} | 
 | 4798 | 	} | 
 | 4799 | 	return 0; | 
 | 4800 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4801 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4802 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4803 | /** | 
 | 4804 |  * snd_hda_ch_mode_put - Put callback helper for the channel mode enum | 
 | 4805 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4806 | int snd_hda_ch_mode_put(struct hda_codec *codec, | 
 | 4807 | 			struct snd_ctl_elem_value *ucontrol, | 
 | 4808 | 			const struct hda_channel_mode *chmode, | 
 | 4809 | 			int num_chmodes, | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4810 | 			int *max_channelsp) | 
 | 4811 | { | 
 | 4812 | 	unsigned int mode; | 
 | 4813 |  | 
 | 4814 | 	mode = ucontrol->value.enumerated.item[0]; | 
| Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 4815 | 	if (mode >= num_chmodes) | 
 | 4816 | 		return -EINVAL; | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4817 | 	if (*max_channelsp == chmode[mode].channels) | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4818 | 		return 0; | 
 | 4819 | 	/* change the current channel setting */ | 
 | 4820 | 	*max_channelsp = chmode[mode].channels; | 
 | 4821 | 	if (chmode[mode].sequence) | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4822 | 		snd_hda_sequence_write_cache(codec, chmode[mode].sequence); | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4823 | 	return 1; | 
 | 4824 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4825 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); | 
| Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4826 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | /* | 
 | 4828 |  * input MUX helper | 
 | 4829 |  */ | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4830 |  | 
 | 4831 | /** | 
 | 4832 |  * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum | 
 | 4833 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4834 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, | 
 | 4835 | 			   struct snd_ctl_elem_info *uinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | { | 
 | 4837 | 	unsigned int index; | 
 | 4838 |  | 
 | 4839 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | 
 | 4840 | 	uinfo->count = 1; | 
 | 4841 | 	uinfo->value.enumerated.items = imux->num_items; | 
| Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4842 | 	if (!imux->num_items) | 
 | 4843 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | 	index = uinfo->value.enumerated.item; | 
 | 4845 | 	if (index >= imux->num_items) | 
 | 4846 | 		index = imux->num_items - 1; | 
 | 4847 | 	strcpy(uinfo->value.enumerated.name, imux->items[index].label); | 
 | 4848 | 	return 0; | 
 | 4849 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4850 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4852 | /** | 
 | 4853 |  * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum | 
 | 4854 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4855 | int snd_hda_input_mux_put(struct hda_codec *codec, | 
 | 4856 | 			  const struct hda_input_mux *imux, | 
 | 4857 | 			  struct snd_ctl_elem_value *ucontrol, | 
 | 4858 | 			  hda_nid_t nid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | 			  unsigned int *cur_val) | 
 | 4860 | { | 
 | 4861 | 	unsigned int idx; | 
 | 4862 |  | 
| Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4863 | 	if (!imux->num_items) | 
 | 4864 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | 	idx = ucontrol->value.enumerated.item[0]; | 
 | 4866 | 	if (idx >= imux->num_items) | 
 | 4867 | 		idx = imux->num_items - 1; | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4868 | 	if (*cur_val == idx) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | 		return 0; | 
| Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4870 | 	snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, | 
 | 4871 | 				  imux->items[idx].index); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | 	*cur_val = idx; | 
 | 4873 | 	return 1; | 
 | 4874 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4875 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 |  | 
 | 4877 |  | 
 | 4878 | /* | 
| Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4879 |  * process kcontrol info callback of a simple string enum array | 
 | 4880 |  * when @num_items is 0 or @texts is NULL, assume a boolean enum array | 
 | 4881 |  */ | 
 | 4882 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, | 
 | 4883 | 			     struct snd_ctl_elem_info *uinfo, | 
 | 4884 | 			     int num_items, const char * const *texts) | 
 | 4885 | { | 
 | 4886 | 	static const char * const texts_default[] = { | 
 | 4887 | 		"Disabled", "Enabled" | 
 | 4888 | 	}; | 
 | 4889 |  | 
 | 4890 | 	if (!texts || !num_items) { | 
 | 4891 | 		num_items = 2; | 
 | 4892 | 		texts = texts_default; | 
 | 4893 | 	} | 
 | 4894 |  | 
 | 4895 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | 
 | 4896 | 	uinfo->count = 1; | 
 | 4897 | 	uinfo->value.enumerated.items = num_items; | 
 | 4898 | 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) | 
 | 4899 | 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; | 
 | 4900 | 	strcpy(uinfo->value.enumerated.name, | 
 | 4901 | 	       texts[uinfo->value.enumerated.item]); | 
 | 4902 | 	return 0; | 
 | 4903 | } | 
 | 4904 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); | 
 | 4905 |  | 
 | 4906 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 |  * Multi-channel / digital-out PCM helper functions | 
 | 4908 |  */ | 
 | 4909 |  | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4910 | /* setup SPDIF output stream */ | 
 | 4911 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, | 
 | 4912 | 				 unsigned int stream_tag, unsigned int format) | 
 | 4913 | { | 
| Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4914 | 	struct hda_spdif_out *spdif; | 
 | 4915 | 	unsigned int curr_fmt; | 
 | 4916 | 	bool reset; | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4917 |  | 
| Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4918 | 	spdif = snd_hda_spdif_out_of_nid(codec, nid); | 
 | 4919 | 	curr_fmt = snd_hda_codec_read(codec, nid, 0, | 
 | 4920 | 				      AC_VERB_GET_STREAM_FORMAT, 0); | 
 | 4921 | 	reset = codec->spdif_status_reset && | 
 | 4922 | 		(spdif->ctls & AC_DIG1_ENABLE) && | 
 | 4923 | 		curr_fmt != format; | 
 | 4924 |  | 
 | 4925 | 	/* turn off SPDIF if needed; otherwise the IEC958 bits won't be | 
 | 4926 | 	   updated */ | 
 | 4927 | 	if (reset) | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4928 | 		set_dig_out_convert(codec, nid, | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4929 | 				    spdif->ctls & ~AC_DIG1_ENABLE & 0xff, | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4930 | 				    -1); | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4931 | 	snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4932 | 	if (codec->slave_dig_outs) { | 
| Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4933 | 		const hda_nid_t *d; | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4934 | 		for (d = codec->slave_dig_outs; *d; d++) | 
 | 4935 | 			snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, | 
 | 4936 | 						   format); | 
| Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4937 | 	} | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4938 | 	/* turn on again (if needed) */ | 
| Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4939 | 	if (reset) | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4940 | 		set_dig_out_convert(codec, nid, | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4941 | 				    spdif->ctls & 0xff, -1); | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4942 | } | 
| Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4943 |  | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4944 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) | 
 | 4945 | { | 
 | 4946 | 	snd_hda_codec_cleanup_stream(codec, nid); | 
 | 4947 | 	if (codec->slave_dig_outs) { | 
| Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4948 | 		const hda_nid_t *d; | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4949 | 		for (d = codec->slave_dig_outs; *d; d++) | 
 | 4950 | 			snd_hda_codec_cleanup_stream(codec, *d); | 
 | 4951 | 	} | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4952 | } | 
 | 4953 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4954 | /** | 
 | 4955 |  * snd_hda_bus_reboot_notify - call the reboot notifier of each codec | 
 | 4956 |  * @bus: HD-audio bus | 
 | 4957 |  */ | 
| Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4958 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) | 
 | 4959 | { | 
 | 4960 | 	struct hda_codec *codec; | 
 | 4961 |  | 
 | 4962 | 	if (!bus) | 
 | 4963 | 		return; | 
 | 4964 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
| Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 4965 | 		if (hda_codec_is_power_on(codec) && | 
 | 4966 | 		    codec->patch_ops.reboot_notify) | 
| Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4967 | 			codec->patch_ops.reboot_notify(codec); | 
 | 4968 | 	} | 
 | 4969 | } | 
| Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 4970 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); | 
| Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4971 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4972 | /** | 
 | 4973 |  * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4975 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, | 
 | 4976 | 			       struct hda_multi_out *mout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | { | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4978 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 4979 | 	if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) | 
 | 4980 | 		/* already opened as analog dup; reset it once */ | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4981 | 		cleanup_dig_out_stream(codec, mout->dig_out_nid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | 	mout->dig_out_used = HDA_DIG_EXCLUSIVE; | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4983 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | 	return 0; | 
 | 4985 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4986 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4988 | /** | 
 | 4989 |  * snd_hda_multi_out_dig_prepare - prepare the digital out stream | 
 | 4990 |  */ | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4991 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, | 
 | 4992 | 				  struct hda_multi_out *mout, | 
 | 4993 | 				  unsigned int stream_tag, | 
 | 4994 | 				  unsigned int format, | 
 | 4995 | 				  struct snd_pcm_substream *substream) | 
 | 4996 | { | 
 | 4997 | 	mutex_lock(&codec->spdif_mutex); | 
 | 4998 | 	setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); | 
 | 4999 | 	mutex_unlock(&codec->spdif_mutex); | 
 | 5000 | 	return 0; | 
 | 5001 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5002 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5003 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5004 | /** | 
 | 5005 |  * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream | 
 | 5006 |  */ | 
| Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5007 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, | 
 | 5008 | 				  struct hda_multi_out *mout) | 
 | 5009 | { | 
 | 5010 | 	mutex_lock(&codec->spdif_mutex); | 
 | 5011 | 	cleanup_dig_out_stream(codec, mout->dig_out_nid); | 
 | 5012 | 	mutex_unlock(&codec->spdif_mutex); | 
 | 5013 | 	return 0; | 
 | 5014 | } | 
 | 5015 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); | 
 | 5016 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5017 | /** | 
 | 5018 |  * snd_hda_multi_out_dig_close - release the digital out stream | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5020 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, | 
 | 5021 | 				struct hda_multi_out *mout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | { | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5023 | 	mutex_lock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | 	mout->dig_out_used = 0; | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5025 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | 	return 0; | 
 | 5027 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5028 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5030 | /** | 
 | 5031 |  * snd_hda_multi_out_analog_open - open analog outputs | 
 | 5032 |  * | 
 | 5033 |  * Open analog outputs and set up the hw-constraints. | 
 | 5034 |  * If the digital outputs can be opened as slave, open the digital | 
 | 5035 |  * outputs, too. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5037 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, | 
 | 5038 | 				  struct hda_multi_out *mout, | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5039 | 				  struct snd_pcm_substream *substream, | 
 | 5040 | 				  struct hda_pcm_stream *hinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | { | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5042 | 	struct snd_pcm_runtime *runtime = substream->runtime; | 
 | 5043 | 	runtime->hw.channels_max = mout->max_channels; | 
 | 5044 | 	if (mout->dig_out_nid) { | 
 | 5045 | 		if (!mout->analog_rates) { | 
 | 5046 | 			mout->analog_rates = hinfo->rates; | 
 | 5047 | 			mout->analog_formats = hinfo->formats; | 
 | 5048 | 			mout->analog_maxbps = hinfo->maxbps; | 
 | 5049 | 		} else { | 
 | 5050 | 			runtime->hw.rates = mout->analog_rates; | 
 | 5051 | 			runtime->hw.formats = mout->analog_formats; | 
 | 5052 | 			hinfo->maxbps = mout->analog_maxbps; | 
 | 5053 | 		} | 
 | 5054 | 		if (!mout->spdif_rates) { | 
 | 5055 | 			snd_hda_query_supported_pcm(codec, mout->dig_out_nid, | 
 | 5056 | 						    &mout->spdif_rates, | 
 | 5057 | 						    &mout->spdif_formats, | 
 | 5058 | 						    &mout->spdif_maxbps); | 
 | 5059 | 		} | 
 | 5060 | 		mutex_lock(&codec->spdif_mutex); | 
 | 5061 | 		if (mout->share_spdif) { | 
| Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5062 | 			if ((runtime->hw.rates & mout->spdif_rates) && | 
 | 5063 | 			    (runtime->hw.formats & mout->spdif_formats)) { | 
 | 5064 | 				runtime->hw.rates &= mout->spdif_rates; | 
 | 5065 | 				runtime->hw.formats &= mout->spdif_formats; | 
 | 5066 | 				if (mout->spdif_maxbps < hinfo->maxbps) | 
 | 5067 | 					hinfo->maxbps = mout->spdif_maxbps; | 
 | 5068 | 			} else { | 
 | 5069 | 				mout->share_spdif = 0; | 
 | 5070 | 				/* FIXME: need notify? */ | 
 | 5071 | 			} | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5072 | 		} | 
| Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5073 | 		mutex_unlock(&codec->spdif_mutex); | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5074 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | 	return snd_pcm_hw_constraint_step(substream->runtime, 0, | 
 | 5076 | 					  SNDRV_PCM_HW_PARAM_CHANNELS, 2); | 
 | 5077 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5078 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5080 | /** | 
 | 5081 |  * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. | 
 | 5082 |  * | 
 | 5083 |  * Set up the i/o for analog out. | 
 | 5084 |  * When the digital out is available, copy the front out to digital out, too. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5086 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, | 
 | 5087 | 				     struct hda_multi_out *mout, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | 				     unsigned int stream_tag, | 
 | 5089 | 				     unsigned int format, | 
| Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5090 | 				     struct snd_pcm_substream *substream) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | { | 
| Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5092 | 	const hda_nid_t *nids = mout->dac_nids; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | 	int chs = substream->runtime->channels; | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5094 | 	struct hda_spdif_out *spdif; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | 	int i; | 
 | 5096 |  | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5097 | 	mutex_lock(&codec->spdif_mutex); | 
| Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5098 | 	spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); | 
| Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5099 | 	if (mout->dig_out_nid && mout->share_spdif && | 
 | 5100 | 	    mout->dig_out_used != HDA_DIG_EXCLUSIVE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | 		if (chs == 2 && | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5102 | 		    snd_hda_is_supported_format(codec, mout->dig_out_nid, | 
 | 5103 | 						format) && | 
| Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5104 | 		    !(spdif->status & IEC958_AES0_NONAUDIO)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | 			mout->dig_out_used = HDA_DIG_ANALOG_DUP; | 
| Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5106 | 			setup_dig_out_stream(codec, mout->dig_out_nid, | 
 | 5107 | 					     stream_tag, format); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | 		} else { | 
 | 5109 | 			mout->dig_out_used = 0; | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5110 | 			cleanup_dig_out_stream(codec, mout->dig_out_nid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | 		} | 
 | 5112 | 	} | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5113 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 |  | 
 | 5115 | 	/* front */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5116 | 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, | 
 | 5117 | 				   0, format); | 
| Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5118 | 	if (!mout->no_share_stream && | 
 | 5119 | 	    mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | 		/* headphone out will just decode front left/right (stereo) */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5121 | 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, | 
 | 5122 | 					   0, format); | 
| Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5123 | 	/* extra outputs copied from front */ | 
| Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5124 | 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) | 
 | 5125 | 		if (!mout->no_share_stream && mout->hp_out_nid[i]) | 
 | 5126 | 			snd_hda_codec_setup_stream(codec, | 
 | 5127 | 						   mout->hp_out_nid[i], | 
 | 5128 | 						   stream_tag, 0, format); | 
| Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5129 | 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) | 
| Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5130 | 		if (!mout->no_share_stream && mout->extra_out_nid[i]) | 
| Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5131 | 			snd_hda_codec_setup_stream(codec, | 
 | 5132 | 						   mout->extra_out_nid[i], | 
 | 5133 | 						   stream_tag, 0, format); | 
 | 5134 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | 	/* surrounds */ | 
 | 5136 | 	for (i = 1; i < mout->num_dacs; i++) { | 
| Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5137 | 		if (chs >= (i + 1) * 2) /* independent out */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5138 | 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag, | 
 | 5139 | 						   i * 2, format); | 
| Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5140 | 		else if (!mout->no_share_stream) /* copy front */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5141 | 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag, | 
 | 5142 | 						   0, format); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | 	} | 
 | 5144 | 	return 0; | 
 | 5145 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5146 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5148 | /** | 
 | 5149 |  * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 |  */ | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5151 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, | 
 | 5152 | 				     struct hda_multi_out *mout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | { | 
| Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5154 | 	const hda_nid_t *nids = mout->dac_nids; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | 	int i; | 
 | 5156 |  | 
 | 5157 | 	for (i = 0; i < mout->num_dacs; i++) | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5158 | 		snd_hda_codec_cleanup_stream(codec, nids[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | 	if (mout->hp_nid) | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5160 | 		snd_hda_codec_cleanup_stream(codec, mout->hp_nid); | 
| Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5161 | 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) | 
 | 5162 | 		if (mout->hp_out_nid[i]) | 
 | 5163 | 			snd_hda_codec_cleanup_stream(codec, | 
 | 5164 | 						     mout->hp_out_nid[i]); | 
| Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5165 | 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) | 
 | 5166 | 		if (mout->extra_out_nid[i]) | 
| Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5167 | 			snd_hda_codec_cleanup_stream(codec, | 
 | 5168 | 						     mout->extra_out_nid[i]); | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5169 | 	mutex_lock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | 	if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { | 
| Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5171 | 		cleanup_dig_out_stream(codec, mout->dig_out_nid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | 		mout->dig_out_used = 0; | 
 | 5173 | 	} | 
| Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5174 | 	mutex_unlock(&codec->spdif_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | 	return 0; | 
 | 5176 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5177 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 |  | 
| Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5179 | /** | 
 | 5180 |  * snd_hda_get_default_vref - Get the default (mic) VREF pin bits | 
 | 5181 |  * | 
 | 5182 |  * Guess the suitable VREF pin bits to be set as the pin-control value. | 
 | 5183 |  * Note: the function doesn't set the AC_PINCTL_IN_EN bit. | 
 | 5184 |  */ | 
 | 5185 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) | 
 | 5186 | { | 
 | 5187 | 	unsigned int pincap; | 
 | 5188 | 	unsigned int oldval; | 
 | 5189 | 	oldval = snd_hda_codec_read(codec, pin, 0, | 
 | 5190 | 				    AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | 
 | 5191 | 	pincap = snd_hda_query_pin_caps(codec, pin); | 
 | 5192 | 	pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; | 
 | 5193 | 	/* Exception: if the default pin setup is vref50, we give it priority */ | 
 | 5194 | 	if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) | 
 | 5195 | 		return AC_PINCTL_VREF_80; | 
 | 5196 | 	else if (pincap & AC_PINCAP_VREF_50) | 
 | 5197 | 		return AC_PINCTL_VREF_50; | 
 | 5198 | 	else if (pincap & AC_PINCAP_VREF_100) | 
 | 5199 | 		return AC_PINCTL_VREF_100; | 
 | 5200 | 	else if (pincap & AC_PINCAP_VREF_GRD) | 
 | 5201 | 		return AC_PINCTL_VREF_GRD; | 
 | 5202 | 	return AC_PINCTL_VREF_HIZ; | 
 | 5203 | } | 
 | 5204 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); | 
 | 5205 |  | 
| Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5206 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, | 
 | 5207 | 			 unsigned int val, bool cached) | 
 | 5208 | { | 
 | 5209 | 	if (val) { | 
 | 5210 | 		unsigned int cap = snd_hda_query_pin_caps(codec, pin); | 
| Takashi Iwai | 6942c10 | 2012-04-20 13:08:40 +0200 | [diff] [blame] | 5211 | 		if (cap && (val & AC_PINCTL_OUT_EN)) { | 
| Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5212 | 			if (!(cap & AC_PINCAP_OUT)) | 
 | 5213 | 				val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); | 
 | 5214 | 			else if ((val & AC_PINCTL_HP_EN) && | 
 | 5215 | 				 !(cap & AC_PINCAP_HP_DRV)) | 
 | 5216 | 				val &= ~AC_PINCTL_HP_EN; | 
 | 5217 | 		} | 
| Takashi Iwai | 6942c10 | 2012-04-20 13:08:40 +0200 | [diff] [blame] | 5218 | 		if (cap && (val & AC_PINCTL_IN_EN)) { | 
| Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5219 | 			if (!(cap & AC_PINCAP_IN)) | 
 | 5220 | 				val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); | 
 | 5221 | 		} | 
 | 5222 | 	} | 
 | 5223 | 	if (cached) | 
 | 5224 | 		return snd_hda_codec_update_cache(codec, pin, 0, | 
 | 5225 | 				AC_VERB_SET_PIN_WIDGET_CONTROL, val); | 
 | 5226 | 	else | 
 | 5227 | 		return snd_hda_codec_write(codec, pin, 0, | 
 | 5228 | 					   AC_VERB_SET_PIN_WIDGET_CONTROL, val); | 
 | 5229 | } | 
 | 5230 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); | 
 | 5231 |  | 
| Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5232 | /** | 
 | 5233 |  * snd_hda_add_imux_item - Add an item to input_mux | 
 | 5234 |  * | 
 | 5235 |  * When the same label is used already in the existing items, the number | 
 | 5236 |  * suffix is appended to the label.  This label index number is stored | 
 | 5237 |  * to type_idx when non-NULL pointer is given. | 
 | 5238 |  */ | 
| Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5239 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, | 
 | 5240 | 			  int index, int *type_idx) | 
 | 5241 | { | 
 | 5242 | 	int i, label_idx = 0; | 
 | 5243 | 	if (imux->num_items >= HDA_MAX_NUM_INPUTS) { | 
 | 5244 | 		snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); | 
 | 5245 | 		return -EINVAL; | 
 | 5246 | 	} | 
 | 5247 | 	for (i = 0; i < imux->num_items; i++) { | 
 | 5248 | 		if (!strncmp(label, imux->items[i].label, strlen(label))) | 
 | 5249 | 			label_idx++; | 
 | 5250 | 	} | 
 | 5251 | 	if (type_idx) | 
 | 5252 | 		*type_idx = label_idx; | 
 | 5253 | 	if (label_idx > 0) | 
 | 5254 | 		snprintf(imux->items[imux->num_items].label, | 
 | 5255 | 			 sizeof(imux->items[imux->num_items].label), | 
 | 5256 | 			 "%s %d", label, label_idx); | 
 | 5257 | 	else | 
 | 5258 | 		strlcpy(imux->items[imux->num_items].label, label, | 
 | 5259 | 			sizeof(imux->items[imux->num_items].label)); | 
 | 5260 | 	imux->items[imux->num_items].index = index; | 
 | 5261 | 	imux->num_items++; | 
 | 5262 | 	return 0; | 
 | 5263 | } | 
 | 5264 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); | 
| Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5265 |  | 
| Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5266 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | #ifdef CONFIG_PM | 
 | 5268 | /* | 
 | 5269 |  * power management | 
 | 5270 |  */ | 
 | 5271 |  | 
 | 5272 | /** | 
 | 5273 |  * snd_hda_suspend - suspend the codecs | 
 | 5274 |  * @bus: the HDA bus | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 |  * | 
 | 5276 |  * Returns 0 if successful. | 
 | 5277 |  */ | 
| Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5278 | int snd_hda_suspend(struct hda_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5280 | 	struct hda_codec *codec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5282 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
| David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5283 | 		cancel_delayed_work_sync(&codec->jackpoll_work); | 
| Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5284 | 		if (hda_codec_is_power_on(codec)) | 
| Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5285 | 			hda_call_codec_suspend(codec, false); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | 	} | 
 | 5287 | 	return 0; | 
 | 5288 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5289 | EXPORT_SYMBOL_HDA(snd_hda_suspend); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 |  | 
 | 5291 | /** | 
 | 5292 |  * snd_hda_resume - resume the codecs | 
 | 5293 |  * @bus: the HDA bus | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 |  * | 
 | 5295 |  * Returns 0 if successful. | 
 | 5296 |  */ | 
 | 5297 | int snd_hda_resume(struct hda_bus *bus) | 
 | 5298 | { | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5299 | 	struct hda_codec *codec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 |  | 
| Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5301 | 	list_for_each_entry(codec, &bus->codec_list, list) { | 
| Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5302 | 		hda_call_codec_resume(codec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | 	return 0; | 
 | 5305 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5306 | EXPORT_SYMBOL_HDA(snd_hda_resume); | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5307 | #endif /* CONFIG_PM */ | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5308 |  | 
 | 5309 | /* | 
 | 5310 |  * generic arrays | 
 | 5311 |  */ | 
 | 5312 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5313 | /** | 
 | 5314 |  * snd_array_new - get a new element from the given array | 
 | 5315 |  * @array: the array object | 
| Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5316 |  * | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5317 |  * Get a new element from the given array.  If it exceeds the | 
 | 5318 |  * pre-allocated array size, re-allocate the array. | 
 | 5319 |  * | 
 | 5320 |  * Returns NULL if allocation failed. | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5321 |  */ | 
 | 5322 | void *snd_array_new(struct snd_array *array) | 
 | 5323 | { | 
| Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5324 | 	if (snd_BUG_ON(!array->elem_size)) | 
 | 5325 | 		return NULL; | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5326 | 	if (array->used >= array->alloced) { | 
 | 5327 | 		int num = array->alloced + array->alloc_align; | 
| Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5328 | 		int size = (num + 1) * array->elem_size; | 
| Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5329 | 		int oldsize = array->alloced * array->elem_size; | 
| Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5330 | 		void *nlist; | 
 | 5331 | 		if (snd_BUG_ON(num >= 4096)) | 
 | 5332 | 			return NULL; | 
| Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5333 | 		nlist = krealloc(array->list, size, GFP_KERNEL); | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5334 | 		if (!nlist) | 
 | 5335 | 			return NULL; | 
| Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5336 | 		memset(nlist + oldsize, 0, size - oldsize); | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5337 | 		array->list = nlist; | 
 | 5338 | 		array->alloced = num; | 
 | 5339 | 	} | 
| Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5340 | 	return snd_array_elem(array, array->used++); | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5341 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5342 | EXPORT_SYMBOL_HDA(snd_array_new); | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5343 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5344 | /** | 
 | 5345 |  * snd_array_free - free the given array elements | 
 | 5346 |  * @array: the array object | 
 | 5347 |  */ | 
| Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5348 | void snd_array_free(struct snd_array *array) | 
 | 5349 | { | 
 | 5350 | 	kfree(array->list); | 
 | 5351 | 	array->used = 0; | 
 | 5352 | 	array->alloced = 0; | 
 | 5353 | 	array->list = NULL; | 
 | 5354 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5355 | EXPORT_SYMBOL_HDA(snd_array_free); | 
| Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5356 |  | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5357 | /** | 
| Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5358 |  * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer | 
 | 5359 |  * @pcm: PCM caps bits | 
 | 5360 |  * @buf: the string buffer to write | 
 | 5361 |  * @buflen: the max buffer length | 
 | 5362 |  * | 
 | 5363 |  * used by hda_proc.c and hda_eld.c | 
 | 5364 |  */ | 
| Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5365 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) | 
 | 5366 | { | 
 | 5367 | 	static unsigned int bits[] = { 8, 16, 20, 24, 32 }; | 
 | 5368 | 	int i, j; | 
 | 5369 |  | 
 | 5370 | 	for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) | 
 | 5371 | 		if (pcm & (AC_SUPPCM_BITS_8 << i)) | 
 | 5372 | 			j += snprintf(buf + j, buflen - j,  " %d", bits[i]); | 
 | 5373 |  | 
 | 5374 | 	buf[j] = '\0'; /* necessary when j == 0 */ | 
 | 5375 | } | 
| Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5376 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); | 
| Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5377 |  | 
 | 5378 | MODULE_DESCRIPTION("HDA codec core"); | 
 | 5379 | MODULE_LICENSE("GPL"); |