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", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 176 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 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 |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 188 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags, |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 189 | unsigned int verb, unsigned int parm) |
| 190 | { |
| 191 | u32 val; |
| 192 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 193 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 194 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 195 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n", |
| 196 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 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)nid << 20; |
| 202 | val |= verb << 8; |
| 203 | val |= parm; |
| 204 | return val; |
| 205 | } |
| 206 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 207 | /* |
| 208 | * Send and receive a verb |
| 209 | */ |
| 210 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 211 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 212 | { |
| 213 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 214 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 215 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 216 | if (cmd == ~0) |
| 217 | return -1; |
| 218 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 219 | if (res) |
| 220 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 221 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 222 | snd_hda_power_up(codec); |
| 223 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 224 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 225 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 226 | for (;;) { |
| 227 | trace_hda_send_cmd(codec, cmd); |
| 228 | err = bus->ops.command(bus, cmd); |
| 229 | if (err != -EAGAIN) |
| 230 | break; |
| 231 | /* process pending verbs */ |
| 232 | bus->ops.get_response(bus, codec->addr); |
| 233 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 234 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 235 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 236 | trace_hda_get_response(codec, *res); |
| 237 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 238 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 239 | mutex_unlock(&bus->cmd_mutex); |
| 240 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 241 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 242 | if (bus->response_reset) { |
| 243 | snd_printd("hda_codec: resetting BUS due to " |
| 244 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 245 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 246 | bus->ops.bus_reset(bus); |
| 247 | } |
| 248 | goto again; |
| 249 | } |
| 250 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 251 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 252 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 253 | return err; |
| 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /** |
| 257 | * snd_hda_codec_read - send a command and get the response |
| 258 | * @codec: the HDA codec |
| 259 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 260 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * @verb: the verb to send |
| 262 | * @parm: the parameter for the verb |
| 263 | * |
| 264 | * Send a single command and read the corresponding response. |
| 265 | * |
| 266 | * Returns the obtained response value, or -1 for an error. |
| 267 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 268 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 269 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | unsigned int verb, unsigned int parm) |
| 271 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 272 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 273 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 274 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 275 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return res; |
| 277 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 278 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * snd_hda_codec_write - send a single command without waiting for response |
| 282 | * @codec: the HDA codec |
| 283 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 284 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | * @verb: the verb to send |
| 286 | * @parm: the parameter for the verb |
| 287 | * |
| 288 | * Send a single command without waiting for response. |
| 289 | * |
| 290 | * Returns 0 if successful, or a negative error code. |
| 291 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 292 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 293 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 295 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 296 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 297 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 298 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 300 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * snd_hda_sequence_write - sequence writes |
| 304 | * @codec: the HDA codec |
| 305 | * @seq: VERB array to send |
| 306 | * |
| 307 | * Send the commands sequentially from the given array. |
| 308 | * The array must be terminated with NID=0. |
| 309 | */ |
| 310 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 311 | { |
| 312 | for (; seq->nid; seq++) |
| 313 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 314 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 315 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 319 | * @codec: the HDA codec |
| 320 | * @nid: NID to parse |
| 321 | * @start_id: the pointer to store the start NID |
| 322 | * |
| 323 | * Parse the NID and store the start NID of its sub-nodes. |
| 324 | * Returns the number of sub-nodes. |
| 325 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 326 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 327 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | unsigned int parm; |
| 330 | |
| 331 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 332 | if (parm == -1) |
| 333 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | *start_id = (parm >> 16) & 0x7fff; |
| 335 | return (int)(parm & 0x7fff); |
| 336 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 337 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 339 | /* connection list element */ |
| 340 | struct hda_conn_list { |
| 341 | struct list_head list; |
| 342 | int len; |
| 343 | hda_nid_t nid; |
| 344 | hda_nid_t conns[0]; |
| 345 | }; |
| 346 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 347 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 348 | static struct hda_conn_list * |
| 349 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 350 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 351 | struct hda_conn_list *p; |
| 352 | list_for_each_entry(p, &codec->conn_list, list) { |
| 353 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 354 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 355 | } |
| 356 | return NULL; |
| 357 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 358 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 359 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 360 | const hda_nid_t *list) |
| 361 | { |
| 362 | struct hda_conn_list *p; |
| 363 | |
| 364 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 365 | if (!p) |
| 366 | return -ENOMEM; |
| 367 | p->len = len; |
| 368 | p->nid = nid; |
| 369 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 370 | list_add(&p->list, &codec->conn_list); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static void remove_conn_list(struct hda_codec *codec) |
| 375 | { |
| 376 | while (!list_empty(&codec->conn_list)) { |
| 377 | struct hda_conn_list *p; |
| 378 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 379 | list_del(&p->list); |
| 380 | kfree(p); |
| 381 | } |
| 382 | } |
| 383 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 384 | /* read the connection and add to the cache */ |
| 385 | 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] | 386 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 387 | hda_nid_t list[32]; |
| 388 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 389 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 390 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 391 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 392 | if (len == -ENOSPC) { |
| 393 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 394 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 395 | if (!result) |
| 396 | return -ENOMEM; |
| 397 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 398 | } |
| 399 | if (len >= 0) |
| 400 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 401 | if (result != list) |
| 402 | kfree(result); |
| 403 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 404 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 405 | |
| 406 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 407 | * snd_hda_get_conn_list - get connection list |
| 408 | * @codec: the HDA codec |
| 409 | * @nid: NID to parse |
| 410 | * @len: number of connection list entries |
| 411 | * @listp: the pointer to store NID list |
| 412 | * |
| 413 | * Parses the connection list of the given widget and stores the pointer |
| 414 | * to the list of NIDs. |
| 415 | * |
| 416 | * Returns the number of connections, or a negative error code. |
| 417 | * |
| 418 | * Note that the returned pointer isn't protected against the list |
| 419 | * modification. If snd_hda_override_conn_list() might be called |
| 420 | * concurrently, protect with a mutex appropriately. |
| 421 | */ |
| 422 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 423 | const hda_nid_t **listp) |
| 424 | { |
| 425 | bool added = false; |
| 426 | |
| 427 | for (;;) { |
| 428 | int err; |
| 429 | const struct hda_conn_list *p; |
| 430 | |
| 431 | /* if the connection-list is already cached, read it */ |
| 432 | p = lookup_conn_list(codec, nid); |
| 433 | if (p) { |
| 434 | if (listp) |
| 435 | *listp = p->conns; |
| 436 | return p->len; |
| 437 | } |
| 438 | if (snd_BUG_ON(added)) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | err = read_and_add_raw_conns(codec, nid); |
| 442 | if (err < 0) |
| 443 | return err; |
| 444 | added = true; |
| 445 | } |
| 446 | } |
| 447 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 448 | |
| 449 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 450 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * @codec: the HDA codec |
| 452 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 453 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | * @max_conns: max. number of connections to store |
| 455 | * |
| 456 | * Parses the connection list of the given widget and stores the list |
| 457 | * of NIDs. |
| 458 | * |
| 459 | * Returns the number of connections, or a negative error code. |
| 460 | */ |
| 461 | 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] | 462 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 463 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 464 | const hda_nid_t *list; |
| 465 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 466 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 467 | if (len > 0 && conn_list) { |
| 468 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 469 | snd_printk(KERN_ERR "hda_codec: " |
| 470 | "Too many connections %d for NID 0x%x\n", |
| 471 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 472 | return -EINVAL; |
| 473 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 474 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 475 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 476 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 477 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 478 | } |
| 479 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 480 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 481 | /* return CONNLIST_LEN parameter of the given widget */ |
| 482 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 483 | { |
| 484 | unsigned int wcaps = get_wcaps(codec, nid); |
| 485 | unsigned int parm; |
| 486 | |
| 487 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 488 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 489 | return 0; |
| 490 | |
| 491 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 492 | if (parm == -1) |
| 493 | parm = 0; |
| 494 | return parm; |
| 495 | } |
| 496 | |
| 497 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 498 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 499 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 502 | /** |
| 503 | * snd_hda_get_raw_connections - copy connection list without cache |
| 504 | * @codec: the HDA codec |
| 505 | * @nid: NID to parse |
| 506 | * @conn_list: connection list array |
| 507 | * @max_conns: max. number of connections to store |
| 508 | * |
| 509 | * Like snd_hda_get_connections(), copy the connection list but without |
| 510 | * checking through the connection-list cache. |
| 511 | * Currently called only from hda_proc.c, so not exported. |
| 512 | */ |
| 513 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 514 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
| 516 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 517 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 519 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 520 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 522 | parm = get_num_conns(codec, nid); |
| 523 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 524 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (parm & AC_CLIST_LONG) { |
| 527 | /* long form */ |
| 528 | shift = 16; |
| 529 | num_elems = 2; |
| 530 | } else { |
| 531 | /* short form */ |
| 532 | shift = 8; |
| 533 | num_elems = 4; |
| 534 | } |
| 535 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | mask = (1 << (shift-1)) - 1; |
| 537 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 538 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return 0; /* no connection */ |
| 540 | |
| 541 | if (conn_len == 1) { |
| 542 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 543 | parm = snd_hda_codec_read(codec, nid, 0, |
| 544 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 545 | if (parm == -1 && codec->bus->rirb_error) |
| 546 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 547 | if (conn_list) |
| 548 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* multi connection */ |
| 553 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 554 | prev_nid = 0; |
| 555 | for (i = 0; i < conn_len; i++) { |
| 556 | int range_val; |
| 557 | hda_nid_t val, n; |
| 558 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 559 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 560 | parm = snd_hda_codec_read(codec, nid, 0, |
| 561 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 562 | if (parm == -1 && codec->bus->rirb_error) |
| 563 | return -EIO; |
| 564 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 565 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 566 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 567 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4758fed | 2013-10-17 17:56:25 +0200 | [diff] [blame] | 568 | snd_printdd("hda_codec: " |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 569 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 570 | nid, i, parm); |
| 571 | return 0; |
| 572 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 573 | parm >>= shift; |
| 574 | if (range_val) { |
| 575 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 576 | if (!prev_nid || prev_nid >= val) { |
| 577 | snd_printk(KERN_WARNING "hda_codec: " |
| 578 | "invalid dep_range_val %x:%x\n", |
| 579 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 580 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 582 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 583 | if (conn_list) { |
| 584 | if (conns >= max_conns) |
| 585 | return -ENOSPC; |
| 586 | conn_list[conns] = n; |
| 587 | } |
| 588 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 589 | } |
| 590 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 591 | if (conn_list) { |
| 592 | if (conns >= max_conns) |
| 593 | return -ENOSPC; |
| 594 | conn_list[conns] = val; |
| 595 | } |
| 596 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 598 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | return conns; |
| 601 | } |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 604 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 605 | * @codec: the HDA codec |
| 606 | * @nid: NID to parse |
| 607 | * @len: number of connection list entries |
| 608 | * @list: the list of connection entries |
| 609 | * |
| 610 | * Add or modify the given connection-list to the cache. If the corresponding |
| 611 | * cache already exists, invalidate it and append a new one. |
| 612 | * |
| 613 | * Returns zero or a negative error code. |
| 614 | */ |
| 615 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 616 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 618 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 619 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 620 | p = lookup_conn_list(codec, nid); |
| 621 | if (p) { |
| 622 | list_del(&p->list); |
| 623 | kfree(p); |
| 624 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 625 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 626 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 628 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 629 | |
| 630 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 631 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 632 | * @codec: the HDA codec |
| 633 | * @mux: NID containing the list |
| 634 | * @nid: NID to select |
| 635 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 636 | * |
| 637 | * Parses the connection list of the widget @mux and checks whether the |
| 638 | * widget @nid is present. If it is, return the connection index. |
| 639 | * Otherwise it returns -1. |
| 640 | */ |
| 641 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 642 | hda_nid_t nid, int recursive) |
| 643 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 644 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 645 | int i, nums; |
| 646 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 647 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 648 | for (i = 0; i < nums; i++) |
| 649 | if (conn[i] == nid) |
| 650 | return i; |
| 651 | if (!recursive) |
| 652 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 653 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 654 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 655 | return -1; |
| 656 | } |
| 657 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 658 | for (i = 0; i < nums; i++) { |
| 659 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 660 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 661 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 662 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 663 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 664 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 669 | |
| 670 | /* return DEVLIST_LEN parameter of the given widget */ |
| 671 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 672 | { |
| 673 | unsigned int wcaps = get_wcaps(codec, nid); |
| 674 | unsigned int parm; |
| 675 | |
| 676 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 677 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 678 | return 0; |
| 679 | |
| 680 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 681 | if (parm == -1 && codec->bus->rirb_error) |
| 682 | parm = 0; |
| 683 | return parm & AC_DEV_LIST_LEN_MASK; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * snd_hda_get_devices - copy device list without cache |
| 688 | * @codec: the HDA codec |
| 689 | * @nid: NID of the pin to parse |
| 690 | * @dev_list: device list array |
| 691 | * @max_devices: max. number of devices to store |
| 692 | * |
| 693 | * Copy the device list. This info is dynamic and so not cached. |
| 694 | * Currently called only from hda_proc.c, so not exported. |
| 695 | */ |
| 696 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 697 | u8 *dev_list, int max_devices) |
| 698 | { |
| 699 | unsigned int parm; |
| 700 | int i, dev_len, devices; |
| 701 | |
| 702 | parm = get_num_devices(codec, nid); |
| 703 | if (!parm) /* not multi-stream capable */ |
| 704 | return 0; |
| 705 | |
| 706 | dev_len = parm + 1; |
| 707 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 708 | |
| 709 | devices = 0; |
| 710 | while (devices < dev_len) { |
| 711 | parm = snd_hda_codec_read(codec, nid, 0, |
| 712 | AC_VERB_GET_DEVICE_LIST, devices); |
| 713 | if (parm == -1 && codec->bus->rirb_error) |
| 714 | break; |
| 715 | |
| 716 | for (i = 0; i < 8; i++) { |
| 717 | dev_list[devices] = (u8)parm; |
| 718 | parm >>= 4; |
| 719 | devices++; |
| 720 | if (devices >= dev_len) |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | return devices; |
| 725 | } |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | /** |
| 728 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 729 | * @bus: the BUS |
| 730 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 731 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 732 | * |
| 733 | * Adds the given event to the queue. The events are processed in |
| 734 | * the workqueue asynchronously. Call this function in the interrupt |
| 735 | * hanlder when RIRB receives an unsolicited event. |
| 736 | * |
| 737 | * Returns 0 if successful, or a negative error code. |
| 738 | */ |
| 739 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 740 | { |
| 741 | struct hda_bus_unsolicited *unsol; |
| 742 | unsigned int wp; |
| 743 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 744 | if (!bus || !bus->workq) |
| 745 | return 0; |
| 746 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 747 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 748 | unsol = bus->unsol; |
| 749 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return 0; |
| 751 | |
| 752 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 753 | unsol->wp = wp; |
| 754 | |
| 755 | wp <<= 1; |
| 756 | unsol->queue[wp] = res; |
| 757 | unsol->queue[wp + 1] = res_ex; |
| 758 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 759 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | return 0; |
| 762 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 763 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 766 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 768 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 770 | struct hda_bus_unsolicited *unsol = |
| 771 | container_of(work, struct hda_bus_unsolicited, work); |
| 772 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | struct hda_codec *codec; |
| 774 | unsigned int rp, caddr, res; |
| 775 | |
| 776 | while (unsol->rp != unsol->wp) { |
| 777 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 778 | unsol->rp = rp; |
| 779 | rp <<= 1; |
| 780 | res = unsol->queue[rp]; |
| 781 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 782 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | continue; |
| 784 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 785 | if (codec && codec->patch_ops.unsol_event) |
| 786 | codec->patch_ops.unsol_event(codec, res); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * initialize unsolicited queue |
| 792 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 793 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
| 795 | struct hda_bus_unsolicited *unsol; |
| 796 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 797 | if (bus->unsol) /* already initialized */ |
| 798 | return 0; |
| 799 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 800 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 801 | if (!unsol) { |
| 802 | snd_printk(KERN_ERR "hda_codec: " |
| 803 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return -ENOMEM; |
| 805 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 806 | INIT_WORK(&unsol->work, process_unsol_events); |
| 807 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | bus->unsol = unsol; |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * destructor |
| 814 | */ |
| 815 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 816 | |
| 817 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 818 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 819 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 821 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 823 | if (bus->workq) |
| 824 | flush_workqueue(bus->workq); |
| 825 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 827 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | snd_hda_codec_free(codec); |
| 829 | } |
| 830 | if (bus->ops.private_free) |
| 831 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 832 | if (bus->workq) |
| 833 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | kfree(bus); |
| 835 | return 0; |
| 836 | } |
| 837 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 838 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
| 840 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 841 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return snd_hda_bus_free(bus); |
| 843 | } |
| 844 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 845 | #ifdef CONFIG_SND_HDA_HWDEP |
| 846 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 847 | { |
| 848 | struct hda_bus *bus = device->device_data; |
| 849 | struct hda_codec *codec; |
| 850 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 851 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 852 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 853 | } |
| 854 | return 0; |
| 855 | } |
| 856 | #else |
| 857 | #define snd_hda_bus_dev_register NULL |
| 858 | #endif |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | /** |
| 861 | * snd_hda_bus_new - create a HDA bus |
| 862 | * @card: the card entry |
| 863 | * @temp: the template for hda_bus information |
| 864 | * @busp: the pointer to store the created bus instance |
| 865 | * |
| 866 | * Returns 0 if successful, or a negative error code. |
| 867 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 868 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 869 | const struct hda_bus_template *temp, |
| 870 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
| 872 | struct hda_bus *bus; |
| 873 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 874 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 875 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | .dev_free = snd_hda_bus_dev_free, |
| 877 | }; |
| 878 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 879 | if (snd_BUG_ON(!temp)) |
| 880 | return -EINVAL; |
| 881 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 882 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | if (busp) |
| 885 | *busp = NULL; |
| 886 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 887 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (bus == NULL) { |
| 889 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 890 | return -ENOMEM; |
| 891 | } |
| 892 | |
| 893 | bus->card = card; |
| 894 | bus->private_data = temp->private_data; |
| 895 | bus->pci = temp->pci; |
| 896 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 897 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | bus->ops = temp->ops; |
| 899 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 900 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 901 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | INIT_LIST_HEAD(&bus->codec_list); |
| 903 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 904 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 905 | "hd-audio%d", card->number); |
| 906 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 907 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 908 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 909 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 910 | kfree(bus); |
| 911 | return -ENOMEM; |
| 912 | } |
| 913 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 914 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 915 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | snd_hda_bus_free(bus); |
| 917 | return err; |
| 918 | } |
| 919 | if (busp) |
| 920 | *busp = bus; |
| 921 | return 0; |
| 922 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 923 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 925 | #ifdef CONFIG_SND_HDA_GENERIC |
| 926 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 927 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 928 | #else |
| 929 | #define is_generic_config(codec) 0 |
| 930 | #endif |
| 931 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 932 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 933 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 934 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 935 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 936 | #endif |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * find a matching codec preset |
| 940 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 941 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 942 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 944 | struct hda_codec_preset_list *tbl; |
| 945 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 946 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 948 | again: |
| 949 | mutex_lock(&preset_mutex); |
| 950 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 951 | if (!try_module_get(tbl->owner)) { |
| 952 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 953 | continue; |
| 954 | } |
| 955 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 957 | if (preset->afg && preset->afg != codec->afg) |
| 958 | continue; |
| 959 | if (preset->mfg && preset->mfg != codec->mfg) |
| 960 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 961 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 963 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 964 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 965 | preset->rev == codec->revision_id)) { |
| 966 | mutex_unlock(&preset_mutex); |
| 967 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 971 | module_put(tbl->owner); |
| 972 | } |
| 973 | mutex_unlock(&preset_mutex); |
| 974 | |
| 975 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 976 | char name[32]; |
| 977 | if (!mod_requested) |
| 978 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 979 | codec->vendor_id); |
| 980 | else |
| 981 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 982 | (codec->vendor_id >> 16) & 0xffff); |
| 983 | request_module(name); |
| 984 | mod_requested++; |
| 985 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 991 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 993 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
| 995 | const struct hda_vendor_id *c; |
| 996 | const char *vendor = NULL; |
| 997 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 998 | char tmp[16]; |
| 999 | |
| 1000 | if (codec->vendor_name) |
| 1001 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | for (c = hda_vendor_ids; c->id; c++) { |
| 1004 | if (c->id == vendor_id) { |
| 1005 | vendor = c->name; |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1009 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1011 | vendor = tmp; |
| 1012 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1013 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1014 | if (!codec->vendor_name) |
| 1015 | return -ENOMEM; |
| 1016 | |
| 1017 | get_chip_name: |
| 1018 | if (codec->chip_name) |
| 1019 | return 0; |
| 1020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1022 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1023 | else { |
| 1024 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1025 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1026 | } |
| 1027 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1028 | return -ENOMEM; |
| 1029 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1033 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1035 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1037 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | hda_nid_t nid; |
| 1039 | |
| 1040 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1041 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1042 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1043 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1044 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1045 | case AC_GRP_AUDIO_FUNCTION: |
| 1046 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1047 | codec->afg_function_id = function_id & 0xff; |
| 1048 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1049 | break; |
| 1050 | case AC_GRP_MODEM_FUNCTION: |
| 1051 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1052 | codec->mfg_function_id = function_id & 0xff; |
| 1053 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1054 | break; |
| 1055 | default: |
| 1056 | break; |
| 1057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1062 | * read widget caps for each widget and store in cache |
| 1063 | */ |
| 1064 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1065 | { |
| 1066 | int i; |
| 1067 | hda_nid_t nid; |
| 1068 | |
| 1069 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1070 | &codec->start_nid); |
| 1071 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1072 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1073 | return -ENOMEM; |
| 1074 | nid = codec->start_nid; |
| 1075 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1076 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1077 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1081 | /* read all pin default configurations and save codec->init_pins */ |
| 1082 | static int read_pin_defaults(struct hda_codec *codec) |
| 1083 | { |
| 1084 | int i; |
| 1085 | hda_nid_t nid = codec->start_nid; |
| 1086 | |
| 1087 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1088 | struct hda_pincfg *pin; |
| 1089 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1090 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1091 | if (wid_type != AC_WID_PIN) |
| 1092 | continue; |
| 1093 | pin = snd_array_new(&codec->init_pins); |
| 1094 | if (!pin) |
| 1095 | return -ENOMEM; |
| 1096 | pin->nid = nid; |
| 1097 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1098 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1099 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1100 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1101 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* look up the given pin config list and return the item matching with NID */ |
| 1107 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1108 | struct snd_array *array, |
| 1109 | hda_nid_t nid) |
| 1110 | { |
| 1111 | int i; |
| 1112 | for (i = 0; i < array->used; i++) { |
| 1113 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1114 | if (pin->nid == nid) |
| 1115 | return pin; |
| 1116 | } |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1120 | /* set the current pin config value for the given NID. |
| 1121 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1122 | */ |
| 1123 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1124 | hda_nid_t nid, unsigned int cfg) |
| 1125 | { |
| 1126 | struct hda_pincfg *pin; |
| 1127 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1128 | /* the check below may be invalid when pins are added by a fixup |
| 1129 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1130 | * for now |
| 1131 | */ |
| 1132 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1133 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1134 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1135 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1136 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1137 | pin = look_up_pincfg(codec, list, nid); |
| 1138 | if (!pin) { |
| 1139 | pin = snd_array_new(list); |
| 1140 | if (!pin) |
| 1141 | return -ENOMEM; |
| 1142 | pin->nid = nid; |
| 1143 | } |
| 1144 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1148 | /** |
| 1149 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1150 | * @codec: the HDA codec |
| 1151 | * @nid: NID to set the pin config |
| 1152 | * @cfg: the pin default config value |
| 1153 | * |
| 1154 | * Override a pin default configuration value in the cache. |
| 1155 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1156 | * priority than the real hardware value. |
| 1157 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1158 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1159 | hda_nid_t nid, unsigned int cfg) |
| 1160 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1161 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1162 | } |
| 1163 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1164 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1165 | /** |
| 1166 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1167 | * @codec: the HDA codec |
| 1168 | * @nid: NID to get the pin config |
| 1169 | * |
| 1170 | * Get the current pin config value of the given pin NID. |
| 1171 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1172 | * returns the cached value. |
| 1173 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1174 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1175 | { |
| 1176 | struct hda_pincfg *pin; |
| 1177 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1178 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1179 | { |
| 1180 | unsigned int cfg = 0; |
| 1181 | mutex_lock(&codec->user_mutex); |
| 1182 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1183 | if (pin) |
| 1184 | cfg = pin->cfg; |
| 1185 | mutex_unlock(&codec->user_mutex); |
| 1186 | if (cfg) |
| 1187 | return cfg; |
| 1188 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1189 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1190 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1191 | if (pin) |
| 1192 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1193 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1194 | if (pin) |
| 1195 | return pin->cfg; |
| 1196 | return 0; |
| 1197 | } |
| 1198 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1199 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1200 | /* remember the current pinctl target value */ |
| 1201 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1202 | unsigned int val) |
| 1203 | { |
| 1204 | struct hda_pincfg *pin; |
| 1205 | |
| 1206 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1207 | if (!pin) |
| 1208 | return -EINVAL; |
| 1209 | pin->target = val; |
| 1210 | return 0; |
| 1211 | } |
| 1212 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target); |
| 1213 | |
| 1214 | /* return the current pinctl target value */ |
| 1215 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1216 | { |
| 1217 | struct hda_pincfg *pin; |
| 1218 | |
| 1219 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1220 | if (!pin) |
| 1221 | return 0; |
| 1222 | return pin->target; |
| 1223 | } |
| 1224 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target); |
| 1225 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1226 | /** |
| 1227 | * snd_hda_shutup_pins - Shut up all pins |
| 1228 | * @codec: the HDA codec |
| 1229 | * |
| 1230 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1231 | * The controls aren't cached so that they can be resumed properly. |
| 1232 | */ |
| 1233 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1234 | { |
| 1235 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1236 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1237 | * the default pin setup at the next load of the driver |
| 1238 | */ |
| 1239 | if (codec->bus->shutdown) |
| 1240 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1241 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1242 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1243 | /* use read here for syncing after issuing each verb */ |
| 1244 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1245 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1246 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1247 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1248 | } |
| 1249 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1250 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1251 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1252 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1253 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1254 | { |
| 1255 | int i; |
| 1256 | if (!codec->pins_shutup) |
| 1257 | return; |
| 1258 | if (codec->bus->shutdown) |
| 1259 | return; |
| 1260 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1261 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1262 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1263 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1264 | pin->ctrl); |
| 1265 | } |
| 1266 | codec->pins_shutup = 0; |
| 1267 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1268 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1269 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1270 | static void hda_jackpoll_work(struct work_struct *work) |
| 1271 | { |
| 1272 | struct hda_codec *codec = |
| 1273 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1274 | |
| 1275 | snd_hda_jack_set_dirty_all(codec); |
| 1276 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1277 | |
| 1278 | if (!codec->jackpoll_interval) |
| 1279 | return; |
| 1280 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1281 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1282 | codec->jackpoll_interval); |
| 1283 | } |
| 1284 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1285 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1286 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1287 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1288 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1289 | /* release all pincfg lists */ |
| 1290 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1291 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1292 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1293 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1294 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1295 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1296 | snd_array_free(&codec->init_pins); |
| 1297 | } |
| 1298 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1299 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1300 | * audio-converter setup caches |
| 1301 | */ |
| 1302 | struct hda_cvt_setup { |
| 1303 | hda_nid_t nid; |
| 1304 | u8 stream_tag; |
| 1305 | u8 channel_id; |
| 1306 | u16 format_id; |
| 1307 | unsigned char active; /* cvt is currently used */ |
| 1308 | unsigned char dirty; /* setups should be cleared */ |
| 1309 | }; |
| 1310 | |
| 1311 | /* get or create a cache entry for the given audio converter NID */ |
| 1312 | static struct hda_cvt_setup * |
| 1313 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1314 | { |
| 1315 | struct hda_cvt_setup *p; |
| 1316 | int i; |
| 1317 | |
| 1318 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1319 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1320 | if (p->nid == nid) |
| 1321 | return p; |
| 1322 | } |
| 1323 | p = snd_array_new(&codec->cvt_setups); |
| 1324 | if (p) |
| 1325 | p->nid = nid; |
| 1326 | return p; |
| 1327 | } |
| 1328 | |
| 1329 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1330 | * Dynamic symbol binding for the codec parsers |
| 1331 | */ |
| 1332 | #ifdef MODULE |
| 1333 | #define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym)) |
| 1334 | #define unload_parser_addr(addr) symbol_put_addr(addr) |
| 1335 | #else |
| 1336 | #define load_parser_sym(sym) (sym) |
| 1337 | #define unload_parser_addr(addr) do {} while (0) |
| 1338 | #endif |
| 1339 | |
| 1340 | #define load_parser(codec, sym) \ |
| 1341 | ((codec)->parser = load_parser_sym(sym)) |
| 1342 | |
| 1343 | static void unload_parser(struct hda_codec *codec) |
| 1344 | { |
| 1345 | if (codec->parser) { |
| 1346 | unload_parser_addr(codec->parser); |
| 1347 | codec->parser = NULL; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | * codec destructor |
| 1353 | */ |
| 1354 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1355 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1356 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1358 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1359 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1360 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1361 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1362 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1363 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1364 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1366 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1367 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1368 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1369 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1370 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1372 | if (codec->patch_ops.free) |
| 1373 | codec->patch_ops.free(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1374 | #ifdef CONFIG_PM |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 1375 | if (!codec->pm_down_notified) /* cancel leftover refcounts */ |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1376 | hda_call_pm_notify(codec->bus, false); |
| 1377 | #endif |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1378 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1379 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1380 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1381 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1382 | kfree(codec->vendor_name); |
| 1383 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1384 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1385 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | kfree(codec); |
| 1387 | } |
| 1388 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1389 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1390 | hda_nid_t fg, unsigned int power_state); |
| 1391 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1392 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1393 | unsigned int power_state); |
| 1394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | /** |
| 1396 | * snd_hda_codec_new - create a HDA codec |
| 1397 | * @bus: the bus to assign |
| 1398 | * @codec_addr: the codec address |
| 1399 | * @codecp: the pointer to store the generated codec |
| 1400 | * |
| 1401 | * Returns 0 if successful, or a negative error code. |
| 1402 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1403 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1404 | unsigned int codec_addr, |
| 1405 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { |
| 1407 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1408 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1409 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | int err; |
| 1411 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1412 | if (snd_BUG_ON(!bus)) |
| 1413 | return -EINVAL; |
| 1414 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1415 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1418 | snd_printk(KERN_ERR "hda_codec: " |
| 1419 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return -EBUSY; |
| 1421 | } |
| 1422 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1423 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | if (codec == NULL) { |
| 1425 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1426 | return -ENOMEM; |
| 1427 | } |
| 1428 | |
| 1429 | codec->bus = bus; |
| 1430 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1431 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1432 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1433 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1434 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1435 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1436 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1437 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1438 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1439 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1440 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1441 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1442 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1443 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1444 | INIT_LIST_HEAD(&codec->conn_list); |
| 1445 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1446 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1448 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1449 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1450 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1451 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1452 | * the caller has to power down appropriatley after initialization |
| 1453 | * phase. |
| 1454 | */ |
| 1455 | hda_keep_power_on(codec); |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1456 | hda_call_pm_notify(bus, true); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1457 | #endif |
| 1458 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1459 | if (codec->bus->modelname) { |
| 1460 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1461 | if (!codec->modelname) { |
| 1462 | snd_hda_codec_free(codec); |
| 1463 | return -ENODEV; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | list_add_tail(&codec->list, &bus->codec_list); |
| 1468 | bus->caddr_tbl[codec_addr] = codec; |
| 1469 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1470 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1471 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1472 | if (codec->vendor_id == -1) |
| 1473 | /* read again, hopefully the access method was corrected |
| 1474 | * in the last read... |
| 1475 | */ |
| 1476 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1477 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1478 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1479 | AC_PAR_SUBSYSTEM_ID); |
| 1480 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1481 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1483 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1484 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1485 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1486 | err = -ENODEV; |
| 1487 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1490 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1491 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1492 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1493 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1494 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1495 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1496 | err = read_pin_defaults(codec); |
| 1497 | if (err < 0) |
| 1498 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1499 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1500 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1501 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1502 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1503 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1506 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1507 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1508 | AC_PWRST_CLKSTOP); |
| 1509 | if (!codec->d3_stop_clk) |
| 1510 | bus->power_keep_link_on = 1; |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1511 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1512 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1513 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1514 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1515 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1516 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1517 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1518 | snd_hda_codec_proc_new(codec); |
| 1519 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1520 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1521 | |
| 1522 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1523 | codec->subsystem_id, codec->revision_id); |
| 1524 | snd_component_add(codec->bus->card, component); |
| 1525 | |
| 1526 | if (codecp) |
| 1527 | *codecp = codec; |
| 1528 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1529 | |
| 1530 | error: |
| 1531 | snd_hda_codec_free(codec); |
| 1532 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1533 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1534 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1535 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1536 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1537 | { |
| 1538 | hda_nid_t fg; |
| 1539 | int err; |
| 1540 | |
| 1541 | /* Assume the function group node does not change, |
| 1542 | * only the widget nodes may change. |
| 1543 | */ |
| 1544 | kfree(codec->wcaps); |
| 1545 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1546 | err = read_widget_caps(codec, fg); |
| 1547 | if (err < 0) { |
| 1548 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1549 | return err; |
| 1550 | } |
| 1551 | |
| 1552 | snd_array_free(&codec->init_pins); |
| 1553 | err = read_pin_defaults(codec); |
| 1554 | |
| 1555 | return err; |
| 1556 | } |
| 1557 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets); |
| 1558 | |
| 1559 | |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame^] | 1560 | #ifdef CONFIG_SND_HDA_CODEC_HDMI |
| 1561 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1562 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1563 | { |
| 1564 | hda_nid_t nid = codec->start_nid; |
| 1565 | int i; |
| 1566 | |
| 1567 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1568 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1569 | switch (get_wcaps_type(wcaps)) { |
| 1570 | case AC_WID_AUD_IN: |
| 1571 | return false; /* HDMI parser supports only HDMI out */ |
| 1572 | case AC_WID_AUD_OUT: |
| 1573 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1574 | return false; |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | return true; |
| 1579 | } |
| 1580 | #else |
| 1581 | /* no HDMI codec parser support */ |
| 1582 | #define is_likely_hdmi_codec(codec) false |
| 1583 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1584 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1585 | /** |
| 1586 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1587 | * @codec: the HDA codec |
| 1588 | * |
| 1589 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1590 | * patch instance. |
| 1591 | * |
| 1592 | * Returns 0 if successful or a negative error code. |
| 1593 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1594 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1595 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1596 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1597 | int err; |
| 1598 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1599 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1600 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1601 | err = get_codec_name(codec); |
| 1602 | if (err < 0) |
| 1603 | return err; |
| 1604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1606 | if (!is_generic_config(codec) && codec->preset) |
| 1607 | patch = codec->preset->patch; |
| 1608 | if (!patch) { |
| 1609 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame^] | 1610 | if (is_likely_hdmi_codec(codec)) |
| 1611 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1612 | #ifdef CONFIG_SND_HDA_GENERIC |
| 1613 | if (!patch) |
| 1614 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1615 | #endif |
| 1616 | if (!patch) { |
| 1617 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
| 1618 | return -ENODEV; |
| 1619 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1622 | err = patch(codec); |
| 1623 | if (err < 0) { |
| 1624 | unload_parser(codec); |
| 1625 | return err; |
| 1626 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1627 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1628 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1629 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1630 | if (err < 0) |
| 1631 | return err; |
| 1632 | } |
| 1633 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1634 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1635 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1636 | snprintf(codec->bus->card->mixername, |
| 1637 | sizeof(codec->bus->card->mixername), |
| 1638 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1639 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1641 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1643 | /* update the stream-id if changed */ |
| 1644 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1645 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1646 | u32 stream_tag, int channel_id) |
| 1647 | { |
| 1648 | unsigned int oldval, newval; |
| 1649 | |
| 1650 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1651 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1652 | newval = (stream_tag << 4) | channel_id; |
| 1653 | if (oldval != newval) |
| 1654 | snd_hda_codec_write(codec, nid, 0, |
| 1655 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1656 | newval); |
| 1657 | p->stream_tag = stream_tag; |
| 1658 | p->channel_id = channel_id; |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | /* update the format-id if changed */ |
| 1663 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1664 | hda_nid_t nid, int format) |
| 1665 | { |
| 1666 | unsigned int oldval; |
| 1667 | |
| 1668 | if (p->format_id != format) { |
| 1669 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1670 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1671 | if (oldval != format) { |
| 1672 | msleep(1); |
| 1673 | snd_hda_codec_write(codec, nid, 0, |
| 1674 | AC_VERB_SET_STREAM_FORMAT, |
| 1675 | format); |
| 1676 | } |
| 1677 | p->format_id = format; |
| 1678 | } |
| 1679 | } |
| 1680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | /** |
| 1682 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1683 | * @codec: the CODEC to set up |
| 1684 | * @nid: the NID to set up |
| 1685 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1686 | * @channel_id: channel id to pass, zero based. |
| 1687 | * @format: stream format. |
| 1688 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1689 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1690 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | int channel_id, int format) |
| 1692 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1693 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1694 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1695 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1696 | int i; |
| 1697 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1698 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1699 | return; |
| 1700 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1701 | snd_printdd("hda_codec_setup_stream: " |
| 1702 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1704 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1705 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1706 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1707 | |
| 1708 | if (codec->pcm_format_first) |
| 1709 | update_pcm_format(codec, p, nid, format); |
| 1710 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1711 | if (!codec->pcm_format_first) |
| 1712 | update_pcm_format(codec, p, nid, format); |
| 1713 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1714 | p->active = 1; |
| 1715 | p->dirty = 0; |
| 1716 | |
| 1717 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1718 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1719 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1720 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1721 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1722 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1723 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1724 | p->dirty = 1; |
| 1725 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1728 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1730 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1731 | struct hda_cvt_setup *q); |
| 1732 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1733 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1734 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1735 | * @codec: the CODEC to clean up |
| 1736 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1737 | * @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] | 1738 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1739 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1740 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1741 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1742 | struct hda_cvt_setup *p; |
| 1743 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1744 | if (!nid) |
| 1745 | return; |
| 1746 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1747 | if (codec->no_sticky_stream) |
| 1748 | do_now = 1; |
| 1749 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1750 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1751 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1752 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1753 | /* here we just clear the active flag when do_now isn't set; |
| 1754 | * actual clean-ups will be done later in |
| 1755 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1756 | */ |
| 1757 | if (do_now) |
| 1758 | really_cleanup_stream(codec, p); |
| 1759 | else |
| 1760 | p->active = 0; |
| 1761 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1762 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1763 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1764 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1765 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1766 | struct hda_cvt_setup *q) |
| 1767 | { |
| 1768 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1769 | if (q->stream_tag || q->channel_id) |
| 1770 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1771 | if (q->format_id) |
| 1772 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1773 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1774 | memset(q, 0, sizeof(*q)); |
| 1775 | q->nid = nid; |
| 1776 | } |
| 1777 | |
| 1778 | /* clean up the all conflicting obsolete streams */ |
| 1779 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1780 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1781 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1782 | int i; |
| 1783 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1784 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1785 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1786 | struct hda_cvt_setup *p; |
| 1787 | p = snd_array_elem(&c->cvt_setups, i); |
| 1788 | if (p->dirty) |
| 1789 | really_cleanup_stream(c, p); |
| 1790 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1794 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1795 | /* clean up all streams; called from suspend */ |
| 1796 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1797 | { |
| 1798 | int i; |
| 1799 | |
| 1800 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1801 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1802 | if (p->stream_tag) |
| 1803 | really_cleanup_stream(codec, p); |
| 1804 | } |
| 1805 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1806 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | /* |
| 1809 | * amp access functions |
| 1810 | */ |
| 1811 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1812 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1813 | #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] | 1814 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1815 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1816 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1818 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
| 1820 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1821 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1822 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1824 | memset(cache, 0, sizeof(*cache)); |
| 1825 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1826 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1829 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1830 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1831 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1835 | 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] | 1836 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1837 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1838 | u16 cur = cache->hash[idx]; |
| 1839 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
| 1841 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1842 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | if (info->key == key) |
| 1844 | return info; |
| 1845 | cur = info->next; |
| 1846 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1847 | return NULL; |
| 1848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1850 | /* query the hash. allocate an entry if not found. */ |
| 1851 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1852 | u32 key) |
| 1853 | { |
| 1854 | struct hda_cache_head *info = get_hash(cache, key); |
| 1855 | if (!info) { |
| 1856 | u16 idx, cur; |
| 1857 | /* add a new hash entry */ |
| 1858 | info = snd_array_new(&cache->buf); |
| 1859 | if (!info) |
| 1860 | return NULL; |
| 1861 | cur = snd_array_index(&cache->buf, info); |
| 1862 | info->key = key; |
| 1863 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1864 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1865 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1866 | info->next = cache->hash[idx]; |
| 1867 | cache->hash[idx] = cur; |
| 1868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | return info; |
| 1870 | } |
| 1871 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1872 | /* query and allocate an amp hash entry */ |
| 1873 | static inline struct hda_amp_info * |
| 1874 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1875 | { |
| 1876 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1877 | } |
| 1878 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1879 | /* overwrite the value with the key in the caps hash */ |
| 1880 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1881 | { |
| 1882 | struct hda_amp_info *info; |
| 1883 | |
| 1884 | mutex_lock(&codec->hash_mutex); |
| 1885 | info = get_alloc_amp_hash(codec, key); |
| 1886 | if (!info) { |
| 1887 | mutex_unlock(&codec->hash_mutex); |
| 1888 | return -EINVAL; |
| 1889 | } |
| 1890 | info->amp_caps = val; |
| 1891 | info->head.val |= INFO_AMP_CAPS; |
| 1892 | mutex_unlock(&codec->hash_mutex); |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /* query the value from the caps hash; if not found, fetch the current |
| 1897 | * value from the given function and store in the hash |
| 1898 | */ |
| 1899 | static unsigned int |
| 1900 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1901 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1902 | { |
| 1903 | struct hda_amp_info *info; |
| 1904 | unsigned int val; |
| 1905 | |
| 1906 | mutex_lock(&codec->hash_mutex); |
| 1907 | info = get_alloc_amp_hash(codec, key); |
| 1908 | if (!info) { |
| 1909 | mutex_unlock(&codec->hash_mutex); |
| 1910 | return 0; |
| 1911 | } |
| 1912 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1913 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1914 | val = func(codec, nid, dir); |
| 1915 | write_caps_hash(codec, key, val); |
| 1916 | } else { |
| 1917 | val = info->amp_caps; |
| 1918 | mutex_unlock(&codec->hash_mutex); |
| 1919 | } |
| 1920 | return val; |
| 1921 | } |
| 1922 | |
| 1923 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1924 | int direction) |
| 1925 | { |
| 1926 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1927 | nid = codec->afg; |
| 1928 | return snd_hda_param_read(codec, nid, |
| 1929 | direction == HDA_OUTPUT ? |
| 1930 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1931 | } |
| 1932 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1933 | /** |
| 1934 | * query_amp_caps - query AMP capabilities |
| 1935 | * @codec: the HD-auio codec |
| 1936 | * @nid: the NID to query |
| 1937 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1938 | * |
| 1939 | * Query AMP capabilities for the given widget and direction. |
| 1940 | * Returns the obtained capability bits. |
| 1941 | * |
| 1942 | * When cap bits have been already read, this doesn't read again but |
| 1943 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1945 | 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] | 1946 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1947 | return query_caps_hash(codec, nid, direction, |
| 1948 | HDA_HASH_KEY(nid, direction, 0), |
| 1949 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1951 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1953 | /** |
| 1954 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1955 | * @codec: the CODEC to clean up |
| 1956 | * @nid: the NID to clean up |
| 1957 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1958 | * @caps: the capability bits to set |
| 1959 | * |
| 1960 | * Override the cached AMP caps bits value by the given one. |
| 1961 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1962 | * e.g. limit to 0dB, etc. |
| 1963 | * |
| 1964 | * Returns zero if successful or a negative error code. |
| 1965 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1966 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1967 | unsigned int caps) |
| 1968 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1969 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1970 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1971 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1972 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1973 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1974 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1975 | { |
| 1976 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1977 | } |
| 1978 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1979 | /** |
| 1980 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1981 | * @codec: the HD-auio codec |
| 1982 | * @nid: the NID to query |
| 1983 | * |
| 1984 | * Query PIN capabilities for the given widget. |
| 1985 | * Returns the obtained capability bits. |
| 1986 | * |
| 1987 | * When cap bits have been already read, this doesn't read again but |
| 1988 | * returns the cached value. |
| 1989 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1990 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1991 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1992 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1993 | read_pin_cap); |
| 1994 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1995 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 1996 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1997 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1998 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1999 | * @codec: the CODEC |
| 2000 | * @nid: the NID to override |
| 2001 | * @caps: the capability bits to set |
| 2002 | * |
| 2003 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2004 | * |
| 2005 | * Returns zero if successful or a negative error code. |
| 2006 | */ |
| 2007 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2008 | unsigned int caps) |
| 2009 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2010 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2011 | } |
| 2012 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 2013 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2014 | /* read or sync the hash value with the current value; |
| 2015 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2017 | static struct hda_amp_info * |
| 2018 | update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2019 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2021 | struct hda_amp_info *info; |
| 2022 | unsigned int parm, val = 0; |
| 2023 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2025 | retry: |
| 2026 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2027 | if (!info) |
| 2028 | return NULL; |
| 2029 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2030 | if (!val_read) { |
| 2031 | mutex_unlock(&codec->hash_mutex); |
| 2032 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2033 | parm |= direction == HDA_OUTPUT ? |
| 2034 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2035 | parm |= index; |
| 2036 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2037 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2038 | val &= 0xff; |
| 2039 | val_read = true; |
| 2040 | mutex_lock(&codec->hash_mutex); |
| 2041 | goto retry; |
| 2042 | } |
| 2043 | info->vol[ch] = val; |
| 2044 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2045 | } else if (init_only) |
| 2046 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2047 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2051 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2053 | static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2054 | hda_nid_t nid, int ch, int direction, int index, |
| 2055 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | { |
| 2057 | u32 parm; |
| 2058 | |
| 2059 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2060 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2061 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2062 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2063 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2064 | ; /* set the zero value as a fake mute */ |
| 2065 | else |
| 2066 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2068 | } |
| 2069 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2070 | /** |
| 2071 | * snd_hda_codec_amp_read - Read AMP value |
| 2072 | * @codec: HD-audio codec |
| 2073 | * @nid: NID to read the AMP value |
| 2074 | * @ch: channel (left=0 or right=1) |
| 2075 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2076 | * @index: the index value (only for input direction) |
| 2077 | * |
| 2078 | * 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] | 2079 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2080 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2081 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2083 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2084 | unsigned int val = 0; |
| 2085 | |
| 2086 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2087 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2088 | if (info) |
| 2089 | val = info->vol[ch]; |
| 2090 | mutex_unlock(&codec->hash_mutex); |
| 2091 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2093 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2095 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2096 | int direction, int idx, int mask, int val, |
| 2097 | bool init_only) |
| 2098 | { |
| 2099 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2100 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2101 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2102 | |
| 2103 | if (snd_BUG_ON(mask & ~0xff)) |
| 2104 | mask &= 0xff; |
| 2105 | val &= mask; |
| 2106 | |
| 2107 | mutex_lock(&codec->hash_mutex); |
| 2108 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2109 | if (!info) { |
| 2110 | mutex_unlock(&codec->hash_mutex); |
| 2111 | return 0; |
| 2112 | } |
| 2113 | val |= info->vol[ch] & ~mask; |
| 2114 | if (info->vol[ch] == val) { |
| 2115 | mutex_unlock(&codec->hash_mutex); |
| 2116 | return 0; |
| 2117 | } |
| 2118 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2119 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2120 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2121 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2122 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2123 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2124 | return 1; |
| 2125 | } |
| 2126 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2127 | /** |
| 2128 | * snd_hda_codec_amp_update - update the AMP value |
| 2129 | * @codec: HD-audio codec |
| 2130 | * @nid: NID to read the AMP value |
| 2131 | * @ch: channel (left=0 or right=1) |
| 2132 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2133 | * @idx: the index value (only for input direction) |
| 2134 | * @mask: bit mask to set |
| 2135 | * @val: the bits value to set |
| 2136 | * |
| 2137 | * Update the AMP value with a bit mask. |
| 2138 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2139 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2140 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2141 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2143 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2145 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2147 | /** |
| 2148 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2149 | * @codec: HD-audio codec |
| 2150 | * @nid: NID to read the AMP value |
| 2151 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2152 | * @idx: the index value (only for input direction) |
| 2153 | * @mask: bit mask to set |
| 2154 | * @val: the bits value to set |
| 2155 | * |
| 2156 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2157 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2158 | */ |
| 2159 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2160 | int direction, int idx, int mask, int val) |
| 2161 | { |
| 2162 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2163 | |
| 2164 | if (snd_BUG_ON(mask & ~0xff)) |
| 2165 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2166 | for (ch = 0; ch < 2; ch++) |
| 2167 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2168 | idx, mask, val); |
| 2169 | return ret; |
| 2170 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2171 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2172 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2173 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2174 | * the first access. If the amp was already initialized / updated beforehand, |
| 2175 | * this does nothing. |
| 2176 | */ |
| 2177 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2178 | int dir, int idx, int mask, int val) |
| 2179 | { |
| 2180 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2181 | } |
| 2182 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); |
| 2183 | |
| 2184 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2185 | int dir, int idx, int mask, int val) |
| 2186 | { |
| 2187 | int ch, ret = 0; |
| 2188 | |
| 2189 | if (snd_BUG_ON(mask & ~0xff)) |
| 2190 | mask &= 0xff; |
| 2191 | for (ch = 0; ch < 2; ch++) |
| 2192 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2193 | idx, mask, val); |
| 2194 | return ret; |
| 2195 | } |
| 2196 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); |
| 2197 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2198 | /** |
| 2199 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2200 | * @codec: HD-audio codec |
| 2201 | * |
| 2202 | * Resume the all amp commands from the cache. |
| 2203 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2204 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2205 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2206 | int i; |
| 2207 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2208 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2209 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2210 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2211 | struct hda_amp_info *buffer; |
| 2212 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2213 | hda_nid_t nid; |
| 2214 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2215 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2216 | |
| 2217 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2218 | if (!buffer->head.dirty) |
| 2219 | continue; |
| 2220 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2221 | info = *buffer; |
| 2222 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2223 | if (!key) |
| 2224 | continue; |
| 2225 | nid = key & 0xff; |
| 2226 | idx = (key >> 16) & 0xff; |
| 2227 | dir = (key >> 24) & 0xff; |
| 2228 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2229 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2230 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2231 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2232 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2233 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2234 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2235 | } |
| 2236 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2237 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2238 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2239 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2241 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2242 | unsigned int ofs) |
| 2243 | { |
| 2244 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2245 | /* get num steps */ |
| 2246 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2247 | if (ofs < caps) |
| 2248 | caps -= ofs; |
| 2249 | return caps; |
| 2250 | } |
| 2251 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2252 | /** |
| 2253 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2254 | * |
| 2255 | * The control element is supposed to have the private_value field |
| 2256 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2257 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2258 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2259 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | { |
| 2261 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2262 | u16 nid = get_amp_nid(kcontrol); |
| 2263 | u8 chs = get_amp_channels(kcontrol); |
| 2264 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2265 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2267 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2268 | uinfo->count = chs == 3 ? 2 : 1; |
| 2269 | uinfo->value.integer.min = 0; |
| 2270 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2271 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2272 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2273 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2274 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | return -EINVAL; |
| 2276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | return 0; |
| 2278 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2279 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2281 | |
| 2282 | static inline unsigned int |
| 2283 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2284 | int ch, int dir, int idx, unsigned int ofs) |
| 2285 | { |
| 2286 | unsigned int val; |
| 2287 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2288 | val &= HDA_AMP_VOLMASK; |
| 2289 | if (val >= ofs) |
| 2290 | val -= ofs; |
| 2291 | else |
| 2292 | val = 0; |
| 2293 | return val; |
| 2294 | } |
| 2295 | |
| 2296 | static inline int |
| 2297 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2298 | int ch, int dir, int idx, unsigned int ofs, |
| 2299 | unsigned int val) |
| 2300 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2301 | unsigned int maxval; |
| 2302 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2303 | if (val > 0) |
| 2304 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2305 | /* ofs = 0: raw max value */ |
| 2306 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2307 | if (val > maxval) |
| 2308 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2309 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2310 | HDA_AMP_VOLMASK, val); |
| 2311 | } |
| 2312 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2313 | /** |
| 2314 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2315 | * |
| 2316 | * The control element is supposed to have the private_value field |
| 2317 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2318 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2319 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2320 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | { |
| 2322 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2323 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2324 | int chs = get_amp_channels(kcontrol); |
| 2325 | int dir = get_amp_direction(kcontrol); |
| 2326 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2327 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | long *valp = ucontrol->value.integer.value; |
| 2329 | |
| 2330 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2331 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2333 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | return 0; |
| 2335 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2336 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2338 | /** |
| 2339 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2340 | * |
| 2341 | * The control element is supposed to have the private_value field |
| 2342 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2343 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2344 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2345 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | { |
| 2347 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2348 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2349 | int chs = get_amp_channels(kcontrol); |
| 2350 | int dir = get_amp_direction(kcontrol); |
| 2351 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2352 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | long *valp = ucontrol->value.integer.value; |
| 2354 | int change = 0; |
| 2355 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2356 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2357 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2358 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2359 | valp++; |
| 2360 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2361 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2362 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2363 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | return change; |
| 2365 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2366 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2368 | /** |
| 2369 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2370 | * |
| 2371 | * The control element is supposed to have the private_value field |
| 2372 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2373 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2374 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2375 | unsigned int size, unsigned int __user *_tlv) |
| 2376 | { |
| 2377 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2378 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2379 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2380 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2381 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2382 | u32 caps, val1, val2; |
| 2383 | |
| 2384 | if (size < 4 * sizeof(unsigned int)) |
| 2385 | return -ENOMEM; |
| 2386 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2387 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2388 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2389 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2390 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2391 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2392 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2393 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2394 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2395 | return -EFAULT; |
| 2396 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2397 | return -EFAULT; |
| 2398 | if (put_user(val1, _tlv + 2)) |
| 2399 | return -EFAULT; |
| 2400 | if (put_user(val2, _tlv + 3)) |
| 2401 | return -EFAULT; |
| 2402 | return 0; |
| 2403 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2404 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2405 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2406 | /** |
| 2407 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2408 | * @codec: HD-audio codec |
| 2409 | * @nid: NID of a reference widget |
| 2410 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2411 | * @tlv: TLV data to be stored, at least 4 elements |
| 2412 | * |
| 2413 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2414 | * obtained from the reference NID. |
| 2415 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2416 | */ |
| 2417 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2418 | unsigned int *tlv) |
| 2419 | { |
| 2420 | u32 caps; |
| 2421 | int nums, step; |
| 2422 | |
| 2423 | caps = query_amp_caps(codec, nid, dir); |
| 2424 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2425 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2426 | step = (step + 1) * 25; |
| 2427 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2428 | tlv[1] = 2 * sizeof(unsigned int); |
| 2429 | tlv[2] = -nums * step; |
| 2430 | tlv[3] = step; |
| 2431 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2432 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2433 | |
| 2434 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2435 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2436 | find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2437 | { |
| 2438 | struct snd_ctl_elem_id id; |
| 2439 | memset(&id, 0, sizeof(id)); |
| 2440 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2441 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2442 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2443 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2444 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2445 | strcpy(id.name, name); |
| 2446 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2447 | } |
| 2448 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2449 | /** |
| 2450 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2451 | * @codec: HD-audio codec |
| 2452 | * @name: ctl id name string |
| 2453 | * |
| 2454 | * Get the control element with the given id string and IFACE_MIXER. |
| 2455 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2456 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2457 | const char *name) |
| 2458 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2459 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2460 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2461 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2462 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2463 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2464 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2465 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2466 | int i, idx; |
| 2467 | /* 16 ctlrs should be large enough */ |
| 2468 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2469 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2470 | return idx; |
| 2471 | } |
| 2472 | return -EBUSY; |
| 2473 | } |
| 2474 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2475 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2476 | * 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] | 2477 | * @codec: HD-audio codec |
| 2478 | * @nid: corresponding NID (optional) |
| 2479 | * @kctl: the control element to assign |
| 2480 | * |
| 2481 | * Add the given control element to an array inside the codec instance. |
| 2482 | * All control elements belonging to a codec are supposed to be added |
| 2483 | * by this function so that a proper clean-up works at the free or |
| 2484 | * reconfiguration time. |
| 2485 | * |
| 2486 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2487 | * The assignment is shown in the codec proc file. |
| 2488 | * |
| 2489 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2490 | * #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] | 2491 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2492 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2493 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2494 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2495 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2496 | { |
| 2497 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2498 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2499 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2500 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2501 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2502 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2503 | if (nid == 0) |
| 2504 | nid = get_amp_nid_(kctl->private_value); |
| 2505 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2506 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2507 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2508 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2509 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2510 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2511 | if (err < 0) |
| 2512 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2513 | item = snd_array_new(&codec->mixers); |
| 2514 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2515 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2516 | item->kctl = kctl; |
| 2517 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2518 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2519 | return 0; |
| 2520 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2521 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2522 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2523 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2524 | * snd_hda_add_nid - Assign a NID to a control element |
| 2525 | * @codec: HD-audio codec |
| 2526 | * @nid: corresponding NID (optional) |
| 2527 | * @kctl: the control element to assign |
| 2528 | * @index: index to kctl |
| 2529 | * |
| 2530 | * Add the given control element to an array inside the codec instance. |
| 2531 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2532 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2533 | */ |
| 2534 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2535 | unsigned int index, hda_nid_t nid) |
| 2536 | { |
| 2537 | struct hda_nid_item *item; |
| 2538 | |
| 2539 | if (nid > 0) { |
| 2540 | item = snd_array_new(&codec->nids); |
| 2541 | if (!item) |
| 2542 | return -ENOMEM; |
| 2543 | item->kctl = kctl; |
| 2544 | item->index = index; |
| 2545 | item->nid = nid; |
| 2546 | return 0; |
| 2547 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2548 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2549 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2550 | return -EINVAL; |
| 2551 | } |
| 2552 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2553 | |
| 2554 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2555 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2556 | * @codec: HD-audio codec |
| 2557 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2558 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2559 | { |
| 2560 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2561 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2562 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2563 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2564 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2565 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2566 | } |
| 2567 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2568 | /* pseudo device locking |
| 2569 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2570 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2571 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2572 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2573 | struct snd_card *card = bus->card; |
| 2574 | struct hda_codec *codec; |
| 2575 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2576 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2577 | if (card->shutdown) |
| 2578 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2579 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2580 | if (!list_empty(&card->ctl_files)) |
| 2581 | goto err_clear; |
| 2582 | |
| 2583 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2584 | int pcm; |
| 2585 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2586 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2587 | if (!cpcm->pcm) |
| 2588 | continue; |
| 2589 | if (cpcm->pcm->streams[0].substream_opened || |
| 2590 | cpcm->pcm->streams[1].substream_opened) |
| 2591 | goto err_clear; |
| 2592 | } |
| 2593 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2594 | spin_unlock(&card->files_lock); |
| 2595 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2596 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2597 | err_clear: |
| 2598 | card->shutdown = 0; |
| 2599 | err_unlock: |
| 2600 | spin_unlock(&card->files_lock); |
| 2601 | return -EINVAL; |
| 2602 | } |
| 2603 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); |
| 2604 | |
| 2605 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2606 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2607 | struct snd_card *card = bus->card; |
| 2608 | |
| 2609 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2610 | spin_lock(&card->files_lock); |
| 2611 | card->shutdown = 0; |
| 2612 | spin_unlock(&card->files_lock); |
| 2613 | } |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2614 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2615 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2616 | /** |
| 2617 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2618 | * @codec: HD-audio codec |
| 2619 | * |
| 2620 | * This frees the all PCM and control elements assigned to the codec, and |
| 2621 | * clears the caches and restores the pin default configurations. |
| 2622 | * |
| 2623 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2624 | * returns zero. |
| 2625 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2626 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2627 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2628 | struct hda_bus *bus = codec->bus; |
| 2629 | struct snd_card *card = bus->card; |
| 2630 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2631 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2632 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2633 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2634 | |
| 2635 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2636 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2637 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2638 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2639 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2640 | #endif |
| 2641 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2642 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2643 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2644 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2645 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2646 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2647 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2648 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2649 | } |
| 2650 | if (codec->patch_ops.free) |
| 2651 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2652 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2653 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2654 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2655 | codec->spec = NULL; |
| 2656 | free_hda_cache(&codec->amp_cache); |
| 2657 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2658 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2659 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2660 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2661 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2662 | snd_array_free(&codec->cvt_setups); |
| 2663 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2664 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2665 | codec->num_pcms = 0; |
| 2666 | codec->pcm_info = NULL; |
| 2667 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2668 | codec->slave_dig_outs = NULL; |
| 2669 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2670 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2671 | module_put(codec->owner); |
| 2672 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2673 | |
| 2674 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2675 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2676 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2679 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2680 | |
| 2681 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2682 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2683 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2684 | { |
| 2685 | struct hda_nid_item *items; |
| 2686 | const char * const *s; |
| 2687 | int i, err; |
| 2688 | |
| 2689 | items = codec->mixers.list; |
| 2690 | for (i = 0; i < codec->mixers.used; i++) { |
| 2691 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2692 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2693 | continue; |
| 2694 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2695 | char tmpname[sizeof(sctl->id.name)]; |
| 2696 | const char *name = *s; |
| 2697 | if (suffix) { |
| 2698 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2699 | name, suffix); |
| 2700 | name = tmpname; |
| 2701 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2702 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2703 | err = func(data, sctl); |
| 2704 | if (err) |
| 2705 | return err; |
| 2706 | break; |
| 2707 | } |
| 2708 | } |
| 2709 | } |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
| 2713 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2714 | { |
| 2715 | return 1; |
| 2716 | } |
| 2717 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2718 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2719 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2720 | { |
| 2721 | int _tlv[4]; |
| 2722 | const int *tlv = NULL; |
| 2723 | int val = -1; |
| 2724 | |
| 2725 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2726 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2727 | mm_segment_t fs = get_fs(); |
| 2728 | set_fs(get_ds()); |
| 2729 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2730 | tlv = _tlv; |
| 2731 | set_fs(fs); |
| 2732 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2733 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2734 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2735 | int step = tlv[3]; |
| 2736 | step &= ~TLV_DB_SCALE_MUTE; |
| 2737 | if (!step) |
| 2738 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2739 | if (*step_to_check && *step_to_check != step) { |
| 2740 | snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n", |
| 2741 | *step_to_check, step); |
| 2742 | return -1; |
| 2743 | } |
| 2744 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2745 | val = -tlv[2] / step; |
| 2746 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2747 | return val; |
| 2748 | } |
| 2749 | |
| 2750 | /* call kctl->put with the given value(s) */ |
| 2751 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2752 | { |
| 2753 | struct snd_ctl_elem_value *ucontrol; |
| 2754 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2755 | if (!ucontrol) |
| 2756 | return -ENOMEM; |
| 2757 | ucontrol->value.integer.value[0] = val; |
| 2758 | ucontrol->value.integer.value[1] = val; |
| 2759 | kctl->put(kctl, ucontrol); |
| 2760 | kfree(ucontrol); |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | /* initialize the slave volume with 0dB */ |
| 2765 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2766 | { |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2767 | int offset = get_kctl_0dB_offset(slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2768 | if (offset > 0) |
| 2769 | put_kctl_with_value(slave, offset); |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | /* unmute the slave */ |
| 2774 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2775 | { |
| 2776 | return put_kctl_with_value(slave, 1); |
| 2777 | } |
| 2778 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2779 | /** |
| 2780 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2781 | * @codec: HD-audio codec |
| 2782 | * @name: vmaster control name |
| 2783 | * @tlv: TLV data (optional) |
| 2784 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2785 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2786 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2787 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2788 | * |
| 2789 | * Create a virtual master control with the given name. The TLV data |
| 2790 | * must be either NULL or a valid data. |
| 2791 | * |
| 2792 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2793 | * slave control name. All controls with these names are assigned to |
| 2794 | * the new virtual master control. |
| 2795 | * |
| 2796 | * This function returns zero if successful or a negative error code. |
| 2797 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2798 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2799 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2800 | const char *suffix, bool init_slave_vol, |
| 2801 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2802 | { |
| 2803 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2804 | int err; |
| 2805 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2806 | if (ctl_ret) |
| 2807 | *ctl_ret = NULL; |
| 2808 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2809 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2810 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2811 | snd_printdd("No slave found for %s\n", name); |
| 2812 | return 0; |
| 2813 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2814 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2815 | if (!kctl) |
| 2816 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2817 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2818 | if (err < 0) |
| 2819 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2820 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2821 | err = map_slaves(codec, slaves, suffix, |
| 2822 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2823 | if (err < 0) |
| 2824 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2825 | |
| 2826 | /* init with master mute & zero volume */ |
| 2827 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2828 | if (init_slave_vol) { |
| 2829 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2830 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2831 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2832 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2833 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2834 | if (ctl_ret) |
| 2835 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2838 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2839 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2840 | /* |
| 2841 | * mute-LED control using vmaster |
| 2842 | */ |
| 2843 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2844 | struct snd_ctl_elem_info *uinfo) |
| 2845 | { |
| 2846 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2847 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2848 | }; |
| 2849 | unsigned int index; |
| 2850 | |
| 2851 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2852 | uinfo->count = 1; |
| 2853 | uinfo->value.enumerated.items = 3; |
| 2854 | index = uinfo->value.enumerated.item; |
| 2855 | if (index >= 3) |
| 2856 | index = 2; |
| 2857 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
| 2861 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2862 | struct snd_ctl_elem_value *ucontrol) |
| 2863 | { |
| 2864 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2865 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2870 | struct snd_ctl_elem_value *ucontrol) |
| 2871 | { |
| 2872 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2873 | unsigned int old_mode = hook->mute_mode; |
| 2874 | |
| 2875 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2876 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2877 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2878 | if (old_mode == hook->mute_mode) |
| 2879 | return 0; |
| 2880 | snd_hda_sync_vmaster_hook(hook); |
| 2881 | return 1; |
| 2882 | } |
| 2883 | |
| 2884 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2885 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2886 | .name = "Mute-LED Mode", |
| 2887 | .info = vmaster_mute_mode_info, |
| 2888 | .get = vmaster_mute_mode_get, |
| 2889 | .put = vmaster_mute_mode_put, |
| 2890 | }; |
| 2891 | |
| 2892 | /* |
| 2893 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2894 | * "Mute-LED Mode" control is automatically created and associated with |
| 2895 | * the given hook. |
| 2896 | */ |
| 2897 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2898 | struct hda_vmaster_mute_hook *hook, |
| 2899 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2900 | { |
| 2901 | struct snd_kcontrol *kctl; |
| 2902 | |
| 2903 | if (!hook->hook || !hook->sw_kctl) |
| 2904 | return 0; |
| 2905 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2906 | hook->codec = codec; |
| 2907 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2908 | if (!expose_enum_ctl) |
| 2909 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2910 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2911 | if (!kctl) |
| 2912 | return -ENOMEM; |
| 2913 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2914 | } |
| 2915 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); |
| 2916 | |
| 2917 | /* |
| 2918 | * Call the hook with the current value for synchronization |
| 2919 | * Should be called in init callback |
| 2920 | */ |
| 2921 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2922 | { |
| 2923 | if (!hook->hook || !hook->codec) |
| 2924 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2925 | /* don't call vmaster hook in the destructor since it might have |
| 2926 | * been already destroyed |
| 2927 | */ |
| 2928 | if (hook->codec->bus->shutdown) |
| 2929 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2930 | switch (hook->mute_mode) { |
| 2931 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2932 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2933 | break; |
| 2934 | default: |
| 2935 | hook->hook(hook->codec, hook->mute_mode); |
| 2936 | break; |
| 2937 | } |
| 2938 | } |
| 2939 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); |
| 2940 | |
| 2941 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2942 | /** |
| 2943 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2944 | * |
| 2945 | * The control element is supposed to have the private_value field |
| 2946 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2947 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2948 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2949 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | { |
| 2951 | int chs = get_amp_channels(kcontrol); |
| 2952 | |
| 2953 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2954 | uinfo->count = chs == 3 ? 2 : 1; |
| 2955 | uinfo->value.integer.min = 0; |
| 2956 | uinfo->value.integer.max = 1; |
| 2957 | return 0; |
| 2958 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2959 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2961 | /** |
| 2962 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2963 | * |
| 2964 | * The control element is supposed to have the private_value field |
| 2965 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2966 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2967 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2968 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | { |
| 2970 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2971 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2972 | int chs = get_amp_channels(kcontrol); |
| 2973 | int dir = get_amp_direction(kcontrol); |
| 2974 | int idx = get_amp_index(kcontrol); |
| 2975 | long *valp = ucontrol->value.integer.value; |
| 2976 | |
| 2977 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2978 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2979 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2981 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2982 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | return 0; |
| 2984 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2985 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2987 | /** |
| 2988 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2989 | * |
| 2990 | * The control element is supposed to have the private_value field |
| 2991 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2992 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2993 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2994 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | { |
| 2996 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2997 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2998 | int chs = get_amp_channels(kcontrol); |
| 2999 | int dir = get_amp_direction(kcontrol); |
| 3000 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | long *valp = ucontrol->value.integer.value; |
| 3002 | int change = 0; |
| 3003 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3004 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3005 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3006 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3007 | HDA_AMP_MUTE, |
| 3008 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3009 | valp++; |
| 3010 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3011 | if (chs & 2) |
| 3012 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3013 | HDA_AMP_MUTE, |
| 3014 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3015 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3016 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | return change; |
| 3018 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3019 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | |
| 3021 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3022 | * bound volume controls |
| 3023 | * |
| 3024 | * bind multiple volumes (# indices, from 0) |
| 3025 | */ |
| 3026 | |
| 3027 | #define AMP_VAL_IDX_SHIFT 19 |
| 3028 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3029 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3030 | /** |
| 3031 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 3032 | * |
| 3033 | * The control element is supposed to have the private_value field |
| 3034 | * set up via HDA_BIND_MUTE*() macros. |
| 3035 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3036 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3037 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3038 | { |
| 3039 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3040 | unsigned long pval; |
| 3041 | int err; |
| 3042 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3043 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3044 | pval = kcontrol->private_value; |
| 3045 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3046 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3047 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3048 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3049 | return err; |
| 3050 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3051 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3052 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3053 | /** |
| 3054 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 3055 | * |
| 3056 | * The control element is supposed to have the private_value field |
| 3057 | * set up via HDA_BIND_MUTE*() macros. |
| 3058 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3059 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3060 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3061 | { |
| 3062 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3063 | unsigned long pval; |
| 3064 | int i, indices, err = 0, change = 0; |
| 3065 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3066 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3067 | pval = kcontrol->private_value; |
| 3068 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3069 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3070 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3071 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3072 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3073 | if (err < 0) |
| 3074 | break; |
| 3075 | change |= err; |
| 3076 | } |
| 3077 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3078 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3079 | return err < 0 ? err : change; |
| 3080 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3081 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3082 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3083 | /** |
| 3084 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 3085 | * |
| 3086 | * The control element is supposed to have the private_value field |
| 3087 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3088 | */ |
| 3089 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3090 | struct snd_ctl_elem_info *uinfo) |
| 3091 | { |
| 3092 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3093 | struct hda_bind_ctls *c; |
| 3094 | int err; |
| 3095 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3096 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3097 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3098 | kcontrol->private_value = *c->values; |
| 3099 | err = c->ops->info(kcontrol, uinfo); |
| 3100 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3101 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3102 | return err; |
| 3103 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3104 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3105 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3106 | /** |
| 3107 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 3108 | * |
| 3109 | * The control element is supposed to have the private_value field |
| 3110 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3111 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3112 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3113 | struct snd_ctl_elem_value *ucontrol) |
| 3114 | { |
| 3115 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3116 | struct hda_bind_ctls *c; |
| 3117 | int err; |
| 3118 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3119 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3120 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3121 | kcontrol->private_value = *c->values; |
| 3122 | err = c->ops->get(kcontrol, ucontrol); |
| 3123 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3124 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3125 | return err; |
| 3126 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3127 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3128 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3129 | /** |
| 3130 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3131 | * |
| 3132 | * The control element is supposed to have the private_value field |
| 3133 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3134 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3135 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3136 | struct snd_ctl_elem_value *ucontrol) |
| 3137 | { |
| 3138 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3139 | struct hda_bind_ctls *c; |
| 3140 | unsigned long *vals; |
| 3141 | int err = 0, change = 0; |
| 3142 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3143 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3144 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3145 | for (vals = c->values; *vals; vals++) { |
| 3146 | kcontrol->private_value = *vals; |
| 3147 | err = c->ops->put(kcontrol, ucontrol); |
| 3148 | if (err < 0) |
| 3149 | break; |
| 3150 | change |= err; |
| 3151 | } |
| 3152 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3153 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3154 | return err < 0 ? err : change; |
| 3155 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3156 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3158 | /** |
| 3159 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3160 | * |
| 3161 | * The control element is supposed to have the private_value field |
| 3162 | * set up via HDA_BIND_VOL() macro. |
| 3163 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3164 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3165 | unsigned int size, unsigned int __user *tlv) |
| 3166 | { |
| 3167 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3168 | struct hda_bind_ctls *c; |
| 3169 | int err; |
| 3170 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3171 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3172 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3173 | kcontrol->private_value = *c->values; |
| 3174 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3175 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3176 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3177 | return err; |
| 3178 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3179 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3180 | |
| 3181 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3182 | .info = snd_hda_mixer_amp_volume_info, |
| 3183 | .get = snd_hda_mixer_amp_volume_get, |
| 3184 | .put = snd_hda_mixer_amp_volume_put, |
| 3185 | .tlv = snd_hda_mixer_amp_tlv |
| 3186 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3187 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3188 | |
| 3189 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3190 | .info = snd_hda_mixer_amp_switch_info, |
| 3191 | .get = snd_hda_mixer_amp_switch_get, |
| 3192 | .put = snd_hda_mixer_amp_switch_put, |
| 3193 | .tlv = snd_hda_mixer_amp_tlv |
| 3194 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3195 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3196 | |
| 3197 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | * SPDIF out controls |
| 3199 | */ |
| 3200 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3201 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3202 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | { |
| 3204 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3205 | uinfo->count = 1; |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3209 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3210 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | { |
| 3212 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3213 | IEC958_AES0_NONAUDIO | |
| 3214 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3215 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3216 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3217 | IEC958_AES1_CON_ORIGINAL; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3221 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3222 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | { |
| 3224 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3225 | IEC958_AES0_NONAUDIO | |
| 3226 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3230 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3231 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | { |
| 3233 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3234 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3235 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3237 | mutex_lock(&codec->spdif_mutex); |
| 3238 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3239 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3240 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3241 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3242 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3243 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
| 3245 | return 0; |
| 3246 | } |
| 3247 | |
| 3248 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3249 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3250 | */ |
| 3251 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3252 | { |
| 3253 | unsigned short val = 0; |
| 3254 | |
| 3255 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3256 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3258 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3260 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3261 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3262 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3264 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3265 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3266 | val |= AC_DIG1_EMPHASIS; |
| 3267 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3268 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3270 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3272 | } |
| 3273 | return val; |
| 3274 | } |
| 3275 | |
| 3276 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3277 | */ |
| 3278 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3279 | { |
| 3280 | unsigned int sbits = 0; |
| 3281 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3282 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3284 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3286 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3287 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3289 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3290 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3292 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3294 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3296 | sbits |= val & (0x7f << 8); |
| 3297 | } |
| 3298 | return sbits; |
| 3299 | } |
| 3300 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3301 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3302 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3303 | int verb, int val) |
| 3304 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3305 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3306 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3307 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3308 | d = codec->slave_dig_outs; |
| 3309 | if (!d) |
| 3310 | return; |
| 3311 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3312 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3316 | int dig1, int dig2) |
| 3317 | { |
| 3318 | if (dig1 != -1) |
| 3319 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3320 | if (dig2 != -1) |
| 3321 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3322 | } |
| 3323 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3324 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3325 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | { |
| 3327 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3328 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3329 | struct hda_spdif_out *spdif; |
| 3330 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | unsigned short val; |
| 3332 | int change; |
| 3333 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3334 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3335 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3336 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3337 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3339 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3340 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3341 | val = convert_from_spdif_status(spdif->status); |
| 3342 | val |= spdif->ctls & 1; |
| 3343 | change = spdif->ctls != val; |
| 3344 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3345 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3346 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3347 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | return change; |
| 3349 | } |
| 3350 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3351 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3353 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3354 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | { |
| 3356 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3357 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3358 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3360 | mutex_lock(&codec->spdif_mutex); |
| 3361 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3362 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3363 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | return 0; |
| 3365 | } |
| 3366 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3367 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3368 | int dig1, int dig2) |
| 3369 | { |
| 3370 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3371 | /* unmute amp switch (if any) */ |
| 3372 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3373 | (dig1 & AC_DIG1_ENABLE)) |
| 3374 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3375 | HDA_AMP_MUTE, 0); |
| 3376 | } |
| 3377 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3378 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3379 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | { |
| 3381 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3382 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3383 | struct hda_spdif_out *spdif; |
| 3384 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | unsigned short val; |
| 3386 | int change; |
| 3387 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3388 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3389 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3390 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3391 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3393 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3394 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3395 | spdif->ctls = val; |
| 3396 | if (change && nid != (u16)-1) |
| 3397 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3398 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | return change; |
| 3400 | } |
| 3401 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3402 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | { |
| 3404 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3405 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3406 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | .info = snd_hda_spdif_mask_info, |
| 3408 | .get = snd_hda_spdif_cmask_get, |
| 3409 | }, |
| 3410 | { |
| 3411 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3412 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3413 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | .info = snd_hda_spdif_mask_info, |
| 3415 | .get = snd_hda_spdif_pmask_get, |
| 3416 | }, |
| 3417 | { |
| 3418 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3419 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | .info = snd_hda_spdif_mask_info, |
| 3421 | .get = snd_hda_spdif_default_get, |
| 3422 | .put = snd_hda_spdif_default_put, |
| 3423 | }, |
| 3424 | { |
| 3425 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3426 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | .info = snd_hda_spdif_out_switch_info, |
| 3428 | .get = snd_hda_spdif_out_switch_get, |
| 3429 | .put = snd_hda_spdif_out_switch_put, |
| 3430 | }, |
| 3431 | { } /* end */ |
| 3432 | }; |
| 3433 | |
| 3434 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3435 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3437 | * @associated_nid: NID that new ctls associated with |
| 3438 | * @cvt_nid: converter NID |
| 3439 | * @type: HDA_PCM_TYPE_* |
| 3440 | * Creates controls related with the digital output. |
| 3441 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | * |
| 3443 | * Returns 0 if successful, or a negative error code. |
| 3444 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3445 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3446 | hda_nid_t associated_nid, |
| 3447 | hda_nid_t cvt_nid, |
| 3448 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | { |
| 3450 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3451 | struct snd_kcontrol *kctl; |
| 3452 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3453 | int idx = 0; |
| 3454 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3455 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3456 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3458 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3459 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3460 | idx = spdif_index; |
| 3461 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3462 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3463 | /* suppose a single SPDIF device */ |
| 3464 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3465 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3466 | if (!kctl) |
| 3467 | break; |
| 3468 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3469 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3470 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3471 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3472 | if (!bus->primary_dig_out_type) |
| 3473 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3474 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3475 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3476 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3477 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3478 | return -EBUSY; |
| 3479 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3480 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3481 | if (!spdif) |
| 3482 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3484 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3485 | if (!kctl) |
| 3486 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3487 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3488 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3489 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3490 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | return err; |
| 3492 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3493 | spdif->nid = cvt_nid; |
| 3494 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3495 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3496 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | return 0; |
| 3498 | } |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3499 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3501 | /* get the hda_spdif_out entry from the given NID |
| 3502 | * call within spdif_mutex lock |
| 3503 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3504 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3505 | hda_nid_t nid) |
| 3506 | { |
| 3507 | int i; |
| 3508 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3509 | struct hda_spdif_out *spdif = |
| 3510 | snd_array_elem(&codec->spdif_out, i); |
| 3511 | if (spdif->nid == nid) |
| 3512 | return spdif; |
| 3513 | } |
| 3514 | return NULL; |
| 3515 | } |
| 3516 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3517 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3518 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3519 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3520 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3521 | |
| 3522 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3523 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3524 | spdif->nid = (u16)-1; |
| 3525 | mutex_unlock(&codec->spdif_mutex); |
| 3526 | } |
| 3527 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3528 | |
| 3529 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3530 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3531 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3532 | unsigned short val; |
| 3533 | |
| 3534 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3535 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3536 | if (spdif->nid != nid) { |
| 3537 | spdif->nid = nid; |
| 3538 | val = spdif->ctls; |
| 3539 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3540 | } |
| 3541 | mutex_unlock(&codec->spdif_mutex); |
| 3542 | } |
| 3543 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3546 | * SPDIF sharing with analog output |
| 3547 | */ |
| 3548 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3549 | struct snd_ctl_elem_value *ucontrol) |
| 3550 | { |
| 3551 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3552 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3557 | struct snd_ctl_elem_value *ucontrol) |
| 3558 | { |
| 3559 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3560 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3561 | return 0; |
| 3562 | } |
| 3563 | |
| 3564 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3565 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3566 | .name = "IEC958 Default PCM Playback Switch", |
| 3567 | .info = snd_ctl_boolean_mono_info, |
| 3568 | .get = spdif_share_sw_get, |
| 3569 | .put = spdif_share_sw_put, |
| 3570 | }; |
| 3571 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3572 | /** |
| 3573 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3574 | * @codec: the HDA codec |
| 3575 | * @mout: multi-out instance |
| 3576 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3577 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3578 | struct hda_multi_out *mout) |
| 3579 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3580 | struct snd_kcontrol *kctl; |
| 3581 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3582 | if (!mout->dig_out_nid) |
| 3583 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3584 | |
| 3585 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3586 | if (!kctl) |
| 3587 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3588 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3589 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3590 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3591 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3592 | |
| 3593 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | * SPDIF input |
| 3595 | */ |
| 3596 | |
| 3597 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3598 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3599 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3600 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | { |
| 3602 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3603 | |
| 3604 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3608 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3609 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | { |
| 3611 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3612 | hda_nid_t nid = kcontrol->private_value; |
| 3613 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3614 | int change; |
| 3615 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3616 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3618 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3620 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3621 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3623 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | return change; |
| 3625 | } |
| 3626 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3627 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3628 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | { |
| 3630 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3631 | hda_nid_t nid = kcontrol->private_value; |
| 3632 | unsigned short val; |
| 3633 | unsigned int sbits; |
| 3634 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3635 | 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] | 3636 | sbits = convert_to_spdif_status(val); |
| 3637 | ucontrol->value.iec958.status[0] = sbits; |
| 3638 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3639 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3640 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3644 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | { |
| 3646 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3647 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | .info = snd_hda_spdif_in_switch_info, |
| 3649 | .get = snd_hda_spdif_in_switch_get, |
| 3650 | .put = snd_hda_spdif_in_switch_put, |
| 3651 | }, |
| 3652 | { |
| 3653 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3654 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3655 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | .info = snd_hda_spdif_mask_info, |
| 3657 | .get = snd_hda_spdif_in_status_get, |
| 3658 | }, |
| 3659 | { } /* end */ |
| 3660 | }; |
| 3661 | |
| 3662 | /** |
| 3663 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3664 | * @codec: the HDA codec |
| 3665 | * @nid: audio in widget NID |
| 3666 | * |
| 3667 | * Creates controls related with the SPDIF input. |
| 3668 | * Called from each patch supporting the SPDIF in. |
| 3669 | * |
| 3670 | * Returns 0 if successful, or a negative error code. |
| 3671 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3672 | 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] | 3673 | { |
| 3674 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3675 | struct snd_kcontrol *kctl; |
| 3676 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3677 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3679 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3680 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3681 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3682 | return -EBUSY; |
| 3683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3685 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3686 | if (!kctl) |
| 3687 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3689 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3690 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | return err; |
| 3692 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3693 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3694 | snd_hda_codec_read(codec, nid, 0, |
| 3695 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3696 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3699 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3701 | /* |
| 3702 | * command cache |
| 3703 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3705 | /* build a 31bit cache key with the widget id and the command parameter */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3706 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3707 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3708 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3709 | |
| 3710 | /** |
| 3711 | * snd_hda_codec_write_cache - send a single command with caching |
| 3712 | * @codec: the HDA codec |
| 3713 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3714 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3715 | * @verb: the verb to send |
| 3716 | * @parm: the parameter for the verb |
| 3717 | * |
| 3718 | * Send a single command without waiting for response. |
| 3719 | * |
| 3720 | * Returns 0 if successful, or a negative error code. |
| 3721 | */ |
| 3722 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3723 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3724 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3725 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3726 | struct hda_cache_head *c; |
| 3727 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3728 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3729 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3730 | cache_only = codec->cached_write; |
| 3731 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3732 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3733 | if (err < 0) |
| 3734 | return err; |
| 3735 | } |
| 3736 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3737 | /* parm may contain the verb stuff for get/set amp */ |
| 3738 | verb = verb | (parm >> 8); |
| 3739 | parm &= 0xff; |
| 3740 | key = build_cmd_cache_key(nid, verb); |
| 3741 | mutex_lock(&codec->bus->cmd_mutex); |
| 3742 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3743 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3744 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3745 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3746 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3747 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3748 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3749 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3750 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3751 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3752 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3753 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3754 | * @codec: the HDA codec |
| 3755 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3756 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3757 | * @verb: the verb to send |
| 3758 | * @parm: the parameter for the verb |
| 3759 | * |
| 3760 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3761 | * command if the parameter is already identical with the cached value. |
| 3762 | * If not, it sends the command and refreshes the cache. |
| 3763 | * |
| 3764 | * Returns 0 if successful, or a negative error code. |
| 3765 | */ |
| 3766 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3767 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3768 | { |
| 3769 | struct hda_cache_head *c; |
| 3770 | u32 key; |
| 3771 | |
| 3772 | /* parm may contain the verb stuff for get/set amp */ |
| 3773 | verb = verb | (parm >> 8); |
| 3774 | parm &= 0xff; |
| 3775 | key = build_cmd_cache_key(nid, verb); |
| 3776 | mutex_lock(&codec->bus->cmd_mutex); |
| 3777 | c = get_hash(&codec->cmd_cache, key); |
| 3778 | if (c && c->val == parm) { |
| 3779 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3780 | return 0; |
| 3781 | } |
| 3782 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3783 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3784 | } |
| 3785 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3786 | |
| 3787 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3788 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3789 | * @codec: HD-audio codec |
| 3790 | * |
| 3791 | * Execute all verbs recorded in the command caches to resume. |
| 3792 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3793 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3794 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3795 | int i; |
| 3796 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3797 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3798 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3799 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3800 | struct hda_cache_head *buffer; |
| 3801 | u32 key; |
| 3802 | |
| 3803 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3804 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3805 | if (!key) |
| 3806 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3807 | if (!buffer->dirty) |
| 3808 | continue; |
| 3809 | buffer->dirty = 0; |
| 3810 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3811 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3812 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3813 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3814 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3815 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3816 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3817 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3818 | |
| 3819 | /** |
| 3820 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3821 | * @codec: the HDA codec |
| 3822 | * @seq: VERB array to send |
| 3823 | * |
| 3824 | * Send the commands sequentially from the given array. |
| 3825 | * Thte commands are recorded on cache for power-save and resume. |
| 3826 | * The array must be terminated with NID=0. |
| 3827 | */ |
| 3828 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3829 | const struct hda_verb *seq) |
| 3830 | { |
| 3831 | for (; seq->nid; seq++) |
| 3832 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3833 | seq->param); |
| 3834 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3835 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3836 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3837 | /** |
| 3838 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3839 | * @codec: HD-audio codec |
| 3840 | */ |
| 3841 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3842 | { |
| 3843 | snd_hda_codec_resume_amp(codec); |
| 3844 | snd_hda_codec_resume_cache(codec); |
| 3845 | } |
| 3846 | EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache); |
| 3847 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3848 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3849 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3850 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3851 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3852 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3853 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3854 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3855 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3856 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3857 | if (!(wcaps & AC_WCAP_POWER)) |
| 3858 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3859 | if (codec->power_filter) { |
| 3860 | state = codec->power_filter(codec, nid, power_state); |
| 3861 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3862 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3863 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3864 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3865 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3866 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3867 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3868 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3869 | |
| 3870 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3871 | * supported power states check |
| 3872 | */ |
| 3873 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3874 | unsigned int power_state) |
| 3875 | { |
| 3876 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3877 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3878 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3879 | return false; |
| 3880 | if (sup & power_state) |
| 3881 | return true; |
| 3882 | else |
| 3883 | return false; |
| 3884 | } |
| 3885 | |
| 3886 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3887 | * wait until the state is reached, returns the current state |
| 3888 | */ |
| 3889 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3890 | hda_nid_t fg, |
| 3891 | unsigned int power_state) |
| 3892 | { |
| 3893 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3894 | unsigned int state, actual_state; |
| 3895 | |
| 3896 | for (;;) { |
| 3897 | state = snd_hda_codec_read(codec, fg, 0, |
| 3898 | AC_VERB_GET_POWER_STATE, 0); |
| 3899 | if (state & AC_PWRST_ERROR) |
| 3900 | break; |
| 3901 | actual_state = (state >> 4) & 0x0f; |
| 3902 | if (actual_state == power_state) |
| 3903 | break; |
| 3904 | if (time_after_eq(jiffies, end_time)) |
| 3905 | break; |
| 3906 | /* wait until the codec reachs to the target state */ |
| 3907 | msleep(1); |
| 3908 | } |
| 3909 | return state; |
| 3910 | } |
| 3911 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3912 | /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3913 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3914 | hda_nid_t nid, |
| 3915 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3916 | { |
| 3917 | if (power_state == AC_PWRST_D3 && |
| 3918 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3919 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3920 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3921 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3922 | if (eapd & 0x02) |
| 3923 | return AC_PWRST_D0; |
| 3924 | } |
| 3925 | return power_state; |
| 3926 | } |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3927 | EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3928 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3929 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3930 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3931 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3932 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3933 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3934 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3935 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3936 | int count; |
| 3937 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3938 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3939 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3940 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3941 | if (power_state == AC_PWRST_D3) { |
| 3942 | /* transition time less than 10ms for power down */ |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 3943 | msleep(codec->epss ? 10 : 100); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3944 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3945 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3946 | |
| 3947 | /* repeat power states setting at most 10 times*/ |
| 3948 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3949 | if (codec->patch_ops.set_power_state) |
| 3950 | codec->patch_ops.set_power_state(codec, fg, |
| 3951 | power_state); |
| 3952 | else { |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3953 | snd_hda_codec_read(codec, fg, flags, |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3954 | AC_VERB_SET_POWER_STATE, |
| 3955 | power_state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3956 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3957 | } |
| 3958 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3959 | if (!(state & AC_PWRST_ERROR)) |
| 3960 | break; |
| 3961 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3962 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3963 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3964 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3965 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3966 | /* sync power states of all widgets; |
| 3967 | * this is called at the end of codec parsing |
| 3968 | */ |
| 3969 | static void sync_power_up_states(struct hda_codec *codec) |
| 3970 | { |
| 3971 | hda_nid_t nid = codec->start_nid; |
| 3972 | int i; |
| 3973 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3974 | /* don't care if no filter is used */ |
| 3975 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3976 | return; |
| 3977 | |
| 3978 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3979 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3980 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3981 | if (!(wcaps & AC_WCAP_POWER)) |
| 3982 | continue; |
| 3983 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3984 | if (target == AC_PWRST_D0) |
| 3985 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3986 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3987 | snd_hda_codec_write(codec, nid, 0, |
| 3988 | AC_VERB_SET_POWER_STATE, target); |
| 3989 | } |
| 3990 | } |
| 3991 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3992 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3993 | /* execute additional init verbs */ |
| 3994 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3995 | { |
| 3996 | if (codec->init_verbs.list) |
| 3997 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3998 | } |
| 3999 | #else |
| 4000 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4001 | #endif |
| 4002 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4003 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4004 | /* |
| 4005 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4006 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4007 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4008 | 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] | 4009 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4010 | unsigned int state; |
| 4011 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4012 | codec->in_pm = 1; |
| 4013 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4014 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4015 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4016 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4017 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4018 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4019 | if (!in_wq) |
| 4020 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4021 | spin_lock(&codec->power_lock); |
| 4022 | snd_hda_update_power_acct(codec); |
| 4023 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4024 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4025 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4026 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4027 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4028 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4029 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4030 | } |
| 4031 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4032 | /* mark all entries of cmd and amp caches dirty */ |
| 4033 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4034 | { |
| 4035 | int i; |
| 4036 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4037 | struct hda_cache_head *cmd; |
| 4038 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4039 | cmd->dirty = 1; |
| 4040 | } |
| 4041 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4042 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4043 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4044 | amp->head.dirty = 1; |
| 4045 | } |
| 4046 | } |
| 4047 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4048 | /* |
| 4049 | * kick up codec; used both from PM and power-save |
| 4050 | */ |
| 4051 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4052 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4053 | codec->in_pm = 1; |
| 4054 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4055 | hda_mark_cmd_cache_dirty(codec); |
| 4056 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4057 | /* set as if powered on for avoiding re-entering the resume |
| 4058 | * in the resume / power-save sequence |
| 4059 | */ |
| 4060 | hda_keep_power_on(codec); |
Takashi Iwai | 0fc28fc | 2013-11-20 12:15:07 +0100 | [diff] [blame] | 4061 | if (codec->pm_down_notified) { |
| 4062 | codec->pm_down_notified = 0; |
| 4063 | hda_call_pm_notify(codec->bus, true); |
| 4064 | } |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4065 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4066 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4067 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4068 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4069 | if (codec->patch_ops.resume) |
| 4070 | codec->patch_ops.resume(codec); |
| 4071 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4072 | if (codec->patch_ops.init) |
| 4073 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4074 | snd_hda_codec_resume_amp(codec); |
| 4075 | snd_hda_codec_resume_cache(codec); |
| 4076 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4077 | |
| 4078 | if (codec->jackpoll_interval) |
| 4079 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4080 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4081 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4082 | |
| 4083 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4084 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4085 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4086 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4087 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | /** |
| 4090 | * snd_hda_build_controls - build mixer controls |
| 4091 | * @bus: the BUS |
| 4092 | * |
| 4093 | * Creates mixer controls for each codec included in the bus. |
| 4094 | * |
| 4095 | * Returns 0 if successful, otherwise a negative error code. |
| 4096 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4097 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4099 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4101 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4102 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4103 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 4104 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4105 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4106 | err = snd_hda_codec_reset(codec); |
| 4107 | if (err < 0) { |
| 4108 | printk(KERN_ERR |
| 4109 | "hda_codec: cannot revert codec\n"); |
| 4110 | return err; |
| 4111 | } |
| 4112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4114 | return 0; |
| 4115 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4116 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4118 | /* |
| 4119 | * add standard channel maps if not specified |
| 4120 | */ |
| 4121 | static int add_std_chmaps(struct hda_codec *codec) |
| 4122 | { |
| 4123 | int i, str, err; |
| 4124 | |
| 4125 | for (i = 0; i < codec->num_pcms; i++) { |
| 4126 | for (str = 0; str < 2; str++) { |
| 4127 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4128 | struct hda_pcm_stream *hinfo = |
| 4129 | &codec->pcm_info[i].stream[str]; |
| 4130 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4131 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4132 | |
| 4133 | if (codec->pcm_info[i].own_chmap) |
| 4134 | continue; |
| 4135 | if (!pcm || !hinfo->substreams) |
| 4136 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4137 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4138 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4139 | hinfo->channels_max, |
| 4140 | 0, &chmap); |
| 4141 | if (err < 0) |
| 4142 | return err; |
| 4143 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4144 | } |
| 4145 | } |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4149 | /* default channel maps for 2.1 speakers; |
| 4150 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4151 | */ |
| 4152 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4153 | { .channels = 2, |
| 4154 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4155 | { .channels = 4, |
| 4156 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4157 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4158 | { } |
| 4159 | }; |
| 4160 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4161 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4162 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4163 | { |
| 4164 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4165 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4166 | /* continue to initialize... */ |
| 4167 | if (codec->patch_ops.init) |
| 4168 | err = codec->patch_ops.init(codec); |
| 4169 | if (!err && codec->patch_ops.build_controls) |
| 4170 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4171 | if (err < 0) |
| 4172 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4173 | |
| 4174 | /* we create chmaps here instead of build_pcms */ |
| 4175 | err = add_std_chmaps(codec); |
| 4176 | if (err < 0) |
| 4177 | return err; |
| 4178 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4179 | if (codec->jackpoll_interval) |
| 4180 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4181 | else |
| 4182 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4183 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | return 0; |
| 4185 | } |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | /* |
| 4188 | * stream formats |
| 4189 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4190 | struct hda_rate_tbl { |
| 4191 | unsigned int hz; |
| 4192 | unsigned int alsa_bits; |
| 4193 | unsigned int hda_fmt; |
| 4194 | }; |
| 4195 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4196 | /* rate = base * mult / div */ |
| 4197 | #define HDA_RATE(base, mult, div) \ |
| 4198 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4199 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4200 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4201 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4203 | |
| 4204 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4205 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4206 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4207 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4208 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4209 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4210 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4211 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4212 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4213 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4214 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4215 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4216 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4217 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4218 | |
| 4219 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4220 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4221 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4222 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | }; |
| 4224 | |
| 4225 | /** |
| 4226 | * snd_hda_calc_stream_format - calculate format bitset |
| 4227 | * @rate: the sample rate |
| 4228 | * @channels: the number of channels |
| 4229 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4230 | * @maxbps: the max. bps |
| 4231 | * |
| 4232 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4233 | * |
| 4234 | * Return zero if invalid. |
| 4235 | */ |
| 4236 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4237 | unsigned int channels, |
| 4238 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4239 | unsigned int maxbps, |
| 4240 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | { |
| 4242 | int i; |
| 4243 | unsigned int val = 0; |
| 4244 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4245 | for (i = 0; rate_bits[i].hz; i++) |
| 4246 | if (rate_bits[i].hz == rate) { |
| 4247 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | break; |
| 4249 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4250 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | snd_printdd("invalid rate %d\n", rate); |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
| 4255 | if (channels == 0 || channels > 8) { |
| 4256 | snd_printdd("invalid channels %d\n", channels); |
| 4257 | return 0; |
| 4258 | } |
| 4259 | val |= channels - 1; |
| 4260 | |
| 4261 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4262 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4263 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4264 | break; |
| 4265 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4266 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4267 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | case 20: |
| 4269 | case 24: |
| 4270 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4271 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4272 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4274 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4276 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | break; |
| 4278 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4279 | snd_printdd("invalid format width %d\n", |
| 4280 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | return 0; |
| 4282 | } |
| 4283 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4284 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4285 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | return val; |
| 4288 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4289 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4291 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4292 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4293 | { |
| 4294 | unsigned int val = 0; |
| 4295 | if (nid != codec->afg && |
| 4296 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4297 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4298 | if (!val || val == -1) |
| 4299 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4300 | if (!val || val == -1) |
| 4301 | return 0; |
| 4302 | return val; |
| 4303 | } |
| 4304 | |
| 4305 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4306 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4307 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4308 | get_pcm_param); |
| 4309 | } |
| 4310 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4311 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4312 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4313 | { |
| 4314 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4315 | if (!streams || streams == -1) |
| 4316 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4317 | if (!streams || streams == -1) |
| 4318 | return 0; |
| 4319 | return streams; |
| 4320 | } |
| 4321 | |
| 4322 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4323 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4324 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4325 | get_stream_param); |
| 4326 | } |
| 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | /** |
| 4329 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4330 | * @codec: the HDA codec |
| 4331 | * @nid: NID to query |
| 4332 | * @ratesp: the pointer to store the detected rate bitflags |
| 4333 | * @formatsp: the pointer to store the detected formats |
| 4334 | * @bpsp: the pointer to store the detected format widths |
| 4335 | * |
| 4336 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4337 | * or @bsps argument is ignored. |
| 4338 | * |
| 4339 | * Returns 0 if successful, otherwise a negative error code. |
| 4340 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4341 | 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] | 4342 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4343 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4344 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4346 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4347 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | |
| 4349 | if (ratesp) { |
| 4350 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4351 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4353 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4355 | if (rates == 0) { |
| 4356 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4357 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4358 | nid, val, |
| 4359 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4360 | return -EIO; |
| 4361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | *ratesp = rates; |
| 4363 | } |
| 4364 | |
| 4365 | if (formatsp || bpsp) { |
| 4366 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4367 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4369 | streams = query_stream_param(codec, nid); |
| 4370 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | |
| 4373 | bps = 0; |
| 4374 | if (streams & AC_SUPFMT_PCM) { |
| 4375 | if (val & AC_SUPPCM_BITS_8) { |
| 4376 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4377 | bps = 8; |
| 4378 | } |
| 4379 | if (val & AC_SUPPCM_BITS_16) { |
| 4380 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4381 | bps = 16; |
| 4382 | } |
| 4383 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4384 | if (val & AC_SUPPCM_BITS_32) |
| 4385 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4386 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4387 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4388 | if (val & AC_SUPPCM_BITS_24) |
| 4389 | bps = 24; |
| 4390 | else if (val & AC_SUPPCM_BITS_20) |
| 4391 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4392 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4393 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4395 | if (val & AC_SUPPCM_BITS_32) |
| 4396 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | else if (val & AC_SUPPCM_BITS_24) |
| 4398 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4399 | else if (val & AC_SUPPCM_BITS_20) |
| 4400 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | } |
| 4402 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4403 | #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] | 4404 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4406 | if (!bps) |
| 4407 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4408 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4409 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4410 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4411 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | /* temporary hack: we have still no proper support |
| 4413 | * for the direct AC3 stream... |
| 4414 | */ |
| 4415 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4416 | bps = 8; |
| 4417 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4418 | if (formats == 0) { |
| 4419 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4420 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4421 | "streams=0x%x)\n", |
| 4422 | nid, val, |
| 4423 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4424 | streams); |
| 4425 | return -EIO; |
| 4426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | if (formatsp) |
| 4428 | *formatsp = formats; |
| 4429 | if (bpsp) |
| 4430 | *bpsp = bps; |
| 4431 | } |
| 4432 | |
| 4433 | return 0; |
| 4434 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4435 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
| 4437 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4438 | * snd_hda_is_supported_format - Check the validity of the format |
| 4439 | * @codec: HD-audio codec |
| 4440 | * @nid: NID to check |
| 4441 | * @format: the HD-audio format value to check |
| 4442 | * |
| 4443 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | * |
| 4445 | * Returns 1 if supported, 0 if not. |
| 4446 | */ |
| 4447 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4448 | unsigned int format) |
| 4449 | { |
| 4450 | int i; |
| 4451 | unsigned int val = 0, rate, stream; |
| 4452 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4453 | val = query_pcm_param(codec, nid); |
| 4454 | if (!val) |
| 4455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
| 4457 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4458 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4459 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | if (val & (1 << i)) |
| 4461 | break; |
| 4462 | return 0; |
| 4463 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4464 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | return 0; |
| 4466 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4467 | stream = query_stream_param(codec, nid); |
| 4468 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | return 0; |
| 4470 | |
| 4471 | if (stream & AC_SUPFMT_PCM) { |
| 4472 | switch (format & 0xf0) { |
| 4473 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4474 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | return 0; |
| 4476 | break; |
| 4477 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4478 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | return 0; |
| 4480 | break; |
| 4481 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4482 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | return 0; |
| 4484 | break; |
| 4485 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4486 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | return 0; |
| 4488 | break; |
| 4489 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4490 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | return 0; |
| 4492 | break; |
| 4493 | default: |
| 4494 | return 0; |
| 4495 | } |
| 4496 | } else { |
| 4497 | /* FIXME: check for float32 and AC3? */ |
| 4498 | } |
| 4499 | |
| 4500 | return 1; |
| 4501 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4502 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
| 4504 | /* |
| 4505 | * PCM stuff |
| 4506 | */ |
| 4507 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4508 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4509 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | { |
| 4511 | return 0; |
| 4512 | } |
| 4513 | |
| 4514 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4515 | struct hda_codec *codec, |
| 4516 | unsigned int stream_tag, |
| 4517 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4518 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | { |
| 4520 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4521 | return 0; |
| 4522 | } |
| 4523 | |
| 4524 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4525 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4526 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4528 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return 0; |
| 4530 | } |
| 4531 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4532 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4533 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4535 | int err; |
| 4536 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4537 | /* query support PCM information from the given NID */ |
| 4538 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4539 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4540 | info->rates ? NULL : &info->rates, |
| 4541 | info->formats ? NULL : &info->formats, |
| 4542 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4543 | if (err < 0) |
| 4544 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | } |
| 4546 | if (info->ops.open == NULL) |
| 4547 | info->ops.open = hda_pcm_default_open_close; |
| 4548 | if (info->ops.close == NULL) |
| 4549 | info->ops.close = hda_pcm_default_open_close; |
| 4550 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4551 | if (snd_BUG_ON(!info->nid)) |
| 4552 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | info->ops.prepare = hda_pcm_default_prepare; |
| 4554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4556 | if (snd_BUG_ON(!info->nid)) |
| 4557 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4559 | } |
| 4560 | return 0; |
| 4561 | } |
| 4562 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4563 | /* |
| 4564 | * codec prepare/cleanup entries |
| 4565 | */ |
| 4566 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4567 | struct hda_pcm_stream *hinfo, |
| 4568 | unsigned int stream, |
| 4569 | unsigned int format, |
| 4570 | struct snd_pcm_substream *substream) |
| 4571 | { |
| 4572 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4573 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4574 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4575 | if (ret >= 0) |
| 4576 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4577 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4578 | return ret; |
| 4579 | } |
| 4580 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 4581 | |
| 4582 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4583 | struct hda_pcm_stream *hinfo, |
| 4584 | struct snd_pcm_substream *substream) |
| 4585 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4586 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4587 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4588 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4589 | } |
| 4590 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 4591 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4592 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4593 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4594 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4595 | }; |
| 4596 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4597 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4598 | * get the empty PCM device number to assign |
| 4599 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4600 | static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4601 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4602 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4603 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4604 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4605 | */ |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4606 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4607 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4608 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4609 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4610 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4611 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4612 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4613 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4614 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4615 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4616 | return -EINVAL; |
| 4617 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4618 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4619 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4620 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4621 | if (audio_idx[type][i] >= 8) |
| 4622 | break; |
| 4623 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4624 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4625 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4626 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4627 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4628 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4629 | /* non-fixed slots starting from 10 */ |
| 4630 | for (i = 10; i < 32; i++) { |
| 4631 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4632 | return i; |
| 4633 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4634 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4635 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4636 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4637 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4638 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4639 | snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
| 4640 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4641 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4645 | * attach a new PCM stream |
| 4646 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4647 | 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] | 4648 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4649 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4650 | struct hda_pcm_stream *info; |
| 4651 | int stream, err; |
| 4652 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4653 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4654 | return -EINVAL; |
| 4655 | for (stream = 0; stream < 2; stream++) { |
| 4656 | info = &pcm->stream[stream]; |
| 4657 | if (info->substreams) { |
| 4658 | err = set_pcm_default_values(codec, info); |
| 4659 | if (err < 0) |
| 4660 | return err; |
| 4661 | } |
| 4662 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4663 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4664 | } |
| 4665 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4666 | /* assign all PCMs of the given codec */ |
| 4667 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4668 | { |
| 4669 | unsigned int pcm; |
| 4670 | int err; |
| 4671 | |
| 4672 | if (!codec->num_pcms) { |
| 4673 | if (!codec->patch_ops.build_pcms) |
| 4674 | return 0; |
| 4675 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4676 | if (err < 0) { |
| 4677 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4678 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4679 | err = snd_hda_codec_reset(codec); |
| 4680 | if (err < 0) { |
| 4681 | printk(KERN_ERR |
| 4682 | "hda_codec: cannot revert codec\n"); |
| 4683 | return err; |
| 4684 | } |
| 4685 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4686 | } |
| 4687 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4688 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4689 | int dev; |
| 4690 | |
| 4691 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4692 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4693 | |
| 4694 | if (!cpcm->pcm) { |
| 4695 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4696 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4697 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4698 | cpcm->device = dev; |
| 4699 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4700 | if (err < 0) { |
| 4701 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4702 | "PCM stream %d for codec #%d\n", |
| 4703 | dev, codec->addr); |
| 4704 | continue; /* no fatal error */ |
| 4705 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | /** |
| 4712 | * snd_hda_build_pcms - build PCM information |
| 4713 | * @bus: the BUS |
| 4714 | * |
| 4715 | * Create PCM information for each codec included in the bus. |
| 4716 | * |
| 4717 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4718 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4719 | * to create its PCM instances. |
| 4720 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4721 | * callback. |
| 4722 | * |
| 4723 | * At least, substreams, channels_min and channels_max must be filled for |
| 4724 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4725 | * When rates and/or formats are zero, the supported values are queried |
| 4726 | * from the given nid. The nid is used also by the default ops.prepare |
| 4727 | * and ops.cleanup callbacks. |
| 4728 | * |
| 4729 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4730 | * ops.close is supposed to be called in the close callback. |
| 4731 | * ops.prepare should be called in the prepare or hw_params callback |
| 4732 | * with the proper parameters for set up. |
| 4733 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4734 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4735 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4737 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4739 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4741 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4742 | int err = snd_hda_codec_build_pcms(codec); |
| 4743 | if (err < 0) |
| 4744 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | } |
| 4746 | return 0; |
| 4747 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4748 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | /** |
| 4751 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4752 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4753 | * @num_configs: number of config enums |
| 4754 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | * @tbl: configuration table, terminated by null entries |
| 4756 | * |
| 4757 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4758 | * given configuration table. If a matching entry is found, returns its |
| 4759 | * config value (supposed to be 0 or positive). |
| 4760 | * |
| 4761 | * If no entries are matching, the function returns a negative value. |
| 4762 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4763 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4764 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4765 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4767 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4768 | int i; |
| 4769 | for (i = 0; i < num_configs; i++) { |
| 4770 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4771 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4772 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4773 | "selected\n", models[i]); |
| 4774 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4779 | if (!codec->bus->pci || !tbl) |
| 4780 | return -1; |
| 4781 | |
| 4782 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4783 | if (!tbl) |
| 4784 | return -1; |
| 4785 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4786 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4787 | char tmp[10]; |
| 4788 | const char *model = NULL; |
| 4789 | if (models) |
| 4790 | model = models[tbl->value]; |
| 4791 | if (!model) { |
| 4792 | sprintf(tmp, "#%d", tbl->value); |
| 4793 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4795 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4796 | "for config %x:%x (%s)\n", |
| 4797 | model, tbl->subvendor, tbl->subdevice, |
| 4798 | (tbl->name ? tbl->name : "Unknown device")); |
| 4799 | #endif |
| 4800 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | } |
| 4802 | return -1; |
| 4803 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4804 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | |
| 4806 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4807 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4808 | subsystem ID with the |
| 4809 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4810 | |
| 4811 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4812 | where the vendor ID of the PCI device is: |
| 4813 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4814 | and the vendor/subvendor are found only at the codec. |
| 4815 | |
| 4816 | * @codec: the HDA codec |
| 4817 | * @num_configs: number of config enums |
| 4818 | * @models: array of model name strings |
| 4819 | * @tbl: configuration table, terminated by null entries |
| 4820 | * |
| 4821 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4822 | * given configuration table. If a matching entry is found, returns its |
| 4823 | * config value (supposed to be 0 or positive). |
| 4824 | * |
| 4825 | * If no entries are matching, the function returns a negative value. |
| 4826 | */ |
| 4827 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4828 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4829 | const struct snd_pci_quirk *tbl) |
| 4830 | { |
| 4831 | const struct snd_pci_quirk *q; |
| 4832 | |
| 4833 | /* Search for codec ID */ |
| 4834 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4835 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4836 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4837 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4838 | break; |
| 4839 | } |
| 4840 | |
| 4841 | if (!q->subvendor) |
| 4842 | return -1; |
| 4843 | |
| 4844 | tbl = q; |
| 4845 | |
| 4846 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4847 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4848 | char tmp[10]; |
| 4849 | const char *model = NULL; |
| 4850 | if (models) |
| 4851 | model = models[tbl->value]; |
| 4852 | if (!model) { |
| 4853 | sprintf(tmp, "#%d", tbl->value); |
| 4854 | model = tmp; |
| 4855 | } |
| 4856 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4857 | "for config %x:%x (%s)\n", |
| 4858 | model, tbl->subvendor, tbl->subdevice, |
| 4859 | (tbl->name ? tbl->name : "Unknown device")); |
| 4860 | #endif |
| 4861 | return tbl->value; |
| 4862 | } |
| 4863 | return -1; |
| 4864 | } |
| 4865 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4866 | |
| 4867 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | * snd_hda_add_new_ctls - create controls from the array |
| 4869 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4870 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | * |
| 4872 | * This helper function creates and add new controls in the given array. |
| 4873 | * The array must be terminated with an empty entry as terminator. |
| 4874 | * |
| 4875 | * Returns 0 if successful, or a negative error code. |
| 4876 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4877 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4878 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4880 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | |
| 4882 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4883 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4884 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4885 | if (knew->iface == -1) /* skip this codec private value */ |
| 4886 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4887 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4888 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4889 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4890 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4891 | if (addr > 0) |
| 4892 | kctl->id.device = addr; |
| 4893 | if (idx > 0) |
| 4894 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4895 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4896 | if (!err) |
| 4897 | break; |
| 4898 | /* try first with another device index corresponding to |
| 4899 | * the codec addr; if it still fails (or it's the |
| 4900 | * primary codec), then try another control index |
| 4901 | */ |
| 4902 | if (!addr && codec->addr) |
| 4903 | addr = codec->addr; |
| 4904 | else if (!idx && !knew->index) { |
| 4905 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4906 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4907 | if (idx <= 0) |
| 4908 | return err; |
| 4909 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4910 | return err; |
| 4911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | } |
| 4913 | return 0; |
| 4914 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4915 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4917 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4918 | static void hda_power_work(struct work_struct *work) |
| 4919 | { |
| 4920 | struct hda_codec *codec = |
| 4921 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4922 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4923 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4924 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4925 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4926 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4927 | spin_unlock(&codec->power_lock); |
| 4928 | return; |
| 4929 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4930 | if (!codec->power_on || codec->power_count) { |
| 4931 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4932 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4933 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4934 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4935 | spin_unlock(&codec->power_lock); |
| 4936 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4937 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | e6bbe66 | 2013-10-24 01:20:24 +0200 | [diff] [blame] | 4938 | if (!codec->pm_down_notified && |
| 4939 | !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4940 | codec->pm_down_notified = 1; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4941 | hda_call_pm_notify(bus, false); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4942 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4946 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4947 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4948 | codec->power_count++; |
| 4949 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4950 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4951 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4954 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4955 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4956 | { |
| 4957 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4958 | if (codec->power_on) |
| 4959 | codec->power_on_acct += delta; |
| 4960 | else |
| 4961 | codec->power_off_acct += delta; |
| 4962 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4963 | } |
| 4964 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4965 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4966 | * transition to D3 to complete. A pending D3 transition is indicated |
| 4967 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4968 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4969 | 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] | 4970 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4971 | struct hda_bus *bus = codec->bus; |
| 4972 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4973 | /* Return if power_on or transitioning to power_on, unless currently |
| 4974 | * powering down. */ |
| 4975 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4976 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4977 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4978 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4979 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4980 | cancel_delayed_work_sync(&codec->power_work); |
| 4981 | |
| 4982 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4983 | /* If the power down delayed work was cancelled above before starting, |
| 4984 | * then there is no need to go through power up here. |
| 4985 | */ |
| 4986 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 4987 | if (codec->power_transition < 0) |
| 4988 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4989 | return; |
| 4990 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4991 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4992 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4993 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4994 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4995 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4996 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4997 | spin_unlock(&codec->power_lock); |
| 4998 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4999 | if (codec->pm_down_notified) { |
| 5000 | codec->pm_down_notified = 0; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 5001 | hda_call_pm_notify(bus, true); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 5002 | } |
| 5003 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5004 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5005 | |
| 5006 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5007 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5008 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5009 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5010 | #define power_save(codec) \ |
| 5011 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5012 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5013 | /* Transition to powered down */ |
| 5014 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5015 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5016 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5017 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5018 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5019 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5020 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5021 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5022 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5023 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | /** |
| 5027 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5028 | * @codec: HD-audio codec |
| 5029 | * @delta: the counter delta to change |
| 5030 | * |
| 5031 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5032 | * appropriately. For the power-down, queue to the delayed action. |
| 5033 | * Passing zero to @delta means to synchronize the power state. |
| 5034 | */ |
| 5035 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5036 | { |
| 5037 | spin_lock(&codec->power_lock); |
| 5038 | codec->power_count += delta; |
| 5039 | trace_hda_power_count(codec); |
| 5040 | if (delta > 0) |
| 5041 | __snd_hda_power_up(codec, d3wait); |
| 5042 | else |
| 5043 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5044 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5045 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5046 | EXPORT_SYMBOL_HDA(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5047 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5048 | /** |
| 5049 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5050 | * @codec: HD-audio codec |
| 5051 | * @check: the object containing an AMP list and the status |
| 5052 | * @nid: NID to check / update |
| 5053 | * |
| 5054 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5055 | * check the current AMP status, and update the the power-status according |
| 5056 | * to the mute status. |
| 5057 | * |
| 5058 | * This function is supposed to be set or called from the check_power_status |
| 5059 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5060 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5061 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5062 | struct hda_loopback_check *check, |
| 5063 | hda_nid_t nid) |
| 5064 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5065 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5066 | int ch, v; |
| 5067 | |
| 5068 | if (!check->amplist) |
| 5069 | return 0; |
| 5070 | for (p = check->amplist; p->nid; p++) { |
| 5071 | if (p->nid == nid) |
| 5072 | break; |
| 5073 | } |
| 5074 | if (!p->nid) |
| 5075 | return 0; /* nothing changed */ |
| 5076 | |
| 5077 | for (p = check->amplist; p->nid; p++) { |
| 5078 | for (ch = 0; ch < 2; ch++) { |
| 5079 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5080 | p->idx); |
| 5081 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5082 | if (!check->power_on) { |
| 5083 | check->power_on = 1; |
| 5084 | snd_hda_power_up(codec); |
| 5085 | } |
| 5086 | return 1; |
| 5087 | } |
| 5088 | } |
| 5089 | } |
| 5090 | if (check->power_on) { |
| 5091 | check->power_on = 0; |
| 5092 | snd_hda_power_down(codec); |
| 5093 | } |
| 5094 | return 0; |
| 5095 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5096 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5097 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5099 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5100 | * Channel mode helper |
| 5101 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5102 | |
| 5103 | /** |
| 5104 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 5105 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5106 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5107 | struct snd_ctl_elem_info *uinfo, |
| 5108 | const struct hda_channel_mode *chmode, |
| 5109 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5110 | { |
| 5111 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5112 | uinfo->count = 1; |
| 5113 | uinfo->value.enumerated.items = num_chmodes; |
| 5114 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5115 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5116 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5117 | chmode[uinfo->value.enumerated.item].channels); |
| 5118 | return 0; |
| 5119 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5120 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5121 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5122 | /** |
| 5123 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 5124 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5125 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5126 | struct snd_ctl_elem_value *ucontrol, |
| 5127 | const struct hda_channel_mode *chmode, |
| 5128 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5129 | int max_channels) |
| 5130 | { |
| 5131 | int i; |
| 5132 | |
| 5133 | for (i = 0; i < num_chmodes; i++) { |
| 5134 | if (max_channels == chmode[i].channels) { |
| 5135 | ucontrol->value.enumerated.item[0] = i; |
| 5136 | break; |
| 5137 | } |
| 5138 | } |
| 5139 | return 0; |
| 5140 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5141 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5142 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5143 | /** |
| 5144 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 5145 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5146 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5147 | struct snd_ctl_elem_value *ucontrol, |
| 5148 | const struct hda_channel_mode *chmode, |
| 5149 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5150 | int *max_channelsp) |
| 5151 | { |
| 5152 | unsigned int mode; |
| 5153 | |
| 5154 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5155 | if (mode >= num_chmodes) |
| 5156 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5157 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5158 | return 0; |
| 5159 | /* change the current channel setting */ |
| 5160 | *max_channelsp = chmode[mode].channels; |
| 5161 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5162 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5163 | return 1; |
| 5164 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5165 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | /* |
| 5168 | * input MUX helper |
| 5169 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5170 | |
| 5171 | /** |
| 5172 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5173 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5174 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5175 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | { |
| 5177 | unsigned int index; |
| 5178 | |
| 5179 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5180 | uinfo->count = 1; |
| 5181 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5182 | if (!imux->num_items) |
| 5183 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | index = uinfo->value.enumerated.item; |
| 5185 | if (index >= imux->num_items) |
| 5186 | index = imux->num_items - 1; |
| 5187 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5188 | return 0; |
| 5189 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5190 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5192 | /** |
| 5193 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5194 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5195 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5196 | const struct hda_input_mux *imux, |
| 5197 | struct snd_ctl_elem_value *ucontrol, |
| 5198 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | unsigned int *cur_val) |
| 5200 | { |
| 5201 | unsigned int idx; |
| 5202 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5203 | if (!imux->num_items) |
| 5204 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | idx = ucontrol->value.enumerated.item[0]; |
| 5206 | if (idx >= imux->num_items) |
| 5207 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5208 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5210 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5211 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | *cur_val = idx; |
| 5213 | return 1; |
| 5214 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5215 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | |
| 5217 | |
| 5218 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5219 | * process kcontrol info callback of a simple string enum array |
| 5220 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5221 | */ |
| 5222 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5223 | struct snd_ctl_elem_info *uinfo, |
| 5224 | int num_items, const char * const *texts) |
| 5225 | { |
| 5226 | static const char * const texts_default[] = { |
| 5227 | "Disabled", "Enabled" |
| 5228 | }; |
| 5229 | |
| 5230 | if (!texts || !num_items) { |
| 5231 | num_items = 2; |
| 5232 | texts = texts_default; |
| 5233 | } |
| 5234 | |
| 5235 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5236 | uinfo->count = 1; |
| 5237 | uinfo->value.enumerated.items = num_items; |
| 5238 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5239 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5240 | strcpy(uinfo->value.enumerated.name, |
| 5241 | texts[uinfo->value.enumerated.item]); |
| 5242 | return 0; |
| 5243 | } |
| 5244 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); |
| 5245 | |
| 5246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | * Multi-channel / digital-out PCM helper functions |
| 5248 | */ |
| 5249 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5250 | /* setup SPDIF output stream */ |
| 5251 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5252 | unsigned int stream_tag, unsigned int format) |
| 5253 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5254 | struct hda_spdif_out *spdif; |
| 5255 | unsigned int curr_fmt; |
| 5256 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5257 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5258 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5259 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5260 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5261 | reset = codec->spdif_status_reset && |
| 5262 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5263 | curr_fmt != format; |
| 5264 | |
| 5265 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5266 | updated */ |
| 5267 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5268 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5269 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5270 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5271 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5272 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5273 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5274 | for (d = codec->slave_dig_outs; *d; d++) |
| 5275 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5276 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5277 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5278 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5279 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5280 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5281 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5282 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5283 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5284 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5285 | { |
| 5286 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5287 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5288 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5289 | for (d = codec->slave_dig_outs; *d; d++) |
| 5290 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5291 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5292 | } |
| 5293 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5294 | /** |
| 5295 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5296 | * @bus: HD-audio bus |
| 5297 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5298 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5299 | { |
| 5300 | struct hda_codec *codec; |
| 5301 | |
| 5302 | if (!bus) |
| 5303 | return; |
| 5304 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5305 | if (hda_codec_is_power_on(codec) && |
| 5306 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5307 | codec->patch_ops.reboot_notify(codec); |
| 5308 | } |
| 5309 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 5310 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5311 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5312 | /** |
| 5313 | * 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] | 5314 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5315 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5316 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5318 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5319 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5320 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5321 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5323 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | return 0; |
| 5325 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5326 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5328 | /** |
| 5329 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5330 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5331 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5332 | struct hda_multi_out *mout, |
| 5333 | unsigned int stream_tag, |
| 5334 | unsigned int format, |
| 5335 | struct snd_pcm_substream *substream) |
| 5336 | { |
| 5337 | mutex_lock(&codec->spdif_mutex); |
| 5338 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5339 | mutex_unlock(&codec->spdif_mutex); |
| 5340 | return 0; |
| 5341 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5342 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5343 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5344 | /** |
| 5345 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5346 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5347 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5348 | struct hda_multi_out *mout) |
| 5349 | { |
| 5350 | mutex_lock(&codec->spdif_mutex); |
| 5351 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5352 | mutex_unlock(&codec->spdif_mutex); |
| 5353 | return 0; |
| 5354 | } |
| 5355 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 5356 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5357 | /** |
| 5358 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5360 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5361 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5363 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5365 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | return 0; |
| 5367 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5368 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5370 | /** |
| 5371 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5372 | * |
| 5373 | * Open analog outputs and set up the hw-constraints. |
| 5374 | * If the digital outputs can be opened as slave, open the digital |
| 5375 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5377 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5378 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5379 | struct snd_pcm_substream *substream, |
| 5380 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5382 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5383 | runtime->hw.channels_max = mout->max_channels; |
| 5384 | if (mout->dig_out_nid) { |
| 5385 | if (!mout->analog_rates) { |
| 5386 | mout->analog_rates = hinfo->rates; |
| 5387 | mout->analog_formats = hinfo->formats; |
| 5388 | mout->analog_maxbps = hinfo->maxbps; |
| 5389 | } else { |
| 5390 | runtime->hw.rates = mout->analog_rates; |
| 5391 | runtime->hw.formats = mout->analog_formats; |
| 5392 | hinfo->maxbps = mout->analog_maxbps; |
| 5393 | } |
| 5394 | if (!mout->spdif_rates) { |
| 5395 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5396 | &mout->spdif_rates, |
| 5397 | &mout->spdif_formats, |
| 5398 | &mout->spdif_maxbps); |
| 5399 | } |
| 5400 | mutex_lock(&codec->spdif_mutex); |
| 5401 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5402 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5403 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5404 | runtime->hw.rates &= mout->spdif_rates; |
| 5405 | runtime->hw.formats &= mout->spdif_formats; |
| 5406 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5407 | hinfo->maxbps = mout->spdif_maxbps; |
| 5408 | } else { |
| 5409 | mout->share_spdif = 0; |
| 5410 | /* FIXME: need notify? */ |
| 5411 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5412 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5413 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5416 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5417 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5418 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5420 | /** |
| 5421 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5422 | * |
| 5423 | * Set up the i/o for analog out. |
| 5424 | * 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] | 5425 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5426 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5427 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | unsigned int stream_tag, |
| 5429 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5430 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5432 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5434 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | int i; |
| 5436 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5437 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5438 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5439 | if (mout->dig_out_nid && mout->share_spdif && |
| 5440 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5442 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5443 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5444 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5446 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5447 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | } else { |
| 5449 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5450 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5453 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | |
| 5455 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5456 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5457 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5458 | if (!mout->no_share_stream && |
| 5459 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5461 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5462 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5463 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5464 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5465 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5466 | snd_hda_codec_setup_stream(codec, |
| 5467 | mout->hp_out_nid[i], |
| 5468 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | /* surrounds */ |
| 5471 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5472 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5473 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5474 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5475 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5476 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5477 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5479 | |
| 5480 | /* extra surrounds */ |
| 5481 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5482 | int ch = 0; |
| 5483 | if (!mout->extra_out_nid[i]) |
| 5484 | break; |
| 5485 | if (chs >= (i + 1) * 2) |
| 5486 | ch = i * 2; |
| 5487 | else if (!mout->no_share_stream) |
| 5488 | break; |
| 5489 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5490 | stream_tag, ch, format); |
| 5491 | } |
| 5492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | return 0; |
| 5494 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5495 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5497 | /** |
| 5498 | * 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] | 5499 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5500 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5501 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5503 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | int i; |
| 5505 | |
| 5506 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5507 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5509 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5510 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5511 | if (mout->hp_out_nid[i]) |
| 5512 | snd_hda_codec_cleanup_stream(codec, |
| 5513 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5514 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5515 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5516 | snd_hda_codec_cleanup_stream(codec, |
| 5517 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5518 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | 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] | 5520 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | mout->dig_out_used = 0; |
| 5522 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5523 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | return 0; |
| 5525 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5526 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5528 | /** |
| 5529 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5530 | * |
| 5531 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5532 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5533 | */ |
| 5534 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5535 | { |
| 5536 | unsigned int pincap; |
| 5537 | unsigned int oldval; |
| 5538 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5539 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5540 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5541 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5542 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5543 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5544 | return AC_PINCTL_VREF_80; |
| 5545 | else if (pincap & AC_PINCAP_VREF_50) |
| 5546 | return AC_PINCTL_VREF_50; |
| 5547 | else if (pincap & AC_PINCAP_VREF_100) |
| 5548 | return AC_PINCTL_VREF_100; |
| 5549 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5550 | return AC_PINCTL_VREF_GRD; |
| 5551 | return AC_PINCTL_VREF_HIZ; |
| 5552 | } |
| 5553 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); |
| 5554 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5555 | /* correct the pin ctl value for matching with the pin cap */ |
| 5556 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5557 | hda_nid_t pin, unsigned int val) |
| 5558 | { |
| 5559 | static unsigned int cap_lists[][2] = { |
| 5560 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5561 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5562 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5563 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5564 | }; |
| 5565 | unsigned int cap; |
| 5566 | |
| 5567 | if (!val) |
| 5568 | return 0; |
| 5569 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5570 | if (!cap) |
| 5571 | return val; /* don't know what to do... */ |
| 5572 | |
| 5573 | if (val & AC_PINCTL_OUT_EN) { |
| 5574 | if (!(cap & AC_PINCAP_OUT)) |
| 5575 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5576 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5577 | val &= ~AC_PINCTL_HP_EN; |
| 5578 | } |
| 5579 | |
| 5580 | if (val & AC_PINCTL_IN_EN) { |
| 5581 | if (!(cap & AC_PINCAP_IN)) |
| 5582 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5583 | else { |
| 5584 | unsigned int vcap, vref; |
| 5585 | int i; |
| 5586 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5587 | vref = val & AC_PINCTL_VREFEN; |
| 5588 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5589 | if (vref == cap_lists[i][0] && |
| 5590 | !(vcap & cap_lists[i][1])) { |
| 5591 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5592 | vref = AC_PINCTL_VREF_HIZ; |
| 5593 | else |
| 5594 | vref = cap_lists[i + 1][0]; |
| 5595 | } |
| 5596 | } |
| 5597 | val &= ~AC_PINCTL_VREFEN; |
| 5598 | val |= vref; |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | return val; |
| 5603 | } |
| 5604 | EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl); |
| 5605 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5606 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5607 | unsigned int val, bool cached) |
| 5608 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5609 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5610 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5611 | if (cached) |
| 5612 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5613 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5614 | else |
| 5615 | return snd_hda_codec_write(codec, pin, 0, |
| 5616 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5617 | } |
| 5618 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); |
| 5619 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5620 | /** |
| 5621 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5622 | * |
| 5623 | * When the same label is used already in the existing items, the number |
| 5624 | * suffix is appended to the label. This label index number is stored |
| 5625 | * to type_idx when non-NULL pointer is given. |
| 5626 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5627 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5628 | int index, int *type_idx) |
| 5629 | { |
| 5630 | int i, label_idx = 0; |
| 5631 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5632 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5633 | return -EINVAL; |
| 5634 | } |
| 5635 | for (i = 0; i < imux->num_items; i++) { |
| 5636 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5637 | label_idx++; |
| 5638 | } |
| 5639 | if (type_idx) |
| 5640 | *type_idx = label_idx; |
| 5641 | if (label_idx > 0) |
| 5642 | snprintf(imux->items[imux->num_items].label, |
| 5643 | sizeof(imux->items[imux->num_items].label), |
| 5644 | "%s %d", label, label_idx); |
| 5645 | else |
| 5646 | strlcpy(imux->items[imux->num_items].label, label, |
| 5647 | sizeof(imux->items[imux->num_items].label)); |
| 5648 | imux->items[imux->num_items].index = index; |
| 5649 | imux->num_items++; |
| 5650 | return 0; |
| 5651 | } |
| 5652 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5653 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | #ifdef CONFIG_PM |
| 5656 | /* |
| 5657 | * power management |
| 5658 | */ |
| 5659 | |
| 5660 | /** |
| 5661 | * snd_hda_suspend - suspend the codecs |
| 5662 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | * |
| 5664 | * Returns 0 if successful. |
| 5665 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5666 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5668 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5670 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5671 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5672 | if (hda_codec_is_power_on(codec)) |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5673 | hda_call_codec_suspend(codec, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | } |
| 5675 | return 0; |
| 5676 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5677 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | |
| 5679 | /** |
| 5680 | * snd_hda_resume - resume the codecs |
| 5681 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | * |
| 5683 | * Returns 0 if successful. |
| 5684 | */ |
| 5685 | int snd_hda_resume(struct hda_bus *bus) |
| 5686 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5687 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5689 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5690 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | return 0; |
| 5693 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5694 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5695 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5696 | |
| 5697 | /* |
| 5698 | * generic arrays |
| 5699 | */ |
| 5700 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5701 | /** |
| 5702 | * snd_array_new - get a new element from the given array |
| 5703 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5704 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5705 | * Get a new element from the given array. If it exceeds the |
| 5706 | * pre-allocated array size, re-allocate the array. |
| 5707 | * |
| 5708 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5709 | */ |
| 5710 | void *snd_array_new(struct snd_array *array) |
| 5711 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5712 | if (snd_BUG_ON(!array->elem_size)) |
| 5713 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5714 | if (array->used >= array->alloced) { |
| 5715 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5716 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5717 | void *nlist; |
| 5718 | if (snd_BUG_ON(num >= 4096)) |
| 5719 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5720 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5721 | if (!nlist) |
| 5722 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5723 | array->list = nlist; |
| 5724 | array->alloced = num; |
| 5725 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5726 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5727 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5728 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5729 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5730 | /** |
| 5731 | * snd_array_free - free the given array elements |
| 5732 | * @array: the array object |
| 5733 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5734 | void snd_array_free(struct snd_array *array) |
| 5735 | { |
| 5736 | kfree(array->list); |
| 5737 | array->used = 0; |
| 5738 | array->alloced = 0; |
| 5739 | array->list = NULL; |
| 5740 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5741 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5742 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5743 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5744 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5745 | * @pcm: PCM caps bits |
| 5746 | * @buf: the string buffer to write |
| 5747 | * @buflen: the max buffer length |
| 5748 | * |
| 5749 | * used by hda_proc.c and hda_eld.c |
| 5750 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5751 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5752 | { |
| 5753 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5754 | int i, j; |
| 5755 | |
| 5756 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5757 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5758 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5759 | |
| 5760 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5761 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5762 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5763 | |
| 5764 | MODULE_DESCRIPTION("HDA codec core"); |
| 5765 | MODULE_LICENSE("GPL"); |