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