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