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