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