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