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