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