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