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 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/pci.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <sound/core.h> |
| 30 | #include "hda_codec.h" |
| 31 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 32 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 34 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 36 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 37 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 38 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 40 | #define CREATE_TRACE_POINTS |
| 41 | #include "hda_trace.h" |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * vendor / preset table |
| 45 | */ |
| 46 | |
| 47 | struct hda_vendor_id { |
| 48 | unsigned int id; |
| 49 | const char *name; |
| 50 | }; |
| 51 | |
| 52 | /* codec vendor labels */ |
| 53 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 54 | { 0x1002, "ATI" }, |
Takashi Iwai | e5f1424 | 2009-07-01 18:11:44 +0200 | [diff] [blame] | 55 | { 0x1013, "Cirrus Logic" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 56 | { 0x1057, "Motorola" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 57 | { 0x1095, "Silicon Image" }, |
Takashi Iwai | 31117b7 | 2008-12-16 14:43:21 +0100 | [diff] [blame] | 58 | { 0x10de, "Nvidia" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 59 | { 0x10ec, "Realtek" }, |
Takashi Iwai | 4e01f54 | 2009-04-16 08:53:34 +0200 | [diff] [blame] | 60 | { 0x1102, "Creative" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 61 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 62 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 63 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 64 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 66 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 67 | { 0x17e8, "Chrontel" }, |
| 68 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 69 | { 0x1aec, "Wolfson Microelectronics" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { 0x434d, "C-Media" }, |
Takashi Iwai | 74c6113 | 2008-12-18 09:11:33 +0100 | [diff] [blame] | 71 | { 0x8086, "Intel" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 72 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | {} /* terminator */ |
| 74 | }; |
| 75 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 76 | static DEFINE_MUTEX(preset_mutex); |
| 77 | static LIST_HEAD(hda_preset_tables); |
| 78 | |
| 79 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 80 | { |
| 81 | mutex_lock(&preset_mutex); |
| 82 | list_add_tail(&preset->list, &hda_preset_tables); |
| 83 | mutex_unlock(&preset_mutex); |
| 84 | return 0; |
| 85 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 86 | EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 87 | |
| 88 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 89 | { |
| 90 | mutex_lock(&preset_mutex); |
| 91 | list_del(&preset->list); |
| 92 | mutex_unlock(&preset_mutex); |
| 93 | return 0; |
| 94 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 95 | EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 98 | static void hda_power_work(struct work_struct *work); |
| 99 | static 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) ((codec)->power_on) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 101 | #else |
| 102 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 103 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 104 | #endif |
| 105 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 106 | /** |
| 107 | * snd_hda_get_jack_location - Give a location string of the jack |
| 108 | * @cfg: pin default config value |
| 109 | * |
| 110 | * Parse the pin default config value and returns the string of the |
| 111 | * jack location, e.g. "Rear", "Front", etc. |
| 112 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 113 | const char *snd_hda_get_jack_location(u32 cfg) |
| 114 | { |
| 115 | static char *bases[7] = { |
| 116 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 117 | }; |
| 118 | static unsigned char specials_idx[] = { |
| 119 | 0x07, 0x08, |
| 120 | 0x17, 0x18, 0x19, |
| 121 | 0x37, 0x38 |
| 122 | }; |
| 123 | static char *specials[] = { |
| 124 | "Rear Panel", "Drive Bar", |
| 125 | "Riser", "HDMI", "ATAPI", |
| 126 | "Mobile-In", "Mobile-Out" |
| 127 | }; |
| 128 | int i; |
| 129 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 130 | if ((cfg & 0x0f) < 7) |
| 131 | return bases[cfg & 0x0f]; |
| 132 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 133 | if (cfg == specials_idx[i]) |
| 134 | return specials[i]; |
| 135 | } |
| 136 | return "UNKNOWN"; |
| 137 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 138 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 139 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 140 | /** |
| 141 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 142 | * @cfg: pin default config value |
| 143 | * |
| 144 | * Parse the pin default config value and returns the string of the |
| 145 | * jack connectivity, i.e. external or internal connection. |
| 146 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 147 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 148 | { |
| 149 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 150 | |
| 151 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 152 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 153 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 154 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 155 | /** |
| 156 | * snd_hda_get_jack_type - Give a type string of the jack |
| 157 | * @cfg: pin default config value |
| 158 | * |
| 159 | * Parse the pin default config value and returns the string of the |
| 160 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 161 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 162 | const char *snd_hda_get_jack_type(u32 cfg) |
| 163 | { |
| 164 | static char *jack_types[16] = { |
| 165 | "Line Out", "Speaker", "HP Out", "CD", |
| 166 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 167 | "Line In", "Aux", "Mic", "Telephony", |
| 168 | "SPDIF In", "Digitial In", "Reserved", "Other" |
| 169 | }; |
| 170 | |
| 171 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 172 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 173 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 174 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 175 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 176 | /* |
| 177 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 178 | */ |
| 179 | static inline unsigned int |
| 180 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 181 | unsigned int verb, unsigned int parm) |
| 182 | { |
| 183 | u32 val; |
| 184 | |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 185 | if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || |
| 186 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 187 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", |
| 188 | codec->addr, direct, nid, verb, parm); |
| 189 | return ~0; |
| 190 | } |
| 191 | |
| 192 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 193 | val |= (u32)direct << 27; |
| 194 | val |= (u32)nid << 20; |
| 195 | val |= verb << 8; |
| 196 | val |= parm; |
| 197 | return val; |
| 198 | } |
| 199 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Send and receive a verb |
| 202 | */ |
| 203 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
| 204 | unsigned int *res) |
| 205 | { |
| 206 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 207 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 208 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 209 | if (cmd == ~0) |
| 210 | return -1; |
| 211 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 212 | if (res) |
| 213 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 214 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 215 | snd_hda_power_up(codec); |
| 216 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 217 | trace_hda_send_cmd(codec, cmd); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 218 | err = bus->ops.command(bus, cmd); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 219 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 220 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 221 | trace_hda_get_response(codec, *res); |
| 222 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 223 | mutex_unlock(&bus->cmd_mutex); |
| 224 | snd_hda_power_down(codec); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 225 | if (res && *res == -1 && bus->rirb_error) { |
| 226 | if (bus->response_reset) { |
| 227 | snd_printd("hda_codec: resetting BUS due to " |
| 228 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 229 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 230 | bus->ops.bus_reset(bus); |
| 231 | } |
| 232 | goto again; |
| 233 | } |
| 234 | /* clear reset-flag when the communication gets recovered */ |
| 235 | if (!err) |
| 236 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | /** |
| 241 | * snd_hda_codec_read - send a command and get the response |
| 242 | * @codec: the HDA codec |
| 243 | * @nid: NID to send the command |
| 244 | * @direct: direct flag |
| 245 | * @verb: the verb to send |
| 246 | * @parm: the parameter for the verb |
| 247 | * |
| 248 | * Send a single command and read the corresponding response. |
| 249 | * |
| 250 | * Returns the obtained response value, or -1 for an error. |
| 251 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 252 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 253 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | unsigned int verb, unsigned int parm) |
| 255 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 256 | unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
| 257 | unsigned int res; |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 258 | if (codec_exec_verb(codec, cmd, &res)) |
| 259 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return res; |
| 261 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 262 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * snd_hda_codec_write - send a single command without waiting for response |
| 266 | * @codec: the HDA codec |
| 267 | * @nid: NID to send the command |
| 268 | * @direct: direct flag |
| 269 | * @verb: the verb to send |
| 270 | * @parm: the parameter for the verb |
| 271 | * |
| 272 | * Send a single command without waiting for response. |
| 273 | * |
| 274 | * Returns 0 if successful, or a negative error code. |
| 275 | */ |
| 276 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 277 | unsigned int verb, unsigned int parm) |
| 278 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 279 | unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 280 | unsigned int res; |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 281 | return codec_exec_verb(codec, cmd, |
| 282 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 284 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * snd_hda_sequence_write - sequence writes |
| 288 | * @codec: the HDA codec |
| 289 | * @seq: VERB array to send |
| 290 | * |
| 291 | * Send the commands sequentially from the given array. |
| 292 | * The array must be terminated with NID=0. |
| 293 | */ |
| 294 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 295 | { |
| 296 | for (; seq->nid; seq++) |
| 297 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 298 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 299 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 303 | * @codec: the HDA codec |
| 304 | * @nid: NID to parse |
| 305 | * @start_id: the pointer to store the start NID |
| 306 | * |
| 307 | * Parse the NID and store the start NID of its sub-nodes. |
| 308 | * Returns the number of sub-nodes. |
| 309 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 310 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 311 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | unsigned int parm; |
| 314 | |
| 315 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 316 | if (parm == -1) |
| 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | *start_id = (parm >> 16) & 0x7fff; |
| 319 | return (int)(parm & 0x7fff); |
| 320 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 321 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 323 | /* look up the cached results */ |
| 324 | static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid) |
| 325 | { |
| 326 | int i, len; |
| 327 | for (i = 0; i < array->used; ) { |
| 328 | hda_nid_t *p = snd_array_elem(array, i); |
| 329 | if (nid == *p) |
| 330 | return p; |
| 331 | len = p[1]; |
| 332 | i += len + 2; |
| 333 | } |
| 334 | return NULL; |
| 335 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 338 | * snd_hda_get_conn_list - get connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | * @codec: the HDA codec |
| 340 | * @nid: NID to parse |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 341 | * @listp: the pointer to store NID list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | * |
| 343 | * Parses the connection list of the given widget and stores the list |
| 344 | * of NIDs. |
| 345 | * |
| 346 | * Returns the number of connections, or a negative error code. |
| 347 | */ |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 348 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 349 | const hda_nid_t **listp) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 350 | { |
| 351 | struct snd_array *array = &codec->conn_lists; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 352 | int len, err; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 353 | hda_nid_t list[HDA_MAX_CONNECTIONS]; |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 354 | hda_nid_t *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 355 | bool added = false; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 356 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 357 | again: |
| 358 | /* if the connection-list is already cached, read it */ |
| 359 | p = lookup_conn_list(array, nid); |
| 360 | if (p) { |
| 361 | if (listp) |
| 362 | *listp = p + 2; |
| 363 | return p[1]; |
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 | if (snd_BUG_ON(added)) |
| 366 | return -EINVAL; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 367 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 368 | /* read the connection and add to the cache */ |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 369 | len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 370 | if (len < 0) |
| 371 | return len; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 372 | err = snd_hda_override_conn_list(codec, nid, len, list); |
| 373 | if (err < 0) |
| 374 | return err; |
| 375 | added = true; |
| 376 | goto again; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 377 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 378 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 379 | |
| 380 | /** |
| 381 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * @codec: the HDA codec |
| 383 | * @nid: NID to parse |
| 384 | * @conn_list: connection list array |
| 385 | * @max_conns: max. number of connections to store |
| 386 | * |
| 387 | * Parses the connection list of the given widget and stores the list |
| 388 | * of NIDs. |
| 389 | * |
| 390 | * Returns the number of connections, or a negative error code. |
| 391 | */ |
| 392 | 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] | 393 | hda_nid_t *conn_list, int max_conns) |
| 394 | { |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 395 | const hda_nid_t *list; |
| 396 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 397 | |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 398 | if (len <= 0) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 399 | return len; |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 400 | if (len > max_conns) { |
| 401 | snd_printk(KERN_ERR "hda_codec: " |
| 402 | "Too many connections %d for NID 0x%x\n", |
| 403 | len, nid); |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
| 407 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 408 | } |
| 409 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 410 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 411 | /** |
| 412 | * snd_hda_get_raw_connections - copy connection list without cache |
| 413 | * @codec: the HDA codec |
| 414 | * @nid: NID to parse |
| 415 | * @conn_list: connection list array |
| 416 | * @max_conns: max. number of connections to store |
| 417 | * |
| 418 | * Like snd_hda_get_connections(), copy the connection list but without |
| 419 | * checking through the connection-list cache. |
| 420 | * Currently called only from hda_proc.c, so not exported. |
| 421 | */ |
| 422 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 423 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
| 425 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 426 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 428 | unsigned int wcaps; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 429 | hda_nid_t prev_nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 431 | if (snd_BUG_ON(!conn_list || max_conns <= 0)) |
| 432 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Takashi Iwai | 1ba7a7c | 2009-07-27 12:56:26 +0200 | [diff] [blame] | 434 | wcaps = get_wcaps(codec, nid); |
| 435 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 436 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 437 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 440 | if (parm & AC_CLIST_LONG) { |
| 441 | /* long form */ |
| 442 | shift = 16; |
| 443 | num_elems = 2; |
| 444 | } else { |
| 445 | /* short form */ |
| 446 | shift = 8; |
| 447 | num_elems = 4; |
| 448 | } |
| 449 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | mask = (1 << (shift-1)) - 1; |
| 451 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 452 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return 0; /* no connection */ |
| 454 | |
| 455 | if (conn_len == 1) { |
| 456 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 457 | parm = snd_hda_codec_read(codec, nid, 0, |
| 458 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 459 | if (parm == -1 && codec->bus->rirb_error) |
| 460 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | conn_list[0] = parm & mask; |
| 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | /* multi connection */ |
| 466 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 467 | prev_nid = 0; |
| 468 | for (i = 0; i < conn_len; i++) { |
| 469 | int range_val; |
| 470 | hda_nid_t val, n; |
| 471 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 472 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 473 | parm = snd_hda_codec_read(codec, nid, 0, |
| 474 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 475 | if (parm == -1 && codec->bus->rirb_error) |
| 476 | return -EIO; |
| 477 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 478 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 479 | val = parm & mask; |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 480 | if (val == 0) { |
| 481 | snd_printk(KERN_WARNING "hda_codec: " |
| 482 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 483 | nid, i, parm); |
| 484 | return 0; |
| 485 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 486 | parm >>= shift; |
| 487 | if (range_val) { |
| 488 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 489 | if (!prev_nid || prev_nid >= val) { |
| 490 | snd_printk(KERN_WARNING "hda_codec: " |
| 491 | "invalid dep_range_val %x:%x\n", |
| 492 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 493 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 495 | for (n = prev_nid + 1; n <= val; n++) { |
| 496 | if (conns >= max_conns) { |
Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 497 | snd_printk(KERN_ERR "hda_codec: " |
| 498 | "Too many connections %d for NID 0x%x\n", |
| 499 | conns, nid); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 500 | return -EINVAL; |
| 501 | } |
| 502 | conn_list[conns++] = n; |
| 503 | } |
| 504 | } else { |
| 505 | if (conns >= max_conns) { |
Takashi Iwai | 5aacc21 | 2010-07-30 10:36:29 +0200 | [diff] [blame] | 506 | snd_printk(KERN_ERR "hda_codec: " |
| 507 | "Too many connections %d for NID 0x%x\n", |
| 508 | conns, nid); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | } |
| 511 | conn_list[conns++] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 513 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | return conns; |
| 516 | } |
| 517 | |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 518 | static bool add_conn_list(struct snd_array *array, hda_nid_t nid) |
| 519 | { |
| 520 | hda_nid_t *p = snd_array_new(array); |
| 521 | if (!p) |
| 522 | return false; |
| 523 | *p = nid; |
| 524 | return true; |
| 525 | } |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 528 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 529 | * @codec: the HDA codec |
| 530 | * @nid: NID to parse |
| 531 | * @len: number of connection list entries |
| 532 | * @list: the list of connection entries |
| 533 | * |
| 534 | * Add or modify the given connection-list to the cache. If the corresponding |
| 535 | * cache already exists, invalidate it and append a new one. |
| 536 | * |
| 537 | * Returns zero or a negative error code. |
| 538 | */ |
| 539 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 540 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 542 | struct snd_array *array = &codec->conn_lists; |
| 543 | hda_nid_t *p; |
| 544 | int i, old_used; |
| 545 | |
| 546 | p = lookup_conn_list(array, nid); |
| 547 | if (p) |
| 548 | *p = -1; /* invalidate the old entry */ |
| 549 | |
| 550 | old_used = array->used; |
| 551 | if (!add_conn_list(array, nid) || !add_conn_list(array, len)) |
| 552 | goto error_add; |
| 553 | for (i = 0; i < len; i++) |
| 554 | if (!add_conn_list(array, list[i])) |
| 555 | goto error_add; |
| 556 | return 0; |
| 557 | |
| 558 | error_add: |
| 559 | array->used = old_used; |
| 560 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 562 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 563 | |
| 564 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 565 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 566 | * @codec: the HDA codec |
| 567 | * @mux: NID containing the list |
| 568 | * @nid: NID to select |
| 569 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 570 | * |
| 571 | * Parses the connection list of the widget @mux and checks whether the |
| 572 | * widget @nid is present. If it is, return the connection index. |
| 573 | * Otherwise it returns -1. |
| 574 | */ |
| 575 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 576 | hda_nid_t nid, int recursive) |
| 577 | { |
| 578 | hda_nid_t conn[HDA_MAX_NUM_INPUTS]; |
| 579 | int i, nums; |
| 580 | |
| 581 | nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn)); |
| 582 | for (i = 0; i < nums; i++) |
| 583 | if (conn[i] == nid) |
| 584 | return i; |
| 585 | if (!recursive) |
| 586 | return -1; |
| 587 | if (recursive > 5) { |
| 588 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 589 | return -1; |
| 590 | } |
| 591 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 592 | for (i = 0; i < nums; i++) { |
| 593 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 594 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 595 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 596 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 597 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 598 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 599 | return -1; |
| 600 | } |
| 601 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 605 | * @bus: the BUS |
| 606 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 607 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 608 | * |
| 609 | * Adds the given event to the queue. The events are processed in |
| 610 | * the workqueue asynchronously. Call this function in the interrupt |
| 611 | * hanlder when RIRB receives an unsolicited event. |
| 612 | * |
| 613 | * Returns 0 if successful, or a negative error code. |
| 614 | */ |
| 615 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 616 | { |
| 617 | struct hda_bus_unsolicited *unsol; |
| 618 | unsigned int wp; |
| 619 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 620 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 621 | unsol = bus->unsol; |
| 622 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | |
| 625 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 626 | unsol->wp = wp; |
| 627 | |
| 628 | wp <<= 1; |
| 629 | unsol->queue[wp] = res; |
| 630 | unsol->queue[wp + 1] = res_ex; |
| 631 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 632 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 636 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 639 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 641 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 643 | struct hda_bus_unsolicited *unsol = |
| 644 | container_of(work, struct hda_bus_unsolicited, work); |
| 645 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | struct hda_codec *codec; |
| 647 | unsigned int rp, caddr, res; |
| 648 | |
| 649 | while (unsol->rp != unsol->wp) { |
| 650 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 651 | unsol->rp = rp; |
| 652 | rp <<= 1; |
| 653 | res = unsol->queue[rp]; |
| 654 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 655 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | continue; |
| 657 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 658 | if (codec && codec->patch_ops.unsol_event) |
| 659 | codec->patch_ops.unsol_event(codec, res); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * initialize unsolicited queue |
| 665 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 666 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | struct hda_bus_unsolicited *unsol; |
| 669 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 670 | if (bus->unsol) /* already initialized */ |
| 671 | return 0; |
| 672 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 673 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 674 | if (!unsol) { |
| 675 | snd_printk(KERN_ERR "hda_codec: " |
| 676 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -ENOMEM; |
| 678 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 679 | INIT_WORK(&unsol->work, process_unsol_events); |
| 680 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | bus->unsol = unsol; |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * destructor |
| 687 | */ |
| 688 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 689 | |
| 690 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 691 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 692 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 694 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 696 | if (bus->workq) |
| 697 | flush_workqueue(bus->workq); |
| 698 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 700 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | snd_hda_codec_free(codec); |
| 702 | } |
| 703 | if (bus->ops.private_free) |
| 704 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 705 | if (bus->workq) |
| 706 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | kfree(bus); |
| 708 | return 0; |
| 709 | } |
| 710 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 711 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
| 713 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 714 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return snd_hda_bus_free(bus); |
| 716 | } |
| 717 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 718 | #ifdef CONFIG_SND_HDA_HWDEP |
| 719 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 720 | { |
| 721 | struct hda_bus *bus = device->device_data; |
| 722 | struct hda_codec *codec; |
| 723 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 724 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 725 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | #else |
| 730 | #define snd_hda_bus_dev_register NULL |
| 731 | #endif |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /** |
| 734 | * snd_hda_bus_new - create a HDA bus |
| 735 | * @card: the card entry |
| 736 | * @temp: the template for hda_bus information |
| 737 | * @busp: the pointer to store the created bus instance |
| 738 | * |
| 739 | * Returns 0 if successful, or a negative error code. |
| 740 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 741 | int /*__devinit*/ snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 742 | const struct hda_bus_template *temp, |
| 743 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
| 745 | struct hda_bus *bus; |
| 746 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 747 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 748 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | .dev_free = snd_hda_bus_dev_free, |
| 750 | }; |
| 751 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 752 | if (snd_BUG_ON(!temp)) |
| 753 | return -EINVAL; |
| 754 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 755 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | if (busp) |
| 758 | *busp = NULL; |
| 759 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 760 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if (bus == NULL) { |
| 762 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 763 | return -ENOMEM; |
| 764 | } |
| 765 | |
| 766 | bus->card = card; |
| 767 | bus->private_data = temp->private_data; |
| 768 | bus->pci = temp->pci; |
| 769 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 770 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | bus->ops = temp->ops; |
| 772 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 773 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 774 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | INIT_LIST_HEAD(&bus->codec_list); |
| 776 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 777 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 778 | "hd-audio%d", card->number); |
| 779 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 780 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 781 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 782 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 783 | kfree(bus); |
| 784 | return -ENOMEM; |
| 785 | } |
| 786 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 787 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 788 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | snd_hda_bus_free(bus); |
| 790 | return err; |
| 791 | } |
| 792 | if (busp) |
| 793 | *busp = bus; |
| 794 | return 0; |
| 795 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 796 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 798 | #ifdef CONFIG_SND_HDA_GENERIC |
| 799 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 800 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 801 | #else |
| 802 | #define is_generic_config(codec) 0 |
| 803 | #endif |
| 804 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 805 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 806 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 807 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 808 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 809 | #endif |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /* |
| 812 | * find a matching codec preset |
| 813 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 814 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 815 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 817 | struct hda_codec_preset_list *tbl; |
| 818 | const struct hda_codec_preset *preset; |
| 819 | int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 821 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 822 | return NULL; /* use the generic parser */ |
| 823 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 824 | again: |
| 825 | mutex_lock(&preset_mutex); |
| 826 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 827 | if (!try_module_get(tbl->owner)) { |
| 828 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 829 | continue; |
| 830 | } |
| 831 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 833 | if (preset->afg && preset->afg != codec->afg) |
| 834 | continue; |
| 835 | if (preset->mfg && preset->mfg != codec->mfg) |
| 836 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 837 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 839 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 840 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 841 | preset->rev == codec->revision_id)) { |
| 842 | mutex_unlock(&preset_mutex); |
| 843 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 847 | module_put(tbl->owner); |
| 848 | } |
| 849 | mutex_unlock(&preset_mutex); |
| 850 | |
| 851 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 852 | char name[32]; |
| 853 | if (!mod_requested) |
| 854 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 855 | codec->vendor_id); |
| 856 | else |
| 857 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 858 | (codec->vendor_id >> 16) & 0xffff); |
| 859 | request_module(name); |
| 860 | mod_requested++; |
| 861 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | return NULL; |
| 864 | } |
| 865 | |
| 866 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 867 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 869 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
| 871 | const struct hda_vendor_id *c; |
| 872 | const char *vendor = NULL; |
| 873 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 874 | char tmp[16]; |
| 875 | |
| 876 | if (codec->vendor_name) |
| 877 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | for (c = hda_vendor_ids; c->id; c++) { |
| 880 | if (c->id == vendor_id) { |
| 881 | vendor = c->name; |
| 882 | break; |
| 883 | } |
| 884 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 885 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | sprintf(tmp, "Generic %04x", vendor_id); |
| 887 | vendor = tmp; |
| 888 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 889 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 890 | if (!codec->vendor_name) |
| 891 | return -ENOMEM; |
| 892 | |
| 893 | get_chip_name: |
| 894 | if (codec->chip_name) |
| 895 | return 0; |
| 896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 898 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 899 | else { |
| 900 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 901 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 902 | } |
| 903 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 904 | return -ENOMEM; |
| 905 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 909 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 911 | static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 913 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | hda_nid_t nid; |
| 915 | |
| 916 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 917 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 918 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 919 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 920 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 921 | case AC_GRP_AUDIO_FUNCTION: |
| 922 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 923 | codec->afg_function_id = function_id & 0xff; |
| 924 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 925 | break; |
| 926 | case AC_GRP_MODEM_FUNCTION: |
| 927 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 928 | codec->mfg_function_id = function_id & 0xff; |
| 929 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 930 | break; |
| 931 | default: |
| 932 | break; |
| 933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 938 | * read widget caps for each widget and store in cache |
| 939 | */ |
| 940 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 941 | { |
| 942 | int i; |
| 943 | hda_nid_t nid; |
| 944 | |
| 945 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 946 | &codec->start_nid); |
| 947 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 948 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 949 | return -ENOMEM; |
| 950 | nid = codec->start_nid; |
| 951 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 952 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 953 | AC_PAR_AUDIO_WIDGET_CAP); |
| 954 | return 0; |
| 955 | } |
| 956 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 957 | /* read all pin default configurations and save codec->init_pins */ |
| 958 | static int read_pin_defaults(struct hda_codec *codec) |
| 959 | { |
| 960 | int i; |
| 961 | hda_nid_t nid = codec->start_nid; |
| 962 | |
| 963 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 964 | struct hda_pincfg *pin; |
| 965 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 966 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 967 | if (wid_type != AC_WID_PIN) |
| 968 | continue; |
| 969 | pin = snd_array_new(&codec->init_pins); |
| 970 | if (!pin) |
| 971 | return -ENOMEM; |
| 972 | pin->nid = nid; |
| 973 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 974 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 975 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 976 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 977 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 978 | } |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | /* look up the given pin config list and return the item matching with NID */ |
| 983 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 984 | struct snd_array *array, |
| 985 | hda_nid_t nid) |
| 986 | { |
| 987 | int i; |
| 988 | for (i = 0; i < array->used; i++) { |
| 989 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 990 | if (pin->nid == nid) |
| 991 | return pin; |
| 992 | } |
| 993 | return NULL; |
| 994 | } |
| 995 | |
| 996 | /* write a config value for the given NID */ |
| 997 | static void set_pincfg(struct hda_codec *codec, hda_nid_t nid, |
| 998 | unsigned int cfg) |
| 999 | { |
| 1000 | int i; |
| 1001 | for (i = 0; i < 4; i++) { |
| 1002 | snd_hda_codec_write(codec, nid, 0, |
| 1003 | AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i, |
| 1004 | cfg & 0xff); |
| 1005 | cfg >>= 8; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | /* set the current pin config value for the given NID. |
| 1010 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1011 | */ |
| 1012 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1013 | hda_nid_t nid, unsigned int cfg) |
| 1014 | { |
| 1015 | struct hda_pincfg *pin; |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1016 | unsigned int oldcfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1017 | |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1018 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1019 | return -EINVAL; |
| 1020 | |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1021 | oldcfg = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1022 | pin = look_up_pincfg(codec, list, nid); |
| 1023 | if (!pin) { |
| 1024 | pin = snd_array_new(list); |
| 1025 | if (!pin) |
| 1026 | return -ENOMEM; |
| 1027 | pin->nid = nid; |
| 1028 | } |
| 1029 | pin->cfg = cfg; |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1030 | |
| 1031 | /* change only when needed; e.g. if the pincfg is already present |
| 1032 | * in user_pins[], don't write it |
| 1033 | */ |
| 1034 | cfg = snd_hda_codec_get_pincfg(codec, nid); |
| 1035 | if (oldcfg != cfg) |
| 1036 | set_pincfg(codec, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1040 | /** |
| 1041 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1042 | * @codec: the HDA codec |
| 1043 | * @nid: NID to set the pin config |
| 1044 | * @cfg: the pin default config value |
| 1045 | * |
| 1046 | * Override a pin default configuration value in the cache. |
| 1047 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1048 | * priority than the real hardware value. |
| 1049 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1050 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1051 | hda_nid_t nid, unsigned int cfg) |
| 1052 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1053 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1054 | } |
| 1055 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1056 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1057 | /** |
| 1058 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1059 | * @codec: the HDA codec |
| 1060 | * @nid: NID to get the pin config |
| 1061 | * |
| 1062 | * Get the current pin config value of the given pin NID. |
| 1063 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1064 | * returns the cached value. |
| 1065 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1066 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1067 | { |
| 1068 | struct hda_pincfg *pin; |
| 1069 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1070 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1071 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1072 | if (pin) |
| 1073 | return pin->cfg; |
| 1074 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1075 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1076 | if (pin) |
| 1077 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1078 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1079 | if (pin) |
| 1080 | return pin->cfg; |
| 1081 | return 0; |
| 1082 | } |
| 1083 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1084 | |
| 1085 | /* restore all current pin configs */ |
| 1086 | static void restore_pincfgs(struct hda_codec *codec) |
| 1087 | { |
| 1088 | int i; |
| 1089 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1090 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1091 | set_pincfg(codec, pin->nid, |
| 1092 | snd_hda_codec_get_pincfg(codec, pin->nid)); |
| 1093 | } |
| 1094 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1095 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1096 | /** |
| 1097 | * snd_hda_shutup_pins - Shut up all pins |
| 1098 | * @codec: the HDA codec |
| 1099 | * |
| 1100 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1101 | * The controls aren't cached so that they can be resumed properly. |
| 1102 | */ |
| 1103 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1104 | { |
| 1105 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1106 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1107 | * the default pin setup at the next load of the driver |
| 1108 | */ |
| 1109 | if (codec->bus->shutdown) |
| 1110 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1111 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1112 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1113 | /* use read here for syncing after issuing each verb */ |
| 1114 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1115 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1116 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1117 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1118 | } |
| 1119 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1120 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1121 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1122 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1123 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1124 | { |
| 1125 | int i; |
| 1126 | if (!codec->pins_shutup) |
| 1127 | return; |
| 1128 | if (codec->bus->shutdown) |
| 1129 | return; |
| 1130 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1131 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1132 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1133 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1134 | pin->ctrl); |
| 1135 | } |
| 1136 | codec->pins_shutup = 0; |
| 1137 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1138 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1139 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1140 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1141 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1142 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1143 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1144 | /* restore the initial pin cfgs and release all pincfg lists */ |
| 1145 | static void restore_init_pincfgs(struct hda_codec *codec) |
| 1146 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1147 | /* first free driver_pins and user_pins, then call restore_pincfg |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1148 | * so that only the values in init_pins are restored |
| 1149 | */ |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1150 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1151 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1152 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1153 | #endif |
| 1154 | restore_pincfgs(codec); |
| 1155 | snd_array_free(&codec->init_pins); |
| 1156 | } |
| 1157 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1158 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1159 | * audio-converter setup caches |
| 1160 | */ |
| 1161 | struct hda_cvt_setup { |
| 1162 | hda_nid_t nid; |
| 1163 | u8 stream_tag; |
| 1164 | u8 channel_id; |
| 1165 | u16 format_id; |
| 1166 | unsigned char active; /* cvt is currently used */ |
| 1167 | unsigned char dirty; /* setups should be cleared */ |
| 1168 | }; |
| 1169 | |
| 1170 | /* get or create a cache entry for the given audio converter NID */ |
| 1171 | static struct hda_cvt_setup * |
| 1172 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1173 | { |
| 1174 | struct hda_cvt_setup *p; |
| 1175 | int i; |
| 1176 | |
| 1177 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1178 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1179 | if (p->nid == nid) |
| 1180 | return p; |
| 1181 | } |
| 1182 | p = snd_array_new(&codec->cvt_setups); |
| 1183 | if (p) |
| 1184 | p->nid = nid; |
| 1185 | return p; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | * codec destructor |
| 1190 | */ |
| 1191 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1192 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1193 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | return; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1195 | restore_init_pincfgs(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1196 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1197 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1198 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1199 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1201 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1202 | snd_array_free(&codec->nids); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1203 | snd_array_free(&codec->conn_lists); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1204 | snd_array_free(&codec->spdif_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1206 | if (codec->patch_ops.free) |
| 1207 | codec->patch_ops.free(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1208 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1209 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1210 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1211 | kfree(codec->vendor_name); |
| 1212 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1213 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1214 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | kfree(codec); |
| 1216 | } |
| 1217 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1218 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 1219 | unsigned int power_state); |
| 1220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | /** |
| 1222 | * snd_hda_codec_new - create a HDA codec |
| 1223 | * @bus: the bus to assign |
| 1224 | * @codec_addr: the codec address |
| 1225 | * @codecp: the pointer to store the generated codec |
| 1226 | * |
| 1227 | * Returns 0 if successful, or a negative error code. |
| 1228 | */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1229 | int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, |
| 1230 | unsigned int codec_addr, |
| 1231 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | { |
| 1233 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1234 | char component[31]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | int err; |
| 1236 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1237 | if (snd_BUG_ON(!bus)) |
| 1238 | return -EINVAL; |
| 1239 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1240 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
| 1242 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1243 | snd_printk(KERN_ERR "hda_codec: " |
| 1244 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return -EBUSY; |
| 1246 | } |
| 1247 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1248 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (codec == NULL) { |
| 1250 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1251 | return -ENOMEM; |
| 1252 | } |
| 1253 | |
| 1254 | codec->bus = bus; |
| 1255 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1256 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1257 | mutex_init(&codec->control_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1258 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1259 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1260 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1261 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1262 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1263 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1264 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 1265 | snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1266 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1267 | if (codec->bus->modelname) { |
| 1268 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1269 | if (!codec->modelname) { |
| 1270 | snd_hda_codec_free(codec); |
| 1271 | return -ENODEV; |
| 1272 | } |
| 1273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1275 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1276 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1277 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1278 | * the caller has to power down appropriatley after initialization |
| 1279 | * phase. |
| 1280 | */ |
| 1281 | hda_keep_power_on(codec); |
| 1282 | #endif |
| 1283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | list_add_tail(&codec->list, &bus->codec_list); |
| 1285 | bus->caddr_tbl[codec_addr] = codec; |
| 1286 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1287 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1288 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1289 | if (codec->vendor_id == -1) |
| 1290 | /* read again, hopefully the access method was corrected |
| 1291 | * in the last read... |
| 1292 | */ |
| 1293 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1294 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1295 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1296 | AC_PAR_SUBSYSTEM_ID); |
| 1297 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1298 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1300 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1301 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1302 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1303 | err = -ENODEV; |
| 1304 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1307 | err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg); |
| 1308 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1309 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1310 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1311 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1312 | err = read_pin_defaults(codec); |
| 1313 | if (err < 0) |
| 1314 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1315 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1316 | if (!codec->subsystem_id) { |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1317 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1318 | codec->subsystem_id = |
| 1319 | snd_hda_codec_read(codec, nid, 0, |
| 1320 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1323 | /* power-up all before initialization */ |
| 1324 | hda_set_power_state(codec, |
| 1325 | codec->afg ? codec->afg : codec->mfg, |
| 1326 | AC_PWRST_D0); |
| 1327 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1328 | snd_hda_codec_proc_new(codec); |
| 1329 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1330 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1331 | |
| 1332 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1333 | codec->subsystem_id, codec->revision_id); |
| 1334 | snd_component_add(codec->bus->card, component); |
| 1335 | |
| 1336 | if (codecp) |
| 1337 | *codecp = codec; |
| 1338 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1339 | |
| 1340 | error: |
| 1341 | snd_hda_codec_free(codec); |
| 1342 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1343 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1344 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1345 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1346 | /** |
| 1347 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1348 | * @codec: the HDA codec |
| 1349 | * |
| 1350 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1351 | * patch instance. |
| 1352 | * |
| 1353 | * Returns 0 if successful or a negative error code. |
| 1354 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1355 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1356 | { |
| 1357 | int err; |
| 1358 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1359 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1360 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1361 | err = get_codec_name(codec); |
| 1362 | if (err < 0) |
| 1363 | return err; |
| 1364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1366 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1368 | goto patched; |
| 1369 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1370 | if (codec->preset && codec->preset->patch) { |
| 1371 | err = codec->preset->patch(codec); |
| 1372 | goto patched; |
| 1373 | } |
| 1374 | |
| 1375 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1376 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 1377 | if (err < 0) |
| 1378 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1379 | |
| 1380 | patched: |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1381 | if (!err && codec->patch_ops.unsol_event) |
| 1382 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1383 | /* audio codec should override the mixer name */ |
| 1384 | if (!err && (codec->afg || !*codec->bus->card->mixername)) |
| 1385 | snprintf(codec->bus->card->mixername, |
| 1386 | sizeof(codec->bus->card->mixername), |
| 1387 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1388 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1390 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | /** |
| 1393 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1394 | * @codec: the CODEC to set up |
| 1395 | * @nid: the NID to set up |
| 1396 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1397 | * @channel_id: channel id to pass, zero based. |
| 1398 | * @format: stream format. |
| 1399 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1400 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1401 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | int channel_id, int format) |
| 1403 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1404 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1405 | struct hda_cvt_setup *p; |
| 1406 | unsigned int oldval, newval; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1407 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1408 | int i; |
| 1409 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1410 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1411 | return; |
| 1412 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1413 | snd_printdd("hda_codec_setup_stream: " |
| 1414 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1416 | p = get_hda_cvt_setup(codec, nid); |
| 1417 | if (!p) |
| 1418 | return; |
| 1419 | /* update the stream-id if changed */ |
| 1420 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1421 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1422 | newval = (stream_tag << 4) | channel_id; |
| 1423 | if (oldval != newval) |
| 1424 | snd_hda_codec_write(codec, nid, 0, |
| 1425 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1426 | newval); |
| 1427 | p->stream_tag = stream_tag; |
| 1428 | p->channel_id = channel_id; |
| 1429 | } |
| 1430 | /* update the format-id if changed */ |
| 1431 | if (p->format_id != format) { |
| 1432 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1433 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1434 | if (oldval != format) { |
| 1435 | msleep(1); |
| 1436 | snd_hda_codec_write(codec, nid, 0, |
| 1437 | AC_VERB_SET_STREAM_FORMAT, |
| 1438 | format); |
| 1439 | } |
| 1440 | p->format_id = format; |
| 1441 | } |
| 1442 | p->active = 1; |
| 1443 | p->dirty = 0; |
| 1444 | |
| 1445 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1446 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1447 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1448 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1449 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1450 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1451 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1452 | p->dirty = 1; |
| 1453 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1456 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1458 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1459 | struct hda_cvt_setup *q); |
| 1460 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1461 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1462 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1463 | * @codec: the CODEC to clean up |
| 1464 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1465 | * @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] | 1466 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1467 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1468 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1469 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1470 | struct hda_cvt_setup *p; |
| 1471 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1472 | if (!nid) |
| 1473 | return; |
| 1474 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1475 | if (codec->no_sticky_stream) |
| 1476 | do_now = 1; |
| 1477 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1478 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1479 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1480 | if (p) { |
| 1481 | /* here we just clear the active flag when do_now isn't set; |
| 1482 | * actual clean-ups will be done later in |
| 1483 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1484 | */ |
| 1485 | if (do_now) |
| 1486 | really_cleanup_stream(codec, p); |
| 1487 | else |
| 1488 | p->active = 0; |
| 1489 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1490 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1491 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1492 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1493 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1494 | struct hda_cvt_setup *q) |
| 1495 | { |
| 1496 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1497 | if (q->stream_tag || q->channel_id) |
| 1498 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1499 | if (q->format_id) |
| 1500 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1501 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1502 | memset(q, 0, sizeof(*q)); |
| 1503 | q->nid = nid; |
| 1504 | } |
| 1505 | |
| 1506 | /* clean up the all conflicting obsolete streams */ |
| 1507 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1508 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1509 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1510 | int i; |
| 1511 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1512 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1513 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1514 | struct hda_cvt_setup *p; |
| 1515 | p = snd_array_elem(&c->cvt_setups, i); |
| 1516 | if (p->dirty) |
| 1517 | really_cleanup_stream(c, p); |
| 1518 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1522 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1523 | /* clean up all streams; called from suspend */ |
| 1524 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1525 | { |
| 1526 | int i; |
| 1527 | |
| 1528 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1529 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1530 | if (p->stream_tag) |
| 1531 | really_cleanup_stream(codec, p); |
| 1532 | } |
| 1533 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1534 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | /* |
| 1537 | * amp access functions |
| 1538 | */ |
| 1539 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1540 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1541 | #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] | 1542 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1543 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1544 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1546 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | |
| 1548 | /* initialize the hash table */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1549 | static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1550 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1552 | memset(cache, 0, sizeof(*cache)); |
| 1553 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1554 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1557 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1558 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1559 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1563 | 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] | 1564 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1565 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1566 | u16 cur = cache->hash[idx]; |
| 1567 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1570 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (info->key == key) |
| 1572 | return info; |
| 1573 | cur = info->next; |
| 1574 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1575 | return NULL; |
| 1576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1578 | /* query the hash. allocate an entry if not found. */ |
| 1579 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1580 | u32 key) |
| 1581 | { |
| 1582 | struct hda_cache_head *info = get_hash(cache, key); |
| 1583 | if (!info) { |
| 1584 | u16 idx, cur; |
| 1585 | /* add a new hash entry */ |
| 1586 | info = snd_array_new(&cache->buf); |
| 1587 | if (!info) |
| 1588 | return NULL; |
| 1589 | cur = snd_array_index(&cache->buf, info); |
| 1590 | info->key = key; |
| 1591 | info->val = 0; |
| 1592 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1593 | info->next = cache->hash[idx]; |
| 1594 | cache->hash[idx] = cur; |
| 1595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | return info; |
| 1597 | } |
| 1598 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1599 | /* query and allocate an amp hash entry */ |
| 1600 | static inline struct hda_amp_info * |
| 1601 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1602 | { |
| 1603 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1604 | } |
| 1605 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1606 | /** |
| 1607 | * query_amp_caps - query AMP capabilities |
| 1608 | * @codec: the HD-auio codec |
| 1609 | * @nid: the NID to query |
| 1610 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1611 | * |
| 1612 | * Query AMP capabilities for the given widget and direction. |
| 1613 | * Returns the obtained capability bits. |
| 1614 | * |
| 1615 | * When cap bits have been already read, this doesn't read again but |
| 1616 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1618 | 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] | 1619 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1620 | struct hda_amp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1622 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
| 1623 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | return 0; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1625 | if (!(info->head.val & INFO_AMP_CAPS)) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1626 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | nid = codec->afg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1628 | info->amp_caps = snd_hda_param_read(codec, nid, |
| 1629 | direction == HDA_OUTPUT ? |
| 1630 | AC_PAR_AMP_OUT_CAP : |
| 1631 | AC_PAR_AMP_IN_CAP); |
Takashi Iwai | b75e53f | 2007-05-10 16:56:09 +0200 | [diff] [blame] | 1632 | if (info->amp_caps) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1633 | info->head.val |= INFO_AMP_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | } |
| 1635 | return info->amp_caps; |
| 1636 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1637 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1639 | /** |
| 1640 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1641 | * @codec: the CODEC to clean up |
| 1642 | * @nid: the NID to clean up |
| 1643 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1644 | * @caps: the capability bits to set |
| 1645 | * |
| 1646 | * Override the cached AMP caps bits value by the given one. |
| 1647 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1648 | * e.g. limit to 0dB, etc. |
| 1649 | * |
| 1650 | * Returns zero if successful or a negative error code. |
| 1651 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1652 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1653 | unsigned int caps) |
| 1654 | { |
| 1655 | struct hda_amp_info *info; |
| 1656 | |
| 1657 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0)); |
| 1658 | if (!info) |
| 1659 | return -EINVAL; |
| 1660 | info->amp_caps = caps; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1661 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1664 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1665 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1666 | static unsigned int |
| 1667 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key, |
| 1668 | unsigned int (*func)(struct hda_codec *, hda_nid_t)) |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1669 | { |
| 1670 | struct hda_amp_info *info; |
| 1671 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1672 | info = get_alloc_amp_hash(codec, key); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1673 | if (!info) |
| 1674 | return 0; |
| 1675 | if (!info->head.val) { |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1676 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1677 | info->amp_caps = func(codec, nid); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1678 | } |
| 1679 | return info->amp_caps; |
| 1680 | } |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1681 | |
| 1682 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid) |
| 1683 | { |
| 1684 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1685 | } |
| 1686 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1687 | /** |
| 1688 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1689 | * @codec: the HD-auio codec |
| 1690 | * @nid: the NID to query |
| 1691 | * |
| 1692 | * Query PIN capabilities for the given widget. |
| 1693 | * Returns the obtained capability bits. |
| 1694 | * |
| 1695 | * When cap bits have been already read, this doesn't read again but |
| 1696 | * returns the cached value. |
| 1697 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1698 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1699 | { |
| 1700 | return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid), |
| 1701 | read_pin_cap); |
| 1702 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1703 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 1704 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1705 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1706 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1707 | * @codec: the CODEC |
| 1708 | * @nid: the NID to override |
| 1709 | * @caps: the capability bits to set |
| 1710 | * |
| 1711 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1712 | * |
| 1713 | * Returns zero if successful or a negative error code. |
| 1714 | */ |
| 1715 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1716 | unsigned int caps) |
| 1717 | { |
| 1718 | struct hda_amp_info *info; |
| 1719 | info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid)); |
| 1720 | if (!info) |
| 1721 | return -ENOMEM; |
| 1722 | info->amp_caps = caps; |
| 1723 | info->head.val |= INFO_AMP_CAPS; |
| 1724 | return 0; |
| 1725 | } |
| 1726 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | /* |
| 1729 | * read the current volume to info |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1730 | * if the cache exists, read the cache value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1732 | static unsigned int get_vol_mute(struct hda_codec *codec, |
| 1733 | struct hda_amp_info *info, hda_nid_t nid, |
| 1734 | int ch, int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | { |
| 1736 | u32 val, parm; |
| 1737 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1738 | if (info->head.val & INFO_AMP_VOL(ch)) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1739 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1742 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1743 | parm |= index; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1744 | val = snd_hda_codec_read(codec, nid, 0, |
| 1745 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | info->vol[ch] = val & 0xff; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1747 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1748 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1752 | * write the current volume in info to the h/w and update the cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | */ |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1754 | static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1755 | hda_nid_t nid, int ch, int direction, int index, |
| 1756 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | { |
| 1758 | u32 parm; |
| 1759 | |
| 1760 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1761 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1762 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1763 | if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) && |
| 1764 | (info->amp_caps & AC_AMPCAP_MIN_MUTE)) |
| 1765 | ; /* set the zero value as a fake mute */ |
| 1766 | else |
| 1767 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | 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] | 1769 | info->vol[ch] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1772 | /** |
| 1773 | * snd_hda_codec_amp_read - Read AMP value |
| 1774 | * @codec: HD-audio codec |
| 1775 | * @nid: NID to read the AMP value |
| 1776 | * @ch: channel (left=0 or right=1) |
| 1777 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1778 | * @index: the index value (only for input direction) |
| 1779 | * |
| 1780 | * 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] | 1781 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1782 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1783 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1785 | struct hda_amp_info *info; |
| 1786 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1787 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1789 | return get_vol_mute(codec, info, nid, ch, direction, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1791 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1793 | /** |
| 1794 | * snd_hda_codec_amp_update - update the AMP value |
| 1795 | * @codec: HD-audio codec |
| 1796 | * @nid: NID to read the AMP value |
| 1797 | * @ch: channel (left=0 or right=1) |
| 1798 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1799 | * @idx: the index value (only for input direction) |
| 1800 | * @mask: bit mask to set |
| 1801 | * @val: the bits value to set |
| 1802 | * |
| 1803 | * Update the AMP value with a bit mask. |
| 1804 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1805 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1806 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1807 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1809 | struct hda_amp_info *info; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1810 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1811 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
| 1812 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1814 | if (snd_BUG_ON(mask & ~0xff)) |
| 1815 | mask &= 0xff; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1816 | val &= mask; |
| 1817 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1818 | if (info->vol[ch] == val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1820 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | return 1; |
| 1822 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1823 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1825 | /** |
| 1826 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1827 | * @codec: HD-audio codec |
| 1828 | * @nid: NID to read the AMP value |
| 1829 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1830 | * @idx: the index value (only for input direction) |
| 1831 | * @mask: bit mask to set |
| 1832 | * @val: the bits value to set |
| 1833 | * |
| 1834 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1835 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1836 | */ |
| 1837 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1838 | int direction, int idx, int mask, int val) |
| 1839 | { |
| 1840 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1841 | |
| 1842 | if (snd_BUG_ON(mask & ~0xff)) |
| 1843 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1844 | for (ch = 0; ch < 2; ch++) |
| 1845 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1846 | idx, mask, val); |
| 1847 | return ret; |
| 1848 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1849 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1850 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1851 | #ifdef CONFIG_PM |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1852 | /** |
| 1853 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1854 | * @codec: HD-audio codec |
| 1855 | * |
| 1856 | * Resume the all amp commands from the cache. |
| 1857 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1858 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1859 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1860 | struct hda_amp_info *buffer = codec->amp_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1861 | int i; |
| 1862 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1863 | for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1864 | u32 key = buffer->head.key; |
| 1865 | hda_nid_t nid; |
| 1866 | unsigned int idx, dir, ch; |
| 1867 | if (!key) |
| 1868 | continue; |
| 1869 | nid = key & 0xff; |
| 1870 | idx = (key >> 16) & 0xff; |
| 1871 | dir = (key >> 24) & 0xff; |
| 1872 | for (ch = 0; ch < 2; ch++) { |
| 1873 | if (!(buffer->head.val & INFO_AMP_VOL(ch))) |
| 1874 | continue; |
| 1875 | put_vol_mute(codec, buffer, nid, ch, dir, idx, |
| 1876 | buffer->vol[ch]); |
| 1877 | } |
| 1878 | } |
| 1879 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1880 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1881 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1883 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1884 | unsigned int ofs) |
| 1885 | { |
| 1886 | u32 caps = query_amp_caps(codec, nid, dir); |
| 1887 | /* get num steps */ |
| 1888 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 1889 | if (ofs < caps) |
| 1890 | caps -= ofs; |
| 1891 | return caps; |
| 1892 | } |
| 1893 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1894 | /** |
| 1895 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 1896 | * |
| 1897 | * The control element is supposed to have the private_value field |
| 1898 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1899 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1900 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 1901 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | { |
| 1903 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1904 | u16 nid = get_amp_nid(kcontrol); |
| 1905 | u8 chs = get_amp_channels(kcontrol); |
| 1906 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1907 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1909 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1910 | uinfo->count = chs == 3 ? 2 : 1; |
| 1911 | uinfo->value.integer.min = 0; |
| 1912 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 1913 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1914 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 1915 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 1916 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | return -EINVAL; |
| 1918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | return 0; |
| 1920 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1921 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1923 | |
| 1924 | static inline unsigned int |
| 1925 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 1926 | int ch, int dir, int idx, unsigned int ofs) |
| 1927 | { |
| 1928 | unsigned int val; |
| 1929 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 1930 | val &= HDA_AMP_VOLMASK; |
| 1931 | if (val >= ofs) |
| 1932 | val -= ofs; |
| 1933 | else |
| 1934 | val = 0; |
| 1935 | return val; |
| 1936 | } |
| 1937 | |
| 1938 | static inline int |
| 1939 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 1940 | int ch, int dir, int idx, unsigned int ofs, |
| 1941 | unsigned int val) |
| 1942 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1943 | unsigned int maxval; |
| 1944 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1945 | if (val > 0) |
| 1946 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 1947 | /* ofs = 0: raw max value */ |
| 1948 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 1949 | if (val > maxval) |
| 1950 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1951 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 1952 | HDA_AMP_VOLMASK, val); |
| 1953 | } |
| 1954 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1955 | /** |
| 1956 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 1957 | * |
| 1958 | * The control element is supposed to have the private_value field |
| 1959 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1960 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1961 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 1962 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | { |
| 1964 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1965 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1966 | int chs = get_amp_channels(kcontrol); |
| 1967 | int dir = get_amp_direction(kcontrol); |
| 1968 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1969 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | long *valp = ucontrol->value.integer.value; |
| 1971 | |
| 1972 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1973 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1975 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | return 0; |
| 1977 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1978 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1980 | /** |
| 1981 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 1982 | * |
| 1983 | * The control element is supposed to have the private_value field |
| 1984 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 1985 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1986 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 1987 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | { |
| 1989 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1990 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1991 | int chs = get_amp_channels(kcontrol); |
| 1992 | int dir = get_amp_direction(kcontrol); |
| 1993 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 1994 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | long *valp = ucontrol->value.integer.value; |
| 1996 | int change = 0; |
| 1997 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1998 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1999 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2000 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2001 | valp++; |
| 2002 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2003 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2004 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2005 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | return change; |
| 2007 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2008 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
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 - TLV 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 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2016 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2017 | unsigned int size, unsigned int __user *_tlv) |
| 2018 | { |
| 2019 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2020 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2021 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2022 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2023 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2024 | u32 caps, val1, val2; |
| 2025 | |
| 2026 | if (size < 4 * sizeof(unsigned int)) |
| 2027 | return -ENOMEM; |
| 2028 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2029 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2030 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2031 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2032 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2033 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2034 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2035 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2036 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2037 | return -EFAULT; |
| 2038 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2039 | return -EFAULT; |
| 2040 | if (put_user(val1, _tlv + 2)) |
| 2041 | return -EFAULT; |
| 2042 | if (put_user(val2, _tlv + 3)) |
| 2043 | return -EFAULT; |
| 2044 | return 0; |
| 2045 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2046 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2047 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2048 | /** |
| 2049 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2050 | * @codec: HD-audio codec |
| 2051 | * @nid: NID of a reference widget |
| 2052 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2053 | * @tlv: TLV data to be stored, at least 4 elements |
| 2054 | * |
| 2055 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2056 | * obtained from the reference NID. |
| 2057 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2058 | */ |
| 2059 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2060 | unsigned int *tlv) |
| 2061 | { |
| 2062 | u32 caps; |
| 2063 | int nums, step; |
| 2064 | |
| 2065 | caps = query_amp_caps(codec, nid, dir); |
| 2066 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2067 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2068 | step = (step + 1) * 25; |
| 2069 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2070 | tlv[1] = 2 * sizeof(unsigned int); |
| 2071 | tlv[2] = -nums * step; |
| 2072 | tlv[3] = step; |
| 2073 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2074 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2077 | static struct snd_kcontrol * |
| 2078 | _snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2079 | const char *name, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2080 | { |
| 2081 | struct snd_ctl_elem_id id; |
| 2082 | memset(&id, 0, sizeof(id)); |
| 2083 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2084 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2085 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2086 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2087 | strcpy(id.name, name); |
| 2088 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2089 | } |
| 2090 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2091 | /** |
| 2092 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2093 | * @codec: HD-audio codec |
| 2094 | * @name: ctl id name string |
| 2095 | * |
| 2096 | * Get the control element with the given id string and IFACE_MIXER. |
| 2097 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2098 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2099 | const char *name) |
| 2100 | { |
| 2101 | return _snd_hda_find_mixer_ctl(codec, name, 0); |
| 2102 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2103 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2104 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2105 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name) |
| 2106 | { |
| 2107 | int idx; |
| 2108 | for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */ |
| 2109 | if (!_snd_hda_find_mixer_ctl(codec, name, idx)) |
| 2110 | return idx; |
| 2111 | } |
| 2112 | return -EBUSY; |
| 2113 | } |
| 2114 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2115 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2116 | * 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] | 2117 | * @codec: HD-audio codec |
| 2118 | * @nid: corresponding NID (optional) |
| 2119 | * @kctl: the control element to assign |
| 2120 | * |
| 2121 | * Add the given control element to an array inside the codec instance. |
| 2122 | * All control elements belonging to a codec are supposed to be added |
| 2123 | * by this function so that a proper clean-up works at the free or |
| 2124 | * reconfiguration time. |
| 2125 | * |
| 2126 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2127 | * The assignment is shown in the codec proc file. |
| 2128 | * |
| 2129 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2130 | * #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] | 2131 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2132 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2133 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2134 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2135 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2136 | { |
| 2137 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2138 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2139 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2140 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2141 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2142 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2143 | if (nid == 0) |
| 2144 | nid = get_amp_nid_(kctl->private_value); |
| 2145 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2146 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2147 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2148 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2149 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2150 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2151 | if (err < 0) |
| 2152 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2153 | item = snd_array_new(&codec->mixers); |
| 2154 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2155 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2156 | item->kctl = kctl; |
| 2157 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2158 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2159 | return 0; |
| 2160 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2161 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2162 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2163 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2164 | * snd_hda_add_nid - Assign a NID to a control element |
| 2165 | * @codec: HD-audio codec |
| 2166 | * @nid: corresponding NID (optional) |
| 2167 | * @kctl: the control element to assign |
| 2168 | * @index: index to kctl |
| 2169 | * |
| 2170 | * Add the given control element to an array inside the codec instance. |
| 2171 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2172 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2173 | */ |
| 2174 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2175 | unsigned int index, hda_nid_t nid) |
| 2176 | { |
| 2177 | struct hda_nid_item *item; |
| 2178 | |
| 2179 | if (nid > 0) { |
| 2180 | item = snd_array_new(&codec->nids); |
| 2181 | if (!item) |
| 2182 | return -ENOMEM; |
| 2183 | item->kctl = kctl; |
| 2184 | item->index = index; |
| 2185 | item->nid = nid; |
| 2186 | return 0; |
| 2187 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2188 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2189 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2190 | return -EINVAL; |
| 2191 | } |
| 2192 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2193 | |
| 2194 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2195 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2196 | * @codec: HD-audio codec |
| 2197 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2198 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2199 | { |
| 2200 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2201 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2202 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2203 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2204 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2205 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2206 | } |
| 2207 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2208 | /* pseudo device locking |
| 2209 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2210 | */ |
| 2211 | static int hda_lock_devices(struct snd_card *card) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2212 | { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2213 | spin_lock(&card->files_lock); |
| 2214 | if (card->shutdown) { |
| 2215 | spin_unlock(&card->files_lock); |
| 2216 | return -EINVAL; |
| 2217 | } |
| 2218 | card->shutdown = 1; |
| 2219 | spin_unlock(&card->files_lock); |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | static void hda_unlock_devices(struct snd_card *card) |
| 2224 | { |
| 2225 | spin_lock(&card->files_lock); |
| 2226 | card->shutdown = 0; |
| 2227 | spin_unlock(&card->files_lock); |
| 2228 | } |
| 2229 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2230 | /** |
| 2231 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2232 | * @codec: HD-audio codec |
| 2233 | * |
| 2234 | * This frees the all PCM and control elements assigned to the codec, and |
| 2235 | * clears the caches and restores the pin default configurations. |
| 2236 | * |
| 2237 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2238 | * returns zero. |
| 2239 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2240 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2241 | { |
| 2242 | struct snd_card *card = codec->bus->card; |
| 2243 | int i, pcm; |
| 2244 | |
| 2245 | if (hda_lock_devices(card) < 0) |
| 2246 | return -EBUSY; |
| 2247 | /* check whether the codec isn't used by any mixer or PCM streams */ |
| 2248 | if (!list_empty(&card->ctl_files)) { |
| 2249 | hda_unlock_devices(card); |
| 2250 | return -EBUSY; |
| 2251 | } |
| 2252 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2253 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2254 | if (!cpcm->pcm) |
| 2255 | continue; |
| 2256 | if (cpcm->pcm->streams[0].substream_opened || |
| 2257 | cpcm->pcm->streams[1].substream_opened) { |
| 2258 | hda_unlock_devices(card); |
| 2259 | return -EBUSY; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | /* OK, let it free */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2264 | |
| 2265 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 2266 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 2267 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2268 | #endif |
| 2269 | snd_hda_ctls_clear(codec); |
| 2270 | /* relase PCMs */ |
| 2271 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2272 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2273 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2274 | clear_bit(codec->pcm_info[i].device, |
| 2275 | codec->bus->pcm_dev_bits); |
| 2276 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2277 | } |
| 2278 | if (codec->patch_ops.free) |
| 2279 | codec->patch_ops.free(codec); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2280 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2281 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2282 | codec->spec = NULL; |
| 2283 | free_hda_cache(&codec->amp_cache); |
| 2284 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2285 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2286 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2287 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2288 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 2289 | restore_pincfgs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2290 | codec->num_pcms = 0; |
| 2291 | codec->pcm_info = NULL; |
| 2292 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2293 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
| 2294 | codec->slave_dig_outs = NULL; |
| 2295 | codec->spdif_status_reset = 0; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2296 | module_put(codec->owner); |
| 2297 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2298 | |
| 2299 | /* allow device access again */ |
| 2300 | hda_unlock_devices(card); |
| 2301 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2302 | } |
| 2303 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2304 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2305 | |
| 2306 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2307 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2308 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2309 | { |
| 2310 | struct hda_nid_item *items; |
| 2311 | const char * const *s; |
| 2312 | int i, err; |
| 2313 | |
| 2314 | items = codec->mixers.list; |
| 2315 | for (i = 0; i < codec->mixers.used; i++) { |
| 2316 | struct snd_kcontrol *sctl = items[i].kctl; |
| 2317 | if (!sctl || !sctl->id.name || |
| 2318 | sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
| 2319 | continue; |
| 2320 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2321 | char tmpname[sizeof(sctl->id.name)]; |
| 2322 | const char *name = *s; |
| 2323 | if (suffix) { |
| 2324 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2325 | name, suffix); |
| 2326 | name = tmpname; |
| 2327 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2328 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2329 | err = func(data, sctl); |
| 2330 | if (err) |
| 2331 | return err; |
| 2332 | break; |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
| 2339 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2340 | { |
| 2341 | return 1; |
| 2342 | } |
| 2343 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2344 | /* guess the value corresponding to 0dB */ |
| 2345 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl) |
| 2346 | { |
| 2347 | int _tlv[4]; |
| 2348 | const int *tlv = NULL; |
| 2349 | int val = -1; |
| 2350 | |
| 2351 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2352 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2353 | mm_segment_t fs = get_fs(); |
| 2354 | set_fs(get_ds()); |
| 2355 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2356 | tlv = _tlv; |
| 2357 | set_fs(fs); |
| 2358 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2359 | tlv = kctl->tlv.p; |
| 2360 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) |
| 2361 | val = -tlv[2] / tlv[3]; |
| 2362 | return val; |
| 2363 | } |
| 2364 | |
| 2365 | /* call kctl->put with the given value(s) */ |
| 2366 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2367 | { |
| 2368 | struct snd_ctl_elem_value *ucontrol; |
| 2369 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2370 | if (!ucontrol) |
| 2371 | return -ENOMEM; |
| 2372 | ucontrol->value.integer.value[0] = val; |
| 2373 | ucontrol->value.integer.value[1] = val; |
| 2374 | kctl->put(kctl, ucontrol); |
| 2375 | kfree(ucontrol); |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
| 2379 | /* initialize the slave volume with 0dB */ |
| 2380 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2381 | { |
| 2382 | int offset = get_kctl_0dB_offset(slave); |
| 2383 | if (offset > 0) |
| 2384 | put_kctl_with_value(slave, offset); |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | /* unmute the slave */ |
| 2389 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2390 | { |
| 2391 | return put_kctl_with_value(slave, 1); |
| 2392 | } |
| 2393 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2394 | /** |
| 2395 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2396 | * @codec: HD-audio codec |
| 2397 | * @name: vmaster control name |
| 2398 | * @tlv: TLV data (optional) |
| 2399 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2400 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2401 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame^] | 2402 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2403 | * |
| 2404 | * Create a virtual master control with the given name. The TLV data |
| 2405 | * must be either NULL or a valid data. |
| 2406 | * |
| 2407 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2408 | * slave control name. All controls with these names are assigned to |
| 2409 | * the new virtual master control. |
| 2410 | * |
| 2411 | * This function returns zero if successful or a negative error code. |
| 2412 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2413 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2414 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame^] | 2415 | const char *suffix, bool init_slave_vol, |
| 2416 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2417 | { |
| 2418 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2419 | int err; |
| 2420 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame^] | 2421 | if (ctl_ret) |
| 2422 | *ctl_ret = NULL; |
| 2423 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2424 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2425 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2426 | snd_printdd("No slave found for %s\n", name); |
| 2427 | return 0; |
| 2428 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2429 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2430 | if (!kctl) |
| 2431 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2432 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2433 | if (err < 0) |
| 2434 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2435 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2436 | err = map_slaves(codec, slaves, suffix, |
| 2437 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2438 | if (err < 0) |
| 2439 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2440 | |
| 2441 | /* init with master mute & zero volume */ |
| 2442 | put_kctl_with_value(kctl, 0); |
| 2443 | if (init_slave_vol) |
| 2444 | map_slaves(codec, slaves, suffix, |
| 2445 | tlv ? init_slave_0dB : init_slave_unmute, kctl); |
| 2446 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame^] | 2447 | if (ctl_ret) |
| 2448 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2449 | return 0; |
| 2450 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2451 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2452 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2453 | /** |
| 2454 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2455 | * |
| 2456 | * The control element is supposed to have the private_value field |
| 2457 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2458 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2459 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2460 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | { |
| 2462 | int chs = get_amp_channels(kcontrol); |
| 2463 | |
| 2464 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2465 | uinfo->count = chs == 3 ? 2 : 1; |
| 2466 | uinfo->value.integer.min = 0; |
| 2467 | uinfo->value.integer.max = 1; |
| 2468 | return 0; |
| 2469 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2470 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2472 | /** |
| 2473 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2474 | * |
| 2475 | * The control element is supposed to have the private_value field |
| 2476 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2477 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2478 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2479 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | { |
| 2481 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2482 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2483 | int chs = get_amp_channels(kcontrol); |
| 2484 | int dir = get_amp_direction(kcontrol); |
| 2485 | int idx = get_amp_index(kcontrol); |
| 2486 | long *valp = ucontrol->value.integer.value; |
| 2487 | |
| 2488 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2489 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2490 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2492 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2493 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | return 0; |
| 2495 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2496 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2498 | /** |
| 2499 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2500 | * |
| 2501 | * The control element is supposed to have the private_value field |
| 2502 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2503 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2504 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2505 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | { |
| 2507 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2508 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2509 | int chs = get_amp_channels(kcontrol); |
| 2510 | int dir = get_amp_direction(kcontrol); |
| 2511 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | long *valp = ucontrol->value.integer.value; |
| 2513 | int change = 0; |
| 2514 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2515 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2516 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2517 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2518 | HDA_AMP_MUTE, |
| 2519 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2520 | valp++; |
| 2521 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2522 | if (chs & 2) |
| 2523 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2524 | HDA_AMP_MUTE, |
| 2525 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2526 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2527 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | return change; |
| 2529 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2530 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
Takashi Iwai | 67d634c | 2009-11-16 15:35:59 +0100 | [diff] [blame] | 2532 | #ifdef CONFIG_SND_HDA_INPUT_BEEP |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2533 | /** |
| 2534 | * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch |
| 2535 | * |
| 2536 | * This function calls snd_hda_enable_beep_device(), which behaves differently |
| 2537 | * depending on beep_mode option. |
| 2538 | */ |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 2539 | int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol, |
| 2540 | struct snd_ctl_elem_value *ucontrol) |
| 2541 | { |
| 2542 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2543 | long *valp = ucontrol->value.integer.value; |
| 2544 | |
| 2545 | snd_hda_enable_beep_device(codec, *valp); |
| 2546 | return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2547 | } |
| 2548 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep); |
Takashi Iwai | 67d634c | 2009-11-16 15:35:59 +0100 | [diff] [blame] | 2549 | #endif /* CONFIG_SND_HDA_INPUT_BEEP */ |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2552 | * bound volume controls |
| 2553 | * |
| 2554 | * bind multiple volumes (# indices, from 0) |
| 2555 | */ |
| 2556 | |
| 2557 | #define AMP_VAL_IDX_SHIFT 19 |
| 2558 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2559 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2560 | /** |
| 2561 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 2562 | * |
| 2563 | * The control element is supposed to have the private_value field |
| 2564 | * set up via HDA_BIND_MUTE*() macros. |
| 2565 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2566 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2567 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2568 | { |
| 2569 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2570 | unsigned long pval; |
| 2571 | int err; |
| 2572 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2573 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2574 | pval = kcontrol->private_value; |
| 2575 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2576 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2577 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2578 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2579 | return err; |
| 2580 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2581 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2582 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2583 | /** |
| 2584 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 2585 | * |
| 2586 | * The control element is supposed to have the private_value field |
| 2587 | * set up via HDA_BIND_MUTE*() macros. |
| 2588 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2589 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2590 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2591 | { |
| 2592 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2593 | unsigned long pval; |
| 2594 | int i, indices, err = 0, change = 0; |
| 2595 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2596 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2597 | pval = kcontrol->private_value; |
| 2598 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2599 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2600 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2601 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2602 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2603 | if (err < 0) |
| 2604 | break; |
| 2605 | change |= err; |
| 2606 | } |
| 2607 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2608 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2609 | return err < 0 ? err : change; |
| 2610 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2611 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2612 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2613 | /** |
| 2614 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 2615 | * |
| 2616 | * The control element is supposed to have the private_value field |
| 2617 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2618 | */ |
| 2619 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2620 | struct snd_ctl_elem_info *uinfo) |
| 2621 | { |
| 2622 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2623 | struct hda_bind_ctls *c; |
| 2624 | int err; |
| 2625 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2626 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2627 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2628 | kcontrol->private_value = *c->values; |
| 2629 | err = c->ops->info(kcontrol, uinfo); |
| 2630 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2631 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2632 | return err; |
| 2633 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2634 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2635 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2636 | /** |
| 2637 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 2638 | * |
| 2639 | * The control element is supposed to have the private_value field |
| 2640 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2641 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2642 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2643 | struct snd_ctl_elem_value *ucontrol) |
| 2644 | { |
| 2645 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2646 | struct hda_bind_ctls *c; |
| 2647 | int err; |
| 2648 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2649 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2650 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2651 | kcontrol->private_value = *c->values; |
| 2652 | err = c->ops->get(kcontrol, ucontrol); |
| 2653 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2654 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2655 | return err; |
| 2656 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2657 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2658 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2659 | /** |
| 2660 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 2661 | * |
| 2662 | * The control element is supposed to have the private_value field |
| 2663 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2664 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2665 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2666 | struct snd_ctl_elem_value *ucontrol) |
| 2667 | { |
| 2668 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2669 | struct hda_bind_ctls *c; |
| 2670 | unsigned long *vals; |
| 2671 | int err = 0, change = 0; |
| 2672 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2673 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2674 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2675 | for (vals = c->values; *vals; vals++) { |
| 2676 | kcontrol->private_value = *vals; |
| 2677 | err = c->ops->put(kcontrol, ucontrol); |
| 2678 | if (err < 0) |
| 2679 | break; |
| 2680 | change |= err; |
| 2681 | } |
| 2682 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2683 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2684 | return err < 0 ? err : change; |
| 2685 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2686 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2687 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2688 | /** |
| 2689 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 2690 | * |
| 2691 | * The control element is supposed to have the private_value field |
| 2692 | * set up via HDA_BIND_VOL() macro. |
| 2693 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2694 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2695 | unsigned int size, unsigned int __user *tlv) |
| 2696 | { |
| 2697 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2698 | struct hda_bind_ctls *c; |
| 2699 | int err; |
| 2700 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2701 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2702 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2703 | kcontrol->private_value = *c->values; |
| 2704 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 2705 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2706 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2707 | return err; |
| 2708 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2709 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2710 | |
| 2711 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 2712 | .info = snd_hda_mixer_amp_volume_info, |
| 2713 | .get = snd_hda_mixer_amp_volume_get, |
| 2714 | .put = snd_hda_mixer_amp_volume_put, |
| 2715 | .tlv = snd_hda_mixer_amp_tlv |
| 2716 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2717 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2718 | |
| 2719 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 2720 | .info = snd_hda_mixer_amp_switch_info, |
| 2721 | .get = snd_hda_mixer_amp_switch_get, |
| 2722 | .put = snd_hda_mixer_amp_switch_put, |
| 2723 | .tlv = snd_hda_mixer_amp_tlv |
| 2724 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2725 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | * SPDIF out controls |
| 2729 | */ |
| 2730 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2731 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 2732 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | { |
| 2734 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 2735 | uinfo->count = 1; |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2739 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 2740 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | { |
| 2742 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 2743 | IEC958_AES0_NONAUDIO | |
| 2744 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 2745 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 2746 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 2747 | IEC958_AES1_CON_ORIGINAL; |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2751 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 2752 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | { |
| 2754 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 2755 | IEC958_AES0_NONAUDIO | |
| 2756 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 2757 | return 0; |
| 2758 | } |
| 2759 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2760 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 2761 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | { |
| 2763 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2764 | int idx = kcontrol->private_value; |
| 2765 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2767 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 2768 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 2769 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 2770 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 2776 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 2777 | */ |
| 2778 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 2779 | { |
| 2780 | unsigned short val = 0; |
| 2781 | |
| 2782 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2783 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2785 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2787 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 2788 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 2789 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2791 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 2792 | IEC958_AES0_CON_EMPHASIS_5015) |
| 2793 | val |= AC_DIG1_EMPHASIS; |
| 2794 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 2795 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2797 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 2799 | } |
| 2800 | return val; |
| 2801 | } |
| 2802 | |
| 2803 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 2804 | */ |
| 2805 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 2806 | { |
| 2807 | unsigned int sbits = 0; |
| 2808 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2809 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2811 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 2813 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2814 | if (sbits & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 2816 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2817 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2819 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2821 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 2823 | sbits |= val & (0x7f << 8); |
| 2824 | } |
| 2825 | return sbits; |
| 2826 | } |
| 2827 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2828 | /* set digital convert verbs both for the given NID and its slaves */ |
| 2829 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 2830 | int verb, int val) |
| 2831 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 2832 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2833 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 2834 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2835 | d = codec->slave_dig_outs; |
| 2836 | if (!d) |
| 2837 | return; |
| 2838 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 2839 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 2843 | int dig1, int dig2) |
| 2844 | { |
| 2845 | if (dig1 != -1) |
| 2846 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 2847 | if (dig2 != -1) |
| 2848 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 2849 | } |
| 2850 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2851 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 2852 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | { |
| 2854 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2855 | int idx = kcontrol->private_value; |
| 2856 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2857 | hda_nid_t nid = spdif->nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | unsigned short val; |
| 2859 | int change; |
| 2860 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2861 | mutex_lock(&codec->spdif_mutex); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2862 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 2864 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 2865 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2866 | val = convert_from_spdif_status(spdif->status); |
| 2867 | val |= spdif->ctls & 1; |
| 2868 | change = spdif->ctls != val; |
| 2869 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2870 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2871 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2872 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | return change; |
| 2874 | } |
| 2875 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 2876 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2878 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 2879 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | { |
| 2881 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2882 | int idx = kcontrol->private_value; |
| 2883 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2885 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | return 0; |
| 2887 | } |
| 2888 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2889 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 2890 | int dig1, int dig2) |
| 2891 | { |
| 2892 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 2893 | /* unmute amp switch (if any) */ |
| 2894 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 2895 | (dig1 & AC_DIG1_ENABLE)) |
| 2896 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 2897 | HDA_AMP_MUTE, 0); |
| 2898 | } |
| 2899 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2900 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 2901 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | { |
| 2903 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2904 | int idx = kcontrol->private_value; |
| 2905 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 2906 | hda_nid_t nid = spdif->nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | unsigned short val; |
| 2908 | int change; |
| 2909 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2910 | mutex_lock(&codec->spdif_mutex); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2911 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2913 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2914 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2915 | spdif->ctls = val; |
| 2916 | if (change && nid != (u16)-1) |
| 2917 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2918 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | return change; |
| 2920 | } |
| 2921 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2922 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | { |
| 2924 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 2925 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2926 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | .info = snd_hda_spdif_mask_info, |
| 2928 | .get = snd_hda_spdif_cmask_get, |
| 2929 | }, |
| 2930 | { |
| 2931 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 2932 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2933 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | .info = snd_hda_spdif_mask_info, |
| 2935 | .get = snd_hda_spdif_pmask_get, |
| 2936 | }, |
| 2937 | { |
| 2938 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2939 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | .info = snd_hda_spdif_mask_info, |
| 2941 | .get = snd_hda_spdif_default_get, |
| 2942 | .put = snd_hda_spdif_default_put, |
| 2943 | }, |
| 2944 | { |
| 2945 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2946 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | .info = snd_hda_spdif_out_switch_info, |
| 2948 | .get = snd_hda_spdif_out_switch_get, |
| 2949 | .put = snd_hda_spdif_out_switch_put, |
| 2950 | }, |
| 2951 | { } /* end */ |
| 2952 | }; |
| 2953 | |
| 2954 | /** |
| 2955 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls |
| 2956 | * @codec: the HDA codec |
| 2957 | * @nid: audio out widget NID |
| 2958 | * |
| 2959 | * Creates controls related with the SPDIF output. |
| 2960 | * Called from each patch supporting the SPDIF out. |
| 2961 | * |
| 2962 | * Returns 0 if successful, or a negative error code. |
| 2963 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2964 | int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, |
| 2965 | hda_nid_t associated_nid, |
| 2966 | hda_nid_t cvt_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | { |
| 2968 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2969 | struct snd_kcontrol *kctl; |
| 2970 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2971 | int idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2972 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2974 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch"); |
| 2975 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2976 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 2977 | return -EBUSY; |
| 2978 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2979 | spdif = snd_array_new(&codec->spdif_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 2981 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 2982 | if (!kctl) |
| 2983 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2984 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2985 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2986 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2987 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | return err; |
| 2989 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 2990 | spdif->nid = cvt_nid; |
| 2991 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2992 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 2993 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | return 0; |
| 2995 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2996 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 2998 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 2999 | hda_nid_t nid) |
| 3000 | { |
| 3001 | int i; |
| 3002 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3003 | struct hda_spdif_out *spdif = |
| 3004 | snd_array_elem(&codec->spdif_out, i); |
| 3005 | if (spdif->nid == nid) |
| 3006 | return spdif; |
| 3007 | } |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3011 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3012 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3013 | { |
| 3014 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3015 | |
| 3016 | mutex_lock(&codec->spdif_mutex); |
| 3017 | spdif->nid = (u16)-1; |
| 3018 | mutex_unlock(&codec->spdif_mutex); |
| 3019 | } |
| 3020 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3021 | |
| 3022 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3023 | { |
| 3024 | struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3025 | unsigned short val; |
| 3026 | |
| 3027 | mutex_lock(&codec->spdif_mutex); |
| 3028 | if (spdif->nid != nid) { |
| 3029 | spdif->nid = nid; |
| 3030 | val = spdif->ctls; |
| 3031 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3032 | } |
| 3033 | mutex_unlock(&codec->spdif_mutex); |
| 3034 | } |
| 3035 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3038 | * SPDIF sharing with analog output |
| 3039 | */ |
| 3040 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3041 | struct snd_ctl_elem_value *ucontrol) |
| 3042 | { |
| 3043 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3044 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
| 3048 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3049 | struct snd_ctl_elem_value *ucontrol) |
| 3050 | { |
| 3051 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3052 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3053 | return 0; |
| 3054 | } |
| 3055 | |
| 3056 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3057 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3058 | .name = "IEC958 Default PCM Playback Switch", |
| 3059 | .info = snd_ctl_boolean_mono_info, |
| 3060 | .get = spdif_share_sw_get, |
| 3061 | .put = spdif_share_sw_put, |
| 3062 | }; |
| 3063 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3064 | /** |
| 3065 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3066 | * @codec: the HDA codec |
| 3067 | * @mout: multi-out instance |
| 3068 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3069 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3070 | struct hda_multi_out *mout) |
| 3071 | { |
| 3072 | if (!mout->dig_out_nid) |
| 3073 | return 0; |
| 3074 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3075 | return snd_hda_ctl_add(codec, mout->dig_out_nid, |
| 3076 | snd_ctl_new1(&spdif_share_sw, mout)); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3077 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3078 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3079 | |
| 3080 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | * SPDIF input |
| 3082 | */ |
| 3083 | |
| 3084 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3085 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3086 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3087 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | { |
| 3089 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3090 | |
| 3091 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3095 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3096 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | { |
| 3098 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3099 | hda_nid_t nid = kcontrol->private_value; |
| 3100 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3101 | int change; |
| 3102 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3103 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3105 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3107 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3108 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3110 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | return change; |
| 3112 | } |
| 3113 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3114 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3115 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | { |
| 3117 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3118 | hda_nid_t nid = kcontrol->private_value; |
| 3119 | unsigned short val; |
| 3120 | unsigned int sbits; |
| 3121 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3122 | 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] | 3123 | sbits = convert_to_spdif_status(val); |
| 3124 | ucontrol->value.iec958.status[0] = sbits; |
| 3125 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3126 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3127 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3131 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | { |
| 3133 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3134 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | .info = snd_hda_spdif_in_switch_info, |
| 3136 | .get = snd_hda_spdif_in_switch_get, |
| 3137 | .put = snd_hda_spdif_in_switch_put, |
| 3138 | }, |
| 3139 | { |
| 3140 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3141 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3142 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | .info = snd_hda_spdif_mask_info, |
| 3144 | .get = snd_hda_spdif_in_status_get, |
| 3145 | }, |
| 3146 | { } /* end */ |
| 3147 | }; |
| 3148 | |
| 3149 | /** |
| 3150 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3151 | * @codec: the HDA codec |
| 3152 | * @nid: audio in widget NID |
| 3153 | * |
| 3154 | * Creates controls related with the SPDIF input. |
| 3155 | * Called from each patch supporting the SPDIF in. |
| 3156 | * |
| 3157 | * Returns 0 if successful, or a negative error code. |
| 3158 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3159 | 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] | 3160 | { |
| 3161 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3162 | struct snd_kcontrol *kctl; |
| 3163 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3164 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3166 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch"); |
| 3167 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3168 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3169 | return -EBUSY; |
| 3170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3172 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3173 | if (!kctl) |
| 3174 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3176 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3177 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | return err; |
| 3179 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3180 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3181 | snd_hda_codec_read(codec, nid, 0, |
| 3182 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3183 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | return 0; |
| 3185 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3186 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3188 | #ifdef CONFIG_PM |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3189 | /* |
| 3190 | * command cache |
| 3191 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3193 | /* build a 32bit cache key with the widget id and the command parameter */ |
| 3194 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3195 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3196 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3197 | |
| 3198 | /** |
| 3199 | * snd_hda_codec_write_cache - send a single command with caching |
| 3200 | * @codec: the HDA codec |
| 3201 | * @nid: NID to send the command |
| 3202 | * @direct: direct flag |
| 3203 | * @verb: the verb to send |
| 3204 | * @parm: the parameter for the verb |
| 3205 | * |
| 3206 | * Send a single command without waiting for response. |
| 3207 | * |
| 3208 | * Returns 0 if successful, or a negative error code. |
| 3209 | */ |
| 3210 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3211 | int direct, unsigned int verb, unsigned int parm) |
| 3212 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3213 | int err = snd_hda_codec_write(codec, nid, direct, verb, parm); |
| 3214 | struct hda_cache_head *c; |
| 3215 | u32 key; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3216 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3217 | if (err < 0) |
| 3218 | return err; |
| 3219 | /* parm may contain the verb stuff for get/set amp */ |
| 3220 | verb = verb | (parm >> 8); |
| 3221 | parm &= 0xff; |
| 3222 | key = build_cmd_cache_key(nid, verb); |
| 3223 | mutex_lock(&codec->bus->cmd_mutex); |
| 3224 | c = get_alloc_hash(&codec->cmd_cache, key); |
| 3225 | if (c) |
| 3226 | c->val = parm; |
| 3227 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3228 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3229 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3230 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3231 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3232 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3233 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3234 | * @codec: the HDA codec |
| 3235 | * @nid: NID to send the command |
| 3236 | * @direct: direct flag |
| 3237 | * @verb: the verb to send |
| 3238 | * @parm: the parameter for the verb |
| 3239 | * |
| 3240 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3241 | * command if the parameter is already identical with the cached value. |
| 3242 | * If not, it sends the command and refreshes the cache. |
| 3243 | * |
| 3244 | * Returns 0 if successful, or a negative error code. |
| 3245 | */ |
| 3246 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3247 | int direct, unsigned int verb, unsigned int parm) |
| 3248 | { |
| 3249 | struct hda_cache_head *c; |
| 3250 | u32 key; |
| 3251 | |
| 3252 | /* parm may contain the verb stuff for get/set amp */ |
| 3253 | verb = verb | (parm >> 8); |
| 3254 | parm &= 0xff; |
| 3255 | key = build_cmd_cache_key(nid, verb); |
| 3256 | mutex_lock(&codec->bus->cmd_mutex); |
| 3257 | c = get_hash(&codec->cmd_cache, key); |
| 3258 | if (c && c->val == parm) { |
| 3259 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3260 | return 0; |
| 3261 | } |
| 3262 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3263 | return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); |
| 3264 | } |
| 3265 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3266 | |
| 3267 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3268 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3269 | * @codec: HD-audio codec |
| 3270 | * |
| 3271 | * Execute all verbs recorded in the command caches to resume. |
| 3272 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3273 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3274 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3275 | struct hda_cache_head *buffer = codec->cmd_cache.buf.list; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3276 | int i; |
| 3277 | |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3278 | for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3279 | u32 key = buffer->key; |
| 3280 | if (!key) |
| 3281 | continue; |
| 3282 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3283 | get_cmd_cache_cmd(key), buffer->val); |
| 3284 | } |
| 3285 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3286 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3287 | |
| 3288 | /** |
| 3289 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3290 | * @codec: the HDA codec |
| 3291 | * @seq: VERB array to send |
| 3292 | * |
| 3293 | * Send the commands sequentially from the given array. |
| 3294 | * Thte commands are recorded on cache for power-save and resume. |
| 3295 | * The array must be terminated with NID=0. |
| 3296 | */ |
| 3297 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3298 | const struct hda_verb *seq) |
| 3299 | { |
| 3300 | for (; seq->nid; seq++) |
| 3301 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3302 | seq->param); |
| 3303 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3304 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3305 | #endif /* CONFIG_PM */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3306 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3307 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
| 3308 | unsigned int power_state, |
| 3309 | bool eapd_workaround) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3310 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3311 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3312 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3313 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3314 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3315 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3316 | if (!(wcaps & AC_WCAP_POWER)) |
| 3317 | continue; |
| 3318 | /* don't power down the widget if it controls eapd and |
| 3319 | * EAPD_BTLENABLE is set. |
| 3320 | */ |
| 3321 | if (eapd_workaround && power_state == AC_PWRST_D3 && |
| 3322 | get_wcaps_type(wcaps) == AC_WID_PIN && |
| 3323 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3324 | int eapd = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3325 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3326 | if (eapd & 0x02) |
| 3327 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3328 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3329 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
| 3330 | power_state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3331 | } |
| 3332 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3333 | if (power_state == AC_PWRST_D0) { |
| 3334 | unsigned long end_time; |
| 3335 | int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3336 | /* wait until the codec reachs to D0 */ |
| 3337 | end_time = jiffies + msecs_to_jiffies(500); |
| 3338 | do { |
| 3339 | state = snd_hda_codec_read(codec, fg, 0, |
| 3340 | AC_VERB_GET_POWER_STATE, 0); |
| 3341 | if (state == power_state) |
| 3342 | break; |
| 3343 | msleep(1); |
| 3344 | } while (time_after_eq(end_time, jiffies)); |
| 3345 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3346 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3347 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3348 | |
| 3349 | /* |
| 3350 | * set power state of the codec |
| 3351 | */ |
| 3352 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 3353 | unsigned int power_state) |
| 3354 | { |
| 3355 | if (codec->patch_ops.set_power_state) { |
| 3356 | codec->patch_ops.set_power_state(codec, fg, power_state); |
| 3357 | return; |
| 3358 | } |
| 3359 | |
| 3360 | /* this delay seems necessary to avoid click noise at power-down */ |
| 3361 | if (power_state == AC_PWRST_D3) |
| 3362 | msleep(100); |
| 3363 | snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, |
| 3364 | power_state); |
| 3365 | snd_hda_codec_set_power_to_all(codec, fg, power_state, true); |
| 3366 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3367 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3368 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3369 | /* execute additional init verbs */ |
| 3370 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3371 | { |
| 3372 | if (codec->init_verbs.list) |
| 3373 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3374 | } |
| 3375 | #else |
| 3376 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3377 | #endif |
| 3378 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3379 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3380 | /* |
| 3381 | * call suspend and power-down; used both from PM and power-save |
| 3382 | */ |
| 3383 | static void hda_call_codec_suspend(struct hda_codec *codec) |
| 3384 | { |
| 3385 | if (codec->patch_ops.suspend) |
| 3386 | codec->patch_ops.suspend(codec, PMSG_SUSPEND); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3387 | hda_cleanup_all_streams(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3388 | hda_set_power_state(codec, |
| 3389 | codec->afg ? codec->afg : codec->mfg, |
| 3390 | AC_PWRST_D3); |
| 3391 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3392 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3393 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 3394 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 3395 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3396 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3397 | #endif |
| 3398 | } |
| 3399 | |
| 3400 | /* |
| 3401 | * kick up codec; used both from PM and power-save |
| 3402 | */ |
| 3403 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3404 | { |
| 3405 | hda_set_power_state(codec, |
| 3406 | codec->afg ? codec->afg : codec->mfg, |
| 3407 | AC_PWRST_D0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 3408 | restore_pincfgs(codec); /* restore all current pin configs */ |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3409 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3410 | hda_exec_init_verbs(codec); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 3411 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3412 | if (codec->patch_ops.resume) |
| 3413 | codec->patch_ops.resume(codec); |
| 3414 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3415 | if (codec->patch_ops.init) |
| 3416 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3417 | snd_hda_codec_resume_amp(codec); |
| 3418 | snd_hda_codec_resume_cache(codec); |
| 3419 | } |
| 3420 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3421 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3422 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | /** |
| 3425 | * snd_hda_build_controls - build mixer controls |
| 3426 | * @bus: the BUS |
| 3427 | * |
| 3428 | * Creates mixer controls for each codec included in the bus. |
| 3429 | * |
| 3430 | * Returns 0 if successful, otherwise a negative error code. |
| 3431 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 3432 | int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3434 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3436 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3437 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3438 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 3439 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3440 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3441 | err = snd_hda_codec_reset(codec); |
| 3442 | if (err < 0) { |
| 3443 | printk(KERN_ERR |
| 3444 | "hda_codec: cannot revert codec\n"); |
| 3445 | return err; |
| 3446 | } |
| 3447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3449 | return 0; |
| 3450 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3451 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3453 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 3454 | { |
| 3455 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3456 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3457 | /* continue to initialize... */ |
| 3458 | if (codec->patch_ops.init) |
| 3459 | err = codec->patch_ops.init(codec); |
| 3460 | if (!err && codec->patch_ops.build_controls) |
| 3461 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3462 | if (err < 0) |
| 3463 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | return 0; |
| 3465 | } |
| 3466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | /* |
| 3468 | * stream formats |
| 3469 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3470 | struct hda_rate_tbl { |
| 3471 | unsigned int hz; |
| 3472 | unsigned int alsa_bits; |
| 3473 | unsigned int hda_fmt; |
| 3474 | }; |
| 3475 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3476 | /* rate = base * mult / div */ |
| 3477 | #define HDA_RATE(base, mult, div) \ |
| 3478 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 3479 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 3480 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3481 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3485 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 3486 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 3487 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 3488 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 3489 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 3490 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 3491 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 3492 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 3493 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 3494 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 3495 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3496 | #define AC_PAR_PCM_RATE_BITS 11 |
| 3497 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 3498 | |
| 3499 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3500 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 3501 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3502 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | }; |
| 3504 | |
| 3505 | /** |
| 3506 | * snd_hda_calc_stream_format - calculate format bitset |
| 3507 | * @rate: the sample rate |
| 3508 | * @channels: the number of channels |
| 3509 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 3510 | * @maxbps: the max. bps |
| 3511 | * |
| 3512 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 3513 | * |
| 3514 | * Return zero if invalid. |
| 3515 | */ |
| 3516 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 3517 | unsigned int channels, |
| 3518 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3519 | unsigned int maxbps, |
| 3520 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | { |
| 3522 | int i; |
| 3523 | unsigned int val = 0; |
| 3524 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3525 | for (i = 0; rate_bits[i].hz; i++) |
| 3526 | if (rate_bits[i].hz == rate) { |
| 3527 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | break; |
| 3529 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3530 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | snd_printdd("invalid rate %d\n", rate); |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
| 3535 | if (channels == 0 || channels > 8) { |
| 3536 | snd_printdd("invalid channels %d\n", channels); |
| 3537 | return 0; |
| 3538 | } |
| 3539 | val |= channels - 1; |
| 3540 | |
| 3541 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3542 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3543 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3544 | break; |
| 3545 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3546 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3547 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | case 20: |
| 3549 | case 24: |
| 3550 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 3551 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3552 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3554 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3556 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | break; |
| 3558 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3559 | snd_printdd("invalid format width %d\n", |
| 3560 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | return 0; |
| 3562 | } |
| 3563 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3564 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 3565 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 3566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | return val; |
| 3568 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3569 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3571 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 3572 | { |
| 3573 | unsigned int val = 0; |
| 3574 | if (nid != codec->afg && |
| 3575 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 3576 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 3577 | if (!val || val == -1) |
| 3578 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 3579 | if (!val || val == -1) |
| 3580 | return 0; |
| 3581 | return val; |
| 3582 | } |
| 3583 | |
| 3584 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 3585 | { |
| 3586 | return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid), |
| 3587 | get_pcm_param); |
| 3588 | } |
| 3589 | |
| 3590 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 3591 | { |
| 3592 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 3593 | if (!streams || streams == -1) |
| 3594 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 3595 | if (!streams || streams == -1) |
| 3596 | return 0; |
| 3597 | return streams; |
| 3598 | } |
| 3599 | |
| 3600 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 3601 | { |
| 3602 | return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid), |
| 3603 | get_stream_param); |
| 3604 | } |
| 3605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | /** |
| 3607 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 3608 | * @codec: the HDA codec |
| 3609 | * @nid: NID to query |
| 3610 | * @ratesp: the pointer to store the detected rate bitflags |
| 3611 | * @formatsp: the pointer to store the detected formats |
| 3612 | * @bpsp: the pointer to store the detected format widths |
| 3613 | * |
| 3614 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 3615 | * or @bsps argument is ignored. |
| 3616 | * |
| 3617 | * Returns 0 if successful, otherwise a negative error code. |
| 3618 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 3619 | 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] | 3620 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 3621 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3622 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3624 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3625 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | |
| 3627 | if (ratesp) { |
| 3628 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3629 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3631 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3633 | if (rates == 0) { |
| 3634 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 3635 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 3636 | nid, val, |
| 3637 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 3638 | return -EIO; |
| 3639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | *ratesp = rates; |
| 3641 | } |
| 3642 | |
| 3643 | if (formatsp || bpsp) { |
| 3644 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3645 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3647 | streams = query_stream_param(codec, nid); |
| 3648 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | |
| 3651 | bps = 0; |
| 3652 | if (streams & AC_SUPFMT_PCM) { |
| 3653 | if (val & AC_SUPPCM_BITS_8) { |
| 3654 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 3655 | bps = 8; |
| 3656 | } |
| 3657 | if (val & AC_SUPPCM_BITS_16) { |
| 3658 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 3659 | bps = 16; |
| 3660 | } |
| 3661 | if (wcaps & AC_WCAP_DIGITAL) { |
| 3662 | if (val & AC_SUPPCM_BITS_32) |
| 3663 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 3664 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 3665 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 3666 | if (val & AC_SUPPCM_BITS_24) |
| 3667 | bps = 24; |
| 3668 | else if (val & AC_SUPPCM_BITS_20) |
| 3669 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3670 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 3671 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 3673 | if (val & AC_SUPPCM_BITS_32) |
| 3674 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | else if (val & AC_SUPPCM_BITS_24) |
| 3676 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 3677 | else if (val & AC_SUPPCM_BITS_20) |
| 3678 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | } |
| 3680 | } |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 3681 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 3683 | if (!bps) |
| 3684 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 3685 | } |
| 3686 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3687 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | /* temporary hack: we have still no proper support |
| 3689 | * for the direct AC3 stream... |
| 3690 | */ |
| 3691 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 3692 | bps = 8; |
| 3693 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3694 | if (formats == 0) { |
| 3695 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 3696 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 3697 | "streams=0x%x)\n", |
| 3698 | nid, val, |
| 3699 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 3700 | streams); |
| 3701 | return -EIO; |
| 3702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | if (formatsp) |
| 3704 | *formatsp = formats; |
| 3705 | if (bpsp) |
| 3706 | *bpsp = bps; |
| 3707 | } |
| 3708 | |
| 3709 | return 0; |
| 3710 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 3711 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | |
| 3713 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3714 | * snd_hda_is_supported_format - Check the validity of the format |
| 3715 | * @codec: HD-audio codec |
| 3716 | * @nid: NID to check |
| 3717 | * @format: the HD-audio format value to check |
| 3718 | * |
| 3719 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | * |
| 3721 | * Returns 1 if supported, 0 if not. |
| 3722 | */ |
| 3723 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 3724 | unsigned int format) |
| 3725 | { |
| 3726 | int i; |
| 3727 | unsigned int val = 0, rate, stream; |
| 3728 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3729 | val = query_pcm_param(codec, nid); |
| 3730 | if (!val) |
| 3731 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | |
| 3733 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3734 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 3735 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | if (val & (1 << i)) |
| 3737 | break; |
| 3738 | return 0; |
| 3739 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 3740 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | return 0; |
| 3742 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 3743 | stream = query_stream_param(codec, nid); |
| 3744 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | return 0; |
| 3746 | |
| 3747 | if (stream & AC_SUPFMT_PCM) { |
| 3748 | switch (format & 0xf0) { |
| 3749 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3750 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return 0; |
| 3752 | break; |
| 3753 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3754 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | return 0; |
| 3756 | break; |
| 3757 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3758 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | return 0; |
| 3760 | break; |
| 3761 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3762 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | return 0; |
| 3764 | break; |
| 3765 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3766 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | return 0; |
| 3768 | break; |
| 3769 | default: |
| 3770 | return 0; |
| 3771 | } |
| 3772 | } else { |
| 3773 | /* FIXME: check for float32 and AC3? */ |
| 3774 | } |
| 3775 | |
| 3776 | return 1; |
| 3777 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3778 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | |
| 3780 | /* |
| 3781 | * PCM stuff |
| 3782 | */ |
| 3783 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 3784 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3785 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | { |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 3791 | struct hda_codec *codec, |
| 3792 | unsigned int stream_tag, |
| 3793 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3794 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | { |
| 3796 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
| 3800 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 3801 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3802 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 3804 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | return 0; |
| 3806 | } |
| 3807 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3808 | static int set_pcm_default_values(struct hda_codec *codec, |
| 3809 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3811 | int err; |
| 3812 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3813 | /* query support PCM information from the given NID */ |
| 3814 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3815 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3816 | info->rates ? NULL : &info->rates, |
| 3817 | info->formats ? NULL : &info->formats, |
| 3818 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 3819 | if (err < 0) |
| 3820 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | } |
| 3822 | if (info->ops.open == NULL) |
| 3823 | info->ops.open = hda_pcm_default_open_close; |
| 3824 | if (info->ops.close == NULL) |
| 3825 | info->ops.close = hda_pcm_default_open_close; |
| 3826 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 3827 | if (snd_BUG_ON(!info->nid)) |
| 3828 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | info->ops.prepare = hda_pcm_default_prepare; |
| 3830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 3832 | if (snd_BUG_ON(!info->nid)) |
| 3833 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 3835 | } |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3839 | /* |
| 3840 | * codec prepare/cleanup entries |
| 3841 | */ |
| 3842 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 3843 | struct hda_pcm_stream *hinfo, |
| 3844 | unsigned int stream, |
| 3845 | unsigned int format, |
| 3846 | struct snd_pcm_substream *substream) |
| 3847 | { |
| 3848 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3849 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3850 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 3851 | if (ret >= 0) |
| 3852 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3853 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3854 | return ret; |
| 3855 | } |
| 3856 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 3857 | |
| 3858 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 3859 | struct hda_pcm_stream *hinfo, |
| 3860 | struct snd_pcm_substream *substream) |
| 3861 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3862 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3863 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 3864 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3865 | } |
| 3866 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 3867 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3868 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 3869 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 3870 | "Audio", "SPDIF", "HDMI", "Modem" |
| 3871 | }; |
| 3872 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3873 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3874 | * get the empty PCM device number to assign |
Takashi Iwai | c893622 | 2010-01-28 17:08:53 +0100 | [diff] [blame] | 3875 | * |
| 3876 | * 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] | 3877 | */ |
| 3878 | static int get_empty_pcm_device(struct hda_bus *bus, int type) |
| 3879 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3880 | /* audio device indices; not linear to keep compatibility */ |
| 3881 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 3882 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 3883 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 3884 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3885 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3886 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3887 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3888 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3889 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3890 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 3891 | return -EINVAL; |
| 3892 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3893 | |
| 3894 | for (i = 0; audio_idx[type][i] >= 0 ; i++) |
| 3895 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 3896 | return audio_idx[type][i]; |
| 3897 | |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 3898 | /* non-fixed slots starting from 10 */ |
| 3899 | for (i = 10; i < 32; i++) { |
| 3900 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 3901 | return i; |
| 3902 | } |
| 3903 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3904 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 3905 | snd_hda_pcm_type_name[type]); |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 3906 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3910 | * attach a new PCM stream |
| 3911 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3912 | 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] | 3913 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3914 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3915 | struct hda_pcm_stream *info; |
| 3916 | int stream, err; |
| 3917 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3918 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3919 | return -EINVAL; |
| 3920 | for (stream = 0; stream < 2; stream++) { |
| 3921 | info = &pcm->stream[stream]; |
| 3922 | if (info->substreams) { |
| 3923 | err = set_pcm_default_values(codec, info); |
| 3924 | if (err < 0) |
| 3925 | return err; |
| 3926 | } |
| 3927 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3928 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 3929 | } |
| 3930 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3931 | /* assign all PCMs of the given codec */ |
| 3932 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 3933 | { |
| 3934 | unsigned int pcm; |
| 3935 | int err; |
| 3936 | |
| 3937 | if (!codec->num_pcms) { |
| 3938 | if (!codec->patch_ops.build_pcms) |
| 3939 | return 0; |
| 3940 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3941 | if (err < 0) { |
| 3942 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3943 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3944 | err = snd_hda_codec_reset(codec); |
| 3945 | if (err < 0) { |
| 3946 | printk(KERN_ERR |
| 3947 | "hda_codec: cannot revert codec\n"); |
| 3948 | return err; |
| 3949 | } |
| 3950 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3951 | } |
| 3952 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 3953 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 3954 | int dev; |
| 3955 | |
| 3956 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 3957 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3958 | |
| 3959 | if (!cpcm->pcm) { |
| 3960 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 3961 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3962 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3963 | cpcm->device = dev; |
| 3964 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 3965 | if (err < 0) { |
| 3966 | printk(KERN_ERR "hda_codec: cannot attach " |
| 3967 | "PCM stream %d for codec #%d\n", |
| 3968 | dev, codec->addr); |
| 3969 | continue; /* no fatal error */ |
| 3970 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 3971 | } |
| 3972 | } |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | /** |
| 3977 | * snd_hda_build_pcms - build PCM information |
| 3978 | * @bus: the BUS |
| 3979 | * |
| 3980 | * Create PCM information for each codec included in the bus. |
| 3981 | * |
| 3982 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 3983 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 3984 | * to create its PCM instances. |
| 3985 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 3986 | * callback. |
| 3987 | * |
| 3988 | * At least, substreams, channels_min and channels_max must be filled for |
| 3989 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 3990 | * When rates and/or formats are zero, the supported values are queried |
| 3991 | * from the given nid. The nid is used also by the default ops.prepare |
| 3992 | * and ops.cleanup callbacks. |
| 3993 | * |
| 3994 | * The driver needs to call ops.open in its open callback. Similarly, |
| 3995 | * ops.close is supposed to be called in the close callback. |
| 3996 | * ops.prepare should be called in the prepare or hw_params callback |
| 3997 | * with the proper parameters for set up. |
| 3998 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 3999 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4000 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4002 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4004 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4006 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4007 | int err = snd_hda_codec_build_pcms(codec); |
| 4008 | if (err < 0) |
| 4009 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | } |
| 4011 | return 0; |
| 4012 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4013 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | /** |
| 4016 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4017 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4018 | * @num_configs: number of config enums |
| 4019 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | * @tbl: configuration table, terminated by null entries |
| 4021 | * |
| 4022 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4023 | * given configuration table. If a matching entry is found, returns its |
| 4024 | * config value (supposed to be 0 or positive). |
| 4025 | * |
| 4026 | * If no entries are matching, the function returns a negative value. |
| 4027 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4028 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4029 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4030 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4032 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4033 | int i; |
| 4034 | for (i = 0; i < num_configs; i++) { |
| 4035 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4036 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4037 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4038 | "selected\n", models[i]); |
| 4039 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | } |
| 4043 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4044 | if (!codec->bus->pci || !tbl) |
| 4045 | return -1; |
| 4046 | |
| 4047 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4048 | if (!tbl) |
| 4049 | return -1; |
| 4050 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4051 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4052 | char tmp[10]; |
| 4053 | const char *model = NULL; |
| 4054 | if (models) |
| 4055 | model = models[tbl->value]; |
| 4056 | if (!model) { |
| 4057 | sprintf(tmp, "#%d", tbl->value); |
| 4058 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4060 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4061 | "for config %x:%x (%s)\n", |
| 4062 | model, tbl->subvendor, tbl->subdevice, |
| 4063 | (tbl->name ? tbl->name : "Unknown device")); |
| 4064 | #endif |
| 4065 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | } |
| 4067 | return -1; |
| 4068 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4069 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | |
| 4071 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4072 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4073 | subsystem ID with the |
| 4074 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4075 | |
| 4076 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4077 | where the vendor ID of the PCI device is: |
| 4078 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4079 | and the vendor/subvendor are found only at the codec. |
| 4080 | |
| 4081 | * @codec: the HDA codec |
| 4082 | * @num_configs: number of config enums |
| 4083 | * @models: array of model name strings |
| 4084 | * @tbl: configuration table, terminated by null entries |
| 4085 | * |
| 4086 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4087 | * given configuration table. If a matching entry is found, returns its |
| 4088 | * config value (supposed to be 0 or positive). |
| 4089 | * |
| 4090 | * If no entries are matching, the function returns a negative value. |
| 4091 | */ |
| 4092 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4093 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4094 | const struct snd_pci_quirk *tbl) |
| 4095 | { |
| 4096 | const struct snd_pci_quirk *q; |
| 4097 | |
| 4098 | /* Search for codec ID */ |
| 4099 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4100 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4101 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4102 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4103 | break; |
| 4104 | } |
| 4105 | |
| 4106 | if (!q->subvendor) |
| 4107 | return -1; |
| 4108 | |
| 4109 | tbl = q; |
| 4110 | |
| 4111 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4112 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4113 | char tmp[10]; |
| 4114 | const char *model = NULL; |
| 4115 | if (models) |
| 4116 | model = models[tbl->value]; |
| 4117 | if (!model) { |
| 4118 | sprintf(tmp, "#%d", tbl->value); |
| 4119 | model = tmp; |
| 4120 | } |
| 4121 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4122 | "for config %x:%x (%s)\n", |
| 4123 | model, tbl->subvendor, tbl->subdevice, |
| 4124 | (tbl->name ? tbl->name : "Unknown device")); |
| 4125 | #endif |
| 4126 | return tbl->value; |
| 4127 | } |
| 4128 | return -1; |
| 4129 | } |
| 4130 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4131 | |
| 4132 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | * snd_hda_add_new_ctls - create controls from the array |
| 4134 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4135 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | * |
| 4137 | * This helper function creates and add new controls in the given array. |
| 4138 | * The array must be terminated with an empty entry as terminator. |
| 4139 | * |
| 4140 | * Returns 0 if successful, or a negative error code. |
| 4141 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4142 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4143 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4145 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | |
| 4147 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4148 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4149 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4150 | if (knew->iface == -1) /* skip this codec private value */ |
| 4151 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4152 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4153 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4154 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4155 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4156 | if (addr > 0) |
| 4157 | kctl->id.device = addr; |
| 4158 | if (idx > 0) |
| 4159 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4160 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4161 | if (!err) |
| 4162 | break; |
| 4163 | /* try first with another device index corresponding to |
| 4164 | * the codec addr; if it still fails (or it's the |
| 4165 | * primary codec), then try another control index |
| 4166 | */ |
| 4167 | if (!addr && codec->addr) |
| 4168 | addr = codec->addr; |
| 4169 | else if (!idx && !knew->index) { |
| 4170 | idx = find_empty_mixer_ctl_idx(codec, |
| 4171 | knew->name); |
| 4172 | if (idx <= 0) |
| 4173 | return err; |
| 4174 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4175 | return err; |
| 4176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } |
| 4178 | return 0; |
| 4179 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4180 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4182 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4183 | static void hda_power_work(struct work_struct *work) |
| 4184 | { |
| 4185 | struct hda_codec *codec = |
| 4186 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4187 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4188 | |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4189 | if (!codec->power_on || codec->power_count) { |
| 4190 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4191 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4192 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4193 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4194 | trace_hda_power_down(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4195 | hda_call_codec_suspend(codec); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4196 | if (bus->ops.pm_notify) |
| 4197 | bus->ops.pm_notify(bus); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4201 | { |
| 4202 | codec->power_count++; |
| 4203 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4204 | codec->power_jiffies = jiffies; |
| 4205 | } |
| 4206 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4207 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4208 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4209 | { |
| 4210 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4211 | if (codec->power_on) |
| 4212 | codec->power_on_acct += delta; |
| 4213 | else |
| 4214 | codec->power_off_acct += delta; |
| 4215 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4216 | } |
| 4217 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4218 | /** |
| 4219 | * snd_hda_power_up - Power-up the codec |
| 4220 | * @codec: HD-audio codec |
| 4221 | * |
| 4222 | * Increment the power-up counter and power up the hardware really when |
| 4223 | * not turned on yet. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4224 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4225 | void snd_hda_power_up(struct hda_codec *codec) |
| 4226 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4227 | struct hda_bus *bus = codec->bus; |
| 4228 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4229 | codec->power_count++; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4230 | if (codec->power_on || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4231 | return; |
| 4232 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4233 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4234 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4235 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4236 | codec->power_jiffies = jiffies; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4237 | if (bus->ops.pm_notify) |
| 4238 | bus->ops.pm_notify(bus); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4239 | hda_call_codec_resume(codec); |
| 4240 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4241 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4242 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4243 | EXPORT_SYMBOL_HDA(snd_hda_power_up); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 4244 | |
| 4245 | #define power_save(codec) \ |
| 4246 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4247 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4248 | /** |
| 4249 | * snd_hda_power_down - Power-down the codec |
| 4250 | * @codec: HD-audio codec |
| 4251 | * |
| 4252 | * Decrement the power-up counter and schedules the power-off work if |
| 4253 | * the counter rearches to zero. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4254 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4255 | void snd_hda_power_down(struct hda_codec *codec) |
| 4256 | { |
| 4257 | --codec->power_count; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4258 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4259 | return; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4260 | if (power_save(codec)) { |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4261 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 4262 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4263 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4264 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4265 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4266 | EXPORT_SYMBOL_HDA(snd_hda_power_down); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4267 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4268 | /** |
| 4269 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4270 | * @codec: HD-audio codec |
| 4271 | * @check: the object containing an AMP list and the status |
| 4272 | * @nid: NID to check / update |
| 4273 | * |
| 4274 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4275 | * check the current AMP status, and update the the power-status according |
| 4276 | * to the mute status. |
| 4277 | * |
| 4278 | * This function is supposed to be set or called from the check_power_status |
| 4279 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4280 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4281 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4282 | struct hda_loopback_check *check, |
| 4283 | hda_nid_t nid) |
| 4284 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4285 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4286 | int ch, v; |
| 4287 | |
| 4288 | if (!check->amplist) |
| 4289 | return 0; |
| 4290 | for (p = check->amplist; p->nid; p++) { |
| 4291 | if (p->nid == nid) |
| 4292 | break; |
| 4293 | } |
| 4294 | if (!p->nid) |
| 4295 | return 0; /* nothing changed */ |
| 4296 | |
| 4297 | for (p = check->amplist; p->nid; p++) { |
| 4298 | for (ch = 0; ch < 2; ch++) { |
| 4299 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4300 | p->idx); |
| 4301 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4302 | if (!check->power_on) { |
| 4303 | check->power_on = 1; |
| 4304 | snd_hda_power_up(codec); |
| 4305 | } |
| 4306 | return 1; |
| 4307 | } |
| 4308 | } |
| 4309 | } |
| 4310 | if (check->power_on) { |
| 4311 | check->power_on = 0; |
| 4312 | snd_hda_power_down(codec); |
| 4313 | } |
| 4314 | return 0; |
| 4315 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4316 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4317 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4319 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4320 | * Channel mode helper |
| 4321 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4322 | |
| 4323 | /** |
| 4324 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 4325 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4326 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4327 | struct snd_ctl_elem_info *uinfo, |
| 4328 | const struct hda_channel_mode *chmode, |
| 4329 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4330 | { |
| 4331 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4332 | uinfo->count = 1; |
| 4333 | uinfo->value.enumerated.items = num_chmodes; |
| 4334 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4335 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4336 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4337 | chmode[uinfo->value.enumerated.item].channels); |
| 4338 | return 0; |
| 4339 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4340 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4341 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4342 | /** |
| 4343 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 4344 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4345 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4346 | struct snd_ctl_elem_value *ucontrol, |
| 4347 | const struct hda_channel_mode *chmode, |
| 4348 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4349 | int max_channels) |
| 4350 | { |
| 4351 | int i; |
| 4352 | |
| 4353 | for (i = 0; i < num_chmodes; i++) { |
| 4354 | if (max_channels == chmode[i].channels) { |
| 4355 | ucontrol->value.enumerated.item[0] = i; |
| 4356 | break; |
| 4357 | } |
| 4358 | } |
| 4359 | return 0; |
| 4360 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4361 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4362 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4363 | /** |
| 4364 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 4365 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4366 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 4367 | struct snd_ctl_elem_value *ucontrol, |
| 4368 | const struct hda_channel_mode *chmode, |
| 4369 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4370 | int *max_channelsp) |
| 4371 | { |
| 4372 | unsigned int mode; |
| 4373 | |
| 4374 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 4375 | if (mode >= num_chmodes) |
| 4376 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4377 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4378 | return 0; |
| 4379 | /* change the current channel setting */ |
| 4380 | *max_channelsp = chmode[mode].channels; |
| 4381 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4382 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4383 | return 1; |
| 4384 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4385 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | /* |
| 4388 | * input MUX helper |
| 4389 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4390 | |
| 4391 | /** |
| 4392 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 4393 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4394 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4395 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | { |
| 4397 | unsigned int index; |
| 4398 | |
| 4399 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4400 | uinfo->count = 1; |
| 4401 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4402 | if (!imux->num_items) |
| 4403 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | index = uinfo->value.enumerated.item; |
| 4405 | if (index >= imux->num_items) |
| 4406 | index = imux->num_items - 1; |
| 4407 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4408 | return 0; |
| 4409 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4410 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4412 | /** |
| 4413 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 4414 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4415 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4416 | const struct hda_input_mux *imux, |
| 4417 | struct snd_ctl_elem_value *ucontrol, |
| 4418 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | unsigned int *cur_val) |
| 4420 | { |
| 4421 | unsigned int idx; |
| 4422 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4423 | if (!imux->num_items) |
| 4424 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | idx = ucontrol->value.enumerated.item[0]; |
| 4426 | if (idx >= imux->num_items) |
| 4427 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4428 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4430 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4431 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | *cur_val = idx; |
| 4433 | return 1; |
| 4434 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4435 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
| 4437 | |
| 4438 | /* |
| 4439 | * Multi-channel / digital-out PCM helper functions |
| 4440 | */ |
| 4441 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4442 | /* setup SPDIF output stream */ |
| 4443 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 4444 | unsigned int stream_tag, unsigned int format) |
| 4445 | { |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4446 | struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 4447 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4448 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4449 | if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4450 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4451 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4452 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4453 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4454 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4455 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4456 | for (d = codec->slave_dig_outs; *d; d++) |
| 4457 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 4458 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4459 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4460 | /* turn on again (if needed) */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4461 | if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4462 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4463 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4464 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4465 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4466 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 4467 | { |
| 4468 | snd_hda_codec_cleanup_stream(codec, nid); |
| 4469 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4470 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4471 | for (d = codec->slave_dig_outs; *d; d++) |
| 4472 | snd_hda_codec_cleanup_stream(codec, *d); |
| 4473 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4474 | } |
| 4475 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4476 | /** |
| 4477 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 4478 | * @bus: HD-audio bus |
| 4479 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4480 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 4481 | { |
| 4482 | struct hda_codec *codec; |
| 4483 | |
| 4484 | if (!bus) |
| 4485 | return; |
| 4486 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 4487 | if (hda_codec_is_power_on(codec) && |
| 4488 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4489 | codec->patch_ops.reboot_notify(codec); |
| 4490 | } |
| 4491 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 4492 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4493 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4494 | /** |
| 4495 | * 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] | 4496 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4497 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 4498 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4500 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 4501 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 4502 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4503 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4505 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | return 0; |
| 4507 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4508 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4510 | /** |
| 4511 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 4512 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4513 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 4514 | struct hda_multi_out *mout, |
| 4515 | unsigned int stream_tag, |
| 4516 | unsigned int format, |
| 4517 | struct snd_pcm_substream *substream) |
| 4518 | { |
| 4519 | mutex_lock(&codec->spdif_mutex); |
| 4520 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 4521 | mutex_unlock(&codec->spdif_mutex); |
| 4522 | return 0; |
| 4523 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4524 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4525 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4526 | /** |
| 4527 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 4528 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 4529 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 4530 | struct hda_multi_out *mout) |
| 4531 | { |
| 4532 | mutex_lock(&codec->spdif_mutex); |
| 4533 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 4534 | mutex_unlock(&codec->spdif_mutex); |
| 4535 | return 0; |
| 4536 | } |
| 4537 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 4538 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4539 | /** |
| 4540 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4542 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 4543 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4545 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4547 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4550 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4552 | /** |
| 4553 | * snd_hda_multi_out_analog_open - open analog outputs |
| 4554 | * |
| 4555 | * Open analog outputs and set up the hw-constraints. |
| 4556 | * If the digital outputs can be opened as slave, open the digital |
| 4557 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4559 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 4560 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4561 | struct snd_pcm_substream *substream, |
| 4562 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4564 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 4565 | runtime->hw.channels_max = mout->max_channels; |
| 4566 | if (mout->dig_out_nid) { |
| 4567 | if (!mout->analog_rates) { |
| 4568 | mout->analog_rates = hinfo->rates; |
| 4569 | mout->analog_formats = hinfo->formats; |
| 4570 | mout->analog_maxbps = hinfo->maxbps; |
| 4571 | } else { |
| 4572 | runtime->hw.rates = mout->analog_rates; |
| 4573 | runtime->hw.formats = mout->analog_formats; |
| 4574 | hinfo->maxbps = mout->analog_maxbps; |
| 4575 | } |
| 4576 | if (!mout->spdif_rates) { |
| 4577 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 4578 | &mout->spdif_rates, |
| 4579 | &mout->spdif_formats, |
| 4580 | &mout->spdif_maxbps); |
| 4581 | } |
| 4582 | mutex_lock(&codec->spdif_mutex); |
| 4583 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 4584 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 4585 | (runtime->hw.formats & mout->spdif_formats)) { |
| 4586 | runtime->hw.rates &= mout->spdif_rates; |
| 4587 | runtime->hw.formats &= mout->spdif_formats; |
| 4588 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 4589 | hinfo->maxbps = mout->spdif_maxbps; |
| 4590 | } else { |
| 4591 | mout->share_spdif = 0; |
| 4592 | /* FIXME: need notify? */ |
| 4593 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4594 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 4595 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 4598 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 4599 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4600 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4602 | /** |
| 4603 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 4604 | * |
| 4605 | * Set up the i/o for analog out. |
| 4606 | * 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] | 4607 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4608 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 4609 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | unsigned int stream_tag, |
| 4611 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4612 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4614 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | int chs = substream->runtime->channels; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4616 | struct hda_spdif_out *spdif = |
| 4617 | snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | int i; |
| 4619 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4620 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4621 | if (mout->dig_out_nid && mout->share_spdif && |
| 4622 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4624 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 4625 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4626 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4628 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 4629 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | } else { |
| 4631 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4632 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | } |
| 4634 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4635 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | |
| 4637 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4638 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 4639 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4640 | if (!mout->no_share_stream && |
| 4641 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4643 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 4644 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4645 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 4646 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 4647 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 4648 | snd_hda_codec_setup_stream(codec, |
| 4649 | mout->hp_out_nid[i], |
| 4650 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4651 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4652 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4653 | snd_hda_codec_setup_stream(codec, |
| 4654 | mout->extra_out_nid[i], |
| 4655 | stream_tag, 0, format); |
| 4656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | /* surrounds */ |
| 4658 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 4659 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4660 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 4661 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 4662 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4663 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 4664 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | } |
| 4666 | return 0; |
| 4667 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4668 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4670 | /** |
| 4671 | * 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] | 4672 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4673 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 4674 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4676 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | int i; |
| 4678 | |
| 4679 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4680 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4682 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 4683 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 4684 | if (mout->hp_out_nid[i]) |
| 4685 | snd_hda_codec_cleanup_stream(codec, |
| 4686 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4687 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 4688 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4689 | snd_hda_codec_cleanup_stream(codec, |
| 4690 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4691 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | 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] | 4693 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | mout->dig_out_used = 0; |
| 4695 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4696 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | return 0; |
| 4698 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4699 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4701 | /* |
Wu Fengguang | 6b34500 | 2008-10-07 14:21:41 +0800 | [diff] [blame] | 4702 | * Helper for automatic pin configuration |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4703 | */ |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 4704 | |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4705 | 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] | 4706 | { |
| 4707 | for (; *list; list++) |
| 4708 | if (*list == nid) |
| 4709 | return 1; |
| 4710 | return 0; |
| 4711 | } |
| 4712 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4713 | |
| 4714 | /* |
| 4715 | * Sort an associated group of pins according to their sequence numbers. |
| 4716 | */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4717 | static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences, |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4718 | int num_pins) |
| 4719 | { |
| 4720 | int i, j; |
| 4721 | short seq; |
| 4722 | hda_nid_t nid; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4723 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4724 | for (i = 0; i < num_pins; i++) { |
| 4725 | for (j = i + 1; j < num_pins; j++) { |
| 4726 | if (sequences[i] > sequences[j]) { |
| 4727 | seq = sequences[i]; |
| 4728 | sequences[i] = sequences[j]; |
| 4729 | sequences[j] = seq; |
| 4730 | nid = pins[i]; |
| 4731 | pins[i] = pins[j]; |
| 4732 | pins[j] = nid; |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4739 | /* add the found input-pin to the cfg->inputs[] table */ |
| 4740 | static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid, |
| 4741 | int type) |
| 4742 | { |
| 4743 | if (cfg->num_inputs < AUTO_CFG_MAX_INS) { |
| 4744 | cfg->inputs[cfg->num_inputs].pin = nid; |
| 4745 | cfg->inputs[cfg->num_inputs].type = type; |
| 4746 | cfg->num_inputs++; |
| 4747 | } |
| 4748 | } |
| 4749 | |
Takashi Iwai | 4a4d4a6 | 2010-09-09 22:22:02 +0200 | [diff] [blame] | 4750 | /* sort inputs in the order of AUTO_PIN_* type */ |
| 4751 | static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg) |
| 4752 | { |
| 4753 | int i, j; |
| 4754 | |
| 4755 | for (i = 0; i < cfg->num_inputs; i++) { |
| 4756 | for (j = i + 1; j < cfg->num_inputs; j++) { |
| 4757 | if (cfg->inputs[i].type > cfg->inputs[j].type) { |
| 4758 | struct auto_pin_cfg_item tmp; |
| 4759 | tmp = cfg->inputs[i]; |
| 4760 | cfg->inputs[i] = cfg->inputs[j]; |
| 4761 | cfg->inputs[j] = tmp; |
| 4762 | } |
| 4763 | } |
| 4764 | } |
| 4765 | } |
| 4766 | |
Takashi Iwai | 8fa7ab4 | 2011-10-26 16:06:27 +0200 | [diff] [blame] | 4767 | /* Reorder the surround channels |
| 4768 | * ALSA sequence is front/surr/clfe/side |
| 4769 | * HDA sequence is: |
| 4770 | * 4-ch: front/surr => OK as it is |
| 4771 | * 6-ch: front/clfe/surr |
| 4772 | * 8-ch: front/clfe/rear/side|fc |
| 4773 | */ |
| 4774 | static void reorder_outputs(unsigned int nums, hda_nid_t *pins) |
| 4775 | { |
| 4776 | hda_nid_t nid; |
| 4777 | |
| 4778 | switch (nums) { |
| 4779 | case 3: |
| 4780 | case 4: |
| 4781 | nid = pins[1]; |
| 4782 | pins[1] = pins[2]; |
| 4783 | pins[2] = nid; |
| 4784 | break; |
| 4785 | } |
| 4786 | } |
| 4787 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4788 | /* |
| 4789 | * Parse all pin widgets and store the useful pin nids to cfg |
| 4790 | * |
| 4791 | * The number of line-outs or any primary output is stored in line_outs, |
| 4792 | * and the corresponding output pins are assigned to line_out_pins[], |
| 4793 | * in the order of front, rear, CLFE, side, ... |
| 4794 | * |
| 4795 | * If more extra outputs (speaker and headphone) are found, the pins are |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4796 | * 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] | 4797 | * is detected, one of speaker of HP pins is assigned as the primary |
| 4798 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 4799 | * if any analog output exists. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4800 | * |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4801 | * The analog input pins are assigned to inputs array. |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4802 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 4803 | * respectively. |
| 4804 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4805 | int snd_hda_parse_pin_defcfg(struct hda_codec *codec, |
| 4806 | struct auto_pin_cfg *cfg, |
| 4807 | const hda_nid_t *ignore_nids, |
| 4808 | unsigned int cond_flags) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4809 | { |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 4810 | hda_nid_t nid, end_nid; |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4811 | short seq, assoc_line_out; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4812 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 4813 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4814 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4815 | int i; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4816 | |
| 4817 | memset(cfg, 0, sizeof(*cfg)); |
| 4818 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4819 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 4820 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4821 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4822 | assoc_line_out = 0; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4823 | |
Takashi Iwai | 2f451d2 | 2011-11-10 12:36:46 +0100 | [diff] [blame] | 4824 | codec->ignore_misc_bit = true; |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 4825 | end_nid = codec->start_nid + codec->num_nodes; |
| 4826 | for (nid = codec->start_nid; nid < end_nid; nid++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4827 | unsigned int wid_caps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 4828 | unsigned int wid_type = get_wcaps_type(wid_caps); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4829 | unsigned int def_conf; |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4830 | short assoc, loc, conn, dev; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4831 | |
| 4832 | /* read all default configuration for pin complex */ |
| 4833 | if (wid_type != AC_WID_PIN) |
| 4834 | continue; |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 4835 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 4836 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 4837 | continue; |
| 4838 | |
Takashi Iwai | c17a1ab | 2009-02-23 09:28:12 +0100 | [diff] [blame] | 4839 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 2f451d2 | 2011-11-10 12:36:46 +0100 | [diff] [blame] | 4840 | if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
| 4841 | AC_DEFCFG_MISC_NO_PRESENCE)) |
| 4842 | codec->ignore_misc_bit = false; |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4843 | conn = get_defcfg_connect(def_conf); |
| 4844 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4845 | continue; |
| 4846 | loc = get_defcfg_location(def_conf); |
Takashi Iwai | 1af7c5f | 2011-06-24 10:43:03 +0200 | [diff] [blame] | 4847 | dev = get_defcfg_device(def_conf); |
| 4848 | |
| 4849 | /* workaround for buggy BIOS setups */ |
| 4850 | if (dev == AC_JACK_LINE_OUT) { |
| 4851 | if (conn == AC_JACK_PORT_FIXED) |
| 4852 | dev = AC_JACK_SPEAKER; |
| 4853 | } |
| 4854 | |
| 4855 | switch (dev) { |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4856 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4857 | seq = get_defcfg_sequence(def_conf); |
| 4858 | assoc = get_defcfg_association(def_conf); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 4859 | |
| 4860 | if (!(wid_caps & AC_WCAP_STEREO)) |
| 4861 | if (!cfg->mono_out_pin) |
| 4862 | cfg->mono_out_pin = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4863 | if (!assoc) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4864 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4865 | if (!assoc_line_out) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4866 | assoc_line_out = assoc; |
| 4867 | else if (assoc_line_out != assoc) |
| 4868 | continue; |
| 4869 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 4870 | continue; |
| 4871 | cfg->line_out_pins[cfg->line_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4872 | sequences_line_out[cfg->line_outs] = seq; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4873 | cfg->line_outs++; |
| 4874 | break; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 4875 | case AC_JACK_SPEAKER: |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4876 | seq = get_defcfg_sequence(def_conf); |
| 4877 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4878 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 4879 | continue; |
| 4880 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
Takashi Iwai | 965f1b2 | 2011-08-19 09:10:29 +0200 | [diff] [blame] | 4881 | sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4882 | cfg->speaker_outs++; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 4883 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4884 | case AC_JACK_HP_OUT: |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4885 | seq = get_defcfg_sequence(def_conf); |
| 4886 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4887 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 4888 | continue; |
| 4889 | cfg->hp_pins[cfg->hp_outs] = nid; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4890 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 4891 | cfg->hp_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4892 | break; |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4893 | case AC_JACK_MIC_IN: |
| 4894 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4895 | break; |
Takashi Iwai | 86e2959 | 2010-09-09 14:50:17 +0200 | [diff] [blame] | 4896 | case AC_JACK_LINE_IN: |
| 4897 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4898 | break; |
| 4899 | case AC_JACK_CD: |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4900 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4901 | break; |
| 4902 | case AC_JACK_AUX: |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 4903 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4904 | break; |
| 4905 | case AC_JACK_SPDIF_OUT: |
Takashi Iwai | 1b52ae7 | 2009-01-20 17:17:29 +0100 | [diff] [blame] | 4906 | case AC_JACK_DIG_OTHER_OUT: |
Takashi Iwai | 0852d7a | 2009-02-11 11:35:15 +0100 | [diff] [blame] | 4907 | if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) |
| 4908 | continue; |
| 4909 | cfg->dig_out_pins[cfg->dig_outs] = nid; |
| 4910 | cfg->dig_out_type[cfg->dig_outs] = |
| 4911 | (loc == AC_JACK_LOC_HDMI) ? |
| 4912 | HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF; |
| 4913 | cfg->dig_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4914 | break; |
| 4915 | case AC_JACK_SPDIF_IN: |
Takashi Iwai | 1b52ae7 | 2009-01-20 17:17:29 +0100 | [diff] [blame] | 4916 | case AC_JACK_DIG_OTHER_IN: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4917 | cfg->dig_in_pin = nid; |
Takashi Iwai | 2297bd6 | 2009-01-20 18:24:13 +0100 | [diff] [blame] | 4918 | if (loc == AC_JACK_LOC_HDMI) |
| 4919 | cfg->dig_in_type = HDA_PCM_TYPE_HDMI; |
| 4920 | else |
| 4921 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4922 | break; |
| 4923 | } |
| 4924 | } |
| 4925 | |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4926 | /* FIX-UP: |
| 4927 | * If no line-out is defined but multiple HPs are found, |
| 4928 | * some of them might be the real line-outs. |
| 4929 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4930 | if (!cfg->line_outs && cfg->hp_outs > 1 && |
| 4931 | !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) { |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4932 | int i = 0; |
| 4933 | while (i < cfg->hp_outs) { |
| 4934 | /* The real HPs should have the sequence 0x0f */ |
| 4935 | if ((sequences_hp[i] & 0x0f) == 0x0f) { |
| 4936 | i++; |
| 4937 | continue; |
| 4938 | } |
| 4939 | /* Move it to the line-out table */ |
| 4940 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; |
| 4941 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; |
| 4942 | cfg->line_outs++; |
| 4943 | cfg->hp_outs--; |
| 4944 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, |
| 4945 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); |
Takashi Iwai | 122661b | 2010-09-08 14:57:04 +0200 | [diff] [blame] | 4946 | memmove(sequences_hp + i, sequences_hp + i + 1, |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4947 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); |
| 4948 | } |
Takashi Iwai | 03642c9 | 2010-09-08 15:28:19 +0200 | [diff] [blame] | 4949 | memset(cfg->hp_pins + cfg->hp_outs, 0, |
| 4950 | sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs)); |
Takashi Iwai | b2d0576 | 2011-01-10 14:47:35 +0100 | [diff] [blame] | 4951 | if (!cfg->hp_outs) |
| 4952 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 4953 | |
Takashi Iwai | 5832fcf8 | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4956 | /* sort by sequence */ |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4957 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 4958 | cfg->line_outs); |
| 4959 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 4960 | cfg->speaker_outs); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 4961 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
| 4962 | cfg->hp_outs); |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4963 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4964 | /* |
| 4965 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 4966 | * as a primary output |
| 4967 | */ |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 4968 | if (!cfg->line_outs && |
| 4969 | !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) { |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 4970 | if (cfg->speaker_outs) { |
| 4971 | cfg->line_outs = cfg->speaker_outs; |
| 4972 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 4973 | sizeof(cfg->speaker_pins)); |
| 4974 | cfg->speaker_outs = 0; |
| 4975 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 4976 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 4977 | } else if (cfg->hp_outs) { |
| 4978 | cfg->line_outs = cfg->hp_outs; |
| 4979 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 4980 | sizeof(cfg->hp_pins)); |
| 4981 | cfg->hp_outs = 0; |
| 4982 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 4983 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 4984 | } |
| 4985 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4986 | |
Takashi Iwai | 8fa7ab4 | 2011-10-26 16:06:27 +0200 | [diff] [blame] | 4987 | reorder_outputs(cfg->line_outs, cfg->line_out_pins); |
| 4988 | reorder_outputs(cfg->hp_outs, cfg->hp_pins); |
| 4989 | reorder_outputs(cfg->speaker_outs, cfg->speaker_pins); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 4990 | |
Takashi Iwai | 4a4d4a6 | 2010-09-09 22:22:02 +0200 | [diff] [blame] | 4991 | sort_autocfg_input_pins(cfg); |
| 4992 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 4993 | /* |
| 4994 | * debug prints of the parsed results |
| 4995 | */ |
Takashi Iwai | c2de187 | 2011-04-29 13:00:40 +0200 | [diff] [blame] | 4996 | 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] | 4997 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 4998 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
Takashi Iwai | c2de187 | 2011-04-29 13:00:40 +0200 | [diff] [blame] | 4999 | cfg->line_out_pins[4], |
| 5000 | cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" : |
| 5001 | (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ? |
| 5002 | "speaker" : "line")); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5003 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 5004 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 5005 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 5006 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 5007 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 5008 | cfg->hp_outs, cfg->hp_pins[0], |
| 5009 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 5010 | cfg->hp_pins[3], cfg->hp_pins[4]); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 5011 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
Takashi Iwai | 0852d7a | 2009-02-11 11:35:15 +0100 | [diff] [blame] | 5012 | if (cfg->dig_outs) |
| 5013 | snd_printd(" dig-out=0x%x/0x%x\n", |
| 5014 | cfg->dig_out_pins[0], cfg->dig_out_pins[1]); |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 5015 | snd_printd(" inputs:"); |
| 5016 | for (i = 0; i < cfg->num_inputs; i++) { |
Takashi Iwai | 0b62673 | 2011-05-26 14:10:44 +0200 | [diff] [blame] | 5017 | snd_printd(" %s=0x%x", |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5018 | hda_get_autocfg_input_label(codec, cfg, i), |
Takashi Iwai | 75e0eb2 | 2010-08-30 12:56:55 +0200 | [diff] [blame] | 5019 | cfg->inputs[i].pin); |
| 5020 | } |
| 5021 | snd_printd("\n"); |
Takashi Iwai | 32d2c7f | 2009-02-11 11:33:13 +0100 | [diff] [blame] | 5022 | if (cfg->dig_in_pin) |
Takashi Iwai | 89ce9e8 | 2009-01-20 17:15:57 +0100 | [diff] [blame] | 5023 | snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5024 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 5025 | return 0; |
| 5026 | } |
Takashi Iwai | d025feb | 2011-08-23 15:24:39 +0200 | [diff] [blame] | 5027 | EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 5028 | |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5029 | int snd_hda_get_input_pin_attr(unsigned int def_conf) |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5030 | { |
| 5031 | unsigned int loc = get_defcfg_location(def_conf); |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 5032 | unsigned int conn = get_defcfg_connect(def_conf); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5033 | if (conn == AC_JACK_PORT_NONE) |
| 5034 | return INPUT_PIN_ATTR_UNUSED; |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 5035 | /* Windows may claim the internal mic to be BOTH, too */ |
| 5036 | if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5037 | return INPUT_PIN_ATTR_INT; |
Takashi Iwai | 41c89ef | 2010-09-17 10:26:37 +0200 | [diff] [blame] | 5038 | if ((loc & 0x30) == AC_JACK_LOC_INTERNAL) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5039 | return INPUT_PIN_ATTR_INT; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5040 | if ((loc & 0x30) == AC_JACK_LOC_SEPARATE) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5041 | return INPUT_PIN_ATTR_DOCK; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5042 | if (loc == AC_JACK_LOC_REAR) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5043 | return INPUT_PIN_ATTR_REAR; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5044 | if (loc == AC_JACK_LOC_FRONT) |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5045 | return INPUT_PIN_ATTR_FRONT; |
| 5046 | return INPUT_PIN_ATTR_NORMAL; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5047 | } |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5048 | EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr); |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5049 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5050 | /** |
| 5051 | * hda_get_input_pin_label - Give a label for the given input pin |
| 5052 | * |
| 5053 | * When check_location is true, the function checks the pin location |
| 5054 | * for mic and line-in pins, and set an appropriate prefix like "Front", |
| 5055 | * "Rear", "Internal". |
| 5056 | */ |
| 5057 | |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5058 | static const char *hda_get_input_pin_label(struct hda_codec *codec, |
| 5059 | hda_nid_t pin, bool check_location) |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5060 | { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5061 | unsigned int def_conf; |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 5062 | static const char * const mic_names[] = { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5063 | "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic", |
| 5064 | }; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5065 | int attr; |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5066 | |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5067 | def_conf = snd_hda_codec_get_pincfg(codec, pin); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5068 | |
| 5069 | switch (get_defcfg_device(def_conf)) { |
| 5070 | case AC_JACK_MIC_IN: |
| 5071 | if (!check_location) |
| 5072 | return "Mic"; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5073 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 5074 | if (!attr) |
| 5075 | return "None"; |
| 5076 | return mic_names[attr - 1]; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5077 | case AC_JACK_LINE_IN: |
| 5078 | if (!check_location) |
| 5079 | return "Line"; |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5080 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 5081 | if (!attr) |
| 5082 | return "None"; |
| 5083 | if (attr == INPUT_PIN_ATTR_DOCK) |
| 5084 | return "Dock Line"; |
| 5085 | return "Line"; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5086 | case AC_JACK_AUX: |
| 5087 | return "Aux"; |
| 5088 | case AC_JACK_CD: |
| 5089 | return "CD"; |
| 5090 | case AC_JACK_SPDIF_IN: |
| 5091 | return "SPDIF In"; |
| 5092 | case AC_JACK_DIG_OTHER_IN: |
| 5093 | return "Digital In"; |
| 5094 | default: |
| 5095 | return "Misc"; |
| 5096 | } |
| 5097 | } |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5098 | |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5099 | /* Check whether the location prefix needs to be added to the label. |
| 5100 | * If all mic-jacks are in the same location (e.g. rear panel), we don't |
| 5101 | * have to put "Front" prefix to each label. In such a case, returns false. |
| 5102 | */ |
| 5103 | static int check_mic_location_need(struct hda_codec *codec, |
| 5104 | const struct auto_pin_cfg *cfg, |
| 5105 | int input) |
| 5106 | { |
| 5107 | unsigned int defc; |
| 5108 | int i, attr, attr2; |
| 5109 | |
| 5110 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5111 | attr = snd_hda_get_input_pin_attr(defc); |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5112 | /* for internal or docking mics, we need locations */ |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5113 | if (attr <= INPUT_PIN_ATTR_NORMAL) |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5114 | return 1; |
| 5115 | |
| 5116 | attr = 0; |
| 5117 | for (i = 0; i < cfg->num_inputs; i++) { |
| 5118 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin); |
Takashi Iwai | 99ae28b | 2010-09-17 14:42:34 +0200 | [diff] [blame] | 5119 | attr2 = snd_hda_get_input_pin_attr(defc); |
| 5120 | if (attr2 >= INPUT_PIN_ATTR_NORMAL) { |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5121 | if (attr && attr != attr2) |
| 5122 | return 1; /* different locations found */ |
| 5123 | attr = attr2; |
| 5124 | } |
| 5125 | } |
| 5126 | return 0; |
| 5127 | } |
| 5128 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5129 | /** |
| 5130 | * hda_get_autocfg_input_label - Get a label for the given input |
| 5131 | * |
| 5132 | * Get a label for the given input pin defined by the autocfg item. |
| 5133 | * Unlike hda_get_input_pin_label(), this function checks all inputs |
| 5134 | * defined in autocfg and avoids the redundant mic/line prefix as much as |
| 5135 | * possible. |
| 5136 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5137 | const char *hda_get_autocfg_input_label(struct hda_codec *codec, |
| 5138 | const struct auto_pin_cfg *cfg, |
| 5139 | int input) |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5140 | { |
| 5141 | int type = cfg->inputs[input].type; |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5142 | int has_multiple_pins = 0; |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5143 | |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5144 | if ((input > 0 && cfg->inputs[input - 1].type == type) || |
| 5145 | (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type)) |
| 5146 | has_multiple_pins = 1; |
Takashi Iwai | a1c9851 | 2010-09-09 21:36:27 +0200 | [diff] [blame] | 5147 | if (has_multiple_pins && type == AUTO_PIN_MIC) |
| 5148 | has_multiple_pins &= check_mic_location_need(codec, cfg, input); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5149 | return hda_get_input_pin_label(codec, cfg->inputs[input].pin, |
| 5150 | has_multiple_pins); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5151 | } |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5152 | EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label); |
| 5153 | |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5154 | /* return the position of NID in the list, or -1 if not found */ |
| 5155 | static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) |
| 5156 | { |
| 5157 | int i; |
| 5158 | for (i = 0; i < nums; i++) |
| 5159 | if (list[i] == nid) |
| 5160 | return i; |
| 5161 | return -1; |
| 5162 | } |
| 5163 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5164 | /* get a unique suffix or an index number */ |
| 5165 | static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins, |
| 5166 | int num_pins, int *indexp) |
| 5167 | { |
| 5168 | static const char * const channel_sfx[] = { |
Takashi Iwai | d6018bb | 2011-11-21 14:17:16 +0100 | [diff] [blame] | 5169 | " Front", " Surround", " CLFE", " Side" |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5170 | }; |
| 5171 | int i; |
| 5172 | |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5173 | i = find_idx_in_nid_list(nid, pins, num_pins); |
| 5174 | if (i < 0) |
| 5175 | return NULL; |
| 5176 | if (num_pins == 1) |
| 5177 | return ""; |
| 5178 | if (num_pins > ARRAY_SIZE(channel_sfx)) { |
| 5179 | if (indexp) |
| 5180 | *indexp = i; |
| 5181 | return ""; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5182 | } |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5183 | return channel_sfx[i]; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid, |
| 5187 | const struct auto_pin_cfg *cfg, |
| 5188 | const char *name, char *label, int maxlen, |
| 5189 | int *indexp) |
| 5190 | { |
| 5191 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 5192 | int attr = snd_hda_get_input_pin_attr(def_conf); |
| 5193 | const char *pfx = "", *sfx = ""; |
| 5194 | |
| 5195 | /* handle as a speaker if it's a fixed line-out */ |
Takashi Iwai | e49a343 | 2012-03-01 18:14:41 +0100 | [diff] [blame] | 5196 | if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5197 | name = "Speaker"; |
| 5198 | /* check the location */ |
| 5199 | switch (attr) { |
| 5200 | case INPUT_PIN_ATTR_DOCK: |
| 5201 | pfx = "Dock "; |
| 5202 | break; |
| 5203 | case INPUT_PIN_ATTR_FRONT: |
| 5204 | pfx = "Front "; |
| 5205 | break; |
| 5206 | } |
| 5207 | if (cfg) { |
| 5208 | /* try to give a unique suffix if needed */ |
| 5209 | sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs, |
| 5210 | indexp); |
| 5211 | if (!sfx) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5212 | sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs, |
| 5213 | indexp); |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5214 | if (!sfx) { |
| 5215 | /* don't add channel suffix for Headphone controls */ |
| 5216 | int idx = find_idx_in_nid_list(nid, cfg->hp_pins, |
| 5217 | cfg->hp_outs); |
| 5218 | if (idx >= 0) |
| 5219 | *indexp = idx; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5220 | sfx = ""; |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5221 | } |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5222 | } |
| 5223 | snprintf(label, maxlen, "%s%s%s", pfx, name, sfx); |
| 5224 | return 1; |
| 5225 | } |
| 5226 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5227 | /** |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5228 | * snd_hda_get_pin_label - Get a label for the given I/O pin |
| 5229 | * |
| 5230 | * Get a label for the given pin. This function works for both input and |
| 5231 | * output pins. When @cfg is given as non-NULL, the function tries to get |
| 5232 | * an optimized label using hda_get_autocfg_input_label(). |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5233 | * |
| 5234 | * This function tries to give a unique label string for the pin as much as |
| 5235 | * possible. For example, when the multiple line-outs are present, it adds |
| 5236 | * the channel suffix like "Front", "Surround", etc (only when @cfg is given). |
| 5237 | * If no unique name with a suffix is available and @indexp is non-NULL, the |
| 5238 | * index number is stored in the pointer. |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5239 | */ |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5240 | int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid, |
| 5241 | const struct auto_pin_cfg *cfg, |
| 5242 | char *label, int maxlen, int *indexp) |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5243 | { |
| 5244 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5245 | const char *name = NULL; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5246 | int i; |
| 5247 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5248 | if (indexp) |
| 5249 | *indexp = 0; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5250 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5251 | return 0; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5252 | |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5253 | switch (get_defcfg_device(def_conf)) { |
| 5254 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e49a343 | 2012-03-01 18:14:41 +0100 | [diff] [blame] | 5255 | return fill_audio_out_name(codec, nid, cfg, "Line Out", |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5256 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5257 | case AC_JACK_SPEAKER: |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5258 | return fill_audio_out_name(codec, nid, cfg, "Speaker", |
| 5259 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5260 | case AC_JACK_HP_OUT: |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5261 | return fill_audio_out_name(codec, nid, cfg, "Headphone", |
| 5262 | label, maxlen, indexp); |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5263 | case AC_JACK_SPDIF_OUT: |
| 5264 | case AC_JACK_DIG_OTHER_OUT: |
| 5265 | if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI) |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5266 | name = "HDMI"; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5267 | else |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5268 | name = "SPDIF"; |
| 5269 | if (cfg && indexp) { |
Takashi Iwai | 358b6e6 | 2011-11-21 14:34:20 +0100 | [diff] [blame] | 5270 | i = find_idx_in_nid_list(nid, cfg->dig_out_pins, |
| 5271 | cfg->dig_outs); |
| 5272 | if (i >= 0) |
| 5273 | *indexp = i; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5274 | } |
| 5275 | break; |
| 5276 | default: |
| 5277 | if (cfg) { |
| 5278 | for (i = 0; i < cfg->num_inputs; i++) { |
| 5279 | if (cfg->inputs[i].pin != nid) |
| 5280 | continue; |
| 5281 | name = hda_get_autocfg_input_label(codec, cfg, i); |
| 5282 | if (name) |
| 5283 | break; |
| 5284 | } |
| 5285 | } |
| 5286 | if (!name) |
| 5287 | name = hda_get_input_pin_label(codec, nid, true); |
| 5288 | break; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5289 | } |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 5290 | if (!name) |
| 5291 | return 0; |
| 5292 | strlcpy(label, name, maxlen); |
| 5293 | return 1; |
Takashi Iwai | 04f5ade | 2011-10-27 20:47:07 +0200 | [diff] [blame] | 5294 | } |
| 5295 | EXPORT_SYMBOL_HDA(snd_hda_get_pin_label); |
| 5296 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5297 | /** |
| 5298 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5299 | * |
| 5300 | * When the same label is used already in the existing items, the number |
| 5301 | * suffix is appended to the label. This label index number is stored |
| 5302 | * to type_idx when non-NULL pointer is given. |
| 5303 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5304 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5305 | int index, int *type_idx) |
| 5306 | { |
| 5307 | int i, label_idx = 0; |
| 5308 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5309 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5310 | return -EINVAL; |
| 5311 | } |
| 5312 | for (i = 0; i < imux->num_items; i++) { |
| 5313 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5314 | label_idx++; |
| 5315 | } |
| 5316 | if (type_idx) |
| 5317 | *type_idx = label_idx; |
| 5318 | if (label_idx > 0) |
| 5319 | snprintf(imux->items[imux->num_items].label, |
| 5320 | sizeof(imux->items[imux->num_items].label), |
| 5321 | "%s %d", label, label_idx); |
| 5322 | else |
| 5323 | strlcpy(imux->items[imux->num_items].label, label, |
| 5324 | sizeof(imux->items[imux->num_items].label)); |
| 5325 | imux->items[imux->num_items].index = index; |
| 5326 | imux->num_items++; |
| 5327 | return 0; |
| 5328 | } |
| 5329 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5330 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | #ifdef CONFIG_PM |
| 5333 | /* |
| 5334 | * power management |
| 5335 | */ |
| 5336 | |
| 5337 | /** |
| 5338 | * snd_hda_suspend - suspend the codecs |
| 5339 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | * |
| 5341 | * Returns 0 if successful. |
| 5342 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5343 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5345 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5347 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5348 | if (hda_codec_is_power_on(codec)) |
| 5349 | hda_call_codec_suspend(codec); |
Takashi Iwai | 785f857 | 2012-03-07 10:58:39 +0100 | [diff] [blame] | 5350 | else /* forcibly change the power to D3 even if not used */ |
| 5351 | hda_set_power_state(codec, |
| 5352 | codec->afg ? codec->afg : codec->mfg, |
| 5353 | AC_PWRST_D3); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5354 | if (codec->patch_ops.post_suspend) |
| 5355 | codec->patch_ops.post_suspend(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | } |
| 5357 | return 0; |
| 5358 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5359 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | |
| 5361 | /** |
| 5362 | * snd_hda_resume - resume the codecs |
| 5363 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | * |
| 5365 | * Returns 0 if successful. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5366 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5367 | * This function is defined only when POWER_SAVE isn't set. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5368 | * In the power-save mode, the codec is resumed dynamically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | */ |
| 5370 | int snd_hda_resume(struct hda_bus *bus) |
| 5371 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5372 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5374 | list_for_each_entry(codec, &bus->codec_list, list) { |
Vitaliy Kulikov | d02667e | 2011-07-22 18:18:15 -0500 | [diff] [blame] | 5375 | if (codec->patch_ops.pre_resume) |
| 5376 | codec->patch_ops.pre_resume(codec); |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 5377 | if (snd_hda_codec_needs_resume(codec)) |
| 5378 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | return 0; |
| 5381 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5382 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5383 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5384 | |
| 5385 | /* |
| 5386 | * generic arrays |
| 5387 | */ |
| 5388 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5389 | /** |
| 5390 | * snd_array_new - get a new element from the given array |
| 5391 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5392 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5393 | * Get a new element from the given array. If it exceeds the |
| 5394 | * pre-allocated array size, re-allocate the array. |
| 5395 | * |
| 5396 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5397 | */ |
| 5398 | void *snd_array_new(struct snd_array *array) |
| 5399 | { |
| 5400 | if (array->used >= array->alloced) { |
| 5401 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5402 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5403 | int oldsize = array->alloced * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5404 | void *nlist; |
| 5405 | if (snd_BUG_ON(num >= 4096)) |
| 5406 | return NULL; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5407 | nlist = krealloc(array->list, size, GFP_KERNEL); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5408 | if (!nlist) |
| 5409 | return NULL; |
Takashi Iwai | 00ef961 | 2011-07-14 15:57:27 +0200 | [diff] [blame] | 5410 | memset(nlist + oldsize, 0, size - oldsize); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5411 | array->list = nlist; |
| 5412 | array->alloced = num; |
| 5413 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5414 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5415 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5416 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5417 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5418 | /** |
| 5419 | * snd_array_free - free the given array elements |
| 5420 | * @array: the array object |
| 5421 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5422 | void snd_array_free(struct snd_array *array) |
| 5423 | { |
| 5424 | kfree(array->list); |
| 5425 | array->used = 0; |
| 5426 | array->alloced = 0; |
| 5427 | array->list = NULL; |
| 5428 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5429 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5430 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5431 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5432 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5433 | * @pcm: PCM caps bits |
| 5434 | * @buf: the string buffer to write |
| 5435 | * @buflen: the max buffer length |
| 5436 | * |
| 5437 | * used by hda_proc.c and hda_eld.c |
| 5438 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5439 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5440 | { |
| 5441 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5442 | int i, j; |
| 5443 | |
| 5444 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5445 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5446 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5447 | |
| 5448 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5449 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5450 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5451 | |
| 5452 | MODULE_DESCRIPTION("HDA codec core"); |
| 5453 | MODULE_LICENSE("GPL"); |