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