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 | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 765 | unsol = bus->unsol; |
| 766 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return 0; |
| 768 | |
| 769 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 770 | unsol->wp = wp; |
| 771 | |
| 772 | wp <<= 1; |
| 773 | unsol->queue[wp] = res; |
| 774 | unsol->queue[wp + 1] = res_ex; |
| 775 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 776 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
| 778 | return 0; |
| 779 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 780 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 783 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 785 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 787 | struct hda_bus_unsolicited *unsol = |
| 788 | container_of(work, struct hda_bus_unsolicited, work); |
| 789 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | struct hda_codec *codec; |
| 791 | unsigned int rp, caddr, res; |
| 792 | |
| 793 | while (unsol->rp != unsol->wp) { |
| 794 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 795 | unsol->rp = rp; |
| 796 | rp <<= 1; |
| 797 | res = unsol->queue[rp]; |
| 798 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 799 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | continue; |
| 801 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 802 | if (codec && codec->patch_ops.unsol_event) |
| 803 | codec->patch_ops.unsol_event(codec, res); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * initialize unsolicited queue |
| 809 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 810 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
| 812 | struct hda_bus_unsolicited *unsol; |
| 813 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 814 | if (bus->unsol) /* already initialized */ |
| 815 | return 0; |
| 816 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 817 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 818 | if (!unsol) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 819 | dev_err(bus->card->dev, "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | return -ENOMEM; |
| 821 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 822 | INIT_WORK(&unsol->work, process_unsol_events); |
| 823 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | bus->unsol = unsol; |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * destructor |
| 830 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 831 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 833 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 834 | return; |
| 835 | |
| 836 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 837 | if (bus->workq) |
| 838 | flush_workqueue(bus->workq); |
Markus Elfring | f0acd28 | 2014-11-17 10:44:33 +0100 | [diff] [blame] | 839 | kfree(bus->unsol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | if (bus->ops.private_free) |
| 841 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 842 | if (bus->workq) |
| 843 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 848 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 850 | snd_hda_bus_free(device->device_data); |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 855 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 857 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 858 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
| 862 | * snd_hda_bus_new - create a HDA bus |
| 863 | * @card: the card entry |
| 864 | * @temp: the template for hda_bus information |
| 865 | * @busp: the pointer to store the created bus instance |
| 866 | * |
| 867 | * Returns 0 if successful, or a negative error code. |
| 868 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 869 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 870 | const struct hda_bus_template *temp, |
| 871 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | struct hda_bus *bus; |
| 874 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 875 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 876 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | .dev_free = snd_hda_bus_dev_free, |
| 878 | }; |
| 879 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 880 | if (snd_BUG_ON(!temp)) |
| 881 | return -EINVAL; |
| 882 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 883 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | if (busp) |
| 886 | *busp = NULL; |
| 887 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 888 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 890 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return -ENOMEM; |
| 892 | } |
| 893 | |
| 894 | bus->card = card; |
| 895 | bus->private_data = temp->private_data; |
| 896 | bus->pci = temp->pci; |
| 897 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 898 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | bus->ops = temp->ops; |
| 900 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 901 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 902 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | INIT_LIST_HEAD(&bus->codec_list); |
| 904 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 905 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 906 | "hd-audio%d", card->number); |
| 907 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 908 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 909 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 910 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 911 | kfree(bus); |
| 912 | return -ENOMEM; |
| 913 | } |
| 914 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 915 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 916 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | snd_hda_bus_free(bus); |
| 918 | return err; |
| 919 | } |
| 920 | if (busp) |
| 921 | *busp = bus; |
| 922 | return 0; |
| 923 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 924 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 926 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 927 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 928 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 929 | #else |
| 930 | #define is_generic_config(codec) 0 |
| 931 | #endif |
| 932 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 933 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 934 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 935 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 936 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 937 | #endif |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | /* |
| 940 | * find a matching codec preset |
| 941 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 942 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 943 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 945 | struct hda_codec_preset_list *tbl; |
| 946 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 947 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 949 | again: |
| 950 | mutex_lock(&preset_mutex); |
| 951 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 952 | if (!try_module_get(tbl->owner)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 953 | codec_err(codec, "cannot module_get\n"); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 954 | continue; |
| 955 | } |
| 956 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 958 | if (preset->afg && preset->afg != codec->afg) |
| 959 | continue; |
| 960 | if (preset->mfg && preset->mfg != codec->mfg) |
| 961 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 962 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 964 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 965 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 966 | preset->rev == codec->revision_id)) { |
| 967 | mutex_unlock(&preset_mutex); |
| 968 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 972 | module_put(tbl->owner); |
| 973 | } |
| 974 | mutex_unlock(&preset_mutex); |
| 975 | |
| 976 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 977 | if (!mod_requested) |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 978 | request_module("snd-hda-codec-id:%08x", |
| 979 | codec->vendor_id); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 980 | else |
Takashi Iwai | 66797f3 | 2014-10-27 15:15:44 +0100 | [diff] [blame] | 981 | request_module("snd-hda-codec-id:%04x*", |
| 982 | (codec->vendor_id >> 16) & 0xffff); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 983 | mod_requested++; |
| 984 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 990 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 992 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | { |
| 994 | const struct hda_vendor_id *c; |
| 995 | const char *vendor = NULL; |
| 996 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 997 | char tmp[16]; |
| 998 | |
| 999 | if (codec->vendor_name) |
| 1000 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | for (c = hda_vendor_ids; c->id; c++) { |
| 1003 | if (c->id == vendor_id) { |
| 1004 | vendor = c->name; |
| 1005 | break; |
| 1006 | } |
| 1007 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1008 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1010 | vendor = tmp; |
| 1011 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1012 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1013 | if (!codec->vendor_name) |
| 1014 | return -ENOMEM; |
| 1015 | |
| 1016 | get_chip_name: |
| 1017 | if (codec->chip_name) |
| 1018 | return 0; |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1021 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1022 | else { |
| 1023 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1024 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1025 | } |
| 1026 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1027 | return -ENOMEM; |
| 1028 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1032 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1034 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1036 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | hda_nid_t nid; |
| 1038 | |
| 1039 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1040 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1041 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1042 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1043 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1044 | case AC_GRP_AUDIO_FUNCTION: |
| 1045 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1046 | codec->afg_function_id = function_id & 0xff; |
| 1047 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1048 | break; |
| 1049 | case AC_GRP_MODEM_FUNCTION: |
| 1050 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1051 | codec->mfg_function_id = function_id & 0xff; |
| 1052 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1053 | break; |
| 1054 | default: |
| 1055 | break; |
| 1056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1061 | * read widget caps for each widget and store in cache |
| 1062 | */ |
| 1063 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1064 | { |
| 1065 | int i; |
| 1066 | hda_nid_t nid; |
| 1067 | |
| 1068 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1069 | &codec->start_nid); |
| 1070 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1071 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1072 | return -ENOMEM; |
| 1073 | nid = codec->start_nid; |
| 1074 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1075 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1076 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1080 | /* read all pin default configurations and save codec->init_pins */ |
| 1081 | static int read_pin_defaults(struct hda_codec *codec) |
| 1082 | { |
| 1083 | int i; |
| 1084 | hda_nid_t nid = codec->start_nid; |
| 1085 | |
| 1086 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1087 | struct hda_pincfg *pin; |
| 1088 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1089 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1090 | if (wid_type != AC_WID_PIN) |
| 1091 | continue; |
| 1092 | pin = snd_array_new(&codec->init_pins); |
| 1093 | if (!pin) |
| 1094 | return -ENOMEM; |
| 1095 | pin->nid = nid; |
| 1096 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1097 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1098 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1099 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1100 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1101 | } |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | /* look up the given pin config list and return the item matching with NID */ |
| 1106 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1107 | struct snd_array *array, |
| 1108 | hda_nid_t nid) |
| 1109 | { |
| 1110 | int i; |
| 1111 | for (i = 0; i < array->used; i++) { |
| 1112 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1113 | if (pin->nid == nid) |
| 1114 | return pin; |
| 1115 | } |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1119 | /* set the current pin config value for the given NID. |
| 1120 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1121 | */ |
| 1122 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1123 | hda_nid_t nid, unsigned int cfg) |
| 1124 | { |
| 1125 | struct hda_pincfg *pin; |
| 1126 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1127 | /* the check below may be invalid when pins are added by a fixup |
| 1128 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1129 | * for now |
| 1130 | */ |
| 1131 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1132 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1133 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1134 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1135 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1136 | pin = look_up_pincfg(codec, list, nid); |
| 1137 | if (!pin) { |
| 1138 | pin = snd_array_new(list); |
| 1139 | if (!pin) |
| 1140 | return -ENOMEM; |
| 1141 | pin->nid = nid; |
| 1142 | } |
| 1143 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1144 | return 0; |
| 1145 | } |
| 1146 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1147 | /** |
| 1148 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1149 | * @codec: the HDA codec |
| 1150 | * @nid: NID to set the pin config |
| 1151 | * @cfg: the pin default config value |
| 1152 | * |
| 1153 | * Override a pin default configuration value in the cache. |
| 1154 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1155 | * priority than the real hardware value. |
| 1156 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1157 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1158 | hda_nid_t nid, unsigned int cfg) |
| 1159 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1160 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1161 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1162 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1163 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1164 | /** |
| 1165 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1166 | * @codec: the HDA codec |
| 1167 | * @nid: NID to get the pin config |
| 1168 | * |
| 1169 | * Get the current pin config value of the given pin NID. |
| 1170 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1171 | * returns the cached value. |
| 1172 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1173 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1174 | { |
| 1175 | struct hda_pincfg *pin; |
| 1176 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1177 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1178 | { |
| 1179 | unsigned int cfg = 0; |
| 1180 | mutex_lock(&codec->user_mutex); |
| 1181 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1182 | if (pin) |
| 1183 | cfg = pin->cfg; |
| 1184 | mutex_unlock(&codec->user_mutex); |
| 1185 | if (cfg) |
| 1186 | return cfg; |
| 1187 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1188 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1189 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1190 | if (pin) |
| 1191 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1192 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1193 | if (pin) |
| 1194 | return pin->cfg; |
| 1195 | return 0; |
| 1196 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1197 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1198 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1199 | /** |
| 1200 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 1201 | * @codec: the HDA codec |
| 1202 | * @nid: pin NID |
| 1203 | * @val: assigned pinctl value |
| 1204 | * |
| 1205 | * This function stores the given value to a pinctl target value in the |
| 1206 | * pincfg table. This isn't always as same as the actually written value |
| 1207 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 1208 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1209 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1210 | unsigned int val) |
| 1211 | { |
| 1212 | struct hda_pincfg *pin; |
| 1213 | |
| 1214 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1215 | if (!pin) |
| 1216 | return -EINVAL; |
| 1217 | pin->target = val; |
| 1218 | return 0; |
| 1219 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1220 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1221 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1222 | /** |
| 1223 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1224 | * @codec: the HDA codec |
| 1225 | * @nid: pin NID |
| 1226 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1227 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1228 | { |
| 1229 | struct hda_pincfg *pin; |
| 1230 | |
| 1231 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1232 | if (!pin) |
| 1233 | return 0; |
| 1234 | return pin->target; |
| 1235 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1236 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1237 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1238 | /** |
| 1239 | * snd_hda_shutup_pins - Shut up all pins |
| 1240 | * @codec: the HDA codec |
| 1241 | * |
| 1242 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1243 | * The controls aren't cached so that they can be resumed properly. |
| 1244 | */ |
| 1245 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1246 | { |
| 1247 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1248 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1249 | * the default pin setup at the next load of the driver |
| 1250 | */ |
| 1251 | if (codec->bus->shutdown) |
| 1252 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1253 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1254 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1255 | /* use read here for syncing after issuing each verb */ |
| 1256 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1257 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1258 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1259 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1260 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1261 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1262 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1263 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1264 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1265 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1266 | { |
| 1267 | int i; |
| 1268 | if (!codec->pins_shutup) |
| 1269 | return; |
| 1270 | if (codec->bus->shutdown) |
| 1271 | return; |
| 1272 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1273 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1274 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1275 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1276 | pin->ctrl); |
| 1277 | } |
| 1278 | codec->pins_shutup = 0; |
| 1279 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1280 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1281 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1282 | static void hda_jackpoll_work(struct work_struct *work) |
| 1283 | { |
| 1284 | struct hda_codec *codec = |
| 1285 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1286 | |
| 1287 | snd_hda_jack_set_dirty_all(codec); |
| 1288 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1289 | |
| 1290 | if (!codec->jackpoll_interval) |
| 1291 | return; |
| 1292 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1293 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1294 | codec->jackpoll_interval); |
| 1295 | } |
| 1296 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1297 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1298 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1299 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1300 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1301 | /* release all pincfg lists */ |
| 1302 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1303 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1304 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1305 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1306 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1307 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1308 | snd_array_free(&codec->init_pins); |
| 1309 | } |
| 1310 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1311 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1312 | * audio-converter setup caches |
| 1313 | */ |
| 1314 | struct hda_cvt_setup { |
| 1315 | hda_nid_t nid; |
| 1316 | u8 stream_tag; |
| 1317 | u8 channel_id; |
| 1318 | u16 format_id; |
| 1319 | unsigned char active; /* cvt is currently used */ |
| 1320 | unsigned char dirty; /* setups should be cleared */ |
| 1321 | }; |
| 1322 | |
| 1323 | /* get or create a cache entry for the given audio converter NID */ |
| 1324 | static struct hda_cvt_setup * |
| 1325 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1326 | { |
| 1327 | struct hda_cvt_setup *p; |
| 1328 | int i; |
| 1329 | |
| 1330 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1331 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1332 | if (p->nid == nid) |
| 1333 | return p; |
| 1334 | } |
| 1335 | p = snd_array_new(&codec->cvt_setups); |
| 1336 | if (p) |
| 1337 | p->nid = nid; |
| 1338 | return p; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1342 | * Dynamic symbol binding for the codec parsers |
| 1343 | */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1344 | |
| 1345 | #define load_parser(codec, sym) \ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1346 | ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1347 | |
| 1348 | static void unload_parser(struct hda_codec *codec) |
| 1349 | { |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1350 | if (codec->parser) |
| 1351 | symbol_put_addr(codec->parser); |
| 1352 | codec->parser = NULL; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | * codec destructor |
| 1357 | */ |
| 1358 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1359 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1360 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1362 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1363 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1364 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1365 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1366 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1367 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1368 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1370 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1371 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1372 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1373 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1374 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1376 | if (codec->patch_ops.free) |
| 1377 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1378 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1379 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1380 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1381 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1382 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1383 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1384 | kfree(codec->vendor_name); |
| 1385 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1386 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1387 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1388 | codec->bus->num_codecs--; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1389 | put_device(&codec->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1392 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1393 | hda_nid_t fg, unsigned int power_state); |
| 1394 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1395 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1396 | unsigned int power_state); |
| 1397 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1398 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1399 | { |
| 1400 | struct hda_codec *codec = device->device_data; |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1401 | int err = device_add(&codec->dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1402 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1403 | if (err < 0) |
| 1404 | return err; |
| 1405 | snd_hda_register_beep_device(codec); |
| 1406 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1410 | { |
| 1411 | struct hda_codec *codec = device->device_data; |
| 1412 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1413 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1414 | device_del(&codec->dev); |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1418 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1419 | { |
| 1420 | snd_hda_codec_free(device->device_data); |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1424 | /* just free the container */ |
| 1425 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1426 | { |
| 1427 | kfree(container_of(dev, struct hda_codec, dev)); |
| 1428 | } |
| 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | /** |
| 1431 | * snd_hda_codec_new - create a HDA codec |
| 1432 | * @bus: the bus to assign |
| 1433 | * @codec_addr: the codec address |
| 1434 | * @codecp: the pointer to store the generated codec |
| 1435 | * |
| 1436 | * Returns 0 if successful, or a negative error code. |
| 1437 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1438 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1439 | unsigned int codec_addr, |
| 1440 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
| 1442 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1443 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1444 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1446 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1447 | .dev_register = snd_hda_codec_dev_register, |
| 1448 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1449 | .dev_free = snd_hda_codec_dev_free, |
| 1450 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1452 | if (snd_BUG_ON(!bus)) |
| 1453 | return -EINVAL; |
| 1454 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1455 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
| 1457 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1458 | dev_err(bus->card->dev, |
| 1459 | "address 0x%x is already occupied\n", |
| 1460 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | return -EBUSY; |
| 1462 | } |
| 1463 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1464 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1466 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | return -ENOMEM; |
| 1468 | } |
| 1469 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1470 | device_initialize(&codec->dev); |
| 1471 | codec->dev.parent = &bus->card->card_dev; |
| 1472 | codec->dev.class = sound_class; |
| 1473 | codec->dev.release = snd_hda_codec_dev_release; |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1474 | codec->dev.groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1475 | dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number, |
| 1476 | codec_addr); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1477 | dev_set_drvdata(&codec->dev, codec); /* for sysfs */ |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | codec->bus = bus; |
| 1480 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1481 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1482 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1483 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1484 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1485 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1486 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1487 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1488 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1489 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1490 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1491 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1492 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1493 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1494 | INIT_LIST_HEAD(&codec->conn_list); |
| 1495 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1496 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1497 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1498 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1500 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1501 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1502 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1503 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1504 | * the caller has to power down appropriatley after initialization |
| 1505 | * phase. |
| 1506 | */ |
| 1507 | hda_keep_power_on(codec); |
| 1508 | #endif |
| 1509 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1510 | snd_hda_sysfs_init(codec); |
| 1511 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1512 | if (codec->bus->modelname) { |
| 1513 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1514 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1515 | err = -ENODEV; |
| 1516 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1521 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | bus->caddr_tbl[codec_addr] = codec; |
| 1524 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1525 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1526 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1527 | if (codec->vendor_id == -1) |
| 1528 | /* read again, hopefully the access method was corrected |
| 1529 | * in the last read... |
| 1530 | */ |
| 1531 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1532 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1533 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1534 | AC_PAR_SUBSYSTEM_ID); |
| 1535 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1536 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1538 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1539 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1540 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1541 | err = -ENODEV; |
| 1542 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1545 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1546 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1547 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1548 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1549 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1550 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1551 | err = read_pin_defaults(codec); |
| 1552 | if (err < 0) |
| 1553 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1554 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1555 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1556 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1557 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1558 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1561 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1562 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1563 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1564 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1565 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1566 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1567 | #ifdef CONFIG_PM |
| 1568 | if (!codec->d3_stop_clk || !codec->epss) |
| 1569 | bus->power_keep_link_on = 1; |
| 1570 | #endif |
| 1571 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1572 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1573 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1574 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1575 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1576 | snd_hda_codec_proc_new(codec); |
| 1577 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1578 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1579 | |
| 1580 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1581 | codec->subsystem_id, codec->revision_id); |
| 1582 | snd_component_add(codec->bus->card, component); |
| 1583 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1584 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1585 | if (err < 0) |
| 1586 | goto error; |
| 1587 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1588 | if (codecp) |
| 1589 | *codecp = codec; |
| 1590 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1591 | |
| 1592 | error: |
| 1593 | snd_hda_codec_free(codec); |
| 1594 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1595 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1596 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1597 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1598 | /** |
| 1599 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1600 | * @codec: the HDA codec |
| 1601 | * |
| 1602 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1603 | * the given codec. |
| 1604 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1605 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1606 | { |
| 1607 | hda_nid_t fg; |
| 1608 | int err; |
| 1609 | |
| 1610 | /* Assume the function group node does not change, |
| 1611 | * only the widget nodes may change. |
| 1612 | */ |
| 1613 | kfree(codec->wcaps); |
| 1614 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1615 | err = read_widget_caps(codec, fg); |
| 1616 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1617 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1618 | return err; |
| 1619 | } |
| 1620 | |
| 1621 | snd_array_free(&codec->init_pins); |
| 1622 | err = read_pin_defaults(codec); |
| 1623 | |
| 1624 | return err; |
| 1625 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1626 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1627 | |
| 1628 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 1629 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1630 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1631 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1632 | { |
| 1633 | hda_nid_t nid = codec->start_nid; |
| 1634 | int i; |
| 1635 | |
| 1636 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1637 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1638 | switch (get_wcaps_type(wcaps)) { |
| 1639 | case AC_WID_AUD_IN: |
| 1640 | return false; /* HDMI parser supports only HDMI out */ |
| 1641 | case AC_WID_AUD_OUT: |
| 1642 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1643 | return false; |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | return true; |
| 1648 | } |
| 1649 | #else |
| 1650 | /* no HDMI codec parser support */ |
| 1651 | #define is_likely_hdmi_codec(codec) false |
| 1652 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1653 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1654 | /** |
| 1655 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1656 | * @codec: the HDA codec |
| 1657 | * |
| 1658 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1659 | * patch instance. |
| 1660 | * |
| 1661 | * Returns 0 if successful or a negative error code. |
| 1662 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1663 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1664 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1665 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1666 | int err; |
| 1667 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1668 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1669 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1670 | err = get_codec_name(codec); |
| 1671 | if (err < 0) |
| 1672 | return err; |
| 1673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1675 | if (!is_generic_config(codec) && codec->preset) |
| 1676 | patch = codec->preset->patch; |
| 1677 | if (!patch) { |
| 1678 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1679 | if (is_likely_hdmi_codec(codec)) { |
| 1680 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1681 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1682 | #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) |
| 1683 | patch = snd_hda_parse_hdmi_codec; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1684 | #endif |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1685 | } |
| 1686 | if (!patch) { |
| 1687 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 1688 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1689 | #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) |
| 1690 | patch = snd_hda_parse_generic_codec; |
| 1691 | #endif |
| 1692 | } |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1693 | if (!patch) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1694 | codec_err(codec, "No codec parser is available\n"); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1695 | return -ENODEV; |
| 1696 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1699 | err = patch(codec); |
| 1700 | if (err < 0) { |
| 1701 | unload_parser(codec); |
| 1702 | return err; |
| 1703 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1704 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1705 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1706 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1707 | if (err < 0) |
| 1708 | return err; |
| 1709 | } |
| 1710 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1711 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1712 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1713 | snprintf(codec->bus->card->mixername, |
| 1714 | sizeof(codec->bus->card->mixername), |
| 1715 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1716 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1718 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1720 | /* update the stream-id if changed */ |
| 1721 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1722 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1723 | u32 stream_tag, int channel_id) |
| 1724 | { |
| 1725 | unsigned int oldval, newval; |
| 1726 | |
| 1727 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1728 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1729 | newval = (stream_tag << 4) | channel_id; |
| 1730 | if (oldval != newval) |
| 1731 | snd_hda_codec_write(codec, nid, 0, |
| 1732 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1733 | newval); |
| 1734 | p->stream_tag = stream_tag; |
| 1735 | p->channel_id = channel_id; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* update the format-id if changed */ |
| 1740 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1741 | hda_nid_t nid, int format) |
| 1742 | { |
| 1743 | unsigned int oldval; |
| 1744 | |
| 1745 | if (p->format_id != format) { |
| 1746 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1747 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1748 | if (oldval != format) { |
| 1749 | msleep(1); |
| 1750 | snd_hda_codec_write(codec, nid, 0, |
| 1751 | AC_VERB_SET_STREAM_FORMAT, |
| 1752 | format); |
| 1753 | } |
| 1754 | p->format_id = format; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | /** |
| 1759 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1760 | * @codec: the CODEC to set up |
| 1761 | * @nid: the NID to set up |
| 1762 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1763 | * @channel_id: channel id to pass, zero based. |
| 1764 | * @format: stream format. |
| 1765 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1766 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1767 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | int channel_id, int format) |
| 1769 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1770 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1771 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1772 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1773 | int i; |
| 1774 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1775 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1776 | return; |
| 1777 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1778 | codec_dbg(codec, |
| 1779 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1780 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1781 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1782 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1783 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1784 | |
| 1785 | if (codec->pcm_format_first) |
| 1786 | update_pcm_format(codec, p, nid, format); |
| 1787 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1788 | if (!codec->pcm_format_first) |
| 1789 | update_pcm_format(codec, p, nid, format); |
| 1790 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1791 | p->active = 1; |
| 1792 | p->dirty = 0; |
| 1793 | |
| 1794 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1795 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1796 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1797 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1798 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1799 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1800 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1801 | p->dirty = 1; |
| 1802 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1805 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1807 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1808 | struct hda_cvt_setup *q); |
| 1809 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1810 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1811 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1812 | * @codec: the CODEC to clean up |
| 1813 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1814 | * @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] | 1815 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1816 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1817 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1818 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1819 | struct hda_cvt_setup *p; |
| 1820 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1821 | if (!nid) |
| 1822 | return; |
| 1823 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1824 | if (codec->no_sticky_stream) |
| 1825 | do_now = 1; |
| 1826 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1827 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1828 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1829 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1830 | /* here we just clear the active flag when do_now isn't set; |
| 1831 | * actual clean-ups will be done later in |
| 1832 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1833 | */ |
| 1834 | if (do_now) |
| 1835 | really_cleanup_stream(codec, p); |
| 1836 | else |
| 1837 | p->active = 0; |
| 1838 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1839 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1840 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1841 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1842 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1843 | struct hda_cvt_setup *q) |
| 1844 | { |
| 1845 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1846 | if (q->stream_tag || q->channel_id) |
| 1847 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1848 | if (q->format_id) |
| 1849 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1850 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1851 | memset(q, 0, sizeof(*q)); |
| 1852 | q->nid = nid; |
| 1853 | } |
| 1854 | |
| 1855 | /* clean up the all conflicting obsolete streams */ |
| 1856 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1857 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1858 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1859 | int i; |
| 1860 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1861 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1862 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1863 | struct hda_cvt_setup *p; |
| 1864 | p = snd_array_elem(&c->cvt_setups, i); |
| 1865 | if (p->dirty) |
| 1866 | really_cleanup_stream(c, p); |
| 1867 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1871 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1872 | /* clean up all streams; called from suspend */ |
| 1873 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1874 | { |
| 1875 | int i; |
| 1876 | |
| 1877 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1878 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1879 | if (p->stream_tag) |
| 1880 | really_cleanup_stream(codec, p); |
| 1881 | } |
| 1882 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1883 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | /* |
| 1886 | * amp access functions |
| 1887 | */ |
| 1888 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1889 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1890 | #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] | 1891 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1892 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1893 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1895 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | |
| 1897 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1898 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1899 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1901 | memset(cache, 0, sizeof(*cache)); |
| 1902 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1903 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1906 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1907 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1908 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1912 | 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] | 1913 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1914 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1915 | u16 cur = cache->hash[idx]; |
| 1916 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
| 1918 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1919 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | if (info->key == key) |
| 1921 | return info; |
| 1922 | cur = info->next; |
| 1923 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1924 | return NULL; |
| 1925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1927 | /* query the hash. allocate an entry if not found. */ |
| 1928 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1929 | u32 key) |
| 1930 | { |
| 1931 | struct hda_cache_head *info = get_hash(cache, key); |
| 1932 | if (!info) { |
| 1933 | u16 idx, cur; |
| 1934 | /* add a new hash entry */ |
| 1935 | info = snd_array_new(&cache->buf); |
| 1936 | if (!info) |
| 1937 | return NULL; |
| 1938 | cur = snd_array_index(&cache->buf, info); |
| 1939 | info->key = key; |
| 1940 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1941 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1942 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1943 | info->next = cache->hash[idx]; |
| 1944 | cache->hash[idx] = cur; |
| 1945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | return info; |
| 1947 | } |
| 1948 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1949 | /* query and allocate an amp hash entry */ |
| 1950 | static inline struct hda_amp_info * |
| 1951 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1952 | { |
| 1953 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1954 | } |
| 1955 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1956 | /* overwrite the value with the key in the caps hash */ |
| 1957 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1958 | { |
| 1959 | struct hda_amp_info *info; |
| 1960 | |
| 1961 | mutex_lock(&codec->hash_mutex); |
| 1962 | info = get_alloc_amp_hash(codec, key); |
| 1963 | if (!info) { |
| 1964 | mutex_unlock(&codec->hash_mutex); |
| 1965 | return -EINVAL; |
| 1966 | } |
| 1967 | info->amp_caps = val; |
| 1968 | info->head.val |= INFO_AMP_CAPS; |
| 1969 | mutex_unlock(&codec->hash_mutex); |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | /* query the value from the caps hash; if not found, fetch the current |
| 1974 | * value from the given function and store in the hash |
| 1975 | */ |
| 1976 | static unsigned int |
| 1977 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1978 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1979 | { |
| 1980 | struct hda_amp_info *info; |
| 1981 | unsigned int val; |
| 1982 | |
| 1983 | mutex_lock(&codec->hash_mutex); |
| 1984 | info = get_alloc_amp_hash(codec, key); |
| 1985 | if (!info) { |
| 1986 | mutex_unlock(&codec->hash_mutex); |
| 1987 | return 0; |
| 1988 | } |
| 1989 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1990 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1991 | val = func(codec, nid, dir); |
| 1992 | write_caps_hash(codec, key, val); |
| 1993 | } else { |
| 1994 | val = info->amp_caps; |
| 1995 | mutex_unlock(&codec->hash_mutex); |
| 1996 | } |
| 1997 | return val; |
| 1998 | } |
| 1999 | |
| 2000 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2001 | int direction) |
| 2002 | { |
| 2003 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 2004 | nid = codec->afg; |
| 2005 | return snd_hda_param_read(codec, nid, |
| 2006 | direction == HDA_OUTPUT ? |
| 2007 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 2008 | } |
| 2009 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2010 | /** |
| 2011 | * query_amp_caps - query AMP capabilities |
| 2012 | * @codec: the HD-auio codec |
| 2013 | * @nid: the NID to query |
| 2014 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 2015 | * |
| 2016 | * Query AMP capabilities for the given widget and direction. |
| 2017 | * Returns the obtained capability bits. |
| 2018 | * |
| 2019 | * When cap bits have been already read, this doesn't read again but |
| 2020 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 2022 | 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] | 2023 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2024 | return query_caps_hash(codec, nid, direction, |
| 2025 | HDA_HASH_KEY(nid, direction, 0), |
| 2026 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2028 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2030 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 2031 | * snd_hda_check_amp_caps - query AMP capabilities |
| 2032 | * @codec: the HD-audio codec |
| 2033 | * @nid: the NID to query |
| 2034 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2035 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 2036 | * |
| 2037 | * Check whether the widget has the given amp capability for the direction. |
| 2038 | */ |
| 2039 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2040 | int dir, unsigned int bits) |
| 2041 | { |
| 2042 | if (!nid) |
| 2043 | return false; |
| 2044 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 2045 | if (query_amp_caps(codec, nid, dir) & bits) |
| 2046 | return true; |
| 2047 | return false; |
| 2048 | } |
| 2049 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 2050 | |
| 2051 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2052 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 2053 | * @codec: the CODEC to clean up |
| 2054 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2055 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2056 | * @caps: the capability bits to set |
| 2057 | * |
| 2058 | * Override the cached AMP caps bits value by the given one. |
| 2059 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 2060 | * e.g. limit to 0dB, etc. |
| 2061 | * |
| 2062 | * Returns zero if successful or a negative error code. |
| 2063 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2064 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2065 | unsigned int caps) |
| 2066 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2067 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2068 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2069 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2070 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2071 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2072 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2073 | { |
| 2074 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 2075 | } |
| 2076 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2077 | /** |
| 2078 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 2079 | * @codec: the HD-auio codec |
| 2080 | * @nid: the NID to query |
| 2081 | * |
| 2082 | * Query PIN capabilities for the given widget. |
| 2083 | * Returns the obtained capability bits. |
| 2084 | * |
| 2085 | * When cap bits have been already read, this doesn't read again but |
| 2086 | * returns the cached value. |
| 2087 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2088 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2089 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2090 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2091 | read_pin_cap); |
| 2092 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2093 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2094 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2095 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2096 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2097 | * @codec: the CODEC |
| 2098 | * @nid: the NID to override |
| 2099 | * @caps: the capability bits to set |
| 2100 | * |
| 2101 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2102 | * |
| 2103 | * Returns zero if successful or a negative error code. |
| 2104 | */ |
| 2105 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2106 | unsigned int caps) |
| 2107 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2108 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2109 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2110 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2111 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2112 | /* read or sync the hash value with the current value; |
| 2113 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2115 | static struct hda_amp_info * |
| 2116 | 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] | 2117 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2119 | struct hda_amp_info *info; |
| 2120 | unsigned int parm, val = 0; |
| 2121 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2123 | retry: |
| 2124 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2125 | if (!info) |
| 2126 | return NULL; |
| 2127 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2128 | if (!val_read) { |
| 2129 | mutex_unlock(&codec->hash_mutex); |
| 2130 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2131 | parm |= direction == HDA_OUTPUT ? |
| 2132 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2133 | parm |= index; |
| 2134 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2135 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2136 | val &= 0xff; |
| 2137 | val_read = true; |
| 2138 | mutex_lock(&codec->hash_mutex); |
| 2139 | goto retry; |
| 2140 | } |
| 2141 | info->vol[ch] = val; |
| 2142 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2143 | } else if (init_only) |
| 2144 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2145 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2149 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2151 | 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] | 2152 | hda_nid_t nid, int ch, int direction, int index, |
| 2153 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | { |
| 2155 | u32 parm; |
| 2156 | |
| 2157 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2158 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2159 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2160 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2161 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2162 | ; /* set the zero value as a fake mute */ |
| 2163 | else |
| 2164 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2166 | } |
| 2167 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2168 | /** |
| 2169 | * snd_hda_codec_amp_read - Read AMP value |
| 2170 | * @codec: HD-audio codec |
| 2171 | * @nid: NID to read the AMP value |
| 2172 | * @ch: channel (left=0 or right=1) |
| 2173 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2174 | * @index: the index value (only for input direction) |
| 2175 | * |
| 2176 | * 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] | 2177 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2178 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2179 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2181 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2182 | unsigned int val = 0; |
| 2183 | |
| 2184 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2185 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2186 | if (info) |
| 2187 | val = info->vol[ch]; |
| 2188 | mutex_unlock(&codec->hash_mutex); |
| 2189 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2191 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2193 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2194 | int direction, int idx, int mask, int val, |
| 2195 | bool init_only) |
| 2196 | { |
| 2197 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2198 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2199 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2200 | |
| 2201 | if (snd_BUG_ON(mask & ~0xff)) |
| 2202 | mask &= 0xff; |
| 2203 | val &= mask; |
| 2204 | |
| 2205 | mutex_lock(&codec->hash_mutex); |
| 2206 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2207 | if (!info) { |
| 2208 | mutex_unlock(&codec->hash_mutex); |
| 2209 | return 0; |
| 2210 | } |
| 2211 | val |= info->vol[ch] & ~mask; |
| 2212 | if (info->vol[ch] == val) { |
| 2213 | mutex_unlock(&codec->hash_mutex); |
| 2214 | return 0; |
| 2215 | } |
| 2216 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2217 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2218 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2219 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2220 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2221 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2222 | return 1; |
| 2223 | } |
| 2224 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2225 | /** |
| 2226 | * snd_hda_codec_amp_update - update the AMP value |
| 2227 | * @codec: HD-audio codec |
| 2228 | * @nid: NID to read the AMP value |
| 2229 | * @ch: channel (left=0 or right=1) |
| 2230 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2231 | * @idx: the index value (only for input direction) |
| 2232 | * @mask: bit mask to set |
| 2233 | * @val: the bits value to set |
| 2234 | * |
| 2235 | * Update the AMP value with a bit mask. |
| 2236 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2237 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2238 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2239 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2241 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2243 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2245 | /** |
| 2246 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2247 | * @codec: HD-audio codec |
| 2248 | * @nid: NID to read the AMP value |
| 2249 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2250 | * @idx: the index value (only for input direction) |
| 2251 | * @mask: bit mask to set |
| 2252 | * @val: the bits value to set |
| 2253 | * |
| 2254 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2255 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2256 | */ |
| 2257 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2258 | int direction, int idx, int mask, int val) |
| 2259 | { |
| 2260 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2261 | |
| 2262 | if (snd_BUG_ON(mask & ~0xff)) |
| 2263 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2264 | for (ch = 0; ch < 2; ch++) |
| 2265 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2266 | idx, mask, val); |
| 2267 | return ret; |
| 2268 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2269 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2270 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2271 | /** |
| 2272 | * snd_hda_codec_amp_init - initialize the AMP value |
| 2273 | * @codec: the HDA codec |
| 2274 | * @nid: NID to read the AMP value |
| 2275 | * @ch: channel (left=0 or right=1) |
| 2276 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2277 | * @idx: the index value (only for input direction) |
| 2278 | * @mask: bit mask to set |
| 2279 | * @val: the bits value to set |
| 2280 | * |
| 2281 | * 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] | 2282 | * the first access. If the amp was already initialized / updated beforehand, |
| 2283 | * this does nothing. |
| 2284 | */ |
| 2285 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2286 | int dir, int idx, int mask, int val) |
| 2287 | { |
| 2288 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2289 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2290 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2291 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2292 | /** |
| 2293 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 2294 | * @codec: the HDA codec |
| 2295 | * @nid: NID to read the AMP value |
| 2296 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2297 | * @idx: the index value (only for input direction) |
| 2298 | * @mask: bit mask to set |
| 2299 | * @val: the bits value to set |
| 2300 | * |
| 2301 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 2302 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2303 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2304 | int dir, int idx, int mask, int val) |
| 2305 | { |
| 2306 | int ch, ret = 0; |
| 2307 | |
| 2308 | if (snd_BUG_ON(mask & ~0xff)) |
| 2309 | mask &= 0xff; |
| 2310 | for (ch = 0; ch < 2; ch++) |
| 2311 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2312 | idx, mask, val); |
| 2313 | return ret; |
| 2314 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2315 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2316 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2317 | /** |
| 2318 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2319 | * @codec: HD-audio codec |
| 2320 | * |
| 2321 | * Resume the all amp commands from the cache. |
| 2322 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2323 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2324 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2325 | int i; |
| 2326 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2327 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2328 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2329 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2330 | struct hda_amp_info *buffer; |
| 2331 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2332 | hda_nid_t nid; |
| 2333 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2334 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2335 | |
| 2336 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2337 | if (!buffer->head.dirty) |
| 2338 | continue; |
| 2339 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2340 | info = *buffer; |
| 2341 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2342 | if (!key) |
| 2343 | continue; |
| 2344 | nid = key & 0xff; |
| 2345 | idx = (key >> 16) & 0xff; |
| 2346 | dir = (key >> 24) & 0xff; |
| 2347 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2348 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2349 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2350 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2351 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2352 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2353 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2354 | } |
| 2355 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2356 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2357 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2358 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2360 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2361 | unsigned int ofs) |
| 2362 | { |
| 2363 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2364 | /* get num steps */ |
| 2365 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2366 | if (ofs < caps) |
| 2367 | caps -= ofs; |
| 2368 | return caps; |
| 2369 | } |
| 2370 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2371 | /** |
| 2372 | * 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] | 2373 | * @kcontrol: referred ctl element |
| 2374 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2375 | * |
| 2376 | * The control element is supposed to have the private_value field |
| 2377 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2378 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2379 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2380 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | { |
| 2382 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2383 | u16 nid = get_amp_nid(kcontrol); |
| 2384 | u8 chs = get_amp_channels(kcontrol); |
| 2385 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2386 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2388 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2389 | uinfo->count = chs == 3 ? 2 : 1; |
| 2390 | uinfo->value.integer.min = 0; |
| 2391 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2392 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2393 | codec_warn(codec, |
| 2394 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2395 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | return -EINVAL; |
| 2397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | return 0; |
| 2399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2400 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2402 | |
| 2403 | static inline unsigned int |
| 2404 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2405 | int ch, int dir, int idx, unsigned int ofs) |
| 2406 | { |
| 2407 | unsigned int val; |
| 2408 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2409 | val &= HDA_AMP_VOLMASK; |
| 2410 | if (val >= ofs) |
| 2411 | val -= ofs; |
| 2412 | else |
| 2413 | val = 0; |
| 2414 | return val; |
| 2415 | } |
| 2416 | |
| 2417 | static inline int |
| 2418 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2419 | int ch, int dir, int idx, unsigned int ofs, |
| 2420 | unsigned int val) |
| 2421 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2422 | unsigned int maxval; |
| 2423 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2424 | if (val > 0) |
| 2425 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2426 | /* ofs = 0: raw max value */ |
| 2427 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2428 | if (val > maxval) |
| 2429 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2430 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2431 | HDA_AMP_VOLMASK, val); |
| 2432 | } |
| 2433 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2434 | /** |
| 2435 | * 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] | 2436 | * @kcontrol: ctl element |
| 2437 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2438 | * |
| 2439 | * The control element is supposed to have the private_value field |
| 2440 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2441 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2442 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2443 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | { |
| 2445 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2446 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2447 | int chs = get_amp_channels(kcontrol); |
| 2448 | int dir = get_amp_direction(kcontrol); |
| 2449 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2450 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | long *valp = ucontrol->value.integer.value; |
| 2452 | |
| 2453 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2454 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2456 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2459 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2461 | /** |
| 2462 | * 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] | 2463 | * @kcontrol: ctl element |
| 2464 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2465 | * |
| 2466 | * The control element is supposed to have the private_value field |
| 2467 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2468 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2469 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2470 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | { |
| 2472 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2473 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2474 | int chs = get_amp_channels(kcontrol); |
| 2475 | int dir = get_amp_direction(kcontrol); |
| 2476 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2477 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | long *valp = ucontrol->value.integer.value; |
| 2479 | int change = 0; |
| 2480 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2481 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2482 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2483 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2484 | valp++; |
| 2485 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2486 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2487 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2488 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | return change; |
| 2490 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2491 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2493 | /** |
| 2494 | * 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] | 2495 | * @kcontrol: ctl element |
| 2496 | * @op_flag: operation flag |
| 2497 | * @size: byte size of input TLV |
| 2498 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2499 | * |
| 2500 | * The control element is supposed to have the private_value field |
| 2501 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2502 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2503 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2504 | unsigned int size, unsigned int __user *_tlv) |
| 2505 | { |
| 2506 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2507 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2508 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2509 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2510 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2511 | u32 caps, val1, val2; |
| 2512 | |
| 2513 | if (size < 4 * sizeof(unsigned int)) |
| 2514 | return -ENOMEM; |
| 2515 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2516 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2517 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2518 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2519 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2520 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2521 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2522 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2523 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2524 | return -EFAULT; |
| 2525 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2526 | return -EFAULT; |
| 2527 | if (put_user(val1, _tlv + 2)) |
| 2528 | return -EFAULT; |
| 2529 | if (put_user(val2, _tlv + 3)) |
| 2530 | return -EFAULT; |
| 2531 | return 0; |
| 2532 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2533 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2534 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2535 | /** |
| 2536 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2537 | * @codec: HD-audio codec |
| 2538 | * @nid: NID of a reference widget |
| 2539 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2540 | * @tlv: TLV data to be stored, at least 4 elements |
| 2541 | * |
| 2542 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2543 | * obtained from the reference NID. |
| 2544 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2545 | */ |
| 2546 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2547 | unsigned int *tlv) |
| 2548 | { |
| 2549 | u32 caps; |
| 2550 | int nums, step; |
| 2551 | |
| 2552 | caps = query_amp_caps(codec, nid, dir); |
| 2553 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2554 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2555 | step = (step + 1) * 25; |
| 2556 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2557 | tlv[1] = 2 * sizeof(unsigned int); |
| 2558 | tlv[2] = -nums * step; |
| 2559 | tlv[3] = step; |
| 2560 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2561 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2562 | |
| 2563 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2564 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2565 | 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] | 2566 | { |
| 2567 | struct snd_ctl_elem_id id; |
| 2568 | memset(&id, 0, sizeof(id)); |
| 2569 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2570 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2571 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2572 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2573 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2574 | strcpy(id.name, name); |
| 2575 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2576 | } |
| 2577 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2578 | /** |
| 2579 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2580 | * @codec: HD-audio codec |
| 2581 | * @name: ctl id name string |
| 2582 | * |
| 2583 | * Get the control element with the given id string and IFACE_MIXER. |
| 2584 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2585 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2586 | const char *name) |
| 2587 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2588 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2589 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2590 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2591 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2592 | 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] | 2593 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2594 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2595 | int i, idx; |
| 2596 | /* 16 ctlrs should be large enough */ |
| 2597 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2598 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2599 | return idx; |
| 2600 | } |
| 2601 | return -EBUSY; |
| 2602 | } |
| 2603 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2604 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2605 | * 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] | 2606 | * @codec: HD-audio codec |
| 2607 | * @nid: corresponding NID (optional) |
| 2608 | * @kctl: the control element to assign |
| 2609 | * |
| 2610 | * Add the given control element to an array inside the codec instance. |
| 2611 | * All control elements belonging to a codec are supposed to be added |
| 2612 | * by this function so that a proper clean-up works at the free or |
| 2613 | * reconfiguration time. |
| 2614 | * |
| 2615 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2616 | * The assignment is shown in the codec proc file. |
| 2617 | * |
| 2618 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2619 | * #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] | 2620 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2621 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2622 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2623 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2624 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2625 | { |
| 2626 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2627 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2628 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2629 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2630 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2631 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2632 | if (nid == 0) |
| 2633 | nid = get_amp_nid_(kctl->private_value); |
| 2634 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2635 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2636 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2637 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2638 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2639 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2640 | if (err < 0) |
| 2641 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2642 | item = snd_array_new(&codec->mixers); |
| 2643 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2644 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2645 | item->kctl = kctl; |
| 2646 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2647 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2648 | return 0; |
| 2649 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2650 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2651 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2652 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2653 | * snd_hda_add_nid - Assign a NID to a control element |
| 2654 | * @codec: HD-audio codec |
| 2655 | * @nid: corresponding NID (optional) |
| 2656 | * @kctl: the control element to assign |
| 2657 | * @index: index to kctl |
| 2658 | * |
| 2659 | * Add the given control element to an array inside the codec instance. |
| 2660 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2661 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2662 | */ |
| 2663 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2664 | unsigned int index, hda_nid_t nid) |
| 2665 | { |
| 2666 | struct hda_nid_item *item; |
| 2667 | |
| 2668 | if (nid > 0) { |
| 2669 | item = snd_array_new(&codec->nids); |
| 2670 | if (!item) |
| 2671 | return -ENOMEM; |
| 2672 | item->kctl = kctl; |
| 2673 | item->index = index; |
| 2674 | item->nid = nid; |
| 2675 | return 0; |
| 2676 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2677 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2678 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2679 | return -EINVAL; |
| 2680 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2681 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2682 | |
| 2683 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2684 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2685 | * @codec: HD-audio codec |
| 2686 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2687 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2688 | { |
| 2689 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2690 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2691 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2692 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2693 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2694 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2697 | /** |
| 2698 | * snd_hda_lock_devices - pseudo device locking |
| 2699 | * @bus: the BUS |
| 2700 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2701 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2702 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2703 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2704 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2705 | struct snd_card *card = bus->card; |
| 2706 | struct hda_codec *codec; |
| 2707 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2708 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2709 | if (card->shutdown) |
| 2710 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2711 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2712 | if (!list_empty(&card->ctl_files)) |
| 2713 | goto err_clear; |
| 2714 | |
| 2715 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2716 | int pcm; |
| 2717 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2718 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2719 | if (!cpcm->pcm) |
| 2720 | continue; |
| 2721 | if (cpcm->pcm->streams[0].substream_opened || |
| 2722 | cpcm->pcm->streams[1].substream_opened) |
| 2723 | goto err_clear; |
| 2724 | } |
| 2725 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2726 | spin_unlock(&card->files_lock); |
| 2727 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2728 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2729 | err_clear: |
| 2730 | card->shutdown = 0; |
| 2731 | err_unlock: |
| 2732 | spin_unlock(&card->files_lock); |
| 2733 | return -EINVAL; |
| 2734 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2735 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2736 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2737 | /** |
| 2738 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2739 | * @bus: the BUS |
| 2740 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2741 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2742 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2743 | struct snd_card *card = bus->card; |
| 2744 | |
| 2745 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2746 | spin_lock(&card->files_lock); |
| 2747 | card->shutdown = 0; |
| 2748 | spin_unlock(&card->files_lock); |
| 2749 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2750 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2751 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2752 | /** |
| 2753 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2754 | * @codec: HD-audio codec |
| 2755 | * |
| 2756 | * This frees the all PCM and control elements assigned to the codec, and |
| 2757 | * clears the caches and restores the pin default configurations. |
| 2758 | * |
| 2759 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2760 | * returns zero. |
| 2761 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2762 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2763 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2764 | struct hda_bus *bus = codec->bus; |
| 2765 | struct snd_card *card = bus->card; |
| 2766 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2767 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2768 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2769 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2772 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2773 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2774 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2775 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2776 | #endif |
| 2777 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2778 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2779 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2780 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2781 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2782 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2783 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2784 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2785 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2786 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2787 | if (codec->patch_ops.free) |
| 2788 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2789 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2790 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2791 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2792 | codec->spec = NULL; |
| 2793 | free_hda_cache(&codec->amp_cache); |
| 2794 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2795 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2796 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2797 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2798 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2799 | snd_array_free(&codec->cvt_setups); |
| 2800 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2801 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2802 | codec->num_pcms = 0; |
| 2803 | codec->pcm_info = NULL; |
| 2804 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2805 | codec->slave_dig_outs = NULL; |
| 2806 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2807 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2808 | module_put(codec->owner); |
| 2809 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2812 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2813 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2814 | } |
| 2815 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2816 | 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] | 2817 | |
| 2818 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2819 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2820 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2821 | { |
| 2822 | struct hda_nid_item *items; |
| 2823 | const char * const *s; |
| 2824 | int i, err; |
| 2825 | |
| 2826 | items = codec->mixers.list; |
| 2827 | for (i = 0; i < codec->mixers.used; i++) { |
| 2828 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2829 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2830 | continue; |
| 2831 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2832 | char tmpname[sizeof(sctl->id.name)]; |
| 2833 | const char *name = *s; |
| 2834 | if (suffix) { |
| 2835 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2836 | name, suffix); |
| 2837 | name = tmpname; |
| 2838 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2839 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2840 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2841 | if (err) |
| 2842 | return err; |
| 2843 | break; |
| 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2850 | static int check_slave_present(struct hda_codec *codec, |
| 2851 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2852 | { |
| 2853 | return 1; |
| 2854 | } |
| 2855 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2856 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2857 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2858 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2859 | { |
| 2860 | int _tlv[4]; |
| 2861 | const int *tlv = NULL; |
| 2862 | int val = -1; |
| 2863 | |
| 2864 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2865 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2866 | mm_segment_t fs = get_fs(); |
| 2867 | set_fs(get_ds()); |
| 2868 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2869 | tlv = _tlv; |
| 2870 | set_fs(fs); |
| 2871 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2872 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2873 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2874 | int step = tlv[3]; |
| 2875 | step &= ~TLV_DB_SCALE_MUTE; |
| 2876 | if (!step) |
| 2877 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2878 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2879 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2880 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2881 | return -1; |
| 2882 | } |
| 2883 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2884 | val = -tlv[2] / step; |
| 2885 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2886 | return val; |
| 2887 | } |
| 2888 | |
| 2889 | /* call kctl->put with the given value(s) */ |
| 2890 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2891 | { |
| 2892 | struct snd_ctl_elem_value *ucontrol; |
| 2893 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2894 | if (!ucontrol) |
| 2895 | return -ENOMEM; |
| 2896 | ucontrol->value.integer.value[0] = val; |
| 2897 | ucontrol->value.integer.value[1] = val; |
| 2898 | kctl->put(kctl, ucontrol); |
| 2899 | kfree(ucontrol); |
| 2900 | return 0; |
| 2901 | } |
| 2902 | |
| 2903 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2904 | static int init_slave_0dB(struct hda_codec *codec, |
| 2905 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2906 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2907 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2908 | if (offset > 0) |
| 2909 | put_kctl_with_value(slave, offset); |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2914 | static int init_slave_unmute(struct hda_codec *codec, |
| 2915 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2916 | { |
| 2917 | return put_kctl_with_value(slave, 1); |
| 2918 | } |
| 2919 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2920 | static int add_slave(struct hda_codec *codec, |
| 2921 | void *data, struct snd_kcontrol *slave) |
| 2922 | { |
| 2923 | return snd_ctl_add_slave(data, slave); |
| 2924 | } |
| 2925 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2926 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2927 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2928 | * @codec: HD-audio codec |
| 2929 | * @name: vmaster control name |
| 2930 | * @tlv: TLV data (optional) |
| 2931 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2932 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2933 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2934 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2935 | * |
| 2936 | * Create a virtual master control with the given name. The TLV data |
| 2937 | * must be either NULL or a valid data. |
| 2938 | * |
| 2939 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2940 | * slave control name. All controls with these names are assigned to |
| 2941 | * the new virtual master control. |
| 2942 | * |
| 2943 | * This function returns zero if successful or a negative error code. |
| 2944 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2945 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2946 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2947 | const char *suffix, bool init_slave_vol, |
| 2948 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2949 | { |
| 2950 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2951 | int err; |
| 2952 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2953 | if (ctl_ret) |
| 2954 | *ctl_ret = NULL; |
| 2955 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2956 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2957 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2958 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2959 | return 0; |
| 2960 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2961 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2962 | if (!kctl) |
| 2963 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2964 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2965 | if (err < 0) |
| 2966 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2967 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2968 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2969 | if (err < 0) |
| 2970 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2971 | |
| 2972 | /* init with master mute & zero volume */ |
| 2973 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2974 | if (init_slave_vol) { |
| 2975 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2976 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2977 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2978 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2979 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2980 | if (ctl_ret) |
| 2981 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2982 | return 0; |
| 2983 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2984 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2985 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2986 | /* |
| 2987 | * mute-LED control using vmaster |
| 2988 | */ |
| 2989 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2990 | struct snd_ctl_elem_info *uinfo) |
| 2991 | { |
| 2992 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2993 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2994 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2995 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2996 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 3000 | struct snd_ctl_elem_value *ucontrol) |
| 3001 | { |
| 3002 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 3003 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 3004 | return 0; |
| 3005 | } |
| 3006 | |
| 3007 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 3008 | struct snd_ctl_elem_value *ucontrol) |
| 3009 | { |
| 3010 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 3011 | unsigned int old_mode = hook->mute_mode; |
| 3012 | |
| 3013 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 3014 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 3015 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 3016 | if (old_mode == hook->mute_mode) |
| 3017 | return 0; |
| 3018 | snd_hda_sync_vmaster_hook(hook); |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
| 3022 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 3023 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3024 | .name = "Mute-LED Mode", |
| 3025 | .info = vmaster_mute_mode_info, |
| 3026 | .get = vmaster_mute_mode_get, |
| 3027 | .put = vmaster_mute_mode_put, |
| 3028 | }; |
| 3029 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3030 | /** |
| 3031 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 3032 | * @codec: the HDA codec |
| 3033 | * @hook: the vmaster hook object |
| 3034 | * @expose_enum_ctl: flag to create an enum ctl |
| 3035 | * |
| 3036 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 3037 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 3038 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3039 | */ |
| 3040 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 3041 | struct hda_vmaster_mute_hook *hook, |
| 3042 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3043 | { |
| 3044 | struct snd_kcontrol *kctl; |
| 3045 | |
| 3046 | if (!hook->hook || !hook->sw_kctl) |
| 3047 | return 0; |
| 3048 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 3049 | hook->codec = codec; |
| 3050 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 3051 | if (!expose_enum_ctl) |
| 3052 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3053 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 3054 | if (!kctl) |
| 3055 | return -ENOMEM; |
| 3056 | return snd_hda_ctl_add(codec, 0, kctl); |
| 3057 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3058 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3059 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3060 | /** |
| 3061 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 3062 | * @hook: the vmaster hook |
| 3063 | * |
| 3064 | * Call the hook with the current value for synchronization. |
| 3065 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3066 | */ |
| 3067 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 3068 | { |
| 3069 | if (!hook->hook || !hook->codec) |
| 3070 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 3071 | /* don't call vmaster hook in the destructor since it might have |
| 3072 | * been already destroyed |
| 3073 | */ |
| 3074 | if (hook->codec->bus->shutdown) |
| 3075 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3076 | switch (hook->mute_mode) { |
| 3077 | case HDA_VMUTE_FOLLOW_MASTER: |
| 3078 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 3079 | break; |
| 3080 | default: |
| 3081 | hook->hook(hook->codec, hook->mute_mode); |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3085 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3086 | |
| 3087 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3088 | /** |
| 3089 | * 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] | 3090 | * @kcontrol: referred ctl element |
| 3091 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3092 | * |
| 3093 | * The control element is supposed to have the private_value field |
| 3094 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3095 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3096 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 3097 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | { |
| 3099 | int chs = get_amp_channels(kcontrol); |
| 3100 | |
| 3101 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 3102 | uinfo->count = chs == 3 ? 2 : 1; |
| 3103 | uinfo->value.integer.min = 0; |
| 3104 | uinfo->value.integer.max = 1; |
| 3105 | return 0; |
| 3106 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3107 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3109 | /** |
| 3110 | * 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] | 3111 | * @kcontrol: ctl element |
| 3112 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3113 | * |
| 3114 | * The control element is supposed to have the private_value field |
| 3115 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3116 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3117 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 3118 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | { |
| 3120 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3121 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3122 | int chs = get_amp_channels(kcontrol); |
| 3123 | int dir = get_amp_direction(kcontrol); |
| 3124 | int idx = get_amp_index(kcontrol); |
| 3125 | long *valp = ucontrol->value.integer.value; |
| 3126 | |
| 3127 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3128 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3129 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3131 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3132 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | return 0; |
| 3134 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3135 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3137 | /** |
| 3138 | * 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] | 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_COMPOSE_AMP_VAL*() or related macros. |
| 3144 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3145 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3146 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | { |
| 3148 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3149 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3150 | int chs = get_amp_channels(kcontrol); |
| 3151 | int dir = get_amp_direction(kcontrol); |
| 3152 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | long *valp = ucontrol->value.integer.value; |
| 3154 | int change = 0; |
| 3155 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3156 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3157 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3158 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3159 | HDA_AMP_MUTE, |
| 3160 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3161 | valp++; |
| 3162 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3163 | if (chs & 2) |
| 3164 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3165 | HDA_AMP_MUTE, |
| 3166 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3167 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3168 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | return change; |
| 3170 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3171 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | |
| 3173 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3174 | * bound volume controls |
| 3175 | * |
| 3176 | * bind multiple volumes (# indices, from 0) |
| 3177 | */ |
| 3178 | |
| 3179 | #define AMP_VAL_IDX_SHIFT 19 |
| 3180 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3181 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3182 | /** |
| 3183 | * 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] | 3184 | * @kcontrol: ctl element |
| 3185 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3186 | * |
| 3187 | * The control element is supposed to have the private_value field |
| 3188 | * set up via HDA_BIND_MUTE*() macros. |
| 3189 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3190 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3191 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3192 | { |
| 3193 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3194 | unsigned long pval; |
| 3195 | int err; |
| 3196 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3197 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3198 | pval = kcontrol->private_value; |
| 3199 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3200 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3201 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3202 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3203 | return err; |
| 3204 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3205 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3206 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3207 | /** |
| 3208 | * 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] | 3209 | * @kcontrol: ctl element |
| 3210 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3211 | * |
| 3212 | * The control element is supposed to have the private_value field |
| 3213 | * set up via HDA_BIND_MUTE*() macros. |
| 3214 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3215 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3216 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3217 | { |
| 3218 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3219 | unsigned long pval; |
| 3220 | int i, indices, err = 0, change = 0; |
| 3221 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3222 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3223 | pval = kcontrol->private_value; |
| 3224 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3225 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3226 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3227 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3228 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3229 | if (err < 0) |
| 3230 | break; |
| 3231 | change |= err; |
| 3232 | } |
| 3233 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3234 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3235 | return err < 0 ? err : change; |
| 3236 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3237 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3238 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3239 | /** |
| 3240 | * 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] | 3241 | * @kcontrol: referred ctl element |
| 3242 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3243 | * |
| 3244 | * The control element is supposed to have the private_value field |
| 3245 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3246 | */ |
| 3247 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3248 | struct snd_ctl_elem_info *uinfo) |
| 3249 | { |
| 3250 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3251 | struct hda_bind_ctls *c; |
| 3252 | int err; |
| 3253 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3254 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3255 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3256 | kcontrol->private_value = *c->values; |
| 3257 | err = c->ops->info(kcontrol, uinfo); |
| 3258 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3259 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3260 | return err; |
| 3261 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3262 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3263 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3264 | /** |
| 3265 | * 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] | 3266 | * @kcontrol: ctl element |
| 3267 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3268 | * |
| 3269 | * The control element is supposed to have the private_value field |
| 3270 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3271 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3272 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3273 | struct snd_ctl_elem_value *ucontrol) |
| 3274 | { |
| 3275 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3276 | struct hda_bind_ctls *c; |
| 3277 | int err; |
| 3278 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3279 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3280 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3281 | kcontrol->private_value = *c->values; |
| 3282 | err = c->ops->get(kcontrol, ucontrol); |
| 3283 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3284 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3285 | return err; |
| 3286 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3287 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3288 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3289 | /** |
| 3290 | * 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] | 3291 | * @kcontrol: ctl element |
| 3292 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3293 | * |
| 3294 | * The control element is supposed to have the private_value field |
| 3295 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3296 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3297 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3298 | struct snd_ctl_elem_value *ucontrol) |
| 3299 | { |
| 3300 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3301 | struct hda_bind_ctls *c; |
| 3302 | unsigned long *vals; |
| 3303 | int err = 0, change = 0; |
| 3304 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3305 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3306 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3307 | for (vals = c->values; *vals; vals++) { |
| 3308 | kcontrol->private_value = *vals; |
| 3309 | err = c->ops->put(kcontrol, ucontrol); |
| 3310 | if (err < 0) |
| 3311 | break; |
| 3312 | change |= err; |
| 3313 | } |
| 3314 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3315 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3316 | return err < 0 ? err : change; |
| 3317 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3318 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3319 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3320 | /** |
| 3321 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3322 | * @kcontrol: ctl element |
| 3323 | * @op_flag: operation flag |
| 3324 | * @size: byte size of input TLV |
| 3325 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3326 | * |
| 3327 | * The control element is supposed to have the private_value field |
| 3328 | * set up via HDA_BIND_VOL() macro. |
| 3329 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3330 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3331 | unsigned int size, unsigned int __user *tlv) |
| 3332 | { |
| 3333 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3334 | struct hda_bind_ctls *c; |
| 3335 | int err; |
| 3336 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3337 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3338 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3339 | kcontrol->private_value = *c->values; |
| 3340 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3341 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3342 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3343 | return err; |
| 3344 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3345 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3346 | |
| 3347 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3348 | .info = snd_hda_mixer_amp_volume_info, |
| 3349 | .get = snd_hda_mixer_amp_volume_get, |
| 3350 | .put = snd_hda_mixer_amp_volume_put, |
| 3351 | .tlv = snd_hda_mixer_amp_tlv |
| 3352 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3353 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3354 | |
| 3355 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3356 | .info = snd_hda_mixer_amp_switch_info, |
| 3357 | .get = snd_hda_mixer_amp_switch_get, |
| 3358 | .put = snd_hda_mixer_amp_switch_put, |
| 3359 | .tlv = snd_hda_mixer_amp_tlv |
| 3360 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3361 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3362 | |
| 3363 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | * SPDIF out controls |
| 3365 | */ |
| 3366 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3367 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3368 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | { |
| 3370 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3371 | uinfo->count = 1; |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3375 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3376 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | { |
| 3378 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3379 | IEC958_AES0_NONAUDIO | |
| 3380 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3381 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3382 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3383 | IEC958_AES1_CON_ORIGINAL; |
| 3384 | return 0; |
| 3385 | } |
| 3386 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3387 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3388 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | { |
| 3390 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3391 | IEC958_AES0_NONAUDIO | |
| 3392 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3396 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3397 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | { |
| 3399 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3400 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3401 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3403 | mutex_lock(&codec->spdif_mutex); |
| 3404 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3405 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3406 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3407 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3408 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3409 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3415 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3416 | */ |
| 3417 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3418 | { |
| 3419 | unsigned short val = 0; |
| 3420 | |
| 3421 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3422 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3424 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3426 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3427 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3428 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3430 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3431 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3432 | val |= AC_DIG1_EMPHASIS; |
| 3433 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3434 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3436 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3438 | } |
| 3439 | return val; |
| 3440 | } |
| 3441 | |
| 3442 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3443 | */ |
| 3444 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3445 | { |
| 3446 | unsigned int sbits = 0; |
| 3447 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3448 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3450 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3452 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3453 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3455 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3456 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3458 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3460 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3462 | sbits |= val & (0x7f << 8); |
| 3463 | } |
| 3464 | return sbits; |
| 3465 | } |
| 3466 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3467 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3468 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3469 | int verb, int val) |
| 3470 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3471 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3472 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3473 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3474 | d = codec->slave_dig_outs; |
| 3475 | if (!d) |
| 3476 | return; |
| 3477 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3478 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3482 | int dig1, int dig2) |
| 3483 | { |
| 3484 | if (dig1 != -1) |
| 3485 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3486 | if (dig2 != -1) |
| 3487 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3488 | } |
| 3489 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3490 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3491 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | { |
| 3493 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3494 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3495 | struct hda_spdif_out *spdif; |
| 3496 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | unsigned short val; |
| 3498 | int change; |
| 3499 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3500 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3501 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3502 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3503 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3505 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3506 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3507 | val = convert_from_spdif_status(spdif->status); |
| 3508 | val |= spdif->ctls & 1; |
| 3509 | change = spdif->ctls != val; |
| 3510 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3511 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3512 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3513 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | return change; |
| 3515 | } |
| 3516 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3517 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3519 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3520 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | { |
| 3522 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3523 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3524 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3526 | mutex_lock(&codec->spdif_mutex); |
| 3527 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3528 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3529 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | return 0; |
| 3531 | } |
| 3532 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3533 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3534 | int dig1, int dig2) |
| 3535 | { |
| 3536 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3537 | /* unmute amp switch (if any) */ |
| 3538 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3539 | (dig1 & AC_DIG1_ENABLE)) |
| 3540 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3541 | HDA_AMP_MUTE, 0); |
| 3542 | } |
| 3543 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3544 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3545 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | { |
| 3547 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3548 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3549 | struct hda_spdif_out *spdif; |
| 3550 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | unsigned short val; |
| 3552 | int change; |
| 3553 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3554 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3555 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3556 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3557 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3559 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3560 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3561 | spdif->ctls = val; |
| 3562 | if (change && nid != (u16)-1) |
| 3563 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3564 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | return change; |
| 3566 | } |
| 3567 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3568 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | { |
| 3570 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3571 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3572 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | .info = snd_hda_spdif_mask_info, |
| 3574 | .get = snd_hda_spdif_cmask_get, |
| 3575 | }, |
| 3576 | { |
| 3577 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3578 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3579 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | .info = snd_hda_spdif_mask_info, |
| 3581 | .get = snd_hda_spdif_pmask_get, |
| 3582 | }, |
| 3583 | { |
| 3584 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3585 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | .info = snd_hda_spdif_mask_info, |
| 3587 | .get = snd_hda_spdif_default_get, |
| 3588 | .put = snd_hda_spdif_default_put, |
| 3589 | }, |
| 3590 | { |
| 3591 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3592 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | .info = snd_hda_spdif_out_switch_info, |
| 3594 | .get = snd_hda_spdif_out_switch_get, |
| 3595 | .put = snd_hda_spdif_out_switch_put, |
| 3596 | }, |
| 3597 | { } /* end */ |
| 3598 | }; |
| 3599 | |
| 3600 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3601 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3603 | * @associated_nid: NID that new ctls associated with |
| 3604 | * @cvt_nid: converter NID |
| 3605 | * @type: HDA_PCM_TYPE_* |
| 3606 | * Creates controls related with the digital output. |
| 3607 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3608 | * |
| 3609 | * Returns 0 if successful, or a negative error code. |
| 3610 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3611 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3612 | hda_nid_t associated_nid, |
| 3613 | hda_nid_t cvt_nid, |
| 3614 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | { |
| 3616 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3617 | struct snd_kcontrol *kctl; |
| 3618 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3619 | int idx = 0; |
| 3620 | const int spdif_index = 16; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3621 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3622 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3624 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3625 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3626 | idx = spdif_index; |
| 3627 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3628 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3629 | /* suppose a single SPDIF device */ |
| 3630 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3631 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3632 | if (!kctl) |
| 3633 | break; |
| 3634 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3635 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3636 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3637 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3638 | if (!bus->primary_dig_out_type) |
| 3639 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3640 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3641 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3642 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3643 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3644 | return -EBUSY; |
| 3645 | } |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3646 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3647 | if (!spdif) |
| 3648 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3650 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3651 | if (!kctl) |
| 3652 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3653 | kctl->id.index = idx; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3654 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3655 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3656 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | return err; |
| 3658 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3659 | spdif->nid = cvt_nid; |
| 3660 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3661 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3662 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | return 0; |
| 3664 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3665 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3667 | /** |
| 3668 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3669 | * @codec: the HDA codec |
| 3670 | * @nid: widget NID |
| 3671 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3672 | * call within spdif_mutex lock |
| 3673 | */ |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3674 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3675 | hda_nid_t nid) |
| 3676 | { |
| 3677 | int i; |
| 3678 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3679 | struct hda_spdif_out *spdif = |
| 3680 | snd_array_elem(&codec->spdif_out, i); |
| 3681 | if (spdif->nid == nid) |
| 3682 | return spdif; |
| 3683 | } |
| 3684 | return NULL; |
| 3685 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3686 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3687 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3688 | /** |
| 3689 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3690 | * @codec: the HDA codec |
| 3691 | * @idx: the SPDIF ctl index |
| 3692 | * |
| 3693 | * Unassign the widget from the given SPDIF control. |
| 3694 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3695 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3696 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3697 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3698 | |
| 3699 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3700 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3701 | spdif->nid = (u16)-1; |
| 3702 | mutex_unlock(&codec->spdif_mutex); |
| 3703 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3704 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3705 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3706 | /** |
| 3707 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3708 | * @codec: the HDA codec |
| 3709 | * @idx: the SPDIF ctl idx |
| 3710 | * @nid: widget NID |
| 3711 | * |
| 3712 | * Assign the widget to the SPDIF control with the given index. |
| 3713 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3714 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3715 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3716 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3717 | unsigned short val; |
| 3718 | |
| 3719 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3720 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3721 | if (spdif->nid != nid) { |
| 3722 | spdif->nid = nid; |
| 3723 | val = spdif->ctls; |
| 3724 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3725 | } |
| 3726 | mutex_unlock(&codec->spdif_mutex); |
| 3727 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3728 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3731 | * SPDIF sharing with analog output |
| 3732 | */ |
| 3733 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3734 | struct snd_ctl_elem_value *ucontrol) |
| 3735 | { |
| 3736 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3737 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3738 | return 0; |
| 3739 | } |
| 3740 | |
| 3741 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3742 | struct snd_ctl_elem_value *ucontrol) |
| 3743 | { |
| 3744 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3745 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3746 | return 0; |
| 3747 | } |
| 3748 | |
| 3749 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3750 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3751 | .name = "IEC958 Default PCM Playback Switch", |
| 3752 | .info = snd_ctl_boolean_mono_info, |
| 3753 | .get = spdif_share_sw_get, |
| 3754 | .put = spdif_share_sw_put, |
| 3755 | }; |
| 3756 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3757 | /** |
| 3758 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3759 | * @codec: the HDA codec |
| 3760 | * @mout: multi-out instance |
| 3761 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3762 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3763 | struct hda_multi_out *mout) |
| 3764 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3765 | struct snd_kcontrol *kctl; |
| 3766 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3767 | if (!mout->dig_out_nid) |
| 3768 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3769 | |
| 3770 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3771 | if (!kctl) |
| 3772 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3773 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3774 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3776 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3777 | |
| 3778 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | * SPDIF input |
| 3780 | */ |
| 3781 | |
| 3782 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3783 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3784 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3785 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | { |
| 3787 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3788 | |
| 3789 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3793 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3794 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | { |
| 3796 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3797 | hda_nid_t nid = kcontrol->private_value; |
| 3798 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3799 | int change; |
| 3800 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3801 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3803 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3805 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3806 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3808 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | return change; |
| 3810 | } |
| 3811 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3812 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3813 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | { |
| 3815 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3816 | hda_nid_t nid = kcontrol->private_value; |
| 3817 | unsigned short val; |
| 3818 | unsigned int sbits; |
| 3819 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3820 | 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] | 3821 | sbits = convert_to_spdif_status(val); |
| 3822 | ucontrol->value.iec958.status[0] = sbits; |
| 3823 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3824 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3825 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3829 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | { |
| 3831 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3832 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | .info = snd_hda_spdif_in_switch_info, |
| 3834 | .get = snd_hda_spdif_in_switch_get, |
| 3835 | .put = snd_hda_spdif_in_switch_put, |
| 3836 | }, |
| 3837 | { |
| 3838 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3839 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3840 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | .info = snd_hda_spdif_mask_info, |
| 3842 | .get = snd_hda_spdif_in_status_get, |
| 3843 | }, |
| 3844 | { } /* end */ |
| 3845 | }; |
| 3846 | |
| 3847 | /** |
| 3848 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3849 | * @codec: the HDA codec |
| 3850 | * @nid: audio in widget NID |
| 3851 | * |
| 3852 | * Creates controls related with the SPDIF input. |
| 3853 | * Called from each patch supporting the SPDIF in. |
| 3854 | * |
| 3855 | * Returns 0 if successful, or a negative error code. |
| 3856 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3857 | 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] | 3858 | { |
| 3859 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3860 | struct snd_kcontrol *kctl; |
| 3861 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3862 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3863 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3864 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3865 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3866 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3867 | return -EBUSY; |
| 3868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3870 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3871 | if (!kctl) |
| 3872 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3874 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3875 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | return err; |
| 3877 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3878 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3879 | snd_hda_codec_read(codec, nid, 0, |
| 3880 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3881 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | return 0; |
| 3883 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3884 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3886 | /* |
| 3887 | * command cache |
| 3888 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3890 | /* 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] | 3891 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3892 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3893 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3894 | |
| 3895 | /** |
| 3896 | * snd_hda_codec_write_cache - send a single command with caching |
| 3897 | * @codec: the HDA codec |
| 3898 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3899 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3900 | * @verb: the verb to send |
| 3901 | * @parm: the parameter for the verb |
| 3902 | * |
| 3903 | * Send a single command without waiting for response. |
| 3904 | * |
| 3905 | * Returns 0 if successful, or a negative error code. |
| 3906 | */ |
| 3907 | 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] | 3908 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3909 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3910 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3911 | struct hda_cache_head *c; |
| 3912 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3913 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3914 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3915 | cache_only = codec->cached_write; |
| 3916 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3917 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3918 | if (err < 0) |
| 3919 | return err; |
| 3920 | } |
| 3921 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3922 | /* parm may contain the verb stuff for get/set amp */ |
| 3923 | verb = verb | (parm >> 8); |
| 3924 | parm &= 0xff; |
| 3925 | key = build_cmd_cache_key(nid, verb); |
| 3926 | mutex_lock(&codec->bus->cmd_mutex); |
| 3927 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3928 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3929 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3930 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3931 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3932 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3933 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3934 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3935 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3936 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3937 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3938 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3939 | * @codec: the HDA codec |
| 3940 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3941 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3942 | * @verb: the verb to send |
| 3943 | * @parm: the parameter for the verb |
| 3944 | * |
| 3945 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3946 | * command if the parameter is already identical with the cached value. |
| 3947 | * If not, it sends the command and refreshes the cache. |
| 3948 | * |
| 3949 | * Returns 0 if successful, or a negative error code. |
| 3950 | */ |
| 3951 | 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] | 3952 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3953 | { |
| 3954 | struct hda_cache_head *c; |
| 3955 | u32 key; |
| 3956 | |
| 3957 | /* parm may contain the verb stuff for get/set amp */ |
| 3958 | verb = verb | (parm >> 8); |
| 3959 | parm &= 0xff; |
| 3960 | key = build_cmd_cache_key(nid, verb); |
| 3961 | mutex_lock(&codec->bus->cmd_mutex); |
| 3962 | c = get_hash(&codec->cmd_cache, key); |
| 3963 | if (c && c->val == parm) { |
| 3964 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3965 | return 0; |
| 3966 | } |
| 3967 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3968 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3969 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3970 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3971 | |
| 3972 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3973 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3974 | * @codec: HD-audio codec |
| 3975 | * |
| 3976 | * Execute all verbs recorded in the command caches to resume. |
| 3977 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3978 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3979 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3980 | int i; |
| 3981 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3982 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3983 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3984 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3985 | struct hda_cache_head *buffer; |
| 3986 | u32 key; |
| 3987 | |
| 3988 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3989 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3990 | if (!key) |
| 3991 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3992 | if (!buffer->dirty) |
| 3993 | continue; |
| 3994 | buffer->dirty = 0; |
| 3995 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3996 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3997 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3998 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3999 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4000 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4001 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4002 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4003 | |
| 4004 | /** |
| 4005 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 4006 | * @codec: the HDA codec |
| 4007 | * @seq: VERB array to send |
| 4008 | * |
| 4009 | * Send the commands sequentially from the given array. |
| 4010 | * Thte commands are recorded on cache for power-save and resume. |
| 4011 | * The array must be terminated with NID=0. |
| 4012 | */ |
| 4013 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 4014 | const struct hda_verb *seq) |
| 4015 | { |
| 4016 | for (; seq->nid; seq++) |
| 4017 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 4018 | seq->param); |
| 4019 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4020 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 4021 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 4022 | /** |
| 4023 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 4024 | * @codec: HD-audio codec |
| 4025 | */ |
| 4026 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 4027 | { |
| 4028 | snd_hda_codec_resume_amp(codec); |
| 4029 | snd_hda_codec_resume_cache(codec); |
| 4030 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4031 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 4032 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4033 | /** |
| 4034 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 4035 | * @codec: the HDA codec |
| 4036 | * @fg: function group (not used now) |
| 4037 | * @power_state: the power state to set (AC_PWRST_*) |
| 4038 | * |
| 4039 | * Set the given power state to all widgets that have the power control. |
| 4040 | * If the codec has power_filter set, it evaluates the power state and |
| 4041 | * filter out if it's unchanged as D3. |
| 4042 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4043 | 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] | 4044 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4045 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4046 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4047 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4048 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4049 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 4050 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4051 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4052 | if (!(wcaps & AC_WCAP_POWER)) |
| 4053 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4054 | if (codec->power_filter) { |
| 4055 | state = codec->power_filter(codec, nid, power_state); |
| 4056 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4057 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 4058 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4059 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4060 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4061 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4062 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4063 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4064 | |
| 4065 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4066 | * supported power states check |
| 4067 | */ |
| 4068 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 4069 | unsigned int power_state) |
| 4070 | { |
| 4071 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 4072 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 4073 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 4074 | return false; |
| 4075 | if (sup & power_state) |
| 4076 | return true; |
| 4077 | else |
| 4078 | return false; |
| 4079 | } |
| 4080 | |
| 4081 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4082 | * wait until the state is reached, returns the current state |
| 4083 | */ |
| 4084 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 4085 | hda_nid_t fg, |
| 4086 | unsigned int power_state) |
| 4087 | { |
| 4088 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 4089 | unsigned int state, actual_state; |
| 4090 | |
| 4091 | for (;;) { |
| 4092 | state = snd_hda_codec_read(codec, fg, 0, |
| 4093 | AC_VERB_GET_POWER_STATE, 0); |
| 4094 | if (state & AC_PWRST_ERROR) |
| 4095 | break; |
| 4096 | actual_state = (state >> 4) & 0x0f; |
| 4097 | if (actual_state == power_state) |
| 4098 | break; |
| 4099 | if (time_after_eq(jiffies, end_time)) |
| 4100 | break; |
| 4101 | /* wait until the codec reachs to the target state */ |
| 4102 | msleep(1); |
| 4103 | } |
| 4104 | return state; |
| 4105 | } |
| 4106 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4107 | /** |
| 4108 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 4109 | * @codec: the HDA codec |
| 4110 | * @nid: widget NID |
| 4111 | * @power_state: power state to evalue |
| 4112 | * |
| 4113 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 4114 | * This can be used a codec power_filter callback. |
| 4115 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4116 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 4117 | hda_nid_t nid, |
| 4118 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4119 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4120 | if (nid == codec->afg || nid == codec->mfg) |
| 4121 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4122 | if (power_state == AC_PWRST_D3 && |
| 4123 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 4124 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 4125 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 4126 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 4127 | if (eapd & 0x02) |
| 4128 | return AC_PWRST_D0; |
| 4129 | } |
| 4130 | return power_state; |
| 4131 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4132 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4133 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4134 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4135 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4136 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4137 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4138 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4139 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4140 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4141 | int count; |
| 4142 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4143 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4144 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4145 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4146 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 4147 | if (codec->depop_delay < 0) |
| 4148 | msleep(codec->epss ? 10 : 100); |
| 4149 | else if (codec->depop_delay > 0) |
| 4150 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4151 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4152 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4153 | |
| 4154 | /* repeat power states setting at most 10 times*/ |
| 4155 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4156 | if (codec->patch_ops.set_power_state) |
| 4157 | codec->patch_ops.set_power_state(codec, fg, |
| 4158 | power_state); |
| 4159 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4160 | state = power_state; |
| 4161 | if (codec->power_filter) |
| 4162 | state = codec->power_filter(codec, fg, state); |
| 4163 | if (state == power_state || power_state != AC_PWRST_D3) |
| 4164 | snd_hda_codec_read(codec, fg, flags, |
| 4165 | AC_VERB_SET_POWER_STATE, |
| 4166 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4167 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4168 | } |
| 4169 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4170 | if (!(state & AC_PWRST_ERROR)) |
| 4171 | break; |
| 4172 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 4173 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4174 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4175 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4176 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4177 | /* sync power states of all widgets; |
| 4178 | * this is called at the end of codec parsing |
| 4179 | */ |
| 4180 | static void sync_power_up_states(struct hda_codec *codec) |
| 4181 | { |
| 4182 | hda_nid_t nid = codec->start_nid; |
| 4183 | int i; |
| 4184 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4185 | /* don't care if no filter is used */ |
| 4186 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4187 | return; |
| 4188 | |
| 4189 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4190 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4191 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4192 | if (!(wcaps & AC_WCAP_POWER)) |
| 4193 | continue; |
| 4194 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4195 | if (target == AC_PWRST_D0) |
| 4196 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4197 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4198 | snd_hda_codec_write(codec, nid, 0, |
| 4199 | AC_VERB_SET_POWER_STATE, target); |
| 4200 | } |
| 4201 | } |
| 4202 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 4203 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4204 | /* execute additional init verbs */ |
| 4205 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4206 | { |
| 4207 | if (codec->init_verbs.list) |
| 4208 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4209 | } |
| 4210 | #else |
| 4211 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4212 | #endif |
| 4213 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4214 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4215 | /* |
| 4216 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4217 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4218 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4219 | 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] | 4220 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4221 | unsigned int state; |
| 4222 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4223 | codec->in_pm = 1; |
| 4224 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4225 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4226 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4227 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4228 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4229 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4230 | if (!in_wq) |
| 4231 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4232 | spin_lock(&codec->power_lock); |
| 4233 | snd_hda_update_power_acct(codec); |
| 4234 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4235 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4236 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4237 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4238 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4239 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4240 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4241 | } |
| 4242 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4243 | /* mark all entries of cmd and amp caches dirty */ |
| 4244 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4245 | { |
| 4246 | int i; |
| 4247 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4248 | struct hda_cache_head *cmd; |
| 4249 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4250 | cmd->dirty = 1; |
| 4251 | } |
| 4252 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4253 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4254 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4255 | amp->head.dirty = 1; |
| 4256 | } |
| 4257 | } |
| 4258 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4259 | /* |
| 4260 | * kick up codec; used both from PM and power-save |
| 4261 | */ |
| 4262 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4263 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4264 | codec->in_pm = 1; |
| 4265 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4266 | hda_mark_cmd_cache_dirty(codec); |
| 4267 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4268 | /* set as if powered on for avoiding re-entering the resume |
| 4269 | * in the resume / power-save sequence |
| 4270 | */ |
| 4271 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4272 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4273 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4274 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4275 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4276 | if (codec->patch_ops.resume) |
| 4277 | codec->patch_ops.resume(codec); |
| 4278 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4279 | if (codec->patch_ops.init) |
| 4280 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4281 | snd_hda_codec_resume_amp(codec); |
| 4282 | snd_hda_codec_resume_cache(codec); |
| 4283 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4284 | |
| 4285 | if (codec->jackpoll_interval) |
| 4286 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4287 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4288 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4289 | |
| 4290 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4291 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4292 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4293 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4294 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | /** |
| 4297 | * snd_hda_build_controls - build mixer controls |
| 4298 | * @bus: the BUS |
| 4299 | * |
| 4300 | * Creates mixer controls for each codec included in the bus. |
| 4301 | * |
| 4302 | * Returns 0 if successful, otherwise a negative error code. |
| 4303 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4304 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4306 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4308 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4309 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4310 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4311 | codec_err(codec, |
| 4312 | "cannot build controls for #%d (error %d)\n", |
| 4313 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4314 | err = snd_hda_codec_reset(codec); |
| 4315 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4316 | codec_err(codec, |
| 4317 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4318 | return err; |
| 4319 | } |
| 4320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4322 | return 0; |
| 4323 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4324 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4326 | /* |
| 4327 | * add standard channel maps if not specified |
| 4328 | */ |
| 4329 | static int add_std_chmaps(struct hda_codec *codec) |
| 4330 | { |
| 4331 | int i, str, err; |
| 4332 | |
| 4333 | for (i = 0; i < codec->num_pcms; i++) { |
| 4334 | for (str = 0; str < 2; str++) { |
| 4335 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4336 | struct hda_pcm_stream *hinfo = |
| 4337 | &codec->pcm_info[i].stream[str]; |
| 4338 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4339 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4340 | |
| 4341 | if (codec->pcm_info[i].own_chmap) |
| 4342 | continue; |
| 4343 | if (!pcm || !hinfo->substreams) |
| 4344 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4345 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4346 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4347 | hinfo->channels_max, |
| 4348 | 0, &chmap); |
| 4349 | if (err < 0) |
| 4350 | return err; |
| 4351 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4352 | } |
| 4353 | } |
| 4354 | return 0; |
| 4355 | } |
| 4356 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4357 | /* default channel maps for 2.1 speakers; |
| 4358 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4359 | */ |
| 4360 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4361 | { .channels = 2, |
| 4362 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4363 | { .channels = 4, |
| 4364 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4365 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4366 | { } |
| 4367 | }; |
| 4368 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4369 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4370 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4371 | { |
| 4372 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4373 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4374 | /* continue to initialize... */ |
| 4375 | if (codec->patch_ops.init) |
| 4376 | err = codec->patch_ops.init(codec); |
| 4377 | if (!err && codec->patch_ops.build_controls) |
| 4378 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4379 | if (err < 0) |
| 4380 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4381 | |
| 4382 | /* we create chmaps here instead of build_pcms */ |
| 4383 | err = add_std_chmaps(codec); |
| 4384 | if (err < 0) |
| 4385 | return err; |
| 4386 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4387 | if (codec->jackpoll_interval) |
| 4388 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4389 | else |
| 4390 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4391 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | return 0; |
| 4393 | } |
| 4394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | /* |
| 4396 | * stream formats |
| 4397 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4398 | struct hda_rate_tbl { |
| 4399 | unsigned int hz; |
| 4400 | unsigned int alsa_bits; |
| 4401 | unsigned int hda_fmt; |
| 4402 | }; |
| 4403 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4404 | /* rate = base * mult / div */ |
| 4405 | #define HDA_RATE(base, mult, div) \ |
| 4406 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4407 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4408 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4409 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4411 | |
| 4412 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4413 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4414 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4415 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4416 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4417 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4418 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4419 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4420 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4421 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4422 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4423 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4424 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4425 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4426 | |
| 4427 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4428 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4429 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4430 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | }; |
| 4432 | |
| 4433 | /** |
| 4434 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4435 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | * @rate: the sample rate |
| 4437 | * @channels: the number of channels |
| 4438 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4439 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4440 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | * |
| 4442 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4443 | * |
| 4444 | * Return zero if invalid. |
| 4445 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4446 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4447 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | unsigned int channels, |
| 4449 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4450 | unsigned int maxbps, |
| 4451 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | { |
| 4453 | int i; |
| 4454 | unsigned int val = 0; |
| 4455 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4456 | for (i = 0; rate_bits[i].hz; i++) |
| 4457 | if (rate_bits[i].hz == rate) { |
| 4458 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | break; |
| 4460 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4461 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4462 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | return 0; |
| 4464 | } |
| 4465 | |
| 4466 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4467 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | return 0; |
| 4469 | } |
| 4470 | val |= channels - 1; |
| 4471 | |
| 4472 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4473 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4474 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4475 | break; |
| 4476 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4477 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4478 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | case 20: |
| 4480 | case 24: |
| 4481 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4482 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4483 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4485 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4487 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | break; |
| 4489 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4490 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4491 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | return 0; |
| 4493 | } |
| 4494 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4495 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4496 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | return val; |
| 4499 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4500 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4502 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4503 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4504 | { |
| 4505 | unsigned int val = 0; |
| 4506 | if (nid != codec->afg && |
| 4507 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4508 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4509 | if (!val || val == -1) |
| 4510 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4511 | if (!val || val == -1) |
| 4512 | return 0; |
| 4513 | return val; |
| 4514 | } |
| 4515 | |
| 4516 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4517 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4518 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4519 | get_pcm_param); |
| 4520 | } |
| 4521 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4522 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4523 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4524 | { |
| 4525 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4526 | if (!streams || streams == -1) |
| 4527 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4528 | if (!streams || streams == -1) |
| 4529 | return 0; |
| 4530 | return streams; |
| 4531 | } |
| 4532 | |
| 4533 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4534 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4535 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4536 | get_stream_param); |
| 4537 | } |
| 4538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | /** |
| 4540 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4541 | * @codec: the HDA codec |
| 4542 | * @nid: NID to query |
| 4543 | * @ratesp: the pointer to store the detected rate bitflags |
| 4544 | * @formatsp: the pointer to store the detected formats |
| 4545 | * @bpsp: the pointer to store the detected format widths |
| 4546 | * |
| 4547 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4548 | * or @bsps argument is ignored. |
| 4549 | * |
| 4550 | * Returns 0 if successful, otherwise a negative error code. |
| 4551 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4552 | 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] | 4553 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4554 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4555 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4557 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4558 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | |
| 4560 | if (ratesp) { |
| 4561 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4562 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4564 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4566 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4567 | codec_err(codec, |
| 4568 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4569 | nid, val, |
| 4570 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4571 | return -EIO; |
| 4572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | *ratesp = rates; |
| 4574 | } |
| 4575 | |
| 4576 | if (formatsp || bpsp) { |
| 4577 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4578 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4580 | streams = query_stream_param(codec, nid); |
| 4581 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
| 4584 | bps = 0; |
| 4585 | if (streams & AC_SUPFMT_PCM) { |
| 4586 | if (val & AC_SUPPCM_BITS_8) { |
| 4587 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4588 | bps = 8; |
| 4589 | } |
| 4590 | if (val & AC_SUPPCM_BITS_16) { |
| 4591 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4592 | bps = 16; |
| 4593 | } |
| 4594 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4595 | if (val & AC_SUPPCM_BITS_32) |
| 4596 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4597 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4598 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4599 | if (val & AC_SUPPCM_BITS_24) |
| 4600 | bps = 24; |
| 4601 | else if (val & AC_SUPPCM_BITS_20) |
| 4602 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4603 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4604 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4606 | if (val & AC_SUPPCM_BITS_32) |
| 4607 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | else if (val & AC_SUPPCM_BITS_24) |
| 4609 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4610 | else if (val & AC_SUPPCM_BITS_20) |
| 4611 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | } |
| 4613 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4614 | #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] | 4615 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4617 | if (!bps) |
| 4618 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4619 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4620 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4621 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4622 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | /* temporary hack: we have still no proper support |
| 4624 | * for the direct AC3 stream... |
| 4625 | */ |
| 4626 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4627 | bps = 8; |
| 4628 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4629 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4630 | codec_err(codec, |
| 4631 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4632 | nid, val, |
| 4633 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4634 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4635 | return -EIO; |
| 4636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | if (formatsp) |
| 4638 | *formatsp = formats; |
| 4639 | if (bpsp) |
| 4640 | *bpsp = bps; |
| 4641 | } |
| 4642 | |
| 4643 | return 0; |
| 4644 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4645 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | |
| 4647 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4648 | * snd_hda_is_supported_format - Check the validity of the format |
| 4649 | * @codec: HD-audio codec |
| 4650 | * @nid: NID to check |
| 4651 | * @format: the HD-audio format value to check |
| 4652 | * |
| 4653 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | * |
| 4655 | * Returns 1 if supported, 0 if not. |
| 4656 | */ |
| 4657 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4658 | unsigned int format) |
| 4659 | { |
| 4660 | int i; |
| 4661 | unsigned int val = 0, rate, stream; |
| 4662 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4663 | val = query_pcm_param(codec, nid); |
| 4664 | if (!val) |
| 4665 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | |
| 4667 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4668 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4669 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | if (val & (1 << i)) |
| 4671 | break; |
| 4672 | return 0; |
| 4673 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4674 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | return 0; |
| 4676 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4677 | stream = query_stream_param(codec, nid); |
| 4678 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | return 0; |
| 4680 | |
| 4681 | if (stream & AC_SUPFMT_PCM) { |
| 4682 | switch (format & 0xf0) { |
| 4683 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4684 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | return 0; |
| 4686 | break; |
| 4687 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4688 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | return 0; |
| 4690 | break; |
| 4691 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4692 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | return 0; |
| 4694 | break; |
| 4695 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4696 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | return 0; |
| 4698 | break; |
| 4699 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4700 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | return 0; |
| 4702 | break; |
| 4703 | default: |
| 4704 | return 0; |
| 4705 | } |
| 4706 | } else { |
| 4707 | /* FIXME: check for float32 and AC3? */ |
| 4708 | } |
| 4709 | |
| 4710 | return 1; |
| 4711 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4712 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | |
| 4714 | /* |
| 4715 | * PCM stuff |
| 4716 | */ |
| 4717 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4718 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4719 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | { |
| 4721 | return 0; |
| 4722 | } |
| 4723 | |
| 4724 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4725 | struct hda_codec *codec, |
| 4726 | unsigned int stream_tag, |
| 4727 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4728 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | { |
| 4730 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
| 4734 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4735 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4736 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4738 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | return 0; |
| 4740 | } |
| 4741 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4742 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4743 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4745 | int err; |
| 4746 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4747 | /* query support PCM information from the given NID */ |
| 4748 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4749 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4750 | info->rates ? NULL : &info->rates, |
| 4751 | info->formats ? NULL : &info->formats, |
| 4752 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4753 | if (err < 0) |
| 4754 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | } |
| 4756 | if (info->ops.open == NULL) |
| 4757 | info->ops.open = hda_pcm_default_open_close; |
| 4758 | if (info->ops.close == NULL) |
| 4759 | info->ops.close = hda_pcm_default_open_close; |
| 4760 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4761 | if (snd_BUG_ON(!info->nid)) |
| 4762 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | info->ops.prepare = hda_pcm_default_prepare; |
| 4764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4766 | if (snd_BUG_ON(!info->nid)) |
| 4767 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4769 | } |
| 4770 | return 0; |
| 4771 | } |
| 4772 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4773 | /* |
| 4774 | * codec prepare/cleanup entries |
| 4775 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4776 | /** |
| 4777 | * snd_hda_codec_prepare - Prepare a stream |
| 4778 | * @codec: the HDA codec |
| 4779 | * @hinfo: PCM information |
| 4780 | * @stream: stream tag to assign |
| 4781 | * @format: format id to assign |
| 4782 | * @substream: PCM substream to assign |
| 4783 | * |
| 4784 | * Calls the prepare callback set by the codec with the given arguments. |
| 4785 | * Clean up the inactive streams when successful. |
| 4786 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4787 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4788 | struct hda_pcm_stream *hinfo, |
| 4789 | unsigned int stream, |
| 4790 | unsigned int format, |
| 4791 | struct snd_pcm_substream *substream) |
| 4792 | { |
| 4793 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4794 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4795 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4796 | if (ret >= 0) |
| 4797 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4798 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4799 | return ret; |
| 4800 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4801 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4802 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4803 | /** |
| 4804 | * snd_hda_codec_cleanup - Prepare a stream |
| 4805 | * @codec: the HDA codec |
| 4806 | * @hinfo: PCM information |
| 4807 | * @substream: PCM substream |
| 4808 | * |
| 4809 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4810 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4811 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4812 | struct hda_pcm_stream *hinfo, |
| 4813 | struct snd_pcm_substream *substream) |
| 4814 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4815 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4816 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4817 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4818 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4819 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4820 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4821 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4822 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4823 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4824 | }; |
| 4825 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4826 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4827 | * get the empty PCM device number to assign |
| 4828 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4829 | 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] | 4830 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4831 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4832 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4833 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4834 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4835 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4836 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4837 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4838 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4839 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4840 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4841 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4842 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4843 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4844 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4845 | return -EINVAL; |
| 4846 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4847 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4848 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4849 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4850 | if (audio_idx[type][i] >= 8) |
| 4851 | break; |
| 4852 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4853 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4854 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4855 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4856 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4857 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4858 | /* non-fixed slots starting from 10 */ |
| 4859 | for (i = 10; i < 32; i++) { |
| 4860 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4861 | return i; |
| 4862 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4863 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4864 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4865 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4866 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4867 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4868 | dev_warn(bus->card->dev, |
| 4869 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4870 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4871 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4875 | * attach a new PCM stream |
| 4876 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4877 | 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] | 4878 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4879 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4880 | struct hda_pcm_stream *info; |
| 4881 | int stream, err; |
| 4882 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4883 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4884 | return -EINVAL; |
| 4885 | for (stream = 0; stream < 2; stream++) { |
| 4886 | info = &pcm->stream[stream]; |
| 4887 | if (info->substreams) { |
| 4888 | err = set_pcm_default_values(codec, info); |
| 4889 | if (err < 0) |
| 4890 | return err; |
| 4891 | } |
| 4892 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4893 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4894 | } |
| 4895 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4896 | /* assign all PCMs of the given codec */ |
| 4897 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4898 | { |
| 4899 | unsigned int pcm; |
| 4900 | int err; |
| 4901 | |
| 4902 | if (!codec->num_pcms) { |
| 4903 | if (!codec->patch_ops.build_pcms) |
| 4904 | return 0; |
| 4905 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4906 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4907 | codec_err(codec, |
| 4908 | "cannot build PCMs for #%d (error %d)\n", |
| 4909 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4910 | err = snd_hda_codec_reset(codec); |
| 4911 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4912 | codec_err(codec, |
| 4913 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4914 | return err; |
| 4915 | } |
| 4916 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4917 | } |
| 4918 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4919 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4920 | int dev; |
| 4921 | |
| 4922 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4923 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4924 | |
| 4925 | if (!cpcm->pcm) { |
| 4926 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4927 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4928 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4929 | cpcm->device = dev; |
| 4930 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4931 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4932 | codec_err(codec, |
| 4933 | "cannot attach PCM stream %d for codec #%d\n", |
| 4934 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4935 | continue; /* no fatal error */ |
| 4936 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | return 0; |
| 4940 | } |
| 4941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | /** |
| 4943 | * snd_hda_build_pcms - build PCM information |
| 4944 | * @bus: the BUS |
| 4945 | * |
| 4946 | * Create PCM information for each codec included in the bus. |
| 4947 | * |
| 4948 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4949 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4950 | * to create its PCM instances. |
| 4951 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4952 | * callback. |
| 4953 | * |
| 4954 | * At least, substreams, channels_min and channels_max must be filled for |
| 4955 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4956 | * When rates and/or formats are zero, the supported values are queried |
| 4957 | * from the given nid. The nid is used also by the default ops.prepare |
| 4958 | * and ops.cleanup callbacks. |
| 4959 | * |
| 4960 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4961 | * ops.close is supposed to be called in the close callback. |
| 4962 | * ops.prepare should be called in the prepare or hw_params callback |
| 4963 | * with the proper parameters for set up. |
| 4964 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4965 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4966 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4968 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4970 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4972 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4973 | int err = snd_hda_codec_build_pcms(codec); |
| 4974 | if (err < 0) |
| 4975 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | } |
| 4977 | return 0; |
| 4978 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4979 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | * snd_hda_add_new_ctls - create controls from the array |
| 4983 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4984 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | * |
| 4986 | * This helper function creates and add new controls in the given array. |
| 4987 | * The array must be terminated with an empty entry as terminator. |
| 4988 | * |
| 4989 | * Returns 0 if successful, or a negative error code. |
| 4990 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4991 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4992 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4994 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | |
| 4996 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4997 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4998 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4999 | if (knew->iface == -1) /* skip this codec private value */ |
| 5000 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5001 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5002 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5003 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5004 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5005 | if (addr > 0) |
| 5006 | kctl->id.device = addr; |
| 5007 | if (idx > 0) |
| 5008 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 5009 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5010 | if (!err) |
| 5011 | break; |
| 5012 | /* try first with another device index corresponding to |
| 5013 | * the codec addr; if it still fails (or it's the |
| 5014 | * primary codec), then try another control index |
| 5015 | */ |
| 5016 | if (!addr && codec->addr) |
| 5017 | addr = codec->addr; |
| 5018 | else if (!idx && !knew->index) { |
| 5019 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 5020 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 5021 | if (idx <= 0) |
| 5022 | return err; |
| 5023 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 5024 | return err; |
| 5025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | } |
| 5027 | return 0; |
| 5028 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5029 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 5031 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5032 | static void hda_power_work(struct work_struct *work) |
| 5033 | { |
| 5034 | struct hda_codec *codec = |
| 5035 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 5036 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 5037 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5038 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5039 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5040 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 5041 | spin_unlock(&codec->power_lock); |
| 5042 | return; |
| 5043 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5044 | if (!codec->power_on || codec->power_count) { |
| 5045 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5046 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5047 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5048 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5049 | spin_unlock(&codec->power_lock); |
| 5050 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5051 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5052 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 5053 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | static void hda_keep_power_on(struct hda_codec *codec) |
| 5057 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5058 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5059 | codec->power_count++; |
| 5060 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5061 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5062 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5063 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5064 | } |
| 5065 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5066 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5067 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 5068 | { |
| 5069 | unsigned long delta = jiffies - codec->power_jiffies; |
| 5070 | if (codec->power_on) |
| 5071 | codec->power_on_acct += delta; |
| 5072 | else |
| 5073 | codec->power_off_acct += delta; |
| 5074 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5075 | } |
| 5076 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5077 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 5078 | * transition to D3 to complete. A pending D3 transition is indicated |
| 5079 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5080 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5081 | 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] | 5082 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5083 | /* Return if power_on or transitioning to power_on, unless currently |
| 5084 | * powering down. */ |
| 5085 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5086 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5087 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5088 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5089 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5090 | cancel_delayed_work_sync(&codec->power_work); |
| 5091 | |
| 5092 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5093 | /* If the power down delayed work was cancelled above before starting, |
| 5094 | * then there is no need to go through power up here. |
| 5095 | */ |
| 5096 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5097 | if (codec->power_transition < 0) |
| 5098 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5099 | return; |
| 5100 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5101 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5102 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5103 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5104 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5105 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5106 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5107 | spin_unlock(&codec->power_lock); |
| 5108 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5109 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5110 | |
| 5111 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5112 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5113 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5114 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5115 | #define power_save(codec) \ |
| 5116 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5117 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5118 | /* Transition to powered down */ |
| 5119 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5120 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5121 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5122 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5123 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5124 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5125 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5126 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5127 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5128 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | /** |
| 5132 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5133 | * @codec: HD-audio codec |
| 5134 | * @delta: the counter delta to change |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5135 | * @d3wait: sync for D3 transition complete |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5136 | * |
| 5137 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5138 | * appropriately. For the power-down, queue to the delayed action. |
| 5139 | * Passing zero to @delta means to synchronize the power state. |
| 5140 | */ |
| 5141 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5142 | { |
| 5143 | spin_lock(&codec->power_lock); |
| 5144 | codec->power_count += delta; |
| 5145 | trace_hda_power_count(codec); |
| 5146 | if (delta > 0) |
| 5147 | __snd_hda_power_up(codec, d3wait); |
| 5148 | else |
| 5149 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5150 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5151 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5152 | EXPORT_SYMBOL_GPL(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5153 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5154 | /** |
| 5155 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5156 | * @codec: HD-audio codec |
| 5157 | * @check: the object containing an AMP list and the status |
| 5158 | * @nid: NID to check / update |
| 5159 | * |
| 5160 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5161 | * check the current AMP status, and update the the power-status according |
| 5162 | * to the mute status. |
| 5163 | * |
| 5164 | * This function is supposed to be set or called from the check_power_status |
| 5165 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5166 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5167 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5168 | struct hda_loopback_check *check, |
| 5169 | hda_nid_t nid) |
| 5170 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5171 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5172 | int ch, v; |
| 5173 | |
| 5174 | if (!check->amplist) |
| 5175 | return 0; |
| 5176 | for (p = check->amplist; p->nid; p++) { |
| 5177 | if (p->nid == nid) |
| 5178 | break; |
| 5179 | } |
| 5180 | if (!p->nid) |
| 5181 | return 0; /* nothing changed */ |
| 5182 | |
| 5183 | for (p = check->amplist; p->nid; p++) { |
| 5184 | for (ch = 0; ch < 2; ch++) { |
| 5185 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5186 | p->idx); |
| 5187 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5188 | if (!check->power_on) { |
| 5189 | check->power_on = 1; |
| 5190 | snd_hda_power_up(codec); |
| 5191 | } |
| 5192 | return 1; |
| 5193 | } |
| 5194 | } |
| 5195 | } |
| 5196 | if (check->power_on) { |
| 5197 | check->power_on = 0; |
| 5198 | snd_hda_power_down(codec); |
| 5199 | } |
| 5200 | return 0; |
| 5201 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5202 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5203 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5205 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5206 | * Channel mode helper |
| 5207 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5208 | |
| 5209 | /** |
| 5210 | * 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] | 5211 | * @codec: the HDA codec |
| 5212 | * @uinfo: pointer to get/store the data |
| 5213 | * @chmode: channel mode array |
| 5214 | * @num_chmodes: channel mode array size |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5215 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5216 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5217 | struct snd_ctl_elem_info *uinfo, |
| 5218 | const struct hda_channel_mode *chmode, |
| 5219 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5220 | { |
| 5221 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5222 | uinfo->count = 1; |
| 5223 | uinfo->value.enumerated.items = num_chmodes; |
| 5224 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5225 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5226 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5227 | chmode[uinfo->value.enumerated.item].channels); |
| 5228 | return 0; |
| 5229 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5230 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5231 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5232 | /** |
| 5233 | * 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] | 5234 | * @codec: the HDA codec |
| 5235 | * @ucontrol: pointer to get/store the data |
| 5236 | * @chmode: channel mode array |
| 5237 | * @num_chmodes: channel mode array size |
| 5238 | * @max_channels: max number of channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5239 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5240 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5241 | struct snd_ctl_elem_value *ucontrol, |
| 5242 | const struct hda_channel_mode *chmode, |
| 5243 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5244 | int max_channels) |
| 5245 | { |
| 5246 | int i; |
| 5247 | |
| 5248 | for (i = 0; i < num_chmodes; i++) { |
| 5249 | if (max_channels == chmode[i].channels) { |
| 5250 | ucontrol->value.enumerated.item[0] = i; |
| 5251 | break; |
| 5252 | } |
| 5253 | } |
| 5254 | return 0; |
| 5255 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5256 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5257 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5258 | /** |
| 5259 | * 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] | 5260 | * @codec: the HDA codec |
| 5261 | * @ucontrol: pointer to get/store the data |
| 5262 | * @chmode: channel mode array |
| 5263 | * @num_chmodes: channel mode array size |
| 5264 | * @max_channelsp: pointer to store the max channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5266 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5267 | struct snd_ctl_elem_value *ucontrol, |
| 5268 | const struct hda_channel_mode *chmode, |
| 5269 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5270 | int *max_channelsp) |
| 5271 | { |
| 5272 | unsigned int mode; |
| 5273 | |
| 5274 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5275 | if (mode >= num_chmodes) |
| 5276 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5277 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5278 | return 0; |
| 5279 | /* change the current channel setting */ |
| 5280 | *max_channelsp = chmode[mode].channels; |
| 5281 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5282 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5283 | return 1; |
| 5284 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5285 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | /* |
| 5288 | * input MUX helper |
| 5289 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5290 | |
| 5291 | /** |
| 5292 | * 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] | 5293 | * @imux: imux helper object |
| 5294 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5295 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5296 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5297 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | { |
| 5299 | unsigned int index; |
| 5300 | |
| 5301 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5302 | uinfo->count = 1; |
| 5303 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5304 | if (!imux->num_items) |
| 5305 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | index = uinfo->value.enumerated.item; |
| 5307 | if (index >= imux->num_items) |
| 5308 | index = imux->num_items - 1; |
| 5309 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5310 | return 0; |
| 5311 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5312 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5314 | /** |
| 5315 | * 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] | 5316 | * @codec: the HDA codec |
| 5317 | * @imux: imux helper object |
| 5318 | * @ucontrol: pointer to get/store the data |
| 5319 | * @nid: input mux NID |
| 5320 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5321 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5322 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5323 | const struct hda_input_mux *imux, |
| 5324 | struct snd_ctl_elem_value *ucontrol, |
| 5325 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | unsigned int *cur_val) |
| 5327 | { |
| 5328 | unsigned int idx; |
| 5329 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5330 | if (!imux->num_items) |
| 5331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | idx = ucontrol->value.enumerated.item[0]; |
| 5333 | if (idx >= imux->num_items) |
| 5334 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5335 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5337 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5338 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | *cur_val = idx; |
| 5340 | return 1; |
| 5341 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5342 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | |
| 5344 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5345 | /** |
| 5346 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 5347 | * @kcontrol: ctl element |
| 5348 | * @uinfo: pointer to get/store the data |
| 5349 | * @num_items: number of enum items |
| 5350 | * @texts: enum item string array |
| 5351 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5352 | * process kcontrol info callback of a simple string enum array |
| 5353 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5354 | */ |
| 5355 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5356 | struct snd_ctl_elem_info *uinfo, |
| 5357 | int num_items, const char * const *texts) |
| 5358 | { |
| 5359 | static const char * const texts_default[] = { |
| 5360 | "Disabled", "Enabled" |
| 5361 | }; |
| 5362 | |
| 5363 | if (!texts || !num_items) { |
| 5364 | num_items = 2; |
| 5365 | texts = texts_default; |
| 5366 | } |
| 5367 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 5368 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5369 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5370 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5371 | |
| 5372 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | * Multi-channel / digital-out PCM helper functions |
| 5374 | */ |
| 5375 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5376 | /* setup SPDIF output stream */ |
| 5377 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5378 | unsigned int stream_tag, unsigned int format) |
| 5379 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5380 | struct hda_spdif_out *spdif; |
| 5381 | unsigned int curr_fmt; |
| 5382 | bool reset; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5383 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5384 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5385 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5386 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5387 | reset = codec->spdif_status_reset && |
| 5388 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5389 | curr_fmt != format; |
| 5390 | |
| 5391 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5392 | updated */ |
| 5393 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5394 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5395 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5396 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5397 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5398 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5399 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5400 | for (d = codec->slave_dig_outs; *d; d++) |
| 5401 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5402 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5403 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5404 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5405 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5406 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5407 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5408 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5409 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5410 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5411 | { |
| 5412 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5413 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5414 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5415 | for (d = codec->slave_dig_outs; *d; d++) |
| 5416 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5417 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5418 | } |
| 5419 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5420 | /** |
| 5421 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5422 | * @bus: HD-audio bus |
| 5423 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5424 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5425 | { |
| 5426 | struct hda_codec *codec; |
| 5427 | |
| 5428 | if (!bus) |
| 5429 | return; |
| 5430 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5431 | if (hda_codec_is_power_on(codec) && |
| 5432 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5433 | codec->patch_ops.reboot_notify(codec); |
| 5434 | } |
| 5435 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5436 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5437 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5438 | /** |
| 5439 | * 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] | 5440 | * @codec: the HDA codec |
| 5441 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5443 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5444 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5446 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5447 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5448 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5449 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5451 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | return 0; |
| 5453 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5454 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5456 | /** |
| 5457 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5458 | * @codec: the HDA codec |
| 5459 | * @mout: hda_multi_out object |
| 5460 | * @stream_tag: stream tag to assign |
| 5461 | * @format: format id to assign |
| 5462 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5463 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5464 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5465 | struct hda_multi_out *mout, |
| 5466 | unsigned int stream_tag, |
| 5467 | unsigned int format, |
| 5468 | struct snd_pcm_substream *substream) |
| 5469 | { |
| 5470 | mutex_lock(&codec->spdif_mutex); |
| 5471 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5472 | mutex_unlock(&codec->spdif_mutex); |
| 5473 | return 0; |
| 5474 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5475 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5476 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5477 | /** |
| 5478 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5479 | * @codec: the HDA codec |
| 5480 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5481 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5482 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5483 | struct hda_multi_out *mout) |
| 5484 | { |
| 5485 | mutex_lock(&codec->spdif_mutex); |
| 5486 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5487 | mutex_unlock(&codec->spdif_mutex); |
| 5488 | return 0; |
| 5489 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5490 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5491 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5492 | /** |
| 5493 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5494 | * @codec: the HDA codec |
| 5495 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5497 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5498 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5500 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5502 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | return 0; |
| 5504 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5505 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5507 | /** |
| 5508 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5509 | * @codec: the HDA codec |
| 5510 | * @mout: hda_multi_out object |
| 5511 | * @substream: PCM substream to assign |
| 5512 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5513 | * |
| 5514 | * Open analog outputs and set up the hw-constraints. |
| 5515 | * If the digital outputs can be opened as slave, open the digital |
| 5516 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5518 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5519 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5520 | struct snd_pcm_substream *substream, |
| 5521 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5523 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5524 | runtime->hw.channels_max = mout->max_channels; |
| 5525 | if (mout->dig_out_nid) { |
| 5526 | if (!mout->analog_rates) { |
| 5527 | mout->analog_rates = hinfo->rates; |
| 5528 | mout->analog_formats = hinfo->formats; |
| 5529 | mout->analog_maxbps = hinfo->maxbps; |
| 5530 | } else { |
| 5531 | runtime->hw.rates = mout->analog_rates; |
| 5532 | runtime->hw.formats = mout->analog_formats; |
| 5533 | hinfo->maxbps = mout->analog_maxbps; |
| 5534 | } |
| 5535 | if (!mout->spdif_rates) { |
| 5536 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5537 | &mout->spdif_rates, |
| 5538 | &mout->spdif_formats, |
| 5539 | &mout->spdif_maxbps); |
| 5540 | } |
| 5541 | mutex_lock(&codec->spdif_mutex); |
| 5542 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5543 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5544 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5545 | runtime->hw.rates &= mout->spdif_rates; |
| 5546 | runtime->hw.formats &= mout->spdif_formats; |
| 5547 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5548 | hinfo->maxbps = mout->spdif_maxbps; |
| 5549 | } else { |
| 5550 | mout->share_spdif = 0; |
| 5551 | /* FIXME: need notify? */ |
| 5552 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5553 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5554 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5557 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5558 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5559 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5561 | /** |
| 5562 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5563 | * @codec: the HDA codec |
| 5564 | * @mout: hda_multi_out object |
| 5565 | * @stream_tag: stream tag to assign |
| 5566 | * @format: format id to assign |
| 5567 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5568 | * |
| 5569 | * Set up the i/o for analog out. |
| 5570 | * 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] | 5571 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5572 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5573 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | unsigned int stream_tag, |
| 5575 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5576 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5578 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5580 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | int i; |
| 5582 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5583 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5584 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5585 | if (mout->dig_out_nid && mout->share_spdif && |
| 5586 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5588 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5589 | format) && |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5590 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5592 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5593 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | } else { |
| 5595 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5596 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | } |
| 5598 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5599 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | |
| 5601 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5602 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5603 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5604 | if (!mout->no_share_stream && |
| 5605 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5607 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5608 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5609 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5610 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5611 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5612 | snd_hda_codec_setup_stream(codec, |
| 5613 | mout->hp_out_nid[i], |
| 5614 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | /* surrounds */ |
| 5617 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5618 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5619 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5620 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5621 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5622 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5623 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5625 | |
| 5626 | /* extra surrounds */ |
| 5627 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5628 | int ch = 0; |
| 5629 | if (!mout->extra_out_nid[i]) |
| 5630 | break; |
| 5631 | if (chs >= (i + 1) * 2) |
| 5632 | ch = i * 2; |
| 5633 | else if (!mout->no_share_stream) |
| 5634 | break; |
| 5635 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5636 | stream_tag, ch, format); |
| 5637 | } |
| 5638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | return 0; |
| 5640 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5641 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5643 | /** |
| 5644 | * 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] | 5645 | * @codec: the HDA codec |
| 5646 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5648 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5649 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5651 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | int i; |
| 5653 | |
| 5654 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5655 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5657 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5658 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5659 | if (mout->hp_out_nid[i]) |
| 5660 | snd_hda_codec_cleanup_stream(codec, |
| 5661 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5662 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5663 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5664 | snd_hda_codec_cleanup_stream(codec, |
| 5665 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5666 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | 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] | 5668 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | mout->dig_out_used = 0; |
| 5670 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5671 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | return 0; |
| 5673 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5674 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5676 | /** |
| 5677 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5678 | * @codec: the HDA codec |
| 5679 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5680 | * |
| 5681 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5682 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5683 | */ |
| 5684 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5685 | { |
| 5686 | unsigned int pincap; |
| 5687 | unsigned int oldval; |
| 5688 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5689 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5690 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5691 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5692 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5693 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5694 | return AC_PINCTL_VREF_80; |
| 5695 | else if (pincap & AC_PINCAP_VREF_50) |
| 5696 | return AC_PINCTL_VREF_50; |
| 5697 | else if (pincap & AC_PINCAP_VREF_100) |
| 5698 | return AC_PINCTL_VREF_100; |
| 5699 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5700 | return AC_PINCTL_VREF_GRD; |
| 5701 | return AC_PINCTL_VREF_HIZ; |
| 5702 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5703 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5704 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5705 | /** |
| 5706 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5707 | * @codec: the HDA codec |
| 5708 | * @pin: referred pin NID |
| 5709 | * @val: pin ctl value to audit |
| 5710 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5711 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5712 | hda_nid_t pin, unsigned int val) |
| 5713 | { |
| 5714 | static unsigned int cap_lists[][2] = { |
| 5715 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5716 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5717 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5718 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5719 | }; |
| 5720 | unsigned int cap; |
| 5721 | |
| 5722 | if (!val) |
| 5723 | return 0; |
| 5724 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5725 | if (!cap) |
| 5726 | return val; /* don't know what to do... */ |
| 5727 | |
| 5728 | if (val & AC_PINCTL_OUT_EN) { |
| 5729 | if (!(cap & AC_PINCAP_OUT)) |
| 5730 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5731 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5732 | val &= ~AC_PINCTL_HP_EN; |
| 5733 | } |
| 5734 | |
| 5735 | if (val & AC_PINCTL_IN_EN) { |
| 5736 | if (!(cap & AC_PINCAP_IN)) |
| 5737 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5738 | else { |
| 5739 | unsigned int vcap, vref; |
| 5740 | int i; |
| 5741 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5742 | vref = val & AC_PINCTL_VREFEN; |
| 5743 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5744 | if (vref == cap_lists[i][0] && |
| 5745 | !(vcap & cap_lists[i][1])) { |
| 5746 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5747 | vref = AC_PINCTL_VREF_HIZ; |
| 5748 | else |
| 5749 | vref = cap_lists[i + 1][0]; |
| 5750 | } |
| 5751 | } |
| 5752 | val &= ~AC_PINCTL_VREFEN; |
| 5753 | val |= vref; |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | return val; |
| 5758 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5759 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5760 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5761 | /** |
| 5762 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5763 | * @codec: the HDA codec |
| 5764 | * @pin: referred pin NID |
| 5765 | * @val: pin control value to set |
| 5766 | * @cached: access over codec pinctl cache or direct write |
| 5767 | * |
| 5768 | * This function is a helper to set a pin ctl value more safely. |
| 5769 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5770 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5771 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5772 | * snd_hda_codec_write() depending on @cached flag. |
| 5773 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5774 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5775 | unsigned int val, bool cached) |
| 5776 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5777 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5778 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5779 | if (cached) |
| 5780 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5781 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5782 | else |
| 5783 | return snd_hda_codec_write(codec, pin, 0, |
| 5784 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5785 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5786 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5787 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5788 | /** |
| 5789 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5790 | * @codec: the HDA codec |
| 5791 | * @imux: imux helper object |
| 5792 | * @label: the name of imux item to assign |
| 5793 | * @index: index number of imux item to assign |
| 5794 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5795 | * |
| 5796 | * When the same label is used already in the existing items, the number |
| 5797 | * suffix is appended to the label. This label index number is stored |
| 5798 | * to type_idx when non-NULL pointer is given. |
| 5799 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5800 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5801 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5802 | int index, int *type_idx) |
| 5803 | { |
| 5804 | int i, label_idx = 0; |
| 5805 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5806 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5807 | return -EINVAL; |
| 5808 | } |
| 5809 | for (i = 0; i < imux->num_items; i++) { |
| 5810 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5811 | label_idx++; |
| 5812 | } |
| 5813 | if (type_idx) |
| 5814 | *type_idx = label_idx; |
| 5815 | if (label_idx > 0) |
| 5816 | snprintf(imux->items[imux->num_items].label, |
| 5817 | sizeof(imux->items[imux->num_items].label), |
| 5818 | "%s %d", label, label_idx); |
| 5819 | else |
| 5820 | strlcpy(imux->items[imux->num_items].label, label, |
| 5821 | sizeof(imux->items[imux->num_items].label)); |
| 5822 | imux->items[imux->num_items].index = index; |
| 5823 | imux->num_items++; |
| 5824 | return 0; |
| 5825 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5826 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5827 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | #ifdef CONFIG_PM |
| 5830 | /* |
| 5831 | * power management |
| 5832 | */ |
| 5833 | |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5834 | |
| 5835 | static void hda_async_suspend(void *data, async_cookie_t cookie) |
| 5836 | { |
| 5837 | hda_call_codec_suspend(data, false); |
| 5838 | } |
| 5839 | |
| 5840 | static void hda_async_resume(void *data, async_cookie_t cookie) |
| 5841 | { |
| 5842 | hda_call_codec_resume(data); |
| 5843 | } |
| 5844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | /** |
| 5846 | * snd_hda_suspend - suspend the codecs |
| 5847 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | * |
| 5849 | * Returns 0 if successful. |
| 5850 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5851 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5853 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5854 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5856 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5857 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5858 | if (hda_codec_is_power_on(codec)) { |
| 5859 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5860 | async_schedule_domain(hda_async_suspend, codec, |
| 5861 | &domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5862 | else |
| 5863 | hda_call_codec_suspend(codec, false); |
| 5864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5866 | |
| 5867 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5868 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | return 0; |
| 5871 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5872 | EXPORT_SYMBOL_GPL(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | |
| 5874 | /** |
| 5875 | * snd_hda_resume - resume the codecs |
| 5876 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | * |
| 5878 | * Returns 0 if successful. |
| 5879 | */ |
| 5880 | int snd_hda_resume(struct hda_bus *bus) |
| 5881 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5882 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5883 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5885 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5886 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5887 | async_schedule_domain(hda_async_resume, codec, &domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5888 | else |
| 5889 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5891 | |
| 5892 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5893 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | return 0; |
| 5896 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5897 | EXPORT_SYMBOL_GPL(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5898 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5899 | |
| 5900 | /* |
| 5901 | * generic arrays |
| 5902 | */ |
| 5903 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5904 | /** |
| 5905 | * snd_array_new - get a new element from the given array |
| 5906 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5907 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5908 | * Get a new element from the given array. If it exceeds the |
| 5909 | * pre-allocated array size, re-allocate the array. |
| 5910 | * |
| 5911 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5912 | */ |
| 5913 | void *snd_array_new(struct snd_array *array) |
| 5914 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5915 | if (snd_BUG_ON(!array->elem_size)) |
| 5916 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5917 | if (array->used >= array->alloced) { |
| 5918 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5919 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5920 | void *nlist; |
| 5921 | if (snd_BUG_ON(num >= 4096)) |
| 5922 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5923 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5924 | if (!nlist) |
| 5925 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5926 | array->list = nlist; |
| 5927 | array->alloced = num; |
| 5928 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5929 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5930 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5931 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5932 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5933 | /** |
| 5934 | * snd_array_free - free the given array elements |
| 5935 | * @array: the array object |
| 5936 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5937 | void snd_array_free(struct snd_array *array) |
| 5938 | { |
| 5939 | kfree(array->list); |
| 5940 | array->used = 0; |
| 5941 | array->alloced = 0; |
| 5942 | array->list = NULL; |
| 5943 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5944 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5945 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5946 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5947 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5948 | * @pcm: PCM caps bits |
| 5949 | * @buf: the string buffer to write |
| 5950 | * @buflen: the max buffer length |
| 5951 | * |
| 5952 | * used by hda_proc.c and hda_eld.c |
| 5953 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5954 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5955 | { |
| 5956 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5957 | int i, j; |
| 5958 | |
| 5959 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5960 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5961 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5962 | |
| 5963 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5964 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5965 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5966 | |
| 5967 | MODULE_DESCRIPTION("HDA codec core"); |
| 5968 | MODULE_LICENSE("GPL"); |