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