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