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