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