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