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