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> |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 29 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <sound/core.h> |
| 31 | #include "hda_codec.h" |
| 32 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 33 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 35 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 37 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 38 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 39 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 41 | #define CREATE_TRACE_POINTS |
| 42 | #include "hda_trace.h" |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * vendor / preset table |
| 46 | */ |
| 47 | |
| 48 | struct hda_vendor_id { |
| 49 | unsigned int id; |
| 50 | const char *name; |
| 51 | }; |
| 52 | |
| 53 | /* codec vendor labels */ |
| 54 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 55 | { 0x1002, "ATI" }, |
Takashi Iwai | e5f1424 | 2009-07-01 18:11:44 +0200 | [diff] [blame] | 56 | { 0x1013, "Cirrus Logic" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 57 | { 0x1057, "Motorola" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 58 | { 0x1095, "Silicon Image" }, |
Takashi Iwai | 31117b7 | 2008-12-16 14:43:21 +0100 | [diff] [blame] | 59 | { 0x10de, "Nvidia" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 60 | { 0x10ec, "Realtek" }, |
Takashi Iwai | 4e01f54 | 2009-04-16 08:53:34 +0200 | [diff] [blame] | 61 | { 0x1102, "Creative" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 62 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 63 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 64 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 65 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 67 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 68 | { 0x17e8, "Chrontel" }, |
| 69 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 70 | { 0x1aec, "Wolfson Microelectronics" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { 0x434d, "C-Media" }, |
Takashi Iwai | 74c6113 | 2008-12-18 09:11:33 +0100 | [diff] [blame] | 72 | { 0x8086, "Intel" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 73 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | {} /* terminator */ |
| 75 | }; |
| 76 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 77 | static DEFINE_MUTEX(preset_mutex); |
| 78 | static LIST_HEAD(hda_preset_tables); |
| 79 | |
| 80 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 81 | { |
| 82 | mutex_lock(&preset_mutex); |
| 83 | list_add_tail(&preset->list, &hda_preset_tables); |
| 84 | mutex_unlock(&preset_mutex); |
| 85 | return 0; |
| 86 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 88 | |
| 89 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 90 | { |
| 91 | mutex_lock(&preset_mutex); |
| 92 | list_del(&preset->list); |
| 93 | mutex_unlock(&preset_mutex); |
| 94 | return 0; |
| 95 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_PM |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 99 | #define codec_in_pm(codec) ((codec)->in_pm) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 100 | static void hda_power_work(struct work_struct *work); |
| 101 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 102 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 103 | |
| 104 | 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] | 105 | { |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 106 | struct hda_bus *bus = codec->bus; |
| 107 | |
| 108 | if ((power_up && codec->pm_up_notified) || |
| 109 | (!power_up && !codec->pm_up_notified)) |
| 110 | return; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 111 | if (bus->ops.pm_notify) |
| 112 | bus->ops.pm_notify(bus, power_up); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 113 | codec->pm_up_notified = power_up; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 114 | } |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 115 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 116 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 117 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 118 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 119 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 120 | #define hda_call_pm_notify(codec, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 121 | #endif |
| 122 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 123 | /** |
| 124 | * snd_hda_get_jack_location - Give a location string of the jack |
| 125 | * @cfg: pin default config value |
| 126 | * |
| 127 | * Parse the pin default config value and returns the string of the |
| 128 | * jack location, e.g. "Rear", "Front", etc. |
| 129 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 130 | const char *snd_hda_get_jack_location(u32 cfg) |
| 131 | { |
| 132 | static char *bases[7] = { |
| 133 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 134 | }; |
| 135 | static unsigned char specials_idx[] = { |
| 136 | 0x07, 0x08, |
| 137 | 0x17, 0x18, 0x19, |
| 138 | 0x37, 0x38 |
| 139 | }; |
| 140 | static char *specials[] = { |
| 141 | "Rear Panel", "Drive Bar", |
| 142 | "Riser", "HDMI", "ATAPI", |
| 143 | "Mobile-In", "Mobile-Out" |
| 144 | }; |
| 145 | int i; |
| 146 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 147 | if ((cfg & 0x0f) < 7) |
| 148 | return bases[cfg & 0x0f]; |
| 149 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 150 | if (cfg == specials_idx[i]) |
| 151 | return specials[i]; |
| 152 | } |
| 153 | return "UNKNOWN"; |
| 154 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 155 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 156 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 157 | /** |
| 158 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 159 | * @cfg: pin default config value |
| 160 | * |
| 161 | * Parse the pin default config value and returns the string of the |
| 162 | * jack connectivity, i.e. external or internal connection. |
| 163 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 164 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 165 | { |
| 166 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 167 | |
| 168 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 169 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 171 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 172 | /** |
| 173 | * snd_hda_get_jack_type - Give a type string of the jack |
| 174 | * @cfg: pin default config value |
| 175 | * |
| 176 | * Parse the pin default config value and returns the string of the |
| 177 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 178 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 179 | const char *snd_hda_get_jack_type(u32 cfg) |
| 180 | { |
| 181 | static char *jack_types[16] = { |
| 182 | "Line Out", "Speaker", "HP Out", "CD", |
| 183 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 184 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 185 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 189 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 190 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 192 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 195 | */ |
| 196 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 197 | 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] | 198 | unsigned int verb, unsigned int parm) |
| 199 | { |
| 200 | u32 val; |
| 201 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 202 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 203 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 204 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n", |
| 205 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 206 | return ~0; |
| 207 | } |
| 208 | |
| 209 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 210 | val |= (u32)nid << 20; |
| 211 | val |= verb << 8; |
| 212 | val |= parm; |
| 213 | return val; |
| 214 | } |
| 215 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Send and receive a verb |
| 218 | */ |
| 219 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 220 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 221 | { |
| 222 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 223 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 224 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 225 | if (cmd == ~0) |
| 226 | return -1; |
| 227 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 228 | if (res) |
| 229 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 230 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 231 | snd_hda_power_up(codec); |
| 232 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 233 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 234 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 235 | for (;;) { |
| 236 | trace_hda_send_cmd(codec, cmd); |
| 237 | err = bus->ops.command(bus, cmd); |
| 238 | if (err != -EAGAIN) |
| 239 | break; |
| 240 | /* process pending verbs */ |
| 241 | bus->ops.get_response(bus, codec->addr); |
| 242 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 243 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 244 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 245 | trace_hda_get_response(codec, *res); |
| 246 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 247 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 248 | mutex_unlock(&bus->cmd_mutex); |
| 249 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 250 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 251 | if (bus->response_reset) { |
| 252 | snd_printd("hda_codec: resetting BUS due to " |
| 253 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 254 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 255 | bus->ops.bus_reset(bus); |
| 256 | } |
| 257 | goto again; |
| 258 | } |
| 259 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 260 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 261 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 262 | return err; |
| 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /** |
| 266 | * snd_hda_codec_read - send a command and get the response |
| 267 | * @codec: the HDA codec |
| 268 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 269 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * @verb: the verb to send |
| 271 | * @parm: the parameter for the verb |
| 272 | * |
| 273 | * Send a single command and read the corresponding response. |
| 274 | * |
| 275 | * Returns the obtained response value, or -1 for an error. |
| 276 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 277 | 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] | 278 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | unsigned int verb, unsigned int parm) |
| 280 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 281 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 282 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 283 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 284 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return res; |
| 286 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 287 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * snd_hda_codec_write - send a single command without waiting for response |
| 291 | * @codec: the HDA codec |
| 292 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 293 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | * @verb: the verb to send |
| 295 | * @parm: the parameter for the verb |
| 296 | * |
| 297 | * Send a single command without waiting for response. |
| 298 | * |
| 299 | * Returns 0 if successful, or a negative error code. |
| 300 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 301 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 302 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 304 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 305 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 306 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 307 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * snd_hda_sequence_write - sequence writes |
| 313 | * @codec: the HDA codec |
| 314 | * @seq: VERB array to send |
| 315 | * |
| 316 | * Send the commands sequentially from the given array. |
| 317 | * The array must be terminated with NID=0. |
| 318 | */ |
| 319 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 320 | { |
| 321 | for (; seq->nid; seq++) |
| 322 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 323 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 324 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 328 | * @codec: the HDA codec |
| 329 | * @nid: NID to parse |
| 330 | * @start_id: the pointer to store the start NID |
| 331 | * |
| 332 | * Parse the NID and store the start NID of its sub-nodes. |
| 333 | * Returns the number of sub-nodes. |
| 334 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 335 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 336 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | unsigned int parm; |
| 339 | |
| 340 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 341 | if (parm == -1) |
| 342 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | *start_id = (parm >> 16) & 0x7fff; |
| 344 | return (int)(parm & 0x7fff); |
| 345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 348 | /* connection list element */ |
| 349 | struct hda_conn_list { |
| 350 | struct list_head list; |
| 351 | int len; |
| 352 | hda_nid_t nid; |
| 353 | hda_nid_t conns[0]; |
| 354 | }; |
| 355 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 356 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 357 | static struct hda_conn_list * |
| 358 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 359 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 360 | struct hda_conn_list *p; |
| 361 | list_for_each_entry(p, &codec->conn_list, list) { |
| 362 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 363 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 364 | } |
| 365 | return NULL; |
| 366 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 367 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 368 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 369 | const hda_nid_t *list) |
| 370 | { |
| 371 | struct hda_conn_list *p; |
| 372 | |
| 373 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 374 | if (!p) |
| 375 | return -ENOMEM; |
| 376 | p->len = len; |
| 377 | p->nid = nid; |
| 378 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 379 | list_add(&p->list, &codec->conn_list); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static void remove_conn_list(struct hda_codec *codec) |
| 384 | { |
| 385 | while (!list_empty(&codec->conn_list)) { |
| 386 | struct hda_conn_list *p; |
| 387 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 388 | list_del(&p->list); |
| 389 | kfree(p); |
| 390 | } |
| 391 | } |
| 392 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 393 | /* read the connection and add to the cache */ |
| 394 | 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] | 395 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 396 | hda_nid_t list[32]; |
| 397 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 398 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 399 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 400 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 401 | if (len == -ENOSPC) { |
| 402 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 403 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 404 | if (!result) |
| 405 | return -ENOMEM; |
| 406 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 407 | } |
| 408 | if (len >= 0) |
| 409 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 410 | if (result != list) |
| 411 | kfree(result); |
| 412 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 413 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 414 | |
| 415 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 416 | * snd_hda_get_conn_list - get connection list |
| 417 | * @codec: the HDA codec |
| 418 | * @nid: NID to parse |
| 419 | * @len: number of connection list entries |
| 420 | * @listp: the pointer to store NID list |
| 421 | * |
| 422 | * Parses the connection list of the given widget and stores the pointer |
| 423 | * to the list of NIDs. |
| 424 | * |
| 425 | * Returns the number of connections, or a negative error code. |
| 426 | * |
| 427 | * Note that the returned pointer isn't protected against the list |
| 428 | * modification. If snd_hda_override_conn_list() might be called |
| 429 | * concurrently, protect with a mutex appropriately. |
| 430 | */ |
| 431 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 432 | const hda_nid_t **listp) |
| 433 | { |
| 434 | bool added = false; |
| 435 | |
| 436 | for (;;) { |
| 437 | int err; |
| 438 | const struct hda_conn_list *p; |
| 439 | |
| 440 | /* if the connection-list is already cached, read it */ |
| 441 | p = lookup_conn_list(codec, nid); |
| 442 | if (p) { |
| 443 | if (listp) |
| 444 | *listp = p->conns; |
| 445 | return p->len; |
| 446 | } |
| 447 | if (snd_BUG_ON(added)) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | err = read_and_add_raw_conns(codec, nid); |
| 451 | if (err < 0) |
| 452 | return err; |
| 453 | added = true; |
| 454 | } |
| 455 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 457 | |
| 458 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 459 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | * @codec: the HDA codec |
| 461 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 462 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * @max_conns: max. number of connections to store |
| 464 | * |
| 465 | * Parses the connection list of the given widget and stores the list |
| 466 | * of NIDs. |
| 467 | * |
| 468 | * Returns the number of connections, or a negative error code. |
| 469 | */ |
| 470 | 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] | 471 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 472 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 473 | const hda_nid_t *list; |
| 474 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 475 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 476 | if (len > 0 && conn_list) { |
| 477 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 478 | snd_printk(KERN_ERR "hda_codec: " |
| 479 | "Too many connections %d for NID 0x%x\n", |
| 480 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 481 | return -EINVAL; |
| 482 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 483 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 484 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 485 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 486 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 487 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 488 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 489 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 490 | /* return CONNLIST_LEN parameter of the given widget */ |
| 491 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 492 | { |
| 493 | unsigned int wcaps = get_wcaps(codec, nid); |
| 494 | unsigned int parm; |
| 495 | |
| 496 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 497 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 498 | return 0; |
| 499 | |
| 500 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 501 | if (parm == -1) |
| 502 | parm = 0; |
| 503 | return parm; |
| 504 | } |
| 505 | |
| 506 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 507 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 508 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 511 | /** |
| 512 | * snd_hda_get_raw_connections - copy connection list without cache |
| 513 | * @codec: the HDA codec |
| 514 | * @nid: NID to parse |
| 515 | * @conn_list: connection list array |
| 516 | * @max_conns: max. number of connections to store |
| 517 | * |
| 518 | * Like snd_hda_get_connections(), copy the connection list but without |
| 519 | * checking through the connection-list cache. |
| 520 | * Currently called only from hda_proc.c, so not exported. |
| 521 | */ |
| 522 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 523 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 526 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 528 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 529 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 531 | parm = get_num_conns(codec, nid); |
| 532 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 533 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (parm & AC_CLIST_LONG) { |
| 536 | /* long form */ |
| 537 | shift = 16; |
| 538 | num_elems = 2; |
| 539 | } else { |
| 540 | /* short form */ |
| 541 | shift = 8; |
| 542 | num_elems = 4; |
| 543 | } |
| 544 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | mask = (1 << (shift-1)) - 1; |
| 546 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 547 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | return 0; /* no connection */ |
| 549 | |
| 550 | if (conn_len == 1) { |
| 551 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 552 | parm = snd_hda_codec_read(codec, nid, 0, |
| 553 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 554 | if (parm == -1 && codec->bus->rirb_error) |
| 555 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 556 | if (conn_list) |
| 557 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | /* multi connection */ |
| 562 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 563 | prev_nid = 0; |
| 564 | for (i = 0; i < conn_len; i++) { |
| 565 | int range_val; |
| 566 | hda_nid_t val, n; |
| 567 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 568 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 569 | parm = snd_hda_codec_read(codec, nid, 0, |
| 570 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 571 | if (parm == -1 && codec->bus->rirb_error) |
| 572 | return -EIO; |
| 573 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 574 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 575 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 576 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4758fed | 2013-10-17 17:56:25 +0200 | [diff] [blame] | 577 | snd_printdd("hda_codec: " |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 578 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 579 | nid, i, parm); |
| 580 | return 0; |
| 581 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 582 | parm >>= shift; |
| 583 | if (range_val) { |
| 584 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 585 | if (!prev_nid || prev_nid >= val) { |
| 586 | snd_printk(KERN_WARNING "hda_codec: " |
| 587 | "invalid dep_range_val %x:%x\n", |
| 588 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 589 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 591 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 592 | if (conn_list) { |
| 593 | if (conns >= max_conns) |
| 594 | return -ENOSPC; |
| 595 | conn_list[conns] = n; |
| 596 | } |
| 597 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 598 | } |
| 599 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 600 | if (conn_list) { |
| 601 | if (conns >= max_conns) |
| 602 | return -ENOSPC; |
| 603 | conn_list[conns] = val; |
| 604 | } |
| 605 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 607 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | return conns; |
| 610 | } |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 613 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 614 | * @codec: the HDA codec |
| 615 | * @nid: NID to parse |
| 616 | * @len: number of connection list entries |
| 617 | * @list: the list of connection entries |
| 618 | * |
| 619 | * Add or modify the given connection-list to the cache. If the corresponding |
| 620 | * cache already exists, invalidate it and append a new one. |
| 621 | * |
| 622 | * Returns zero or a negative error code. |
| 623 | */ |
| 624 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 625 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 627 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 628 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 629 | p = lookup_conn_list(codec, nid); |
| 630 | if (p) { |
| 631 | list_del(&p->list); |
| 632 | kfree(p); |
| 633 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 634 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 635 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 637 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 638 | |
| 639 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 640 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 641 | * @codec: the HDA codec |
| 642 | * @mux: NID containing the list |
| 643 | * @nid: NID to select |
| 644 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 645 | * |
| 646 | * Parses the connection list of the widget @mux and checks whether the |
| 647 | * widget @nid is present. If it is, return the connection index. |
| 648 | * Otherwise it returns -1. |
| 649 | */ |
| 650 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 651 | hda_nid_t nid, int recursive) |
| 652 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 653 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 654 | int i, nums; |
| 655 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 656 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 657 | for (i = 0; i < nums; i++) |
| 658 | if (conn[i] == nid) |
| 659 | return i; |
| 660 | if (!recursive) |
| 661 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 662 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 663 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 664 | return -1; |
| 665 | } |
| 666 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 667 | for (i = 0; i < nums; i++) { |
| 668 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 669 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 670 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 671 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 672 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 673 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 674 | return -1; |
| 675 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 676 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 678 | |
| 679 | /* return DEVLIST_LEN parameter of the given widget */ |
| 680 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 681 | { |
| 682 | unsigned int wcaps = get_wcaps(codec, nid); |
| 683 | unsigned int parm; |
| 684 | |
| 685 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 686 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 687 | return 0; |
| 688 | |
| 689 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 690 | if (parm == -1 && codec->bus->rirb_error) |
| 691 | parm = 0; |
| 692 | return parm & AC_DEV_LIST_LEN_MASK; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * snd_hda_get_devices - copy device list without cache |
| 697 | * @codec: the HDA codec |
| 698 | * @nid: NID of the pin to parse |
| 699 | * @dev_list: device list array |
| 700 | * @max_devices: max. number of devices to store |
| 701 | * |
| 702 | * Copy the device list. This info is dynamic and so not cached. |
| 703 | * Currently called only from hda_proc.c, so not exported. |
| 704 | */ |
| 705 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 706 | u8 *dev_list, int max_devices) |
| 707 | { |
| 708 | unsigned int parm; |
| 709 | int i, dev_len, devices; |
| 710 | |
| 711 | parm = get_num_devices(codec, nid); |
| 712 | if (!parm) /* not multi-stream capable */ |
| 713 | return 0; |
| 714 | |
| 715 | dev_len = parm + 1; |
| 716 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 717 | |
| 718 | devices = 0; |
| 719 | while (devices < dev_len) { |
| 720 | parm = snd_hda_codec_read(codec, nid, 0, |
| 721 | AC_VERB_GET_DEVICE_LIST, devices); |
| 722 | if (parm == -1 && codec->bus->rirb_error) |
| 723 | break; |
| 724 | |
| 725 | for (i = 0; i < 8; i++) { |
| 726 | dev_list[devices] = (u8)parm; |
| 727 | parm >>= 4; |
| 728 | devices++; |
| 729 | if (devices >= dev_len) |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | return devices; |
| 734 | } |
| 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | /** |
| 737 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 738 | * @bus: the BUS |
| 739 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 740 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 741 | * |
| 742 | * Adds the given event to the queue. The events are processed in |
| 743 | * the workqueue asynchronously. Call this function in the interrupt |
| 744 | * hanlder when RIRB receives an unsolicited event. |
| 745 | * |
| 746 | * Returns 0 if successful, or a negative error code. |
| 747 | */ |
| 748 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 749 | { |
| 750 | struct hda_bus_unsolicited *unsol; |
| 751 | unsigned int wp; |
| 752 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 753 | if (!bus || !bus->workq) |
| 754 | return 0; |
| 755 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 756 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 757 | unsol = bus->unsol; |
| 758 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return 0; |
| 760 | |
| 761 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 762 | unsol->wp = wp; |
| 763 | |
| 764 | wp <<= 1; |
| 765 | unsol->queue[wp] = res; |
| 766 | unsol->queue[wp + 1] = res_ex; |
| 767 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 768 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | return 0; |
| 771 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 772 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 775 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 777 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 779 | struct hda_bus_unsolicited *unsol = |
| 780 | container_of(work, struct hda_bus_unsolicited, work); |
| 781 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | struct hda_codec *codec; |
| 783 | unsigned int rp, caddr, res; |
| 784 | |
| 785 | while (unsol->rp != unsol->wp) { |
| 786 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 787 | unsol->rp = rp; |
| 788 | rp <<= 1; |
| 789 | res = unsol->queue[rp]; |
| 790 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 791 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | continue; |
| 793 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 794 | if (codec && codec->patch_ops.unsol_event) |
| 795 | codec->patch_ops.unsol_event(codec, res); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * initialize unsolicited queue |
| 801 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 802 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
| 804 | struct hda_bus_unsolicited *unsol; |
| 805 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 806 | if (bus->unsol) /* already initialized */ |
| 807 | return 0; |
| 808 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 809 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 810 | if (!unsol) { |
| 811 | snd_printk(KERN_ERR "hda_codec: " |
| 812 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return -ENOMEM; |
| 814 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 815 | INIT_WORK(&unsol->work, process_unsol_events); |
| 816 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | bus->unsol = unsol; |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * destructor |
| 823 | */ |
| 824 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 825 | |
| 826 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 827 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 828 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 830 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 832 | if (bus->workq) |
| 833 | flush_workqueue(bus->workq); |
| 834 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 836 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | snd_hda_codec_free(codec); |
| 838 | } |
| 839 | if (bus->ops.private_free) |
| 840 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 841 | if (bus->workq) |
| 842 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | kfree(bus); |
| 845 | return 0; |
| 846 | } |
| 847 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 848 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
| 850 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 851 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return snd_hda_bus_free(bus); |
| 853 | } |
| 854 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 855 | #ifdef CONFIG_SND_HDA_HWDEP |
| 856 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 857 | { |
| 858 | struct hda_bus *bus = device->device_data; |
| 859 | struct hda_codec *codec; |
| 860 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 861 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 862 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | #else |
| 867 | #define snd_hda_bus_dev_register NULL |
| 868 | #endif |
| 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | /** |
| 871 | * snd_hda_bus_new - create a HDA bus |
| 872 | * @card: the card entry |
| 873 | * @temp: the template for hda_bus information |
| 874 | * @busp: the pointer to store the created bus instance |
| 875 | * |
| 876 | * Returns 0 if successful, or a negative error code. |
| 877 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 878 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 879 | const struct hda_bus_template *temp, |
| 880 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
| 882 | struct hda_bus *bus; |
| 883 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 884 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 885 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | .dev_free = snd_hda_bus_dev_free, |
| 887 | }; |
| 888 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 889 | if (snd_BUG_ON(!temp)) |
| 890 | return -EINVAL; |
| 891 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 892 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
| 894 | if (busp) |
| 895 | *busp = NULL; |
| 896 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 897 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (bus == NULL) { |
| 899 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 900 | return -ENOMEM; |
| 901 | } |
| 902 | |
| 903 | bus->card = card; |
| 904 | bus->private_data = temp->private_data; |
| 905 | bus->pci = temp->pci; |
| 906 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 907 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | bus->ops = temp->ops; |
| 909 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 910 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 911 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | INIT_LIST_HEAD(&bus->codec_list); |
| 913 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 914 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 915 | "hd-audio%d", card->number); |
| 916 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 917 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 918 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 919 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 920 | kfree(bus); |
| 921 | return -ENOMEM; |
| 922 | } |
| 923 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 924 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 925 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | snd_hda_bus_free(bus); |
| 927 | return err; |
| 928 | } |
| 929 | if (busp) |
| 930 | *busp = bus; |
| 931 | return 0; |
| 932 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 933 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 935 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 936 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 937 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 938 | #else |
| 939 | #define is_generic_config(codec) 0 |
| 940 | #endif |
| 941 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 942 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 943 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 944 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 945 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 946 | #endif |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | /* |
| 949 | * find a matching codec preset |
| 950 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 951 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 952 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 954 | struct hda_codec_preset_list *tbl; |
| 955 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 956 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 958 | again: |
| 959 | mutex_lock(&preset_mutex); |
| 960 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 961 | if (!try_module_get(tbl->owner)) { |
| 962 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 963 | continue; |
| 964 | } |
| 965 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 967 | if (preset->afg && preset->afg != codec->afg) |
| 968 | continue; |
| 969 | if (preset->mfg && preset->mfg != codec->mfg) |
| 970 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 971 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 973 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 974 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 975 | preset->rev == codec->revision_id)) { |
| 976 | mutex_unlock(&preset_mutex); |
| 977 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 981 | module_put(tbl->owner); |
| 982 | } |
| 983 | mutex_unlock(&preset_mutex); |
| 984 | |
| 985 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 986 | char name[32]; |
| 987 | if (!mod_requested) |
| 988 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 989 | codec->vendor_id); |
| 990 | else |
| 991 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 992 | (codec->vendor_id >> 16) & 0xffff); |
| 993 | request_module(name); |
| 994 | mod_requested++; |
| 995 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | } |
| 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1001 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1003 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
| 1005 | const struct hda_vendor_id *c; |
| 1006 | const char *vendor = NULL; |
| 1007 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1008 | char tmp[16]; |
| 1009 | |
| 1010 | if (codec->vendor_name) |
| 1011 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
| 1013 | for (c = hda_vendor_ids; c->id; c++) { |
| 1014 | if (c->id == vendor_id) { |
| 1015 | vendor = c->name; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1019 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1021 | vendor = tmp; |
| 1022 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1023 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1024 | if (!codec->vendor_name) |
| 1025 | return -ENOMEM; |
| 1026 | |
| 1027 | get_chip_name: |
| 1028 | if (codec->chip_name) |
| 1029 | return 0; |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1032 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1033 | else { |
| 1034 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1035 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1036 | } |
| 1037 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1038 | return -ENOMEM; |
| 1039 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1043 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1045 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1047 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | hda_nid_t nid; |
| 1049 | |
| 1050 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1051 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1052 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1053 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1054 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1055 | case AC_GRP_AUDIO_FUNCTION: |
| 1056 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1057 | codec->afg_function_id = function_id & 0xff; |
| 1058 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1059 | break; |
| 1060 | case AC_GRP_MODEM_FUNCTION: |
| 1061 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1062 | codec->mfg_function_id = function_id & 0xff; |
| 1063 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1064 | break; |
| 1065 | default: |
| 1066 | break; |
| 1067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1072 | * read widget caps for each widget and store in cache |
| 1073 | */ |
| 1074 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1075 | { |
| 1076 | int i; |
| 1077 | hda_nid_t nid; |
| 1078 | |
| 1079 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1080 | &codec->start_nid); |
| 1081 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1082 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1083 | return -ENOMEM; |
| 1084 | nid = codec->start_nid; |
| 1085 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1086 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1087 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1091 | /* read all pin default configurations and save codec->init_pins */ |
| 1092 | static int read_pin_defaults(struct hda_codec *codec) |
| 1093 | { |
| 1094 | int i; |
| 1095 | hda_nid_t nid = codec->start_nid; |
| 1096 | |
| 1097 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1098 | struct hda_pincfg *pin; |
| 1099 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1100 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1101 | if (wid_type != AC_WID_PIN) |
| 1102 | continue; |
| 1103 | pin = snd_array_new(&codec->init_pins); |
| 1104 | if (!pin) |
| 1105 | return -ENOMEM; |
| 1106 | pin->nid = nid; |
| 1107 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1108 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1109 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1110 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1111 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1112 | } |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /* look up the given pin config list and return the item matching with NID */ |
| 1117 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1118 | struct snd_array *array, |
| 1119 | hda_nid_t nid) |
| 1120 | { |
| 1121 | int i; |
| 1122 | for (i = 0; i < array->used; i++) { |
| 1123 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1124 | if (pin->nid == nid) |
| 1125 | return pin; |
| 1126 | } |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1130 | /* set the current pin config value for the given NID. |
| 1131 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1132 | */ |
| 1133 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1134 | hda_nid_t nid, unsigned int cfg) |
| 1135 | { |
| 1136 | struct hda_pincfg *pin; |
| 1137 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1138 | /* the check below may be invalid when pins are added by a fixup |
| 1139 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1140 | * for now |
| 1141 | */ |
| 1142 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1143 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1144 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1145 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1146 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1147 | pin = look_up_pincfg(codec, list, nid); |
| 1148 | if (!pin) { |
| 1149 | pin = snd_array_new(list); |
| 1150 | if (!pin) |
| 1151 | return -ENOMEM; |
| 1152 | pin->nid = nid; |
| 1153 | } |
| 1154 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1158 | /** |
| 1159 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1160 | * @codec: the HDA codec |
| 1161 | * @nid: NID to set the pin config |
| 1162 | * @cfg: the pin default config value |
| 1163 | * |
| 1164 | * Override a pin default configuration value in the cache. |
| 1165 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1166 | * priority than the real hardware value. |
| 1167 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1168 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1169 | hda_nid_t nid, unsigned int cfg) |
| 1170 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1171 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1172 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1173 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1174 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1175 | /** |
| 1176 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1177 | * @codec: the HDA codec |
| 1178 | * @nid: NID to get the pin config |
| 1179 | * |
| 1180 | * Get the current pin config value of the given pin NID. |
| 1181 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1182 | * returns the cached value. |
| 1183 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1184 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1185 | { |
| 1186 | struct hda_pincfg *pin; |
| 1187 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1188 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1189 | { |
| 1190 | unsigned int cfg = 0; |
| 1191 | mutex_lock(&codec->user_mutex); |
| 1192 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1193 | if (pin) |
| 1194 | cfg = pin->cfg; |
| 1195 | mutex_unlock(&codec->user_mutex); |
| 1196 | if (cfg) |
| 1197 | return cfg; |
| 1198 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1199 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1200 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1201 | if (pin) |
| 1202 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1203 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1204 | if (pin) |
| 1205 | return pin->cfg; |
| 1206 | return 0; |
| 1207 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1208 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1209 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1210 | /* remember the current pinctl target value */ |
| 1211 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1212 | unsigned int val) |
| 1213 | { |
| 1214 | struct hda_pincfg *pin; |
| 1215 | |
| 1216 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1217 | if (!pin) |
| 1218 | return -EINVAL; |
| 1219 | pin->target = val; |
| 1220 | return 0; |
| 1221 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1222 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1223 | |
| 1224 | /* return the current pinctl target value */ |
| 1225 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1226 | { |
| 1227 | struct hda_pincfg *pin; |
| 1228 | |
| 1229 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1230 | if (!pin) |
| 1231 | return 0; |
| 1232 | return pin->target; |
| 1233 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1234 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1235 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1236 | /** |
| 1237 | * snd_hda_shutup_pins - Shut up all pins |
| 1238 | * @codec: the HDA codec |
| 1239 | * |
| 1240 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1241 | * The controls aren't cached so that they can be resumed properly. |
| 1242 | */ |
| 1243 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1244 | { |
| 1245 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1246 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1247 | * the default pin setup at the next load of the driver |
| 1248 | */ |
| 1249 | if (codec->bus->shutdown) |
| 1250 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1251 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1252 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1253 | /* use read here for syncing after issuing each verb */ |
| 1254 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1255 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1256 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1257 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1258 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1259 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1260 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1261 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1262 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1263 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1264 | { |
| 1265 | int i; |
| 1266 | if (!codec->pins_shutup) |
| 1267 | return; |
| 1268 | if (codec->bus->shutdown) |
| 1269 | return; |
| 1270 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1271 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1272 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1273 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1274 | pin->ctrl); |
| 1275 | } |
| 1276 | codec->pins_shutup = 0; |
| 1277 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1278 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1279 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1280 | static void hda_jackpoll_work(struct work_struct *work) |
| 1281 | { |
| 1282 | struct hda_codec *codec = |
| 1283 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1284 | |
| 1285 | snd_hda_jack_set_dirty_all(codec); |
| 1286 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1287 | |
| 1288 | if (!codec->jackpoll_interval) |
| 1289 | return; |
| 1290 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1291 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1292 | codec->jackpoll_interval); |
| 1293 | } |
| 1294 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1295 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1296 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1297 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1298 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1299 | /* release all pincfg lists */ |
| 1300 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1301 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1302 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1303 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1304 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1305 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1306 | snd_array_free(&codec->init_pins); |
| 1307 | } |
| 1308 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1309 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1310 | * audio-converter setup caches |
| 1311 | */ |
| 1312 | struct hda_cvt_setup { |
| 1313 | hda_nid_t nid; |
| 1314 | u8 stream_tag; |
| 1315 | u8 channel_id; |
| 1316 | u16 format_id; |
| 1317 | unsigned char active; /* cvt is currently used */ |
| 1318 | unsigned char dirty; /* setups should be cleared */ |
| 1319 | }; |
| 1320 | |
| 1321 | /* get or create a cache entry for the given audio converter NID */ |
| 1322 | static struct hda_cvt_setup * |
| 1323 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1324 | { |
| 1325 | struct hda_cvt_setup *p; |
| 1326 | int i; |
| 1327 | |
| 1328 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1329 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1330 | if (p->nid == nid) |
| 1331 | return p; |
| 1332 | } |
| 1333 | p = snd_array_new(&codec->cvt_setups); |
| 1334 | if (p) |
| 1335 | p->nid = nid; |
| 1336 | return p; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1340 | * Dynamic symbol binding for the codec parsers |
| 1341 | */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1342 | |
| 1343 | #define load_parser(codec, sym) \ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1344 | ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1345 | |
| 1346 | static void unload_parser(struct hda_codec *codec) |
| 1347 | { |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1348 | if (codec->parser) |
| 1349 | symbol_put_addr(codec->parser); |
| 1350 | codec->parser = NULL; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | * codec destructor |
| 1355 | */ |
| 1356 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1357 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1358 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1360 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1361 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1362 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1363 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1364 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1365 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1366 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1368 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1369 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1370 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1371 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1372 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1374 | if (codec->patch_ops.free) |
| 1375 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1376 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1377 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1378 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1379 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1380 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1381 | kfree(codec->vendor_name); |
| 1382 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1383 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1384 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1385 | codec->bus->num_codecs--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | kfree(codec); |
| 1387 | } |
| 1388 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1389 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1390 | hda_nid_t fg, unsigned int power_state); |
| 1391 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1392 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1393 | unsigned int power_state); |
| 1394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | /** |
| 1396 | * snd_hda_codec_new - create a HDA codec |
| 1397 | * @bus: the bus to assign |
| 1398 | * @codec_addr: the codec address |
| 1399 | * @codecp: the pointer to store the generated codec |
| 1400 | * |
| 1401 | * Returns 0 if successful, or a negative error code. |
| 1402 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1403 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1404 | unsigned int codec_addr, |
| 1405 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { |
| 1407 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1408 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1409 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | int err; |
| 1411 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1412 | if (snd_BUG_ON(!bus)) |
| 1413 | return -EINVAL; |
| 1414 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1415 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1418 | snd_printk(KERN_ERR "hda_codec: " |
| 1419 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return -EBUSY; |
| 1421 | } |
| 1422 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1423 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | if (codec == NULL) { |
| 1425 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1426 | return -ENOMEM; |
| 1427 | } |
| 1428 | |
| 1429 | codec->bus = bus; |
| 1430 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1431 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1432 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1433 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1434 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1435 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1436 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1437 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1438 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1439 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1440 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1441 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1442 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1443 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1444 | INIT_LIST_HEAD(&codec->conn_list); |
| 1445 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1446 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1447 | codec->depop_delay = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1449 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1450 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1451 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1452 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1453 | * the caller has to power down appropriatley after initialization |
| 1454 | * phase. |
| 1455 | */ |
| 1456 | hda_keep_power_on(codec); |
| 1457 | #endif |
| 1458 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1459 | if (codec->bus->modelname) { |
| 1460 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1461 | if (!codec->modelname) { |
| 1462 | snd_hda_codec_free(codec); |
| 1463 | return -ENODEV; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1468 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | bus->caddr_tbl[codec_addr] = codec; |
| 1471 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1472 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1473 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1474 | if (codec->vendor_id == -1) |
| 1475 | /* read again, hopefully the access method was corrected |
| 1476 | * in the last read... |
| 1477 | */ |
| 1478 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1479 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1480 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1481 | AC_PAR_SUBSYSTEM_ID); |
| 1482 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1483 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1485 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1486 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1487 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1488 | err = -ENODEV; |
| 1489 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1492 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1493 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1494 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1495 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1496 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1497 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1498 | err = read_pin_defaults(codec); |
| 1499 | if (err < 0) |
| 1500 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1501 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1502 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1503 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1504 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1505 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1508 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1509 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1510 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1511 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1512 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1513 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1514 | #ifdef CONFIG_PM |
| 1515 | if (!codec->d3_stop_clk || !codec->epss) |
| 1516 | bus->power_keep_link_on = 1; |
| 1517 | #endif |
| 1518 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1519 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1520 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1521 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1522 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1523 | snd_hda_codec_proc_new(codec); |
| 1524 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1525 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1526 | |
| 1527 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1528 | codec->subsystem_id, codec->revision_id); |
| 1529 | snd_component_add(codec->bus->card, component); |
| 1530 | |
| 1531 | if (codecp) |
| 1532 | *codecp = codec; |
| 1533 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1534 | |
| 1535 | error: |
| 1536 | snd_hda_codec_free(codec); |
| 1537 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1538 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1539 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1540 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1541 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1542 | { |
| 1543 | hda_nid_t fg; |
| 1544 | int err; |
| 1545 | |
| 1546 | /* Assume the function group node does not change, |
| 1547 | * only the widget nodes may change. |
| 1548 | */ |
| 1549 | kfree(codec->wcaps); |
| 1550 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1551 | err = read_widget_caps(codec, fg); |
| 1552 | if (err < 0) { |
| 1553 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1554 | return err; |
| 1555 | } |
| 1556 | |
| 1557 | snd_array_free(&codec->init_pins); |
| 1558 | err = read_pin_defaults(codec); |
| 1559 | |
| 1560 | return err; |
| 1561 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1562 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1563 | |
| 1564 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 1565 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1566 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1567 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1568 | { |
| 1569 | hda_nid_t nid = codec->start_nid; |
| 1570 | int i; |
| 1571 | |
| 1572 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1573 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1574 | switch (get_wcaps_type(wcaps)) { |
| 1575 | case AC_WID_AUD_IN: |
| 1576 | return false; /* HDMI parser supports only HDMI out */ |
| 1577 | case AC_WID_AUD_OUT: |
| 1578 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1579 | return false; |
| 1580 | break; |
| 1581 | } |
| 1582 | } |
| 1583 | return true; |
| 1584 | } |
| 1585 | #else |
| 1586 | /* no HDMI codec parser support */ |
| 1587 | #define is_likely_hdmi_codec(codec) false |
| 1588 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1589 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1590 | /** |
| 1591 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1592 | * @codec: the HDA codec |
| 1593 | * |
| 1594 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1595 | * patch instance. |
| 1596 | * |
| 1597 | * Returns 0 if successful or a negative error code. |
| 1598 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1599 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1600 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1601 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1602 | int err; |
| 1603 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1604 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1605 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1606 | err = get_codec_name(codec); |
| 1607 | if (err < 0) |
| 1608 | return err; |
| 1609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1611 | if (!is_generic_config(codec) && codec->preset) |
| 1612 | patch = codec->preset->patch; |
| 1613 | if (!patch) { |
| 1614 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1615 | if (is_likely_hdmi_codec(codec)) { |
| 1616 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1617 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1618 | #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) |
| 1619 | patch = snd_hda_parse_hdmi_codec; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1620 | #endif |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1621 | } |
| 1622 | if (!patch) { |
| 1623 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 1624 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1625 | #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) |
| 1626 | patch = snd_hda_parse_generic_codec; |
| 1627 | #endif |
| 1628 | } |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1629 | if (!patch) { |
| 1630 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
| 1631 | return -ENODEV; |
| 1632 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1633 | } |
| 1634 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1635 | err = patch(codec); |
| 1636 | if (err < 0) { |
| 1637 | unload_parser(codec); |
| 1638 | return err; |
| 1639 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1640 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1641 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1642 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1643 | if (err < 0) |
| 1644 | return err; |
| 1645 | } |
| 1646 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1647 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1648 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1649 | snprintf(codec->bus->card->mixername, |
| 1650 | sizeof(codec->bus->card->mixername), |
| 1651 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1652 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1654 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1656 | /* update the stream-id if changed */ |
| 1657 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1658 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1659 | u32 stream_tag, int channel_id) |
| 1660 | { |
| 1661 | unsigned int oldval, newval; |
| 1662 | |
| 1663 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1664 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1665 | newval = (stream_tag << 4) | channel_id; |
| 1666 | if (oldval != newval) |
| 1667 | snd_hda_codec_write(codec, nid, 0, |
| 1668 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1669 | newval); |
| 1670 | p->stream_tag = stream_tag; |
| 1671 | p->channel_id = channel_id; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /* update the format-id if changed */ |
| 1676 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1677 | hda_nid_t nid, int format) |
| 1678 | { |
| 1679 | unsigned int oldval; |
| 1680 | |
| 1681 | if (p->format_id != format) { |
| 1682 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1683 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1684 | if (oldval != format) { |
| 1685 | msleep(1); |
| 1686 | snd_hda_codec_write(codec, nid, 0, |
| 1687 | AC_VERB_SET_STREAM_FORMAT, |
| 1688 | format); |
| 1689 | } |
| 1690 | p->format_id = format; |
| 1691 | } |
| 1692 | } |
| 1693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | /** |
| 1695 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1696 | * @codec: the CODEC to set up |
| 1697 | * @nid: the NID to set up |
| 1698 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1699 | * @channel_id: channel id to pass, zero based. |
| 1700 | * @format: stream format. |
| 1701 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1702 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1703 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | int channel_id, int format) |
| 1705 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1706 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1707 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1708 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1709 | int i; |
| 1710 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1711 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1712 | return; |
| 1713 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1714 | snd_printdd("hda_codec_setup_stream: " |
| 1715 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1717 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1718 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1719 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1720 | |
| 1721 | if (codec->pcm_format_first) |
| 1722 | update_pcm_format(codec, p, nid, format); |
| 1723 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1724 | if (!codec->pcm_format_first) |
| 1725 | update_pcm_format(codec, p, nid, format); |
| 1726 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1727 | p->active = 1; |
| 1728 | p->dirty = 0; |
| 1729 | |
| 1730 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1731 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1732 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1733 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1734 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1735 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1736 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1737 | p->dirty = 1; |
| 1738 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1741 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1743 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1744 | struct hda_cvt_setup *q); |
| 1745 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1746 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1747 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1748 | * @codec: the CODEC to clean up |
| 1749 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1750 | * @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] | 1751 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1752 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1753 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1754 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1755 | struct hda_cvt_setup *p; |
| 1756 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1757 | if (!nid) |
| 1758 | return; |
| 1759 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1760 | if (codec->no_sticky_stream) |
| 1761 | do_now = 1; |
| 1762 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1763 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1764 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1765 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1766 | /* here we just clear the active flag when do_now isn't set; |
| 1767 | * actual clean-ups will be done later in |
| 1768 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1769 | */ |
| 1770 | if (do_now) |
| 1771 | really_cleanup_stream(codec, p); |
| 1772 | else |
| 1773 | p->active = 0; |
| 1774 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1776 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1777 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1778 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1779 | struct hda_cvt_setup *q) |
| 1780 | { |
| 1781 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1782 | if (q->stream_tag || q->channel_id) |
| 1783 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1784 | if (q->format_id) |
| 1785 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1786 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1787 | memset(q, 0, sizeof(*q)); |
| 1788 | q->nid = nid; |
| 1789 | } |
| 1790 | |
| 1791 | /* clean up the all conflicting obsolete streams */ |
| 1792 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1793 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1794 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1795 | int i; |
| 1796 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1797 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1798 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1799 | struct hda_cvt_setup *p; |
| 1800 | p = snd_array_elem(&c->cvt_setups, i); |
| 1801 | if (p->dirty) |
| 1802 | really_cleanup_stream(c, p); |
| 1803 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1807 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1808 | /* clean up all streams; called from suspend */ |
| 1809 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1810 | { |
| 1811 | int i; |
| 1812 | |
| 1813 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1814 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1815 | if (p->stream_tag) |
| 1816 | really_cleanup_stream(codec, p); |
| 1817 | } |
| 1818 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1819 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | /* |
| 1822 | * amp access functions |
| 1823 | */ |
| 1824 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1825 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1826 | #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] | 1827 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1828 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1829 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1831 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | |
| 1833 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1834 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1835 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1837 | memset(cache, 0, sizeof(*cache)); |
| 1838 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1839 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1842 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1843 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1844 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1848 | 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] | 1849 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1850 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1851 | u16 cur = cache->hash[idx]; |
| 1852 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
| 1854 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1855 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | if (info->key == key) |
| 1857 | return info; |
| 1858 | cur = info->next; |
| 1859 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1860 | return NULL; |
| 1861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1863 | /* query the hash. allocate an entry if not found. */ |
| 1864 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1865 | u32 key) |
| 1866 | { |
| 1867 | struct hda_cache_head *info = get_hash(cache, key); |
| 1868 | if (!info) { |
| 1869 | u16 idx, cur; |
| 1870 | /* add a new hash entry */ |
| 1871 | info = snd_array_new(&cache->buf); |
| 1872 | if (!info) |
| 1873 | return NULL; |
| 1874 | cur = snd_array_index(&cache->buf, info); |
| 1875 | info->key = key; |
| 1876 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1877 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1878 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1879 | info->next = cache->hash[idx]; |
| 1880 | cache->hash[idx] = cur; |
| 1881 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | return info; |
| 1883 | } |
| 1884 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1885 | /* query and allocate an amp hash entry */ |
| 1886 | static inline struct hda_amp_info * |
| 1887 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1888 | { |
| 1889 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1890 | } |
| 1891 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1892 | /* overwrite the value with the key in the caps hash */ |
| 1893 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1894 | { |
| 1895 | struct hda_amp_info *info; |
| 1896 | |
| 1897 | mutex_lock(&codec->hash_mutex); |
| 1898 | info = get_alloc_amp_hash(codec, key); |
| 1899 | if (!info) { |
| 1900 | mutex_unlock(&codec->hash_mutex); |
| 1901 | return -EINVAL; |
| 1902 | } |
| 1903 | info->amp_caps = val; |
| 1904 | info->head.val |= INFO_AMP_CAPS; |
| 1905 | mutex_unlock(&codec->hash_mutex); |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | /* query the value from the caps hash; if not found, fetch the current |
| 1910 | * value from the given function and store in the hash |
| 1911 | */ |
| 1912 | static unsigned int |
| 1913 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1914 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1915 | { |
| 1916 | struct hda_amp_info *info; |
| 1917 | unsigned int val; |
| 1918 | |
| 1919 | mutex_lock(&codec->hash_mutex); |
| 1920 | info = get_alloc_amp_hash(codec, key); |
| 1921 | if (!info) { |
| 1922 | mutex_unlock(&codec->hash_mutex); |
| 1923 | return 0; |
| 1924 | } |
| 1925 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1926 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1927 | val = func(codec, nid, dir); |
| 1928 | write_caps_hash(codec, key, val); |
| 1929 | } else { |
| 1930 | val = info->amp_caps; |
| 1931 | mutex_unlock(&codec->hash_mutex); |
| 1932 | } |
| 1933 | return val; |
| 1934 | } |
| 1935 | |
| 1936 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1937 | int direction) |
| 1938 | { |
| 1939 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1940 | nid = codec->afg; |
| 1941 | return snd_hda_param_read(codec, nid, |
| 1942 | direction == HDA_OUTPUT ? |
| 1943 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1944 | } |
| 1945 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1946 | /** |
| 1947 | * query_amp_caps - query AMP capabilities |
| 1948 | * @codec: the HD-auio codec |
| 1949 | * @nid: the NID to query |
| 1950 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1951 | * |
| 1952 | * Query AMP capabilities for the given widget and direction. |
| 1953 | * Returns the obtained capability bits. |
| 1954 | * |
| 1955 | * When cap bits have been already read, this doesn't read again but |
| 1956 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1958 | 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] | 1959 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1960 | return query_caps_hash(codec, nid, direction, |
| 1961 | HDA_HASH_KEY(nid, direction, 0), |
| 1962 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1964 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1966 | /** |
| 1967 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1968 | * @codec: the CODEC to clean up |
| 1969 | * @nid: the NID to clean up |
| 1970 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1971 | * @caps: the capability bits to set |
| 1972 | * |
| 1973 | * Override the cached AMP caps bits value by the given one. |
| 1974 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1975 | * e.g. limit to 0dB, etc. |
| 1976 | * |
| 1977 | * Returns zero if successful or a negative error code. |
| 1978 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1979 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1980 | unsigned int caps) |
| 1981 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1982 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1983 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1984 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1985 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1986 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1987 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1988 | { |
| 1989 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1990 | } |
| 1991 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1992 | /** |
| 1993 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1994 | * @codec: the HD-auio codec |
| 1995 | * @nid: the NID to query |
| 1996 | * |
| 1997 | * Query PIN capabilities for the given widget. |
| 1998 | * Returns the obtained capability bits. |
| 1999 | * |
| 2000 | * When cap bits have been already read, this doesn't read again but |
| 2001 | * returns the cached value. |
| 2002 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2003 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2004 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2005 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2006 | read_pin_cap); |
| 2007 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2008 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2009 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2010 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2011 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2012 | * @codec: the CODEC |
| 2013 | * @nid: the NID to override |
| 2014 | * @caps: the capability bits to set |
| 2015 | * |
| 2016 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2017 | * |
| 2018 | * Returns zero if successful or a negative error code. |
| 2019 | */ |
| 2020 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2021 | unsigned int caps) |
| 2022 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2023 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2024 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2025 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2026 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2027 | /* read or sync the hash value with the current value; |
| 2028 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2030 | static struct hda_amp_info * |
| 2031 | 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] | 2032 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2034 | struct hda_amp_info *info; |
| 2035 | unsigned int parm, val = 0; |
| 2036 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2038 | retry: |
| 2039 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2040 | if (!info) |
| 2041 | return NULL; |
| 2042 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2043 | if (!val_read) { |
| 2044 | mutex_unlock(&codec->hash_mutex); |
| 2045 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2046 | parm |= direction == HDA_OUTPUT ? |
| 2047 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2048 | parm |= index; |
| 2049 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2050 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2051 | val &= 0xff; |
| 2052 | val_read = true; |
| 2053 | mutex_lock(&codec->hash_mutex); |
| 2054 | goto retry; |
| 2055 | } |
| 2056 | info->vol[ch] = val; |
| 2057 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2058 | } else if (init_only) |
| 2059 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2060 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2064 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2066 | 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] | 2067 | hda_nid_t nid, int ch, int direction, int index, |
| 2068 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | { |
| 2070 | u32 parm; |
| 2071 | |
| 2072 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2073 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2074 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2075 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2076 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2077 | ; /* set the zero value as a fake mute */ |
| 2078 | else |
| 2079 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2081 | } |
| 2082 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2083 | /** |
| 2084 | * snd_hda_codec_amp_read - Read AMP value |
| 2085 | * @codec: HD-audio codec |
| 2086 | * @nid: NID to read the AMP value |
| 2087 | * @ch: channel (left=0 or right=1) |
| 2088 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2089 | * @index: the index value (only for input direction) |
| 2090 | * |
| 2091 | * 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] | 2092 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2093 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2094 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2096 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2097 | unsigned int val = 0; |
| 2098 | |
| 2099 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2100 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2101 | if (info) |
| 2102 | val = info->vol[ch]; |
| 2103 | mutex_unlock(&codec->hash_mutex); |
| 2104 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2106 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2108 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2109 | int direction, int idx, int mask, int val, |
| 2110 | bool init_only) |
| 2111 | { |
| 2112 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2113 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2114 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2115 | |
| 2116 | if (snd_BUG_ON(mask & ~0xff)) |
| 2117 | mask &= 0xff; |
| 2118 | val &= mask; |
| 2119 | |
| 2120 | mutex_lock(&codec->hash_mutex); |
| 2121 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2122 | if (!info) { |
| 2123 | mutex_unlock(&codec->hash_mutex); |
| 2124 | return 0; |
| 2125 | } |
| 2126 | val |= info->vol[ch] & ~mask; |
| 2127 | if (info->vol[ch] == val) { |
| 2128 | mutex_unlock(&codec->hash_mutex); |
| 2129 | return 0; |
| 2130 | } |
| 2131 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2132 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2133 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2134 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2135 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2136 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2137 | return 1; |
| 2138 | } |
| 2139 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2140 | /** |
| 2141 | * snd_hda_codec_amp_update - update the AMP value |
| 2142 | * @codec: HD-audio codec |
| 2143 | * @nid: NID to read the AMP value |
| 2144 | * @ch: channel (left=0 or right=1) |
| 2145 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2146 | * @idx: the index value (only for input direction) |
| 2147 | * @mask: bit mask to set |
| 2148 | * @val: the bits value to set |
| 2149 | * |
| 2150 | * Update the AMP value with a bit mask. |
| 2151 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2152 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2153 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2154 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2156 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2158 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2160 | /** |
| 2161 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2162 | * @codec: HD-audio codec |
| 2163 | * @nid: NID to read the AMP value |
| 2164 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2165 | * @idx: the index value (only for input direction) |
| 2166 | * @mask: bit mask to set |
| 2167 | * @val: the bits value to set |
| 2168 | * |
| 2169 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2170 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2171 | */ |
| 2172 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2173 | int direction, int idx, int mask, int val) |
| 2174 | { |
| 2175 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2176 | |
| 2177 | if (snd_BUG_ON(mask & ~0xff)) |
| 2178 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2179 | for (ch = 0; ch < 2; ch++) |
| 2180 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2181 | idx, mask, val); |
| 2182 | return ret; |
| 2183 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2184 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2185 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2186 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2187 | * the first access. If the amp was already initialized / updated beforehand, |
| 2188 | * this does nothing. |
| 2189 | */ |
| 2190 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2191 | int dir, int idx, int mask, int val) |
| 2192 | { |
| 2193 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2194 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2195 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2196 | |
| 2197 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2198 | int dir, int idx, int mask, int val) |
| 2199 | { |
| 2200 | int ch, ret = 0; |
| 2201 | |
| 2202 | if (snd_BUG_ON(mask & ~0xff)) |
| 2203 | mask &= 0xff; |
| 2204 | for (ch = 0; ch < 2; ch++) |
| 2205 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2206 | idx, mask, val); |
| 2207 | return ret; |
| 2208 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2209 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2210 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2211 | /** |
| 2212 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2213 | * @codec: HD-audio codec |
| 2214 | * |
| 2215 | * Resume the all amp commands from the cache. |
| 2216 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2217 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2218 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2219 | int i; |
| 2220 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2221 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2222 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2223 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2224 | struct hda_amp_info *buffer; |
| 2225 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2226 | hda_nid_t nid; |
| 2227 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2228 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2229 | |
| 2230 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2231 | if (!buffer->head.dirty) |
| 2232 | continue; |
| 2233 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2234 | info = *buffer; |
| 2235 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2236 | if (!key) |
| 2237 | continue; |
| 2238 | nid = key & 0xff; |
| 2239 | idx = (key >> 16) & 0xff; |
| 2240 | dir = (key >> 24) & 0xff; |
| 2241 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2242 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2243 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2244 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2245 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2246 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2247 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2248 | } |
| 2249 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2250 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2251 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2252 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2254 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2255 | unsigned int ofs) |
| 2256 | { |
| 2257 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2258 | /* get num steps */ |
| 2259 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2260 | if (ofs < caps) |
| 2261 | caps -= ofs; |
| 2262 | return caps; |
| 2263 | } |
| 2264 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2265 | /** |
| 2266 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2267 | * |
| 2268 | * The control element is supposed to have the private_value field |
| 2269 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2270 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2271 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2272 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | { |
| 2274 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2275 | u16 nid = get_amp_nid(kcontrol); |
| 2276 | u8 chs = get_amp_channels(kcontrol); |
| 2277 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2278 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2280 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2281 | uinfo->count = chs == 3 ? 2 : 1; |
| 2282 | uinfo->value.integer.min = 0; |
| 2283 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2284 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2285 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2286 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2287 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | return -EINVAL; |
| 2289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | return 0; |
| 2291 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2292 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2294 | |
| 2295 | static inline unsigned int |
| 2296 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2297 | int ch, int dir, int idx, unsigned int ofs) |
| 2298 | { |
| 2299 | unsigned int val; |
| 2300 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2301 | val &= HDA_AMP_VOLMASK; |
| 2302 | if (val >= ofs) |
| 2303 | val -= ofs; |
| 2304 | else |
| 2305 | val = 0; |
| 2306 | return val; |
| 2307 | } |
| 2308 | |
| 2309 | static inline int |
| 2310 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2311 | int ch, int dir, int idx, unsigned int ofs, |
| 2312 | unsigned int val) |
| 2313 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2314 | unsigned int maxval; |
| 2315 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2316 | if (val > 0) |
| 2317 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2318 | /* ofs = 0: raw max value */ |
| 2319 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2320 | if (val > maxval) |
| 2321 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2322 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2323 | HDA_AMP_VOLMASK, val); |
| 2324 | } |
| 2325 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2326 | /** |
| 2327 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2328 | * |
| 2329 | * The control element is supposed to have the private_value field |
| 2330 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2331 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2332 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2333 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | { |
| 2335 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2336 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2337 | int chs = get_amp_channels(kcontrol); |
| 2338 | int dir = get_amp_direction(kcontrol); |
| 2339 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2340 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | long *valp = ucontrol->value.integer.value; |
| 2342 | |
| 2343 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2344 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2346 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2349 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2351 | /** |
| 2352 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2353 | * |
| 2354 | * The control element is supposed to have the private_value field |
| 2355 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2356 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2357 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2358 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | { |
| 2360 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2361 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2362 | int chs = get_amp_channels(kcontrol); |
| 2363 | int dir = get_amp_direction(kcontrol); |
| 2364 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2365 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | long *valp = ucontrol->value.integer.value; |
| 2367 | int change = 0; |
| 2368 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2369 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2370 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2371 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2372 | valp++; |
| 2373 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2374 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2375 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2376 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | return change; |
| 2378 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2379 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2381 | /** |
| 2382 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2383 | * |
| 2384 | * The control element is supposed to have the private_value field |
| 2385 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2386 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2387 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2388 | unsigned int size, unsigned int __user *_tlv) |
| 2389 | { |
| 2390 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2391 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2392 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2393 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2394 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2395 | u32 caps, val1, val2; |
| 2396 | |
| 2397 | if (size < 4 * sizeof(unsigned int)) |
| 2398 | return -ENOMEM; |
| 2399 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2400 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2401 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2402 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2403 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2404 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2405 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2406 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2407 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2408 | return -EFAULT; |
| 2409 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2410 | return -EFAULT; |
| 2411 | if (put_user(val1, _tlv + 2)) |
| 2412 | return -EFAULT; |
| 2413 | if (put_user(val2, _tlv + 3)) |
| 2414 | return -EFAULT; |
| 2415 | return 0; |
| 2416 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2417 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2418 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2419 | /** |
| 2420 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2421 | * @codec: HD-audio codec |
| 2422 | * @nid: NID of a reference widget |
| 2423 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2424 | * @tlv: TLV data to be stored, at least 4 elements |
| 2425 | * |
| 2426 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2427 | * obtained from the reference NID. |
| 2428 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2429 | */ |
| 2430 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2431 | unsigned int *tlv) |
| 2432 | { |
| 2433 | u32 caps; |
| 2434 | int nums, step; |
| 2435 | |
| 2436 | caps = query_amp_caps(codec, nid, dir); |
| 2437 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2438 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2439 | step = (step + 1) * 25; |
| 2440 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2441 | tlv[1] = 2 * sizeof(unsigned int); |
| 2442 | tlv[2] = -nums * step; |
| 2443 | tlv[3] = step; |
| 2444 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2445 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2446 | |
| 2447 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2448 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2449 | 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] | 2450 | { |
| 2451 | struct snd_ctl_elem_id id; |
| 2452 | memset(&id, 0, sizeof(id)); |
| 2453 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2454 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2455 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2456 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2457 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2458 | strcpy(id.name, name); |
| 2459 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2460 | } |
| 2461 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2462 | /** |
| 2463 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2464 | * @codec: HD-audio codec |
| 2465 | * @name: ctl id name string |
| 2466 | * |
| 2467 | * Get the control element with the given id string and IFACE_MIXER. |
| 2468 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2469 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2470 | const char *name) |
| 2471 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2472 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2473 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2474 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2475 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2476 | 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] | 2477 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2478 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2479 | int i, idx; |
| 2480 | /* 16 ctlrs should be large enough */ |
| 2481 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2482 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2483 | return idx; |
| 2484 | } |
| 2485 | return -EBUSY; |
| 2486 | } |
| 2487 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2488 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2489 | * 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] | 2490 | * @codec: HD-audio codec |
| 2491 | * @nid: corresponding NID (optional) |
| 2492 | * @kctl: the control element to assign |
| 2493 | * |
| 2494 | * Add the given control element to an array inside the codec instance. |
| 2495 | * All control elements belonging to a codec are supposed to be added |
| 2496 | * by this function so that a proper clean-up works at the free or |
| 2497 | * reconfiguration time. |
| 2498 | * |
| 2499 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2500 | * The assignment is shown in the codec proc file. |
| 2501 | * |
| 2502 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2503 | * #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] | 2504 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2505 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2506 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2507 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2508 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2509 | { |
| 2510 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2511 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2512 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2513 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2514 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2515 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2516 | if (nid == 0) |
| 2517 | nid = get_amp_nid_(kctl->private_value); |
| 2518 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2519 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2520 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2521 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2522 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2523 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2524 | if (err < 0) |
| 2525 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2526 | item = snd_array_new(&codec->mixers); |
| 2527 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2528 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2529 | item->kctl = kctl; |
| 2530 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2531 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2532 | return 0; |
| 2533 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2534 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2535 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2536 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2537 | * snd_hda_add_nid - Assign a NID to a control element |
| 2538 | * @codec: HD-audio codec |
| 2539 | * @nid: corresponding NID (optional) |
| 2540 | * @kctl: the control element to assign |
| 2541 | * @index: index to kctl |
| 2542 | * |
| 2543 | * Add the given control element to an array inside the codec instance. |
| 2544 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2545 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2546 | */ |
| 2547 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2548 | unsigned int index, hda_nid_t nid) |
| 2549 | { |
| 2550 | struct hda_nid_item *item; |
| 2551 | |
| 2552 | if (nid > 0) { |
| 2553 | item = snd_array_new(&codec->nids); |
| 2554 | if (!item) |
| 2555 | return -ENOMEM; |
| 2556 | item->kctl = kctl; |
| 2557 | item->index = index; |
| 2558 | item->nid = nid; |
| 2559 | return 0; |
| 2560 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2561 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2562 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2563 | return -EINVAL; |
| 2564 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2565 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2566 | |
| 2567 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2568 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2569 | * @codec: HD-audio codec |
| 2570 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2571 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2572 | { |
| 2573 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2574 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2575 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2576 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2577 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2578 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2581 | /* pseudo device locking |
| 2582 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2583 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2584 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2585 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2586 | struct snd_card *card = bus->card; |
| 2587 | struct hda_codec *codec; |
| 2588 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2589 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2590 | if (card->shutdown) |
| 2591 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2592 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2593 | if (!list_empty(&card->ctl_files)) |
| 2594 | goto err_clear; |
| 2595 | |
| 2596 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2597 | int pcm; |
| 2598 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2599 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2600 | if (!cpcm->pcm) |
| 2601 | continue; |
| 2602 | if (cpcm->pcm->streams[0].substream_opened || |
| 2603 | cpcm->pcm->streams[1].substream_opened) |
| 2604 | goto err_clear; |
| 2605 | } |
| 2606 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2607 | spin_unlock(&card->files_lock); |
| 2608 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2609 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2610 | err_clear: |
| 2611 | card->shutdown = 0; |
| 2612 | err_unlock: |
| 2613 | spin_unlock(&card->files_lock); |
| 2614 | return -EINVAL; |
| 2615 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2616 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2617 | |
| 2618 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2619 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2620 | struct snd_card *card = bus->card; |
| 2621 | |
| 2622 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2623 | spin_lock(&card->files_lock); |
| 2624 | card->shutdown = 0; |
| 2625 | spin_unlock(&card->files_lock); |
| 2626 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2627 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2628 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2629 | /** |
| 2630 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2631 | * @codec: HD-audio codec |
| 2632 | * |
| 2633 | * This frees the all PCM and control elements assigned to the codec, and |
| 2634 | * clears the caches and restores the pin default configurations. |
| 2635 | * |
| 2636 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2637 | * returns zero. |
| 2638 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2639 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2640 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2641 | struct hda_bus *bus = codec->bus; |
| 2642 | struct snd_card *card = bus->card; |
| 2643 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2644 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2645 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2646 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2647 | |
| 2648 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2649 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2650 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2651 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2652 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2653 | #endif |
| 2654 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2655 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2656 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2657 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2658 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2659 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2660 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2661 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2662 | } |
| 2663 | if (codec->patch_ops.free) |
| 2664 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2665 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2666 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2667 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2668 | codec->spec = NULL; |
| 2669 | free_hda_cache(&codec->amp_cache); |
| 2670 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2671 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2672 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2673 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2674 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2675 | snd_array_free(&codec->cvt_setups); |
| 2676 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2677 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2678 | codec->num_pcms = 0; |
| 2679 | codec->pcm_info = NULL; |
| 2680 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2681 | codec->slave_dig_outs = NULL; |
| 2682 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2683 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2684 | module_put(codec->owner); |
| 2685 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2686 | |
| 2687 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2688 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2689 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2690 | } |
| 2691 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2692 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2693 | |
| 2694 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2695 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2696 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2697 | { |
| 2698 | struct hda_nid_item *items; |
| 2699 | const char * const *s; |
| 2700 | int i, err; |
| 2701 | |
| 2702 | items = codec->mixers.list; |
| 2703 | for (i = 0; i < codec->mixers.used; i++) { |
| 2704 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2705 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2706 | continue; |
| 2707 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2708 | char tmpname[sizeof(sctl->id.name)]; |
| 2709 | const char *name = *s; |
| 2710 | if (suffix) { |
| 2711 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2712 | name, suffix); |
| 2713 | name = tmpname; |
| 2714 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2715 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2716 | err = func(data, sctl); |
| 2717 | if (err) |
| 2718 | return err; |
| 2719 | break; |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | return 0; |
| 2724 | } |
| 2725 | |
| 2726 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2727 | { |
| 2728 | return 1; |
| 2729 | } |
| 2730 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2731 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2732 | 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] | 2733 | { |
| 2734 | int _tlv[4]; |
| 2735 | const int *tlv = NULL; |
| 2736 | int val = -1; |
| 2737 | |
| 2738 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2739 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2740 | mm_segment_t fs = get_fs(); |
| 2741 | set_fs(get_ds()); |
| 2742 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2743 | tlv = _tlv; |
| 2744 | set_fs(fs); |
| 2745 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2746 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2747 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2748 | int step = tlv[3]; |
| 2749 | step &= ~TLV_DB_SCALE_MUTE; |
| 2750 | if (!step) |
| 2751 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2752 | if (*step_to_check && *step_to_check != step) { |
| 2753 | snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n", |
| 2754 | *step_to_check, step); |
| 2755 | return -1; |
| 2756 | } |
| 2757 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2758 | val = -tlv[2] / step; |
| 2759 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2760 | return val; |
| 2761 | } |
| 2762 | |
| 2763 | /* call kctl->put with the given value(s) */ |
| 2764 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2765 | { |
| 2766 | struct snd_ctl_elem_value *ucontrol; |
| 2767 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2768 | if (!ucontrol) |
| 2769 | return -ENOMEM; |
| 2770 | ucontrol->value.integer.value[0] = val; |
| 2771 | ucontrol->value.integer.value[1] = val; |
| 2772 | kctl->put(kctl, ucontrol); |
| 2773 | kfree(ucontrol); |
| 2774 | return 0; |
| 2775 | } |
| 2776 | |
| 2777 | /* initialize the slave volume with 0dB */ |
| 2778 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2779 | { |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2780 | int offset = get_kctl_0dB_offset(slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2781 | if (offset > 0) |
| 2782 | put_kctl_with_value(slave, offset); |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
| 2786 | /* unmute the slave */ |
| 2787 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2788 | { |
| 2789 | return put_kctl_with_value(slave, 1); |
| 2790 | } |
| 2791 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2792 | /** |
| 2793 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2794 | * @codec: HD-audio codec |
| 2795 | * @name: vmaster control name |
| 2796 | * @tlv: TLV data (optional) |
| 2797 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2798 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2799 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2800 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2801 | * |
| 2802 | * Create a virtual master control with the given name. The TLV data |
| 2803 | * must be either NULL or a valid data. |
| 2804 | * |
| 2805 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2806 | * slave control name. All controls with these names are assigned to |
| 2807 | * the new virtual master control. |
| 2808 | * |
| 2809 | * This function returns zero if successful or a negative error code. |
| 2810 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2811 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2812 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2813 | const char *suffix, bool init_slave_vol, |
| 2814 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2815 | { |
| 2816 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2817 | int err; |
| 2818 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2819 | if (ctl_ret) |
| 2820 | *ctl_ret = NULL; |
| 2821 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2822 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2823 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2824 | snd_printdd("No slave found for %s\n", name); |
| 2825 | return 0; |
| 2826 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2827 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2828 | if (!kctl) |
| 2829 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2830 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2831 | if (err < 0) |
| 2832 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2833 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2834 | err = map_slaves(codec, slaves, suffix, |
| 2835 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2836 | if (err < 0) |
| 2837 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2838 | |
| 2839 | /* init with master mute & zero volume */ |
| 2840 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2841 | if (init_slave_vol) { |
| 2842 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2843 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2844 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2845 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2846 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2847 | if (ctl_ret) |
| 2848 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2849 | return 0; |
| 2850 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2851 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2852 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2853 | /* |
| 2854 | * mute-LED control using vmaster |
| 2855 | */ |
| 2856 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2857 | struct snd_ctl_elem_info *uinfo) |
| 2858 | { |
| 2859 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2860 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2861 | }; |
| 2862 | unsigned int index; |
| 2863 | |
| 2864 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2865 | uinfo->count = 1; |
| 2866 | uinfo->value.enumerated.items = 3; |
| 2867 | index = uinfo->value.enumerated.item; |
| 2868 | if (index >= 3) |
| 2869 | index = 2; |
| 2870 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2871 | return 0; |
| 2872 | } |
| 2873 | |
| 2874 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2875 | struct snd_ctl_elem_value *ucontrol) |
| 2876 | { |
| 2877 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2878 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2879 | return 0; |
| 2880 | } |
| 2881 | |
| 2882 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2883 | struct snd_ctl_elem_value *ucontrol) |
| 2884 | { |
| 2885 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2886 | unsigned int old_mode = hook->mute_mode; |
| 2887 | |
| 2888 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2889 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2890 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2891 | if (old_mode == hook->mute_mode) |
| 2892 | return 0; |
| 2893 | snd_hda_sync_vmaster_hook(hook); |
| 2894 | return 1; |
| 2895 | } |
| 2896 | |
| 2897 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2898 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2899 | .name = "Mute-LED Mode", |
| 2900 | .info = vmaster_mute_mode_info, |
| 2901 | .get = vmaster_mute_mode_get, |
| 2902 | .put = vmaster_mute_mode_put, |
| 2903 | }; |
| 2904 | |
| 2905 | /* |
| 2906 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2907 | * "Mute-LED Mode" control is automatically created and associated with |
| 2908 | * the given hook. |
| 2909 | */ |
| 2910 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2911 | struct hda_vmaster_mute_hook *hook, |
| 2912 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2913 | { |
| 2914 | struct snd_kcontrol *kctl; |
| 2915 | |
| 2916 | if (!hook->hook || !hook->sw_kctl) |
| 2917 | return 0; |
| 2918 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2919 | hook->codec = codec; |
| 2920 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2921 | if (!expose_enum_ctl) |
| 2922 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2923 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2924 | if (!kctl) |
| 2925 | return -ENOMEM; |
| 2926 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2927 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2928 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2929 | |
| 2930 | /* |
| 2931 | * Call the hook with the current value for synchronization |
| 2932 | * Should be called in init callback |
| 2933 | */ |
| 2934 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2935 | { |
| 2936 | if (!hook->hook || !hook->codec) |
| 2937 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2938 | /* don't call vmaster hook in the destructor since it might have |
| 2939 | * been already destroyed |
| 2940 | */ |
| 2941 | if (hook->codec->bus->shutdown) |
| 2942 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2943 | switch (hook->mute_mode) { |
| 2944 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2945 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2946 | break; |
| 2947 | default: |
| 2948 | hook->hook(hook->codec, hook->mute_mode); |
| 2949 | break; |
| 2950 | } |
| 2951 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2952 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2953 | |
| 2954 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2955 | /** |
| 2956 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2957 | * |
| 2958 | * The control element is supposed to have the private_value field |
| 2959 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2960 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2961 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2962 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | { |
| 2964 | int chs = get_amp_channels(kcontrol); |
| 2965 | |
| 2966 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2967 | uinfo->count = chs == 3 ? 2 : 1; |
| 2968 | uinfo->value.integer.min = 0; |
| 2969 | uinfo->value.integer.max = 1; |
| 2970 | return 0; |
| 2971 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2972 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2974 | /** |
| 2975 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 2976 | * |
| 2977 | * The control element is supposed to have the private_value field |
| 2978 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2979 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2980 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2981 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | { |
| 2983 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2984 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2985 | int chs = get_amp_channels(kcontrol); |
| 2986 | int dir = get_amp_direction(kcontrol); |
| 2987 | int idx = get_amp_index(kcontrol); |
| 2988 | long *valp = ucontrol->value.integer.value; |
| 2989 | |
| 2990 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2991 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2992 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2994 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2995 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | return 0; |
| 2997 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2998 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3000 | /** |
| 3001 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 3002 | * |
| 3003 | * The control element is supposed to have the private_value field |
| 3004 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3005 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3006 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3007 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | { |
| 3009 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3010 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3011 | int chs = get_amp_channels(kcontrol); |
| 3012 | int dir = get_amp_direction(kcontrol); |
| 3013 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | long *valp = ucontrol->value.integer.value; |
| 3015 | int change = 0; |
| 3016 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3017 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3018 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3019 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3020 | HDA_AMP_MUTE, |
| 3021 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3022 | valp++; |
| 3023 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3024 | if (chs & 2) |
| 3025 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3026 | HDA_AMP_MUTE, |
| 3027 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3028 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3029 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | return change; |
| 3031 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3032 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | |
| 3034 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3035 | * bound volume controls |
| 3036 | * |
| 3037 | * bind multiple volumes (# indices, from 0) |
| 3038 | */ |
| 3039 | |
| 3040 | #define AMP_VAL_IDX_SHIFT 19 |
| 3041 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3042 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3043 | /** |
| 3044 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 3045 | * |
| 3046 | * The control element is supposed to have the private_value field |
| 3047 | * set up via HDA_BIND_MUTE*() macros. |
| 3048 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3049 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3050 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3051 | { |
| 3052 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3053 | unsigned long pval; |
| 3054 | int err; |
| 3055 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3056 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3057 | pval = kcontrol->private_value; |
| 3058 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3059 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3060 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3061 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3062 | return err; |
| 3063 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3064 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3065 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3066 | /** |
| 3067 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 3068 | * |
| 3069 | * The control element is supposed to have the private_value field |
| 3070 | * set up via HDA_BIND_MUTE*() macros. |
| 3071 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3072 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3073 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3074 | { |
| 3075 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3076 | unsigned long pval; |
| 3077 | int i, indices, err = 0, change = 0; |
| 3078 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3079 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3080 | pval = kcontrol->private_value; |
| 3081 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3082 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3084 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3085 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3086 | if (err < 0) |
| 3087 | break; |
| 3088 | change |= err; |
| 3089 | } |
| 3090 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3091 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3092 | return err < 0 ? err : change; |
| 3093 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3094 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3095 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3096 | /** |
| 3097 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 3098 | * |
| 3099 | * The control element is supposed to have the private_value field |
| 3100 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3101 | */ |
| 3102 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3103 | struct snd_ctl_elem_info *uinfo) |
| 3104 | { |
| 3105 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3106 | struct hda_bind_ctls *c; |
| 3107 | int err; |
| 3108 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3109 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3110 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3111 | kcontrol->private_value = *c->values; |
| 3112 | err = c->ops->info(kcontrol, uinfo); |
| 3113 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3114 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3115 | return err; |
| 3116 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3117 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3118 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3119 | /** |
| 3120 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 3121 | * |
| 3122 | * The control element is supposed to have the private_value field |
| 3123 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3124 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3125 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3126 | struct snd_ctl_elem_value *ucontrol) |
| 3127 | { |
| 3128 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3129 | struct hda_bind_ctls *c; |
| 3130 | int err; |
| 3131 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3132 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3133 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3134 | kcontrol->private_value = *c->values; |
| 3135 | err = c->ops->get(kcontrol, ucontrol); |
| 3136 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3137 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3138 | return err; |
| 3139 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3140 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3141 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3142 | /** |
| 3143 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3144 | * |
| 3145 | * The control element is supposed to have the private_value field |
| 3146 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3147 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3148 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3149 | struct snd_ctl_elem_value *ucontrol) |
| 3150 | { |
| 3151 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3152 | struct hda_bind_ctls *c; |
| 3153 | unsigned long *vals; |
| 3154 | int err = 0, change = 0; |
| 3155 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3156 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3157 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3158 | for (vals = c->values; *vals; vals++) { |
| 3159 | kcontrol->private_value = *vals; |
| 3160 | err = c->ops->put(kcontrol, ucontrol); |
| 3161 | if (err < 0) |
| 3162 | break; |
| 3163 | change |= err; |
| 3164 | } |
| 3165 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3166 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3167 | return err < 0 ? err : change; |
| 3168 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3169 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3170 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3171 | /** |
| 3172 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3173 | * |
| 3174 | * The control element is supposed to have the private_value field |
| 3175 | * set up via HDA_BIND_VOL() macro. |
| 3176 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3177 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3178 | unsigned int size, unsigned int __user *tlv) |
| 3179 | { |
| 3180 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3181 | struct hda_bind_ctls *c; |
| 3182 | int err; |
| 3183 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3184 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3185 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3186 | kcontrol->private_value = *c->values; |
| 3187 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3188 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3189 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3190 | return err; |
| 3191 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3192 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3193 | |
| 3194 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3195 | .info = snd_hda_mixer_amp_volume_info, |
| 3196 | .get = snd_hda_mixer_amp_volume_get, |
| 3197 | .put = snd_hda_mixer_amp_volume_put, |
| 3198 | .tlv = snd_hda_mixer_amp_tlv |
| 3199 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3200 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3201 | |
| 3202 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3203 | .info = snd_hda_mixer_amp_switch_info, |
| 3204 | .get = snd_hda_mixer_amp_switch_get, |
| 3205 | .put = snd_hda_mixer_amp_switch_put, |
| 3206 | .tlv = snd_hda_mixer_amp_tlv |
| 3207 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3208 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3209 | |
| 3210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | * SPDIF out controls |
| 3212 | */ |
| 3213 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3214 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3215 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | { |
| 3217 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3218 | uinfo->count = 1; |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3222 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3223 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | { |
| 3225 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3226 | IEC958_AES0_NONAUDIO | |
| 3227 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3228 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3229 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3230 | IEC958_AES1_CON_ORIGINAL; |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3234 | static int snd_hda_spdif_pmask_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 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3238 | IEC958_AES0_NONAUDIO | |
| 3239 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3240 | return 0; |
| 3241 | } |
| 3242 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3243 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3244 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | { |
| 3246 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3247 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3248 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3250 | mutex_lock(&codec->spdif_mutex); |
| 3251 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3252 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3253 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3254 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3255 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3256 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
| 3261 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3262 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3263 | */ |
| 3264 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3265 | { |
| 3266 | unsigned short val = 0; |
| 3267 | |
| 3268 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3269 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3271 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3273 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3274 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3275 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3277 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3278 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3279 | val |= AC_DIG1_EMPHASIS; |
| 3280 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3281 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3283 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3285 | } |
| 3286 | return val; |
| 3287 | } |
| 3288 | |
| 3289 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3290 | */ |
| 3291 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3292 | { |
| 3293 | unsigned int sbits = 0; |
| 3294 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3295 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3297 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3299 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3300 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3302 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3303 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3305 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3307 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3309 | sbits |= val & (0x7f << 8); |
| 3310 | } |
| 3311 | return sbits; |
| 3312 | } |
| 3313 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3314 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3315 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3316 | int verb, int val) |
| 3317 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3318 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3319 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3320 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3321 | d = codec->slave_dig_outs; |
| 3322 | if (!d) |
| 3323 | return; |
| 3324 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3325 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3329 | int dig1, int dig2) |
| 3330 | { |
| 3331 | if (dig1 != -1) |
| 3332 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3333 | if (dig2 != -1) |
| 3334 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3335 | } |
| 3336 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3337 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3338 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | { |
| 3340 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3341 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3342 | struct hda_spdif_out *spdif; |
| 3343 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | unsigned short val; |
| 3345 | int change; |
| 3346 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3347 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3348 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3349 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3350 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3352 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3353 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3354 | val = convert_from_spdif_status(spdif->status); |
| 3355 | val |= spdif->ctls & 1; |
| 3356 | change = spdif->ctls != val; |
| 3357 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3358 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3359 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3360 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | return change; |
| 3362 | } |
| 3363 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3364 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3366 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3367 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | { |
| 3369 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3370 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3371 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3373 | mutex_lock(&codec->spdif_mutex); |
| 3374 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3375 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3376 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | return 0; |
| 3378 | } |
| 3379 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3380 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3381 | int dig1, int dig2) |
| 3382 | { |
| 3383 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3384 | /* unmute amp switch (if any) */ |
| 3385 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3386 | (dig1 & AC_DIG1_ENABLE)) |
| 3387 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3388 | HDA_AMP_MUTE, 0); |
| 3389 | } |
| 3390 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3391 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3392 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | { |
| 3394 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3395 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3396 | struct hda_spdif_out *spdif; |
| 3397 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | unsigned short val; |
| 3399 | int change; |
| 3400 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3401 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3402 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3403 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3404 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3406 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3407 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3408 | spdif->ctls = val; |
| 3409 | if (change && nid != (u16)-1) |
| 3410 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3411 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | return change; |
| 3413 | } |
| 3414 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3415 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | { |
| 3417 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3418 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3419 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | .info = snd_hda_spdif_mask_info, |
| 3421 | .get = snd_hda_spdif_cmask_get, |
| 3422 | }, |
| 3423 | { |
| 3424 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3425 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3426 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | .info = snd_hda_spdif_mask_info, |
| 3428 | .get = snd_hda_spdif_pmask_get, |
| 3429 | }, |
| 3430 | { |
| 3431 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3432 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | .info = snd_hda_spdif_mask_info, |
| 3434 | .get = snd_hda_spdif_default_get, |
| 3435 | .put = snd_hda_spdif_default_put, |
| 3436 | }, |
| 3437 | { |
| 3438 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3439 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | .info = snd_hda_spdif_out_switch_info, |
| 3441 | .get = snd_hda_spdif_out_switch_get, |
| 3442 | .put = snd_hda_spdif_out_switch_put, |
| 3443 | }, |
| 3444 | { } /* end */ |
| 3445 | }; |
| 3446 | |
| 3447 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3448 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3450 | * @associated_nid: NID that new ctls associated with |
| 3451 | * @cvt_nid: converter NID |
| 3452 | * @type: HDA_PCM_TYPE_* |
| 3453 | * Creates controls related with the digital output. |
| 3454 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | * |
| 3456 | * Returns 0 if successful, or a negative error code. |
| 3457 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3458 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3459 | hda_nid_t associated_nid, |
| 3460 | hda_nid_t cvt_nid, |
| 3461 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | { |
| 3463 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3464 | struct snd_kcontrol *kctl; |
| 3465 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3466 | int idx = 0; |
| 3467 | const int spdif_index = 16; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3468 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3469 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3471 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3472 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3473 | idx = spdif_index; |
| 3474 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3475 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3476 | /* suppose a single SPDIF device */ |
| 3477 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3478 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3479 | if (!kctl) |
| 3480 | break; |
| 3481 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3482 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3483 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3484 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3485 | if (!bus->primary_dig_out_type) |
| 3486 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3487 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3488 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3489 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3490 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3491 | return -EBUSY; |
| 3492 | } |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3493 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3494 | if (!spdif) |
| 3495 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3497 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3498 | if (!kctl) |
| 3499 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3500 | kctl->id.index = idx; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3501 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3502 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3503 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | return err; |
| 3505 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3506 | spdif->nid = cvt_nid; |
| 3507 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3508 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3509 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | return 0; |
| 3511 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3512 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3514 | /* get the hda_spdif_out entry from the given NID |
| 3515 | * call within spdif_mutex lock |
| 3516 | */ |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3517 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3518 | hda_nid_t nid) |
| 3519 | { |
| 3520 | int i; |
| 3521 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3522 | struct hda_spdif_out *spdif = |
| 3523 | snd_array_elem(&codec->spdif_out, i); |
| 3524 | if (spdif->nid == nid) |
| 3525 | return spdif; |
| 3526 | } |
| 3527 | return NULL; |
| 3528 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3529 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3530 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3531 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3532 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3533 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3534 | |
| 3535 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3536 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3537 | spdif->nid = (u16)-1; |
| 3538 | mutex_unlock(&codec->spdif_mutex); |
| 3539 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3540 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3541 | |
| 3542 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3543 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3544 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3545 | unsigned short val; |
| 3546 | |
| 3547 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3548 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3549 | if (spdif->nid != nid) { |
| 3550 | spdif->nid = nid; |
| 3551 | val = spdif->ctls; |
| 3552 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3553 | } |
| 3554 | mutex_unlock(&codec->spdif_mutex); |
| 3555 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3556 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3559 | * SPDIF sharing with analog output |
| 3560 | */ |
| 3561 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3562 | struct snd_ctl_elem_value *ucontrol) |
| 3563 | { |
| 3564 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3565 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3570 | struct snd_ctl_elem_value *ucontrol) |
| 3571 | { |
| 3572 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3573 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
| 3577 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3578 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3579 | .name = "IEC958 Default PCM Playback Switch", |
| 3580 | .info = snd_ctl_boolean_mono_info, |
| 3581 | .get = spdif_share_sw_get, |
| 3582 | .put = spdif_share_sw_put, |
| 3583 | }; |
| 3584 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3585 | /** |
| 3586 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3587 | * @codec: the HDA codec |
| 3588 | * @mout: multi-out instance |
| 3589 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3590 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3591 | struct hda_multi_out *mout) |
| 3592 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3593 | struct snd_kcontrol *kctl; |
| 3594 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3595 | if (!mout->dig_out_nid) |
| 3596 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3597 | |
| 3598 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3599 | if (!kctl) |
| 3600 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3601 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3602 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3603 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3604 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3605 | |
| 3606 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | * SPDIF input |
| 3608 | */ |
| 3609 | |
| 3610 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3611 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3612 | static int snd_hda_spdif_in_switch_get(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 | |
| 3617 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3621 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3622 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | { |
| 3624 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3625 | hda_nid_t nid = kcontrol->private_value; |
| 3626 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3627 | int change; |
| 3628 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3629 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3631 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3633 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3634 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3636 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | return change; |
| 3638 | } |
| 3639 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3640 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3641 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | { |
| 3643 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3644 | hda_nid_t nid = kcontrol->private_value; |
| 3645 | unsigned short val; |
| 3646 | unsigned int sbits; |
| 3647 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3648 | 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] | 3649 | sbits = convert_to_spdif_status(val); |
| 3650 | ucontrol->value.iec958.status[0] = sbits; |
| 3651 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3652 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3653 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3654 | return 0; |
| 3655 | } |
| 3656 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3657 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | { |
| 3659 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3660 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | .info = snd_hda_spdif_in_switch_info, |
| 3662 | .get = snd_hda_spdif_in_switch_get, |
| 3663 | .put = snd_hda_spdif_in_switch_put, |
| 3664 | }, |
| 3665 | { |
| 3666 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3667 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3668 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | .info = snd_hda_spdif_mask_info, |
| 3670 | .get = snd_hda_spdif_in_status_get, |
| 3671 | }, |
| 3672 | { } /* end */ |
| 3673 | }; |
| 3674 | |
| 3675 | /** |
| 3676 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3677 | * @codec: the HDA codec |
| 3678 | * @nid: audio in widget NID |
| 3679 | * |
| 3680 | * Creates controls related with the SPDIF input. |
| 3681 | * Called from each patch supporting the SPDIF in. |
| 3682 | * |
| 3683 | * Returns 0 if successful, or a negative error code. |
| 3684 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3685 | 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] | 3686 | { |
| 3687 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3688 | struct snd_kcontrol *kctl; |
| 3689 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3690 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3692 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3693 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3694 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3695 | return -EBUSY; |
| 3696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3698 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3699 | if (!kctl) |
| 3700 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3702 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3703 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | return err; |
| 3705 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3706 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3707 | snd_hda_codec_read(codec, nid, 0, |
| 3708 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3709 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | return 0; |
| 3711 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3712 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3714 | /* |
| 3715 | * command cache |
| 3716 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3718 | /* 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] | 3719 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3720 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3721 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3722 | |
| 3723 | /** |
| 3724 | * snd_hda_codec_write_cache - send a single command with caching |
| 3725 | * @codec: the HDA codec |
| 3726 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3727 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3728 | * @verb: the verb to send |
| 3729 | * @parm: the parameter for the verb |
| 3730 | * |
| 3731 | * Send a single command without waiting for response. |
| 3732 | * |
| 3733 | * Returns 0 if successful, or a negative error code. |
| 3734 | */ |
| 3735 | 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] | 3736 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3737 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3738 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3739 | struct hda_cache_head *c; |
| 3740 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3741 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3742 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3743 | cache_only = codec->cached_write; |
| 3744 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3745 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3746 | if (err < 0) |
| 3747 | return err; |
| 3748 | } |
| 3749 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3750 | /* parm may contain the verb stuff for get/set amp */ |
| 3751 | verb = verb | (parm >> 8); |
| 3752 | parm &= 0xff; |
| 3753 | key = build_cmd_cache_key(nid, verb); |
| 3754 | mutex_lock(&codec->bus->cmd_mutex); |
| 3755 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3756 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3757 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3758 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3759 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3760 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3761 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3762 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3763 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3764 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3765 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3766 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3767 | * @codec: the HDA codec |
| 3768 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3769 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3770 | * @verb: the verb to send |
| 3771 | * @parm: the parameter for the verb |
| 3772 | * |
| 3773 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3774 | * command if the parameter is already identical with the cached value. |
| 3775 | * If not, it sends the command and refreshes the cache. |
| 3776 | * |
| 3777 | * Returns 0 if successful, or a negative error code. |
| 3778 | */ |
| 3779 | 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] | 3780 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3781 | { |
| 3782 | struct hda_cache_head *c; |
| 3783 | u32 key; |
| 3784 | |
| 3785 | /* parm may contain the verb stuff for get/set amp */ |
| 3786 | verb = verb | (parm >> 8); |
| 3787 | parm &= 0xff; |
| 3788 | key = build_cmd_cache_key(nid, verb); |
| 3789 | mutex_lock(&codec->bus->cmd_mutex); |
| 3790 | c = get_hash(&codec->cmd_cache, key); |
| 3791 | if (c && c->val == parm) { |
| 3792 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3796 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3797 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3798 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3799 | |
| 3800 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3801 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3802 | * @codec: HD-audio codec |
| 3803 | * |
| 3804 | * Execute all verbs recorded in the command caches to resume. |
| 3805 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3806 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3807 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3808 | int i; |
| 3809 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3810 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3811 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3812 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3813 | struct hda_cache_head *buffer; |
| 3814 | u32 key; |
| 3815 | |
| 3816 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3817 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3818 | if (!key) |
| 3819 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3820 | if (!buffer->dirty) |
| 3821 | continue; |
| 3822 | buffer->dirty = 0; |
| 3823 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3824 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3825 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3826 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3827 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3828 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3829 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3830 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3831 | |
| 3832 | /** |
| 3833 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3834 | * @codec: the HDA codec |
| 3835 | * @seq: VERB array to send |
| 3836 | * |
| 3837 | * Send the commands sequentially from the given array. |
| 3838 | * Thte commands are recorded on cache for power-save and resume. |
| 3839 | * The array must be terminated with NID=0. |
| 3840 | */ |
| 3841 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3842 | const struct hda_verb *seq) |
| 3843 | { |
| 3844 | for (; seq->nid; seq++) |
| 3845 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3846 | seq->param); |
| 3847 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3848 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3849 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3850 | /** |
| 3851 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3852 | * @codec: HD-audio codec |
| 3853 | */ |
| 3854 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3855 | { |
| 3856 | snd_hda_codec_resume_amp(codec); |
| 3857 | snd_hda_codec_resume_cache(codec); |
| 3858 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3859 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3860 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3861 | 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] | 3862 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3863 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3864 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3865 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3866 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3867 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3868 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3869 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3870 | if (!(wcaps & AC_WCAP_POWER)) |
| 3871 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3872 | if (codec->power_filter) { |
| 3873 | state = codec->power_filter(codec, nid, power_state); |
| 3874 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3875 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3876 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3877 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3878 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3879 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3880 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3881 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3882 | |
| 3883 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3884 | * supported power states check |
| 3885 | */ |
| 3886 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3887 | unsigned int power_state) |
| 3888 | { |
| 3889 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3890 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3891 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3892 | return false; |
| 3893 | if (sup & power_state) |
| 3894 | return true; |
| 3895 | else |
| 3896 | return false; |
| 3897 | } |
| 3898 | |
| 3899 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3900 | * wait until the state is reached, returns the current state |
| 3901 | */ |
| 3902 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3903 | hda_nid_t fg, |
| 3904 | unsigned int power_state) |
| 3905 | { |
| 3906 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3907 | unsigned int state, actual_state; |
| 3908 | |
| 3909 | for (;;) { |
| 3910 | state = snd_hda_codec_read(codec, fg, 0, |
| 3911 | AC_VERB_GET_POWER_STATE, 0); |
| 3912 | if (state & AC_PWRST_ERROR) |
| 3913 | break; |
| 3914 | actual_state = (state >> 4) & 0x0f; |
| 3915 | if (actual_state == power_state) |
| 3916 | break; |
| 3917 | if (time_after_eq(jiffies, end_time)) |
| 3918 | break; |
| 3919 | /* wait until the codec reachs to the target state */ |
| 3920 | msleep(1); |
| 3921 | } |
| 3922 | return state; |
| 3923 | } |
| 3924 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3925 | /* 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] | 3926 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3927 | hda_nid_t nid, |
| 3928 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3929 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3930 | if (nid == codec->afg || nid == codec->mfg) |
| 3931 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3932 | if (power_state == AC_PWRST_D3 && |
| 3933 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3934 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3935 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3936 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3937 | if (eapd & 0x02) |
| 3938 | return AC_PWRST_D0; |
| 3939 | } |
| 3940 | return power_state; |
| 3941 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3942 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3943 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3944 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3945 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3946 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3947 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3948 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3949 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3950 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3951 | int count; |
| 3952 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3953 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3954 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3955 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3956 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3957 | if (codec->depop_delay < 0) |
| 3958 | msleep(codec->epss ? 10 : 100); |
| 3959 | else if (codec->depop_delay > 0) |
| 3960 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3961 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3962 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3963 | |
| 3964 | /* repeat power states setting at most 10 times*/ |
| 3965 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3966 | if (codec->patch_ops.set_power_state) |
| 3967 | codec->patch_ops.set_power_state(codec, fg, |
| 3968 | power_state); |
| 3969 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3970 | state = power_state; |
| 3971 | if (codec->power_filter) |
| 3972 | state = codec->power_filter(codec, fg, state); |
| 3973 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3974 | snd_hda_codec_read(codec, fg, flags, |
| 3975 | AC_VERB_SET_POWER_STATE, |
| 3976 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3977 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3978 | } |
| 3979 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3980 | if (!(state & AC_PWRST_ERROR)) |
| 3981 | break; |
| 3982 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3983 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3984 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3985 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3986 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3987 | /* sync power states of all widgets; |
| 3988 | * this is called at the end of codec parsing |
| 3989 | */ |
| 3990 | static void sync_power_up_states(struct hda_codec *codec) |
| 3991 | { |
| 3992 | hda_nid_t nid = codec->start_nid; |
| 3993 | int i; |
| 3994 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3995 | /* don't care if no filter is used */ |
| 3996 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3997 | return; |
| 3998 | |
| 3999 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4000 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4001 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4002 | if (!(wcaps & AC_WCAP_POWER)) |
| 4003 | continue; |
| 4004 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4005 | if (target == AC_PWRST_D0) |
| 4006 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4007 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4008 | snd_hda_codec_write(codec, nid, 0, |
| 4009 | AC_VERB_SET_POWER_STATE, target); |
| 4010 | } |
| 4011 | } |
| 4012 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4013 | #ifdef CONFIG_SND_HDA_HWDEP |
| 4014 | /* execute additional init verbs */ |
| 4015 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4016 | { |
| 4017 | if (codec->init_verbs.list) |
| 4018 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4019 | } |
| 4020 | #else |
| 4021 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4022 | #endif |
| 4023 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4024 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4025 | /* |
| 4026 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4027 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4028 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4029 | 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] | 4030 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4031 | unsigned int state; |
| 4032 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4033 | codec->in_pm = 1; |
| 4034 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4035 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4036 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4037 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4038 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4039 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4040 | if (!in_wq) |
| 4041 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4042 | spin_lock(&codec->power_lock); |
| 4043 | snd_hda_update_power_acct(codec); |
| 4044 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4045 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4046 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4047 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4048 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4049 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4050 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4053 | /* mark all entries of cmd and amp caches dirty */ |
| 4054 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4055 | { |
| 4056 | int i; |
| 4057 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4058 | struct hda_cache_head *cmd; |
| 4059 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4060 | cmd->dirty = 1; |
| 4061 | } |
| 4062 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4063 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4064 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4065 | amp->head.dirty = 1; |
| 4066 | } |
| 4067 | } |
| 4068 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4069 | /* |
| 4070 | * kick up codec; used both from PM and power-save |
| 4071 | */ |
| 4072 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4073 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4074 | codec->in_pm = 1; |
| 4075 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4076 | hda_mark_cmd_cache_dirty(codec); |
| 4077 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4078 | /* set as if powered on for avoiding re-entering the resume |
| 4079 | * in the resume / power-save sequence |
| 4080 | */ |
| 4081 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4082 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4083 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4084 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4085 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4086 | if (codec->patch_ops.resume) |
| 4087 | codec->patch_ops.resume(codec); |
| 4088 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4089 | if (codec->patch_ops.init) |
| 4090 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4091 | snd_hda_codec_resume_amp(codec); |
| 4092 | snd_hda_codec_resume_cache(codec); |
| 4093 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4094 | |
| 4095 | if (codec->jackpoll_interval) |
| 4096 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4097 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4098 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4099 | |
| 4100 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4101 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4102 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4103 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4104 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | /** |
| 4107 | * snd_hda_build_controls - build mixer controls |
| 4108 | * @bus: the BUS |
| 4109 | * |
| 4110 | * Creates mixer controls for each codec included in the bus. |
| 4111 | * |
| 4112 | * Returns 0 if successful, otherwise a negative error code. |
| 4113 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4114 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4116 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4118 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4119 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4120 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 4121 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4122 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4123 | err = snd_hda_codec_reset(codec); |
| 4124 | if (err < 0) { |
| 4125 | printk(KERN_ERR |
| 4126 | "hda_codec: cannot revert codec\n"); |
| 4127 | return err; |
| 4128 | } |
| 4129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4131 | return 0; |
| 4132 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4133 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4135 | /* |
| 4136 | * add standard channel maps if not specified |
| 4137 | */ |
| 4138 | static int add_std_chmaps(struct hda_codec *codec) |
| 4139 | { |
| 4140 | int i, str, err; |
| 4141 | |
| 4142 | for (i = 0; i < codec->num_pcms; i++) { |
| 4143 | for (str = 0; str < 2; str++) { |
| 4144 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4145 | struct hda_pcm_stream *hinfo = |
| 4146 | &codec->pcm_info[i].stream[str]; |
| 4147 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4148 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4149 | |
| 4150 | if (codec->pcm_info[i].own_chmap) |
| 4151 | continue; |
| 4152 | if (!pcm || !hinfo->substreams) |
| 4153 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4154 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4155 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4156 | hinfo->channels_max, |
| 4157 | 0, &chmap); |
| 4158 | if (err < 0) |
| 4159 | return err; |
| 4160 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4161 | } |
| 4162 | } |
| 4163 | return 0; |
| 4164 | } |
| 4165 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4166 | /* default channel maps for 2.1 speakers; |
| 4167 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4168 | */ |
| 4169 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4170 | { .channels = 2, |
| 4171 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4172 | { .channels = 4, |
| 4173 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4174 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4175 | { } |
| 4176 | }; |
| 4177 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4178 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4179 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4180 | { |
| 4181 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4182 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4183 | /* continue to initialize... */ |
| 4184 | if (codec->patch_ops.init) |
| 4185 | err = codec->patch_ops.init(codec); |
| 4186 | if (!err && codec->patch_ops.build_controls) |
| 4187 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4188 | if (err < 0) |
| 4189 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4190 | |
| 4191 | /* we create chmaps here instead of build_pcms */ |
| 4192 | err = add_std_chmaps(codec); |
| 4193 | if (err < 0) |
| 4194 | return err; |
| 4195 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4196 | if (codec->jackpoll_interval) |
| 4197 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4198 | else |
| 4199 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4200 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | return 0; |
| 4202 | } |
| 4203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | /* |
| 4205 | * stream formats |
| 4206 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4207 | struct hda_rate_tbl { |
| 4208 | unsigned int hz; |
| 4209 | unsigned int alsa_bits; |
| 4210 | unsigned int hda_fmt; |
| 4211 | }; |
| 4212 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4213 | /* rate = base * mult / div */ |
| 4214 | #define HDA_RATE(base, mult, div) \ |
| 4215 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4216 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4217 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4218 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4220 | |
| 4221 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4222 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4223 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4224 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4225 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4226 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4227 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4228 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4229 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4230 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4231 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4232 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4233 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4234 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4235 | |
| 4236 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4237 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4238 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4239 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | }; |
| 4241 | |
| 4242 | /** |
| 4243 | * snd_hda_calc_stream_format - calculate format bitset |
| 4244 | * @rate: the sample rate |
| 4245 | * @channels: the number of channels |
| 4246 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4247 | * @maxbps: the max. bps |
| 4248 | * |
| 4249 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4250 | * |
| 4251 | * Return zero if invalid. |
| 4252 | */ |
| 4253 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4254 | unsigned int channels, |
| 4255 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4256 | unsigned int maxbps, |
| 4257 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | { |
| 4259 | int i; |
| 4260 | unsigned int val = 0; |
| 4261 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4262 | for (i = 0; rate_bits[i].hz; i++) |
| 4263 | if (rate_bits[i].hz == rate) { |
| 4264 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | break; |
| 4266 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4267 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | snd_printdd("invalid rate %d\n", rate); |
| 4269 | return 0; |
| 4270 | } |
| 4271 | |
| 4272 | if (channels == 0 || channels > 8) { |
| 4273 | snd_printdd("invalid channels %d\n", channels); |
| 4274 | return 0; |
| 4275 | } |
| 4276 | val |= channels - 1; |
| 4277 | |
| 4278 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4279 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4280 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4281 | break; |
| 4282 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4283 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4284 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | case 20: |
| 4286 | case 24: |
| 4287 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4288 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4289 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4291 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4293 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | break; |
| 4295 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4296 | snd_printdd("invalid format width %d\n", |
| 4297 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | return 0; |
| 4299 | } |
| 4300 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4301 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4302 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | return val; |
| 4305 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4306 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4308 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4309 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4310 | { |
| 4311 | unsigned int val = 0; |
| 4312 | if (nid != codec->afg && |
| 4313 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4314 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4315 | if (!val || val == -1) |
| 4316 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4317 | if (!val || val == -1) |
| 4318 | return 0; |
| 4319 | return val; |
| 4320 | } |
| 4321 | |
| 4322 | static unsigned int query_pcm_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_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4325 | get_pcm_param); |
| 4326 | } |
| 4327 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4328 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4329 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4330 | { |
| 4331 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4332 | if (!streams || streams == -1) |
| 4333 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4334 | if (!streams || streams == -1) |
| 4335 | return 0; |
| 4336 | return streams; |
| 4337 | } |
| 4338 | |
| 4339 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4340 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4341 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4342 | get_stream_param); |
| 4343 | } |
| 4344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | /** |
| 4346 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4347 | * @codec: the HDA codec |
| 4348 | * @nid: NID to query |
| 4349 | * @ratesp: the pointer to store the detected rate bitflags |
| 4350 | * @formatsp: the pointer to store the detected formats |
| 4351 | * @bpsp: the pointer to store the detected format widths |
| 4352 | * |
| 4353 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4354 | * or @bsps argument is ignored. |
| 4355 | * |
| 4356 | * Returns 0 if successful, otherwise a negative error code. |
| 4357 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4358 | 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] | 4359 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4360 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4361 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4363 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4364 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | |
| 4366 | if (ratesp) { |
| 4367 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4368 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4370 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4372 | if (rates == 0) { |
| 4373 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4374 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4375 | nid, val, |
| 4376 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4377 | return -EIO; |
| 4378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | *ratesp = rates; |
| 4380 | } |
| 4381 | |
| 4382 | if (formatsp || bpsp) { |
| 4383 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4384 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4386 | streams = query_stream_param(codec, nid); |
| 4387 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | |
| 4390 | bps = 0; |
| 4391 | if (streams & AC_SUPFMT_PCM) { |
| 4392 | if (val & AC_SUPPCM_BITS_8) { |
| 4393 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4394 | bps = 8; |
| 4395 | } |
| 4396 | if (val & AC_SUPPCM_BITS_16) { |
| 4397 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4398 | bps = 16; |
| 4399 | } |
| 4400 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4401 | if (val & AC_SUPPCM_BITS_32) |
| 4402 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4403 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4404 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4405 | if (val & AC_SUPPCM_BITS_24) |
| 4406 | bps = 24; |
| 4407 | else if (val & AC_SUPPCM_BITS_20) |
| 4408 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4409 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4410 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4412 | if (val & AC_SUPPCM_BITS_32) |
| 4413 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | else if (val & AC_SUPPCM_BITS_24) |
| 4415 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4416 | else if (val & AC_SUPPCM_BITS_20) |
| 4417 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | } |
| 4419 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4420 | #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] | 4421 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4423 | if (!bps) |
| 4424 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4425 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4426 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4427 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4428 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | /* temporary hack: we have still no proper support |
| 4430 | * for the direct AC3 stream... |
| 4431 | */ |
| 4432 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4433 | bps = 8; |
| 4434 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4435 | if (formats == 0) { |
| 4436 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4437 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4438 | "streams=0x%x)\n", |
| 4439 | nid, val, |
| 4440 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4441 | streams); |
| 4442 | return -EIO; |
| 4443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | if (formatsp) |
| 4445 | *formatsp = formats; |
| 4446 | if (bpsp) |
| 4447 | *bpsp = bps; |
| 4448 | } |
| 4449 | |
| 4450 | return 0; |
| 4451 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4452 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | |
| 4454 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4455 | * snd_hda_is_supported_format - Check the validity of the format |
| 4456 | * @codec: HD-audio codec |
| 4457 | * @nid: NID to check |
| 4458 | * @format: the HD-audio format value to check |
| 4459 | * |
| 4460 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | * |
| 4462 | * Returns 1 if supported, 0 if not. |
| 4463 | */ |
| 4464 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4465 | unsigned int format) |
| 4466 | { |
| 4467 | int i; |
| 4468 | unsigned int val = 0, rate, stream; |
| 4469 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4470 | val = query_pcm_param(codec, nid); |
| 4471 | if (!val) |
| 4472 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | |
| 4474 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4475 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4476 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | if (val & (1 << i)) |
| 4478 | break; |
| 4479 | return 0; |
| 4480 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4481 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | return 0; |
| 4483 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4484 | stream = query_stream_param(codec, nid); |
| 4485 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | return 0; |
| 4487 | |
| 4488 | if (stream & AC_SUPFMT_PCM) { |
| 4489 | switch (format & 0xf0) { |
| 4490 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4491 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | return 0; |
| 4493 | break; |
| 4494 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4495 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | return 0; |
| 4497 | break; |
| 4498 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4499 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | return 0; |
| 4501 | break; |
| 4502 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4503 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | return 0; |
| 4505 | break; |
| 4506 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4507 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | return 0; |
| 4509 | break; |
| 4510 | default: |
| 4511 | return 0; |
| 4512 | } |
| 4513 | } else { |
| 4514 | /* FIXME: check for float32 and AC3? */ |
| 4515 | } |
| 4516 | |
| 4517 | return 1; |
| 4518 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4519 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | |
| 4521 | /* |
| 4522 | * PCM stuff |
| 4523 | */ |
| 4524 | static int hda_pcm_default_open_close(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 | { |
| 4528 | return 0; |
| 4529 | } |
| 4530 | |
| 4531 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4532 | struct hda_codec *codec, |
| 4533 | unsigned int stream_tag, |
| 4534 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4535 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | { |
| 4537 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4538 | return 0; |
| 4539 | } |
| 4540 | |
| 4541 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4542 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4543 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4545 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | } |
| 4548 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4549 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4550 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4552 | int err; |
| 4553 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4554 | /* query support PCM information from the given NID */ |
| 4555 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4556 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4557 | info->rates ? NULL : &info->rates, |
| 4558 | info->formats ? NULL : &info->formats, |
| 4559 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4560 | if (err < 0) |
| 4561 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | } |
| 4563 | if (info->ops.open == NULL) |
| 4564 | info->ops.open = hda_pcm_default_open_close; |
| 4565 | if (info->ops.close == NULL) |
| 4566 | info->ops.close = hda_pcm_default_open_close; |
| 4567 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4568 | if (snd_BUG_ON(!info->nid)) |
| 4569 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | info->ops.prepare = hda_pcm_default_prepare; |
| 4571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4573 | if (snd_BUG_ON(!info->nid)) |
| 4574 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4576 | } |
| 4577 | return 0; |
| 4578 | } |
| 4579 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4580 | /* |
| 4581 | * codec prepare/cleanup entries |
| 4582 | */ |
| 4583 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4584 | struct hda_pcm_stream *hinfo, |
| 4585 | unsigned int stream, |
| 4586 | unsigned int format, |
| 4587 | struct snd_pcm_substream *substream) |
| 4588 | { |
| 4589 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4590 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4591 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4592 | if (ret >= 0) |
| 4593 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4594 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4595 | return ret; |
| 4596 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4597 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4598 | |
| 4599 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4600 | struct hda_pcm_stream *hinfo, |
| 4601 | struct snd_pcm_substream *substream) |
| 4602 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4603 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4604 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4605 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4606 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4607 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4608 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4609 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4610 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4611 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4612 | }; |
| 4613 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4614 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4615 | * get the empty PCM device number to assign |
| 4616 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4617 | 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] | 4618 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4619 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4620 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4621 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4622 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4623 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4624 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4625 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4626 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4627 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4628 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4629 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4630 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4631 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4632 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4633 | return -EINVAL; |
| 4634 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4635 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4636 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4637 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4638 | if (audio_idx[type][i] >= 8) |
| 4639 | break; |
| 4640 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4641 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4642 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4643 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4644 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4645 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4646 | /* non-fixed slots starting from 10 */ |
| 4647 | for (i = 10; i < 32; i++) { |
| 4648 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4649 | return i; |
| 4650 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4651 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4652 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4653 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4654 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4655 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4656 | snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
| 4657 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4658 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4662 | * attach a new PCM stream |
| 4663 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4664 | 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] | 4665 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4666 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4667 | struct hda_pcm_stream *info; |
| 4668 | int stream, err; |
| 4669 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4670 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4671 | return -EINVAL; |
| 4672 | for (stream = 0; stream < 2; stream++) { |
| 4673 | info = &pcm->stream[stream]; |
| 4674 | if (info->substreams) { |
| 4675 | err = set_pcm_default_values(codec, info); |
| 4676 | if (err < 0) |
| 4677 | return err; |
| 4678 | } |
| 4679 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4680 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4681 | } |
| 4682 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4683 | /* assign all PCMs of the given codec */ |
| 4684 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4685 | { |
| 4686 | unsigned int pcm; |
| 4687 | int err; |
| 4688 | |
| 4689 | if (!codec->num_pcms) { |
| 4690 | if (!codec->patch_ops.build_pcms) |
| 4691 | return 0; |
| 4692 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4693 | if (err < 0) { |
| 4694 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4695 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4696 | err = snd_hda_codec_reset(codec); |
| 4697 | if (err < 0) { |
| 4698 | printk(KERN_ERR |
| 4699 | "hda_codec: cannot revert codec\n"); |
| 4700 | return err; |
| 4701 | } |
| 4702 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4703 | } |
| 4704 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4705 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4706 | int dev; |
| 4707 | |
| 4708 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4709 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4710 | |
| 4711 | if (!cpcm->pcm) { |
| 4712 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4713 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4714 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4715 | cpcm->device = dev; |
| 4716 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4717 | if (err < 0) { |
| 4718 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4719 | "PCM stream %d for codec #%d\n", |
| 4720 | dev, codec->addr); |
| 4721 | continue; /* no fatal error */ |
| 4722 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4723 | } |
| 4724 | } |
| 4725 | return 0; |
| 4726 | } |
| 4727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | /** |
| 4729 | * snd_hda_build_pcms - build PCM information |
| 4730 | * @bus: the BUS |
| 4731 | * |
| 4732 | * Create PCM information for each codec included in the bus. |
| 4733 | * |
| 4734 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4735 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4736 | * to create its PCM instances. |
| 4737 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4738 | * callback. |
| 4739 | * |
| 4740 | * At least, substreams, channels_min and channels_max must be filled for |
| 4741 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4742 | * When rates and/or formats are zero, the supported values are queried |
| 4743 | * from the given nid. The nid is used also by the default ops.prepare |
| 4744 | * and ops.cleanup callbacks. |
| 4745 | * |
| 4746 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4747 | * ops.close is supposed to be called in the close callback. |
| 4748 | * ops.prepare should be called in the prepare or hw_params callback |
| 4749 | * with the proper parameters for set up. |
| 4750 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4751 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4752 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4754 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4756 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4758 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4759 | int err = snd_hda_codec_build_pcms(codec); |
| 4760 | if (err < 0) |
| 4761 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | } |
| 4763 | return 0; |
| 4764 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4765 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | /** |
| 4768 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4769 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4770 | * @num_configs: number of config enums |
| 4771 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | * @tbl: configuration table, terminated by null entries |
| 4773 | * |
| 4774 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4775 | * given configuration table. If a matching entry is found, returns its |
| 4776 | * config value (supposed to be 0 or positive). |
| 4777 | * |
| 4778 | * If no entries are matching, the function returns a negative value. |
| 4779 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4780 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4781 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4782 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4784 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4785 | int i; |
| 4786 | for (i = 0; i < num_configs; i++) { |
| 4787 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4788 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4789 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4790 | "selected\n", models[i]); |
| 4791 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | } |
| 4793 | } |
| 4794 | } |
| 4795 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4796 | if (!codec->bus->pci || !tbl) |
| 4797 | return -1; |
| 4798 | |
| 4799 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4800 | if (!tbl) |
| 4801 | return -1; |
| 4802 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4803 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4804 | char tmp[10]; |
| 4805 | const char *model = NULL; |
| 4806 | if (models) |
| 4807 | model = models[tbl->value]; |
| 4808 | if (!model) { |
| 4809 | sprintf(tmp, "#%d", tbl->value); |
| 4810 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4812 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4813 | "for config %x:%x (%s)\n", |
| 4814 | model, tbl->subvendor, tbl->subdevice, |
| 4815 | (tbl->name ? tbl->name : "Unknown device")); |
| 4816 | #endif |
| 4817 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | } |
| 4819 | return -1; |
| 4820 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4821 | EXPORT_SYMBOL_GPL(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | |
| 4823 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4824 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4825 | subsystem ID with the |
| 4826 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4827 | |
| 4828 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4829 | where the vendor ID of the PCI device is: |
| 4830 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4831 | and the vendor/subvendor are found only at the codec. |
| 4832 | |
| 4833 | * @codec: the HDA codec |
| 4834 | * @num_configs: number of config enums |
| 4835 | * @models: array of model name strings |
| 4836 | * @tbl: configuration table, terminated by null entries |
| 4837 | * |
| 4838 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4839 | * given configuration table. If a matching entry is found, returns its |
| 4840 | * config value (supposed to be 0 or positive). |
| 4841 | * |
| 4842 | * If no entries are matching, the function returns a negative value. |
| 4843 | */ |
| 4844 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4845 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4846 | const struct snd_pci_quirk *tbl) |
| 4847 | { |
| 4848 | const struct snd_pci_quirk *q; |
| 4849 | |
| 4850 | /* Search for codec ID */ |
| 4851 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4852 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4853 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4854 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4855 | break; |
| 4856 | } |
| 4857 | |
| 4858 | if (!q->subvendor) |
| 4859 | return -1; |
| 4860 | |
| 4861 | tbl = q; |
| 4862 | |
| 4863 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4864 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4865 | char tmp[10]; |
| 4866 | const char *model = NULL; |
| 4867 | if (models) |
| 4868 | model = models[tbl->value]; |
| 4869 | if (!model) { |
| 4870 | sprintf(tmp, "#%d", tbl->value); |
| 4871 | model = tmp; |
| 4872 | } |
| 4873 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4874 | "for config %x:%x (%s)\n", |
| 4875 | model, tbl->subvendor, tbl->subdevice, |
| 4876 | (tbl->name ? tbl->name : "Unknown device")); |
| 4877 | #endif |
| 4878 | return tbl->value; |
| 4879 | } |
| 4880 | return -1; |
| 4881 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4882 | EXPORT_SYMBOL_GPL(snd_hda_check_board_codec_sid_config); |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4883 | |
| 4884 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | * snd_hda_add_new_ctls - create controls from the array |
| 4886 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4887 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | * |
| 4889 | * This helper function creates and add new controls in the given array. |
| 4890 | * The array must be terminated with an empty entry as terminator. |
| 4891 | * |
| 4892 | * Returns 0 if successful, or a negative error code. |
| 4893 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4894 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4895 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4897 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | |
| 4899 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4900 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4901 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4902 | if (knew->iface == -1) /* skip this codec private value */ |
| 4903 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4904 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4905 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4906 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4907 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4908 | if (addr > 0) |
| 4909 | kctl->id.device = addr; |
| 4910 | if (idx > 0) |
| 4911 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4912 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4913 | if (!err) |
| 4914 | break; |
| 4915 | /* try first with another device index corresponding to |
| 4916 | * the codec addr; if it still fails (or it's the |
| 4917 | * primary codec), then try another control index |
| 4918 | */ |
| 4919 | if (!addr && codec->addr) |
| 4920 | addr = codec->addr; |
| 4921 | else if (!idx && !knew->index) { |
| 4922 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4923 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4924 | if (idx <= 0) |
| 4925 | return err; |
| 4926 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4927 | return err; |
| 4928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | } |
| 4930 | return 0; |
| 4931 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4932 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4934 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4935 | static void hda_power_work(struct work_struct *work) |
| 4936 | { |
| 4937 | struct hda_codec *codec = |
| 4938 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4939 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4940 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4941 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4942 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4943 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4944 | spin_unlock(&codec->power_lock); |
| 4945 | return; |
| 4946 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4947 | if (!codec->power_on || codec->power_count) { |
| 4948 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4949 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4950 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4951 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4952 | spin_unlock(&codec->power_lock); |
| 4953 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4954 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4955 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 4956 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4960 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4961 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4962 | codec->power_count++; |
| 4963 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4964 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4965 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4966 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4967 | } |
| 4968 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4969 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4970 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4971 | { |
| 4972 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4973 | if (codec->power_on) |
| 4974 | codec->power_on_acct += delta; |
| 4975 | else |
| 4976 | codec->power_off_acct += delta; |
| 4977 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4978 | } |
| 4979 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4980 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4981 | * transition to D3 to complete. A pending D3 transition is indicated |
| 4982 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4983 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4984 | 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] | 4985 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4986 | /* Return if power_on or transitioning to power_on, unless currently |
| 4987 | * powering down. */ |
| 4988 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4989 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4990 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4991 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4992 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4993 | cancel_delayed_work_sync(&codec->power_work); |
| 4994 | |
| 4995 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 4996 | /* If the power down delayed work was cancelled above before starting, |
| 4997 | * then there is no need to go through power up here. |
| 4998 | */ |
| 4999 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5000 | if (codec->power_transition < 0) |
| 5001 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5002 | return; |
| 5003 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5004 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5005 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5006 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5007 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5008 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5009 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5010 | spin_unlock(&codec->power_lock); |
| 5011 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5012 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5013 | |
| 5014 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5015 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5016 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5017 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5018 | #define power_save(codec) \ |
| 5019 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5020 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5021 | /* Transition to powered down */ |
| 5022 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5023 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5024 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5025 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5026 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5027 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5028 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5029 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5030 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5031 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | /** |
| 5035 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5036 | * @codec: HD-audio codec |
| 5037 | * @delta: the counter delta to change |
| 5038 | * |
| 5039 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5040 | * appropriately. For the power-down, queue to the delayed action. |
| 5041 | * Passing zero to @delta means to synchronize the power state. |
| 5042 | */ |
| 5043 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5044 | { |
| 5045 | spin_lock(&codec->power_lock); |
| 5046 | codec->power_count += delta; |
| 5047 | trace_hda_power_count(codec); |
| 5048 | if (delta > 0) |
| 5049 | __snd_hda_power_up(codec, d3wait); |
| 5050 | else |
| 5051 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5052 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5053 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5054 | EXPORT_SYMBOL_GPL(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5055 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5056 | /** |
| 5057 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5058 | * @codec: HD-audio codec |
| 5059 | * @check: the object containing an AMP list and the status |
| 5060 | * @nid: NID to check / update |
| 5061 | * |
| 5062 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5063 | * check the current AMP status, and update the the power-status according |
| 5064 | * to the mute status. |
| 5065 | * |
| 5066 | * This function is supposed to be set or called from the check_power_status |
| 5067 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5068 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5069 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5070 | struct hda_loopback_check *check, |
| 5071 | hda_nid_t nid) |
| 5072 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5073 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5074 | int ch, v; |
| 5075 | |
| 5076 | if (!check->amplist) |
| 5077 | return 0; |
| 5078 | for (p = check->amplist; p->nid; p++) { |
| 5079 | if (p->nid == nid) |
| 5080 | break; |
| 5081 | } |
| 5082 | if (!p->nid) |
| 5083 | return 0; /* nothing changed */ |
| 5084 | |
| 5085 | for (p = check->amplist; p->nid; p++) { |
| 5086 | for (ch = 0; ch < 2; ch++) { |
| 5087 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5088 | p->idx); |
| 5089 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5090 | if (!check->power_on) { |
| 5091 | check->power_on = 1; |
| 5092 | snd_hda_power_up(codec); |
| 5093 | } |
| 5094 | return 1; |
| 5095 | } |
| 5096 | } |
| 5097 | } |
| 5098 | if (check->power_on) { |
| 5099 | check->power_on = 0; |
| 5100 | snd_hda_power_down(codec); |
| 5101 | } |
| 5102 | return 0; |
| 5103 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5104 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5105 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5107 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5108 | * Channel mode helper |
| 5109 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5110 | |
| 5111 | /** |
| 5112 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 5113 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5114 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5115 | struct snd_ctl_elem_info *uinfo, |
| 5116 | const struct hda_channel_mode *chmode, |
| 5117 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5118 | { |
| 5119 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5120 | uinfo->count = 1; |
| 5121 | uinfo->value.enumerated.items = num_chmodes; |
| 5122 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5123 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5124 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5125 | chmode[uinfo->value.enumerated.item].channels); |
| 5126 | return 0; |
| 5127 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5128 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5129 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5130 | /** |
| 5131 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 5132 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5133 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5134 | struct snd_ctl_elem_value *ucontrol, |
| 5135 | const struct hda_channel_mode *chmode, |
| 5136 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5137 | int max_channels) |
| 5138 | { |
| 5139 | int i; |
| 5140 | |
| 5141 | for (i = 0; i < num_chmodes; i++) { |
| 5142 | if (max_channels == chmode[i].channels) { |
| 5143 | ucontrol->value.enumerated.item[0] = i; |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | return 0; |
| 5148 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5149 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5150 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5151 | /** |
| 5152 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 5153 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5154 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5155 | struct snd_ctl_elem_value *ucontrol, |
| 5156 | const struct hda_channel_mode *chmode, |
| 5157 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5158 | int *max_channelsp) |
| 5159 | { |
| 5160 | unsigned int mode; |
| 5161 | |
| 5162 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5163 | if (mode >= num_chmodes) |
| 5164 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5165 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5166 | return 0; |
| 5167 | /* change the current channel setting */ |
| 5168 | *max_channelsp = chmode[mode].channels; |
| 5169 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5170 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5171 | return 1; |
| 5172 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5173 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | /* |
| 5176 | * input MUX helper |
| 5177 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5178 | |
| 5179 | /** |
| 5180 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5181 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5182 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5183 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | { |
| 5185 | unsigned int index; |
| 5186 | |
| 5187 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5188 | uinfo->count = 1; |
| 5189 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5190 | if (!imux->num_items) |
| 5191 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | index = uinfo->value.enumerated.item; |
| 5193 | if (index >= imux->num_items) |
| 5194 | index = imux->num_items - 1; |
| 5195 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5196 | return 0; |
| 5197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5198 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5200 | /** |
| 5201 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5202 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5203 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5204 | const struct hda_input_mux *imux, |
| 5205 | struct snd_ctl_elem_value *ucontrol, |
| 5206 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | unsigned int *cur_val) |
| 5208 | { |
| 5209 | unsigned int idx; |
| 5210 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5211 | if (!imux->num_items) |
| 5212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | idx = ucontrol->value.enumerated.item[0]; |
| 5214 | if (idx >= imux->num_items) |
| 5215 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5216 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5218 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5219 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | *cur_val = idx; |
| 5221 | return 1; |
| 5222 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5223 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | |
| 5225 | |
| 5226 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5227 | * process kcontrol info callback of a simple string enum array |
| 5228 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5229 | */ |
| 5230 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5231 | struct snd_ctl_elem_info *uinfo, |
| 5232 | int num_items, const char * const *texts) |
| 5233 | { |
| 5234 | static const char * const texts_default[] = { |
| 5235 | "Disabled", "Enabled" |
| 5236 | }; |
| 5237 | |
| 5238 | if (!texts || !num_items) { |
| 5239 | num_items = 2; |
| 5240 | texts = texts_default; |
| 5241 | } |
| 5242 | |
| 5243 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5244 | uinfo->count = 1; |
| 5245 | uinfo->value.enumerated.items = num_items; |
| 5246 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5247 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5248 | strcpy(uinfo->value.enumerated.name, |
| 5249 | texts[uinfo->value.enumerated.item]); |
| 5250 | return 0; |
| 5251 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5252 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5253 | |
| 5254 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | * Multi-channel / digital-out PCM helper functions |
| 5256 | */ |
| 5257 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5258 | /* setup SPDIF output stream */ |
| 5259 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5260 | unsigned int stream_tag, unsigned int format) |
| 5261 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5262 | struct hda_spdif_out *spdif; |
| 5263 | unsigned int curr_fmt; |
| 5264 | bool reset; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5265 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5266 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5267 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5268 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5269 | reset = codec->spdif_status_reset && |
| 5270 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5271 | curr_fmt != format; |
| 5272 | |
| 5273 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5274 | updated */ |
| 5275 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5276 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5277 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5278 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5279 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5280 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5281 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5282 | for (d = codec->slave_dig_outs; *d; d++) |
| 5283 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5284 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5285 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5286 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5287 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5288 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5289 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5290 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5291 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5292 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5293 | { |
| 5294 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5295 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5296 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5297 | for (d = codec->slave_dig_outs; *d; d++) |
| 5298 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5299 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5300 | } |
| 5301 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5302 | /** |
| 5303 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5304 | * @bus: HD-audio bus |
| 5305 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5306 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5307 | { |
| 5308 | struct hda_codec *codec; |
| 5309 | |
| 5310 | if (!bus) |
| 5311 | return; |
| 5312 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5313 | if (hda_codec_is_power_on(codec) && |
| 5314 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5315 | codec->patch_ops.reboot_notify(codec); |
| 5316 | } |
| 5317 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5318 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5319 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5320 | /** |
| 5321 | * 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] | 5322 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5323 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5324 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5326 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5327 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5328 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5329 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5331 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | return 0; |
| 5333 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5334 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5336 | /** |
| 5337 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5338 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5339 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5340 | struct hda_multi_out *mout, |
| 5341 | unsigned int stream_tag, |
| 5342 | unsigned int format, |
| 5343 | struct snd_pcm_substream *substream) |
| 5344 | { |
| 5345 | mutex_lock(&codec->spdif_mutex); |
| 5346 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5347 | mutex_unlock(&codec->spdif_mutex); |
| 5348 | return 0; |
| 5349 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5350 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5351 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5352 | /** |
| 5353 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5354 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5355 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5356 | struct hda_multi_out *mout) |
| 5357 | { |
| 5358 | mutex_lock(&codec->spdif_mutex); |
| 5359 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5360 | mutex_unlock(&codec->spdif_mutex); |
| 5361 | return 0; |
| 5362 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5363 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5364 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5365 | /** |
| 5366 | * snd_hda_multi_out_dig_close - release the digital out stream |
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_dig_close(struct hda_codec *codec, |
| 5369 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5371 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5373 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | return 0; |
| 5375 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5376 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5378 | /** |
| 5379 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5380 | * |
| 5381 | * Open analog outputs and set up the hw-constraints. |
| 5382 | * If the digital outputs can be opened as slave, open the digital |
| 5383 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5385 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5386 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5387 | struct snd_pcm_substream *substream, |
| 5388 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5390 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5391 | runtime->hw.channels_max = mout->max_channels; |
| 5392 | if (mout->dig_out_nid) { |
| 5393 | if (!mout->analog_rates) { |
| 5394 | mout->analog_rates = hinfo->rates; |
| 5395 | mout->analog_formats = hinfo->formats; |
| 5396 | mout->analog_maxbps = hinfo->maxbps; |
| 5397 | } else { |
| 5398 | runtime->hw.rates = mout->analog_rates; |
| 5399 | runtime->hw.formats = mout->analog_formats; |
| 5400 | hinfo->maxbps = mout->analog_maxbps; |
| 5401 | } |
| 5402 | if (!mout->spdif_rates) { |
| 5403 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5404 | &mout->spdif_rates, |
| 5405 | &mout->spdif_formats, |
| 5406 | &mout->spdif_maxbps); |
| 5407 | } |
| 5408 | mutex_lock(&codec->spdif_mutex); |
| 5409 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5410 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5411 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5412 | runtime->hw.rates &= mout->spdif_rates; |
| 5413 | runtime->hw.formats &= mout->spdif_formats; |
| 5414 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5415 | hinfo->maxbps = mout->spdif_maxbps; |
| 5416 | } else { |
| 5417 | mout->share_spdif = 0; |
| 5418 | /* FIXME: need notify? */ |
| 5419 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5420 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5421 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5424 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5425 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5426 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5428 | /** |
| 5429 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5430 | * |
| 5431 | * Set up the i/o for analog out. |
| 5432 | * 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] | 5433 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5434 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5435 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | unsigned int stream_tag, |
| 5437 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5438 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5440 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5442 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | int i; |
| 5444 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5445 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5446 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5447 | if (mout->dig_out_nid && mout->share_spdif && |
| 5448 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5450 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5451 | format) && |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5452 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5454 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5455 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | } else { |
| 5457 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5458 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | } |
| 5460 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5461 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | |
| 5463 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5464 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5465 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5466 | if (!mout->no_share_stream && |
| 5467 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5469 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5470 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5471 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5472 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5473 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5474 | snd_hda_codec_setup_stream(codec, |
| 5475 | mout->hp_out_nid[i], |
| 5476 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | /* surrounds */ |
| 5479 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5480 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5481 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5482 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5483 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5484 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5485 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5487 | |
| 5488 | /* extra surrounds */ |
| 5489 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5490 | int ch = 0; |
| 5491 | if (!mout->extra_out_nid[i]) |
| 5492 | break; |
| 5493 | if (chs >= (i + 1) * 2) |
| 5494 | ch = i * 2; |
| 5495 | else if (!mout->no_share_stream) |
| 5496 | break; |
| 5497 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5498 | stream_tag, ch, format); |
| 5499 | } |
| 5500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | return 0; |
| 5502 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5503 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5505 | /** |
| 5506 | * 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] | 5507 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5508 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5509 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5511 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | int i; |
| 5513 | |
| 5514 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5515 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5517 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5518 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5519 | if (mout->hp_out_nid[i]) |
| 5520 | snd_hda_codec_cleanup_stream(codec, |
| 5521 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5522 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5523 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5524 | snd_hda_codec_cleanup_stream(codec, |
| 5525 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5526 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | 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] | 5528 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | mout->dig_out_used = 0; |
| 5530 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5531 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | return 0; |
| 5533 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5534 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5536 | /** |
| 5537 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5538 | * |
| 5539 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5540 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5541 | */ |
| 5542 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5543 | { |
| 5544 | unsigned int pincap; |
| 5545 | unsigned int oldval; |
| 5546 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5547 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5548 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5549 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5550 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5551 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5552 | return AC_PINCTL_VREF_80; |
| 5553 | else if (pincap & AC_PINCAP_VREF_50) |
| 5554 | return AC_PINCTL_VREF_50; |
| 5555 | else if (pincap & AC_PINCAP_VREF_100) |
| 5556 | return AC_PINCTL_VREF_100; |
| 5557 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5558 | return AC_PINCTL_VREF_GRD; |
| 5559 | return AC_PINCTL_VREF_HIZ; |
| 5560 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5561 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5562 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5563 | /* correct the pin ctl value for matching with the pin cap */ |
| 5564 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5565 | hda_nid_t pin, unsigned int val) |
| 5566 | { |
| 5567 | static unsigned int cap_lists[][2] = { |
| 5568 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5569 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5570 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5571 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5572 | }; |
| 5573 | unsigned int cap; |
| 5574 | |
| 5575 | if (!val) |
| 5576 | return 0; |
| 5577 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5578 | if (!cap) |
| 5579 | return val; /* don't know what to do... */ |
| 5580 | |
| 5581 | if (val & AC_PINCTL_OUT_EN) { |
| 5582 | if (!(cap & AC_PINCAP_OUT)) |
| 5583 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5584 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5585 | val &= ~AC_PINCTL_HP_EN; |
| 5586 | } |
| 5587 | |
| 5588 | if (val & AC_PINCTL_IN_EN) { |
| 5589 | if (!(cap & AC_PINCAP_IN)) |
| 5590 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5591 | else { |
| 5592 | unsigned int vcap, vref; |
| 5593 | int i; |
| 5594 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5595 | vref = val & AC_PINCTL_VREFEN; |
| 5596 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5597 | if (vref == cap_lists[i][0] && |
| 5598 | !(vcap & cap_lists[i][1])) { |
| 5599 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5600 | vref = AC_PINCTL_VREF_HIZ; |
| 5601 | else |
| 5602 | vref = cap_lists[i + 1][0]; |
| 5603 | } |
| 5604 | } |
| 5605 | val &= ~AC_PINCTL_VREFEN; |
| 5606 | val |= vref; |
| 5607 | } |
| 5608 | } |
| 5609 | |
| 5610 | return val; |
| 5611 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5612 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5613 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5614 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5615 | unsigned int val, bool cached) |
| 5616 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5617 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5618 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5619 | if (cached) |
| 5620 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5621 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5622 | else |
| 5623 | return snd_hda_codec_write(codec, pin, 0, |
| 5624 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5625 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5626 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5627 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5628 | /** |
| 5629 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5630 | * |
| 5631 | * When the same label is used already in the existing items, the number |
| 5632 | * suffix is appended to the label. This label index number is stored |
| 5633 | * to type_idx when non-NULL pointer is given. |
| 5634 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5635 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5636 | int index, int *type_idx) |
| 5637 | { |
| 5638 | int i, label_idx = 0; |
| 5639 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5640 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5641 | return -EINVAL; |
| 5642 | } |
| 5643 | for (i = 0; i < imux->num_items; i++) { |
| 5644 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5645 | label_idx++; |
| 5646 | } |
| 5647 | if (type_idx) |
| 5648 | *type_idx = label_idx; |
| 5649 | if (label_idx > 0) |
| 5650 | snprintf(imux->items[imux->num_items].label, |
| 5651 | sizeof(imux->items[imux->num_items].label), |
| 5652 | "%s %d", label, label_idx); |
| 5653 | else |
| 5654 | strlcpy(imux->items[imux->num_items].label, label, |
| 5655 | sizeof(imux->items[imux->num_items].label)); |
| 5656 | imux->items[imux->num_items].index = index; |
| 5657 | imux->num_items++; |
| 5658 | return 0; |
| 5659 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5660 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5661 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | #ifdef CONFIG_PM |
| 5664 | /* |
| 5665 | * power management |
| 5666 | */ |
| 5667 | |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5668 | |
| 5669 | static void hda_async_suspend(void *data, async_cookie_t cookie) |
| 5670 | { |
| 5671 | hda_call_codec_suspend(data, false); |
| 5672 | } |
| 5673 | |
| 5674 | static void hda_async_resume(void *data, async_cookie_t cookie) |
| 5675 | { |
| 5676 | hda_call_codec_resume(data); |
| 5677 | } |
| 5678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | /** |
| 5680 | * snd_hda_suspend - suspend the codecs |
| 5681 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | * |
| 5683 | * Returns 0 if successful. |
| 5684 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5685 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5687 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5688 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5690 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5691 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5692 | if (hda_codec_is_power_on(codec)) { |
| 5693 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5694 | async_schedule_domain(hda_async_suspend, codec, |
| 5695 | &domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5696 | else |
| 5697 | hda_call_codec_suspend(codec, false); |
| 5698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5700 | |
| 5701 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5702 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | return 0; |
| 5705 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5706 | EXPORT_SYMBOL_GPL(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | |
| 5708 | /** |
| 5709 | * snd_hda_resume - resume the codecs |
| 5710 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | * |
| 5712 | * Returns 0 if successful. |
| 5713 | */ |
| 5714 | int snd_hda_resume(struct hda_bus *bus) |
| 5715 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5716 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5717 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5719 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5720 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5721 | async_schedule_domain(hda_async_resume, codec, &domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5722 | else |
| 5723 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5725 | |
| 5726 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5727 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | return 0; |
| 5730 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5731 | EXPORT_SYMBOL_GPL(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5732 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5733 | |
| 5734 | /* |
| 5735 | * generic arrays |
| 5736 | */ |
| 5737 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5738 | /** |
| 5739 | * snd_array_new - get a new element from the given array |
| 5740 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5741 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5742 | * Get a new element from the given array. If it exceeds the |
| 5743 | * pre-allocated array size, re-allocate the array. |
| 5744 | * |
| 5745 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5746 | */ |
| 5747 | void *snd_array_new(struct snd_array *array) |
| 5748 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5749 | if (snd_BUG_ON(!array->elem_size)) |
| 5750 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5751 | if (array->used >= array->alloced) { |
| 5752 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5753 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5754 | void *nlist; |
| 5755 | if (snd_BUG_ON(num >= 4096)) |
| 5756 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5757 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5758 | if (!nlist) |
| 5759 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5760 | array->list = nlist; |
| 5761 | array->alloced = num; |
| 5762 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5763 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5764 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5765 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5766 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5767 | /** |
| 5768 | * snd_array_free - free the given array elements |
| 5769 | * @array: the array object |
| 5770 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5771 | void snd_array_free(struct snd_array *array) |
| 5772 | { |
| 5773 | kfree(array->list); |
| 5774 | array->used = 0; |
| 5775 | array->alloced = 0; |
| 5776 | array->list = NULL; |
| 5777 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5778 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5779 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5780 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5781 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5782 | * @pcm: PCM caps bits |
| 5783 | * @buf: the string buffer to write |
| 5784 | * @buflen: the max buffer length |
| 5785 | * |
| 5786 | * used by hda_proc.c and hda_eld.c |
| 5787 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5788 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5789 | { |
| 5790 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5791 | int i, j; |
| 5792 | |
| 5793 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5794 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5795 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5796 | |
| 5797 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5798 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5799 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5800 | |
| 5801 | MODULE_DESCRIPTION("HDA codec core"); |
| 5802 | MODULE_LICENSE("GPL"); |