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> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include "hda_codec.h" |
| 30 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 31 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 33 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 35 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 36 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 37 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 39 | #define CREATE_TRACE_POINTS |
| 40 | #include "hda_trace.h" |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * vendor / preset table |
| 44 | */ |
| 45 | |
| 46 | struct hda_vendor_id { |
| 47 | unsigned int id; |
| 48 | const char *name; |
| 49 | }; |
| 50 | |
| 51 | /* codec vendor labels */ |
| 52 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 53 | { 0x1002, "ATI" }, |
Takashi Iwai | e5f1424 | 2009-07-01 18:11:44 +0200 | [diff] [blame] | 54 | { 0x1013, "Cirrus Logic" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 55 | { 0x1057, "Motorola" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 56 | { 0x1095, "Silicon Image" }, |
Takashi Iwai | 31117b7 | 2008-12-16 14:43:21 +0100 | [diff] [blame] | 57 | { 0x10de, "Nvidia" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 58 | { 0x10ec, "Realtek" }, |
Takashi Iwai | 4e01f54 | 2009-04-16 08:53:34 +0200 | [diff] [blame] | 59 | { 0x1102, "Creative" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 60 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 61 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 62 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 63 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 65 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 66 | { 0x17e8, "Chrontel" }, |
| 67 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 68 | { 0x1aec, "Wolfson Microelectronics" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { 0x434d, "C-Media" }, |
Takashi Iwai | 74c6113 | 2008-12-18 09:11:33 +0100 | [diff] [blame] | 70 | { 0x8086, "Intel" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 71 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | {} /* terminator */ |
| 73 | }; |
| 74 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 75 | static DEFINE_MUTEX(preset_mutex); |
| 76 | static LIST_HEAD(hda_preset_tables); |
| 77 | |
| 78 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 79 | { |
| 80 | mutex_lock(&preset_mutex); |
| 81 | list_add_tail(&preset->list, &hda_preset_tables); |
| 82 | mutex_unlock(&preset_mutex); |
| 83 | return 0; |
| 84 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 85 | EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 86 | |
| 87 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 88 | { |
| 89 | mutex_lock(&preset_mutex); |
| 90 | list_del(&preset->list); |
| 91 | mutex_unlock(&preset_mutex); |
| 92 | return 0; |
| 93 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 94 | EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 97 | static void hda_power_work(struct work_struct *work); |
| 98 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 99 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 100 | #else |
| 101 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 102 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 103 | #endif |
| 104 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 105 | /** |
| 106 | * snd_hda_get_jack_location - Give a location string of the jack |
| 107 | * @cfg: pin default config value |
| 108 | * |
| 109 | * Parse the pin default config value and returns the string of the |
| 110 | * jack location, e.g. "Rear", "Front", etc. |
| 111 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 112 | const char *snd_hda_get_jack_location(u32 cfg) |
| 113 | { |
| 114 | static char *bases[7] = { |
| 115 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 116 | }; |
| 117 | static unsigned char specials_idx[] = { |
| 118 | 0x07, 0x08, |
| 119 | 0x17, 0x18, 0x19, |
| 120 | 0x37, 0x38 |
| 121 | }; |
| 122 | static char *specials[] = { |
| 123 | "Rear Panel", "Drive Bar", |
| 124 | "Riser", "HDMI", "ATAPI", |
| 125 | "Mobile-In", "Mobile-Out" |
| 126 | }; |
| 127 | int i; |
| 128 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 129 | if ((cfg & 0x0f) < 7) |
| 130 | return bases[cfg & 0x0f]; |
| 131 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 132 | if (cfg == specials_idx[i]) |
| 133 | return specials[i]; |
| 134 | } |
| 135 | return "UNKNOWN"; |
| 136 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 137 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 138 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 139 | /** |
| 140 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 141 | * @cfg: pin default config value |
| 142 | * |
| 143 | * Parse the pin default config value and returns the string of the |
| 144 | * jack connectivity, i.e. external or internal connection. |
| 145 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 146 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 147 | { |
| 148 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 149 | |
| 150 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 151 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 153 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 154 | /** |
| 155 | * snd_hda_get_jack_type - Give a type string of the jack |
| 156 | * @cfg: pin default config value |
| 157 | * |
| 158 | * Parse the pin default config value and returns the string of the |
| 159 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 160 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 161 | const char *snd_hda_get_jack_type(u32 cfg) |
| 162 | { |
| 163 | static char *jack_types[16] = { |
| 164 | "Line Out", "Speaker", "HP Out", "CD", |
| 165 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 166 | "Line In", "Aux", "Mic", "Telephony", |
| 167 | "SPDIF In", "Digitial In", "Reserved", "Other" |
| 168 | }; |
| 169 | |
| 170 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 171 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 172 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 173 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 174 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 175 | /* |
| 176 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 177 | */ |
| 178 | static inline unsigned int |
| 179 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 180 | unsigned int verb, unsigned int parm) |
| 181 | { |
| 182 | u32 val; |
| 183 | |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 184 | if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || |
| 185 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 186 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", |
| 187 | codec->addr, direct, nid, verb, parm); |
| 188 | return ~0; |
| 189 | } |
| 190 | |
| 191 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 192 | val |= (u32)direct << 27; |
| 193 | val |= (u32)nid << 20; |
| 194 | val |= verb << 8; |
| 195 | val |= parm; |
| 196 | return val; |
| 197 | } |
| 198 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 199 | /* |
| 200 | * Send and receive a verb |
| 201 | */ |
| 202 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
| 203 | unsigned int *res) |
| 204 | { |
| 205 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 206 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 207 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 208 | if (cmd == ~0) |
| 209 | return -1; |
| 210 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 211 | if (res) |
| 212 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 213 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 214 | snd_hda_power_up(codec); |
| 215 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 216 | trace_hda_send_cmd(codec, cmd); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 217 | err = bus->ops.command(bus, cmd); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 218 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 219 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 220 | trace_hda_get_response(codec, *res); |
| 221 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 222 | mutex_unlock(&bus->cmd_mutex); |
| 223 | snd_hda_power_down(codec); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 224 | if (res && *res == -1 && bus->rirb_error) { |
| 225 | if (bus->response_reset) { |
| 226 | snd_printd("hda_codec: resetting BUS due to " |
| 227 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 228 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 229 | bus->ops.bus_reset(bus); |
| 230 | } |
| 231 | goto again; |
| 232 | } |
| 233 | /* clear reset-flag when the communication gets recovered */ |
| 234 | if (!err) |
| 235 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 236 | return err; |
| 237 | } |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /** |
| 240 | * snd_hda_codec_read - send a command and get the response |
| 241 | * @codec: the HDA codec |
| 242 | * @nid: NID to send the command |
| 243 | * @direct: direct flag |
| 244 | * @verb: the verb to send |
| 245 | * @parm: the parameter for the verb |
| 246 | * |
| 247 | * Send a single command and read the corresponding response. |
| 248 | * |
| 249 | * Returns the obtained response value, or -1 for an error. |
| 250 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 251 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 252 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | unsigned int verb, unsigned int parm) |
| 254 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 255 | unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
| 256 | unsigned int res; |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 257 | if (codec_exec_verb(codec, cmd, &res)) |
| 258 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return res; |
| 260 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 261 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * snd_hda_codec_write - send a single command without waiting for response |
| 265 | * @codec: the HDA codec |
| 266 | * @nid: NID to send the command |
| 267 | * @direct: direct flag |
| 268 | * @verb: the verb to send |
| 269 | * @parm: the parameter for the verb |
| 270 | * |
| 271 | * Send a single command without waiting for response. |
| 272 | * |
| 273 | * Returns 0 if successful, or a negative error code. |
| 274 | */ |
| 275 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 276 | unsigned int verb, unsigned int parm) |
| 277 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 278 | unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 279 | unsigned int res; |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 280 | return codec_exec_verb(codec, cmd, |
| 281 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 283 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * snd_hda_sequence_write - sequence writes |
| 287 | * @codec: the HDA codec |
| 288 | * @seq: VERB array to send |
| 289 | * |
| 290 | * Send the commands sequentially from the given array. |
| 291 | * The array must be terminated with NID=0. |
| 292 | */ |
| 293 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 294 | { |
| 295 | for (; seq->nid; seq++) |
| 296 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 297 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 298 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 302 | * @codec: the HDA codec |
| 303 | * @nid: NID to parse |
| 304 | * @start_id: the pointer to store the start NID |
| 305 | * |
| 306 | * Parse the NID and store the start NID of its sub-nodes. |
| 307 | * Returns the number of sub-nodes. |
| 308 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 309 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 310 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | unsigned int parm; |
| 313 | |
| 314 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 315 | if (parm == -1) |
| 316 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | *start_id = (parm >> 16) & 0x7fff; |
| 318 | return (int)(parm & 0x7fff); |
| 319 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 320 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 322 | /* look up the cached results */ |
| 323 | static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid) |
| 324 | { |
| 325 | int i, len; |
| 326 | for (i = 0; i < array->used; ) { |
| 327 | hda_nid_t *p = snd_array_elem(array, i); |
| 328 | if (nid == *p) |
| 329 | return p; |
| 330 | len = p[1]; |
| 331 | i += len + 2; |
| 332 | } |
| 333 | return NULL; |
| 334 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 337 | * snd_hda_get_conn_list - get connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * @codec: the HDA codec |
| 339 | * @nid: NID to parse |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 340 | * @listp: the pointer to store NID list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | * |
| 342 | * Parses the connection list of the given widget and stores the list |
| 343 | * of NIDs. |
| 344 | * |
| 345 | * Returns the number of connections, or a negative error code. |
| 346 | */ |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 347 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 348 | const hda_nid_t **listp) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 349 | { |
| 350 | struct snd_array *array = &codec->conn_lists; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 351 | int len, err; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 352 | hda_nid_t list[HDA_MAX_CONNECTIONS]; |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 353 | hda_nid_t *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 354 | bool added = false; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 355 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 356 | again: |
| 357 | /* if the connection-list is already cached, read it */ |
| 358 | p = lookup_conn_list(array, nid); |
| 359 | if (p) { |
| 360 | if (listp) |
| 361 | *listp = p + 2; |
| 362 | return p[1]; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 363 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 364 | if (snd_BUG_ON(added)) |
| 365 | return -EINVAL; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 366 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 367 | /* read the connection and add to the cache */ |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 368 | len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 369 | if (len < 0) |
| 370 | return len; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 371 | err = snd_hda_override_conn_list(codec, nid, len, list); |
| 372 | if (err < 0) |
| 373 | return err; |
| 374 | added = true; |
| 375 | goto again; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 376 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 377 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 378 | |
| 379 | /** |
| 380 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | * @codec: the HDA codec |
| 382 | * @nid: NID to parse |
| 383 | * @conn_list: connection list array |
| 384 | * @max_conns: max. number of connections to store |
| 385 | * |
| 386 | * Parses the connection list of the given widget and stores the list |
| 387 | * of NIDs. |
| 388 | * |
| 389 | * Returns the number of connections, or a negative error code. |
| 390 | */ |
| 391 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 392 | hda_nid_t *conn_list, int max_conns) |
| 393 | { |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 394 | const hda_nid_t *list; |
| 395 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 396 | |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 397 | if (len <= 0) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 398 | return len; |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 399 | if (len > max_conns) { |
| 400 | snd_printk(KERN_ERR "hda_codec: " |
| 401 | "Too many connections %d for NID 0x%x\n", |
| 402 | len, nid); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
| 406 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 409 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 410 | /** |
| 411 | * snd_hda_get_raw_connections - copy connection list without cache |
| 412 | * @codec: the HDA codec |
| 413 | * @nid: NID to parse |
| 414 | * @conn_list: connection list array |
| 415 | * @max_conns: max. number of connections to store |
| 416 | * |
| 417 | * Like snd_hda_get_connections(), copy the connection list but without |
| 418 | * checking through the connection-list cache. |
| 419 | * Currently called only from hda_proc.c, so not exported. |
| 420 | */ |
| 421 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 422 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 425 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 427 | unsigned int wcaps; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 428 | hda_nid_t prev_nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 430 | if (snd_BUG_ON(!conn_list || max_conns <= 0)) |
| 431 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 433 | wcaps = get_wcaps(codec, nid); |
| 434 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 435 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 436 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 439 | if (parm & AC_CLIST_LONG) { |
| 440 | /* long form */ |
| 441 | shift = 16; |
| 442 | num_elems = 2; |
| 443 | } else { |
| 444 | /* short form */ |
| 445 | shift = 8; |
| 446 | num_elems = 4; |
| 447 | } |
| 448 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | mask = (1 << (shift-1)) - 1; |
| 450 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 451 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return 0; /* no connection */ |
| 453 | |
| 454 | if (conn_len == 1) { |
| 455 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 456 | parm = snd_hda_codec_read(codec, nid, 0, |
| 457 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 458 | if (parm == -1 && codec->bus->rirb_error) |
| 459 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | conn_list[0] = parm & mask; |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | /* multi connection */ |
| 465 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 466 | prev_nid = 0; |
| 467 | for (i = 0; i < conn_len; i++) { |
| 468 | int range_val; |
| 469 | hda_nid_t val, n; |
| 470 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 471 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 472 | parm = snd_hda_codec_read(codec, nid, 0, |
| 473 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 474 | if (parm == -1 && codec->bus->rirb_error) |
| 475 | return -EIO; |
| 476 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 477 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 478 | val = parm & mask; |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 479 | if (val == 0) { |
| 480 | snd_printk(KERN_WARNING "hda_codec: " |
| 481 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 482 | nid, i, parm); |
| 483 | return 0; |
| 484 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 485 | parm >>= shift; |
| 486 | if (range_val) { |
| 487 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 488 | if (!prev_nid || prev_nid >= val) { |
| 489 | snd_printk(KERN_WARNING "hda_codec: " |
| 490 | "invalid dep_range_val %x:%x\n", |
| 491 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 492 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 494 | for (n = prev_nid + 1; n <= val; n++) { |
| 495 | if (conns >= max_conns) { |
Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 496 | snd_printk(KERN_ERR "hda_codec: " |
| 497 | "Too many connections %d for NID 0x%x\n", |
| 498 | conns, nid); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 499 | return -EINVAL; |
| 500 | } |
| 501 | conn_list[conns++] = n; |
| 502 | } |
| 503 | } else { |
| 504 | if (conns >= max_conns) { |
Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 505 | snd_printk(KERN_ERR "hda_codec: " |
| 506 | "Too many connections %d for NID 0x%x\n", |
| 507 | conns, nid); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 508 | return -EINVAL; |
| 509 | } |
| 510 | conn_list[conns++] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 512 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | return conns; |
| 515 | } |
| 516 | |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 517 | static bool add_conn_list(struct snd_array *array, hda_nid_t nid) |
| 518 | { |
| 519 | hda_nid_t *p = snd_array_new(array); |
| 520 | if (!p) |
| 521 | return false; |
| 522 | *p = nid; |
| 523 | return true; |
| 524 | } |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 527 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 528 | * @codec: the HDA codec |
| 529 | * @nid: NID to parse |
| 530 | * @len: number of connection list entries |
| 531 | * @list: the list of connection entries |
| 532 | * |
| 533 | * Add or modify the given connection-list to the cache. If the corresponding |
| 534 | * cache already exists, invalidate it and append a new one. |
| 535 | * |
| 536 | * Returns zero or a negative error code. |
| 537 | */ |
| 538 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 539 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 541 | struct snd_array *array = &codec->conn_lists; |
| 542 | hda_nid_t *p; |
| 543 | int i, old_used; |
| 544 | |
| 545 | p = lookup_conn_list(array, nid); |
| 546 | if (p) |
| 547 | *p = -1; /* invalidate the old entry */ |
| 548 | |
| 549 | old_used = array->used; |
| 550 | if (!add_conn_list(array, nid) || !add_conn_list(array, len)) |
| 551 | goto error_add; |
| 552 | for (i = 0; i < len; i++) |
| 553 | if (!add_conn_list(array, list[i])) |
| 554 | goto error_add; |
| 555 | return 0; |
| 556 | |
| 557 | error_add: |
| 558 | array->used = old_used; |
| 559 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 561 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 562 | |
| 563 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 564 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 565 | * @codec: the HDA codec |
| 566 | * @mux: NID containing the list |
| 567 | * @nid: NID to select |
| 568 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 569 | * |
| 570 | * Parses the connection list of the widget @mux and checks whether the |
| 571 | * widget @nid is present. If it is, return the connection index. |
| 572 | * Otherwise it returns -1. |
| 573 | */ |
| 574 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 575 | hda_nid_t nid, int recursive) |
| 576 | { |
| 577 | hda_nid_t conn[HDA_MAX_NUM_INPUTS]; |
| 578 | int i, nums; |
| 579 | |
| 580 | nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn)); |
| 581 | for (i = 0; i < nums; i++) |
| 582 | if (conn[i] == nid) |
| 583 | return i; |
| 584 | if (!recursive) |
| 585 | return -1; |
| 586 | if (recursive > 5) { |
| 587 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 588 | return -1; |
| 589 | } |
| 590 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 591 | for (i = 0; i < nums; i++) { |
| 592 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 593 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 594 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 595 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 596 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 597 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 598 | return -1; |
| 599 | } |
| 600 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | /** |
| 603 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 604 | * @bus: the BUS |
| 605 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 606 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 607 | * |
| 608 | * Adds the given event to the queue. The events are processed in |
| 609 | * the workqueue asynchronously. Call this function in the interrupt |
| 610 | * hanlder when RIRB receives an unsolicited event. |
| 611 | * |
| 612 | * Returns 0 if successful, or a negative error code. |
| 613 | */ |
| 614 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 615 | { |
| 616 | struct hda_bus_unsolicited *unsol; |
| 617 | unsigned int wp; |
| 618 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 619 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 620 | unsol = bus->unsol; |
| 621 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | |
| 624 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 625 | unsol->wp = wp; |
| 626 | |
| 627 | wp <<= 1; |
| 628 | unsol->queue[wp] = res; |
| 629 | unsol->queue[wp + 1] = res_ex; |
| 630 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 631 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | return 0; |
| 634 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 635 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 638 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 640 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 642 | struct hda_bus_unsolicited *unsol = |
| 643 | container_of(work, struct hda_bus_unsolicited, work); |
| 644 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | struct hda_codec *codec; |
| 646 | unsigned int rp, caddr, res; |
| 647 | |
| 648 | while (unsol->rp != unsol->wp) { |
| 649 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 650 | unsol->rp = rp; |
| 651 | rp <<= 1; |
| 652 | res = unsol->queue[rp]; |
| 653 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 654 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | continue; |
| 656 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 657 | if (codec && codec->patch_ops.unsol_event) |
| 658 | codec->patch_ops.unsol_event(codec, res); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * initialize unsolicited queue |
| 664 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 665 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
| 667 | struct hda_bus_unsolicited *unsol; |
| 668 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 669 | if (bus->unsol) /* already initialized */ |
| 670 | return 0; |
| 671 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 672 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 673 | if (!unsol) { |
| 674 | snd_printk(KERN_ERR "hda_codec: " |
| 675 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return -ENOMEM; |
| 677 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 678 | INIT_WORK(&unsol->work, process_unsol_events); |
| 679 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | bus->unsol = unsol; |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * destructor |
| 686 | */ |
| 687 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 688 | |
| 689 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 690 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 691 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 693 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 695 | if (bus->workq) |
| 696 | flush_workqueue(bus->workq); |
| 697 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 699 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | snd_hda_codec_free(codec); |
| 701 | } |
| 702 | if (bus->ops.private_free) |
| 703 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 704 | if (bus->workq) |
| 705 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | kfree(bus); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 710 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 713 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | return snd_hda_bus_free(bus); |
| 715 | } |
| 716 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 717 | #ifdef CONFIG_SND_HDA_HWDEP |
| 718 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 719 | { |
| 720 | struct hda_bus *bus = device->device_data; |
| 721 | struct hda_codec *codec; |
| 722 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 723 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 724 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | #else |
| 729 | #define snd_hda_bus_dev_register NULL |
| 730 | #endif |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | /** |
| 733 | * snd_hda_bus_new - create a HDA bus |
| 734 | * @card: the card entry |
| 735 | * @temp: the template for hda_bus information |
| 736 | * @busp: the pointer to store the created bus instance |
| 737 | * |
| 738 | * Returns 0 if successful, or a negative error code. |
| 739 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 740 | int /*__devinit*/ snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 741 | const struct hda_bus_template *temp, |
| 742 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
| 744 | struct hda_bus *bus; |
| 745 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 746 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 747 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | .dev_free = snd_hda_bus_dev_free, |
| 749 | }; |
| 750 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 751 | if (snd_BUG_ON(!temp)) |
| 752 | return -EINVAL; |
| 753 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 754 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | if (busp) |
| 757 | *busp = NULL; |
| 758 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 759 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | if (bus == NULL) { |
| 761 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 762 | return -ENOMEM; |
| 763 | } |
| 764 | |
| 765 | bus->card = card; |
| 766 | bus->private_data = temp->private_data; |
| 767 | bus->pci = temp->pci; |
| 768 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 769 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | bus->ops = temp->ops; |
| 771 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 772 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 773 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | INIT_LIST_HEAD(&bus->codec_list); |
| 775 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 776 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 777 | "hd-audio%d", card->number); |
| 778 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 779 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 780 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 781 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 782 | kfree(bus); |
| 783 | return -ENOMEM; |
| 784 | } |
| 785 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 786 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 787 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | snd_hda_bus_free(bus); |
| 789 | return err; |
| 790 | } |
| 791 | if (busp) |
| 792 | *busp = bus; |
| 793 | return 0; |
| 794 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 795 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 797 | #ifdef CONFIG_SND_HDA_GENERIC |
| 798 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 799 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 800 | #else |
| 801 | #define is_generic_config(codec) 0 |
| 802 | #endif |
| 803 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 804 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 805 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 806 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 807 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 808 | #endif |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /* |
| 811 | * find a matching codec preset |
| 812 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 813 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 814 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 816 | struct hda_codec_preset_list *tbl; |
| 817 | const struct hda_codec_preset *preset; |
| 818 | int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 820 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 821 | return NULL; /* use the generic parser */ |
| 822 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 823 | again: |
| 824 | mutex_lock(&preset_mutex); |
| 825 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 826 | if (!try_module_get(tbl->owner)) { |
| 827 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 828 | continue; |
| 829 | } |
| 830 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 832 | if (preset->afg && preset->afg != codec->afg) |
| 833 | continue; |
| 834 | if (preset->mfg && preset->mfg != codec->mfg) |
| 835 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 836 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 838 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 839 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 840 | preset->rev == codec->revision_id)) { |
| 841 | mutex_unlock(&preset_mutex); |
| 842 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 846 | module_put(tbl->owner); |
| 847 | } |
| 848 | mutex_unlock(&preset_mutex); |
| 849 | |
| 850 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 851 | char name[32]; |
| 852 | if (!mod_requested) |
| 853 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 854 | codec->vendor_id); |
| 855 | else |
| 856 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 857 | (codec->vendor_id >> 16) & 0xffff); |
| 858 | request_module(name); |
| 859 | mod_requested++; |
| 860 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 866 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 868 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
| 870 | const struct hda_vendor_id *c; |
| 871 | const char *vendor = NULL; |
| 872 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 873 | char tmp[16]; |
| 874 | |
| 875 | if (codec->vendor_name) |
| 876 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | for (c = hda_vendor_ids; c->id; c++) { |
| 879 | if (c->id == vendor_id) { |
| 880 | vendor = c->name; |
| 881 | break; |
| 882 | } |
| 883 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 884 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | sprintf(tmp, "Generic %04x", vendor_id); |
| 886 | vendor = tmp; |
| 887 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 888 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 889 | if (!codec->vendor_name) |
| 890 | return -ENOMEM; |
| 891 | |
| 892 | get_chip_name: |
| 893 | if (codec->chip_name) |
| 894 | return 0; |
| 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 897 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 898 | else { |
| 899 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 900 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 901 | } |
| 902 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 903 | return -ENOMEM; |
| 904 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 908 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 910 | static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 912 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | hda_nid_t nid; |
| 914 | |
| 915 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 916 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 917 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 918 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 919 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 920 | case AC_GRP_AUDIO_FUNCTION: |
| 921 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 922 | codec->afg_function_id = function_id & 0xff; |
| 923 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 924 | break; |
| 925 | case AC_GRP_MODEM_FUNCTION: |
| 926 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 927 | codec->mfg_function_id = function_id & 0xff; |
| 928 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 929 | break; |
| 930 | default: |
| 931 | break; |
| 932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 937 | * read widget caps for each widget and store in cache |
| 938 | */ |
| 939 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 940 | { |
| 941 | int i; |
| 942 | hda_nid_t nid; |
| 943 | |
| 944 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 945 | &codec->start_nid); |
| 946 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 947 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 948 | return -ENOMEM; |
| 949 | nid = codec->start_nid; |
| 950 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 951 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 952 | AC_PAR_AUDIO_WIDGET_CAP); |
| 953 | return 0; |
| 954 | } |
| 955 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 956 | /* read all pin default configurations and save codec->init_pins */ |
| 957 | static int read_pin_defaults(struct hda_codec *codec) |
| 958 | { |
| 959 | int i; |
| 960 | hda_nid_t nid = codec->start_nid; |
| 961 | |
| 962 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 963 | struct hda_pincfg *pin; |
| 964 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 965 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 966 | if (wid_type != AC_WID_PIN) |
| 967 | continue; |
| 968 | pin = snd_array_new(&codec->init_pins); |
| 969 | if (!pin) |
| 970 | return -ENOMEM; |
| 971 | pin->nid = nid; |
| 972 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 973 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 974 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 975 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 976 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 977 | } |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | /* look up the given pin config list and return the item matching with NID */ |
| 982 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 983 | struct snd_array *array, |
| 984 | hda_nid_t nid) |
| 985 | { |
| 986 | int i; |
| 987 | for (i = 0; i < array->used; i++) { |
| 988 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 989 | if (pin->nid == nid) |
| 990 | return pin; |
| 991 | } |
| 992 | return NULL; |
| 993 | } |
| 994 | |
| 995 | /* write a config value for the given NID */ |
| 996 | static void set_pincfg(struct hda_codec *codec, hda_nid_t nid, |
| 997 | unsigned int cfg) |
| 998 | { |
| 999 | int i; |
| 1000 | for (i = 0; i < 4; i++) { |
| 1001 | snd_hda_codec_write(codec, nid, 0, |
| 1002 | AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i, |
| 1003 | cfg & 0xff); |
| 1004 | cfg >>= 8; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* set the current pin config value for the given NID. |
| 1009 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1010 | */ |
| 1011 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1012 | hda_nid_t nid, unsigned int cfg) |
| 1013 | { |
| 1014 | struct hda_pincfg *pin; |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1015 | unsigned int oldcfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1016 | |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1017 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1018 | return -EINVAL; |
| 1019 | |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1020 | oldcfg = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1021 | pin = look_up_pincfg(codec, list, nid); |
| 1022 | if (!pin) { |
| 1023 | pin = snd_array_new(list); |
| 1024 | if (!pin) |
| 1025 | return -ENOMEM; |
| 1026 | pin->nid = nid; |
| 1027 | } |
| 1028 | pin->cfg = cfg; |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1029 | |
| 1030 | /* change only when needed; e.g. if the pincfg is already present |
| 1031 | * in user_pins[], don't write it |
| 1032 | */ |
| 1033 | cfg = snd_hda_codec_get_pincfg(codec, nid); |
| 1034 | if (oldcfg != cfg) |
| 1035 | set_pincfg(codec, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1039 | /** |
| 1040 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1041 | * @codec: the HDA codec |
| 1042 | * @nid: NID to set the pin config |
| 1043 | * @cfg: the pin default config value |
| 1044 | * |
| 1045 | * Override a pin default configuration value in the cache. |
| 1046 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1047 | * priority than the real hardware value. |
| 1048 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1049 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1050 | hda_nid_t nid, unsigned int cfg) |
| 1051 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1052 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1053 | } |
| 1054 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1055 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1056 | /** |
| 1057 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1058 | * @codec: the HDA codec |
| 1059 | * @nid: NID to get the pin config |
| 1060 | * |
| 1061 | * Get the current pin config value of the given pin NID. |
| 1062 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1063 | * returns the cached value. |
| 1064 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1065 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1066 | { |
| 1067 | struct hda_pincfg *pin; |
| 1068 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1069 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1070 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1071 | if (pin) |
| 1072 | return pin->cfg; |
| 1073 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1074 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1075 | if (pin) |
| 1076 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1077 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1078 | if (pin) |
| 1079 | return pin->cfg; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1083 | |
| 1084 | /* restore all current pin configs */ |
| 1085 | static void restore_pincfgs(struct hda_codec *codec) |
| 1086 | { |
| 1087 | int i; |
| 1088 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1089 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1090 | set_pincfg(codec, pin->nid, |
| 1091 | snd_hda_codec_get_pincfg(codec, pin->nid)); |
| 1092 | } |
| 1093 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1094 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1095 | /** |
| 1096 | * snd_hda_shutup_pins - Shut up all pins |
| 1097 | * @codec: the HDA codec |
| 1098 | * |
| 1099 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1100 | * The controls aren't cached so that they can be resumed properly. |
| 1101 | */ |
| 1102 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1103 | { |
| 1104 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1105 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1106 | * the default pin setup at the next load of the driver |
| 1107 | */ |
| 1108 | if (codec->bus->shutdown) |
| 1109 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1110 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1111 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1112 | /* use read here for syncing after issuing each verb */ |
| 1113 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1114 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1115 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1116 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1117 | } |
| 1118 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1119 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1120 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1121 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1122 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1123 | { |
| 1124 | int i; |
| 1125 | if (!codec->pins_shutup) |
| 1126 | return; |
| 1127 | if (codec->bus->shutdown) |
| 1128 | return; |
| 1129 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1130 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1131 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1132 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1133 | pin->ctrl); |
| 1134 | } |
| 1135 | codec->pins_shutup = 0; |
| 1136 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1137 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1138 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1139 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1140 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1141 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1142 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1143 | /* restore the initial pin cfgs and release all pincfg lists */ |
| 1144 | static void restore_init_pincfgs(struct hda_codec *codec) |
| 1145 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1146 | /* first free driver_pins and user_pins, then call restore_pincfg |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1147 | * so that only the values in init_pins are restored |
| 1148 | */ |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1149 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1150 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1151 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1152 | #endif |
| 1153 | restore_pincfgs(codec); |
| 1154 | snd_array_free(&codec->init_pins); |
| 1155 | } |
| 1156 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1157 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1158 | * audio-converter setup caches |
| 1159 | */ |
| 1160 | struct hda_cvt_setup { |
| 1161 | hda_nid_t nid; |
| 1162 | u8 stream_tag; |
| 1163 | u8 channel_id; |
| 1164 | u16 format_id; |
| 1165 | unsigned char active; /* cvt is currently used */ |
| 1166 | unsigned char dirty; /* setups should be cleared */ |
| 1167 | }; |
| 1168 | |
| 1169 | /* get or create a cache entry for the given audio converter NID */ |
| 1170 | static struct hda_cvt_setup * |
| 1171 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1172 | { |
| 1173 | struct hda_cvt_setup *p; |
| 1174 | int i; |
| 1175 | |
| 1176 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1177 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1178 | if (p->nid == nid) |
| 1179 | return p; |
| 1180 | } |
| 1181 | p = snd_array_new(&codec->cvt_setups); |
| 1182 | if (p) |
| 1183 | p->nid = nid; |
| 1184 | return p; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | * codec destructor |
| 1189 | */ |
| 1190 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1191 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1192 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | return; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1194 | restore_init_pincfgs(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1195 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1196 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1197 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1198 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1200 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1201 | snd_array_free(&codec->nids); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1202 | snd_array_free(&codec->conn_lists); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1203 | snd_array_free(&codec->spdif_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1205 | if (codec->patch_ops.free) |
| 1206 | codec->patch_ops.free(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1207 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1208 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1209 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1210 | kfree(codec->vendor_name); |
| 1211 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1212 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1213 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | kfree(codec); |
| 1215 | } |
| 1216 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1217 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 1218 | unsigned int power_state); |
| 1219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | /** |
| 1221 | * snd_hda_codec_new - create a HDA codec |
| 1222 | * @bus: the bus to assign |
| 1223 | * @codec_addr: the codec address |
| 1224 | * @codecp: the pointer to store the generated codec |
| 1225 | * |
| 1226 | * Returns 0 if successful, or a negative error code. |
| 1227 | */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1228 | int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, |
| 1229 | unsigned int codec_addr, |
| 1230 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { |
| 1232 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1233 | char component[31]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | int err; |
| 1235 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1236 | if (snd_BUG_ON(!bus)) |
| 1237 | return -EINVAL; |
| 1238 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1239 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1242 | snd_printk(KERN_ERR "hda_codec: " |
| 1243 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | return -EBUSY; |
| 1245 | } |
| 1246 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1247 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | if (codec == NULL) { |
| 1249 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1250 | return -ENOMEM; |
| 1251 | } |
| 1252 | |
| 1253 | codec->bus = bus; |
| 1254 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1255 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1256 | mutex_init(&codec->control_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1257 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1258 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1259 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1260 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1261 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1262 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1263 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1264 | snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1265 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1266 | if (codec->bus->modelname) { |
| 1267 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1268 | if (!codec->modelname) { |
| 1269 | snd_hda_codec_free(codec); |
| 1270 | return -ENODEV; |
| 1271 | } |
| 1272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1274 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1275 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1276 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1277 | * the caller has to power down appropriatley after initialization |
| 1278 | * phase. |
| 1279 | */ |
| 1280 | hda_keep_power_on(codec); |
| 1281 | #endif |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | list_add_tail(&codec->list, &bus->codec_list); |
| 1284 | bus->caddr_tbl[codec_addr] = codec; |
| 1285 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1286 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1287 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1288 | if (codec->vendor_id == -1) |
| 1289 | /* read again, hopefully the access method was corrected |
| 1290 | * in the last read... |
| 1291 | */ |
| 1292 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1293 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1294 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1295 | AC_PAR_SUBSYSTEM_ID); |
| 1296 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1297 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1299 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1300 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1301 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1302 | err = -ENODEV; |
| 1303 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1306 | err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg); |
| 1307 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1308 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1309 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1310 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1311 | err = read_pin_defaults(codec); |
| 1312 | if (err < 0) |
| 1313 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1314 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1315 | if (!codec->subsystem_id) { |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1316 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1317 | codec->subsystem_id = |
| 1318 | snd_hda_codec_read(codec, nid, 0, |
| 1319 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1322 | /* power-up all before initialization */ |
| 1323 | hda_set_power_state(codec, |
| 1324 | codec->afg ? codec->afg : codec->mfg, |
| 1325 | AC_PWRST_D0); |
| 1326 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1327 | snd_hda_codec_proc_new(codec); |
| 1328 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1329 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1330 | |
| 1331 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1332 | codec->subsystem_id, codec->revision_id); |
| 1333 | snd_component_add(codec->bus->card, component); |
| 1334 | |
| 1335 | if (codecp) |
| 1336 | *codecp = codec; |
| 1337 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1338 | |
| 1339 | error: |
| 1340 | snd_hda_codec_free(codec); |
| 1341 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1342 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1343 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1344 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1345 | /** |
| 1346 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1347 | * @codec: the HDA codec |
| 1348 | * |
| 1349 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1350 | * patch instance. |
| 1351 | * |
| 1352 | * Returns 0 if successful or a negative error code. |
| 1353 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1354 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1355 | { |
| 1356 | int err; |
| 1357 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1358 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1359 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1360 | err = get_codec_name(codec); |
| 1361 | if (err < 0) |
| 1362 | return err; |
| 1363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1365 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1367 | goto patched; |
| 1368 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1369 | if (codec->preset && codec->preset->patch) { |
| 1370 | err = codec->preset->patch(codec); |
| 1371 | goto patched; |
| 1372 | } |
| 1373 | |
| 1374 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1375 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 1376 | if (err < 0) |
| 1377 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1378 | |
| 1379 | patched: |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1380 | if (!err && codec->patch_ops.unsol_event) |
| 1381 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1382 | /* audio codec should override the mixer name */ |
| 1383 | if (!err && (codec->afg || !*codec->bus->card->mixername)) |
| 1384 | snprintf(codec->bus->card->mixername, |
| 1385 | sizeof(codec->bus->card->mixername), |
| 1386 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1387 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1389 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
| 1391 | /** |
| 1392 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1393 | * @codec: the CODEC to set up |
| 1394 | * @nid: the NID to set up |
| 1395 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1396 | * @channel_id: channel id to pass, zero based. |
| 1397 | * @format: stream format. |
| 1398 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1399 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1400 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | int channel_id, int format) |
| 1402 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1403 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1404 | struct hda_cvt_setup *p; |
| 1405 | unsigned int oldval, newval; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1406 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1407 | int i; |
| 1408 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1409 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1410 | return; |
| 1411 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1412 | snd_printdd("hda_codec_setup_stream: " |
| 1413 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1415 | p = get_hda_cvt_setup(codec, nid); |
| 1416 | if (!p) |
| 1417 | return; |
| 1418 | /* update the stream-id if changed */ |
| 1419 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1420 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1421 | newval = (stream_tag << 4) | channel_id; |
| 1422 | if (oldval != newval) |
| 1423 | snd_hda_codec_write(codec, nid, 0, |
| 1424 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1425 | newval); |
| 1426 | p->stream_tag = stream_tag; |
| 1427 | p->channel_id = channel_id; |
| 1428 | } |
| 1429 | /* update the format-id if changed */ |
| 1430 | if (p->format_id != format) { |
| 1431 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1432 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1433 | if (oldval != format) { |
| 1434 | msleep(1); |
| 1435 | snd_hda_codec_write(codec, nid, 0, |
| 1436 | AC_VERB_SET_STREAM_FORMAT, |
| 1437 | format); |
| 1438 | } |
| 1439 | p->format_id = format; |
| 1440 | } |
| 1441 | p->active = 1; |
| 1442 | p->dirty = 0; |
| 1443 | |
| 1444 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1445 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1446 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1447 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1448 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1449 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1450 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1451 | p->dirty = 1; |
| 1452 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1455 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1457 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1458 | struct hda_cvt_setup *q); |
| 1459 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1460 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1461 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1462 | * @codec: the CODEC to clean up |
| 1463 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1464 | * @do_now: really clean up the stream instead of clearing the active flag |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1465 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1466 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1467 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1468 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1469 | struct hda_cvt_setup *p; |
| 1470 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1471 | if (!nid) |
| 1472 | return; |
| 1473 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1474 | if (codec->no_sticky_stream) |
| 1475 | do_now = 1; |
| 1476 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1477 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1478 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1479 | if (p) { |
| 1480 | /* here we just clear the active flag when do_now isn't set; |
| 1481 | * actual clean-ups will be done later in |
| 1482 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1483 | */ |
| 1484 | if (do_now) |
| 1485 | really_cleanup_stream(codec, p); |
| 1486 | else |
| 1487 | p->active = 0; |
| 1488 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1489 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1490 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1491 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1492 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1493 | struct hda_cvt_setup *q) |
| 1494 | { |
| 1495 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1496 | if (q->stream_tag || q->channel_id) |
| 1497 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1498 | if (q->format_id) |
| 1499 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1500 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1501 | memset(q, 0, sizeof(*q)); |
| 1502 | q->nid = nid; |
| 1503 | } |
| 1504 | |
| 1505 | /* clean up the all conflicting obsolete streams */ |
| 1506 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1507 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1508 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1509 | int i; |
| 1510 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1511 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1512 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1513 | struct hda_cvt_setup *p; |
| 1514 | p = snd_array_elem(&c->cvt_setups, i); |
| 1515 | if (p->dirty) |
| 1516 | really_cleanup_stream(c, p); |
| 1517 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1521 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1522 | /* clean up all streams; called from suspend */ |
| 1523 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1524 | { |
| 1525 | int i; |
| 1526 | |
| 1527 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1528 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1529 | if (p->stream_tag) |
| 1530 | really_cleanup_stream(codec, p); |
| 1531 | } |
| 1532 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1533 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | /* |
| 1536 | * amp access functions |
| 1537 | */ |
| 1538 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1539 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1540 | #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] | 1541 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1542 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1543 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1545 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | /* initialize the hash table */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1548 | static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1549 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1551 | memset(cache, 0, sizeof(*cache)); |
| 1552 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1553 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1556 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1557 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1558 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1562 | static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1564 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1565 | u16 cur = cache->hash[idx]; |
| 1566 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
| 1568 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1569 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | if (info->key == key) |
| 1571 | return info; |
| 1572 | cur = info->next; |
| 1573 | } |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1574 | return NULL; |
| 1575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1577 | /* query the hash. allocate an entry if not found. */ |
| 1578 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1579 | u32 key) |
| 1580 | { |
| 1581 | struct hda_cache_head *info = get_hash(cache, key); |
| 1582 | if (!info) { |
| 1583 | u16 idx, cur; |
| 1584 | /* add a new hash entry */ |
| 1585 | info = snd_array_new(&cache->buf); |
| 1586 | if (!info) |
| 1587 | return NULL; |
| 1588 | cur = snd_array_index(&cache->buf, info); |
| 1589 | info->key = key; |
| 1590 | info->val = 0; |
| 1591 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1592 | info->next = cache->hash[idx]; |
| 1593 | cache->hash[idx] = cur; |
| 1594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | return info; |
| 1596 | } |
| 1597 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1598 | /* query and allocate an amp hash entry */ |
| 1599 | static inline struct hda_amp_info * |
| 1600 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1601 | { |
| 1602 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1603 | } |
| 1604 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1605 | /** |
| 1606 | * query_amp_caps - query AMP capabilities |
| 1607 | * @codec: the HD-auio codec |
| 1608 | * @nid: the NID to query |
| 1609 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1610 | * |
| 1611 | * Query AMP capabilities for the given widget and direction. |
| 1612 | * Returns the obtained capability bits. |
| 1613 | * |
| 1614 | * When cap bits have been already read, this doesn't read again but |
| 1615 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1617 | 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] | 1618 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1619 | struct hda_amp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1621 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
| 1622 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | return 0; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1624 | if (!(info->head.val & INFO_AMP_CAPS)) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1625 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | nid = codec->afg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1627 | info->amp_caps = snd_hda_param_read(codec, nid, |
| 1628 | direction == HDA_OUTPUT ? |
| 1629 | AC_PAR_AMP_OUT_CAP : |
| 1630 | AC_PAR_AMP_IN_CAP); |
Takashi Iwai | b75e53f | 2007-05-10 16:56:09 +0200 | [diff] [blame] | 1631 | if (info->amp_caps) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1632 | info->head.val |= INFO_AMP_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | return info->amp_caps; |
| 1635 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1636 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1638 | /** |
| 1639 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1640 | * @codec: the CODEC to clean up |
| 1641 | * @nid: the NID to clean up |
| 1642 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1643 | * @caps: the capability bits to set |
| 1644 | * |
| 1645 | * Override the cached AMP caps bits value by the given one. |
| 1646 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1647 | * e.g. limit to 0dB, etc. |
| 1648 | * |
| 1649 | * Returns zero if successful or a negative error code. |
| 1650 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1651 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1652 | unsigned int caps) |
| 1653 | { |
| 1654 | struct hda_amp_info *info; |
| 1655 | |
| 1656 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0)); |
| 1657 | if (!info) |
| 1658 | return -EINVAL; |
| 1659 | info->amp_caps = caps; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1660 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1661 | return 0; |
| 1662 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1663 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1664 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1665 | static unsigned int |
| 1666 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key, |
| 1667 | unsigned int (*func)(struct hda_codec *, hda_nid_t)) |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1668 | { |
| 1669 | struct hda_amp_info *info; |
| 1670 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1671 | info = get_alloc_amp_hash(codec, key); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1672 | if (!info) |
| 1673 | return 0; |
| 1674 | if (!info->head.val) { |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1675 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1676 | info->amp_caps = func(codec, nid); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1677 | } |
| 1678 | return info->amp_caps; |
| 1679 | } |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1680 | |
| 1681 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid) |
| 1682 | { |
| 1683 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1684 | } |
| 1685 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1686 | /** |
| 1687 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1688 | * @codec: the HD-auio codec |
| 1689 | * @nid: the NID to query |
| 1690 | * |
| 1691 | * Query PIN capabilities for the given widget. |
| 1692 | * Returns the obtained capability bits. |
| 1693 | * |
| 1694 | * When cap bits have been already read, this doesn't read again but |
| 1695 | * returns the cached value. |
| 1696 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1697 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1698 | { |
| 1699 | return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid), |
| 1700 | read_pin_cap); |
| 1701 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1702 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 1703 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1704 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1705 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1706 | * @codec: the CODEC |
| 1707 | * @nid: the NID to override |
| 1708 | * @caps: the capability bits to set |
| 1709 | * |
| 1710 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1711 | * |
| 1712 | * Returns zero if successful or a negative error code. |
| 1713 | */ |
| 1714 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1715 | unsigned int caps) |
| 1716 | { |
| 1717 | struct hda_amp_info *info; |
| 1718 | info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid)); |
| 1719 | if (!info) |
| 1720 | return -ENOMEM; |
| 1721 | info->amp_caps = caps; |
| 1722 | info->head.val |= INFO_AMP_CAPS; |
| 1723 | return 0; |
| 1724 | } |
| 1725 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 1726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | /* |
| 1728 | * read the current volume to info |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1729 | * if the cache exists, read the cache value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1731 | static unsigned int get_vol_mute(struct hda_codec *codec, |
| 1732 | struct hda_amp_info *info, hda_nid_t nid, |
| 1733 | int ch, int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
| 1735 | u32 val, parm; |
| 1736 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1737 | if (info->head.val & INFO_AMP_VOL(ch)) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1738 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
| 1740 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1741 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1742 | parm |= index; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1743 | val = snd_hda_codec_read(codec, nid, 0, |
| 1744 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | info->vol[ch] = val & 0xff; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1746 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1747 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1751 | * 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] | 1752 | */ |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1753 | 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] | 1754 | hda_nid_t nid, int ch, int direction, int index, |
| 1755 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | { |
| 1757 | u32 parm; |
| 1758 | |
| 1759 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1760 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1761 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame^] | 1762 | if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) && |
| 1763 | (info->amp_caps & AC_AMPCAP_MIN_MUTE)) |
| 1764 | ; /* set the zero value as a fake mute */ |
| 1765 | else |
| 1766 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | 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] | 1768 | info->vol[ch] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1771 | /** |
| 1772 | * snd_hda_codec_amp_read - Read AMP value |
| 1773 | * @codec: HD-audio codec |
| 1774 | * @nid: NID to read the AMP value |
| 1775 | * @ch: channel (left=0 or right=1) |
| 1776 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1777 | * @index: the index value (only for input direction) |
| 1778 | * |
| 1779 | * 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] | 1780 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1781 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1782 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1784 | struct hda_amp_info *info; |
| 1785 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1786 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1788 | return get_vol_mute(codec, info, nid, ch, direction, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1790 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1792 | /** |
| 1793 | * snd_hda_codec_amp_update - update the AMP value |
| 1794 | * @codec: HD-audio codec |
| 1795 | * @nid: NID to read the AMP value |
| 1796 | * @ch: channel (left=0 or right=1) |
| 1797 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1798 | * @idx: the index value (only for input direction) |
| 1799 | * @mask: bit mask to set |
| 1800 | * @val: the bits value to set |
| 1801 | * |
| 1802 | * Update the AMP value with a bit mask. |
| 1803 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1804 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1805 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1806 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1808 | struct hda_amp_info *info; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1809 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1810 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
| 1811 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | return 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1813 | if (snd_BUG_ON(mask & ~0xff)) |
| 1814 | mask &= 0xff; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1815 | val &= mask; |
| 1816 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1817 | if (info->vol[ch] == val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1819 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | return 1; |
| 1821 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1822 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1824 | /** |
| 1825 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1826 | * @codec: HD-audio codec |
| 1827 | * @nid: NID to read the AMP value |
| 1828 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1829 | * @idx: the index value (only for input direction) |
| 1830 | * @mask: bit mask to set |
| 1831 | * @val: the bits value to set |
| 1832 | * |
| 1833 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1834 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1835 | */ |
| 1836 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1837 | int direction, int idx, int mask, int val) |
| 1838 | { |
| 1839 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1840 | |
| 1841 | if (snd_BUG_ON(mask & ~0xff)) |
| 1842 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1843 | for (ch = 0; ch < 2; ch++) |
| 1844 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1845 | idx, mask, val); |
| 1846 | return ret; |
| 1847 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1848 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1849 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1850 | #ifdef CONFIG_PM |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1851 | /** |
| 1852 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1853 | * @codec: HD-audio codec |
| 1854 | * |
| 1855 | * Resume the all amp commands from the cache. |
| 1856 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1857 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1858 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1859 | struct hda_amp_info *buffer = codec->amp_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1860 | int i; |
| 1861 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1862 | for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1863 | u32 key = buffer->head.key; |
| 1864 | hda_nid_t nid; |
| 1865 | unsigned int idx, dir, ch; |
| 1866 | if (!key) |
| 1867 | continue; |
| 1868 | nid = key & 0xff; |
| 1869 | idx = (key >> 16) & 0xff; |
| 1870 | dir = (key >> 24) & 0xff; |
| 1871 | for (ch = 0; ch < 2; ch++) { |
| 1872 | if (!(buffer->head.val & INFO_AMP_VOL(ch))) |
| 1873 | continue; |
| 1874 | put_vol_mute(codec, buffer, nid, ch, dir, idx, |
| 1875 | buffer->vol[ch]); |
| 1876 | } |
| 1877 | } |
| 1878 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1879 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1880 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1882 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1883 | unsigned int ofs) |
| 1884 | { |
| 1885 | u32 caps = query_amp_caps(codec, nid, dir); |
| 1886 | /* get num steps */ |
| 1887 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 1888 | if (ofs < caps) |
| 1889 | caps -= ofs; |
| 1890 | return caps; |
| 1891 | } |
| 1892 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1893 | /** |
| 1894 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 1895 | * |
| 1896 | * The control element is supposed to have the private_value field |
| 1897 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1898 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1899 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 1900 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | { |
| 1902 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1903 | u16 nid = get_amp_nid(kcontrol); |
| 1904 | u8 chs = get_amp_channels(kcontrol); |
| 1905 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1906 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1908 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1909 | uinfo->count = chs == 3 ? 2 : 1; |
| 1910 | uinfo->value.integer.min = 0; |
| 1911 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 1912 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1913 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 1914 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 1915 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | return -EINVAL; |
| 1917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | return 0; |
| 1919 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1920 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1922 | |
| 1923 | static inline unsigned int |
| 1924 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 1925 | int ch, int dir, int idx, unsigned int ofs) |
| 1926 | { |
| 1927 | unsigned int val; |
| 1928 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 1929 | val &= HDA_AMP_VOLMASK; |
| 1930 | if (val >= ofs) |
| 1931 | val -= ofs; |
| 1932 | else |
| 1933 | val = 0; |
| 1934 | return val; |
| 1935 | } |
| 1936 | |
| 1937 | static inline int |
| 1938 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 1939 | int ch, int dir, int idx, unsigned int ofs, |
| 1940 | unsigned int val) |
| 1941 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1942 | unsigned int maxval; |
| 1943 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1944 | if (val > 0) |
| 1945 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 1946 | /* ofs = 0: raw max value */ |
| 1947 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1948 | if (val > maxval) |
| 1949 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1950 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 1951 | HDA_AMP_VOLMASK, val); |
| 1952 | } |
| 1953 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1954 | /** |
| 1955 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 1956 | * |
| 1957 | * The control element is supposed to have the private_value field |
| 1958 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1959 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1960 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 1961 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | { |
| 1963 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1964 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1965 | int chs = get_amp_channels(kcontrol); |
| 1966 | int dir = get_amp_direction(kcontrol); |
| 1967 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1968 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | long *valp = ucontrol->value.integer.value; |
| 1970 | |
| 1971 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1972 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1974 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | return 0; |
| 1976 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1977 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1979 | /** |
| 1980 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 1981 | * |
| 1982 | * The control element is supposed to have the private_value field |
| 1983 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1984 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1985 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 1986 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | { |
| 1988 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1989 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1990 | int chs = get_amp_channels(kcontrol); |
| 1991 | int dir = get_amp_direction(kcontrol); |
| 1992 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1993 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | long *valp = ucontrol->value.integer.value; |
| 1995 | int change = 0; |
| 1996 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1997 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1998 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1999 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2000 | valp++; |
| 2001 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2002 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2003 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2004 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | return change; |
| 2006 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2007 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2009 | /** |
| 2010 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2011 | * |
| 2012 | * The control element is supposed to have the private_value field |
| 2013 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2014 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2015 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2016 | unsigned int size, unsigned int __user *_tlv) |
| 2017 | { |
| 2018 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2019 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2020 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2021 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2022 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2023 | u32 caps, val1, val2; |
| 2024 | |
| 2025 | if (size < 4 * sizeof(unsigned int)) |
| 2026 | return -ENOMEM; |
| 2027 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2028 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2029 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2030 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2031 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2032 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame^] | 2033 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2034 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2035 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2036 | return -EFAULT; |
| 2037 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2038 | return -EFAULT; |
| 2039 | if (put_user(val1, _tlv + 2)) |
| 2040 | return -EFAULT; |
| 2041 | if (put_user(val2, _tlv + 3)) |
| 2042 | return -EFAULT; |
| 2043 | return 0; |
| 2044 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2045 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2046 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2047 | /** |
| 2048 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2049 | * @codec: HD-audio codec |
| 2050 | * @nid: NID of a reference widget |
| 2051 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2052 | * @tlv: TLV data to be stored, at least 4 elements |
| 2053 | * |
| 2054 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2055 | * obtained from the reference NID. |
| 2056 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2057 | */ |
| 2058 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2059 | unsigned int *tlv) |
| 2060 | { |
| 2061 | u32 caps; |
| 2062 | int nums, step; |
| 2063 | |
| 2064 | caps = query_amp_caps(codec, nid, dir); |
| 2065 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2066 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2067 | step = (step + 1) * 25; |
| 2068 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2069 | tlv[1] = 2 * sizeof(unsigned int); |
| 2070 | tlv[2] = -nums * step; |
| 2071 | tlv[3] = step; |
| 2072 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2073 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2076 | static struct snd_kcontrol * |
| 2077 | _snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2078 | const char *name, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2079 | { |
| 2080 | struct snd_ctl_elem_id id; |
| 2081 | memset(&id, 0, sizeof(id)); |
| 2082 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2083 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2084 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2085 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2086 | strcpy(id.name, name); |
| 2087 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2088 | } |
| 2089 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2090 | /** |
| 2091 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2092 | * @codec: HD-audio codec |
| 2093 | * @name: ctl id name string |
| 2094 | * |
| 2095 | * Get the control element with the given id string and IFACE_MIXER. |
| 2096 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2097 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2098 | const char *name) |
| 2099 | { |
| 2100 | return _snd_hda_find_mixer_ctl(codec, name, 0); |
| 2101 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2102 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2103 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2104 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name) |
| 2105 | { |
| 2106 | int idx; |
| 2107 | for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */ |
| 2108 | if (!_snd_hda_find_mixer_ctl(codec, name, idx)) |
| 2109 | return idx; |
| 2110 | } |
| 2111 | return -EBUSY; |
| 2112 | } |
| 2113 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2114 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2115 | * 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] | 2116 | * @codec: HD-audio codec |
| 2117 | * @nid: corresponding NID (optional) |
| 2118 | * @kctl: the control element to assign |
| 2119 | * |
| 2120 | * Add the given control element to an array inside the codec instance. |
| 2121 | * All control elements belonging to a codec are supposed to be added |
| 2122 | * by this function so that a proper clean-up works at the free or |
| 2123 | * reconfiguration time. |
| 2124 | * |
| 2125 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2126 | * The assignment is shown in the codec proc file. |
| 2127 | * |
| 2128 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2129 | * #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] | 2130 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2131 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2132 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2133 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2134 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2135 | { |
| 2136 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2137 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2138 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2139 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2140 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2141 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2142 | if (nid == 0) |
| 2143 | nid = get_amp_nid_(kctl->private_value); |
| 2144 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2145 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2146 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2147 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2148 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2149 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2150 | if (err < 0) |
| 2151 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2152 | item = snd_array_new(&codec->mixers); |
| 2153 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2154 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2155 | item->kctl = kctl; |
| 2156 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2157 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2160 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2161 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2162 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2163 | * snd_hda_add_nid - Assign a NID to a control element |
| 2164 | * @codec: HD-audio codec |
| 2165 | * @nid: corresponding NID (optional) |
| 2166 | * @kctl: the control element to assign |
| 2167 | * @index: index to kctl |
| 2168 | * |
| 2169 | * Add the given control element to an array inside the codec instance. |
| 2170 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2171 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2172 | */ |
| 2173 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2174 | unsigned int index, hda_nid_t nid) |
| 2175 | { |
| 2176 | struct hda_nid_item *item; |
| 2177 | |
| 2178 | if (nid > 0) { |
| 2179 | item = snd_array_new(&codec->nids); |
| 2180 | if (!item) |
| 2181 | return -ENOMEM; |
| 2182 | item->kctl = kctl; |
| 2183 | item->index = index; |
| 2184 | item->nid = nid; |
| 2185 | return 0; |
| 2186 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2187 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2188 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2189 | return -EINVAL; |
| 2190 | } |
| 2191 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2192 | |
| 2193 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2194 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2195 | * @codec: HD-audio codec |
| 2196 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2197 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2198 | { |
| 2199 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2200 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2201 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2202 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2203 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2204 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2207 | /* pseudo device locking |
| 2208 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2209 | */ |
| 2210 | static int hda_lock_devices(struct snd_card *card) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2211 | { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2212 | spin_lock(&card->files_lock); |
| 2213 | if (card->shutdown) { |
| 2214 | spin_unlock(&card->files_lock); |
| 2215 | return -EINVAL; |
| 2216 | } |
| 2217 | card->shutdown = 1; |
| 2218 | spin_unlock(&card->files_lock); |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | static void hda_unlock_devices(struct snd_card *card) |
| 2223 | { |
| 2224 | spin_lock(&card->files_lock); |
| 2225 | card->shutdown = 0; |
| 2226 | spin_unlock(&card->files_lock); |
| 2227 | } |
| 2228 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2229 | /** |
| 2230 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2231 | * @codec: HD-audio codec |
| 2232 | * |
| 2233 | * This frees the all PCM and control elements assigned to the codec, and |
| 2234 | * clears the caches and restores the pin default configurations. |
| 2235 | * |
| 2236 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2237 | * returns zero. |
| 2238 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2239 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2240 | { |
| 2241 | struct snd_card *card = codec->bus->card; |
| 2242 | int i, pcm; |
| 2243 | |
| 2244 | if (hda_lock_devices(card) < 0) |
| 2245 | return -EBUSY; |
| 2246 | /* check whether the codec isn't used by any mixer or PCM streams */ |
| 2247 | if (!list_empty(&card->ctl_files)) { |
| 2248 | hda_unlock_devices(card); |
| 2249 | return -EBUSY; |
| 2250 | } |
| 2251 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2252 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2253 | if (!cpcm->pcm) |
| 2254 | continue; |
| 2255 | if (cpcm->pcm->streams[0].substream_opened || |
| 2256 | cpcm->pcm->streams[1].substream_opened) { |
| 2257 | hda_unlock_devices(card); |
| 2258 | return -EBUSY; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | /* OK, let it free */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2263 | |
| 2264 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 2265 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 2266 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2267 | #endif |
| 2268 | snd_hda_ctls_clear(codec); |
| 2269 | /* relase PCMs */ |
| 2270 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2271 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2272 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2273 | clear_bit(codec->pcm_info[i].device, |
| 2274 | codec->bus->pcm_dev_bits); |
| 2275 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2276 | } |
| 2277 | if (codec->patch_ops.free) |
| 2278 | codec->patch_ops.free(codec); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2279 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2280 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2281 | codec->spec = NULL; |
| 2282 | free_hda_cache(&codec->amp_cache); |
| 2283 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2284 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2285 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2286 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2287 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 2288 | restore_pincfgs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2289 | codec->num_pcms = 0; |
| 2290 | codec->pcm_info = NULL; |
| 2291 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2292 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
| 2293 | codec->slave_dig_outs = NULL; |
| 2294 | codec->spdif_status_reset = 0; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2295 | module_put(codec->owner); |
| 2296 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2297 | |
| 2298 | /* allow device access again */ |
| 2299 | hda_unlock_devices(card); |
| 2300 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2303 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2304 | |
| 2305 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2306 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
| 2307 | map_slave_func_t func, void *data) |
| 2308 | { |
| 2309 | struct hda_nid_item *items; |
| 2310 | const char * const *s; |
| 2311 | int i, err; |
| 2312 | |
| 2313 | items = codec->mixers.list; |
| 2314 | for (i = 0; i < codec->mixers.used; i++) { |
| 2315 | struct snd_kcontrol *sctl = items[i].kctl; |
| 2316 | if (!sctl || !sctl->id.name || |
| 2317 | sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
| 2318 | continue; |
| 2319 | for (s = slaves; *s; s++) { |
| 2320 | if (!strcmp(sctl->id.name, *s)) { |
| 2321 | err = func(data, sctl); |
| 2322 | if (err) |
| 2323 | return err; |
| 2324 | break; |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
| 2331 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2332 | { |
| 2333 | return 1; |
| 2334 | } |
| 2335 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2336 | /** |
| 2337 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2338 | * @codec: HD-audio codec |
| 2339 | * @name: vmaster control name |
| 2340 | * @tlv: TLV data (optional) |
| 2341 | * @slaves: slave control names (optional) |
| 2342 | * |
| 2343 | * Create a virtual master control with the given name. The TLV data |
| 2344 | * must be either NULL or a valid data. |
| 2345 | * |
| 2346 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2347 | * slave control name. All controls with these names are assigned to |
| 2348 | * the new virtual master control. |
| 2349 | * |
| 2350 | * This function returns zero if successful or a negative error code. |
| 2351 | */ |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2352 | int snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 2353 | unsigned int *tlv, const char * const *slaves) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2354 | { |
| 2355 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2356 | int err; |
| 2357 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2358 | err = map_slaves(codec, slaves, check_slave_present, NULL); |
| 2359 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2360 | snd_printdd("No slave found for %s\n", name); |
| 2361 | return 0; |
| 2362 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2363 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2364 | if (!kctl) |
| 2365 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2366 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2367 | if (err < 0) |
| 2368 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2369 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2370 | err = map_slaves(codec, slaves, (map_slave_func_t)snd_ctl_add_slave, |
| 2371 | kctl); |
| 2372 | if (err < 0) |
| 2373 | return err; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2376 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2377 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2378 | /** |
| 2379 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2380 | * |
| 2381 | * The control element is supposed to have the private_value field |
| 2382 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2383 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2384 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2385 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | { |
| 2387 | int chs = get_amp_channels(kcontrol); |
| 2388 | |
| 2389 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2390 | uinfo->count = chs == 3 ? 2 : 1; |
| 2391 | uinfo->value.integer.min = 0; |
| 2392 | uinfo->value.integer.max = 1; |
| 2393 | return 0; |
| 2394 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2395 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2397 | /** |
| 2398 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2399 | * |
| 2400 | * The control element is supposed to have the private_value field |
| 2401 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2402 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2403 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2404 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | { |
| 2406 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2407 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2408 | int chs = get_amp_channels(kcontrol); |
| 2409 | int dir = get_amp_direction(kcontrol); |
| 2410 | int idx = get_amp_index(kcontrol); |
| 2411 | long *valp = ucontrol->value.integer.value; |
| 2412 | |
| 2413 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2414 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2415 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2417 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2418 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | return 0; |
| 2420 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2421 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2423 | /** |
| 2424 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2425 | * |
| 2426 | * The control element is supposed to have the private_value field |
| 2427 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2428 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2429 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2430 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | { |
| 2432 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2433 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2434 | int chs = get_amp_channels(kcontrol); |
| 2435 | int dir = get_amp_direction(kcontrol); |
| 2436 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | long *valp = ucontrol->value.integer.value; |
| 2438 | int change = 0; |
| 2439 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2440 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2441 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2442 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2443 | HDA_AMP_MUTE, |
| 2444 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2445 | valp++; |
| 2446 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2447 | if (chs & 2) |
| 2448 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2449 | HDA_AMP_MUTE, |
| 2450 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2451 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2452 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | return change; |
| 2454 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2455 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | |
Takashi Iwai | 67d634c | 2009-11-16 15:35:59 +0100 | [diff] [blame] | 2457 | #ifdef CONFIG_SND_HDA_INPUT_BEEP |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2458 | /** |
| 2459 | * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch |
| 2460 | * |
| 2461 | * This function calls snd_hda_enable_beep_device(), which behaves differently |
| 2462 | * depending on beep_mode option. |
| 2463 | */ |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 2464 | int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol, |
| 2465 | struct snd_ctl_elem_value *ucontrol) |
| 2466 | { |
| 2467 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2468 | long *valp = ucontrol->value.integer.value; |
| 2469 | |
| 2470 | snd_hda_enable_beep_device(codec, *valp); |
| 2471 | return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2472 | } |
| 2473 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep); |
Takashi Iwai | 67d634c | 2009-11-16 15:35:59 +0100 | [diff] [blame] | 2474 | #endif /* CONFIG_SND_HDA_INPUT_BEEP */ |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 2475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2477 | * bound volume controls |
| 2478 | * |
| 2479 | * bind multiple volumes (# indices, from 0) |
| 2480 | */ |
| 2481 | |
| 2482 | #define AMP_VAL_IDX_SHIFT 19 |
| 2483 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2484 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2485 | /** |
| 2486 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 2487 | * |
| 2488 | * The control element is supposed to have the private_value field |
| 2489 | * set up via HDA_BIND_MUTE*() macros. |
| 2490 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2491 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2492 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2493 | { |
| 2494 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2495 | unsigned long pval; |
| 2496 | int err; |
| 2497 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2498 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2499 | pval = kcontrol->private_value; |
| 2500 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2501 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2502 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2503 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2504 | return err; |
| 2505 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2506 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2507 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2508 | /** |
| 2509 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 2510 | * |
| 2511 | * The control element is supposed to have the private_value field |
| 2512 | * set up via HDA_BIND_MUTE*() macros. |
| 2513 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2514 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2515 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2516 | { |
| 2517 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2518 | unsigned long pval; |
| 2519 | int i, indices, err = 0, change = 0; |
| 2520 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2521 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2522 | pval = kcontrol->private_value; |
| 2523 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2524 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2525 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2526 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2527 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2528 | if (err < 0) |
| 2529 | break; |
| 2530 | change |= err; |
| 2531 | } |
| 2532 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2533 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2534 | return err < 0 ? err : change; |
| 2535 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2536 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2537 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2538 | /** |
| 2539 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 2540 | * |
| 2541 | * The control element is supposed to have the private_value field |
| 2542 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2543 | */ |
| 2544 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2545 | struct snd_ctl_elem_info *uinfo) |
| 2546 | { |
| 2547 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2548 | struct hda_bind_ctls *c; |
| 2549 | int err; |
| 2550 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2551 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2552 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2553 | kcontrol->private_value = *c->values; |
| 2554 | err = c->ops->info(kcontrol, uinfo); |
| 2555 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2556 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2557 | return err; |
| 2558 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2559 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2560 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2561 | /** |
| 2562 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 2563 | * |
| 2564 | * The control element is supposed to have the private_value field |
| 2565 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2566 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2567 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2568 | struct snd_ctl_elem_value *ucontrol) |
| 2569 | { |
| 2570 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2571 | struct hda_bind_ctls *c; |
| 2572 | int err; |
| 2573 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2574 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2575 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2576 | kcontrol->private_value = *c->values; |
| 2577 | err = c->ops->get(kcontrol, ucontrol); |
| 2578 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2579 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2580 | return err; |
| 2581 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2582 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2583 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2584 | /** |
| 2585 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 2586 | * |
| 2587 | * The control element is supposed to have the private_value field |
| 2588 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2589 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2590 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2591 | struct snd_ctl_elem_value *ucontrol) |
| 2592 | { |
| 2593 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2594 | struct hda_bind_ctls *c; |
| 2595 | unsigned long *vals; |
| 2596 | int err = 0, change = 0; |
| 2597 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2598 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2599 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2600 | for (vals = c->values; *vals; vals++) { |
| 2601 | kcontrol->private_value = *vals; |
| 2602 | err = c->ops->put(kcontrol, ucontrol); |
| 2603 | if (err < 0) |
| 2604 | break; |
| 2605 | change |= err; |
| 2606 | } |
| 2607 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2608 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2609 | return err < 0 ? err : change; |
| 2610 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2611 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2612 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2613 | /** |
| 2614 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 2615 | * |
| 2616 | * The control element is supposed to have the private_value field |
| 2617 | * set up via HDA_BIND_VOL() macro. |
| 2618 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2619 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2620 | unsigned int size, unsigned int __user *tlv) |
| 2621 | { |
| 2622 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2623 | struct hda_bind_ctls *c; |
| 2624 | int err; |
| 2625 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2626 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2627 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2628 | kcontrol->private_value = *c->values; |
| 2629 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 2630 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2631 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2632 | return err; |
| 2633 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2634 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2635 | |
| 2636 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 2637 | .info = snd_hda_mixer_amp_volume_info, |
| 2638 | .get = snd_hda_mixer_amp_volume_get, |
| 2639 | .put = snd_hda_mixer_amp_volume_put, |
| 2640 | .tlv = snd_hda_mixer_amp_tlv |
| 2641 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2642 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2643 | |
| 2644 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 2645 | .info = snd_hda_mixer_amp_switch_info, |
| 2646 | .get = snd_hda_mixer_amp_switch_get, |
| 2647 | .put = snd_hda_mixer_amp_switch_put, |
| 2648 | .tlv = snd_hda_mixer_amp_tlv |
| 2649 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2650 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2651 | |
| 2652 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | * SPDIF out controls |
| 2654 | */ |
| 2655 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2656 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 2657 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | { |
| 2659 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 2660 | uinfo->count = 1; |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2664 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 2665 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | { |
| 2667 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 2668 | IEC958_AES0_NONAUDIO | |
| 2669 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 2670 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 2671 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 2672 | IEC958_AES1_CON_ORIGINAL; |
| 2673 | return 0; |
| 2674 | } |
| 2675 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2676 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 2677 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | { |
| 2679 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 2680 | IEC958_AES0_NONAUDIO | |
| 2681 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2685 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 2686 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | { |
| 2688 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2689 | int idx = kcontrol->private_value; |
| 2690 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2692 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 2693 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 2694 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 2695 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 2701 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 2702 | */ |
| 2703 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 2704 | { |
| 2705 | unsigned short val = 0; |
| 2706 | |
| 2707 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2708 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2710 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2712 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 2713 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 2714 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2716 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 2717 | IEC958_AES0_CON_EMPHASIS_5015) |
| 2718 | val |= AC_DIG1_EMPHASIS; |
| 2719 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 2720 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2722 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 2724 | } |
| 2725 | return val; |
| 2726 | } |
| 2727 | |
| 2728 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 2729 | */ |
| 2730 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 2731 | { |
| 2732 | unsigned int sbits = 0; |
| 2733 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2734 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2736 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 2738 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2739 | if (sbits & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 2741 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2742 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2744 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2746 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 2748 | sbits |= val & (0x7f << 8); |
| 2749 | } |
| 2750 | return sbits; |
| 2751 | } |
| 2752 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2753 | /* set digital convert verbs both for the given NID and its slaves */ |
| 2754 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 2755 | int verb, int val) |
| 2756 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 2757 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2758 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 2759 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2760 | d = codec->slave_dig_outs; |
| 2761 | if (!d) |
| 2762 | return; |
| 2763 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 2764 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 2768 | int dig1, int dig2) |
| 2769 | { |
| 2770 | if (dig1 != -1) |
| 2771 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 2772 | if (dig2 != -1) |
| 2773 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 2774 | } |
| 2775 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2776 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 2777 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | { |
| 2779 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2780 | int idx = kcontrol->private_value; |
| 2781 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2782 | hda_nid_t nid = spdif->nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | unsigned short val; |
| 2784 | int change; |
| 2785 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2786 | mutex_lock(&codec->spdif_mutex); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2787 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 2789 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 2790 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2791 | val = convert_from_spdif_status(spdif->status); |
| 2792 | val |= spdif->ctls & 1; |
| 2793 | change = spdif->ctls != val; |
| 2794 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2795 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2796 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2797 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | return change; |
| 2799 | } |
| 2800 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 2801 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2803 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 2804 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | { |
| 2806 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2807 | int idx = kcontrol->private_value; |
| 2808 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2810 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | return 0; |
| 2812 | } |
| 2813 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2814 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 2815 | int dig1, int dig2) |
| 2816 | { |
| 2817 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 2818 | /* unmute amp switch (if any) */ |
| 2819 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 2820 | (dig1 & AC_DIG1_ENABLE)) |
| 2821 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 2822 | HDA_AMP_MUTE, 0); |
| 2823 | } |
| 2824 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2825 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 2826 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | { |
| 2828 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2829 | int idx = kcontrol->private_value; |
| 2830 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2831 | hda_nid_t nid = spdif->nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | unsigned short val; |
| 2833 | int change; |
| 2834 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2835 | mutex_lock(&codec->spdif_mutex); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2836 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2838 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2839 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2840 | spdif->ctls = val; |
| 2841 | if (change && nid != (u16)-1) |
| 2842 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2843 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | return change; |
| 2845 | } |
| 2846 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2847 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | { |
| 2849 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 2850 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2851 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | .info = snd_hda_spdif_mask_info, |
| 2853 | .get = snd_hda_spdif_cmask_get, |
| 2854 | }, |
| 2855 | { |
| 2856 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 2857 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2858 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | .info = snd_hda_spdif_mask_info, |
| 2860 | .get = snd_hda_spdif_pmask_get, |
| 2861 | }, |
| 2862 | { |
| 2863 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2864 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | .info = snd_hda_spdif_mask_info, |
| 2866 | .get = snd_hda_spdif_default_get, |
| 2867 | .put = snd_hda_spdif_default_put, |
| 2868 | }, |
| 2869 | { |
| 2870 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2871 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | .info = snd_hda_spdif_out_switch_info, |
| 2873 | .get = snd_hda_spdif_out_switch_get, |
| 2874 | .put = snd_hda_spdif_out_switch_put, |
| 2875 | }, |
| 2876 | { } /* end */ |
| 2877 | }; |
| 2878 | |
| 2879 | /** |
| 2880 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls |
| 2881 | * @codec: the HDA codec |
| 2882 | * @nid: audio out widget NID |
| 2883 | * |
| 2884 | * Creates controls related with the SPDIF output. |
| 2885 | * Called from each patch supporting the SPDIF out. |
| 2886 | * |
| 2887 | * Returns 0 if successful, or a negative error code. |
| 2888 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2889 | int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, |
| 2890 | hda_nid_t associated_nid, |
| 2891 | hda_nid_t cvt_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | { |
| 2893 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2894 | struct snd_kcontrol *kctl; |
| 2895 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2896 | int idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2897 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2899 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch"); |
| 2900 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2901 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 2902 | return -EBUSY; |
| 2903 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2904 | spdif = snd_array_new(&codec->spdif_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 2906 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 2907 | if (!kctl) |
| 2908 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2909 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2910 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2911 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2912 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | return err; |
| 2914 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2915 | spdif->nid = cvt_nid; |
| 2916 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2917 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 2918 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | return 0; |
| 2920 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2921 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2923 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 2924 | hda_nid_t nid) |
| 2925 | { |
| 2926 | int i; |
| 2927 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 2928 | struct hda_spdif_out *spdif = |
| 2929 | snd_array_elem(&codec->spdif_out, i); |
| 2930 | if (spdif->nid == nid) |
| 2931 | return spdif; |
| 2932 | } |
| 2933 | return NULL; |
| 2934 | } |
| 2935 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 2936 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2937 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 2938 | { |
| 2939 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2940 | |
| 2941 | mutex_lock(&codec->spdif_mutex); |
| 2942 | spdif->nid = (u16)-1; |
| 2943 | mutex_unlock(&codec->spdif_mutex); |
| 2944 | } |
| 2945 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 2946 | |
| 2947 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 2948 | { |
| 2949 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2950 | unsigned short val; |
| 2951 | |
| 2952 | mutex_lock(&codec->spdif_mutex); |
| 2953 | if (spdif->nid != nid) { |
| 2954 | spdif->nid = nid; |
| 2955 | val = spdif->ctls; |
| 2956 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 2957 | } |
| 2958 | mutex_unlock(&codec->spdif_mutex); |
| 2959 | } |
| 2960 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 2961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2963 | * SPDIF sharing with analog output |
| 2964 | */ |
| 2965 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 2966 | struct snd_ctl_elem_value *ucontrol) |
| 2967 | { |
| 2968 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 2969 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 2970 | return 0; |
| 2971 | } |
| 2972 | |
| 2973 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 2974 | struct snd_ctl_elem_value *ucontrol) |
| 2975 | { |
| 2976 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 2977 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | static struct snd_kcontrol_new spdif_share_sw = { |
| 2982 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2983 | .name = "IEC958 Default PCM Playback Switch", |
| 2984 | .info = snd_ctl_boolean_mono_info, |
| 2985 | .get = spdif_share_sw_get, |
| 2986 | .put = spdif_share_sw_put, |
| 2987 | }; |
| 2988 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2989 | /** |
| 2990 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 2991 | * @codec: the HDA codec |
| 2992 | * @mout: multi-out instance |
| 2993 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2994 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 2995 | struct hda_multi_out *mout) |
| 2996 | { |
| 2997 | if (!mout->dig_out_nid) |
| 2998 | return 0; |
| 2999 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3000 | return snd_hda_ctl_add(codec, mout->dig_out_nid, |
| 3001 | snd_ctl_new1(&spdif_share_sw, mout)); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3002 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3003 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3004 | |
| 3005 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | * SPDIF input |
| 3007 | */ |
| 3008 | |
| 3009 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3010 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3011 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3012 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | { |
| 3014 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3015 | |
| 3016 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3020 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3021 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | { |
| 3023 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3024 | hda_nid_t nid = kcontrol->private_value; |
| 3025 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3026 | int change; |
| 3027 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3028 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3030 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3032 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3033 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3035 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | return change; |
| 3037 | } |
| 3038 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3039 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3040 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | { |
| 3042 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3043 | hda_nid_t nid = kcontrol->private_value; |
| 3044 | unsigned short val; |
| 3045 | unsigned int sbits; |
| 3046 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3047 | 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] | 3048 | sbits = convert_to_spdif_status(val); |
| 3049 | ucontrol->value.iec958.status[0] = sbits; |
| 3050 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3051 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3052 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3053 | return 0; |
| 3054 | } |
| 3055 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3056 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
| 3058 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3059 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | .info = snd_hda_spdif_in_switch_info, |
| 3061 | .get = snd_hda_spdif_in_switch_get, |
| 3062 | .put = snd_hda_spdif_in_switch_put, |
| 3063 | }, |
| 3064 | { |
| 3065 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3066 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3067 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | .info = snd_hda_spdif_mask_info, |
| 3069 | .get = snd_hda_spdif_in_status_get, |
| 3070 | }, |
| 3071 | { } /* end */ |
| 3072 | }; |
| 3073 | |
| 3074 | /** |
| 3075 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3076 | * @codec: the HDA codec |
| 3077 | * @nid: audio in widget NID |
| 3078 | * |
| 3079 | * Creates controls related with the SPDIF input. |
| 3080 | * Called from each patch supporting the SPDIF in. |
| 3081 | * |
| 3082 | * Returns 0 if successful, or a negative error code. |
| 3083 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3084 | 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] | 3085 | { |
| 3086 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3087 | struct snd_kcontrol *kctl; |
| 3088 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3089 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3091 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch"); |
| 3092 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3093 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3094 | return -EBUSY; |
| 3095 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3097 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3098 | if (!kctl) |
| 3099 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3101 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3102 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | return err; |
| 3104 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3105 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3106 | snd_hda_codec_read(codec, nid, 0, |
| 3107 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3108 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | return 0; |
| 3110 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3111 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3113 | #ifdef CONFIG_PM |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3114 | /* |
| 3115 | * command cache |
| 3116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3118 | /* build a 32bit cache key with the widget id and the command parameter */ |
| 3119 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3120 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3121 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3122 | |
| 3123 | /** |
| 3124 | * snd_hda_codec_write_cache - send a single command with caching |
| 3125 | * @codec: the HDA codec |
| 3126 | * @nid: NID to send the command |
| 3127 | * @direct: direct flag |
| 3128 | * @verb: the verb to send |
| 3129 | * @parm: the parameter for the verb |
| 3130 | * |
| 3131 | * Send a single command without waiting for response. |
| 3132 | * |
| 3133 | * Returns 0 if successful, or a negative error code. |
| 3134 | */ |
| 3135 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3136 | int direct, unsigned int verb, unsigned int parm) |
| 3137 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3138 | int err = snd_hda_codec_write(codec, nid, direct, verb, parm); |
| 3139 | struct hda_cache_head *c; |
| 3140 | u32 key; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3141 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3142 | if (err < 0) |
| 3143 | return err; |
| 3144 | /* parm may contain the verb stuff for get/set amp */ |
| 3145 | verb = verb | (parm >> 8); |
| 3146 | parm &= 0xff; |
| 3147 | key = build_cmd_cache_key(nid, verb); |
| 3148 | mutex_lock(&codec->bus->cmd_mutex); |
| 3149 | c = get_alloc_hash(&codec->cmd_cache, key); |
| 3150 | if (c) |
| 3151 | c->val = parm; |
| 3152 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3153 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3154 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3155 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3156 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3157 | /** |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3158 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3159 | * @codec: the HDA codec |
| 3160 | * @nid: NID to send the command |
| 3161 | * @direct: direct flag |
| 3162 | * @verb: the verb to send |
| 3163 | * @parm: the parameter for the verb |
| 3164 | * |
| 3165 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3166 | * command if the parameter is already identical with the cached value. |
| 3167 | * If not, it sends the command and refreshes the cache. |
| 3168 | * |
| 3169 | * Returns 0 if successful, or a negative error code. |
| 3170 | */ |
| 3171 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3172 | int direct, unsigned int verb, unsigned int parm) |
| 3173 | { |
| 3174 | struct hda_cache_head *c; |
| 3175 | u32 key; |
| 3176 | |
| 3177 | /* parm may contain the verb stuff for get/set amp */ |
| 3178 | verb = verb | (parm >> 8); |
| 3179 | parm &= 0xff; |
| 3180 | key = build_cmd_cache_key(nid, verb); |
| 3181 | mutex_lock(&codec->bus->cmd_mutex); |
| 3182 | c = get_hash(&codec->cmd_cache, key); |
| 3183 | if (c && c->val == parm) { |
| 3184 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3185 | return 0; |
| 3186 | } |
| 3187 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3188 | return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); |
| 3189 | } |
| 3190 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3191 | |
| 3192 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3193 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3194 | * @codec: HD-audio codec |
| 3195 | * |
| 3196 | * Execute all verbs recorded in the command caches to resume. |
| 3197 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3198 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3199 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3200 | struct hda_cache_head *buffer = codec->cmd_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3201 | int i; |
| 3202 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3203 | for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3204 | u32 key = buffer->key; |
| 3205 | if (!key) |
| 3206 | continue; |
| 3207 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3208 | get_cmd_cache_cmd(key), buffer->val); |
| 3209 | } |
| 3210 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3211 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3212 | |
| 3213 | /** |
| 3214 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3215 | * @codec: the HDA codec |
| 3216 | * @seq: VERB array to send |
| 3217 | * |
| 3218 | * Send the commands sequentially from the given array. |
| 3219 | * Thte commands are recorded on cache for power-save and resume. |
| 3220 | * The array must be terminated with NID=0. |
| 3221 | */ |
| 3222 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3223 | const struct hda_verb *seq) |
| 3224 | { |
| 3225 | for (; seq->nid; seq++) |
| 3226 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3227 | seq->param); |
| 3228 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3229 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3230 | #endif /* CONFIG_PM */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3231 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3232 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
| 3233 | unsigned int power_state, |
| 3234 | bool eapd_workaround) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3235 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3236 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3237 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3238 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3239 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3240 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3241 | if (!(wcaps & AC_WCAP_POWER)) |
| 3242 | continue; |
| 3243 | /* don't power down the widget if it controls eapd and |
| 3244 | * EAPD_BTLENABLE is set. |
| 3245 | */ |
| 3246 | if (eapd_workaround && power_state == AC_PWRST_D3 && |
| 3247 | get_wcaps_type(wcaps) == AC_WID_PIN && |
| 3248 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3249 | int eapd = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3250 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3251 | if (eapd & 0x02) |
| 3252 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3253 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3254 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
| 3255 | power_state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3256 | } |
| 3257 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3258 | if (power_state == AC_PWRST_D0) { |
| 3259 | unsigned long end_time; |
| 3260 | int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3261 | /* wait until the codec reachs to D0 */ |
| 3262 | end_time = jiffies + msecs_to_jiffies(500); |
| 3263 | do { |
| 3264 | state = snd_hda_codec_read(codec, fg, 0, |
| 3265 | AC_VERB_GET_POWER_STATE, 0); |
| 3266 | if (state == power_state) |
| 3267 | break; |
| 3268 | msleep(1); |
| 3269 | } while (time_after_eq(end_time, jiffies)); |
| 3270 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3271 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3272 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3273 | |
| 3274 | /* |
| 3275 | * set power state of the codec |
| 3276 | */ |
| 3277 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 3278 | unsigned int power_state) |
| 3279 | { |
| 3280 | if (codec->patch_ops.set_power_state) { |
| 3281 | codec->patch_ops.set_power_state(codec, fg, power_state); |
| 3282 | return; |
| 3283 | } |
| 3284 | |
| 3285 | /* this delay seems necessary to avoid click noise at power-down */ |
| 3286 | if (power_state == AC_PWRST_D3) |
| 3287 | msleep(100); |
| 3288 | snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, |
| 3289 | power_state); |
| 3290 | snd_hda_codec_set_power_to_all(codec, fg, power_state, true); |
| 3291 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3292 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3293 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3294 | /* execute additional init verbs */ |
| 3295 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3296 | { |
| 3297 | if (codec->init_verbs.list) |
| 3298 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3299 | } |
| 3300 | #else |
| 3301 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3302 | #endif |
| 3303 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3304 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3305 | /* |
| 3306 | * call suspend and power-down; used both from PM and power-save |
| 3307 | */ |
| 3308 | static void hda_call_codec_suspend(struct hda_codec *codec) |
| 3309 | { |
| 3310 | if (codec->patch_ops.suspend) |
| 3311 | codec->patch_ops.suspend(codec, PMSG_SUSPEND); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3312 | hda_cleanup_all_streams(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3313 | hda_set_power_state(codec, |
| 3314 | codec->afg ? codec->afg : codec->mfg, |
| 3315 | AC_PWRST_D3); |
| 3316 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3317 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3318 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 3319 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 3320 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3321 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3322 | #endif |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * kick up codec; used both from PM and power-save |
| 3327 | */ |
| 3328 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3329 | { |
| 3330 | hda_set_power_state(codec, |
| 3331 | codec->afg ? codec->afg : codec->mfg, |
| 3332 | AC_PWRST_D0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 3333 | restore_pincfgs(codec); /* restore all current pin configs */ |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3334 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3335 | hda_exec_init_verbs(codec); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 3336 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3337 | if (codec->patch_ops.resume) |
| 3338 | codec->patch_ops.resume(codec); |
| 3339 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3340 | if (codec->patch_ops.init) |
| 3341 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3342 | snd_hda_codec_resume_amp(codec); |
| 3343 | snd_hda_codec_resume_cache(codec); |
| 3344 | } |
| 3345 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3346 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3347 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | /** |
| 3350 | * snd_hda_build_controls - build mixer controls |
| 3351 | * @bus: the BUS |
| 3352 | * |
| 3353 | * Creates mixer controls for each codec included in the bus. |
| 3354 | * |
| 3355 | * Returns 0 if successful, otherwise a negative error code. |
| 3356 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 3357 | int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3359 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3361 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3362 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3363 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 3364 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3365 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3366 | err = snd_hda_codec_reset(codec); |
| 3367 | if (err < 0) { |
| 3368 | printk(KERN_ERR |
| 3369 | "hda_codec: cannot revert codec\n"); |
| 3370 | return err; |
| 3371 | } |
| 3372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3374 | return 0; |
| 3375 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3376 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3378 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 3379 | { |
| 3380 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3381 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3382 | /* continue to initialize... */ |
| 3383 | if (codec->patch_ops.init) |
| 3384 | err = codec->patch_ops.init(codec); |
| 3385 | if (!err && codec->patch_ops.build_controls) |
| 3386 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3387 | if (err < 0) |
| 3388 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | return 0; |
| 3390 | } |
| 3391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | /* |
| 3393 | * stream formats |
| 3394 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3395 | struct hda_rate_tbl { |
| 3396 | unsigned int hz; |
| 3397 | unsigned int alsa_bits; |
| 3398 | unsigned int hda_fmt; |
| 3399 | }; |
| 3400 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3401 | /* rate = base * mult / div */ |
| 3402 | #define HDA_RATE(base, mult, div) \ |
| 3403 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 3404 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 3405 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3406 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3408 | |
| 3409 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3410 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 3411 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 3412 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 3413 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 3414 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 3415 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 3416 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 3417 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 3418 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 3419 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 3420 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3421 | #define AC_PAR_PCM_RATE_BITS 11 |
| 3422 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 3423 | |
| 3424 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3425 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3426 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3427 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | }; |
| 3429 | |
| 3430 | /** |
| 3431 | * snd_hda_calc_stream_format - calculate format bitset |
| 3432 | * @rate: the sample rate |
| 3433 | * @channels: the number of channels |
| 3434 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 3435 | * @maxbps: the max. bps |
| 3436 | * |
| 3437 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 3438 | * |
| 3439 | * Return zero if invalid. |
| 3440 | */ |
| 3441 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 3442 | unsigned int channels, |
| 3443 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3444 | unsigned int maxbps, |
| 3445 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | { |
| 3447 | int i; |
| 3448 | unsigned int val = 0; |
| 3449 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3450 | for (i = 0; rate_bits[i].hz; i++) |
| 3451 | if (rate_bits[i].hz == rate) { |
| 3452 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | break; |
| 3454 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3455 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | snd_printdd("invalid rate %d\n", rate); |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | if (channels == 0 || channels > 8) { |
| 3461 | snd_printdd("invalid channels %d\n", channels); |
| 3462 | return 0; |
| 3463 | } |
| 3464 | val |= channels - 1; |
| 3465 | |
| 3466 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3467 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3468 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3469 | break; |
| 3470 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3471 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3472 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | case 20: |
| 3474 | case 24: |
| 3475 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 3476 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3477 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3479 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3481 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | break; |
| 3483 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3484 | snd_printdd("invalid format width %d\n", |
| 3485 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | return 0; |
| 3487 | } |
| 3488 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3489 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3490 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | return val; |
| 3493 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3494 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3496 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 3497 | { |
| 3498 | unsigned int val = 0; |
| 3499 | if (nid != codec->afg && |
| 3500 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 3501 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 3502 | if (!val || val == -1) |
| 3503 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 3504 | if (!val || val == -1) |
| 3505 | return 0; |
| 3506 | return val; |
| 3507 | } |
| 3508 | |
| 3509 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 3510 | { |
| 3511 | return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid), |
| 3512 | get_pcm_param); |
| 3513 | } |
| 3514 | |
| 3515 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 3516 | { |
| 3517 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 3518 | if (!streams || streams == -1) |
| 3519 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 3520 | if (!streams || streams == -1) |
| 3521 | return 0; |
| 3522 | return streams; |
| 3523 | } |
| 3524 | |
| 3525 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 3526 | { |
| 3527 | return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid), |
| 3528 | get_stream_param); |
| 3529 | } |
| 3530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | /** |
| 3532 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 3533 | * @codec: the HDA codec |
| 3534 | * @nid: NID to query |
| 3535 | * @ratesp: the pointer to store the detected rate bitflags |
| 3536 | * @formatsp: the pointer to store the detected formats |
| 3537 | * @bpsp: the pointer to store the detected format widths |
| 3538 | * |
| 3539 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 3540 | * or @bsps argument is ignored. |
| 3541 | * |
| 3542 | * Returns 0 if successful, otherwise a negative error code. |
| 3543 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 3544 | 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] | 3545 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 3546 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3547 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3549 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3550 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | |
| 3552 | if (ratesp) { |
| 3553 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3554 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3556 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3558 | if (rates == 0) { |
| 3559 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 3560 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 3561 | nid, val, |
| 3562 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 3563 | return -EIO; |
| 3564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | *ratesp = rates; |
| 3566 | } |
| 3567 | |
| 3568 | if (formatsp || bpsp) { |
| 3569 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3570 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3572 | streams = query_stream_param(codec, nid); |
| 3573 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | |
| 3576 | bps = 0; |
| 3577 | if (streams & AC_SUPFMT_PCM) { |
| 3578 | if (val & AC_SUPPCM_BITS_8) { |
| 3579 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 3580 | bps = 8; |
| 3581 | } |
| 3582 | if (val & AC_SUPPCM_BITS_16) { |
| 3583 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 3584 | bps = 16; |
| 3585 | } |
| 3586 | if (wcaps & AC_WCAP_DIGITAL) { |
| 3587 | if (val & AC_SUPPCM_BITS_32) |
| 3588 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 3589 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 3590 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 3591 | if (val & AC_SUPPCM_BITS_24) |
| 3592 | bps = 24; |
| 3593 | else if (val & AC_SUPPCM_BITS_20) |
| 3594 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3595 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 3596 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 3598 | if (val & AC_SUPPCM_BITS_32) |
| 3599 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | else if (val & AC_SUPPCM_BITS_24) |
| 3601 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 3602 | else if (val & AC_SUPPCM_BITS_20) |
| 3603 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | } |
| 3605 | } |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 3606 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 3608 | if (!bps) |
| 3609 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 3610 | } |
| 3611 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3612 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | /* temporary hack: we have still no proper support |
| 3614 | * for the direct AC3 stream... |
| 3615 | */ |
| 3616 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 3617 | bps = 8; |
| 3618 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3619 | if (formats == 0) { |
| 3620 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 3621 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 3622 | "streams=0x%x)\n", |
| 3623 | nid, val, |
| 3624 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 3625 | streams); |
| 3626 | return -EIO; |
| 3627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | if (formatsp) |
| 3629 | *formatsp = formats; |
| 3630 | if (bpsp) |
| 3631 | *bpsp = bps; |
| 3632 | } |
| 3633 | |
| 3634 | return 0; |
| 3635 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 3636 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | |
| 3638 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3639 | * snd_hda_is_supported_format - Check the validity of the format |
| 3640 | * @codec: HD-audio codec |
| 3641 | * @nid: NID to check |
| 3642 | * @format: the HD-audio format value to check |
| 3643 | * |
| 3644 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | * |
| 3646 | * Returns 1 if supported, 0 if not. |
| 3647 | */ |
| 3648 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 3649 | unsigned int format) |
| 3650 | { |
| 3651 | int i; |
| 3652 | unsigned int val = 0, rate, stream; |
| 3653 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3654 | val = query_pcm_param(codec, nid); |
| 3655 | if (!val) |
| 3656 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | |
| 3658 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3659 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3660 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | if (val & (1 << i)) |
| 3662 | break; |
| 3663 | return 0; |
| 3664 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3665 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | return 0; |
| 3667 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3668 | stream = query_stream_param(codec, nid); |
| 3669 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | return 0; |
| 3671 | |
| 3672 | if (stream & AC_SUPFMT_PCM) { |
| 3673 | switch (format & 0xf0) { |
| 3674 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3675 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | return 0; |
| 3677 | break; |
| 3678 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3679 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | return 0; |
| 3681 | break; |
| 3682 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3683 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | return 0; |
| 3685 | break; |
| 3686 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3687 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | return 0; |
| 3689 | break; |
| 3690 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3691 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | return 0; |
| 3693 | break; |
| 3694 | default: |
| 3695 | return 0; |
| 3696 | } |
| 3697 | } else { |
| 3698 | /* FIXME: check for float32 and AC3? */ |
| 3699 | } |
| 3700 | |
| 3701 | return 1; |
| 3702 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3703 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | |
| 3705 | /* |
| 3706 | * PCM stuff |
| 3707 | */ |
| 3708 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 3709 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3710 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | { |
| 3712 | return 0; |
| 3713 | } |
| 3714 | |
| 3715 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 3716 | struct hda_codec *codec, |
| 3717 | unsigned int stream_tag, |
| 3718 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3719 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | { |
| 3721 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
| 3725 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 3726 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3727 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 3729 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | return 0; |
| 3731 | } |
| 3732 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3733 | static int set_pcm_default_values(struct hda_codec *codec, |
| 3734 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3736 | int err; |
| 3737 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3738 | /* query support PCM information from the given NID */ |
| 3739 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3740 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3741 | info->rates ? NULL : &info->rates, |
| 3742 | info->formats ? NULL : &info->formats, |
| 3743 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3744 | if (err < 0) |
| 3745 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | } |
| 3747 | if (info->ops.open == NULL) |
| 3748 | info->ops.open = hda_pcm_default_open_close; |
| 3749 | if (info->ops.close == NULL) |
| 3750 | info->ops.close = hda_pcm_default_open_close; |
| 3751 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 3752 | if (snd_BUG_ON(!info->nid)) |
| 3753 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | info->ops.prepare = hda_pcm_default_prepare; |
| 3755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 3757 | if (snd_BUG_ON(!info->nid)) |
| 3758 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 3760 | } |
| 3761 | return 0; |
| 3762 | } |
| 3763 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3764 | /* |
| 3765 | * codec prepare/cleanup entries |
| 3766 | */ |
| 3767 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 3768 | struct hda_pcm_stream *hinfo, |
| 3769 | unsigned int stream, |
| 3770 | unsigned int format, |
| 3771 | struct snd_pcm_substream *substream) |
| 3772 | { |
| 3773 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3774 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3775 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 3776 | if (ret >= 0) |
| 3777 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3778 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3779 | return ret; |
| 3780 | } |
| 3781 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 3782 | |
| 3783 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 3784 | struct hda_pcm_stream *hinfo, |
| 3785 | struct snd_pcm_substream *substream) |
| 3786 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3787 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3788 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3789 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3790 | } |
| 3791 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 3792 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3793 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 3794 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 3795 | "Audio", "SPDIF", "HDMI", "Modem" |
| 3796 | }; |
| 3797 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3798 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3799 | * get the empty PCM device number to assign |
Takashi Iwai | c893622 | 2010-01-28 17:08:53 +0100 | [diff] [blame] | 3800 | * |
| 3801 | * 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] | 3802 | */ |
| 3803 | static int get_empty_pcm_device(struct hda_bus *bus, int type) |
| 3804 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3805 | /* audio device indices; not linear to keep compatibility */ |
| 3806 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 3807 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 3808 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 3809 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3810 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3811 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3812 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3813 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3814 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3815 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 3816 | return -EINVAL; |
| 3817 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3818 | |
| 3819 | for (i = 0; audio_idx[type][i] >= 0 ; i++) |
| 3820 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 3821 | return audio_idx[type][i]; |
| 3822 | |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 3823 | /* non-fixed slots starting from 10 */ |
| 3824 | for (i = 10; i < 32; i++) { |
| 3825 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 3826 | return i; |
| 3827 | } |
| 3828 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3829 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 3830 | snd_hda_pcm_type_name[type]); |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3831 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3835 | * attach a new PCM stream |
| 3836 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3837 | 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] | 3838 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3839 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3840 | struct hda_pcm_stream *info; |
| 3841 | int stream, err; |
| 3842 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3843 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3844 | return -EINVAL; |
| 3845 | for (stream = 0; stream < 2; stream++) { |
| 3846 | info = &pcm->stream[stream]; |
| 3847 | if (info->substreams) { |
| 3848 | err = set_pcm_default_values(codec, info); |
| 3849 | if (err < 0) |
| 3850 | return err; |
| 3851 | } |
| 3852 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3853 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3854 | } |
| 3855 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3856 | /* assign all PCMs of the given codec */ |
| 3857 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 3858 | { |
| 3859 | unsigned int pcm; |
| 3860 | int err; |
| 3861 | |
| 3862 | if (!codec->num_pcms) { |
| 3863 | if (!codec->patch_ops.build_pcms) |
| 3864 | return 0; |
| 3865 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3866 | if (err < 0) { |
| 3867 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3868 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3869 | err = snd_hda_codec_reset(codec); |
| 3870 | if (err < 0) { |
| 3871 | printk(KERN_ERR |
| 3872 | "hda_codec: cannot revert codec\n"); |
| 3873 | return err; |
| 3874 | } |
| 3875 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3876 | } |
| 3877 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 3878 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 3879 | int dev; |
| 3880 | |
| 3881 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 3882 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3883 | |
| 3884 | if (!cpcm->pcm) { |
| 3885 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 3886 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3887 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3888 | cpcm->device = dev; |
| 3889 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3890 | if (err < 0) { |
| 3891 | printk(KERN_ERR "hda_codec: cannot attach " |
| 3892 | "PCM stream %d for codec #%d\n", |
| 3893 | dev, codec->addr); |
| 3894 | continue; /* no fatal error */ |
| 3895 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | /** |
| 3902 | * snd_hda_build_pcms - build PCM information |
| 3903 | * @bus: the BUS |
| 3904 | * |
| 3905 | * Create PCM information for each codec included in the bus. |
| 3906 | * |
| 3907 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 3908 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 3909 | * to create its PCM instances. |
| 3910 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 3911 | * callback. |
| 3912 | * |
| 3913 | * At least, substreams, channels_min and channels_max must be filled for |
| 3914 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 3915 | * When rates and/or formats are zero, the supported values are queried |
| 3916 | * from the given nid. The nid is used also by the default ops.prepare |
| 3917 | * and ops.cleanup callbacks. |
| 3918 | * |
| 3919 | * The driver needs to call ops.open in its open callback. Similarly, |
| 3920 | * ops.close is supposed to be called in the close callback. |
| 3921 | * ops.prepare should be called in the prepare or hw_params callback |
| 3922 | * with the proper parameters for set up. |
| 3923 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 3924 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3925 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3927 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3929 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3931 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3932 | int err = snd_hda_codec_build_pcms(codec); |
| 3933 | if (err < 0) |
| 3934 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | } |
| 3936 | return 0; |
| 3937 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3938 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | /** |
| 3941 | * snd_hda_check_board_config - compare the current codec with the config table |
| 3942 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3943 | * @num_configs: number of config enums |
| 3944 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | * @tbl: configuration table, terminated by null entries |
| 3946 | * |
| 3947 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 3948 | * given configuration table. If a matching entry is found, returns its |
| 3949 | * config value (supposed to be 0 or positive). |
| 3950 | * |
| 3951 | * If no entries are matching, the function returns a negative value. |
| 3952 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3953 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 3954 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3955 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3957 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3958 | int i; |
| 3959 | for (i = 0; i < num_configs; i++) { |
| 3960 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3961 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3962 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 3963 | "selected\n", models[i]); |
| 3964 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3969 | if (!codec->bus->pci || !tbl) |
| 3970 | return -1; |
| 3971 | |
| 3972 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 3973 | if (!tbl) |
| 3974 | return -1; |
| 3975 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 3976 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3977 | char tmp[10]; |
| 3978 | const char *model = NULL; |
| 3979 | if (models) |
| 3980 | model = models[tbl->value]; |
| 3981 | if (!model) { |
| 3982 | sprintf(tmp, "#%d", tbl->value); |
| 3983 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 3985 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 3986 | "for config %x:%x (%s)\n", |
| 3987 | model, tbl->subvendor, tbl->subdevice, |
| 3988 | (tbl->name ? tbl->name : "Unknown device")); |
| 3989 | #endif |
| 3990 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | } |
| 3992 | return -1; |
| 3993 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3994 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | |
| 3996 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 3997 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3998 | subsystem ID with the |
| 3999 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4000 | |
| 4001 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4002 | where the vendor ID of the PCI device is: |
| 4003 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4004 | and the vendor/subvendor are found only at the codec. |
| 4005 | |
| 4006 | * @codec: the HDA codec |
| 4007 | * @num_configs: number of config enums |
| 4008 | * @models: array of model name strings |
| 4009 | * @tbl: configuration table, terminated by null entries |
| 4010 | * |
| 4011 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4012 | * given configuration table. If a matching entry is found, returns its |
| 4013 | * config value (supposed to be 0 or positive). |
| 4014 | * |
| 4015 | * If no entries are matching, the function returns a negative value. |
| 4016 | */ |
| 4017 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4018 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4019 | const struct snd_pci_quirk *tbl) |
| 4020 | { |
| 4021 | const struct snd_pci_quirk *q; |
| 4022 | |
| 4023 | /* Search for codec ID */ |
| 4024 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4025 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4026 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4027 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4028 | break; |
| 4029 | } |
| 4030 | |
| 4031 | if (!q->subvendor) |
| 4032 | return -1; |
| 4033 | |
| 4034 | tbl = q; |
| 4035 | |
| 4036 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4037 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4038 | char tmp[10]; |
| 4039 | const char *model = NULL; |
| 4040 | if (models) |
| 4041 | model = models[tbl->value]; |
| 4042 | if (!model) { |
| 4043 | sprintf(tmp, "#%d", tbl->value); |
| 4044 | model = tmp; |
| 4045 | } |
| 4046 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4047 | "for config %x:%x (%s)\n", |
| 4048 | model, tbl->subvendor, tbl->subdevice, |
| 4049 | (tbl->name ? tbl->name : "Unknown device")); |
| 4050 | #endif |
| 4051 | return tbl->value; |
| 4052 | } |
| 4053 | return -1; |
| 4054 | } |
| 4055 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4056 | |
| 4057 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | * snd_hda_add_new_ctls - create controls from the array |
| 4059 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4060 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | * |
| 4062 | * This helper function creates and add new controls in the given array. |
| 4063 | * The array must be terminated with an empty entry as terminator. |
| 4064 | * |
| 4065 | * Returns 0 if successful, or a negative error code. |
| 4066 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4067 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4068 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4070 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | |
| 4072 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4073 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4074 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4075 | if (knew->iface == -1) /* skip this codec private value */ |
| 4076 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4077 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4078 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4079 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4080 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4081 | if (addr > 0) |
| 4082 | kctl->id.device = addr; |
| 4083 | if (idx > 0) |
| 4084 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4085 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4086 | if (!err) |
| 4087 | break; |
| 4088 | /* try first with another device index corresponding to |
| 4089 | * the codec addr; if it still fails (or it's the |
| 4090 | * primary codec), then try another control index |
| 4091 | */ |
| 4092 | if (!addr && codec->addr) |
| 4093 | addr = codec->addr; |
| 4094 | else if (!idx && !knew->index) { |
| 4095 | idx = find_empty_mixer_ctl_idx(codec, |
| 4096 | knew->name); |
| 4097 | if (idx <= 0) |
| 4098 | return err; |
| 4099 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4100 | return err; |
| 4101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | } |
| 4103 | return 0; |
| 4104 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4105 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4107 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4108 | static void hda_power_work(struct work_struct *work) |
| 4109 | { |
| 4110 | struct hda_codec *codec = |
| 4111 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4112 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4113 | |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4114 | if (!codec->power_on || codec->power_count) { |
| 4115 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4116 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4117 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4118 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4119 | trace_hda_power_down(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4120 | hda_call_codec_suspend(codec); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4121 | if (bus->ops.pm_notify) |
| 4122 | bus->ops.pm_notify(bus); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4126 | { |
| 4127 | codec->power_count++; |
| 4128 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4129 | codec->power_jiffies = jiffies; |
| 4130 | } |
| 4131 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4132 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4133 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4134 | { |
| 4135 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4136 | if (codec->power_on) |
| 4137 | codec->power_on_acct += delta; |
| 4138 | else |
| 4139 | codec->power_off_acct += delta; |
| 4140 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4143 | /** |
| 4144 | * snd_hda_power_up - Power-up the codec |
| 4145 | * @codec: HD-audio codec |
| 4146 | * |
| 4147 | * Increment the power-up counter and power up the hardware really when |
| 4148 | * not turned on yet. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4149 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4150 | void snd_hda_power_up(struct hda_codec *codec) |
| 4151 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4152 | struct hda_bus *bus = codec->bus; |
| 4153 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4154 | codec->power_count++; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4155 | if (codec->power_on || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4156 | return; |
| 4157 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4158 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4159 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4160 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4161 | codec->power_jiffies = jiffies; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4162 | if (bus->ops.pm_notify) |
| 4163 | bus->ops.pm_notify(bus); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4164 | hda_call_codec_resume(codec); |
| 4165 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4166 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4167 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4168 | EXPORT_SYMBOL_HDA(snd_hda_power_up); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 4169 | |
| 4170 | #define power_save(codec) \ |
| 4171 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4172 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4173 | /** |
| 4174 | * snd_hda_power_down - Power-down the codec |
| 4175 | * @codec: HD-audio codec |
| 4176 | * |
| 4177 | * Decrement the power-up counter and schedules the power-off work if |
| 4178 | * the counter rearches to zero. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4179 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4180 | void snd_hda_power_down(struct hda_codec *codec) |
| 4181 | { |
| 4182 | --codec->power_count; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4183 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4184 | return; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4185 | if (power_save(codec)) { |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4186 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 4187 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4188 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4189 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4190 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4191 | EXPORT_SYMBOL_HDA(snd_hda_power_down); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4192 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4193 | /** |
| 4194 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4195 | * @codec: HD-audio codec |
| 4196 | * @check: the object containing an AMP list and the status |
| 4197 | * @nid: NID to check / update |
| 4198 | * |
| 4199 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4200 | * check the current AMP status, and update the the power-status according |
| 4201 | * to the mute status. |
| 4202 | * |
| 4203 | * This function is supposed to be set or called from the check_power_status |
| 4204 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4205 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4206 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4207 | struct hda_loopback_check *check, |
| 4208 | hda_nid_t nid) |
| 4209 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4210 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4211 | int ch, v; |
| 4212 | |
| 4213 | if (!check->amplist) |
| 4214 | return 0; |
| 4215 | for (p = check->amplist; p->nid; p++) { |
| 4216 | if (p->nid == nid) |
| 4217 | break; |
| 4218 | } |
| 4219 | if (!p->nid) |
| 4220 | return 0; /* nothing changed */ |
| 4221 | |
| 4222 | for (p = check->amplist; p->nid; p++) { |
| 4223 | for (ch = 0; ch < 2; ch++) { |
| 4224 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4225 | p->idx); |
| 4226 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4227 | if (!check->power_on) { |
| 4228 | check->power_on = 1; |
| 4229 | snd_hda_power_up(codec); |
| 4230 | } |
| 4231 | return 1; |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | if (check->power_on) { |
| 4236 | check->power_on = 0; |
| 4237 | snd_hda_power_down(codec); |
| 4238 | } |
| 4239 | return 0; |
| 4240 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4241 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4242 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4244 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4245 | * Channel mode helper |
| 4246 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4247 | |
| 4248 | /** |
| 4249 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 4250 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4251 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4252 | struct snd_ctl_elem_info *uinfo, |
| 4253 | const struct hda_channel_mode *chmode, |
| 4254 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4255 | { |
| 4256 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4257 | uinfo->count = 1; |
| 4258 | uinfo->value.enumerated.items = num_chmodes; |
| 4259 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4260 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4261 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4262 | chmode[uinfo->value.enumerated.item].channels); |
| 4263 | return 0; |
| 4264 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4265 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4266 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4267 | /** |
| 4268 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 4269 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4270 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4271 | struct snd_ctl_elem_value *ucontrol, |
| 4272 | const struct hda_channel_mode *chmode, |
| 4273 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4274 | int max_channels) |
| 4275 | { |
| 4276 | int i; |
| 4277 | |
| 4278 | for (i = 0; i < num_chmodes; i++) { |
| 4279 | if (max_channels == chmode[i].channels) { |
| 4280 | ucontrol->value.enumerated.item[0] = i; |
| 4281 | break; |
| 4282 | } |
| 4283 | } |
| 4284 | return 0; |
| 4285 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4286 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4287 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4288 | /** |
| 4289 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 4290 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4291 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 4292 | struct snd_ctl_elem_value *ucontrol, |
| 4293 | const struct hda_channel_mode *chmode, |
| 4294 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4295 | int *max_channelsp) |
| 4296 | { |
| 4297 | unsigned int mode; |
| 4298 | |
| 4299 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 4300 | if (mode >= num_chmodes) |
| 4301 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4302 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4303 | return 0; |
| 4304 | /* change the current channel setting */ |
| 4305 | *max_channelsp = chmode[mode].channels; |
| 4306 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4307 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4308 | return 1; |
| 4309 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4310 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | /* |
| 4313 | * input MUX helper |
| 4314 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4315 | |
| 4316 | /** |
| 4317 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 4318 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4319 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4320 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | { |
| 4322 | unsigned int index; |
| 4323 | |
| 4324 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4325 | uinfo->count = 1; |
| 4326 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4327 | if (!imux->num_items) |
| 4328 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | index = uinfo->value.enumerated.item; |
| 4330 | if (index >= imux->num_items) |
| 4331 | index = imux->num_items - 1; |
| 4332 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4333 | return 0; |
| 4334 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4335 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4337 | /** |
| 4338 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 4339 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4340 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4341 | const struct hda_input_mux *imux, |
| 4342 | struct snd_ctl_elem_value *ucontrol, |
| 4343 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | unsigned int *cur_val) |
| 4345 | { |
| 4346 | unsigned int idx; |
| 4347 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4348 | if (!imux->num_items) |
| 4349 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | idx = ucontrol->value.enumerated.item[0]; |
| 4351 | if (idx >= imux->num_items) |
| 4352 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4353 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4355 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4356 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | *cur_val = idx; |
| 4358 | return 1; |
| 4359 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4360 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | |
| 4362 | |
| 4363 | /* |
| 4364 | * Multi-channel / digital-out PCM helper functions |
| 4365 | */ |
| 4366 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4367 | /* setup SPDIF output stream */ |
| 4368 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 4369 | unsigned int stream_tag, unsigned int format) |
| 4370 | { |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4371 | struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 4372 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4373 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4374 | if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4375 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4376 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4377 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4378 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4379 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4380 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4381 | for (d = codec->slave_dig_outs; *d; d++) |
| 4382 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 4383 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4384 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4385 | /* turn on again (if needed) */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4386 | if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4387 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4388 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4389 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4390 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4391 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 4392 | { |
| 4393 | snd_hda_codec_cleanup_stream(codec, nid); |
| 4394 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4395 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4396 | for (d = codec->slave_dig_outs; *d; d++) |
| 4397 | snd_hda_codec_cleanup_stream(codec, *d); |
| 4398 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4399 | } |
| 4400 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4401 | /** |
| 4402 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 4403 | * @bus: HD-audio bus |
| 4404 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4405 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 4406 | { |
| 4407 | struct hda_codec *codec; |
| 4408 | |
| 4409 | if (!bus) |
| 4410 | return; |
| 4411 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 4412 | if (hda_codec_is_power_on(codec) && |
| 4413 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4414 | codec->patch_ops.reboot_notify(codec); |
| 4415 | } |
| 4416 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 4417 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4418 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4419 | /** |
| 4420 | * 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] | 4421 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4422 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 4423 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4425 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 4426 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 4427 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4428 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4430 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | return 0; |
| 4432 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4433 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4435 | /** |
| 4436 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 4437 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4438 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 4439 | struct hda_multi_out *mout, |
| 4440 | unsigned int stream_tag, |
| 4441 | unsigned int format, |
| 4442 | struct snd_pcm_substream *substream) |
| 4443 | { |
| 4444 | mutex_lock(&codec->spdif_mutex); |
| 4445 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 4446 | mutex_unlock(&codec->spdif_mutex); |
| 4447 | return 0; |
| 4448 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4449 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4450 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4451 | /** |
| 4452 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 4453 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 4454 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 4455 | struct hda_multi_out *mout) |
| 4456 | { |
| 4457 | mutex_lock(&codec->spdif_mutex); |
| 4458 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 4459 | mutex_unlock(&codec->spdif_mutex); |
| 4460 | return 0; |
| 4461 | } |
| 4462 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 4463 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4464 | /** |
| 4465 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4467 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 4468 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4470 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4472 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | return 0; |
| 4474 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4475 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4477 | /** |
| 4478 | * snd_hda_multi_out_analog_open - open analog outputs |
| 4479 | * |
| 4480 | * Open analog outputs and set up the hw-constraints. |
| 4481 | * If the digital outputs can be opened as slave, open the digital |
| 4482 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4484 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 4485 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4486 | struct snd_pcm_substream *substream, |
| 4487 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4489 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 4490 | runtime->hw.channels_max = mout->max_channels; |
| 4491 | if (mout->dig_out_nid) { |
| 4492 | if (!mout->analog_rates) { |
| 4493 | mout->analog_rates = hinfo->rates; |
| 4494 | mout->analog_formats = hinfo->formats; |
| 4495 | mout->analog_maxbps = hinfo->maxbps; |
| 4496 | } else { |
| 4497 | runtime->hw.rates = mout->analog_rates; |
| 4498 | runtime->hw.formats = mout->analog_formats; |
| 4499 | hinfo->maxbps = mout->analog_maxbps; |
| 4500 | } |
| 4501 | if (!mout->spdif_rates) { |
| 4502 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 4503 | &mout->spdif_rates, |
| 4504 | &mout->spdif_formats, |
| 4505 | &mout->spdif_maxbps); |
| 4506 | } |
| 4507 | mutex_lock(&codec->spdif_mutex); |
| 4508 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 4509 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 4510 | (runtime->hw.formats & mout->spdif_formats)) { |
| 4511 | runtime->hw.rates &= mout->spdif_rates; |
| 4512 | runtime->hw.formats &= mout->spdif_formats; |
| 4513 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 4514 | hinfo->maxbps = mout->spdif_maxbps; |
| 4515 | } else { |
| 4516 | mout->share_spdif = 0; |
| 4517 | /* FIXME: need notify? */ |
| 4518 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4519 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 4520 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 4523 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 4524 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4525 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4527 | /** |
| 4528 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 4529 | * |
| 4530 | * Set up the i/o for analog out. |
| 4531 | * 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] | 4532 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4533 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 4534 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | unsigned int stream_tag, |
| 4536 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4537 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4539 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | int chs = substream->runtime->channels; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4541 | struct hda_spdif_out *spdif = |
| 4542 | snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | int i; |
| 4544 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4545 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4546 | if (mout->dig_out_nid && mout->share_spdif && |
| 4547 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4549 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 4550 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4551 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4553 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 4554 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | } else { |
| 4556 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4557 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | } |
| 4559 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4560 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | |
| 4562 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4563 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 4564 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4565 | if (!mout->no_share_stream && |
| 4566 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4568 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 4569 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4570 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 4571 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 4572 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 4573 | snd_hda_codec_setup_stream(codec, |
| 4574 | mout->hp_out_nid[i], |
| 4575 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4576 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4577 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4578 | snd_hda_codec_setup_stream(codec, |
| 4579 | mout->extra_out_nid[i], |
| 4580 | stream_tag, 0, format); |
| 4581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | /* surrounds */ |
| 4583 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 4584 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4585 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 4586 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4587 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4588 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 4589 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
| 4591 | return 0; |
| 4592 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4593 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4595 | /** |
| 4596 | * 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] | 4597 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4598 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 4599 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4601 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | int i; |
| 4603 | |
| 4604 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4605 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4607 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 4608 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 4609 | if (mout->hp_out_nid[i]) |
| 4610 | snd_hda_codec_cleanup_stream(codec, |
| 4611 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4612 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 4613 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4614 | snd_hda_codec_cleanup_stream(codec, |
| 4615 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4616 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | 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] | 4618 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | mout->dig_out_used = 0; |
| 4620 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4621 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | return 0; |
| 4623 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4624 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4626 | /* |
Wu Fengguang | 6b34500 | 2008-10-07 14:21:41 +0800 | [diff] [blame] | 4627 | * Helper for automatic pin configuration |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4628 | */ |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 4629 | |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4630 | static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list) |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 4631 | { |
| 4632 | for (; *list; list++) |
| 4633 | if (*list == nid) |
| 4634 | return 1; |
| 4635 | return 0; |
| 4636 | } |
| 4637 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4638 | |
| 4639 | /* |
| 4640 | * Sort an associated group of pins according to their sequence numbers. |
| 4641 | */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4642 | static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences, |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4643 | int num_pins) |
| 4644 | { |
| 4645 | int i, j; |
| 4646 | short seq; |
| 4647 | hda_nid_t nid; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4648 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4649 | for (i = 0; i < num_pins; i++) { |
| 4650 | for (j = i + 1; j < num_pins; j++) { |
| 4651 | if (sequences[i] > sequences[j]) { |
| 4652 | seq = sequences[i]; |
| 4653 | sequences[i] = sequences[j]; |
| 4654 | sequences[j] = seq; |
| 4655 | nid = pins[i]; |
| 4656 | pins[i] = pins[j]; |
| 4657 | pins[j] = nid; |
| 4658 | } |
| 4659 | } |
| 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4664 | /* add the found input-pin to the cfg->inputs[] table */ |
| 4665 | static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid, |
| 4666 | int type) |
| 4667 | { |
| 4668 | if (cfg->num_inputs < AUTO_CFG_MAX_INS) { |
| 4669 | cfg->inputs[cfg->num_inputs].pin = nid; |
| 4670 | cfg->inputs[cfg->num_inputs].type = type; |
| 4671 | cfg->num_inputs++; |
| 4672 | } |
| 4673 | } |
| 4674 | |
Takashi Iwai | 4a4d4a6 | 2010-09-09 22:22:02 +0200 | [diff] [blame] | 4675 | /* sort inputs in the order of AUTO_PIN_* type */ |
| 4676 | static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg) |
| 4677 | { |
| 4678 | int i, j; |
| 4679 | |
| 4680 | for (i = 0; i < cfg->num_inputs; i++) { |
| 4681 | for (j = i + 1; j < cfg->num_inputs; j++) { |
| 4682 | if (cfg->inputs[i].type > cfg->inputs[j].type) { |
| 4683 | struct auto_pin_cfg_item tmp; |
| 4684 | tmp = cfg->inputs[i]; |
| 4685 | cfg->inputs[i] = cfg->inputs[j]; |
| 4686 | cfg->inputs[j] = tmp; |
| 4687 | } |
| 4688 | } |
| 4689 | } |
| 4690 | } |
| 4691 | |
Takashi Iwai | 8fa7ab4 | 2011-10-26 16:06:27 +0200 | [diff] [blame] | 4692 | /* Reorder the surround channels |
| 4693 | * ALSA sequence is front/surr/clfe/side |
| 4694 | * HDA sequence is: |
| 4695 | * 4-ch: front/surr => OK as it is |
| 4696 | * 6-ch: front/clfe/surr |
| 4697 | * 8-ch: front/clfe/rear/side|fc |
| 4698 | */ |
| 4699 | static void reorder_outputs(unsigned int nums, hda_nid_t *pins) |
| 4700 | { |
| 4701 | hda_nid_t nid; |
| 4702 | |
| 4703 | switch (nums) { |
| 4704 | case 3: |
| 4705 | case 4: |
| 4706 | nid = pins[1]; |
| 4707 | pins[1] = pins[2]; |
| 4708 | pins[2] = nid; |
| 4709 | break; |
| 4710 | } |
| 4711 | } |
| 4712 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4713 | /* |
| 4714 | * Parse all pin widgets and store the useful pin nids to cfg |
| 4715 | * |
| 4716 | * The number of line-outs or any primary output is stored in line_outs, |
| 4717 | * and the corresponding output pins are assigned to line_out_pins[], |
| 4718 | * in the order of front, rear, CLFE, side, ... |
| 4719 | * |
| 4720 | * If more extra outputs (speaker and headphone) are found, the pins are |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4721 | * 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] | 4722 | * is detected, one of speaker of HP pins is assigned as the primary |
| 4723 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 4724 | * if any analog output exists. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4725 | * |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4726 | * The analog input pins are assigned to inputs array. |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4727 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 4728 | * respectively. |
| 4729 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4730 | int snd_hda_parse_pin_defcfg(struct hda_codec *codec, |
| 4731 | struct auto_pin_cfg *cfg, |
| 4732 | const hda_nid_t *ignore_nids, |
| 4733 | unsigned int cond_flags) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4734 | { |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 4735 | hda_nid_t nid, end_nid; |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4736 | short seq, assoc_line_out; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4737 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 4738 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4739 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4740 | int i; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4741 | |
| 4742 | memset(cfg, 0, sizeof(*cfg)); |
| 4743 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4744 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 4745 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4746 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4747 | assoc_line_out = 0; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4748 | |
Takashi Iwai | 2f451d2 | 2011-11-10 12:36:46 +0100 | [diff] [blame] | 4749 | codec->ignore_misc_bit = true; |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 4750 | end_nid = codec->start_nid + codec->num_nodes; |
| 4751 | for (nid = codec->start_nid; nid < end_nid; nid++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4752 | unsigned int wid_caps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 4753 | unsigned int wid_type = get_wcaps_type(wid_caps); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4754 | unsigned int def_conf; |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4755 | short assoc, loc, conn, dev; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4756 | |
| 4757 | /* read all default configuration for pin complex */ |
| 4758 | if (wid_type != AC_WID_PIN) |
| 4759 | continue; |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 4760 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 4761 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 4762 | continue; |
| 4763 | |
Takashi Iwai | c17a1ab | 2009-02-23 09:28:12 +0100 | [diff] [blame] | 4764 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 2f451d2 | 2011-11-10 12:36:46 +0100 | [diff] [blame] | 4765 | if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
| 4766 | AC_DEFCFG_MISC_NO_PRESENCE)) |
| 4767 | codec->ignore_misc_bit = false; |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4768 | conn = get_defcfg_connect(def_conf); |
| 4769 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4770 | continue; |
| 4771 | loc = get_defcfg_location(def_conf); |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4772 | dev = get_defcfg_device(def_conf); |
| 4773 | |
| 4774 | /* workaround for buggy BIOS setups */ |
| 4775 | if (dev == AC_JACK_LINE_OUT) { |
| 4776 | if (conn == AC_JACK_PORT_FIXED) |
| 4777 | dev = AC_JACK_SPEAKER; |
| 4778 | } |
| 4779 | |
| 4780 | switch (dev) { |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4781 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4782 | seq = get_defcfg_sequence(def_conf); |
| 4783 | assoc = get_defcfg_association(def_conf); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 4784 | |
| 4785 | if (!(wid_caps & AC_WCAP_STEREO)) |
| 4786 | if (!cfg->mono_out_pin) |
| 4787 | cfg->mono_out_pin = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4788 | if (!assoc) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4789 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4790 | if (!assoc_line_out) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4791 | assoc_line_out = assoc; |
| 4792 | else if (assoc_line_out != assoc) |
| 4793 | continue; |
| 4794 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 4795 | continue; |
| 4796 | cfg->line_out_pins[cfg->line_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4797 | sequences_line_out[cfg->line_outs] = seq; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4798 | cfg->line_outs++; |
| 4799 | break; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 4800 | case AC_JACK_SPEAKER: |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4801 | seq = get_defcfg_sequence(def_conf); |
| 4802 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4803 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 4804 | continue; |
| 4805 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4806 | sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4807 | cfg->speaker_outs++; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 4808 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4809 | case AC_JACK_HP_OUT: |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4810 | seq = get_defcfg_sequence(def_conf); |
| 4811 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4812 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 4813 | continue; |
| 4814 | cfg->hp_pins[cfg->hp_outs] = nid; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4815 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4816 | cfg->hp_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4817 | break; |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4818 | case AC_JACK_MIC_IN: |
| 4819 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4820 | break; |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4821 | case AC_JACK_LINE_IN: |
| 4822 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4823 | break; |
| 4824 | case AC_JACK_CD: |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4825 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4826 | break; |
| 4827 | case AC_JACK_AUX: |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4828 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4829 | break; |
| 4830 | case AC_JACK_SPDIF_OUT: |
Takashi Iwai | 1b52ae7 | 2009-01-20 17:17:29 +0100 | [diff] [blame] | 4831 | case AC_JACK_DIG_OTHER_OUT: |
Takashi Iwai | 0852d7a | 2009-02-11 11:35:15 +0100 | [diff] [blame] | 4832 | if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) |
| 4833 | continue; |
| 4834 | cfg->dig_out_pins[cfg->dig_outs] = nid; |
| 4835 | cfg->dig_out_type[cfg->dig_outs] = |
| 4836 | (loc == AC_JACK_LOC_HDMI) ? |
| 4837 | HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF; |
| 4838 | cfg->dig_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4839 | break; |
| 4840 | case AC_JACK_SPDIF_IN: |
Takashi Iwai | 1b52ae7 | 2009-01-20 17:17:29 +0100 | [diff] [blame] | 4841 | case AC_JACK_DIG_OTHER_IN: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4842 | cfg->dig_in_pin = nid; |
Takashi Iwai | 2297bd6 | 2009-01-20 18:24:13 +0100 | [diff] [blame] | 4843 | if (loc == AC_JACK_LOC_HDMI) |
| 4844 | cfg->dig_in_type = HDA_PCM_TYPE_HDMI; |
| 4845 | else |
| 4846 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4847 | break; |
| 4848 | } |
| 4849 | } |
| 4850 | |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4851 | /* FIX-UP: |
| 4852 | * If no line-out is defined but multiple HPs are found, |
| 4853 | * some of them might be the real line-outs. |
| 4854 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4855 | if (!cfg->line_outs && cfg->hp_outs > 1 && |
| 4856 | !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) { |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4857 | int i = 0; |
| 4858 | while (i < cfg->hp_outs) { |
| 4859 | /* The real HPs should have the sequence 0x0f */ |
| 4860 | if ((sequences_hp[i] & 0x0f) == 0x0f) { |
| 4861 | i++; |
| 4862 | continue; |
| 4863 | } |
| 4864 | /* Move it to the line-out table */ |
| 4865 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; |
| 4866 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; |
| 4867 | cfg->line_outs++; |
| 4868 | cfg->hp_outs--; |
| 4869 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, |
| 4870 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); |
Takashi Iwai | 122661b | 2010-09-08 14:57:04 +0200 | [diff] [blame] | 4871 | memmove(sequences_hp + i, sequences_hp + i + 1, |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4872 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); |
| 4873 | } |
Takashi Iwai | 03642c9 | 2010-09-08 15:28:19 +0200 | [diff] [blame] | 4874 | memset(cfg->hp_pins + cfg->hp_outs, 0, |
| 4875 | sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs)); |
Takashi Iwai | b2d0576 | 2011-01-10 14:47:35 +0100 | [diff] [blame] | 4876 | if (!cfg->hp_outs) |
| 4877 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 4878 | |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4881 | /* sort by sequence */ |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4882 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 4883 | cfg->line_outs); |
| 4884 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 4885 | cfg->speaker_outs); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4886 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
| 4887 | cfg->hp_outs); |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4888 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4889 | /* |
| 4890 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 4891 | * as a primary output |
| 4892 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4893 | if (!cfg->line_outs && |
| 4894 | !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) { |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4895 | if (cfg->speaker_outs) { |
| 4896 | cfg->line_outs = cfg->speaker_outs; |
| 4897 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 4898 | sizeof(cfg->speaker_pins)); |
| 4899 | cfg->speaker_outs = 0; |
| 4900 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 4901 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 4902 | } else if (cfg->hp_outs) { |
| 4903 | cfg->line_outs = cfg->hp_outs; |
| 4904 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 4905 | sizeof(cfg->hp_pins)); |
| 4906 | cfg->hp_outs = 0; |
| 4907 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 4908 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 4909 | } |
| 4910 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4911 | |
Takashi Iwai | 8fa7ab4 | 2011-10-26 16:06:27 +0200 | [diff] [blame] | 4912 | reorder_outputs(cfg->line_outs, cfg->line_out_pins); |
| 4913 | reorder_outputs(cfg->hp_outs, cfg->hp_pins); |
| 4914 | reorder_outputs(cfg->speaker_outs, cfg->speaker_pins); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4915 | |
Takashi Iwai | 4a4d4a6 | 2010-09-09 22:22:02 +0200 | [diff] [blame] | 4916 | sort_autocfg_input_pins(cfg); |
| 4917 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4918 | /* |
| 4919 | * debug prints of the parsed results |
| 4920 | */ |
Takashi Iwai | c2de187 | 2011-04-29 13:00:40 +0200 | [diff] [blame] | 4921 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n", |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4922 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 4923 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
Takashi Iwai | c2de187 | 2011-04-29 13:00:40 +0200 | [diff] [blame] | 4924 | cfg->line_out_pins[4], |
| 4925 | cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" : |
| 4926 | (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ? |
| 4927 | "speaker" : "line")); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4928 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 4929 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 4930 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 4931 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4932 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 4933 | cfg->hp_outs, cfg->hp_pins[0], |
| 4934 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 4935 | cfg->hp_pins[3], cfg->hp_pins[4]); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 4936 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
Takashi Iwai | 0852d7a | 2009-02-11 11:35:15 +0100 | [diff] [blame] | 4937 | if (cfg->dig_outs) |
| 4938 | snd_printd(" dig-out=0x%x/0x%x\n", |
| 4939 | cfg->dig_out_pins[0], cfg->dig_out_pins[1]); |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4940 | snd_printd(" inputs:"); |
| 4941 | for (i = 0; i < cfg->num_inputs; i++) { |
Takashi Iwai | 0b62673 | 2011-05-26 14:10:44 +0200 | [diff] [blame] | 4942 | snd_printd(" %s=0x%x", |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 4943 | hda_get_autocfg_input_label(codec, cfg, i), |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4944 | cfg->inputs[i].pin); |
| 4945 | } |
| 4946 | snd_printd("\n"); |
Takashi Iwai | 32d2c7f | 2009-02-11 11:33:13 +0100 | [diff] [blame] | 4947 | if (cfg->dig_in_pin) |
Takashi Iwai | 89ce9e8 | 2009-01-20 17:15:57 +0100 | [diff] [blame] | 4948 | snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4949 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4950 | return 0; |
| 4951 | } |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4952 | EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4953 | |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4954 | int snd_hda_get_input_pin_attr(unsigned int def_conf) |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4955 | { |
| 4956 | unsigned int loc = get_defcfg_location(def_conf); |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 4957 | unsigned int conn = get_defcfg_connect(def_conf); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4958 | if (conn == AC_JACK_PORT_NONE) |
| 4959 | return INPUT_PIN_ATTR_UNUSED; |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 4960 | /* Windows may claim the internal mic to be BOTH, too */ |
| 4961 | if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4962 | return INPUT_PIN_ATTR_INT; |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 4963 | if ((loc & 0x30) == AC_JACK_LOC_INTERNAL) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4964 | return INPUT_PIN_ATTR_INT; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4965 | if ((loc & 0x30) == AC_JACK_LOC_SEPARATE) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4966 | return INPUT_PIN_ATTR_DOCK; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4967 | if (loc == AC_JACK_LOC_REAR) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4968 | return INPUT_PIN_ATTR_REAR; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4969 | if (loc == AC_JACK_LOC_FRONT) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4970 | return INPUT_PIN_ATTR_FRONT; |
| 4971 | return INPUT_PIN_ATTR_NORMAL; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4972 | } |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4973 | EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr); |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4974 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 4975 | /** |
| 4976 | * hda_get_input_pin_label - Give a label for the given input pin |
| 4977 | * |
| 4978 | * When check_location is true, the function checks the pin location |
| 4979 | * for mic and line-in pins, and set an appropriate prefix like "Front", |
| 4980 | * "Rear", "Internal". |
| 4981 | */ |
| 4982 | |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 4983 | static const char *hda_get_input_pin_label(struct hda_codec *codec, |
| 4984 | hda_nid_t pin, bool check_location) |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 4985 | { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4986 | unsigned int def_conf; |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4987 | static const char * const mic_names[] = { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 4988 | "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic", |
| 4989 | }; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4990 | int attr; |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 4991 | |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 4992 | def_conf = snd_hda_codec_get_pincfg(codec, pin); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 4993 | |
| 4994 | switch (get_defcfg_device(def_conf)) { |
| 4995 | case AC_JACK_MIC_IN: |
| 4996 | if (!check_location) |
| 4997 | return "Mic"; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 4998 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 4999 | if (!attr) |
| 5000 | return "None"; |
| 5001 | return mic_names[attr - 1]; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5002 | case AC_JACK_LINE_IN: |
| 5003 | if (!check_location) |
| 5004 | return "Line"; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5005 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 5006 | if (!attr) |
| 5007 | return "None"; |
| 5008 | if (attr == INPUT_PIN_ATTR_DOCK) |
| 5009 | return "Dock Line"; |
| 5010 | return "Line"; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5011 | case AC_JACK_AUX: |
| 5012 | return "Aux"; |
| 5013 | case AC_JACK_CD: |
| 5014 | return "CD"; |
| 5015 | case AC_JACK_SPDIF_IN: |
| 5016 | return "SPDIF In"; |
| 5017 | case AC_JACK_DIG_OTHER_IN: |
| 5018 | return "Digital In"; |
| 5019 | default: |
| 5020 | return "Misc"; |
| 5021 | } |
| 5022 | } |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5023 | |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5024 | /* Check whether the location prefix needs to be added to the label. |
| 5025 | * If all mic-jacks are in the same location (e.g. rear panel), we don't |
| 5026 | * have to put "Front" prefix to each label. In such a case, returns false. |
| 5027 | */ |
| 5028 | static int check_mic_location_need(struct hda_codec *codec, |
| 5029 | const struct auto_pin_cfg *cfg, |
| 5030 | int input) |
| 5031 | { |
| 5032 | unsigned int defc; |
| 5033 | int i, attr, attr2; |
| 5034 | |
| 5035 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5036 | attr = snd_hda_get_input_pin_attr(defc); |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5037 | /* for internal or docking mics, we need locations */ |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5038 | if (attr <= INPUT_PIN_ATTR_NORMAL) |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5039 | return 1; |
| 5040 | |
| 5041 | attr = 0; |
| 5042 | for (i = 0; i < cfg->num_inputs; i++) { |
| 5043 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5044 | attr2 = snd_hda_get_input_pin_attr(defc); |
| 5045 | if (attr2 >= INPUT_PIN_ATTR_NORMAL) { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5046 | if (attr && attr != attr2) |
| 5047 | return 1; /* different locations found */ |
| 5048 | attr = attr2; |
| 5049 | } |
| 5050 | } |
| 5051 | return 0; |
| 5052 | } |
| 5053 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5054 | /** |
| 5055 | * hda_get_autocfg_input_label - Get a label for the given input |
| 5056 | * |
| 5057 | * Get a label for the given input pin defined by the autocfg item. |
| 5058 | * Unlike hda_get_input_pin_label(), this function checks all inputs |
| 5059 | * defined in autocfg and avoids the redundant mic/line prefix as much as |
| 5060 | * possible. |
| 5061 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5062 | const char *hda_get_autocfg_input_label(struct hda_codec *codec, |
| 5063 | const struct auto_pin_cfg *cfg, |
| 5064 | int input) |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5065 | { |
| 5066 | int type = cfg->inputs[input].type; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5067 | int has_multiple_pins = 0; |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5068 | |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5069 | if ((input > 0 && cfg->inputs[input - 1].type == type) || |
| 5070 | (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type)) |
| 5071 | has_multiple_pins = 1; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5072 | if (has_multiple_pins && type == AUTO_PIN_MIC) |
| 5073 | has_multiple_pins &= check_mic_location_need(codec, cfg, input); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5074 | return hda_get_input_pin_label(codec, cfg->inputs[input].pin, |
| 5075 | has_multiple_pins); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5076 | } |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5077 | EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label); |
| 5078 | |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5079 | /* return the position of NID in the list, or -1 if not found */ |
| 5080 | static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) |
| 5081 | { |
| 5082 | int i; |
| 5083 | for (i = 0; i < nums; i++) |
| 5084 | if (list[i] == nid) |
| 5085 | return i; |
| 5086 | return -1; |
| 5087 | } |
| 5088 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5089 | /* get a unique suffix or an index number */ |
| 5090 | static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins, |
| 5091 | int num_pins, int *indexp) |
| 5092 | { |
| 5093 | static const char * const channel_sfx[] = { |
Takashi Iwai | d6018bb | 2011-11-21 14:17:16 +0100 | [diff] [blame] | 5094 | " Front", " Surround", " CLFE", " Side" |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5095 | }; |
| 5096 | int i; |
| 5097 | |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5098 | i = find_idx_in_nid_list(nid, pins, num_pins); |
| 5099 | if (i < 0) |
| 5100 | return NULL; |
| 5101 | if (num_pins == 1) |
| 5102 | return ""; |
| 5103 | if (num_pins > ARRAY_SIZE(channel_sfx)) { |
| 5104 | if (indexp) |
| 5105 | *indexp = i; |
| 5106 | return ""; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5107 | } |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5108 | return channel_sfx[i]; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid, |
| 5112 | const struct auto_pin_cfg *cfg, |
| 5113 | const char *name, char *label, int maxlen, |
| 5114 | int *indexp) |
| 5115 | { |
| 5116 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 5117 | int attr = snd_hda_get_input_pin_attr(def_conf); |
| 5118 | const char *pfx = "", *sfx = ""; |
| 5119 | |
| 5120 | /* handle as a speaker if it's a fixed line-out */ |
| 5121 | if (!strcmp(name, "Line-Out") && attr == INPUT_PIN_ATTR_INT) |
| 5122 | name = "Speaker"; |
| 5123 | /* check the location */ |
| 5124 | switch (attr) { |
| 5125 | case INPUT_PIN_ATTR_DOCK: |
| 5126 | pfx = "Dock "; |
| 5127 | break; |
| 5128 | case INPUT_PIN_ATTR_FRONT: |
| 5129 | pfx = "Front "; |
| 5130 | break; |
| 5131 | } |
| 5132 | if (cfg) { |
| 5133 | /* try to give a unique suffix if needed */ |
| 5134 | sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs, |
| 5135 | indexp); |
| 5136 | if (!sfx) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5137 | sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs, |
| 5138 | indexp); |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5139 | if (!sfx) { |
| 5140 | /* don't add channel suffix for Headphone controls */ |
| 5141 | int idx = find_idx_in_nid_list(nid, cfg->hp_pins, |
| 5142 | cfg->hp_outs); |
| 5143 | if (idx >= 0) |
| 5144 | *indexp = idx; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5145 | sfx = ""; |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5146 | } |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5147 | } |
| 5148 | snprintf(label, maxlen, "%s%s%s", pfx, name, sfx); |
| 5149 | return 1; |
| 5150 | } |
| 5151 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5152 | /** |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5153 | * snd_hda_get_pin_label - Get a label for the given I/O pin |
| 5154 | * |
| 5155 | * Get a label for the given pin. This function works for both input and |
| 5156 | * output pins. When @cfg is given as non-NULL, the function tries to get |
| 5157 | * an optimized label using hda_get_autocfg_input_label(). |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5158 | * |
| 5159 | * This function tries to give a unique label string for the pin as much as |
| 5160 | * possible. For example, when the multiple line-outs are present, it adds |
| 5161 | * the channel suffix like "Front", "Surround", etc (only when @cfg is given). |
| 5162 | * If no unique name with a suffix is available and @indexp is non-NULL, the |
| 5163 | * index number is stored in the pointer. |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5164 | */ |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5165 | int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid, |
| 5166 | const struct auto_pin_cfg *cfg, |
| 5167 | char *label, int maxlen, int *indexp) |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5168 | { |
| 5169 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5170 | const char *name = NULL; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5171 | int i; |
| 5172 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5173 | if (indexp) |
| 5174 | *indexp = 0; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5175 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5176 | return 0; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5177 | |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5178 | switch (get_defcfg_device(def_conf)) { |
| 5179 | case AC_JACK_LINE_OUT: |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5180 | return fill_audio_out_name(codec, nid, cfg, "Line-Out", |
| 5181 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5182 | case AC_JACK_SPEAKER: |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5183 | return fill_audio_out_name(codec, nid, cfg, "Speaker", |
| 5184 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5185 | case AC_JACK_HP_OUT: |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5186 | return fill_audio_out_name(codec, nid, cfg, "Headphone", |
| 5187 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5188 | case AC_JACK_SPDIF_OUT: |
| 5189 | case AC_JACK_DIG_OTHER_OUT: |
| 5190 | if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5191 | name = "HDMI"; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5192 | else |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5193 | name = "SPDIF"; |
| 5194 | if (cfg && indexp) { |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5195 | i = find_idx_in_nid_list(nid, cfg->dig_out_pins, |
| 5196 | cfg->dig_outs); |
| 5197 | if (i >= 0) |
| 5198 | *indexp = i; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5199 | } |
| 5200 | break; |
| 5201 | default: |
| 5202 | if (cfg) { |
| 5203 | for (i = 0; i < cfg->num_inputs; i++) { |
| 5204 | if (cfg->inputs[i].pin != nid) |
| 5205 | continue; |
| 5206 | name = hda_get_autocfg_input_label(codec, cfg, i); |
| 5207 | if (name) |
| 5208 | break; |
| 5209 | } |
| 5210 | } |
| 5211 | if (!name) |
| 5212 | name = hda_get_input_pin_label(codec, nid, true); |
| 5213 | break; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5214 | } |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5215 | if (!name) |
| 5216 | return 0; |
| 5217 | strlcpy(label, name, maxlen); |
| 5218 | return 1; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5219 | } |
| 5220 | EXPORT_SYMBOL_HDA(snd_hda_get_pin_label); |
| 5221 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5222 | /** |
| 5223 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5224 | * |
| 5225 | * When the same label is used already in the existing items, the number |
| 5226 | * suffix is appended to the label. This label index number is stored |
| 5227 | * to type_idx when non-NULL pointer is given. |
| 5228 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5229 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5230 | int index, int *type_idx) |
| 5231 | { |
| 5232 | int i, label_idx = 0; |
| 5233 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5234 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5235 | return -EINVAL; |
| 5236 | } |
| 5237 | for (i = 0; i < imux->num_items; i++) { |
| 5238 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5239 | label_idx++; |
| 5240 | } |
| 5241 | if (type_idx) |
| 5242 | *type_idx = label_idx; |
| 5243 | if (label_idx > 0) |
| 5244 | snprintf(imux->items[imux->num_items].label, |
| 5245 | sizeof(imux->items[imux->num_items].label), |
| 5246 | "%s %d", label, label_idx); |
| 5247 | else |
| 5248 | strlcpy(imux->items[imux->num_items].label, label, |
| 5249 | sizeof(imux->items[imux->num_items].label)); |
| 5250 | imux->items[imux->num_items].index = index; |
| 5251 | imux->num_items++; |
| 5252 | return 0; |
| 5253 | } |
| 5254 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5255 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | #ifdef CONFIG_PM |
| 5258 | /* |
| 5259 | * power management |
| 5260 | */ |
| 5261 | |
| 5262 | /** |
| 5263 | * snd_hda_suspend - suspend the codecs |
| 5264 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | * |
| 5266 | * Returns 0 if successful. |
| 5267 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5268 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5270 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5272 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5273 | if (hda_codec_is_power_on(codec)) |
| 5274 | hda_call_codec_suspend(codec); |
| 5275 | if (codec->patch_ops.post_suspend) |
| 5276 | codec->patch_ops.post_suspend(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | } |
| 5278 | return 0; |
| 5279 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5280 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | |
| 5282 | /** |
| 5283 | * snd_hda_resume - resume the codecs |
| 5284 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | * |
| 5286 | * Returns 0 if successful. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5287 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5288 | * This function is defined only when POWER_SAVE isn't set. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5289 | * In the power-save mode, the codec is resumed dynamically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | */ |
| 5291 | int snd_hda_resume(struct hda_bus *bus) |
| 5292 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5293 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5295 | list_for_each_entry(codec, &bus->codec_list, list) { |
Vitaliy Kulikov | d02667e | 2011-07-22 18:18:15 -0500 | [diff] [blame] | 5296 | if (codec->patch_ops.pre_resume) |
| 5297 | codec->patch_ops.pre_resume(codec); |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 5298 | if (snd_hda_codec_needs_resume(codec)) |
| 5299 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | return 0; |
| 5302 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5303 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5304 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5305 | |
| 5306 | /* |
| 5307 | * generic arrays |
| 5308 | */ |
| 5309 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5310 | /** |
| 5311 | * snd_array_new - get a new element from the given array |
| 5312 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5313 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5314 | * Get a new element from the given array. If it exceeds the |
| 5315 | * pre-allocated array size, re-allocate the array. |
| 5316 | * |
| 5317 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5318 | */ |
| 5319 | void *snd_array_new(struct snd_array *array) |
| 5320 | { |
| 5321 | if (array->used >= array->alloced) { |
| 5322 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5323 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5324 | int oldsize = array->alloced * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5325 | void *nlist; |
| 5326 | if (snd_BUG_ON(num >= 4096)) |
| 5327 | return NULL; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5328 | nlist = krealloc(array->list, size, GFP_KERNEL); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5329 | if (!nlist) |
| 5330 | return NULL; |
Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5331 | memset(nlist + oldsize, 0, size - oldsize); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5332 | array->list = nlist; |
| 5333 | array->alloced = num; |
| 5334 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5335 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5336 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5337 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5338 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5339 | /** |
| 5340 | * snd_array_free - free the given array elements |
| 5341 | * @array: the array object |
| 5342 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5343 | void snd_array_free(struct snd_array *array) |
| 5344 | { |
| 5345 | kfree(array->list); |
| 5346 | array->used = 0; |
| 5347 | array->alloced = 0; |
| 5348 | array->list = NULL; |
| 5349 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5350 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5351 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5352 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5353 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5354 | * @pcm: PCM caps bits |
| 5355 | * @buf: the string buffer to write |
| 5356 | * @buflen: the max buffer length |
| 5357 | * |
| 5358 | * used by hda_proc.c and hda_eld.c |
| 5359 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5360 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5361 | { |
| 5362 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5363 | int i, j; |
| 5364 | |
| 5365 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5366 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5367 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5368 | |
| 5369 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5370 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5371 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5372 | |
| 5373 | MODULE_DESCRIPTION("HDA codec core"); |
| 5374 | MODULE_LICENSE("GPL"); |