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> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 28 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <sound/core.h> |
| 30 | #include "hda_codec.h" |
| 31 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 32 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 34 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 36 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 37 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 38 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 40 | #define CREATE_TRACE_POINTS |
| 41 | #include "hda_trace.h" |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * vendor / preset table |
| 45 | */ |
| 46 | |
| 47 | struct hda_vendor_id { |
| 48 | unsigned int id; |
| 49 | const char *name; |
| 50 | }; |
| 51 | |
| 52 | /* codec vendor labels */ |
| 53 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 54 | { 0x1002, "ATI" }, |
Takashi Iwai | e5f1424 | 2009-07-01 18:11:44 +0200 | [diff] [blame] | 55 | { 0x1013, "Cirrus Logic" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 56 | { 0x1057, "Motorola" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 57 | { 0x1095, "Silicon Image" }, |
Takashi Iwai | 31117b7 | 2008-12-16 14:43:21 +0100 | [diff] [blame] | 58 | { 0x10de, "Nvidia" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 59 | { 0x10ec, "Realtek" }, |
Takashi Iwai | 4e01f54 | 2009-04-16 08:53:34 +0200 | [diff] [blame] | 60 | { 0x1102, "Creative" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 61 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 62 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 63 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 64 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 66 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 67 | { 0x17e8, "Chrontel" }, |
| 68 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 69 | { 0x1aec, "Wolfson Microelectronics" }, |
Takashi Iwai | dff86f8 | 2014-02-19 11:11:20 +0100 | [diff] [blame] | 70 | { 0x1af4, "QEMU" }, |
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 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 80 | /** |
| 81 | * snd_hda_add_codec_preset - Add a codec preset to the chain |
| 82 | * @preset: codec preset table to add |
| 83 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 84 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 85 | { |
| 86 | mutex_lock(&preset_mutex); |
| 87 | list_add_tail(&preset->list, &hda_preset_tables); |
| 88 | mutex_unlock(&preset_mutex); |
| 89 | return 0; |
| 90 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 91 | EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 92 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 93 | /** |
| 94 | * snd_hda_delete_codec_preset - Delete a codec preset from the chain |
| 95 | * @preset: codec preset table to delete |
| 96 | */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 97 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 98 | { |
| 99 | mutex_lock(&preset_mutex); |
| 100 | list_del(&preset->list); |
| 101 | mutex_unlock(&preset_mutex); |
| 102 | return 0; |
| 103 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 106 | #ifdef CONFIG_PM |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 107 | #define codec_in_pm(codec) ((codec)->in_pm) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 108 | static void hda_power_work(struct work_struct *work); |
| 109 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 110 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 111 | |
| 112 | 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] | 113 | { |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 114 | struct hda_bus *bus = codec->bus; |
| 115 | |
| 116 | if ((power_up && codec->pm_up_notified) || |
| 117 | (!power_up && !codec->pm_up_notified)) |
| 118 | return; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 119 | if (bus->ops.pm_notify) |
| 120 | bus->ops.pm_notify(bus, power_up); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 121 | codec->pm_up_notified = power_up; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 122 | } |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 123 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 124 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 125 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 126 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 127 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 128 | #define hda_call_pm_notify(codec, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 129 | #endif |
| 130 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 131 | /** |
| 132 | * snd_hda_get_jack_location - Give a location string of the jack |
| 133 | * @cfg: pin default config value |
| 134 | * |
| 135 | * Parse the pin default config value and returns the string of the |
| 136 | * jack location, e.g. "Rear", "Front", etc. |
| 137 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 138 | const char *snd_hda_get_jack_location(u32 cfg) |
| 139 | { |
| 140 | static char *bases[7] = { |
| 141 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 142 | }; |
| 143 | static unsigned char specials_idx[] = { |
| 144 | 0x07, 0x08, |
| 145 | 0x17, 0x18, 0x19, |
| 146 | 0x37, 0x38 |
| 147 | }; |
| 148 | static char *specials[] = { |
| 149 | "Rear Panel", "Drive Bar", |
| 150 | "Riser", "HDMI", "ATAPI", |
| 151 | "Mobile-In", "Mobile-Out" |
| 152 | }; |
| 153 | int i; |
| 154 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 155 | if ((cfg & 0x0f) < 7) |
| 156 | return bases[cfg & 0x0f]; |
| 157 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 158 | if (cfg == specials_idx[i]) |
| 159 | return specials[i]; |
| 160 | } |
| 161 | return "UNKNOWN"; |
| 162 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 164 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 165 | /** |
| 166 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 167 | * @cfg: pin default config value |
| 168 | * |
| 169 | * Parse the pin default config value and returns the string of the |
| 170 | * jack connectivity, i.e. external or internal connection. |
| 171 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 172 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 173 | { |
| 174 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 175 | |
| 176 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 177 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 178 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 179 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 180 | /** |
| 181 | * snd_hda_get_jack_type - Give a type string of the jack |
| 182 | * @cfg: pin default config value |
| 183 | * |
| 184 | * Parse the pin default config value and returns the string of the |
| 185 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 186 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 187 | const char *snd_hda_get_jack_type(u32 cfg) |
| 188 | { |
| 189 | static char *jack_types[16] = { |
| 190 | "Line Out", "Speaker", "HP Out", "CD", |
| 191 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 192 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 193 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 197 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 198 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 200 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 203 | */ |
| 204 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 205 | 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] | 206 | unsigned int verb, unsigned int parm) |
| 207 | { |
| 208 | u32 val; |
| 209 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 210 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 211 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 212 | codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n", |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 213 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 214 | return ~0; |
| 215 | } |
| 216 | |
| 217 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 218 | val |= (u32)nid << 20; |
| 219 | val |= verb << 8; |
| 220 | val |= parm; |
| 221 | return val; |
| 222 | } |
| 223 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 224 | /* |
| 225 | * Send and receive a verb |
| 226 | */ |
| 227 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 228 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 229 | { |
| 230 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 231 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 232 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 233 | if (cmd == ~0) |
| 234 | return -1; |
| 235 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 236 | if (res) |
| 237 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 238 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 239 | snd_hda_power_up(codec); |
| 240 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 241 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 242 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 243 | for (;;) { |
| 244 | trace_hda_send_cmd(codec, cmd); |
| 245 | err = bus->ops.command(bus, cmd); |
| 246 | if (err != -EAGAIN) |
| 247 | break; |
| 248 | /* process pending verbs */ |
| 249 | bus->ops.get_response(bus, codec->addr); |
| 250 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 251 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 252 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 253 | trace_hda_get_response(codec, *res); |
| 254 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 255 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 256 | mutex_unlock(&bus->cmd_mutex); |
| 257 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 258 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 259 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 260 | codec_dbg(codec, |
| 261 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 262 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 263 | bus->ops.bus_reset(bus); |
| 264 | } |
| 265 | goto again; |
| 266 | } |
| 267 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 268 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 269 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 270 | return err; |
| 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /** |
| 274 | * snd_hda_codec_read - send a command and get the response |
| 275 | * @codec: the HDA codec |
| 276 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 277 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | * @verb: the verb to send |
| 279 | * @parm: the parameter for the verb |
| 280 | * |
| 281 | * Send a single command and read the corresponding response. |
| 282 | * |
| 283 | * Returns the obtained response value, or -1 for an error. |
| 284 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 285 | 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] | 286 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | unsigned int verb, unsigned int parm) |
| 288 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 289 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 290 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 291 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 292 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return res; |
| 294 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 295 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * snd_hda_codec_write - send a single command without waiting for response |
| 299 | * @codec: the HDA codec |
| 300 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 301 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * @verb: the verb to send |
| 303 | * @parm: the parameter for the verb |
| 304 | * |
| 305 | * Send a single command without waiting for response. |
| 306 | * |
| 307 | * Returns 0 if successful, or a negative error code. |
| 308 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 309 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 310 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 312 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 313 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 314 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 315 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * snd_hda_sequence_write - sequence writes |
| 321 | * @codec: the HDA codec |
| 322 | * @seq: VERB array to send |
| 323 | * |
| 324 | * Send the commands sequentially from the given array. |
| 325 | * The array must be terminated with NID=0. |
| 326 | */ |
| 327 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 328 | { |
| 329 | for (; seq->nid; seq++) |
| 330 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 331 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 336 | * @codec: the HDA codec |
| 337 | * @nid: NID to parse |
| 338 | * @start_id: the pointer to store the start NID |
| 339 | * |
| 340 | * Parse the NID and store the start NID of its sub-nodes. |
| 341 | * Returns the number of sub-nodes. |
| 342 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 343 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 344 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | unsigned int parm; |
| 347 | |
| 348 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 349 | if (parm == -1) |
| 350 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | *start_id = (parm >> 16) & 0x7fff; |
| 352 | return (int)(parm & 0x7fff); |
| 353 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 354 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 356 | /* connection list element */ |
| 357 | struct hda_conn_list { |
| 358 | struct list_head list; |
| 359 | int len; |
| 360 | hda_nid_t nid; |
| 361 | hda_nid_t conns[0]; |
| 362 | }; |
| 363 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 364 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 365 | static struct hda_conn_list * |
| 366 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 367 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 368 | struct hda_conn_list *p; |
| 369 | list_for_each_entry(p, &codec->conn_list, list) { |
| 370 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 371 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 372 | } |
| 373 | return NULL; |
| 374 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 375 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 376 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 377 | const hda_nid_t *list) |
| 378 | { |
| 379 | struct hda_conn_list *p; |
| 380 | |
| 381 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 382 | if (!p) |
| 383 | return -ENOMEM; |
| 384 | p->len = len; |
| 385 | p->nid = nid; |
| 386 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 387 | list_add(&p->list, &codec->conn_list); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void remove_conn_list(struct hda_codec *codec) |
| 392 | { |
| 393 | while (!list_empty(&codec->conn_list)) { |
| 394 | struct hda_conn_list *p; |
| 395 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 396 | list_del(&p->list); |
| 397 | kfree(p); |
| 398 | } |
| 399 | } |
| 400 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 401 | /* read the connection and add to the cache */ |
| 402 | 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] | 403 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 404 | hda_nid_t list[32]; |
| 405 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 406 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 407 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 408 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 409 | if (len == -ENOSPC) { |
| 410 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 411 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 412 | if (!result) |
| 413 | return -ENOMEM; |
| 414 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 415 | } |
| 416 | if (len >= 0) |
| 417 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 418 | if (result != list) |
| 419 | kfree(result); |
| 420 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 421 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 422 | |
| 423 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 424 | * snd_hda_get_conn_list - get connection list |
| 425 | * @codec: the HDA codec |
| 426 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 427 | * @listp: the pointer to store NID list |
| 428 | * |
| 429 | * Parses the connection list of the given widget and stores the pointer |
| 430 | * to the list of NIDs. |
| 431 | * |
| 432 | * Returns the number of connections, or a negative error code. |
| 433 | * |
| 434 | * Note that the returned pointer isn't protected against the list |
| 435 | * modification. If snd_hda_override_conn_list() might be called |
| 436 | * concurrently, protect with a mutex appropriately. |
| 437 | */ |
| 438 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 439 | const hda_nid_t **listp) |
| 440 | { |
| 441 | bool added = false; |
| 442 | |
| 443 | for (;;) { |
| 444 | int err; |
| 445 | const struct hda_conn_list *p; |
| 446 | |
| 447 | /* if the connection-list is already cached, read it */ |
| 448 | p = lookup_conn_list(codec, nid); |
| 449 | if (p) { |
| 450 | if (listp) |
| 451 | *listp = p->conns; |
| 452 | return p->len; |
| 453 | } |
| 454 | if (snd_BUG_ON(added)) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | err = read_and_add_raw_conns(codec, nid); |
| 458 | if (err < 0) |
| 459 | return err; |
| 460 | added = true; |
| 461 | } |
| 462 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 463 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 464 | |
| 465 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 466 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | * @codec: the HDA codec |
| 468 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 469 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | * @max_conns: max. number of connections to store |
| 471 | * |
| 472 | * Parses the connection list of the given widget and stores the list |
| 473 | * of NIDs. |
| 474 | * |
| 475 | * Returns the number of connections, or a negative error code. |
| 476 | */ |
| 477 | 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] | 478 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 479 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 480 | const hda_nid_t *list; |
| 481 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 482 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 483 | if (len > 0 && conn_list) { |
| 484 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 485 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 486 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 487 | return -EINVAL; |
| 488 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 489 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 490 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 491 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 492 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 493 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 495 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 496 | /* return CONNLIST_LEN parameter of the given widget */ |
| 497 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 498 | { |
| 499 | unsigned int wcaps = get_wcaps(codec, nid); |
| 500 | unsigned int parm; |
| 501 | |
| 502 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 503 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 504 | return 0; |
| 505 | |
| 506 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 507 | if (parm == -1) |
| 508 | parm = 0; |
| 509 | return parm; |
| 510 | } |
| 511 | |
| 512 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 513 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 514 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 517 | /** |
| 518 | * snd_hda_get_raw_connections - copy connection list without cache |
| 519 | * @codec: the HDA codec |
| 520 | * @nid: NID to parse |
| 521 | * @conn_list: connection list array |
| 522 | * @max_conns: max. number of connections to store |
| 523 | * |
| 524 | * Like snd_hda_get_connections(), copy the connection list but without |
| 525 | * checking through the connection-list cache. |
| 526 | * Currently called only from hda_proc.c, so not exported. |
| 527 | */ |
| 528 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 529 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
| 531 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 532 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 534 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 535 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 537 | parm = get_num_conns(codec, nid); |
| 538 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 539 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (parm & AC_CLIST_LONG) { |
| 542 | /* long form */ |
| 543 | shift = 16; |
| 544 | num_elems = 2; |
| 545 | } else { |
| 546 | /* short form */ |
| 547 | shift = 8; |
| 548 | num_elems = 4; |
| 549 | } |
| 550 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | mask = (1 << (shift-1)) - 1; |
| 552 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 553 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | return 0; /* no connection */ |
| 555 | |
| 556 | if (conn_len == 1) { |
| 557 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 558 | parm = snd_hda_codec_read(codec, nid, 0, |
| 559 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 560 | if (parm == -1 && codec->bus->rirb_error) |
| 561 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 562 | if (conn_list) |
| 563 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | /* multi connection */ |
| 568 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 569 | prev_nid = 0; |
| 570 | for (i = 0; i < conn_len; i++) { |
| 571 | int range_val; |
| 572 | hda_nid_t val, n; |
| 573 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 574 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 575 | parm = snd_hda_codec_read(codec, nid, 0, |
| 576 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 577 | if (parm == -1 && codec->bus->rirb_error) |
| 578 | return -EIO; |
| 579 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 580 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 581 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 582 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 583 | codec_dbg(codec, |
| 584 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 585 | nid, i, parm); |
| 586 | return 0; |
| 587 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 588 | parm >>= shift; |
| 589 | if (range_val) { |
| 590 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 591 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 592 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 593 | "invalid dep_range_val %x:%x\n", |
| 594 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 595 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 597 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 598 | if (conn_list) { |
| 599 | if (conns >= max_conns) |
| 600 | return -ENOSPC; |
| 601 | conn_list[conns] = n; |
| 602 | } |
| 603 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 604 | } |
| 605 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 606 | if (conn_list) { |
| 607 | if (conns >= max_conns) |
| 608 | return -ENOSPC; |
| 609 | conn_list[conns] = val; |
| 610 | } |
| 611 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 613 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | return conns; |
| 616 | } |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 619 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 620 | * @codec: the HDA codec |
| 621 | * @nid: NID to parse |
| 622 | * @len: number of connection list entries |
| 623 | * @list: the list of connection entries |
| 624 | * |
| 625 | * Add or modify the given connection-list to the cache. If the corresponding |
| 626 | * cache already exists, invalidate it and append a new one. |
| 627 | * |
| 628 | * Returns zero or a negative error code. |
| 629 | */ |
| 630 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 631 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 633 | struct hda_conn_list *p; |
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 | p = lookup_conn_list(codec, nid); |
| 636 | if (p) { |
| 637 | list_del(&p->list); |
| 638 | kfree(p); |
| 639 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 640 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 641 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 644 | |
| 645 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 646 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 647 | * @codec: the HDA codec |
| 648 | * @mux: NID containing the list |
| 649 | * @nid: NID to select |
| 650 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 651 | * |
| 652 | * Parses the connection list of the widget @mux and checks whether the |
| 653 | * widget @nid is present. If it is, return the connection index. |
| 654 | * Otherwise it returns -1. |
| 655 | */ |
| 656 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 657 | hda_nid_t nid, int recursive) |
| 658 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 659 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 660 | int i, nums; |
| 661 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 662 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 663 | for (i = 0; i < nums; i++) |
| 664 | if (conn[i] == nid) |
| 665 | return i; |
| 666 | if (!recursive) |
| 667 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 668 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 669 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 670 | return -1; |
| 671 | } |
| 672 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 673 | for (i = 0; i < nums; i++) { |
| 674 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 675 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 676 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 677 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 678 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 679 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 680 | return -1; |
| 681 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 682 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 684 | |
| 685 | /* return DEVLIST_LEN parameter of the given widget */ |
| 686 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 687 | { |
| 688 | unsigned int wcaps = get_wcaps(codec, nid); |
| 689 | unsigned int parm; |
| 690 | |
| 691 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 692 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 693 | return 0; |
| 694 | |
| 695 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 696 | if (parm == -1 && codec->bus->rirb_error) |
| 697 | parm = 0; |
| 698 | return parm & AC_DEV_LIST_LEN_MASK; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * snd_hda_get_devices - copy device list without cache |
| 703 | * @codec: the HDA codec |
| 704 | * @nid: NID of the pin to parse |
| 705 | * @dev_list: device list array |
| 706 | * @max_devices: max. number of devices to store |
| 707 | * |
| 708 | * Copy the device list. This info is dynamic and so not cached. |
| 709 | * Currently called only from hda_proc.c, so not exported. |
| 710 | */ |
| 711 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 712 | u8 *dev_list, int max_devices) |
| 713 | { |
| 714 | unsigned int parm; |
| 715 | int i, dev_len, devices; |
| 716 | |
| 717 | parm = get_num_devices(codec, nid); |
| 718 | if (!parm) /* not multi-stream capable */ |
| 719 | return 0; |
| 720 | |
| 721 | dev_len = parm + 1; |
| 722 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 723 | |
| 724 | devices = 0; |
| 725 | while (devices < dev_len) { |
| 726 | parm = snd_hda_codec_read(codec, nid, 0, |
| 727 | AC_VERB_GET_DEVICE_LIST, devices); |
| 728 | if (parm == -1 && codec->bus->rirb_error) |
| 729 | break; |
| 730 | |
| 731 | for (i = 0; i < 8; i++) { |
| 732 | dev_list[devices] = (u8)parm; |
| 733 | parm >>= 4; |
| 734 | devices++; |
| 735 | if (devices >= dev_len) |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | return devices; |
| 740 | } |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | /** |
| 743 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 744 | * @bus: the BUS |
| 745 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 746 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 747 | * |
| 748 | * Adds the given event to the queue. The events are processed in |
| 749 | * the workqueue asynchronously. Call this function in the interrupt |
| 750 | * hanlder when RIRB receives an unsolicited event. |
| 751 | * |
| 752 | * Returns 0 if successful, or a negative error code. |
| 753 | */ |
| 754 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 755 | { |
| 756 | struct hda_bus_unsolicited *unsol; |
| 757 | unsigned int wp; |
| 758 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 759 | if (!bus || !bus->workq) |
| 760 | return 0; |
| 761 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 762 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 763 | unsol = bus->unsol; |
| 764 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return 0; |
| 766 | |
| 767 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 768 | unsol->wp = wp; |
| 769 | |
| 770 | wp <<= 1; |
| 771 | unsol->queue[wp] = res; |
| 772 | unsol->queue[wp + 1] = res_ex; |
| 773 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 774 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | return 0; |
| 777 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 778 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 781 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 783 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 785 | struct hda_bus_unsolicited *unsol = |
| 786 | container_of(work, struct hda_bus_unsolicited, work); |
| 787 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct hda_codec *codec; |
| 789 | unsigned int rp, caddr, res; |
| 790 | |
| 791 | while (unsol->rp != unsol->wp) { |
| 792 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 793 | unsol->rp = rp; |
| 794 | rp <<= 1; |
| 795 | res = unsol->queue[rp]; |
| 796 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 797 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | continue; |
| 799 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 800 | if (codec && codec->patch_ops.unsol_event) |
| 801 | codec->patch_ops.unsol_event(codec, res); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * initialize unsolicited queue |
| 807 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 808 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { |
| 810 | struct hda_bus_unsolicited *unsol; |
| 811 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 812 | if (bus->unsol) /* already initialized */ |
| 813 | return 0; |
| 814 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 815 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 816 | if (!unsol) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 817 | dev_err(bus->card->dev, "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return -ENOMEM; |
| 819 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 820 | INIT_WORK(&unsol->work, process_unsol_events); |
| 821 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | bus->unsol = unsol; |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * destructor |
| 828 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 829 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 831 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 832 | return; |
| 833 | |
| 834 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 835 | if (bus->workq) |
| 836 | flush_workqueue(bus->workq); |
| 837 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | kfree(bus->unsol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 847 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 849 | snd_hda_bus_free(device->device_data); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 854 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 856 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 857 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /** |
| 861 | * snd_hda_bus_new - create a HDA bus |
| 862 | * @card: the card entry |
| 863 | * @temp: the template for hda_bus information |
| 864 | * @busp: the pointer to store the created bus instance |
| 865 | * |
| 866 | * Returns 0 if successful, or a negative error code. |
| 867 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 868 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 869 | const struct hda_bus_template *temp, |
| 870 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
| 872 | struct hda_bus *bus; |
| 873 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 874 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 875 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | .dev_free = snd_hda_bus_dev_free, |
| 877 | }; |
| 878 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 879 | if (snd_BUG_ON(!temp)) |
| 880 | return -EINVAL; |
| 881 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 882 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | if (busp) |
| 885 | *busp = NULL; |
| 886 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 887 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 889 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | return -ENOMEM; |
| 891 | } |
| 892 | |
| 893 | bus->card = card; |
| 894 | bus->private_data = temp->private_data; |
| 895 | bus->pci = temp->pci; |
| 896 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 897 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | bus->ops = temp->ops; |
| 899 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 900 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 901 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | INIT_LIST_HEAD(&bus->codec_list); |
| 903 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 904 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 905 | "hd-audio%d", card->number); |
| 906 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 907 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 908 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 909 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 910 | kfree(bus); |
| 911 | return -ENOMEM; |
| 912 | } |
| 913 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 914 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 915 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | snd_hda_bus_free(bus); |
| 917 | return err; |
| 918 | } |
| 919 | if (busp) |
| 920 | *busp = bus; |
| 921 | return 0; |
| 922 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 923 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 925 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 926 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 927 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 928 | #else |
| 929 | #define is_generic_config(codec) 0 |
| 930 | #endif |
| 931 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 932 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 933 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 934 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 935 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 936 | #endif |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * find a matching codec preset |
| 940 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 941 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 942 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 944 | struct hda_codec_preset_list *tbl; |
| 945 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 946 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 948 | again: |
| 949 | mutex_lock(&preset_mutex); |
| 950 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 951 | if (!try_module_get(tbl->owner)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 952 | codec_err(codec, "cannot module_get\n"); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 953 | continue; |
| 954 | } |
| 955 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 957 | if (preset->afg && preset->afg != codec->afg) |
| 958 | continue; |
| 959 | if (preset->mfg && preset->mfg != codec->mfg) |
| 960 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 961 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 963 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 964 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 965 | preset->rev == codec->revision_id)) { |
| 966 | mutex_unlock(&preset_mutex); |
| 967 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 971 | module_put(tbl->owner); |
| 972 | } |
| 973 | mutex_unlock(&preset_mutex); |
| 974 | |
| 975 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 976 | if (!mod_requested) |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 977 | request_module("snd-hda-codec-id:%08x", |
| 978 | codec->vendor_id); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 979 | else |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 980 | request_module("snd-hda-codec-id:%04x*", |
| 981 | (codec->vendor_id >> 16) & 0xffff); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 982 | mod_requested++; |
| 983 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | return NULL; |
| 986 | } |
| 987 | |
| 988 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 989 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 991 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { |
| 993 | const struct hda_vendor_id *c; |
| 994 | const char *vendor = NULL; |
| 995 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 996 | char tmp[16]; |
| 997 | |
| 998 | if (codec->vendor_name) |
| 999 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | for (c = hda_vendor_ids; c->id; c++) { |
| 1002 | if (c->id == vendor_id) { |
| 1003 | vendor = c->name; |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1007 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1009 | vendor = tmp; |
| 1010 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1011 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1012 | if (!codec->vendor_name) |
| 1013 | return -ENOMEM; |
| 1014 | |
| 1015 | get_chip_name: |
| 1016 | if (codec->chip_name) |
| 1017 | return 0; |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1020 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1021 | else { |
| 1022 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1023 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1024 | } |
| 1025 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1026 | return -ENOMEM; |
| 1027 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1031 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1033 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1035 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | hda_nid_t nid; |
| 1037 | |
| 1038 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1039 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1040 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1041 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1042 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1043 | case AC_GRP_AUDIO_FUNCTION: |
| 1044 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1045 | codec->afg_function_id = function_id & 0xff; |
| 1046 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1047 | break; |
| 1048 | case AC_GRP_MODEM_FUNCTION: |
| 1049 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1050 | codec->mfg_function_id = function_id & 0xff; |
| 1051 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1052 | break; |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1060 | * read widget caps for each widget and store in cache |
| 1061 | */ |
| 1062 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1063 | { |
| 1064 | int i; |
| 1065 | hda_nid_t nid; |
| 1066 | |
| 1067 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1068 | &codec->start_nid); |
| 1069 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1070 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1071 | return -ENOMEM; |
| 1072 | nid = codec->start_nid; |
| 1073 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1074 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1075 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1079 | /* read all pin default configurations and save codec->init_pins */ |
| 1080 | static int read_pin_defaults(struct hda_codec *codec) |
| 1081 | { |
| 1082 | int i; |
| 1083 | hda_nid_t nid = codec->start_nid; |
| 1084 | |
| 1085 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1086 | struct hda_pincfg *pin; |
| 1087 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1088 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1089 | if (wid_type != AC_WID_PIN) |
| 1090 | continue; |
| 1091 | pin = snd_array_new(&codec->init_pins); |
| 1092 | if (!pin) |
| 1093 | return -ENOMEM; |
| 1094 | pin->nid = nid; |
| 1095 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1096 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1097 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1098 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1099 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1100 | } |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* look up the given pin config list and return the item matching with NID */ |
| 1105 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1106 | struct snd_array *array, |
| 1107 | hda_nid_t nid) |
| 1108 | { |
| 1109 | int i; |
| 1110 | for (i = 0; i < array->used; i++) { |
| 1111 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1112 | if (pin->nid == nid) |
| 1113 | return pin; |
| 1114 | } |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1118 | /* set the current pin config value for the given NID. |
| 1119 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1120 | */ |
| 1121 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1122 | hda_nid_t nid, unsigned int cfg) |
| 1123 | { |
| 1124 | struct hda_pincfg *pin; |
| 1125 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1126 | /* the check below may be invalid when pins are added by a fixup |
| 1127 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1128 | * for now |
| 1129 | */ |
| 1130 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1131 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1132 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1133 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1134 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1135 | pin = look_up_pincfg(codec, list, nid); |
| 1136 | if (!pin) { |
| 1137 | pin = snd_array_new(list); |
| 1138 | if (!pin) |
| 1139 | return -ENOMEM; |
| 1140 | pin->nid = nid; |
| 1141 | } |
| 1142 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1146 | /** |
| 1147 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1148 | * @codec: the HDA codec |
| 1149 | * @nid: NID to set the pin config |
| 1150 | * @cfg: the pin default config value |
| 1151 | * |
| 1152 | * Override a pin default configuration value in the cache. |
| 1153 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1154 | * priority than the real hardware value. |
| 1155 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1156 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1157 | hda_nid_t nid, unsigned int cfg) |
| 1158 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1159 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1160 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1161 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1162 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1163 | /** |
| 1164 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1165 | * @codec: the HDA codec |
| 1166 | * @nid: NID to get the pin config |
| 1167 | * |
| 1168 | * Get the current pin config value of the given pin NID. |
| 1169 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1170 | * returns the cached value. |
| 1171 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1172 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1173 | { |
| 1174 | struct hda_pincfg *pin; |
| 1175 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1176 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1177 | { |
| 1178 | unsigned int cfg = 0; |
| 1179 | mutex_lock(&codec->user_mutex); |
| 1180 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1181 | if (pin) |
| 1182 | cfg = pin->cfg; |
| 1183 | mutex_unlock(&codec->user_mutex); |
| 1184 | if (cfg) |
| 1185 | return cfg; |
| 1186 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1187 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1188 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1189 | if (pin) |
| 1190 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1191 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1192 | if (pin) |
| 1193 | return pin->cfg; |
| 1194 | return 0; |
| 1195 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1196 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1197 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 1198 | /** |
| 1199 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 1200 | * @codec: the HDA codec |
| 1201 | * @nid: pin NID |
| 1202 | * @val: assigned pinctl value |
| 1203 | * |
| 1204 | * This function stores the given value to a pinctl target value in the |
| 1205 | * pincfg table. This isn't always as same as the actually written value |
| 1206 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 1207 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1208 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1209 | unsigned int val) |
| 1210 | { |
| 1211 | struct hda_pincfg *pin; |
| 1212 | |
| 1213 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1214 | if (!pin) |
| 1215 | return -EINVAL; |
| 1216 | pin->target = val; |
| 1217 | return 0; |
| 1218 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1219 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1220 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 1221 | /** |
| 1222 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1223 | * @codec: the HDA codec |
| 1224 | * @nid: pin NID |
| 1225 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1226 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1227 | { |
| 1228 | struct hda_pincfg *pin; |
| 1229 | |
| 1230 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1231 | if (!pin) |
| 1232 | return 0; |
| 1233 | return pin->target; |
| 1234 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1235 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1236 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1237 | /** |
| 1238 | * snd_hda_shutup_pins - Shut up all pins |
| 1239 | * @codec: the HDA codec |
| 1240 | * |
| 1241 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1242 | * The controls aren't cached so that they can be resumed properly. |
| 1243 | */ |
| 1244 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1245 | { |
| 1246 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1247 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1248 | * the default pin setup at the next load of the driver |
| 1249 | */ |
| 1250 | if (codec->bus->shutdown) |
| 1251 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1252 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1253 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1254 | /* use read here for syncing after issuing each verb */ |
| 1255 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1256 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1257 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1258 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1259 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1260 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1261 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1262 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1263 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1264 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1265 | { |
| 1266 | int i; |
| 1267 | if (!codec->pins_shutup) |
| 1268 | return; |
| 1269 | if (codec->bus->shutdown) |
| 1270 | return; |
| 1271 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1272 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1273 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1274 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1275 | pin->ctrl); |
| 1276 | } |
| 1277 | codec->pins_shutup = 0; |
| 1278 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1279 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1280 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1281 | static void hda_jackpoll_work(struct work_struct *work) |
| 1282 | { |
| 1283 | struct hda_codec *codec = |
| 1284 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1285 | |
| 1286 | snd_hda_jack_set_dirty_all(codec); |
| 1287 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1288 | |
| 1289 | if (!codec->jackpoll_interval) |
| 1290 | return; |
| 1291 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1292 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1293 | codec->jackpoll_interval); |
| 1294 | } |
| 1295 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1296 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1297 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1298 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1299 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1300 | /* release all pincfg lists */ |
| 1301 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1302 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1303 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1304 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1305 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1306 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1307 | snd_array_free(&codec->init_pins); |
| 1308 | } |
| 1309 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1310 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1311 | * audio-converter setup caches |
| 1312 | */ |
| 1313 | struct hda_cvt_setup { |
| 1314 | hda_nid_t nid; |
| 1315 | u8 stream_tag; |
| 1316 | u8 channel_id; |
| 1317 | u16 format_id; |
| 1318 | unsigned char active; /* cvt is currently used */ |
| 1319 | unsigned char dirty; /* setups should be cleared */ |
| 1320 | }; |
| 1321 | |
| 1322 | /* get or create a cache entry for the given audio converter NID */ |
| 1323 | static struct hda_cvt_setup * |
| 1324 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1325 | { |
| 1326 | struct hda_cvt_setup *p; |
| 1327 | int i; |
| 1328 | |
| 1329 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1330 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1331 | if (p->nid == nid) |
| 1332 | return p; |
| 1333 | } |
| 1334 | p = snd_array_new(&codec->cvt_setups); |
| 1335 | if (p) |
| 1336 | p->nid = nid; |
| 1337 | return p; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1341 | * Dynamic symbol binding for the codec parsers |
| 1342 | */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1343 | |
| 1344 | #define load_parser(codec, sym) \ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1345 | ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1346 | |
| 1347 | static void unload_parser(struct hda_codec *codec) |
| 1348 | { |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1349 | if (codec->parser) |
| 1350 | symbol_put_addr(codec->parser); |
| 1351 | codec->parser = NULL; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | * codec destructor |
| 1356 | */ |
| 1357 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1358 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1359 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1361 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1362 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1363 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1364 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1365 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1366 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1367 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1369 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1370 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1371 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1372 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1373 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1375 | if (codec->patch_ops.free) |
| 1376 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1377 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1378 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1379 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1380 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1381 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1382 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1383 | kfree(codec->vendor_name); |
| 1384 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1385 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1386 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1387 | codec->bus->num_codecs--; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1388 | put_device(&codec->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1391 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1392 | hda_nid_t fg, unsigned int power_state); |
| 1393 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1394 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1395 | unsigned int power_state); |
| 1396 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1397 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1398 | { |
| 1399 | struct hda_codec *codec = device->device_data; |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1400 | int err = device_add(&codec->dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1401 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1402 | if (err < 0) |
| 1403 | return err; |
| 1404 | snd_hda_register_beep_device(codec); |
| 1405 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1409 | { |
| 1410 | struct hda_codec *codec = device->device_data; |
| 1411 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1412 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1413 | device_del(&codec->dev); |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1417 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1418 | { |
| 1419 | snd_hda_codec_free(device->device_data); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1423 | /* just free the container */ |
| 1424 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1425 | { |
| 1426 | kfree(container_of(dev, struct hda_codec, dev)); |
| 1427 | } |
| 1428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | /** |
| 1430 | * snd_hda_codec_new - create a HDA codec |
| 1431 | * @bus: the bus to assign |
| 1432 | * @codec_addr: the codec address |
| 1433 | * @codecp: the pointer to store the generated codec |
| 1434 | * |
| 1435 | * Returns 0 if successful, or a negative error code. |
| 1436 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1437 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1438 | unsigned int codec_addr, |
| 1439 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | { |
| 1441 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1442 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1443 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1445 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1446 | .dev_register = snd_hda_codec_dev_register, |
| 1447 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1448 | .dev_free = snd_hda_codec_dev_free, |
| 1449 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1451 | if (snd_BUG_ON(!bus)) |
| 1452 | return -EINVAL; |
| 1453 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1454 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
| 1456 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1457 | dev_err(bus->card->dev, |
| 1458 | "address 0x%x is already occupied\n", |
| 1459 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | return -EBUSY; |
| 1461 | } |
| 1462 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1463 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1465 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | return -ENOMEM; |
| 1467 | } |
| 1468 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1469 | device_initialize(&codec->dev); |
| 1470 | codec->dev.parent = &bus->card->card_dev; |
| 1471 | codec->dev.class = sound_class; |
| 1472 | codec->dev.release = snd_hda_codec_dev_release; |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1473 | codec->dev.groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1474 | dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number, |
| 1475 | codec_addr); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1476 | dev_set_drvdata(&codec->dev, codec); /* for sysfs */ |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | codec->bus = bus; |
| 1479 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1480 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1481 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1482 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1483 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1484 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1485 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1486 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1487 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1488 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1489 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1490 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1491 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1492 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1493 | INIT_LIST_HEAD(&codec->conn_list); |
| 1494 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1495 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1496 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1497 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1499 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1500 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1501 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1502 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1503 | * the caller has to power down appropriatley after initialization |
| 1504 | * phase. |
| 1505 | */ |
| 1506 | hda_keep_power_on(codec); |
| 1507 | #endif |
| 1508 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1509 | snd_hda_sysfs_init(codec); |
| 1510 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1511 | if (codec->bus->modelname) { |
| 1512 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1513 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1514 | err = -ENODEV; |
| 1515 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1520 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | bus->caddr_tbl[codec_addr] = codec; |
| 1523 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1524 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1525 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1526 | if (codec->vendor_id == -1) |
| 1527 | /* read again, hopefully the access method was corrected |
| 1528 | * in the last read... |
| 1529 | */ |
| 1530 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1531 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1532 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1533 | AC_PAR_SUBSYSTEM_ID); |
| 1534 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1535 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1537 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1538 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1539 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1540 | err = -ENODEV; |
| 1541 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1544 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1545 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1546 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1547 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1548 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1549 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1550 | err = read_pin_defaults(codec); |
| 1551 | if (err < 0) |
| 1552 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1553 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1554 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1555 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1556 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1557 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1560 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1561 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1562 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1563 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1564 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1565 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1566 | #ifdef CONFIG_PM |
| 1567 | if (!codec->d3_stop_clk || !codec->epss) |
| 1568 | bus->power_keep_link_on = 1; |
| 1569 | #endif |
| 1570 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1571 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1572 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1573 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1574 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1575 | snd_hda_codec_proc_new(codec); |
| 1576 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1577 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1578 | |
| 1579 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1580 | codec->subsystem_id, codec->revision_id); |
| 1581 | snd_component_add(codec->bus->card, component); |
| 1582 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1583 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1584 | if (err < 0) |
| 1585 | goto error; |
| 1586 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1587 | if (codecp) |
| 1588 | *codecp = codec; |
| 1589 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1590 | |
| 1591 | error: |
| 1592 | snd_hda_codec_free(codec); |
| 1593 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1594 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1595 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1596 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 1597 | /** |
| 1598 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1599 | * @codec: the HDA codec |
| 1600 | * |
| 1601 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1602 | * the given codec. |
| 1603 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1604 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1605 | { |
| 1606 | hda_nid_t fg; |
| 1607 | int err; |
| 1608 | |
| 1609 | /* Assume the function group node does not change, |
| 1610 | * only the widget nodes may change. |
| 1611 | */ |
| 1612 | kfree(codec->wcaps); |
| 1613 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1614 | err = read_widget_caps(codec, fg); |
| 1615 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1616 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1617 | return err; |
| 1618 | } |
| 1619 | |
| 1620 | snd_array_free(&codec->init_pins); |
| 1621 | err = read_pin_defaults(codec); |
| 1622 | |
| 1623 | return err; |
| 1624 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1625 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1626 | |
| 1627 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 1628 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1629 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1630 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1631 | { |
| 1632 | hda_nid_t nid = codec->start_nid; |
| 1633 | int i; |
| 1634 | |
| 1635 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1636 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1637 | switch (get_wcaps_type(wcaps)) { |
| 1638 | case AC_WID_AUD_IN: |
| 1639 | return false; /* HDMI parser supports only HDMI out */ |
| 1640 | case AC_WID_AUD_OUT: |
| 1641 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1642 | return false; |
| 1643 | break; |
| 1644 | } |
| 1645 | } |
| 1646 | return true; |
| 1647 | } |
| 1648 | #else |
| 1649 | /* no HDMI codec parser support */ |
| 1650 | #define is_likely_hdmi_codec(codec) false |
| 1651 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1652 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1653 | /** |
| 1654 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1655 | * @codec: the HDA codec |
| 1656 | * |
| 1657 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1658 | * patch instance. |
| 1659 | * |
| 1660 | * Returns 0 if successful or a negative error code. |
| 1661 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1662 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1663 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1664 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1665 | int err; |
| 1666 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1667 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1668 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1669 | err = get_codec_name(codec); |
| 1670 | if (err < 0) |
| 1671 | return err; |
| 1672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1674 | if (!is_generic_config(codec) && codec->preset) |
| 1675 | patch = codec->preset->patch; |
| 1676 | if (!patch) { |
| 1677 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1678 | if (is_likely_hdmi_codec(codec)) { |
| 1679 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1680 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1681 | #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) |
| 1682 | patch = snd_hda_parse_hdmi_codec; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1683 | #endif |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1684 | } |
| 1685 | if (!patch) { |
| 1686 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 1687 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1688 | #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) |
| 1689 | patch = snd_hda_parse_generic_codec; |
| 1690 | #endif |
| 1691 | } |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1692 | if (!patch) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1693 | codec_err(codec, "No codec parser is available\n"); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1694 | return -ENODEV; |
| 1695 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1698 | err = patch(codec); |
| 1699 | if (err < 0) { |
| 1700 | unload_parser(codec); |
| 1701 | return err; |
| 1702 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1703 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1704 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1705 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1706 | if (err < 0) |
| 1707 | return err; |
| 1708 | } |
| 1709 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1710 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1711 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1712 | snprintf(codec->bus->card->mixername, |
| 1713 | sizeof(codec->bus->card->mixername), |
| 1714 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1715 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1717 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1719 | /* update the stream-id if changed */ |
| 1720 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1721 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1722 | u32 stream_tag, int channel_id) |
| 1723 | { |
| 1724 | unsigned int oldval, newval; |
| 1725 | |
| 1726 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1727 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1728 | newval = (stream_tag << 4) | channel_id; |
| 1729 | if (oldval != newval) |
| 1730 | snd_hda_codec_write(codec, nid, 0, |
| 1731 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1732 | newval); |
| 1733 | p->stream_tag = stream_tag; |
| 1734 | p->channel_id = channel_id; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | /* update the format-id if changed */ |
| 1739 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1740 | hda_nid_t nid, int format) |
| 1741 | { |
| 1742 | unsigned int oldval; |
| 1743 | |
| 1744 | if (p->format_id != format) { |
| 1745 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1746 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1747 | if (oldval != format) { |
| 1748 | msleep(1); |
| 1749 | snd_hda_codec_write(codec, nid, 0, |
| 1750 | AC_VERB_SET_STREAM_FORMAT, |
| 1751 | format); |
| 1752 | } |
| 1753 | p->format_id = format; |
| 1754 | } |
| 1755 | } |
| 1756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | /** |
| 1758 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1759 | * @codec: the CODEC to set up |
| 1760 | * @nid: the NID to set up |
| 1761 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1762 | * @channel_id: channel id to pass, zero based. |
| 1763 | * @format: stream format. |
| 1764 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1765 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1766 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | int channel_id, int format) |
| 1768 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1769 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1770 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1771 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1772 | int i; |
| 1773 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1774 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1775 | return; |
| 1776 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1777 | codec_dbg(codec, |
| 1778 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1779 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1780 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1781 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1782 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1783 | |
| 1784 | if (codec->pcm_format_first) |
| 1785 | update_pcm_format(codec, p, nid, format); |
| 1786 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1787 | if (!codec->pcm_format_first) |
| 1788 | update_pcm_format(codec, p, nid, format); |
| 1789 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1790 | p->active = 1; |
| 1791 | p->dirty = 0; |
| 1792 | |
| 1793 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1794 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1795 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1796 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1797 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1798 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1799 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1800 | p->dirty = 1; |
| 1801 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1804 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1806 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1807 | struct hda_cvt_setup *q); |
| 1808 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1809 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1810 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1811 | * @codec: the CODEC to clean up |
| 1812 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1813 | * @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] | 1814 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1815 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1816 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1817 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1818 | struct hda_cvt_setup *p; |
| 1819 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1820 | if (!nid) |
| 1821 | return; |
| 1822 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1823 | if (codec->no_sticky_stream) |
| 1824 | do_now = 1; |
| 1825 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1826 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1827 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1828 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1829 | /* here we just clear the active flag when do_now isn't set; |
| 1830 | * actual clean-ups will be done later in |
| 1831 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1832 | */ |
| 1833 | if (do_now) |
| 1834 | really_cleanup_stream(codec, p); |
| 1835 | else |
| 1836 | p->active = 0; |
| 1837 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1838 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1839 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1840 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1841 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1842 | struct hda_cvt_setup *q) |
| 1843 | { |
| 1844 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1845 | if (q->stream_tag || q->channel_id) |
| 1846 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1847 | if (q->format_id) |
| 1848 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1849 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1850 | memset(q, 0, sizeof(*q)); |
| 1851 | q->nid = nid; |
| 1852 | } |
| 1853 | |
| 1854 | /* clean up the all conflicting obsolete streams */ |
| 1855 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1856 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1857 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1858 | int i; |
| 1859 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1860 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1861 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1862 | struct hda_cvt_setup *p; |
| 1863 | p = snd_array_elem(&c->cvt_setups, i); |
| 1864 | if (p->dirty) |
| 1865 | really_cleanup_stream(c, p); |
| 1866 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1870 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1871 | /* clean up all streams; called from suspend */ |
| 1872 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1873 | { |
| 1874 | int i; |
| 1875 | |
| 1876 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1877 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1878 | if (p->stream_tag) |
| 1879 | really_cleanup_stream(codec, p); |
| 1880 | } |
| 1881 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1882 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | /* |
| 1885 | * amp access functions |
| 1886 | */ |
| 1887 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1888 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1889 | #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] | 1890 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1891 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1892 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1894 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
| 1896 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1897 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1898 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1900 | memset(cache, 0, sizeof(*cache)); |
| 1901 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1902 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1905 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1906 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1907 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1911 | 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] | 1912 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1913 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1914 | u16 cur = cache->hash[idx]; |
| 1915 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | |
| 1917 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1918 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | if (info->key == key) |
| 1920 | return info; |
| 1921 | cur = info->next; |
| 1922 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1923 | return NULL; |
| 1924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1926 | /* query the hash. allocate an entry if not found. */ |
| 1927 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1928 | u32 key) |
| 1929 | { |
| 1930 | struct hda_cache_head *info = get_hash(cache, key); |
| 1931 | if (!info) { |
| 1932 | u16 idx, cur; |
| 1933 | /* add a new hash entry */ |
| 1934 | info = snd_array_new(&cache->buf); |
| 1935 | if (!info) |
| 1936 | return NULL; |
| 1937 | cur = snd_array_index(&cache->buf, info); |
| 1938 | info->key = key; |
| 1939 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1940 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1941 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1942 | info->next = cache->hash[idx]; |
| 1943 | cache->hash[idx] = cur; |
| 1944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | return info; |
| 1946 | } |
| 1947 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1948 | /* query and allocate an amp hash entry */ |
| 1949 | static inline struct hda_amp_info * |
| 1950 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1951 | { |
| 1952 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1953 | } |
| 1954 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1955 | /* overwrite the value with the key in the caps hash */ |
| 1956 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1957 | { |
| 1958 | struct hda_amp_info *info; |
| 1959 | |
| 1960 | mutex_lock(&codec->hash_mutex); |
| 1961 | info = get_alloc_amp_hash(codec, key); |
| 1962 | if (!info) { |
| 1963 | mutex_unlock(&codec->hash_mutex); |
| 1964 | return -EINVAL; |
| 1965 | } |
| 1966 | info->amp_caps = val; |
| 1967 | info->head.val |= INFO_AMP_CAPS; |
| 1968 | mutex_unlock(&codec->hash_mutex); |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | /* query the value from the caps hash; if not found, fetch the current |
| 1973 | * value from the given function and store in the hash |
| 1974 | */ |
| 1975 | static unsigned int |
| 1976 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1977 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1978 | { |
| 1979 | struct hda_amp_info *info; |
| 1980 | unsigned int val; |
| 1981 | |
| 1982 | mutex_lock(&codec->hash_mutex); |
| 1983 | info = get_alloc_amp_hash(codec, key); |
| 1984 | if (!info) { |
| 1985 | mutex_unlock(&codec->hash_mutex); |
| 1986 | return 0; |
| 1987 | } |
| 1988 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1989 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1990 | val = func(codec, nid, dir); |
| 1991 | write_caps_hash(codec, key, val); |
| 1992 | } else { |
| 1993 | val = info->amp_caps; |
| 1994 | mutex_unlock(&codec->hash_mutex); |
| 1995 | } |
| 1996 | return val; |
| 1997 | } |
| 1998 | |
| 1999 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2000 | int direction) |
| 2001 | { |
| 2002 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 2003 | nid = codec->afg; |
| 2004 | return snd_hda_param_read(codec, nid, |
| 2005 | direction == HDA_OUTPUT ? |
| 2006 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 2007 | } |
| 2008 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2009 | /** |
| 2010 | * query_amp_caps - query AMP capabilities |
| 2011 | * @codec: the HD-auio codec |
| 2012 | * @nid: the NID to query |
| 2013 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 2014 | * |
| 2015 | * Query AMP capabilities for the given widget and direction. |
| 2016 | * Returns the obtained capability bits. |
| 2017 | * |
| 2018 | * When cap bits have been already read, this doesn't read again but |
| 2019 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 2021 | 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] | 2022 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2023 | return query_caps_hash(codec, nid, direction, |
| 2024 | HDA_HASH_KEY(nid, direction, 0), |
| 2025 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2027 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2029 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 2030 | * snd_hda_check_amp_caps - query AMP capabilities |
| 2031 | * @codec: the HD-audio codec |
| 2032 | * @nid: the NID to query |
| 2033 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2034 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 2035 | * |
| 2036 | * Check whether the widget has the given amp capability for the direction. |
| 2037 | */ |
| 2038 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2039 | int dir, unsigned int bits) |
| 2040 | { |
| 2041 | if (!nid) |
| 2042 | return false; |
| 2043 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 2044 | if (query_amp_caps(codec, nid, dir) & bits) |
| 2045 | return true; |
| 2046 | return false; |
| 2047 | } |
| 2048 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 2049 | |
| 2050 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2051 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 2052 | * @codec: the CODEC to clean up |
| 2053 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2054 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2055 | * @caps: the capability bits to set |
| 2056 | * |
| 2057 | * Override the cached AMP caps bits value by the given one. |
| 2058 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 2059 | * e.g. limit to 0dB, etc. |
| 2060 | * |
| 2061 | * Returns zero if successful or a negative error code. |
| 2062 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2063 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2064 | unsigned int caps) |
| 2065 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2066 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2067 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2068 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2069 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2070 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2071 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2072 | { |
| 2073 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 2074 | } |
| 2075 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2076 | /** |
| 2077 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 2078 | * @codec: the HD-auio codec |
| 2079 | * @nid: the NID to query |
| 2080 | * |
| 2081 | * Query PIN capabilities for the given widget. |
| 2082 | * Returns the obtained capability bits. |
| 2083 | * |
| 2084 | * When cap bits have been already read, this doesn't read again but |
| 2085 | * returns the cached value. |
| 2086 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2087 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2088 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2089 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2090 | read_pin_cap); |
| 2091 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2092 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2093 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2094 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2095 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2096 | * @codec: the CODEC |
| 2097 | * @nid: the NID to override |
| 2098 | * @caps: the capability bits to set |
| 2099 | * |
| 2100 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2101 | * |
| 2102 | * Returns zero if successful or a negative error code. |
| 2103 | */ |
| 2104 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2105 | unsigned int caps) |
| 2106 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2107 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2108 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2109 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2110 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2111 | /* read or sync the hash value with the current value; |
| 2112 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2114 | static struct hda_amp_info * |
| 2115 | 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] | 2116 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2118 | struct hda_amp_info *info; |
| 2119 | unsigned int parm, val = 0; |
| 2120 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2122 | retry: |
| 2123 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2124 | if (!info) |
| 2125 | return NULL; |
| 2126 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2127 | if (!val_read) { |
| 2128 | mutex_unlock(&codec->hash_mutex); |
| 2129 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2130 | parm |= direction == HDA_OUTPUT ? |
| 2131 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2132 | parm |= index; |
| 2133 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2134 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2135 | val &= 0xff; |
| 2136 | val_read = true; |
| 2137 | mutex_lock(&codec->hash_mutex); |
| 2138 | goto retry; |
| 2139 | } |
| 2140 | info->vol[ch] = val; |
| 2141 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2142 | } else if (init_only) |
| 2143 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2144 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2148 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2150 | 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] | 2151 | hda_nid_t nid, int ch, int direction, int index, |
| 2152 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | { |
| 2154 | u32 parm; |
| 2155 | |
| 2156 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2157 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2158 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2159 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2160 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2161 | ; /* set the zero value as a fake mute */ |
| 2162 | else |
| 2163 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2165 | } |
| 2166 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2167 | /** |
| 2168 | * snd_hda_codec_amp_read - Read AMP value |
| 2169 | * @codec: HD-audio codec |
| 2170 | * @nid: NID to read the AMP value |
| 2171 | * @ch: channel (left=0 or right=1) |
| 2172 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2173 | * @index: the index value (only for input direction) |
| 2174 | * |
| 2175 | * 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] | 2176 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2177 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2178 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2180 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2181 | unsigned int val = 0; |
| 2182 | |
| 2183 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2184 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2185 | if (info) |
| 2186 | val = info->vol[ch]; |
| 2187 | mutex_unlock(&codec->hash_mutex); |
| 2188 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2190 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2192 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2193 | int direction, int idx, int mask, int val, |
| 2194 | bool init_only) |
| 2195 | { |
| 2196 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2197 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2198 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2199 | |
| 2200 | if (snd_BUG_ON(mask & ~0xff)) |
| 2201 | mask &= 0xff; |
| 2202 | val &= mask; |
| 2203 | |
| 2204 | mutex_lock(&codec->hash_mutex); |
| 2205 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2206 | if (!info) { |
| 2207 | mutex_unlock(&codec->hash_mutex); |
| 2208 | return 0; |
| 2209 | } |
| 2210 | val |= info->vol[ch] & ~mask; |
| 2211 | if (info->vol[ch] == val) { |
| 2212 | mutex_unlock(&codec->hash_mutex); |
| 2213 | return 0; |
| 2214 | } |
| 2215 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2216 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2217 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2218 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2219 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2220 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2221 | return 1; |
| 2222 | } |
| 2223 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2224 | /** |
| 2225 | * snd_hda_codec_amp_update - update the AMP value |
| 2226 | * @codec: HD-audio codec |
| 2227 | * @nid: NID to read the AMP value |
| 2228 | * @ch: channel (left=0 or right=1) |
| 2229 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2230 | * @idx: the index value (only for input direction) |
| 2231 | * @mask: bit mask to set |
| 2232 | * @val: the bits value to set |
| 2233 | * |
| 2234 | * Update the AMP value with a bit mask. |
| 2235 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2236 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2237 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2238 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2240 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2242 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2244 | /** |
| 2245 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2246 | * @codec: HD-audio codec |
| 2247 | * @nid: NID to read the AMP value |
| 2248 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2249 | * @idx: the index value (only for input direction) |
| 2250 | * @mask: bit mask to set |
| 2251 | * @val: the bits value to set |
| 2252 | * |
| 2253 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2254 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2255 | */ |
| 2256 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2257 | int direction, int idx, int mask, int val) |
| 2258 | { |
| 2259 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2260 | |
| 2261 | if (snd_BUG_ON(mask & ~0xff)) |
| 2262 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2263 | for (ch = 0; ch < 2; ch++) |
| 2264 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2265 | idx, mask, val); |
| 2266 | return ret; |
| 2267 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2268 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2269 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 2270 | /** |
| 2271 | * snd_hda_codec_amp_init - initialize the AMP value |
| 2272 | * @codec: the HDA codec |
| 2273 | * @nid: NID to read the AMP value |
| 2274 | * @ch: channel (left=0 or right=1) |
| 2275 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2276 | * @idx: the index value (only for input direction) |
| 2277 | * @mask: bit mask to set |
| 2278 | * @val: the bits value to set |
| 2279 | * |
| 2280 | * Works like snd_hda_codec_amp_update() but it writes the value only at |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2281 | * the first access. If the amp was already initialized / updated beforehand, |
| 2282 | * this does nothing. |
| 2283 | */ |
| 2284 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2285 | int dir, int idx, int mask, int val) |
| 2286 | { |
| 2287 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2288 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2289 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2290 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 2291 | /** |
| 2292 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 2293 | * @codec: the HDA codec |
| 2294 | * @nid: NID to read the AMP value |
| 2295 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2296 | * @idx: the index value (only for input direction) |
| 2297 | * @mask: bit mask to set |
| 2298 | * @val: the bits value to set |
| 2299 | * |
| 2300 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 2301 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2302 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2303 | int dir, int idx, int mask, int val) |
| 2304 | { |
| 2305 | int ch, ret = 0; |
| 2306 | |
| 2307 | if (snd_BUG_ON(mask & ~0xff)) |
| 2308 | mask &= 0xff; |
| 2309 | for (ch = 0; ch < 2; ch++) |
| 2310 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2311 | idx, mask, val); |
| 2312 | return ret; |
| 2313 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2314 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2315 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2316 | /** |
| 2317 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2318 | * @codec: HD-audio codec |
| 2319 | * |
| 2320 | * Resume the all amp commands from the cache. |
| 2321 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2322 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2323 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2324 | int i; |
| 2325 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2326 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2327 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2328 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2329 | struct hda_amp_info *buffer; |
| 2330 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2331 | hda_nid_t nid; |
| 2332 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2333 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2334 | |
| 2335 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2336 | if (!buffer->head.dirty) |
| 2337 | continue; |
| 2338 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2339 | info = *buffer; |
| 2340 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2341 | if (!key) |
| 2342 | continue; |
| 2343 | nid = key & 0xff; |
| 2344 | idx = (key >> 16) & 0xff; |
| 2345 | dir = (key >> 24) & 0xff; |
| 2346 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2347 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2348 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2349 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2350 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2351 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2352 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2353 | } |
| 2354 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2355 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2356 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2357 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2359 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2360 | unsigned int ofs) |
| 2361 | { |
| 2362 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2363 | /* get num steps */ |
| 2364 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2365 | if (ofs < caps) |
| 2366 | caps -= ofs; |
| 2367 | return caps; |
| 2368 | } |
| 2369 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2370 | /** |
| 2371 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2372 | * @kcontrol: referred ctl element |
| 2373 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2374 | * |
| 2375 | * The control element is supposed to have the private_value field |
| 2376 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2377 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2378 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2379 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | { |
| 2381 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2382 | u16 nid = get_amp_nid(kcontrol); |
| 2383 | u8 chs = get_amp_channels(kcontrol); |
| 2384 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2385 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2387 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2388 | uinfo->count = chs == 3 ? 2 : 1; |
| 2389 | uinfo->value.integer.min = 0; |
| 2390 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2391 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2392 | codec_warn(codec, |
| 2393 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2394 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | return -EINVAL; |
| 2396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | return 0; |
| 2398 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2399 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2401 | |
| 2402 | static inline unsigned int |
| 2403 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2404 | int ch, int dir, int idx, unsigned int ofs) |
| 2405 | { |
| 2406 | unsigned int val; |
| 2407 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2408 | val &= HDA_AMP_VOLMASK; |
| 2409 | if (val >= ofs) |
| 2410 | val -= ofs; |
| 2411 | else |
| 2412 | val = 0; |
| 2413 | return val; |
| 2414 | } |
| 2415 | |
| 2416 | static inline int |
| 2417 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2418 | int ch, int dir, int idx, unsigned int ofs, |
| 2419 | unsigned int val) |
| 2420 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2421 | unsigned int maxval; |
| 2422 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2423 | if (val > 0) |
| 2424 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2425 | /* ofs = 0: raw max value */ |
| 2426 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2427 | if (val > maxval) |
| 2428 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2429 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2430 | HDA_AMP_VOLMASK, val); |
| 2431 | } |
| 2432 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2433 | /** |
| 2434 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2435 | * @kcontrol: ctl element |
| 2436 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2437 | * |
| 2438 | * The control element is supposed to have the private_value field |
| 2439 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2440 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2441 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2442 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | { |
| 2444 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2445 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2446 | int chs = get_amp_channels(kcontrol); |
| 2447 | int dir = get_amp_direction(kcontrol); |
| 2448 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2449 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | long *valp = ucontrol->value.integer.value; |
| 2451 | |
| 2452 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2453 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2455 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | return 0; |
| 2457 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2458 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2460 | /** |
| 2461 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2462 | * @kcontrol: ctl element |
| 2463 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2464 | * |
| 2465 | * The control element is supposed to have the private_value field |
| 2466 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2467 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2468 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2469 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | { |
| 2471 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2472 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2473 | int chs = get_amp_channels(kcontrol); |
| 2474 | int dir = get_amp_direction(kcontrol); |
| 2475 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2476 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | long *valp = ucontrol->value.integer.value; |
| 2478 | int change = 0; |
| 2479 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2480 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2481 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2482 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2483 | valp++; |
| 2484 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2485 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2486 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2487 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | return change; |
| 2489 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2490 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2492 | /** |
| 2493 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2494 | * @kcontrol: ctl element |
| 2495 | * @op_flag: operation flag |
| 2496 | * @size: byte size of input TLV |
| 2497 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2498 | * |
| 2499 | * The control element is supposed to have the private_value field |
| 2500 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2501 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2502 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2503 | unsigned int size, unsigned int __user *_tlv) |
| 2504 | { |
| 2505 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2506 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2507 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2508 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2509 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2510 | u32 caps, val1, val2; |
| 2511 | |
| 2512 | if (size < 4 * sizeof(unsigned int)) |
| 2513 | return -ENOMEM; |
| 2514 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2515 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2516 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2517 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2518 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2519 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2520 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2521 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2522 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2523 | return -EFAULT; |
| 2524 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2525 | return -EFAULT; |
| 2526 | if (put_user(val1, _tlv + 2)) |
| 2527 | return -EFAULT; |
| 2528 | if (put_user(val2, _tlv + 3)) |
| 2529 | return -EFAULT; |
| 2530 | return 0; |
| 2531 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2532 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2533 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2534 | /** |
| 2535 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2536 | * @codec: HD-audio codec |
| 2537 | * @nid: NID of a reference widget |
| 2538 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2539 | * @tlv: TLV data to be stored, at least 4 elements |
| 2540 | * |
| 2541 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2542 | * obtained from the reference NID. |
| 2543 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2544 | */ |
| 2545 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2546 | unsigned int *tlv) |
| 2547 | { |
| 2548 | u32 caps; |
| 2549 | int nums, step; |
| 2550 | |
| 2551 | caps = query_amp_caps(codec, nid, dir); |
| 2552 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2553 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2554 | step = (step + 1) * 25; |
| 2555 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2556 | tlv[1] = 2 * sizeof(unsigned int); |
| 2557 | tlv[2] = -nums * step; |
| 2558 | tlv[3] = step; |
| 2559 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2560 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2561 | |
| 2562 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2563 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2564 | 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] | 2565 | { |
| 2566 | struct snd_ctl_elem_id id; |
| 2567 | memset(&id, 0, sizeof(id)); |
| 2568 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2569 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2570 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2571 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2572 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2573 | strcpy(id.name, name); |
| 2574 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2575 | } |
| 2576 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2577 | /** |
| 2578 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2579 | * @codec: HD-audio codec |
| 2580 | * @name: ctl id name string |
| 2581 | * |
| 2582 | * Get the control element with the given id string and IFACE_MIXER. |
| 2583 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2584 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2585 | const char *name) |
| 2586 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2587 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2588 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2589 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2590 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2591 | 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] | 2592 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2593 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2594 | int i, idx; |
| 2595 | /* 16 ctlrs should be large enough */ |
| 2596 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2597 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2598 | return idx; |
| 2599 | } |
| 2600 | return -EBUSY; |
| 2601 | } |
| 2602 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2603 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2604 | * 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] | 2605 | * @codec: HD-audio codec |
| 2606 | * @nid: corresponding NID (optional) |
| 2607 | * @kctl: the control element to assign |
| 2608 | * |
| 2609 | * Add the given control element to an array inside the codec instance. |
| 2610 | * All control elements belonging to a codec are supposed to be added |
| 2611 | * by this function so that a proper clean-up works at the free or |
| 2612 | * reconfiguration time. |
| 2613 | * |
| 2614 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2615 | * The assignment is shown in the codec proc file. |
| 2616 | * |
| 2617 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2618 | * #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] | 2619 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2620 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2621 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2622 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2623 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2624 | { |
| 2625 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2626 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2627 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2628 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2629 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2630 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2631 | if (nid == 0) |
| 2632 | nid = get_amp_nid_(kctl->private_value); |
| 2633 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2634 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2635 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2636 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2637 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2638 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2639 | if (err < 0) |
| 2640 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2641 | item = snd_array_new(&codec->mixers); |
| 2642 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2643 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2644 | item->kctl = kctl; |
| 2645 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2646 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2647 | return 0; |
| 2648 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2649 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2650 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2651 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2652 | * snd_hda_add_nid - Assign a NID to a control element |
| 2653 | * @codec: HD-audio codec |
| 2654 | * @nid: corresponding NID (optional) |
| 2655 | * @kctl: the control element to assign |
| 2656 | * @index: index to kctl |
| 2657 | * |
| 2658 | * Add the given control element to an array inside the codec instance. |
| 2659 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2660 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2661 | */ |
| 2662 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2663 | unsigned int index, hda_nid_t nid) |
| 2664 | { |
| 2665 | struct hda_nid_item *item; |
| 2666 | |
| 2667 | if (nid > 0) { |
| 2668 | item = snd_array_new(&codec->nids); |
| 2669 | if (!item) |
| 2670 | return -ENOMEM; |
| 2671 | item->kctl = kctl; |
| 2672 | item->index = index; |
| 2673 | item->nid = nid; |
| 2674 | return 0; |
| 2675 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2676 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2677 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2678 | return -EINVAL; |
| 2679 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2680 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2681 | |
| 2682 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2683 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2684 | * @codec: HD-audio codec |
| 2685 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2686 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2687 | { |
| 2688 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2689 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2690 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2691 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2692 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2693 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2694 | } |
| 2695 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 2696 | /** |
| 2697 | * snd_hda_lock_devices - pseudo device locking |
| 2698 | * @bus: the BUS |
| 2699 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2700 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2701 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2702 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2703 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2704 | struct snd_card *card = bus->card; |
| 2705 | struct hda_codec *codec; |
| 2706 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2707 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2708 | if (card->shutdown) |
| 2709 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2710 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2711 | if (!list_empty(&card->ctl_files)) |
| 2712 | goto err_clear; |
| 2713 | |
| 2714 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2715 | int pcm; |
| 2716 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2717 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2718 | if (!cpcm->pcm) |
| 2719 | continue; |
| 2720 | if (cpcm->pcm->streams[0].substream_opened || |
| 2721 | cpcm->pcm->streams[1].substream_opened) |
| 2722 | goto err_clear; |
| 2723 | } |
| 2724 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2725 | spin_unlock(&card->files_lock); |
| 2726 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2727 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2728 | err_clear: |
| 2729 | card->shutdown = 0; |
| 2730 | err_unlock: |
| 2731 | spin_unlock(&card->files_lock); |
| 2732 | return -EINVAL; |
| 2733 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2734 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2735 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 2736 | /** |
| 2737 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2738 | * @bus: the BUS |
| 2739 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2740 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2741 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2742 | struct snd_card *card = bus->card; |
| 2743 | |
| 2744 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2745 | spin_lock(&card->files_lock); |
| 2746 | card->shutdown = 0; |
| 2747 | spin_unlock(&card->files_lock); |
| 2748 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2749 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2750 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2751 | /** |
| 2752 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2753 | * @codec: HD-audio codec |
| 2754 | * |
| 2755 | * This frees the all PCM and control elements assigned to the codec, and |
| 2756 | * clears the caches and restores the pin default configurations. |
| 2757 | * |
| 2758 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2759 | * returns zero. |
| 2760 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2761 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2762 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2763 | struct hda_bus *bus = codec->bus; |
| 2764 | struct snd_card *card = bus->card; |
| 2765 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2766 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2767 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2768 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2769 | |
| 2770 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2771 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2772 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2773 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2774 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2775 | #endif |
| 2776 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2777 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2778 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2779 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2780 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2781 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2782 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2783 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2784 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2785 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2786 | if (codec->patch_ops.free) |
| 2787 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2788 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2789 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2790 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2791 | codec->spec = NULL; |
| 2792 | free_hda_cache(&codec->amp_cache); |
| 2793 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2794 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2795 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2796 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2797 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2798 | snd_array_free(&codec->cvt_setups); |
| 2799 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2800 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2801 | codec->num_pcms = 0; |
| 2802 | codec->pcm_info = NULL; |
| 2803 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2804 | codec->slave_dig_outs = NULL; |
| 2805 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2806 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2807 | module_put(codec->owner); |
| 2808 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2809 | |
| 2810 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2811 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2812 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2813 | } |
| 2814 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2815 | typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2818 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2819 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2820 | { |
| 2821 | struct hda_nid_item *items; |
| 2822 | const char * const *s; |
| 2823 | int i, err; |
| 2824 | |
| 2825 | items = codec->mixers.list; |
| 2826 | for (i = 0; i < codec->mixers.used; i++) { |
| 2827 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2828 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2829 | continue; |
| 2830 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2831 | char tmpname[sizeof(sctl->id.name)]; |
| 2832 | const char *name = *s; |
| 2833 | if (suffix) { |
| 2834 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2835 | name, suffix); |
| 2836 | name = tmpname; |
| 2837 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2838 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2839 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2840 | if (err) |
| 2841 | return err; |
| 2842 | break; |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | return 0; |
| 2847 | } |
| 2848 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2849 | static int check_slave_present(struct hda_codec *codec, |
| 2850 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2851 | { |
| 2852 | return 1; |
| 2853 | } |
| 2854 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2855 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2856 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2857 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2858 | { |
| 2859 | int _tlv[4]; |
| 2860 | const int *tlv = NULL; |
| 2861 | int val = -1; |
| 2862 | |
| 2863 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2864 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2865 | mm_segment_t fs = get_fs(); |
| 2866 | set_fs(get_ds()); |
| 2867 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2868 | tlv = _tlv; |
| 2869 | set_fs(fs); |
| 2870 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2871 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2872 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2873 | int step = tlv[3]; |
| 2874 | step &= ~TLV_DB_SCALE_MUTE; |
| 2875 | if (!step) |
| 2876 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2877 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2878 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2879 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2880 | return -1; |
| 2881 | } |
| 2882 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2883 | val = -tlv[2] / step; |
| 2884 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2885 | return val; |
| 2886 | } |
| 2887 | |
| 2888 | /* call kctl->put with the given value(s) */ |
| 2889 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2890 | { |
| 2891 | struct snd_ctl_elem_value *ucontrol; |
| 2892 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2893 | if (!ucontrol) |
| 2894 | return -ENOMEM; |
| 2895 | ucontrol->value.integer.value[0] = val; |
| 2896 | ucontrol->value.integer.value[1] = val; |
| 2897 | kctl->put(kctl, ucontrol); |
| 2898 | kfree(ucontrol); |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2903 | static int init_slave_0dB(struct hda_codec *codec, |
| 2904 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2905 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2906 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2907 | if (offset > 0) |
| 2908 | put_kctl_with_value(slave, offset); |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
| 2912 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2913 | static int init_slave_unmute(struct hda_codec *codec, |
| 2914 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2915 | { |
| 2916 | return put_kctl_with_value(slave, 1); |
| 2917 | } |
| 2918 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2919 | static int add_slave(struct hda_codec *codec, |
| 2920 | void *data, struct snd_kcontrol *slave) |
| 2921 | { |
| 2922 | return snd_ctl_add_slave(data, slave); |
| 2923 | } |
| 2924 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2925 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 2926 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2927 | * @codec: HD-audio codec |
| 2928 | * @name: vmaster control name |
| 2929 | * @tlv: TLV data (optional) |
| 2930 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2931 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2932 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2933 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2934 | * |
| 2935 | * Create a virtual master control with the given name. The TLV data |
| 2936 | * must be either NULL or a valid data. |
| 2937 | * |
| 2938 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2939 | * slave control name. All controls with these names are assigned to |
| 2940 | * the new virtual master control. |
| 2941 | * |
| 2942 | * This function returns zero if successful or a negative error code. |
| 2943 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2944 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2945 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2946 | const char *suffix, bool init_slave_vol, |
| 2947 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2948 | { |
| 2949 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2950 | int err; |
| 2951 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2952 | if (ctl_ret) |
| 2953 | *ctl_ret = NULL; |
| 2954 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2955 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2956 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2957 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2958 | return 0; |
| 2959 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2960 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2961 | if (!kctl) |
| 2962 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2963 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2964 | if (err < 0) |
| 2965 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2966 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2967 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2968 | if (err < 0) |
| 2969 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2970 | |
| 2971 | /* init with master mute & zero volume */ |
| 2972 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2973 | if (init_slave_vol) { |
| 2974 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2975 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2976 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2977 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2978 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2979 | if (ctl_ret) |
| 2980 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2981 | return 0; |
| 2982 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2983 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2984 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2985 | /* |
| 2986 | * mute-LED control using vmaster |
| 2987 | */ |
| 2988 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2989 | struct snd_ctl_elem_info *uinfo) |
| 2990 | { |
| 2991 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2992 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2993 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2994 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2995 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2999 | struct snd_ctl_elem_value *ucontrol) |
| 3000 | { |
| 3001 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 3002 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 3007 | struct snd_ctl_elem_value *ucontrol) |
| 3008 | { |
| 3009 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 3010 | unsigned int old_mode = hook->mute_mode; |
| 3011 | |
| 3012 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 3013 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 3014 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 3015 | if (old_mode == hook->mute_mode) |
| 3016 | return 0; |
| 3017 | snd_hda_sync_vmaster_hook(hook); |
| 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 3022 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3023 | .name = "Mute-LED Mode", |
| 3024 | .info = vmaster_mute_mode_info, |
| 3025 | .get = vmaster_mute_mode_get, |
| 3026 | .put = vmaster_mute_mode_put, |
| 3027 | }; |
| 3028 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 3029 | /** |
| 3030 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 3031 | * @codec: the HDA codec |
| 3032 | * @hook: the vmaster hook object |
| 3033 | * @expose_enum_ctl: flag to create an enum ctl |
| 3034 | * |
| 3035 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 3036 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 3037 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3038 | */ |
| 3039 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 3040 | struct hda_vmaster_mute_hook *hook, |
| 3041 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3042 | { |
| 3043 | struct snd_kcontrol *kctl; |
| 3044 | |
| 3045 | if (!hook->hook || !hook->sw_kctl) |
| 3046 | return 0; |
| 3047 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 3048 | hook->codec = codec; |
| 3049 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 3050 | if (!expose_enum_ctl) |
| 3051 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3052 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 3053 | if (!kctl) |
| 3054 | return -ENOMEM; |
| 3055 | return snd_hda_ctl_add(codec, 0, kctl); |
| 3056 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3057 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3058 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 3059 | /** |
| 3060 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 3061 | * @hook: the vmaster hook |
| 3062 | * |
| 3063 | * Call the hook with the current value for synchronization. |
| 3064 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3065 | */ |
| 3066 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 3067 | { |
| 3068 | if (!hook->hook || !hook->codec) |
| 3069 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 3070 | /* don't call vmaster hook in the destructor since it might have |
| 3071 | * been already destroyed |
| 3072 | */ |
| 3073 | if (hook->codec->bus->shutdown) |
| 3074 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3075 | switch (hook->mute_mode) { |
| 3076 | case HDA_VMUTE_FOLLOW_MASTER: |
| 3077 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 3078 | break; |
| 3079 | default: |
| 3080 | hook->hook(hook->codec, hook->mute_mode); |
| 3081 | break; |
| 3082 | } |
| 3083 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3084 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3085 | |
| 3086 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3087 | /** |
| 3088 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3089 | * @kcontrol: referred ctl element |
| 3090 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3091 | * |
| 3092 | * The control element is supposed to have the private_value field |
| 3093 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3094 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3095 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 3096 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | { |
| 3098 | int chs = get_amp_channels(kcontrol); |
| 3099 | |
| 3100 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 3101 | uinfo->count = chs == 3 ? 2 : 1; |
| 3102 | uinfo->value.integer.min = 0; |
| 3103 | uinfo->value.integer.max = 1; |
| 3104 | return 0; |
| 3105 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3106 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3108 | /** |
| 3109 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3110 | * @kcontrol: ctl element |
| 3111 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3112 | * |
| 3113 | * The control element is supposed to have the private_value field |
| 3114 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3115 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3116 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 3117 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | { |
| 3119 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3120 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3121 | int chs = get_amp_channels(kcontrol); |
| 3122 | int dir = get_amp_direction(kcontrol); |
| 3123 | int idx = get_amp_index(kcontrol); |
| 3124 | long *valp = ucontrol->value.integer.value; |
| 3125 | |
| 3126 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3127 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3128 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3130 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3131 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | return 0; |
| 3133 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3134 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3136 | /** |
| 3137 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3138 | * @kcontrol: ctl element |
| 3139 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3140 | * |
| 3141 | * The control element is supposed to have the private_value field |
| 3142 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3143 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3144 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3145 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | { |
| 3147 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3148 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3149 | int chs = get_amp_channels(kcontrol); |
| 3150 | int dir = get_amp_direction(kcontrol); |
| 3151 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | long *valp = ucontrol->value.integer.value; |
| 3153 | int change = 0; |
| 3154 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3155 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3156 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3157 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3158 | HDA_AMP_MUTE, |
| 3159 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3160 | valp++; |
| 3161 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3162 | if (chs & 2) |
| 3163 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3164 | HDA_AMP_MUTE, |
| 3165 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3166 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3167 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | return change; |
| 3169 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3170 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | |
| 3172 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3173 | * bound volume controls |
| 3174 | * |
| 3175 | * bind multiple volumes (# indices, from 0) |
| 3176 | */ |
| 3177 | |
| 3178 | #define AMP_VAL_IDX_SHIFT 19 |
| 3179 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3180 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3181 | /** |
| 3182 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3183 | * @kcontrol: ctl element |
| 3184 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3185 | * |
| 3186 | * The control element is supposed to have the private_value field |
| 3187 | * set up via HDA_BIND_MUTE*() macros. |
| 3188 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3189 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3190 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3191 | { |
| 3192 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3193 | unsigned long pval; |
| 3194 | int err; |
| 3195 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3196 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3197 | pval = kcontrol->private_value; |
| 3198 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3199 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3200 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3201 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3202 | return err; |
| 3203 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3204 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3205 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3206 | /** |
| 3207 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3208 | * @kcontrol: ctl element |
| 3209 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3210 | * |
| 3211 | * The control element is supposed to have the private_value field |
| 3212 | * set up via HDA_BIND_MUTE*() macros. |
| 3213 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3214 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3215 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3216 | { |
| 3217 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3218 | unsigned long pval; |
| 3219 | int i, indices, err = 0, change = 0; |
| 3220 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3221 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3222 | pval = kcontrol->private_value; |
| 3223 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3224 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3225 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3226 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3227 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3228 | if (err < 0) |
| 3229 | break; |
| 3230 | change |= err; |
| 3231 | } |
| 3232 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3233 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3234 | return err < 0 ? err : change; |
| 3235 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3236 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3237 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3238 | /** |
| 3239 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3240 | * @kcontrol: referred ctl element |
| 3241 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3242 | * |
| 3243 | * The control element is supposed to have the private_value field |
| 3244 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3245 | */ |
| 3246 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3247 | struct snd_ctl_elem_info *uinfo) |
| 3248 | { |
| 3249 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3250 | struct hda_bind_ctls *c; |
| 3251 | int err; |
| 3252 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3253 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3254 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3255 | kcontrol->private_value = *c->values; |
| 3256 | err = c->ops->info(kcontrol, uinfo); |
| 3257 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3258 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3259 | return err; |
| 3260 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3261 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3262 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3263 | /** |
| 3264 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3265 | * @kcontrol: ctl element |
| 3266 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3267 | * |
| 3268 | * The control element is supposed to have the private_value field |
| 3269 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3270 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3271 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3272 | struct snd_ctl_elem_value *ucontrol) |
| 3273 | { |
| 3274 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3275 | struct hda_bind_ctls *c; |
| 3276 | int err; |
| 3277 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3278 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3279 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3280 | kcontrol->private_value = *c->values; |
| 3281 | err = c->ops->get(kcontrol, ucontrol); |
| 3282 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3283 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3284 | return err; |
| 3285 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3286 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3287 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3288 | /** |
| 3289 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3290 | * @kcontrol: ctl element |
| 3291 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3292 | * |
| 3293 | * The control element is supposed to have the private_value field |
| 3294 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3295 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3296 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3297 | struct snd_ctl_elem_value *ucontrol) |
| 3298 | { |
| 3299 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3300 | struct hda_bind_ctls *c; |
| 3301 | unsigned long *vals; |
| 3302 | int err = 0, change = 0; |
| 3303 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3304 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3305 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3306 | for (vals = c->values; *vals; vals++) { |
| 3307 | kcontrol->private_value = *vals; |
| 3308 | err = c->ops->put(kcontrol, ucontrol); |
| 3309 | if (err < 0) |
| 3310 | break; |
| 3311 | change |= err; |
| 3312 | } |
| 3313 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3314 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3315 | return err < 0 ? err : change; |
| 3316 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3317 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3318 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3319 | /** |
| 3320 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3321 | * @kcontrol: ctl element |
| 3322 | * @op_flag: operation flag |
| 3323 | * @size: byte size of input TLV |
| 3324 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3325 | * |
| 3326 | * The control element is supposed to have the private_value field |
| 3327 | * set up via HDA_BIND_VOL() macro. |
| 3328 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3329 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3330 | unsigned int size, unsigned int __user *tlv) |
| 3331 | { |
| 3332 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3333 | struct hda_bind_ctls *c; |
| 3334 | int err; |
| 3335 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3336 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3337 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3338 | kcontrol->private_value = *c->values; |
| 3339 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3340 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3341 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3342 | return err; |
| 3343 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3344 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3345 | |
| 3346 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3347 | .info = snd_hda_mixer_amp_volume_info, |
| 3348 | .get = snd_hda_mixer_amp_volume_get, |
| 3349 | .put = snd_hda_mixer_amp_volume_put, |
| 3350 | .tlv = snd_hda_mixer_amp_tlv |
| 3351 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3352 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3353 | |
| 3354 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3355 | .info = snd_hda_mixer_amp_switch_info, |
| 3356 | .get = snd_hda_mixer_amp_switch_get, |
| 3357 | .put = snd_hda_mixer_amp_switch_put, |
| 3358 | .tlv = snd_hda_mixer_amp_tlv |
| 3359 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3360 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3361 | |
| 3362 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | * SPDIF out controls |
| 3364 | */ |
| 3365 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3366 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3367 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | { |
| 3369 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3370 | uinfo->count = 1; |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3374 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3375 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | { |
| 3377 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3378 | IEC958_AES0_NONAUDIO | |
| 3379 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3380 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3381 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3382 | IEC958_AES1_CON_ORIGINAL; |
| 3383 | return 0; |
| 3384 | } |
| 3385 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3386 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3387 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | { |
| 3389 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3390 | IEC958_AES0_NONAUDIO | |
| 3391 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3392 | return 0; |
| 3393 | } |
| 3394 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3395 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3396 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | { |
| 3398 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3399 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3400 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3402 | mutex_lock(&codec->spdif_mutex); |
| 3403 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3404 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3405 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3406 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3407 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3408 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
| 3413 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3414 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3415 | */ |
| 3416 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3417 | { |
| 3418 | unsigned short val = 0; |
| 3419 | |
| 3420 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3421 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3423 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3425 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3426 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3427 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3429 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3430 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3431 | val |= AC_DIG1_EMPHASIS; |
| 3432 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3433 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3435 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3437 | } |
| 3438 | return val; |
| 3439 | } |
| 3440 | |
| 3441 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3442 | */ |
| 3443 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3444 | { |
| 3445 | unsigned int sbits = 0; |
| 3446 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3447 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3449 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3451 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3452 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3454 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3455 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3457 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3459 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3461 | sbits |= val & (0x7f << 8); |
| 3462 | } |
| 3463 | return sbits; |
| 3464 | } |
| 3465 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3466 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3467 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3468 | int verb, int val) |
| 3469 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3470 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3471 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3472 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3473 | d = codec->slave_dig_outs; |
| 3474 | if (!d) |
| 3475 | return; |
| 3476 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3477 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3481 | int dig1, int dig2) |
| 3482 | { |
| 3483 | if (dig1 != -1) |
| 3484 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3485 | if (dig2 != -1) |
| 3486 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3487 | } |
| 3488 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3489 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3490 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | { |
| 3492 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3493 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3494 | struct hda_spdif_out *spdif; |
| 3495 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | unsigned short val; |
| 3497 | int change; |
| 3498 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3499 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3500 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3501 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3502 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3504 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3505 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3506 | val = convert_from_spdif_status(spdif->status); |
| 3507 | val |= spdif->ctls & 1; |
| 3508 | change = spdif->ctls != val; |
| 3509 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3510 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3511 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3512 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | return change; |
| 3514 | } |
| 3515 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3516 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3518 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3519 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | { |
| 3521 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3522 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3523 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3525 | mutex_lock(&codec->spdif_mutex); |
| 3526 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3527 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3528 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | return 0; |
| 3530 | } |
| 3531 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3532 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3533 | int dig1, int dig2) |
| 3534 | { |
| 3535 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3536 | /* unmute amp switch (if any) */ |
| 3537 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3538 | (dig1 & AC_DIG1_ENABLE)) |
| 3539 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3540 | HDA_AMP_MUTE, 0); |
| 3541 | } |
| 3542 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3543 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3544 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | { |
| 3546 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3547 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3548 | struct hda_spdif_out *spdif; |
| 3549 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | unsigned short val; |
| 3551 | int change; |
| 3552 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3553 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3554 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3555 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3556 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3558 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3559 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3560 | spdif->ctls = val; |
| 3561 | if (change && nid != (u16)-1) |
| 3562 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3563 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | return change; |
| 3565 | } |
| 3566 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3567 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | { |
| 3569 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3570 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3571 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | .info = snd_hda_spdif_mask_info, |
| 3573 | .get = snd_hda_spdif_cmask_get, |
| 3574 | }, |
| 3575 | { |
| 3576 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3577 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3578 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | .info = snd_hda_spdif_mask_info, |
| 3580 | .get = snd_hda_spdif_pmask_get, |
| 3581 | }, |
| 3582 | { |
| 3583 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3584 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | .info = snd_hda_spdif_mask_info, |
| 3586 | .get = snd_hda_spdif_default_get, |
| 3587 | .put = snd_hda_spdif_default_put, |
| 3588 | }, |
| 3589 | { |
| 3590 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3591 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | .info = snd_hda_spdif_out_switch_info, |
| 3593 | .get = snd_hda_spdif_out_switch_get, |
| 3594 | .put = snd_hda_spdif_out_switch_put, |
| 3595 | }, |
| 3596 | { } /* end */ |
| 3597 | }; |
| 3598 | |
| 3599 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3600 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3602 | * @associated_nid: NID that new ctls associated with |
| 3603 | * @cvt_nid: converter NID |
| 3604 | * @type: HDA_PCM_TYPE_* |
| 3605 | * Creates controls related with the digital output. |
| 3606 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | * |
| 3608 | * Returns 0 if successful, or a negative error code. |
| 3609 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3610 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3611 | hda_nid_t associated_nid, |
| 3612 | hda_nid_t cvt_nid, |
| 3613 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | { |
| 3615 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3616 | struct snd_kcontrol *kctl; |
| 3617 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3618 | int idx = 0; |
| 3619 | const int spdif_index = 16; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3620 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3621 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3623 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3624 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3625 | idx = spdif_index; |
| 3626 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3627 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3628 | /* suppose a single SPDIF device */ |
| 3629 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3630 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3631 | if (!kctl) |
| 3632 | break; |
| 3633 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3634 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3635 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3636 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3637 | if (!bus->primary_dig_out_type) |
| 3638 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3639 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3640 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3641 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3642 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3643 | return -EBUSY; |
| 3644 | } |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3645 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3646 | if (!spdif) |
| 3647 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3649 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3650 | if (!kctl) |
| 3651 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3652 | kctl->id.index = idx; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3653 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3654 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3655 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | return err; |
| 3657 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3658 | spdif->nid = cvt_nid; |
| 3659 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3660 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3661 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | return 0; |
| 3663 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3664 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 3666 | /** |
| 3667 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3668 | * @codec: the HDA codec |
| 3669 | * @nid: widget NID |
| 3670 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3671 | * call within spdif_mutex lock |
| 3672 | */ |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3673 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3674 | hda_nid_t nid) |
| 3675 | { |
| 3676 | int i; |
| 3677 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3678 | struct hda_spdif_out *spdif = |
| 3679 | snd_array_elem(&codec->spdif_out, i); |
| 3680 | if (spdif->nid == nid) |
| 3681 | return spdif; |
| 3682 | } |
| 3683 | return NULL; |
| 3684 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3685 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3686 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 3687 | /** |
| 3688 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3689 | * @codec: the HDA codec |
| 3690 | * @idx: the SPDIF ctl index |
| 3691 | * |
| 3692 | * Unassign the widget from the given SPDIF control. |
| 3693 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3694 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3695 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3696 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3697 | |
| 3698 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3699 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3700 | spdif->nid = (u16)-1; |
| 3701 | mutex_unlock(&codec->spdif_mutex); |
| 3702 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3703 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3704 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 3705 | /** |
| 3706 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3707 | * @codec: the HDA codec |
| 3708 | * @idx: the SPDIF ctl idx |
| 3709 | * @nid: widget NID |
| 3710 | * |
| 3711 | * Assign the widget to the SPDIF control with the given index. |
| 3712 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3713 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3714 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3715 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3716 | unsigned short val; |
| 3717 | |
| 3718 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3719 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3720 | if (spdif->nid != nid) { |
| 3721 | spdif->nid = nid; |
| 3722 | val = spdif->ctls; |
| 3723 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3724 | } |
| 3725 | mutex_unlock(&codec->spdif_mutex); |
| 3726 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3727 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3730 | * SPDIF sharing with analog output |
| 3731 | */ |
| 3732 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3733 | struct snd_ctl_elem_value *ucontrol) |
| 3734 | { |
| 3735 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3736 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3741 | struct snd_ctl_elem_value *ucontrol) |
| 3742 | { |
| 3743 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3744 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3745 | return 0; |
| 3746 | } |
| 3747 | |
| 3748 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3749 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3750 | .name = "IEC958 Default PCM Playback Switch", |
| 3751 | .info = snd_ctl_boolean_mono_info, |
| 3752 | .get = spdif_share_sw_get, |
| 3753 | .put = spdif_share_sw_put, |
| 3754 | }; |
| 3755 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3756 | /** |
| 3757 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3758 | * @codec: the HDA codec |
| 3759 | * @mout: multi-out instance |
| 3760 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3761 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3762 | struct hda_multi_out *mout) |
| 3763 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3764 | struct snd_kcontrol *kctl; |
| 3765 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3766 | if (!mout->dig_out_nid) |
| 3767 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3768 | |
| 3769 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3770 | if (!kctl) |
| 3771 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3772 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3773 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3774 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3775 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3776 | |
| 3777 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | * SPDIF input |
| 3779 | */ |
| 3780 | |
| 3781 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3782 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3783 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3784 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | { |
| 3786 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3787 | |
| 3788 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3789 | return 0; |
| 3790 | } |
| 3791 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3792 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3793 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | { |
| 3795 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3796 | hda_nid_t nid = kcontrol->private_value; |
| 3797 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3798 | int change; |
| 3799 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3800 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3802 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3804 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3805 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3807 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | return change; |
| 3809 | } |
| 3810 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3811 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3812 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | { |
| 3814 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3815 | hda_nid_t nid = kcontrol->private_value; |
| 3816 | unsigned short val; |
| 3817 | unsigned int sbits; |
| 3818 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3819 | 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] | 3820 | sbits = convert_to_spdif_status(val); |
| 3821 | ucontrol->value.iec958.status[0] = sbits; |
| 3822 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3823 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3824 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3828 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | { |
| 3830 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3831 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | .info = snd_hda_spdif_in_switch_info, |
| 3833 | .get = snd_hda_spdif_in_switch_get, |
| 3834 | .put = snd_hda_spdif_in_switch_put, |
| 3835 | }, |
| 3836 | { |
| 3837 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3838 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3839 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | .info = snd_hda_spdif_mask_info, |
| 3841 | .get = snd_hda_spdif_in_status_get, |
| 3842 | }, |
| 3843 | { } /* end */ |
| 3844 | }; |
| 3845 | |
| 3846 | /** |
| 3847 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3848 | * @codec: the HDA codec |
| 3849 | * @nid: audio in widget NID |
| 3850 | * |
| 3851 | * Creates controls related with the SPDIF input. |
| 3852 | * Called from each patch supporting the SPDIF in. |
| 3853 | * |
| 3854 | * Returns 0 if successful, or a negative error code. |
| 3855 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3856 | 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] | 3857 | { |
| 3858 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3859 | struct snd_kcontrol *kctl; |
| 3860 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3861 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3863 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3864 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3865 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3866 | return -EBUSY; |
| 3867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3869 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3870 | if (!kctl) |
| 3871 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3873 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3874 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | return err; |
| 3876 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3877 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3878 | snd_hda_codec_read(codec, nid, 0, |
| 3879 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3880 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | return 0; |
| 3882 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3883 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3885 | /* |
| 3886 | * command cache |
| 3887 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3889 | /* 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] | 3890 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3891 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3892 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3893 | |
| 3894 | /** |
| 3895 | * snd_hda_codec_write_cache - send a single command with caching |
| 3896 | * @codec: the HDA codec |
| 3897 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3898 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3899 | * @verb: the verb to send |
| 3900 | * @parm: the parameter for the verb |
| 3901 | * |
| 3902 | * Send a single command without waiting for response. |
| 3903 | * |
| 3904 | * Returns 0 if successful, or a negative error code. |
| 3905 | */ |
| 3906 | 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] | 3907 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3908 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3909 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3910 | struct hda_cache_head *c; |
| 3911 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3912 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3913 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3914 | cache_only = codec->cached_write; |
| 3915 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3916 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3917 | if (err < 0) |
| 3918 | return err; |
| 3919 | } |
| 3920 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3921 | /* parm may contain the verb stuff for get/set amp */ |
| 3922 | verb = verb | (parm >> 8); |
| 3923 | parm &= 0xff; |
| 3924 | key = build_cmd_cache_key(nid, verb); |
| 3925 | mutex_lock(&codec->bus->cmd_mutex); |
| 3926 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3927 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3928 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3929 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3930 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3931 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3932 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3933 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3934 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3935 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3936 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3937 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3938 | * @codec: the HDA codec |
| 3939 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3940 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3941 | * @verb: the verb to send |
| 3942 | * @parm: the parameter for the verb |
| 3943 | * |
| 3944 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3945 | * command if the parameter is already identical with the cached value. |
| 3946 | * If not, it sends the command and refreshes the cache. |
| 3947 | * |
| 3948 | * Returns 0 if successful, or a negative error code. |
| 3949 | */ |
| 3950 | 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] | 3951 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3952 | { |
| 3953 | struct hda_cache_head *c; |
| 3954 | u32 key; |
| 3955 | |
| 3956 | /* parm may contain the verb stuff for get/set amp */ |
| 3957 | verb = verb | (parm >> 8); |
| 3958 | parm &= 0xff; |
| 3959 | key = build_cmd_cache_key(nid, verb); |
| 3960 | mutex_lock(&codec->bus->cmd_mutex); |
| 3961 | c = get_hash(&codec->cmd_cache, key); |
| 3962 | if (c && c->val == parm) { |
| 3963 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3964 | return 0; |
| 3965 | } |
| 3966 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3967 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3968 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3969 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3970 | |
| 3971 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3972 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3973 | * @codec: HD-audio codec |
| 3974 | * |
| 3975 | * Execute all verbs recorded in the command caches to resume. |
| 3976 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3977 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3978 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3979 | int i; |
| 3980 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3981 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3982 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3983 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3984 | struct hda_cache_head *buffer; |
| 3985 | u32 key; |
| 3986 | |
| 3987 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3988 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3989 | if (!key) |
| 3990 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3991 | if (!buffer->dirty) |
| 3992 | continue; |
| 3993 | buffer->dirty = 0; |
| 3994 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3995 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3996 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3997 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3998 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3999 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4000 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4001 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4002 | |
| 4003 | /** |
| 4004 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 4005 | * @codec: the HDA codec |
| 4006 | * @seq: VERB array to send |
| 4007 | * |
| 4008 | * Send the commands sequentially from the given array. |
| 4009 | * Thte commands are recorded on cache for power-save and resume. |
| 4010 | * The array must be terminated with NID=0. |
| 4011 | */ |
| 4012 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 4013 | const struct hda_verb *seq) |
| 4014 | { |
| 4015 | for (; seq->nid; seq++) |
| 4016 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 4017 | seq->param); |
| 4018 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4019 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4020 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 4021 | /** |
| 4022 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 4023 | * @codec: HD-audio codec |
| 4024 | */ |
| 4025 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 4026 | { |
| 4027 | snd_hda_codec_resume_amp(codec); |
| 4028 | snd_hda_codec_resume_cache(codec); |
| 4029 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4030 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 4031 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 4032 | /** |
| 4033 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 4034 | * @codec: the HDA codec |
| 4035 | * @fg: function group (not used now) |
| 4036 | * @power_state: the power state to set (AC_PWRST_*) |
| 4037 | * |
| 4038 | * Set the given power state to all widgets that have the power control. |
| 4039 | * If the codec has power_filter set, it evaluates the power state and |
| 4040 | * filter out if it's unchanged as D3. |
| 4041 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4042 | 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] | 4043 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4044 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4045 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4046 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4047 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4048 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 4049 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4050 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4051 | if (!(wcaps & AC_WCAP_POWER)) |
| 4052 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4053 | if (codec->power_filter) { |
| 4054 | state = codec->power_filter(codec, nid, power_state); |
| 4055 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4056 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 4057 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4058 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4059 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4060 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4061 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4062 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4063 | |
| 4064 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4065 | * supported power states check |
| 4066 | */ |
| 4067 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 4068 | unsigned int power_state) |
| 4069 | { |
| 4070 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 4071 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 4072 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4073 | return false; |
| 4074 | if (sup & power_state) |
| 4075 | return true; |
| 4076 | else |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4081 | * wait until the state is reached, returns the current state |
| 4082 | */ |
| 4083 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 4084 | hda_nid_t fg, |
| 4085 | unsigned int power_state) |
| 4086 | { |
| 4087 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 4088 | unsigned int state, actual_state; |
| 4089 | |
| 4090 | for (;;) { |
| 4091 | state = snd_hda_codec_read(codec, fg, 0, |
| 4092 | AC_VERB_GET_POWER_STATE, 0); |
| 4093 | if (state & AC_PWRST_ERROR) |
| 4094 | break; |
| 4095 | actual_state = (state >> 4) & 0x0f; |
| 4096 | if (actual_state == power_state) |
| 4097 | break; |
| 4098 | if (time_after_eq(jiffies, end_time)) |
| 4099 | break; |
| 4100 | /* wait until the codec reachs to the target state */ |
| 4101 | msleep(1); |
| 4102 | } |
| 4103 | return state; |
| 4104 | } |
| 4105 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 4106 | /** |
| 4107 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 4108 | * @codec: the HDA codec |
| 4109 | * @nid: widget NID |
| 4110 | * @power_state: power state to evalue |
| 4111 | * |
| 4112 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 4113 | * This can be used a codec power_filter callback. |
| 4114 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4115 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 4116 | hda_nid_t nid, |
| 4117 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4118 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4119 | if (nid == codec->afg || nid == codec->mfg) |
| 4120 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4121 | if (power_state == AC_PWRST_D3 && |
| 4122 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 4123 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 4124 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 4125 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 4126 | if (eapd & 0x02) |
| 4127 | return AC_PWRST_D0; |
| 4128 | } |
| 4129 | return power_state; |
| 4130 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4131 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4132 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4133 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4134 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4135 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4136 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4137 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4138 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4139 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4140 | int count; |
| 4141 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4142 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4143 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4144 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4145 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 4146 | if (codec->depop_delay < 0) |
| 4147 | msleep(codec->epss ? 10 : 100); |
| 4148 | else if (codec->depop_delay > 0) |
| 4149 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4150 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4151 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4152 | |
| 4153 | /* repeat power states setting at most 10 times*/ |
| 4154 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4155 | if (codec->patch_ops.set_power_state) |
| 4156 | codec->patch_ops.set_power_state(codec, fg, |
| 4157 | power_state); |
| 4158 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4159 | state = power_state; |
| 4160 | if (codec->power_filter) |
| 4161 | state = codec->power_filter(codec, fg, state); |
| 4162 | if (state == power_state || power_state != AC_PWRST_D3) |
| 4163 | snd_hda_codec_read(codec, fg, flags, |
| 4164 | AC_VERB_SET_POWER_STATE, |
| 4165 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4166 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4167 | } |
| 4168 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4169 | if (!(state & AC_PWRST_ERROR)) |
| 4170 | break; |
| 4171 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 4172 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4173 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4174 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4175 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4176 | /* sync power states of all widgets; |
| 4177 | * this is called at the end of codec parsing |
| 4178 | */ |
| 4179 | static void sync_power_up_states(struct hda_codec *codec) |
| 4180 | { |
| 4181 | hda_nid_t nid = codec->start_nid; |
| 4182 | int i; |
| 4183 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4184 | /* don't care if no filter is used */ |
| 4185 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4186 | return; |
| 4187 | |
| 4188 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4189 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4190 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4191 | if (!(wcaps & AC_WCAP_POWER)) |
| 4192 | continue; |
| 4193 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4194 | if (target == AC_PWRST_D0) |
| 4195 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4196 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4197 | snd_hda_codec_write(codec, nid, 0, |
| 4198 | AC_VERB_SET_POWER_STATE, target); |
| 4199 | } |
| 4200 | } |
| 4201 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 4202 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4203 | /* execute additional init verbs */ |
| 4204 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4205 | { |
| 4206 | if (codec->init_verbs.list) |
| 4207 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4208 | } |
| 4209 | #else |
| 4210 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4211 | #endif |
| 4212 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4213 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4214 | /* |
| 4215 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4216 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4217 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4218 | 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] | 4219 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4220 | unsigned int state; |
| 4221 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4222 | codec->in_pm = 1; |
| 4223 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4224 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4225 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4226 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4227 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4228 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4229 | if (!in_wq) |
| 4230 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4231 | spin_lock(&codec->power_lock); |
| 4232 | snd_hda_update_power_acct(codec); |
| 4233 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4234 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4235 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4236 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4237 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4238 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4239 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4240 | } |
| 4241 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4242 | /* mark all entries of cmd and amp caches dirty */ |
| 4243 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4244 | { |
| 4245 | int i; |
| 4246 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4247 | struct hda_cache_head *cmd; |
| 4248 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4249 | cmd->dirty = 1; |
| 4250 | } |
| 4251 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4252 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4253 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4254 | amp->head.dirty = 1; |
| 4255 | } |
| 4256 | } |
| 4257 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4258 | /* |
| 4259 | * kick up codec; used both from PM and power-save |
| 4260 | */ |
| 4261 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4262 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4263 | codec->in_pm = 1; |
| 4264 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4265 | hda_mark_cmd_cache_dirty(codec); |
| 4266 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4267 | /* set as if powered on for avoiding re-entering the resume |
| 4268 | * in the resume / power-save sequence |
| 4269 | */ |
| 4270 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4271 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4272 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4273 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4274 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4275 | if (codec->patch_ops.resume) |
| 4276 | codec->patch_ops.resume(codec); |
| 4277 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4278 | if (codec->patch_ops.init) |
| 4279 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4280 | snd_hda_codec_resume_amp(codec); |
| 4281 | snd_hda_codec_resume_cache(codec); |
| 4282 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4283 | |
| 4284 | if (codec->jackpoll_interval) |
| 4285 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4286 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4287 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4288 | |
| 4289 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4290 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4291 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4292 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4293 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | /** |
| 4296 | * snd_hda_build_controls - build mixer controls |
| 4297 | * @bus: the BUS |
| 4298 | * |
| 4299 | * Creates mixer controls for each codec included in the bus. |
| 4300 | * |
| 4301 | * Returns 0 if successful, otherwise a negative error code. |
| 4302 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4303 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4305 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4307 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4308 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4309 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4310 | codec_err(codec, |
| 4311 | "cannot build controls for #%d (error %d)\n", |
| 4312 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4313 | err = snd_hda_codec_reset(codec); |
| 4314 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4315 | codec_err(codec, |
| 4316 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4317 | return err; |
| 4318 | } |
| 4319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4321 | return 0; |
| 4322 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4323 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4325 | /* |
| 4326 | * add standard channel maps if not specified |
| 4327 | */ |
| 4328 | static int add_std_chmaps(struct hda_codec *codec) |
| 4329 | { |
| 4330 | int i, str, err; |
| 4331 | |
| 4332 | for (i = 0; i < codec->num_pcms; i++) { |
| 4333 | for (str = 0; str < 2; str++) { |
| 4334 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4335 | struct hda_pcm_stream *hinfo = |
| 4336 | &codec->pcm_info[i].stream[str]; |
| 4337 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4338 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4339 | |
| 4340 | if (codec->pcm_info[i].own_chmap) |
| 4341 | continue; |
| 4342 | if (!pcm || !hinfo->substreams) |
| 4343 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4344 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4345 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4346 | hinfo->channels_max, |
| 4347 | 0, &chmap); |
| 4348 | if (err < 0) |
| 4349 | return err; |
| 4350 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4351 | } |
| 4352 | } |
| 4353 | return 0; |
| 4354 | } |
| 4355 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4356 | /* default channel maps for 2.1 speakers; |
| 4357 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4358 | */ |
| 4359 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4360 | { .channels = 2, |
| 4361 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4362 | { .channels = 4, |
| 4363 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4364 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4365 | { } |
| 4366 | }; |
| 4367 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4368 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4369 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4370 | { |
| 4371 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4372 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4373 | /* continue to initialize... */ |
| 4374 | if (codec->patch_ops.init) |
| 4375 | err = codec->patch_ops.init(codec); |
| 4376 | if (!err && codec->patch_ops.build_controls) |
| 4377 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4378 | if (err < 0) |
| 4379 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4380 | |
| 4381 | /* we create chmaps here instead of build_pcms */ |
| 4382 | err = add_std_chmaps(codec); |
| 4383 | if (err < 0) |
| 4384 | return err; |
| 4385 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4386 | if (codec->jackpoll_interval) |
| 4387 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4388 | else |
| 4389 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4390 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | return 0; |
| 4392 | } |
| 4393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | /* |
| 4395 | * stream formats |
| 4396 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4397 | struct hda_rate_tbl { |
| 4398 | unsigned int hz; |
| 4399 | unsigned int alsa_bits; |
| 4400 | unsigned int hda_fmt; |
| 4401 | }; |
| 4402 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4403 | /* rate = base * mult / div */ |
| 4404 | #define HDA_RATE(base, mult, div) \ |
| 4405 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4406 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4407 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4408 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4410 | |
| 4411 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4412 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4413 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4414 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4415 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4416 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4417 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4418 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4419 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4420 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4421 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4422 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4423 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4424 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4425 | |
| 4426 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4427 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4428 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4429 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | }; |
| 4431 | |
| 4432 | /** |
| 4433 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4434 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | * @rate: the sample rate |
| 4436 | * @channels: the number of channels |
| 4437 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4438 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4439 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | * |
| 4441 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4442 | * |
| 4443 | * Return zero if invalid. |
| 4444 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4445 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4446 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | unsigned int channels, |
| 4448 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4449 | unsigned int maxbps, |
| 4450 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | { |
| 4452 | int i; |
| 4453 | unsigned int val = 0; |
| 4454 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4455 | for (i = 0; rate_bits[i].hz; i++) |
| 4456 | if (rate_bits[i].hz == rate) { |
| 4457 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | break; |
| 4459 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4460 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4461 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | return 0; |
| 4463 | } |
| 4464 | |
| 4465 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4466 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | return 0; |
| 4468 | } |
| 4469 | val |= channels - 1; |
| 4470 | |
| 4471 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4472 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4473 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4474 | break; |
| 4475 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4476 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4477 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | case 20: |
| 4479 | case 24: |
| 4480 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4481 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4482 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4484 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4486 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | break; |
| 4488 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4489 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4490 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | return 0; |
| 4492 | } |
| 4493 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4494 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4495 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | return val; |
| 4498 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4499 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4501 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4502 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4503 | { |
| 4504 | unsigned int val = 0; |
| 4505 | if (nid != codec->afg && |
| 4506 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4507 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4508 | if (!val || val == -1) |
| 4509 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4510 | if (!val || val == -1) |
| 4511 | return 0; |
| 4512 | return val; |
| 4513 | } |
| 4514 | |
| 4515 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4516 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4517 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4518 | get_pcm_param); |
| 4519 | } |
| 4520 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4521 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4522 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4523 | { |
| 4524 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4525 | if (!streams || streams == -1) |
| 4526 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4527 | if (!streams || streams == -1) |
| 4528 | return 0; |
| 4529 | return streams; |
| 4530 | } |
| 4531 | |
| 4532 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4533 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4534 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4535 | get_stream_param); |
| 4536 | } |
| 4537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | /** |
| 4539 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4540 | * @codec: the HDA codec |
| 4541 | * @nid: NID to query |
| 4542 | * @ratesp: the pointer to store the detected rate bitflags |
| 4543 | * @formatsp: the pointer to store the detected formats |
| 4544 | * @bpsp: the pointer to store the detected format widths |
| 4545 | * |
| 4546 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4547 | * or @bsps argument is ignored. |
| 4548 | * |
| 4549 | * Returns 0 if successful, otherwise a negative error code. |
| 4550 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4551 | 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] | 4552 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4553 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4554 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4556 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4557 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | |
| 4559 | if (ratesp) { |
| 4560 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4561 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4563 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4565 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4566 | codec_err(codec, |
| 4567 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4568 | nid, val, |
| 4569 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4570 | return -EIO; |
| 4571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | *ratesp = rates; |
| 4573 | } |
| 4574 | |
| 4575 | if (formatsp || bpsp) { |
| 4576 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4577 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4579 | streams = query_stream_param(codec, nid); |
| 4580 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | |
| 4583 | bps = 0; |
| 4584 | if (streams & AC_SUPFMT_PCM) { |
| 4585 | if (val & AC_SUPPCM_BITS_8) { |
| 4586 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4587 | bps = 8; |
| 4588 | } |
| 4589 | if (val & AC_SUPPCM_BITS_16) { |
| 4590 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4591 | bps = 16; |
| 4592 | } |
| 4593 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4594 | if (val & AC_SUPPCM_BITS_32) |
| 4595 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4596 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4597 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4598 | if (val & AC_SUPPCM_BITS_24) |
| 4599 | bps = 24; |
| 4600 | else if (val & AC_SUPPCM_BITS_20) |
| 4601 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4602 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4603 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4605 | if (val & AC_SUPPCM_BITS_32) |
| 4606 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | else if (val & AC_SUPPCM_BITS_24) |
| 4608 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4609 | else if (val & AC_SUPPCM_BITS_20) |
| 4610 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | } |
| 4612 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4613 | #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] | 4614 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4616 | if (!bps) |
| 4617 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4618 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4619 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4620 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4621 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | /* temporary hack: we have still no proper support |
| 4623 | * for the direct AC3 stream... |
| 4624 | */ |
| 4625 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4626 | bps = 8; |
| 4627 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4628 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4629 | codec_err(codec, |
| 4630 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4631 | nid, val, |
| 4632 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4633 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4634 | return -EIO; |
| 4635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | if (formatsp) |
| 4637 | *formatsp = formats; |
| 4638 | if (bpsp) |
| 4639 | *bpsp = bps; |
| 4640 | } |
| 4641 | |
| 4642 | return 0; |
| 4643 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4644 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
| 4646 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4647 | * snd_hda_is_supported_format - Check the validity of the format |
| 4648 | * @codec: HD-audio codec |
| 4649 | * @nid: NID to check |
| 4650 | * @format: the HD-audio format value to check |
| 4651 | * |
| 4652 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | * |
| 4654 | * Returns 1 if supported, 0 if not. |
| 4655 | */ |
| 4656 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4657 | unsigned int format) |
| 4658 | { |
| 4659 | int i; |
| 4660 | unsigned int val = 0, rate, stream; |
| 4661 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4662 | val = query_pcm_param(codec, nid); |
| 4663 | if (!val) |
| 4664 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
| 4666 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4667 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4668 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | if (val & (1 << i)) |
| 4670 | break; |
| 4671 | return 0; |
| 4672 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4673 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | return 0; |
| 4675 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4676 | stream = query_stream_param(codec, nid); |
| 4677 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | return 0; |
| 4679 | |
| 4680 | if (stream & AC_SUPFMT_PCM) { |
| 4681 | switch (format & 0xf0) { |
| 4682 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4683 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | return 0; |
| 4685 | break; |
| 4686 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4687 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | return 0; |
| 4689 | break; |
| 4690 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4691 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | return 0; |
| 4693 | break; |
| 4694 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4695 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | return 0; |
| 4697 | break; |
| 4698 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4699 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | return 0; |
| 4701 | break; |
| 4702 | default: |
| 4703 | return 0; |
| 4704 | } |
| 4705 | } else { |
| 4706 | /* FIXME: check for float32 and AC3? */ |
| 4707 | } |
| 4708 | |
| 4709 | return 1; |
| 4710 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4711 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | |
| 4713 | /* |
| 4714 | * PCM stuff |
| 4715 | */ |
| 4716 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4717 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4718 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | { |
| 4720 | return 0; |
| 4721 | } |
| 4722 | |
| 4723 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4724 | struct hda_codec *codec, |
| 4725 | unsigned int stream_tag, |
| 4726 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4727 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | { |
| 4729 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
| 4733 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4734 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4735 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4737 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | return 0; |
| 4739 | } |
| 4740 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4741 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4742 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4744 | int err; |
| 4745 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4746 | /* query support PCM information from the given NID */ |
| 4747 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4748 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4749 | info->rates ? NULL : &info->rates, |
| 4750 | info->formats ? NULL : &info->formats, |
| 4751 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4752 | if (err < 0) |
| 4753 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | } |
| 4755 | if (info->ops.open == NULL) |
| 4756 | info->ops.open = hda_pcm_default_open_close; |
| 4757 | if (info->ops.close == NULL) |
| 4758 | info->ops.close = hda_pcm_default_open_close; |
| 4759 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4760 | if (snd_BUG_ON(!info->nid)) |
| 4761 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | info->ops.prepare = hda_pcm_default_prepare; |
| 4763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4765 | if (snd_BUG_ON(!info->nid)) |
| 4766 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4768 | } |
| 4769 | return 0; |
| 4770 | } |
| 4771 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4772 | /* |
| 4773 | * codec prepare/cleanup entries |
| 4774 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 4775 | /** |
| 4776 | * snd_hda_codec_prepare - Prepare a stream |
| 4777 | * @codec: the HDA codec |
| 4778 | * @hinfo: PCM information |
| 4779 | * @stream: stream tag to assign |
| 4780 | * @format: format id to assign |
| 4781 | * @substream: PCM substream to assign |
| 4782 | * |
| 4783 | * Calls the prepare callback set by the codec with the given arguments. |
| 4784 | * Clean up the inactive streams when successful. |
| 4785 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4786 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4787 | struct hda_pcm_stream *hinfo, |
| 4788 | unsigned int stream, |
| 4789 | unsigned int format, |
| 4790 | struct snd_pcm_substream *substream) |
| 4791 | { |
| 4792 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4793 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4794 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4795 | if (ret >= 0) |
| 4796 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4797 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4798 | return ret; |
| 4799 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4800 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4801 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame^] | 4802 | /** |
| 4803 | * snd_hda_codec_cleanup - Prepare a stream |
| 4804 | * @codec: the HDA codec |
| 4805 | * @hinfo: PCM information |
| 4806 | * @substream: PCM substream |
| 4807 | * |
| 4808 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4809 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4810 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4811 | struct hda_pcm_stream *hinfo, |
| 4812 | struct snd_pcm_substream *substream) |
| 4813 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4814 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4815 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4816 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4817 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4818 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4819 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4820 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4821 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4822 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4823 | }; |
| 4824 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4825 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4826 | * get the empty PCM device number to assign |
| 4827 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4828 | 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] | 4829 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4830 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4831 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4832 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4833 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4834 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4835 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4836 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4837 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4838 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4839 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4840 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4841 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4842 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4843 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4844 | return -EINVAL; |
| 4845 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4846 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4847 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4848 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4849 | if (audio_idx[type][i] >= 8) |
| 4850 | break; |
| 4851 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4852 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4853 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4854 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4855 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4856 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4857 | /* non-fixed slots starting from 10 */ |
| 4858 | for (i = 10; i < 32; i++) { |
| 4859 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4860 | return i; |
| 4861 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4862 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4863 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4864 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4865 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4866 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4867 | dev_warn(bus->card->dev, |
| 4868 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4869 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4870 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4874 | * attach a new PCM stream |
| 4875 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4876 | 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] | 4877 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4878 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4879 | struct hda_pcm_stream *info; |
| 4880 | int stream, err; |
| 4881 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4882 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4883 | return -EINVAL; |
| 4884 | for (stream = 0; stream < 2; stream++) { |
| 4885 | info = &pcm->stream[stream]; |
| 4886 | if (info->substreams) { |
| 4887 | err = set_pcm_default_values(codec, info); |
| 4888 | if (err < 0) |
| 4889 | return err; |
| 4890 | } |
| 4891 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4892 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4893 | } |
| 4894 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4895 | /* assign all PCMs of the given codec */ |
| 4896 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4897 | { |
| 4898 | unsigned int pcm; |
| 4899 | int err; |
| 4900 | |
| 4901 | if (!codec->num_pcms) { |
| 4902 | if (!codec->patch_ops.build_pcms) |
| 4903 | return 0; |
| 4904 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4905 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4906 | codec_err(codec, |
| 4907 | "cannot build PCMs for #%d (error %d)\n", |
| 4908 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4909 | err = snd_hda_codec_reset(codec); |
| 4910 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4911 | codec_err(codec, |
| 4912 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4913 | return err; |
| 4914 | } |
| 4915 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4916 | } |
| 4917 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4918 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4919 | int dev; |
| 4920 | |
| 4921 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4922 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4923 | |
| 4924 | if (!cpcm->pcm) { |
| 4925 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4926 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4927 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4928 | cpcm->device = dev; |
| 4929 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4930 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4931 | codec_err(codec, |
| 4932 | "cannot attach PCM stream %d for codec #%d\n", |
| 4933 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4934 | continue; /* no fatal error */ |
| 4935 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | return 0; |
| 4939 | } |
| 4940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | /** |
| 4942 | * snd_hda_build_pcms - build PCM information |
| 4943 | * @bus: the BUS |
| 4944 | * |
| 4945 | * Create PCM information for each codec included in the bus. |
| 4946 | * |
| 4947 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4948 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4949 | * to create its PCM instances. |
| 4950 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4951 | * callback. |
| 4952 | * |
| 4953 | * At least, substreams, channels_min and channels_max must be filled for |
| 4954 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4955 | * When rates and/or formats are zero, the supported values are queried |
| 4956 | * from the given nid. The nid is used also by the default ops.prepare |
| 4957 | * and ops.cleanup callbacks. |
| 4958 | * |
| 4959 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4960 | * ops.close is supposed to be called in the close callback. |
| 4961 | * ops.prepare should be called in the prepare or hw_params callback |
| 4962 | * with the proper parameters for set up. |
| 4963 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4964 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4965 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4967 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4969 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4971 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4972 | int err = snd_hda_codec_build_pcms(codec); |
| 4973 | if (err < 0) |
| 4974 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | } |
| 4976 | return 0; |
| 4977 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4978 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | * snd_hda_add_new_ctls - create controls from the array |
| 4982 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4983 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | * |
| 4985 | * This helper function creates and add new controls in the given array. |
| 4986 | * The array must be terminated with an empty entry as terminator. |
| 4987 | * |
| 4988 | * Returns 0 if successful, or a negative error code. |
| 4989 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4990 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4991 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4993 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | |
| 4995 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4996 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4997 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4998 | if (knew->iface == -1) /* skip this codec private value */ |
| 4999 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5000 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5001 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5002 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5003 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5004 | if (addr > 0) |
| 5005 | kctl->id.device = addr; |
| 5006 | if (idx > 0) |
| 5007 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 5008 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5009 | if (!err) |
| 5010 | break; |
| 5011 | /* try first with another device index corresponding to |
| 5012 | * the codec addr; if it still fails (or it's the |
| 5013 | * primary codec), then try another control index |
| 5014 | */ |
| 5015 | if (!addr && codec->addr) |
| 5016 | addr = codec->addr; |
| 5017 | else if (!idx && !knew->index) { |
| 5018 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 5019 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5020 | if (idx <= 0) |
| 5021 | return err; |
| 5022 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5023 | return err; |
| 5024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | } |
| 5026 | return 0; |
| 5027 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5028 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 5030 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5031 | static void hda_power_work(struct work_struct *work) |
| 5032 | { |
| 5033 | struct hda_codec *codec = |
| 5034 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 5035 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 5036 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5037 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5038 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5039 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 5040 | spin_unlock(&codec->power_lock); |
| 5041 | return; |
| 5042 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5043 | if (!codec->power_on || codec->power_count) { |
| 5044 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5045 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5046 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5047 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5048 | spin_unlock(&codec->power_lock); |
| 5049 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5050 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5051 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 5052 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | static void hda_keep_power_on(struct hda_codec *codec) |
| 5056 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5057 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5058 | codec->power_count++; |
| 5059 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5060 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5061 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5062 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5063 | } |
| 5064 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5065 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5066 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 5067 | { |
| 5068 | unsigned long delta = jiffies - codec->power_jiffies; |
| 5069 | if (codec->power_on) |
| 5070 | codec->power_on_acct += delta; |
| 5071 | else |
| 5072 | codec->power_off_acct += delta; |
| 5073 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5074 | } |
| 5075 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5076 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 5077 | * transition to D3 to complete. A pending D3 transition is indicated |
| 5078 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5079 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5080 | 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] | 5081 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5082 | /* Return if power_on or transitioning to power_on, unless currently |
| 5083 | * powering down. */ |
| 5084 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5085 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5086 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5087 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5088 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5089 | cancel_delayed_work_sync(&codec->power_work); |
| 5090 | |
| 5091 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5092 | /* If the power down delayed work was cancelled above before starting, |
| 5093 | * then there is no need to go through power up here. |
| 5094 | */ |
| 5095 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5096 | if (codec->power_transition < 0) |
| 5097 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5098 | return; |
| 5099 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5100 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5101 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5102 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5103 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5104 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5105 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5106 | spin_unlock(&codec->power_lock); |
| 5107 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5108 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5109 | |
| 5110 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5111 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5112 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5113 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5114 | #define power_save(codec) \ |
| 5115 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5116 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5117 | /* Transition to powered down */ |
| 5118 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5119 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5120 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5121 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5122 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5123 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5124 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5125 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5126 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5127 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | /** |
| 5131 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5132 | * @codec: HD-audio codec |
| 5133 | * @delta: the counter delta to change |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5134 | * @d3wait: sync for D3 transition complete |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5135 | * |
| 5136 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5137 | * appropriately. For the power-down, queue to the delayed action. |
| 5138 | * Passing zero to @delta means to synchronize the power state. |
| 5139 | */ |
| 5140 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5141 | { |
| 5142 | spin_lock(&codec->power_lock); |
| 5143 | codec->power_count += delta; |
| 5144 | trace_hda_power_count(codec); |
| 5145 | if (delta > 0) |
| 5146 | __snd_hda_power_up(codec, d3wait); |
| 5147 | else |
| 5148 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5149 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5150 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5151 | EXPORT_SYMBOL_GPL(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5152 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5153 | /** |
| 5154 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5155 | * @codec: HD-audio codec |
| 5156 | * @check: the object containing an AMP list and the status |
| 5157 | * @nid: NID to check / update |
| 5158 | * |
| 5159 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5160 | * check the current AMP status, and update the the power-status according |
| 5161 | * to the mute status. |
| 5162 | * |
| 5163 | * This function is supposed to be set or called from the check_power_status |
| 5164 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5165 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5166 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5167 | struct hda_loopback_check *check, |
| 5168 | hda_nid_t nid) |
| 5169 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5170 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5171 | int ch, v; |
| 5172 | |
| 5173 | if (!check->amplist) |
| 5174 | return 0; |
| 5175 | for (p = check->amplist; p->nid; p++) { |
| 5176 | if (p->nid == nid) |
| 5177 | break; |
| 5178 | } |
| 5179 | if (!p->nid) |
| 5180 | return 0; /* nothing changed */ |
| 5181 | |
| 5182 | for (p = check->amplist; p->nid; p++) { |
| 5183 | for (ch = 0; ch < 2; ch++) { |
| 5184 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5185 | p->idx); |
| 5186 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5187 | if (!check->power_on) { |
| 5188 | check->power_on = 1; |
| 5189 | snd_hda_power_up(codec); |
| 5190 | } |
| 5191 | return 1; |
| 5192 | } |
| 5193 | } |
| 5194 | } |
| 5195 | if (check->power_on) { |
| 5196 | check->power_on = 0; |
| 5197 | snd_hda_power_down(codec); |
| 5198 | } |
| 5199 | return 0; |
| 5200 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5201 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5204 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5205 | * Channel mode helper |
| 5206 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5207 | |
| 5208 | /** |
| 5209 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5210 | * @codec: the HDA codec |
| 5211 | * @uinfo: pointer to get/store the data |
| 5212 | * @chmode: channel mode array |
| 5213 | * @num_chmodes: channel mode array size |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5214 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5215 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5216 | struct snd_ctl_elem_info *uinfo, |
| 5217 | const struct hda_channel_mode *chmode, |
| 5218 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5219 | { |
| 5220 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5221 | uinfo->count = 1; |
| 5222 | uinfo->value.enumerated.items = num_chmodes; |
| 5223 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5224 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5225 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5226 | chmode[uinfo->value.enumerated.item].channels); |
| 5227 | return 0; |
| 5228 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5229 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5230 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5231 | /** |
| 5232 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5233 | * @codec: the HDA codec |
| 5234 | * @ucontrol: pointer to get/store the data |
| 5235 | * @chmode: channel mode array |
| 5236 | * @num_chmodes: channel mode array size |
| 5237 | * @max_channels: max number of channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5238 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5239 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5240 | struct snd_ctl_elem_value *ucontrol, |
| 5241 | const struct hda_channel_mode *chmode, |
| 5242 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5243 | int max_channels) |
| 5244 | { |
| 5245 | int i; |
| 5246 | |
| 5247 | for (i = 0; i < num_chmodes; i++) { |
| 5248 | if (max_channels == chmode[i].channels) { |
| 5249 | ucontrol->value.enumerated.item[0] = i; |
| 5250 | break; |
| 5251 | } |
| 5252 | } |
| 5253 | return 0; |
| 5254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5255 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5256 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5257 | /** |
| 5258 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5259 | * @codec: the HDA codec |
| 5260 | * @ucontrol: pointer to get/store the data |
| 5261 | * @chmode: channel mode array |
| 5262 | * @num_chmodes: channel mode array size |
| 5263 | * @max_channelsp: pointer to store the max channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5264 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5265 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5266 | struct snd_ctl_elem_value *ucontrol, |
| 5267 | const struct hda_channel_mode *chmode, |
| 5268 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5269 | int *max_channelsp) |
| 5270 | { |
| 5271 | unsigned int mode; |
| 5272 | |
| 5273 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5274 | if (mode >= num_chmodes) |
| 5275 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5276 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5277 | return 0; |
| 5278 | /* change the current channel setting */ |
| 5279 | *max_channelsp = chmode[mode].channels; |
| 5280 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5281 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5282 | return 1; |
| 5283 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5284 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | /* |
| 5287 | * input MUX helper |
| 5288 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5289 | |
| 5290 | /** |
| 5291 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5292 | * @imux: imux helper object |
| 5293 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5294 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5295 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5296 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | { |
| 5298 | unsigned int index; |
| 5299 | |
| 5300 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5301 | uinfo->count = 1; |
| 5302 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5303 | if (!imux->num_items) |
| 5304 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | index = uinfo->value.enumerated.item; |
| 5306 | if (index >= imux->num_items) |
| 5307 | index = imux->num_items - 1; |
| 5308 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5309 | return 0; |
| 5310 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5311 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5313 | /** |
| 5314 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5315 | * @codec: the HDA codec |
| 5316 | * @imux: imux helper object |
| 5317 | * @ucontrol: pointer to get/store the data |
| 5318 | * @nid: input mux NID |
| 5319 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5320 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5321 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5322 | const struct hda_input_mux *imux, |
| 5323 | struct snd_ctl_elem_value *ucontrol, |
| 5324 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | unsigned int *cur_val) |
| 5326 | { |
| 5327 | unsigned int idx; |
| 5328 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5329 | if (!imux->num_items) |
| 5330 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | idx = ucontrol->value.enumerated.item[0]; |
| 5332 | if (idx >= imux->num_items) |
| 5333 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5334 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5336 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5337 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | *cur_val = idx; |
| 5339 | return 1; |
| 5340 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5341 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | |
| 5343 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5344 | /** |
| 5345 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 5346 | * @kcontrol: ctl element |
| 5347 | * @uinfo: pointer to get/store the data |
| 5348 | * @num_items: number of enum items |
| 5349 | * @texts: enum item string array |
| 5350 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5351 | * process kcontrol info callback of a simple string enum array |
| 5352 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5353 | */ |
| 5354 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5355 | struct snd_ctl_elem_info *uinfo, |
| 5356 | int num_items, const char * const *texts) |
| 5357 | { |
| 5358 | static const char * const texts_default[] = { |
| 5359 | "Disabled", "Enabled" |
| 5360 | }; |
| 5361 | |
| 5362 | if (!texts || !num_items) { |
| 5363 | num_items = 2; |
| 5364 | texts = texts_default; |
| 5365 | } |
| 5366 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 5367 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5368 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5369 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5370 | |
| 5371 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | * Multi-channel / digital-out PCM helper functions |
| 5373 | */ |
| 5374 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5375 | /* setup SPDIF output stream */ |
| 5376 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5377 | unsigned int stream_tag, unsigned int format) |
| 5378 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5379 | struct hda_spdif_out *spdif; |
| 5380 | unsigned int curr_fmt; |
| 5381 | bool reset; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5382 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5383 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5384 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5385 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5386 | reset = codec->spdif_status_reset && |
| 5387 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5388 | curr_fmt != format; |
| 5389 | |
| 5390 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5391 | updated */ |
| 5392 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5393 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5394 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5395 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5396 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5397 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5398 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5399 | for (d = codec->slave_dig_outs; *d; d++) |
| 5400 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5401 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5402 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5403 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5404 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5405 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5406 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5407 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5408 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5409 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5410 | { |
| 5411 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5412 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5413 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5414 | for (d = codec->slave_dig_outs; *d; d++) |
| 5415 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5416 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5417 | } |
| 5418 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5419 | /** |
| 5420 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5421 | * @bus: HD-audio bus |
| 5422 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5423 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5424 | { |
| 5425 | struct hda_codec *codec; |
| 5426 | |
| 5427 | if (!bus) |
| 5428 | return; |
| 5429 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5430 | if (hda_codec_is_power_on(codec) && |
| 5431 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5432 | codec->patch_ops.reboot_notify(codec); |
| 5433 | } |
| 5434 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5435 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5436 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5437 | /** |
| 5438 | * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5439 | * @codec: the HDA codec |
| 5440 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5442 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5443 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5445 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5446 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5447 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5448 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5450 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | return 0; |
| 5452 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5453 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5455 | /** |
| 5456 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5457 | * @codec: the HDA codec |
| 5458 | * @mout: hda_multi_out object |
| 5459 | * @stream_tag: stream tag to assign |
| 5460 | * @format: format id to assign |
| 5461 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5462 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5463 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5464 | struct hda_multi_out *mout, |
| 5465 | unsigned int stream_tag, |
| 5466 | unsigned int format, |
| 5467 | struct snd_pcm_substream *substream) |
| 5468 | { |
| 5469 | mutex_lock(&codec->spdif_mutex); |
| 5470 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5471 | mutex_unlock(&codec->spdif_mutex); |
| 5472 | return 0; |
| 5473 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5474 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5475 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5476 | /** |
| 5477 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5478 | * @codec: the HDA codec |
| 5479 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5480 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5481 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5482 | struct hda_multi_out *mout) |
| 5483 | { |
| 5484 | mutex_lock(&codec->spdif_mutex); |
| 5485 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5486 | mutex_unlock(&codec->spdif_mutex); |
| 5487 | return 0; |
| 5488 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5489 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5490 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5491 | /** |
| 5492 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5493 | * @codec: the HDA codec |
| 5494 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5496 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5497 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5499 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5501 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | return 0; |
| 5503 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5504 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5506 | /** |
| 5507 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5508 | * @codec: the HDA codec |
| 5509 | * @mout: hda_multi_out object |
| 5510 | * @substream: PCM substream to assign |
| 5511 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5512 | * |
| 5513 | * Open analog outputs and set up the hw-constraints. |
| 5514 | * If the digital outputs can be opened as slave, open the digital |
| 5515 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5517 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5518 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5519 | struct snd_pcm_substream *substream, |
| 5520 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5522 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5523 | runtime->hw.channels_max = mout->max_channels; |
| 5524 | if (mout->dig_out_nid) { |
| 5525 | if (!mout->analog_rates) { |
| 5526 | mout->analog_rates = hinfo->rates; |
| 5527 | mout->analog_formats = hinfo->formats; |
| 5528 | mout->analog_maxbps = hinfo->maxbps; |
| 5529 | } else { |
| 5530 | runtime->hw.rates = mout->analog_rates; |
| 5531 | runtime->hw.formats = mout->analog_formats; |
| 5532 | hinfo->maxbps = mout->analog_maxbps; |
| 5533 | } |
| 5534 | if (!mout->spdif_rates) { |
| 5535 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5536 | &mout->spdif_rates, |
| 5537 | &mout->spdif_formats, |
| 5538 | &mout->spdif_maxbps); |
| 5539 | } |
| 5540 | mutex_lock(&codec->spdif_mutex); |
| 5541 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5542 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5543 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5544 | runtime->hw.rates &= mout->spdif_rates; |
| 5545 | runtime->hw.formats &= mout->spdif_formats; |
| 5546 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5547 | hinfo->maxbps = mout->spdif_maxbps; |
| 5548 | } else { |
| 5549 | mout->share_spdif = 0; |
| 5550 | /* FIXME: need notify? */ |
| 5551 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5552 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5553 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5556 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5557 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5558 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5560 | /** |
| 5561 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5562 | * @codec: the HDA codec |
| 5563 | * @mout: hda_multi_out object |
| 5564 | * @stream_tag: stream tag to assign |
| 5565 | * @format: format id to assign |
| 5566 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5567 | * |
| 5568 | * Set up the i/o for analog out. |
| 5569 | * 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] | 5570 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5571 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5572 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | unsigned int stream_tag, |
| 5574 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5575 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5577 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5579 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | int i; |
| 5581 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5582 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5583 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5584 | if (mout->dig_out_nid && mout->share_spdif && |
| 5585 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5587 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5588 | format) && |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5589 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5591 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5592 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | } else { |
| 5594 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5595 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | } |
| 5597 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5598 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | |
| 5600 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5601 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5602 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5603 | if (!mout->no_share_stream && |
| 5604 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5606 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5607 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5608 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5609 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5610 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5611 | snd_hda_codec_setup_stream(codec, |
| 5612 | mout->hp_out_nid[i], |
| 5613 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | /* surrounds */ |
| 5616 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5617 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5618 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5619 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5620 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5621 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5622 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5624 | |
| 5625 | /* extra surrounds */ |
| 5626 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5627 | int ch = 0; |
| 5628 | if (!mout->extra_out_nid[i]) |
| 5629 | break; |
| 5630 | if (chs >= (i + 1) * 2) |
| 5631 | ch = i * 2; |
| 5632 | else if (!mout->no_share_stream) |
| 5633 | break; |
| 5634 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5635 | stream_tag, ch, format); |
| 5636 | } |
| 5637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | return 0; |
| 5639 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5640 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5642 | /** |
| 5643 | * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5644 | * @codec: the HDA codec |
| 5645 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5647 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5648 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5650 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | int i; |
| 5652 | |
| 5653 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5654 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5656 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5657 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5658 | if (mout->hp_out_nid[i]) |
| 5659 | snd_hda_codec_cleanup_stream(codec, |
| 5660 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5661 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5662 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5663 | snd_hda_codec_cleanup_stream(codec, |
| 5664 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5665 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | 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] | 5667 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | mout->dig_out_used = 0; |
| 5669 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5670 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | return 0; |
| 5672 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5673 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5675 | /** |
| 5676 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5677 | * @codec: the HDA codec |
| 5678 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5679 | * |
| 5680 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5681 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5682 | */ |
| 5683 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5684 | { |
| 5685 | unsigned int pincap; |
| 5686 | unsigned int oldval; |
| 5687 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5688 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5689 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5690 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5691 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5692 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5693 | return AC_PINCTL_VREF_80; |
| 5694 | else if (pincap & AC_PINCAP_VREF_50) |
| 5695 | return AC_PINCTL_VREF_50; |
| 5696 | else if (pincap & AC_PINCAP_VREF_100) |
| 5697 | return AC_PINCTL_VREF_100; |
| 5698 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5699 | return AC_PINCTL_VREF_GRD; |
| 5700 | return AC_PINCTL_VREF_HIZ; |
| 5701 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5702 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5703 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5704 | /** |
| 5705 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5706 | * @codec: the HDA codec |
| 5707 | * @pin: referred pin NID |
| 5708 | * @val: pin ctl value to audit |
| 5709 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5710 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5711 | hda_nid_t pin, unsigned int val) |
| 5712 | { |
| 5713 | static unsigned int cap_lists[][2] = { |
| 5714 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5715 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5716 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5717 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5718 | }; |
| 5719 | unsigned int cap; |
| 5720 | |
| 5721 | if (!val) |
| 5722 | return 0; |
| 5723 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5724 | if (!cap) |
| 5725 | return val; /* don't know what to do... */ |
| 5726 | |
| 5727 | if (val & AC_PINCTL_OUT_EN) { |
| 5728 | if (!(cap & AC_PINCAP_OUT)) |
| 5729 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5730 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5731 | val &= ~AC_PINCTL_HP_EN; |
| 5732 | } |
| 5733 | |
| 5734 | if (val & AC_PINCTL_IN_EN) { |
| 5735 | if (!(cap & AC_PINCAP_IN)) |
| 5736 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5737 | else { |
| 5738 | unsigned int vcap, vref; |
| 5739 | int i; |
| 5740 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5741 | vref = val & AC_PINCTL_VREFEN; |
| 5742 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5743 | if (vref == cap_lists[i][0] && |
| 5744 | !(vcap & cap_lists[i][1])) { |
| 5745 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5746 | vref = AC_PINCTL_VREF_HIZ; |
| 5747 | else |
| 5748 | vref = cap_lists[i + 1][0]; |
| 5749 | } |
| 5750 | } |
| 5751 | val &= ~AC_PINCTL_VREFEN; |
| 5752 | val |= vref; |
| 5753 | } |
| 5754 | } |
| 5755 | |
| 5756 | return val; |
| 5757 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5758 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5759 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5760 | /** |
| 5761 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5762 | * @codec: the HDA codec |
| 5763 | * @pin: referred pin NID |
| 5764 | * @val: pin control value to set |
| 5765 | * @cached: access over codec pinctl cache or direct write |
| 5766 | * |
| 5767 | * This function is a helper to set a pin ctl value more safely. |
| 5768 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5769 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5770 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5771 | * snd_hda_codec_write() depending on @cached flag. |
| 5772 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5773 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5774 | unsigned int val, bool cached) |
| 5775 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5776 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5777 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5778 | if (cached) |
| 5779 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5780 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5781 | else |
| 5782 | return snd_hda_codec_write(codec, pin, 0, |
| 5783 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5784 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5785 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5786 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5787 | /** |
| 5788 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5789 | * @codec: the HDA codec |
| 5790 | * @imux: imux helper object |
| 5791 | * @label: the name of imux item to assign |
| 5792 | * @index: index number of imux item to assign |
| 5793 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5794 | * |
| 5795 | * When the same label is used already in the existing items, the number |
| 5796 | * suffix is appended to the label. This label index number is stored |
| 5797 | * to type_idx when non-NULL pointer is given. |
| 5798 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5799 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5800 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5801 | int index, int *type_idx) |
| 5802 | { |
| 5803 | int i, label_idx = 0; |
| 5804 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5805 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5806 | return -EINVAL; |
| 5807 | } |
| 5808 | for (i = 0; i < imux->num_items; i++) { |
| 5809 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5810 | label_idx++; |
| 5811 | } |
| 5812 | if (type_idx) |
| 5813 | *type_idx = label_idx; |
| 5814 | if (label_idx > 0) |
| 5815 | snprintf(imux->items[imux->num_items].label, |
| 5816 | sizeof(imux->items[imux->num_items].label), |
| 5817 | "%s %d", label, label_idx); |
| 5818 | else |
| 5819 | strlcpy(imux->items[imux->num_items].label, label, |
| 5820 | sizeof(imux->items[imux->num_items].label)); |
| 5821 | imux->items[imux->num_items].index = index; |
| 5822 | imux->num_items++; |
| 5823 | return 0; |
| 5824 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5825 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5826 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | #ifdef CONFIG_PM |
| 5829 | /* |
| 5830 | * power management |
| 5831 | */ |
| 5832 | |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5833 | |
| 5834 | static void hda_async_suspend(void *data, async_cookie_t cookie) |
| 5835 | { |
| 5836 | hda_call_codec_suspend(data, false); |
| 5837 | } |
| 5838 | |
| 5839 | static void hda_async_resume(void *data, async_cookie_t cookie) |
| 5840 | { |
| 5841 | hda_call_codec_resume(data); |
| 5842 | } |
| 5843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | /** |
| 5845 | * snd_hda_suspend - suspend the codecs |
| 5846 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | * |
| 5848 | * Returns 0 if successful. |
| 5849 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5850 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5852 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5853 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5855 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5856 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5857 | if (hda_codec_is_power_on(codec)) { |
| 5858 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5859 | async_schedule_domain(hda_async_suspend, codec, |
| 5860 | &domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5861 | else |
| 5862 | hda_call_codec_suspend(codec, false); |
| 5863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5865 | |
| 5866 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5867 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | return 0; |
| 5870 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5871 | EXPORT_SYMBOL_GPL(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | |
| 5873 | /** |
| 5874 | * snd_hda_resume - resume the codecs |
| 5875 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | * |
| 5877 | * Returns 0 if successful. |
| 5878 | */ |
| 5879 | int snd_hda_resume(struct hda_bus *bus) |
| 5880 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5881 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5882 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5884 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5885 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5886 | async_schedule_domain(hda_async_resume, codec, &domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5887 | else |
| 5888 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5890 | |
| 5891 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5892 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | return 0; |
| 5895 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5896 | EXPORT_SYMBOL_GPL(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5897 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5898 | |
| 5899 | /* |
| 5900 | * generic arrays |
| 5901 | */ |
| 5902 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5903 | /** |
| 5904 | * snd_array_new - get a new element from the given array |
| 5905 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5906 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5907 | * Get a new element from the given array. If it exceeds the |
| 5908 | * pre-allocated array size, re-allocate the array. |
| 5909 | * |
| 5910 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5911 | */ |
| 5912 | void *snd_array_new(struct snd_array *array) |
| 5913 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5914 | if (snd_BUG_ON(!array->elem_size)) |
| 5915 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5916 | if (array->used >= array->alloced) { |
| 5917 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5918 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5919 | void *nlist; |
| 5920 | if (snd_BUG_ON(num >= 4096)) |
| 5921 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5922 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5923 | if (!nlist) |
| 5924 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5925 | array->list = nlist; |
| 5926 | array->alloced = num; |
| 5927 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5928 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5929 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5930 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5931 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5932 | /** |
| 5933 | * snd_array_free - free the given array elements |
| 5934 | * @array: the array object |
| 5935 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5936 | void snd_array_free(struct snd_array *array) |
| 5937 | { |
| 5938 | kfree(array->list); |
| 5939 | array->used = 0; |
| 5940 | array->alloced = 0; |
| 5941 | array->list = NULL; |
| 5942 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5943 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5944 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5945 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5946 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5947 | * @pcm: PCM caps bits |
| 5948 | * @buf: the string buffer to write |
| 5949 | * @buflen: the max buffer length |
| 5950 | * |
| 5951 | * used by hda_proc.c and hda_eld.c |
| 5952 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5953 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5954 | { |
| 5955 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5956 | int i, j; |
| 5957 | |
| 5958 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5959 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5960 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5961 | |
| 5962 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5963 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5964 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5965 | |
| 5966 | MODULE_DESCRIPTION("HDA codec core"); |
| 5967 | MODULE_LICENSE("GPL"); |