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