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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/pci.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include "hda_codec.h" |
| 29 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 30 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/initval.h> |
| 32 | #include "hda_local.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 33 | #include <sound/hda_hwdep.h> |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 34 | #include "hda_patch.h" /* codec presets */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 37 | /* define this option here to hide as static */ |
Takashi Iwai | 7a5a27c | 2007-09-17 19:07:46 +0200 | [diff] [blame] | 38 | static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 39 | module_param(power_save, int, 0644); |
| 40 | MODULE_PARM_DESC(power_save, "Automatic power-saving timeout " |
| 41 | "(in second, 0 = disable)."); |
| 42 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * vendor / preset table |
| 46 | */ |
| 47 | |
| 48 | struct hda_vendor_id { |
| 49 | unsigned int id; |
| 50 | const char *name; |
| 51 | }; |
| 52 | |
| 53 | /* codec vendor labels */ |
| 54 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 55 | { 0x1002, "ATI" }, |
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" }, |
| 58 | { 0x10ec, "Realtek" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 59 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 60 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 61 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 62 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 64 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 65 | { 0x17e8, "Chrontel" }, |
| 66 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 67 | { 0x1aec, "Wolfson Microelectronics" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { 0x434d, "C-Media" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 69 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | {} /* terminator */ |
| 71 | }; |
| 72 | |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 73 | static const struct hda_codec_preset *hda_preset_tables[] = { |
| 74 | #ifdef CONFIG_SND_HDA_CODEC_REALTEK |
| 75 | snd_hda_preset_realtek, |
| 76 | #endif |
| 77 | #ifdef CONFIG_SND_HDA_CODEC_CMEDIA |
| 78 | snd_hda_preset_cmedia, |
| 79 | #endif |
| 80 | #ifdef CONFIG_SND_HDA_CODEC_ANALOG |
| 81 | snd_hda_preset_analog, |
| 82 | #endif |
| 83 | #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL |
| 84 | snd_hda_preset_sigmatel, |
| 85 | #endif |
| 86 | #ifdef CONFIG_SND_HDA_CODEC_SI3054 |
| 87 | snd_hda_preset_si3054, |
| 88 | #endif |
| 89 | #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI |
| 90 | snd_hda_preset_atihdmi, |
| 91 | #endif |
| 92 | #ifdef CONFIG_SND_HDA_CODEC_CONEXANT |
| 93 | snd_hda_preset_conexant, |
| 94 | #endif |
| 95 | #ifdef CONFIG_SND_HDA_CODEC_VIA |
| 96 | snd_hda_preset_via, |
| 97 | #endif |
Wei Ni | 9a10eb2 | 2008-09-26 13:45:46 +0800 | [diff] [blame] | 98 | #ifdef CONFIG_SND_HDA_CODEC_NVHDMI |
| 99 | snd_hda_preset_nvhdmi, |
| 100 | #endif |
Wu, Fengguang | 9150487 | 2008-11-05 11:16:56 +0800 | [diff] [blame^] | 101 | #ifdef CONFIG_SND_HDA_CODEC_INTELHDMI |
| 102 | snd_hda_preset_intelhdmi, |
| 103 | #endif |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 104 | NULL |
| 105 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 107 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 108 | static void hda_power_work(struct work_struct *work); |
| 109 | static void hda_keep_power_on(struct hda_codec *codec); |
| 110 | #else |
| 111 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
| 112 | #endif |
| 113 | |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 114 | const char *snd_hda_get_jack_location(u32 cfg) |
| 115 | { |
| 116 | static char *bases[7] = { |
| 117 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 118 | }; |
| 119 | static unsigned char specials_idx[] = { |
| 120 | 0x07, 0x08, |
| 121 | 0x17, 0x18, 0x19, |
| 122 | 0x37, 0x38 |
| 123 | }; |
| 124 | static char *specials[] = { |
| 125 | "Rear Panel", "Drive Bar", |
| 126 | "Riser", "HDMI", "ATAPI", |
| 127 | "Mobile-In", "Mobile-Out" |
| 128 | }; |
| 129 | int i; |
| 130 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 131 | if ((cfg & 0x0f) < 7) |
| 132 | return bases[cfg & 0x0f]; |
| 133 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 134 | if (cfg == specials_idx[i]) |
| 135 | return specials[i]; |
| 136 | } |
| 137 | return "UNKNOWN"; |
| 138 | } |
| 139 | |
| 140 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 141 | { |
| 142 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 143 | |
| 144 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 145 | } |
| 146 | |
| 147 | const char *snd_hda_get_jack_type(u32 cfg) |
| 148 | { |
| 149 | static char *jack_types[16] = { |
| 150 | "Line Out", "Speaker", "HP Out", "CD", |
| 151 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 152 | "Line In", "Aux", "Mic", "Telephony", |
| 153 | "SPDIF In", "Digitial In", "Reserved", "Other" |
| 154 | }; |
| 155 | |
| 156 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 157 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /** |
| 161 | * snd_hda_codec_read - send a command and get the response |
| 162 | * @codec: the HDA codec |
| 163 | * @nid: NID to send the command |
| 164 | * @direct: direct flag |
| 165 | * @verb: the verb to send |
| 166 | * @parm: the parameter for the verb |
| 167 | * |
| 168 | * Send a single command and read the corresponding response. |
| 169 | * |
| 170 | * Returns the obtained response value, or -1 for an error. |
| 171 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 172 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 173 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | unsigned int verb, unsigned int parm) |
| 175 | { |
| 176 | unsigned int res; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 177 | snd_hda_power_up(codec); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 178 | mutex_lock(&codec->bus->cmd_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 179 | if (!codec->bus->ops.command(codec, nid, direct, verb, parm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | res = codec->bus->ops.get_response(codec); |
| 181 | else |
| 182 | res = (unsigned int)-1; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 183 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 184 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return res; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * snd_hda_codec_write - send a single command without waiting for response |
| 190 | * @codec: the HDA codec |
| 191 | * @nid: NID to send the command |
| 192 | * @direct: direct flag |
| 193 | * @verb: the verb to send |
| 194 | * @parm: the parameter for the verb |
| 195 | * |
| 196 | * Send a single command without waiting for response. |
| 197 | * |
| 198 | * Returns 0 if successful, or a negative error code. |
| 199 | */ |
| 200 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 201 | unsigned int verb, unsigned int parm) |
| 202 | { |
| 203 | int err; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 204 | snd_hda_power_up(codec); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 205 | mutex_lock(&codec->bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 207 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 208 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return err; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * snd_hda_sequence_write - sequence writes |
| 214 | * @codec: the HDA codec |
| 215 | * @seq: VERB array to send |
| 216 | * |
| 217 | * Send the commands sequentially from the given array. |
| 218 | * The array must be terminated with NID=0. |
| 219 | */ |
| 220 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 221 | { |
| 222 | for (; seq->nid; seq++) |
| 223 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 228 | * @codec: the HDA codec |
| 229 | * @nid: NID to parse |
| 230 | * @start_id: the pointer to store the start NID |
| 231 | * |
| 232 | * Parse the NID and store the start NID of its sub-nodes. |
| 233 | * Returns the number of sub-nodes. |
| 234 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 235 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 236 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | unsigned int parm; |
| 239 | |
| 240 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 241 | if (parm == -1) |
| 242 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | *start_id = (parm >> 16) & 0x7fff; |
| 244 | return (int)(parm & 0x7fff); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * snd_hda_get_connections - get connection list |
| 249 | * @codec: the HDA codec |
| 250 | * @nid: NID to parse |
| 251 | * @conn_list: connection list array |
| 252 | * @max_conns: max. number of connections to store |
| 253 | * |
| 254 | * Parses the connection list of the given widget and stores the list |
| 255 | * of NIDs. |
| 256 | * |
| 257 | * Returns the number of connections, or a negative error code. |
| 258 | */ |
| 259 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
| 260 | hda_nid_t *conn_list, int max_conns) |
| 261 | { |
| 262 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 263 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 265 | hda_nid_t prev_nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 267 | if (snd_BUG_ON(!conn_list || max_conns <= 0)) |
| 268 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 271 | if (parm & AC_CLIST_LONG) { |
| 272 | /* long form */ |
| 273 | shift = 16; |
| 274 | num_elems = 2; |
| 275 | } else { |
| 276 | /* short form */ |
| 277 | shift = 8; |
| 278 | num_elems = 4; |
| 279 | } |
| 280 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | mask = (1 << (shift-1)) - 1; |
| 282 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 283 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return 0; /* no connection */ |
| 285 | |
| 286 | if (conn_len == 1) { |
| 287 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 288 | parm = snd_hda_codec_read(codec, nid, 0, |
| 289 | AC_VERB_GET_CONNECT_LIST, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | conn_list[0] = parm & mask; |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | /* multi connection */ |
| 295 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 296 | prev_nid = 0; |
| 297 | for (i = 0; i < conn_len; i++) { |
| 298 | int range_val; |
| 299 | hda_nid_t val, n; |
| 300 | |
| 301 | if (i % num_elems == 0) |
| 302 | parm = snd_hda_codec_read(codec, nid, 0, |
| 303 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 304 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 305 | val = parm & mask; |
| 306 | parm >>= shift; |
| 307 | if (range_val) { |
| 308 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 309 | if (!prev_nid || prev_nid >= val) { |
| 310 | snd_printk(KERN_WARNING "hda_codec: " |
| 311 | "invalid dep_range_val %x:%x\n", |
| 312 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 313 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 315 | for (n = prev_nid + 1; n <= val; n++) { |
| 316 | if (conns >= max_conns) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 317 | snd_printk(KERN_ERR |
| 318 | "Too many connections\n"); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 319 | return -EINVAL; |
| 320 | } |
| 321 | conn_list[conns++] = n; |
| 322 | } |
| 323 | } else { |
| 324 | if (conns >= max_conns) { |
| 325 | snd_printk(KERN_ERR "Too many connections\n"); |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | conn_list[conns++] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 330 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | return conns; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 338 | * @bus: the BUS |
| 339 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 340 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 341 | * |
| 342 | * Adds the given event to the queue. The events are processed in |
| 343 | * the workqueue asynchronously. Call this function in the interrupt |
| 344 | * hanlder when RIRB receives an unsolicited event. |
| 345 | * |
| 346 | * Returns 0 if successful, or a negative error code. |
| 347 | */ |
| 348 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 349 | { |
| 350 | struct hda_bus_unsolicited *unsol; |
| 351 | unsigned int wp; |
| 352 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 353 | unsol = bus->unsol; |
| 354 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 358 | unsol->wp = wp; |
| 359 | |
| 360 | wp <<= 1; |
| 361 | unsol->queue[wp] = res; |
| 362 | unsol->queue[wp + 1] = res_ex; |
| 363 | |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 364 | schedule_work(&unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 370 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 372 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 374 | struct hda_bus_unsolicited *unsol = |
| 375 | container_of(work, struct hda_bus_unsolicited, work); |
| 376 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct hda_codec *codec; |
| 378 | unsigned int rp, caddr, res; |
| 379 | |
| 380 | while (unsol->rp != unsol->wp) { |
| 381 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 382 | unsol->rp = rp; |
| 383 | rp <<= 1; |
| 384 | res = unsol->queue[rp]; |
| 385 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 386 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | continue; |
| 388 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 389 | if (codec && codec->patch_ops.unsol_event) |
| 390 | codec->patch_ops.unsol_event(codec, res); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * initialize unsolicited queue |
| 396 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 397 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | struct hda_bus_unsolicited *unsol; |
| 400 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 401 | if (bus->unsol) /* already initialized */ |
| 402 | return 0; |
| 403 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 404 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 405 | if (!unsol) { |
| 406 | snd_printk(KERN_ERR "hda_codec: " |
| 407 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return -ENOMEM; |
| 409 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 410 | INIT_WORK(&unsol->work, process_unsol_events); |
| 411 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | bus->unsol = unsol; |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * destructor |
| 418 | */ |
| 419 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 420 | |
| 421 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 422 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 423 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 425 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | if (bus->unsol) { |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 428 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | kfree(bus->unsol); |
| 430 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 431 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | snd_hda_codec_free(codec); |
| 433 | } |
| 434 | if (bus->ops.private_free) |
| 435 | bus->ops.private_free(bus); |
| 436 | kfree(bus); |
| 437 | return 0; |
| 438 | } |
| 439 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 440 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | struct hda_bus *bus = device->device_data; |
| 443 | return snd_hda_bus_free(bus); |
| 444 | } |
| 445 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 446 | #ifdef CONFIG_SND_HDA_HWDEP |
| 447 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 448 | { |
| 449 | struct hda_bus *bus = device->device_data; |
| 450 | struct hda_codec *codec; |
| 451 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 452 | snd_hda_hwdep_add_sysfs(codec); |
| 453 | } |
| 454 | return 0; |
| 455 | } |
| 456 | #else |
| 457 | #define snd_hda_bus_dev_register NULL |
| 458 | #endif |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | /** |
| 461 | * snd_hda_bus_new - create a HDA bus |
| 462 | * @card: the card entry |
| 463 | * @temp: the template for hda_bus information |
| 464 | * @busp: the pointer to store the created bus instance |
| 465 | * |
| 466 | * Returns 0 if successful, or a negative error code. |
| 467 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 468 | int __devinit snd_hda_bus_new(struct snd_card *card, |
| 469 | const struct hda_bus_template *temp, |
| 470 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | struct hda_bus *bus; |
| 473 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 474 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 475 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | .dev_free = snd_hda_bus_dev_free, |
| 477 | }; |
| 478 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 479 | if (snd_BUG_ON(!temp)) |
| 480 | return -EINVAL; |
| 481 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 482 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | if (busp) |
| 485 | *busp = NULL; |
| 486 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 487 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (bus == NULL) { |
| 489 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 490 | return -ENOMEM; |
| 491 | } |
| 492 | |
| 493 | bus->card = card; |
| 494 | bus->private_data = temp->private_data; |
| 495 | bus->pci = temp->pci; |
| 496 | bus->modelname = temp->modelname; |
| 497 | bus->ops = temp->ops; |
| 498 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 499 | mutex_init(&bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | INIT_LIST_HEAD(&bus->codec_list); |
| 501 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 502 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 503 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | snd_hda_bus_free(bus); |
| 505 | return err; |
| 506 | } |
| 507 | if (busp) |
| 508 | *busp = bus; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 512 | #ifdef CONFIG_SND_HDA_GENERIC |
| 513 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 514 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 515 | #else |
| 516 | #define is_generic_config(codec) 0 |
| 517 | #endif |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /* |
| 520 | * find a matching codec preset |
| 521 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 522 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 523 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | const struct hda_codec_preset **tbl, *preset; |
| 526 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 527 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 528 | return NULL; /* use the generic parser */ |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | for (tbl = hda_preset_tables; *tbl; tbl++) { |
| 531 | for (preset = *tbl; preset->id; preset++) { |
| 532 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 533 | if (preset->afg && preset->afg != codec->afg) |
| 534 | continue; |
| 535 | if (preset->mfg && preset->mfg != codec->mfg) |
| 536 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 537 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 539 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 540 | (!preset->rev || |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 541 | preset->rev == codec->revision_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return preset; |
| 543 | } |
| 544 | } |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 549 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 551 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | const struct hda_vendor_id *c; |
| 554 | const char *vendor = NULL; |
| 555 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 556 | char tmp[16], name[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | for (c = hda_vendor_ids; c->id; c++) { |
| 559 | if (c->id == vendor_id) { |
| 560 | vendor = c->name; |
| 561 | break; |
| 562 | } |
| 563 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 564 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | sprintf(tmp, "Generic %04x", vendor_id); |
| 566 | vendor = tmp; |
| 567 | } |
| 568 | if (codec->preset && codec->preset->name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 569 | snprintf(name, sizeof(name), "%s %s", vendor, |
| 570 | codec->preset->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | else |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 572 | snprintf(name, sizeof(name), "%s ID %x", vendor, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 573 | codec->vendor_id & 0xffff); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 574 | codec->name = kstrdup(name, GFP_KERNEL); |
| 575 | if (!codec->name) |
| 576 | return -ENOMEM; |
| 577 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 581 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 583 | static void __devinit setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
| 585 | int i, total_nodes; |
| 586 | hda_nid_t nid; |
| 587 | |
| 588 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 589 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 590 | unsigned int func; |
| 591 | func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE); |
| 592 | switch (func & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 593 | case AC_GRP_AUDIO_FUNCTION: |
| 594 | codec->afg = nid; |
| 595 | break; |
| 596 | case AC_GRP_MODEM_FUNCTION: |
| 597 | codec->mfg = nid; |
| 598 | break; |
| 599 | default: |
| 600 | break; |
| 601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 606 | * read widget caps for each widget and store in cache |
| 607 | */ |
| 608 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 609 | { |
| 610 | int i; |
| 611 | hda_nid_t nid; |
| 612 | |
| 613 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 614 | &codec->start_nid); |
| 615 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 616 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 617 | return -ENOMEM; |
| 618 | nid = codec->start_nid; |
| 619 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 620 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 621 | AC_PAR_AUDIO_WIDGET_CAP); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 626 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 627 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 628 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 629 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | * codec destructor |
| 632 | */ |
| 633 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 634 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 635 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 637 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 638 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 2525fdc | 2007-08-15 22:18:22 +0200 | [diff] [blame] | 639 | flush_scheduled_work(); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 640 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 642 | snd_array_free(&codec->mixers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 644 | if (codec->patch_ops.free) |
| 645 | codec->patch_ops.free(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 646 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 647 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 648 | kfree(codec->name); |
| 649 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 650 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | kfree(codec); |
| 652 | } |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /** |
| 655 | * snd_hda_codec_new - create a HDA codec |
| 656 | * @bus: the bus to assign |
| 657 | * @codec_addr: the codec address |
| 658 | * @codecp: the pointer to store the generated codec |
| 659 | * |
| 660 | * Returns 0 if successful, or a negative error code. |
| 661 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 662 | int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr, |
| 663 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 666 | char component[31]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | int err; |
| 668 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 669 | if (snd_BUG_ON(!bus)) |
| 670 | return -EINVAL; |
| 671 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 672 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 675 | snd_printk(KERN_ERR "hda_codec: " |
| 676 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -EBUSY; |
| 678 | } |
| 679 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 680 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (codec == NULL) { |
| 682 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 683 | return -ENOMEM; |
| 684 | } |
| 685 | |
| 686 | codec->bus = bus; |
| 687 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 688 | mutex_init(&codec->spdif_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 689 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 690 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 691 | snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 692 | if (codec->bus->modelname) { |
| 693 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 694 | if (!codec->modelname) { |
| 695 | snd_hda_codec_free(codec); |
| 696 | return -ENODEV; |
| 697 | } |
| 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 700 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 701 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 702 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 703 | * the caller has to power down appropriatley after initialization |
| 704 | * phase. |
| 705 | */ |
| 706 | hda_keep_power_on(codec); |
| 707 | #endif |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | list_add_tail(&codec->list, &bus->codec_list); |
| 710 | bus->caddr_tbl[codec_addr] = codec; |
| 711 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 712 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 713 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 714 | if (codec->vendor_id == -1) |
| 715 | /* read again, hopefully the access method was corrected |
| 716 | * in the last read... |
| 717 | */ |
| 718 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 719 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 720 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 721 | AC_PAR_SUBSYSTEM_ID); |
| 722 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 723 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 725 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 726 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 727 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | snd_hda_codec_free(codec); |
| 729 | return -ENODEV; |
| 730 | } |
| 731 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 732 | if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) { |
| 733 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 734 | snd_hda_codec_free(codec); |
| 735 | return -ENOMEM; |
| 736 | } |
| 737 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 738 | if (!codec->subsystem_id) { |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 739 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 740 | codec->subsystem_id = |
| 741 | snd_hda_codec_read(codec, nid, 0, |
| 742 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 743 | } |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 744 | if (bus->modelname) |
| 745 | codec->modelname = kstrdup(bus->modelname, GFP_KERNEL); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 746 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 747 | err = snd_hda_codec_configure(codec); |
| 748 | if (err < 0) { |
| 749 | snd_hda_codec_free(codec); |
| 750 | return err; |
| 751 | } |
| 752 | snd_hda_codec_proc_new(codec); |
| 753 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 754 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 755 | |
| 756 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 757 | codec->subsystem_id, codec->revision_id); |
| 758 | snd_component_add(codec->bus->card, component); |
| 759 | |
| 760 | if (codecp) |
| 761 | *codecp = codec; |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 766 | { |
| 767 | int err; |
| 768 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 769 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 770 | if (!codec->name) { |
| 771 | err = get_codec_name(codec); |
| 772 | if (err < 0) |
| 773 | return err; |
| 774 | } |
Takashi Iwai | 43ea1d4 | 2007-04-26 19:12:08 +0200 | [diff] [blame] | 775 | /* audio codec should override the mixer name */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 776 | if (codec->afg || !*codec->bus->card->mixername) |
| 777 | strlcpy(codec->bus->card->mixername, codec->name, |
| 778 | sizeof(codec->bus->card->mixername)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 780 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 782 | goto patched; |
| 783 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 784 | if (codec->preset && codec->preset->patch) { |
| 785 | err = codec->preset->patch(codec); |
| 786 | goto patched; |
| 787 | } |
| 788 | |
| 789 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 790 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 791 | if (err < 0) |
| 792 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 793 | |
| 794 | patched: |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 795 | if (!err && codec->patch_ops.unsol_event) |
| 796 | err = init_unsol_queue(codec->bus); |
| 797 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /** |
| 801 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 802 | * @codec: the CODEC to set up |
| 803 | * @nid: the NID to set up |
| 804 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 805 | * @channel_id: channel id to pass, zero based. |
| 806 | * @format: stream format. |
| 807 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 808 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 809 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | int channel_id, int format) |
| 811 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 812 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 813 | return; |
| 814 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 815 | snd_printdd("hda_codec_setup_stream: " |
| 816 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | nid, stream_tag, channel_id, format); |
| 818 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, |
| 819 | (stream_tag << 4) | channel_id); |
| 820 | msleep(1); |
| 821 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format); |
| 822 | } |
| 823 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 824 | void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid) |
| 825 | { |
| 826 | if (!nid) |
| 827 | return; |
| 828 | |
| 829 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
| 830 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 831 | #if 0 /* keep the format */ |
| 832 | msleep(1); |
| 833 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0); |
| 834 | #endif |
| 835 | } |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* |
| 838 | * amp access functions |
| 839 | */ |
| 840 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 841 | /* FIXME: more better hash key? */ |
| 842 | #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 844 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | /* initialize the hash table */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 847 | static void __devinit init_hda_cache(struct hda_cache_rec *cache, |
| 848 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 850 | memset(cache, 0, sizeof(*cache)); |
| 851 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 852 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 855 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 856 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 857 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 861 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 862 | u32 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 864 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 865 | u16 cur = cache->hash[idx]; |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 866 | struct hda_cache_head *info_head = cache->buf.list; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 867 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
| 869 | while (cur != 0xffff) { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 870 | info = &info_head[cur]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (info->key == key) |
| 872 | return info; |
| 873 | cur = info->next; |
| 874 | } |
| 875 | |
| 876 | /* add a new hash entry */ |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 877 | info = snd_array_new(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | info->key = key; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 879 | info->val = 0; |
| 880 | info->next = cache->hash[idx]; |
| 881 | cache->hash[idx] = cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | return info; |
| 884 | } |
| 885 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 886 | /* query and allocate an amp hash entry */ |
| 887 | static inline struct hda_amp_info * |
| 888 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 889 | { |
| 890 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /* |
| 894 | * query AMP capabilities for the given widget and direction |
| 895 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 896 | 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] | 897 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 898 | struct hda_amp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 900 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
| 901 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | return 0; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 903 | if (!(info->head.val & INFO_AMP_CAPS)) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 904 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | nid = codec->afg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 906 | info->amp_caps = snd_hda_param_read(codec, nid, |
| 907 | direction == HDA_OUTPUT ? |
| 908 | AC_PAR_AMP_OUT_CAP : |
| 909 | AC_PAR_AMP_IN_CAP); |
Takashi Iwai | b75e53f | 2007-05-10 16:56:09 +0200 | [diff] [blame] | 910 | if (info->amp_caps) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 911 | info->head.val |= INFO_AMP_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | } |
| 913 | return info->amp_caps; |
| 914 | } |
| 915 | |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 916 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 917 | unsigned int caps) |
| 918 | { |
| 919 | struct hda_amp_info *info; |
| 920 | |
| 921 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0)); |
| 922 | if (!info) |
| 923 | return -EINVAL; |
| 924 | info->amp_caps = caps; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 925 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | /* |
| 930 | * read the current volume to info |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 931 | * if the cache exists, read the cache value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 933 | static unsigned int get_vol_mute(struct hda_codec *codec, |
| 934 | struct hda_amp_info *info, hda_nid_t nid, |
| 935 | int ch, int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | { |
| 937 | u32 val, parm; |
| 938 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 939 | if (info->head.val & INFO_AMP_VOL(ch)) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 940 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
| 942 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 943 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 944 | parm |= index; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 945 | val = snd_hda_codec_read(codec, nid, 0, |
| 946 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | info->vol[ch] = val & 0xff; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 948 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 949 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 953 | * write the current volume in info to the h/w and update the cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | */ |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 955 | 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] | 956 | hda_nid_t nid, int ch, int direction, int index, |
| 957 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
| 959 | u32 parm; |
| 960 | |
| 961 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 962 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 963 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
| 964 | parm |= val; |
| 965 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 966 | info->vol[ch] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 970 | * 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] | 971 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 972 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 973 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 975 | struct hda_amp_info *info; |
| 976 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 977 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 979 | return get_vol_mute(codec, info, nid, ch, direction, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 982 | /* |
| 983 | * update the AMP value, mask = bit mask to set, val = the value |
| 984 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 985 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 986 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 988 | struct hda_amp_info *info; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 989 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 990 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
| 991 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 993 | val &= mask; |
| 994 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 995 | if (info->vol[ch] == val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 997 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return 1; |
| 999 | } |
| 1000 | |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1001 | /* |
| 1002 | * update the AMP stereo with the same mask and value |
| 1003 | */ |
| 1004 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1005 | int direction, int idx, int mask, int val) |
| 1006 | { |
| 1007 | int ch, ret = 0; |
| 1008 | for (ch = 0; ch < 2; ch++) |
| 1009 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1010 | idx, mask, val); |
| 1011 | return ret; |
| 1012 | } |
| 1013 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1014 | #ifdef SND_HDA_NEEDS_RESUME |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1015 | /* resume the all amp commands from the cache */ |
| 1016 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1017 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1018 | struct hda_amp_info *buffer = codec->amp_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1019 | int i; |
| 1020 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1021 | for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1022 | u32 key = buffer->head.key; |
| 1023 | hda_nid_t nid; |
| 1024 | unsigned int idx, dir, ch; |
| 1025 | if (!key) |
| 1026 | continue; |
| 1027 | nid = key & 0xff; |
| 1028 | idx = (key >> 16) & 0xff; |
| 1029 | dir = (key >> 24) & 0xff; |
| 1030 | for (ch = 0; ch < 2; ch++) { |
| 1031 | if (!(buffer->head.val & INFO_AMP_VOL(ch))) |
| 1032 | continue; |
| 1033 | put_vol_mute(codec, buffer, nid, ch, dir, idx, |
| 1034 | buffer->vol[ch]); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1038 | #endif /* SND_HDA_NEEDS_RESUME */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | /* volume */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1041 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 1042 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
| 1044 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1045 | u16 nid = get_amp_nid(kcontrol); |
| 1046 | u8 chs = get_amp_channels(kcontrol); |
| 1047 | int dir = get_amp_direction(kcontrol); |
| 1048 | u32 caps; |
| 1049 | |
| 1050 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1051 | /* num steps */ |
| 1052 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 1053 | if (!caps) { |
| 1054 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 1055 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 1056 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | return -EINVAL; |
| 1058 | } |
| 1059 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1060 | uinfo->count = chs == 3 ? 2 : 1; |
| 1061 | uinfo->value.integer.min = 0; |
| 1062 | uinfo->value.integer.max = caps; |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1066 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 1067 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | { |
| 1069 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1070 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1071 | int chs = get_amp_channels(kcontrol); |
| 1072 | int dir = get_amp_direction(kcontrol); |
| 1073 | int idx = get_amp_index(kcontrol); |
| 1074 | long *valp = ucontrol->value.integer.value; |
| 1075 | |
| 1076 | if (chs & 1) |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1077 | *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) |
| 1078 | & HDA_AMP_VOLMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | if (chs & 2) |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1080 | *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) |
| 1081 | & HDA_AMP_VOLMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return 0; |
| 1083 | } |
| 1084 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1085 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 1086 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | { |
| 1088 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1089 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1090 | int chs = get_amp_channels(kcontrol); |
| 1091 | int dir = get_amp_direction(kcontrol); |
| 1092 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | long *valp = ucontrol->value.integer.value; |
| 1094 | int change = 0; |
| 1095 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1096 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1097 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1098 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
| 1099 | 0x7f, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1100 | valp++; |
| 1101 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1102 | if (chs & 2) |
| 1103 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1104 | 0x7f, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1105 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return change; |
| 1107 | } |
| 1108 | |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1109 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 1110 | unsigned int size, unsigned int __user *_tlv) |
| 1111 | { |
| 1112 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1113 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1114 | int dir = get_amp_direction(kcontrol); |
| 1115 | u32 caps, val1, val2; |
| 1116 | |
| 1117 | if (size < 4 * sizeof(unsigned int)) |
| 1118 | return -ENOMEM; |
| 1119 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1120 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 1121 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1122 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
| 1123 | val1 = ((int)val1) * ((int)val2); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1124 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 1125 | return -EFAULT; |
| 1126 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 1127 | return -EFAULT; |
| 1128 | if (put_user(val1, _tlv + 2)) |
| 1129 | return -EFAULT; |
| 1130 | if (put_user(val2, _tlv + 3)) |
| 1131 | return -EFAULT; |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1135 | /* |
| 1136 | * set (static) TLV for virtual master volume; recalculated as max 0dB |
| 1137 | */ |
| 1138 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1139 | unsigned int *tlv) |
| 1140 | { |
| 1141 | u32 caps; |
| 1142 | int nums, step; |
| 1143 | |
| 1144 | caps = query_amp_caps(codec, nid, dir); |
| 1145 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 1146 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 1147 | step = (step + 1) * 25; |
| 1148 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 1149 | tlv[1] = 2 * sizeof(unsigned int); |
| 1150 | tlv[2] = -nums * step; |
| 1151 | tlv[3] = step; |
| 1152 | } |
| 1153 | |
| 1154 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1155 | static struct snd_kcontrol * |
| 1156 | _snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 1157 | const char *name, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1158 | { |
| 1159 | struct snd_ctl_elem_id id; |
| 1160 | memset(&id, 0, sizeof(id)); |
| 1161 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1162 | id.index = idx; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1163 | strcpy(id.name, name); |
| 1164 | return snd_ctl_find_id(codec->bus->card, &id); |
| 1165 | } |
| 1166 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1167 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 1168 | const char *name) |
| 1169 | { |
| 1170 | return _snd_hda_find_mixer_ctl(codec, name, 0); |
| 1171 | } |
| 1172 | |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1173 | /* Add a control element and assign to the codec */ |
| 1174 | int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl) |
| 1175 | { |
| 1176 | int err; |
| 1177 | struct snd_kcontrol **knewp; |
| 1178 | |
| 1179 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1180 | if (err < 0) |
| 1181 | return err; |
| 1182 | knewp = snd_array_new(&codec->mixers); |
| 1183 | if (!knewp) |
| 1184 | return -ENOMEM; |
| 1185 | *knewp = kctl; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* Clear all controls assigned to the given codec */ |
| 1190 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 1191 | { |
| 1192 | int i; |
| 1193 | struct snd_kcontrol **kctls = codec->mixers.list; |
| 1194 | for (i = 0; i < codec->mixers.used; i++) |
| 1195 | snd_ctl_remove(codec->bus->card, kctls[i]); |
| 1196 | snd_array_free(&codec->mixers); |
| 1197 | } |
| 1198 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1199 | void snd_hda_codec_reset(struct hda_codec *codec) |
| 1200 | { |
| 1201 | int i; |
| 1202 | |
| 1203 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1204 | cancel_delayed_work(&codec->power_work); |
| 1205 | flush_scheduled_work(); |
| 1206 | #endif |
| 1207 | snd_hda_ctls_clear(codec); |
| 1208 | /* relase PCMs */ |
| 1209 | for (i = 0; i < codec->num_pcms; i++) { |
| 1210 | if (codec->pcm_info[i].pcm) |
| 1211 | snd_device_free(codec->bus->card, |
| 1212 | codec->pcm_info[i].pcm); |
| 1213 | } |
| 1214 | if (codec->patch_ops.free) |
| 1215 | codec->patch_ops.free(codec); |
| 1216 | codec->spec = NULL; |
| 1217 | free_hda_cache(&codec->amp_cache); |
| 1218 | free_hda_cache(&codec->cmd_cache); |
| 1219 | codec->num_pcms = 0; |
| 1220 | codec->pcm_info = NULL; |
| 1221 | codec->preset = NULL; |
| 1222 | } |
| 1223 | |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1224 | /* create a virtual master control and add slaves */ |
| 1225 | int snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
| 1226 | unsigned int *tlv, const char **slaves) |
| 1227 | { |
| 1228 | struct snd_kcontrol *kctl; |
| 1229 | const char **s; |
| 1230 | int err; |
| 1231 | |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 1232 | for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++) |
| 1233 | ; |
| 1234 | if (!*s) { |
| 1235 | snd_printdd("No slave found for %s\n", name); |
| 1236 | return 0; |
| 1237 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1238 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 1239 | if (!kctl) |
| 1240 | return -ENOMEM; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1241 | err = snd_hda_ctl_add(codec, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1242 | if (err < 0) |
| 1243 | return err; |
| 1244 | |
| 1245 | for (s = slaves; *s; s++) { |
| 1246 | struct snd_kcontrol *sctl; |
| 1247 | |
| 1248 | sctl = snd_hda_find_mixer_ctl(codec, *s); |
| 1249 | if (!sctl) { |
| 1250 | snd_printdd("Cannot find slave %s, skipped\n", *s); |
| 1251 | continue; |
| 1252 | } |
| 1253 | err = snd_ctl_add_slave(kctl, sctl); |
| 1254 | if (err < 0) |
| 1255 | return err; |
| 1256 | } |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | /* switch */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1261 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 1262 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | { |
| 1264 | int chs = get_amp_channels(kcontrol); |
| 1265 | |
| 1266 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1267 | uinfo->count = chs == 3 ? 2 : 1; |
| 1268 | uinfo->value.integer.min = 0; |
| 1269 | uinfo->value.integer.max = 1; |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1273 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 1274 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | { |
| 1276 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1277 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1278 | int chs = get_amp_channels(kcontrol); |
| 1279 | int dir = get_amp_direction(kcontrol); |
| 1280 | int idx = get_amp_index(kcontrol); |
| 1281 | long *valp = ucontrol->value.integer.value; |
| 1282 | |
| 1283 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1284 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1285 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1287 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1288 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | return 0; |
| 1290 | } |
| 1291 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1292 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 1293 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | { |
| 1295 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1296 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1297 | int chs = get_amp_channels(kcontrol); |
| 1298 | int dir = get_amp_direction(kcontrol); |
| 1299 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | long *valp = ucontrol->value.integer.value; |
| 1301 | int change = 0; |
| 1302 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1303 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1304 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1305 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1306 | HDA_AMP_MUTE, |
| 1307 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1308 | valp++; |
| 1309 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1310 | if (chs & 2) |
| 1311 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1312 | HDA_AMP_MUTE, |
| 1313 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1314 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1315 | if (codec->patch_ops.check_power_status) |
| 1316 | codec->patch_ops.check_power_status(codec, nid); |
| 1317 | #endif |
| 1318 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | return change; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1323 | * bound volume controls |
| 1324 | * |
| 1325 | * bind multiple volumes (# indices, from 0) |
| 1326 | */ |
| 1327 | |
| 1328 | #define AMP_VAL_IDX_SHIFT 19 |
| 1329 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 1330 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1331 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 1332 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1333 | { |
| 1334 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1335 | unsigned long pval; |
| 1336 | int err; |
| 1337 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1338 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1339 | pval = kcontrol->private_value; |
| 1340 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 1341 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 1342 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1343 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1344 | return err; |
| 1345 | } |
| 1346 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1347 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 1348 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1349 | { |
| 1350 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1351 | unsigned long pval; |
| 1352 | int i, indices, err = 0, change = 0; |
| 1353 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1354 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1355 | pval = kcontrol->private_value; |
| 1356 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 1357 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1358 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 1359 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1360 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 1361 | if (err < 0) |
| 1362 | break; |
| 1363 | change |= err; |
| 1364 | } |
| 1365 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1366 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1367 | return err < 0 ? err : change; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1371 | * generic bound volume/swtich controls |
| 1372 | */ |
| 1373 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 1374 | struct snd_ctl_elem_info *uinfo) |
| 1375 | { |
| 1376 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1377 | struct hda_bind_ctls *c; |
| 1378 | int err; |
| 1379 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1380 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1381 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1382 | kcontrol->private_value = *c->values; |
| 1383 | err = c->ops->info(kcontrol, uinfo); |
| 1384 | kcontrol->private_value = (long)c; |
| 1385 | mutex_unlock(&codec->spdif_mutex); |
| 1386 | return err; |
| 1387 | } |
| 1388 | |
| 1389 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 1390 | struct snd_ctl_elem_value *ucontrol) |
| 1391 | { |
| 1392 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1393 | struct hda_bind_ctls *c; |
| 1394 | int err; |
| 1395 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1396 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1397 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1398 | kcontrol->private_value = *c->values; |
| 1399 | err = c->ops->get(kcontrol, ucontrol); |
| 1400 | kcontrol->private_value = (long)c; |
| 1401 | mutex_unlock(&codec->spdif_mutex); |
| 1402 | return err; |
| 1403 | } |
| 1404 | |
| 1405 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 1406 | struct snd_ctl_elem_value *ucontrol) |
| 1407 | { |
| 1408 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1409 | struct hda_bind_ctls *c; |
| 1410 | unsigned long *vals; |
| 1411 | int err = 0, change = 0; |
| 1412 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1413 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1414 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1415 | for (vals = c->values; *vals; vals++) { |
| 1416 | kcontrol->private_value = *vals; |
| 1417 | err = c->ops->put(kcontrol, ucontrol); |
| 1418 | if (err < 0) |
| 1419 | break; |
| 1420 | change |= err; |
| 1421 | } |
| 1422 | kcontrol->private_value = (long)c; |
| 1423 | mutex_unlock(&codec->spdif_mutex); |
| 1424 | return err < 0 ? err : change; |
| 1425 | } |
| 1426 | |
| 1427 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 1428 | unsigned int size, unsigned int __user *tlv) |
| 1429 | { |
| 1430 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1431 | struct hda_bind_ctls *c; |
| 1432 | int err; |
| 1433 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1434 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1435 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1436 | kcontrol->private_value = *c->values; |
| 1437 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 1438 | kcontrol->private_value = (long)c; |
| 1439 | mutex_unlock(&codec->spdif_mutex); |
| 1440 | return err; |
| 1441 | } |
| 1442 | |
| 1443 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 1444 | .info = snd_hda_mixer_amp_volume_info, |
| 1445 | .get = snd_hda_mixer_amp_volume_get, |
| 1446 | .put = snd_hda_mixer_amp_volume_put, |
| 1447 | .tlv = snd_hda_mixer_amp_tlv |
| 1448 | }; |
| 1449 | |
| 1450 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 1451 | .info = snd_hda_mixer_amp_switch_info, |
| 1452 | .get = snd_hda_mixer_amp_switch_get, |
| 1453 | .put = snd_hda_mixer_amp_switch_put, |
| 1454 | .tlv = snd_hda_mixer_amp_tlv |
| 1455 | }; |
| 1456 | |
| 1457 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | * SPDIF out controls |
| 1459 | */ |
| 1460 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1461 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 1462 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { |
| 1464 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 1465 | uinfo->count = 1; |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1469 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 1470 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | { |
| 1472 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1473 | IEC958_AES0_NONAUDIO | |
| 1474 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 1475 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 1476 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 1477 | IEC958_AES1_CON_ORIGINAL; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1481 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 1482 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | { |
| 1484 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1485 | IEC958_AES0_NONAUDIO | |
| 1486 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1490 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 1491 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
| 1493 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1494 | |
| 1495 | ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff; |
| 1496 | ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff; |
| 1497 | ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff; |
| 1498 | ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff; |
| 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 1504 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 1505 | */ |
| 1506 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 1507 | { |
| 1508 | unsigned short val = 0; |
| 1509 | |
| 1510 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1511 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1513 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1515 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 1516 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 1517 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1519 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 1520 | IEC958_AES0_CON_EMPHASIS_5015) |
| 1521 | val |= AC_DIG1_EMPHASIS; |
| 1522 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 1523 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1525 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 1527 | } |
| 1528 | return val; |
| 1529 | } |
| 1530 | |
| 1531 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 1532 | */ |
| 1533 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 1534 | { |
| 1535 | unsigned int sbits = 0; |
| 1536 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1537 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1539 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 1541 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1542 | if (sbits & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 1544 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1545 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1547 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1549 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 1551 | sbits |= val & (0x7f << 8); |
| 1552 | } |
| 1553 | return sbits; |
| 1554 | } |
| 1555 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1556 | /* set digital convert verbs both for the given NID and its slaves */ |
| 1557 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 1558 | int verb, int val) |
| 1559 | { |
| 1560 | hda_nid_t *d; |
| 1561 | |
| 1562 | snd_hda_codec_write(codec, nid, 0, verb, val); |
| 1563 | d = codec->slave_dig_outs; |
| 1564 | if (!d) |
| 1565 | return; |
| 1566 | for (; *d; d++) |
| 1567 | snd_hda_codec_write(codec, *d, 0, verb, val); |
| 1568 | } |
| 1569 | |
| 1570 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 1571 | int dig1, int dig2) |
| 1572 | { |
| 1573 | if (dig1 != -1) |
| 1574 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 1575 | if (dig2 != -1) |
| 1576 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 1577 | } |
| 1578 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1579 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 1580 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | { |
| 1582 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1583 | hda_nid_t nid = kcontrol->private_value; |
| 1584 | unsigned short val; |
| 1585 | int change; |
| 1586 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1587 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | codec->spdif_status = ucontrol->value.iec958.status[0] | |
| 1589 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 1590 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 1591 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
| 1592 | val = convert_from_spdif_status(codec->spdif_status); |
| 1593 | val |= codec->spdif_ctls & 1; |
| 1594 | change = codec->spdif_ctls != val; |
| 1595 | codec->spdif_ctls = val; |
| 1596 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1597 | if (change) |
| 1598 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1600 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | return change; |
| 1602 | } |
| 1603 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 1604 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1606 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 1607 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | { |
| 1609 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1610 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1611 | ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1615 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 1616 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | { |
| 1618 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1619 | hda_nid_t nid = kcontrol->private_value; |
| 1620 | unsigned short val; |
| 1621 | int change; |
| 1622 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1623 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1624 | val = codec->spdif_ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1626 | val |= AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | change = codec->spdif_ctls != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1628 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | codec->spdif_ctls = val; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1630 | set_dig_out_convert(codec, nid, val & 0xff, -1); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1631 | /* unmute amp switch (if any) */ |
| 1632 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1633 | (val & AC_DIG1_ENABLE)) |
| 1634 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 1635 | HDA_AMP_MUTE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1637 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | return change; |
| 1639 | } |
| 1640 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1641 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | { |
| 1643 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1644 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1645 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), |
| 1646 | .info = snd_hda_spdif_mask_info, |
| 1647 | .get = snd_hda_spdif_cmask_get, |
| 1648 | }, |
| 1649 | { |
| 1650 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1651 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1652 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), |
| 1653 | .info = snd_hda_spdif_mask_info, |
| 1654 | .get = snd_hda_spdif_pmask_get, |
| 1655 | }, |
| 1656 | { |
| 1657 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1658 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 1659 | .info = snd_hda_spdif_mask_info, |
| 1660 | .get = snd_hda_spdif_default_get, |
| 1661 | .put = snd_hda_spdif_default_put, |
| 1662 | }, |
| 1663 | { |
| 1664 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1665 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), |
| 1666 | .info = snd_hda_spdif_out_switch_info, |
| 1667 | .get = snd_hda_spdif_out_switch_get, |
| 1668 | .put = snd_hda_spdif_out_switch_put, |
| 1669 | }, |
| 1670 | { } /* end */ |
| 1671 | }; |
| 1672 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1673 | #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */ |
| 1674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | /** |
| 1676 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls |
| 1677 | * @codec: the HDA codec |
| 1678 | * @nid: audio out widget NID |
| 1679 | * |
| 1680 | * Creates controls related with the SPDIF output. |
| 1681 | * Called from each patch supporting the SPDIF out. |
| 1682 | * |
| 1683 | * Returns 0 if successful, or a negative error code. |
| 1684 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 1685 | int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
| 1687 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1688 | struct snd_kcontrol *kctl; |
| 1689 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1690 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1692 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
| 1693 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch", |
| 1694 | idx)) |
| 1695 | break; |
| 1696 | } |
| 1697 | if (idx >= SPDIF_MAX_IDX) { |
| 1698 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 1699 | return -EBUSY; |
| 1700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 1702 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 1703 | if (!kctl) |
| 1704 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1705 | kctl->id.index = idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | kctl->private_value = nid; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1707 | err = snd_hda_ctl_add(codec, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1708 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | return err; |
| 1710 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1711 | codec->spdif_ctls = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1712 | snd_hda_codec_read(codec, nid, 0, |
| 1713 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 1719 | * SPDIF sharing with analog output |
| 1720 | */ |
| 1721 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 1722 | struct snd_ctl_elem_value *ucontrol) |
| 1723 | { |
| 1724 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 1725 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 1730 | struct snd_ctl_elem_value *ucontrol) |
| 1731 | { |
| 1732 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 1733 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | static struct snd_kcontrol_new spdif_share_sw = { |
| 1738 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1739 | .name = "IEC958 Default PCM Playback Switch", |
| 1740 | .info = snd_ctl_boolean_mono_info, |
| 1741 | .get = spdif_share_sw_get, |
| 1742 | .put = spdif_share_sw_put, |
| 1743 | }; |
| 1744 | |
| 1745 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 1746 | struct hda_multi_out *mout) |
| 1747 | { |
| 1748 | if (!mout->dig_out_nid) |
| 1749 | return 0; |
| 1750 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1751 | return snd_hda_ctl_add(codec, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 1752 | snd_ctl_new1(&spdif_share_sw, mout)); |
| 1753 | } |
| 1754 | |
| 1755 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | * SPDIF input |
| 1757 | */ |
| 1758 | |
| 1759 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 1760 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1761 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 1762 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | { |
| 1764 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1765 | |
| 1766 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1770 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 1771 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | { |
| 1773 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1774 | hda_nid_t nid = kcontrol->private_value; |
| 1775 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 1776 | int change; |
| 1777 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1778 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1780 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1782 | snd_hda_codec_write_cache(codec, nid, 0, |
| 1783 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1785 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | return change; |
| 1787 | } |
| 1788 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1789 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 1790 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | { |
| 1792 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1793 | hda_nid_t nid = kcontrol->private_value; |
| 1794 | unsigned short val; |
| 1795 | unsigned int sbits; |
| 1796 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1797 | 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] | 1798 | sbits = convert_to_spdif_status(val); |
| 1799 | ucontrol->value.iec958.status[0] = sbits; |
| 1800 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 1801 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 1802 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1806 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
| 1808 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1809 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), |
| 1810 | .info = snd_hda_spdif_in_switch_info, |
| 1811 | .get = snd_hda_spdif_in_switch_get, |
| 1812 | .put = snd_hda_spdif_in_switch_put, |
| 1813 | }, |
| 1814 | { |
| 1815 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1816 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1817 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 1818 | .info = snd_hda_spdif_mask_info, |
| 1819 | .get = snd_hda_spdif_in_status_get, |
| 1820 | }, |
| 1821 | { } /* end */ |
| 1822 | }; |
| 1823 | |
| 1824 | /** |
| 1825 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 1826 | * @codec: the HDA codec |
| 1827 | * @nid: audio in widget NID |
| 1828 | * |
| 1829 | * Creates controls related with the SPDIF input. |
| 1830 | * Called from each patch supporting the SPDIF in. |
| 1831 | * |
| 1832 | * Returns 0 if successful, or a negative error code. |
| 1833 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 1834 | 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] | 1835 | { |
| 1836 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1837 | struct snd_kcontrol *kctl; |
| 1838 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1839 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1841 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
| 1842 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch", |
| 1843 | idx)) |
| 1844 | break; |
| 1845 | } |
| 1846 | if (idx >= SPDIF_MAX_IDX) { |
| 1847 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 1848 | return -EBUSY; |
| 1849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 1851 | kctl = snd_ctl_new1(dig_mix, codec); |
| 1852 | kctl->private_value = nid; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1853 | err = snd_hda_ctl_add(codec, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1854 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | return err; |
| 1856 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1857 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1858 | snd_hda_codec_read(codec, nid, 0, |
| 1859 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1860 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1864 | #ifdef SND_HDA_NEEDS_RESUME |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * command cache |
| 1867 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1869 | /* build a 32bit cache key with the widget id and the command parameter */ |
| 1870 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 1871 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 1872 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 1873 | |
| 1874 | /** |
| 1875 | * snd_hda_codec_write_cache - send a single command with caching |
| 1876 | * @codec: the HDA codec |
| 1877 | * @nid: NID to send the command |
| 1878 | * @direct: direct flag |
| 1879 | * @verb: the verb to send |
| 1880 | * @parm: the parameter for the verb |
| 1881 | * |
| 1882 | * Send a single command without waiting for response. |
| 1883 | * |
| 1884 | * Returns 0 if successful, or a negative error code. |
| 1885 | */ |
| 1886 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 1887 | int direct, unsigned int verb, unsigned int parm) |
| 1888 | { |
| 1889 | int err; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1890 | snd_hda_power_up(codec); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1891 | mutex_lock(&codec->bus->cmd_mutex); |
| 1892 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
| 1893 | if (!err) { |
| 1894 | struct hda_cache_head *c; |
| 1895 | u32 key = build_cmd_cache_key(nid, verb); |
| 1896 | c = get_alloc_hash(&codec->cmd_cache, key); |
| 1897 | if (c) |
| 1898 | c->val = parm; |
| 1899 | } |
| 1900 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1901 | snd_hda_power_down(codec); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1902 | return err; |
| 1903 | } |
| 1904 | |
| 1905 | /* resume the all commands from the cache */ |
| 1906 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 1907 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1908 | struct hda_cache_head *buffer = codec->cmd_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1909 | int i; |
| 1910 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1911 | for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1912 | u32 key = buffer->key; |
| 1913 | if (!key) |
| 1914 | continue; |
| 1915 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 1916 | get_cmd_cache_cmd(key), buffer->val); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 1922 | * @codec: the HDA codec |
| 1923 | * @seq: VERB array to send |
| 1924 | * |
| 1925 | * Send the commands sequentially from the given array. |
| 1926 | * Thte commands are recorded on cache for power-save and resume. |
| 1927 | * The array must be terminated with NID=0. |
| 1928 | */ |
| 1929 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 1930 | const struct hda_verb *seq) |
| 1931 | { |
| 1932 | for (; seq->nid; seq++) |
| 1933 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 1934 | seq->param); |
| 1935 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1936 | #endif /* SND_HDA_NEEDS_RESUME */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1937 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1938 | /* |
| 1939 | * set power state of the codec |
| 1940 | */ |
| 1941 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 1942 | unsigned int power_state) |
| 1943 | { |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1944 | hda_nid_t nid; |
| 1945 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1946 | |
| 1947 | snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE, |
| 1948 | power_state); |
Marc Boucher | d2595d8 | 2008-01-22 15:23:30 +0100 | [diff] [blame] | 1949 | msleep(10); /* partial workaround for "azx_get_response timeout" */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1950 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1951 | nid = codec->start_nid; |
| 1952 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 1953 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1954 | if (wcaps & AC_WCAP_POWER) { |
| 1955 | unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >> |
| 1956 | AC_WCAP_TYPE_SHIFT; |
| 1957 | if (wid_type == AC_WID_PIN) { |
| 1958 | unsigned int pincap; |
| 1959 | /* |
| 1960 | * don't power down the widget if it controls |
| 1961 | * eapd and EAPD_BTLENABLE is set. |
| 1962 | */ |
| 1963 | pincap = snd_hda_param_read(codec, nid, |
| 1964 | AC_PAR_PIN_CAP); |
| 1965 | if (pincap & AC_PINCAP_EAPD) { |
| 1966 | int eapd = snd_hda_codec_read(codec, |
| 1967 | nid, 0, |
| 1968 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 1969 | eapd &= 0x02; |
| 1970 | if (power_state == AC_PWRST_D3 && eapd) |
| 1971 | continue; |
| 1972 | } |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 1973 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1974 | snd_hda_codec_write(codec, nid, 0, |
| 1975 | AC_VERB_SET_POWER_STATE, |
| 1976 | power_state); |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 1977 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1980 | if (power_state == AC_PWRST_D0) { |
| 1981 | unsigned long end_time; |
| 1982 | int state; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1983 | msleep(10); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1984 | /* wait until the codec reachs to D0 */ |
| 1985 | end_time = jiffies + msecs_to_jiffies(500); |
| 1986 | do { |
| 1987 | state = snd_hda_codec_read(codec, fg, 0, |
| 1988 | AC_VERB_GET_POWER_STATE, 0); |
| 1989 | if (state == power_state) |
| 1990 | break; |
| 1991 | msleep(1); |
| 1992 | } while (time_after_eq(end_time, jiffies)); |
| 1993 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1994 | } |
| 1995 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 1996 | #ifdef CONFIG_SND_HDA_HWDEP |
| 1997 | /* execute additional init verbs */ |
| 1998 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 1999 | { |
| 2000 | if (codec->init_verbs.list) |
| 2001 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 2002 | } |
| 2003 | #else |
| 2004 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 2005 | #endif |
| 2006 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2007 | #ifdef SND_HDA_NEEDS_RESUME |
| 2008 | /* |
| 2009 | * call suspend and power-down; used both from PM and power-save |
| 2010 | */ |
| 2011 | static void hda_call_codec_suspend(struct hda_codec *codec) |
| 2012 | { |
| 2013 | if (codec->patch_ops.suspend) |
| 2014 | codec->patch_ops.suspend(codec, PMSG_SUSPEND); |
| 2015 | hda_set_power_state(codec, |
| 2016 | codec->afg ? codec->afg : codec->mfg, |
| 2017 | AC_PWRST_D3); |
| 2018 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 2019 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 2020 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2021 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2022 | #endif |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * kick up codec; used both from PM and power-save |
| 2027 | */ |
| 2028 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 2029 | { |
| 2030 | hda_set_power_state(codec, |
| 2031 | codec->afg ? codec->afg : codec->mfg, |
| 2032 | AC_PWRST_D0); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 2033 | hda_exec_init_verbs(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2034 | if (codec->patch_ops.resume) |
| 2035 | codec->patch_ops.resume(codec); |
| 2036 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 2037 | if (codec->patch_ops.init) |
| 2038 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2039 | snd_hda_codec_resume_amp(codec); |
| 2040 | snd_hda_codec_resume_cache(codec); |
| 2041 | } |
| 2042 | } |
| 2043 | #endif /* SND_HDA_NEEDS_RESUME */ |
| 2044 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | /** |
| 2047 | * snd_hda_build_controls - build mixer controls |
| 2048 | * @bus: the BUS |
| 2049 | * |
| 2050 | * Creates mixer controls for each codec included in the bus. |
| 2051 | * |
| 2052 | * Returns 0 if successful, otherwise a negative error code. |
| 2053 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 2054 | int __devinit snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2056 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2058 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2059 | int err = snd_hda_codec_build_controls(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | if (err < 0) |
| 2061 | return err; |
| 2062 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2063 | return 0; |
| 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2066 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 2067 | { |
| 2068 | int err = 0; |
| 2069 | /* fake as if already powered-on */ |
| 2070 | hda_keep_power_on(codec); |
| 2071 | /* then fire up */ |
| 2072 | hda_set_power_state(codec, |
| 2073 | codec->afg ? codec->afg : codec->mfg, |
| 2074 | AC_PWRST_D0); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 2075 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2076 | /* continue to initialize... */ |
| 2077 | if (codec->patch_ops.init) |
| 2078 | err = codec->patch_ops.init(codec); |
| 2079 | if (!err && codec->patch_ops.build_controls) |
| 2080 | err = codec->patch_ops.build_controls(codec); |
| 2081 | snd_hda_power_down(codec); |
| 2082 | if (err < 0) |
| 2083 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | return 0; |
| 2085 | } |
| 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | /* |
| 2088 | * stream formats |
| 2089 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2090 | struct hda_rate_tbl { |
| 2091 | unsigned int hz; |
| 2092 | unsigned int alsa_bits; |
| 2093 | unsigned int hda_fmt; |
| 2094 | }; |
| 2095 | |
| 2096 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 2098 | |
| 2099 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */ |
| 2101 | { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */ |
| 2102 | { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */ |
| 2103 | { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */ |
| 2104 | { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */ |
| 2105 | { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */ |
| 2106 | { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */ |
| 2107 | { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */ |
| 2108 | { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */ |
| 2109 | { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */ |
| 2110 | { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */ |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2111 | #define AC_PAR_PCM_RATE_BITS 11 |
| 2112 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 2113 | |
| 2114 | /* not autodetected value */ |
| 2115 | { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 2116 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2117 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | }; |
| 2119 | |
| 2120 | /** |
| 2121 | * snd_hda_calc_stream_format - calculate format bitset |
| 2122 | * @rate: the sample rate |
| 2123 | * @channels: the number of channels |
| 2124 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 2125 | * @maxbps: the max. bps |
| 2126 | * |
| 2127 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 2128 | * |
| 2129 | * Return zero if invalid. |
| 2130 | */ |
| 2131 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 2132 | unsigned int channels, |
| 2133 | unsigned int format, |
| 2134 | unsigned int maxbps) |
| 2135 | { |
| 2136 | int i; |
| 2137 | unsigned int val = 0; |
| 2138 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2139 | for (i = 0; rate_bits[i].hz; i++) |
| 2140 | if (rate_bits[i].hz == rate) { |
| 2141 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | break; |
| 2143 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2144 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | snd_printdd("invalid rate %d\n", rate); |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | if (channels == 0 || channels > 8) { |
| 2150 | snd_printdd("invalid channels %d\n", channels); |
| 2151 | return 0; |
| 2152 | } |
| 2153 | val |= channels - 1; |
| 2154 | |
| 2155 | switch (snd_pcm_format_width(format)) { |
| 2156 | case 8: val |= 0x00; break; |
| 2157 | case 16: val |= 0x10; break; |
| 2158 | case 20: |
| 2159 | case 24: |
| 2160 | case 32: |
| 2161 | if (maxbps >= 32) |
| 2162 | val |= 0x40; |
| 2163 | else if (maxbps >= 24) |
| 2164 | val |= 0x30; |
| 2165 | else |
| 2166 | val |= 0x20; |
| 2167 | break; |
| 2168 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2169 | snd_printdd("invalid format width %d\n", |
| 2170 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | return val; |
| 2175 | } |
| 2176 | |
| 2177 | /** |
| 2178 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 2179 | * @codec: the HDA codec |
| 2180 | * @nid: NID to query |
| 2181 | * @ratesp: the pointer to store the detected rate bitflags |
| 2182 | * @formatsp: the pointer to store the detected formats |
| 2183 | * @bpsp: the pointer to store the detected format widths |
| 2184 | * |
| 2185 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 2186 | * or @bsps argument is ignored. |
| 2187 | * |
| 2188 | * Returns 0 if successful, otherwise a negative error code. |
| 2189 | */ |
| 2190 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
| 2191 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 2192 | { |
| 2193 | int i; |
| 2194 | unsigned int val, streams; |
| 2195 | |
| 2196 | val = 0; |
| 2197 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2198 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 2200 | if (val == -1) |
| 2201 | return -EIO; |
| 2202 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2203 | if (!val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 2205 | |
| 2206 | if (ratesp) { |
| 2207 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2208 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2210 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
| 2212 | *ratesp = rates; |
| 2213 | } |
| 2214 | |
| 2215 | if (formatsp || bpsp) { |
| 2216 | u64 formats = 0; |
| 2217 | unsigned int bps; |
| 2218 | unsigned int wcaps; |
| 2219 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2220 | wcaps = get_wcaps(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 2222 | if (streams == -1) |
| 2223 | return -EIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2224 | if (!streams) { |
| 2225 | streams = snd_hda_param_read(codec, codec->afg, |
| 2226 | AC_PAR_STREAM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | if (streams == -1) |
| 2228 | return -EIO; |
| 2229 | } |
| 2230 | |
| 2231 | bps = 0; |
| 2232 | if (streams & AC_SUPFMT_PCM) { |
| 2233 | if (val & AC_SUPPCM_BITS_8) { |
| 2234 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 2235 | bps = 8; |
| 2236 | } |
| 2237 | if (val & AC_SUPPCM_BITS_16) { |
| 2238 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 2239 | bps = 16; |
| 2240 | } |
| 2241 | if (wcaps & AC_WCAP_DIGITAL) { |
| 2242 | if (val & AC_SUPPCM_BITS_32) |
| 2243 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 2244 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 2245 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 2246 | if (val & AC_SUPPCM_BITS_24) |
| 2247 | bps = 24; |
| 2248 | else if (val & AC_SUPPCM_BITS_20) |
| 2249 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2250 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 2251 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 2253 | if (val & AC_SUPPCM_BITS_32) |
| 2254 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | else if (val & AC_SUPPCM_BITS_24) |
| 2256 | bps = 24; |
Nicolas Graziano | 33ef765 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 2257 | else if (val & AC_SUPPCM_BITS_20) |
| 2258 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | } |
| 2260 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2261 | else if (streams == AC_SUPFMT_FLOAT32) { |
| 2262 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
| 2264 | bps = 32; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2265 | } else if (streams == AC_SUPFMT_AC3) { |
| 2266 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | /* temporary hack: we have still no proper support |
| 2268 | * for the direct AC3 stream... |
| 2269 | */ |
| 2270 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 2271 | bps = 8; |
| 2272 | } |
| 2273 | if (formatsp) |
| 2274 | *formatsp = formats; |
| 2275 | if (bpsp) |
| 2276 | *bpsp = bps; |
| 2277 | } |
| 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | /** |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2283 | * snd_hda_is_supported_format - check whether the given node supports |
| 2284 | * the format val |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | * |
| 2286 | * Returns 1 if supported, 0 if not. |
| 2287 | */ |
| 2288 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 2289 | unsigned int format) |
| 2290 | { |
| 2291 | int i; |
| 2292 | unsigned int val = 0, rate, stream; |
| 2293 | |
| 2294 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2295 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 2297 | if (val == -1) |
| 2298 | return 0; |
| 2299 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2300 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 2302 | if (val == -1) |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
| 2306 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2307 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2308 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | if (val & (1 << i)) |
| 2310 | break; |
| 2311 | return 0; |
| 2312 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2313 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | return 0; |
| 2315 | |
| 2316 | stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 2317 | if (stream == -1) |
| 2318 | return 0; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2319 | if (!stream && nid != codec->afg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2321 | if (!stream || stream == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | return 0; |
| 2323 | |
| 2324 | if (stream & AC_SUPFMT_PCM) { |
| 2325 | switch (format & 0xf0) { |
| 2326 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2327 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | return 0; |
| 2329 | break; |
| 2330 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2331 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | return 0; |
| 2333 | break; |
| 2334 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2335 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | return 0; |
| 2337 | break; |
| 2338 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2339 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | return 0; |
| 2341 | break; |
| 2342 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2343 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | return 0; |
| 2345 | break; |
| 2346 | default: |
| 2347 | return 0; |
| 2348 | } |
| 2349 | } else { |
| 2350 | /* FIXME: check for float32 and AC3? */ |
| 2351 | } |
| 2352 | |
| 2353 | return 1; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * PCM stuff |
| 2358 | */ |
| 2359 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 2360 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2361 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | { |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 2367 | struct hda_codec *codec, |
| 2368 | unsigned int stream_tag, |
| 2369 | unsigned int format, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2370 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | { |
| 2372 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
| 2376 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 2377 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2378 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2380 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | return 0; |
| 2382 | } |
| 2383 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2384 | static int set_pcm_default_values(struct hda_codec *codec, |
| 2385 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2387 | /* query support PCM information from the given NID */ |
| 2388 | if (info->nid && (!info->rates || !info->formats)) { |
| 2389 | snd_hda_query_supported_pcm(codec, info->nid, |
| 2390 | info->rates ? NULL : &info->rates, |
| 2391 | info->formats ? NULL : &info->formats, |
| 2392 | info->maxbps ? NULL : &info->maxbps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | } |
| 2394 | if (info->ops.open == NULL) |
| 2395 | info->ops.open = hda_pcm_default_open_close; |
| 2396 | if (info->ops.close == NULL) |
| 2397 | info->ops.close = hda_pcm_default_open_close; |
| 2398 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 2399 | if (snd_BUG_ON(!info->nid)) |
| 2400 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | info->ops.prepare = hda_pcm_default_prepare; |
| 2402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 2404 | if (snd_BUG_ON(!info->nid)) |
| 2405 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 2407 | } |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2411 | /* |
| 2412 | * attach a new PCM stream |
| 2413 | */ |
| 2414 | static int __devinit |
| 2415 | snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) |
| 2416 | { |
| 2417 | struct hda_pcm_stream *info; |
| 2418 | int stream, err; |
| 2419 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 2420 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2421 | return -EINVAL; |
| 2422 | for (stream = 0; stream < 2; stream++) { |
| 2423 | info = &pcm->stream[stream]; |
| 2424 | if (info->substreams) { |
| 2425 | err = set_pcm_default_values(codec, info); |
| 2426 | if (err < 0) |
| 2427 | return err; |
| 2428 | } |
| 2429 | } |
| 2430 | return codec->bus->ops.attach_pcm(codec, pcm); |
| 2431 | } |
| 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | /** |
| 2434 | * snd_hda_build_pcms - build PCM information |
| 2435 | * @bus: the BUS |
| 2436 | * |
| 2437 | * Create PCM information for each codec included in the bus. |
| 2438 | * |
| 2439 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 2440 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 2441 | * to create its PCM instances. |
| 2442 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 2443 | * callback. |
| 2444 | * |
| 2445 | * At least, substreams, channels_min and channels_max must be filled for |
| 2446 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 2447 | * When rates and/or formats are zero, the supported values are queried |
| 2448 | * from the given nid. The nid is used also by the default ops.prepare |
| 2449 | * and ops.cleanup callbacks. |
| 2450 | * |
| 2451 | * The driver needs to call ops.open in its open callback. Similarly, |
| 2452 | * ops.close is supposed to be called in the close callback. |
| 2453 | * ops.prepare should be called in the prepare or hw_params callback |
| 2454 | * with the proper parameters for set up. |
| 2455 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 2456 | * |
| 2457 | * This function returns 0 if successfull, or a negative error code. |
| 2458 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2459 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | { |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2461 | static const char *dev_name[HDA_PCM_NTYPES] = { |
| 2462 | "Audio", "SPDIF", "HDMI", "Modem" |
| 2463 | }; |
| 2464 | /* starting device index for each PCM type */ |
| 2465 | static int dev_idx[HDA_PCM_NTYPES] = { |
| 2466 | [HDA_PCM_TYPE_AUDIO] = 0, |
| 2467 | [HDA_PCM_TYPE_SPDIF] = 1, |
| 2468 | [HDA_PCM_TYPE_HDMI] = 3, |
| 2469 | [HDA_PCM_TYPE_MODEM] = 6 |
| 2470 | }; |
| 2471 | /* normal audio device indices; not linear to keep compatibility */ |
| 2472 | static int audio_idx[4] = { 0, 2, 4, 5 }; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2473 | struct hda_codec *codec; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2474 | int num_devs[HDA_PCM_NTYPES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2476 | memset(num_devs, 0, sizeof(num_devs)); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2477 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2478 | unsigned int pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | int err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2480 | if (!codec->num_pcms) { |
| 2481 | if (!codec->patch_ops.build_pcms) |
| 2482 | continue; |
| 2483 | err = codec->patch_ops.build_pcms(codec); |
| 2484 | if (err < 0) |
| 2485 | return err; |
| 2486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2488 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2489 | int type = cpcm->pcm_type; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2490 | int dev; |
Takashi Iwai | bfc5c26 | 2008-11-04 17:48:39 +0100 | [diff] [blame] | 2491 | |
| 2492 | if (!cpcm->stream[0].substreams && |
| 2493 | !cpcm->stream[1].substreams) |
| 2494 | continue; /* no substreams assigned */ |
| 2495 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2496 | switch (type) { |
| 2497 | case HDA_PCM_TYPE_AUDIO: |
| 2498 | if (num_devs[type] >= ARRAY_SIZE(audio_idx)) { |
| 2499 | snd_printk(KERN_WARNING |
| 2500 | "Too many audio devices\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | continue; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2502 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2503 | dev = audio_idx[num_devs[type]]; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2504 | break; |
| 2505 | case HDA_PCM_TYPE_SPDIF: |
| 2506 | case HDA_PCM_TYPE_HDMI: |
| 2507 | case HDA_PCM_TYPE_MODEM: |
| 2508 | if (num_devs[type]) { |
| 2509 | snd_printk(KERN_WARNING |
| 2510 | "%s already defined\n", |
| 2511 | dev_name[type]); |
| 2512 | continue; |
| 2513 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2514 | dev = dev_idx[type]; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2515 | break; |
| 2516 | default: |
| 2517 | snd_printk(KERN_WARNING |
| 2518 | "Invalid PCM type %d\n", type); |
| 2519 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | } |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 2521 | num_devs[type]++; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2522 | if (!cpcm->pcm) { |
| 2523 | cpcm->device = dev; |
| 2524 | err = snd_hda_attach_pcm(codec, cpcm); |
| 2525 | if (err < 0) |
| 2526 | return err; |
| 2527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | /** |
| 2534 | * snd_hda_check_board_config - compare the current codec with the config table |
| 2535 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2536 | * @num_configs: number of config enums |
| 2537 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | * @tbl: configuration table, terminated by null entries |
| 2539 | * |
| 2540 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 2541 | * given configuration table. If a matching entry is found, returns its |
| 2542 | * config value (supposed to be 0 or positive). |
| 2543 | * |
| 2544 | * If no entries are matching, the function returns a negative value. |
| 2545 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2546 | int snd_hda_check_board_config(struct hda_codec *codec, |
| 2547 | int num_configs, const char **models, |
| 2548 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2550 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2551 | int i; |
| 2552 | for (i = 0; i < num_configs; i++) { |
| 2553 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2554 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2555 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 2556 | "selected\n", models[i]); |
| 2557 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | } |
| 2559 | } |
| 2560 | } |
| 2561 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2562 | if (!codec->bus->pci || !tbl) |
| 2563 | return -1; |
| 2564 | |
| 2565 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 2566 | if (!tbl) |
| 2567 | return -1; |
| 2568 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 2569 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2570 | char tmp[10]; |
| 2571 | const char *model = NULL; |
| 2572 | if (models) |
| 2573 | model = models[tbl->value]; |
| 2574 | if (!model) { |
| 2575 | sprintf(tmp, "#%d", tbl->value); |
| 2576 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2578 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 2579 | "for config %x:%x (%s)\n", |
| 2580 | model, tbl->subvendor, tbl->subdevice, |
| 2581 | (tbl->name ? tbl->name : "Unknown device")); |
| 2582 | #endif |
| 2583 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | } |
| 2585 | return -1; |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * snd_hda_add_new_ctls - create controls from the array |
| 2590 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2591 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | * |
| 2593 | * This helper function creates and add new controls in the given array. |
| 2594 | * The array must be terminated with an empty entry as terminator. |
| 2595 | * |
| 2596 | * Returns 0 if successful, or a negative error code. |
| 2597 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2598 | int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | { |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2600 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | |
| 2602 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2603 | struct snd_kcontrol *kctl; |
| 2604 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2605 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2606 | return -ENOMEM; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2607 | err = snd_hda_ctl_add(codec, kctl); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2608 | if (err < 0) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2609 | if (!codec->addr) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2610 | return err; |
| 2611 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2612 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2613 | return -ENOMEM; |
| 2614 | kctl->id.device = codec->addr; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2615 | err = snd_hda_ctl_add(codec, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2616 | if (err < 0) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2617 | return err; |
| 2618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | } |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2623 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 2624 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 2625 | unsigned int power_state); |
| 2626 | |
| 2627 | static void hda_power_work(struct work_struct *work) |
| 2628 | { |
| 2629 | struct hda_codec *codec = |
| 2630 | container_of(work, struct hda_codec, power_work.work); |
| 2631 | |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 2632 | if (!codec->power_on || codec->power_count) { |
| 2633 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2634 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 2635 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2636 | |
| 2637 | hda_call_codec_suspend(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2638 | if (codec->bus->ops.pm_notify) |
| 2639 | codec->bus->ops.pm_notify(codec); |
| 2640 | } |
| 2641 | |
| 2642 | static void hda_keep_power_on(struct hda_codec *codec) |
| 2643 | { |
| 2644 | codec->power_count++; |
| 2645 | codec->power_on = 1; |
| 2646 | } |
| 2647 | |
| 2648 | void snd_hda_power_up(struct hda_codec *codec) |
| 2649 | { |
| 2650 | codec->power_count++; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2651 | if (codec->power_on || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2652 | return; |
| 2653 | |
| 2654 | codec->power_on = 1; |
| 2655 | if (codec->bus->ops.pm_notify) |
| 2656 | codec->bus->ops.pm_notify(codec); |
| 2657 | hda_call_codec_resume(codec); |
| 2658 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2659 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | void snd_hda_power_down(struct hda_codec *codec) |
| 2663 | { |
| 2664 | --codec->power_count; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2665 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2666 | return; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2667 | if (power_save) { |
| 2668 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2669 | schedule_delayed_work(&codec->power_work, |
| 2670 | msecs_to_jiffies(power_save * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2671 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 2675 | struct hda_loopback_check *check, |
| 2676 | hda_nid_t nid) |
| 2677 | { |
| 2678 | struct hda_amp_list *p; |
| 2679 | int ch, v; |
| 2680 | |
| 2681 | if (!check->amplist) |
| 2682 | return 0; |
| 2683 | for (p = check->amplist; p->nid; p++) { |
| 2684 | if (p->nid == nid) |
| 2685 | break; |
| 2686 | } |
| 2687 | if (!p->nid) |
| 2688 | return 0; /* nothing changed */ |
| 2689 | |
| 2690 | for (p = check->amplist; p->nid; p++) { |
| 2691 | for (ch = 0; ch < 2; ch++) { |
| 2692 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 2693 | p->idx); |
| 2694 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 2695 | if (!check->power_on) { |
| 2696 | check->power_on = 1; |
| 2697 | snd_hda_power_up(codec); |
| 2698 | } |
| 2699 | return 1; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | if (check->power_on) { |
| 2704 | check->power_on = 0; |
| 2705 | snd_hda_power_down(codec); |
| 2706 | } |
| 2707 | return 0; |
| 2708 | } |
| 2709 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2711 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2712 | * Channel mode helper |
| 2713 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2714 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 2715 | struct snd_ctl_elem_info *uinfo, |
| 2716 | const struct hda_channel_mode *chmode, |
| 2717 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2718 | { |
| 2719 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2720 | uinfo->count = 1; |
| 2721 | uinfo->value.enumerated.items = num_chmodes; |
| 2722 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 2723 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 2724 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 2725 | chmode[uinfo->value.enumerated.item].channels); |
| 2726 | return 0; |
| 2727 | } |
| 2728 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2729 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 2730 | struct snd_ctl_elem_value *ucontrol, |
| 2731 | const struct hda_channel_mode *chmode, |
| 2732 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2733 | int max_channels) |
| 2734 | { |
| 2735 | int i; |
| 2736 | |
| 2737 | for (i = 0; i < num_chmodes; i++) { |
| 2738 | if (max_channels == chmode[i].channels) { |
| 2739 | ucontrol->value.enumerated.item[0] = i; |
| 2740 | break; |
| 2741 | } |
| 2742 | } |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2746 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 2747 | struct snd_ctl_elem_value *ucontrol, |
| 2748 | const struct hda_channel_mode *chmode, |
| 2749 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2750 | int *max_channelsp) |
| 2751 | { |
| 2752 | unsigned int mode; |
| 2753 | |
| 2754 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 2755 | if (mode >= num_chmodes) |
| 2756 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2757 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2758 | return 0; |
| 2759 | /* change the current channel setting */ |
| 2760 | *max_channelsp = chmode[mode].channels; |
| 2761 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2762 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2763 | return 1; |
| 2764 | } |
| 2765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * input MUX helper |
| 2768 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2769 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 2770 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | { |
| 2772 | unsigned int index; |
| 2773 | |
| 2774 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2775 | uinfo->count = 1; |
| 2776 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 2777 | if (!imux->num_items) |
| 2778 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | index = uinfo->value.enumerated.item; |
| 2780 | if (index >= imux->num_items) |
| 2781 | index = imux->num_items - 1; |
| 2782 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2786 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 2787 | const struct hda_input_mux *imux, |
| 2788 | struct snd_ctl_elem_value *ucontrol, |
| 2789 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | unsigned int *cur_val) |
| 2791 | { |
| 2792 | unsigned int idx; |
| 2793 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 2794 | if (!imux->num_items) |
| 2795 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | idx = ucontrol->value.enumerated.item[0]; |
| 2797 | if (idx >= imux->num_items) |
| 2798 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2799 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2801 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 2802 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | *cur_val = idx; |
| 2804 | return 1; |
| 2805 | } |
| 2806 | |
| 2807 | |
| 2808 | /* |
| 2809 | * Multi-channel / digital-out PCM helper functions |
| 2810 | */ |
| 2811 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2812 | /* setup SPDIF output stream */ |
| 2813 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 2814 | unsigned int stream_tag, unsigned int format) |
| 2815 | { |
| 2816 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2817 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
| 2818 | set_dig_out_convert(codec, nid, |
| 2819 | codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff, |
| 2820 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2821 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2822 | if (codec->slave_dig_outs) { |
| 2823 | hda_nid_t *d; |
| 2824 | for (d = codec->slave_dig_outs; *d; d++) |
| 2825 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 2826 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 2827 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2828 | /* turn on again (if needed) */ |
| 2829 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
| 2830 | set_dig_out_convert(codec, nid, |
| 2831 | codec->spdif_ctls & 0xff, -1); |
| 2832 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 2833 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2834 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 2835 | { |
| 2836 | snd_hda_codec_cleanup_stream(codec, nid); |
| 2837 | if (codec->slave_dig_outs) { |
| 2838 | hda_nid_t *d; |
| 2839 | for (d = codec->slave_dig_outs; *d; d++) |
| 2840 | snd_hda_codec_cleanup_stream(codec, *d); |
| 2841 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | /* |
| 2845 | * open the digital out in the exclusive mode |
| 2846 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2847 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 2848 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2850 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 2851 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 2852 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2853 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2855 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | return 0; |
| 2857 | } |
| 2858 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2859 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 2860 | struct hda_multi_out *mout, |
| 2861 | unsigned int stream_tag, |
| 2862 | unsigned int format, |
| 2863 | struct snd_pcm_substream *substream) |
| 2864 | { |
| 2865 | mutex_lock(&codec->spdif_mutex); |
| 2866 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 2867 | mutex_unlock(&codec->spdif_mutex); |
| 2868 | return 0; |
| 2869 | } |
| 2870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | /* |
| 2872 | * release the digital out |
| 2873 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2874 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 2875 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2877 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2879 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | return 0; |
| 2881 | } |
| 2882 | |
| 2883 | /* |
| 2884 | * set up more restrictions for analog out |
| 2885 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2886 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 2887 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2888 | struct snd_pcm_substream *substream, |
| 2889 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2891 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2892 | runtime->hw.channels_max = mout->max_channels; |
| 2893 | if (mout->dig_out_nid) { |
| 2894 | if (!mout->analog_rates) { |
| 2895 | mout->analog_rates = hinfo->rates; |
| 2896 | mout->analog_formats = hinfo->formats; |
| 2897 | mout->analog_maxbps = hinfo->maxbps; |
| 2898 | } else { |
| 2899 | runtime->hw.rates = mout->analog_rates; |
| 2900 | runtime->hw.formats = mout->analog_formats; |
| 2901 | hinfo->maxbps = mout->analog_maxbps; |
| 2902 | } |
| 2903 | if (!mout->spdif_rates) { |
| 2904 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 2905 | &mout->spdif_rates, |
| 2906 | &mout->spdif_formats, |
| 2907 | &mout->spdif_maxbps); |
| 2908 | } |
| 2909 | mutex_lock(&codec->spdif_mutex); |
| 2910 | if (mout->share_spdif) { |
| 2911 | runtime->hw.rates &= mout->spdif_rates; |
| 2912 | runtime->hw.formats &= mout->spdif_formats; |
| 2913 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 2914 | hinfo->maxbps = mout->spdif_maxbps; |
| 2915 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 2916 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 2919 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | * set up the i/o for analog out |
| 2924 | * when the digital out is available, copy the front out to digital out, too. |
| 2925 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2926 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 2927 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | unsigned int stream_tag, |
| 2929 | unsigned int format, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2930 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | { |
| 2932 | hda_nid_t *nids = mout->dac_nids; |
| 2933 | int chs = substream->runtime->channels; |
| 2934 | int i; |
| 2935 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2936 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2937 | if (mout->dig_out_nid && mout->share_spdif && |
| 2938 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2940 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 2941 | format) && |
| 2942 | !(codec->spdif_status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2944 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 2945 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | } else { |
| 2947 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2948 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | } |
| 2950 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2951 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
| 2953 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2954 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 2955 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2956 | if (!mout->no_share_stream && |
| 2957 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2959 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 2960 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2961 | /* extra outputs copied from front */ |
| 2962 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2963 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2964 | snd_hda_codec_setup_stream(codec, |
| 2965 | mout->extra_out_nid[i], |
| 2966 | stream_tag, 0, format); |
| 2967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | /* surrounds */ |
| 2969 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 2970 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2971 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2972 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2973 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2974 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2975 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | } |
| 2977 | return 0; |
| 2978 | } |
| 2979 | |
| 2980 | /* |
| 2981 | * clean up the setting for analog out |
| 2982 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2983 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 2984 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | { |
| 2986 | hda_nid_t *nids = mout->dac_nids; |
| 2987 | int i; |
| 2988 | |
| 2989 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2990 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2992 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2993 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 2994 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2995 | snd_hda_codec_cleanup_stream(codec, |
| 2996 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2997 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | 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] | 2999 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | mout->dig_out_used = 0; |
| 3001 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3002 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | return 0; |
| 3004 | } |
| 3005 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3006 | /* |
Wu Fengguang | 6b34500 | 2008-10-07 14:21:41 +0800 | [diff] [blame] | 3007 | * Helper for automatic pin configuration |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3008 | */ |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 3009 | |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3010 | static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list) |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 3011 | { |
| 3012 | for (; *list; list++) |
| 3013 | if (*list == nid) |
| 3014 | return 1; |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3018 | |
| 3019 | /* |
| 3020 | * Sort an associated group of pins according to their sequence numbers. |
| 3021 | */ |
| 3022 | static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences, |
| 3023 | int num_pins) |
| 3024 | { |
| 3025 | int i, j; |
| 3026 | short seq; |
| 3027 | hda_nid_t nid; |
| 3028 | |
| 3029 | for (i = 0; i < num_pins; i++) { |
| 3030 | for (j = i + 1; j < num_pins; j++) { |
| 3031 | if (sequences[i] > sequences[j]) { |
| 3032 | seq = sequences[i]; |
| 3033 | sequences[i] = sequences[j]; |
| 3034 | sequences[j] = seq; |
| 3035 | nid = pins[i]; |
| 3036 | pins[i] = pins[j]; |
| 3037 | pins[j] = nid; |
| 3038 | } |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3044 | /* |
| 3045 | * Parse all pin widgets and store the useful pin nids to cfg |
| 3046 | * |
| 3047 | * The number of line-outs or any primary output is stored in line_outs, |
| 3048 | * and the corresponding output pins are assigned to line_out_pins[], |
| 3049 | * in the order of front, rear, CLFE, side, ... |
| 3050 | * |
| 3051 | * If more extra outputs (speaker and headphone) are found, the pins are |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 3052 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3053 | * is detected, one of speaker of HP pins is assigned as the primary |
| 3054 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 3055 | * if any analog output exists. |
| 3056 | * |
| 3057 | * The analog input pins are assigned to input_pins array. |
| 3058 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 3059 | * respectively. |
| 3060 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3061 | int snd_hda_parse_pin_def_config(struct hda_codec *codec, |
| 3062 | struct auto_pin_cfg *cfg, |
| 3063 | hda_nid_t *ignore_nids) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3064 | { |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 3065 | hda_nid_t nid, end_nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3066 | short seq, assoc_line_out, assoc_speaker; |
| 3067 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 3068 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3069 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3070 | |
| 3071 | memset(cfg, 0, sizeof(*cfg)); |
| 3072 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3073 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 3074 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3075 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3076 | assoc_line_out = assoc_speaker = 0; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3077 | |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 3078 | end_nid = codec->start_nid + codec->num_nodes; |
| 3079 | for (nid = codec->start_nid; nid < end_nid; nid++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3080 | unsigned int wid_caps = get_wcaps(codec, nid); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3081 | unsigned int wid_type = |
| 3082 | (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3083 | unsigned int def_conf; |
| 3084 | short assoc, loc; |
| 3085 | |
| 3086 | /* read all default configuration for pin complex */ |
| 3087 | if (wid_type != AC_WID_PIN) |
| 3088 | continue; |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 3089 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 3090 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 3091 | continue; |
| 3092 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3093 | def_conf = snd_hda_codec_read(codec, nid, 0, |
| 3094 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3095 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
| 3096 | continue; |
| 3097 | loc = get_defcfg_location(def_conf); |
| 3098 | switch (get_defcfg_device(def_conf)) { |
| 3099 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3100 | seq = get_defcfg_sequence(def_conf); |
| 3101 | assoc = get_defcfg_association(def_conf); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 3102 | |
| 3103 | if (!(wid_caps & AC_WCAP_STEREO)) |
| 3104 | if (!cfg->mono_out_pin) |
| 3105 | cfg->mono_out_pin = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3106 | if (!assoc) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3107 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3108 | if (!assoc_line_out) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3109 | assoc_line_out = assoc; |
| 3110 | else if (assoc_line_out != assoc) |
| 3111 | continue; |
| 3112 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 3113 | continue; |
| 3114 | cfg->line_out_pins[cfg->line_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3115 | sequences_line_out[cfg->line_outs] = seq; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3116 | cfg->line_outs++; |
| 3117 | break; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 3118 | case AC_JACK_SPEAKER: |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3119 | seq = get_defcfg_sequence(def_conf); |
| 3120 | assoc = get_defcfg_association(def_conf); |
| 3121 | if (! assoc) |
| 3122 | continue; |
| 3123 | if (! assoc_speaker) |
| 3124 | assoc_speaker = assoc; |
| 3125 | else if (assoc_speaker != assoc) |
| 3126 | continue; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3127 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 3128 | continue; |
| 3129 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3130 | sequences_speaker[cfg->speaker_outs] = seq; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3131 | cfg->speaker_outs++; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 3132 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3133 | case AC_JACK_HP_OUT: |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3134 | seq = get_defcfg_sequence(def_conf); |
| 3135 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 3136 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 3137 | continue; |
| 3138 | cfg->hp_pins[cfg->hp_outs] = nid; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3139 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 3140 | cfg->hp_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3141 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 3142 | case AC_JACK_MIC_IN: { |
| 3143 | int preferred, alt; |
| 3144 | if (loc == AC_JACK_LOC_FRONT) { |
| 3145 | preferred = AUTO_PIN_FRONT_MIC; |
| 3146 | alt = AUTO_PIN_MIC; |
| 3147 | } else { |
| 3148 | preferred = AUTO_PIN_MIC; |
| 3149 | alt = AUTO_PIN_FRONT_MIC; |
| 3150 | } |
| 3151 | if (!cfg->input_pins[preferred]) |
| 3152 | cfg->input_pins[preferred] = nid; |
| 3153 | else if (!cfg->input_pins[alt]) |
| 3154 | cfg->input_pins[alt] = nid; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3155 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 3156 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3157 | case AC_JACK_LINE_IN: |
| 3158 | if (loc == AC_JACK_LOC_FRONT) |
| 3159 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid; |
| 3160 | else |
| 3161 | cfg->input_pins[AUTO_PIN_LINE] = nid; |
| 3162 | break; |
| 3163 | case AC_JACK_CD: |
| 3164 | cfg->input_pins[AUTO_PIN_CD] = nid; |
| 3165 | break; |
| 3166 | case AC_JACK_AUX: |
| 3167 | cfg->input_pins[AUTO_PIN_AUX] = nid; |
| 3168 | break; |
| 3169 | case AC_JACK_SPDIF_OUT: |
| 3170 | cfg->dig_out_pin = nid; |
| 3171 | break; |
| 3172 | case AC_JACK_SPDIF_IN: |
| 3173 | cfg->dig_in_pin = nid; |
| 3174 | break; |
| 3175 | } |
| 3176 | } |
| 3177 | |
Takashi Iwai | 5832fcf | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 3178 | /* FIX-UP: |
| 3179 | * If no line-out is defined but multiple HPs are found, |
| 3180 | * some of them might be the real line-outs. |
| 3181 | */ |
| 3182 | if (!cfg->line_outs && cfg->hp_outs > 1) { |
| 3183 | int i = 0; |
| 3184 | while (i < cfg->hp_outs) { |
| 3185 | /* The real HPs should have the sequence 0x0f */ |
| 3186 | if ((sequences_hp[i] & 0x0f) == 0x0f) { |
| 3187 | i++; |
| 3188 | continue; |
| 3189 | } |
| 3190 | /* Move it to the line-out table */ |
| 3191 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; |
| 3192 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; |
| 3193 | cfg->line_outs++; |
| 3194 | cfg->hp_outs--; |
| 3195 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, |
| 3196 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); |
| 3197 | memmove(sequences_hp + i - 1, sequences_hp + i, |
| 3198 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); |
| 3199 | } |
| 3200 | } |
| 3201 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3202 | /* sort by sequence */ |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3203 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 3204 | cfg->line_outs); |
| 3205 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 3206 | cfg->speaker_outs); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3207 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
| 3208 | cfg->hp_outs); |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3209 | |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 3210 | /* if we have only one mic, make it AUTO_PIN_MIC */ |
| 3211 | if (!cfg->input_pins[AUTO_PIN_MIC] && |
| 3212 | cfg->input_pins[AUTO_PIN_FRONT_MIC]) { |
| 3213 | cfg->input_pins[AUTO_PIN_MIC] = |
| 3214 | cfg->input_pins[AUTO_PIN_FRONT_MIC]; |
| 3215 | cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0; |
| 3216 | } |
| 3217 | /* ditto for line-in */ |
| 3218 | if (!cfg->input_pins[AUTO_PIN_LINE] && |
| 3219 | cfg->input_pins[AUTO_PIN_FRONT_LINE]) { |
| 3220 | cfg->input_pins[AUTO_PIN_LINE] = |
| 3221 | cfg->input_pins[AUTO_PIN_FRONT_LINE]; |
| 3222 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0; |
| 3223 | } |
| 3224 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3225 | /* |
| 3226 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 3227 | * as a primary output |
| 3228 | */ |
| 3229 | if (!cfg->line_outs) { |
| 3230 | if (cfg->speaker_outs) { |
| 3231 | cfg->line_outs = cfg->speaker_outs; |
| 3232 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 3233 | sizeof(cfg->speaker_pins)); |
| 3234 | cfg->speaker_outs = 0; |
| 3235 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 3236 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 3237 | } else if (cfg->hp_outs) { |
| 3238 | cfg->line_outs = cfg->hp_outs; |
| 3239 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 3240 | sizeof(cfg->hp_pins)); |
| 3241 | cfg->hp_outs = 0; |
| 3242 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 3243 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 3244 | } |
| 3245 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3246 | |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3247 | /* Reorder the surround channels |
| 3248 | * ALSA sequence is front/surr/clfe/side |
| 3249 | * HDA sequence is: |
| 3250 | * 4-ch: front/surr => OK as it is |
| 3251 | * 6-ch: front/clfe/surr |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 3252 | * 8-ch: front/clfe/rear/side|fc |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3253 | */ |
| 3254 | switch (cfg->line_outs) { |
| 3255 | case 3: |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3256 | case 4: |
| 3257 | nid = cfg->line_out_pins[1]; |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 3258 | cfg->line_out_pins[1] = cfg->line_out_pins[2]; |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3259 | cfg->line_out_pins[2] = nid; |
| 3260 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3261 | } |
| 3262 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3263 | /* |
| 3264 | * debug prints of the parsed results |
| 3265 | */ |
| 3266 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3267 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 3268 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
| 3269 | cfg->line_out_pins[4]); |
| 3270 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3271 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 3272 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 3273 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 3274 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3275 | cfg->hp_outs, cfg->hp_pins[0], |
| 3276 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 3277 | cfg->hp_pins[3], cfg->hp_pins[4]); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 3278 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3279 | snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x," |
| 3280 | " cd=0x%x, aux=0x%x\n", |
| 3281 | cfg->input_pins[AUTO_PIN_MIC], |
| 3282 | cfg->input_pins[AUTO_PIN_FRONT_MIC], |
| 3283 | cfg->input_pins[AUTO_PIN_LINE], |
| 3284 | cfg->input_pins[AUTO_PIN_FRONT_LINE], |
| 3285 | cfg->input_pins[AUTO_PIN_CD], |
| 3286 | cfg->input_pins[AUTO_PIN_AUX]); |
| 3287 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3288 | return 0; |
| 3289 | } |
| 3290 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 3291 | /* labels for input pins */ |
| 3292 | const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = { |
| 3293 | "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux" |
| 3294 | }; |
| 3295 | |
| 3296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | #ifdef CONFIG_PM |
| 3298 | /* |
| 3299 | * power management |
| 3300 | */ |
| 3301 | |
| 3302 | /** |
| 3303 | * snd_hda_suspend - suspend the codecs |
| 3304 | * @bus: the HDA bus |
| 3305 | * @state: suspsend state |
| 3306 | * |
| 3307 | * Returns 0 if successful. |
| 3308 | */ |
| 3309 | int snd_hda_suspend(struct hda_bus *bus, pm_message_t state) |
| 3310 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3311 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3313 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 0b7a2e9 | 2007-08-14 15:18:26 +0200 | [diff] [blame] | 3314 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 3315 | if (!codec->power_on) |
| 3316 | continue; |
| 3317 | #endif |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3318 | hda_call_codec_suspend(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | } |
| 3320 | return 0; |
| 3321 | } |
| 3322 | |
| 3323 | /** |
| 3324 | * snd_hda_resume - resume the codecs |
| 3325 | * @bus: the HDA bus |
| 3326 | * @state: resume state |
| 3327 | * |
| 3328 | * Returns 0 if successful. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3329 | * |
| 3330 | * This fucntion is defined only when POWER_SAVE isn't set. |
| 3331 | * In the power-save mode, the codec is resumed dynamically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | */ |
| 3333 | int snd_hda_resume(struct hda_bus *bus) |
| 3334 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3335 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3337 | list_for_each_entry(codec, &bus->codec_list, list) { |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3338 | if (snd_hda_codec_needs_resume(codec)) |
| 3339 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | return 0; |
| 3342 | } |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3343 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 3344 | int snd_hda_codecs_inuse(struct hda_bus *bus) |
| 3345 | { |
| 3346 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3348 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 3349 | if (snd_hda_codec_needs_resume(codec)) |
| 3350 | return 1; |
| 3351 | } |
| 3352 | return 0; |
| 3353 | } |
| 3354 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | #endif |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3356 | |
| 3357 | /* |
| 3358 | * generic arrays |
| 3359 | */ |
| 3360 | |
| 3361 | /* get a new element from the given array |
| 3362 | * if it exceeds the pre-allocated array size, re-allocate the array |
| 3363 | */ |
| 3364 | void *snd_array_new(struct snd_array *array) |
| 3365 | { |
| 3366 | if (array->used >= array->alloced) { |
| 3367 | int num = array->alloced + array->alloc_align; |
| 3368 | void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL); |
| 3369 | if (!nlist) |
| 3370 | return NULL; |
| 3371 | if (array->list) { |
| 3372 | memcpy(nlist, array->list, |
| 3373 | array->elem_size * array->alloced); |
| 3374 | kfree(array->list); |
| 3375 | } |
| 3376 | array->list = nlist; |
| 3377 | array->alloced = num; |
| 3378 | } |
| 3379 | return array->list + (array->used++ * array->elem_size); |
| 3380 | } |
| 3381 | |
| 3382 | /* free the given array elements */ |
| 3383 | void snd_array_free(struct snd_array *array) |
| 3384 | { |
| 3385 | kfree(array->list); |
| 3386 | array->used = 0; |
| 3387 | array->alloced = 0; |
| 3388 | array->list = NULL; |
| 3389 | } |