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