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