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