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