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