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 |
| 188 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 189 | unsigned int verb, unsigned int parm) |
| 190 | { |
| 191 | u32 val; |
| 192 | |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 193 | if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) || |
| 194 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 195 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n", |
| 196 | codec->addr, direct, nid, verb, parm); |
| 197 | return ~0; |
| 198 | } |
| 199 | |
| 200 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 201 | val |= (u32)direct << 27; |
| 202 | val |= (u32)nid << 20; |
| 203 | val |= verb << 8; |
| 204 | val |= parm; |
| 205 | return val; |
| 206 | } |
| 207 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 208 | /* |
| 209 | * Send and receive a verb |
| 210 | */ |
| 211 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
| 212 | unsigned int *res) |
| 213 | { |
| 214 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 215 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 216 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 217 | if (cmd == ~0) |
| 218 | return -1; |
| 219 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 220 | if (res) |
| 221 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 222 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 223 | snd_hda_power_up(codec); |
| 224 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 225 | for (;;) { |
| 226 | trace_hda_send_cmd(codec, cmd); |
| 227 | err = bus->ops.command(bus, cmd); |
| 228 | if (err != -EAGAIN) |
| 229 | break; |
| 230 | /* process pending verbs */ |
| 231 | bus->ops.get_response(bus, codec->addr); |
| 232 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 233 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 234 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 235 | trace_hda_get_response(codec, *res); |
| 236 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 237 | mutex_unlock(&bus->cmd_mutex); |
| 238 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 239 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 240 | if (bus->response_reset) { |
| 241 | snd_printd("hda_codec: resetting BUS due to " |
| 242 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 243 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 244 | bus->ops.bus_reset(bus); |
| 245 | } |
| 246 | goto again; |
| 247 | } |
| 248 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 249 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 250 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 251 | return err; |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /** |
| 255 | * snd_hda_codec_read - send a command and get the response |
| 256 | * @codec: the HDA codec |
| 257 | * @nid: NID to send the command |
| 258 | * @direct: direct flag |
| 259 | * @verb: the verb to send |
| 260 | * @parm: the parameter for the verb |
| 261 | * |
| 262 | * Send a single command and read the corresponding response. |
| 263 | * |
| 264 | * Returns the obtained response value, or -1 for an error. |
| 265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 266 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 267 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | unsigned int verb, unsigned int parm) |
| 269 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 270 | unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
| 271 | unsigned int res; |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 272 | if (codec_exec_verb(codec, cmd, &res)) |
| 273 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return res; |
| 275 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 276 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * snd_hda_codec_write - send a single command without waiting for response |
| 280 | * @codec: the HDA codec |
| 281 | * @nid: NID to send the command |
| 282 | * @direct: direct flag |
| 283 | * @verb: the verb to send |
| 284 | * @parm: the parameter for the verb |
| 285 | * |
| 286 | * Send a single command without waiting for response. |
| 287 | * |
| 288 | * Returns 0 if successful, or a negative error code. |
| 289 | */ |
| 290 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 291 | unsigned int verb, unsigned int parm) |
| 292 | { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 293 | unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 294 | unsigned int res; |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 295 | return codec_exec_verb(codec, cmd, |
| 296 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 298 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * snd_hda_sequence_write - sequence writes |
| 302 | * @codec: the HDA codec |
| 303 | * @seq: VERB array to send |
| 304 | * |
| 305 | * Send the commands sequentially from the given array. |
| 306 | * The array must be terminated with NID=0. |
| 307 | */ |
| 308 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 309 | { |
| 310 | for (; seq->nid; seq++) |
| 311 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 312 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 313 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 317 | * @codec: the HDA codec |
| 318 | * @nid: NID to parse |
| 319 | * @start_id: the pointer to store the start NID |
| 320 | * |
| 321 | * Parse the NID and store the start NID of its sub-nodes. |
| 322 | * Returns the number of sub-nodes. |
| 323 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 324 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 325 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | unsigned int parm; |
| 328 | |
| 329 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 330 | if (parm == -1) |
| 331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | *start_id = (parm >> 16) & 0x7fff; |
| 333 | return (int)(parm & 0x7fff); |
| 334 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 335 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 337 | /* connection list element */ |
| 338 | struct hda_conn_list { |
| 339 | struct list_head list; |
| 340 | int len; |
| 341 | hda_nid_t nid; |
| 342 | hda_nid_t conns[0]; |
| 343 | }; |
| 344 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 345 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 346 | static struct hda_conn_list * |
| 347 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 348 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | struct hda_conn_list *p; |
| 350 | list_for_each_entry(p, &codec->conn_list, list) { |
| 351 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 352 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 353 | } |
| 354 | return NULL; |
| 355 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 356 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 357 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 358 | const hda_nid_t *list) |
| 359 | { |
| 360 | struct hda_conn_list *p; |
| 361 | |
| 362 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 363 | if (!p) |
| 364 | return -ENOMEM; |
| 365 | p->len = len; |
| 366 | p->nid = nid; |
| 367 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 368 | list_add(&p->list, &codec->conn_list); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void remove_conn_list(struct hda_codec *codec) |
| 373 | { |
| 374 | while (!list_empty(&codec->conn_list)) { |
| 375 | struct hda_conn_list *p; |
| 376 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 377 | list_del(&p->list); |
| 378 | kfree(p); |
| 379 | } |
| 380 | } |
| 381 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 382 | /* read the connection and add to the cache */ |
| 383 | 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] | 384 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 385 | hda_nid_t list[32]; |
| 386 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 387 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 388 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 389 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 390 | if (len == -ENOSPC) { |
| 391 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 392 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 393 | if (!result) |
| 394 | return -ENOMEM; |
| 395 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 396 | } |
| 397 | if (len >= 0) |
| 398 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 399 | if (result != list) |
| 400 | kfree(result); |
| 401 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 402 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 403 | |
| 404 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 405 | * snd_hda_get_conn_list - get connection list |
| 406 | * @codec: the HDA codec |
| 407 | * @nid: NID to parse |
| 408 | * @len: number of connection list entries |
| 409 | * @listp: the pointer to store NID list |
| 410 | * |
| 411 | * Parses the connection list of the given widget and stores the pointer |
| 412 | * to the list of NIDs. |
| 413 | * |
| 414 | * Returns the number of connections, or a negative error code. |
| 415 | * |
| 416 | * Note that the returned pointer isn't protected against the list |
| 417 | * modification. If snd_hda_override_conn_list() might be called |
| 418 | * concurrently, protect with a mutex appropriately. |
| 419 | */ |
| 420 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 421 | const hda_nid_t **listp) |
| 422 | { |
| 423 | bool added = false; |
| 424 | |
| 425 | for (;;) { |
| 426 | int err; |
| 427 | const struct hda_conn_list *p; |
| 428 | |
| 429 | /* if the connection-list is already cached, read it */ |
| 430 | p = lookup_conn_list(codec, nid); |
| 431 | if (p) { |
| 432 | if (listp) |
| 433 | *listp = p->conns; |
| 434 | return p->len; |
| 435 | } |
| 436 | if (snd_BUG_ON(added)) |
| 437 | return -EINVAL; |
| 438 | |
| 439 | err = read_and_add_raw_conns(codec, nid); |
| 440 | if (err < 0) |
| 441 | return err; |
| 442 | added = true; |
| 443 | } |
| 444 | } |
| 445 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 446 | |
| 447 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 448 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | * @codec: the HDA codec |
| 450 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 451 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * @max_conns: max. number of connections to store |
| 453 | * |
| 454 | * Parses the connection list of the given widget and stores the list |
| 455 | * of NIDs. |
| 456 | * |
| 457 | * Returns the number of connections, or a negative error code. |
| 458 | */ |
| 459 | 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] | 460 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 461 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 462 | const hda_nid_t *list; |
| 463 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 464 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 465 | if (len > 0 && conn_list) { |
| 466 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 467 | snd_printk(KERN_ERR "hda_codec: " |
| 468 | "Too many connections %d for NID 0x%x\n", |
| 469 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 472 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 473 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 474 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 475 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 476 | } |
| 477 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 478 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 479 | /* return CONNLIST_LEN parameter of the given widget */ |
| 480 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 481 | { |
| 482 | unsigned int wcaps = get_wcaps(codec, nid); |
| 483 | unsigned int parm; |
| 484 | |
| 485 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 486 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 487 | return 0; |
| 488 | |
| 489 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 490 | if (parm == -1) |
| 491 | parm = 0; |
| 492 | return parm; |
| 493 | } |
| 494 | |
| 495 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 496 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 497 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 500 | /** |
| 501 | * snd_hda_get_raw_connections - copy connection list without cache |
| 502 | * @codec: the HDA codec |
| 503 | * @nid: NID to parse |
| 504 | * @conn_list: connection list array |
| 505 | * @max_conns: max. number of connections to store |
| 506 | * |
| 507 | * Like snd_hda_get_connections(), copy the connection list but without |
| 508 | * checking through the connection-list cache. |
| 509 | * Currently called only from hda_proc.c, so not exported. |
| 510 | */ |
| 511 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 512 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 515 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 517 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 518 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 520 | parm = get_num_conns(codec, nid); |
| 521 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 522 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (parm & AC_CLIST_LONG) { |
| 525 | /* long form */ |
| 526 | shift = 16; |
| 527 | num_elems = 2; |
| 528 | } else { |
| 529 | /* short form */ |
| 530 | shift = 8; |
| 531 | num_elems = 4; |
| 532 | } |
| 533 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | mask = (1 << (shift-1)) - 1; |
| 535 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 536 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return 0; /* no connection */ |
| 538 | |
| 539 | if (conn_len == 1) { |
| 540 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 541 | parm = snd_hda_codec_read(codec, nid, 0, |
| 542 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 543 | if (parm == -1 && codec->bus->rirb_error) |
| 544 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 545 | if (conn_list) |
| 546 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | /* multi connection */ |
| 551 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 552 | prev_nid = 0; |
| 553 | for (i = 0; i < conn_len; i++) { |
| 554 | int range_val; |
| 555 | hda_nid_t val, n; |
| 556 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 557 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 558 | parm = snd_hda_codec_read(codec, nid, 0, |
| 559 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 560 | if (parm == -1 && codec->bus->rirb_error) |
| 561 | return -EIO; |
| 562 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 563 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 564 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 565 | if (val == 0 && null_count++) { /* no second chance */ |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 566 | snd_printk(KERN_WARNING "hda_codec: " |
| 567 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 568 | nid, i, parm); |
| 569 | return 0; |
| 570 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 571 | parm >>= shift; |
| 572 | if (range_val) { |
| 573 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 574 | if (!prev_nid || prev_nid >= val) { |
| 575 | snd_printk(KERN_WARNING "hda_codec: " |
| 576 | "invalid dep_range_val %x:%x\n", |
| 577 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 578 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 580 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 581 | if (conn_list) { |
| 582 | if (conns >= max_conns) |
| 583 | return -ENOSPC; |
| 584 | conn_list[conns] = n; |
| 585 | } |
| 586 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 587 | } |
| 588 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 589 | if (conn_list) { |
| 590 | if (conns >= max_conns) |
| 591 | return -ENOSPC; |
| 592 | conn_list[conns] = val; |
| 593 | } |
| 594 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 596 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | return conns; |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 602 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 603 | * @codec: the HDA codec |
| 604 | * @nid: NID to parse |
| 605 | * @len: number of connection list entries |
| 606 | * @list: the list of connection entries |
| 607 | * |
| 608 | * Add or modify the given connection-list to the cache. If the corresponding |
| 609 | * cache already exists, invalidate it and append a new one. |
| 610 | * |
| 611 | * Returns zero or a negative error code. |
| 612 | */ |
| 613 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 614 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 616 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 617 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 618 | p = lookup_conn_list(codec, nid); |
| 619 | if (p) { |
| 620 | list_del(&p->list); |
| 621 | kfree(p); |
| 622 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 623 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 624 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 626 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 627 | |
| 628 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 629 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 630 | * @codec: the HDA codec |
| 631 | * @mux: NID containing the list |
| 632 | * @nid: NID to select |
| 633 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 634 | * |
| 635 | * Parses the connection list of the widget @mux and checks whether the |
| 636 | * widget @nid is present. If it is, return the connection index. |
| 637 | * Otherwise it returns -1. |
| 638 | */ |
| 639 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 640 | hda_nid_t nid, int recursive) |
| 641 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 642 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 643 | int i, nums; |
| 644 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 645 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 646 | for (i = 0; i < nums; i++) |
| 647 | if (conn[i] == nid) |
| 648 | return i; |
| 649 | if (!recursive) |
| 650 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 651 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 652 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 653 | return -1; |
| 654 | } |
| 655 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 656 | for (i = 0; i < nums; i++) { |
| 657 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 658 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 659 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 660 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 661 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 662 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | /** |
| 668 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 669 | * @bus: the BUS |
| 670 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 671 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 672 | * |
| 673 | * Adds the given event to the queue. The events are processed in |
| 674 | * the workqueue asynchronously. Call this function in the interrupt |
| 675 | * hanlder when RIRB receives an unsolicited event. |
| 676 | * |
| 677 | * Returns 0 if successful, or a negative error code. |
| 678 | */ |
| 679 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 680 | { |
| 681 | struct hda_bus_unsolicited *unsol; |
| 682 | unsigned int wp; |
| 683 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 684 | if (!bus || !bus->workq) |
| 685 | return 0; |
| 686 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 687 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 688 | unsol = bus->unsol; |
| 689 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | |
| 692 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 693 | unsol->wp = wp; |
| 694 | |
| 695 | wp <<= 1; |
| 696 | unsol->queue[wp] = res; |
| 697 | unsol->queue[wp + 1] = res_ex; |
| 698 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 699 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 703 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 706 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 708 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 710 | struct hda_bus_unsolicited *unsol = |
| 711 | container_of(work, struct hda_bus_unsolicited, work); |
| 712 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | struct hda_codec *codec; |
| 714 | unsigned int rp, caddr, res; |
| 715 | |
| 716 | while (unsol->rp != unsol->wp) { |
| 717 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 718 | unsol->rp = rp; |
| 719 | rp <<= 1; |
| 720 | res = unsol->queue[rp]; |
| 721 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 722 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | continue; |
| 724 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 725 | if (codec && codec->patch_ops.unsol_event) |
| 726 | codec->patch_ops.unsol_event(codec, res); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * initialize unsolicited queue |
| 732 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 733 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
| 735 | struct hda_bus_unsolicited *unsol; |
| 736 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 737 | if (bus->unsol) /* already initialized */ |
| 738 | return 0; |
| 739 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 740 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 741 | if (!unsol) { |
| 742 | snd_printk(KERN_ERR "hda_codec: " |
| 743 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return -ENOMEM; |
| 745 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 746 | INIT_WORK(&unsol->work, process_unsol_events); |
| 747 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | bus->unsol = unsol; |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * destructor |
| 754 | */ |
| 755 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 756 | |
| 757 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 758 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 759 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 761 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 763 | if (bus->workq) |
| 764 | flush_workqueue(bus->workq); |
| 765 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 767 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | snd_hda_codec_free(codec); |
| 769 | } |
| 770 | if (bus->ops.private_free) |
| 771 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 772 | if (bus->workq) |
| 773 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | kfree(bus); |
| 775 | return 0; |
| 776 | } |
| 777 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 778 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
| 780 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 781 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return snd_hda_bus_free(bus); |
| 783 | } |
| 784 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 785 | #ifdef CONFIG_SND_HDA_HWDEP |
| 786 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 787 | { |
| 788 | struct hda_bus *bus = device->device_data; |
| 789 | struct hda_codec *codec; |
| 790 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 791 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 792 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 793 | } |
| 794 | return 0; |
| 795 | } |
| 796 | #else |
| 797 | #define snd_hda_bus_dev_register NULL |
| 798 | #endif |
| 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | /** |
| 801 | * snd_hda_bus_new - create a HDA bus |
| 802 | * @card: the card entry |
| 803 | * @temp: the template for hda_bus information |
| 804 | * @busp: the pointer to store the created bus instance |
| 805 | * |
| 806 | * Returns 0 if successful, or a negative error code. |
| 807 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 808 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 809 | const struct hda_bus_template *temp, |
| 810 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
| 812 | struct hda_bus *bus; |
| 813 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 814 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 815 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | .dev_free = snd_hda_bus_dev_free, |
| 817 | }; |
| 818 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 819 | if (snd_BUG_ON(!temp)) |
| 820 | return -EINVAL; |
| 821 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 822 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
| 824 | if (busp) |
| 825 | *busp = NULL; |
| 826 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 827 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | if (bus == NULL) { |
| 829 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 830 | return -ENOMEM; |
| 831 | } |
| 832 | |
| 833 | bus->card = card; |
| 834 | bus->private_data = temp->private_data; |
| 835 | bus->pci = temp->pci; |
| 836 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 837 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | bus->ops = temp->ops; |
| 839 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 840 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 841 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | INIT_LIST_HEAD(&bus->codec_list); |
| 843 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 844 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 845 | "hd-audio%d", card->number); |
| 846 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 847 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 848 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 849 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 850 | kfree(bus); |
| 851 | return -ENOMEM; |
| 852 | } |
| 853 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 854 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 855 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | snd_hda_bus_free(bus); |
| 857 | return err; |
| 858 | } |
| 859 | if (busp) |
| 860 | *busp = bus; |
| 861 | return 0; |
| 862 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 863 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 865 | #ifdef CONFIG_SND_HDA_GENERIC |
| 866 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 867 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 868 | #else |
| 869 | #define is_generic_config(codec) 0 |
| 870 | #endif |
| 871 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 872 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 873 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 874 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 875 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 876 | #endif |
| 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | /* |
| 879 | * find a matching codec preset |
| 880 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 881 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 882 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 884 | struct hda_codec_preset_list *tbl; |
| 885 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 886 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 888 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 889 | return NULL; /* use the generic parser */ |
| 890 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 891 | again: |
| 892 | mutex_lock(&preset_mutex); |
| 893 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 894 | if (!try_module_get(tbl->owner)) { |
| 895 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 896 | continue; |
| 897 | } |
| 898 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 900 | if (preset->afg && preset->afg != codec->afg) |
| 901 | continue; |
| 902 | if (preset->mfg && preset->mfg != codec->mfg) |
| 903 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 904 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 906 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 907 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 908 | preset->rev == codec->revision_id)) { |
| 909 | mutex_unlock(&preset_mutex); |
| 910 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 914 | module_put(tbl->owner); |
| 915 | } |
| 916 | mutex_unlock(&preset_mutex); |
| 917 | |
| 918 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 919 | char name[32]; |
| 920 | if (!mod_requested) |
| 921 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 922 | codec->vendor_id); |
| 923 | else |
| 924 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 925 | (codec->vendor_id >> 16) & 0xffff); |
| 926 | request_module(name); |
| 927 | mod_requested++; |
| 928 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | return NULL; |
| 931 | } |
| 932 | |
| 933 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 934 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 936 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { |
| 938 | const struct hda_vendor_id *c; |
| 939 | const char *vendor = NULL; |
| 940 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 941 | char tmp[16]; |
| 942 | |
| 943 | if (codec->vendor_name) |
| 944 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | for (c = hda_vendor_ids; c->id; c++) { |
| 947 | if (c->id == vendor_id) { |
| 948 | vendor = c->name; |
| 949 | break; |
| 950 | } |
| 951 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 952 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | sprintf(tmp, "Generic %04x", vendor_id); |
| 954 | vendor = tmp; |
| 955 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 956 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 957 | if (!codec->vendor_name) |
| 958 | return -ENOMEM; |
| 959 | |
| 960 | get_chip_name: |
| 961 | if (codec->chip_name) |
| 962 | return 0; |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 965 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 966 | else { |
| 967 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 968 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 969 | } |
| 970 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 971 | return -ENOMEM; |
| 972 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 976 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 978 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 980 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | hda_nid_t nid; |
| 982 | |
| 983 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 984 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 985 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 986 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 987 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 988 | case AC_GRP_AUDIO_FUNCTION: |
| 989 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 990 | codec->afg_function_id = function_id & 0xff; |
| 991 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 992 | break; |
| 993 | case AC_GRP_MODEM_FUNCTION: |
| 994 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 995 | codec->mfg_function_id = function_id & 0xff; |
| 996 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 997 | break; |
| 998 | default: |
| 999 | break; |
| 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1005 | * read widget caps for each widget and store in cache |
| 1006 | */ |
| 1007 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1008 | { |
| 1009 | int i; |
| 1010 | hda_nid_t nid; |
| 1011 | |
| 1012 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1013 | &codec->start_nid); |
| 1014 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1015 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1016 | return -ENOMEM; |
| 1017 | nid = codec->start_nid; |
| 1018 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1019 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1020 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1024 | /* read all pin default configurations and save codec->init_pins */ |
| 1025 | static int read_pin_defaults(struct hda_codec *codec) |
| 1026 | { |
| 1027 | int i; |
| 1028 | hda_nid_t nid = codec->start_nid; |
| 1029 | |
| 1030 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1031 | struct hda_pincfg *pin; |
| 1032 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1033 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1034 | if (wid_type != AC_WID_PIN) |
| 1035 | continue; |
| 1036 | pin = snd_array_new(&codec->init_pins); |
| 1037 | if (!pin) |
| 1038 | return -ENOMEM; |
| 1039 | pin->nid = nid; |
| 1040 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1041 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1042 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1043 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1044 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1045 | } |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | /* look up the given pin config list and return the item matching with NID */ |
| 1050 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1051 | struct snd_array *array, |
| 1052 | hda_nid_t nid) |
| 1053 | { |
| 1054 | int i; |
| 1055 | for (i = 0; i < array->used; i++) { |
| 1056 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1057 | if (pin->nid == nid) |
| 1058 | return pin; |
| 1059 | } |
| 1060 | return NULL; |
| 1061 | } |
| 1062 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1063 | /* set the current pin config value for the given NID. |
| 1064 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1065 | */ |
| 1066 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1067 | hda_nid_t nid, unsigned int cfg) |
| 1068 | { |
| 1069 | struct hda_pincfg *pin; |
| 1070 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1071 | /* the check below may be invalid when pins are added by a fixup |
| 1072 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1073 | * for now |
| 1074 | */ |
| 1075 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1076 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1077 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1078 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1079 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1080 | pin = look_up_pincfg(codec, list, nid); |
| 1081 | if (!pin) { |
| 1082 | pin = snd_array_new(list); |
| 1083 | if (!pin) |
| 1084 | return -ENOMEM; |
| 1085 | pin->nid = nid; |
| 1086 | } |
| 1087 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1091 | /** |
| 1092 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1093 | * @codec: the HDA codec |
| 1094 | * @nid: NID to set the pin config |
| 1095 | * @cfg: the pin default config value |
| 1096 | * |
| 1097 | * Override a pin default configuration value in the cache. |
| 1098 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1099 | * priority than the real hardware value. |
| 1100 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1101 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1102 | hda_nid_t nid, unsigned int cfg) |
| 1103 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1104 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1105 | } |
| 1106 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1107 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1108 | /** |
| 1109 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1110 | * @codec: the HDA codec |
| 1111 | * @nid: NID to get the pin config |
| 1112 | * |
| 1113 | * Get the current pin config value of the given pin NID. |
| 1114 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1115 | * returns the cached value. |
| 1116 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1117 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1118 | { |
| 1119 | struct hda_pincfg *pin; |
| 1120 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1121 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1122 | { |
| 1123 | unsigned int cfg = 0; |
| 1124 | mutex_lock(&codec->user_mutex); |
| 1125 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1126 | if (pin) |
| 1127 | cfg = pin->cfg; |
| 1128 | mutex_unlock(&codec->user_mutex); |
| 1129 | if (cfg) |
| 1130 | return cfg; |
| 1131 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1132 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1133 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1134 | if (pin) |
| 1135 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1136 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1137 | if (pin) |
| 1138 | return pin->cfg; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1142 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1143 | /* remember the current pinctl target value */ |
| 1144 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1145 | unsigned int val) |
| 1146 | { |
| 1147 | struct hda_pincfg *pin; |
| 1148 | |
| 1149 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1150 | if (!pin) |
| 1151 | return -EINVAL; |
| 1152 | pin->target = val; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target); |
| 1156 | |
| 1157 | /* return the current pinctl target value */ |
| 1158 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1159 | { |
| 1160 | struct hda_pincfg *pin; |
| 1161 | |
| 1162 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1163 | if (!pin) |
| 1164 | return 0; |
| 1165 | return pin->target; |
| 1166 | } |
| 1167 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target); |
| 1168 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1169 | /** |
| 1170 | * snd_hda_shutup_pins - Shut up all pins |
| 1171 | * @codec: the HDA codec |
| 1172 | * |
| 1173 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1174 | * The controls aren't cached so that they can be resumed properly. |
| 1175 | */ |
| 1176 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1177 | { |
| 1178 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1179 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1180 | * the default pin setup at the next load of the driver |
| 1181 | */ |
| 1182 | if (codec->bus->shutdown) |
| 1183 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1184 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1185 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1186 | /* use read here for syncing after issuing each verb */ |
| 1187 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1188 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1189 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1190 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1191 | } |
| 1192 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1193 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1194 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1195 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1196 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1197 | { |
| 1198 | int i; |
| 1199 | if (!codec->pins_shutup) |
| 1200 | return; |
| 1201 | if (codec->bus->shutdown) |
| 1202 | return; |
| 1203 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1204 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1205 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1206 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1207 | pin->ctrl); |
| 1208 | } |
| 1209 | codec->pins_shutup = 0; |
| 1210 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1211 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1212 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1213 | static void hda_jackpoll_work(struct work_struct *work) |
| 1214 | { |
| 1215 | struct hda_codec *codec = |
| 1216 | container_of(work, struct hda_codec, jackpoll_work.work); |
| 1217 | if (!codec->jackpoll_interval) |
| 1218 | return; |
| 1219 | |
| 1220 | snd_hda_jack_set_dirty_all(codec); |
| 1221 | snd_hda_jack_poll_all(codec); |
| 1222 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1223 | codec->jackpoll_interval); |
| 1224 | } |
| 1225 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1226 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1227 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1228 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1229 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1230 | /* release all pincfg lists */ |
| 1231 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1232 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1233 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1234 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1235 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1236 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1237 | snd_array_free(&codec->init_pins); |
| 1238 | } |
| 1239 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1240 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1241 | * audio-converter setup caches |
| 1242 | */ |
| 1243 | struct hda_cvt_setup { |
| 1244 | hda_nid_t nid; |
| 1245 | u8 stream_tag; |
| 1246 | u8 channel_id; |
| 1247 | u16 format_id; |
| 1248 | unsigned char active; /* cvt is currently used */ |
| 1249 | unsigned char dirty; /* setups should be cleared */ |
| 1250 | }; |
| 1251 | |
| 1252 | /* get or create a cache entry for the given audio converter NID */ |
| 1253 | static struct hda_cvt_setup * |
| 1254 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1255 | { |
| 1256 | struct hda_cvt_setup *p; |
| 1257 | int i; |
| 1258 | |
| 1259 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1260 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1261 | if (p->nid == nid) |
| 1262 | return p; |
| 1263 | } |
| 1264 | p = snd_array_new(&codec->cvt_setups); |
| 1265 | if (p) |
| 1266 | p->nid = nid; |
| 1267 | return p; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | * codec destructor |
| 1272 | */ |
| 1273 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1274 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1275 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1277 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1278 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1279 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1280 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1281 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1282 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1283 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1285 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1286 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1287 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1288 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1289 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1291 | if (codec->patch_ops.free) |
| 1292 | codec->patch_ops.free(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1293 | #ifdef CONFIG_PM |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 1294 | if (!codec->pm_down_notified) /* cancel leftover refcounts */ |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1295 | hda_call_pm_notify(codec->bus, false); |
| 1296 | #endif |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1297 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1298 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1299 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1300 | kfree(codec->vendor_name); |
| 1301 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1302 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1303 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | kfree(codec); |
| 1305 | } |
| 1306 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1307 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1308 | hda_nid_t fg, unsigned int power_state); |
| 1309 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1310 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1311 | unsigned int power_state); |
| 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | /** |
| 1314 | * snd_hda_codec_new - create a HDA codec |
| 1315 | * @bus: the bus to assign |
| 1316 | * @codec_addr: the codec address |
| 1317 | * @codecp: the pointer to store the generated codec |
| 1318 | * |
| 1319 | * Returns 0 if successful, or a negative error code. |
| 1320 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1321 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1322 | unsigned int codec_addr, |
| 1323 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
| 1325 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1326 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1327 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | int err; |
| 1329 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1330 | if (snd_BUG_ON(!bus)) |
| 1331 | return -EINVAL; |
| 1332 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1333 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1336 | snd_printk(KERN_ERR "hda_codec: " |
| 1337 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | return -EBUSY; |
| 1339 | } |
| 1340 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1341 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | if (codec == NULL) { |
| 1343 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1344 | return -ENOMEM; |
| 1345 | } |
| 1346 | |
| 1347 | codec->bus = bus; |
| 1348 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1349 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1350 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1351 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1352 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1353 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1354 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1355 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1356 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1357 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1358 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1359 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1360 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1361 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1362 | INIT_LIST_HEAD(&codec->conn_list); |
| 1363 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1364 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1366 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1367 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1368 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1369 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1370 | * the caller has to power down appropriatley after initialization |
| 1371 | * phase. |
| 1372 | */ |
| 1373 | hda_keep_power_on(codec); |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 1374 | hda_call_pm_notify(bus, true); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1375 | #endif |
| 1376 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1377 | if (codec->bus->modelname) { |
| 1378 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1379 | if (!codec->modelname) { |
| 1380 | snd_hda_codec_free(codec); |
| 1381 | return -ENODEV; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | list_add_tail(&codec->list, &bus->codec_list); |
| 1386 | bus->caddr_tbl[codec_addr] = codec; |
| 1387 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1388 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1389 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1390 | if (codec->vendor_id == -1) |
| 1391 | /* read again, hopefully the access method was corrected |
| 1392 | * in the last read... |
| 1393 | */ |
| 1394 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1395 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1396 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1397 | AC_PAR_SUBSYSTEM_ID); |
| 1398 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1399 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1401 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1402 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1403 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1404 | err = -ENODEV; |
| 1405 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1408 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1409 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1410 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1411 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1412 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1413 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1414 | err = read_pin_defaults(codec); |
| 1415 | if (err < 0) |
| 1416 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1417 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1418 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1419 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1420 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1421 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1424 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1425 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1426 | AC_PWRST_CLKSTOP); |
| 1427 | if (!codec->d3_stop_clk) |
| 1428 | bus->power_keep_link_on = 1; |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1429 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1430 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1431 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1432 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1433 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1434 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1435 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1436 | snd_hda_codec_proc_new(codec); |
| 1437 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1438 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1439 | |
| 1440 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1441 | codec->subsystem_id, codec->revision_id); |
| 1442 | snd_component_add(codec->bus->card, component); |
| 1443 | |
| 1444 | if (codecp) |
| 1445 | *codecp = codec; |
| 1446 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1447 | |
| 1448 | error: |
| 1449 | snd_hda_codec_free(codec); |
| 1450 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1451 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1452 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1453 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1454 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1455 | { |
| 1456 | hda_nid_t fg; |
| 1457 | int err; |
| 1458 | |
| 1459 | /* Assume the function group node does not change, |
| 1460 | * only the widget nodes may change. |
| 1461 | */ |
| 1462 | kfree(codec->wcaps); |
| 1463 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1464 | err = read_widget_caps(codec, fg); |
| 1465 | if (err < 0) { |
| 1466 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1467 | return err; |
| 1468 | } |
| 1469 | |
| 1470 | snd_array_free(&codec->init_pins); |
| 1471 | err = read_pin_defaults(codec); |
| 1472 | |
| 1473 | return err; |
| 1474 | } |
| 1475 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets); |
| 1476 | |
| 1477 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1478 | /** |
| 1479 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1480 | * @codec: the HDA codec |
| 1481 | * |
| 1482 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1483 | * patch instance. |
| 1484 | * |
| 1485 | * Returns 0 if successful or a negative error code. |
| 1486 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1487 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1488 | { |
| 1489 | int err; |
| 1490 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1491 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1492 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1493 | err = get_codec_name(codec); |
| 1494 | if (err < 0) |
| 1495 | return err; |
| 1496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1498 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1500 | goto patched; |
| 1501 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1502 | if (codec->preset && codec->preset->patch) { |
| 1503 | err = codec->preset->patch(codec); |
| 1504 | goto patched; |
| 1505 | } |
| 1506 | |
| 1507 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1508 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 1509 | if (err < 0) |
| 1510 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1511 | |
| 1512 | patched: |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1513 | if (!err && codec->patch_ops.unsol_event) |
| 1514 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1515 | /* audio codec should override the mixer name */ |
| 1516 | if (!err && (codec->afg || !*codec->bus->card->mixername)) |
| 1517 | snprintf(codec->bus->card->mixername, |
| 1518 | sizeof(codec->bus->card->mixername), |
| 1519 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1520 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1522 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1524 | /* update the stream-id if changed */ |
| 1525 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1526 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1527 | u32 stream_tag, int channel_id) |
| 1528 | { |
| 1529 | unsigned int oldval, newval; |
| 1530 | |
| 1531 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1532 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1533 | newval = (stream_tag << 4) | channel_id; |
| 1534 | if (oldval != newval) |
| 1535 | snd_hda_codec_write(codec, nid, 0, |
| 1536 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1537 | newval); |
| 1538 | p->stream_tag = stream_tag; |
| 1539 | p->channel_id = channel_id; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | /* update the format-id if changed */ |
| 1544 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1545 | hda_nid_t nid, int format) |
| 1546 | { |
| 1547 | unsigned int oldval; |
| 1548 | |
| 1549 | if (p->format_id != format) { |
| 1550 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1551 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1552 | if (oldval != format) { |
| 1553 | msleep(1); |
| 1554 | snd_hda_codec_write(codec, nid, 0, |
| 1555 | AC_VERB_SET_STREAM_FORMAT, |
| 1556 | format); |
| 1557 | } |
| 1558 | p->format_id = format; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | /** |
| 1563 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1564 | * @codec: the CODEC to set up |
| 1565 | * @nid: the NID to set up |
| 1566 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1567 | * @channel_id: channel id to pass, zero based. |
| 1568 | * @format: stream format. |
| 1569 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1570 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1571 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | int channel_id, int format) |
| 1573 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1574 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1575 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1576 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1577 | int i; |
| 1578 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1579 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1580 | return; |
| 1581 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1582 | snd_printdd("hda_codec_setup_stream: " |
| 1583 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1585 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1586 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1587 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1588 | |
| 1589 | if (codec->pcm_format_first) |
| 1590 | update_pcm_format(codec, p, nid, format); |
| 1591 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1592 | if (!codec->pcm_format_first) |
| 1593 | update_pcm_format(codec, p, nid, format); |
| 1594 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1595 | p->active = 1; |
| 1596 | p->dirty = 0; |
| 1597 | |
| 1598 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1599 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1600 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1601 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1602 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1603 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1604 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1605 | p->dirty = 1; |
| 1606 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1609 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1611 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1612 | struct hda_cvt_setup *q); |
| 1613 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1614 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1615 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1616 | * @codec: the CODEC to clean up |
| 1617 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1618 | * @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] | 1619 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1620 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1621 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1622 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1623 | struct hda_cvt_setup *p; |
| 1624 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1625 | if (!nid) |
| 1626 | return; |
| 1627 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1628 | if (codec->no_sticky_stream) |
| 1629 | do_now = 1; |
| 1630 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1631 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1632 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1633 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1634 | /* here we just clear the active flag when do_now isn't set; |
| 1635 | * actual clean-ups will be done later in |
| 1636 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1637 | */ |
| 1638 | if (do_now) |
| 1639 | really_cleanup_stream(codec, p); |
| 1640 | else |
| 1641 | p->active = 0; |
| 1642 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1643 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1644 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1645 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1646 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1647 | struct hda_cvt_setup *q) |
| 1648 | { |
| 1649 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1650 | if (q->stream_tag || q->channel_id) |
| 1651 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1652 | if (q->format_id) |
| 1653 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1654 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1655 | memset(q, 0, sizeof(*q)); |
| 1656 | q->nid = nid; |
| 1657 | } |
| 1658 | |
| 1659 | /* clean up the all conflicting obsolete streams */ |
| 1660 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1661 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1662 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1663 | int i; |
| 1664 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1665 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1666 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1667 | struct hda_cvt_setup *p; |
| 1668 | p = snd_array_elem(&c->cvt_setups, i); |
| 1669 | if (p->dirty) |
| 1670 | really_cleanup_stream(c, p); |
| 1671 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1675 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1676 | /* clean up all streams; called from suspend */ |
| 1677 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1678 | { |
| 1679 | int i; |
| 1680 | |
| 1681 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1682 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1683 | if (p->stream_tag) |
| 1684 | really_cleanup_stream(codec, p); |
| 1685 | } |
| 1686 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1687 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | /* |
| 1690 | * amp access functions |
| 1691 | */ |
| 1692 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1693 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1694 | #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] | 1695 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1696 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1697 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1699 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1702 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1703 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1705 | memset(cache, 0, sizeof(*cache)); |
| 1706 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1707 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1710 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1711 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1712 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1716 | 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] | 1717 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1718 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1719 | u16 cur = cache->hash[idx]; |
| 1720 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
| 1722 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1723 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | if (info->key == key) |
| 1725 | return info; |
| 1726 | cur = info->next; |
| 1727 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1728 | return NULL; |
| 1729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1731 | /* query the hash. allocate an entry if not found. */ |
| 1732 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1733 | u32 key) |
| 1734 | { |
| 1735 | struct hda_cache_head *info = get_hash(cache, key); |
| 1736 | if (!info) { |
| 1737 | u16 idx, cur; |
| 1738 | /* add a new hash entry */ |
| 1739 | info = snd_array_new(&cache->buf); |
| 1740 | if (!info) |
| 1741 | return NULL; |
| 1742 | cur = snd_array_index(&cache->buf, info); |
| 1743 | info->key = key; |
| 1744 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1745 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1746 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1747 | info->next = cache->hash[idx]; |
| 1748 | cache->hash[idx] = cur; |
| 1749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | return info; |
| 1751 | } |
| 1752 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1753 | /* query and allocate an amp hash entry */ |
| 1754 | static inline struct hda_amp_info * |
| 1755 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1756 | { |
| 1757 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1758 | } |
| 1759 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1760 | /* overwrite the value with the key in the caps hash */ |
| 1761 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1762 | { |
| 1763 | struct hda_amp_info *info; |
| 1764 | |
| 1765 | mutex_lock(&codec->hash_mutex); |
| 1766 | info = get_alloc_amp_hash(codec, key); |
| 1767 | if (!info) { |
| 1768 | mutex_unlock(&codec->hash_mutex); |
| 1769 | return -EINVAL; |
| 1770 | } |
| 1771 | info->amp_caps = val; |
| 1772 | info->head.val |= INFO_AMP_CAPS; |
| 1773 | mutex_unlock(&codec->hash_mutex); |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | /* query the value from the caps hash; if not found, fetch the current |
| 1778 | * value from the given function and store in the hash |
| 1779 | */ |
| 1780 | static unsigned int |
| 1781 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1782 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1783 | { |
| 1784 | struct hda_amp_info *info; |
| 1785 | unsigned int val; |
| 1786 | |
| 1787 | mutex_lock(&codec->hash_mutex); |
| 1788 | info = get_alloc_amp_hash(codec, key); |
| 1789 | if (!info) { |
| 1790 | mutex_unlock(&codec->hash_mutex); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1794 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1795 | val = func(codec, nid, dir); |
| 1796 | write_caps_hash(codec, key, val); |
| 1797 | } else { |
| 1798 | val = info->amp_caps; |
| 1799 | mutex_unlock(&codec->hash_mutex); |
| 1800 | } |
| 1801 | return val; |
| 1802 | } |
| 1803 | |
| 1804 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1805 | int direction) |
| 1806 | { |
| 1807 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1808 | nid = codec->afg; |
| 1809 | return snd_hda_param_read(codec, nid, |
| 1810 | direction == HDA_OUTPUT ? |
| 1811 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1812 | } |
| 1813 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1814 | /** |
| 1815 | * query_amp_caps - query AMP capabilities |
| 1816 | * @codec: the HD-auio codec |
| 1817 | * @nid: the NID to query |
| 1818 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1819 | * |
| 1820 | * Query AMP capabilities for the given widget and direction. |
| 1821 | * Returns the obtained capability bits. |
| 1822 | * |
| 1823 | * When cap bits have been already read, this doesn't read again but |
| 1824 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1826 | 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] | 1827 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1828 | return query_caps_hash(codec, nid, direction, |
| 1829 | HDA_HASH_KEY(nid, direction, 0), |
| 1830 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1832 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1834 | /** |
| 1835 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1836 | * @codec: the CODEC to clean up |
| 1837 | * @nid: the NID to clean up |
| 1838 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1839 | * @caps: the capability bits to set |
| 1840 | * |
| 1841 | * Override the cached AMP caps bits value by the given one. |
| 1842 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1843 | * e.g. limit to 0dB, etc. |
| 1844 | * |
| 1845 | * Returns zero if successful or a negative error code. |
| 1846 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1847 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1848 | unsigned int caps) |
| 1849 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1850 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1851 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1852 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1853 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1854 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1855 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1856 | { |
| 1857 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1858 | } |
| 1859 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1860 | /** |
| 1861 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1862 | * @codec: the HD-auio codec |
| 1863 | * @nid: the NID to query |
| 1864 | * |
| 1865 | * Query PIN capabilities for the given widget. |
| 1866 | * Returns the obtained capability bits. |
| 1867 | * |
| 1868 | * When cap bits have been already read, this doesn't read again but |
| 1869 | * returns the cached value. |
| 1870 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1871 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1872 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1873 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1874 | read_pin_cap); |
| 1875 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1876 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 1877 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1878 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1879 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1880 | * @codec: the CODEC |
| 1881 | * @nid: the NID to override |
| 1882 | * @caps: the capability bits to set |
| 1883 | * |
| 1884 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1885 | * |
| 1886 | * Returns zero if successful or a negative error code. |
| 1887 | */ |
| 1888 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1889 | unsigned int caps) |
| 1890 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1891 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1892 | } |
| 1893 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 1894 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1895 | /* read or sync the hash value with the current value; |
| 1896 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1898 | static struct hda_amp_info * |
| 1899 | 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] | 1900 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1902 | struct hda_amp_info *info; |
| 1903 | unsigned int parm, val = 0; |
| 1904 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1906 | retry: |
| 1907 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1908 | if (!info) |
| 1909 | return NULL; |
| 1910 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1911 | if (!val_read) { |
| 1912 | mutex_unlock(&codec->hash_mutex); |
| 1913 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1914 | parm |= direction == HDA_OUTPUT ? |
| 1915 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1916 | parm |= index; |
| 1917 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1918 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1919 | val &= 0xff; |
| 1920 | val_read = true; |
| 1921 | mutex_lock(&codec->hash_mutex); |
| 1922 | goto retry; |
| 1923 | } |
| 1924 | info->vol[ch] = val; |
| 1925 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1926 | } else if (init_only) |
| 1927 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1928 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1932 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1934 | 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] | 1935 | hda_nid_t nid, int ch, int direction, int index, |
| 1936 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | { |
| 1938 | u32 parm; |
| 1939 | |
| 1940 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1941 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1942 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1943 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1944 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1945 | ; /* set the zero value as a fake mute */ |
| 1946 | else |
| 1947 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1949 | } |
| 1950 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1951 | /** |
| 1952 | * snd_hda_codec_amp_read - Read AMP value |
| 1953 | * @codec: HD-audio codec |
| 1954 | * @nid: NID to read the AMP value |
| 1955 | * @ch: channel (left=0 or right=1) |
| 1956 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1957 | * @index: the index value (only for input direction) |
| 1958 | * |
| 1959 | * 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] | 1960 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1961 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1962 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1964 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1965 | unsigned int val = 0; |
| 1966 | |
| 1967 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1968 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1969 | if (info) |
| 1970 | val = info->vol[ch]; |
| 1971 | mutex_unlock(&codec->hash_mutex); |
| 1972 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1974 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1976 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1977 | int direction, int idx, int mask, int val, |
| 1978 | bool init_only) |
| 1979 | { |
| 1980 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1981 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1982 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1983 | |
| 1984 | if (snd_BUG_ON(mask & ~0xff)) |
| 1985 | mask &= 0xff; |
| 1986 | val &= mask; |
| 1987 | |
| 1988 | mutex_lock(&codec->hash_mutex); |
| 1989 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1990 | if (!info) { |
| 1991 | mutex_unlock(&codec->hash_mutex); |
| 1992 | return 0; |
| 1993 | } |
| 1994 | val |= info->vol[ch] & ~mask; |
| 1995 | if (info->vol[ch] == val) { |
| 1996 | mutex_unlock(&codec->hash_mutex); |
| 1997 | return 0; |
| 1998 | } |
| 1999 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2000 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2001 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2002 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2003 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2004 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2005 | return 1; |
| 2006 | } |
| 2007 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2008 | /** |
| 2009 | * snd_hda_codec_amp_update - update the AMP value |
| 2010 | * @codec: HD-audio codec |
| 2011 | * @nid: NID to read the AMP value |
| 2012 | * @ch: channel (left=0 or right=1) |
| 2013 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2014 | * @idx: the index value (only for input direction) |
| 2015 | * @mask: bit mask to set |
| 2016 | * @val: the bits value to set |
| 2017 | * |
| 2018 | * Update the AMP value with a bit mask. |
| 2019 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2020 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2021 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2022 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2024 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2026 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2028 | /** |
| 2029 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2030 | * @codec: HD-audio codec |
| 2031 | * @nid: NID to read the AMP value |
| 2032 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2033 | * @idx: the index value (only for input direction) |
| 2034 | * @mask: bit mask to set |
| 2035 | * @val: the bits value to set |
| 2036 | * |
| 2037 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2038 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2039 | */ |
| 2040 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2041 | int direction, int idx, int mask, int val) |
| 2042 | { |
| 2043 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2044 | |
| 2045 | if (snd_BUG_ON(mask & ~0xff)) |
| 2046 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2047 | for (ch = 0; ch < 2; ch++) |
| 2048 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2049 | idx, mask, val); |
| 2050 | return ret; |
| 2051 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2052 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2053 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2054 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2055 | * the first access. If the amp was already initialized / updated beforehand, |
| 2056 | * this does nothing. |
| 2057 | */ |
| 2058 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2059 | int dir, int idx, int mask, int val) |
| 2060 | { |
| 2061 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2062 | } |
| 2063 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); |
| 2064 | |
| 2065 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2066 | int dir, int idx, int mask, int val) |
| 2067 | { |
| 2068 | int ch, ret = 0; |
| 2069 | |
| 2070 | if (snd_BUG_ON(mask & ~0xff)) |
| 2071 | mask &= 0xff; |
| 2072 | for (ch = 0; ch < 2; ch++) |
| 2073 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2074 | idx, mask, val); |
| 2075 | return ret; |
| 2076 | } |
| 2077 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); |
| 2078 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2079 | /** |
| 2080 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2081 | * @codec: HD-audio codec |
| 2082 | * |
| 2083 | * Resume the all amp commands from the cache. |
| 2084 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2085 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2086 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2087 | int i; |
| 2088 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2089 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2090 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2091 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2092 | struct hda_amp_info *buffer; |
| 2093 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2094 | hda_nid_t nid; |
| 2095 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2096 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2097 | |
| 2098 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2099 | if (!buffer->head.dirty) |
| 2100 | continue; |
| 2101 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2102 | info = *buffer; |
| 2103 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2104 | if (!key) |
| 2105 | continue; |
| 2106 | nid = key & 0xff; |
| 2107 | idx = (key >> 16) & 0xff; |
| 2108 | dir = (key >> 24) & 0xff; |
| 2109 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2110 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2111 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2112 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2113 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2114 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2115 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2116 | } |
| 2117 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2118 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2119 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2120 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2122 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2123 | unsigned int ofs) |
| 2124 | { |
| 2125 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2126 | /* get num steps */ |
| 2127 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2128 | if (ofs < caps) |
| 2129 | caps -= ofs; |
| 2130 | return caps; |
| 2131 | } |
| 2132 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2133 | /** |
| 2134 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2135 | * |
| 2136 | * The control element is supposed to have the private_value field |
| 2137 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2138 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2139 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2140 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | { |
| 2142 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2143 | u16 nid = get_amp_nid(kcontrol); |
| 2144 | u8 chs = get_amp_channels(kcontrol); |
| 2145 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2146 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2148 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2149 | uinfo->count = chs == 3 ? 2 : 1; |
| 2150 | uinfo->value.integer.min = 0; |
| 2151 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2152 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2153 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2154 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2155 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | return -EINVAL; |
| 2157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2160 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2162 | |
| 2163 | static inline unsigned int |
| 2164 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2165 | int ch, int dir, int idx, unsigned int ofs) |
| 2166 | { |
| 2167 | unsigned int val; |
| 2168 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2169 | val &= HDA_AMP_VOLMASK; |
| 2170 | if (val >= ofs) |
| 2171 | val -= ofs; |
| 2172 | else |
| 2173 | val = 0; |
| 2174 | return val; |
| 2175 | } |
| 2176 | |
| 2177 | static inline int |
| 2178 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2179 | int ch, int dir, int idx, unsigned int ofs, |
| 2180 | unsigned int val) |
| 2181 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2182 | unsigned int maxval; |
| 2183 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2184 | if (val > 0) |
| 2185 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2186 | /* ofs = 0: raw max value */ |
| 2187 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2188 | if (val > maxval) |
| 2189 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2190 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2191 | HDA_AMP_VOLMASK, val); |
| 2192 | } |
| 2193 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2194 | /** |
| 2195 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2196 | * |
| 2197 | * The control element is supposed to have the private_value field |
| 2198 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2199 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2200 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2201 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | { |
| 2203 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2204 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2205 | int chs = get_amp_channels(kcontrol); |
| 2206 | int dir = get_amp_direction(kcontrol); |
| 2207 | int idx = get_amp_index(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 | long *valp = ucontrol->value.integer.value; |
| 2210 | |
| 2211 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2212 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2214 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | return 0; |
| 2216 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2217 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2219 | /** |
| 2220 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2221 | * |
| 2222 | * The control element is supposed to have the private_value field |
| 2223 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2224 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2225 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2226 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | { |
| 2228 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2229 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2230 | int chs = get_amp_channels(kcontrol); |
| 2231 | int dir = get_amp_direction(kcontrol); |
| 2232 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2233 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | long *valp = ucontrol->value.integer.value; |
| 2235 | int change = 0; |
| 2236 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2237 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2238 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2239 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2240 | valp++; |
| 2241 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2242 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2243 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2244 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | return change; |
| 2246 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2247 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2249 | /** |
| 2250 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2251 | * |
| 2252 | * The control element is supposed to have the private_value field |
| 2253 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2254 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2255 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2256 | unsigned int size, unsigned int __user *_tlv) |
| 2257 | { |
| 2258 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2259 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2260 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2261 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2262 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2263 | u32 caps, val1, val2; |
| 2264 | |
| 2265 | if (size < 4 * sizeof(unsigned int)) |
| 2266 | return -ENOMEM; |
| 2267 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2268 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2269 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2270 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2271 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2272 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2273 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2274 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2275 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2276 | return -EFAULT; |
| 2277 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2278 | return -EFAULT; |
| 2279 | if (put_user(val1, _tlv + 2)) |
| 2280 | return -EFAULT; |
| 2281 | if (put_user(val2, _tlv + 3)) |
| 2282 | return -EFAULT; |
| 2283 | return 0; |
| 2284 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2285 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2286 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2287 | /** |
| 2288 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2289 | * @codec: HD-audio codec |
| 2290 | * @nid: NID of a reference widget |
| 2291 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2292 | * @tlv: TLV data to be stored, at least 4 elements |
| 2293 | * |
| 2294 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2295 | * obtained from the reference NID. |
| 2296 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2297 | */ |
| 2298 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2299 | unsigned int *tlv) |
| 2300 | { |
| 2301 | u32 caps; |
| 2302 | int nums, step; |
| 2303 | |
| 2304 | caps = query_amp_caps(codec, nid, dir); |
| 2305 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2306 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2307 | step = (step + 1) * 25; |
| 2308 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2309 | tlv[1] = 2 * sizeof(unsigned int); |
| 2310 | tlv[2] = -nums * step; |
| 2311 | tlv[3] = step; |
| 2312 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2313 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2314 | |
| 2315 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2316 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2317 | 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] | 2318 | { |
| 2319 | struct snd_ctl_elem_id id; |
| 2320 | memset(&id, 0, sizeof(id)); |
| 2321 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2322 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2323 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2324 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2325 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2326 | strcpy(id.name, name); |
| 2327 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2328 | } |
| 2329 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2330 | /** |
| 2331 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2332 | * @codec: HD-audio codec |
| 2333 | * @name: ctl id name string |
| 2334 | * |
| 2335 | * Get the control element with the given id string and IFACE_MIXER. |
| 2336 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2337 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2338 | const char *name) |
| 2339 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2340 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2341 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2342 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2343 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2344 | 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] | 2345 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2346 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2347 | int i, idx; |
| 2348 | /* 16 ctlrs should be large enough */ |
| 2349 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2350 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2351 | return idx; |
| 2352 | } |
| 2353 | return -EBUSY; |
| 2354 | } |
| 2355 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2356 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2357 | * 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] | 2358 | * @codec: HD-audio codec |
| 2359 | * @nid: corresponding NID (optional) |
| 2360 | * @kctl: the control element to assign |
| 2361 | * |
| 2362 | * Add the given control element to an array inside the codec instance. |
| 2363 | * All control elements belonging to a codec are supposed to be added |
| 2364 | * by this function so that a proper clean-up works at the free or |
| 2365 | * reconfiguration time. |
| 2366 | * |
| 2367 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2368 | * The assignment is shown in the codec proc file. |
| 2369 | * |
| 2370 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2371 | * #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] | 2372 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2373 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2374 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2375 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2376 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2377 | { |
| 2378 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2379 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2380 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2381 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2382 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2383 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2384 | if (nid == 0) |
| 2385 | nid = get_amp_nid_(kctl->private_value); |
| 2386 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2387 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2388 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2389 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2390 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2391 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2392 | if (err < 0) |
| 2393 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2394 | item = snd_array_new(&codec->mixers); |
| 2395 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2396 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2397 | item->kctl = kctl; |
| 2398 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2399 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2400 | return 0; |
| 2401 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2402 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2403 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2404 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2405 | * snd_hda_add_nid - Assign a NID to a control element |
| 2406 | * @codec: HD-audio codec |
| 2407 | * @nid: corresponding NID (optional) |
| 2408 | * @kctl: the control element to assign |
| 2409 | * @index: index to kctl |
| 2410 | * |
| 2411 | * Add the given control element to an array inside the codec instance. |
| 2412 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2413 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2414 | */ |
| 2415 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2416 | unsigned int index, hda_nid_t nid) |
| 2417 | { |
| 2418 | struct hda_nid_item *item; |
| 2419 | |
| 2420 | if (nid > 0) { |
| 2421 | item = snd_array_new(&codec->nids); |
| 2422 | if (!item) |
| 2423 | return -ENOMEM; |
| 2424 | item->kctl = kctl; |
| 2425 | item->index = index; |
| 2426 | item->nid = nid; |
| 2427 | return 0; |
| 2428 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2429 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2430 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2431 | return -EINVAL; |
| 2432 | } |
| 2433 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2434 | |
| 2435 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2436 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2437 | * @codec: HD-audio codec |
| 2438 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2439 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2440 | { |
| 2441 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2442 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2443 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2444 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2445 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2446 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2447 | } |
| 2448 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2449 | /* pseudo device locking |
| 2450 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2451 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2452 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2453 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2454 | struct snd_card *card = bus->card; |
| 2455 | struct hda_codec *codec; |
| 2456 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2457 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2458 | if (card->shutdown) |
| 2459 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2460 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2461 | if (!list_empty(&card->ctl_files)) |
| 2462 | goto err_clear; |
| 2463 | |
| 2464 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2465 | int pcm; |
| 2466 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2467 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2468 | if (!cpcm->pcm) |
| 2469 | continue; |
| 2470 | if (cpcm->pcm->streams[0].substream_opened || |
| 2471 | cpcm->pcm->streams[1].substream_opened) |
| 2472 | goto err_clear; |
| 2473 | } |
| 2474 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2475 | spin_unlock(&card->files_lock); |
| 2476 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2477 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2478 | err_clear: |
| 2479 | card->shutdown = 0; |
| 2480 | err_unlock: |
| 2481 | spin_unlock(&card->files_lock); |
| 2482 | return -EINVAL; |
| 2483 | } |
| 2484 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); |
| 2485 | |
| 2486 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2487 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2488 | struct snd_card *card = bus->card; |
| 2489 | |
| 2490 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2491 | spin_lock(&card->files_lock); |
| 2492 | card->shutdown = 0; |
| 2493 | spin_unlock(&card->files_lock); |
| 2494 | } |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2495 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2496 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2497 | /** |
| 2498 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2499 | * @codec: HD-audio codec |
| 2500 | * |
| 2501 | * This frees the all PCM and control elements assigned to the codec, and |
| 2502 | * clears the caches and restores the pin default configurations. |
| 2503 | * |
| 2504 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2505 | * returns zero. |
| 2506 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2507 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2508 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2509 | struct hda_bus *bus = codec->bus; |
| 2510 | struct snd_card *card = bus->card; |
| 2511 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2512 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2513 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2514 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2515 | |
| 2516 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2517 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2518 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2519 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | 339876d | 2012-05-08 16:57:12 +0200 | [diff] [blame] | 2520 | codec->power_on = 0; |
| 2521 | codec->power_transition = 0; |
| 2522 | codec->power_jiffies = jiffies; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2523 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2524 | #endif |
| 2525 | snd_hda_ctls_clear(codec); |
| 2526 | /* relase PCMs */ |
| 2527 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2528 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2529 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2530 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2531 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2532 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2533 | } |
| 2534 | if (codec->patch_ops.free) |
| 2535 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2536 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2537 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2538 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2539 | codec->spec = NULL; |
| 2540 | free_hda_cache(&codec->amp_cache); |
| 2541 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2542 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2543 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2544 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2545 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2546 | snd_array_free(&codec->cvt_setups); |
| 2547 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2548 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2549 | codec->num_pcms = 0; |
| 2550 | codec->pcm_info = NULL; |
| 2551 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2552 | codec->slave_dig_outs = NULL; |
| 2553 | codec->spdif_status_reset = 0; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2554 | module_put(codec->owner); |
| 2555 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2556 | |
| 2557 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2558 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2559 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2562 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2563 | |
| 2564 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2565 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2566 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2567 | { |
| 2568 | struct hda_nid_item *items; |
| 2569 | const char * const *s; |
| 2570 | int i, err; |
| 2571 | |
| 2572 | items = codec->mixers.list; |
| 2573 | for (i = 0; i < codec->mixers.used; i++) { |
| 2574 | struct snd_kcontrol *sctl = items[i].kctl; |
| 2575 | if (!sctl || !sctl->id.name || |
| 2576 | sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
| 2577 | continue; |
| 2578 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2579 | char tmpname[sizeof(sctl->id.name)]; |
| 2580 | const char *name = *s; |
| 2581 | if (suffix) { |
| 2582 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2583 | name, suffix); |
| 2584 | name = tmpname; |
| 2585 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2586 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2587 | err = func(data, sctl); |
| 2588 | if (err) |
| 2589 | return err; |
| 2590 | break; |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
| 2597 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2598 | { |
| 2599 | return 1; |
| 2600 | } |
| 2601 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2602 | /* guess the value corresponding to 0dB */ |
| 2603 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl) |
| 2604 | { |
| 2605 | int _tlv[4]; |
| 2606 | const int *tlv = NULL; |
| 2607 | int val = -1; |
| 2608 | |
| 2609 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2610 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2611 | mm_segment_t fs = get_fs(); |
| 2612 | set_fs(get_ds()); |
| 2613 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2614 | tlv = _tlv; |
| 2615 | set_fs(fs); |
| 2616 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2617 | tlv = kctl->tlv.p; |
| 2618 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) |
| 2619 | val = -tlv[2] / tlv[3]; |
| 2620 | return val; |
| 2621 | } |
| 2622 | |
| 2623 | /* call kctl->put with the given value(s) */ |
| 2624 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2625 | { |
| 2626 | struct snd_ctl_elem_value *ucontrol; |
| 2627 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2628 | if (!ucontrol) |
| 2629 | return -ENOMEM; |
| 2630 | ucontrol->value.integer.value[0] = val; |
| 2631 | ucontrol->value.integer.value[1] = val; |
| 2632 | kctl->put(kctl, ucontrol); |
| 2633 | kfree(ucontrol); |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
| 2637 | /* initialize the slave volume with 0dB */ |
| 2638 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2639 | { |
| 2640 | int offset = get_kctl_0dB_offset(slave); |
| 2641 | if (offset > 0) |
| 2642 | put_kctl_with_value(slave, offset); |
| 2643 | return 0; |
| 2644 | } |
| 2645 | |
| 2646 | /* unmute the slave */ |
| 2647 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2648 | { |
| 2649 | return put_kctl_with_value(slave, 1); |
| 2650 | } |
| 2651 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2652 | /** |
| 2653 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2654 | * @codec: HD-audio codec |
| 2655 | * @name: vmaster control name |
| 2656 | * @tlv: TLV data (optional) |
| 2657 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2658 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2659 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2660 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2661 | * |
| 2662 | * Create a virtual master control with the given name. The TLV data |
| 2663 | * must be either NULL or a valid data. |
| 2664 | * |
| 2665 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2666 | * slave control name. All controls with these names are assigned to |
| 2667 | * the new virtual master control. |
| 2668 | * |
| 2669 | * This function returns zero if successful or a negative error code. |
| 2670 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2671 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2672 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2673 | const char *suffix, bool init_slave_vol, |
| 2674 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2675 | { |
| 2676 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2677 | int err; |
| 2678 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2679 | if (ctl_ret) |
| 2680 | *ctl_ret = NULL; |
| 2681 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2682 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2683 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2684 | snd_printdd("No slave found for %s\n", name); |
| 2685 | return 0; |
| 2686 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2687 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2688 | if (!kctl) |
| 2689 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2690 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2691 | if (err < 0) |
| 2692 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2693 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2694 | err = map_slaves(codec, slaves, suffix, |
| 2695 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2696 | if (err < 0) |
| 2697 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2698 | |
| 2699 | /* init with master mute & zero volume */ |
| 2700 | put_kctl_with_value(kctl, 0); |
| 2701 | if (init_slave_vol) |
| 2702 | map_slaves(codec, slaves, suffix, |
| 2703 | tlv ? init_slave_0dB : init_slave_unmute, kctl); |
| 2704 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2705 | if (ctl_ret) |
| 2706 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2709 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2710 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2711 | /* |
| 2712 | * mute-LED control using vmaster |
| 2713 | */ |
| 2714 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2715 | struct snd_ctl_elem_info *uinfo) |
| 2716 | { |
| 2717 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2718 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2719 | }; |
| 2720 | unsigned int index; |
| 2721 | |
| 2722 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2723 | uinfo->count = 1; |
| 2724 | uinfo->value.enumerated.items = 3; |
| 2725 | index = uinfo->value.enumerated.item; |
| 2726 | if (index >= 3) |
| 2727 | index = 2; |
| 2728 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2729 | return 0; |
| 2730 | } |
| 2731 | |
| 2732 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2733 | struct snd_ctl_elem_value *ucontrol) |
| 2734 | { |
| 2735 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2736 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2737 | return 0; |
| 2738 | } |
| 2739 | |
| 2740 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2741 | struct snd_ctl_elem_value *ucontrol) |
| 2742 | { |
| 2743 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2744 | unsigned int old_mode = hook->mute_mode; |
| 2745 | |
| 2746 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2747 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2748 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2749 | if (old_mode == hook->mute_mode) |
| 2750 | return 0; |
| 2751 | snd_hda_sync_vmaster_hook(hook); |
| 2752 | return 1; |
| 2753 | } |
| 2754 | |
| 2755 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2756 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2757 | .name = "Mute-LED Mode", |
| 2758 | .info = vmaster_mute_mode_info, |
| 2759 | .get = vmaster_mute_mode_get, |
| 2760 | .put = vmaster_mute_mode_put, |
| 2761 | }; |
| 2762 | |
| 2763 | /* |
| 2764 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2765 | * "Mute-LED Mode" control is automatically created and associated with |
| 2766 | * the given hook. |
| 2767 | */ |
| 2768 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2769 | struct hda_vmaster_mute_hook *hook, |
| 2770 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2771 | { |
| 2772 | struct snd_kcontrol *kctl; |
| 2773 | |
| 2774 | if (!hook->hook || !hook->sw_kctl) |
| 2775 | return 0; |
| 2776 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2777 | hook->codec = codec; |
| 2778 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2779 | if (!expose_enum_ctl) |
| 2780 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2781 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2782 | if (!kctl) |
| 2783 | return -ENOMEM; |
| 2784 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2785 | } |
| 2786 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); |
| 2787 | |
| 2788 | /* |
| 2789 | * Call the hook with the current value for synchronization |
| 2790 | * Should be called in init callback |
| 2791 | */ |
| 2792 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2793 | { |
| 2794 | if (!hook->hook || !hook->codec) |
| 2795 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2796 | /* don't call vmaster hook in the destructor since it might have |
| 2797 | * been already destroyed |
| 2798 | */ |
| 2799 | if (hook->codec->bus->shutdown) |
| 2800 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2801 | switch (hook->mute_mode) { |
| 2802 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2803 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2804 | break; |
| 2805 | default: |
| 2806 | hook->hook(hook->codec, hook->mute_mode); |
| 2807 | break; |
| 2808 | } |
| 2809 | } |
| 2810 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); |
| 2811 | |
| 2812 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2813 | /** |
| 2814 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2815 | * |
| 2816 | * The control element is supposed to have the private_value field |
| 2817 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2818 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2819 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2820 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | { |
| 2822 | int chs = get_amp_channels(kcontrol); |
| 2823 | |
| 2824 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2825 | uinfo->count = chs == 3 ? 2 : 1; |
| 2826 | uinfo->value.integer.min = 0; |
| 2827 | uinfo->value.integer.max = 1; |
| 2828 | return 0; |
| 2829 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2830 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2832 | /** |
| 2833 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2834 | * |
| 2835 | * The control element is supposed to have the private_value field |
| 2836 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2837 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2838 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2839 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | { |
| 2841 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2842 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2843 | int chs = get_amp_channels(kcontrol); |
| 2844 | int dir = get_amp_direction(kcontrol); |
| 2845 | int idx = get_amp_index(kcontrol); |
| 2846 | long *valp = ucontrol->value.integer.value; |
| 2847 | |
| 2848 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2849 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2850 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2852 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2853 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | return 0; |
| 2855 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2856 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2858 | /** |
| 2859 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2860 | * |
| 2861 | * The control element is supposed to have the private_value field |
| 2862 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2863 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2864 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2865 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | { |
| 2867 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2868 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2869 | int chs = get_amp_channels(kcontrol); |
| 2870 | int dir = get_amp_direction(kcontrol); |
| 2871 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | long *valp = ucontrol->value.integer.value; |
| 2873 | int change = 0; |
| 2874 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2875 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2876 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2877 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2878 | HDA_AMP_MUTE, |
| 2879 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2880 | valp++; |
| 2881 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2882 | if (chs & 2) |
| 2883 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2884 | HDA_AMP_MUTE, |
| 2885 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2886 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2887 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | return change; |
| 2889 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2890 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | |
| 2892 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2893 | * bound volume controls |
| 2894 | * |
| 2895 | * bind multiple volumes (# indices, from 0) |
| 2896 | */ |
| 2897 | |
| 2898 | #define AMP_VAL_IDX_SHIFT 19 |
| 2899 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2900 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2901 | /** |
| 2902 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 2903 | * |
| 2904 | * The control element is supposed to have the private_value field |
| 2905 | * set up via HDA_BIND_MUTE*() macros. |
| 2906 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2907 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2908 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2909 | { |
| 2910 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2911 | unsigned long pval; |
| 2912 | int err; |
| 2913 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2914 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2915 | pval = kcontrol->private_value; |
| 2916 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2917 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2918 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2919 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2920 | return err; |
| 2921 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2922 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2923 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2924 | /** |
| 2925 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 2926 | * |
| 2927 | * The control element is supposed to have the private_value field |
| 2928 | * set up via HDA_BIND_MUTE*() macros. |
| 2929 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2930 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2931 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2932 | { |
| 2933 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2934 | unsigned long pval; |
| 2935 | int i, indices, err = 0, change = 0; |
| 2936 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2937 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2938 | pval = kcontrol->private_value; |
| 2939 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2940 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2941 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2942 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2943 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2944 | if (err < 0) |
| 2945 | break; |
| 2946 | change |= err; |
| 2947 | } |
| 2948 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2949 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2950 | return err < 0 ? err : change; |
| 2951 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2952 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2953 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2954 | /** |
| 2955 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 2956 | * |
| 2957 | * The control element is supposed to have the private_value field |
| 2958 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2959 | */ |
| 2960 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2961 | struct snd_ctl_elem_info *uinfo) |
| 2962 | { |
| 2963 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2964 | struct hda_bind_ctls *c; |
| 2965 | int err; |
| 2966 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2967 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2968 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2969 | kcontrol->private_value = *c->values; |
| 2970 | err = c->ops->info(kcontrol, uinfo); |
| 2971 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2972 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2973 | return err; |
| 2974 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2975 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2976 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2977 | /** |
| 2978 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 2979 | * |
| 2980 | * The control element is supposed to have the private_value field |
| 2981 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2982 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2983 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2984 | struct snd_ctl_elem_value *ucontrol) |
| 2985 | { |
| 2986 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2987 | struct hda_bind_ctls *c; |
| 2988 | int err; |
| 2989 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2990 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2991 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2992 | kcontrol->private_value = *c->values; |
| 2993 | err = c->ops->get(kcontrol, ucontrol); |
| 2994 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2995 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2996 | return err; |
| 2997 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2998 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2999 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3000 | /** |
| 3001 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3002 | * |
| 3003 | * The control element is supposed to have the private_value field |
| 3004 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3005 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3006 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3007 | struct snd_ctl_elem_value *ucontrol) |
| 3008 | { |
| 3009 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3010 | struct hda_bind_ctls *c; |
| 3011 | unsigned long *vals; |
| 3012 | int err = 0, change = 0; |
| 3013 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3014 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3015 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3016 | for (vals = c->values; *vals; vals++) { |
| 3017 | kcontrol->private_value = *vals; |
| 3018 | err = c->ops->put(kcontrol, ucontrol); |
| 3019 | if (err < 0) |
| 3020 | break; |
| 3021 | change |= err; |
| 3022 | } |
| 3023 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3024 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3025 | return err < 0 ? err : change; |
| 3026 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3027 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3028 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3029 | /** |
| 3030 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3031 | * |
| 3032 | * The control element is supposed to have the private_value field |
| 3033 | * set up via HDA_BIND_VOL() macro. |
| 3034 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3035 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3036 | unsigned int size, unsigned int __user *tlv) |
| 3037 | { |
| 3038 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3039 | struct hda_bind_ctls *c; |
| 3040 | int err; |
| 3041 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3042 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3043 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3044 | kcontrol->private_value = *c->values; |
| 3045 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3046 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3047 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3048 | return err; |
| 3049 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3050 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3051 | |
| 3052 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3053 | .info = snd_hda_mixer_amp_volume_info, |
| 3054 | .get = snd_hda_mixer_amp_volume_get, |
| 3055 | .put = snd_hda_mixer_amp_volume_put, |
| 3056 | .tlv = snd_hda_mixer_amp_tlv |
| 3057 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3058 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3059 | |
| 3060 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3061 | .info = snd_hda_mixer_amp_switch_info, |
| 3062 | .get = snd_hda_mixer_amp_switch_get, |
| 3063 | .put = snd_hda_mixer_amp_switch_put, |
| 3064 | .tlv = snd_hda_mixer_amp_tlv |
| 3065 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3066 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3067 | |
| 3068 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | * SPDIF out controls |
| 3070 | */ |
| 3071 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3072 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3073 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | { |
| 3075 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3076 | uinfo->count = 1; |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3080 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3081 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | { |
| 3083 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3084 | IEC958_AES0_NONAUDIO | |
| 3085 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3086 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3087 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3088 | IEC958_AES1_CON_ORIGINAL; |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3092 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3093 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | { |
| 3095 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3096 | IEC958_AES0_NONAUDIO | |
| 3097 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3101 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3102 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | { |
| 3104 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3105 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3106 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3108 | mutex_lock(&codec->spdif_mutex); |
| 3109 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3110 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3111 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3112 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3113 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3114 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
| 3119 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3120 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3121 | */ |
| 3122 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3123 | { |
| 3124 | unsigned short val = 0; |
| 3125 | |
| 3126 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3127 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3129 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3131 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3132 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3133 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3135 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3136 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3137 | val |= AC_DIG1_EMPHASIS; |
| 3138 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3139 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3141 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3143 | } |
| 3144 | return val; |
| 3145 | } |
| 3146 | |
| 3147 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3148 | */ |
| 3149 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3150 | { |
| 3151 | unsigned int sbits = 0; |
| 3152 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3153 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3155 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3157 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3158 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3160 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3161 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3163 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3165 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3167 | sbits |= val & (0x7f << 8); |
| 3168 | } |
| 3169 | return sbits; |
| 3170 | } |
| 3171 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3172 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3173 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3174 | int verb, int val) |
| 3175 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3176 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3177 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3178 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3179 | d = codec->slave_dig_outs; |
| 3180 | if (!d) |
| 3181 | return; |
| 3182 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3183 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3187 | int dig1, int dig2) |
| 3188 | { |
| 3189 | if (dig1 != -1) |
| 3190 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3191 | if (dig2 != -1) |
| 3192 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3193 | } |
| 3194 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3195 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3196 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | { |
| 3198 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3199 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3200 | struct hda_spdif_out *spdif; |
| 3201 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | unsigned short val; |
| 3203 | int change; |
| 3204 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3205 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3206 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3207 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3208 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3210 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3211 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3212 | val = convert_from_spdif_status(spdif->status); |
| 3213 | val |= spdif->ctls & 1; |
| 3214 | change = spdif->ctls != val; |
| 3215 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3216 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3217 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3218 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | return change; |
| 3220 | } |
| 3221 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3222 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3224 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3225 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | { |
| 3227 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3228 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3229 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3231 | mutex_lock(&codec->spdif_mutex); |
| 3232 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3233 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3234 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3238 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3239 | int dig1, int dig2) |
| 3240 | { |
| 3241 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3242 | /* unmute amp switch (if any) */ |
| 3243 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3244 | (dig1 & AC_DIG1_ENABLE)) |
| 3245 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3246 | HDA_AMP_MUTE, 0); |
| 3247 | } |
| 3248 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3249 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3250 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | { |
| 3252 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3253 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3254 | struct hda_spdif_out *spdif; |
| 3255 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | unsigned short val; |
| 3257 | int change; |
| 3258 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3259 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3260 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3261 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3262 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3264 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3265 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3266 | spdif->ctls = val; |
| 3267 | if (change && nid != (u16)-1) |
| 3268 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3269 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | return change; |
| 3271 | } |
| 3272 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3273 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | { |
| 3275 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3276 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3277 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | .info = snd_hda_spdif_mask_info, |
| 3279 | .get = snd_hda_spdif_cmask_get, |
| 3280 | }, |
| 3281 | { |
| 3282 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3283 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3284 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | .info = snd_hda_spdif_mask_info, |
| 3286 | .get = snd_hda_spdif_pmask_get, |
| 3287 | }, |
| 3288 | { |
| 3289 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3290 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | .info = snd_hda_spdif_mask_info, |
| 3292 | .get = snd_hda_spdif_default_get, |
| 3293 | .put = snd_hda_spdif_default_put, |
| 3294 | }, |
| 3295 | { |
| 3296 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3297 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | .info = snd_hda_spdif_out_switch_info, |
| 3299 | .get = snd_hda_spdif_out_switch_get, |
| 3300 | .put = snd_hda_spdif_out_switch_put, |
| 3301 | }, |
| 3302 | { } /* end */ |
| 3303 | }; |
| 3304 | |
| 3305 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3306 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3308 | * @associated_nid: NID that new ctls associated with |
| 3309 | * @cvt_nid: converter NID |
| 3310 | * @type: HDA_PCM_TYPE_* |
| 3311 | * Creates controls related with the digital output. |
| 3312 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | * |
| 3314 | * Returns 0 if successful, or a negative error code. |
| 3315 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3316 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3317 | hda_nid_t associated_nid, |
| 3318 | hda_nid_t cvt_nid, |
| 3319 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | { |
| 3321 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3322 | struct snd_kcontrol *kctl; |
| 3323 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3324 | int idx = 0; |
| 3325 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3326 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3327 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3329 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3330 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3331 | idx = spdif_index; |
| 3332 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3333 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3334 | /* suppose a single SPDIF device */ |
| 3335 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3336 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3337 | if (!kctl) |
| 3338 | break; |
| 3339 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3340 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3341 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3342 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3343 | if (!bus->primary_dig_out_type) |
| 3344 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3345 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3346 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3347 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3348 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3349 | return -EBUSY; |
| 3350 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3351 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3352 | if (!spdif) |
| 3353 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3355 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3356 | if (!kctl) |
| 3357 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3358 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3359 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3360 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3361 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | return err; |
| 3363 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3364 | spdif->nid = cvt_nid; |
| 3365 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3366 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3367 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | return 0; |
| 3369 | } |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3370 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3372 | /* get the hda_spdif_out entry from the given NID |
| 3373 | * call within spdif_mutex lock |
| 3374 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3375 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3376 | hda_nid_t nid) |
| 3377 | { |
| 3378 | int i; |
| 3379 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3380 | struct hda_spdif_out *spdif = |
| 3381 | snd_array_elem(&codec->spdif_out, i); |
| 3382 | if (spdif->nid == nid) |
| 3383 | return spdif; |
| 3384 | } |
| 3385 | return NULL; |
| 3386 | } |
| 3387 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3388 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3389 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3390 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3391 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3392 | |
| 3393 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3394 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3395 | spdif->nid = (u16)-1; |
| 3396 | mutex_unlock(&codec->spdif_mutex); |
| 3397 | } |
| 3398 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3399 | |
| 3400 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3401 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3402 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3403 | unsigned short val; |
| 3404 | |
| 3405 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3406 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3407 | if (spdif->nid != nid) { |
| 3408 | spdif->nid = nid; |
| 3409 | val = spdif->ctls; |
| 3410 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3411 | } |
| 3412 | mutex_unlock(&codec->spdif_mutex); |
| 3413 | } |
| 3414 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3417 | * SPDIF sharing with analog output |
| 3418 | */ |
| 3419 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3420 | struct snd_ctl_elem_value *ucontrol) |
| 3421 | { |
| 3422 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3423 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3424 | return 0; |
| 3425 | } |
| 3426 | |
| 3427 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3428 | struct snd_ctl_elem_value *ucontrol) |
| 3429 | { |
| 3430 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3431 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3436 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3437 | .name = "IEC958 Default PCM Playback Switch", |
| 3438 | .info = snd_ctl_boolean_mono_info, |
| 3439 | .get = spdif_share_sw_get, |
| 3440 | .put = spdif_share_sw_put, |
| 3441 | }; |
| 3442 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3443 | /** |
| 3444 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3445 | * @codec: the HDA codec |
| 3446 | * @mout: multi-out instance |
| 3447 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3448 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3449 | struct hda_multi_out *mout) |
| 3450 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3451 | struct snd_kcontrol *kctl; |
| 3452 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3453 | if (!mout->dig_out_nid) |
| 3454 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3455 | |
| 3456 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3457 | if (!kctl) |
| 3458 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3459 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3460 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3461 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3462 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | * SPDIF input |
| 3466 | */ |
| 3467 | |
| 3468 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3469 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3470 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3471 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | { |
| 3473 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3474 | |
| 3475 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3476 | return 0; |
| 3477 | } |
| 3478 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3479 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3480 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | { |
| 3482 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3483 | hda_nid_t nid = kcontrol->private_value; |
| 3484 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3485 | int change; |
| 3486 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3487 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3489 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3491 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3492 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3494 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | return change; |
| 3496 | } |
| 3497 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3498 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3499 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | { |
| 3501 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3502 | hda_nid_t nid = kcontrol->private_value; |
| 3503 | unsigned short val; |
| 3504 | unsigned int sbits; |
| 3505 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3506 | 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] | 3507 | sbits = convert_to_spdif_status(val); |
| 3508 | ucontrol->value.iec958.status[0] = sbits; |
| 3509 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3510 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3511 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3515 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | { |
| 3517 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3518 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | .info = snd_hda_spdif_in_switch_info, |
| 3520 | .get = snd_hda_spdif_in_switch_get, |
| 3521 | .put = snd_hda_spdif_in_switch_put, |
| 3522 | }, |
| 3523 | { |
| 3524 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3525 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3526 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | .info = snd_hda_spdif_mask_info, |
| 3528 | .get = snd_hda_spdif_in_status_get, |
| 3529 | }, |
| 3530 | { } /* end */ |
| 3531 | }; |
| 3532 | |
| 3533 | /** |
| 3534 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3535 | * @codec: the HDA codec |
| 3536 | * @nid: audio in widget NID |
| 3537 | * |
| 3538 | * Creates controls related with the SPDIF input. |
| 3539 | * Called from each patch supporting the SPDIF in. |
| 3540 | * |
| 3541 | * Returns 0 if successful, or a negative error code. |
| 3542 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3543 | 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] | 3544 | { |
| 3545 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3546 | struct snd_kcontrol *kctl; |
| 3547 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3548 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3550 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3551 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3552 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3553 | return -EBUSY; |
| 3554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3556 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3557 | if (!kctl) |
| 3558 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3560 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3561 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | return err; |
| 3563 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3564 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3565 | snd_hda_codec_read(codec, nid, 0, |
| 3566 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3567 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | return 0; |
| 3569 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3570 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3572 | /* |
| 3573 | * command cache |
| 3574 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3576 | /* 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] | 3577 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3578 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3579 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3580 | |
| 3581 | /** |
| 3582 | * snd_hda_codec_write_cache - send a single command with caching |
| 3583 | * @codec: the HDA codec |
| 3584 | * @nid: NID to send the command |
| 3585 | * @direct: direct flag |
| 3586 | * @verb: the verb to send |
| 3587 | * @parm: the parameter for the verb |
| 3588 | * |
| 3589 | * Send a single command without waiting for response. |
| 3590 | * |
| 3591 | * Returns 0 if successful, or a negative error code. |
| 3592 | */ |
| 3593 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3594 | int direct, unsigned int verb, unsigned int parm) |
| 3595 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3596 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3597 | struct hda_cache_head *c; |
| 3598 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3599 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3600 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3601 | cache_only = codec->cached_write; |
| 3602 | if (!cache_only) { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3603 | err = snd_hda_codec_write(codec, nid, direct, verb, parm); |
| 3604 | if (err < 0) |
| 3605 | return err; |
| 3606 | } |
| 3607 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3608 | /* parm may contain the verb stuff for get/set amp */ |
| 3609 | verb = verb | (parm >> 8); |
| 3610 | parm &= 0xff; |
| 3611 | key = build_cmd_cache_key(nid, verb); |
| 3612 | mutex_lock(&codec->bus->cmd_mutex); |
| 3613 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3614 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3615 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3616 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3617 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3618 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3619 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3620 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3621 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3622 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3623 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3624 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3625 | * @codec: the HDA codec |
| 3626 | * @nid: NID to send the command |
| 3627 | * @direct: direct flag |
| 3628 | * @verb: the verb to send |
| 3629 | * @parm: the parameter for the verb |
| 3630 | * |
| 3631 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3632 | * command if the parameter is already identical with the cached value. |
| 3633 | * If not, it sends the command and refreshes the cache. |
| 3634 | * |
| 3635 | * Returns 0 if successful, or a negative error code. |
| 3636 | */ |
| 3637 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
| 3638 | int direct, unsigned int verb, unsigned int parm) |
| 3639 | { |
| 3640 | struct hda_cache_head *c; |
| 3641 | u32 key; |
| 3642 | |
| 3643 | /* parm may contain the verb stuff for get/set amp */ |
| 3644 | verb = verb | (parm >> 8); |
| 3645 | parm &= 0xff; |
| 3646 | key = build_cmd_cache_key(nid, verb); |
| 3647 | mutex_lock(&codec->bus->cmd_mutex); |
| 3648 | c = get_hash(&codec->cmd_cache, key); |
| 3649 | if (c && c->val == parm) { |
| 3650 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3651 | return 0; |
| 3652 | } |
| 3653 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3654 | return snd_hda_codec_write_cache(codec, nid, direct, verb, parm); |
| 3655 | } |
| 3656 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3657 | |
| 3658 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3659 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3660 | * @codec: HD-audio codec |
| 3661 | * |
| 3662 | * Execute all verbs recorded in the command caches to resume. |
| 3663 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3664 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3665 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3666 | int i; |
| 3667 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3668 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3669 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3670 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3671 | struct hda_cache_head *buffer; |
| 3672 | u32 key; |
| 3673 | |
| 3674 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3675 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3676 | if (!key) |
| 3677 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3678 | if (!buffer->dirty) |
| 3679 | continue; |
| 3680 | buffer->dirty = 0; |
| 3681 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3682 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3683 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3684 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3685 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3686 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3687 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3688 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3689 | |
| 3690 | /** |
| 3691 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3692 | * @codec: the HDA codec |
| 3693 | * @seq: VERB array to send |
| 3694 | * |
| 3695 | * Send the commands sequentially from the given array. |
| 3696 | * Thte commands are recorded on cache for power-save and resume. |
| 3697 | * The array must be terminated with NID=0. |
| 3698 | */ |
| 3699 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3700 | const struct hda_verb *seq) |
| 3701 | { |
| 3702 | for (; seq->nid; seq++) |
| 3703 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3704 | seq->param); |
| 3705 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3706 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3707 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3708 | /** |
| 3709 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3710 | * @codec: HD-audio codec |
| 3711 | */ |
| 3712 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3713 | { |
| 3714 | snd_hda_codec_resume_amp(codec); |
| 3715 | snd_hda_codec_resume_cache(codec); |
| 3716 | } |
| 3717 | EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache); |
| 3718 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3719 | 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] | 3720 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3721 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3722 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3723 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3724 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3725 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3726 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3727 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3728 | if (!(wcaps & AC_WCAP_POWER)) |
| 3729 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3730 | if (codec->power_filter) { |
| 3731 | state = codec->power_filter(codec, nid, power_state); |
| 3732 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3733 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3734 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3735 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3736 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3737 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3738 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3739 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3740 | |
| 3741 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3742 | * supported power states check |
| 3743 | */ |
| 3744 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3745 | unsigned int power_state) |
| 3746 | { |
| 3747 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3748 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3749 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3750 | return false; |
| 3751 | if (sup & power_state) |
| 3752 | return true; |
| 3753 | else |
| 3754 | return false; |
| 3755 | } |
| 3756 | |
| 3757 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3758 | * wait until the state is reached, returns the current state |
| 3759 | */ |
| 3760 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3761 | hda_nid_t fg, |
| 3762 | unsigned int power_state) |
| 3763 | { |
| 3764 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3765 | unsigned int state, actual_state; |
| 3766 | |
| 3767 | for (;;) { |
| 3768 | state = snd_hda_codec_read(codec, fg, 0, |
| 3769 | AC_VERB_GET_POWER_STATE, 0); |
| 3770 | if (state & AC_PWRST_ERROR) |
| 3771 | break; |
| 3772 | actual_state = (state >> 4) & 0x0f; |
| 3773 | if (actual_state == power_state) |
| 3774 | break; |
| 3775 | if (time_after_eq(jiffies, end_time)) |
| 3776 | break; |
| 3777 | /* wait until the codec reachs to the target state */ |
| 3778 | msleep(1); |
| 3779 | } |
| 3780 | return state; |
| 3781 | } |
| 3782 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3783 | /* 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] | 3784 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3785 | hda_nid_t nid, |
| 3786 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3787 | { |
| 3788 | if (power_state == AC_PWRST_D3 && |
| 3789 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3790 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3791 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3792 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3793 | if (eapd & 0x02) |
| 3794 | return AC_PWRST_D0; |
| 3795 | } |
| 3796 | return power_state; |
| 3797 | } |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3798 | EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3799 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3800 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3801 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3802 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3803 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3804 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3805 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3806 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3807 | int count; |
| 3808 | unsigned int state; |
| 3809 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3810 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3811 | if (power_state == AC_PWRST_D3) { |
| 3812 | /* transition time less than 10ms for power down */ |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 3813 | msleep(codec->epss ? 10 : 100); |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3814 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3815 | |
| 3816 | /* repeat power states setting at most 10 times*/ |
| 3817 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3818 | if (codec->patch_ops.set_power_state) |
| 3819 | codec->patch_ops.set_power_state(codec, fg, |
| 3820 | power_state); |
| 3821 | else { |
| 3822 | snd_hda_codec_read(codec, fg, 0, |
| 3823 | AC_VERB_SET_POWER_STATE, |
| 3824 | power_state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3825 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3826 | } |
| 3827 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3828 | if (!(state & AC_PWRST_ERROR)) |
| 3829 | break; |
| 3830 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3831 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3832 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3833 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3834 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3835 | /* sync power states of all widgets; |
| 3836 | * this is called at the end of codec parsing |
| 3837 | */ |
| 3838 | static void sync_power_up_states(struct hda_codec *codec) |
| 3839 | { |
| 3840 | hda_nid_t nid = codec->start_nid; |
| 3841 | int i; |
| 3842 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3843 | /* don't care if no filter is used */ |
| 3844 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3845 | return; |
| 3846 | |
| 3847 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3848 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3849 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3850 | if (!(wcaps & AC_WCAP_POWER)) |
| 3851 | continue; |
| 3852 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3853 | if (target == AC_PWRST_D0) |
| 3854 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3855 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3856 | snd_hda_codec_write(codec, nid, 0, |
| 3857 | AC_VERB_SET_POWER_STATE, target); |
| 3858 | } |
| 3859 | } |
| 3860 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3861 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3862 | /* execute additional init verbs */ |
| 3863 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3864 | { |
| 3865 | if (codec->init_verbs.list) |
| 3866 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3867 | } |
| 3868 | #else |
| 3869 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3870 | #endif |
| 3871 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3872 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3873 | /* |
| 3874 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3875 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3876 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3877 | 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] | 3878 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3879 | unsigned int state; |
| 3880 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3881 | codec->in_pm = 1; |
| 3882 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3883 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3884 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3885 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3886 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 3887 | /* Cancel delayed work if we aren't currently running from it. */ |
| 3888 | if (!in_wq) |
| 3889 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3890 | spin_lock(&codec->power_lock); |
| 3891 | snd_hda_update_power_acct(codec); |
| 3892 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 3893 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 3894 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 3895 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3896 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3897 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3898 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3901 | /* mark all entries of cmd and amp caches dirty */ |
| 3902 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3903 | { |
| 3904 | int i; |
| 3905 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3906 | struct hda_cache_head *cmd; |
| 3907 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3908 | cmd->dirty = 1; |
| 3909 | } |
| 3910 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3911 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3912 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3913 | amp->head.dirty = 1; |
| 3914 | } |
| 3915 | } |
| 3916 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3917 | /* |
| 3918 | * kick up codec; used both from PM and power-save |
| 3919 | */ |
| 3920 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3921 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3922 | codec->in_pm = 1; |
| 3923 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3924 | hda_mark_cmd_cache_dirty(codec); |
| 3925 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3926 | /* set as if powered on for avoiding re-entering the resume |
| 3927 | * in the resume / power-save sequence |
| 3928 | */ |
| 3929 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3930 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3931 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3932 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3933 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3934 | if (codec->patch_ops.resume) |
| 3935 | codec->patch_ops.resume(codec); |
| 3936 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3937 | if (codec->patch_ops.init) |
| 3938 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3939 | snd_hda_codec_resume_amp(codec); |
| 3940 | snd_hda_codec_resume_cache(codec); |
| 3941 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3942 | |
| 3943 | if (codec->jackpoll_interval) |
| 3944 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3945 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3946 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3947 | |
| 3948 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 3949 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3950 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3951 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3952 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | /** |
| 3955 | * snd_hda_build_controls - build mixer controls |
| 3956 | * @bus: the BUS |
| 3957 | * |
| 3958 | * Creates mixer controls for each codec included in the bus. |
| 3959 | * |
| 3960 | * Returns 0 if successful, otherwise a negative error code. |
| 3961 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 3962 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3964 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3966 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3967 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3968 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 3969 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3970 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 3971 | err = snd_hda_codec_reset(codec); |
| 3972 | if (err < 0) { |
| 3973 | printk(KERN_ERR |
| 3974 | "hda_codec: cannot revert codec\n"); |
| 3975 | return err; |
| 3976 | } |
| 3977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3979 | return 0; |
| 3980 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3981 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3983 | /* |
| 3984 | * add standard channel maps if not specified |
| 3985 | */ |
| 3986 | static int add_std_chmaps(struct hda_codec *codec) |
| 3987 | { |
| 3988 | int i, str, err; |
| 3989 | |
| 3990 | for (i = 0; i < codec->num_pcms; i++) { |
| 3991 | for (str = 0; str < 2; str++) { |
| 3992 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 3993 | struct hda_pcm_stream *hinfo = |
| 3994 | &codec->pcm_info[i].stream[str]; |
| 3995 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3996 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3997 | |
| 3998 | if (codec->pcm_info[i].own_chmap) |
| 3999 | continue; |
| 4000 | if (!pcm || !hinfo->substreams) |
| 4001 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4002 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4003 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4004 | hinfo->channels_max, |
| 4005 | 0, &chmap); |
| 4006 | if (err < 0) |
| 4007 | return err; |
| 4008 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4009 | } |
| 4010 | } |
| 4011 | return 0; |
| 4012 | } |
| 4013 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4014 | /* default channel maps for 2.1 speakers; |
| 4015 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4016 | */ |
| 4017 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4018 | { .channels = 2, |
| 4019 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4020 | { .channels = 4, |
| 4021 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4022 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4023 | { } |
| 4024 | }; |
| 4025 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4026 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4027 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4028 | { |
| 4029 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4030 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4031 | /* continue to initialize... */ |
| 4032 | if (codec->patch_ops.init) |
| 4033 | err = codec->patch_ops.init(codec); |
| 4034 | if (!err && codec->patch_ops.build_controls) |
| 4035 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4036 | if (err < 0) |
| 4037 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4038 | |
| 4039 | /* we create chmaps here instead of build_pcms */ |
| 4040 | err = add_std_chmaps(codec); |
| 4041 | if (err < 0) |
| 4042 | return err; |
| 4043 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4044 | if (codec->jackpoll_interval) |
| 4045 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4046 | else |
| 4047 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4048 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | return 0; |
| 4050 | } |
| 4051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | /* |
| 4053 | * stream formats |
| 4054 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4055 | struct hda_rate_tbl { |
| 4056 | unsigned int hz; |
| 4057 | unsigned int alsa_bits; |
| 4058 | unsigned int hda_fmt; |
| 4059 | }; |
| 4060 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4061 | /* rate = base * mult / div */ |
| 4062 | #define HDA_RATE(base, mult, div) \ |
| 4063 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4064 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4065 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4066 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4068 | |
| 4069 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4070 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4071 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4072 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4073 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4074 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4075 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4076 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4077 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4078 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4079 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4080 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4081 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4082 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4083 | |
| 4084 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4085 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4086 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4087 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | }; |
| 4089 | |
| 4090 | /** |
| 4091 | * snd_hda_calc_stream_format - calculate format bitset |
| 4092 | * @rate: the sample rate |
| 4093 | * @channels: the number of channels |
| 4094 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4095 | * @maxbps: the max. bps |
| 4096 | * |
| 4097 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4098 | * |
| 4099 | * Return zero if invalid. |
| 4100 | */ |
| 4101 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4102 | unsigned int channels, |
| 4103 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4104 | unsigned int maxbps, |
| 4105 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | { |
| 4107 | int i; |
| 4108 | unsigned int val = 0; |
| 4109 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4110 | for (i = 0; rate_bits[i].hz; i++) |
| 4111 | if (rate_bits[i].hz == rate) { |
| 4112 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | break; |
| 4114 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4115 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | snd_printdd("invalid rate %d\n", rate); |
| 4117 | return 0; |
| 4118 | } |
| 4119 | |
| 4120 | if (channels == 0 || channels > 8) { |
| 4121 | snd_printdd("invalid channels %d\n", channels); |
| 4122 | return 0; |
| 4123 | } |
| 4124 | val |= channels - 1; |
| 4125 | |
| 4126 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4127 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4128 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4129 | break; |
| 4130 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4131 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4132 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | case 20: |
| 4134 | case 24: |
| 4135 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4136 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4137 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4139 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4141 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | break; |
| 4143 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4144 | snd_printdd("invalid format width %d\n", |
| 4145 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | return 0; |
| 4147 | } |
| 4148 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4149 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4150 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | return val; |
| 4153 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4154 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4156 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4157 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4158 | { |
| 4159 | unsigned int val = 0; |
| 4160 | if (nid != codec->afg && |
| 4161 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4162 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4163 | if (!val || val == -1) |
| 4164 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4165 | if (!val || val == -1) |
| 4166 | return 0; |
| 4167 | return val; |
| 4168 | } |
| 4169 | |
| 4170 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4171 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4172 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4173 | get_pcm_param); |
| 4174 | } |
| 4175 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4176 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4177 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4178 | { |
| 4179 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4180 | if (!streams || streams == -1) |
| 4181 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4182 | if (!streams || streams == -1) |
| 4183 | return 0; |
| 4184 | return streams; |
| 4185 | } |
| 4186 | |
| 4187 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4188 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4189 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4190 | get_stream_param); |
| 4191 | } |
| 4192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | /** |
| 4194 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4195 | * @codec: the HDA codec |
| 4196 | * @nid: NID to query |
| 4197 | * @ratesp: the pointer to store the detected rate bitflags |
| 4198 | * @formatsp: the pointer to store the detected formats |
| 4199 | * @bpsp: the pointer to store the detected format widths |
| 4200 | * |
| 4201 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4202 | * or @bsps argument is ignored. |
| 4203 | * |
| 4204 | * Returns 0 if successful, otherwise a negative error code. |
| 4205 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4206 | 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] | 4207 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4208 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4209 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4211 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4212 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
| 4214 | if (ratesp) { |
| 4215 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4216 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4218 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4220 | if (rates == 0) { |
| 4221 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4222 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4223 | nid, val, |
| 4224 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4225 | return -EIO; |
| 4226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | *ratesp = rates; |
| 4228 | } |
| 4229 | |
| 4230 | if (formatsp || bpsp) { |
| 4231 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4232 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4234 | streams = query_stream_param(codec, nid); |
| 4235 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | |
| 4238 | bps = 0; |
| 4239 | if (streams & AC_SUPFMT_PCM) { |
| 4240 | if (val & AC_SUPPCM_BITS_8) { |
| 4241 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4242 | bps = 8; |
| 4243 | } |
| 4244 | if (val & AC_SUPPCM_BITS_16) { |
| 4245 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4246 | bps = 16; |
| 4247 | } |
| 4248 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4249 | if (val & AC_SUPPCM_BITS_32) |
| 4250 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4251 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4252 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4253 | if (val & AC_SUPPCM_BITS_24) |
| 4254 | bps = 24; |
| 4255 | else if (val & AC_SUPPCM_BITS_20) |
| 4256 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4257 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4258 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4260 | if (val & AC_SUPPCM_BITS_32) |
| 4261 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | else if (val & AC_SUPPCM_BITS_24) |
| 4263 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4264 | else if (val & AC_SUPPCM_BITS_20) |
| 4265 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | } |
| 4267 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4268 | #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] | 4269 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4271 | if (!bps) |
| 4272 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4273 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4274 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4275 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4276 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | /* temporary hack: we have still no proper support |
| 4278 | * for the direct AC3 stream... |
| 4279 | */ |
| 4280 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4281 | bps = 8; |
| 4282 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4283 | if (formats == 0) { |
| 4284 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4285 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4286 | "streams=0x%x)\n", |
| 4287 | nid, val, |
| 4288 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4289 | streams); |
| 4290 | return -EIO; |
| 4291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | if (formatsp) |
| 4293 | *formatsp = formats; |
| 4294 | if (bpsp) |
| 4295 | *bpsp = bps; |
| 4296 | } |
| 4297 | |
| 4298 | return 0; |
| 4299 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4300 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | |
| 4302 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4303 | * snd_hda_is_supported_format - Check the validity of the format |
| 4304 | * @codec: HD-audio codec |
| 4305 | * @nid: NID to check |
| 4306 | * @format: the HD-audio format value to check |
| 4307 | * |
| 4308 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | * |
| 4310 | * Returns 1 if supported, 0 if not. |
| 4311 | */ |
| 4312 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4313 | unsigned int format) |
| 4314 | { |
| 4315 | int i; |
| 4316 | unsigned int val = 0, rate, stream; |
| 4317 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4318 | val = query_pcm_param(codec, nid); |
| 4319 | if (!val) |
| 4320 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | |
| 4322 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4323 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4324 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | if (val & (1 << i)) |
| 4326 | break; |
| 4327 | return 0; |
| 4328 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4329 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | return 0; |
| 4331 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4332 | stream = query_stream_param(codec, nid); |
| 4333 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | return 0; |
| 4335 | |
| 4336 | if (stream & AC_SUPFMT_PCM) { |
| 4337 | switch (format & 0xf0) { |
| 4338 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4339 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | return 0; |
| 4341 | break; |
| 4342 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4343 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | return 0; |
| 4345 | break; |
| 4346 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4347 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | return 0; |
| 4349 | break; |
| 4350 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4351 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | return 0; |
| 4353 | break; |
| 4354 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4355 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | return 0; |
| 4357 | break; |
| 4358 | default: |
| 4359 | return 0; |
| 4360 | } |
| 4361 | } else { |
| 4362 | /* FIXME: check for float32 and AC3? */ |
| 4363 | } |
| 4364 | |
| 4365 | return 1; |
| 4366 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4367 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | |
| 4369 | /* |
| 4370 | * PCM stuff |
| 4371 | */ |
| 4372 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4373 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4374 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | { |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
| 4379 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4380 | struct hda_codec *codec, |
| 4381 | unsigned int stream_tag, |
| 4382 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4383 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | { |
| 4385 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4386 | return 0; |
| 4387 | } |
| 4388 | |
| 4389 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4390 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4391 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4393 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | return 0; |
| 4395 | } |
| 4396 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4397 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4398 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4400 | int err; |
| 4401 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4402 | /* query support PCM information from the given NID */ |
| 4403 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4404 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4405 | info->rates ? NULL : &info->rates, |
| 4406 | info->formats ? NULL : &info->formats, |
| 4407 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4408 | if (err < 0) |
| 4409 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | } |
| 4411 | if (info->ops.open == NULL) |
| 4412 | info->ops.open = hda_pcm_default_open_close; |
| 4413 | if (info->ops.close == NULL) |
| 4414 | info->ops.close = hda_pcm_default_open_close; |
| 4415 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4416 | if (snd_BUG_ON(!info->nid)) |
| 4417 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | info->ops.prepare = hda_pcm_default_prepare; |
| 4419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4421 | if (snd_BUG_ON(!info->nid)) |
| 4422 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4424 | } |
| 4425 | return 0; |
| 4426 | } |
| 4427 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4428 | /* |
| 4429 | * codec prepare/cleanup entries |
| 4430 | */ |
| 4431 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4432 | struct hda_pcm_stream *hinfo, |
| 4433 | unsigned int stream, |
| 4434 | unsigned int format, |
| 4435 | struct snd_pcm_substream *substream) |
| 4436 | { |
| 4437 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4438 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4439 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4440 | if (ret >= 0) |
| 4441 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4442 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4443 | return ret; |
| 4444 | } |
| 4445 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 4446 | |
| 4447 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4448 | struct hda_pcm_stream *hinfo, |
| 4449 | struct snd_pcm_substream *substream) |
| 4450 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4451 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4452 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4453 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4454 | } |
| 4455 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 4456 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4457 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4458 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4459 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4460 | }; |
| 4461 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4462 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4463 | * get the empty PCM device number to assign |
Takashi Iwai | c893622 | 2010-01-28 17:08:53 +0100 | [diff] [blame] | 4464 | * |
| 4465 | * note the max device number is limited by HDA_MAX_PCMS, currently 10 |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4466 | */ |
| 4467 | static int get_empty_pcm_device(struct hda_bus *bus, int type) |
| 4468 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4469 | /* audio device indices; not linear to keep compatibility */ |
| 4470 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4471 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4472 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4473 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4474 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4475 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4476 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4477 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4478 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4479 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4480 | return -EINVAL; |
| 4481 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4482 | |
| 4483 | for (i = 0; audio_idx[type][i] >= 0 ; i++) |
| 4484 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4485 | return audio_idx[type][i]; |
| 4486 | |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4487 | /* non-fixed slots starting from 10 */ |
| 4488 | for (i = 10; i < 32; i++) { |
| 4489 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4490 | return i; |
| 4491 | } |
| 4492 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4493 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4494 | snd_hda_pcm_type_name[type]); |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4495 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4499 | * attach a new PCM stream |
| 4500 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4501 | 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] | 4502 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4503 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4504 | struct hda_pcm_stream *info; |
| 4505 | int stream, err; |
| 4506 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4507 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4508 | return -EINVAL; |
| 4509 | for (stream = 0; stream < 2; stream++) { |
| 4510 | info = &pcm->stream[stream]; |
| 4511 | if (info->substreams) { |
| 4512 | err = set_pcm_default_values(codec, info); |
| 4513 | if (err < 0) |
| 4514 | return err; |
| 4515 | } |
| 4516 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4517 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4518 | } |
| 4519 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4520 | /* assign all PCMs of the given codec */ |
| 4521 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4522 | { |
| 4523 | unsigned int pcm; |
| 4524 | int err; |
| 4525 | |
| 4526 | if (!codec->num_pcms) { |
| 4527 | if (!codec->patch_ops.build_pcms) |
| 4528 | return 0; |
| 4529 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4530 | if (err < 0) { |
| 4531 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4532 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4533 | err = snd_hda_codec_reset(codec); |
| 4534 | if (err < 0) { |
| 4535 | printk(KERN_ERR |
| 4536 | "hda_codec: cannot revert codec\n"); |
| 4537 | return err; |
| 4538 | } |
| 4539 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4540 | } |
| 4541 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4542 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4543 | int dev; |
| 4544 | |
| 4545 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4546 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4547 | |
| 4548 | if (!cpcm->pcm) { |
| 4549 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4550 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4551 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4552 | cpcm->device = dev; |
| 4553 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4554 | if (err < 0) { |
| 4555 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4556 | "PCM stream %d for codec #%d\n", |
| 4557 | dev, codec->addr); |
| 4558 | continue; /* no fatal error */ |
| 4559 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | return 0; |
| 4563 | } |
| 4564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | /** |
| 4566 | * snd_hda_build_pcms - build PCM information |
| 4567 | * @bus: the BUS |
| 4568 | * |
| 4569 | * Create PCM information for each codec included in the bus. |
| 4570 | * |
| 4571 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4572 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4573 | * to create its PCM instances. |
| 4574 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4575 | * callback. |
| 4576 | * |
| 4577 | * At least, substreams, channels_min and channels_max must be filled for |
| 4578 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4579 | * When rates and/or formats are zero, the supported values are queried |
| 4580 | * from the given nid. The nid is used also by the default ops.prepare |
| 4581 | * and ops.cleanup callbacks. |
| 4582 | * |
| 4583 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4584 | * ops.close is supposed to be called in the close callback. |
| 4585 | * ops.prepare should be called in the prepare or hw_params callback |
| 4586 | * with the proper parameters for set up. |
| 4587 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4588 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4589 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4591 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4593 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4595 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4596 | int err = snd_hda_codec_build_pcms(codec); |
| 4597 | if (err < 0) |
| 4598 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | } |
| 4600 | return 0; |
| 4601 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4602 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | /** |
| 4605 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4606 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4607 | * @num_configs: number of config enums |
| 4608 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | * @tbl: configuration table, terminated by null entries |
| 4610 | * |
| 4611 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4612 | * given configuration table. If a matching entry is found, returns its |
| 4613 | * config value (supposed to be 0 or positive). |
| 4614 | * |
| 4615 | * If no entries are matching, the function returns a negative value. |
| 4616 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4617 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4618 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4619 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4621 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4622 | int i; |
| 4623 | for (i = 0; i < num_configs; i++) { |
| 4624 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4625 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4626 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4627 | "selected\n", models[i]); |
| 4628 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | } |
| 4630 | } |
| 4631 | } |
| 4632 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4633 | if (!codec->bus->pci || !tbl) |
| 4634 | return -1; |
| 4635 | |
| 4636 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4637 | if (!tbl) |
| 4638 | return -1; |
| 4639 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4640 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4641 | char tmp[10]; |
| 4642 | const char *model = NULL; |
| 4643 | if (models) |
| 4644 | model = models[tbl->value]; |
| 4645 | if (!model) { |
| 4646 | sprintf(tmp, "#%d", tbl->value); |
| 4647 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4649 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4650 | "for config %x:%x (%s)\n", |
| 4651 | model, tbl->subvendor, tbl->subdevice, |
| 4652 | (tbl->name ? tbl->name : "Unknown device")); |
| 4653 | #endif |
| 4654 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | } |
| 4656 | return -1; |
| 4657 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4658 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | |
| 4660 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4661 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4662 | subsystem ID with the |
| 4663 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4664 | |
| 4665 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4666 | where the vendor ID of the PCI device is: |
| 4667 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4668 | and the vendor/subvendor are found only at the codec. |
| 4669 | |
| 4670 | * @codec: the HDA codec |
| 4671 | * @num_configs: number of config enums |
| 4672 | * @models: array of model name strings |
| 4673 | * @tbl: configuration table, terminated by null entries |
| 4674 | * |
| 4675 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4676 | * given configuration table. If a matching entry is found, returns its |
| 4677 | * config value (supposed to be 0 or positive). |
| 4678 | * |
| 4679 | * If no entries are matching, the function returns a negative value. |
| 4680 | */ |
| 4681 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4682 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4683 | const struct snd_pci_quirk *tbl) |
| 4684 | { |
| 4685 | const struct snd_pci_quirk *q; |
| 4686 | |
| 4687 | /* Search for codec ID */ |
| 4688 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4689 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4690 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4691 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4692 | break; |
| 4693 | } |
| 4694 | |
| 4695 | if (!q->subvendor) |
| 4696 | return -1; |
| 4697 | |
| 4698 | tbl = q; |
| 4699 | |
| 4700 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4701 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4702 | char tmp[10]; |
| 4703 | const char *model = NULL; |
| 4704 | if (models) |
| 4705 | model = models[tbl->value]; |
| 4706 | if (!model) { |
| 4707 | sprintf(tmp, "#%d", tbl->value); |
| 4708 | model = tmp; |
| 4709 | } |
| 4710 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4711 | "for config %x:%x (%s)\n", |
| 4712 | model, tbl->subvendor, tbl->subdevice, |
| 4713 | (tbl->name ? tbl->name : "Unknown device")); |
| 4714 | #endif |
| 4715 | return tbl->value; |
| 4716 | } |
| 4717 | return -1; |
| 4718 | } |
| 4719 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4720 | |
| 4721 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | * snd_hda_add_new_ctls - create controls from the array |
| 4723 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4724 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | * |
| 4726 | * This helper function creates and add new controls in the given array. |
| 4727 | * The array must be terminated with an empty entry as terminator. |
| 4728 | * |
| 4729 | * Returns 0 if successful, or a negative error code. |
| 4730 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4731 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4732 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4734 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | |
| 4736 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4737 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4738 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4739 | if (knew->iface == -1) /* skip this codec private value */ |
| 4740 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4741 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4742 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4743 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4744 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4745 | if (addr > 0) |
| 4746 | kctl->id.device = addr; |
| 4747 | if (idx > 0) |
| 4748 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4749 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4750 | if (!err) |
| 4751 | break; |
| 4752 | /* try first with another device index corresponding to |
| 4753 | * the codec addr; if it still fails (or it's the |
| 4754 | * primary codec), then try another control index |
| 4755 | */ |
| 4756 | if (!addr && codec->addr) |
| 4757 | addr = codec->addr; |
| 4758 | else if (!idx && !knew->index) { |
| 4759 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4760 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4761 | if (idx <= 0) |
| 4762 | return err; |
| 4763 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4764 | return err; |
| 4765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | } |
| 4767 | return 0; |
| 4768 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4769 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4771 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4772 | static void hda_power_work(struct work_struct *work) |
| 4773 | { |
| 4774 | struct hda_codec *codec = |
| 4775 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4776 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4777 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4778 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4779 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4780 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4781 | spin_unlock(&codec->power_lock); |
| 4782 | return; |
| 4783 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4784 | if (!codec->power_on || codec->power_count) { |
| 4785 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4786 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4787 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4788 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4789 | spin_unlock(&codec->power_lock); |
| 4790 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4791 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4792 | codec->pm_down_notified = 0; |
| 4793 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) { |
| 4794 | codec->pm_down_notified = 1; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4795 | hda_call_pm_notify(bus, false); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4796 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4800 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4801 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4802 | codec->power_count++; |
| 4803 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4804 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4805 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4806 | } |
| 4807 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4808 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4809 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4810 | { |
| 4811 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4812 | if (codec->power_on) |
| 4813 | codec->power_on_acct += delta; |
| 4814 | else |
| 4815 | codec->power_off_acct += delta; |
| 4816 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4817 | } |
| 4818 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4819 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4820 | * transition to D3 to complete. A pending D3 transition is indicated |
| 4821 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4822 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4823 | 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] | 4824 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4825 | struct hda_bus *bus = codec->bus; |
| 4826 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4827 | /* Return if power_on or transitioning to power_on, unless currently |
| 4828 | * powering down. */ |
| 4829 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4830 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4831 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4832 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4833 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4834 | cancel_delayed_work_sync(&codec->power_work); |
| 4835 | |
| 4836 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4837 | /* If the power down delayed work was cancelled above before starting, |
| 4838 | * then there is no need to go through power up here. |
| 4839 | */ |
| 4840 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 4841 | if (codec->power_transition < 0) |
| 4842 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4843 | return; |
| 4844 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4845 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4846 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4847 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4848 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4849 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4850 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4851 | spin_unlock(&codec->power_lock); |
| 4852 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4853 | if (codec->pm_down_notified) { |
| 4854 | codec->pm_down_notified = 0; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 4855 | hda_call_pm_notify(bus, true); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4858 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4859 | |
| 4860 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4861 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4862 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4863 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 4864 | #define power_save(codec) \ |
| 4865 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4866 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4867 | /* Transition to powered down */ |
| 4868 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4869 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4870 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4871 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4872 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4873 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4874 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 4875 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 4876 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4877 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | /** |
| 4881 | * snd_hda_power_save - Power-up/down/sync the codec |
| 4882 | * @codec: HD-audio codec |
| 4883 | * @delta: the counter delta to change |
| 4884 | * |
| 4885 | * Change the power-up counter via @delta, and power up or down the hardware |
| 4886 | * appropriately. For the power-down, queue to the delayed action. |
| 4887 | * Passing zero to @delta means to synchronize the power state. |
| 4888 | */ |
| 4889 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 4890 | { |
| 4891 | spin_lock(&codec->power_lock); |
| 4892 | codec->power_count += delta; |
| 4893 | trace_hda_power_count(codec); |
| 4894 | if (delta > 0) |
| 4895 | __snd_hda_power_up(codec, d3wait); |
| 4896 | else |
| 4897 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4898 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4899 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4900 | EXPORT_SYMBOL_HDA(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4901 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4902 | /** |
| 4903 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4904 | * @codec: HD-audio codec |
| 4905 | * @check: the object containing an AMP list and the status |
| 4906 | * @nid: NID to check / update |
| 4907 | * |
| 4908 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4909 | * check the current AMP status, and update the the power-status according |
| 4910 | * to the mute status. |
| 4911 | * |
| 4912 | * This function is supposed to be set or called from the check_power_status |
| 4913 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4914 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4915 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4916 | struct hda_loopback_check *check, |
| 4917 | hda_nid_t nid) |
| 4918 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4919 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4920 | int ch, v; |
| 4921 | |
| 4922 | if (!check->amplist) |
| 4923 | return 0; |
| 4924 | for (p = check->amplist; p->nid; p++) { |
| 4925 | if (p->nid == nid) |
| 4926 | break; |
| 4927 | } |
| 4928 | if (!p->nid) |
| 4929 | return 0; /* nothing changed */ |
| 4930 | |
| 4931 | for (p = check->amplist; p->nid; p++) { |
| 4932 | for (ch = 0; ch < 2; ch++) { |
| 4933 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4934 | p->idx); |
| 4935 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4936 | if (!check->power_on) { |
| 4937 | check->power_on = 1; |
| 4938 | snd_hda_power_up(codec); |
| 4939 | } |
| 4940 | return 1; |
| 4941 | } |
| 4942 | } |
| 4943 | } |
| 4944 | if (check->power_on) { |
| 4945 | check->power_on = 0; |
| 4946 | snd_hda_power_down(codec); |
| 4947 | } |
| 4948 | return 0; |
| 4949 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4950 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4951 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4953 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4954 | * Channel mode helper |
| 4955 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4956 | |
| 4957 | /** |
| 4958 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 4959 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4960 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4961 | struct snd_ctl_elem_info *uinfo, |
| 4962 | const struct hda_channel_mode *chmode, |
| 4963 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4964 | { |
| 4965 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4966 | uinfo->count = 1; |
| 4967 | uinfo->value.enumerated.items = num_chmodes; |
| 4968 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4969 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4970 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4971 | chmode[uinfo->value.enumerated.item].channels); |
| 4972 | return 0; |
| 4973 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4974 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4975 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4976 | /** |
| 4977 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 4978 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4979 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4980 | struct snd_ctl_elem_value *ucontrol, |
| 4981 | const struct hda_channel_mode *chmode, |
| 4982 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4983 | int max_channels) |
| 4984 | { |
| 4985 | int i; |
| 4986 | |
| 4987 | for (i = 0; i < num_chmodes; i++) { |
| 4988 | if (max_channels == chmode[i].channels) { |
| 4989 | ucontrol->value.enumerated.item[0] = i; |
| 4990 | break; |
| 4991 | } |
| 4992 | } |
| 4993 | return 0; |
| 4994 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4995 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4996 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4997 | /** |
| 4998 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 4999 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5000 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5001 | struct snd_ctl_elem_value *ucontrol, |
| 5002 | const struct hda_channel_mode *chmode, |
| 5003 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5004 | int *max_channelsp) |
| 5005 | { |
| 5006 | unsigned int mode; |
| 5007 | |
| 5008 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5009 | if (mode >= num_chmodes) |
| 5010 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5011 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5012 | return 0; |
| 5013 | /* change the current channel setting */ |
| 5014 | *max_channelsp = chmode[mode].channels; |
| 5015 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5016 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5017 | return 1; |
| 5018 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5019 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | /* |
| 5022 | * input MUX helper |
| 5023 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5024 | |
| 5025 | /** |
| 5026 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5027 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5028 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5029 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | { |
| 5031 | unsigned int index; |
| 5032 | |
| 5033 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5034 | uinfo->count = 1; |
| 5035 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5036 | if (!imux->num_items) |
| 5037 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | index = uinfo->value.enumerated.item; |
| 5039 | if (index >= imux->num_items) |
| 5040 | index = imux->num_items - 1; |
| 5041 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5042 | return 0; |
| 5043 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5044 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5046 | /** |
| 5047 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5048 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5049 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5050 | const struct hda_input_mux *imux, |
| 5051 | struct snd_ctl_elem_value *ucontrol, |
| 5052 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | unsigned int *cur_val) |
| 5054 | { |
| 5055 | unsigned int idx; |
| 5056 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5057 | if (!imux->num_items) |
| 5058 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | idx = ucontrol->value.enumerated.item[0]; |
| 5060 | if (idx >= imux->num_items) |
| 5061 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5062 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5064 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5065 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | *cur_val = idx; |
| 5067 | return 1; |
| 5068 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5069 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
| 5071 | |
| 5072 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5073 | * process kcontrol info callback of a simple string enum array |
| 5074 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5075 | */ |
| 5076 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5077 | struct snd_ctl_elem_info *uinfo, |
| 5078 | int num_items, const char * const *texts) |
| 5079 | { |
| 5080 | static const char * const texts_default[] = { |
| 5081 | "Disabled", "Enabled" |
| 5082 | }; |
| 5083 | |
| 5084 | if (!texts || !num_items) { |
| 5085 | num_items = 2; |
| 5086 | texts = texts_default; |
| 5087 | } |
| 5088 | |
| 5089 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5090 | uinfo->count = 1; |
| 5091 | uinfo->value.enumerated.items = num_items; |
| 5092 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5093 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5094 | strcpy(uinfo->value.enumerated.name, |
| 5095 | texts[uinfo->value.enumerated.item]); |
| 5096 | return 0; |
| 5097 | } |
| 5098 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); |
| 5099 | |
| 5100 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | * Multi-channel / digital-out PCM helper functions |
| 5102 | */ |
| 5103 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5104 | /* setup SPDIF output stream */ |
| 5105 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5106 | unsigned int stream_tag, unsigned int format) |
| 5107 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5108 | struct hda_spdif_out *spdif; |
| 5109 | unsigned int curr_fmt; |
| 5110 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5111 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5112 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5113 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5114 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5115 | reset = codec->spdif_status_reset && |
| 5116 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5117 | curr_fmt != format; |
| 5118 | |
| 5119 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5120 | updated */ |
| 5121 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5122 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5123 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5124 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5125 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5126 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5127 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5128 | for (d = codec->slave_dig_outs; *d; d++) |
| 5129 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5130 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5131 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5132 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5133 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5134 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5135 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5136 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5137 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5138 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5139 | { |
| 5140 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5141 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5142 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5143 | for (d = codec->slave_dig_outs; *d; d++) |
| 5144 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5145 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5146 | } |
| 5147 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5148 | /** |
| 5149 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5150 | * @bus: HD-audio bus |
| 5151 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5152 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5153 | { |
| 5154 | struct hda_codec *codec; |
| 5155 | |
| 5156 | if (!bus) |
| 5157 | return; |
| 5158 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5159 | if (hda_codec_is_power_on(codec) && |
| 5160 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5161 | codec->patch_ops.reboot_notify(codec); |
| 5162 | } |
| 5163 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 5164 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5165 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5166 | /** |
| 5167 | * 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] | 5168 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5169 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5170 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5172 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5173 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5174 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5175 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5177 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | return 0; |
| 5179 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5180 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5182 | /** |
| 5183 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5184 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5185 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5186 | struct hda_multi_out *mout, |
| 5187 | unsigned int stream_tag, |
| 5188 | unsigned int format, |
| 5189 | struct snd_pcm_substream *substream) |
| 5190 | { |
| 5191 | mutex_lock(&codec->spdif_mutex); |
| 5192 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5193 | mutex_unlock(&codec->spdif_mutex); |
| 5194 | return 0; |
| 5195 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5196 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5197 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5198 | /** |
| 5199 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5200 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5201 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5202 | struct hda_multi_out *mout) |
| 5203 | { |
| 5204 | mutex_lock(&codec->spdif_mutex); |
| 5205 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5206 | mutex_unlock(&codec->spdif_mutex); |
| 5207 | return 0; |
| 5208 | } |
| 5209 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 5210 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5211 | /** |
| 5212 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5214 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5215 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5217 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5219 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | return 0; |
| 5221 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5222 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5224 | /** |
| 5225 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5226 | * |
| 5227 | * Open analog outputs and set up the hw-constraints. |
| 5228 | * If the digital outputs can be opened as slave, open the digital |
| 5229 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5231 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5232 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5233 | struct snd_pcm_substream *substream, |
| 5234 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5236 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5237 | runtime->hw.channels_max = mout->max_channels; |
| 5238 | if (mout->dig_out_nid) { |
| 5239 | if (!mout->analog_rates) { |
| 5240 | mout->analog_rates = hinfo->rates; |
| 5241 | mout->analog_formats = hinfo->formats; |
| 5242 | mout->analog_maxbps = hinfo->maxbps; |
| 5243 | } else { |
| 5244 | runtime->hw.rates = mout->analog_rates; |
| 5245 | runtime->hw.formats = mout->analog_formats; |
| 5246 | hinfo->maxbps = mout->analog_maxbps; |
| 5247 | } |
| 5248 | if (!mout->spdif_rates) { |
| 5249 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5250 | &mout->spdif_rates, |
| 5251 | &mout->spdif_formats, |
| 5252 | &mout->spdif_maxbps); |
| 5253 | } |
| 5254 | mutex_lock(&codec->spdif_mutex); |
| 5255 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5256 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5257 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5258 | runtime->hw.rates &= mout->spdif_rates; |
| 5259 | runtime->hw.formats &= mout->spdif_formats; |
| 5260 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5261 | hinfo->maxbps = mout->spdif_maxbps; |
| 5262 | } else { |
| 5263 | mout->share_spdif = 0; |
| 5264 | /* FIXME: need notify? */ |
| 5265 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5266 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5267 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5270 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5271 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5272 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5274 | /** |
| 5275 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5276 | * |
| 5277 | * Set up the i/o for analog out. |
| 5278 | * 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] | 5279 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5280 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5281 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | unsigned int stream_tag, |
| 5283 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5284 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5286 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5288 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | int i; |
| 5290 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5291 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5292 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5293 | if (mout->dig_out_nid && mout->share_spdif && |
| 5294 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5296 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5297 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5298 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5300 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5301 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | } else { |
| 5303 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5304 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | } |
| 5306 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5307 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | |
| 5309 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5310 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5311 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5312 | if (!mout->no_share_stream && |
| 5313 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5315 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5316 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5317 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5318 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5319 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5320 | snd_hda_codec_setup_stream(codec, |
| 5321 | mout->hp_out_nid[i], |
| 5322 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5323 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5324 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5325 | snd_hda_codec_setup_stream(codec, |
| 5326 | mout->extra_out_nid[i], |
| 5327 | stream_tag, 0, format); |
| 5328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | /* surrounds */ |
| 5330 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5331 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5332 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5333 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5334 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5335 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5336 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | } |
| 5338 | return 0; |
| 5339 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5340 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5342 | /** |
| 5343 | * 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] | 5344 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5345 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5346 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5348 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | int i; |
| 5350 | |
| 5351 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5352 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5354 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5355 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5356 | if (mout->hp_out_nid[i]) |
| 5357 | snd_hda_codec_cleanup_stream(codec, |
| 5358 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5359 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5360 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5361 | snd_hda_codec_cleanup_stream(codec, |
| 5362 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5363 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | 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] | 5365 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | mout->dig_out_used = 0; |
| 5367 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5368 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | return 0; |
| 5370 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5371 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5373 | /** |
| 5374 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5375 | * |
| 5376 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5377 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5378 | */ |
| 5379 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5380 | { |
| 5381 | unsigned int pincap; |
| 5382 | unsigned int oldval; |
| 5383 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5384 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5385 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5386 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5387 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5388 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5389 | return AC_PINCTL_VREF_80; |
| 5390 | else if (pincap & AC_PINCAP_VREF_50) |
| 5391 | return AC_PINCTL_VREF_50; |
| 5392 | else if (pincap & AC_PINCAP_VREF_100) |
| 5393 | return AC_PINCTL_VREF_100; |
| 5394 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5395 | return AC_PINCTL_VREF_GRD; |
| 5396 | return AC_PINCTL_VREF_HIZ; |
| 5397 | } |
| 5398 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); |
| 5399 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5400 | /* correct the pin ctl value for matching with the pin cap */ |
| 5401 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5402 | hda_nid_t pin, unsigned int val) |
| 5403 | { |
| 5404 | static unsigned int cap_lists[][2] = { |
| 5405 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5406 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5407 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5408 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5409 | }; |
| 5410 | unsigned int cap; |
| 5411 | |
| 5412 | if (!val) |
| 5413 | return 0; |
| 5414 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5415 | if (!cap) |
| 5416 | return val; /* don't know what to do... */ |
| 5417 | |
| 5418 | if (val & AC_PINCTL_OUT_EN) { |
| 5419 | if (!(cap & AC_PINCAP_OUT)) |
| 5420 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5421 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5422 | val &= ~AC_PINCTL_HP_EN; |
| 5423 | } |
| 5424 | |
| 5425 | if (val & AC_PINCTL_IN_EN) { |
| 5426 | if (!(cap & AC_PINCAP_IN)) |
| 5427 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5428 | else { |
| 5429 | unsigned int vcap, vref; |
| 5430 | int i; |
| 5431 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5432 | vref = val & AC_PINCTL_VREFEN; |
| 5433 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5434 | if (vref == cap_lists[i][0] && |
| 5435 | !(vcap & cap_lists[i][1])) { |
| 5436 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5437 | vref = AC_PINCTL_VREF_HIZ; |
| 5438 | else |
| 5439 | vref = cap_lists[i + 1][0]; |
| 5440 | } |
| 5441 | } |
| 5442 | val &= ~AC_PINCTL_VREFEN; |
| 5443 | val |= vref; |
| 5444 | } |
| 5445 | } |
| 5446 | |
| 5447 | return val; |
| 5448 | } |
| 5449 | EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl); |
| 5450 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5451 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5452 | unsigned int val, bool cached) |
| 5453 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5454 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5455 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5456 | if (cached) |
| 5457 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5458 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5459 | else |
| 5460 | return snd_hda_codec_write(codec, pin, 0, |
| 5461 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5462 | } |
| 5463 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); |
| 5464 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5465 | /** |
| 5466 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5467 | * |
| 5468 | * When the same label is used already in the existing items, the number |
| 5469 | * suffix is appended to the label. This label index number is stored |
| 5470 | * to type_idx when non-NULL pointer is given. |
| 5471 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5472 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5473 | int index, int *type_idx) |
| 5474 | { |
| 5475 | int i, label_idx = 0; |
| 5476 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5477 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5478 | return -EINVAL; |
| 5479 | } |
| 5480 | for (i = 0; i < imux->num_items; i++) { |
| 5481 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5482 | label_idx++; |
| 5483 | } |
| 5484 | if (type_idx) |
| 5485 | *type_idx = label_idx; |
| 5486 | if (label_idx > 0) |
| 5487 | snprintf(imux->items[imux->num_items].label, |
| 5488 | sizeof(imux->items[imux->num_items].label), |
| 5489 | "%s %d", label, label_idx); |
| 5490 | else |
| 5491 | strlcpy(imux->items[imux->num_items].label, label, |
| 5492 | sizeof(imux->items[imux->num_items].label)); |
| 5493 | imux->items[imux->num_items].index = index; |
| 5494 | imux->num_items++; |
| 5495 | return 0; |
| 5496 | } |
| 5497 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5498 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | #ifdef CONFIG_PM |
| 5501 | /* |
| 5502 | * power management |
| 5503 | */ |
| 5504 | |
| 5505 | /** |
| 5506 | * snd_hda_suspend - suspend the codecs |
| 5507 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | * |
| 5509 | * Returns 0 if successful. |
| 5510 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5511 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5513 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5515 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5516 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5517 | if (hda_codec_is_power_on(codec)) |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5518 | hda_call_codec_suspend(codec, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | } |
| 5520 | return 0; |
| 5521 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5522 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | |
| 5524 | /** |
| 5525 | * snd_hda_resume - resume the codecs |
| 5526 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | * |
| 5528 | * Returns 0 if successful. |
| 5529 | */ |
| 5530 | int snd_hda_resume(struct hda_bus *bus) |
| 5531 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5532 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5534 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5535 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | return 0; |
| 5538 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5539 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5540 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5541 | |
| 5542 | /* |
| 5543 | * generic arrays |
| 5544 | */ |
| 5545 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5546 | /** |
| 5547 | * snd_array_new - get a new element from the given array |
| 5548 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5549 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5550 | * Get a new element from the given array. If it exceeds the |
| 5551 | * pre-allocated array size, re-allocate the array. |
| 5552 | * |
| 5553 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5554 | */ |
| 5555 | void *snd_array_new(struct snd_array *array) |
| 5556 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5557 | if (snd_BUG_ON(!array->elem_size)) |
| 5558 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5559 | if (array->used >= array->alloced) { |
| 5560 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5561 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5562 | void *nlist; |
| 5563 | if (snd_BUG_ON(num >= 4096)) |
| 5564 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5565 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5566 | if (!nlist) |
| 5567 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5568 | array->list = nlist; |
| 5569 | array->alloced = num; |
| 5570 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5571 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5572 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5573 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5574 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5575 | /** |
| 5576 | * snd_array_free - free the given array elements |
| 5577 | * @array: the array object |
| 5578 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5579 | void snd_array_free(struct snd_array *array) |
| 5580 | { |
| 5581 | kfree(array->list); |
| 5582 | array->used = 0; |
| 5583 | array->alloced = 0; |
| 5584 | array->list = NULL; |
| 5585 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5586 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5587 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5588 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5589 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5590 | * @pcm: PCM caps bits |
| 5591 | * @buf: the string buffer to write |
| 5592 | * @buflen: the max buffer length |
| 5593 | * |
| 5594 | * used by hda_proc.c and hda_eld.c |
| 5595 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5596 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5597 | { |
| 5598 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5599 | int i, j; |
| 5600 | |
| 5601 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5602 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5603 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5604 | |
| 5605 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5606 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5607 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5608 | |
| 5609 | MODULE_DESCRIPTION("HDA codec core"); |
| 5610 | MODULE_LICENSE("GPL"); |