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