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