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); |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 349 | if (parm == -1) { |
| 350 | *start_id = 0; |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 351 | return 0; |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | *start_id = (parm >> 16) & 0x7fff; |
| 354 | return (int)(parm & 0x7fff); |
| 355 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 356 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 358 | /* connection list element */ |
| 359 | struct hda_conn_list { |
| 360 | struct list_head list; |
| 361 | int len; |
| 362 | hda_nid_t nid; |
| 363 | hda_nid_t conns[0]; |
| 364 | }; |
| 365 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 366 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 367 | static struct hda_conn_list * |
| 368 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 369 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 370 | struct hda_conn_list *p; |
| 371 | list_for_each_entry(p, &codec->conn_list, list) { |
| 372 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 373 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 374 | } |
| 375 | return NULL; |
| 376 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 377 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 378 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 379 | const hda_nid_t *list) |
| 380 | { |
| 381 | struct hda_conn_list *p; |
| 382 | |
| 383 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 384 | if (!p) |
| 385 | return -ENOMEM; |
| 386 | p->len = len; |
| 387 | p->nid = nid; |
| 388 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 389 | list_add(&p->list, &codec->conn_list); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static void remove_conn_list(struct hda_codec *codec) |
| 394 | { |
| 395 | while (!list_empty(&codec->conn_list)) { |
| 396 | struct hda_conn_list *p; |
| 397 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 398 | list_del(&p->list); |
| 399 | kfree(p); |
| 400 | } |
| 401 | } |
| 402 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 403 | /* read the connection and add to the cache */ |
| 404 | 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] | 405 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 406 | hda_nid_t list[32]; |
| 407 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 408 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 409 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 410 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 411 | if (len == -ENOSPC) { |
| 412 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 413 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 414 | if (!result) |
| 415 | return -ENOMEM; |
| 416 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 417 | } |
| 418 | if (len >= 0) |
| 419 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 420 | if (result != list) |
| 421 | kfree(result); |
| 422 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 423 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 424 | |
| 425 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 426 | * snd_hda_get_conn_list - get connection list |
| 427 | * @codec: the HDA codec |
| 428 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 429 | * @listp: the pointer to store NID list |
| 430 | * |
| 431 | * Parses the connection list of the given widget and stores the pointer |
| 432 | * to the list of NIDs. |
| 433 | * |
| 434 | * Returns the number of connections, or a negative error code. |
| 435 | * |
| 436 | * Note that the returned pointer isn't protected against the list |
| 437 | * modification. If snd_hda_override_conn_list() might be called |
| 438 | * concurrently, protect with a mutex appropriately. |
| 439 | */ |
| 440 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 441 | const hda_nid_t **listp) |
| 442 | { |
| 443 | bool added = false; |
| 444 | |
| 445 | for (;;) { |
| 446 | int err; |
| 447 | const struct hda_conn_list *p; |
| 448 | |
| 449 | /* if the connection-list is already cached, read it */ |
| 450 | p = lookup_conn_list(codec, nid); |
| 451 | if (p) { |
| 452 | if (listp) |
| 453 | *listp = p->conns; |
| 454 | return p->len; |
| 455 | } |
| 456 | if (snd_BUG_ON(added)) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | err = read_and_add_raw_conns(codec, nid); |
| 460 | if (err < 0) |
| 461 | return err; |
| 462 | added = true; |
| 463 | } |
| 464 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 465 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 466 | |
| 467 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 468 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | * @codec: the HDA codec |
| 470 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 471 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * @max_conns: max. number of connections to store |
| 473 | * |
| 474 | * Parses the connection list of the given widget and stores the list |
| 475 | * of NIDs. |
| 476 | * |
| 477 | * Returns the number of connections, or a negative error code. |
| 478 | */ |
| 479 | 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] | 480 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 481 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 482 | const hda_nid_t *list; |
| 483 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 484 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 485 | if (len > 0 && conn_list) { |
| 486 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 487 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 488 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 489 | return -EINVAL; |
| 490 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 491 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 492 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 493 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 494 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 495 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 496 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 497 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 498 | /* return CONNLIST_LEN parameter of the given widget */ |
| 499 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 500 | { |
| 501 | unsigned int wcaps = get_wcaps(codec, nid); |
| 502 | unsigned int parm; |
| 503 | |
| 504 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 505 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 506 | return 0; |
| 507 | |
| 508 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 509 | if (parm == -1) |
| 510 | parm = 0; |
| 511 | return parm; |
| 512 | } |
| 513 | |
| 514 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 515 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 516 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 519 | /** |
| 520 | * snd_hda_get_raw_connections - copy connection list without cache |
| 521 | * @codec: the HDA codec |
| 522 | * @nid: NID to parse |
| 523 | * @conn_list: connection list array |
| 524 | * @max_conns: max. number of connections to store |
| 525 | * |
| 526 | * Like snd_hda_get_connections(), copy the connection list but without |
| 527 | * checking through the connection-list cache. |
| 528 | * Currently called only from hda_proc.c, so not exported. |
| 529 | */ |
| 530 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 531 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 534 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 536 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 537 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 539 | parm = get_num_conns(codec, nid); |
| 540 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 541 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | if (parm & AC_CLIST_LONG) { |
| 544 | /* long form */ |
| 545 | shift = 16; |
| 546 | num_elems = 2; |
| 547 | } else { |
| 548 | /* short form */ |
| 549 | shift = 8; |
| 550 | num_elems = 4; |
| 551 | } |
| 552 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | mask = (1 << (shift-1)) - 1; |
| 554 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 555 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return 0; /* no connection */ |
| 557 | |
| 558 | if (conn_len == 1) { |
| 559 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 560 | parm = snd_hda_codec_read(codec, nid, 0, |
| 561 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 562 | if (parm == -1 && codec->bus->rirb_error) |
| 563 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 564 | if (conn_list) |
| 565 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* multi connection */ |
| 570 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 571 | prev_nid = 0; |
| 572 | for (i = 0; i < conn_len; i++) { |
| 573 | int range_val; |
| 574 | hda_nid_t val, n; |
| 575 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 576 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 577 | parm = snd_hda_codec_read(codec, nid, 0, |
| 578 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 579 | if (parm == -1 && codec->bus->rirb_error) |
| 580 | return -EIO; |
| 581 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 582 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 583 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 584 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 585 | codec_dbg(codec, |
| 586 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 587 | nid, i, parm); |
| 588 | return 0; |
| 589 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 590 | parm >>= shift; |
| 591 | if (range_val) { |
| 592 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 593 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 594 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 595 | "invalid dep_range_val %x:%x\n", |
| 596 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 597 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 599 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 600 | if (conn_list) { |
| 601 | if (conns >= max_conns) |
| 602 | return -ENOSPC; |
| 603 | conn_list[conns] = n; |
| 604 | } |
| 605 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 606 | } |
| 607 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 608 | if (conn_list) { |
| 609 | if (conns >= max_conns) |
| 610 | return -ENOSPC; |
| 611 | conn_list[conns] = val; |
| 612 | } |
| 613 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 615 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | return conns; |
| 618 | } |
| 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 621 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 622 | * @codec: the HDA codec |
| 623 | * @nid: NID to parse |
| 624 | * @len: number of connection list entries |
| 625 | * @list: the list of connection entries |
| 626 | * |
| 627 | * Add or modify the given connection-list to the cache. If the corresponding |
| 628 | * cache already exists, invalidate it and append a new one. |
| 629 | * |
| 630 | * Returns zero or a negative error code. |
| 631 | */ |
| 632 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 633 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 635 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 636 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 637 | p = lookup_conn_list(codec, nid); |
| 638 | if (p) { |
| 639 | list_del(&p->list); |
| 640 | kfree(p); |
| 641 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 642 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 643 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 645 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 646 | |
| 647 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 648 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 649 | * @codec: the HDA codec |
| 650 | * @mux: NID containing the list |
| 651 | * @nid: NID to select |
| 652 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 653 | * |
| 654 | * Parses the connection list of the widget @mux and checks whether the |
| 655 | * widget @nid is present. If it is, return the connection index. |
| 656 | * Otherwise it returns -1. |
| 657 | */ |
| 658 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 659 | hda_nid_t nid, int recursive) |
| 660 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 661 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 662 | int i, nums; |
| 663 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 664 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 665 | for (i = 0; i < nums; i++) |
| 666 | if (conn[i] == nid) |
| 667 | return i; |
| 668 | if (!recursive) |
| 669 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 670 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 671 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 675 | for (i = 0; i < nums; i++) { |
| 676 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 677 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 678 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 679 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 680 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 681 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 682 | return -1; |
| 683 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 684 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 686 | |
| 687 | /* return DEVLIST_LEN parameter of the given widget */ |
| 688 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 689 | { |
| 690 | unsigned int wcaps = get_wcaps(codec, nid); |
| 691 | unsigned int parm; |
| 692 | |
| 693 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 694 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 695 | return 0; |
| 696 | |
| 697 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 698 | if (parm == -1 && codec->bus->rirb_error) |
| 699 | parm = 0; |
| 700 | return parm & AC_DEV_LIST_LEN_MASK; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * snd_hda_get_devices - copy device list without cache |
| 705 | * @codec: the HDA codec |
| 706 | * @nid: NID of the pin to parse |
| 707 | * @dev_list: device list array |
| 708 | * @max_devices: max. number of devices to store |
| 709 | * |
| 710 | * Copy the device list. This info is dynamic and so not cached. |
| 711 | * Currently called only from hda_proc.c, so not exported. |
| 712 | */ |
| 713 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 714 | u8 *dev_list, int max_devices) |
| 715 | { |
| 716 | unsigned int parm; |
| 717 | int i, dev_len, devices; |
| 718 | |
| 719 | parm = get_num_devices(codec, nid); |
| 720 | if (!parm) /* not multi-stream capable */ |
| 721 | return 0; |
| 722 | |
| 723 | dev_len = parm + 1; |
| 724 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 725 | |
| 726 | devices = 0; |
| 727 | while (devices < dev_len) { |
| 728 | parm = snd_hda_codec_read(codec, nid, 0, |
| 729 | AC_VERB_GET_DEVICE_LIST, devices); |
| 730 | if (parm == -1 && codec->bus->rirb_error) |
| 731 | break; |
| 732 | |
| 733 | for (i = 0; i < 8; i++) { |
| 734 | dev_list[devices] = (u8)parm; |
| 735 | parm >>= 4; |
| 736 | devices++; |
| 737 | if (devices >= dev_len) |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | return devices; |
| 742 | } |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | /** |
| 745 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 746 | * @bus: the BUS |
| 747 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 748 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 749 | * |
| 750 | * Adds the given event to the queue. The events are processed in |
| 751 | * the workqueue asynchronously. Call this function in the interrupt |
| 752 | * hanlder when RIRB receives an unsolicited event. |
| 753 | * |
| 754 | * Returns 0 if successful, or a negative error code. |
| 755 | */ |
| 756 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 757 | { |
| 758 | struct hda_bus_unsolicited *unsol; |
| 759 | unsigned int wp; |
| 760 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 761 | if (!bus || !bus->workq) |
| 762 | return 0; |
| 763 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 764 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 765 | unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 767 | unsol->wp = wp; |
| 768 | |
| 769 | wp <<= 1; |
| 770 | unsol->queue[wp] = res; |
| 771 | unsol->queue[wp + 1] = res_ex; |
| 772 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 773 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | return 0; |
| 776 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 777 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 780 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 782 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 784 | struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work); |
| 785 | struct hda_bus_unsolicited *unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | struct hda_codec *codec; |
| 787 | unsigned int rp, caddr, res; |
| 788 | |
| 789 | while (unsol->rp != unsol->wp) { |
| 790 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 791 | unsol->rp = rp; |
| 792 | rp <<= 1; |
| 793 | res = unsol->queue[rp]; |
| 794 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 795 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | continue; |
| 797 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 798 | if (codec && codec->patch_ops.unsol_event) |
| 799 | codec->patch_ops.unsol_event(codec, res); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | * destructor |
| 805 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 806 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 808 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 809 | return; |
| 810 | |
| 811 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 812 | if (bus->workq) |
| 813 | flush_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (bus->ops.private_free) |
| 815 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 816 | if (bus->workq) |
| 817 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 822 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 824 | snd_hda_bus_free(device->device_data); |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 829 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 831 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 832 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * snd_hda_bus_new - create a HDA bus |
| 837 | * @card: the card entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | * @busp: the pointer to store the created bus instance |
| 839 | * |
| 840 | * Returns 0 if successful, or a negative error code. |
| 841 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 842 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | ef74497 | 2015-02-17 13:56:29 +0100 | [diff] [blame] | 843 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
| 845 | struct hda_bus *bus; |
| 846 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 847 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 848 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | .dev_free = snd_hda_bus_dev_free, |
| 850 | }; |
| 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | if (busp) |
| 853 | *busp = NULL; |
| 854 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 855 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 857 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return -ENOMEM; |
| 859 | } |
| 860 | |
| 861 | bus->card = card; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 862 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 863 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | INIT_LIST_HEAD(&bus->codec_list); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 865 | INIT_WORK(&bus->unsol.work, process_unsol_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 867 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 868 | "hd-audio%d", card->number); |
| 869 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 870 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 871 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 872 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 873 | kfree(bus); |
| 874 | return -ENOMEM; |
| 875 | } |
| 876 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 877 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 878 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | snd_hda_bus_free(bus); |
| 880 | return err; |
| 881 | } |
| 882 | if (busp) |
| 883 | *busp = bus; |
| 884 | return 0; |
| 885 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 886 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 888 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 889 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 890 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 891 | #else |
| 892 | #define is_generic_config(codec) 0 |
| 893 | #endif |
| 894 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 895 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 896 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 897 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 898 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 899 | #endif |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /* |
| 902 | * find a matching codec preset |
| 903 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 904 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 905 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 907 | struct hda_codec_preset_list *tbl; |
| 908 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 909 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 911 | again: |
| 912 | mutex_lock(&preset_mutex); |
| 913 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 914 | if (!try_module_get(tbl->owner)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 915 | codec_err(codec, "cannot module_get\n"); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 916 | continue; |
| 917 | } |
| 918 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 920 | if (preset->afg && preset->afg != codec->afg) |
| 921 | continue; |
| 922 | if (preset->mfg && preset->mfg != codec->mfg) |
| 923 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 924 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 926 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 927 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 928 | preset->rev == codec->revision_id)) { |
| 929 | mutex_unlock(&preset_mutex); |
| 930 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 934 | module_put(tbl->owner); |
| 935 | } |
| 936 | mutex_unlock(&preset_mutex); |
| 937 | |
| 938 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 939 | if (!mod_requested) |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 940 | request_module("snd-hda-codec-id:%08x", |
| 941 | codec->vendor_id); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 942 | else |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 943 | request_module("snd-hda-codec-id:%04x*", |
| 944 | (codec->vendor_id >> 16) & 0xffff); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 945 | mod_requested++; |
| 946 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | return NULL; |
| 949 | } |
| 950 | |
| 951 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 952 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 954 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
| 956 | const struct hda_vendor_id *c; |
| 957 | const char *vendor = NULL; |
| 958 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 959 | char tmp[16]; |
| 960 | |
| 961 | if (codec->vendor_name) |
| 962 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
| 964 | for (c = hda_vendor_ids; c->id; c++) { |
| 965 | if (c->id == vendor_id) { |
| 966 | vendor = c->name; |
| 967 | break; |
| 968 | } |
| 969 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 970 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | sprintf(tmp, "Generic %04x", vendor_id); |
| 972 | vendor = tmp; |
| 973 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 974 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 975 | if (!codec->vendor_name) |
| 976 | return -ENOMEM; |
| 977 | |
| 978 | get_chip_name: |
| 979 | if (codec->chip_name) |
| 980 | return 0; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 983 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 984 | else { |
| 985 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 986 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 987 | } |
| 988 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 989 | return -ENOMEM; |
| 990 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 994 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 996 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 998 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | hda_nid_t nid; |
| 1000 | |
| 1001 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1002 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1003 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1004 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1005 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1006 | case AC_GRP_AUDIO_FUNCTION: |
| 1007 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1008 | codec->afg_function_id = function_id & 0xff; |
| 1009 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1010 | break; |
| 1011 | case AC_GRP_MODEM_FUNCTION: |
| 1012 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1013 | codec->mfg_function_id = function_id & 0xff; |
| 1014 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1015 | break; |
| 1016 | default: |
| 1017 | break; |
| 1018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1023 | * read widget caps for each widget and store in cache |
| 1024 | */ |
| 1025 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1026 | { |
| 1027 | int i; |
| 1028 | hda_nid_t nid; |
| 1029 | |
| 1030 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1031 | &codec->start_nid); |
| 1032 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1033 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1034 | return -ENOMEM; |
| 1035 | nid = codec->start_nid; |
| 1036 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1037 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1038 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1042 | /* read all pin default configurations and save codec->init_pins */ |
| 1043 | static int read_pin_defaults(struct hda_codec *codec) |
| 1044 | { |
| 1045 | int i; |
| 1046 | hda_nid_t nid = codec->start_nid; |
| 1047 | |
| 1048 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1049 | struct hda_pincfg *pin; |
| 1050 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1051 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1052 | if (wid_type != AC_WID_PIN) |
| 1053 | continue; |
| 1054 | pin = snd_array_new(&codec->init_pins); |
| 1055 | if (!pin) |
| 1056 | return -ENOMEM; |
| 1057 | pin->nid = nid; |
| 1058 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1059 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1060 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1061 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1062 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1063 | } |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /* look up the given pin config list and return the item matching with NID */ |
| 1068 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1069 | struct snd_array *array, |
| 1070 | hda_nid_t nid) |
| 1071 | { |
| 1072 | int i; |
| 1073 | for (i = 0; i < array->used; i++) { |
| 1074 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1075 | if (pin->nid == nid) |
| 1076 | return pin; |
| 1077 | } |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1081 | /* set the current pin config value for the given NID. |
| 1082 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1083 | */ |
| 1084 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1085 | hda_nid_t nid, unsigned int cfg) |
| 1086 | { |
| 1087 | struct hda_pincfg *pin; |
| 1088 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1089 | /* the check below may be invalid when pins are added by a fixup |
| 1090 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1091 | * for now |
| 1092 | */ |
| 1093 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1094 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1095 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1096 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1097 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1098 | pin = look_up_pincfg(codec, list, nid); |
| 1099 | if (!pin) { |
| 1100 | pin = snd_array_new(list); |
| 1101 | if (!pin) |
| 1102 | return -ENOMEM; |
| 1103 | pin->nid = nid; |
| 1104 | } |
| 1105 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1109 | /** |
| 1110 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1111 | * @codec: the HDA codec |
| 1112 | * @nid: NID to set the pin config |
| 1113 | * @cfg: the pin default config value |
| 1114 | * |
| 1115 | * Override a pin default configuration value in the cache. |
| 1116 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1117 | * priority than the real hardware value. |
| 1118 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1119 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1120 | hda_nid_t nid, unsigned int cfg) |
| 1121 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1122 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1123 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1124 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1125 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1126 | /** |
| 1127 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1128 | * @codec: the HDA codec |
| 1129 | * @nid: NID to get the pin config |
| 1130 | * |
| 1131 | * Get the current pin config value of the given pin NID. |
| 1132 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1133 | * returns the cached value. |
| 1134 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1135 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1136 | { |
| 1137 | struct hda_pincfg *pin; |
| 1138 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1139 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1140 | { |
| 1141 | unsigned int cfg = 0; |
| 1142 | mutex_lock(&codec->user_mutex); |
| 1143 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1144 | if (pin) |
| 1145 | cfg = pin->cfg; |
| 1146 | mutex_unlock(&codec->user_mutex); |
| 1147 | if (cfg) |
| 1148 | return cfg; |
| 1149 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1150 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1151 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1152 | if (pin) |
| 1153 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1154 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1155 | if (pin) |
| 1156 | return pin->cfg; |
| 1157 | return 0; |
| 1158 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1159 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1160 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1161 | /** |
| 1162 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 1163 | * @codec: the HDA codec |
| 1164 | * @nid: pin NID |
| 1165 | * @val: assigned pinctl value |
| 1166 | * |
| 1167 | * This function stores the given value to a pinctl target value in the |
| 1168 | * pincfg table. This isn't always as same as the actually written value |
| 1169 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 1170 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1171 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1172 | unsigned int val) |
| 1173 | { |
| 1174 | struct hda_pincfg *pin; |
| 1175 | |
| 1176 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1177 | if (!pin) |
| 1178 | return -EINVAL; |
| 1179 | pin->target = val; |
| 1180 | return 0; |
| 1181 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1182 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1183 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1184 | /** |
| 1185 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1186 | * @codec: the HDA codec |
| 1187 | * @nid: pin NID |
| 1188 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1189 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1190 | { |
| 1191 | struct hda_pincfg *pin; |
| 1192 | |
| 1193 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1194 | if (!pin) |
| 1195 | return 0; |
| 1196 | return pin->target; |
| 1197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1198 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1199 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1200 | /** |
| 1201 | * snd_hda_shutup_pins - Shut up all pins |
| 1202 | * @codec: the HDA codec |
| 1203 | * |
| 1204 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1205 | * The controls aren't cached so that they can be resumed properly. |
| 1206 | */ |
| 1207 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1208 | { |
| 1209 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1210 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1211 | * the default pin setup at the next load of the driver |
| 1212 | */ |
| 1213 | if (codec->bus->shutdown) |
| 1214 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1215 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1216 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1217 | /* use read here for syncing after issuing each verb */ |
| 1218 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1219 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1220 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1221 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1222 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1223 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1224 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1225 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1226 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1227 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1228 | { |
| 1229 | int i; |
| 1230 | if (!codec->pins_shutup) |
| 1231 | return; |
| 1232 | if (codec->bus->shutdown) |
| 1233 | return; |
| 1234 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1235 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1236 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1237 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1238 | pin->ctrl); |
| 1239 | } |
| 1240 | codec->pins_shutup = 0; |
| 1241 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1242 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1243 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1244 | static void hda_jackpoll_work(struct work_struct *work) |
| 1245 | { |
| 1246 | struct hda_codec *codec = |
| 1247 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1248 | |
| 1249 | snd_hda_jack_set_dirty_all(codec); |
| 1250 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1251 | |
| 1252 | if (!codec->jackpoll_interval) |
| 1253 | return; |
| 1254 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1255 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1256 | codec->jackpoll_interval); |
| 1257 | } |
| 1258 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1259 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1260 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1261 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1262 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1263 | /* release all pincfg lists */ |
| 1264 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1265 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1266 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1267 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1268 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1269 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1270 | snd_array_free(&codec->init_pins); |
| 1271 | } |
| 1272 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1273 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1274 | * audio-converter setup caches |
| 1275 | */ |
| 1276 | struct hda_cvt_setup { |
| 1277 | hda_nid_t nid; |
| 1278 | u8 stream_tag; |
| 1279 | u8 channel_id; |
| 1280 | u16 format_id; |
| 1281 | unsigned char active; /* cvt is currently used */ |
| 1282 | unsigned char dirty; /* setups should be cleared */ |
| 1283 | }; |
| 1284 | |
| 1285 | /* get or create a cache entry for the given audio converter NID */ |
| 1286 | static struct hda_cvt_setup * |
| 1287 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1288 | { |
| 1289 | struct hda_cvt_setup *p; |
| 1290 | int i; |
| 1291 | |
| 1292 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1293 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1294 | if (p->nid == nid) |
| 1295 | return p; |
| 1296 | } |
| 1297 | p = snd_array_new(&codec->cvt_setups); |
| 1298 | if (p) |
| 1299 | p->nid = nid; |
| 1300 | return p; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1304 | * Dynamic symbol binding for the codec parsers |
| 1305 | */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1306 | |
| 1307 | #define load_parser(codec, sym) \ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1308 | ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1309 | |
| 1310 | static void unload_parser(struct hda_codec *codec) |
| 1311 | { |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1312 | if (codec->parser) |
| 1313 | symbol_put_addr(codec->parser); |
| 1314 | codec->parser = NULL; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | * codec destructor |
| 1319 | */ |
| 1320 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1321 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1322 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1324 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1325 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1326 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1327 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1328 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1329 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1330 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1332 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1333 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1334 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1335 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1336 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1338 | if (codec->patch_ops.free) |
| 1339 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1340 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1341 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1342 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1343 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1344 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1345 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1346 | kfree(codec->vendor_name); |
| 1347 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1348 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1349 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1350 | codec->bus->num_codecs--; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1351 | put_device(&codec->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1354 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1355 | hda_nid_t fg, unsigned int power_state); |
| 1356 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1357 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1358 | unsigned int power_state); |
| 1359 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1360 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1361 | { |
| 1362 | struct hda_codec *codec = device->device_data; |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1363 | int err = device_add(&codec->dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1364 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1365 | if (err < 0) |
| 1366 | return err; |
| 1367 | snd_hda_register_beep_device(codec); |
| 1368 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1372 | { |
| 1373 | struct hda_codec *codec = device->device_data; |
| 1374 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1375 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1376 | device_del(&codec->dev); |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1380 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1381 | { |
| 1382 | snd_hda_codec_free(device->device_data); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1386 | /* just free the container */ |
| 1387 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1388 | { |
| 1389 | kfree(container_of(dev, struct hda_codec, dev)); |
| 1390 | } |
| 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /** |
| 1393 | * snd_hda_codec_new - create a HDA codec |
| 1394 | * @bus: the bus to assign |
| 1395 | * @codec_addr: the codec address |
| 1396 | * @codecp: the pointer to store the generated codec |
| 1397 | * |
| 1398 | * Returns 0 if successful, or a negative error code. |
| 1399 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1400 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1401 | unsigned int codec_addr, |
| 1402 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | { |
| 1404 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1405 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1406 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1408 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1409 | .dev_register = snd_hda_codec_dev_register, |
| 1410 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1411 | .dev_free = snd_hda_codec_dev_free, |
| 1412 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1414 | if (snd_BUG_ON(!bus)) |
| 1415 | return -EINVAL; |
| 1416 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1417 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| 1419 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1420 | dev_err(bus->card->dev, |
| 1421 | "address 0x%x is already occupied\n", |
| 1422 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | return -EBUSY; |
| 1424 | } |
| 1425 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1426 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1428 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return -ENOMEM; |
| 1430 | } |
| 1431 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1432 | device_initialize(&codec->dev); |
| 1433 | codec->dev.parent = &bus->card->card_dev; |
| 1434 | codec->dev.class = sound_class; |
| 1435 | codec->dev.release = snd_hda_codec_dev_release; |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1436 | codec->dev.groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1437 | dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number, |
| 1438 | codec_addr); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1439 | dev_set_drvdata(&codec->dev, codec); /* for sysfs */ |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | codec->bus = bus; |
| 1442 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1443 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1444 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1445 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1446 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1447 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1448 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1449 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1450 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1451 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1452 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1453 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1454 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1455 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1456 | INIT_LIST_HEAD(&codec->conn_list); |
| 1457 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1458 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1459 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1460 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1462 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1463 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1464 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1465 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1466 | * the caller has to power down appropriatley after initialization |
| 1467 | * phase. |
| 1468 | */ |
| 1469 | hda_keep_power_on(codec); |
| 1470 | #endif |
| 1471 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1472 | snd_hda_sysfs_init(codec); |
| 1473 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1474 | if (codec->bus->modelname) { |
| 1475 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1476 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1477 | err = -ENODEV; |
| 1478 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1483 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | bus->caddr_tbl[codec_addr] = codec; |
| 1486 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1487 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1488 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1489 | if (codec->vendor_id == -1) |
| 1490 | /* read again, hopefully the access method was corrected |
| 1491 | * in the last read... |
| 1492 | */ |
| 1493 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1494 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1495 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1496 | AC_PAR_SUBSYSTEM_ID); |
| 1497 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1498 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1500 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1501 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1502 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1503 | err = -ENODEV; |
| 1504 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1507 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1508 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1509 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1510 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1511 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1512 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1513 | err = read_pin_defaults(codec); |
| 1514 | if (err < 0) |
| 1515 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1516 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1517 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1518 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1519 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1520 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1521 | } |
| 1522 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1523 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1524 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1525 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1526 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1527 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1528 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1529 | #ifdef CONFIG_PM |
| 1530 | if (!codec->d3_stop_clk || !codec->epss) |
| 1531 | bus->power_keep_link_on = 1; |
| 1532 | #endif |
| 1533 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1534 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1535 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1536 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1537 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1538 | snd_hda_codec_proc_new(codec); |
| 1539 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1540 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1541 | |
| 1542 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1543 | codec->subsystem_id, codec->revision_id); |
| 1544 | snd_component_add(codec->bus->card, component); |
| 1545 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1546 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1547 | if (err < 0) |
| 1548 | goto error; |
| 1549 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1550 | if (codecp) |
| 1551 | *codecp = codec; |
| 1552 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1553 | |
| 1554 | error: |
| 1555 | snd_hda_codec_free(codec); |
| 1556 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1557 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1558 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1559 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1560 | /** |
| 1561 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1562 | * @codec: the HDA codec |
| 1563 | * |
| 1564 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1565 | * the given codec. |
| 1566 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1567 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1568 | { |
| 1569 | hda_nid_t fg; |
| 1570 | int err; |
| 1571 | |
| 1572 | /* Assume the function group node does not change, |
| 1573 | * only the widget nodes may change. |
| 1574 | */ |
| 1575 | kfree(codec->wcaps); |
| 1576 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1577 | err = read_widget_caps(codec, fg); |
| 1578 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1579 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1580 | return err; |
| 1581 | } |
| 1582 | |
| 1583 | snd_array_free(&codec->init_pins); |
| 1584 | err = read_pin_defaults(codec); |
| 1585 | |
| 1586 | return err; |
| 1587 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1588 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1589 | |
| 1590 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 1591 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1592 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1593 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1594 | { |
| 1595 | hda_nid_t nid = codec->start_nid; |
| 1596 | int i; |
| 1597 | |
| 1598 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1599 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1600 | switch (get_wcaps_type(wcaps)) { |
| 1601 | case AC_WID_AUD_IN: |
| 1602 | return false; /* HDMI parser supports only HDMI out */ |
| 1603 | case AC_WID_AUD_OUT: |
| 1604 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1605 | return false; |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | return true; |
| 1610 | } |
| 1611 | #else |
| 1612 | /* no HDMI codec parser support */ |
| 1613 | #define is_likely_hdmi_codec(codec) false |
| 1614 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1615 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1616 | /** |
| 1617 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1618 | * @codec: the HDA codec |
| 1619 | * |
| 1620 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1621 | * patch instance. |
| 1622 | * |
| 1623 | * Returns 0 if successful or a negative error code. |
| 1624 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1625 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1626 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1627 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1628 | int err; |
| 1629 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1630 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1631 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1632 | err = get_codec_name(codec); |
| 1633 | if (err < 0) |
| 1634 | return err; |
| 1635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1637 | if (!is_generic_config(codec) && codec->preset) |
| 1638 | patch = codec->preset->patch; |
| 1639 | if (!patch) { |
| 1640 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1641 | if (is_likely_hdmi_codec(codec)) { |
| 1642 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1643 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1644 | #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) |
| 1645 | patch = snd_hda_parse_hdmi_codec; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1646 | #endif |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1647 | } |
| 1648 | if (!patch) { |
| 1649 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 1650 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1651 | #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) |
| 1652 | patch = snd_hda_parse_generic_codec; |
| 1653 | #endif |
| 1654 | } |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1655 | if (!patch) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1656 | codec_err(codec, "No codec parser is available\n"); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1657 | return -ENODEV; |
| 1658 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1661 | err = patch(codec); |
| 1662 | if (err < 0) { |
| 1663 | unload_parser(codec); |
| 1664 | return err; |
| 1665 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1666 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1667 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1668 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1669 | snprintf(codec->bus->card->mixername, |
| 1670 | sizeof(codec->bus->card->mixername), |
| 1671 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1672 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1674 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1676 | /* update the stream-id if changed */ |
| 1677 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1678 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1679 | u32 stream_tag, int channel_id) |
| 1680 | { |
| 1681 | unsigned int oldval, newval; |
| 1682 | |
| 1683 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1684 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1685 | newval = (stream_tag << 4) | channel_id; |
| 1686 | if (oldval != newval) |
| 1687 | snd_hda_codec_write(codec, nid, 0, |
| 1688 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1689 | newval); |
| 1690 | p->stream_tag = stream_tag; |
| 1691 | p->channel_id = channel_id; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | /* update the format-id if changed */ |
| 1696 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1697 | hda_nid_t nid, int format) |
| 1698 | { |
| 1699 | unsigned int oldval; |
| 1700 | |
| 1701 | if (p->format_id != format) { |
| 1702 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1703 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1704 | if (oldval != format) { |
| 1705 | msleep(1); |
| 1706 | snd_hda_codec_write(codec, nid, 0, |
| 1707 | AC_VERB_SET_STREAM_FORMAT, |
| 1708 | format); |
| 1709 | } |
| 1710 | p->format_id = format; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | /** |
| 1715 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1716 | * @codec: the CODEC to set up |
| 1717 | * @nid: the NID to set up |
| 1718 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1719 | * @channel_id: channel id to pass, zero based. |
| 1720 | * @format: stream format. |
| 1721 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1722 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1723 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | int channel_id, int format) |
| 1725 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1726 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1727 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1728 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1729 | int i; |
| 1730 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1731 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1732 | return; |
| 1733 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1734 | codec_dbg(codec, |
| 1735 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1736 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1737 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1738 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1739 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1740 | |
| 1741 | if (codec->pcm_format_first) |
| 1742 | update_pcm_format(codec, p, nid, format); |
| 1743 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1744 | if (!codec->pcm_format_first) |
| 1745 | update_pcm_format(codec, p, nid, format); |
| 1746 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1747 | p->active = 1; |
| 1748 | p->dirty = 0; |
| 1749 | |
| 1750 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1751 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1752 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1753 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1754 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1755 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1756 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1757 | p->dirty = 1; |
| 1758 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1761 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1763 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1764 | struct hda_cvt_setup *q); |
| 1765 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1766 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1767 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1768 | * @codec: the CODEC to clean up |
| 1769 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1770 | * @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] | 1771 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1772 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1773 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1774 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1775 | struct hda_cvt_setup *p; |
| 1776 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1777 | if (!nid) |
| 1778 | return; |
| 1779 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1780 | if (codec->no_sticky_stream) |
| 1781 | do_now = 1; |
| 1782 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1783 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1784 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1785 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1786 | /* here we just clear the active flag when do_now isn't set; |
| 1787 | * actual clean-ups will be done later in |
| 1788 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1789 | */ |
| 1790 | if (do_now) |
| 1791 | really_cleanup_stream(codec, p); |
| 1792 | else |
| 1793 | p->active = 0; |
| 1794 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1795 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1796 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1797 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1798 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1799 | struct hda_cvt_setup *q) |
| 1800 | { |
| 1801 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1802 | if (q->stream_tag || q->channel_id) |
| 1803 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1804 | if (q->format_id) |
| 1805 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1806 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1807 | memset(q, 0, sizeof(*q)); |
| 1808 | q->nid = nid; |
| 1809 | } |
| 1810 | |
| 1811 | /* clean up the all conflicting obsolete streams */ |
| 1812 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1813 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1814 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1815 | int i; |
| 1816 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1817 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1818 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1819 | struct hda_cvt_setup *p; |
| 1820 | p = snd_array_elem(&c->cvt_setups, i); |
| 1821 | if (p->dirty) |
| 1822 | really_cleanup_stream(c, p); |
| 1823 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1827 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1828 | /* clean up all streams; called from suspend */ |
| 1829 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1830 | { |
| 1831 | int i; |
| 1832 | |
| 1833 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1834 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1835 | if (p->stream_tag) |
| 1836 | really_cleanup_stream(codec, p); |
| 1837 | } |
| 1838 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1839 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | /* |
| 1842 | * amp access functions |
| 1843 | */ |
| 1844 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1845 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1846 | #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] | 1847 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1848 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1849 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1851 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
| 1853 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1854 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1855 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1857 | memset(cache, 0, sizeof(*cache)); |
| 1858 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1859 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1862 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1863 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1864 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1868 | 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] | 1869 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1870 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1871 | u16 cur = cache->hash[idx]; |
| 1872 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
| 1874 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1875 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | if (info->key == key) |
| 1877 | return info; |
| 1878 | cur = info->next; |
| 1879 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1880 | return NULL; |
| 1881 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1883 | /* query the hash. allocate an entry if not found. */ |
| 1884 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1885 | u32 key) |
| 1886 | { |
| 1887 | struct hda_cache_head *info = get_hash(cache, key); |
| 1888 | if (!info) { |
| 1889 | u16 idx, cur; |
| 1890 | /* add a new hash entry */ |
| 1891 | info = snd_array_new(&cache->buf); |
| 1892 | if (!info) |
| 1893 | return NULL; |
| 1894 | cur = snd_array_index(&cache->buf, info); |
| 1895 | info->key = key; |
| 1896 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1897 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1898 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1899 | info->next = cache->hash[idx]; |
| 1900 | cache->hash[idx] = cur; |
| 1901 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | return info; |
| 1903 | } |
| 1904 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1905 | /* query and allocate an amp hash entry */ |
| 1906 | static inline struct hda_amp_info * |
| 1907 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1908 | { |
| 1909 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1910 | } |
| 1911 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1912 | /* overwrite the value with the key in the caps hash */ |
| 1913 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1914 | { |
| 1915 | struct hda_amp_info *info; |
| 1916 | |
| 1917 | mutex_lock(&codec->hash_mutex); |
| 1918 | info = get_alloc_amp_hash(codec, key); |
| 1919 | if (!info) { |
| 1920 | mutex_unlock(&codec->hash_mutex); |
| 1921 | return -EINVAL; |
| 1922 | } |
| 1923 | info->amp_caps = val; |
| 1924 | info->head.val |= INFO_AMP_CAPS; |
| 1925 | mutex_unlock(&codec->hash_mutex); |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
| 1929 | /* query the value from the caps hash; if not found, fetch the current |
| 1930 | * value from the given function and store in the hash |
| 1931 | */ |
| 1932 | static unsigned int |
| 1933 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1934 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1935 | { |
| 1936 | struct hda_amp_info *info; |
| 1937 | unsigned int val; |
| 1938 | |
| 1939 | mutex_lock(&codec->hash_mutex); |
| 1940 | info = get_alloc_amp_hash(codec, key); |
| 1941 | if (!info) { |
| 1942 | mutex_unlock(&codec->hash_mutex); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1946 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1947 | val = func(codec, nid, dir); |
| 1948 | write_caps_hash(codec, key, val); |
| 1949 | } else { |
| 1950 | val = info->amp_caps; |
| 1951 | mutex_unlock(&codec->hash_mutex); |
| 1952 | } |
| 1953 | return val; |
| 1954 | } |
| 1955 | |
| 1956 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1957 | int direction) |
| 1958 | { |
| 1959 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1960 | nid = codec->afg; |
| 1961 | return snd_hda_param_read(codec, nid, |
| 1962 | direction == HDA_OUTPUT ? |
| 1963 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1964 | } |
| 1965 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1966 | /** |
| 1967 | * query_amp_caps - query AMP capabilities |
| 1968 | * @codec: the HD-auio codec |
| 1969 | * @nid: the NID to query |
| 1970 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1971 | * |
| 1972 | * Query AMP capabilities for the given widget and direction. |
| 1973 | * Returns the obtained capability bits. |
| 1974 | * |
| 1975 | * When cap bits have been already read, this doesn't read again but |
| 1976 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1978 | 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] | 1979 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1980 | return query_caps_hash(codec, nid, direction, |
| 1981 | HDA_HASH_KEY(nid, direction, 0), |
| 1982 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1984 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1986 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1987 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1988 | * @codec: the HD-audio codec |
| 1989 | * @nid: the NID to query |
| 1990 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1991 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1992 | * |
| 1993 | * Check whether the widget has the given amp capability for the direction. |
| 1994 | */ |
| 1995 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1996 | int dir, unsigned int bits) |
| 1997 | { |
| 1998 | if (!nid) |
| 1999 | return false; |
| 2000 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 2001 | if (query_amp_caps(codec, nid, dir) & bits) |
| 2002 | return true; |
| 2003 | return false; |
| 2004 | } |
| 2005 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 2006 | |
| 2007 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2008 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 2009 | * @codec: the CODEC to clean up |
| 2010 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2011 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2012 | * @caps: the capability bits to set |
| 2013 | * |
| 2014 | * Override the cached AMP caps bits value by the given one. |
| 2015 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 2016 | * e.g. limit to 0dB, etc. |
| 2017 | * |
| 2018 | * Returns zero if successful or a negative error code. |
| 2019 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2020 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2021 | unsigned int caps) |
| 2022 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2023 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2024 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2025 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2026 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2027 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2028 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2029 | { |
| 2030 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 2031 | } |
| 2032 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2033 | /** |
| 2034 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 2035 | * @codec: the HD-auio codec |
| 2036 | * @nid: the NID to query |
| 2037 | * |
| 2038 | * Query PIN capabilities for the given widget. |
| 2039 | * Returns the obtained capability bits. |
| 2040 | * |
| 2041 | * When cap bits have been already read, this doesn't read again but |
| 2042 | * returns the cached value. |
| 2043 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2044 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2045 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2046 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2047 | read_pin_cap); |
| 2048 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2049 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2050 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2051 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2052 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2053 | * @codec: the CODEC |
| 2054 | * @nid: the NID to override |
| 2055 | * @caps: the capability bits to set |
| 2056 | * |
| 2057 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2058 | * |
| 2059 | * Returns zero if successful or a negative error code. |
| 2060 | */ |
| 2061 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2062 | unsigned int caps) |
| 2063 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2064 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2065 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2066 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2067 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2068 | /* read or sync the hash value with the current value; |
| 2069 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2071 | static struct hda_amp_info * |
| 2072 | 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] | 2073 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2075 | struct hda_amp_info *info; |
| 2076 | unsigned int parm, val = 0; |
| 2077 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2079 | retry: |
| 2080 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2081 | if (!info) |
| 2082 | return NULL; |
| 2083 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2084 | if (!val_read) { |
| 2085 | mutex_unlock(&codec->hash_mutex); |
| 2086 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2087 | parm |= direction == HDA_OUTPUT ? |
| 2088 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2089 | parm |= index; |
| 2090 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2091 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2092 | val &= 0xff; |
| 2093 | val_read = true; |
| 2094 | mutex_lock(&codec->hash_mutex); |
| 2095 | goto retry; |
| 2096 | } |
| 2097 | info->vol[ch] = val; |
| 2098 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2099 | } else if (init_only) |
| 2100 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2101 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2105 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2107 | 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] | 2108 | hda_nid_t nid, int ch, int direction, int index, |
| 2109 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | { |
| 2111 | u32 parm; |
| 2112 | |
| 2113 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2114 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2115 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2116 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2117 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2118 | ; /* set the zero value as a fake mute */ |
| 2119 | else |
| 2120 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2122 | } |
| 2123 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2124 | /** |
| 2125 | * snd_hda_codec_amp_read - Read AMP value |
| 2126 | * @codec: HD-audio codec |
| 2127 | * @nid: NID to read the AMP value |
| 2128 | * @ch: channel (left=0 or right=1) |
| 2129 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2130 | * @index: the index value (only for input direction) |
| 2131 | * |
| 2132 | * 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] | 2133 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2134 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2135 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2137 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2138 | unsigned int val = 0; |
| 2139 | |
| 2140 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2141 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2142 | if (info) |
| 2143 | val = info->vol[ch]; |
| 2144 | mutex_unlock(&codec->hash_mutex); |
| 2145 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2147 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2149 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2150 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 2151 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2152 | { |
| 2153 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2154 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2155 | |
| 2156 | if (snd_BUG_ON(mask & ~0xff)) |
| 2157 | mask &= 0xff; |
| 2158 | val &= mask; |
| 2159 | |
| 2160 | mutex_lock(&codec->hash_mutex); |
| 2161 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2162 | if (!info) { |
| 2163 | mutex_unlock(&codec->hash_mutex); |
| 2164 | return 0; |
| 2165 | } |
| 2166 | val |= info->vol[ch] & ~mask; |
| 2167 | if (info->vol[ch] == val) { |
| 2168 | mutex_unlock(&codec->hash_mutex); |
| 2169 | return 0; |
| 2170 | } |
| 2171 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 2172 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2173 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2174 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2175 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2176 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2177 | return 1; |
| 2178 | } |
| 2179 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2180 | /** |
| 2181 | * snd_hda_codec_amp_update - update the AMP value |
| 2182 | * @codec: HD-audio codec |
| 2183 | * @nid: NID to read the AMP value |
| 2184 | * @ch: channel (left=0 or right=1) |
| 2185 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2186 | * @idx: the index value (only for input direction) |
| 2187 | * @mask: bit mask to set |
| 2188 | * @val: the bits value to set |
| 2189 | * |
| 2190 | * Update the AMP value with a bit mask. |
| 2191 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2192 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2193 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2194 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 2196 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 2197 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2199 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2201 | /** |
| 2202 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2203 | * @codec: HD-audio codec |
| 2204 | * @nid: NID to read the AMP value |
| 2205 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2206 | * @idx: the index value (only for input direction) |
| 2207 | * @mask: bit mask to set |
| 2208 | * @val: the bits value to set |
| 2209 | * |
| 2210 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2211 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2212 | */ |
| 2213 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2214 | int direction, int idx, int mask, int val) |
| 2215 | { |
| 2216 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2217 | |
| 2218 | if (snd_BUG_ON(mask & ~0xff)) |
| 2219 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2220 | for (ch = 0; ch < 2; ch++) |
| 2221 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2222 | idx, mask, val); |
| 2223 | return ret; |
| 2224 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2225 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2226 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2227 | /** |
| 2228 | * snd_hda_codec_amp_init - initialize the AMP value |
| 2229 | * @codec: the HDA codec |
| 2230 | * @nid: NID to read the AMP value |
| 2231 | * @ch: channel (left=0 or right=1) |
| 2232 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2233 | * @idx: the index value (only for input direction) |
| 2234 | * @mask: bit mask to set |
| 2235 | * @val: the bits value to set |
| 2236 | * |
| 2237 | * 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] | 2238 | * the first access. If the amp was already initialized / updated beforehand, |
| 2239 | * this does nothing. |
| 2240 | */ |
| 2241 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2242 | int dir, int idx, int mask, int val) |
| 2243 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 2244 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 2245 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2246 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2247 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2248 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2249 | /** |
| 2250 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 2251 | * @codec: the HDA codec |
| 2252 | * @nid: NID to read the AMP value |
| 2253 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2254 | * @idx: the index value (only for input direction) |
| 2255 | * @mask: bit mask to set |
| 2256 | * @val: the bits value to set |
| 2257 | * |
| 2258 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 2259 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2260 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2261 | int dir, int idx, int mask, int val) |
| 2262 | { |
| 2263 | int ch, ret = 0; |
| 2264 | |
| 2265 | if (snd_BUG_ON(mask & ~0xff)) |
| 2266 | mask &= 0xff; |
| 2267 | for (ch = 0; ch < 2; ch++) |
| 2268 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2269 | idx, mask, val); |
| 2270 | return ret; |
| 2271 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2272 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2273 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2274 | /** |
| 2275 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2276 | * @codec: HD-audio codec |
| 2277 | * |
| 2278 | * Resume the all amp commands from the cache. |
| 2279 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2280 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2281 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2282 | int i; |
| 2283 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2284 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2285 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2286 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2287 | struct hda_amp_info *buffer; |
| 2288 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2289 | hda_nid_t nid; |
| 2290 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2291 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2292 | |
| 2293 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2294 | if (!buffer->head.dirty) |
| 2295 | continue; |
| 2296 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2297 | info = *buffer; |
| 2298 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2299 | if (!key) |
| 2300 | continue; |
| 2301 | nid = key & 0xff; |
| 2302 | idx = (key >> 16) & 0xff; |
| 2303 | dir = (key >> 24) & 0xff; |
| 2304 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2305 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2306 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2307 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2308 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2309 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2310 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2311 | } |
| 2312 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2313 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2314 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2315 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2317 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2318 | unsigned int ofs) |
| 2319 | { |
| 2320 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2321 | /* get num steps */ |
| 2322 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2323 | if (ofs < caps) |
| 2324 | caps -= ofs; |
| 2325 | return caps; |
| 2326 | } |
| 2327 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2328 | /** |
| 2329 | * 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] | 2330 | * @kcontrol: referred ctl element |
| 2331 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2332 | * |
| 2333 | * The control element is supposed to have the private_value field |
| 2334 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2335 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2336 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2337 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | { |
| 2339 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2340 | u16 nid = get_amp_nid(kcontrol); |
| 2341 | u8 chs = get_amp_channels(kcontrol); |
| 2342 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2343 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2345 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2346 | uinfo->count = chs == 3 ? 2 : 1; |
| 2347 | uinfo->value.integer.min = 0; |
| 2348 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2349 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2350 | codec_warn(codec, |
| 2351 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2352 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | return -EINVAL; |
| 2354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | return 0; |
| 2356 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2357 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2359 | |
| 2360 | static inline unsigned int |
| 2361 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2362 | int ch, int dir, int idx, unsigned int ofs) |
| 2363 | { |
| 2364 | unsigned int val; |
| 2365 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2366 | val &= HDA_AMP_VOLMASK; |
| 2367 | if (val >= ofs) |
| 2368 | val -= ofs; |
| 2369 | else |
| 2370 | val = 0; |
| 2371 | return val; |
| 2372 | } |
| 2373 | |
| 2374 | static inline int |
| 2375 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2376 | int ch, int dir, int idx, unsigned int ofs, |
| 2377 | unsigned int val) |
| 2378 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2379 | unsigned int maxval; |
| 2380 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2381 | if (val > 0) |
| 2382 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2383 | /* ofs = 0: raw max value */ |
| 2384 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2385 | if (val > maxval) |
| 2386 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 2387 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2388 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2391 | /** |
| 2392 | * 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] | 2393 | * @kcontrol: ctl element |
| 2394 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2395 | * |
| 2396 | * The control element is supposed to have the private_value field |
| 2397 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2398 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2399 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2400 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | { |
| 2402 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2403 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2404 | int chs = get_amp_channels(kcontrol); |
| 2405 | int dir = get_amp_direction(kcontrol); |
| 2406 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2407 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | long *valp = ucontrol->value.integer.value; |
| 2409 | |
| 2410 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2411 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2413 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | return 0; |
| 2415 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2416 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2418 | /** |
| 2419 | * 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] | 2420 | * @kcontrol: ctl element |
| 2421 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2422 | * |
| 2423 | * The control element is supposed to have the private_value field |
| 2424 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2425 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2426 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2427 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | { |
| 2429 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2430 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2431 | int chs = get_amp_channels(kcontrol); |
| 2432 | int dir = get_amp_direction(kcontrol); |
| 2433 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2434 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | long *valp = ucontrol->value.integer.value; |
| 2436 | int change = 0; |
| 2437 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2438 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2439 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2440 | valp++; |
| 2441 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2442 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2443 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | return change; |
| 2445 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2446 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2448 | /** |
| 2449 | * 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] | 2450 | * @kcontrol: ctl element |
| 2451 | * @op_flag: operation flag |
| 2452 | * @size: byte size of input TLV |
| 2453 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2454 | * |
| 2455 | * The control element is supposed to have the private_value field |
| 2456 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2457 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2458 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2459 | unsigned int size, unsigned int __user *_tlv) |
| 2460 | { |
| 2461 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2462 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2463 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2464 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2465 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2466 | u32 caps, val1, val2; |
| 2467 | |
| 2468 | if (size < 4 * sizeof(unsigned int)) |
| 2469 | return -ENOMEM; |
| 2470 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2471 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2472 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2473 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2474 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2475 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2476 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2477 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2478 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2479 | return -EFAULT; |
| 2480 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2481 | return -EFAULT; |
| 2482 | if (put_user(val1, _tlv + 2)) |
| 2483 | return -EFAULT; |
| 2484 | if (put_user(val2, _tlv + 3)) |
| 2485 | return -EFAULT; |
| 2486 | return 0; |
| 2487 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2488 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2489 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2490 | /** |
| 2491 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2492 | * @codec: HD-audio codec |
| 2493 | * @nid: NID of a reference widget |
| 2494 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2495 | * @tlv: TLV data to be stored, at least 4 elements |
| 2496 | * |
| 2497 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2498 | * obtained from the reference NID. |
| 2499 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2500 | */ |
| 2501 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2502 | unsigned int *tlv) |
| 2503 | { |
| 2504 | u32 caps; |
| 2505 | int nums, step; |
| 2506 | |
| 2507 | caps = query_amp_caps(codec, nid, dir); |
| 2508 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2509 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2510 | step = (step + 1) * 25; |
| 2511 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2512 | tlv[1] = 2 * sizeof(unsigned int); |
| 2513 | tlv[2] = -nums * step; |
| 2514 | tlv[3] = step; |
| 2515 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2516 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2517 | |
| 2518 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2519 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2520 | 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] | 2521 | { |
| 2522 | struct snd_ctl_elem_id id; |
| 2523 | memset(&id, 0, sizeof(id)); |
| 2524 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2525 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2526 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2527 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2528 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2529 | strcpy(id.name, name); |
| 2530 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2531 | } |
| 2532 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2533 | /** |
| 2534 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2535 | * @codec: HD-audio codec |
| 2536 | * @name: ctl id name string |
| 2537 | * |
| 2538 | * Get the control element with the given id string and IFACE_MIXER. |
| 2539 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2540 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2541 | const char *name) |
| 2542 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2543 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2544 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2545 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2546 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2547 | 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] | 2548 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2549 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2550 | int i, idx; |
| 2551 | /* 16 ctlrs should be large enough */ |
| 2552 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2553 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2554 | return idx; |
| 2555 | } |
| 2556 | return -EBUSY; |
| 2557 | } |
| 2558 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2559 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2560 | * 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] | 2561 | * @codec: HD-audio codec |
| 2562 | * @nid: corresponding NID (optional) |
| 2563 | * @kctl: the control element to assign |
| 2564 | * |
| 2565 | * Add the given control element to an array inside the codec instance. |
| 2566 | * All control elements belonging to a codec are supposed to be added |
| 2567 | * by this function so that a proper clean-up works at the free or |
| 2568 | * reconfiguration time. |
| 2569 | * |
| 2570 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2571 | * The assignment is shown in the codec proc file. |
| 2572 | * |
| 2573 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2574 | * #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] | 2575 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2576 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2577 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2578 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2579 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2580 | { |
| 2581 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2582 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2583 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2584 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2585 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2586 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2587 | if (nid == 0) |
| 2588 | nid = get_amp_nid_(kctl->private_value); |
| 2589 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2590 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2591 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2592 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2593 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2594 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2595 | if (err < 0) |
| 2596 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2597 | item = snd_array_new(&codec->mixers); |
| 2598 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2599 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2600 | item->kctl = kctl; |
| 2601 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2602 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2605 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2606 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2607 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2608 | * snd_hda_add_nid - Assign a NID to a control element |
| 2609 | * @codec: HD-audio codec |
| 2610 | * @nid: corresponding NID (optional) |
| 2611 | * @kctl: the control element to assign |
| 2612 | * @index: index to kctl |
| 2613 | * |
| 2614 | * Add the given control element to an array inside the codec instance. |
| 2615 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2616 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2617 | */ |
| 2618 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2619 | unsigned int index, hda_nid_t nid) |
| 2620 | { |
| 2621 | struct hda_nid_item *item; |
| 2622 | |
| 2623 | if (nid > 0) { |
| 2624 | item = snd_array_new(&codec->nids); |
| 2625 | if (!item) |
| 2626 | return -ENOMEM; |
| 2627 | item->kctl = kctl; |
| 2628 | item->index = index; |
| 2629 | item->nid = nid; |
| 2630 | return 0; |
| 2631 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2632 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2633 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2634 | return -EINVAL; |
| 2635 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2636 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2637 | |
| 2638 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2639 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2640 | * @codec: HD-audio codec |
| 2641 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2642 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2643 | { |
| 2644 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2645 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2646 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2647 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2648 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2649 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2652 | /** |
| 2653 | * snd_hda_lock_devices - pseudo device locking |
| 2654 | * @bus: the BUS |
| 2655 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2656 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2657 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2658 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2659 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2660 | struct snd_card *card = bus->card; |
| 2661 | struct hda_codec *codec; |
| 2662 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2663 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2664 | if (card->shutdown) |
| 2665 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2666 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2667 | if (!list_empty(&card->ctl_files)) |
| 2668 | goto err_clear; |
| 2669 | |
| 2670 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2671 | int pcm; |
| 2672 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2673 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2674 | if (!cpcm->pcm) |
| 2675 | continue; |
| 2676 | if (cpcm->pcm->streams[0].substream_opened || |
| 2677 | cpcm->pcm->streams[1].substream_opened) |
| 2678 | goto err_clear; |
| 2679 | } |
| 2680 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2681 | spin_unlock(&card->files_lock); |
| 2682 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2683 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2684 | err_clear: |
| 2685 | card->shutdown = 0; |
| 2686 | err_unlock: |
| 2687 | spin_unlock(&card->files_lock); |
| 2688 | return -EINVAL; |
| 2689 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2690 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2691 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2692 | /** |
| 2693 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2694 | * @bus: the BUS |
| 2695 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2696 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2697 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2698 | struct snd_card *card = bus->card; |
| 2699 | |
| 2700 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2701 | spin_lock(&card->files_lock); |
| 2702 | card->shutdown = 0; |
| 2703 | spin_unlock(&card->files_lock); |
| 2704 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2705 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2706 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2707 | /** |
| 2708 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2709 | * @codec: HD-audio codec |
| 2710 | * |
| 2711 | * This frees the all PCM and control elements assigned to the codec, and |
| 2712 | * clears the caches and restores the pin default configurations. |
| 2713 | * |
| 2714 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2715 | * returns zero. |
| 2716 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2717 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2718 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2719 | struct hda_bus *bus = codec->bus; |
| 2720 | struct snd_card *card = bus->card; |
| 2721 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2722 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2723 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2724 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2727 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2728 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2729 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2730 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2731 | #endif |
| 2732 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2733 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2734 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2735 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2736 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2737 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2738 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2739 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2740 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2741 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2742 | if (codec->patch_ops.free) |
| 2743 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2744 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2745 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2746 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2747 | codec->spec = NULL; |
| 2748 | free_hda_cache(&codec->amp_cache); |
| 2749 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2750 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2751 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2752 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2753 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2754 | snd_array_free(&codec->cvt_setups); |
| 2755 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2756 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2757 | codec->num_pcms = 0; |
| 2758 | codec->pcm_info = NULL; |
| 2759 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2760 | codec->slave_dig_outs = NULL; |
| 2761 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2762 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2763 | module_put(codec->owner); |
| 2764 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2765 | |
| 2766 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2767 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2768 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2771 | 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] | 2772 | |
| 2773 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2774 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2775 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2776 | { |
| 2777 | struct hda_nid_item *items; |
| 2778 | const char * const *s; |
| 2779 | int i, err; |
| 2780 | |
| 2781 | items = codec->mixers.list; |
| 2782 | for (i = 0; i < codec->mixers.used; i++) { |
| 2783 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2784 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2785 | continue; |
| 2786 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2787 | char tmpname[sizeof(sctl->id.name)]; |
| 2788 | const char *name = *s; |
| 2789 | if (suffix) { |
| 2790 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2791 | name, suffix); |
| 2792 | name = tmpname; |
| 2793 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2794 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2795 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2796 | if (err) |
| 2797 | return err; |
| 2798 | break; |
| 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2805 | static int check_slave_present(struct hda_codec *codec, |
| 2806 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2807 | { |
| 2808 | return 1; |
| 2809 | } |
| 2810 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2811 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2812 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2813 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2814 | { |
| 2815 | int _tlv[4]; |
| 2816 | const int *tlv = NULL; |
| 2817 | int val = -1; |
| 2818 | |
| 2819 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2820 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2821 | mm_segment_t fs = get_fs(); |
| 2822 | set_fs(get_ds()); |
| 2823 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2824 | tlv = _tlv; |
| 2825 | set_fs(fs); |
| 2826 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2827 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2828 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2829 | int step = tlv[3]; |
| 2830 | step &= ~TLV_DB_SCALE_MUTE; |
| 2831 | if (!step) |
| 2832 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2833 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2834 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2835 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2836 | return -1; |
| 2837 | } |
| 2838 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2839 | val = -tlv[2] / step; |
| 2840 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2841 | return val; |
| 2842 | } |
| 2843 | |
| 2844 | /* call kctl->put with the given value(s) */ |
| 2845 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2846 | { |
| 2847 | struct snd_ctl_elem_value *ucontrol; |
| 2848 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2849 | if (!ucontrol) |
| 2850 | return -ENOMEM; |
| 2851 | ucontrol->value.integer.value[0] = val; |
| 2852 | ucontrol->value.integer.value[1] = val; |
| 2853 | kctl->put(kctl, ucontrol); |
| 2854 | kfree(ucontrol); |
| 2855 | return 0; |
| 2856 | } |
| 2857 | |
| 2858 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2859 | static int init_slave_0dB(struct hda_codec *codec, |
| 2860 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2861 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2862 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2863 | if (offset > 0) |
| 2864 | put_kctl_with_value(slave, offset); |
| 2865 | return 0; |
| 2866 | } |
| 2867 | |
| 2868 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2869 | static int init_slave_unmute(struct hda_codec *codec, |
| 2870 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2871 | { |
| 2872 | return put_kctl_with_value(slave, 1); |
| 2873 | } |
| 2874 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2875 | static int add_slave(struct hda_codec *codec, |
| 2876 | void *data, struct snd_kcontrol *slave) |
| 2877 | { |
| 2878 | return snd_ctl_add_slave(data, slave); |
| 2879 | } |
| 2880 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2881 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2882 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2883 | * @codec: HD-audio codec |
| 2884 | * @name: vmaster control name |
| 2885 | * @tlv: TLV data (optional) |
| 2886 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2887 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2888 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2889 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2890 | * |
| 2891 | * Create a virtual master control with the given name. The TLV data |
| 2892 | * must be either NULL or a valid data. |
| 2893 | * |
| 2894 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2895 | * slave control name. All controls with these names are assigned to |
| 2896 | * the new virtual master control. |
| 2897 | * |
| 2898 | * This function returns zero if successful or a negative error code. |
| 2899 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2900 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2901 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2902 | const char *suffix, bool init_slave_vol, |
| 2903 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2904 | { |
| 2905 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2906 | int err; |
| 2907 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2908 | if (ctl_ret) |
| 2909 | *ctl_ret = NULL; |
| 2910 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2911 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2912 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2913 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2916 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2917 | if (!kctl) |
| 2918 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2919 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2920 | if (err < 0) |
| 2921 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2922 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2923 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2924 | if (err < 0) |
| 2925 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2926 | |
| 2927 | /* init with master mute & zero volume */ |
| 2928 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2929 | if (init_slave_vol) { |
| 2930 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2931 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2932 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2933 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2934 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2935 | if (ctl_ret) |
| 2936 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2937 | return 0; |
| 2938 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2939 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2940 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2941 | /* |
| 2942 | * mute-LED control using vmaster |
| 2943 | */ |
| 2944 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2945 | struct snd_ctl_elem_info *uinfo) |
| 2946 | { |
| 2947 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2948 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2949 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2950 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2951 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2955 | struct snd_ctl_elem_value *ucontrol) |
| 2956 | { |
| 2957 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2958 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2959 | return 0; |
| 2960 | } |
| 2961 | |
| 2962 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2963 | struct snd_ctl_elem_value *ucontrol) |
| 2964 | { |
| 2965 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2966 | unsigned int old_mode = hook->mute_mode; |
| 2967 | |
| 2968 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2969 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2970 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2971 | if (old_mode == hook->mute_mode) |
| 2972 | return 0; |
| 2973 | snd_hda_sync_vmaster_hook(hook); |
| 2974 | return 1; |
| 2975 | } |
| 2976 | |
| 2977 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2978 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2979 | .name = "Mute-LED Mode", |
| 2980 | .info = vmaster_mute_mode_info, |
| 2981 | .get = vmaster_mute_mode_get, |
| 2982 | .put = vmaster_mute_mode_put, |
| 2983 | }; |
| 2984 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2985 | /** |
| 2986 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2987 | * @codec: the HDA codec |
| 2988 | * @hook: the vmaster hook object |
| 2989 | * @expose_enum_ctl: flag to create an enum ctl |
| 2990 | * |
| 2991 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2992 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2993 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2994 | */ |
| 2995 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2996 | struct hda_vmaster_mute_hook *hook, |
| 2997 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2998 | { |
| 2999 | struct snd_kcontrol *kctl; |
| 3000 | |
| 3001 | if (!hook->hook || !hook->sw_kctl) |
| 3002 | return 0; |
| 3003 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 3004 | hook->codec = codec; |
| 3005 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 3006 | if (!expose_enum_ctl) |
| 3007 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3008 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 3009 | if (!kctl) |
| 3010 | return -ENOMEM; |
| 3011 | return snd_hda_ctl_add(codec, 0, kctl); |
| 3012 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3013 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3014 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3015 | /** |
| 3016 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 3017 | * @hook: the vmaster hook |
| 3018 | * |
| 3019 | * Call the hook with the current value for synchronization. |
| 3020 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3021 | */ |
| 3022 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 3023 | { |
| 3024 | if (!hook->hook || !hook->codec) |
| 3025 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 3026 | /* don't call vmaster hook in the destructor since it might have |
| 3027 | * been already destroyed |
| 3028 | */ |
| 3029 | if (hook->codec->bus->shutdown) |
| 3030 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3031 | switch (hook->mute_mode) { |
| 3032 | case HDA_VMUTE_FOLLOW_MASTER: |
| 3033 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 3034 | break; |
| 3035 | default: |
| 3036 | hook->hook(hook->codec, hook->mute_mode); |
| 3037 | break; |
| 3038 | } |
| 3039 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3040 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3041 | |
| 3042 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3043 | /** |
| 3044 | * 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] | 3045 | * @kcontrol: referred ctl element |
| 3046 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3047 | * |
| 3048 | * The control element is supposed to have the private_value field |
| 3049 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3050 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3051 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 3052 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | { |
| 3054 | int chs = get_amp_channels(kcontrol); |
| 3055 | |
| 3056 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 3057 | uinfo->count = chs == 3 ? 2 : 1; |
| 3058 | uinfo->value.integer.min = 0; |
| 3059 | uinfo->value.integer.max = 1; |
| 3060 | return 0; |
| 3061 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3062 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3064 | /** |
| 3065 | * 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] | 3066 | * @kcontrol: ctl element |
| 3067 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3068 | * |
| 3069 | * The control element is supposed to have the private_value field |
| 3070 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3071 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3072 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 3073 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | { |
| 3075 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3076 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3077 | int chs = get_amp_channels(kcontrol); |
| 3078 | int dir = get_amp_direction(kcontrol); |
| 3079 | int idx = get_amp_index(kcontrol); |
| 3080 | long *valp = ucontrol->value.integer.value; |
| 3081 | |
| 3082 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3084 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3086 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3087 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | return 0; |
| 3089 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3090 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3092 | /** |
| 3093 | * 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] | 3094 | * @kcontrol: ctl element |
| 3095 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3096 | * |
| 3097 | * The control element is supposed to have the private_value field |
| 3098 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3099 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3100 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3101 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | { |
| 3103 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3104 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3105 | int chs = get_amp_channels(kcontrol); |
| 3106 | int dir = get_amp_direction(kcontrol); |
| 3107 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | long *valp = ucontrol->value.integer.value; |
| 3109 | int change = 0; |
| 3110 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3111 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 3112 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 3113 | HDA_AMP_MUTE, |
| 3114 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 3115 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3116 | valp++; |
| 3117 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3118 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame^] | 3119 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 3120 | HDA_AMP_MUTE, |
| 3121 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 3122 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3123 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | return change; |
| 3125 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3126 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | |
| 3128 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3129 | * bound volume controls |
| 3130 | * |
| 3131 | * bind multiple volumes (# indices, from 0) |
| 3132 | */ |
| 3133 | |
| 3134 | #define AMP_VAL_IDX_SHIFT 19 |
| 3135 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3136 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3137 | /** |
| 3138 | * 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] | 3139 | * @kcontrol: ctl element |
| 3140 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3141 | * |
| 3142 | * The control element is supposed to have the private_value field |
| 3143 | * set up via HDA_BIND_MUTE*() macros. |
| 3144 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3145 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3146 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3147 | { |
| 3148 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3149 | unsigned long pval; |
| 3150 | int err; |
| 3151 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3152 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3153 | pval = kcontrol->private_value; |
| 3154 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3155 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3156 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3157 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3158 | return err; |
| 3159 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3160 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3161 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3162 | /** |
| 3163 | * 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] | 3164 | * @kcontrol: ctl element |
| 3165 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3166 | * |
| 3167 | * The control element is supposed to have the private_value field |
| 3168 | * set up via HDA_BIND_MUTE*() macros. |
| 3169 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3170 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3171 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3172 | { |
| 3173 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3174 | unsigned long pval; |
| 3175 | int i, indices, err = 0, change = 0; |
| 3176 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3177 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3178 | pval = kcontrol->private_value; |
| 3179 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3180 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3181 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3182 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3183 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3184 | if (err < 0) |
| 3185 | break; |
| 3186 | change |= err; |
| 3187 | } |
| 3188 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3189 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3190 | return err < 0 ? err : change; |
| 3191 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3192 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3193 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3194 | /** |
| 3195 | * 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] | 3196 | * @kcontrol: referred ctl element |
| 3197 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3198 | * |
| 3199 | * The control element is supposed to have the private_value field |
| 3200 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3201 | */ |
| 3202 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3203 | struct snd_ctl_elem_info *uinfo) |
| 3204 | { |
| 3205 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3206 | struct hda_bind_ctls *c; |
| 3207 | int err; |
| 3208 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3209 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3210 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3211 | kcontrol->private_value = *c->values; |
| 3212 | err = c->ops->info(kcontrol, uinfo); |
| 3213 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3214 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3215 | return err; |
| 3216 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3217 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3218 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3219 | /** |
| 3220 | * 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] | 3221 | * @kcontrol: ctl element |
| 3222 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3223 | * |
| 3224 | * The control element is supposed to have the private_value field |
| 3225 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3226 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3227 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3228 | struct snd_ctl_elem_value *ucontrol) |
| 3229 | { |
| 3230 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3231 | struct hda_bind_ctls *c; |
| 3232 | int err; |
| 3233 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3234 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3235 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3236 | kcontrol->private_value = *c->values; |
| 3237 | err = c->ops->get(kcontrol, ucontrol); |
| 3238 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3239 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3240 | return err; |
| 3241 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3242 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3243 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3244 | /** |
| 3245 | * 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] | 3246 | * @kcontrol: ctl element |
| 3247 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3248 | * |
| 3249 | * The control element is supposed to have the private_value field |
| 3250 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3251 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3252 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3253 | struct snd_ctl_elem_value *ucontrol) |
| 3254 | { |
| 3255 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3256 | struct hda_bind_ctls *c; |
| 3257 | unsigned long *vals; |
| 3258 | int err = 0, change = 0; |
| 3259 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3260 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3261 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3262 | for (vals = c->values; *vals; vals++) { |
| 3263 | kcontrol->private_value = *vals; |
| 3264 | err = c->ops->put(kcontrol, ucontrol); |
| 3265 | if (err < 0) |
| 3266 | break; |
| 3267 | change |= err; |
| 3268 | } |
| 3269 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3270 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3271 | return err < 0 ? err : change; |
| 3272 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3273 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3274 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3275 | /** |
| 3276 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3277 | * @kcontrol: ctl element |
| 3278 | * @op_flag: operation flag |
| 3279 | * @size: byte size of input TLV |
| 3280 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3281 | * |
| 3282 | * The control element is supposed to have the private_value field |
| 3283 | * set up via HDA_BIND_VOL() macro. |
| 3284 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3285 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3286 | unsigned int size, unsigned int __user *tlv) |
| 3287 | { |
| 3288 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3289 | struct hda_bind_ctls *c; |
| 3290 | int err; |
| 3291 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3292 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3293 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3294 | kcontrol->private_value = *c->values; |
| 3295 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3296 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3297 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3298 | return err; |
| 3299 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3300 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3301 | |
| 3302 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3303 | .info = snd_hda_mixer_amp_volume_info, |
| 3304 | .get = snd_hda_mixer_amp_volume_get, |
| 3305 | .put = snd_hda_mixer_amp_volume_put, |
| 3306 | .tlv = snd_hda_mixer_amp_tlv |
| 3307 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3308 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3309 | |
| 3310 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3311 | .info = snd_hda_mixer_amp_switch_info, |
| 3312 | .get = snd_hda_mixer_amp_switch_get, |
| 3313 | .put = snd_hda_mixer_amp_switch_put, |
| 3314 | .tlv = snd_hda_mixer_amp_tlv |
| 3315 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3316 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3317 | |
| 3318 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | * SPDIF out controls |
| 3320 | */ |
| 3321 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3322 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3323 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | { |
| 3325 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3326 | uinfo->count = 1; |
| 3327 | return 0; |
| 3328 | } |
| 3329 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3330 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3331 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | { |
| 3333 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3334 | IEC958_AES0_NONAUDIO | |
| 3335 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3336 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3337 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3338 | IEC958_AES1_CON_ORIGINAL; |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3342 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3343 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | { |
| 3345 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3346 | IEC958_AES0_NONAUDIO | |
| 3347 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3348 | return 0; |
| 3349 | } |
| 3350 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3351 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3352 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | { |
| 3354 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3355 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3356 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3358 | mutex_lock(&codec->spdif_mutex); |
| 3359 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3360 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3361 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3362 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3363 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3364 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | |
| 3366 | return 0; |
| 3367 | } |
| 3368 | |
| 3369 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3370 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3371 | */ |
| 3372 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3373 | { |
| 3374 | unsigned short val = 0; |
| 3375 | |
| 3376 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3377 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3379 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3381 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3382 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3383 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3385 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3386 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3387 | val |= AC_DIG1_EMPHASIS; |
| 3388 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3389 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3391 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3393 | } |
| 3394 | return val; |
| 3395 | } |
| 3396 | |
| 3397 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3398 | */ |
| 3399 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3400 | { |
| 3401 | unsigned int sbits = 0; |
| 3402 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3403 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3405 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3407 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3408 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3410 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3411 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3413 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3415 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3417 | sbits |= val & (0x7f << 8); |
| 3418 | } |
| 3419 | return sbits; |
| 3420 | } |
| 3421 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3422 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3423 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3424 | int verb, int val) |
| 3425 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3426 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3427 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3428 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3429 | d = codec->slave_dig_outs; |
| 3430 | if (!d) |
| 3431 | return; |
| 3432 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3433 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3437 | int dig1, int dig2) |
| 3438 | { |
| 3439 | if (dig1 != -1) |
| 3440 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3441 | if (dig2 != -1) |
| 3442 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3443 | } |
| 3444 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3445 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3446 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | { |
| 3448 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3449 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3450 | struct hda_spdif_out *spdif; |
| 3451 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | unsigned short val; |
| 3453 | int change; |
| 3454 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3455 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3456 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3457 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3458 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3460 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3461 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3462 | val = convert_from_spdif_status(spdif->status); |
| 3463 | val |= spdif->ctls & 1; |
| 3464 | change = spdif->ctls != val; |
| 3465 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3466 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3467 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3468 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | return change; |
| 3470 | } |
| 3471 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3472 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3474 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3475 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | { |
| 3477 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3478 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3479 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3481 | mutex_lock(&codec->spdif_mutex); |
| 3482 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3483 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3484 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | return 0; |
| 3486 | } |
| 3487 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3488 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3489 | int dig1, int dig2) |
| 3490 | { |
| 3491 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3492 | /* unmute amp switch (if any) */ |
| 3493 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3494 | (dig1 & AC_DIG1_ENABLE)) |
| 3495 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3496 | HDA_AMP_MUTE, 0); |
| 3497 | } |
| 3498 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3499 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3500 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | { |
| 3502 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3503 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3504 | struct hda_spdif_out *spdif; |
| 3505 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | unsigned short val; |
| 3507 | int change; |
| 3508 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3509 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3510 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3511 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3512 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3514 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3515 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3516 | spdif->ctls = val; |
| 3517 | if (change && nid != (u16)-1) |
| 3518 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3519 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | return change; |
| 3521 | } |
| 3522 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3523 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | { |
| 3525 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3526 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3527 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | .info = snd_hda_spdif_mask_info, |
| 3529 | .get = snd_hda_spdif_cmask_get, |
| 3530 | }, |
| 3531 | { |
| 3532 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3533 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3534 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | .info = snd_hda_spdif_mask_info, |
| 3536 | .get = snd_hda_spdif_pmask_get, |
| 3537 | }, |
| 3538 | { |
| 3539 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3540 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | .info = snd_hda_spdif_mask_info, |
| 3542 | .get = snd_hda_spdif_default_get, |
| 3543 | .put = snd_hda_spdif_default_put, |
| 3544 | }, |
| 3545 | { |
| 3546 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3547 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | .info = snd_hda_spdif_out_switch_info, |
| 3549 | .get = snd_hda_spdif_out_switch_get, |
| 3550 | .put = snd_hda_spdif_out_switch_put, |
| 3551 | }, |
| 3552 | { } /* end */ |
| 3553 | }; |
| 3554 | |
| 3555 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3556 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3558 | * @associated_nid: NID that new ctls associated with |
| 3559 | * @cvt_nid: converter NID |
| 3560 | * @type: HDA_PCM_TYPE_* |
| 3561 | * Creates controls related with the digital output. |
| 3562 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | * |
| 3564 | * Returns 0 if successful, or a negative error code. |
| 3565 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3566 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3567 | hda_nid_t associated_nid, |
| 3568 | hda_nid_t cvt_nid, |
| 3569 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | { |
| 3571 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3572 | struct snd_kcontrol *kctl; |
| 3573 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3574 | int idx = 0; |
| 3575 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3576 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3577 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3579 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3580 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3581 | idx = spdif_index; |
| 3582 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3583 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3584 | /* suppose a single SPDIF device */ |
| 3585 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3586 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3587 | if (!kctl) |
| 3588 | break; |
| 3589 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3590 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3591 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3592 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3593 | if (!bus->primary_dig_out_type) |
| 3594 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3595 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3596 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3597 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3598 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3599 | return -EBUSY; |
| 3600 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3601 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3602 | if (!spdif) |
| 3603 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3605 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3606 | if (!kctl) |
| 3607 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3608 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3609 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3610 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3611 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | return err; |
| 3613 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3614 | spdif->nid = cvt_nid; |
| 3615 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3616 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3617 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | return 0; |
| 3619 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3620 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3622 | /** |
| 3623 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3624 | * @codec: the HDA codec |
| 3625 | * @nid: widget NID |
| 3626 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3627 | * call within spdif_mutex lock |
| 3628 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3629 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3630 | hda_nid_t nid) |
| 3631 | { |
| 3632 | int i; |
| 3633 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3634 | struct hda_spdif_out *spdif = |
| 3635 | snd_array_elem(&codec->spdif_out, i); |
| 3636 | if (spdif->nid == nid) |
| 3637 | return spdif; |
| 3638 | } |
| 3639 | return NULL; |
| 3640 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3641 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3642 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3643 | /** |
| 3644 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3645 | * @codec: the HDA codec |
| 3646 | * @idx: the SPDIF ctl index |
| 3647 | * |
| 3648 | * Unassign the widget from the given SPDIF control. |
| 3649 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3650 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3651 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3652 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3653 | |
| 3654 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3655 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3656 | spdif->nid = (u16)-1; |
| 3657 | mutex_unlock(&codec->spdif_mutex); |
| 3658 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3659 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3660 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3661 | /** |
| 3662 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3663 | * @codec: the HDA codec |
| 3664 | * @idx: the SPDIF ctl idx |
| 3665 | * @nid: widget NID |
| 3666 | * |
| 3667 | * Assign the widget to the SPDIF control with the given index. |
| 3668 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3669 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3670 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3671 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3672 | unsigned short val; |
| 3673 | |
| 3674 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3675 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3676 | if (spdif->nid != nid) { |
| 3677 | spdif->nid = nid; |
| 3678 | val = spdif->ctls; |
| 3679 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3680 | } |
| 3681 | mutex_unlock(&codec->spdif_mutex); |
| 3682 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3683 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3686 | * SPDIF sharing with analog output |
| 3687 | */ |
| 3688 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3689 | struct snd_ctl_elem_value *ucontrol) |
| 3690 | { |
| 3691 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3692 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3693 | return 0; |
| 3694 | } |
| 3695 | |
| 3696 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3697 | struct snd_ctl_elem_value *ucontrol) |
| 3698 | { |
| 3699 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3700 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
| 3704 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3705 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3706 | .name = "IEC958 Default PCM Playback Switch", |
| 3707 | .info = snd_ctl_boolean_mono_info, |
| 3708 | .get = spdif_share_sw_get, |
| 3709 | .put = spdif_share_sw_put, |
| 3710 | }; |
| 3711 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3712 | /** |
| 3713 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3714 | * @codec: the HDA codec |
| 3715 | * @mout: multi-out instance |
| 3716 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3717 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3718 | struct hda_multi_out *mout) |
| 3719 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3720 | struct snd_kcontrol *kctl; |
| 3721 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3722 | if (!mout->dig_out_nid) |
| 3723 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3724 | |
| 3725 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3726 | if (!kctl) |
| 3727 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3728 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3729 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3730 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3731 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3732 | |
| 3733 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | * SPDIF input |
| 3735 | */ |
| 3736 | |
| 3737 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3738 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3739 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3740 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | { |
| 3742 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3743 | |
| 3744 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3745 | return 0; |
| 3746 | } |
| 3747 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3748 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3749 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | { |
| 3751 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3752 | hda_nid_t nid = kcontrol->private_value; |
| 3753 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3754 | int change; |
| 3755 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3756 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3758 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3760 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3761 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3763 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | return change; |
| 3765 | } |
| 3766 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3767 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3768 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | { |
| 3770 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3771 | hda_nid_t nid = kcontrol->private_value; |
| 3772 | unsigned short val; |
| 3773 | unsigned int sbits; |
| 3774 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3775 | 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] | 3776 | sbits = convert_to_spdif_status(val); |
| 3777 | ucontrol->value.iec958.status[0] = sbits; |
| 3778 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3779 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3780 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3781 | return 0; |
| 3782 | } |
| 3783 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3784 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | { |
| 3786 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3787 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | .info = snd_hda_spdif_in_switch_info, |
| 3789 | .get = snd_hda_spdif_in_switch_get, |
| 3790 | .put = snd_hda_spdif_in_switch_put, |
| 3791 | }, |
| 3792 | { |
| 3793 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3794 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3795 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | .info = snd_hda_spdif_mask_info, |
| 3797 | .get = snd_hda_spdif_in_status_get, |
| 3798 | }, |
| 3799 | { } /* end */ |
| 3800 | }; |
| 3801 | |
| 3802 | /** |
| 3803 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3804 | * @codec: the HDA codec |
| 3805 | * @nid: audio in widget NID |
| 3806 | * |
| 3807 | * Creates controls related with the SPDIF input. |
| 3808 | * Called from each patch supporting the SPDIF in. |
| 3809 | * |
| 3810 | * Returns 0 if successful, or a negative error code. |
| 3811 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3812 | 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] | 3813 | { |
| 3814 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3815 | struct snd_kcontrol *kctl; |
| 3816 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3817 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3819 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3820 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3821 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3822 | return -EBUSY; |
| 3823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3825 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3826 | if (!kctl) |
| 3827 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3829 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3830 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | return err; |
| 3832 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3833 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3834 | snd_hda_codec_read(codec, nid, 0, |
| 3835 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3836 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | return 0; |
| 3838 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3839 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3841 | /* |
| 3842 | * command cache |
| 3843 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3845 | /* 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] | 3846 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3847 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3848 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3849 | |
| 3850 | /** |
| 3851 | * snd_hda_codec_write_cache - send a single command with caching |
| 3852 | * @codec: the HDA codec |
| 3853 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3854 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3855 | * @verb: the verb to send |
| 3856 | * @parm: the parameter for the verb |
| 3857 | * |
| 3858 | * Send a single command without waiting for response. |
| 3859 | * |
| 3860 | * Returns 0 if successful, or a negative error code. |
| 3861 | */ |
| 3862 | 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] | 3863 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3864 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3865 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3866 | struct hda_cache_head *c; |
| 3867 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3868 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3869 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3870 | cache_only = codec->cached_write; |
| 3871 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3872 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3873 | if (err < 0) |
| 3874 | return err; |
| 3875 | } |
| 3876 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3877 | /* parm may contain the verb stuff for get/set amp */ |
| 3878 | verb = verb | (parm >> 8); |
| 3879 | parm &= 0xff; |
| 3880 | key = build_cmd_cache_key(nid, verb); |
| 3881 | mutex_lock(&codec->bus->cmd_mutex); |
| 3882 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3883 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3884 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3885 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3886 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3887 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3888 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3889 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3890 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3891 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3892 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3893 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3894 | * @codec: the HDA codec |
| 3895 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3896 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3897 | * @verb: the verb to send |
| 3898 | * @parm: the parameter for the verb |
| 3899 | * |
| 3900 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3901 | * command if the parameter is already identical with the cached value. |
| 3902 | * If not, it sends the command and refreshes the cache. |
| 3903 | * |
| 3904 | * Returns 0 if successful, or a negative error code. |
| 3905 | */ |
| 3906 | 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] | 3907 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3908 | { |
| 3909 | struct hda_cache_head *c; |
| 3910 | u32 key; |
| 3911 | |
| 3912 | /* parm may contain the verb stuff for get/set amp */ |
| 3913 | verb = verb | (parm >> 8); |
| 3914 | parm &= 0xff; |
| 3915 | key = build_cmd_cache_key(nid, verb); |
| 3916 | mutex_lock(&codec->bus->cmd_mutex); |
| 3917 | c = get_hash(&codec->cmd_cache, key); |
| 3918 | if (c && c->val == parm) { |
| 3919 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3920 | return 0; |
| 3921 | } |
| 3922 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3923 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3924 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3925 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3926 | |
| 3927 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3928 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3929 | * @codec: HD-audio codec |
| 3930 | * |
| 3931 | * Execute all verbs recorded in the command caches to resume. |
| 3932 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3933 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3934 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3935 | int i; |
| 3936 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3937 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3938 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3939 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3940 | struct hda_cache_head *buffer; |
| 3941 | u32 key; |
| 3942 | |
| 3943 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3944 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3945 | if (!key) |
| 3946 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3947 | if (!buffer->dirty) |
| 3948 | continue; |
| 3949 | buffer->dirty = 0; |
| 3950 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3951 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3952 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3953 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3954 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3955 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3956 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3957 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3958 | |
| 3959 | /** |
| 3960 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3961 | * @codec: the HDA codec |
| 3962 | * @seq: VERB array to send |
| 3963 | * |
| 3964 | * Send the commands sequentially from the given array. |
| 3965 | * Thte commands are recorded on cache for power-save and resume. |
| 3966 | * The array must be terminated with NID=0. |
| 3967 | */ |
| 3968 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3969 | const struct hda_verb *seq) |
| 3970 | { |
| 3971 | for (; seq->nid; seq++) |
| 3972 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3973 | seq->param); |
| 3974 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3975 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3976 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3977 | /** |
| 3978 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3979 | * @codec: HD-audio codec |
| 3980 | */ |
| 3981 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3982 | { |
| 3983 | snd_hda_codec_resume_amp(codec); |
| 3984 | snd_hda_codec_resume_cache(codec); |
| 3985 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3986 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3987 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3988 | /** |
| 3989 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3990 | * @codec: the HDA codec |
| 3991 | * @fg: function group (not used now) |
| 3992 | * @power_state: the power state to set (AC_PWRST_*) |
| 3993 | * |
| 3994 | * Set the given power state to all widgets that have the power control. |
| 3995 | * If the codec has power_filter set, it evaluates the power state and |
| 3996 | * filter out if it's unchanged as D3. |
| 3997 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3998 | 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] | 3999 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4000 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4001 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4002 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4003 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4004 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 4005 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4006 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4007 | if (!(wcaps & AC_WCAP_POWER)) |
| 4008 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4009 | if (codec->power_filter) { |
| 4010 | state = codec->power_filter(codec, nid, power_state); |
| 4011 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4012 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 4013 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4014 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4015 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4016 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4017 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4018 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4019 | |
| 4020 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4021 | * supported power states check |
| 4022 | */ |
| 4023 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 4024 | unsigned int power_state) |
| 4025 | { |
| 4026 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 4027 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 4028 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4029 | return false; |
| 4030 | if (sup & power_state) |
| 4031 | return true; |
| 4032 | else |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4037 | * wait until the state is reached, returns the current state |
| 4038 | */ |
| 4039 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 4040 | hda_nid_t fg, |
| 4041 | unsigned int power_state) |
| 4042 | { |
| 4043 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 4044 | unsigned int state, actual_state; |
| 4045 | |
| 4046 | for (;;) { |
| 4047 | state = snd_hda_codec_read(codec, fg, 0, |
| 4048 | AC_VERB_GET_POWER_STATE, 0); |
| 4049 | if (state & AC_PWRST_ERROR) |
| 4050 | break; |
| 4051 | actual_state = (state >> 4) & 0x0f; |
| 4052 | if (actual_state == power_state) |
| 4053 | break; |
| 4054 | if (time_after_eq(jiffies, end_time)) |
| 4055 | break; |
| 4056 | /* wait until the codec reachs to the target state */ |
| 4057 | msleep(1); |
| 4058 | } |
| 4059 | return state; |
| 4060 | } |
| 4061 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4062 | /** |
| 4063 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 4064 | * @codec: the HDA codec |
| 4065 | * @nid: widget NID |
| 4066 | * @power_state: power state to evalue |
| 4067 | * |
| 4068 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 4069 | * This can be used a codec power_filter callback. |
| 4070 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4071 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 4072 | hda_nid_t nid, |
| 4073 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4074 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4075 | if (nid == codec->afg || nid == codec->mfg) |
| 4076 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4077 | if (power_state == AC_PWRST_D3 && |
| 4078 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 4079 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 4080 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 4081 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 4082 | if (eapd & 0x02) |
| 4083 | return AC_PWRST_D0; |
| 4084 | } |
| 4085 | return power_state; |
| 4086 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4087 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4088 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4089 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4090 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4091 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4092 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4093 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4094 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4095 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4096 | int count; |
| 4097 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4098 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4099 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4100 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4101 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 4102 | if (codec->depop_delay < 0) |
| 4103 | msleep(codec->epss ? 10 : 100); |
| 4104 | else if (codec->depop_delay > 0) |
| 4105 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4106 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4107 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4108 | |
| 4109 | /* repeat power states setting at most 10 times*/ |
| 4110 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4111 | if (codec->patch_ops.set_power_state) |
| 4112 | codec->patch_ops.set_power_state(codec, fg, |
| 4113 | power_state); |
| 4114 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4115 | state = power_state; |
| 4116 | if (codec->power_filter) |
| 4117 | state = codec->power_filter(codec, fg, state); |
| 4118 | if (state == power_state || power_state != AC_PWRST_D3) |
| 4119 | snd_hda_codec_read(codec, fg, flags, |
| 4120 | AC_VERB_SET_POWER_STATE, |
| 4121 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4122 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4123 | } |
| 4124 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4125 | if (!(state & AC_PWRST_ERROR)) |
| 4126 | break; |
| 4127 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 4128 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4129 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4130 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4131 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4132 | /* sync power states of all widgets; |
| 4133 | * this is called at the end of codec parsing |
| 4134 | */ |
| 4135 | static void sync_power_up_states(struct hda_codec *codec) |
| 4136 | { |
| 4137 | hda_nid_t nid = codec->start_nid; |
| 4138 | int i; |
| 4139 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4140 | /* don't care if no filter is used */ |
| 4141 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4142 | return; |
| 4143 | |
| 4144 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4145 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4146 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4147 | if (!(wcaps & AC_WCAP_POWER)) |
| 4148 | continue; |
| 4149 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4150 | if (target == AC_PWRST_D0) |
| 4151 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4152 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4153 | snd_hda_codec_write(codec, nid, 0, |
| 4154 | AC_VERB_SET_POWER_STATE, target); |
| 4155 | } |
| 4156 | } |
| 4157 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 4158 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4159 | /* execute additional init verbs */ |
| 4160 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4161 | { |
| 4162 | if (codec->init_verbs.list) |
| 4163 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4164 | } |
| 4165 | #else |
| 4166 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4167 | #endif |
| 4168 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4169 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4170 | /* |
| 4171 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4172 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4173 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4174 | 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] | 4175 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4176 | unsigned int state; |
| 4177 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4178 | codec->in_pm = 1; |
| 4179 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4180 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4181 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4182 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4183 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4184 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4185 | if (!in_wq) |
| 4186 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4187 | spin_lock(&codec->power_lock); |
| 4188 | snd_hda_update_power_acct(codec); |
| 4189 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4190 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4191 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4192 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4193 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4194 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4195 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4196 | } |
| 4197 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4198 | /* mark all entries of cmd and amp caches dirty */ |
| 4199 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4200 | { |
| 4201 | int i; |
| 4202 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4203 | struct hda_cache_head *cmd; |
| 4204 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4205 | cmd->dirty = 1; |
| 4206 | } |
| 4207 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4208 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4209 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4210 | amp->head.dirty = 1; |
| 4211 | } |
| 4212 | } |
| 4213 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4214 | /* |
| 4215 | * kick up codec; used both from PM and power-save |
| 4216 | */ |
| 4217 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4218 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4219 | codec->in_pm = 1; |
| 4220 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4221 | hda_mark_cmd_cache_dirty(codec); |
| 4222 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4223 | /* set as if powered on for avoiding re-entering the resume |
| 4224 | * in the resume / power-save sequence |
| 4225 | */ |
| 4226 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4227 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4228 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4229 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4230 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4231 | if (codec->patch_ops.resume) |
| 4232 | codec->patch_ops.resume(codec); |
| 4233 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4234 | if (codec->patch_ops.init) |
| 4235 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4236 | snd_hda_codec_resume_amp(codec); |
| 4237 | snd_hda_codec_resume_cache(codec); |
| 4238 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4239 | |
| 4240 | if (codec->jackpoll_interval) |
| 4241 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4242 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4243 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4244 | |
| 4245 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4246 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4247 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4248 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4249 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | /** |
| 4252 | * snd_hda_build_controls - build mixer controls |
| 4253 | * @bus: the BUS |
| 4254 | * |
| 4255 | * Creates mixer controls for each codec included in the bus. |
| 4256 | * |
| 4257 | * Returns 0 if successful, otherwise a negative error code. |
| 4258 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4259 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4261 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4263 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4264 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4265 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4266 | codec_err(codec, |
| 4267 | "cannot build controls for #%d (error %d)\n", |
| 4268 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4269 | err = snd_hda_codec_reset(codec); |
| 4270 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4271 | codec_err(codec, |
| 4272 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4273 | return err; |
| 4274 | } |
| 4275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4277 | return 0; |
| 4278 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4279 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4281 | /* |
| 4282 | * add standard channel maps if not specified |
| 4283 | */ |
| 4284 | static int add_std_chmaps(struct hda_codec *codec) |
| 4285 | { |
| 4286 | int i, str, err; |
| 4287 | |
| 4288 | for (i = 0; i < codec->num_pcms; i++) { |
| 4289 | for (str = 0; str < 2; str++) { |
| 4290 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4291 | struct hda_pcm_stream *hinfo = |
| 4292 | &codec->pcm_info[i].stream[str]; |
| 4293 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4294 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4295 | |
| 4296 | if (codec->pcm_info[i].own_chmap) |
| 4297 | continue; |
| 4298 | if (!pcm || !hinfo->substreams) |
| 4299 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4300 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4301 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4302 | hinfo->channels_max, |
| 4303 | 0, &chmap); |
| 4304 | if (err < 0) |
| 4305 | return err; |
| 4306 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4307 | } |
| 4308 | } |
| 4309 | return 0; |
| 4310 | } |
| 4311 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4312 | /* default channel maps for 2.1 speakers; |
| 4313 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4314 | */ |
| 4315 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4316 | { .channels = 2, |
| 4317 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4318 | { .channels = 4, |
| 4319 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4320 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4321 | { } |
| 4322 | }; |
| 4323 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4324 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4325 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4326 | { |
| 4327 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4328 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4329 | /* continue to initialize... */ |
| 4330 | if (codec->patch_ops.init) |
| 4331 | err = codec->patch_ops.init(codec); |
| 4332 | if (!err && codec->patch_ops.build_controls) |
| 4333 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4334 | if (err < 0) |
| 4335 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4336 | |
| 4337 | /* we create chmaps here instead of build_pcms */ |
| 4338 | err = add_std_chmaps(codec); |
| 4339 | if (err < 0) |
| 4340 | return err; |
| 4341 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4342 | if (codec->jackpoll_interval) |
| 4343 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4344 | else |
| 4345 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4346 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | return 0; |
| 4348 | } |
| 4349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | /* |
| 4351 | * stream formats |
| 4352 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4353 | struct hda_rate_tbl { |
| 4354 | unsigned int hz; |
| 4355 | unsigned int alsa_bits; |
| 4356 | unsigned int hda_fmt; |
| 4357 | }; |
| 4358 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4359 | /* rate = base * mult / div */ |
| 4360 | #define HDA_RATE(base, mult, div) \ |
| 4361 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4362 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4363 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4364 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4366 | |
| 4367 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4368 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4369 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4370 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4371 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4372 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4373 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4374 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4375 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4376 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4377 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4378 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4379 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4380 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4381 | |
| 4382 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4383 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4384 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4385 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | }; |
| 4387 | |
| 4388 | /** |
| 4389 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4390 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | * @rate: the sample rate |
| 4392 | * @channels: the number of channels |
| 4393 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4394 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4395 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | * |
| 4397 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4398 | * |
| 4399 | * Return zero if invalid. |
| 4400 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4401 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4402 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | unsigned int channels, |
| 4404 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4405 | unsigned int maxbps, |
| 4406 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | { |
| 4408 | int i; |
| 4409 | unsigned int val = 0; |
| 4410 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4411 | for (i = 0; rate_bits[i].hz; i++) |
| 4412 | if (rate_bits[i].hz == rate) { |
| 4413 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | break; |
| 4415 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4416 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4417 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4422 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | return 0; |
| 4424 | } |
| 4425 | val |= channels - 1; |
| 4426 | |
| 4427 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4428 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4429 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4430 | break; |
| 4431 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4432 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4433 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | case 20: |
| 4435 | case 24: |
| 4436 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4437 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4438 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4440 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4442 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | break; |
| 4444 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4445 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4446 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | return 0; |
| 4448 | } |
| 4449 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4450 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4451 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | return val; |
| 4454 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4455 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4457 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4458 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4459 | { |
| 4460 | unsigned int val = 0; |
| 4461 | if (nid != codec->afg && |
| 4462 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4463 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4464 | if (!val || val == -1) |
| 4465 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4466 | if (!val || val == -1) |
| 4467 | return 0; |
| 4468 | return val; |
| 4469 | } |
| 4470 | |
| 4471 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4472 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4473 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4474 | get_pcm_param); |
| 4475 | } |
| 4476 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4477 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4478 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4479 | { |
| 4480 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4481 | if (!streams || streams == -1) |
| 4482 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4483 | if (!streams || streams == -1) |
| 4484 | return 0; |
| 4485 | return streams; |
| 4486 | } |
| 4487 | |
| 4488 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4489 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4490 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4491 | get_stream_param); |
| 4492 | } |
| 4493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | /** |
| 4495 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4496 | * @codec: the HDA codec |
| 4497 | * @nid: NID to query |
| 4498 | * @ratesp: the pointer to store the detected rate bitflags |
| 4499 | * @formatsp: the pointer to store the detected formats |
| 4500 | * @bpsp: the pointer to store the detected format widths |
| 4501 | * |
| 4502 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4503 | * or @bsps argument is ignored. |
| 4504 | * |
| 4505 | * Returns 0 if successful, otherwise a negative error code. |
| 4506 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4507 | 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] | 4508 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4509 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4510 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4512 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4513 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | |
| 4515 | if (ratesp) { |
| 4516 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4517 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4519 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4521 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4522 | codec_err(codec, |
| 4523 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4524 | nid, val, |
| 4525 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4526 | return -EIO; |
| 4527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | *ratesp = rates; |
| 4529 | } |
| 4530 | |
| 4531 | if (formatsp || bpsp) { |
| 4532 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4533 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4535 | streams = query_stream_param(codec, nid); |
| 4536 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | |
| 4539 | bps = 0; |
| 4540 | if (streams & AC_SUPFMT_PCM) { |
| 4541 | if (val & AC_SUPPCM_BITS_8) { |
| 4542 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4543 | bps = 8; |
| 4544 | } |
| 4545 | if (val & AC_SUPPCM_BITS_16) { |
| 4546 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4547 | bps = 16; |
| 4548 | } |
| 4549 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4550 | if (val & AC_SUPPCM_BITS_32) |
| 4551 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4552 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4553 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4554 | if (val & AC_SUPPCM_BITS_24) |
| 4555 | bps = 24; |
| 4556 | else if (val & AC_SUPPCM_BITS_20) |
| 4557 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4558 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4559 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4561 | if (val & AC_SUPPCM_BITS_32) |
| 4562 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | else if (val & AC_SUPPCM_BITS_24) |
| 4564 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4565 | else if (val & AC_SUPPCM_BITS_20) |
| 4566 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | } |
| 4568 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4569 | #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] | 4570 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4572 | if (!bps) |
| 4573 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4574 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4575 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4576 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4577 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | /* temporary hack: we have still no proper support |
| 4579 | * for the direct AC3 stream... |
| 4580 | */ |
| 4581 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4582 | bps = 8; |
| 4583 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4584 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4585 | codec_err(codec, |
| 4586 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4587 | nid, val, |
| 4588 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4589 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4590 | return -EIO; |
| 4591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | if (formatsp) |
| 4593 | *formatsp = formats; |
| 4594 | if (bpsp) |
| 4595 | *bpsp = bps; |
| 4596 | } |
| 4597 | |
| 4598 | return 0; |
| 4599 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4600 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | |
| 4602 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4603 | * snd_hda_is_supported_format - Check the validity of the format |
| 4604 | * @codec: HD-audio codec |
| 4605 | * @nid: NID to check |
| 4606 | * @format: the HD-audio format value to check |
| 4607 | * |
| 4608 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | * |
| 4610 | * Returns 1 if supported, 0 if not. |
| 4611 | */ |
| 4612 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4613 | unsigned int format) |
| 4614 | { |
| 4615 | int i; |
| 4616 | unsigned int val = 0, rate, stream; |
| 4617 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4618 | val = query_pcm_param(codec, nid); |
| 4619 | if (!val) |
| 4620 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | |
| 4622 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4623 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4624 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | if (val & (1 << i)) |
| 4626 | break; |
| 4627 | return 0; |
| 4628 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4629 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | return 0; |
| 4631 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4632 | stream = query_stream_param(codec, nid); |
| 4633 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | return 0; |
| 4635 | |
| 4636 | if (stream & AC_SUPFMT_PCM) { |
| 4637 | switch (format & 0xf0) { |
| 4638 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4639 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | return 0; |
| 4641 | break; |
| 4642 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4643 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | return 0; |
| 4645 | break; |
| 4646 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4647 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | return 0; |
| 4649 | break; |
| 4650 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4651 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | return 0; |
| 4653 | break; |
| 4654 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4655 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | return 0; |
| 4657 | break; |
| 4658 | default: |
| 4659 | return 0; |
| 4660 | } |
| 4661 | } else { |
| 4662 | /* FIXME: check for float32 and AC3? */ |
| 4663 | } |
| 4664 | |
| 4665 | return 1; |
| 4666 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4667 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | |
| 4669 | /* |
| 4670 | * PCM stuff |
| 4671 | */ |
| 4672 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4673 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4674 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | { |
| 4676 | return 0; |
| 4677 | } |
| 4678 | |
| 4679 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4680 | struct hda_codec *codec, |
| 4681 | unsigned int stream_tag, |
| 4682 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4683 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | { |
| 4685 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
| 4689 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4690 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4691 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4693 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | return 0; |
| 4695 | } |
| 4696 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4697 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4698 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4700 | int err; |
| 4701 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4702 | /* query support PCM information from the given NID */ |
| 4703 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4704 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4705 | info->rates ? NULL : &info->rates, |
| 4706 | info->formats ? NULL : &info->formats, |
| 4707 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4708 | if (err < 0) |
| 4709 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | } |
| 4711 | if (info->ops.open == NULL) |
| 4712 | info->ops.open = hda_pcm_default_open_close; |
| 4713 | if (info->ops.close == NULL) |
| 4714 | info->ops.close = hda_pcm_default_open_close; |
| 4715 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4716 | if (snd_BUG_ON(!info->nid)) |
| 4717 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | info->ops.prepare = hda_pcm_default_prepare; |
| 4719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4721 | if (snd_BUG_ON(!info->nid)) |
| 4722 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4724 | } |
| 4725 | return 0; |
| 4726 | } |
| 4727 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4728 | /* |
| 4729 | * codec prepare/cleanup entries |
| 4730 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4731 | /** |
| 4732 | * snd_hda_codec_prepare - Prepare a stream |
| 4733 | * @codec: the HDA codec |
| 4734 | * @hinfo: PCM information |
| 4735 | * @stream: stream tag to assign |
| 4736 | * @format: format id to assign |
| 4737 | * @substream: PCM substream to assign |
| 4738 | * |
| 4739 | * Calls the prepare callback set by the codec with the given arguments. |
| 4740 | * Clean up the inactive streams when successful. |
| 4741 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4742 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4743 | struct hda_pcm_stream *hinfo, |
| 4744 | unsigned int stream, |
| 4745 | unsigned int format, |
| 4746 | struct snd_pcm_substream *substream) |
| 4747 | { |
| 4748 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4749 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4750 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4751 | if (ret >= 0) |
| 4752 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4753 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4754 | return ret; |
| 4755 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4756 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4757 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4758 | /** |
| 4759 | * snd_hda_codec_cleanup - Prepare a stream |
| 4760 | * @codec: the HDA codec |
| 4761 | * @hinfo: PCM information |
| 4762 | * @substream: PCM substream |
| 4763 | * |
| 4764 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4765 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4766 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4767 | struct hda_pcm_stream *hinfo, |
| 4768 | struct snd_pcm_substream *substream) |
| 4769 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4770 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4771 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4772 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4773 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4774 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4775 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4776 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4777 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4778 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4779 | }; |
| 4780 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4781 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4782 | * get the empty PCM device number to assign |
| 4783 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4784 | 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] | 4785 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4786 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4787 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4788 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4789 | */ |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4790 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4791 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4792 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4793 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4794 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4795 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4796 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4797 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4798 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4799 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4800 | return -EINVAL; |
| 4801 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4802 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4803 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4804 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4805 | if (audio_idx[type][i] >= 8) |
| 4806 | break; |
| 4807 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4808 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4809 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4810 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4811 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4812 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4813 | /* non-fixed slots starting from 10 */ |
| 4814 | for (i = 10; i < 32; i++) { |
| 4815 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4816 | return i; |
| 4817 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4818 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4819 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4820 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4821 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4822 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4823 | dev_warn(bus->card->dev, |
| 4824 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4825 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4826 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4830 | * attach a new PCM stream |
| 4831 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4832 | 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] | 4833 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4834 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4835 | struct hda_pcm_stream *info; |
| 4836 | int stream, err; |
| 4837 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4838 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4839 | return -EINVAL; |
| 4840 | for (stream = 0; stream < 2; stream++) { |
| 4841 | info = &pcm->stream[stream]; |
| 4842 | if (info->substreams) { |
| 4843 | err = set_pcm_default_values(codec, info); |
| 4844 | if (err < 0) |
| 4845 | return err; |
| 4846 | } |
| 4847 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4848 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4849 | } |
| 4850 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4851 | /* assign all PCMs of the given codec */ |
| 4852 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4853 | { |
| 4854 | unsigned int pcm; |
| 4855 | int err; |
| 4856 | |
| 4857 | if (!codec->num_pcms) { |
| 4858 | if (!codec->patch_ops.build_pcms) |
| 4859 | return 0; |
| 4860 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4861 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4862 | codec_err(codec, |
| 4863 | "cannot build PCMs for #%d (error %d)\n", |
| 4864 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4865 | err = snd_hda_codec_reset(codec); |
| 4866 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4867 | codec_err(codec, |
| 4868 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4869 | return err; |
| 4870 | } |
| 4871 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4872 | } |
| 4873 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4874 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4875 | int dev; |
| 4876 | |
| 4877 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4878 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4879 | |
| 4880 | if (!cpcm->pcm) { |
| 4881 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4882 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4883 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4884 | cpcm->device = dev; |
| 4885 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4886 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4887 | codec_err(codec, |
| 4888 | "cannot attach PCM stream %d for codec #%d\n", |
| 4889 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4890 | continue; /* no fatal error */ |
| 4891 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | return 0; |
| 4895 | } |
| 4896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | /** |
| 4898 | * snd_hda_build_pcms - build PCM information |
| 4899 | * @bus: the BUS |
| 4900 | * |
| 4901 | * Create PCM information for each codec included in the bus. |
| 4902 | * |
| 4903 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4904 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4905 | * to create its PCM instances. |
| 4906 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4907 | * callback. |
| 4908 | * |
| 4909 | * At least, substreams, channels_min and channels_max must be filled for |
| 4910 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4911 | * When rates and/or formats are zero, the supported values are queried |
| 4912 | * from the given nid. The nid is used also by the default ops.prepare |
| 4913 | * and ops.cleanup callbacks. |
| 4914 | * |
| 4915 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4916 | * ops.close is supposed to be called in the close callback. |
| 4917 | * ops.prepare should be called in the prepare or hw_params callback |
| 4918 | * with the proper parameters for set up. |
| 4919 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4920 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4921 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4923 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4925 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4927 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4928 | int err = snd_hda_codec_build_pcms(codec); |
| 4929 | if (err < 0) |
| 4930 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | } |
| 4932 | return 0; |
| 4933 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4934 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | * snd_hda_add_new_ctls - create controls from the array |
| 4938 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4939 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | * |
| 4941 | * This helper function creates and add new controls in the given array. |
| 4942 | * The array must be terminated with an empty entry as terminator. |
| 4943 | * |
| 4944 | * Returns 0 if successful, or a negative error code. |
| 4945 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4946 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4947 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4949 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | |
| 4951 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4952 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4953 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4954 | if (knew->iface == -1) /* skip this codec private value */ |
| 4955 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4956 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4957 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4958 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4959 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4960 | if (addr > 0) |
| 4961 | kctl->id.device = addr; |
| 4962 | if (idx > 0) |
| 4963 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4964 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4965 | if (!err) |
| 4966 | break; |
| 4967 | /* try first with another device index corresponding to |
| 4968 | * the codec addr; if it still fails (or it's the |
| 4969 | * primary codec), then try another control index |
| 4970 | */ |
| 4971 | if (!addr && codec->addr) |
| 4972 | addr = codec->addr; |
| 4973 | else if (!idx && !knew->index) { |
| 4974 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4975 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4976 | if (idx <= 0) |
| 4977 | return err; |
| 4978 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4979 | return err; |
| 4980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | } |
| 4982 | return 0; |
| 4983 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4984 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4986 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4987 | static void hda_power_work(struct work_struct *work) |
| 4988 | { |
| 4989 | struct hda_codec *codec = |
| 4990 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4991 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4992 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4993 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4994 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4995 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4996 | spin_unlock(&codec->power_lock); |
| 4997 | return; |
| 4998 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4999 | if (!codec->power_on || codec->power_count) { |
| 5000 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5001 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5002 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5003 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5004 | spin_unlock(&codec->power_lock); |
| 5005 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5006 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5007 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 5008 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | static void hda_keep_power_on(struct hda_codec *codec) |
| 5012 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5013 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5014 | codec->power_count++; |
| 5015 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5016 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5017 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5018 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5019 | } |
| 5020 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5021 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5022 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 5023 | { |
| 5024 | unsigned long delta = jiffies - codec->power_jiffies; |
| 5025 | if (codec->power_on) |
| 5026 | codec->power_on_acct += delta; |
| 5027 | else |
| 5028 | codec->power_off_acct += delta; |
| 5029 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5032 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 5033 | * transition to D3 to complete. A pending D3 transition is indicated |
| 5034 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5035 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5036 | 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] | 5037 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5038 | /* Return if power_on or transitioning to power_on, unless currently |
| 5039 | * powering down. */ |
| 5040 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5041 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5042 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5043 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5044 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5045 | cancel_delayed_work_sync(&codec->power_work); |
| 5046 | |
| 5047 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5048 | /* If the power down delayed work was cancelled above before starting, |
| 5049 | * then there is no need to go through power up here. |
| 5050 | */ |
| 5051 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5052 | if (codec->power_transition < 0) |
| 5053 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5054 | return; |
| 5055 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5056 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5057 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5058 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5059 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5060 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5061 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5062 | spin_unlock(&codec->power_lock); |
| 5063 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5064 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5065 | |
| 5066 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5067 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5068 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5069 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5070 | #define power_save(codec) \ |
| 5071 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5072 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5073 | /* Transition to powered down */ |
| 5074 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5075 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5076 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5077 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5078 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5079 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5080 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5081 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5082 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5083 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | /** |
| 5087 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5088 | * @codec: HD-audio codec |
| 5089 | * @delta: the counter delta to change |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5090 | * @d3wait: sync for D3 transition complete |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5091 | * |
| 5092 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5093 | * appropriately. For the power-down, queue to the delayed action. |
| 5094 | * Passing zero to @delta means to synchronize the power state. |
| 5095 | */ |
| 5096 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5097 | { |
| 5098 | spin_lock(&codec->power_lock); |
| 5099 | codec->power_count += delta; |
| 5100 | trace_hda_power_count(codec); |
| 5101 | if (delta > 0) |
| 5102 | __snd_hda_power_up(codec, d3wait); |
| 5103 | else |
| 5104 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5105 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5106 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5107 | EXPORT_SYMBOL_GPL(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5108 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5109 | /** |
| 5110 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5111 | * @codec: HD-audio codec |
| 5112 | * @check: the object containing an AMP list and the status |
| 5113 | * @nid: NID to check / update |
| 5114 | * |
| 5115 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5116 | * check the current AMP status, and update the the power-status according |
| 5117 | * to the mute status. |
| 5118 | * |
| 5119 | * This function is supposed to be set or called from the check_power_status |
| 5120 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5121 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5122 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5123 | struct hda_loopback_check *check, |
| 5124 | hda_nid_t nid) |
| 5125 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5126 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5127 | int ch, v; |
| 5128 | |
| 5129 | if (!check->amplist) |
| 5130 | return 0; |
| 5131 | for (p = check->amplist; p->nid; p++) { |
| 5132 | if (p->nid == nid) |
| 5133 | break; |
| 5134 | } |
| 5135 | if (!p->nid) |
| 5136 | return 0; /* nothing changed */ |
| 5137 | |
| 5138 | for (p = check->amplist; p->nid; p++) { |
| 5139 | for (ch = 0; ch < 2; ch++) { |
| 5140 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5141 | p->idx); |
| 5142 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5143 | if (!check->power_on) { |
| 5144 | check->power_on = 1; |
| 5145 | snd_hda_power_up(codec); |
| 5146 | } |
| 5147 | return 1; |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | if (check->power_on) { |
| 5152 | check->power_on = 0; |
| 5153 | snd_hda_power_down(codec); |
| 5154 | } |
| 5155 | return 0; |
| 5156 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5157 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5158 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5160 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5161 | * Channel mode helper |
| 5162 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5163 | |
| 5164 | /** |
| 5165 | * 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] | 5166 | * @codec: the HDA codec |
| 5167 | * @uinfo: pointer to get/store the data |
| 5168 | * @chmode: channel mode array |
| 5169 | * @num_chmodes: channel mode array size |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5170 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5171 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5172 | struct snd_ctl_elem_info *uinfo, |
| 5173 | const struct hda_channel_mode *chmode, |
| 5174 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5175 | { |
| 5176 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5177 | uinfo->count = 1; |
| 5178 | uinfo->value.enumerated.items = num_chmodes; |
| 5179 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5180 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5181 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5182 | chmode[uinfo->value.enumerated.item].channels); |
| 5183 | return 0; |
| 5184 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5185 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5186 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5187 | /** |
| 5188 | * 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] | 5189 | * @codec: the HDA codec |
| 5190 | * @ucontrol: pointer to get/store the data |
| 5191 | * @chmode: channel mode array |
| 5192 | * @num_chmodes: channel mode array size |
| 5193 | * @max_channels: max number of channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5194 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5195 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5196 | struct snd_ctl_elem_value *ucontrol, |
| 5197 | const struct hda_channel_mode *chmode, |
| 5198 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5199 | int max_channels) |
| 5200 | { |
| 5201 | int i; |
| 5202 | |
| 5203 | for (i = 0; i < num_chmodes; i++) { |
| 5204 | if (max_channels == chmode[i].channels) { |
| 5205 | ucontrol->value.enumerated.item[0] = i; |
| 5206 | break; |
| 5207 | } |
| 5208 | } |
| 5209 | return 0; |
| 5210 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5211 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5212 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5213 | /** |
| 5214 | * 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] | 5215 | * @codec: the HDA codec |
| 5216 | * @ucontrol: pointer to get/store the data |
| 5217 | * @chmode: channel mode array |
| 5218 | * @num_chmodes: channel mode array size |
| 5219 | * @max_channelsp: pointer to store the max channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5220 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5221 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5222 | struct snd_ctl_elem_value *ucontrol, |
| 5223 | const struct hda_channel_mode *chmode, |
| 5224 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5225 | int *max_channelsp) |
| 5226 | { |
| 5227 | unsigned int mode; |
| 5228 | |
| 5229 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5230 | if (mode >= num_chmodes) |
| 5231 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5232 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5233 | return 0; |
| 5234 | /* change the current channel setting */ |
| 5235 | *max_channelsp = chmode[mode].channels; |
| 5236 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5237 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5238 | return 1; |
| 5239 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5240 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | /* |
| 5243 | * input MUX helper |
| 5244 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5245 | |
| 5246 | /** |
| 5247 | * 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] | 5248 | * @imux: imux helper object |
| 5249 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5250 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5251 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5252 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | { |
| 5254 | unsigned int index; |
| 5255 | |
| 5256 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5257 | uinfo->count = 1; |
| 5258 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5259 | if (!imux->num_items) |
| 5260 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | index = uinfo->value.enumerated.item; |
| 5262 | if (index >= imux->num_items) |
| 5263 | index = imux->num_items - 1; |
| 5264 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5265 | return 0; |
| 5266 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5267 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5269 | /** |
| 5270 | * 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] | 5271 | * @codec: the HDA codec |
| 5272 | * @imux: imux helper object |
| 5273 | * @ucontrol: pointer to get/store the data |
| 5274 | * @nid: input mux NID |
| 5275 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5276 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5277 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5278 | const struct hda_input_mux *imux, |
| 5279 | struct snd_ctl_elem_value *ucontrol, |
| 5280 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | unsigned int *cur_val) |
| 5282 | { |
| 5283 | unsigned int idx; |
| 5284 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5285 | if (!imux->num_items) |
| 5286 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | idx = ucontrol->value.enumerated.item[0]; |
| 5288 | if (idx >= imux->num_items) |
| 5289 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5290 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5292 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5293 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | *cur_val = idx; |
| 5295 | return 1; |
| 5296 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5297 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | |
| 5299 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5300 | /** |
| 5301 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 5302 | * @kcontrol: ctl element |
| 5303 | * @uinfo: pointer to get/store the data |
| 5304 | * @num_items: number of enum items |
| 5305 | * @texts: enum item string array |
| 5306 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5307 | * process kcontrol info callback of a simple string enum array |
| 5308 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5309 | */ |
| 5310 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5311 | struct snd_ctl_elem_info *uinfo, |
| 5312 | int num_items, const char * const *texts) |
| 5313 | { |
| 5314 | static const char * const texts_default[] = { |
| 5315 | "Disabled", "Enabled" |
| 5316 | }; |
| 5317 | |
| 5318 | if (!texts || !num_items) { |
| 5319 | num_items = 2; |
| 5320 | texts = texts_default; |
| 5321 | } |
| 5322 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 5323 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5324 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5325 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5326 | |
| 5327 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | * Multi-channel / digital-out PCM helper functions |
| 5329 | */ |
| 5330 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5331 | /* setup SPDIF output stream */ |
| 5332 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5333 | unsigned int stream_tag, unsigned int format) |
| 5334 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5335 | struct hda_spdif_out *spdif; |
| 5336 | unsigned int curr_fmt; |
| 5337 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5338 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5339 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5340 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5341 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5342 | reset = codec->spdif_status_reset && |
| 5343 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5344 | curr_fmt != format; |
| 5345 | |
| 5346 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5347 | updated */ |
| 5348 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5349 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5350 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5351 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5352 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5353 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5354 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5355 | for (d = codec->slave_dig_outs; *d; d++) |
| 5356 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5357 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5358 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5359 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5360 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5361 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5362 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5363 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5364 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5365 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5366 | { |
| 5367 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5368 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5369 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5370 | for (d = codec->slave_dig_outs; *d; d++) |
| 5371 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5372 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5373 | } |
| 5374 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5375 | /** |
| 5376 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5377 | * @bus: HD-audio bus |
| 5378 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5379 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5380 | { |
| 5381 | struct hda_codec *codec; |
| 5382 | |
| 5383 | if (!bus) |
| 5384 | return; |
| 5385 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5386 | if (hda_codec_is_power_on(codec) && |
| 5387 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5388 | codec->patch_ops.reboot_notify(codec); |
| 5389 | } |
| 5390 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5391 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5392 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5393 | /** |
| 5394 | * 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] | 5395 | * @codec: the HDA codec |
| 5396 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5398 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5399 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5401 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5402 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5403 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5404 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5406 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | return 0; |
| 5408 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5409 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5411 | /** |
| 5412 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5413 | * @codec: the HDA codec |
| 5414 | * @mout: hda_multi_out object |
| 5415 | * @stream_tag: stream tag to assign |
| 5416 | * @format: format id to assign |
| 5417 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5418 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5419 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5420 | struct hda_multi_out *mout, |
| 5421 | unsigned int stream_tag, |
| 5422 | unsigned int format, |
| 5423 | struct snd_pcm_substream *substream) |
| 5424 | { |
| 5425 | mutex_lock(&codec->spdif_mutex); |
| 5426 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5427 | mutex_unlock(&codec->spdif_mutex); |
| 5428 | return 0; |
| 5429 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5430 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5431 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5432 | /** |
| 5433 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5434 | * @codec: the HDA codec |
| 5435 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5436 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5437 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5438 | struct hda_multi_out *mout) |
| 5439 | { |
| 5440 | mutex_lock(&codec->spdif_mutex); |
| 5441 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5442 | mutex_unlock(&codec->spdif_mutex); |
| 5443 | return 0; |
| 5444 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5445 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5446 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5447 | /** |
| 5448 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5449 | * @codec: the HDA codec |
| 5450 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5452 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5453 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5455 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5457 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | return 0; |
| 5459 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5460 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5462 | /** |
| 5463 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5464 | * @codec: the HDA codec |
| 5465 | * @mout: hda_multi_out object |
| 5466 | * @substream: PCM substream to assign |
| 5467 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5468 | * |
| 5469 | * Open analog outputs and set up the hw-constraints. |
| 5470 | * If the digital outputs can be opened as slave, open the digital |
| 5471 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5473 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5474 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5475 | struct snd_pcm_substream *substream, |
| 5476 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5478 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5479 | runtime->hw.channels_max = mout->max_channels; |
| 5480 | if (mout->dig_out_nid) { |
| 5481 | if (!mout->analog_rates) { |
| 5482 | mout->analog_rates = hinfo->rates; |
| 5483 | mout->analog_formats = hinfo->formats; |
| 5484 | mout->analog_maxbps = hinfo->maxbps; |
| 5485 | } else { |
| 5486 | runtime->hw.rates = mout->analog_rates; |
| 5487 | runtime->hw.formats = mout->analog_formats; |
| 5488 | hinfo->maxbps = mout->analog_maxbps; |
| 5489 | } |
| 5490 | if (!mout->spdif_rates) { |
| 5491 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5492 | &mout->spdif_rates, |
| 5493 | &mout->spdif_formats, |
| 5494 | &mout->spdif_maxbps); |
| 5495 | } |
| 5496 | mutex_lock(&codec->spdif_mutex); |
| 5497 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5498 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5499 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5500 | runtime->hw.rates &= mout->spdif_rates; |
| 5501 | runtime->hw.formats &= mout->spdif_formats; |
| 5502 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5503 | hinfo->maxbps = mout->spdif_maxbps; |
| 5504 | } else { |
| 5505 | mout->share_spdif = 0; |
| 5506 | /* FIXME: need notify? */ |
| 5507 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5508 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5509 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5512 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5513 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5514 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5516 | /** |
| 5517 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5518 | * @codec: the HDA codec |
| 5519 | * @mout: hda_multi_out object |
| 5520 | * @stream_tag: stream tag to assign |
| 5521 | * @format: format id to assign |
| 5522 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5523 | * |
| 5524 | * Set up the i/o for analog out. |
| 5525 | * 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] | 5526 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5527 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5528 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | unsigned int stream_tag, |
| 5530 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5531 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5533 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5535 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | int i; |
| 5537 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5538 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5539 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5540 | if (mout->dig_out_nid && mout->share_spdif && |
| 5541 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5543 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5544 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5545 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5547 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5548 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | } else { |
| 5550 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5551 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | } |
| 5553 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5554 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | |
| 5556 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5557 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5558 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5559 | if (!mout->no_share_stream && |
| 5560 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5562 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5563 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5564 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5565 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5566 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5567 | snd_hda_codec_setup_stream(codec, |
| 5568 | mout->hp_out_nid[i], |
| 5569 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | /* surrounds */ |
| 5572 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5573 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5574 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5575 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5576 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5577 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5578 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5580 | |
| 5581 | /* extra surrounds */ |
| 5582 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5583 | int ch = 0; |
| 5584 | if (!mout->extra_out_nid[i]) |
| 5585 | break; |
| 5586 | if (chs >= (i + 1) * 2) |
| 5587 | ch = i * 2; |
| 5588 | else if (!mout->no_share_stream) |
| 5589 | break; |
| 5590 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5591 | stream_tag, ch, format); |
| 5592 | } |
| 5593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | return 0; |
| 5595 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5596 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5598 | /** |
| 5599 | * 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] | 5600 | * @codec: the HDA codec |
| 5601 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5603 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5604 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5606 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | int i; |
| 5608 | |
| 5609 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5610 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5612 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5613 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5614 | if (mout->hp_out_nid[i]) |
| 5615 | snd_hda_codec_cleanup_stream(codec, |
| 5616 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5617 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5618 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5619 | snd_hda_codec_cleanup_stream(codec, |
| 5620 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5621 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | 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] | 5623 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | mout->dig_out_used = 0; |
| 5625 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5626 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | return 0; |
| 5628 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5629 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5631 | /** |
| 5632 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5633 | * @codec: the HDA codec |
| 5634 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5635 | * |
| 5636 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5637 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5638 | */ |
| 5639 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5640 | { |
| 5641 | unsigned int pincap; |
| 5642 | unsigned int oldval; |
| 5643 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5644 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5645 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5646 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5647 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5648 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5649 | return AC_PINCTL_VREF_80; |
| 5650 | else if (pincap & AC_PINCAP_VREF_50) |
| 5651 | return AC_PINCTL_VREF_50; |
| 5652 | else if (pincap & AC_PINCAP_VREF_100) |
| 5653 | return AC_PINCTL_VREF_100; |
| 5654 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5655 | return AC_PINCTL_VREF_GRD; |
| 5656 | return AC_PINCTL_VREF_HIZ; |
| 5657 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5658 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5659 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5660 | /** |
| 5661 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5662 | * @codec: the HDA codec |
| 5663 | * @pin: referred pin NID |
| 5664 | * @val: pin ctl value to audit |
| 5665 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5666 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5667 | hda_nid_t pin, unsigned int val) |
| 5668 | { |
| 5669 | static unsigned int cap_lists[][2] = { |
| 5670 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5671 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5672 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5673 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5674 | }; |
| 5675 | unsigned int cap; |
| 5676 | |
| 5677 | if (!val) |
| 5678 | return 0; |
| 5679 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5680 | if (!cap) |
| 5681 | return val; /* don't know what to do... */ |
| 5682 | |
| 5683 | if (val & AC_PINCTL_OUT_EN) { |
| 5684 | if (!(cap & AC_PINCAP_OUT)) |
| 5685 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5686 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5687 | val &= ~AC_PINCTL_HP_EN; |
| 5688 | } |
| 5689 | |
| 5690 | if (val & AC_PINCTL_IN_EN) { |
| 5691 | if (!(cap & AC_PINCAP_IN)) |
| 5692 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5693 | else { |
| 5694 | unsigned int vcap, vref; |
| 5695 | int i; |
| 5696 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5697 | vref = val & AC_PINCTL_VREFEN; |
| 5698 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5699 | if (vref == cap_lists[i][0] && |
| 5700 | !(vcap & cap_lists[i][1])) { |
| 5701 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5702 | vref = AC_PINCTL_VREF_HIZ; |
| 5703 | else |
| 5704 | vref = cap_lists[i + 1][0]; |
| 5705 | } |
| 5706 | } |
| 5707 | val &= ~AC_PINCTL_VREFEN; |
| 5708 | val |= vref; |
| 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | return val; |
| 5713 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5714 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5715 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5716 | /** |
| 5717 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5718 | * @codec: the HDA codec |
| 5719 | * @pin: referred pin NID |
| 5720 | * @val: pin control value to set |
| 5721 | * @cached: access over codec pinctl cache or direct write |
| 5722 | * |
| 5723 | * This function is a helper to set a pin ctl value more safely. |
| 5724 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5725 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5726 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5727 | * snd_hda_codec_write() depending on @cached flag. |
| 5728 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5729 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5730 | unsigned int val, bool cached) |
| 5731 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5732 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5733 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5734 | if (cached) |
| 5735 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5736 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5737 | else |
| 5738 | return snd_hda_codec_write(codec, pin, 0, |
| 5739 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5740 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5741 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5742 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5743 | /** |
| 5744 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5745 | * @codec: the HDA codec |
| 5746 | * @imux: imux helper object |
| 5747 | * @label: the name of imux item to assign |
| 5748 | * @index: index number of imux item to assign |
| 5749 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5750 | * |
| 5751 | * When the same label is used already in the existing items, the number |
| 5752 | * suffix is appended to the label. This label index number is stored |
| 5753 | * to type_idx when non-NULL pointer is given. |
| 5754 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5755 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5756 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5757 | int index, int *type_idx) |
| 5758 | { |
| 5759 | int i, label_idx = 0; |
| 5760 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5761 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5762 | return -EINVAL; |
| 5763 | } |
| 5764 | for (i = 0; i < imux->num_items; i++) { |
| 5765 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5766 | label_idx++; |
| 5767 | } |
| 5768 | if (type_idx) |
| 5769 | *type_idx = label_idx; |
| 5770 | if (label_idx > 0) |
| 5771 | snprintf(imux->items[imux->num_items].label, |
| 5772 | sizeof(imux->items[imux->num_items].label), |
| 5773 | "%s %d", label, label_idx); |
| 5774 | else |
| 5775 | strlcpy(imux->items[imux->num_items].label, label, |
| 5776 | sizeof(imux->items[imux->num_items].label)); |
| 5777 | imux->items[imux->num_items].index = index; |
| 5778 | imux->num_items++; |
| 5779 | return 0; |
| 5780 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5781 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5782 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | #ifdef CONFIG_PM |
| 5785 | /* |
| 5786 | * power management |
| 5787 | */ |
| 5788 | |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5789 | |
| 5790 | static void hda_async_suspend(void *data, async_cookie_t cookie) |
| 5791 | { |
| 5792 | hda_call_codec_suspend(data, false); |
| 5793 | } |
| 5794 | |
| 5795 | static void hda_async_resume(void *data, async_cookie_t cookie) |
| 5796 | { |
| 5797 | hda_call_codec_resume(data); |
| 5798 | } |
| 5799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | /** |
| 5801 | * snd_hda_suspend - suspend the codecs |
| 5802 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | * |
| 5804 | * Returns 0 if successful. |
| 5805 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5806 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5808 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5809 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5811 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5812 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5813 | if (hda_codec_is_power_on(codec)) { |
| 5814 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5815 | async_schedule_domain(hda_async_suspend, codec, |
| 5816 | &domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5817 | else |
| 5818 | hda_call_codec_suspend(codec, false); |
| 5819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5821 | |
| 5822 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5823 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | return 0; |
| 5826 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5827 | EXPORT_SYMBOL_GPL(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | |
| 5829 | /** |
| 5830 | * snd_hda_resume - resume the codecs |
| 5831 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | * |
| 5833 | * Returns 0 if successful. |
| 5834 | */ |
| 5835 | int snd_hda_resume(struct hda_bus *bus) |
| 5836 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5837 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5838 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5840 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5841 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5842 | async_schedule_domain(hda_async_resume, codec, &domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5843 | else |
| 5844 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5846 | |
| 5847 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5848 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | return 0; |
| 5851 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5852 | EXPORT_SYMBOL_GPL(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5853 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5854 | |
| 5855 | /* |
| 5856 | * generic arrays |
| 5857 | */ |
| 5858 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5859 | /** |
| 5860 | * snd_array_new - get a new element from the given array |
| 5861 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5862 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5863 | * Get a new element from the given array. If it exceeds the |
| 5864 | * pre-allocated array size, re-allocate the array. |
| 5865 | * |
| 5866 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5867 | */ |
| 5868 | void *snd_array_new(struct snd_array *array) |
| 5869 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5870 | if (snd_BUG_ON(!array->elem_size)) |
| 5871 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5872 | if (array->used >= array->alloced) { |
| 5873 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5874 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5875 | void *nlist; |
| 5876 | if (snd_BUG_ON(num >= 4096)) |
| 5877 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5878 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5879 | if (!nlist) |
| 5880 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5881 | array->list = nlist; |
| 5882 | array->alloced = num; |
| 5883 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5884 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5885 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5886 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5887 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5888 | /** |
| 5889 | * snd_array_free - free the given array elements |
| 5890 | * @array: the array object |
| 5891 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5892 | void snd_array_free(struct snd_array *array) |
| 5893 | { |
| 5894 | kfree(array->list); |
| 5895 | array->used = 0; |
| 5896 | array->alloced = 0; |
| 5897 | array->list = NULL; |
| 5898 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5899 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5900 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5901 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5902 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5903 | * @pcm: PCM caps bits |
| 5904 | * @buf: the string buffer to write |
| 5905 | * @buflen: the max buffer length |
| 5906 | * |
| 5907 | * used by hda_proc.c and hda_eld.c |
| 5908 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5909 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5910 | { |
| 5911 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5912 | int i, j; |
| 5913 | |
| 5914 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5915 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5916 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5917 | |
| 5918 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5919 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5920 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5921 | |
| 5922 | MODULE_DESCRIPTION("HDA codec core"); |
| 5923 | MODULE_LICENSE("GPL"); |