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 | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 98 | #define codec_in_pm(codec) ((codec)->in_pm) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 99 | static void hda_power_work(struct work_struct *work); |
| 100 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 101 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 102 | static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up) |
| 103 | { |
| 104 | if (bus->ops.pm_notify) |
| 105 | bus->ops.pm_notify(bus, power_up); |
| 106 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 107 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 108 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 109 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 110 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 111 | #define hda_call_pm_notify(bus, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 112 | #endif |
| 113 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 114 | /** |
| 115 | * snd_hda_get_jack_location - Give a location string of the jack |
| 116 | * @cfg: pin default config value |
| 117 | * |
| 118 | * Parse the pin default config value and returns the string of the |
| 119 | * jack location, e.g. "Rear", "Front", etc. |
| 120 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 121 | const char *snd_hda_get_jack_location(u32 cfg) |
| 122 | { |
| 123 | static char *bases[7] = { |
| 124 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 125 | }; |
| 126 | static unsigned char specials_idx[] = { |
| 127 | 0x07, 0x08, |
| 128 | 0x17, 0x18, 0x19, |
| 129 | 0x37, 0x38 |
| 130 | }; |
| 131 | static char *specials[] = { |
| 132 | "Rear Panel", "Drive Bar", |
| 133 | "Riser", "HDMI", "ATAPI", |
| 134 | "Mobile-In", "Mobile-Out" |
| 135 | }; |
| 136 | int i; |
| 137 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 138 | if ((cfg & 0x0f) < 7) |
| 139 | return bases[cfg & 0x0f]; |
| 140 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 141 | if (cfg == specials_idx[i]) |
| 142 | return specials[i]; |
| 143 | } |
| 144 | return "UNKNOWN"; |
| 145 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 146 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 147 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 148 | /** |
| 149 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 150 | * @cfg: pin default config value |
| 151 | * |
| 152 | * Parse the pin default config value and returns the string of the |
| 153 | * jack connectivity, i.e. external or internal connection. |
| 154 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 155 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 156 | { |
| 157 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 158 | |
| 159 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 160 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 161 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 162 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 163 | /** |
| 164 | * snd_hda_get_jack_type - Give a type string of the jack |
| 165 | * @cfg: pin default config value |
| 166 | * |
| 167 | * Parse the pin default config value and returns the string of the |
| 168 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 169 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 170 | const char *snd_hda_get_jack_type(u32 cfg) |
| 171 | { |
| 172 | static char *jack_types[16] = { |
| 173 | "Line Out", "Speaker", "HP Out", "CD", |
| 174 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 175 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 176 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 180 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 181 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 182 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 183 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 184 | /* |
| 185 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 186 | */ |
| 187 | static inline unsigned int |
| 188 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 189 | unsigned int verb, unsigned int parm) |
| 190 | { |
| 191 | u32 val; |
| 192 | |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 193 | if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || |
| 194 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 195 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", |
| 196 | codec->addr, direct, nid, verb, parm); |
| 197 | return ~0; |
| 198 | } |
| 199 | |
| 200 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 201 | val |= (u32)direct << 27; |
| 202 | val |= (u32)nid << 20; |
| 203 | val |= verb << 8; |
| 204 | val |= parm; |
| 205 | return val; |
| 206 | } |
| 207 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 208 | /* |
| 209 | * Send and receive a verb |
| 210 | */ |
| 211 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
| 212 | unsigned int *res) |
| 213 | { |
| 214 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 215 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 216 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 217 | if (cmd == ~0) |
| 218 | return -1; |
| 219 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 220 | if (res) |
| 221 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 222 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 223 | snd_hda_power_up(codec); |
| 224 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 225 | for (;;) { |
| 226 | trace_hda_send_cmd(codec, cmd); |
| 227 | err = bus->ops.command(bus, cmd); |
| 228 | if (err != -EAGAIN) |
| 229 | break; |
| 230 | /* process pending verbs */ |
| 231 | bus->ops.get_response(bus, codec->addr); |
| 232 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 233 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 234 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 235 | trace_hda_get_response(codec, *res); |
| 236 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 237 | mutex_unlock(&bus->cmd_mutex); |
| 238 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 239 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 240 | if (bus->response_reset) { |
| 241 | snd_printd("hda_codec: resetting BUS due to " |
| 242 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 243 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 244 | bus->ops.bus_reset(bus); |
| 245 | } |
| 246 | goto again; |
| 247 | } |
| 248 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 249 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 250 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 251 | return err; |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /** |
| 255 | * snd_hda_codec_read - send a command and get the response |
| 256 | * @codec: the HDA codec |
| 257 | * @nid: NID to send the command |
| 258 | * @direct: direct flag |
| 259 | * @verb: the verb to send |
| 260 | * @parm: the parameter for the verb |
| 261 | * |
| 262 | * Send a single command and read the corresponding response. |
| 263 | * |
| 264 | * Returns the obtained response value, or -1 for an error. |
| 265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 266 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 267 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | unsigned int verb, unsigned int parm) |
| 269 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 270 | unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
| 271 | unsigned int res; |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 272 | if (codec_exec_verb(codec, cmd, &res)) |
| 273 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return res; |
| 275 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 276 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * snd_hda_codec_write - send a single command without waiting for response |
| 280 | * @codec: the HDA codec |
| 281 | * @nid: NID to send the command |
| 282 | * @direct: direct flag |
| 283 | * @verb: the verb to send |
| 284 | * @parm: the parameter for the verb |
| 285 | * |
| 286 | * Send a single command without waiting for response. |
| 287 | * |
| 288 | * Returns 0 if successful, or a negative error code. |
| 289 | */ |
| 290 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 291 | unsigned int verb, unsigned int parm) |
| 292 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 293 | unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 294 | unsigned int res; |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 295 | return codec_exec_verb(codec, cmd, |
| 296 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 298 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * snd_hda_sequence_write - sequence writes |
| 302 | * @codec: the HDA codec |
| 303 | * @seq: VERB array to send |
| 304 | * |
| 305 | * Send the commands sequentially from the given array. |
| 306 | * The array must be terminated with NID=0. |
| 307 | */ |
| 308 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 309 | { |
| 310 | for (; seq->nid; seq++) |
| 311 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 312 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 313 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 317 | * @codec: the HDA codec |
| 318 | * @nid: NID to parse |
| 319 | * @start_id: the pointer to store the start NID |
| 320 | * |
| 321 | * Parse the NID and store the start NID of its sub-nodes. |
| 322 | * Returns the number of sub-nodes. |
| 323 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 324 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 325 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | unsigned int parm; |
| 328 | |
| 329 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 330 | if (parm == -1) |
| 331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | *start_id = (parm >> 16) & 0x7fff; |
| 333 | return (int)(parm & 0x7fff); |
| 334 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 335 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 337 | /* connection list element */ |
| 338 | struct hda_conn_list { |
| 339 | struct list_head list; |
| 340 | int len; |
| 341 | hda_nid_t nid; |
| 342 | hda_nid_t conns[0]; |
| 343 | }; |
| 344 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 345 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 346 | static struct hda_conn_list * |
| 347 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 348 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | struct hda_conn_list *p; |
| 350 | list_for_each_entry(p, &codec->conn_list, list) { |
| 351 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 352 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 353 | } |
| 354 | return NULL; |
| 355 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 356 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 357 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 358 | const hda_nid_t *list) |
| 359 | { |
| 360 | struct hda_conn_list *p; |
| 361 | |
| 362 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 363 | if (!p) |
| 364 | return -ENOMEM; |
| 365 | p->len = len; |
| 366 | p->nid = nid; |
| 367 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 368 | list_add(&p->list, &codec->conn_list); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void remove_conn_list(struct hda_codec *codec) |
| 373 | { |
| 374 | while (!list_empty(&codec->conn_list)) { |
| 375 | struct hda_conn_list *p; |
| 376 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 377 | list_del(&p->list); |
| 378 | kfree(p); |
| 379 | } |
| 380 | } |
| 381 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 382 | /* read the connection and add to the cache */ |
| 383 | 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] | 384 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 385 | hda_nid_t list[32]; |
| 386 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 387 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 388 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 389 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 390 | if (len == -ENOSPC) { |
| 391 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 392 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 393 | if (!result) |
| 394 | return -ENOMEM; |
| 395 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 396 | } |
| 397 | if (len >= 0) |
| 398 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 399 | if (result != list) |
| 400 | kfree(result); |
| 401 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 402 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 403 | |
| 404 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 405 | * snd_hda_get_conn_list - get connection list |
| 406 | * @codec: the HDA codec |
| 407 | * @nid: NID to parse |
| 408 | * @len: number of connection list entries |
| 409 | * @listp: the pointer to store NID list |
| 410 | * |
| 411 | * Parses the connection list of the given widget and stores the pointer |
| 412 | * to the list of NIDs. |
| 413 | * |
| 414 | * Returns the number of connections, or a negative error code. |
| 415 | * |
| 416 | * Note that the returned pointer isn't protected against the list |
| 417 | * modification. If snd_hda_override_conn_list() might be called |
| 418 | * concurrently, protect with a mutex appropriately. |
| 419 | */ |
| 420 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 421 | const hda_nid_t **listp) |
| 422 | { |
| 423 | bool added = false; |
| 424 | |
| 425 | for (;;) { |
| 426 | int err; |
| 427 | const struct hda_conn_list *p; |
| 428 | |
| 429 | /* if the connection-list is already cached, read it */ |
| 430 | p = lookup_conn_list(codec, nid); |
| 431 | if (p) { |
| 432 | if (listp) |
| 433 | *listp = p->conns; |
| 434 | return p->len; |
| 435 | } |
| 436 | if (snd_BUG_ON(added)) |
| 437 | return -EINVAL; |
| 438 | |
| 439 | err = read_and_add_raw_conns(codec, nid); |
| 440 | if (err < 0) |
| 441 | return err; |
| 442 | added = true; |
| 443 | } |
| 444 | } |
| 445 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 446 | |
| 447 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 448 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | * @codec: the HDA codec |
| 450 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 451 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * @max_conns: max. number of connections to store |
| 453 | * |
| 454 | * Parses the connection list of the given widget and stores the list |
| 455 | * of NIDs. |
| 456 | * |
| 457 | * Returns the number of connections, or a negative error code. |
| 458 | */ |
| 459 | 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] | 460 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 461 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 462 | const hda_nid_t *list; |
| 463 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 464 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 465 | if (len > 0 && conn_list) { |
| 466 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 467 | snd_printk(KERN_ERR "hda_codec: " |
| 468 | "Too many connections %d for NID 0x%x\n", |
| 469 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 472 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 473 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 474 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 475 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 476 | } |
| 477 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 478 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 479 | /* return CONNLIST_LEN parameter of the given widget */ |
| 480 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 481 | { |
| 482 | unsigned int wcaps = get_wcaps(codec, nid); |
| 483 | unsigned int parm; |
| 484 | |
| 485 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 486 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 487 | return 0; |
| 488 | |
| 489 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 490 | if (parm == -1) |
| 491 | parm = 0; |
| 492 | return parm; |
| 493 | } |
| 494 | |
| 495 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 496 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 497 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 500 | /** |
| 501 | * snd_hda_get_raw_connections - copy connection list without cache |
| 502 | * @codec: the HDA codec |
| 503 | * @nid: NID to parse |
| 504 | * @conn_list: connection list array |
| 505 | * @max_conns: max. number of connections to store |
| 506 | * |
| 507 | * Like snd_hda_get_connections(), copy the connection list but without |
| 508 | * checking through the connection-list cache. |
| 509 | * Currently called only from hda_proc.c, so not exported. |
| 510 | */ |
| 511 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 512 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 515 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 517 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 518 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 520 | parm = get_num_conns(codec, nid); |
| 521 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 522 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (parm & AC_CLIST_LONG) { |
| 525 | /* long form */ |
| 526 | shift = 16; |
| 527 | num_elems = 2; |
| 528 | } else { |
| 529 | /* short form */ |
| 530 | shift = 8; |
| 531 | num_elems = 4; |
| 532 | } |
| 533 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | mask = (1 << (shift-1)) - 1; |
| 535 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 536 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return 0; /* no connection */ |
| 538 | |
| 539 | if (conn_len == 1) { |
| 540 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 541 | parm = snd_hda_codec_read(codec, nid, 0, |
| 542 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 543 | if (parm == -1 && codec->bus->rirb_error) |
| 544 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 545 | if (conn_list) |
| 546 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | /* multi connection */ |
| 551 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 552 | prev_nid = 0; |
| 553 | for (i = 0; i < conn_len; i++) { |
| 554 | int range_val; |
| 555 | hda_nid_t val, n; |
| 556 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 557 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 558 | parm = snd_hda_codec_read(codec, nid, 0, |
| 559 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 560 | if (parm == -1 && codec->bus->rirb_error) |
| 561 | return -EIO; |
| 562 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 563 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 564 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 565 | if (val == 0 && null_count++) { /* no second chance */ |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 566 | snd_printk(KERN_WARNING "hda_codec: " |
| 567 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 568 | nid, i, parm); |
| 569 | return 0; |
| 570 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 571 | parm >>= shift; |
| 572 | if (range_val) { |
| 573 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 574 | if (!prev_nid || prev_nid >= val) { |
| 575 | snd_printk(KERN_WARNING "hda_codec: " |
| 576 | "invalid dep_range_val %x:%x\n", |
| 577 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 578 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 580 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 581 | if (conn_list) { |
| 582 | if (conns >= max_conns) |
| 583 | return -ENOSPC; |
| 584 | conn_list[conns] = n; |
| 585 | } |
| 586 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 587 | } |
| 588 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 589 | if (conn_list) { |
| 590 | if (conns >= max_conns) |
| 591 | return -ENOSPC; |
| 592 | conn_list[conns] = val; |
| 593 | } |
| 594 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 596 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | return conns; |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 602 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 603 | * @codec: the HDA codec |
| 604 | * @nid: NID to parse |
| 605 | * @len: number of connection list entries |
| 606 | * @list: the list of connection entries |
| 607 | * |
| 608 | * Add or modify the given connection-list to the cache. If the corresponding |
| 609 | * cache already exists, invalidate it and append a new one. |
| 610 | * |
| 611 | * Returns zero or a negative error code. |
| 612 | */ |
| 613 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 614 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 616 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 617 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 618 | p = lookup_conn_list(codec, nid); |
| 619 | if (p) { |
| 620 | list_del(&p->list); |
| 621 | kfree(p); |
| 622 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 623 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 624 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 626 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 627 | |
| 628 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 629 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 630 | * @codec: the HDA codec |
| 631 | * @mux: NID containing the list |
| 632 | * @nid: NID to select |
| 633 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 634 | * |
| 635 | * Parses the connection list of the widget @mux and checks whether the |
| 636 | * widget @nid is present. If it is, return the connection index. |
| 637 | * Otherwise it returns -1. |
| 638 | */ |
| 639 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 640 | hda_nid_t nid, int recursive) |
| 641 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 642 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 643 | int i, nums; |
| 644 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 645 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 646 | for (i = 0; i < nums; i++) |
| 647 | if (conn[i] == nid) |
| 648 | return i; |
| 649 | if (!recursive) |
| 650 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 651 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 652 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 653 | return -1; |
| 654 | } |
| 655 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 656 | for (i = 0; i < nums; i++) { |
| 657 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 658 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 659 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 660 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 661 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 662 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | /** |
| 668 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 669 | * @bus: the BUS |
| 670 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 671 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 672 | * |
| 673 | * Adds the given event to the queue. The events are processed in |
| 674 | * the workqueue asynchronously. Call this function in the interrupt |
| 675 | * hanlder when RIRB receives an unsolicited event. |
| 676 | * |
| 677 | * Returns 0 if successful, or a negative error code. |
| 678 | */ |
| 679 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 680 | { |
| 681 | struct hda_bus_unsolicited *unsol; |
| 682 | unsigned int wp; |
| 683 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 684 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 685 | unsol = bus->unsol; |
| 686 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | return 0; |
| 688 | |
| 689 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 690 | unsol->wp = wp; |
| 691 | |
| 692 | wp <<= 1; |
| 693 | unsol->queue[wp] = res; |
| 694 | unsol->queue[wp + 1] = res_ex; |
| 695 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 696 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | return 0; |
| 699 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 700 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 703 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 705 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 707 | struct hda_bus_unsolicited *unsol = |
| 708 | container_of(work, struct hda_bus_unsolicited, work); |
| 709 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | struct hda_codec *codec; |
| 711 | unsigned int rp, caddr, res; |
| 712 | |
| 713 | while (unsol->rp != unsol->wp) { |
| 714 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 715 | unsol->rp = rp; |
| 716 | rp <<= 1; |
| 717 | res = unsol->queue[rp]; |
| 718 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 719 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | continue; |
| 721 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 722 | if (codec && codec->patch_ops.unsol_event) |
| 723 | codec->patch_ops.unsol_event(codec, res); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * initialize unsolicited queue |
| 729 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 730 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
| 732 | struct hda_bus_unsolicited *unsol; |
| 733 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 734 | if (bus->unsol) /* already initialized */ |
| 735 | return 0; |
| 736 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 737 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 738 | if (!unsol) { |
| 739 | snd_printk(KERN_ERR "hda_codec: " |
| 740 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | return -ENOMEM; |
| 742 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 743 | INIT_WORK(&unsol->work, process_unsol_events); |
| 744 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | bus->unsol = unsol; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * destructor |
| 751 | */ |
| 752 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 753 | |
| 754 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 755 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 756 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 758 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 760 | if (bus->workq) |
| 761 | flush_workqueue(bus->workq); |
| 762 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 764 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | snd_hda_codec_free(codec); |
| 766 | } |
| 767 | if (bus->ops.private_free) |
| 768 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 769 | if (bus->workq) |
| 770 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | kfree(bus); |
| 772 | return 0; |
| 773 | } |
| 774 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 775 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
| 777 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 778 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return snd_hda_bus_free(bus); |
| 780 | } |
| 781 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 782 | #ifdef CONFIG_SND_HDA_HWDEP |
| 783 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 784 | { |
| 785 | struct hda_bus *bus = device->device_data; |
| 786 | struct hda_codec *codec; |
| 787 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 788 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 789 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 790 | } |
| 791 | return 0; |
| 792 | } |
| 793 | #else |
| 794 | #define snd_hda_bus_dev_register NULL |
| 795 | #endif |
| 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | /** |
| 798 | * snd_hda_bus_new - create a HDA bus |
| 799 | * @card: the card entry |
| 800 | * @temp: the template for hda_bus information |
| 801 | * @busp: the pointer to store the created bus instance |
| 802 | * |
| 803 | * Returns 0 if successful, or a negative error code. |
| 804 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 805 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 806 | const struct hda_bus_template *temp, |
| 807 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
| 809 | struct hda_bus *bus; |
| 810 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 811 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 812 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | .dev_free = snd_hda_bus_dev_free, |
| 814 | }; |
| 815 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 816 | if (snd_BUG_ON(!temp)) |
| 817 | return -EINVAL; |
| 818 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 819 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | if (busp) |
| 822 | *busp = NULL; |
| 823 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 824 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (bus == NULL) { |
| 826 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 827 | return -ENOMEM; |
| 828 | } |
| 829 | |
| 830 | bus->card = card; |
| 831 | bus->private_data = temp->private_data; |
| 832 | bus->pci = temp->pci; |
| 833 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 834 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | bus->ops = temp->ops; |
| 836 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 837 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 838 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | INIT_LIST_HEAD(&bus->codec_list); |
| 840 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 841 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 842 | "hd-audio%d", card->number); |
| 843 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 844 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 845 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 846 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 847 | kfree(bus); |
| 848 | return -ENOMEM; |
| 849 | } |
| 850 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 851 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 852 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | snd_hda_bus_free(bus); |
| 854 | return err; |
| 855 | } |
| 856 | if (busp) |
| 857 | *busp = bus; |
| 858 | return 0; |
| 859 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 860 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 862 | #ifdef CONFIG_SND_HDA_GENERIC |
| 863 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 864 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 865 | #else |
| 866 | #define is_generic_config(codec) 0 |
| 867 | #endif |
| 868 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 869 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 870 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 871 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 872 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 873 | #endif |
| 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | /* |
| 876 | * find a matching codec preset |
| 877 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 878 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 879 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 881 | struct hda_codec_preset_list *tbl; |
| 882 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 883 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 885 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 886 | return NULL; /* use the generic parser */ |
| 887 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 888 | again: |
| 889 | mutex_lock(&preset_mutex); |
| 890 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 891 | if (!try_module_get(tbl->owner)) { |
| 892 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 893 | continue; |
| 894 | } |
| 895 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 897 | if (preset->afg && preset->afg != codec->afg) |
| 898 | continue; |
| 899 | if (preset->mfg && preset->mfg != codec->mfg) |
| 900 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 901 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 903 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 904 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 905 | preset->rev == codec->revision_id)) { |
| 906 | mutex_unlock(&preset_mutex); |
| 907 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 911 | module_put(tbl->owner); |
| 912 | } |
| 913 | mutex_unlock(&preset_mutex); |
| 914 | |
| 915 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 916 | char name[32]; |
| 917 | if (!mod_requested) |
| 918 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 919 | codec->vendor_id); |
| 920 | else |
| 921 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 922 | (codec->vendor_id >> 16) & 0xffff); |
| 923 | request_module(name); |
| 924 | mod_requested++; |
| 925 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 931 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 933 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | { |
| 935 | const struct hda_vendor_id *c; |
| 936 | const char *vendor = NULL; |
| 937 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 938 | char tmp[16]; |
| 939 | |
| 940 | if (codec->vendor_name) |
| 941 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | for (c = hda_vendor_ids; c->id; c++) { |
| 944 | if (c->id == vendor_id) { |
| 945 | vendor = c->name; |
| 946 | break; |
| 947 | } |
| 948 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 949 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | sprintf(tmp, "Generic %04x", vendor_id); |
| 951 | vendor = tmp; |
| 952 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 953 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 954 | if (!codec->vendor_name) |
| 955 | return -ENOMEM; |
| 956 | |
| 957 | get_chip_name: |
| 958 | if (codec->chip_name) |
| 959 | return 0; |
| 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 962 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 963 | else { |
| 964 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 965 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 966 | } |
| 967 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 968 | return -ENOMEM; |
| 969 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 973 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 975 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 977 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | hda_nid_t nid; |
| 979 | |
| 980 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 981 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 982 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 983 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 984 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 985 | case AC_GRP_AUDIO_FUNCTION: |
| 986 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 987 | codec->afg_function_id = function_id & 0xff; |
| 988 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 989 | break; |
| 990 | case AC_GRP_MODEM_FUNCTION: |
| 991 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 992 | codec->mfg_function_id = function_id & 0xff; |
| 993 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 994 | break; |
| 995 | default: |
| 996 | break; |
| 997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1002 | * read widget caps for each widget and store in cache |
| 1003 | */ |
| 1004 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1005 | { |
| 1006 | int i; |
| 1007 | hda_nid_t nid; |
| 1008 | |
| 1009 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1010 | &codec->start_nid); |
| 1011 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1012 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1013 | return -ENOMEM; |
| 1014 | nid = codec->start_nid; |
| 1015 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1016 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1017 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1021 | /* read all pin default configurations and save codec->init_pins */ |
| 1022 | static int read_pin_defaults(struct hda_codec *codec) |
| 1023 | { |
| 1024 | int i; |
| 1025 | hda_nid_t nid = codec->start_nid; |
| 1026 | |
| 1027 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1028 | struct hda_pincfg *pin; |
| 1029 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1030 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1031 | if (wid_type != AC_WID_PIN) |
| 1032 | continue; |
| 1033 | pin = snd_array_new(&codec->init_pins); |
| 1034 | if (!pin) |
| 1035 | return -ENOMEM; |
| 1036 | pin->nid = nid; |
| 1037 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1038 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1039 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1040 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1041 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* look up the given pin config list and return the item matching with NID */ |
| 1047 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1048 | struct snd_array *array, |
| 1049 | hda_nid_t nid) |
| 1050 | { |
| 1051 | int i; |
| 1052 | for (i = 0; i < array->used; i++) { |
| 1053 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1054 | if (pin->nid == nid) |
| 1055 | return pin; |
| 1056 | } |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1060 | /* set the current pin config value for the given NID. |
| 1061 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1062 | */ |
| 1063 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1064 | hda_nid_t nid, unsigned int cfg) |
| 1065 | { |
| 1066 | struct hda_pincfg *pin; |
| 1067 | |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1068 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1069 | return -EINVAL; |
| 1070 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1071 | pin = look_up_pincfg(codec, list, nid); |
| 1072 | if (!pin) { |
| 1073 | pin = snd_array_new(list); |
| 1074 | if (!pin) |
| 1075 | return -ENOMEM; |
| 1076 | pin->nid = nid; |
| 1077 | } |
| 1078 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1079 | return 0; |
| 1080 | } |
| 1081 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1082 | /** |
| 1083 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1084 | * @codec: the HDA codec |
| 1085 | * @nid: NID to set the pin config |
| 1086 | * @cfg: the pin default config value |
| 1087 | * |
| 1088 | * Override a pin default configuration value in the cache. |
| 1089 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1090 | * priority than the real hardware value. |
| 1091 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1092 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1093 | hda_nid_t nid, unsigned int cfg) |
| 1094 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1095 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1096 | } |
| 1097 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1098 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1099 | /** |
| 1100 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1101 | * @codec: the HDA codec |
| 1102 | * @nid: NID to get the pin config |
| 1103 | * |
| 1104 | * Get the current pin config value of the given pin NID. |
| 1105 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1106 | * returns the cached value. |
| 1107 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1108 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1109 | { |
| 1110 | struct hda_pincfg *pin; |
| 1111 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1112 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1113 | { |
| 1114 | unsigned int cfg = 0; |
| 1115 | mutex_lock(&codec->user_mutex); |
| 1116 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1117 | if (pin) |
| 1118 | cfg = pin->cfg; |
| 1119 | mutex_unlock(&codec->user_mutex); |
| 1120 | if (cfg) |
| 1121 | return cfg; |
| 1122 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1123 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1124 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1125 | if (pin) |
| 1126 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1127 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1128 | if (pin) |
| 1129 | return pin->cfg; |
| 1130 | return 0; |
| 1131 | } |
| 1132 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1133 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1134 | /* remember the current pinctl target value */ |
| 1135 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1136 | unsigned int val) |
| 1137 | { |
| 1138 | struct hda_pincfg *pin; |
| 1139 | |
| 1140 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1141 | if (!pin) |
| 1142 | return -EINVAL; |
| 1143 | pin->target = val; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target); |
| 1147 | |
| 1148 | /* return the current pinctl target value */ |
| 1149 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1150 | { |
| 1151 | struct hda_pincfg *pin; |
| 1152 | |
| 1153 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1154 | if (!pin) |
| 1155 | return 0; |
| 1156 | return pin->target; |
| 1157 | } |
| 1158 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target); |
| 1159 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1160 | /** |
| 1161 | * snd_hda_shutup_pins - Shut up all pins |
| 1162 | * @codec: the HDA codec |
| 1163 | * |
| 1164 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1165 | * The controls aren't cached so that they can be resumed properly. |
| 1166 | */ |
| 1167 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1168 | { |
| 1169 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1170 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1171 | * the default pin setup at the next load of the driver |
| 1172 | */ |
| 1173 | if (codec->bus->shutdown) |
| 1174 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1175 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1176 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1177 | /* use read here for syncing after issuing each verb */ |
| 1178 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1179 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1180 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1181 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1182 | } |
| 1183 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1184 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1185 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1186 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1187 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1188 | { |
| 1189 | int i; |
| 1190 | if (!codec->pins_shutup) |
| 1191 | return; |
| 1192 | if (codec->bus->shutdown) |
| 1193 | return; |
| 1194 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1195 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1196 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1197 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1198 | pin->ctrl); |
| 1199 | } |
| 1200 | codec->pins_shutup = 0; |
| 1201 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1202 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1203 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1204 | static void hda_jackpoll_work(struct work_struct *work) |
| 1205 | { |
| 1206 | struct hda_codec *codec = |
| 1207 | container_of(work, struct hda_codec, jackpoll_work.work); |
| 1208 | if (!codec->jackpoll_interval) |
| 1209 | return; |
| 1210 | |
| 1211 | snd_hda_jack_set_dirty_all(codec); |
| 1212 | snd_hda_jack_poll_all(codec); |
| 1213 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1214 | codec->jackpoll_interval); |
| 1215 | } |
| 1216 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1217 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1218 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1219 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1220 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1221 | /* release all pincfg lists */ |
| 1222 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1223 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1224 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1225 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1226 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1227 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1228 | snd_array_free(&codec->init_pins); |
| 1229 | } |
| 1230 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1231 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1232 | * audio-converter setup caches |
| 1233 | */ |
| 1234 | struct hda_cvt_setup { |
| 1235 | hda_nid_t nid; |
| 1236 | u8 stream_tag; |
| 1237 | u8 channel_id; |
| 1238 | u16 format_id; |
| 1239 | unsigned char active; /* cvt is currently used */ |
| 1240 | unsigned char dirty; /* setups should be cleared */ |
| 1241 | }; |
| 1242 | |
| 1243 | /* get or create a cache entry for the given audio converter NID */ |
| 1244 | static struct hda_cvt_setup * |
| 1245 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1246 | { |
| 1247 | struct hda_cvt_setup *p; |
| 1248 | int i; |
| 1249 | |
| 1250 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1251 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1252 | if (p->nid == nid) |
| 1253 | return p; |
| 1254 | } |
| 1255 | p = snd_array_new(&codec->cvt_setups); |
| 1256 | if (p) |
| 1257 | p->nid = nid; |
| 1258 | return p; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | * codec destructor |
| 1263 | */ |
| 1264 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1265 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1266 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1268 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1269 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1270 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1271 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1272 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1273 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1274 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1276 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1277 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1278 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1279 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1280 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1282 | if (codec->patch_ops.free) |
| 1283 | codec->patch_ops.free(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1284 | #ifdef CONFIG_PM |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 1285 | if (!codec->pm_down_notified) /* cancel leftover refcounts */ |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1286 | hda_call_pm_notify(codec->bus, false); |
| 1287 | #endif |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1288 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1289 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1290 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1291 | kfree(codec->vendor_name); |
| 1292 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1293 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1294 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | kfree(codec); |
| 1296 | } |
| 1297 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1298 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1299 | hda_nid_t fg, unsigned int power_state); |
| 1300 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1301 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1302 | unsigned int power_state); |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | /** |
| 1305 | * snd_hda_codec_new - create a HDA codec |
| 1306 | * @bus: the bus to assign |
| 1307 | * @codec_addr: the codec address |
| 1308 | * @codecp: the pointer to store the generated codec |
| 1309 | * |
| 1310 | * Returns 0 if successful, or a negative error code. |
| 1311 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1312 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1313 | unsigned int codec_addr, |
| 1314 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | { |
| 1316 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1317 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1318 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | int err; |
| 1320 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1321 | if (snd_BUG_ON(!bus)) |
| 1322 | return -EINVAL; |
| 1323 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1324 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1327 | snd_printk(KERN_ERR "hda_codec: " |
| 1328 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return -EBUSY; |
| 1330 | } |
| 1331 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1332 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (codec == NULL) { |
| 1334 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1335 | return -ENOMEM; |
| 1336 | } |
| 1337 | |
| 1338 | codec->bus = bus; |
| 1339 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1340 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1341 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1342 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1343 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1344 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1345 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1346 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1347 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1348 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1349 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1350 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1351 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1352 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1353 | INIT_LIST_HEAD(&codec->conn_list); |
| 1354 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1355 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1357 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1358 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1359 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1360 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1361 | * the caller has to power down appropriatley after initialization |
| 1362 | * phase. |
| 1363 | */ |
| 1364 | hda_keep_power_on(codec); |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1365 | hda_call_pm_notify(bus, true); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1366 | #endif |
| 1367 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1368 | if (codec->bus->modelname) { |
| 1369 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1370 | if (!codec->modelname) { |
| 1371 | snd_hda_codec_free(codec); |
| 1372 | return -ENODEV; |
| 1373 | } |
| 1374 | } |
| 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | list_add_tail(&codec->list, &bus->codec_list); |
| 1377 | bus->caddr_tbl[codec_addr] = codec; |
| 1378 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1379 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1380 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1381 | if (codec->vendor_id == -1) |
| 1382 | /* read again, hopefully the access method was corrected |
| 1383 | * in the last read... |
| 1384 | */ |
| 1385 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1386 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1387 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1388 | AC_PAR_SUBSYSTEM_ID); |
| 1389 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1390 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1392 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1393 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1394 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1395 | err = -ENODEV; |
| 1396 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1399 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1400 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1401 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1402 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1403 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1404 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1405 | err = read_pin_defaults(codec); |
| 1406 | if (err < 0) |
| 1407 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1408 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1409 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1410 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1411 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1412 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1415 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1416 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1417 | AC_PWRST_CLKSTOP); |
| 1418 | if (!codec->d3_stop_clk) |
| 1419 | bus->power_keep_link_on = 1; |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1420 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1421 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1422 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1423 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1424 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1425 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1426 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1427 | snd_hda_codec_proc_new(codec); |
| 1428 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1429 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1430 | |
| 1431 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1432 | codec->subsystem_id, codec->revision_id); |
| 1433 | snd_component_add(codec->bus->card, component); |
| 1434 | |
| 1435 | if (codecp) |
| 1436 | *codecp = codec; |
| 1437 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1438 | |
| 1439 | error: |
| 1440 | snd_hda_codec_free(codec); |
| 1441 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1442 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1443 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1444 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1445 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1446 | { |
| 1447 | hda_nid_t fg; |
| 1448 | int err; |
| 1449 | |
| 1450 | /* Assume the function group node does not change, |
| 1451 | * only the widget nodes may change. |
| 1452 | */ |
| 1453 | kfree(codec->wcaps); |
| 1454 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1455 | err = read_widget_caps(codec, fg); |
| 1456 | if (err < 0) { |
| 1457 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1458 | return err; |
| 1459 | } |
| 1460 | |
| 1461 | snd_array_free(&codec->init_pins); |
| 1462 | err = read_pin_defaults(codec); |
| 1463 | |
| 1464 | return err; |
| 1465 | } |
| 1466 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets); |
| 1467 | |
| 1468 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1469 | /** |
| 1470 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1471 | * @codec: the HDA codec |
| 1472 | * |
| 1473 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1474 | * patch instance. |
| 1475 | * |
| 1476 | * Returns 0 if successful or a negative error code. |
| 1477 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1478 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1479 | { |
| 1480 | int err; |
| 1481 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1482 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1483 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1484 | err = get_codec_name(codec); |
| 1485 | if (err < 0) |
| 1486 | return err; |
| 1487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1489 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1491 | goto patched; |
| 1492 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1493 | if (codec->preset && codec->preset->patch) { |
| 1494 | err = codec->preset->patch(codec); |
| 1495 | goto patched; |
| 1496 | } |
| 1497 | |
| 1498 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1499 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 1500 | if (err < 0) |
| 1501 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1502 | |
| 1503 | patched: |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1504 | if (!err && codec->patch_ops.unsol_event) |
| 1505 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1506 | /* audio codec should override the mixer name */ |
| 1507 | if (!err && (codec->afg || !*codec->bus->card->mixername)) |
| 1508 | snprintf(codec->bus->card->mixername, |
| 1509 | sizeof(codec->bus->card->mixername), |
| 1510 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1511 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1513 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1515 | /* update the stream-id if changed */ |
| 1516 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1517 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1518 | u32 stream_tag, int channel_id) |
| 1519 | { |
| 1520 | unsigned int oldval, newval; |
| 1521 | |
| 1522 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1523 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1524 | newval = (stream_tag << 4) | channel_id; |
| 1525 | if (oldval != newval) |
| 1526 | snd_hda_codec_write(codec, nid, 0, |
| 1527 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1528 | newval); |
| 1529 | p->stream_tag = stream_tag; |
| 1530 | p->channel_id = channel_id; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | /* update the format-id if changed */ |
| 1535 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1536 | hda_nid_t nid, int format) |
| 1537 | { |
| 1538 | unsigned int oldval; |
| 1539 | |
| 1540 | if (p->format_id != format) { |
| 1541 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1542 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1543 | if (oldval != format) { |
| 1544 | msleep(1); |
| 1545 | snd_hda_codec_write(codec, nid, 0, |
| 1546 | AC_VERB_SET_STREAM_FORMAT, |
| 1547 | format); |
| 1548 | } |
| 1549 | p->format_id = format; |
| 1550 | } |
| 1551 | } |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | /** |
| 1554 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1555 | * @codec: the CODEC to set up |
| 1556 | * @nid: the NID to set up |
| 1557 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1558 | * @channel_id: channel id to pass, zero based. |
| 1559 | * @format: stream format. |
| 1560 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1561 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1562 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | int channel_id, int format) |
| 1564 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1565 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1566 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1567 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1568 | int i; |
| 1569 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1570 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1571 | return; |
| 1572 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1573 | snd_printdd("hda_codec_setup_stream: " |
| 1574 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1576 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | affdb62 | 2013-01-07 15:36:45 +0100 | [diff] [blame] | 1577 | if (!p || p->active) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1578 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1579 | |
| 1580 | if (codec->pcm_format_first) |
| 1581 | update_pcm_format(codec, p, nid, format); |
| 1582 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1583 | if (!codec->pcm_format_first) |
| 1584 | update_pcm_format(codec, p, nid, format); |
| 1585 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1586 | p->active = 1; |
| 1587 | p->dirty = 0; |
| 1588 | |
| 1589 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1590 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1591 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1592 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1593 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1594 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1595 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1596 | p->dirty = 1; |
| 1597 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1600 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1602 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1603 | struct hda_cvt_setup *q); |
| 1604 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1605 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1606 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1607 | * @codec: the CODEC to clean up |
| 1608 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1609 | * @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] | 1610 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1611 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1612 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1613 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1614 | struct hda_cvt_setup *p; |
| 1615 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1616 | if (!nid) |
| 1617 | return; |
| 1618 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1619 | if (codec->no_sticky_stream) |
| 1620 | do_now = 1; |
| 1621 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1622 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1623 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | affdb62 | 2013-01-07 15:36:45 +0100 | [diff] [blame] | 1624 | if (p && p->active) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1625 | /* here we just clear the active flag when do_now isn't set; |
| 1626 | * actual clean-ups will be done later in |
| 1627 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1628 | */ |
| 1629 | if (do_now) |
| 1630 | really_cleanup_stream(codec, p); |
| 1631 | else |
| 1632 | p->active = 0; |
| 1633 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1634 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1635 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1636 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1637 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1638 | struct hda_cvt_setup *q) |
| 1639 | { |
| 1640 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1641 | if (q->stream_tag || q->channel_id) |
| 1642 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1643 | if (q->format_id) |
| 1644 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1645 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1646 | memset(q, 0, sizeof(*q)); |
| 1647 | q->nid = nid; |
| 1648 | } |
| 1649 | |
| 1650 | /* clean up the all conflicting obsolete streams */ |
| 1651 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1652 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1653 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1654 | int i; |
| 1655 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1656 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1657 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1658 | struct hda_cvt_setup *p; |
| 1659 | p = snd_array_elem(&c->cvt_setups, i); |
| 1660 | if (p->dirty) |
| 1661 | really_cleanup_stream(c, p); |
| 1662 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1666 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1667 | /* clean up all streams; called from suspend */ |
| 1668 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1669 | { |
| 1670 | int i; |
| 1671 | |
| 1672 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1673 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1674 | if (p->stream_tag) |
| 1675 | really_cleanup_stream(codec, p); |
| 1676 | } |
| 1677 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1678 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | /* |
| 1681 | * amp access functions |
| 1682 | */ |
| 1683 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1684 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1685 | #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] | 1686 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1687 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1688 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1690 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
| 1692 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1693 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1694 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1696 | memset(cache, 0, sizeof(*cache)); |
| 1697 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1698 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1699 | } |
| 1700 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1701 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1702 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1703 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1707 | 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] | 1708 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1709 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1710 | u16 cur = cache->hash[idx]; |
| 1711 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | |
| 1713 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1714 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | if (info->key == key) |
| 1716 | return info; |
| 1717 | cur = info->next; |
| 1718 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1719 | return NULL; |
| 1720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1722 | /* query the hash. allocate an entry if not found. */ |
| 1723 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1724 | u32 key) |
| 1725 | { |
| 1726 | struct hda_cache_head *info = get_hash(cache, key); |
| 1727 | if (!info) { |
| 1728 | u16 idx, cur; |
| 1729 | /* add a new hash entry */ |
| 1730 | info = snd_array_new(&cache->buf); |
| 1731 | if (!info) |
| 1732 | return NULL; |
| 1733 | cur = snd_array_index(&cache->buf, info); |
| 1734 | info->key = key; |
| 1735 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1736 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1737 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1738 | info->next = cache->hash[idx]; |
| 1739 | cache->hash[idx] = cur; |
| 1740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | return info; |
| 1742 | } |
| 1743 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1744 | /* query and allocate an amp hash entry */ |
| 1745 | static inline struct hda_amp_info * |
| 1746 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1747 | { |
| 1748 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1749 | } |
| 1750 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1751 | /* overwrite the value with the key in the caps hash */ |
| 1752 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1753 | { |
| 1754 | struct hda_amp_info *info; |
| 1755 | |
| 1756 | mutex_lock(&codec->hash_mutex); |
| 1757 | info = get_alloc_amp_hash(codec, key); |
| 1758 | if (!info) { |
| 1759 | mutex_unlock(&codec->hash_mutex); |
| 1760 | return -EINVAL; |
| 1761 | } |
| 1762 | info->amp_caps = val; |
| 1763 | info->head.val |= INFO_AMP_CAPS; |
| 1764 | mutex_unlock(&codec->hash_mutex); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | /* query the value from the caps hash; if not found, fetch the current |
| 1769 | * value from the given function and store in the hash |
| 1770 | */ |
| 1771 | static unsigned int |
| 1772 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1773 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1774 | { |
| 1775 | struct hda_amp_info *info; |
| 1776 | unsigned int val; |
| 1777 | |
| 1778 | mutex_lock(&codec->hash_mutex); |
| 1779 | info = get_alloc_amp_hash(codec, key); |
| 1780 | if (!info) { |
| 1781 | mutex_unlock(&codec->hash_mutex); |
| 1782 | return 0; |
| 1783 | } |
| 1784 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1785 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1786 | val = func(codec, nid, dir); |
| 1787 | write_caps_hash(codec, key, val); |
| 1788 | } else { |
| 1789 | val = info->amp_caps; |
| 1790 | mutex_unlock(&codec->hash_mutex); |
| 1791 | } |
| 1792 | return val; |
| 1793 | } |
| 1794 | |
| 1795 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1796 | int direction) |
| 1797 | { |
| 1798 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1799 | nid = codec->afg; |
| 1800 | return snd_hda_param_read(codec, nid, |
| 1801 | direction == HDA_OUTPUT ? |
| 1802 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1803 | } |
| 1804 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1805 | /** |
| 1806 | * query_amp_caps - query AMP capabilities |
| 1807 | * @codec: the HD-auio codec |
| 1808 | * @nid: the NID to query |
| 1809 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1810 | * |
| 1811 | * Query AMP capabilities for the given widget and direction. |
| 1812 | * Returns the obtained capability bits. |
| 1813 | * |
| 1814 | * When cap bits have been already read, this doesn't read again but |
| 1815 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1817 | 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] | 1818 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1819 | return query_caps_hash(codec, nid, direction, |
| 1820 | HDA_HASH_KEY(nid, direction, 0), |
| 1821 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1823 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1825 | /** |
| 1826 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1827 | * @codec: the CODEC to clean up |
| 1828 | * @nid: the NID to clean up |
| 1829 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1830 | * @caps: the capability bits to set |
| 1831 | * |
| 1832 | * Override the cached AMP caps bits value by the given one. |
| 1833 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1834 | * e.g. limit to 0dB, etc. |
| 1835 | * |
| 1836 | * Returns zero if successful or a negative error code. |
| 1837 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1838 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1839 | unsigned int caps) |
| 1840 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1841 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1842 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1843 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1844 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1845 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1846 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1847 | { |
| 1848 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1849 | } |
| 1850 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1851 | /** |
| 1852 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1853 | * @codec: the HD-auio codec |
| 1854 | * @nid: the NID to query |
| 1855 | * |
| 1856 | * Query PIN capabilities for the given widget. |
| 1857 | * Returns the obtained capability bits. |
| 1858 | * |
| 1859 | * When cap bits have been already read, this doesn't read again but |
| 1860 | * returns the cached value. |
| 1861 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1862 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1863 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1864 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1865 | read_pin_cap); |
| 1866 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1867 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 1868 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1869 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1870 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1871 | * @codec: the CODEC |
| 1872 | * @nid: the NID to override |
| 1873 | * @caps: the capability bits to set |
| 1874 | * |
| 1875 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1876 | * |
| 1877 | * Returns zero if successful or a negative error code. |
| 1878 | */ |
| 1879 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1880 | unsigned int caps) |
| 1881 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1882 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1883 | } |
| 1884 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 1885 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1886 | /* read or sync the hash value with the current value; |
| 1887 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1889 | static struct hda_amp_info * |
| 1890 | update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1891 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1893 | struct hda_amp_info *info; |
| 1894 | unsigned int parm, val = 0; |
| 1895 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1897 | retry: |
| 1898 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1899 | if (!info) |
| 1900 | return NULL; |
| 1901 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1902 | if (!val_read) { |
| 1903 | mutex_unlock(&codec->hash_mutex); |
| 1904 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1905 | parm |= direction == HDA_OUTPUT ? |
| 1906 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1907 | parm |= index; |
| 1908 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1909 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1910 | val &= 0xff; |
| 1911 | val_read = true; |
| 1912 | mutex_lock(&codec->hash_mutex); |
| 1913 | goto retry; |
| 1914 | } |
| 1915 | info->vol[ch] = val; |
| 1916 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1917 | } else if (init_only) |
| 1918 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1919 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1923 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1925 | static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1926 | hda_nid_t nid, int ch, int direction, int index, |
| 1927 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | { |
| 1929 | u32 parm; |
| 1930 | |
| 1931 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1932 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1933 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1934 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1935 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1936 | ; /* set the zero value as a fake mute */ |
| 1937 | else |
| 1938 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1940 | } |
| 1941 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1942 | /** |
| 1943 | * snd_hda_codec_amp_read - Read AMP value |
| 1944 | * @codec: HD-audio codec |
| 1945 | * @nid: NID to read the AMP value |
| 1946 | * @ch: channel (left=0 or right=1) |
| 1947 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1948 | * @index: the index value (only for input direction) |
| 1949 | * |
| 1950 | * 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] | 1951 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1952 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1953 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1955 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1956 | unsigned int val = 0; |
| 1957 | |
| 1958 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1959 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1960 | if (info) |
| 1961 | val = info->vol[ch]; |
| 1962 | mutex_unlock(&codec->hash_mutex); |
| 1963 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1965 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1967 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1968 | int direction, int idx, int mask, int val, |
| 1969 | bool init_only) |
| 1970 | { |
| 1971 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1972 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1973 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1974 | |
| 1975 | if (snd_BUG_ON(mask & ~0xff)) |
| 1976 | mask &= 0xff; |
| 1977 | val &= mask; |
| 1978 | |
| 1979 | mutex_lock(&codec->hash_mutex); |
| 1980 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1981 | if (!info) { |
| 1982 | mutex_unlock(&codec->hash_mutex); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | val |= info->vol[ch] & ~mask; |
| 1986 | if (info->vol[ch] == val) { |
| 1987 | mutex_unlock(&codec->hash_mutex); |
| 1988 | return 0; |
| 1989 | } |
| 1990 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1991 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1992 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1993 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1994 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1995 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1996 | return 1; |
| 1997 | } |
| 1998 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1999 | /** |
| 2000 | * snd_hda_codec_amp_update - update the AMP value |
| 2001 | * @codec: HD-audio codec |
| 2002 | * @nid: NID to read the AMP value |
| 2003 | * @ch: channel (left=0 or right=1) |
| 2004 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2005 | * @idx: the index value (only for input direction) |
| 2006 | * @mask: bit mask to set |
| 2007 | * @val: the bits value to set |
| 2008 | * |
| 2009 | * Update the AMP value with a bit mask. |
| 2010 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2011 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2012 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2013 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2015 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2017 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2019 | /** |
| 2020 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2021 | * @codec: HD-audio codec |
| 2022 | * @nid: NID to read the AMP value |
| 2023 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2024 | * @idx: the index value (only for input direction) |
| 2025 | * @mask: bit mask to set |
| 2026 | * @val: the bits value to set |
| 2027 | * |
| 2028 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2029 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2030 | */ |
| 2031 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2032 | int direction, int idx, int mask, int val) |
| 2033 | { |
| 2034 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2035 | |
| 2036 | if (snd_BUG_ON(mask & ~0xff)) |
| 2037 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2038 | for (ch = 0; ch < 2; ch++) |
| 2039 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2040 | idx, mask, val); |
| 2041 | return ret; |
| 2042 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2043 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2044 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2045 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2046 | * the first access. If the amp was already initialized / updated beforehand, |
| 2047 | * this does nothing. |
| 2048 | */ |
| 2049 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2050 | int dir, int idx, int mask, int val) |
| 2051 | { |
| 2052 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2053 | } |
| 2054 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); |
| 2055 | |
| 2056 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2057 | int dir, int idx, int mask, int val) |
| 2058 | { |
| 2059 | int ch, ret = 0; |
| 2060 | |
| 2061 | if (snd_BUG_ON(mask & ~0xff)) |
| 2062 | mask &= 0xff; |
| 2063 | for (ch = 0; ch < 2; ch++) |
| 2064 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2065 | idx, mask, val); |
| 2066 | return ret; |
| 2067 | } |
| 2068 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); |
| 2069 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2070 | /** |
| 2071 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2072 | * @codec: HD-audio codec |
| 2073 | * |
| 2074 | * Resume the all amp commands from the cache. |
| 2075 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2076 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2077 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2078 | int i; |
| 2079 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2080 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2081 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2082 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2083 | struct hda_amp_info *buffer; |
| 2084 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2085 | hda_nid_t nid; |
| 2086 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2087 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2088 | |
| 2089 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2090 | if (!buffer->head.dirty) |
| 2091 | continue; |
| 2092 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2093 | info = *buffer; |
| 2094 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2095 | if (!key) |
| 2096 | continue; |
| 2097 | nid = key & 0xff; |
| 2098 | idx = (key >> 16) & 0xff; |
| 2099 | dir = (key >> 24) & 0xff; |
| 2100 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2101 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2102 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2103 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2104 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2105 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2106 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2107 | } |
| 2108 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2109 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2110 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2111 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2113 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2114 | unsigned int ofs) |
| 2115 | { |
| 2116 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2117 | /* get num steps */ |
| 2118 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2119 | if (ofs < caps) |
| 2120 | caps -= ofs; |
| 2121 | return caps; |
| 2122 | } |
| 2123 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2124 | /** |
| 2125 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2126 | * |
| 2127 | * The control element is supposed to have the private_value field |
| 2128 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2129 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2130 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2131 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | { |
| 2133 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2134 | u16 nid = get_amp_nid(kcontrol); |
| 2135 | u8 chs = get_amp_channels(kcontrol); |
| 2136 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2137 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2139 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2140 | uinfo->count = chs == 3 ? 2 : 1; |
| 2141 | uinfo->value.integer.min = 0; |
| 2142 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2143 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2144 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2145 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2146 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | return -EINVAL; |
| 2148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | return 0; |
| 2150 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2151 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2153 | |
| 2154 | static inline unsigned int |
| 2155 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2156 | int ch, int dir, int idx, unsigned int ofs) |
| 2157 | { |
| 2158 | unsigned int val; |
| 2159 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2160 | val &= HDA_AMP_VOLMASK; |
| 2161 | if (val >= ofs) |
| 2162 | val -= ofs; |
| 2163 | else |
| 2164 | val = 0; |
| 2165 | return val; |
| 2166 | } |
| 2167 | |
| 2168 | static inline int |
| 2169 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2170 | int ch, int dir, int idx, unsigned int ofs, |
| 2171 | unsigned int val) |
| 2172 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2173 | unsigned int maxval; |
| 2174 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2175 | if (val > 0) |
| 2176 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2177 | /* ofs = 0: raw max value */ |
| 2178 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2179 | if (val > maxval) |
| 2180 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2181 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2182 | HDA_AMP_VOLMASK, val); |
| 2183 | } |
| 2184 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2185 | /** |
| 2186 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2187 | * |
| 2188 | * The control element is supposed to have the private_value field |
| 2189 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2190 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2191 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2192 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
| 2194 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2195 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2196 | int chs = get_amp_channels(kcontrol); |
| 2197 | int dir = get_amp_direction(kcontrol); |
| 2198 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2199 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | long *valp = ucontrol->value.integer.value; |
| 2201 | |
| 2202 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2203 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2205 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2208 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2210 | /** |
| 2211 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2212 | * |
| 2213 | * The control element is supposed to have the private_value field |
| 2214 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2215 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2216 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2217 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2220 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2221 | int chs = get_amp_channels(kcontrol); |
| 2222 | int dir = get_amp_direction(kcontrol); |
| 2223 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2224 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | long *valp = ucontrol->value.integer.value; |
| 2226 | int change = 0; |
| 2227 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2228 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2229 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2230 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2231 | valp++; |
| 2232 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2233 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2234 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2235 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | return change; |
| 2237 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2238 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2240 | /** |
| 2241 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2242 | * |
| 2243 | * The control element is supposed to have the private_value field |
| 2244 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2245 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2246 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2247 | unsigned int size, unsigned int __user *_tlv) |
| 2248 | { |
| 2249 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2250 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2251 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2252 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2253 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2254 | u32 caps, val1, val2; |
| 2255 | |
| 2256 | if (size < 4 * sizeof(unsigned int)) |
| 2257 | return -ENOMEM; |
| 2258 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2259 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2260 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2261 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2262 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2263 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2264 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2265 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2266 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2267 | return -EFAULT; |
| 2268 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2269 | return -EFAULT; |
| 2270 | if (put_user(val1, _tlv + 2)) |
| 2271 | return -EFAULT; |
| 2272 | if (put_user(val2, _tlv + 3)) |
| 2273 | return -EFAULT; |
| 2274 | return 0; |
| 2275 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2276 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2277 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2278 | /** |
| 2279 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2280 | * @codec: HD-audio codec |
| 2281 | * @nid: NID of a reference widget |
| 2282 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2283 | * @tlv: TLV data to be stored, at least 4 elements |
| 2284 | * |
| 2285 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2286 | * obtained from the reference NID. |
| 2287 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2288 | */ |
| 2289 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2290 | unsigned int *tlv) |
| 2291 | { |
| 2292 | u32 caps; |
| 2293 | int nums, step; |
| 2294 | |
| 2295 | caps = query_amp_caps(codec, nid, dir); |
| 2296 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2297 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2298 | step = (step + 1) * 25; |
| 2299 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2300 | tlv[1] = 2 * sizeof(unsigned int); |
| 2301 | tlv[2] = -nums * step; |
| 2302 | tlv[3] = step; |
| 2303 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2304 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2305 | |
| 2306 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2307 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2308 | 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] | 2309 | { |
| 2310 | struct snd_ctl_elem_id id; |
| 2311 | memset(&id, 0, sizeof(id)); |
| 2312 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2313 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2314 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2315 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2316 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2317 | strcpy(id.name, name); |
| 2318 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2319 | } |
| 2320 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2321 | /** |
| 2322 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2323 | * @codec: HD-audio codec |
| 2324 | * @name: ctl id name string |
| 2325 | * |
| 2326 | * Get the control element with the given id string and IFACE_MIXER. |
| 2327 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2328 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2329 | const char *name) |
| 2330 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2331 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2332 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2333 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2334 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2335 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2336 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2337 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2338 | int i, idx; |
| 2339 | /* 16 ctlrs should be large enough */ |
| 2340 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2341 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2342 | return idx; |
| 2343 | } |
| 2344 | return -EBUSY; |
| 2345 | } |
| 2346 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2347 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2348 | * 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] | 2349 | * @codec: HD-audio codec |
| 2350 | * @nid: corresponding NID (optional) |
| 2351 | * @kctl: the control element to assign |
| 2352 | * |
| 2353 | * Add the given control element to an array inside the codec instance. |
| 2354 | * All control elements belonging to a codec are supposed to be added |
| 2355 | * by this function so that a proper clean-up works at the free or |
| 2356 | * reconfiguration time. |
| 2357 | * |
| 2358 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2359 | * The assignment is shown in the codec proc file. |
| 2360 | * |
| 2361 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2362 | * #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] | 2363 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2364 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2365 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2366 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2367 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2368 | { |
| 2369 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2370 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2371 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2372 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2373 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2374 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2375 | if (nid == 0) |
| 2376 | nid = get_amp_nid_(kctl->private_value); |
| 2377 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2378 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2379 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2380 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2381 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2382 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2383 | if (err < 0) |
| 2384 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2385 | item = snd_array_new(&codec->mixers); |
| 2386 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2387 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2388 | item->kctl = kctl; |
| 2389 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2390 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2391 | return 0; |
| 2392 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2393 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2394 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2395 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2396 | * snd_hda_add_nid - Assign a NID to a control element |
| 2397 | * @codec: HD-audio codec |
| 2398 | * @nid: corresponding NID (optional) |
| 2399 | * @kctl: the control element to assign |
| 2400 | * @index: index to kctl |
| 2401 | * |
| 2402 | * Add the given control element to an array inside the codec instance. |
| 2403 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2404 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2405 | */ |
| 2406 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2407 | unsigned int index, hda_nid_t nid) |
| 2408 | { |
| 2409 | struct hda_nid_item *item; |
| 2410 | |
| 2411 | if (nid > 0) { |
| 2412 | item = snd_array_new(&codec->nids); |
| 2413 | if (!item) |
| 2414 | return -ENOMEM; |
| 2415 | item->kctl = kctl; |
| 2416 | item->index = index; |
| 2417 | item->nid = nid; |
| 2418 | return 0; |
| 2419 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2420 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2421 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2422 | return -EINVAL; |
| 2423 | } |
| 2424 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2425 | |
| 2426 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2427 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2428 | * @codec: HD-audio codec |
| 2429 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2430 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2431 | { |
| 2432 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2433 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2434 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2435 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2436 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2437 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2440 | /* pseudo device locking |
| 2441 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2442 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2443 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2444 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2445 | struct snd_card *card = bus->card; |
| 2446 | struct hda_codec *codec; |
| 2447 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2448 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2449 | if (card->shutdown) |
| 2450 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2451 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2452 | if (!list_empty(&card->ctl_files)) |
| 2453 | goto err_clear; |
| 2454 | |
| 2455 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2456 | int pcm; |
| 2457 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2458 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2459 | if (!cpcm->pcm) |
| 2460 | continue; |
| 2461 | if (cpcm->pcm->streams[0].substream_opened || |
| 2462 | cpcm->pcm->streams[1].substream_opened) |
| 2463 | goto err_clear; |
| 2464 | } |
| 2465 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2466 | spin_unlock(&card->files_lock); |
| 2467 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2468 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2469 | err_clear: |
| 2470 | card->shutdown = 0; |
| 2471 | err_unlock: |
| 2472 | spin_unlock(&card->files_lock); |
| 2473 | return -EINVAL; |
| 2474 | } |
| 2475 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); |
| 2476 | |
| 2477 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2478 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2479 | struct snd_card *card = bus->card; |
| 2480 | |
| 2481 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2482 | spin_lock(&card->files_lock); |
| 2483 | card->shutdown = 0; |
| 2484 | spin_unlock(&card->files_lock); |
| 2485 | } |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2486 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2487 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2488 | /** |
| 2489 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2490 | * @codec: HD-audio codec |
| 2491 | * |
| 2492 | * This frees the all PCM and control elements assigned to the codec, and |
| 2493 | * clears the caches and restores the pin default configurations. |
| 2494 | * |
| 2495 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2496 | * returns zero. |
| 2497 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2498 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2499 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2500 | struct hda_bus *bus = codec->bus; |
| 2501 | struct snd_card *card = bus->card; |
| 2502 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2503 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2504 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2505 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2508 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2509 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2510 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | 339876d | 2012-05-08 16:57:12 +0200 | [diff] [blame] | 2511 | codec->power_on = 0; |
| 2512 | codec->power_transition = 0; |
| 2513 | codec->power_jiffies = jiffies; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2514 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2515 | #endif |
| 2516 | snd_hda_ctls_clear(codec); |
| 2517 | /* relase PCMs */ |
| 2518 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2519 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2520 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2521 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2522 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2523 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2524 | } |
| 2525 | if (codec->patch_ops.free) |
| 2526 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2527 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2528 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2529 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2530 | codec->spec = NULL; |
| 2531 | free_hda_cache(&codec->amp_cache); |
| 2532 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2533 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2534 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2535 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2536 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2537 | snd_array_free(&codec->cvt_setups); |
| 2538 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2539 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2540 | codec->num_pcms = 0; |
| 2541 | codec->pcm_info = NULL; |
| 2542 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2543 | codec->slave_dig_outs = NULL; |
| 2544 | codec->spdif_status_reset = 0; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2545 | module_put(codec->owner); |
| 2546 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2547 | |
| 2548 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2549 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2550 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2551 | } |
| 2552 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2553 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2554 | |
| 2555 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2556 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2557 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2558 | { |
| 2559 | struct hda_nid_item *items; |
| 2560 | const char * const *s; |
| 2561 | int i, err; |
| 2562 | |
| 2563 | items = codec->mixers.list; |
| 2564 | for (i = 0; i < codec->mixers.used; i++) { |
| 2565 | struct snd_kcontrol *sctl = items[i].kctl; |
| 2566 | if (!sctl || !sctl->id.name || |
| 2567 | sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
| 2568 | continue; |
| 2569 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2570 | char tmpname[sizeof(sctl->id.name)]; |
| 2571 | const char *name = *s; |
| 2572 | if (suffix) { |
| 2573 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2574 | name, suffix); |
| 2575 | name = tmpname; |
| 2576 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2577 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2578 | err = func(data, sctl); |
| 2579 | if (err) |
| 2580 | return err; |
| 2581 | break; |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2589 | { |
| 2590 | return 1; |
| 2591 | } |
| 2592 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2593 | /* guess the value corresponding to 0dB */ |
| 2594 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl) |
| 2595 | { |
| 2596 | int _tlv[4]; |
| 2597 | const int *tlv = NULL; |
| 2598 | int val = -1; |
| 2599 | |
| 2600 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2601 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2602 | mm_segment_t fs = get_fs(); |
| 2603 | set_fs(get_ds()); |
| 2604 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2605 | tlv = _tlv; |
| 2606 | set_fs(fs); |
| 2607 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2608 | tlv = kctl->tlv.p; |
| 2609 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) |
| 2610 | val = -tlv[2] / tlv[3]; |
| 2611 | return val; |
| 2612 | } |
| 2613 | |
| 2614 | /* call kctl->put with the given value(s) */ |
| 2615 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2616 | { |
| 2617 | struct snd_ctl_elem_value *ucontrol; |
| 2618 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2619 | if (!ucontrol) |
| 2620 | return -ENOMEM; |
| 2621 | ucontrol->value.integer.value[0] = val; |
| 2622 | ucontrol->value.integer.value[1] = val; |
| 2623 | kctl->put(kctl, ucontrol); |
| 2624 | kfree(ucontrol); |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
| 2628 | /* initialize the slave volume with 0dB */ |
| 2629 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2630 | { |
| 2631 | int offset = get_kctl_0dB_offset(slave); |
| 2632 | if (offset > 0) |
| 2633 | put_kctl_with_value(slave, offset); |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
| 2637 | /* unmute the slave */ |
| 2638 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2639 | { |
| 2640 | return put_kctl_with_value(slave, 1); |
| 2641 | } |
| 2642 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2643 | /** |
| 2644 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2645 | * @codec: HD-audio codec |
| 2646 | * @name: vmaster control name |
| 2647 | * @tlv: TLV data (optional) |
| 2648 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2649 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2650 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2651 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2652 | * |
| 2653 | * Create a virtual master control with the given name. The TLV data |
| 2654 | * must be either NULL or a valid data. |
| 2655 | * |
| 2656 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2657 | * slave control name. All controls with these names are assigned to |
| 2658 | * the new virtual master control. |
| 2659 | * |
| 2660 | * This function returns zero if successful or a negative error code. |
| 2661 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2662 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2663 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2664 | const char *suffix, bool init_slave_vol, |
| 2665 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2666 | { |
| 2667 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2668 | int err; |
| 2669 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2670 | if (ctl_ret) |
| 2671 | *ctl_ret = NULL; |
| 2672 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2673 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2674 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2675 | snd_printdd("No slave found for %s\n", name); |
| 2676 | return 0; |
| 2677 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2678 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2679 | if (!kctl) |
| 2680 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2681 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2682 | if (err < 0) |
| 2683 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2684 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2685 | err = map_slaves(codec, slaves, suffix, |
| 2686 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2687 | if (err < 0) |
| 2688 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2689 | |
| 2690 | /* init with master mute & zero volume */ |
| 2691 | put_kctl_with_value(kctl, 0); |
| 2692 | if (init_slave_vol) |
| 2693 | map_slaves(codec, slaves, suffix, |
| 2694 | tlv ? init_slave_0dB : init_slave_unmute, kctl); |
| 2695 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2696 | if (ctl_ret) |
| 2697 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2698 | return 0; |
| 2699 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2700 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2701 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2702 | /* |
| 2703 | * mute-LED control using vmaster |
| 2704 | */ |
| 2705 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2706 | struct snd_ctl_elem_info *uinfo) |
| 2707 | { |
| 2708 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2709 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2710 | }; |
| 2711 | unsigned int index; |
| 2712 | |
| 2713 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2714 | uinfo->count = 1; |
| 2715 | uinfo->value.enumerated.items = 3; |
| 2716 | index = uinfo->value.enumerated.item; |
| 2717 | if (index >= 3) |
| 2718 | index = 2; |
| 2719 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2724 | struct snd_ctl_elem_value *ucontrol) |
| 2725 | { |
| 2726 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2727 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2728 | return 0; |
| 2729 | } |
| 2730 | |
| 2731 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2732 | struct snd_ctl_elem_value *ucontrol) |
| 2733 | { |
| 2734 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2735 | unsigned int old_mode = hook->mute_mode; |
| 2736 | |
| 2737 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2738 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2739 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2740 | if (old_mode == hook->mute_mode) |
| 2741 | return 0; |
| 2742 | snd_hda_sync_vmaster_hook(hook); |
| 2743 | return 1; |
| 2744 | } |
| 2745 | |
| 2746 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2747 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2748 | .name = "Mute-LED Mode", |
| 2749 | .info = vmaster_mute_mode_info, |
| 2750 | .get = vmaster_mute_mode_get, |
| 2751 | .put = vmaster_mute_mode_put, |
| 2752 | }; |
| 2753 | |
| 2754 | /* |
| 2755 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2756 | * "Mute-LED Mode" control is automatically created and associated with |
| 2757 | * the given hook. |
| 2758 | */ |
| 2759 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2760 | struct hda_vmaster_mute_hook *hook, |
| 2761 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2762 | { |
| 2763 | struct snd_kcontrol *kctl; |
| 2764 | |
| 2765 | if (!hook->hook || !hook->sw_kctl) |
| 2766 | return 0; |
| 2767 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2768 | hook->codec = codec; |
| 2769 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2770 | if (!expose_enum_ctl) |
| 2771 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2772 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2773 | if (!kctl) |
| 2774 | return -ENOMEM; |
| 2775 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2776 | } |
| 2777 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); |
| 2778 | |
| 2779 | /* |
| 2780 | * Call the hook with the current value for synchronization |
| 2781 | * Should be called in init callback |
| 2782 | */ |
| 2783 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2784 | { |
| 2785 | if (!hook->hook || !hook->codec) |
| 2786 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame^] | 2787 | /* don't call vmaster hook in the destructor since it might have |
| 2788 | * been already destroyed |
| 2789 | */ |
| 2790 | if (hook->codec->bus->shutdown) |
| 2791 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2792 | switch (hook->mute_mode) { |
| 2793 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2794 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2795 | break; |
| 2796 | default: |
| 2797 | hook->hook(hook->codec, hook->mute_mode); |
| 2798 | break; |
| 2799 | } |
| 2800 | } |
| 2801 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); |
| 2802 | |
| 2803 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2804 | /** |
| 2805 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2806 | * |
| 2807 | * The control element is supposed to have the private_value field |
| 2808 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2809 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2810 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2811 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | { |
| 2813 | int chs = get_amp_channels(kcontrol); |
| 2814 | |
| 2815 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2816 | uinfo->count = chs == 3 ? 2 : 1; |
| 2817 | uinfo->value.integer.min = 0; |
| 2818 | uinfo->value.integer.max = 1; |
| 2819 | return 0; |
| 2820 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2821 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2823 | /** |
| 2824 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2825 | * |
| 2826 | * The control element is supposed to have the private_value field |
| 2827 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2828 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2829 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2830 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | { |
| 2832 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2833 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2834 | int chs = get_amp_channels(kcontrol); |
| 2835 | int dir = get_amp_direction(kcontrol); |
| 2836 | int idx = get_amp_index(kcontrol); |
| 2837 | long *valp = ucontrol->value.integer.value; |
| 2838 | |
| 2839 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2840 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2841 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2843 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2844 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | return 0; |
| 2846 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2847 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2849 | /** |
| 2850 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2851 | * |
| 2852 | * The control element is supposed to have the private_value field |
| 2853 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2854 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2855 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2856 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | { |
| 2858 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2859 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2860 | int chs = get_amp_channels(kcontrol); |
| 2861 | int dir = get_amp_direction(kcontrol); |
| 2862 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | long *valp = ucontrol->value.integer.value; |
| 2864 | int change = 0; |
| 2865 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2866 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2867 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2868 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2869 | HDA_AMP_MUTE, |
| 2870 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2871 | valp++; |
| 2872 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2873 | if (chs & 2) |
| 2874 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2875 | HDA_AMP_MUTE, |
| 2876 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2877 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2878 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | return change; |
| 2880 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2881 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | |
| 2883 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2884 | * bound volume controls |
| 2885 | * |
| 2886 | * bind multiple volumes (# indices, from 0) |
| 2887 | */ |
| 2888 | |
| 2889 | #define AMP_VAL_IDX_SHIFT 19 |
| 2890 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2891 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2892 | /** |
| 2893 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 2894 | * |
| 2895 | * The control element is supposed to have the private_value field |
| 2896 | * set up via HDA_BIND_MUTE*() macros. |
| 2897 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2898 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2899 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2900 | { |
| 2901 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2902 | unsigned long pval; |
| 2903 | int err; |
| 2904 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2905 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2906 | pval = kcontrol->private_value; |
| 2907 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2908 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2909 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2910 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2911 | return err; |
| 2912 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2913 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2914 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2915 | /** |
| 2916 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 2917 | * |
| 2918 | * The control element is supposed to have the private_value field |
| 2919 | * set up via HDA_BIND_MUTE*() macros. |
| 2920 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2921 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2922 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2923 | { |
| 2924 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2925 | unsigned long pval; |
| 2926 | int i, indices, err = 0, change = 0; |
| 2927 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2928 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2929 | pval = kcontrol->private_value; |
| 2930 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2931 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2932 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2933 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2934 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2935 | if (err < 0) |
| 2936 | break; |
| 2937 | change |= err; |
| 2938 | } |
| 2939 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2940 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2941 | return err < 0 ? err : change; |
| 2942 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2943 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2944 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2945 | /** |
| 2946 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 2947 | * |
| 2948 | * The control element is supposed to have the private_value field |
| 2949 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2950 | */ |
| 2951 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2952 | struct snd_ctl_elem_info *uinfo) |
| 2953 | { |
| 2954 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2955 | struct hda_bind_ctls *c; |
| 2956 | int err; |
| 2957 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2958 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2959 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2960 | kcontrol->private_value = *c->values; |
| 2961 | err = c->ops->info(kcontrol, uinfo); |
| 2962 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2963 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2964 | return err; |
| 2965 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2966 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2967 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2968 | /** |
| 2969 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 2970 | * |
| 2971 | * The control element is supposed to have the private_value field |
| 2972 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2973 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2974 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2975 | struct snd_ctl_elem_value *ucontrol) |
| 2976 | { |
| 2977 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2978 | struct hda_bind_ctls *c; |
| 2979 | int err; |
| 2980 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2981 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2982 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2983 | kcontrol->private_value = *c->values; |
| 2984 | err = c->ops->get(kcontrol, ucontrol); |
| 2985 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2986 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2987 | return err; |
| 2988 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2989 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2990 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2991 | /** |
| 2992 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 2993 | * |
| 2994 | * The control element is supposed to have the private_value field |
| 2995 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2996 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2997 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2998 | struct snd_ctl_elem_value *ucontrol) |
| 2999 | { |
| 3000 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3001 | struct hda_bind_ctls *c; |
| 3002 | unsigned long *vals; |
| 3003 | int err = 0, change = 0; |
| 3004 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3005 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3006 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3007 | for (vals = c->values; *vals; vals++) { |
| 3008 | kcontrol->private_value = *vals; |
| 3009 | err = c->ops->put(kcontrol, ucontrol); |
| 3010 | if (err < 0) |
| 3011 | break; |
| 3012 | change |= err; |
| 3013 | } |
| 3014 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3015 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3016 | return err < 0 ? err : change; |
| 3017 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3018 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3019 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3020 | /** |
| 3021 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3022 | * |
| 3023 | * The control element is supposed to have the private_value field |
| 3024 | * set up via HDA_BIND_VOL() macro. |
| 3025 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3026 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3027 | unsigned int size, unsigned int __user *tlv) |
| 3028 | { |
| 3029 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3030 | struct hda_bind_ctls *c; |
| 3031 | int err; |
| 3032 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3033 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3034 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3035 | kcontrol->private_value = *c->values; |
| 3036 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3037 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3038 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3039 | return err; |
| 3040 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3041 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3042 | |
| 3043 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3044 | .info = snd_hda_mixer_amp_volume_info, |
| 3045 | .get = snd_hda_mixer_amp_volume_get, |
| 3046 | .put = snd_hda_mixer_amp_volume_put, |
| 3047 | .tlv = snd_hda_mixer_amp_tlv |
| 3048 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3049 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3050 | |
| 3051 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3052 | .info = snd_hda_mixer_amp_switch_info, |
| 3053 | .get = snd_hda_mixer_amp_switch_get, |
| 3054 | .put = snd_hda_mixer_amp_switch_put, |
| 3055 | .tlv = snd_hda_mixer_amp_tlv |
| 3056 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3057 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3058 | |
| 3059 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | * SPDIF out controls |
| 3061 | */ |
| 3062 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3063 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3064 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | { |
| 3066 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3067 | uinfo->count = 1; |
| 3068 | return 0; |
| 3069 | } |
| 3070 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3071 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3072 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | { |
| 3074 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3075 | IEC958_AES0_NONAUDIO | |
| 3076 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3077 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3078 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3079 | IEC958_AES1_CON_ORIGINAL; |
| 3080 | return 0; |
| 3081 | } |
| 3082 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3084 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | { |
| 3086 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3087 | IEC958_AES0_NONAUDIO | |
| 3088 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3092 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3093 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | { |
| 3095 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3096 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3097 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3099 | mutex_lock(&codec->spdif_mutex); |
| 3100 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3101 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3102 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3103 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3104 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3105 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | return 0; |
| 3108 | } |
| 3109 | |
| 3110 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3111 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3112 | */ |
| 3113 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3114 | { |
| 3115 | unsigned short val = 0; |
| 3116 | |
| 3117 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3118 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3120 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3122 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3123 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3124 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3126 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3127 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3128 | val |= AC_DIG1_EMPHASIS; |
| 3129 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3130 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3132 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3134 | } |
| 3135 | return val; |
| 3136 | } |
| 3137 | |
| 3138 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3139 | */ |
| 3140 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3141 | { |
| 3142 | unsigned int sbits = 0; |
| 3143 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3144 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3146 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3148 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3149 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3151 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3152 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3154 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3156 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3158 | sbits |= val & (0x7f << 8); |
| 3159 | } |
| 3160 | return sbits; |
| 3161 | } |
| 3162 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3163 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3164 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3165 | int verb, int val) |
| 3166 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3167 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3168 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3169 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3170 | d = codec->slave_dig_outs; |
| 3171 | if (!d) |
| 3172 | return; |
| 3173 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3174 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3178 | int dig1, int dig2) |
| 3179 | { |
| 3180 | if (dig1 != -1) |
| 3181 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3182 | if (dig2 != -1) |
| 3183 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3184 | } |
| 3185 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3186 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3187 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | { |
| 3189 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3190 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3191 | struct hda_spdif_out *spdif; |
| 3192 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | unsigned short val; |
| 3194 | int change; |
| 3195 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3196 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3197 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3198 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3199 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3201 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3202 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3203 | val = convert_from_spdif_status(spdif->status); |
| 3204 | val |= spdif->ctls & 1; |
| 3205 | change = spdif->ctls != val; |
| 3206 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3207 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3208 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3209 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | return change; |
| 3211 | } |
| 3212 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3213 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3215 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3216 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | { |
| 3218 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3219 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3220 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3222 | mutex_lock(&codec->spdif_mutex); |
| 3223 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3224 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3225 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | return 0; |
| 3227 | } |
| 3228 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3229 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3230 | int dig1, int dig2) |
| 3231 | { |
| 3232 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3233 | /* unmute amp switch (if any) */ |
| 3234 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3235 | (dig1 & AC_DIG1_ENABLE)) |
| 3236 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3237 | HDA_AMP_MUTE, 0); |
| 3238 | } |
| 3239 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3240 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3241 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | { |
| 3243 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3244 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3245 | struct hda_spdif_out *spdif; |
| 3246 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | unsigned short val; |
| 3248 | int change; |
| 3249 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3250 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3251 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3252 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3253 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3255 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3256 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3257 | spdif->ctls = val; |
| 3258 | if (change && nid != (u16)-1) |
| 3259 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3260 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | return change; |
| 3262 | } |
| 3263 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3264 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
| 3266 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3267 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3268 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | .info = snd_hda_spdif_mask_info, |
| 3270 | .get = snd_hda_spdif_cmask_get, |
| 3271 | }, |
| 3272 | { |
| 3273 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3274 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3275 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | .info = snd_hda_spdif_mask_info, |
| 3277 | .get = snd_hda_spdif_pmask_get, |
| 3278 | }, |
| 3279 | { |
| 3280 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3281 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | .info = snd_hda_spdif_mask_info, |
| 3283 | .get = snd_hda_spdif_default_get, |
| 3284 | .put = snd_hda_spdif_default_put, |
| 3285 | }, |
| 3286 | { |
| 3287 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3288 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | .info = snd_hda_spdif_out_switch_info, |
| 3290 | .get = snd_hda_spdif_out_switch_get, |
| 3291 | .put = snd_hda_spdif_out_switch_put, |
| 3292 | }, |
| 3293 | { } /* end */ |
| 3294 | }; |
| 3295 | |
| 3296 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3297 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3299 | * @associated_nid: NID that new ctls associated with |
| 3300 | * @cvt_nid: converter NID |
| 3301 | * @type: HDA_PCM_TYPE_* |
| 3302 | * Creates controls related with the digital output. |
| 3303 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | * |
| 3305 | * Returns 0 if successful, or a negative error code. |
| 3306 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3307 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3308 | hda_nid_t associated_nid, |
| 3309 | hda_nid_t cvt_nid, |
| 3310 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | { |
| 3312 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3313 | struct snd_kcontrol *kctl; |
| 3314 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3315 | int idx = 0; |
| 3316 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3317 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3318 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3320 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3321 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3322 | idx = spdif_index; |
| 3323 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3324 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3325 | /* suppose a single SPDIF device */ |
| 3326 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3327 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3328 | if (!kctl) |
| 3329 | break; |
| 3330 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3331 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3332 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3333 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3334 | if (!bus->primary_dig_out_type) |
| 3335 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3336 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3337 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3338 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3339 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3340 | return -EBUSY; |
| 3341 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3342 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3343 | if (!spdif) |
| 3344 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3346 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3347 | if (!kctl) |
| 3348 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3349 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3350 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3351 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3352 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | return err; |
| 3354 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3355 | spdif->nid = cvt_nid; |
| 3356 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3357 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3358 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | return 0; |
| 3360 | } |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3361 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3363 | /* get the hda_spdif_out entry from the given NID |
| 3364 | * call within spdif_mutex lock |
| 3365 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3366 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3367 | hda_nid_t nid) |
| 3368 | { |
| 3369 | int i; |
| 3370 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3371 | struct hda_spdif_out *spdif = |
| 3372 | snd_array_elem(&codec->spdif_out, i); |
| 3373 | if (spdif->nid == nid) |
| 3374 | return spdif; |
| 3375 | } |
| 3376 | return NULL; |
| 3377 | } |
| 3378 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3379 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3380 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3381 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3382 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3383 | |
| 3384 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3385 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3386 | spdif->nid = (u16)-1; |
| 3387 | mutex_unlock(&codec->spdif_mutex); |
| 3388 | } |
| 3389 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3390 | |
| 3391 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3392 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3393 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3394 | unsigned short val; |
| 3395 | |
| 3396 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3397 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3398 | if (spdif->nid != nid) { |
| 3399 | spdif->nid = nid; |
| 3400 | val = spdif->ctls; |
| 3401 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3402 | } |
| 3403 | mutex_unlock(&codec->spdif_mutex); |
| 3404 | } |
| 3405 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3408 | * SPDIF sharing with analog output |
| 3409 | */ |
| 3410 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3411 | struct snd_ctl_elem_value *ucontrol) |
| 3412 | { |
| 3413 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3414 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3415 | return 0; |
| 3416 | } |
| 3417 | |
| 3418 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3419 | struct snd_ctl_elem_value *ucontrol) |
| 3420 | { |
| 3421 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3422 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3423 | return 0; |
| 3424 | } |
| 3425 | |
| 3426 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3427 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3428 | .name = "IEC958 Default PCM Playback Switch", |
| 3429 | .info = snd_ctl_boolean_mono_info, |
| 3430 | .get = spdif_share_sw_get, |
| 3431 | .put = spdif_share_sw_put, |
| 3432 | }; |
| 3433 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3434 | /** |
| 3435 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3436 | * @codec: the HDA codec |
| 3437 | * @mout: multi-out instance |
| 3438 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3439 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3440 | struct hda_multi_out *mout) |
| 3441 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3442 | struct snd_kcontrol *kctl; |
| 3443 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3444 | if (!mout->dig_out_nid) |
| 3445 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3446 | |
| 3447 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3448 | if (!kctl) |
| 3449 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3450 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3451 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3452 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3453 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | * SPDIF input |
| 3457 | */ |
| 3458 | |
| 3459 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3460 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3461 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3462 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | { |
| 3464 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3465 | |
| 3466 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3470 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3471 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | { |
| 3473 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3474 | hda_nid_t nid = kcontrol->private_value; |
| 3475 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3476 | int change; |
| 3477 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3478 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3480 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3482 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3483 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3485 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | return change; |
| 3487 | } |
| 3488 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3489 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3490 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | { |
| 3492 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3493 | hda_nid_t nid = kcontrol->private_value; |
| 3494 | unsigned short val; |
| 3495 | unsigned int sbits; |
| 3496 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3497 | 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] | 3498 | sbits = convert_to_spdif_status(val); |
| 3499 | ucontrol->value.iec958.status[0] = sbits; |
| 3500 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3501 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3502 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3506 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | { |
| 3508 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3509 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | .info = snd_hda_spdif_in_switch_info, |
| 3511 | .get = snd_hda_spdif_in_switch_get, |
| 3512 | .put = snd_hda_spdif_in_switch_put, |
| 3513 | }, |
| 3514 | { |
| 3515 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3516 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3517 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | .info = snd_hda_spdif_mask_info, |
| 3519 | .get = snd_hda_spdif_in_status_get, |
| 3520 | }, |
| 3521 | { } /* end */ |
| 3522 | }; |
| 3523 | |
| 3524 | /** |
| 3525 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3526 | * @codec: the HDA codec |
| 3527 | * @nid: audio in widget NID |
| 3528 | * |
| 3529 | * Creates controls related with the SPDIF input. |
| 3530 | * Called from each patch supporting the SPDIF in. |
| 3531 | * |
| 3532 | * Returns 0 if successful, or a negative error code. |
| 3533 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3534 | 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] | 3535 | { |
| 3536 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3537 | struct snd_kcontrol *kctl; |
| 3538 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3539 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3541 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3542 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3543 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3544 | return -EBUSY; |
| 3545 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3547 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3548 | if (!kctl) |
| 3549 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3551 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3552 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | return err; |
| 3554 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3555 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3556 | snd_hda_codec_read(codec, nid, 0, |
| 3557 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3558 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | return 0; |
| 3560 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3561 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3563 | /* |
| 3564 | * command cache |
| 3565 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3567 | /* build a 31bit cache key with the widget id and the command parameter */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3568 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3569 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3570 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3571 | |
| 3572 | /** |
| 3573 | * snd_hda_codec_write_cache - send a single command with caching |
| 3574 | * @codec: the HDA codec |
| 3575 | * @nid: NID to send the command |
| 3576 | * @direct: direct flag |
| 3577 | * @verb: the verb to send |
| 3578 | * @parm: the parameter for the verb |
| 3579 | * |
| 3580 | * Send a single command without waiting for response. |
| 3581 | * |
| 3582 | * Returns 0 if successful, or a negative error code. |
| 3583 | */ |
| 3584 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3585 | int direct, unsigned int verb, unsigned int parm) |
| 3586 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3587 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3588 | struct hda_cache_head *c; |
| 3589 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3590 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3591 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3592 | cache_only = codec->cached_write; |
| 3593 | if (!cache_only) { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3594 | err = snd_hda_codec_write(codec, nid, direct, verb, parm); |
| 3595 | if (err < 0) |
| 3596 | return err; |
| 3597 | } |
| 3598 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3599 | /* parm may contain the verb stuff for get/set amp */ |
| 3600 | verb = verb | (parm >> 8); |
| 3601 | parm &= 0xff; |
| 3602 | key = build_cmd_cache_key(nid, verb); |
| 3603 | mutex_lock(&codec->bus->cmd_mutex); |
| 3604 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3605 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3606 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3607 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3608 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3609 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3610 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3611 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3612 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3613 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3614 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3615 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3616 | * @codec: the HDA codec |
| 3617 | * @nid: NID to send the command |
| 3618 | * @direct: direct flag |
| 3619 | * @verb: the verb to send |
| 3620 | * @parm: the parameter for the verb |
| 3621 | * |
| 3622 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3623 | * command if the parameter is already identical with the cached value. |
| 3624 | * If not, it sends the command and refreshes the cache. |
| 3625 | * |
| 3626 | * Returns 0 if successful, or a negative error code. |
| 3627 | */ |
| 3628 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3629 | int direct, unsigned int verb, unsigned int parm) |
| 3630 | { |
| 3631 | struct hda_cache_head *c; |
| 3632 | u32 key; |
| 3633 | |
| 3634 | /* parm may contain the verb stuff for get/set amp */ |
| 3635 | verb = verb | (parm >> 8); |
| 3636 | parm &= 0xff; |
| 3637 | key = build_cmd_cache_key(nid, verb); |
| 3638 | mutex_lock(&codec->bus->cmd_mutex); |
| 3639 | c = get_hash(&codec->cmd_cache, key); |
| 3640 | if (c && c->val == parm) { |
| 3641 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3642 | return 0; |
| 3643 | } |
| 3644 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3645 | return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); |
| 3646 | } |
| 3647 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3648 | |
| 3649 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3650 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3651 | * @codec: HD-audio codec |
| 3652 | * |
| 3653 | * Execute all verbs recorded in the command caches to resume. |
| 3654 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3655 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3656 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3657 | int i; |
| 3658 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3659 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3660 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3661 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3662 | struct hda_cache_head *buffer; |
| 3663 | u32 key; |
| 3664 | |
| 3665 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3666 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3667 | if (!key) |
| 3668 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3669 | if (!buffer->dirty) |
| 3670 | continue; |
| 3671 | buffer->dirty = 0; |
| 3672 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3673 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3674 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3675 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3676 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3677 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3678 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3679 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3680 | |
| 3681 | /** |
| 3682 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3683 | * @codec: the HDA codec |
| 3684 | * @seq: VERB array to send |
| 3685 | * |
| 3686 | * Send the commands sequentially from the given array. |
| 3687 | * Thte commands are recorded on cache for power-save and resume. |
| 3688 | * The array must be terminated with NID=0. |
| 3689 | */ |
| 3690 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3691 | const struct hda_verb *seq) |
| 3692 | { |
| 3693 | for (; seq->nid; seq++) |
| 3694 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3695 | seq->param); |
| 3696 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3697 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3698 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3699 | /** |
| 3700 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3701 | * @codec: HD-audio codec |
| 3702 | */ |
| 3703 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3704 | { |
| 3705 | snd_hda_codec_resume_amp(codec); |
| 3706 | snd_hda_codec_resume_cache(codec); |
| 3707 | } |
| 3708 | EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache); |
| 3709 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3710 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3711 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3712 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3713 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3714 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3715 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3716 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3717 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3718 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3719 | if (!(wcaps & AC_WCAP_POWER)) |
| 3720 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3721 | if (codec->power_filter) { |
| 3722 | state = codec->power_filter(codec, nid, power_state); |
| 3723 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3724 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3725 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3726 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3727 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3728 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3729 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3730 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3731 | |
| 3732 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3733 | * supported power states check |
| 3734 | */ |
| 3735 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3736 | unsigned int power_state) |
| 3737 | { |
| 3738 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3739 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3740 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3741 | return false; |
| 3742 | if (sup & power_state) |
| 3743 | return true; |
| 3744 | else |
| 3745 | return false; |
| 3746 | } |
| 3747 | |
| 3748 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3749 | * wait until the state is reached, returns the current state |
| 3750 | */ |
| 3751 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3752 | hda_nid_t fg, |
| 3753 | unsigned int power_state) |
| 3754 | { |
| 3755 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3756 | unsigned int state, actual_state; |
| 3757 | |
| 3758 | for (;;) { |
| 3759 | state = snd_hda_codec_read(codec, fg, 0, |
| 3760 | AC_VERB_GET_POWER_STATE, 0); |
| 3761 | if (state & AC_PWRST_ERROR) |
| 3762 | break; |
| 3763 | actual_state = (state >> 4) & 0x0f; |
| 3764 | if (actual_state == power_state) |
| 3765 | break; |
| 3766 | if (time_after_eq(jiffies, end_time)) |
| 3767 | break; |
| 3768 | /* wait until the codec reachs to the target state */ |
| 3769 | msleep(1); |
| 3770 | } |
| 3771 | return state; |
| 3772 | } |
| 3773 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3774 | /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3775 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3776 | hda_nid_t nid, |
| 3777 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3778 | { |
| 3779 | if (power_state == AC_PWRST_D3 && |
| 3780 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3781 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3782 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3783 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3784 | if (eapd & 0x02) |
| 3785 | return AC_PWRST_D0; |
| 3786 | } |
| 3787 | return power_state; |
| 3788 | } |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3789 | EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3790 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3791 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3792 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3793 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3794 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3795 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3796 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3797 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3798 | int count; |
| 3799 | unsigned int state; |
| 3800 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3801 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3802 | if (power_state == AC_PWRST_D3) { |
| 3803 | /* transition time less than 10ms for power down */ |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 3804 | msleep(codec->epss ? 10 : 100); |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3805 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3806 | |
| 3807 | /* repeat power states setting at most 10 times*/ |
| 3808 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3809 | if (codec->patch_ops.set_power_state) |
| 3810 | codec->patch_ops.set_power_state(codec, fg, |
| 3811 | power_state); |
| 3812 | else { |
| 3813 | snd_hda_codec_read(codec, fg, 0, |
| 3814 | AC_VERB_SET_POWER_STATE, |
| 3815 | power_state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3816 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3817 | } |
| 3818 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3819 | if (!(state & AC_PWRST_ERROR)) |
| 3820 | break; |
| 3821 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3822 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3823 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3824 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3825 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3826 | /* sync power states of all widgets; |
| 3827 | * this is called at the end of codec parsing |
| 3828 | */ |
| 3829 | static void sync_power_up_states(struct hda_codec *codec) |
| 3830 | { |
| 3831 | hda_nid_t nid = codec->start_nid; |
| 3832 | int i; |
| 3833 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3834 | /* don't care if no filter is used */ |
| 3835 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3836 | return; |
| 3837 | |
| 3838 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3839 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3840 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3841 | if (!(wcaps & AC_WCAP_POWER)) |
| 3842 | continue; |
| 3843 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3844 | if (target == AC_PWRST_D0) |
| 3845 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3846 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3847 | snd_hda_codec_write(codec, nid, 0, |
| 3848 | AC_VERB_SET_POWER_STATE, target); |
| 3849 | } |
| 3850 | } |
| 3851 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3852 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3853 | /* execute additional init verbs */ |
| 3854 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3855 | { |
| 3856 | if (codec->init_verbs.list) |
| 3857 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3858 | } |
| 3859 | #else |
| 3860 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3861 | #endif |
| 3862 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3863 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3864 | /* |
| 3865 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3866 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3867 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3868 | 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] | 3869 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3870 | unsigned int state; |
| 3871 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3872 | codec->in_pm = 1; |
| 3873 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3874 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3875 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3876 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3877 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3878 | /* Cancel delayed work if we aren't currently running from it. */ |
| 3879 | if (!in_wq) |
| 3880 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3881 | spin_lock(&codec->power_lock); |
| 3882 | snd_hda_update_power_acct(codec); |
| 3883 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 3884 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 3885 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3886 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3887 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3888 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3889 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3892 | /* mark all entries of cmd and amp caches dirty */ |
| 3893 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3894 | { |
| 3895 | int i; |
| 3896 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3897 | struct hda_cache_head *cmd; |
| 3898 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3899 | cmd->dirty = 1; |
| 3900 | } |
| 3901 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3902 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3903 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3904 | amp->head.dirty = 1; |
| 3905 | } |
| 3906 | } |
| 3907 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3908 | /* |
| 3909 | * kick up codec; used both from PM and power-save |
| 3910 | */ |
| 3911 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3912 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3913 | codec->in_pm = 1; |
| 3914 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3915 | hda_mark_cmd_cache_dirty(codec); |
| 3916 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3917 | /* set as if powered on for avoiding re-entering the resume |
| 3918 | * in the resume / power-save sequence |
| 3919 | */ |
| 3920 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3921 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3922 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3923 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3924 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3925 | if (codec->patch_ops.resume) |
| 3926 | codec->patch_ops.resume(codec); |
| 3927 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3928 | if (codec->patch_ops.init) |
| 3929 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3930 | snd_hda_codec_resume_amp(codec); |
| 3931 | snd_hda_codec_resume_cache(codec); |
| 3932 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3933 | |
| 3934 | if (codec->jackpoll_interval) |
| 3935 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3936 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3937 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3938 | |
| 3939 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3940 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3941 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3942 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3943 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | /** |
| 3946 | * snd_hda_build_controls - build mixer controls |
| 3947 | * @bus: the BUS |
| 3948 | * |
| 3949 | * Creates mixer controls for each codec included in the bus. |
| 3950 | * |
| 3951 | * Returns 0 if successful, otherwise a negative error code. |
| 3952 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 3953 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3955 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3957 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3958 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3959 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 3960 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3961 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3962 | err = snd_hda_codec_reset(codec); |
| 3963 | if (err < 0) { |
| 3964 | printk(KERN_ERR |
| 3965 | "hda_codec: cannot revert codec\n"); |
| 3966 | return err; |
| 3967 | } |
| 3968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3970 | return 0; |
| 3971 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3972 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3974 | /* |
| 3975 | * add standard channel maps if not specified |
| 3976 | */ |
| 3977 | static int add_std_chmaps(struct hda_codec *codec) |
| 3978 | { |
| 3979 | int i, str, err; |
| 3980 | |
| 3981 | for (i = 0; i < codec->num_pcms; i++) { |
| 3982 | for (str = 0; str < 2; str++) { |
| 3983 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 3984 | struct hda_pcm_stream *hinfo = |
| 3985 | &codec->pcm_info[i].stream[str]; |
| 3986 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3987 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3988 | |
| 3989 | if (codec->pcm_info[i].own_chmap) |
| 3990 | continue; |
| 3991 | if (!pcm || !hinfo->substreams) |
| 3992 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3993 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 3994 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3995 | hinfo->channels_max, |
| 3996 | 0, &chmap); |
| 3997 | if (err < 0) |
| 3998 | return err; |
| 3999 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4000 | } |
| 4001 | } |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4005 | /* default channel maps for 2.1 speakers; |
| 4006 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4007 | */ |
| 4008 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4009 | { .channels = 2, |
| 4010 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4011 | { .channels = 4, |
| 4012 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4013 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4014 | { } |
| 4015 | }; |
| 4016 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4017 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4018 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4019 | { |
| 4020 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4021 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4022 | /* continue to initialize... */ |
| 4023 | if (codec->patch_ops.init) |
| 4024 | err = codec->patch_ops.init(codec); |
| 4025 | if (!err && codec->patch_ops.build_controls) |
| 4026 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4027 | if (err < 0) |
| 4028 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4029 | |
| 4030 | /* we create chmaps here instead of build_pcms */ |
| 4031 | err = add_std_chmaps(codec); |
| 4032 | if (err < 0) |
| 4033 | return err; |
| 4034 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4035 | if (codec->jackpoll_interval) |
| 4036 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4037 | else |
| 4038 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4039 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | return 0; |
| 4041 | } |
| 4042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | /* |
| 4044 | * stream formats |
| 4045 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4046 | struct hda_rate_tbl { |
| 4047 | unsigned int hz; |
| 4048 | unsigned int alsa_bits; |
| 4049 | unsigned int hda_fmt; |
| 4050 | }; |
| 4051 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4052 | /* rate = base * mult / div */ |
| 4053 | #define HDA_RATE(base, mult, div) \ |
| 4054 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4055 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4056 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4057 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4059 | |
| 4060 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4061 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4062 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4063 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4064 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4065 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4066 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4067 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4068 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4069 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4070 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4071 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4072 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4073 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4074 | |
| 4075 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4076 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4077 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4078 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | }; |
| 4080 | |
| 4081 | /** |
| 4082 | * snd_hda_calc_stream_format - calculate format bitset |
| 4083 | * @rate: the sample rate |
| 4084 | * @channels: the number of channels |
| 4085 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4086 | * @maxbps: the max. bps |
| 4087 | * |
| 4088 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4089 | * |
| 4090 | * Return zero if invalid. |
| 4091 | */ |
| 4092 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4093 | unsigned int channels, |
| 4094 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4095 | unsigned int maxbps, |
| 4096 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | { |
| 4098 | int i; |
| 4099 | unsigned int val = 0; |
| 4100 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4101 | for (i = 0; rate_bits[i].hz; i++) |
| 4102 | if (rate_bits[i].hz == rate) { |
| 4103 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | break; |
| 4105 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4106 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | snd_printdd("invalid rate %d\n", rate); |
| 4108 | return 0; |
| 4109 | } |
| 4110 | |
| 4111 | if (channels == 0 || channels > 8) { |
| 4112 | snd_printdd("invalid channels %d\n", channels); |
| 4113 | return 0; |
| 4114 | } |
| 4115 | val |= channels - 1; |
| 4116 | |
| 4117 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4118 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4119 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4120 | break; |
| 4121 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4122 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4123 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | case 20: |
| 4125 | case 24: |
| 4126 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4127 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4128 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4130 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4132 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | break; |
| 4134 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4135 | snd_printdd("invalid format width %d\n", |
| 4136 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | return 0; |
| 4138 | } |
| 4139 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4140 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4141 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | return val; |
| 4144 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4145 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4147 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4148 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4149 | { |
| 4150 | unsigned int val = 0; |
| 4151 | if (nid != codec->afg && |
| 4152 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4153 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4154 | if (!val || val == -1) |
| 4155 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4156 | if (!val || val == -1) |
| 4157 | return 0; |
| 4158 | return val; |
| 4159 | } |
| 4160 | |
| 4161 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4162 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4163 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4164 | get_pcm_param); |
| 4165 | } |
| 4166 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4167 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4168 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4169 | { |
| 4170 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4171 | if (!streams || streams == -1) |
| 4172 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4173 | if (!streams || streams == -1) |
| 4174 | return 0; |
| 4175 | return streams; |
| 4176 | } |
| 4177 | |
| 4178 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4179 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4180 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4181 | get_stream_param); |
| 4182 | } |
| 4183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | /** |
| 4185 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4186 | * @codec: the HDA codec |
| 4187 | * @nid: NID to query |
| 4188 | * @ratesp: the pointer to store the detected rate bitflags |
| 4189 | * @formatsp: the pointer to store the detected formats |
| 4190 | * @bpsp: the pointer to store the detected format widths |
| 4191 | * |
| 4192 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4193 | * or @bsps argument is ignored. |
| 4194 | * |
| 4195 | * Returns 0 if successful, otherwise a negative error code. |
| 4196 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4197 | 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] | 4198 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4199 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4200 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4202 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4203 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | |
| 4205 | if (ratesp) { |
| 4206 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4207 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4209 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4211 | if (rates == 0) { |
| 4212 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4213 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4214 | nid, val, |
| 4215 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4216 | return -EIO; |
| 4217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | *ratesp = rates; |
| 4219 | } |
| 4220 | |
| 4221 | if (formatsp || bpsp) { |
| 4222 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4223 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4225 | streams = query_stream_param(codec, nid); |
| 4226 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | |
| 4229 | bps = 0; |
| 4230 | if (streams & AC_SUPFMT_PCM) { |
| 4231 | if (val & AC_SUPPCM_BITS_8) { |
| 4232 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4233 | bps = 8; |
| 4234 | } |
| 4235 | if (val & AC_SUPPCM_BITS_16) { |
| 4236 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4237 | bps = 16; |
| 4238 | } |
| 4239 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4240 | if (val & AC_SUPPCM_BITS_32) |
| 4241 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4242 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4243 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4244 | if (val & AC_SUPPCM_BITS_24) |
| 4245 | bps = 24; |
| 4246 | else if (val & AC_SUPPCM_BITS_20) |
| 4247 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4248 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4249 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4251 | if (val & AC_SUPPCM_BITS_32) |
| 4252 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | else if (val & AC_SUPPCM_BITS_24) |
| 4254 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4255 | else if (val & AC_SUPPCM_BITS_20) |
| 4256 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4259 | #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] | 4260 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4262 | if (!bps) |
| 4263 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4264 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4265 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4266 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4267 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | /* temporary hack: we have still no proper support |
| 4269 | * for the direct AC3 stream... |
| 4270 | */ |
| 4271 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4272 | bps = 8; |
| 4273 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4274 | if (formats == 0) { |
| 4275 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4276 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4277 | "streams=0x%x)\n", |
| 4278 | nid, val, |
| 4279 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4280 | streams); |
| 4281 | return -EIO; |
| 4282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | if (formatsp) |
| 4284 | *formatsp = formats; |
| 4285 | if (bpsp) |
| 4286 | *bpsp = bps; |
| 4287 | } |
| 4288 | |
| 4289 | return 0; |
| 4290 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4291 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | |
| 4293 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4294 | * snd_hda_is_supported_format - Check the validity of the format |
| 4295 | * @codec: HD-audio codec |
| 4296 | * @nid: NID to check |
| 4297 | * @format: the HD-audio format value to check |
| 4298 | * |
| 4299 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | * |
| 4301 | * Returns 1 if supported, 0 if not. |
| 4302 | */ |
| 4303 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4304 | unsigned int format) |
| 4305 | { |
| 4306 | int i; |
| 4307 | unsigned int val = 0, rate, stream; |
| 4308 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4309 | val = query_pcm_param(codec, nid); |
| 4310 | if (!val) |
| 4311 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
| 4313 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4314 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4315 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | if (val & (1 << i)) |
| 4317 | break; |
| 4318 | return 0; |
| 4319 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4320 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | return 0; |
| 4322 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4323 | stream = query_stream_param(codec, nid); |
| 4324 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | return 0; |
| 4326 | |
| 4327 | if (stream & AC_SUPFMT_PCM) { |
| 4328 | switch (format & 0xf0) { |
| 4329 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4330 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | return 0; |
| 4332 | break; |
| 4333 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4334 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | return 0; |
| 4336 | break; |
| 4337 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4338 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | return 0; |
| 4340 | break; |
| 4341 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4342 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | return 0; |
| 4344 | break; |
| 4345 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4346 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | return 0; |
| 4348 | break; |
| 4349 | default: |
| 4350 | return 0; |
| 4351 | } |
| 4352 | } else { |
| 4353 | /* FIXME: check for float32 and AC3? */ |
| 4354 | } |
| 4355 | |
| 4356 | return 1; |
| 4357 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4358 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | |
| 4360 | /* |
| 4361 | * PCM stuff |
| 4362 | */ |
| 4363 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4364 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4365 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | { |
| 4367 | return 0; |
| 4368 | } |
| 4369 | |
| 4370 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4371 | struct hda_codec *codec, |
| 4372 | unsigned int stream_tag, |
| 4373 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4374 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | { |
| 4376 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4377 | return 0; |
| 4378 | } |
| 4379 | |
| 4380 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4381 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4382 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4384 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | return 0; |
| 4386 | } |
| 4387 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4388 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4389 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4391 | int err; |
| 4392 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4393 | /* query support PCM information from the given NID */ |
| 4394 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4395 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4396 | info->rates ? NULL : &info->rates, |
| 4397 | info->formats ? NULL : &info->formats, |
| 4398 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4399 | if (err < 0) |
| 4400 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | } |
| 4402 | if (info->ops.open == NULL) |
| 4403 | info->ops.open = hda_pcm_default_open_close; |
| 4404 | if (info->ops.close == NULL) |
| 4405 | info->ops.close = hda_pcm_default_open_close; |
| 4406 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4407 | if (snd_BUG_ON(!info->nid)) |
| 4408 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | info->ops.prepare = hda_pcm_default_prepare; |
| 4410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4412 | if (snd_BUG_ON(!info->nid)) |
| 4413 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4415 | } |
| 4416 | return 0; |
| 4417 | } |
| 4418 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4419 | /* |
| 4420 | * codec prepare/cleanup entries |
| 4421 | */ |
| 4422 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4423 | struct hda_pcm_stream *hinfo, |
| 4424 | unsigned int stream, |
| 4425 | unsigned int format, |
| 4426 | struct snd_pcm_substream *substream) |
| 4427 | { |
| 4428 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4429 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4430 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4431 | if (ret >= 0) |
| 4432 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4433 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4434 | return ret; |
| 4435 | } |
| 4436 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 4437 | |
| 4438 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4439 | struct hda_pcm_stream *hinfo, |
| 4440 | struct snd_pcm_substream *substream) |
| 4441 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4442 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4443 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4444 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4445 | } |
| 4446 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 4447 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4448 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4449 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4450 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4451 | }; |
| 4452 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4453 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4454 | * get the empty PCM device number to assign |
Takashi Iwai | c893622 | 2010-01-28 17:08:53 +0100 | [diff] [blame] | 4455 | * |
| 4456 | * 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] | 4457 | */ |
| 4458 | static int get_empty_pcm_device(struct hda_bus *bus, int type) |
| 4459 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4460 | /* audio device indices; not linear to keep compatibility */ |
| 4461 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4462 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4463 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4464 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4465 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4466 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4467 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4468 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4469 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4470 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4471 | return -EINVAL; |
| 4472 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4473 | |
| 4474 | for (i = 0; audio_idx[type][i] >= 0 ; i++) |
| 4475 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4476 | return audio_idx[type][i]; |
| 4477 | |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4478 | /* non-fixed slots starting from 10 */ |
| 4479 | for (i = 10; i < 32; i++) { |
| 4480 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4481 | return i; |
| 4482 | } |
| 4483 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4484 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4485 | snd_hda_pcm_type_name[type]); |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4486 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4490 | * attach a new PCM stream |
| 4491 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4492 | 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] | 4493 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4494 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4495 | struct hda_pcm_stream *info; |
| 4496 | int stream, err; |
| 4497 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4498 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4499 | return -EINVAL; |
| 4500 | for (stream = 0; stream < 2; stream++) { |
| 4501 | info = &pcm->stream[stream]; |
| 4502 | if (info->substreams) { |
| 4503 | err = set_pcm_default_values(codec, info); |
| 4504 | if (err < 0) |
| 4505 | return err; |
| 4506 | } |
| 4507 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4508 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4509 | } |
| 4510 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4511 | /* assign all PCMs of the given codec */ |
| 4512 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4513 | { |
| 4514 | unsigned int pcm; |
| 4515 | int err; |
| 4516 | |
| 4517 | if (!codec->num_pcms) { |
| 4518 | if (!codec->patch_ops.build_pcms) |
| 4519 | return 0; |
| 4520 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4521 | if (err < 0) { |
| 4522 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4523 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4524 | err = snd_hda_codec_reset(codec); |
| 4525 | if (err < 0) { |
| 4526 | printk(KERN_ERR |
| 4527 | "hda_codec: cannot revert codec\n"); |
| 4528 | return err; |
| 4529 | } |
| 4530 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4531 | } |
| 4532 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4533 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4534 | int dev; |
| 4535 | |
| 4536 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4537 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4538 | |
| 4539 | if (!cpcm->pcm) { |
| 4540 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4541 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4542 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4543 | cpcm->device = dev; |
| 4544 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4545 | if (err < 0) { |
| 4546 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4547 | "PCM stream %d for codec #%d\n", |
| 4548 | dev, codec->addr); |
| 4549 | continue; /* no fatal error */ |
| 4550 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | return 0; |
| 4554 | } |
| 4555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | /** |
| 4557 | * snd_hda_build_pcms - build PCM information |
| 4558 | * @bus: the BUS |
| 4559 | * |
| 4560 | * Create PCM information for each codec included in the bus. |
| 4561 | * |
| 4562 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4563 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4564 | * to create its PCM instances. |
| 4565 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4566 | * callback. |
| 4567 | * |
| 4568 | * At least, substreams, channels_min and channels_max must be filled for |
| 4569 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4570 | * When rates and/or formats are zero, the supported values are queried |
| 4571 | * from the given nid. The nid is used also by the default ops.prepare |
| 4572 | * and ops.cleanup callbacks. |
| 4573 | * |
| 4574 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4575 | * ops.close is supposed to be called in the close callback. |
| 4576 | * ops.prepare should be called in the prepare or hw_params callback |
| 4577 | * with the proper parameters for set up. |
| 4578 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4579 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4580 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4582 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4584 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4586 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4587 | int err = snd_hda_codec_build_pcms(codec); |
| 4588 | if (err < 0) |
| 4589 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
| 4591 | return 0; |
| 4592 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4593 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | /** |
| 4596 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4597 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4598 | * @num_configs: number of config enums |
| 4599 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | * @tbl: configuration table, terminated by null entries |
| 4601 | * |
| 4602 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4603 | * given configuration table. If a matching entry is found, returns its |
| 4604 | * config value (supposed to be 0 or positive). |
| 4605 | * |
| 4606 | * If no entries are matching, the function returns a negative value. |
| 4607 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4608 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4609 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4610 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4612 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4613 | int i; |
| 4614 | for (i = 0; i < num_configs; i++) { |
| 4615 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4616 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4617 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4618 | "selected\n", models[i]); |
| 4619 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | } |
| 4623 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4624 | if (!codec->bus->pci || !tbl) |
| 4625 | return -1; |
| 4626 | |
| 4627 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4628 | if (!tbl) |
| 4629 | return -1; |
| 4630 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4631 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4632 | char tmp[10]; |
| 4633 | const char *model = NULL; |
| 4634 | if (models) |
| 4635 | model = models[tbl->value]; |
| 4636 | if (!model) { |
| 4637 | sprintf(tmp, "#%d", tbl->value); |
| 4638 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4640 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4641 | "for config %x:%x (%s)\n", |
| 4642 | model, tbl->subvendor, tbl->subdevice, |
| 4643 | (tbl->name ? tbl->name : "Unknown device")); |
| 4644 | #endif |
| 4645 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | } |
| 4647 | return -1; |
| 4648 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4649 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | |
| 4651 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4652 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4653 | subsystem ID with the |
| 4654 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4655 | |
| 4656 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4657 | where the vendor ID of the PCI device is: |
| 4658 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4659 | and the vendor/subvendor are found only at the codec. |
| 4660 | |
| 4661 | * @codec: the HDA codec |
| 4662 | * @num_configs: number of config enums |
| 4663 | * @models: array of model name strings |
| 4664 | * @tbl: configuration table, terminated by null entries |
| 4665 | * |
| 4666 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4667 | * given configuration table. If a matching entry is found, returns its |
| 4668 | * config value (supposed to be 0 or positive). |
| 4669 | * |
| 4670 | * If no entries are matching, the function returns a negative value. |
| 4671 | */ |
| 4672 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4673 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4674 | const struct snd_pci_quirk *tbl) |
| 4675 | { |
| 4676 | const struct snd_pci_quirk *q; |
| 4677 | |
| 4678 | /* Search for codec ID */ |
| 4679 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4680 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4681 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4682 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4683 | break; |
| 4684 | } |
| 4685 | |
| 4686 | if (!q->subvendor) |
| 4687 | return -1; |
| 4688 | |
| 4689 | tbl = q; |
| 4690 | |
| 4691 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4692 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4693 | char tmp[10]; |
| 4694 | const char *model = NULL; |
| 4695 | if (models) |
| 4696 | model = models[tbl->value]; |
| 4697 | if (!model) { |
| 4698 | sprintf(tmp, "#%d", tbl->value); |
| 4699 | model = tmp; |
| 4700 | } |
| 4701 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4702 | "for config %x:%x (%s)\n", |
| 4703 | model, tbl->subvendor, tbl->subdevice, |
| 4704 | (tbl->name ? tbl->name : "Unknown device")); |
| 4705 | #endif |
| 4706 | return tbl->value; |
| 4707 | } |
| 4708 | return -1; |
| 4709 | } |
| 4710 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4711 | |
| 4712 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | * snd_hda_add_new_ctls - create controls from the array |
| 4714 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4715 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | * |
| 4717 | * This helper function creates and add new controls in the given array. |
| 4718 | * The array must be terminated with an empty entry as terminator. |
| 4719 | * |
| 4720 | * Returns 0 if successful, or a negative error code. |
| 4721 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4722 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4723 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4725 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | |
| 4727 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4728 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4729 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4730 | if (knew->iface == -1) /* skip this codec private value */ |
| 4731 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4732 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4733 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4734 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4735 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4736 | if (addr > 0) |
| 4737 | kctl->id.device = addr; |
| 4738 | if (idx > 0) |
| 4739 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4740 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4741 | if (!err) |
| 4742 | break; |
| 4743 | /* try first with another device index corresponding to |
| 4744 | * the codec addr; if it still fails (or it's the |
| 4745 | * primary codec), then try another control index |
| 4746 | */ |
| 4747 | if (!addr && codec->addr) |
| 4748 | addr = codec->addr; |
| 4749 | else if (!idx && !knew->index) { |
| 4750 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4751 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4752 | if (idx <= 0) |
| 4753 | return err; |
| 4754 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4755 | return err; |
| 4756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | } |
| 4758 | return 0; |
| 4759 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4760 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4762 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4763 | static void hda_power_work(struct work_struct *work) |
| 4764 | { |
| 4765 | struct hda_codec *codec = |
| 4766 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4767 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4768 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4769 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4770 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4771 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4772 | spin_unlock(&codec->power_lock); |
| 4773 | return; |
| 4774 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4775 | if (!codec->power_on || codec->power_count) { |
| 4776 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4777 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4778 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4779 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4780 | spin_unlock(&codec->power_lock); |
| 4781 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4782 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4783 | codec->pm_down_notified = 0; |
| 4784 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) { |
| 4785 | codec->pm_down_notified = 1; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4786 | hda_call_pm_notify(bus, false); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4787 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4791 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4792 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4793 | codec->power_count++; |
| 4794 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4795 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4796 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4797 | } |
| 4798 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4799 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4800 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4801 | { |
| 4802 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4803 | if (codec->power_on) |
| 4804 | codec->power_on_acct += delta; |
| 4805 | else |
| 4806 | codec->power_off_acct += delta; |
| 4807 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4810 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4811 | * transition to D3 to complete. A pending D3 transition is indicated |
| 4812 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4813 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4814 | 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] | 4815 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4816 | struct hda_bus *bus = codec->bus; |
| 4817 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4818 | /* Return if power_on or transitioning to power_on, unless currently |
| 4819 | * powering down. */ |
| 4820 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4821 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4822 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4823 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4824 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4825 | cancel_delayed_work_sync(&codec->power_work); |
| 4826 | |
| 4827 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4828 | /* If the power down delayed work was cancelled above before starting, |
| 4829 | * then there is no need to go through power up here. |
| 4830 | */ |
| 4831 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 4832 | if (codec->power_transition < 0) |
| 4833 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4834 | return; |
| 4835 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4836 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4837 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4838 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4839 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4840 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4841 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4842 | spin_unlock(&codec->power_lock); |
| 4843 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4844 | if (codec->pm_down_notified) { |
| 4845 | codec->pm_down_notified = 0; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4846 | hda_call_pm_notify(bus, true); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4847 | } |
| 4848 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4849 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4850 | |
| 4851 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4852 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4853 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4854 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 4855 | #define power_save(codec) \ |
| 4856 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4857 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4858 | /* Transition to powered down */ |
| 4859 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4860 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4861 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4862 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4863 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4864 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4865 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 4866 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4867 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4868 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | /** |
| 4872 | * snd_hda_power_save - Power-up/down/sync the codec |
| 4873 | * @codec: HD-audio codec |
| 4874 | * @delta: the counter delta to change |
| 4875 | * |
| 4876 | * Change the power-up counter via @delta, and power up or down the hardware |
| 4877 | * appropriately. For the power-down, queue to the delayed action. |
| 4878 | * Passing zero to @delta means to synchronize the power state. |
| 4879 | */ |
| 4880 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 4881 | { |
| 4882 | spin_lock(&codec->power_lock); |
| 4883 | codec->power_count += delta; |
| 4884 | trace_hda_power_count(codec); |
| 4885 | if (delta > 0) |
| 4886 | __snd_hda_power_up(codec, d3wait); |
| 4887 | else |
| 4888 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4889 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4890 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4891 | EXPORT_SYMBOL_HDA(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4892 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4893 | /** |
| 4894 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4895 | * @codec: HD-audio codec |
| 4896 | * @check: the object containing an AMP list and the status |
| 4897 | * @nid: NID to check / update |
| 4898 | * |
| 4899 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4900 | * check the current AMP status, and update the the power-status according |
| 4901 | * to the mute status. |
| 4902 | * |
| 4903 | * This function is supposed to be set or called from the check_power_status |
| 4904 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4905 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4906 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4907 | struct hda_loopback_check *check, |
| 4908 | hda_nid_t nid) |
| 4909 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4910 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4911 | int ch, v; |
| 4912 | |
| 4913 | if (!check->amplist) |
| 4914 | return 0; |
| 4915 | for (p = check->amplist; p->nid; p++) { |
| 4916 | if (p->nid == nid) |
| 4917 | break; |
| 4918 | } |
| 4919 | if (!p->nid) |
| 4920 | return 0; /* nothing changed */ |
| 4921 | |
| 4922 | for (p = check->amplist; p->nid; p++) { |
| 4923 | for (ch = 0; ch < 2; ch++) { |
| 4924 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4925 | p->idx); |
| 4926 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4927 | if (!check->power_on) { |
| 4928 | check->power_on = 1; |
| 4929 | snd_hda_power_up(codec); |
| 4930 | } |
| 4931 | return 1; |
| 4932 | } |
| 4933 | } |
| 4934 | } |
| 4935 | if (check->power_on) { |
| 4936 | check->power_on = 0; |
| 4937 | snd_hda_power_down(codec); |
| 4938 | } |
| 4939 | return 0; |
| 4940 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4941 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4942 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4944 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4945 | * Channel mode helper |
| 4946 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4947 | |
| 4948 | /** |
| 4949 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 4950 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4951 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4952 | struct snd_ctl_elem_info *uinfo, |
| 4953 | const struct hda_channel_mode *chmode, |
| 4954 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4955 | { |
| 4956 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4957 | uinfo->count = 1; |
| 4958 | uinfo->value.enumerated.items = num_chmodes; |
| 4959 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4960 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4961 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4962 | chmode[uinfo->value.enumerated.item].channels); |
| 4963 | return 0; |
| 4964 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4965 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4966 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4967 | /** |
| 4968 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 4969 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4970 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4971 | struct snd_ctl_elem_value *ucontrol, |
| 4972 | const struct hda_channel_mode *chmode, |
| 4973 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4974 | int max_channels) |
| 4975 | { |
| 4976 | int i; |
| 4977 | |
| 4978 | for (i = 0; i < num_chmodes; i++) { |
| 4979 | if (max_channels == chmode[i].channels) { |
| 4980 | ucontrol->value.enumerated.item[0] = i; |
| 4981 | break; |
| 4982 | } |
| 4983 | } |
| 4984 | return 0; |
| 4985 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4986 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4987 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4988 | /** |
| 4989 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 4990 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4991 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 4992 | struct snd_ctl_elem_value *ucontrol, |
| 4993 | const struct hda_channel_mode *chmode, |
| 4994 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4995 | int *max_channelsp) |
| 4996 | { |
| 4997 | unsigned int mode; |
| 4998 | |
| 4999 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5000 | if (mode >= num_chmodes) |
| 5001 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5002 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5003 | return 0; |
| 5004 | /* change the current channel setting */ |
| 5005 | *max_channelsp = chmode[mode].channels; |
| 5006 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5007 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5008 | return 1; |
| 5009 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5010 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | /* |
| 5013 | * input MUX helper |
| 5014 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5015 | |
| 5016 | /** |
| 5017 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5018 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5019 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5020 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | { |
| 5022 | unsigned int index; |
| 5023 | |
| 5024 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5025 | uinfo->count = 1; |
| 5026 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5027 | if (!imux->num_items) |
| 5028 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | index = uinfo->value.enumerated.item; |
| 5030 | if (index >= imux->num_items) |
| 5031 | index = imux->num_items - 1; |
| 5032 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5033 | return 0; |
| 5034 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5035 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5037 | /** |
| 5038 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5039 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5040 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5041 | const struct hda_input_mux *imux, |
| 5042 | struct snd_ctl_elem_value *ucontrol, |
| 5043 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | unsigned int *cur_val) |
| 5045 | { |
| 5046 | unsigned int idx; |
| 5047 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5048 | if (!imux->num_items) |
| 5049 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | idx = ucontrol->value.enumerated.item[0]; |
| 5051 | if (idx >= imux->num_items) |
| 5052 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5053 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5055 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5056 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | *cur_val = idx; |
| 5058 | return 1; |
| 5059 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5060 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | |
| 5062 | |
| 5063 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5064 | * process kcontrol info callback of a simple string enum array |
| 5065 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5066 | */ |
| 5067 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5068 | struct snd_ctl_elem_info *uinfo, |
| 5069 | int num_items, const char * const *texts) |
| 5070 | { |
| 5071 | static const char * const texts_default[] = { |
| 5072 | "Disabled", "Enabled" |
| 5073 | }; |
| 5074 | |
| 5075 | if (!texts || !num_items) { |
| 5076 | num_items = 2; |
| 5077 | texts = texts_default; |
| 5078 | } |
| 5079 | |
| 5080 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5081 | uinfo->count = 1; |
| 5082 | uinfo->value.enumerated.items = num_items; |
| 5083 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5084 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5085 | strcpy(uinfo->value.enumerated.name, |
| 5086 | texts[uinfo->value.enumerated.item]); |
| 5087 | return 0; |
| 5088 | } |
| 5089 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); |
| 5090 | |
| 5091 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | * Multi-channel / digital-out PCM helper functions |
| 5093 | */ |
| 5094 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5095 | /* setup SPDIF output stream */ |
| 5096 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5097 | unsigned int stream_tag, unsigned int format) |
| 5098 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5099 | struct hda_spdif_out *spdif; |
| 5100 | unsigned int curr_fmt; |
| 5101 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5102 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5103 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5104 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5105 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5106 | reset = codec->spdif_status_reset && |
| 5107 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5108 | curr_fmt != format; |
| 5109 | |
| 5110 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5111 | updated */ |
| 5112 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5113 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5114 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5115 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5116 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5117 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5118 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5119 | for (d = codec->slave_dig_outs; *d; d++) |
| 5120 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5121 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5122 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5123 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5124 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5125 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5126 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5127 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5128 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5129 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5130 | { |
| 5131 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5132 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5133 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5134 | for (d = codec->slave_dig_outs; *d; d++) |
| 5135 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5136 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5137 | } |
| 5138 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5139 | /** |
| 5140 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5141 | * @bus: HD-audio bus |
| 5142 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5143 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5144 | { |
| 5145 | struct hda_codec *codec; |
| 5146 | |
| 5147 | if (!bus) |
| 5148 | return; |
| 5149 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5150 | if (hda_codec_is_power_on(codec) && |
| 5151 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5152 | codec->patch_ops.reboot_notify(codec); |
| 5153 | } |
| 5154 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 5155 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5156 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5157 | /** |
| 5158 | * 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] | 5159 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5160 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5161 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5163 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5164 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5165 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5166 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5168 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | return 0; |
| 5170 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5171 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5173 | /** |
| 5174 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5175 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5176 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5177 | struct hda_multi_out *mout, |
| 5178 | unsigned int stream_tag, |
| 5179 | unsigned int format, |
| 5180 | struct snd_pcm_substream *substream) |
| 5181 | { |
| 5182 | mutex_lock(&codec->spdif_mutex); |
| 5183 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5184 | mutex_unlock(&codec->spdif_mutex); |
| 5185 | return 0; |
| 5186 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5187 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5188 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5189 | /** |
| 5190 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5191 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5192 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5193 | struct hda_multi_out *mout) |
| 5194 | { |
| 5195 | mutex_lock(&codec->spdif_mutex); |
| 5196 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5197 | mutex_unlock(&codec->spdif_mutex); |
| 5198 | return 0; |
| 5199 | } |
| 5200 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 5201 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5202 | /** |
| 5203 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5205 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5206 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5208 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5210 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | return 0; |
| 5212 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5213 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5215 | /** |
| 5216 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5217 | * |
| 5218 | * Open analog outputs and set up the hw-constraints. |
| 5219 | * If the digital outputs can be opened as slave, open the digital |
| 5220 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5222 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5223 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5224 | struct snd_pcm_substream *substream, |
| 5225 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5227 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5228 | runtime->hw.channels_max = mout->max_channels; |
| 5229 | if (mout->dig_out_nid) { |
| 5230 | if (!mout->analog_rates) { |
| 5231 | mout->analog_rates = hinfo->rates; |
| 5232 | mout->analog_formats = hinfo->formats; |
| 5233 | mout->analog_maxbps = hinfo->maxbps; |
| 5234 | } else { |
| 5235 | runtime->hw.rates = mout->analog_rates; |
| 5236 | runtime->hw.formats = mout->analog_formats; |
| 5237 | hinfo->maxbps = mout->analog_maxbps; |
| 5238 | } |
| 5239 | if (!mout->spdif_rates) { |
| 5240 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5241 | &mout->spdif_rates, |
| 5242 | &mout->spdif_formats, |
| 5243 | &mout->spdif_maxbps); |
| 5244 | } |
| 5245 | mutex_lock(&codec->spdif_mutex); |
| 5246 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5247 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5248 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5249 | runtime->hw.rates &= mout->spdif_rates; |
| 5250 | runtime->hw.formats &= mout->spdif_formats; |
| 5251 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5252 | hinfo->maxbps = mout->spdif_maxbps; |
| 5253 | } else { |
| 5254 | mout->share_spdif = 0; |
| 5255 | /* FIXME: need notify? */ |
| 5256 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5257 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5258 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5261 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5262 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5263 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5265 | /** |
| 5266 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5267 | * |
| 5268 | * Set up the i/o for analog out. |
| 5269 | * 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] | 5270 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5271 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5272 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | unsigned int stream_tag, |
| 5274 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5275 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5277 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5279 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | int i; |
| 5281 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5282 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5283 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5284 | if (mout->dig_out_nid && mout->share_spdif && |
| 5285 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5287 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5288 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5289 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5291 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5292 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | } else { |
| 5294 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5295 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | } |
| 5297 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5298 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | |
| 5300 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5301 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5302 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5303 | if (!mout->no_share_stream && |
| 5304 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5306 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5307 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5308 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5309 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5310 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5311 | snd_hda_codec_setup_stream(codec, |
| 5312 | mout->hp_out_nid[i], |
| 5313 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5314 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5315 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5316 | snd_hda_codec_setup_stream(codec, |
| 5317 | mout->extra_out_nid[i], |
| 5318 | stream_tag, 0, format); |
| 5319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | /* surrounds */ |
| 5321 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5322 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5323 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5324 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5325 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5326 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5327 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | } |
| 5329 | return 0; |
| 5330 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5331 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5333 | /** |
| 5334 | * 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] | 5335 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5336 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5337 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5339 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | int i; |
| 5341 | |
| 5342 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5343 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5345 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5346 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5347 | if (mout->hp_out_nid[i]) |
| 5348 | snd_hda_codec_cleanup_stream(codec, |
| 5349 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5350 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5351 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5352 | snd_hda_codec_cleanup_stream(codec, |
| 5353 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5354 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | 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] | 5356 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | mout->dig_out_used = 0; |
| 5358 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5359 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | return 0; |
| 5361 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5362 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5364 | /** |
| 5365 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5366 | * |
| 5367 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5368 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5369 | */ |
| 5370 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5371 | { |
| 5372 | unsigned int pincap; |
| 5373 | unsigned int oldval; |
| 5374 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5375 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5376 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5377 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5378 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5379 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5380 | return AC_PINCTL_VREF_80; |
| 5381 | else if (pincap & AC_PINCAP_VREF_50) |
| 5382 | return AC_PINCTL_VREF_50; |
| 5383 | else if (pincap & AC_PINCAP_VREF_100) |
| 5384 | return AC_PINCTL_VREF_100; |
| 5385 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5386 | return AC_PINCTL_VREF_GRD; |
| 5387 | return AC_PINCTL_VREF_HIZ; |
| 5388 | } |
| 5389 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); |
| 5390 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5391 | /* correct the pin ctl value for matching with the pin cap */ |
| 5392 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5393 | hda_nid_t pin, unsigned int val) |
| 5394 | { |
| 5395 | static unsigned int cap_lists[][2] = { |
| 5396 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5397 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5398 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5399 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5400 | }; |
| 5401 | unsigned int cap; |
| 5402 | |
| 5403 | if (!val) |
| 5404 | return 0; |
| 5405 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5406 | if (!cap) |
| 5407 | return val; /* don't know what to do... */ |
| 5408 | |
| 5409 | if (val & AC_PINCTL_OUT_EN) { |
| 5410 | if (!(cap & AC_PINCAP_OUT)) |
| 5411 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5412 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5413 | val &= ~AC_PINCTL_HP_EN; |
| 5414 | } |
| 5415 | |
| 5416 | if (val & AC_PINCTL_IN_EN) { |
| 5417 | if (!(cap & AC_PINCAP_IN)) |
| 5418 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5419 | else { |
| 5420 | unsigned int vcap, vref; |
| 5421 | int i; |
| 5422 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5423 | vref = val & AC_PINCTL_VREFEN; |
| 5424 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5425 | if (vref == cap_lists[i][0] && |
| 5426 | !(vcap & cap_lists[i][1])) { |
| 5427 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5428 | vref = AC_PINCTL_VREF_HIZ; |
| 5429 | else |
| 5430 | vref = cap_lists[i + 1][0]; |
| 5431 | } |
| 5432 | } |
| 5433 | val &= ~AC_PINCTL_VREFEN; |
| 5434 | val |= vref; |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | return val; |
| 5439 | } |
| 5440 | EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl); |
| 5441 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5442 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5443 | unsigned int val, bool cached) |
| 5444 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5445 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5446 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5447 | if (cached) |
| 5448 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5449 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5450 | else |
| 5451 | return snd_hda_codec_write(codec, pin, 0, |
| 5452 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5453 | } |
| 5454 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); |
| 5455 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5456 | /** |
| 5457 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5458 | * |
| 5459 | * When the same label is used already in the existing items, the number |
| 5460 | * suffix is appended to the label. This label index number is stored |
| 5461 | * to type_idx when non-NULL pointer is given. |
| 5462 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5463 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5464 | int index, int *type_idx) |
| 5465 | { |
| 5466 | int i, label_idx = 0; |
| 5467 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5468 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5469 | return -EINVAL; |
| 5470 | } |
| 5471 | for (i = 0; i < imux->num_items; i++) { |
| 5472 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5473 | label_idx++; |
| 5474 | } |
| 5475 | if (type_idx) |
| 5476 | *type_idx = label_idx; |
| 5477 | if (label_idx > 0) |
| 5478 | snprintf(imux->items[imux->num_items].label, |
| 5479 | sizeof(imux->items[imux->num_items].label), |
| 5480 | "%s %d", label, label_idx); |
| 5481 | else |
| 5482 | strlcpy(imux->items[imux->num_items].label, label, |
| 5483 | sizeof(imux->items[imux->num_items].label)); |
| 5484 | imux->items[imux->num_items].index = index; |
| 5485 | imux->num_items++; |
| 5486 | return 0; |
| 5487 | } |
| 5488 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5489 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | #ifdef CONFIG_PM |
| 5492 | /* |
| 5493 | * power management |
| 5494 | */ |
| 5495 | |
| 5496 | /** |
| 5497 | * snd_hda_suspend - suspend the codecs |
| 5498 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | * |
| 5500 | * Returns 0 if successful. |
| 5501 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5502 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5504 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5506 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5507 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5508 | if (hda_codec_is_power_on(codec)) |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5509 | hda_call_codec_suspend(codec, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | } |
| 5511 | return 0; |
| 5512 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5513 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | |
| 5515 | /** |
| 5516 | * snd_hda_resume - resume the codecs |
| 5517 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | * |
| 5519 | * Returns 0 if successful. |
| 5520 | */ |
| 5521 | int snd_hda_resume(struct hda_bus *bus) |
| 5522 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5523 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5525 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5526 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | return 0; |
| 5529 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5530 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5531 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5532 | |
| 5533 | /* |
| 5534 | * generic arrays |
| 5535 | */ |
| 5536 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5537 | /** |
| 5538 | * snd_array_new - get a new element from the given array |
| 5539 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5540 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5541 | * Get a new element from the given array. If it exceeds the |
| 5542 | * pre-allocated array size, re-allocate the array. |
| 5543 | * |
| 5544 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5545 | */ |
| 5546 | void *snd_array_new(struct snd_array *array) |
| 5547 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5548 | if (snd_BUG_ON(!array->elem_size)) |
| 5549 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5550 | if (array->used >= array->alloced) { |
| 5551 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5552 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5553 | void *nlist; |
| 5554 | if (snd_BUG_ON(num >= 4096)) |
| 5555 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5556 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5557 | if (!nlist) |
| 5558 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5559 | array->list = nlist; |
| 5560 | array->alloced = num; |
| 5561 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5562 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5563 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5564 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5565 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5566 | /** |
| 5567 | * snd_array_free - free the given array elements |
| 5568 | * @array: the array object |
| 5569 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5570 | void snd_array_free(struct snd_array *array) |
| 5571 | { |
| 5572 | kfree(array->list); |
| 5573 | array->used = 0; |
| 5574 | array->alloced = 0; |
| 5575 | array->list = NULL; |
| 5576 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5577 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5578 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5579 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5580 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5581 | * @pcm: PCM caps bits |
| 5582 | * @buf: the string buffer to write |
| 5583 | * @buflen: the max buffer length |
| 5584 | * |
| 5585 | * used by hda_proc.c and hda_eld.c |
| 5586 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5587 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5588 | { |
| 5589 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5590 | int i, j; |
| 5591 | |
| 5592 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5593 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5594 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5595 | |
| 5596 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5597 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5598 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5599 | |
| 5600 | MODULE_DESCRIPTION("HDA codec core"); |
| 5601 | MODULE_LICENSE("GPL"); |