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