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 | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 102 | |
| 103 | static void hda_call_pm_notify(struct hda_codec *codec, bool power_up) |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 104 | { |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 105 | struct hda_bus *bus = codec->bus; |
| 106 | |
| 107 | if ((power_up && codec->pm_up_notified) || |
| 108 | (!power_up && !codec->pm_up_notified)) |
| 109 | return; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 110 | if (bus->ops.pm_notify) |
| 111 | bus->ops.pm_notify(bus, power_up); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 112 | codec->pm_up_notified = power_up; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 113 | } |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 114 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 115 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 116 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 117 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 118 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 119 | #define hda_call_pm_notify(codec, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 120 | #endif |
| 121 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 122 | /** |
| 123 | * snd_hda_get_jack_location - Give a location string of the jack |
| 124 | * @cfg: pin default config value |
| 125 | * |
| 126 | * Parse the pin default config value and returns the string of the |
| 127 | * jack location, e.g. "Rear", "Front", etc. |
| 128 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 129 | const char *snd_hda_get_jack_location(u32 cfg) |
| 130 | { |
| 131 | static char *bases[7] = { |
| 132 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 133 | }; |
| 134 | static unsigned char specials_idx[] = { |
| 135 | 0x07, 0x08, |
| 136 | 0x17, 0x18, 0x19, |
| 137 | 0x37, 0x38 |
| 138 | }; |
| 139 | static char *specials[] = { |
| 140 | "Rear Panel", "Drive Bar", |
| 141 | "Riser", "HDMI", "ATAPI", |
| 142 | "Mobile-In", "Mobile-Out" |
| 143 | }; |
| 144 | int i; |
| 145 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 146 | if ((cfg & 0x0f) < 7) |
| 147 | return bases[cfg & 0x0f]; |
| 148 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 149 | if (cfg == specials_idx[i]) |
| 150 | return specials[i]; |
| 151 | } |
| 152 | return "UNKNOWN"; |
| 153 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 154 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 155 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 156 | /** |
| 157 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 158 | * @cfg: pin default config value |
| 159 | * |
| 160 | * Parse the pin default config value and returns the string of the |
| 161 | * jack connectivity, i.e. external or internal connection. |
| 162 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 163 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 164 | { |
| 165 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 166 | |
| 167 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 168 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 169 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 170 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 171 | /** |
| 172 | * snd_hda_get_jack_type - Give a type string of the jack |
| 173 | * @cfg: pin default config value |
| 174 | * |
| 175 | * Parse the pin default config value and returns the string of the |
| 176 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 177 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 178 | const char *snd_hda_get_jack_type(u32 cfg) |
| 179 | { |
| 180 | static char *jack_types[16] = { |
| 181 | "Line Out", "Speaker", "HP Out", "CD", |
| 182 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 183 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 184 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 188 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 189 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 190 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 191 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 192 | /* |
| 193 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 194 | */ |
| 195 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 196 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags, |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 197 | unsigned int verb, unsigned int parm) |
| 198 | { |
| 199 | u32 val; |
| 200 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 201 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 202 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 203 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n", |
| 204 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 205 | return ~0; |
| 206 | } |
| 207 | |
| 208 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 209 | val |= (u32)nid << 20; |
| 210 | val |= verb << 8; |
| 211 | val |= parm; |
| 212 | return val; |
| 213 | } |
| 214 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 215 | /* |
| 216 | * Send and receive a verb |
| 217 | */ |
| 218 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 219 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 220 | { |
| 221 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 222 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 223 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 224 | if (cmd == ~0) |
| 225 | return -1; |
| 226 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 227 | if (res) |
| 228 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 229 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 230 | snd_hda_power_up(codec); |
| 231 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 232 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 233 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 234 | for (;;) { |
| 235 | trace_hda_send_cmd(codec, cmd); |
| 236 | err = bus->ops.command(bus, cmd); |
| 237 | if (err != -EAGAIN) |
| 238 | break; |
| 239 | /* process pending verbs */ |
| 240 | bus->ops.get_response(bus, codec->addr); |
| 241 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 242 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 243 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 244 | trace_hda_get_response(codec, *res); |
| 245 | } |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 246 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 247 | mutex_unlock(&bus->cmd_mutex); |
| 248 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 249 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 250 | if (bus->response_reset) { |
| 251 | snd_printd("hda_codec: resetting BUS due to " |
| 252 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 253 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 254 | bus->ops.bus_reset(bus); |
| 255 | } |
| 256 | goto again; |
| 257 | } |
| 258 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 259 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 260 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 261 | return err; |
| 262 | } |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | /** |
| 265 | * snd_hda_codec_read - send a command and get the response |
| 266 | * @codec: the HDA codec |
| 267 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 268 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * @verb: the verb to send |
| 270 | * @parm: the parameter for the verb |
| 271 | * |
| 272 | * Send a single command and read the corresponding response. |
| 273 | * |
| 274 | * Returns the obtained response value, or -1 for an error. |
| 275 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 276 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 277 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | unsigned int verb, unsigned int parm) |
| 279 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 280 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 281 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 282 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 283 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return res; |
| 285 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 286 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * snd_hda_codec_write - send a single command without waiting for response |
| 290 | * @codec: the HDA codec |
| 291 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 292 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * @verb: the verb to send |
| 294 | * @parm: the parameter for the verb |
| 295 | * |
| 296 | * Send a single command without waiting for response. |
| 297 | * |
| 298 | * Returns 0 if successful, or a negative error code. |
| 299 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 300 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 301 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 303 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 304 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 305 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 306 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 308 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /** |
| 311 | * snd_hda_sequence_write - sequence writes |
| 312 | * @codec: the HDA codec |
| 313 | * @seq: VERB array to send |
| 314 | * |
| 315 | * Send the commands sequentially from the given array. |
| 316 | * The array must be terminated with NID=0. |
| 317 | */ |
| 318 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 319 | { |
| 320 | for (; seq->nid; seq++) |
| 321 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 322 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 323 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 327 | * @codec: the HDA codec |
| 328 | * @nid: NID to parse |
| 329 | * @start_id: the pointer to store the start NID |
| 330 | * |
| 331 | * Parse the NID and store the start NID of its sub-nodes. |
| 332 | * Returns the number of sub-nodes. |
| 333 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 334 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 335 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
| 337 | unsigned int parm; |
| 338 | |
| 339 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 340 | if (parm == -1) |
| 341 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | *start_id = (parm >> 16) & 0x7fff; |
| 343 | return (int)(parm & 0x7fff); |
| 344 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 345 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 347 | /* connection list element */ |
| 348 | struct hda_conn_list { |
| 349 | struct list_head list; |
| 350 | int len; |
| 351 | hda_nid_t nid; |
| 352 | hda_nid_t conns[0]; |
| 353 | }; |
| 354 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 355 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 356 | static struct hda_conn_list * |
| 357 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 358 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 359 | struct hda_conn_list *p; |
| 360 | list_for_each_entry(p, &codec->conn_list, list) { |
| 361 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 362 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 363 | } |
| 364 | return NULL; |
| 365 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 366 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 367 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 368 | const hda_nid_t *list) |
| 369 | { |
| 370 | struct hda_conn_list *p; |
| 371 | |
| 372 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 373 | if (!p) |
| 374 | return -ENOMEM; |
| 375 | p->len = len; |
| 376 | p->nid = nid; |
| 377 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 378 | list_add(&p->list, &codec->conn_list); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void remove_conn_list(struct hda_codec *codec) |
| 383 | { |
| 384 | while (!list_empty(&codec->conn_list)) { |
| 385 | struct hda_conn_list *p; |
| 386 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 387 | list_del(&p->list); |
| 388 | kfree(p); |
| 389 | } |
| 390 | } |
| 391 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 392 | /* read the connection and add to the cache */ |
| 393 | 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] | 394 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 395 | hda_nid_t list[32]; |
| 396 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 397 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 398 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 399 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 400 | if (len == -ENOSPC) { |
| 401 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 402 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 403 | if (!result) |
| 404 | return -ENOMEM; |
| 405 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 406 | } |
| 407 | if (len >= 0) |
| 408 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 409 | if (result != list) |
| 410 | kfree(result); |
| 411 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 412 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 413 | |
| 414 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 415 | * snd_hda_get_conn_list - get connection list |
| 416 | * @codec: the HDA codec |
| 417 | * @nid: NID to parse |
| 418 | * @len: number of connection list entries |
| 419 | * @listp: the pointer to store NID list |
| 420 | * |
| 421 | * Parses the connection list of the given widget and stores the pointer |
| 422 | * to the list of NIDs. |
| 423 | * |
| 424 | * Returns the number of connections, or a negative error code. |
| 425 | * |
| 426 | * Note that the returned pointer isn't protected against the list |
| 427 | * modification. If snd_hda_override_conn_list() might be called |
| 428 | * concurrently, protect with a mutex appropriately. |
| 429 | */ |
| 430 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 431 | const hda_nid_t **listp) |
| 432 | { |
| 433 | bool added = false; |
| 434 | |
| 435 | for (;;) { |
| 436 | int err; |
| 437 | const struct hda_conn_list *p; |
| 438 | |
| 439 | /* if the connection-list is already cached, read it */ |
| 440 | p = lookup_conn_list(codec, nid); |
| 441 | if (p) { |
| 442 | if (listp) |
| 443 | *listp = p->conns; |
| 444 | return p->len; |
| 445 | } |
| 446 | if (snd_BUG_ON(added)) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | err = read_and_add_raw_conns(codec, nid); |
| 450 | if (err < 0) |
| 451 | return err; |
| 452 | added = true; |
| 453 | } |
| 454 | } |
| 455 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 456 | |
| 457 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 458 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | * @codec: the HDA codec |
| 460 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 461 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * @max_conns: max. number of connections to store |
| 463 | * |
| 464 | * Parses the connection list of the given widget and stores the list |
| 465 | * of NIDs. |
| 466 | * |
| 467 | * Returns the number of connections, or a negative error code. |
| 468 | */ |
| 469 | 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] | 470 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 471 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 472 | const hda_nid_t *list; |
| 473 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 474 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 475 | if (len > 0 && conn_list) { |
| 476 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 477 | snd_printk(KERN_ERR "hda_codec: " |
| 478 | "Too many connections %d for NID 0x%x\n", |
| 479 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 480 | return -EINVAL; |
| 481 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 482 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 483 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 484 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 485 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 486 | } |
| 487 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 488 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 489 | /* return CONNLIST_LEN parameter of the given widget */ |
| 490 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 491 | { |
| 492 | unsigned int wcaps = get_wcaps(codec, nid); |
| 493 | unsigned int parm; |
| 494 | |
| 495 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 496 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 497 | return 0; |
| 498 | |
| 499 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 500 | if (parm == -1) |
| 501 | parm = 0; |
| 502 | return parm; |
| 503 | } |
| 504 | |
| 505 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 506 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 507 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 510 | /** |
| 511 | * snd_hda_get_raw_connections - copy connection list without cache |
| 512 | * @codec: the HDA codec |
| 513 | * @nid: NID to parse |
| 514 | * @conn_list: connection list array |
| 515 | * @max_conns: max. number of connections to store |
| 516 | * |
| 517 | * Like snd_hda_get_connections(), copy the connection list but without |
| 518 | * checking through the connection-list cache. |
| 519 | * Currently called only from hda_proc.c, so not exported. |
| 520 | */ |
| 521 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 522 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 525 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 527 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 528 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 530 | parm = get_num_conns(codec, nid); |
| 531 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 532 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (parm & AC_CLIST_LONG) { |
| 535 | /* long form */ |
| 536 | shift = 16; |
| 537 | num_elems = 2; |
| 538 | } else { |
| 539 | /* short form */ |
| 540 | shift = 8; |
| 541 | num_elems = 4; |
| 542 | } |
| 543 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | mask = (1 << (shift-1)) - 1; |
| 545 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 546 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return 0; /* no connection */ |
| 548 | |
| 549 | if (conn_len == 1) { |
| 550 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 551 | parm = snd_hda_codec_read(codec, nid, 0, |
| 552 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 553 | if (parm == -1 && codec->bus->rirb_error) |
| 554 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 555 | if (conn_list) |
| 556 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | /* multi connection */ |
| 561 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 562 | prev_nid = 0; |
| 563 | for (i = 0; i < conn_len; i++) { |
| 564 | int range_val; |
| 565 | hda_nid_t val, n; |
| 566 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 567 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 568 | parm = snd_hda_codec_read(codec, nid, 0, |
| 569 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 570 | if (parm == -1 && codec->bus->rirb_error) |
| 571 | return -EIO; |
| 572 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 573 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 574 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 575 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4758fed | 2013-10-17 17:56:25 +0200 | [diff] [blame] | 576 | snd_printdd("hda_codec: " |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 577 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 578 | nid, i, parm); |
| 579 | return 0; |
| 580 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 581 | parm >>= shift; |
| 582 | if (range_val) { |
| 583 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 584 | if (!prev_nid || prev_nid >= val) { |
| 585 | snd_printk(KERN_WARNING "hda_codec: " |
| 586 | "invalid dep_range_val %x:%x\n", |
| 587 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 588 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 590 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 591 | if (conn_list) { |
| 592 | if (conns >= max_conns) |
| 593 | return -ENOSPC; |
| 594 | conn_list[conns] = n; |
| 595 | } |
| 596 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 597 | } |
| 598 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 599 | if (conn_list) { |
| 600 | if (conns >= max_conns) |
| 601 | return -ENOSPC; |
| 602 | conn_list[conns] = val; |
| 603 | } |
| 604 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 606 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
| 608 | return conns; |
| 609 | } |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 612 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 613 | * @codec: the HDA codec |
| 614 | * @nid: NID to parse |
| 615 | * @len: number of connection list entries |
| 616 | * @list: the list of connection entries |
| 617 | * |
| 618 | * Add or modify the given connection-list to the cache. If the corresponding |
| 619 | * cache already exists, invalidate it and append a new one. |
| 620 | * |
| 621 | * Returns zero or a negative error code. |
| 622 | */ |
| 623 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 624 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 626 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 627 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 628 | p = lookup_conn_list(codec, nid); |
| 629 | if (p) { |
| 630 | list_del(&p->list); |
| 631 | kfree(p); |
| 632 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 633 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 634 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 636 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 637 | |
| 638 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 639 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 640 | * @codec: the HDA codec |
| 641 | * @mux: NID containing the list |
| 642 | * @nid: NID to select |
| 643 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 644 | * |
| 645 | * Parses the connection list of the widget @mux and checks whether the |
| 646 | * widget @nid is present. If it is, return the connection index. |
| 647 | * Otherwise it returns -1. |
| 648 | */ |
| 649 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 650 | hda_nid_t nid, int recursive) |
| 651 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 652 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 653 | int i, nums; |
| 654 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 655 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 656 | for (i = 0; i < nums; i++) |
| 657 | if (conn[i] == nid) |
| 658 | return i; |
| 659 | if (!recursive) |
| 660 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 661 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 662 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 663 | return -1; |
| 664 | } |
| 665 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 666 | for (i = 0; i < nums; i++) { |
| 667 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 668 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 669 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 670 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 671 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 672 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 673 | return -1; |
| 674 | } |
| 675 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 677 | |
| 678 | /* return DEVLIST_LEN parameter of the given widget */ |
| 679 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 680 | { |
| 681 | unsigned int wcaps = get_wcaps(codec, nid); |
| 682 | unsigned int parm; |
| 683 | |
| 684 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 685 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 686 | return 0; |
| 687 | |
| 688 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 689 | if (parm == -1 && codec->bus->rirb_error) |
| 690 | parm = 0; |
| 691 | return parm & AC_DEV_LIST_LEN_MASK; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * snd_hda_get_devices - copy device list without cache |
| 696 | * @codec: the HDA codec |
| 697 | * @nid: NID of the pin to parse |
| 698 | * @dev_list: device list array |
| 699 | * @max_devices: max. number of devices to store |
| 700 | * |
| 701 | * Copy the device list. This info is dynamic and so not cached. |
| 702 | * Currently called only from hda_proc.c, so not exported. |
| 703 | */ |
| 704 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 705 | u8 *dev_list, int max_devices) |
| 706 | { |
| 707 | unsigned int parm; |
| 708 | int i, dev_len, devices; |
| 709 | |
| 710 | parm = get_num_devices(codec, nid); |
| 711 | if (!parm) /* not multi-stream capable */ |
| 712 | return 0; |
| 713 | |
| 714 | dev_len = parm + 1; |
| 715 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 716 | |
| 717 | devices = 0; |
| 718 | while (devices < dev_len) { |
| 719 | parm = snd_hda_codec_read(codec, nid, 0, |
| 720 | AC_VERB_GET_DEVICE_LIST, devices); |
| 721 | if (parm == -1 && codec->bus->rirb_error) |
| 722 | break; |
| 723 | |
| 724 | for (i = 0; i < 8; i++) { |
| 725 | dev_list[devices] = (u8)parm; |
| 726 | parm >>= 4; |
| 727 | devices++; |
| 728 | if (devices >= dev_len) |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | return devices; |
| 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | /** |
| 736 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 737 | * @bus: the BUS |
| 738 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 739 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 740 | * |
| 741 | * Adds the given event to the queue. The events are processed in |
| 742 | * the workqueue asynchronously. Call this function in the interrupt |
| 743 | * hanlder when RIRB receives an unsolicited event. |
| 744 | * |
| 745 | * Returns 0 if successful, or a negative error code. |
| 746 | */ |
| 747 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 748 | { |
| 749 | struct hda_bus_unsolicited *unsol; |
| 750 | unsigned int wp; |
| 751 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 752 | if (!bus || !bus->workq) |
| 753 | return 0; |
| 754 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 755 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 756 | unsol = bus->unsol; |
| 757 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return 0; |
| 759 | |
| 760 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 761 | unsol->wp = wp; |
| 762 | |
| 763 | wp <<= 1; |
| 764 | unsol->queue[wp] = res; |
| 765 | unsol->queue[wp + 1] = res_ex; |
| 766 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 767 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | return 0; |
| 770 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 771 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 774 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 776 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 778 | struct hda_bus_unsolicited *unsol = |
| 779 | container_of(work, struct hda_bus_unsolicited, work); |
| 780 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | struct hda_codec *codec; |
| 782 | unsigned int rp, caddr, res; |
| 783 | |
| 784 | while (unsol->rp != unsol->wp) { |
| 785 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 786 | unsol->rp = rp; |
| 787 | rp <<= 1; |
| 788 | res = unsol->queue[rp]; |
| 789 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 790 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | continue; |
| 792 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 793 | if (codec && codec->patch_ops.unsol_event) |
| 794 | codec->patch_ops.unsol_event(codec, res); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * initialize unsolicited queue |
| 800 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 801 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
| 803 | struct hda_bus_unsolicited *unsol; |
| 804 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 805 | if (bus->unsol) /* already initialized */ |
| 806 | return 0; |
| 807 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 808 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 809 | if (!unsol) { |
| 810 | snd_printk(KERN_ERR "hda_codec: " |
| 811 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return -ENOMEM; |
| 813 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 814 | INIT_WORK(&unsol->work, process_unsol_events); |
| 815 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | bus->unsol = unsol; |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * destructor |
| 822 | */ |
| 823 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 824 | |
| 825 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 826 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 827 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 829 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 831 | if (bus->workq) |
| 832 | flush_workqueue(bus->workq); |
| 833 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 835 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | snd_hda_codec_free(codec); |
| 837 | } |
| 838 | if (bus->ops.private_free) |
| 839 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 840 | if (bus->workq) |
| 841 | destroy_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | kfree(bus); |
| 843 | return 0; |
| 844 | } |
| 845 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 846 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
| 848 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 849 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return snd_hda_bus_free(bus); |
| 851 | } |
| 852 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 853 | #ifdef CONFIG_SND_HDA_HWDEP |
| 854 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 855 | { |
| 856 | struct hda_bus *bus = device->device_data; |
| 857 | struct hda_codec *codec; |
| 858 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 859 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 860 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 861 | } |
| 862 | return 0; |
| 863 | } |
| 864 | #else |
| 865 | #define snd_hda_bus_dev_register NULL |
| 866 | #endif |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | /** |
| 869 | * snd_hda_bus_new - create a HDA bus |
| 870 | * @card: the card entry |
| 871 | * @temp: the template for hda_bus information |
| 872 | * @busp: the pointer to store the created bus instance |
| 873 | * |
| 874 | * Returns 0 if successful, or a negative error code. |
| 875 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 876 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 877 | const struct hda_bus_template *temp, |
| 878 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
| 880 | struct hda_bus *bus; |
| 881 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 882 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 883 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | .dev_free = snd_hda_bus_dev_free, |
| 885 | }; |
| 886 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 887 | if (snd_BUG_ON(!temp)) |
| 888 | return -EINVAL; |
| 889 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 890 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | if (busp) |
| 893 | *busp = NULL; |
| 894 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 895 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (bus == NULL) { |
| 897 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 898 | return -ENOMEM; |
| 899 | } |
| 900 | |
| 901 | bus->card = card; |
| 902 | bus->private_data = temp->private_data; |
| 903 | bus->pci = temp->pci; |
| 904 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 905 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | bus->ops = temp->ops; |
| 907 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 908 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 909 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | INIT_LIST_HEAD(&bus->codec_list); |
| 911 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 912 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 913 | "hd-audio%d", card->number); |
| 914 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 915 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 916 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 917 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 918 | kfree(bus); |
| 919 | return -ENOMEM; |
| 920 | } |
| 921 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 922 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 923 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | snd_hda_bus_free(bus); |
| 925 | return err; |
| 926 | } |
| 927 | if (busp) |
| 928 | *busp = bus; |
| 929 | return 0; |
| 930 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 931 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 933 | #ifdef CONFIG_SND_HDA_GENERIC |
| 934 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 935 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 936 | #else |
| 937 | #define is_generic_config(codec) 0 |
| 938 | #endif |
| 939 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 940 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 941 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 942 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 943 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 944 | #endif |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | /* |
| 947 | * find a matching codec preset |
| 948 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 949 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 950 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 952 | struct hda_codec_preset_list *tbl; |
| 953 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 954 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 956 | again: |
| 957 | mutex_lock(&preset_mutex); |
| 958 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 959 | if (!try_module_get(tbl->owner)) { |
| 960 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 961 | continue; |
| 962 | } |
| 963 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 965 | if (preset->afg && preset->afg != codec->afg) |
| 966 | continue; |
| 967 | if (preset->mfg && preset->mfg != codec->mfg) |
| 968 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 969 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 971 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 972 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 973 | preset->rev == codec->revision_id)) { |
| 974 | mutex_unlock(&preset_mutex); |
| 975 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 979 | module_put(tbl->owner); |
| 980 | } |
| 981 | mutex_unlock(&preset_mutex); |
| 982 | |
| 983 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 984 | char name[32]; |
| 985 | if (!mod_requested) |
| 986 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 987 | codec->vendor_id); |
| 988 | else |
| 989 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 990 | (codec->vendor_id >> 16) & 0xffff); |
| 991 | request_module(name); |
| 992 | mod_requested++; |
| 993 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 999 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1001 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | { |
| 1003 | const struct hda_vendor_id *c; |
| 1004 | const char *vendor = NULL; |
| 1005 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1006 | char tmp[16]; |
| 1007 | |
| 1008 | if (codec->vendor_name) |
| 1009 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | for (c = hda_vendor_ids; c->id; c++) { |
| 1012 | if (c->id == vendor_id) { |
| 1013 | vendor = c->name; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1017 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1019 | vendor = tmp; |
| 1020 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1021 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1022 | if (!codec->vendor_name) |
| 1023 | return -ENOMEM; |
| 1024 | |
| 1025 | get_chip_name: |
| 1026 | if (codec->chip_name) |
| 1027 | return 0; |
| 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1030 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1031 | else { |
| 1032 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1033 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1034 | } |
| 1035 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1036 | return -ENOMEM; |
| 1037 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1041 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1043 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1045 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | hda_nid_t nid; |
| 1047 | |
| 1048 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1049 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1050 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1051 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1052 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1053 | case AC_GRP_AUDIO_FUNCTION: |
| 1054 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1055 | codec->afg_function_id = function_id & 0xff; |
| 1056 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1057 | break; |
| 1058 | case AC_GRP_MODEM_FUNCTION: |
| 1059 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1060 | codec->mfg_function_id = function_id & 0xff; |
| 1061 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1062 | break; |
| 1063 | default: |
| 1064 | break; |
| 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1070 | * read widget caps for each widget and store in cache |
| 1071 | */ |
| 1072 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1073 | { |
| 1074 | int i; |
| 1075 | hda_nid_t nid; |
| 1076 | |
| 1077 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1078 | &codec->start_nid); |
| 1079 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1080 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1081 | return -ENOMEM; |
| 1082 | nid = codec->start_nid; |
| 1083 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1084 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1085 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1089 | /* read all pin default configurations and save codec->init_pins */ |
| 1090 | static int read_pin_defaults(struct hda_codec *codec) |
| 1091 | { |
| 1092 | int i; |
| 1093 | hda_nid_t nid = codec->start_nid; |
| 1094 | |
| 1095 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1096 | struct hda_pincfg *pin; |
| 1097 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1098 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1099 | if (wid_type != AC_WID_PIN) |
| 1100 | continue; |
| 1101 | pin = snd_array_new(&codec->init_pins); |
| 1102 | if (!pin) |
| 1103 | return -ENOMEM; |
| 1104 | pin->nid = nid; |
| 1105 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1106 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1107 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1108 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1109 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1110 | } |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | /* look up the given pin config list and return the item matching with NID */ |
| 1115 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1116 | struct snd_array *array, |
| 1117 | hda_nid_t nid) |
| 1118 | { |
| 1119 | int i; |
| 1120 | for (i = 0; i < array->used; i++) { |
| 1121 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1122 | if (pin->nid == nid) |
| 1123 | return pin; |
| 1124 | } |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1128 | /* set the current pin config value for the given NID. |
| 1129 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1130 | */ |
| 1131 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1132 | hda_nid_t nid, unsigned int cfg) |
| 1133 | { |
| 1134 | struct hda_pincfg *pin; |
| 1135 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1136 | /* the check below may be invalid when pins are added by a fixup |
| 1137 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1138 | * for now |
| 1139 | */ |
| 1140 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1141 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1142 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1143 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1144 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1145 | pin = look_up_pincfg(codec, list, nid); |
| 1146 | if (!pin) { |
| 1147 | pin = snd_array_new(list); |
| 1148 | if (!pin) |
| 1149 | return -ENOMEM; |
| 1150 | pin->nid = nid; |
| 1151 | } |
| 1152 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1156 | /** |
| 1157 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1158 | * @codec: the HDA codec |
| 1159 | * @nid: NID to set the pin config |
| 1160 | * @cfg: the pin default config value |
| 1161 | * |
| 1162 | * Override a pin default configuration value in the cache. |
| 1163 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1164 | * priority than the real hardware value. |
| 1165 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1166 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1167 | hda_nid_t nid, unsigned int cfg) |
| 1168 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1169 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1170 | } |
| 1171 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1172 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1173 | /** |
| 1174 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1175 | * @codec: the HDA codec |
| 1176 | * @nid: NID to get the pin config |
| 1177 | * |
| 1178 | * Get the current pin config value of the given pin NID. |
| 1179 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1180 | * returns the cached value. |
| 1181 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1182 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1183 | { |
| 1184 | struct hda_pincfg *pin; |
| 1185 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1186 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1187 | { |
| 1188 | unsigned int cfg = 0; |
| 1189 | mutex_lock(&codec->user_mutex); |
| 1190 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1191 | if (pin) |
| 1192 | cfg = pin->cfg; |
| 1193 | mutex_unlock(&codec->user_mutex); |
| 1194 | if (cfg) |
| 1195 | return cfg; |
| 1196 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1197 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1198 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1199 | if (pin) |
| 1200 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1201 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1202 | if (pin) |
| 1203 | return pin->cfg; |
| 1204 | return 0; |
| 1205 | } |
| 1206 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1207 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1208 | /* remember the current pinctl target value */ |
| 1209 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1210 | unsigned int val) |
| 1211 | { |
| 1212 | struct hda_pincfg *pin; |
| 1213 | |
| 1214 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1215 | if (!pin) |
| 1216 | return -EINVAL; |
| 1217 | pin->target = val; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target); |
| 1221 | |
| 1222 | /* return the current pinctl target value */ |
| 1223 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1224 | { |
| 1225 | struct hda_pincfg *pin; |
| 1226 | |
| 1227 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1228 | if (!pin) |
| 1229 | return 0; |
| 1230 | return pin->target; |
| 1231 | } |
| 1232 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target); |
| 1233 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1234 | /** |
| 1235 | * snd_hda_shutup_pins - Shut up all pins |
| 1236 | * @codec: the HDA codec |
| 1237 | * |
| 1238 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1239 | * The controls aren't cached so that they can be resumed properly. |
| 1240 | */ |
| 1241 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1242 | { |
| 1243 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1244 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1245 | * the default pin setup at the next load of the driver |
| 1246 | */ |
| 1247 | if (codec->bus->shutdown) |
| 1248 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1249 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1250 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1251 | /* use read here for syncing after issuing each verb */ |
| 1252 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1253 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1254 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1255 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1256 | } |
| 1257 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1258 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1259 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1260 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1261 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1262 | { |
| 1263 | int i; |
| 1264 | if (!codec->pins_shutup) |
| 1265 | return; |
| 1266 | if (codec->bus->shutdown) |
| 1267 | return; |
| 1268 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1269 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1270 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1271 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1272 | pin->ctrl); |
| 1273 | } |
| 1274 | codec->pins_shutup = 0; |
| 1275 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1276 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1277 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1278 | static void hda_jackpoll_work(struct work_struct *work) |
| 1279 | { |
| 1280 | struct hda_codec *codec = |
| 1281 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1282 | |
| 1283 | snd_hda_jack_set_dirty_all(codec); |
| 1284 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1285 | |
| 1286 | if (!codec->jackpoll_interval) |
| 1287 | return; |
| 1288 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1289 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1290 | codec->jackpoll_interval); |
| 1291 | } |
| 1292 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1293 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1294 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1295 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1296 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1297 | /* release all pincfg lists */ |
| 1298 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1299 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1300 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1301 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1302 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1303 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1304 | snd_array_free(&codec->init_pins); |
| 1305 | } |
| 1306 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1307 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1308 | * audio-converter setup caches |
| 1309 | */ |
| 1310 | struct hda_cvt_setup { |
| 1311 | hda_nid_t nid; |
| 1312 | u8 stream_tag; |
| 1313 | u8 channel_id; |
| 1314 | u16 format_id; |
| 1315 | unsigned char active; /* cvt is currently used */ |
| 1316 | unsigned char dirty; /* setups should be cleared */ |
| 1317 | }; |
| 1318 | |
| 1319 | /* get or create a cache entry for the given audio converter NID */ |
| 1320 | static struct hda_cvt_setup * |
| 1321 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1322 | { |
| 1323 | struct hda_cvt_setup *p; |
| 1324 | int i; |
| 1325 | |
| 1326 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1327 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1328 | if (p->nid == nid) |
| 1329 | return p; |
| 1330 | } |
| 1331 | p = snd_array_new(&codec->cvt_setups); |
| 1332 | if (p) |
| 1333 | p->nid = nid; |
| 1334 | return p; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1338 | * Dynamic symbol binding for the codec parsers |
| 1339 | */ |
| 1340 | #ifdef MODULE |
| 1341 | #define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym)) |
| 1342 | #define unload_parser_addr(addr) symbol_put_addr(addr) |
| 1343 | #else |
| 1344 | #define load_parser_sym(sym) (sym) |
| 1345 | #define unload_parser_addr(addr) do {} while (0) |
| 1346 | #endif |
| 1347 | |
| 1348 | #define load_parser(codec, sym) \ |
| 1349 | ((codec)->parser = load_parser_sym(sym)) |
| 1350 | |
| 1351 | static void unload_parser(struct hda_codec *codec) |
| 1352 | { |
| 1353 | if (codec->parser) { |
| 1354 | unload_parser_addr(codec->parser); |
| 1355 | codec->parser = NULL; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | * codec destructor |
| 1361 | */ |
| 1362 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1363 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1364 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1366 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1367 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1368 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1369 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1370 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1371 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1372 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1374 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1375 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1376 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1377 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1378 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1380 | if (codec->patch_ops.free) |
| 1381 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1382 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1383 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1384 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1385 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1386 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1387 | kfree(codec->vendor_name); |
| 1388 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1389 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1390 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | kfree(codec); |
| 1392 | } |
| 1393 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1394 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1395 | hda_nid_t fg, unsigned int power_state); |
| 1396 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1397 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1398 | unsigned int power_state); |
| 1399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | /** |
| 1401 | * snd_hda_codec_new - create a HDA codec |
| 1402 | * @bus: the bus to assign |
| 1403 | * @codec_addr: the codec address |
| 1404 | * @codecp: the pointer to store the generated codec |
| 1405 | * |
| 1406 | * Returns 0 if successful, or a negative error code. |
| 1407 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1408 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1409 | unsigned int codec_addr, |
| 1410 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | { |
| 1412 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1413 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1414 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | int err; |
| 1416 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1417 | if (snd_BUG_ON(!bus)) |
| 1418 | return -EINVAL; |
| 1419 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1420 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1423 | snd_printk(KERN_ERR "hda_codec: " |
| 1424 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | return -EBUSY; |
| 1426 | } |
| 1427 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1428 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (codec == NULL) { |
| 1430 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1431 | return -ENOMEM; |
| 1432 | } |
| 1433 | |
| 1434 | codec->bus = bus; |
| 1435 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1436 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1437 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1438 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1439 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1440 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1441 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1442 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1443 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1444 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1445 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1446 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1447 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1448 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1449 | INIT_LIST_HEAD(&codec->conn_list); |
| 1450 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1451 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1453 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1454 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1455 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1456 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1457 | * the caller has to power down appropriatley after initialization |
| 1458 | * phase. |
| 1459 | */ |
| 1460 | hda_keep_power_on(codec); |
| 1461 | #endif |
| 1462 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1463 | if (codec->bus->modelname) { |
| 1464 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1465 | if (!codec->modelname) { |
| 1466 | snd_hda_codec_free(codec); |
| 1467 | return -ENODEV; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | list_add_tail(&codec->list, &bus->codec_list); |
| 1472 | bus->caddr_tbl[codec_addr] = codec; |
| 1473 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1474 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1475 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1476 | if (codec->vendor_id == -1) |
| 1477 | /* read again, hopefully the access method was corrected |
| 1478 | * in the last read... |
| 1479 | */ |
| 1480 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1481 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1482 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1483 | AC_PAR_SUBSYSTEM_ID); |
| 1484 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1485 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1487 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1488 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1489 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1490 | err = -ENODEV; |
| 1491 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1494 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1495 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1496 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1497 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1498 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1499 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1500 | err = read_pin_defaults(codec); |
| 1501 | if (err < 0) |
| 1502 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1503 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1504 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1505 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1506 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1507 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1510 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1511 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1512 | AC_PWRST_CLKSTOP); |
| 1513 | if (!codec->d3_stop_clk) |
| 1514 | bus->power_keep_link_on = 1; |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1515 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1516 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1517 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1518 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1519 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1520 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1521 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1522 | snd_hda_codec_proc_new(codec); |
| 1523 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1524 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1525 | |
| 1526 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1527 | codec->subsystem_id, codec->revision_id); |
| 1528 | snd_component_add(codec->bus->card, component); |
| 1529 | |
| 1530 | if (codecp) |
| 1531 | *codecp = codec; |
| 1532 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1533 | |
| 1534 | error: |
| 1535 | snd_hda_codec_free(codec); |
| 1536 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1537 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1538 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1539 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1540 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1541 | { |
| 1542 | hda_nid_t fg; |
| 1543 | int err; |
| 1544 | |
| 1545 | /* Assume the function group node does not change, |
| 1546 | * only the widget nodes may change. |
| 1547 | */ |
| 1548 | kfree(codec->wcaps); |
| 1549 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1550 | err = read_widget_caps(codec, fg); |
| 1551 | if (err < 0) { |
| 1552 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1553 | return err; |
| 1554 | } |
| 1555 | |
| 1556 | snd_array_free(&codec->init_pins); |
| 1557 | err = read_pin_defaults(codec); |
| 1558 | |
| 1559 | return err; |
| 1560 | } |
| 1561 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets); |
| 1562 | |
| 1563 | |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1564 | #ifdef CONFIG_SND_HDA_CODEC_HDMI |
| 1565 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1566 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1567 | { |
| 1568 | hda_nid_t nid = codec->start_nid; |
| 1569 | int i; |
| 1570 | |
| 1571 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1572 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1573 | switch (get_wcaps_type(wcaps)) { |
| 1574 | case AC_WID_AUD_IN: |
| 1575 | return false; /* HDMI parser supports only HDMI out */ |
| 1576 | case AC_WID_AUD_OUT: |
| 1577 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1578 | return false; |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | return true; |
| 1583 | } |
| 1584 | #else |
| 1585 | /* no HDMI codec parser support */ |
| 1586 | #define is_likely_hdmi_codec(codec) false |
| 1587 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1588 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1589 | /** |
| 1590 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1591 | * @codec: the HDA codec |
| 1592 | * |
| 1593 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1594 | * patch instance. |
| 1595 | * |
| 1596 | * Returns 0 if successful or a negative error code. |
| 1597 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1598 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1599 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1600 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1601 | int err; |
| 1602 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1603 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1604 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1605 | err = get_codec_name(codec); |
| 1606 | if (err < 0) |
| 1607 | return err; |
| 1608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1610 | if (!is_generic_config(codec) && codec->preset) |
| 1611 | patch = codec->preset->patch; |
| 1612 | if (!patch) { |
| 1613 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1614 | if (is_likely_hdmi_codec(codec)) |
| 1615 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1616 | #ifdef CONFIG_SND_HDA_GENERIC |
| 1617 | if (!patch) |
| 1618 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1619 | #endif |
| 1620 | if (!patch) { |
| 1621 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
| 1622 | return -ENODEV; |
| 1623 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1626 | err = patch(codec); |
| 1627 | if (err < 0) { |
| 1628 | unload_parser(codec); |
| 1629 | return err; |
| 1630 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1631 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1632 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1633 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1634 | if (err < 0) |
| 1635 | return err; |
| 1636 | } |
| 1637 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1638 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1639 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1640 | snprintf(codec->bus->card->mixername, |
| 1641 | sizeof(codec->bus->card->mixername), |
| 1642 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1643 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1645 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1647 | /* update the stream-id if changed */ |
| 1648 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1649 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1650 | u32 stream_tag, int channel_id) |
| 1651 | { |
| 1652 | unsigned int oldval, newval; |
| 1653 | |
| 1654 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1655 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1656 | newval = (stream_tag << 4) | channel_id; |
| 1657 | if (oldval != newval) |
| 1658 | snd_hda_codec_write(codec, nid, 0, |
| 1659 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1660 | newval); |
| 1661 | p->stream_tag = stream_tag; |
| 1662 | p->channel_id = channel_id; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | /* update the format-id if changed */ |
| 1667 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1668 | hda_nid_t nid, int format) |
| 1669 | { |
| 1670 | unsigned int oldval; |
| 1671 | |
| 1672 | if (p->format_id != format) { |
| 1673 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1674 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1675 | if (oldval != format) { |
| 1676 | msleep(1); |
| 1677 | snd_hda_codec_write(codec, nid, 0, |
| 1678 | AC_VERB_SET_STREAM_FORMAT, |
| 1679 | format); |
| 1680 | } |
| 1681 | p->format_id = format; |
| 1682 | } |
| 1683 | } |
| 1684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | /** |
| 1686 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1687 | * @codec: the CODEC to set up |
| 1688 | * @nid: the NID to set up |
| 1689 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1690 | * @channel_id: channel id to pass, zero based. |
| 1691 | * @format: stream format. |
| 1692 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1693 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1694 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | int channel_id, int format) |
| 1696 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1697 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1698 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1699 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1700 | int i; |
| 1701 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1702 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1703 | return; |
| 1704 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1705 | snd_printdd("hda_codec_setup_stream: " |
| 1706 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1708 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1709 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1710 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1711 | |
| 1712 | if (codec->pcm_format_first) |
| 1713 | update_pcm_format(codec, p, nid, format); |
| 1714 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1715 | if (!codec->pcm_format_first) |
| 1716 | update_pcm_format(codec, p, nid, format); |
| 1717 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1718 | p->active = 1; |
| 1719 | p->dirty = 0; |
| 1720 | |
| 1721 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1722 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1723 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1724 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1725 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1726 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1727 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1728 | p->dirty = 1; |
| 1729 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1732 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1734 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1735 | struct hda_cvt_setup *q); |
| 1736 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1737 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1738 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1739 | * @codec: the CODEC to clean up |
| 1740 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1741 | * @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] | 1742 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1743 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1744 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1745 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1746 | struct hda_cvt_setup *p; |
| 1747 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1748 | if (!nid) |
| 1749 | return; |
| 1750 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1751 | if (codec->no_sticky_stream) |
| 1752 | do_now = 1; |
| 1753 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1754 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1755 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1756 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1757 | /* here we just clear the active flag when do_now isn't set; |
| 1758 | * actual clean-ups will be done later in |
| 1759 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1760 | */ |
| 1761 | if (do_now) |
| 1762 | really_cleanup_stream(codec, p); |
| 1763 | else |
| 1764 | p->active = 0; |
| 1765 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1766 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1767 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1768 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1769 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1770 | struct hda_cvt_setup *q) |
| 1771 | { |
| 1772 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1773 | if (q->stream_tag || q->channel_id) |
| 1774 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1775 | if (q->format_id) |
| 1776 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1777 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1778 | memset(q, 0, sizeof(*q)); |
| 1779 | q->nid = nid; |
| 1780 | } |
| 1781 | |
| 1782 | /* clean up the all conflicting obsolete streams */ |
| 1783 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1784 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1785 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1786 | int i; |
| 1787 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1788 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1789 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1790 | struct hda_cvt_setup *p; |
| 1791 | p = snd_array_elem(&c->cvt_setups, i); |
| 1792 | if (p->dirty) |
| 1793 | really_cleanup_stream(c, p); |
| 1794 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1798 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1799 | /* clean up all streams; called from suspend */ |
| 1800 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1801 | { |
| 1802 | int i; |
| 1803 | |
| 1804 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1805 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1806 | if (p->stream_tag) |
| 1807 | really_cleanup_stream(codec, p); |
| 1808 | } |
| 1809 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1810 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | /* |
| 1813 | * amp access functions |
| 1814 | */ |
| 1815 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1816 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1817 | #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] | 1818 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1819 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1820 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1822 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
| 1824 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1825 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1826 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1828 | memset(cache, 0, sizeof(*cache)); |
| 1829 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1830 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1831 | } |
| 1832 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1833 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1834 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1835 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1839 | 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] | 1840 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1841 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1842 | u16 cur = cache->hash[idx]; |
| 1843 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
| 1845 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1846 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | if (info->key == key) |
| 1848 | return info; |
| 1849 | cur = info->next; |
| 1850 | } |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1851 | return NULL; |
| 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1854 | /* query the hash. allocate an entry if not found. */ |
| 1855 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1856 | u32 key) |
| 1857 | { |
| 1858 | struct hda_cache_head *info = get_hash(cache, key); |
| 1859 | if (!info) { |
| 1860 | u16 idx, cur; |
| 1861 | /* add a new hash entry */ |
| 1862 | info = snd_array_new(&cache->buf); |
| 1863 | if (!info) |
| 1864 | return NULL; |
| 1865 | cur = snd_array_index(&cache->buf, info); |
| 1866 | info->key = key; |
| 1867 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1868 | info->dirty = 0; |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1869 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1870 | info->next = cache->hash[idx]; |
| 1871 | cache->hash[idx] = cur; |
| 1872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | return info; |
| 1874 | } |
| 1875 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1876 | /* query and allocate an amp hash entry */ |
| 1877 | static inline struct hda_amp_info * |
| 1878 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1879 | { |
| 1880 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1881 | } |
| 1882 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1883 | /* overwrite the value with the key in the caps hash */ |
| 1884 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1885 | { |
| 1886 | struct hda_amp_info *info; |
| 1887 | |
| 1888 | mutex_lock(&codec->hash_mutex); |
| 1889 | info = get_alloc_amp_hash(codec, key); |
| 1890 | if (!info) { |
| 1891 | mutex_unlock(&codec->hash_mutex); |
| 1892 | return -EINVAL; |
| 1893 | } |
| 1894 | info->amp_caps = val; |
| 1895 | info->head.val |= INFO_AMP_CAPS; |
| 1896 | mutex_unlock(&codec->hash_mutex); |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | /* query the value from the caps hash; if not found, fetch the current |
| 1901 | * value from the given function and store in the hash |
| 1902 | */ |
| 1903 | static unsigned int |
| 1904 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1905 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1906 | { |
| 1907 | struct hda_amp_info *info; |
| 1908 | unsigned int val; |
| 1909 | |
| 1910 | mutex_lock(&codec->hash_mutex); |
| 1911 | info = get_alloc_amp_hash(codec, key); |
| 1912 | if (!info) { |
| 1913 | mutex_unlock(&codec->hash_mutex); |
| 1914 | return 0; |
| 1915 | } |
| 1916 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1917 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1918 | val = func(codec, nid, dir); |
| 1919 | write_caps_hash(codec, key, val); |
| 1920 | } else { |
| 1921 | val = info->amp_caps; |
| 1922 | mutex_unlock(&codec->hash_mutex); |
| 1923 | } |
| 1924 | return val; |
| 1925 | } |
| 1926 | |
| 1927 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1928 | int direction) |
| 1929 | { |
| 1930 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1931 | nid = codec->afg; |
| 1932 | return snd_hda_param_read(codec, nid, |
| 1933 | direction == HDA_OUTPUT ? |
| 1934 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1935 | } |
| 1936 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1937 | /** |
| 1938 | * query_amp_caps - query AMP capabilities |
| 1939 | * @codec: the HD-auio codec |
| 1940 | * @nid: the NID to query |
| 1941 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1942 | * |
| 1943 | * Query AMP capabilities for the given widget and direction. |
| 1944 | * Returns the obtained capability bits. |
| 1945 | * |
| 1946 | * When cap bits have been already read, this doesn't read again but |
| 1947 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1949 | 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] | 1950 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1951 | return query_caps_hash(codec, nid, direction, |
| 1952 | HDA_HASH_KEY(nid, direction, 0), |
| 1953 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1955 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1957 | /** |
| 1958 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1959 | * @codec: the CODEC to clean up |
| 1960 | * @nid: the NID to clean up |
| 1961 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1962 | * @caps: the capability bits to set |
| 1963 | * |
| 1964 | * Override the cached AMP caps bits value by the given one. |
| 1965 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1966 | * e.g. limit to 0dB, etc. |
| 1967 | * |
| 1968 | * Returns zero if successful or a negative error code. |
| 1969 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1970 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1971 | unsigned int caps) |
| 1972 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1973 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1974 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1975 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1976 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1977 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1978 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1979 | { |
| 1980 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1981 | } |
| 1982 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1983 | /** |
| 1984 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1985 | * @codec: the HD-auio codec |
| 1986 | * @nid: the NID to query |
| 1987 | * |
| 1988 | * Query PIN capabilities for the given widget. |
| 1989 | * Returns the obtained capability bits. |
| 1990 | * |
| 1991 | * When cap bits have been already read, this doesn't read again but |
| 1992 | * returns the cached value. |
| 1993 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1994 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1995 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1996 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1997 | read_pin_cap); |
| 1998 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1999 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 2000 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2001 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2002 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2003 | * @codec: the CODEC |
| 2004 | * @nid: the NID to override |
| 2005 | * @caps: the capability bits to set |
| 2006 | * |
| 2007 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2008 | * |
| 2009 | * Returns zero if successful or a negative error code. |
| 2010 | */ |
| 2011 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2012 | unsigned int caps) |
| 2013 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2014 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2015 | } |
| 2016 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 2017 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2018 | /* read or sync the hash value with the current value; |
| 2019 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2021 | static struct hda_amp_info * |
| 2022 | update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2023 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2025 | struct hda_amp_info *info; |
| 2026 | unsigned int parm, val = 0; |
| 2027 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2029 | retry: |
| 2030 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2031 | if (!info) |
| 2032 | return NULL; |
| 2033 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2034 | if (!val_read) { |
| 2035 | mutex_unlock(&codec->hash_mutex); |
| 2036 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2037 | parm |= direction == HDA_OUTPUT ? |
| 2038 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2039 | parm |= index; |
| 2040 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2041 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2042 | val &= 0xff; |
| 2043 | val_read = true; |
| 2044 | mutex_lock(&codec->hash_mutex); |
| 2045 | goto retry; |
| 2046 | } |
| 2047 | info->vol[ch] = val; |
| 2048 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2049 | } else if (init_only) |
| 2050 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2051 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2055 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2057 | static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2058 | hda_nid_t nid, int ch, int direction, int index, |
| 2059 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
| 2061 | u32 parm; |
| 2062 | |
| 2063 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2064 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2065 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2066 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2067 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2068 | ; /* set the zero value as a fake mute */ |
| 2069 | else |
| 2070 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2072 | } |
| 2073 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2074 | /** |
| 2075 | * snd_hda_codec_amp_read - Read AMP value |
| 2076 | * @codec: HD-audio codec |
| 2077 | * @nid: NID to read the AMP value |
| 2078 | * @ch: channel (left=0 or right=1) |
| 2079 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2080 | * @index: the index value (only for input direction) |
| 2081 | * |
| 2082 | * 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] | 2083 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2084 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2085 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2087 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2088 | unsigned int val = 0; |
| 2089 | |
| 2090 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2091 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2092 | if (info) |
| 2093 | val = info->vol[ch]; |
| 2094 | mutex_unlock(&codec->hash_mutex); |
| 2095 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2097 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2099 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2100 | int direction, int idx, int mask, int val, |
| 2101 | bool init_only) |
| 2102 | { |
| 2103 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2104 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2105 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2106 | |
| 2107 | if (snd_BUG_ON(mask & ~0xff)) |
| 2108 | mask &= 0xff; |
| 2109 | val &= mask; |
| 2110 | |
| 2111 | mutex_lock(&codec->hash_mutex); |
| 2112 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2113 | if (!info) { |
| 2114 | mutex_unlock(&codec->hash_mutex); |
| 2115 | return 0; |
| 2116 | } |
| 2117 | val |= info->vol[ch] & ~mask; |
| 2118 | if (info->vol[ch] == val) { |
| 2119 | mutex_unlock(&codec->hash_mutex); |
| 2120 | return 0; |
| 2121 | } |
| 2122 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2123 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2124 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2125 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2126 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2127 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2128 | return 1; |
| 2129 | } |
| 2130 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2131 | /** |
| 2132 | * snd_hda_codec_amp_update - update the AMP value |
| 2133 | * @codec: HD-audio codec |
| 2134 | * @nid: NID to read the AMP value |
| 2135 | * @ch: channel (left=0 or right=1) |
| 2136 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2137 | * @idx: the index value (only for input direction) |
| 2138 | * @mask: bit mask to set |
| 2139 | * @val: the bits value to set |
| 2140 | * |
| 2141 | * Update the AMP value with a bit mask. |
| 2142 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2143 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2144 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2145 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2147 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2149 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2151 | /** |
| 2152 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2153 | * @codec: HD-audio codec |
| 2154 | * @nid: NID to read the AMP value |
| 2155 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2156 | * @idx: the index value (only for input direction) |
| 2157 | * @mask: bit mask to set |
| 2158 | * @val: the bits value to set |
| 2159 | * |
| 2160 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2161 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2162 | */ |
| 2163 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2164 | int direction, int idx, int mask, int val) |
| 2165 | { |
| 2166 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2167 | |
| 2168 | if (snd_BUG_ON(mask & ~0xff)) |
| 2169 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2170 | for (ch = 0; ch < 2; ch++) |
| 2171 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2172 | idx, mask, val); |
| 2173 | return ret; |
| 2174 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2175 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2176 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2177 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2178 | * the first access. If the amp was already initialized / updated beforehand, |
| 2179 | * this does nothing. |
| 2180 | */ |
| 2181 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2182 | int dir, int idx, int mask, int val) |
| 2183 | { |
| 2184 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2185 | } |
| 2186 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); |
| 2187 | |
| 2188 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2189 | int dir, int idx, int mask, int val) |
| 2190 | { |
| 2191 | int ch, ret = 0; |
| 2192 | |
| 2193 | if (snd_BUG_ON(mask & ~0xff)) |
| 2194 | mask &= 0xff; |
| 2195 | for (ch = 0; ch < 2; ch++) |
| 2196 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2197 | idx, mask, val); |
| 2198 | return ret; |
| 2199 | } |
| 2200 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); |
| 2201 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2202 | /** |
| 2203 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2204 | * @codec: HD-audio codec |
| 2205 | * |
| 2206 | * Resume the all amp commands from the cache. |
| 2207 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2208 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2209 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2210 | int i; |
| 2211 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2212 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2213 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2214 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2215 | struct hda_amp_info *buffer; |
| 2216 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2217 | hda_nid_t nid; |
| 2218 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2219 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2220 | |
| 2221 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2222 | if (!buffer->head.dirty) |
| 2223 | continue; |
| 2224 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2225 | info = *buffer; |
| 2226 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2227 | if (!key) |
| 2228 | continue; |
| 2229 | nid = key & 0xff; |
| 2230 | idx = (key >> 16) & 0xff; |
| 2231 | dir = (key >> 24) & 0xff; |
| 2232 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2233 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2234 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2235 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2236 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2237 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2238 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2239 | } |
| 2240 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2241 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2242 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2243 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2245 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2246 | unsigned int ofs) |
| 2247 | { |
| 2248 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2249 | /* get num steps */ |
| 2250 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2251 | if (ofs < caps) |
| 2252 | caps -= ofs; |
| 2253 | return caps; |
| 2254 | } |
| 2255 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2256 | /** |
| 2257 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2258 | * |
| 2259 | * The control element is supposed to have the private_value field |
| 2260 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2261 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2262 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2263 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | { |
| 2265 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2266 | u16 nid = get_amp_nid(kcontrol); |
| 2267 | u8 chs = get_amp_channels(kcontrol); |
| 2268 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2269 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2271 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2272 | uinfo->count = chs == 3 ? 2 : 1; |
| 2273 | uinfo->value.integer.min = 0; |
| 2274 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2275 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2276 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2277 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2278 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | return -EINVAL; |
| 2280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2283 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2285 | |
| 2286 | static inline unsigned int |
| 2287 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2288 | int ch, int dir, int idx, unsigned int ofs) |
| 2289 | { |
| 2290 | unsigned int val; |
| 2291 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2292 | val &= HDA_AMP_VOLMASK; |
| 2293 | if (val >= ofs) |
| 2294 | val -= ofs; |
| 2295 | else |
| 2296 | val = 0; |
| 2297 | return val; |
| 2298 | } |
| 2299 | |
| 2300 | static inline int |
| 2301 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2302 | int ch, int dir, int idx, unsigned int ofs, |
| 2303 | unsigned int val) |
| 2304 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2305 | unsigned int maxval; |
| 2306 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2307 | if (val > 0) |
| 2308 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2309 | /* ofs = 0: raw max value */ |
| 2310 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2311 | if (val > maxval) |
| 2312 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2313 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2314 | HDA_AMP_VOLMASK, val); |
| 2315 | } |
| 2316 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2317 | /** |
| 2318 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2319 | * |
| 2320 | * The control element is supposed to have the private_value field |
| 2321 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2322 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2323 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2324 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
| 2326 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2327 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2328 | int chs = get_amp_channels(kcontrol); |
| 2329 | int dir = get_amp_direction(kcontrol); |
| 2330 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2331 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | long *valp = ucontrol->value.integer.value; |
| 2333 | |
| 2334 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2335 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2337 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2340 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2342 | /** |
| 2343 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2344 | * |
| 2345 | * The control element is supposed to have the private_value field |
| 2346 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2347 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2348 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2349 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | { |
| 2351 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2352 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2353 | int chs = get_amp_channels(kcontrol); |
| 2354 | int dir = get_amp_direction(kcontrol); |
| 2355 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2356 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | long *valp = ucontrol->value.integer.value; |
| 2358 | int change = 0; |
| 2359 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2360 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2361 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2362 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2363 | valp++; |
| 2364 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2365 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2366 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2367 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | return change; |
| 2369 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2370 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2372 | /** |
| 2373 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2374 | * |
| 2375 | * The control element is supposed to have the private_value field |
| 2376 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2377 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2378 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2379 | unsigned int size, unsigned int __user *_tlv) |
| 2380 | { |
| 2381 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2382 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2383 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2384 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2385 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2386 | u32 caps, val1, val2; |
| 2387 | |
| 2388 | if (size < 4 * sizeof(unsigned int)) |
| 2389 | return -ENOMEM; |
| 2390 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2391 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2392 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2393 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2394 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2395 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2396 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2397 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2398 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2399 | return -EFAULT; |
| 2400 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2401 | return -EFAULT; |
| 2402 | if (put_user(val1, _tlv + 2)) |
| 2403 | return -EFAULT; |
| 2404 | if (put_user(val2, _tlv + 3)) |
| 2405 | return -EFAULT; |
| 2406 | return 0; |
| 2407 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2408 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2409 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2410 | /** |
| 2411 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2412 | * @codec: HD-audio codec |
| 2413 | * @nid: NID of a reference widget |
| 2414 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2415 | * @tlv: TLV data to be stored, at least 4 elements |
| 2416 | * |
| 2417 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2418 | * obtained from the reference NID. |
| 2419 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2420 | */ |
| 2421 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2422 | unsigned int *tlv) |
| 2423 | { |
| 2424 | u32 caps; |
| 2425 | int nums, step; |
| 2426 | |
| 2427 | caps = query_amp_caps(codec, nid, dir); |
| 2428 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2429 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2430 | step = (step + 1) * 25; |
| 2431 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2432 | tlv[1] = 2 * sizeof(unsigned int); |
| 2433 | tlv[2] = -nums * step; |
| 2434 | tlv[3] = step; |
| 2435 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2436 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2437 | |
| 2438 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2439 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2440 | 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] | 2441 | { |
| 2442 | struct snd_ctl_elem_id id; |
| 2443 | memset(&id, 0, sizeof(id)); |
| 2444 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2445 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2446 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2447 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2448 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2449 | strcpy(id.name, name); |
| 2450 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2451 | } |
| 2452 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2453 | /** |
| 2454 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2455 | * @codec: HD-audio codec |
| 2456 | * @name: ctl id name string |
| 2457 | * |
| 2458 | * Get the control element with the given id string and IFACE_MIXER. |
| 2459 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2460 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2461 | const char *name) |
| 2462 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2463 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2464 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2465 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2466 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2467 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2468 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2469 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2470 | int i, idx; |
| 2471 | /* 16 ctlrs should be large enough */ |
| 2472 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2473 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2474 | return idx; |
| 2475 | } |
| 2476 | return -EBUSY; |
| 2477 | } |
| 2478 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2479 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2480 | * 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] | 2481 | * @codec: HD-audio codec |
| 2482 | * @nid: corresponding NID (optional) |
| 2483 | * @kctl: the control element to assign |
| 2484 | * |
| 2485 | * Add the given control element to an array inside the codec instance. |
| 2486 | * All control elements belonging to a codec are supposed to be added |
| 2487 | * by this function so that a proper clean-up works at the free or |
| 2488 | * reconfiguration time. |
| 2489 | * |
| 2490 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2491 | * The assignment is shown in the codec proc file. |
| 2492 | * |
| 2493 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2494 | * #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] | 2495 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2496 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2497 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2498 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2499 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2500 | { |
| 2501 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2502 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2503 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2504 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2505 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2506 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2507 | if (nid == 0) |
| 2508 | nid = get_amp_nid_(kctl->private_value); |
| 2509 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2510 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2511 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2512 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2513 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2514 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2515 | if (err < 0) |
| 2516 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2517 | item = snd_array_new(&codec->mixers); |
| 2518 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2519 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2520 | item->kctl = kctl; |
| 2521 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2522 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2523 | return 0; |
| 2524 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2525 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2526 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2527 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2528 | * snd_hda_add_nid - Assign a NID to a control element |
| 2529 | * @codec: HD-audio codec |
| 2530 | * @nid: corresponding NID (optional) |
| 2531 | * @kctl: the control element to assign |
| 2532 | * @index: index to kctl |
| 2533 | * |
| 2534 | * Add the given control element to an array inside the codec instance. |
| 2535 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2536 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2537 | */ |
| 2538 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2539 | unsigned int index, hda_nid_t nid) |
| 2540 | { |
| 2541 | struct hda_nid_item *item; |
| 2542 | |
| 2543 | if (nid > 0) { |
| 2544 | item = snd_array_new(&codec->nids); |
| 2545 | if (!item) |
| 2546 | return -ENOMEM; |
| 2547 | item->kctl = kctl; |
| 2548 | item->index = index; |
| 2549 | item->nid = nid; |
| 2550 | return 0; |
| 2551 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2552 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2553 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2554 | return -EINVAL; |
| 2555 | } |
| 2556 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2557 | |
| 2558 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2559 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2560 | * @codec: HD-audio codec |
| 2561 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2562 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2563 | { |
| 2564 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2565 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2566 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2567 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2568 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2569 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2570 | } |
| 2571 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2572 | /* pseudo device locking |
| 2573 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2574 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2575 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2576 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2577 | struct snd_card *card = bus->card; |
| 2578 | struct hda_codec *codec; |
| 2579 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2580 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2581 | if (card->shutdown) |
| 2582 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2583 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2584 | if (!list_empty(&card->ctl_files)) |
| 2585 | goto err_clear; |
| 2586 | |
| 2587 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2588 | int pcm; |
| 2589 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2590 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2591 | if (!cpcm->pcm) |
| 2592 | continue; |
| 2593 | if (cpcm->pcm->streams[0].substream_opened || |
| 2594 | cpcm->pcm->streams[1].substream_opened) |
| 2595 | goto err_clear; |
| 2596 | } |
| 2597 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2598 | spin_unlock(&card->files_lock); |
| 2599 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2600 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2601 | err_clear: |
| 2602 | card->shutdown = 0; |
| 2603 | err_unlock: |
| 2604 | spin_unlock(&card->files_lock); |
| 2605 | return -EINVAL; |
| 2606 | } |
| 2607 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); |
| 2608 | |
| 2609 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2610 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2611 | struct snd_card *card = bus->card; |
| 2612 | |
| 2613 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2614 | spin_lock(&card->files_lock); |
| 2615 | card->shutdown = 0; |
| 2616 | spin_unlock(&card->files_lock); |
| 2617 | } |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2618 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2619 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2620 | /** |
| 2621 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2622 | * @codec: HD-audio codec |
| 2623 | * |
| 2624 | * This frees the all PCM and control elements assigned to the codec, and |
| 2625 | * clears the caches and restores the pin default configurations. |
| 2626 | * |
| 2627 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2628 | * returns zero. |
| 2629 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2630 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2631 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2632 | struct hda_bus *bus = codec->bus; |
| 2633 | struct snd_card *card = bus->card; |
| 2634 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2635 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2636 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2637 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2638 | |
| 2639 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2640 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2641 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2642 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2643 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2644 | #endif |
| 2645 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2646 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2647 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2648 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2649 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2650 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2651 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2652 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2653 | } |
| 2654 | if (codec->patch_ops.free) |
| 2655 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2656 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2657 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2658 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2659 | codec->spec = NULL; |
| 2660 | free_hda_cache(&codec->amp_cache); |
| 2661 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2662 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2663 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2664 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2665 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2666 | snd_array_free(&codec->cvt_setups); |
| 2667 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2668 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2669 | codec->num_pcms = 0; |
| 2670 | codec->pcm_info = NULL; |
| 2671 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2672 | codec->slave_dig_outs = NULL; |
| 2673 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2674 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2675 | module_put(codec->owner); |
| 2676 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2677 | |
| 2678 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2679 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2680 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2681 | } |
| 2682 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2683 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2684 | |
| 2685 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2686 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2687 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2688 | { |
| 2689 | struct hda_nid_item *items; |
| 2690 | const char * const *s; |
| 2691 | int i, err; |
| 2692 | |
| 2693 | items = codec->mixers.list; |
| 2694 | for (i = 0; i < codec->mixers.used; i++) { |
| 2695 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2696 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2697 | continue; |
| 2698 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2699 | char tmpname[sizeof(sctl->id.name)]; |
| 2700 | const char *name = *s; |
| 2701 | if (suffix) { |
| 2702 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2703 | name, suffix); |
| 2704 | name = tmpname; |
| 2705 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2706 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2707 | err = func(data, sctl); |
| 2708 | if (err) |
| 2709 | return err; |
| 2710 | break; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | return 0; |
| 2715 | } |
| 2716 | |
| 2717 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2718 | { |
| 2719 | return 1; |
| 2720 | } |
| 2721 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2722 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2723 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2724 | { |
| 2725 | int _tlv[4]; |
| 2726 | const int *tlv = NULL; |
| 2727 | int val = -1; |
| 2728 | |
| 2729 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2730 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2731 | mm_segment_t fs = get_fs(); |
| 2732 | set_fs(get_ds()); |
| 2733 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2734 | tlv = _tlv; |
| 2735 | set_fs(fs); |
| 2736 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2737 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2738 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2739 | int step = tlv[3]; |
| 2740 | step &= ~TLV_DB_SCALE_MUTE; |
| 2741 | if (!step) |
| 2742 | return -1; |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2743 | if (*step_to_check && *step_to_check != step) { |
| 2744 | snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n", |
| 2745 | *step_to_check, step); |
| 2746 | return -1; |
| 2747 | } |
| 2748 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2749 | val = -tlv[2] / step; |
| 2750 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2751 | return val; |
| 2752 | } |
| 2753 | |
| 2754 | /* call kctl->put with the given value(s) */ |
| 2755 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2756 | { |
| 2757 | struct snd_ctl_elem_value *ucontrol; |
| 2758 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2759 | if (!ucontrol) |
| 2760 | return -ENOMEM; |
| 2761 | ucontrol->value.integer.value[0] = val; |
| 2762 | ucontrol->value.integer.value[1] = val; |
| 2763 | kctl->put(kctl, ucontrol); |
| 2764 | kfree(ucontrol); |
| 2765 | return 0; |
| 2766 | } |
| 2767 | |
| 2768 | /* initialize the slave volume with 0dB */ |
| 2769 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2770 | { |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2771 | int offset = get_kctl_0dB_offset(slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2772 | if (offset > 0) |
| 2773 | put_kctl_with_value(slave, offset); |
| 2774 | return 0; |
| 2775 | } |
| 2776 | |
| 2777 | /* unmute the slave */ |
| 2778 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2779 | { |
| 2780 | return put_kctl_with_value(slave, 1); |
| 2781 | } |
| 2782 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2783 | /** |
| 2784 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2785 | * @codec: HD-audio codec |
| 2786 | * @name: vmaster control name |
| 2787 | * @tlv: TLV data (optional) |
| 2788 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2789 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2790 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2791 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2792 | * |
| 2793 | * Create a virtual master control with the given name. The TLV data |
| 2794 | * must be either NULL or a valid data. |
| 2795 | * |
| 2796 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2797 | * slave control name. All controls with these names are assigned to |
| 2798 | * the new virtual master control. |
| 2799 | * |
| 2800 | * This function returns zero if successful or a negative error code. |
| 2801 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2802 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2803 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2804 | const char *suffix, bool init_slave_vol, |
| 2805 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2806 | { |
| 2807 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2808 | int err; |
| 2809 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2810 | if (ctl_ret) |
| 2811 | *ctl_ret = NULL; |
| 2812 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2813 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2814 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2815 | snd_printdd("No slave found for %s\n", name); |
| 2816 | return 0; |
| 2817 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2818 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2819 | if (!kctl) |
| 2820 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2821 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2822 | if (err < 0) |
| 2823 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2824 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2825 | err = map_slaves(codec, slaves, suffix, |
| 2826 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2827 | if (err < 0) |
| 2828 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* init with master mute & zero volume */ |
| 2831 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2832 | if (init_slave_vol) { |
| 2833 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2834 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2835 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2836 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2837 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2838 | if (ctl_ret) |
| 2839 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2840 | return 0; |
| 2841 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2842 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2843 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2844 | /* |
| 2845 | * mute-LED control using vmaster |
| 2846 | */ |
| 2847 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2848 | struct snd_ctl_elem_info *uinfo) |
| 2849 | { |
| 2850 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2851 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2852 | }; |
| 2853 | unsigned int index; |
| 2854 | |
| 2855 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2856 | uinfo->count = 1; |
| 2857 | uinfo->value.enumerated.items = 3; |
| 2858 | index = uinfo->value.enumerated.item; |
| 2859 | if (index >= 3) |
| 2860 | index = 2; |
| 2861 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2866 | struct snd_ctl_elem_value *ucontrol) |
| 2867 | { |
| 2868 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2869 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
| 2873 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2874 | struct snd_ctl_elem_value *ucontrol) |
| 2875 | { |
| 2876 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2877 | unsigned int old_mode = hook->mute_mode; |
| 2878 | |
| 2879 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2880 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2881 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2882 | if (old_mode == hook->mute_mode) |
| 2883 | return 0; |
| 2884 | snd_hda_sync_vmaster_hook(hook); |
| 2885 | return 1; |
| 2886 | } |
| 2887 | |
| 2888 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2889 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2890 | .name = "Mute-LED Mode", |
| 2891 | .info = vmaster_mute_mode_info, |
| 2892 | .get = vmaster_mute_mode_get, |
| 2893 | .put = vmaster_mute_mode_put, |
| 2894 | }; |
| 2895 | |
| 2896 | /* |
| 2897 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2898 | * "Mute-LED Mode" control is automatically created and associated with |
| 2899 | * the given hook. |
| 2900 | */ |
| 2901 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2902 | struct hda_vmaster_mute_hook *hook, |
| 2903 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2904 | { |
| 2905 | struct snd_kcontrol *kctl; |
| 2906 | |
| 2907 | if (!hook->hook || !hook->sw_kctl) |
| 2908 | return 0; |
| 2909 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2910 | hook->codec = codec; |
| 2911 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2912 | if (!expose_enum_ctl) |
| 2913 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2914 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2915 | if (!kctl) |
| 2916 | return -ENOMEM; |
| 2917 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2918 | } |
| 2919 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); |
| 2920 | |
| 2921 | /* |
| 2922 | * Call the hook with the current value for synchronization |
| 2923 | * Should be called in init callback |
| 2924 | */ |
| 2925 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2926 | { |
| 2927 | if (!hook->hook || !hook->codec) |
| 2928 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2929 | /* don't call vmaster hook in the destructor since it might have |
| 2930 | * been already destroyed |
| 2931 | */ |
| 2932 | if (hook->codec->bus->shutdown) |
| 2933 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2934 | switch (hook->mute_mode) { |
| 2935 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2936 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2937 | break; |
| 2938 | default: |
| 2939 | hook->hook(hook->codec, hook->mute_mode); |
| 2940 | break; |
| 2941 | } |
| 2942 | } |
| 2943 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); |
| 2944 | |
| 2945 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2946 | /** |
| 2947 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2948 | * |
| 2949 | * The control element is supposed to have the private_value field |
| 2950 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2951 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2952 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2953 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | { |
| 2955 | int chs = get_amp_channels(kcontrol); |
| 2956 | |
| 2957 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2958 | uinfo->count = chs == 3 ? 2 : 1; |
| 2959 | uinfo->value.integer.min = 0; |
| 2960 | uinfo->value.integer.max = 1; |
| 2961 | return 0; |
| 2962 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2963 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2965 | /** |
| 2966 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2967 | * |
| 2968 | * The control element is supposed to have the private_value field |
| 2969 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2970 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2971 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2972 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | { |
| 2974 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2975 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2976 | int chs = get_amp_channels(kcontrol); |
| 2977 | int dir = get_amp_direction(kcontrol); |
| 2978 | int idx = get_amp_index(kcontrol); |
| 2979 | long *valp = ucontrol->value.integer.value; |
| 2980 | |
| 2981 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2982 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2983 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2985 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2986 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | return 0; |
| 2988 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2989 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2991 | /** |
| 2992 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 2993 | * |
| 2994 | * The control element is supposed to have the private_value field |
| 2995 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2996 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2997 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2998 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | { |
| 3000 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3001 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3002 | int chs = get_amp_channels(kcontrol); |
| 3003 | int dir = get_amp_direction(kcontrol); |
| 3004 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | long *valp = ucontrol->value.integer.value; |
| 3006 | int change = 0; |
| 3007 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3008 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3009 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3010 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3011 | HDA_AMP_MUTE, |
| 3012 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3013 | valp++; |
| 3014 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3015 | if (chs & 2) |
| 3016 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3017 | HDA_AMP_MUTE, |
| 3018 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3019 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3020 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | return change; |
| 3022 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3023 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | |
| 3025 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3026 | * bound volume controls |
| 3027 | * |
| 3028 | * bind multiple volumes (# indices, from 0) |
| 3029 | */ |
| 3030 | |
| 3031 | #define AMP_VAL_IDX_SHIFT 19 |
| 3032 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3033 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3034 | /** |
| 3035 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 3036 | * |
| 3037 | * The control element is supposed to have the private_value field |
| 3038 | * set up via HDA_BIND_MUTE*() macros. |
| 3039 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3040 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3041 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3042 | { |
| 3043 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3044 | unsigned long pval; |
| 3045 | int err; |
| 3046 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3047 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3048 | pval = kcontrol->private_value; |
| 3049 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3050 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3051 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3052 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3053 | return err; |
| 3054 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3055 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3056 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3057 | /** |
| 3058 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 3059 | * |
| 3060 | * The control element is supposed to have the private_value field |
| 3061 | * set up via HDA_BIND_MUTE*() macros. |
| 3062 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3063 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3064 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3065 | { |
| 3066 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3067 | unsigned long pval; |
| 3068 | int i, indices, err = 0, change = 0; |
| 3069 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3070 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3071 | pval = kcontrol->private_value; |
| 3072 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3073 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3074 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3075 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3076 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3077 | if (err < 0) |
| 3078 | break; |
| 3079 | change |= err; |
| 3080 | } |
| 3081 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3082 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3083 | return err < 0 ? err : change; |
| 3084 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3085 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3086 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3087 | /** |
| 3088 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 3089 | * |
| 3090 | * The control element is supposed to have the private_value field |
| 3091 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3092 | */ |
| 3093 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3094 | struct snd_ctl_elem_info *uinfo) |
| 3095 | { |
| 3096 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3097 | struct hda_bind_ctls *c; |
| 3098 | int err; |
| 3099 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3100 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3101 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3102 | kcontrol->private_value = *c->values; |
| 3103 | err = c->ops->info(kcontrol, uinfo); |
| 3104 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3105 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3106 | return err; |
| 3107 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3108 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3109 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3110 | /** |
| 3111 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 3112 | * |
| 3113 | * The control element is supposed to have the private_value field |
| 3114 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3115 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3116 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3117 | struct snd_ctl_elem_value *ucontrol) |
| 3118 | { |
| 3119 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3120 | struct hda_bind_ctls *c; |
| 3121 | int err; |
| 3122 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3123 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3124 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3125 | kcontrol->private_value = *c->values; |
| 3126 | err = c->ops->get(kcontrol, ucontrol); |
| 3127 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3128 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3129 | return err; |
| 3130 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3131 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3132 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3133 | /** |
| 3134 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3135 | * |
| 3136 | * The control element is supposed to have the private_value field |
| 3137 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3138 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3139 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3140 | struct snd_ctl_elem_value *ucontrol) |
| 3141 | { |
| 3142 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3143 | struct hda_bind_ctls *c; |
| 3144 | unsigned long *vals; |
| 3145 | int err = 0, change = 0; |
| 3146 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3147 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3148 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3149 | for (vals = c->values; *vals; vals++) { |
| 3150 | kcontrol->private_value = *vals; |
| 3151 | err = c->ops->put(kcontrol, ucontrol); |
| 3152 | if (err < 0) |
| 3153 | break; |
| 3154 | change |= err; |
| 3155 | } |
| 3156 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3157 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3158 | return err < 0 ? err : change; |
| 3159 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3160 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3161 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3162 | /** |
| 3163 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3164 | * |
| 3165 | * The control element is supposed to have the private_value field |
| 3166 | * set up via HDA_BIND_VOL() macro. |
| 3167 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3168 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3169 | unsigned int size, unsigned int __user *tlv) |
| 3170 | { |
| 3171 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3172 | struct hda_bind_ctls *c; |
| 3173 | int err; |
| 3174 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3175 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3176 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3177 | kcontrol->private_value = *c->values; |
| 3178 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3179 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3180 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3181 | return err; |
| 3182 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3183 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3184 | |
| 3185 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3186 | .info = snd_hda_mixer_amp_volume_info, |
| 3187 | .get = snd_hda_mixer_amp_volume_get, |
| 3188 | .put = snd_hda_mixer_amp_volume_put, |
| 3189 | .tlv = snd_hda_mixer_amp_tlv |
| 3190 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3191 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3192 | |
| 3193 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3194 | .info = snd_hda_mixer_amp_switch_info, |
| 3195 | .get = snd_hda_mixer_amp_switch_get, |
| 3196 | .put = snd_hda_mixer_amp_switch_put, |
| 3197 | .tlv = snd_hda_mixer_amp_tlv |
| 3198 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3199 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3200 | |
| 3201 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | * SPDIF out controls |
| 3203 | */ |
| 3204 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3205 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3206 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | { |
| 3208 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3209 | uinfo->count = 1; |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3213 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3214 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | { |
| 3216 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3217 | IEC958_AES0_NONAUDIO | |
| 3218 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3219 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3220 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3221 | IEC958_AES1_CON_ORIGINAL; |
| 3222 | return 0; |
| 3223 | } |
| 3224 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3225 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3226 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | { |
| 3228 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3229 | IEC958_AES0_NONAUDIO | |
| 3230 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3234 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3235 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | { |
| 3237 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3238 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3239 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3241 | mutex_lock(&codec->spdif_mutex); |
| 3242 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3243 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3244 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3245 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3246 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3247 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | |
| 3249 | return 0; |
| 3250 | } |
| 3251 | |
| 3252 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3253 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3254 | */ |
| 3255 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3256 | { |
| 3257 | unsigned short val = 0; |
| 3258 | |
| 3259 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3260 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3262 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3264 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3265 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3266 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3268 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3269 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3270 | val |= AC_DIG1_EMPHASIS; |
| 3271 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3272 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3274 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3276 | } |
| 3277 | return val; |
| 3278 | } |
| 3279 | |
| 3280 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3281 | */ |
| 3282 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3283 | { |
| 3284 | unsigned int sbits = 0; |
| 3285 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3286 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3288 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3290 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3291 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3293 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3294 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3296 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3298 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3300 | sbits |= val & (0x7f << 8); |
| 3301 | } |
| 3302 | return sbits; |
| 3303 | } |
| 3304 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3305 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3306 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3307 | int verb, int val) |
| 3308 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3309 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3310 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3311 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3312 | d = codec->slave_dig_outs; |
| 3313 | if (!d) |
| 3314 | return; |
| 3315 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3316 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3320 | int dig1, int dig2) |
| 3321 | { |
| 3322 | if (dig1 != -1) |
| 3323 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3324 | if (dig2 != -1) |
| 3325 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3326 | } |
| 3327 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3328 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3329 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | { |
| 3331 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3332 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3333 | struct hda_spdif_out *spdif; |
| 3334 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | unsigned short val; |
| 3336 | int change; |
| 3337 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3338 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3339 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3340 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3341 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3343 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3344 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3345 | val = convert_from_spdif_status(spdif->status); |
| 3346 | val |= spdif->ctls & 1; |
| 3347 | change = spdif->ctls != val; |
| 3348 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3349 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3350 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3351 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | return change; |
| 3353 | } |
| 3354 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3355 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3357 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3358 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | { |
| 3360 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3361 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3362 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3364 | mutex_lock(&codec->spdif_mutex); |
| 3365 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3366 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3367 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | return 0; |
| 3369 | } |
| 3370 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3371 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3372 | int dig1, int dig2) |
| 3373 | { |
| 3374 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3375 | /* unmute amp switch (if any) */ |
| 3376 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3377 | (dig1 & AC_DIG1_ENABLE)) |
| 3378 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3379 | HDA_AMP_MUTE, 0); |
| 3380 | } |
| 3381 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3382 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3383 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | { |
| 3385 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3386 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3387 | struct hda_spdif_out *spdif; |
| 3388 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | unsigned short val; |
| 3390 | int change; |
| 3391 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3392 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3393 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3394 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3395 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3397 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3398 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3399 | spdif->ctls = val; |
| 3400 | if (change && nid != (u16)-1) |
| 3401 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3402 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | return change; |
| 3404 | } |
| 3405 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3406 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | { |
| 3408 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3409 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3410 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | .info = snd_hda_spdif_mask_info, |
| 3412 | .get = snd_hda_spdif_cmask_get, |
| 3413 | }, |
| 3414 | { |
| 3415 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3416 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3417 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | .info = snd_hda_spdif_mask_info, |
| 3419 | .get = snd_hda_spdif_pmask_get, |
| 3420 | }, |
| 3421 | { |
| 3422 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3423 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | .info = snd_hda_spdif_mask_info, |
| 3425 | .get = snd_hda_spdif_default_get, |
| 3426 | .put = snd_hda_spdif_default_put, |
| 3427 | }, |
| 3428 | { |
| 3429 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3430 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | .info = snd_hda_spdif_out_switch_info, |
| 3432 | .get = snd_hda_spdif_out_switch_get, |
| 3433 | .put = snd_hda_spdif_out_switch_put, |
| 3434 | }, |
| 3435 | { } /* end */ |
| 3436 | }; |
| 3437 | |
| 3438 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3439 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3441 | * @associated_nid: NID that new ctls associated with |
| 3442 | * @cvt_nid: converter NID |
| 3443 | * @type: HDA_PCM_TYPE_* |
| 3444 | * Creates controls related with the digital output. |
| 3445 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | * |
| 3447 | * Returns 0 if successful, or a negative error code. |
| 3448 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3449 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3450 | hda_nid_t associated_nid, |
| 3451 | hda_nid_t cvt_nid, |
| 3452 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | { |
| 3454 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3455 | struct snd_kcontrol *kctl; |
| 3456 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3457 | int idx = 0; |
| 3458 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3459 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3460 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3462 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3463 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3464 | idx = spdif_index; |
| 3465 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3466 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3467 | /* suppose a single SPDIF device */ |
| 3468 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3469 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3470 | if (!kctl) |
| 3471 | break; |
| 3472 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3473 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3474 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3475 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3476 | if (!bus->primary_dig_out_type) |
| 3477 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3478 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3479 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3480 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3481 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3482 | return -EBUSY; |
| 3483 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3484 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3485 | if (!spdif) |
| 3486 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3488 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3489 | if (!kctl) |
| 3490 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3491 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3492 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3493 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3494 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | return err; |
| 3496 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3497 | spdif->nid = cvt_nid; |
| 3498 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3499 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3500 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3503 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3505 | /* get the hda_spdif_out entry from the given NID |
| 3506 | * call within spdif_mutex lock |
| 3507 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3508 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3509 | hda_nid_t nid) |
| 3510 | { |
| 3511 | int i; |
| 3512 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3513 | struct hda_spdif_out *spdif = |
| 3514 | snd_array_elem(&codec->spdif_out, i); |
| 3515 | if (spdif->nid == nid) |
| 3516 | return spdif; |
| 3517 | } |
| 3518 | return NULL; |
| 3519 | } |
| 3520 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3521 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3522 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3523 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3524 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3525 | |
| 3526 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3527 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3528 | spdif->nid = (u16)-1; |
| 3529 | mutex_unlock(&codec->spdif_mutex); |
| 3530 | } |
| 3531 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3532 | |
| 3533 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3534 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3535 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3536 | unsigned short val; |
| 3537 | |
| 3538 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3539 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3540 | if (spdif->nid != nid) { |
| 3541 | spdif->nid = nid; |
| 3542 | val = spdif->ctls; |
| 3543 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3544 | } |
| 3545 | mutex_unlock(&codec->spdif_mutex); |
| 3546 | } |
| 3547 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3550 | * SPDIF sharing with analog output |
| 3551 | */ |
| 3552 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3553 | struct snd_ctl_elem_value *ucontrol) |
| 3554 | { |
| 3555 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3556 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3557 | return 0; |
| 3558 | } |
| 3559 | |
| 3560 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3561 | struct snd_ctl_elem_value *ucontrol) |
| 3562 | { |
| 3563 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3564 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
| 3568 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3569 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3570 | .name = "IEC958 Default PCM Playback Switch", |
| 3571 | .info = snd_ctl_boolean_mono_info, |
| 3572 | .get = spdif_share_sw_get, |
| 3573 | .put = spdif_share_sw_put, |
| 3574 | }; |
| 3575 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3576 | /** |
| 3577 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3578 | * @codec: the HDA codec |
| 3579 | * @mout: multi-out instance |
| 3580 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3581 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3582 | struct hda_multi_out *mout) |
| 3583 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3584 | struct snd_kcontrol *kctl; |
| 3585 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3586 | if (!mout->dig_out_nid) |
| 3587 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3588 | |
| 3589 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3590 | if (!kctl) |
| 3591 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3592 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3593 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3594 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3595 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3596 | |
| 3597 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | * SPDIF input |
| 3599 | */ |
| 3600 | |
| 3601 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3602 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3603 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3604 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | { |
| 3606 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3607 | |
| 3608 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3612 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3613 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | { |
| 3615 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3616 | hda_nid_t nid = kcontrol->private_value; |
| 3617 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3618 | int change; |
| 3619 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3620 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3622 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3624 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3625 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3627 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | return change; |
| 3629 | } |
| 3630 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3631 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3632 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | { |
| 3634 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3635 | hda_nid_t nid = kcontrol->private_value; |
| 3636 | unsigned short val; |
| 3637 | unsigned int sbits; |
| 3638 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3639 | 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] | 3640 | sbits = convert_to_spdif_status(val); |
| 3641 | ucontrol->value.iec958.status[0] = sbits; |
| 3642 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3643 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3644 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3648 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | { |
| 3650 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3651 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | .info = snd_hda_spdif_in_switch_info, |
| 3653 | .get = snd_hda_spdif_in_switch_get, |
| 3654 | .put = snd_hda_spdif_in_switch_put, |
| 3655 | }, |
| 3656 | { |
| 3657 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3658 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3659 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | .info = snd_hda_spdif_mask_info, |
| 3661 | .get = snd_hda_spdif_in_status_get, |
| 3662 | }, |
| 3663 | { } /* end */ |
| 3664 | }; |
| 3665 | |
| 3666 | /** |
| 3667 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3668 | * @codec: the HDA codec |
| 3669 | * @nid: audio in widget NID |
| 3670 | * |
| 3671 | * Creates controls related with the SPDIF input. |
| 3672 | * Called from each patch supporting the SPDIF in. |
| 3673 | * |
| 3674 | * Returns 0 if successful, or a negative error code. |
| 3675 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3676 | 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] | 3677 | { |
| 3678 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3679 | struct snd_kcontrol *kctl; |
| 3680 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3681 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3683 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3684 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3685 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3686 | return -EBUSY; |
| 3687 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3689 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3690 | if (!kctl) |
| 3691 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3693 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3694 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | return err; |
| 3696 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3697 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3698 | snd_hda_codec_read(codec, nid, 0, |
| 3699 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3700 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | return 0; |
| 3702 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3703 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3705 | /* |
| 3706 | * command cache |
| 3707 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3709 | /* build a 31bit cache key with the widget id and the command parameter */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3710 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3711 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3712 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3713 | |
| 3714 | /** |
| 3715 | * snd_hda_codec_write_cache - send a single command with caching |
| 3716 | * @codec: the HDA codec |
| 3717 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3718 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3719 | * @verb: the verb to send |
| 3720 | * @parm: the parameter for the verb |
| 3721 | * |
| 3722 | * Send a single command without waiting for response. |
| 3723 | * |
| 3724 | * Returns 0 if successful, or a negative error code. |
| 3725 | */ |
| 3726 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3727 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3728 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3729 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3730 | struct hda_cache_head *c; |
| 3731 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3732 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3733 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3734 | cache_only = codec->cached_write; |
| 3735 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3736 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3737 | if (err < 0) |
| 3738 | return err; |
| 3739 | } |
| 3740 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3741 | /* parm may contain the verb stuff for get/set amp */ |
| 3742 | verb = verb | (parm >> 8); |
| 3743 | parm &= 0xff; |
| 3744 | key = build_cmd_cache_key(nid, verb); |
| 3745 | mutex_lock(&codec->bus->cmd_mutex); |
| 3746 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3747 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3748 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3749 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3750 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3751 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3752 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3753 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3754 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3755 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3756 | /** |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3757 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3758 | * @codec: the HDA codec |
| 3759 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3760 | * @flags: optional bit flags |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3761 | * @verb: the verb to send |
| 3762 | * @parm: the parameter for the verb |
| 3763 | * |
| 3764 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3765 | * command if the parameter is already identical with the cached value. |
| 3766 | * If not, it sends the command and refreshes the cache. |
| 3767 | * |
| 3768 | * Returns 0 if successful, or a negative error code. |
| 3769 | */ |
| 3770 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3771 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3772 | { |
| 3773 | struct hda_cache_head *c; |
| 3774 | u32 key; |
| 3775 | |
| 3776 | /* parm may contain the verb stuff for get/set amp */ |
| 3777 | verb = verb | (parm >> 8); |
| 3778 | parm &= 0xff; |
| 3779 | key = build_cmd_cache_key(nid, verb); |
| 3780 | mutex_lock(&codec->bus->cmd_mutex); |
| 3781 | c = get_hash(&codec->cmd_cache, key); |
| 3782 | if (c && c->val == parm) { |
| 3783 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3784 | return 0; |
| 3785 | } |
| 3786 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3787 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3788 | } |
| 3789 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3790 | |
| 3791 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3792 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3793 | * @codec: HD-audio codec |
| 3794 | * |
| 3795 | * Execute all verbs recorded in the command caches to resume. |
| 3796 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3797 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3798 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3799 | int i; |
| 3800 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3801 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3802 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3803 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3804 | struct hda_cache_head *buffer; |
| 3805 | u32 key; |
| 3806 | |
| 3807 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3808 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3809 | if (!key) |
| 3810 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3811 | if (!buffer->dirty) |
| 3812 | continue; |
| 3813 | buffer->dirty = 0; |
| 3814 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3815 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3816 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3817 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3818 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3819 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3820 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3821 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3822 | |
| 3823 | /** |
| 3824 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3825 | * @codec: the HDA codec |
| 3826 | * @seq: VERB array to send |
| 3827 | * |
| 3828 | * Send the commands sequentially from the given array. |
| 3829 | * Thte commands are recorded on cache for power-save and resume. |
| 3830 | * The array must be terminated with NID=0. |
| 3831 | */ |
| 3832 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3833 | const struct hda_verb *seq) |
| 3834 | { |
| 3835 | for (; seq->nid; seq++) |
| 3836 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3837 | seq->param); |
| 3838 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3839 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3840 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3841 | /** |
| 3842 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3843 | * @codec: HD-audio codec |
| 3844 | */ |
| 3845 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3846 | { |
| 3847 | snd_hda_codec_resume_amp(codec); |
| 3848 | snd_hda_codec_resume_cache(codec); |
| 3849 | } |
| 3850 | EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache); |
| 3851 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3852 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3853 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3854 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3855 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3856 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3857 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3858 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3859 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3860 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3861 | if (!(wcaps & AC_WCAP_POWER)) |
| 3862 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3863 | if (codec->power_filter) { |
| 3864 | state = codec->power_filter(codec, nid, power_state); |
| 3865 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3866 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3867 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3868 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3869 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3870 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3871 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3872 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3873 | |
| 3874 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3875 | * supported power states check |
| 3876 | */ |
| 3877 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3878 | unsigned int power_state) |
| 3879 | { |
| 3880 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3881 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3882 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3883 | return false; |
| 3884 | if (sup & power_state) |
| 3885 | return true; |
| 3886 | else |
| 3887 | return false; |
| 3888 | } |
| 3889 | |
| 3890 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3891 | * wait until the state is reached, returns the current state |
| 3892 | */ |
| 3893 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3894 | hda_nid_t fg, |
| 3895 | unsigned int power_state) |
| 3896 | { |
| 3897 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3898 | unsigned int state, actual_state; |
| 3899 | |
| 3900 | for (;;) { |
| 3901 | state = snd_hda_codec_read(codec, fg, 0, |
| 3902 | AC_VERB_GET_POWER_STATE, 0); |
| 3903 | if (state & AC_PWRST_ERROR) |
| 3904 | break; |
| 3905 | actual_state = (state >> 4) & 0x0f; |
| 3906 | if (actual_state == power_state) |
| 3907 | break; |
| 3908 | if (time_after_eq(jiffies, end_time)) |
| 3909 | break; |
| 3910 | /* wait until the codec reachs to the target state */ |
| 3911 | msleep(1); |
| 3912 | } |
| 3913 | return state; |
| 3914 | } |
| 3915 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3916 | /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3917 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3918 | hda_nid_t nid, |
| 3919 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3920 | { |
| 3921 | if (power_state == AC_PWRST_D3 && |
| 3922 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3923 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3924 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3925 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3926 | if (eapd & 0x02) |
| 3927 | return AC_PWRST_D0; |
| 3928 | } |
| 3929 | return power_state; |
| 3930 | } |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3931 | EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3932 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3933 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3934 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3935 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3936 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3937 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3938 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3939 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3940 | int count; |
| 3941 | unsigned int state; |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3942 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3943 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3944 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3945 | if (power_state == AC_PWRST_D3) { |
| 3946 | /* transition time less than 10ms for power down */ |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 3947 | msleep(codec->epss ? 10 : 100); |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3948 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3949 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3950 | |
| 3951 | /* repeat power states setting at most 10 times*/ |
| 3952 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3953 | if (codec->patch_ops.set_power_state) |
| 3954 | codec->patch_ops.set_power_state(codec, fg, |
| 3955 | power_state); |
| 3956 | else { |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3957 | snd_hda_codec_read(codec, fg, flags, |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3958 | AC_VERB_SET_POWER_STATE, |
| 3959 | power_state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3960 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3961 | } |
| 3962 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3963 | if (!(state & AC_PWRST_ERROR)) |
| 3964 | break; |
| 3965 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3966 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3967 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3968 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3969 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3970 | /* sync power states of all widgets; |
| 3971 | * this is called at the end of codec parsing |
| 3972 | */ |
| 3973 | static void sync_power_up_states(struct hda_codec *codec) |
| 3974 | { |
| 3975 | hda_nid_t nid = codec->start_nid; |
| 3976 | int i; |
| 3977 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3978 | /* don't care if no filter is used */ |
| 3979 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3980 | return; |
| 3981 | |
| 3982 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3983 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3984 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3985 | if (!(wcaps & AC_WCAP_POWER)) |
| 3986 | continue; |
| 3987 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3988 | if (target == AC_PWRST_D0) |
| 3989 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3990 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3991 | snd_hda_codec_write(codec, nid, 0, |
| 3992 | AC_VERB_SET_POWER_STATE, target); |
| 3993 | } |
| 3994 | } |
| 3995 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3996 | #ifdef CONFIG_SND_HDA_HWDEP |
| 3997 | /* execute additional init verbs */ |
| 3998 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3999 | { |
| 4000 | if (codec->init_verbs.list) |
| 4001 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4002 | } |
| 4003 | #else |
| 4004 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4005 | #endif |
| 4006 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4007 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4008 | /* |
| 4009 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4010 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4011 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4012 | 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] | 4013 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4014 | unsigned int state; |
| 4015 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4016 | codec->in_pm = 1; |
| 4017 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4018 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4019 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4020 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4021 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4022 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4023 | if (!in_wq) |
| 4024 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4025 | spin_lock(&codec->power_lock); |
| 4026 | snd_hda_update_power_acct(codec); |
| 4027 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4028 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4029 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4030 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4031 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4032 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4033 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4034 | } |
| 4035 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4036 | /* mark all entries of cmd and amp caches dirty */ |
| 4037 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4038 | { |
| 4039 | int i; |
| 4040 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4041 | struct hda_cache_head *cmd; |
| 4042 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4043 | cmd->dirty = 1; |
| 4044 | } |
| 4045 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4046 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4047 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4048 | amp->head.dirty = 1; |
| 4049 | } |
| 4050 | } |
| 4051 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4052 | /* |
| 4053 | * kick up codec; used both from PM and power-save |
| 4054 | */ |
| 4055 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4056 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4057 | codec->in_pm = 1; |
| 4058 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4059 | hda_mark_cmd_cache_dirty(codec); |
| 4060 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4061 | /* set as if powered on for avoiding re-entering the resume |
| 4062 | * in the resume / power-save sequence |
| 4063 | */ |
| 4064 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4065 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4066 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4067 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4068 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4069 | if (codec->patch_ops.resume) |
| 4070 | codec->patch_ops.resume(codec); |
| 4071 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4072 | if (codec->patch_ops.init) |
| 4073 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4074 | snd_hda_codec_resume_amp(codec); |
| 4075 | snd_hda_codec_resume_cache(codec); |
| 4076 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4077 | |
| 4078 | if (codec->jackpoll_interval) |
| 4079 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4080 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4081 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4082 | |
| 4083 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4084 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4085 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4086 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4087 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | /** |
| 4090 | * snd_hda_build_controls - build mixer controls |
| 4091 | * @bus: the BUS |
| 4092 | * |
| 4093 | * Creates mixer controls for each codec included in the bus. |
| 4094 | * |
| 4095 | * Returns 0 if successful, otherwise a negative error code. |
| 4096 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4097 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4099 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4101 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4102 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4103 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 4104 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4105 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4106 | err = snd_hda_codec_reset(codec); |
| 4107 | if (err < 0) { |
| 4108 | printk(KERN_ERR |
| 4109 | "hda_codec: cannot revert codec\n"); |
| 4110 | return err; |
| 4111 | } |
| 4112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4114 | return 0; |
| 4115 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4116 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4118 | /* |
| 4119 | * add standard channel maps if not specified |
| 4120 | */ |
| 4121 | static int add_std_chmaps(struct hda_codec *codec) |
| 4122 | { |
| 4123 | int i, str, err; |
| 4124 | |
| 4125 | for (i = 0; i < codec->num_pcms; i++) { |
| 4126 | for (str = 0; str < 2; str++) { |
| 4127 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4128 | struct hda_pcm_stream *hinfo = |
| 4129 | &codec->pcm_info[i].stream[str]; |
| 4130 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4131 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4132 | |
| 4133 | if (codec->pcm_info[i].own_chmap) |
| 4134 | continue; |
| 4135 | if (!pcm || !hinfo->substreams) |
| 4136 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4137 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4138 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4139 | hinfo->channels_max, |
| 4140 | 0, &chmap); |
| 4141 | if (err < 0) |
| 4142 | return err; |
| 4143 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4144 | } |
| 4145 | } |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4149 | /* default channel maps for 2.1 speakers; |
| 4150 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4151 | */ |
| 4152 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4153 | { .channels = 2, |
| 4154 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4155 | { .channels = 4, |
| 4156 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4157 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4158 | { } |
| 4159 | }; |
| 4160 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4161 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4162 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4163 | { |
| 4164 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4165 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4166 | /* continue to initialize... */ |
| 4167 | if (codec->patch_ops.init) |
| 4168 | err = codec->patch_ops.init(codec); |
| 4169 | if (!err && codec->patch_ops.build_controls) |
| 4170 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4171 | if (err < 0) |
| 4172 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4173 | |
| 4174 | /* we create chmaps here instead of build_pcms */ |
| 4175 | err = add_std_chmaps(codec); |
| 4176 | if (err < 0) |
| 4177 | return err; |
| 4178 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4179 | if (codec->jackpoll_interval) |
| 4180 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4181 | else |
| 4182 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4183 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | return 0; |
| 4185 | } |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | /* |
| 4188 | * stream formats |
| 4189 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4190 | struct hda_rate_tbl { |
| 4191 | unsigned int hz; |
| 4192 | unsigned int alsa_bits; |
| 4193 | unsigned int hda_fmt; |
| 4194 | }; |
| 4195 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4196 | /* rate = base * mult / div */ |
| 4197 | #define HDA_RATE(base, mult, div) \ |
| 4198 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4199 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4200 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4201 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4203 | |
| 4204 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4205 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4206 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4207 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4208 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4209 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4210 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4211 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4212 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4213 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4214 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4215 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4216 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4217 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4218 | |
| 4219 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4220 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4221 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4222 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | }; |
| 4224 | |
| 4225 | /** |
| 4226 | * snd_hda_calc_stream_format - calculate format bitset |
| 4227 | * @rate: the sample rate |
| 4228 | * @channels: the number of channels |
| 4229 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4230 | * @maxbps: the max. bps |
| 4231 | * |
| 4232 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4233 | * |
| 4234 | * Return zero if invalid. |
| 4235 | */ |
| 4236 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4237 | unsigned int channels, |
| 4238 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4239 | unsigned int maxbps, |
| 4240 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | { |
| 4242 | int i; |
| 4243 | unsigned int val = 0; |
| 4244 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4245 | for (i = 0; rate_bits[i].hz; i++) |
| 4246 | if (rate_bits[i].hz == rate) { |
| 4247 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | break; |
| 4249 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4250 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | snd_printdd("invalid rate %d\n", rate); |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
| 4255 | if (channels == 0 || channels > 8) { |
| 4256 | snd_printdd("invalid channels %d\n", channels); |
| 4257 | return 0; |
| 4258 | } |
| 4259 | val |= channels - 1; |
| 4260 | |
| 4261 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4262 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4263 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4264 | break; |
| 4265 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4266 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4267 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | case 20: |
| 4269 | case 24: |
| 4270 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4271 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4272 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4274 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4276 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | break; |
| 4278 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4279 | snd_printdd("invalid format width %d\n", |
| 4280 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | return 0; |
| 4282 | } |
| 4283 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4284 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4285 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | return val; |
| 4288 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4289 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4291 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4292 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4293 | { |
| 4294 | unsigned int val = 0; |
| 4295 | if (nid != codec->afg && |
| 4296 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4297 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4298 | if (!val || val == -1) |
| 4299 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4300 | if (!val || val == -1) |
| 4301 | return 0; |
| 4302 | return val; |
| 4303 | } |
| 4304 | |
| 4305 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4306 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4307 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4308 | get_pcm_param); |
| 4309 | } |
| 4310 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4311 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4312 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4313 | { |
| 4314 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4315 | if (!streams || streams == -1) |
| 4316 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4317 | if (!streams || streams == -1) |
| 4318 | return 0; |
| 4319 | return streams; |
| 4320 | } |
| 4321 | |
| 4322 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4323 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4324 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4325 | get_stream_param); |
| 4326 | } |
| 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | /** |
| 4329 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4330 | * @codec: the HDA codec |
| 4331 | * @nid: NID to query |
| 4332 | * @ratesp: the pointer to store the detected rate bitflags |
| 4333 | * @formatsp: the pointer to store the detected formats |
| 4334 | * @bpsp: the pointer to store the detected format widths |
| 4335 | * |
| 4336 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4337 | * or @bsps argument is ignored. |
| 4338 | * |
| 4339 | * Returns 0 if successful, otherwise a negative error code. |
| 4340 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4341 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4343 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4344 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4346 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4347 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | |
| 4349 | if (ratesp) { |
| 4350 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4351 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4353 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4355 | if (rates == 0) { |
| 4356 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4357 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4358 | nid, val, |
| 4359 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4360 | return -EIO; |
| 4361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | *ratesp = rates; |
| 4363 | } |
| 4364 | |
| 4365 | if (formatsp || bpsp) { |
| 4366 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4367 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4369 | streams = query_stream_param(codec, nid); |
| 4370 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | |
| 4373 | bps = 0; |
| 4374 | if (streams & AC_SUPFMT_PCM) { |
| 4375 | if (val & AC_SUPPCM_BITS_8) { |
| 4376 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4377 | bps = 8; |
| 4378 | } |
| 4379 | if (val & AC_SUPPCM_BITS_16) { |
| 4380 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4381 | bps = 16; |
| 4382 | } |
| 4383 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4384 | if (val & AC_SUPPCM_BITS_32) |
| 4385 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4386 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4387 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4388 | if (val & AC_SUPPCM_BITS_24) |
| 4389 | bps = 24; |
| 4390 | else if (val & AC_SUPPCM_BITS_20) |
| 4391 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4392 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4393 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4395 | if (val & AC_SUPPCM_BITS_32) |
| 4396 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | else if (val & AC_SUPPCM_BITS_24) |
| 4398 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4399 | else if (val & AC_SUPPCM_BITS_20) |
| 4400 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | } |
| 4402 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4403 | #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4404 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4406 | if (!bps) |
| 4407 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4408 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4409 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4410 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4411 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | /* temporary hack: we have still no proper support |
| 4413 | * for the direct AC3 stream... |
| 4414 | */ |
| 4415 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4416 | bps = 8; |
| 4417 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4418 | if (formats == 0) { |
| 4419 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4420 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4421 | "streams=0x%x)\n", |
| 4422 | nid, val, |
| 4423 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4424 | streams); |
| 4425 | return -EIO; |
| 4426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | if (formatsp) |
| 4428 | *formatsp = formats; |
| 4429 | if (bpsp) |
| 4430 | *bpsp = bps; |
| 4431 | } |
| 4432 | |
| 4433 | return 0; |
| 4434 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4435 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
| 4437 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4438 | * snd_hda_is_supported_format - Check the validity of the format |
| 4439 | * @codec: HD-audio codec |
| 4440 | * @nid: NID to check |
| 4441 | * @format: the HD-audio format value to check |
| 4442 | * |
| 4443 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | * |
| 4445 | * Returns 1 if supported, 0 if not. |
| 4446 | */ |
| 4447 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4448 | unsigned int format) |
| 4449 | { |
| 4450 | int i; |
| 4451 | unsigned int val = 0, rate, stream; |
| 4452 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4453 | val = query_pcm_param(codec, nid); |
| 4454 | if (!val) |
| 4455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
| 4457 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4458 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4459 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | if (val & (1 << i)) |
| 4461 | break; |
| 4462 | return 0; |
| 4463 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4464 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | return 0; |
| 4466 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4467 | stream = query_stream_param(codec, nid); |
| 4468 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | return 0; |
| 4470 | |
| 4471 | if (stream & AC_SUPFMT_PCM) { |
| 4472 | switch (format & 0xf0) { |
| 4473 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4474 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | return 0; |
| 4476 | break; |
| 4477 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4478 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | return 0; |
| 4480 | break; |
| 4481 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4482 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | return 0; |
| 4484 | break; |
| 4485 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4486 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | return 0; |
| 4488 | break; |
| 4489 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4490 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | return 0; |
| 4492 | break; |
| 4493 | default: |
| 4494 | return 0; |
| 4495 | } |
| 4496 | } else { |
| 4497 | /* FIXME: check for float32 and AC3? */ |
| 4498 | } |
| 4499 | |
| 4500 | return 1; |
| 4501 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4502 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
| 4504 | /* |
| 4505 | * PCM stuff |
| 4506 | */ |
| 4507 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4508 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4509 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | { |
| 4511 | return 0; |
| 4512 | } |
| 4513 | |
| 4514 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4515 | struct hda_codec *codec, |
| 4516 | unsigned int stream_tag, |
| 4517 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4518 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | { |
| 4520 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4521 | return 0; |
| 4522 | } |
| 4523 | |
| 4524 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4525 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4526 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4528 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return 0; |
| 4530 | } |
| 4531 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4532 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4533 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4535 | int err; |
| 4536 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4537 | /* query support PCM information from the given NID */ |
| 4538 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4539 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4540 | info->rates ? NULL : &info->rates, |
| 4541 | info->formats ? NULL : &info->formats, |
| 4542 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4543 | if (err < 0) |
| 4544 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | } |
| 4546 | if (info->ops.open == NULL) |
| 4547 | info->ops.open = hda_pcm_default_open_close; |
| 4548 | if (info->ops.close == NULL) |
| 4549 | info->ops.close = hda_pcm_default_open_close; |
| 4550 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4551 | if (snd_BUG_ON(!info->nid)) |
| 4552 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | info->ops.prepare = hda_pcm_default_prepare; |
| 4554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4556 | if (snd_BUG_ON(!info->nid)) |
| 4557 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4559 | } |
| 4560 | return 0; |
| 4561 | } |
| 4562 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4563 | /* |
| 4564 | * codec prepare/cleanup entries |
| 4565 | */ |
| 4566 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4567 | struct hda_pcm_stream *hinfo, |
| 4568 | unsigned int stream, |
| 4569 | unsigned int format, |
| 4570 | struct snd_pcm_substream *substream) |
| 4571 | { |
| 4572 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4573 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4574 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4575 | if (ret >= 0) |
| 4576 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4577 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4578 | return ret; |
| 4579 | } |
| 4580 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 4581 | |
| 4582 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4583 | struct hda_pcm_stream *hinfo, |
| 4584 | struct snd_pcm_substream *substream) |
| 4585 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4586 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4587 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4588 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4589 | } |
| 4590 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 4591 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4592 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4593 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4594 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4595 | }; |
| 4596 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4597 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4598 | * get the empty PCM device number to assign |
| 4599 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4600 | static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4601 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4602 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4603 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4604 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4605 | */ |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4606 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4607 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4608 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4609 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4610 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4611 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4612 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4613 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4614 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4615 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4616 | return -EINVAL; |
| 4617 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4618 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4619 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4620 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4621 | if (audio_idx[type][i] >= 8) |
| 4622 | break; |
| 4623 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4624 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4625 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4626 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4627 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4628 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4629 | /* non-fixed slots starting from 10 */ |
| 4630 | for (i = 10; i < 32; i++) { |
| 4631 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4632 | return i; |
| 4633 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4634 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4635 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4636 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4637 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4638 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4639 | snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
| 4640 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4641 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4645 | * attach a new PCM stream |
| 4646 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4647 | static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4648 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4649 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4650 | struct hda_pcm_stream *info; |
| 4651 | int stream, err; |
| 4652 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4653 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4654 | return -EINVAL; |
| 4655 | for (stream = 0; stream < 2; stream++) { |
| 4656 | info = &pcm->stream[stream]; |
| 4657 | if (info->substreams) { |
| 4658 | err = set_pcm_default_values(codec, info); |
| 4659 | if (err < 0) |
| 4660 | return err; |
| 4661 | } |
| 4662 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4663 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4664 | } |
| 4665 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4666 | /* assign all PCMs of the given codec */ |
| 4667 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4668 | { |
| 4669 | unsigned int pcm; |
| 4670 | int err; |
| 4671 | |
| 4672 | if (!codec->num_pcms) { |
| 4673 | if (!codec->patch_ops.build_pcms) |
| 4674 | return 0; |
| 4675 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4676 | if (err < 0) { |
| 4677 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4678 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4679 | err = snd_hda_codec_reset(codec); |
| 4680 | if (err < 0) { |
| 4681 | printk(KERN_ERR |
| 4682 | "hda_codec: cannot revert codec\n"); |
| 4683 | return err; |
| 4684 | } |
| 4685 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4686 | } |
| 4687 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4688 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4689 | int dev; |
| 4690 | |
| 4691 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4692 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4693 | |
| 4694 | if (!cpcm->pcm) { |
| 4695 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4696 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4697 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4698 | cpcm->device = dev; |
| 4699 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4700 | if (err < 0) { |
| 4701 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4702 | "PCM stream %d for codec #%d\n", |
| 4703 | dev, codec->addr); |
| 4704 | continue; /* no fatal error */ |
| 4705 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | /** |
| 4712 | * snd_hda_build_pcms - build PCM information |
| 4713 | * @bus: the BUS |
| 4714 | * |
| 4715 | * Create PCM information for each codec included in the bus. |
| 4716 | * |
| 4717 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4718 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4719 | * to create its PCM instances. |
| 4720 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4721 | * callback. |
| 4722 | * |
| 4723 | * At least, substreams, channels_min and channels_max must be filled for |
| 4724 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4725 | * When rates and/or formats are zero, the supported values are queried |
| 4726 | * from the given nid. The nid is used also by the default ops.prepare |
| 4727 | * and ops.cleanup callbacks. |
| 4728 | * |
| 4729 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4730 | * ops.close is supposed to be called in the close callback. |
| 4731 | * ops.prepare should be called in the prepare or hw_params callback |
| 4732 | * with the proper parameters for set up. |
| 4733 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4734 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4735 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4737 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4739 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4741 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4742 | int err = snd_hda_codec_build_pcms(codec); |
| 4743 | if (err < 0) |
| 4744 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | } |
| 4746 | return 0; |
| 4747 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4748 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | /** |
| 4751 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4752 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4753 | * @num_configs: number of config enums |
| 4754 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | * @tbl: configuration table, terminated by null entries |
| 4756 | * |
| 4757 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4758 | * given configuration table. If a matching entry is found, returns its |
| 4759 | * config value (supposed to be 0 or positive). |
| 4760 | * |
| 4761 | * If no entries are matching, the function returns a negative value. |
| 4762 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4763 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4764 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4765 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4767 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4768 | int i; |
| 4769 | for (i = 0; i < num_configs; i++) { |
| 4770 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4771 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4772 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4773 | "selected\n", models[i]); |
| 4774 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4779 | if (!codec->bus->pci || !tbl) |
| 4780 | return -1; |
| 4781 | |
| 4782 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4783 | if (!tbl) |
| 4784 | return -1; |
| 4785 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4786 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4787 | char tmp[10]; |
| 4788 | const char *model = NULL; |
| 4789 | if (models) |
| 4790 | model = models[tbl->value]; |
| 4791 | if (!model) { |
| 4792 | sprintf(tmp, "#%d", tbl->value); |
| 4793 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4795 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4796 | "for config %x:%x (%s)\n", |
| 4797 | model, tbl->subvendor, tbl->subdevice, |
| 4798 | (tbl->name ? tbl->name : "Unknown device")); |
| 4799 | #endif |
| 4800 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | } |
| 4802 | return -1; |
| 4803 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4804 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | |
| 4806 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4807 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4808 | subsystem ID with the |
| 4809 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4810 | |
| 4811 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4812 | where the vendor ID of the PCI device is: |
| 4813 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4814 | and the vendor/subvendor are found only at the codec. |
| 4815 | |
| 4816 | * @codec: the HDA codec |
| 4817 | * @num_configs: number of config enums |
| 4818 | * @models: array of model name strings |
| 4819 | * @tbl: configuration table, terminated by null entries |
| 4820 | * |
| 4821 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4822 | * given configuration table. If a matching entry is found, returns its |
| 4823 | * config value (supposed to be 0 or positive). |
| 4824 | * |
| 4825 | * If no entries are matching, the function returns a negative value. |
| 4826 | */ |
| 4827 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4828 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4829 | const struct snd_pci_quirk *tbl) |
| 4830 | { |
| 4831 | const struct snd_pci_quirk *q; |
| 4832 | |
| 4833 | /* Search for codec ID */ |
| 4834 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4835 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4836 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4837 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4838 | break; |
| 4839 | } |
| 4840 | |
| 4841 | if (!q->subvendor) |
| 4842 | return -1; |
| 4843 | |
| 4844 | tbl = q; |
| 4845 | |
| 4846 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4847 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4848 | char tmp[10]; |
| 4849 | const char *model = NULL; |
| 4850 | if (models) |
| 4851 | model = models[tbl->value]; |
| 4852 | if (!model) { |
| 4853 | sprintf(tmp, "#%d", tbl->value); |
| 4854 | model = tmp; |
| 4855 | } |
| 4856 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4857 | "for config %x:%x (%s)\n", |
| 4858 | model, tbl->subvendor, tbl->subdevice, |
| 4859 | (tbl->name ? tbl->name : "Unknown device")); |
| 4860 | #endif |
| 4861 | return tbl->value; |
| 4862 | } |
| 4863 | return -1; |
| 4864 | } |
| 4865 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4866 | |
| 4867 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | * snd_hda_add_new_ctls - create controls from the array |
| 4869 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4870 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | * |
| 4872 | * This helper function creates and add new controls in the given array. |
| 4873 | * The array must be terminated with an empty entry as terminator. |
| 4874 | * |
| 4875 | * Returns 0 if successful, or a negative error code. |
| 4876 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4877 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4878 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4880 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | |
| 4882 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4883 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4884 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4885 | if (knew->iface == -1) /* skip this codec private value */ |
| 4886 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4887 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4888 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4889 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4890 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4891 | if (addr > 0) |
| 4892 | kctl->id.device = addr; |
| 4893 | if (idx > 0) |
| 4894 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4895 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4896 | if (!err) |
| 4897 | break; |
| 4898 | /* try first with another device index corresponding to |
| 4899 | * the codec addr; if it still fails (or it's the |
| 4900 | * primary codec), then try another control index |
| 4901 | */ |
| 4902 | if (!addr && codec->addr) |
| 4903 | addr = codec->addr; |
| 4904 | else if (!idx && !knew->index) { |
| 4905 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4906 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4907 | if (idx <= 0) |
| 4908 | return err; |
| 4909 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4910 | return err; |
| 4911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | } |
| 4913 | return 0; |
| 4914 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4915 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4917 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4918 | static void hda_power_work(struct work_struct *work) |
| 4919 | { |
| 4920 | struct hda_codec *codec = |
| 4921 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4922 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4923 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4924 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4925 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4926 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4927 | spin_unlock(&codec->power_lock); |
| 4928 | return; |
| 4929 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4930 | if (!codec->power_on || codec->power_count) { |
| 4931 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4932 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4933 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4934 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4935 | spin_unlock(&codec->power_lock); |
| 4936 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4937 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4938 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 4939 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4943 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4944 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4945 | codec->power_count++; |
| 4946 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4947 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4948 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4949 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4952 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4953 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4954 | { |
| 4955 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4956 | if (codec->power_on) |
| 4957 | codec->power_on_acct += delta; |
| 4958 | else |
| 4959 | codec->power_off_acct += delta; |
| 4960 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4961 | } |
| 4962 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4963 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4964 | * transition to D3 to complete. A pending D3 transition is indicated |
| 4965 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4966 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4967 | 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] | 4968 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4969 | /* Return if power_on or transitioning to power_on, unless currently |
| 4970 | * powering down. */ |
| 4971 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4972 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4973 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4974 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4975 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4976 | cancel_delayed_work_sync(&codec->power_work); |
| 4977 | |
| 4978 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4979 | /* If the power down delayed work was cancelled above before starting, |
| 4980 | * then there is no need to go through power up here. |
| 4981 | */ |
| 4982 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 4983 | if (codec->power_transition < 0) |
| 4984 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4985 | return; |
| 4986 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4987 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 4988 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4989 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4990 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4991 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4992 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4993 | spin_unlock(&codec->power_lock); |
| 4994 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4995 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4996 | |
| 4997 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4998 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4999 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5000 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5001 | #define power_save(codec) \ |
| 5002 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5003 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5004 | /* Transition to powered down */ |
| 5005 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5006 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5007 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5008 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5009 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5010 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5011 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5012 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5013 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5014 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /** |
| 5018 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5019 | * @codec: HD-audio codec |
| 5020 | * @delta: the counter delta to change |
| 5021 | * |
| 5022 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5023 | * appropriately. For the power-down, queue to the delayed action. |
| 5024 | * Passing zero to @delta means to synchronize the power state. |
| 5025 | */ |
| 5026 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5027 | { |
| 5028 | spin_lock(&codec->power_lock); |
| 5029 | codec->power_count += delta; |
| 5030 | trace_hda_power_count(codec); |
| 5031 | if (delta > 0) |
| 5032 | __snd_hda_power_up(codec, d3wait); |
| 5033 | else |
| 5034 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5035 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5036 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5037 | EXPORT_SYMBOL_HDA(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5038 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5039 | /** |
| 5040 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5041 | * @codec: HD-audio codec |
| 5042 | * @check: the object containing an AMP list and the status |
| 5043 | * @nid: NID to check / update |
| 5044 | * |
| 5045 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5046 | * check the current AMP status, and update the the power-status according |
| 5047 | * to the mute status. |
| 5048 | * |
| 5049 | * This function is supposed to be set or called from the check_power_status |
| 5050 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5051 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5052 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5053 | struct hda_loopback_check *check, |
| 5054 | hda_nid_t nid) |
| 5055 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5056 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5057 | int ch, v; |
| 5058 | |
| 5059 | if (!check->amplist) |
| 5060 | return 0; |
| 5061 | for (p = check->amplist; p->nid; p++) { |
| 5062 | if (p->nid == nid) |
| 5063 | break; |
| 5064 | } |
| 5065 | if (!p->nid) |
| 5066 | return 0; /* nothing changed */ |
| 5067 | |
| 5068 | for (p = check->amplist; p->nid; p++) { |
| 5069 | for (ch = 0; ch < 2; ch++) { |
| 5070 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5071 | p->idx); |
| 5072 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5073 | if (!check->power_on) { |
| 5074 | check->power_on = 1; |
| 5075 | snd_hda_power_up(codec); |
| 5076 | } |
| 5077 | return 1; |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | if (check->power_on) { |
| 5082 | check->power_on = 0; |
| 5083 | snd_hda_power_down(codec); |
| 5084 | } |
| 5085 | return 0; |
| 5086 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5087 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5088 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5090 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5091 | * Channel mode helper |
| 5092 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5093 | |
| 5094 | /** |
| 5095 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 5096 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5097 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5098 | struct snd_ctl_elem_info *uinfo, |
| 5099 | const struct hda_channel_mode *chmode, |
| 5100 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5101 | { |
| 5102 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5103 | uinfo->count = 1; |
| 5104 | uinfo->value.enumerated.items = num_chmodes; |
| 5105 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5106 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5107 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5108 | chmode[uinfo->value.enumerated.item].channels); |
| 5109 | return 0; |
| 5110 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5111 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5112 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5113 | /** |
| 5114 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 5115 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5116 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5117 | struct snd_ctl_elem_value *ucontrol, |
| 5118 | const struct hda_channel_mode *chmode, |
| 5119 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5120 | int max_channels) |
| 5121 | { |
| 5122 | int i; |
| 5123 | |
| 5124 | for (i = 0; i < num_chmodes; i++) { |
| 5125 | if (max_channels == chmode[i].channels) { |
| 5126 | ucontrol->value.enumerated.item[0] = i; |
| 5127 | break; |
| 5128 | } |
| 5129 | } |
| 5130 | return 0; |
| 5131 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5132 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5133 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5134 | /** |
| 5135 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 5136 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5137 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5138 | struct snd_ctl_elem_value *ucontrol, |
| 5139 | const struct hda_channel_mode *chmode, |
| 5140 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5141 | int *max_channelsp) |
| 5142 | { |
| 5143 | unsigned int mode; |
| 5144 | |
| 5145 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5146 | if (mode >= num_chmodes) |
| 5147 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5148 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5149 | return 0; |
| 5150 | /* change the current channel setting */ |
| 5151 | *max_channelsp = chmode[mode].channels; |
| 5152 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5153 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5154 | return 1; |
| 5155 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5156 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | /* |
| 5159 | * input MUX helper |
| 5160 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5161 | |
| 5162 | /** |
| 5163 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5164 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5165 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5166 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | { |
| 5168 | unsigned int index; |
| 5169 | |
| 5170 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5171 | uinfo->count = 1; |
| 5172 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5173 | if (!imux->num_items) |
| 5174 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | index = uinfo->value.enumerated.item; |
| 5176 | if (index >= imux->num_items) |
| 5177 | index = imux->num_items - 1; |
| 5178 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5179 | return 0; |
| 5180 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5181 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5183 | /** |
| 5184 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5185 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5186 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5187 | const struct hda_input_mux *imux, |
| 5188 | struct snd_ctl_elem_value *ucontrol, |
| 5189 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | unsigned int *cur_val) |
| 5191 | { |
| 5192 | unsigned int idx; |
| 5193 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5194 | if (!imux->num_items) |
| 5195 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | idx = ucontrol->value.enumerated.item[0]; |
| 5197 | if (idx >= imux->num_items) |
| 5198 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5199 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5201 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5202 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | *cur_val = idx; |
| 5204 | return 1; |
| 5205 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5206 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | |
| 5208 | |
| 5209 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5210 | * process kcontrol info callback of a simple string enum array |
| 5211 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5212 | */ |
| 5213 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5214 | struct snd_ctl_elem_info *uinfo, |
| 5215 | int num_items, const char * const *texts) |
| 5216 | { |
| 5217 | static const char * const texts_default[] = { |
| 5218 | "Disabled", "Enabled" |
| 5219 | }; |
| 5220 | |
| 5221 | if (!texts || !num_items) { |
| 5222 | num_items = 2; |
| 5223 | texts = texts_default; |
| 5224 | } |
| 5225 | |
| 5226 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5227 | uinfo->count = 1; |
| 5228 | uinfo->value.enumerated.items = num_items; |
| 5229 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5230 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5231 | strcpy(uinfo->value.enumerated.name, |
| 5232 | texts[uinfo->value.enumerated.item]); |
| 5233 | return 0; |
| 5234 | } |
| 5235 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); |
| 5236 | |
| 5237 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | * Multi-channel / digital-out PCM helper functions |
| 5239 | */ |
| 5240 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5241 | /* setup SPDIF output stream */ |
| 5242 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5243 | unsigned int stream_tag, unsigned int format) |
| 5244 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5245 | struct hda_spdif_out *spdif; |
| 5246 | unsigned int curr_fmt; |
| 5247 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5248 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5249 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5250 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5251 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5252 | reset = codec->spdif_status_reset && |
| 5253 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5254 | curr_fmt != format; |
| 5255 | |
| 5256 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5257 | updated */ |
| 5258 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5259 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5260 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5261 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5262 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5263 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5264 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5265 | for (d = codec->slave_dig_outs; *d; d++) |
| 5266 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5267 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5268 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5269 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5270 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5271 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5272 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5273 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5274 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5275 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5276 | { |
| 5277 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5278 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5279 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5280 | for (d = codec->slave_dig_outs; *d; d++) |
| 5281 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5282 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5283 | } |
| 5284 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5285 | /** |
| 5286 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5287 | * @bus: HD-audio bus |
| 5288 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5289 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5290 | { |
| 5291 | struct hda_codec *codec; |
| 5292 | |
| 5293 | if (!bus) |
| 5294 | return; |
| 5295 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5296 | if (hda_codec_is_power_on(codec) && |
| 5297 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5298 | codec->patch_ops.reboot_notify(codec); |
| 5299 | } |
| 5300 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 5301 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5302 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5303 | /** |
| 5304 | * 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] | 5305 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5306 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5307 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5309 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5310 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5311 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5312 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5314 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | return 0; |
| 5316 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5317 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5319 | /** |
| 5320 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5321 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5322 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5323 | struct hda_multi_out *mout, |
| 5324 | unsigned int stream_tag, |
| 5325 | unsigned int format, |
| 5326 | struct snd_pcm_substream *substream) |
| 5327 | { |
| 5328 | mutex_lock(&codec->spdif_mutex); |
| 5329 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5330 | mutex_unlock(&codec->spdif_mutex); |
| 5331 | return 0; |
| 5332 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5333 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5334 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5335 | /** |
| 5336 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5337 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5338 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5339 | struct hda_multi_out *mout) |
| 5340 | { |
| 5341 | mutex_lock(&codec->spdif_mutex); |
| 5342 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5343 | mutex_unlock(&codec->spdif_mutex); |
| 5344 | return 0; |
| 5345 | } |
| 5346 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 5347 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5348 | /** |
| 5349 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5351 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5352 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5354 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5356 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | return 0; |
| 5358 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5359 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5361 | /** |
| 5362 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5363 | * |
| 5364 | * Open analog outputs and set up the hw-constraints. |
| 5365 | * If the digital outputs can be opened as slave, open the digital |
| 5366 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5368 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5369 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5370 | struct snd_pcm_substream *substream, |
| 5371 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5373 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5374 | runtime->hw.channels_max = mout->max_channels; |
| 5375 | if (mout->dig_out_nid) { |
| 5376 | if (!mout->analog_rates) { |
| 5377 | mout->analog_rates = hinfo->rates; |
| 5378 | mout->analog_formats = hinfo->formats; |
| 5379 | mout->analog_maxbps = hinfo->maxbps; |
| 5380 | } else { |
| 5381 | runtime->hw.rates = mout->analog_rates; |
| 5382 | runtime->hw.formats = mout->analog_formats; |
| 5383 | hinfo->maxbps = mout->analog_maxbps; |
| 5384 | } |
| 5385 | if (!mout->spdif_rates) { |
| 5386 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5387 | &mout->spdif_rates, |
| 5388 | &mout->spdif_formats, |
| 5389 | &mout->spdif_maxbps); |
| 5390 | } |
| 5391 | mutex_lock(&codec->spdif_mutex); |
| 5392 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5393 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5394 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5395 | runtime->hw.rates &= mout->spdif_rates; |
| 5396 | runtime->hw.formats &= mout->spdif_formats; |
| 5397 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5398 | hinfo->maxbps = mout->spdif_maxbps; |
| 5399 | } else { |
| 5400 | mout->share_spdif = 0; |
| 5401 | /* FIXME: need notify? */ |
| 5402 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5403 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5404 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5407 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5408 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5409 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5411 | /** |
| 5412 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5413 | * |
| 5414 | * Set up the i/o for analog out. |
| 5415 | * 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] | 5416 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5417 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5418 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | unsigned int stream_tag, |
| 5420 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5421 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5423 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5425 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | int i; |
| 5427 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5428 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5429 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5430 | if (mout->dig_out_nid && mout->share_spdif && |
| 5431 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5433 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5434 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5435 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5437 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5438 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | } else { |
| 5440 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5441 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | } |
| 5443 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5444 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | |
| 5446 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5447 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5448 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5449 | if (!mout->no_share_stream && |
| 5450 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5452 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5453 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5454 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5455 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5456 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5457 | snd_hda_codec_setup_stream(codec, |
| 5458 | mout->hp_out_nid[i], |
| 5459 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | /* surrounds */ |
| 5462 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5463 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5464 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5465 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5466 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5467 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5468 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5470 | |
| 5471 | /* extra surrounds */ |
| 5472 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5473 | int ch = 0; |
| 5474 | if (!mout->extra_out_nid[i]) |
| 5475 | break; |
| 5476 | if (chs >= (i + 1) * 2) |
| 5477 | ch = i * 2; |
| 5478 | else if (!mout->no_share_stream) |
| 5479 | break; |
| 5480 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5481 | stream_tag, ch, format); |
| 5482 | } |
| 5483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | return 0; |
| 5485 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5486 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5488 | /** |
| 5489 | * 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] | 5490 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5491 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5492 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5494 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | int i; |
| 5496 | |
| 5497 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5498 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5500 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5501 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5502 | if (mout->hp_out_nid[i]) |
| 5503 | snd_hda_codec_cleanup_stream(codec, |
| 5504 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5505 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5506 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5507 | snd_hda_codec_cleanup_stream(codec, |
| 5508 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5509 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | 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] | 5511 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | mout->dig_out_used = 0; |
| 5513 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5514 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5517 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5519 | /** |
| 5520 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5521 | * |
| 5522 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5523 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5524 | */ |
| 5525 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5526 | { |
| 5527 | unsigned int pincap; |
| 5528 | unsigned int oldval; |
| 5529 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5530 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5531 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5532 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5533 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5534 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5535 | return AC_PINCTL_VREF_80; |
| 5536 | else if (pincap & AC_PINCAP_VREF_50) |
| 5537 | return AC_PINCTL_VREF_50; |
| 5538 | else if (pincap & AC_PINCAP_VREF_100) |
| 5539 | return AC_PINCTL_VREF_100; |
| 5540 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5541 | return AC_PINCTL_VREF_GRD; |
| 5542 | return AC_PINCTL_VREF_HIZ; |
| 5543 | } |
| 5544 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); |
| 5545 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5546 | /* correct the pin ctl value for matching with the pin cap */ |
| 5547 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5548 | hda_nid_t pin, unsigned int val) |
| 5549 | { |
| 5550 | static unsigned int cap_lists[][2] = { |
| 5551 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5552 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5553 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5554 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5555 | }; |
| 5556 | unsigned int cap; |
| 5557 | |
| 5558 | if (!val) |
| 5559 | return 0; |
| 5560 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5561 | if (!cap) |
| 5562 | return val; /* don't know what to do... */ |
| 5563 | |
| 5564 | if (val & AC_PINCTL_OUT_EN) { |
| 5565 | if (!(cap & AC_PINCAP_OUT)) |
| 5566 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5567 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5568 | val &= ~AC_PINCTL_HP_EN; |
| 5569 | } |
| 5570 | |
| 5571 | if (val & AC_PINCTL_IN_EN) { |
| 5572 | if (!(cap & AC_PINCAP_IN)) |
| 5573 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5574 | else { |
| 5575 | unsigned int vcap, vref; |
| 5576 | int i; |
| 5577 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5578 | vref = val & AC_PINCTL_VREFEN; |
| 5579 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5580 | if (vref == cap_lists[i][0] && |
| 5581 | !(vcap & cap_lists[i][1])) { |
| 5582 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5583 | vref = AC_PINCTL_VREF_HIZ; |
| 5584 | else |
| 5585 | vref = cap_lists[i + 1][0]; |
| 5586 | } |
| 5587 | } |
| 5588 | val &= ~AC_PINCTL_VREFEN; |
| 5589 | val |= vref; |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | return val; |
| 5594 | } |
| 5595 | EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl); |
| 5596 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5597 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5598 | unsigned int val, bool cached) |
| 5599 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5600 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5601 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5602 | if (cached) |
| 5603 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5604 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5605 | else |
| 5606 | return snd_hda_codec_write(codec, pin, 0, |
| 5607 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5608 | } |
| 5609 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); |
| 5610 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5611 | /** |
| 5612 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5613 | * |
| 5614 | * When the same label is used already in the existing items, the number |
| 5615 | * suffix is appended to the label. This label index number is stored |
| 5616 | * to type_idx when non-NULL pointer is given. |
| 5617 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5618 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5619 | int index, int *type_idx) |
| 5620 | { |
| 5621 | int i, label_idx = 0; |
| 5622 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5623 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5624 | return -EINVAL; |
| 5625 | } |
| 5626 | for (i = 0; i < imux->num_items; i++) { |
| 5627 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5628 | label_idx++; |
| 5629 | } |
| 5630 | if (type_idx) |
| 5631 | *type_idx = label_idx; |
| 5632 | if (label_idx > 0) |
| 5633 | snprintf(imux->items[imux->num_items].label, |
| 5634 | sizeof(imux->items[imux->num_items].label), |
| 5635 | "%s %d", label, label_idx); |
| 5636 | else |
| 5637 | strlcpy(imux->items[imux->num_items].label, label, |
| 5638 | sizeof(imux->items[imux->num_items].label)); |
| 5639 | imux->items[imux->num_items].index = index; |
| 5640 | imux->num_items++; |
| 5641 | return 0; |
| 5642 | } |
| 5643 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5644 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | #ifdef CONFIG_PM |
| 5647 | /* |
| 5648 | * power management |
| 5649 | */ |
| 5650 | |
| 5651 | /** |
| 5652 | * snd_hda_suspend - suspend the codecs |
| 5653 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | * |
| 5655 | * Returns 0 if successful. |
| 5656 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5657 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5659 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5661 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5662 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5663 | if (hda_codec_is_power_on(codec)) |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5664 | hda_call_codec_suspend(codec, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | } |
| 5666 | return 0; |
| 5667 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5668 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | |
| 5670 | /** |
| 5671 | * snd_hda_resume - resume the codecs |
| 5672 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | * |
| 5674 | * Returns 0 if successful. |
| 5675 | */ |
| 5676 | int snd_hda_resume(struct hda_bus *bus) |
| 5677 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5678 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5680 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5681 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5685 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5686 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5687 | |
| 5688 | /* |
| 5689 | * generic arrays |
| 5690 | */ |
| 5691 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5692 | /** |
| 5693 | * snd_array_new - get a new element from the given array |
| 5694 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5695 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5696 | * Get a new element from the given array. If it exceeds the |
| 5697 | * pre-allocated array size, re-allocate the array. |
| 5698 | * |
| 5699 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5700 | */ |
| 5701 | void *snd_array_new(struct snd_array *array) |
| 5702 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5703 | if (snd_BUG_ON(!array->elem_size)) |
| 5704 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5705 | if (array->used >= array->alloced) { |
| 5706 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5707 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5708 | void *nlist; |
| 5709 | if (snd_BUG_ON(num >= 4096)) |
| 5710 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5711 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5712 | if (!nlist) |
| 5713 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5714 | array->list = nlist; |
| 5715 | array->alloced = num; |
| 5716 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5717 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5718 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5719 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5720 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5721 | /** |
| 5722 | * snd_array_free - free the given array elements |
| 5723 | * @array: the array object |
| 5724 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5725 | void snd_array_free(struct snd_array *array) |
| 5726 | { |
| 5727 | kfree(array->list); |
| 5728 | array->used = 0; |
| 5729 | array->alloced = 0; |
| 5730 | array->list = NULL; |
| 5731 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5732 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5733 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5734 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5735 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5736 | * @pcm: PCM caps bits |
| 5737 | * @buf: the string buffer to write |
| 5738 | * @buflen: the max buffer length |
| 5739 | * |
| 5740 | * used by hda_proc.c and hda_eld.c |
| 5741 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5742 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5743 | { |
| 5744 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5745 | int i, j; |
| 5746 | |
| 5747 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5748 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5749 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5750 | |
| 5751 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5752 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5753 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5754 | |
| 5755 | MODULE_DESCRIPTION("HDA codec core"); |
| 5756 | MODULE_LICENSE("GPL"); |