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