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