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 | |
| 80 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 81 | { |
| 82 | mutex_lock(&preset_mutex); |
| 83 | list_add_tail(&preset->list, &hda_preset_tables); |
| 84 | mutex_unlock(&preset_mutex); |
| 85 | return 0; |
| 86 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 88 | |
| 89 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 90 | { |
| 91 | mutex_lock(&preset_mutex); |
| 92 | list_del(&preset->list); |
| 93 | mutex_unlock(&preset_mutex); |
| 94 | return 0; |
| 95 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_PM |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 99 | #define codec_in_pm(codec) ((codec)->in_pm) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 100 | static void hda_power_work(struct work_struct *work); |
| 101 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 102 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 103 | |
| 104 | static void hda_call_pm_notify(struct hda_codec *codec, bool power_up) |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 105 | { |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 106 | struct hda_bus *bus = codec->bus; |
| 107 | |
| 108 | if ((power_up && codec->pm_up_notified) || |
| 109 | (!power_up && !codec->pm_up_notified)) |
| 110 | return; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 111 | if (bus->ops.pm_notify) |
| 112 | bus->ops.pm_notify(bus, power_up); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 113 | codec->pm_up_notified = power_up; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 114 | } |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 115 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 116 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 117 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 118 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 119 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 120 | #define hda_call_pm_notify(codec, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 121 | #endif |
| 122 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 123 | /** |
| 124 | * snd_hda_get_jack_location - Give a location string of the jack |
| 125 | * @cfg: pin default config value |
| 126 | * |
| 127 | * Parse the pin default config value and returns the string of the |
| 128 | * jack location, e.g. "Rear", "Front", etc. |
| 129 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 130 | const char *snd_hda_get_jack_location(u32 cfg) |
| 131 | { |
| 132 | static char *bases[7] = { |
| 133 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 134 | }; |
| 135 | static unsigned char specials_idx[] = { |
| 136 | 0x07, 0x08, |
| 137 | 0x17, 0x18, 0x19, |
| 138 | 0x37, 0x38 |
| 139 | }; |
| 140 | static char *specials[] = { |
| 141 | "Rear Panel", "Drive Bar", |
| 142 | "Riser", "HDMI", "ATAPI", |
| 143 | "Mobile-In", "Mobile-Out" |
| 144 | }; |
| 145 | int i; |
| 146 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 147 | if ((cfg & 0x0f) < 7) |
| 148 | return bases[cfg & 0x0f]; |
| 149 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 150 | if (cfg == specials_idx[i]) |
| 151 | return specials[i]; |
| 152 | } |
| 153 | return "UNKNOWN"; |
| 154 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 155 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 156 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 157 | /** |
| 158 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 159 | * @cfg: pin default config value |
| 160 | * |
| 161 | * Parse the pin default config value and returns the string of the |
| 162 | * jack connectivity, i.e. external or internal connection. |
| 163 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 164 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 165 | { |
| 166 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 167 | |
| 168 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 169 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 171 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 172 | /** |
| 173 | * snd_hda_get_jack_type - Give a type string of the jack |
| 174 | * @cfg: pin default config value |
| 175 | * |
| 176 | * Parse the pin default config value and returns the string of the |
| 177 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 178 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 179 | const char *snd_hda_get_jack_type(u32 cfg) |
| 180 | { |
| 181 | static char *jack_types[16] = { |
| 182 | "Line Out", "Speaker", "HP Out", "CD", |
| 183 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 184 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 185 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 189 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 190 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 192 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 195 | */ |
| 196 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 197 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags, |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 198 | unsigned int verb, unsigned int parm) |
| 199 | { |
| 200 | u32 val; |
| 201 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 202 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 203 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 204 | 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] | 205 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 206 | return ~0; |
| 207 | } |
| 208 | |
| 209 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 210 | val |= (u32)nid << 20; |
| 211 | val |= verb << 8; |
| 212 | val |= parm; |
| 213 | return val; |
| 214 | } |
| 215 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Send and receive a verb |
| 218 | */ |
| 219 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 220 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 221 | { |
| 222 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 223 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 224 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 225 | if (cmd == ~0) |
| 226 | return -1; |
| 227 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 228 | if (res) |
| 229 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 230 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 231 | snd_hda_power_up(codec); |
| 232 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 233 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 234 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 235 | for (;;) { |
| 236 | trace_hda_send_cmd(codec, cmd); |
| 237 | err = bus->ops.command(bus, cmd); |
| 238 | if (err != -EAGAIN) |
| 239 | break; |
| 240 | /* process pending verbs */ |
| 241 | bus->ops.get_response(bus, codec->addr); |
| 242 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 243 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 244 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 245 | trace_hda_get_response(codec, *res); |
| 246 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 247 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 248 | mutex_unlock(&bus->cmd_mutex); |
| 249 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 250 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 251 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 252 | codec_dbg(codec, |
| 253 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 254 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 255 | bus->ops.bus_reset(bus); |
| 256 | } |
| 257 | goto again; |
| 258 | } |
| 259 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 260 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 261 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 262 | return err; |
| 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /** |
| 266 | * snd_hda_codec_read - send a command and get the response |
| 267 | * @codec: the HDA codec |
| 268 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 269 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * @verb: the verb to send |
| 271 | * @parm: the parameter for the verb |
| 272 | * |
| 273 | * Send a single command and read the corresponding response. |
| 274 | * |
| 275 | * Returns the obtained response value, or -1 for an error. |
| 276 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 277 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 278 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | unsigned int verb, unsigned int parm) |
| 280 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 281 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 282 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 283 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 284 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return res; |
| 286 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 287 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * snd_hda_codec_write - send a single command without waiting for response |
| 291 | * @codec: the HDA codec |
| 292 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 293 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | * @verb: the verb to send |
| 295 | * @parm: the parameter for the verb |
| 296 | * |
| 297 | * Send a single command without waiting for response. |
| 298 | * |
| 299 | * Returns 0 if successful, or a negative error code. |
| 300 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 301 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 302 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 304 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 305 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 306 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 307 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * snd_hda_sequence_write - sequence writes |
| 313 | * @codec: the HDA codec |
| 314 | * @seq: VERB array to send |
| 315 | * |
| 316 | * Send the commands sequentially from the given array. |
| 317 | * The array must be terminated with NID=0. |
| 318 | */ |
| 319 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 320 | { |
| 321 | for (; seq->nid; seq++) |
| 322 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 323 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 324 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 328 | * @codec: the HDA codec |
| 329 | * @nid: NID to parse |
| 330 | * @start_id: the pointer to store the start NID |
| 331 | * |
| 332 | * Parse the NID and store the start NID of its sub-nodes. |
| 333 | * Returns the number of sub-nodes. |
| 334 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 335 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 336 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | unsigned int parm; |
| 339 | |
| 340 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 341 | if (parm == -1) |
| 342 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | *start_id = (parm >> 16) & 0x7fff; |
| 344 | return (int)(parm & 0x7fff); |
| 345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 348 | /* connection list element */ |
| 349 | struct hda_conn_list { |
| 350 | struct list_head list; |
| 351 | int len; |
| 352 | hda_nid_t nid; |
| 353 | hda_nid_t conns[0]; |
| 354 | }; |
| 355 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 356 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 357 | static struct hda_conn_list * |
| 358 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 359 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 360 | struct hda_conn_list *p; |
| 361 | list_for_each_entry(p, &codec->conn_list, list) { |
| 362 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 363 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 364 | } |
| 365 | return NULL; |
| 366 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 367 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 368 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 369 | const hda_nid_t *list) |
| 370 | { |
| 371 | struct hda_conn_list *p; |
| 372 | |
| 373 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 374 | if (!p) |
| 375 | return -ENOMEM; |
| 376 | p->len = len; |
| 377 | p->nid = nid; |
| 378 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 379 | list_add(&p->list, &codec->conn_list); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static void remove_conn_list(struct hda_codec *codec) |
| 384 | { |
| 385 | while (!list_empty(&codec->conn_list)) { |
| 386 | struct hda_conn_list *p; |
| 387 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 388 | list_del(&p->list); |
| 389 | kfree(p); |
| 390 | } |
| 391 | } |
| 392 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 393 | /* read the connection and add to the cache */ |
| 394 | static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 395 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 396 | hda_nid_t list[32]; |
| 397 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 398 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 399 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 400 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 401 | if (len == -ENOSPC) { |
| 402 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 403 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 404 | if (!result) |
| 405 | return -ENOMEM; |
| 406 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 407 | } |
| 408 | if (len >= 0) |
| 409 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 410 | if (result != list) |
| 411 | kfree(result); |
| 412 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 413 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 414 | |
| 415 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 416 | * snd_hda_get_conn_list - get connection list |
| 417 | * @codec: the HDA codec |
| 418 | * @nid: NID to parse |
| 419 | * @len: number of connection list entries |
| 420 | * @listp: the pointer to store NID list |
| 421 | * |
| 422 | * Parses the connection list of the given widget and stores the pointer |
| 423 | * to the list of NIDs. |
| 424 | * |
| 425 | * Returns the number of connections, or a negative error code. |
| 426 | * |
| 427 | * Note that the returned pointer isn't protected against the list |
| 428 | * modification. If snd_hda_override_conn_list() might be called |
| 429 | * concurrently, protect with a mutex appropriately. |
| 430 | */ |
| 431 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 432 | const hda_nid_t **listp) |
| 433 | { |
| 434 | bool added = false; |
| 435 | |
| 436 | for (;;) { |
| 437 | int err; |
| 438 | const struct hda_conn_list *p; |
| 439 | |
| 440 | /* if the connection-list is already cached, read it */ |
| 441 | p = lookup_conn_list(codec, nid); |
| 442 | if (p) { |
| 443 | if (listp) |
| 444 | *listp = p->conns; |
| 445 | return p->len; |
| 446 | } |
| 447 | if (snd_BUG_ON(added)) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | err = read_and_add_raw_conns(codec, nid); |
| 451 | if (err < 0) |
| 452 | return err; |
| 453 | added = true; |
| 454 | } |
| 455 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 457 | |
| 458 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 459 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | * @codec: the HDA codec |
| 461 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 462 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * @max_conns: max. number of connections to store |
| 464 | * |
| 465 | * Parses the connection list of the given widget and stores the list |
| 466 | * of NIDs. |
| 467 | * |
| 468 | * Returns the number of connections, or a negative error code. |
| 469 | */ |
| 470 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 471 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 472 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 473 | const hda_nid_t *list; |
| 474 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 475 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 476 | if (len > 0 && conn_list) { |
| 477 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 478 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 479 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 480 | return -EINVAL; |
| 481 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 482 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 483 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 484 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 485 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 486 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 488 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 489 | /* return CONNLIST_LEN parameter of the given widget */ |
| 490 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 491 | { |
| 492 | unsigned int wcaps = get_wcaps(codec, nid); |
| 493 | unsigned int parm; |
| 494 | |
| 495 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 496 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 497 | return 0; |
| 498 | |
| 499 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 500 | if (parm == -1) |
| 501 | parm = 0; |
| 502 | return parm; |
| 503 | } |
| 504 | |
| 505 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 506 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 507 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 510 | /** |
| 511 | * snd_hda_get_raw_connections - copy connection list without cache |
| 512 | * @codec: the HDA codec |
| 513 | * @nid: NID to parse |
| 514 | * @conn_list: connection list array |
| 515 | * @max_conns: max. number of connections to store |
| 516 | * |
| 517 | * Like snd_hda_get_connections(), copy the connection list but without |
| 518 | * checking through the connection-list cache. |
| 519 | * Currently called only from hda_proc.c, so not exported. |
| 520 | */ |
| 521 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 522 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 525 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 527 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 528 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 530 | parm = get_num_conns(codec, nid); |
| 531 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 532 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (parm & AC_CLIST_LONG) { |
| 535 | /* long form */ |
| 536 | shift = 16; |
| 537 | num_elems = 2; |
| 538 | } else { |
| 539 | /* short form */ |
| 540 | shift = 8; |
| 541 | num_elems = 4; |
| 542 | } |
| 543 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | mask = (1 << (shift-1)) - 1; |
| 545 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 546 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return 0; /* no connection */ |
| 548 | |
| 549 | if (conn_len == 1) { |
| 550 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 551 | parm = snd_hda_codec_read(codec, nid, 0, |
| 552 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 553 | if (parm == -1 && codec->bus->rirb_error) |
| 554 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 555 | if (conn_list) |
| 556 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | /* multi connection */ |
| 561 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 562 | prev_nid = 0; |
| 563 | for (i = 0; i < conn_len; i++) { |
| 564 | int range_val; |
| 565 | hda_nid_t val, n; |
| 566 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 567 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 568 | parm = snd_hda_codec_read(codec, nid, 0, |
| 569 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 570 | if (parm == -1 && codec->bus->rirb_error) |
| 571 | return -EIO; |
| 572 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 573 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 574 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 575 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 576 | codec_dbg(codec, |
| 577 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 578 | nid, i, parm); |
| 579 | return 0; |
| 580 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 581 | parm >>= shift; |
| 582 | if (range_val) { |
| 583 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 584 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 585 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 586 | "invalid dep_range_val %x:%x\n", |
| 587 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 588 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 590 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 591 | if (conn_list) { |
| 592 | if (conns >= max_conns) |
| 593 | return -ENOSPC; |
| 594 | conn_list[conns] = n; |
| 595 | } |
| 596 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 597 | } |
| 598 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 599 | if (conn_list) { |
| 600 | if (conns >= max_conns) |
| 601 | return -ENOSPC; |
| 602 | conn_list[conns] = val; |
| 603 | } |
| 604 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 606 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
| 608 | return conns; |
| 609 | } |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 612 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 613 | * @codec: the HDA codec |
| 614 | * @nid: NID to parse |
| 615 | * @len: number of connection list entries |
| 616 | * @list: the list of connection entries |
| 617 | * |
| 618 | * Add or modify the given connection-list to the cache. If the corresponding |
| 619 | * cache already exists, invalidate it and append a new one. |
| 620 | * |
| 621 | * Returns zero or a negative error code. |
| 622 | */ |
| 623 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 624 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 626 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 627 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 628 | p = lookup_conn_list(codec, nid); |
| 629 | if (p) { |
| 630 | list_del(&p->list); |
| 631 | kfree(p); |
| 632 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 633 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 634 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 637 | |
| 638 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 639 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 640 | * @codec: the HDA codec |
| 641 | * @mux: NID containing the list |
| 642 | * @nid: NID to select |
| 643 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 644 | * |
| 645 | * Parses the connection list of the widget @mux and checks whether the |
| 646 | * widget @nid is present. If it is, return the connection index. |
| 647 | * Otherwise it returns -1. |
| 648 | */ |
| 649 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 650 | hda_nid_t nid, int recursive) |
| 651 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 652 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 653 | int i, nums; |
| 654 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 655 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 656 | for (i = 0; i < nums; i++) |
| 657 | if (conn[i] == nid) |
| 658 | return i; |
| 659 | if (!recursive) |
| 660 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 661 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 662 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 666 | for (i = 0; i < nums; i++) { |
| 667 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 668 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 669 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 670 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 671 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 672 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 673 | return -1; |
| 674 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 675 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 677 | |
| 678 | /* return DEVLIST_LEN parameter of the given widget */ |
| 679 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 680 | { |
| 681 | unsigned int wcaps = get_wcaps(codec, nid); |
| 682 | unsigned int parm; |
| 683 | |
| 684 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 685 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 686 | return 0; |
| 687 | |
| 688 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 689 | if (parm == -1 && codec->bus->rirb_error) |
| 690 | parm = 0; |
| 691 | return parm & AC_DEV_LIST_LEN_MASK; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * snd_hda_get_devices - copy device list without cache |
| 696 | * @codec: the HDA codec |
| 697 | * @nid: NID of the pin to parse |
| 698 | * @dev_list: device list array |
| 699 | * @max_devices: max. number of devices to store |
| 700 | * |
| 701 | * Copy the device list. This info is dynamic and so not cached. |
| 702 | * Currently called only from hda_proc.c, so not exported. |
| 703 | */ |
| 704 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 705 | u8 *dev_list, int max_devices) |
| 706 | { |
| 707 | unsigned int parm; |
| 708 | int i, dev_len, devices; |
| 709 | |
| 710 | parm = get_num_devices(codec, nid); |
| 711 | if (!parm) /* not multi-stream capable */ |
| 712 | return 0; |
| 713 | |
| 714 | dev_len = parm + 1; |
| 715 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 716 | |
| 717 | devices = 0; |
| 718 | while (devices < dev_len) { |
| 719 | parm = snd_hda_codec_read(codec, nid, 0, |
| 720 | AC_VERB_GET_DEVICE_LIST, devices); |
| 721 | if (parm == -1 && codec->bus->rirb_error) |
| 722 | break; |
| 723 | |
| 724 | for (i = 0; i < 8; i++) { |
| 725 | dev_list[devices] = (u8)parm; |
| 726 | parm >>= 4; |
| 727 | devices++; |
| 728 | if (devices >= dev_len) |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | return devices; |
| 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | /** |
| 736 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 737 | * @bus: the BUS |
| 738 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 739 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 740 | * |
| 741 | * Adds the given event to the queue. The events are processed in |
| 742 | * the workqueue asynchronously. Call this function in the interrupt |
| 743 | * hanlder when RIRB receives an unsolicited event. |
| 744 | * |
| 745 | * Returns 0 if successful, or a negative error code. |
| 746 | */ |
| 747 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 748 | { |
| 749 | struct hda_bus_unsolicited *unsol; |
| 750 | unsigned int wp; |
| 751 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 752 | if (!bus || !bus->workq) |
| 753 | return 0; |
| 754 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 755 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 756 | unsol = bus->unsol; |
| 757 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return 0; |
| 759 | |
| 760 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 761 | unsol->wp = wp; |
| 762 | |
| 763 | wp <<= 1; |
| 764 | unsol->queue[wp] = res; |
| 765 | unsol->queue[wp + 1] = res_ex; |
| 766 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 767 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | return 0; |
| 770 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 771 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 774 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 776 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 778 | struct hda_bus_unsolicited *unsol = |
| 779 | container_of(work, struct hda_bus_unsolicited, work); |
| 780 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | struct hda_codec *codec; |
| 782 | unsigned int rp, caddr, res; |
| 783 | |
| 784 | while (unsol->rp != unsol->wp) { |
| 785 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 786 | unsol->rp = rp; |
| 787 | rp <<= 1; |
| 788 | res = unsol->queue[rp]; |
| 789 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 790 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | continue; |
| 792 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 793 | if (codec && codec->patch_ops.unsol_event) |
| 794 | codec->patch_ops.unsol_event(codec, res); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * initialize unsolicited queue |
| 800 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 801 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
| 803 | struct hda_bus_unsolicited *unsol; |
| 804 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 805 | if (bus->unsol) /* already initialized */ |
| 806 | return 0; |
| 807 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 808 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 809 | if (!unsol) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 810 | dev_err(bus->card->dev, "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | return -ENOMEM; |
| 812 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 813 | INIT_WORK(&unsol->work, process_unsol_events); |
| 814 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | bus->unsol = unsol; |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * destructor |
| 821 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 822 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 824 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 825 | return; |
| 826 | |
| 827 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 828 | if (bus->workq) |
| 829 | flush_workqueue(bus->workq); |
| 830 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | kfree(bus->unsol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (bus->ops.private_free) |
| 833 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 834 | if (bus->workq) |
| 835 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 840 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 842 | snd_hda_bus_free(device->device_data); |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 847 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 849 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 850 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /** |
| 854 | * snd_hda_bus_new - create a HDA bus |
| 855 | * @card: the card entry |
| 856 | * @temp: the template for hda_bus information |
| 857 | * @busp: the pointer to store the created bus instance |
| 858 | * |
| 859 | * Returns 0 if successful, or a negative error code. |
| 860 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 861 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 862 | const struct hda_bus_template *temp, |
| 863 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
| 865 | struct hda_bus *bus; |
| 866 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 867 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 868 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | .dev_free = snd_hda_bus_dev_free, |
| 870 | }; |
| 871 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 872 | if (snd_BUG_ON(!temp)) |
| 873 | return -EINVAL; |
| 874 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 875 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
| 877 | if (busp) |
| 878 | *busp = NULL; |
| 879 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 880 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 882 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | return -ENOMEM; |
| 884 | } |
| 885 | |
| 886 | bus->card = card; |
| 887 | bus->private_data = temp->private_data; |
| 888 | bus->pci = temp->pci; |
| 889 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 890 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | bus->ops = temp->ops; |
| 892 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 893 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 894 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | INIT_LIST_HEAD(&bus->codec_list); |
| 896 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 897 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 898 | "hd-audio%d", card->number); |
| 899 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 900 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 901 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 902 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 903 | kfree(bus); |
| 904 | return -ENOMEM; |
| 905 | } |
| 906 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 907 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 908 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | snd_hda_bus_free(bus); |
| 910 | return err; |
| 911 | } |
| 912 | if (busp) |
| 913 | *busp = bus; |
| 914 | return 0; |
| 915 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 916 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 918 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 919 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 920 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 921 | #else |
| 922 | #define is_generic_config(codec) 0 |
| 923 | #endif |
| 924 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 925 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 926 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 927 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 928 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 929 | #endif |
| 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | /* |
| 932 | * find a matching codec preset |
| 933 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 934 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 935 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 937 | struct hda_codec_preset_list *tbl; |
| 938 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 939 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 941 | again: |
| 942 | mutex_lock(&preset_mutex); |
| 943 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 944 | if (!try_module_get(tbl->owner)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 945 | codec_err(codec, "cannot module_get\n"); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 946 | continue; |
| 947 | } |
| 948 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 950 | if (preset->afg && preset->afg != codec->afg) |
| 951 | continue; |
| 952 | if (preset->mfg && preset->mfg != codec->mfg) |
| 953 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 954 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 956 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 957 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 958 | preset->rev == codec->revision_id)) { |
| 959 | mutex_unlock(&preset_mutex); |
| 960 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 964 | module_put(tbl->owner); |
| 965 | } |
| 966 | mutex_unlock(&preset_mutex); |
| 967 | |
| 968 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 969 | char name[32]; |
| 970 | if (!mod_requested) |
| 971 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 972 | codec->vendor_id); |
| 973 | else |
| 974 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 975 | (codec->vendor_id >> 16) & 0xffff); |
| 976 | request_module(name); |
| 977 | mod_requested++; |
| 978 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 984 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 986 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
| 988 | const struct hda_vendor_id *c; |
| 989 | const char *vendor = NULL; |
| 990 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 991 | char tmp[16]; |
| 992 | |
| 993 | if (codec->vendor_name) |
| 994 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | for (c = hda_vendor_ids; c->id; c++) { |
| 997 | if (c->id == vendor_id) { |
| 998 | vendor = c->name; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1002 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1004 | vendor = tmp; |
| 1005 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1006 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1007 | if (!codec->vendor_name) |
| 1008 | return -ENOMEM; |
| 1009 | |
| 1010 | get_chip_name: |
| 1011 | if (codec->chip_name) |
| 1012 | return 0; |
| 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1015 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1016 | else { |
| 1017 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1018 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1019 | } |
| 1020 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1021 | return -ENOMEM; |
| 1022 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1026 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1028 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1030 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | hda_nid_t nid; |
| 1032 | |
| 1033 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1034 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1035 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1036 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1037 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1038 | case AC_GRP_AUDIO_FUNCTION: |
| 1039 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1040 | codec->afg_function_id = function_id & 0xff; |
| 1041 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1042 | break; |
| 1043 | case AC_GRP_MODEM_FUNCTION: |
| 1044 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1045 | codec->mfg_function_id = function_id & 0xff; |
| 1046 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1047 | break; |
| 1048 | default: |
| 1049 | break; |
| 1050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1055 | * read widget caps for each widget and store in cache |
| 1056 | */ |
| 1057 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1058 | { |
| 1059 | int i; |
| 1060 | hda_nid_t nid; |
| 1061 | |
| 1062 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1063 | &codec->start_nid); |
| 1064 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1065 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1066 | return -ENOMEM; |
| 1067 | nid = codec->start_nid; |
| 1068 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1069 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1070 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1074 | /* read all pin default configurations and save codec->init_pins */ |
| 1075 | static int read_pin_defaults(struct hda_codec *codec) |
| 1076 | { |
| 1077 | int i; |
| 1078 | hda_nid_t nid = codec->start_nid; |
| 1079 | |
| 1080 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1081 | struct hda_pincfg *pin; |
| 1082 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1083 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1084 | if (wid_type != AC_WID_PIN) |
| 1085 | continue; |
| 1086 | pin = snd_array_new(&codec->init_pins); |
| 1087 | if (!pin) |
| 1088 | return -ENOMEM; |
| 1089 | pin->nid = nid; |
| 1090 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1091 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1092 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1093 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1094 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1095 | } |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* look up the given pin config list and return the item matching with NID */ |
| 1100 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1101 | struct snd_array *array, |
| 1102 | hda_nid_t nid) |
| 1103 | { |
| 1104 | int i; |
| 1105 | for (i = 0; i < array->used; i++) { |
| 1106 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1107 | if (pin->nid == nid) |
| 1108 | return pin; |
| 1109 | } |
| 1110 | return NULL; |
| 1111 | } |
| 1112 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1113 | /* set the current pin config value for the given NID. |
| 1114 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1115 | */ |
| 1116 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1117 | hda_nid_t nid, unsigned int cfg) |
| 1118 | { |
| 1119 | struct hda_pincfg *pin; |
| 1120 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1121 | /* the check below may be invalid when pins are added by a fixup |
| 1122 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1123 | * for now |
| 1124 | */ |
| 1125 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1126 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1127 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1128 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1129 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1130 | pin = look_up_pincfg(codec, list, nid); |
| 1131 | if (!pin) { |
| 1132 | pin = snd_array_new(list); |
| 1133 | if (!pin) |
| 1134 | return -ENOMEM; |
| 1135 | pin->nid = nid; |
| 1136 | } |
| 1137 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
| 1140 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1141 | /** |
| 1142 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1143 | * @codec: the HDA codec |
| 1144 | * @nid: NID to set the pin config |
| 1145 | * @cfg: the pin default config value |
| 1146 | * |
| 1147 | * Override a pin default configuration value in the cache. |
| 1148 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1149 | * priority than the real hardware value. |
| 1150 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1151 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1152 | hda_nid_t nid, unsigned int cfg) |
| 1153 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1154 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1155 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1156 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1158 | /** |
| 1159 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1160 | * @codec: the HDA codec |
| 1161 | * @nid: NID to get the pin config |
| 1162 | * |
| 1163 | * Get the current pin config value of the given pin NID. |
| 1164 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1165 | * returns the cached value. |
| 1166 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1167 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1168 | { |
| 1169 | struct hda_pincfg *pin; |
| 1170 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1171 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1172 | { |
| 1173 | unsigned int cfg = 0; |
| 1174 | mutex_lock(&codec->user_mutex); |
| 1175 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1176 | if (pin) |
| 1177 | cfg = pin->cfg; |
| 1178 | mutex_unlock(&codec->user_mutex); |
| 1179 | if (cfg) |
| 1180 | return cfg; |
| 1181 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1182 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1183 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1184 | if (pin) |
| 1185 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1186 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1187 | if (pin) |
| 1188 | return pin->cfg; |
| 1189 | return 0; |
| 1190 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1191 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1192 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1193 | /* remember the current pinctl target value */ |
| 1194 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1195 | unsigned int val) |
| 1196 | { |
| 1197 | struct hda_pincfg *pin; |
| 1198 | |
| 1199 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1200 | if (!pin) |
| 1201 | return -EINVAL; |
| 1202 | pin->target = val; |
| 1203 | return 0; |
| 1204 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1205 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1206 | |
| 1207 | /* return the current pinctl target value */ |
| 1208 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1209 | { |
| 1210 | struct hda_pincfg *pin; |
| 1211 | |
| 1212 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1213 | if (!pin) |
| 1214 | return 0; |
| 1215 | return pin->target; |
| 1216 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1217 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1218 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1219 | /** |
| 1220 | * snd_hda_shutup_pins - Shut up all pins |
| 1221 | * @codec: the HDA codec |
| 1222 | * |
| 1223 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1224 | * The controls aren't cached so that they can be resumed properly. |
| 1225 | */ |
| 1226 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1227 | { |
| 1228 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1229 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1230 | * the default pin setup at the next load of the driver |
| 1231 | */ |
| 1232 | if (codec->bus->shutdown) |
| 1233 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1234 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1235 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1236 | /* use read here for syncing after issuing each verb */ |
| 1237 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1238 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1239 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1240 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1241 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1242 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1243 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1244 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1245 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1246 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1247 | { |
| 1248 | int i; |
| 1249 | if (!codec->pins_shutup) |
| 1250 | return; |
| 1251 | if (codec->bus->shutdown) |
| 1252 | return; |
| 1253 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1254 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1255 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1256 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1257 | pin->ctrl); |
| 1258 | } |
| 1259 | codec->pins_shutup = 0; |
| 1260 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1261 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1262 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1263 | static void hda_jackpoll_work(struct work_struct *work) |
| 1264 | { |
| 1265 | struct hda_codec *codec = |
| 1266 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1267 | |
| 1268 | snd_hda_jack_set_dirty_all(codec); |
| 1269 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1270 | |
| 1271 | if (!codec->jackpoll_interval) |
| 1272 | return; |
| 1273 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1274 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1275 | codec->jackpoll_interval); |
| 1276 | } |
| 1277 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1278 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1279 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1280 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1281 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1282 | /* release all pincfg lists */ |
| 1283 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1284 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1285 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1286 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1287 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1288 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1289 | snd_array_free(&codec->init_pins); |
| 1290 | } |
| 1291 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1292 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1293 | * audio-converter setup caches |
| 1294 | */ |
| 1295 | struct hda_cvt_setup { |
| 1296 | hda_nid_t nid; |
| 1297 | u8 stream_tag; |
| 1298 | u8 channel_id; |
| 1299 | u16 format_id; |
| 1300 | unsigned char active; /* cvt is currently used */ |
| 1301 | unsigned char dirty; /* setups should be cleared */ |
| 1302 | }; |
| 1303 | |
| 1304 | /* get or create a cache entry for the given audio converter NID */ |
| 1305 | static struct hda_cvt_setup * |
| 1306 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1307 | { |
| 1308 | struct hda_cvt_setup *p; |
| 1309 | int i; |
| 1310 | |
| 1311 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1312 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1313 | if (p->nid == nid) |
| 1314 | return p; |
| 1315 | } |
| 1316 | p = snd_array_new(&codec->cvt_setups); |
| 1317 | if (p) |
| 1318 | p->nid = nid; |
| 1319 | return p; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1323 | * Dynamic symbol binding for the codec parsers |
| 1324 | */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1325 | |
| 1326 | #define load_parser(codec, sym) \ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1327 | ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1328 | |
| 1329 | static void unload_parser(struct hda_codec *codec) |
| 1330 | { |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1331 | if (codec->parser) |
| 1332 | symbol_put_addr(codec->parser); |
| 1333 | codec->parser = NULL; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | * codec destructor |
| 1338 | */ |
| 1339 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1340 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1341 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1343 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1344 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1345 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1346 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1347 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1348 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1349 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1351 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1352 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1353 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1354 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1355 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1357 | if (codec->patch_ops.free) |
| 1358 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1359 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1360 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1361 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1362 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1363 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1364 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1365 | kfree(codec->vendor_name); |
| 1366 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1367 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1368 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1369 | codec->bus->num_codecs--; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1370 | put_device(&codec->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1373 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1374 | hda_nid_t fg, unsigned int power_state); |
| 1375 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1376 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1377 | unsigned int power_state); |
| 1378 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1379 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1380 | { |
| 1381 | struct hda_codec *codec = device->device_data; |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1382 | int err = device_add(&codec->dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1383 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1384 | if (err < 0) |
| 1385 | return err; |
| 1386 | snd_hda_register_beep_device(codec); |
| 1387 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1391 | { |
| 1392 | struct hda_codec *codec = device->device_data; |
| 1393 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1394 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1395 | device_del(&codec->dev); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1399 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1400 | { |
| 1401 | snd_hda_codec_free(device->device_data); |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1405 | /* just free the container */ |
| 1406 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1407 | { |
| 1408 | kfree(container_of(dev, struct hda_codec, dev)); |
| 1409 | } |
| 1410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | /** |
| 1412 | * snd_hda_codec_new - create a HDA codec |
| 1413 | * @bus: the bus to assign |
| 1414 | * @codec_addr: the codec address |
| 1415 | * @codecp: the pointer to store the generated codec |
| 1416 | * |
| 1417 | * Returns 0 if successful, or a negative error code. |
| 1418 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1419 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1420 | unsigned int codec_addr, |
| 1421 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | { |
| 1423 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1424 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1425 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1427 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1428 | .dev_register = snd_hda_codec_dev_register, |
| 1429 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1430 | .dev_free = snd_hda_codec_dev_free, |
| 1431 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1433 | if (snd_BUG_ON(!bus)) |
| 1434 | return -EINVAL; |
| 1435 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1436 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
| 1438 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1439 | dev_err(bus->card->dev, |
| 1440 | "address 0x%x is already occupied\n", |
| 1441 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | return -EBUSY; |
| 1443 | } |
| 1444 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1445 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1447 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | return -ENOMEM; |
| 1449 | } |
| 1450 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1451 | device_initialize(&codec->dev); |
| 1452 | codec->dev.parent = &bus->card->card_dev; |
| 1453 | codec->dev.class = sound_class; |
| 1454 | codec->dev.release = snd_hda_codec_dev_release; |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1455 | codec->dev.groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1456 | dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number, |
| 1457 | codec_addr); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1458 | dev_set_drvdata(&codec->dev, codec); /* for sysfs */ |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | codec->bus = bus; |
| 1461 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1462 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1463 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1464 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1465 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1466 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1467 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1468 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1469 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1470 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1471 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1472 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1473 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1474 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1475 | INIT_LIST_HEAD(&codec->conn_list); |
| 1476 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1477 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1478 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame^] | 1479 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1481 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1482 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1483 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 1484 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1485 | * the caller has to power down appropriatley after initialization |
| 1486 | * phase. |
| 1487 | */ |
| 1488 | hda_keep_power_on(codec); |
| 1489 | #endif |
| 1490 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1491 | snd_hda_sysfs_init(codec); |
| 1492 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1493 | if (codec->bus->modelname) { |
| 1494 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1495 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1496 | err = -ENODEV; |
| 1497 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1502 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | bus->caddr_tbl[codec_addr] = codec; |
| 1505 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1506 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1507 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1508 | if (codec->vendor_id == -1) |
| 1509 | /* read again, hopefully the access method was corrected |
| 1510 | * in the last read... |
| 1511 | */ |
| 1512 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1513 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1514 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1515 | AC_PAR_SUBSYSTEM_ID); |
| 1516 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1517 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1519 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1520 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1521 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1522 | err = -ENODEV; |
| 1523 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1526 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1527 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1528 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1529 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1530 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1531 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1532 | err = read_pin_defaults(codec); |
| 1533 | if (err < 0) |
| 1534 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1535 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1536 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1537 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1538 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1539 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1542 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1543 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1544 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1545 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1546 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1547 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1548 | #ifdef CONFIG_PM |
| 1549 | if (!codec->d3_stop_clk || !codec->epss) |
| 1550 | bus->power_keep_link_on = 1; |
| 1551 | #endif |
| 1552 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1553 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1554 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1555 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1556 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1557 | snd_hda_codec_proc_new(codec); |
| 1558 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1559 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1560 | |
| 1561 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1562 | codec->subsystem_id, codec->revision_id); |
| 1563 | snd_component_add(codec->bus->card, component); |
| 1564 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1565 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1566 | if (err < 0) |
| 1567 | goto error; |
| 1568 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1569 | if (codecp) |
| 1570 | *codecp = codec; |
| 1571 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1572 | |
| 1573 | error: |
| 1574 | snd_hda_codec_free(codec); |
| 1575 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1576 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1577 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1578 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1579 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1580 | { |
| 1581 | hda_nid_t fg; |
| 1582 | int err; |
| 1583 | |
| 1584 | /* Assume the function group node does not change, |
| 1585 | * only the widget nodes may change. |
| 1586 | */ |
| 1587 | kfree(codec->wcaps); |
| 1588 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1589 | err = read_widget_caps(codec, fg); |
| 1590 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1591 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1592 | return err; |
| 1593 | } |
| 1594 | |
| 1595 | snd_array_free(&codec->init_pins); |
| 1596 | err = read_pin_defaults(codec); |
| 1597 | |
| 1598 | return err; |
| 1599 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1600 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1601 | |
| 1602 | |
Takashi Iwai | f8f1bec | 2014-02-06 18:14:03 +0100 | [diff] [blame] | 1603 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1604 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1605 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1606 | { |
| 1607 | hda_nid_t nid = codec->start_nid; |
| 1608 | int i; |
| 1609 | |
| 1610 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1611 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1612 | switch (get_wcaps_type(wcaps)) { |
| 1613 | case AC_WID_AUD_IN: |
| 1614 | return false; /* HDMI parser supports only HDMI out */ |
| 1615 | case AC_WID_AUD_OUT: |
| 1616 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1617 | return false; |
| 1618 | break; |
| 1619 | } |
| 1620 | } |
| 1621 | return true; |
| 1622 | } |
| 1623 | #else |
| 1624 | /* no HDMI codec parser support */ |
| 1625 | #define is_likely_hdmi_codec(codec) false |
| 1626 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1627 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1628 | /** |
| 1629 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1630 | * @codec: the HDA codec |
| 1631 | * |
| 1632 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1633 | * patch instance. |
| 1634 | * |
| 1635 | * Returns 0 if successful or a negative error code. |
| 1636 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1637 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1638 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1639 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1640 | int err; |
| 1641 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1642 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1643 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1644 | err = get_codec_name(codec); |
| 1645 | if (err < 0) |
| 1646 | return err; |
| 1647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1649 | if (!is_generic_config(codec) && codec->preset) |
| 1650 | patch = codec->preset->patch; |
| 1651 | if (!patch) { |
| 1652 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1653 | if (is_likely_hdmi_codec(codec)) { |
| 1654 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1655 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1656 | #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) |
| 1657 | patch = snd_hda_parse_hdmi_codec; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1658 | #endif |
Takashi Iwai | ef8e39b | 2014-02-10 11:43:41 +0100 | [diff] [blame] | 1659 | } |
| 1660 | if (!patch) { |
| 1661 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 1662 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1663 | #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) |
| 1664 | patch = snd_hda_parse_generic_codec; |
| 1665 | #endif |
| 1666 | } |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1667 | if (!patch) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1668 | codec_err(codec, "No codec parser is available\n"); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1669 | return -ENODEV; |
| 1670 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1673 | err = patch(codec); |
| 1674 | if (err < 0) { |
| 1675 | unload_parser(codec); |
| 1676 | return err; |
| 1677 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1678 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1679 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1680 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1681 | if (err < 0) |
| 1682 | return err; |
| 1683 | } |
| 1684 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1685 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1686 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1687 | snprintf(codec->bus->card->mixername, |
| 1688 | sizeof(codec->bus->card->mixername), |
| 1689 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1690 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1692 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1694 | /* update the stream-id if changed */ |
| 1695 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1696 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1697 | u32 stream_tag, int channel_id) |
| 1698 | { |
| 1699 | unsigned int oldval, newval; |
| 1700 | |
| 1701 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1702 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1703 | newval = (stream_tag << 4) | channel_id; |
| 1704 | if (oldval != newval) |
| 1705 | snd_hda_codec_write(codec, nid, 0, |
| 1706 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1707 | newval); |
| 1708 | p->stream_tag = stream_tag; |
| 1709 | p->channel_id = channel_id; |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | /* update the format-id if changed */ |
| 1714 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1715 | hda_nid_t nid, int format) |
| 1716 | { |
| 1717 | unsigned int oldval; |
| 1718 | |
| 1719 | if (p->format_id != format) { |
| 1720 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1721 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1722 | if (oldval != format) { |
| 1723 | msleep(1); |
| 1724 | snd_hda_codec_write(codec, nid, 0, |
| 1725 | AC_VERB_SET_STREAM_FORMAT, |
| 1726 | format); |
| 1727 | } |
| 1728 | p->format_id = format; |
| 1729 | } |
| 1730 | } |
| 1731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | /** |
| 1733 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1734 | * @codec: the CODEC to set up |
| 1735 | * @nid: the NID to set up |
| 1736 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1737 | * @channel_id: channel id to pass, zero based. |
| 1738 | * @format: stream format. |
| 1739 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1740 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1741 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | int channel_id, int format) |
| 1743 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1744 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1745 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1746 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1747 | int i; |
| 1748 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1749 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1750 | return; |
| 1751 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1752 | codec_dbg(codec, |
| 1753 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1754 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1755 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1756 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1757 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1758 | |
| 1759 | if (codec->pcm_format_first) |
| 1760 | update_pcm_format(codec, p, nid, format); |
| 1761 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1762 | if (!codec->pcm_format_first) |
| 1763 | update_pcm_format(codec, p, nid, format); |
| 1764 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1765 | p->active = 1; |
| 1766 | p->dirty = 0; |
| 1767 | |
| 1768 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1769 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1770 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1771 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1772 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1773 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1774 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1775 | p->dirty = 1; |
| 1776 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1779 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1781 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1782 | struct hda_cvt_setup *q); |
| 1783 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1784 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1785 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1786 | * @codec: the CODEC to clean up |
| 1787 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1788 | * @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] | 1789 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1790 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1791 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1792 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1793 | struct hda_cvt_setup *p; |
| 1794 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1795 | if (!nid) |
| 1796 | return; |
| 1797 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1798 | if (codec->no_sticky_stream) |
| 1799 | do_now = 1; |
| 1800 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1801 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1802 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1803 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1804 | /* here we just clear the active flag when do_now isn't set; |
| 1805 | * actual clean-ups will be done later in |
| 1806 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1807 | */ |
| 1808 | if (do_now) |
| 1809 | really_cleanup_stream(codec, p); |
| 1810 | else |
| 1811 | p->active = 0; |
| 1812 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1813 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1814 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1815 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1816 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1817 | struct hda_cvt_setup *q) |
| 1818 | { |
| 1819 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1820 | if (q->stream_tag || q->channel_id) |
| 1821 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1822 | if (q->format_id) |
| 1823 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1824 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1825 | memset(q, 0, sizeof(*q)); |
| 1826 | q->nid = nid; |
| 1827 | } |
| 1828 | |
| 1829 | /* clean up the all conflicting obsolete streams */ |
| 1830 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1831 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1832 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1833 | int i; |
| 1834 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1835 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1836 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1837 | struct hda_cvt_setup *p; |
| 1838 | p = snd_array_elem(&c->cvt_setups, i); |
| 1839 | if (p->dirty) |
| 1840 | really_cleanup_stream(c, p); |
| 1841 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1845 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1846 | /* clean up all streams; called from suspend */ |
| 1847 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1848 | { |
| 1849 | int i; |
| 1850 | |
| 1851 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1852 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1853 | if (p->stream_tag) |
| 1854 | really_cleanup_stream(codec, p); |
| 1855 | } |
| 1856 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1857 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | /* |
| 1860 | * amp access functions |
| 1861 | */ |
| 1862 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1863 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1864 | #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] | 1865 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1866 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1867 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1869 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | |
| 1871 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1872 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1873 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1875 | memset(cache, 0, sizeof(*cache)); |
| 1876 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1877 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1880 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1881 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1882 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1886 | 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] | 1887 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1888 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1889 | u16 cur = cache->hash[idx]; |
| 1890 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
| 1892 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1893 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | if (info->key == key) |
| 1895 | return info; |
| 1896 | cur = info->next; |
| 1897 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1898 | return NULL; |
| 1899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1901 | /* query the hash. allocate an entry if not found. */ |
| 1902 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1903 | u32 key) |
| 1904 | { |
| 1905 | struct hda_cache_head *info = get_hash(cache, key); |
| 1906 | if (!info) { |
| 1907 | u16 idx, cur; |
| 1908 | /* add a new hash entry */ |
| 1909 | info = snd_array_new(&cache->buf); |
| 1910 | if (!info) |
| 1911 | return NULL; |
| 1912 | cur = snd_array_index(&cache->buf, info); |
| 1913 | info->key = key; |
| 1914 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1915 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1916 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1917 | info->next = cache->hash[idx]; |
| 1918 | cache->hash[idx] = cur; |
| 1919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | return info; |
| 1921 | } |
| 1922 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1923 | /* query and allocate an amp hash entry */ |
| 1924 | static inline struct hda_amp_info * |
| 1925 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1926 | { |
| 1927 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1928 | } |
| 1929 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1930 | /* overwrite the value with the key in the caps hash */ |
| 1931 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1932 | { |
| 1933 | struct hda_amp_info *info; |
| 1934 | |
| 1935 | mutex_lock(&codec->hash_mutex); |
| 1936 | info = get_alloc_amp_hash(codec, key); |
| 1937 | if (!info) { |
| 1938 | mutex_unlock(&codec->hash_mutex); |
| 1939 | return -EINVAL; |
| 1940 | } |
| 1941 | info->amp_caps = val; |
| 1942 | info->head.val |= INFO_AMP_CAPS; |
| 1943 | mutex_unlock(&codec->hash_mutex); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* query the value from the caps hash; if not found, fetch the current |
| 1948 | * value from the given function and store in the hash |
| 1949 | */ |
| 1950 | static unsigned int |
| 1951 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1952 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1953 | { |
| 1954 | struct hda_amp_info *info; |
| 1955 | unsigned int val; |
| 1956 | |
| 1957 | mutex_lock(&codec->hash_mutex); |
| 1958 | info = get_alloc_amp_hash(codec, key); |
| 1959 | if (!info) { |
| 1960 | mutex_unlock(&codec->hash_mutex); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1964 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1965 | val = func(codec, nid, dir); |
| 1966 | write_caps_hash(codec, key, val); |
| 1967 | } else { |
| 1968 | val = info->amp_caps; |
| 1969 | mutex_unlock(&codec->hash_mutex); |
| 1970 | } |
| 1971 | return val; |
| 1972 | } |
| 1973 | |
| 1974 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1975 | int direction) |
| 1976 | { |
| 1977 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1978 | nid = codec->afg; |
| 1979 | return snd_hda_param_read(codec, nid, |
| 1980 | direction == HDA_OUTPUT ? |
| 1981 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1982 | } |
| 1983 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1984 | /** |
| 1985 | * query_amp_caps - query AMP capabilities |
| 1986 | * @codec: the HD-auio codec |
| 1987 | * @nid: the NID to query |
| 1988 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1989 | * |
| 1990 | * Query AMP capabilities for the given widget and direction. |
| 1991 | * Returns the obtained capability bits. |
| 1992 | * |
| 1993 | * When cap bits have been already read, this doesn't read again but |
| 1994 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1996 | 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] | 1997 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1998 | return query_caps_hash(codec, nid, direction, |
| 1999 | HDA_HASH_KEY(nid, direction, 0), |
| 2000 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2002 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2004 | /** |
| 2005 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 2006 | * @codec: the CODEC to clean up |
| 2007 | * @nid: the NID to clean up |
| 2008 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 2009 | * @caps: the capability bits to set |
| 2010 | * |
| 2011 | * Override the cached AMP caps bits value by the given one. |
| 2012 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 2013 | * e.g. limit to 0dB, etc. |
| 2014 | * |
| 2015 | * Returns zero if successful or a negative error code. |
| 2016 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2017 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2018 | unsigned int caps) |
| 2019 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2020 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2021 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2022 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2023 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2024 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2025 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2026 | { |
| 2027 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 2028 | } |
| 2029 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2030 | /** |
| 2031 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 2032 | * @codec: the HD-auio codec |
| 2033 | * @nid: the NID to query |
| 2034 | * |
| 2035 | * Query PIN capabilities for the given widget. |
| 2036 | * Returns the obtained capability bits. |
| 2037 | * |
| 2038 | * When cap bits have been already read, this doesn't read again but |
| 2039 | * returns the cached value. |
| 2040 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2041 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2042 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2043 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2044 | read_pin_cap); |
| 2045 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2046 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2047 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2048 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2049 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2050 | * @codec: the CODEC |
| 2051 | * @nid: the NID to override |
| 2052 | * @caps: the capability bits to set |
| 2053 | * |
| 2054 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2055 | * |
| 2056 | * Returns zero if successful or a negative error code. |
| 2057 | */ |
| 2058 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2059 | unsigned int caps) |
| 2060 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2061 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2062 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2063 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2064 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2065 | /* read or sync the hash value with the current value; |
| 2066 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2068 | static struct hda_amp_info * |
| 2069 | 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] | 2070 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2072 | struct hda_amp_info *info; |
| 2073 | unsigned int parm, val = 0; |
| 2074 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2076 | retry: |
| 2077 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2078 | if (!info) |
| 2079 | return NULL; |
| 2080 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2081 | if (!val_read) { |
| 2082 | mutex_unlock(&codec->hash_mutex); |
| 2083 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2084 | parm |= direction == HDA_OUTPUT ? |
| 2085 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2086 | parm |= index; |
| 2087 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2088 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2089 | val &= 0xff; |
| 2090 | val_read = true; |
| 2091 | mutex_lock(&codec->hash_mutex); |
| 2092 | goto retry; |
| 2093 | } |
| 2094 | info->vol[ch] = val; |
| 2095 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2096 | } else if (init_only) |
| 2097 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2098 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2102 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2104 | 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] | 2105 | hda_nid_t nid, int ch, int direction, int index, |
| 2106 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | { |
| 2108 | u32 parm; |
| 2109 | |
| 2110 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2111 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2112 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2113 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2114 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2115 | ; /* set the zero value as a fake mute */ |
| 2116 | else |
| 2117 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2119 | } |
| 2120 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2121 | /** |
| 2122 | * snd_hda_codec_amp_read - Read AMP value |
| 2123 | * @codec: HD-audio codec |
| 2124 | * @nid: NID to read the AMP value |
| 2125 | * @ch: channel (left=0 or right=1) |
| 2126 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2127 | * @index: the index value (only for input direction) |
| 2128 | * |
| 2129 | * 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] | 2130 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2131 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2132 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2134 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2135 | unsigned int val = 0; |
| 2136 | |
| 2137 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2138 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2139 | if (info) |
| 2140 | val = info->vol[ch]; |
| 2141 | mutex_unlock(&codec->hash_mutex); |
| 2142 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2144 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2146 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2147 | int direction, int idx, int mask, int val, |
| 2148 | bool init_only) |
| 2149 | { |
| 2150 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2151 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2152 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2153 | |
| 2154 | if (snd_BUG_ON(mask & ~0xff)) |
| 2155 | mask &= 0xff; |
| 2156 | val &= mask; |
| 2157 | |
| 2158 | mutex_lock(&codec->hash_mutex); |
| 2159 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2160 | if (!info) { |
| 2161 | mutex_unlock(&codec->hash_mutex); |
| 2162 | return 0; |
| 2163 | } |
| 2164 | val |= info->vol[ch] & ~mask; |
| 2165 | if (info->vol[ch] == val) { |
| 2166 | mutex_unlock(&codec->hash_mutex); |
| 2167 | return 0; |
| 2168 | } |
| 2169 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2170 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2171 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2172 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2173 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2174 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2175 | return 1; |
| 2176 | } |
| 2177 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2178 | /** |
| 2179 | * snd_hda_codec_amp_update - update the AMP value |
| 2180 | * @codec: HD-audio codec |
| 2181 | * @nid: NID to read the AMP value |
| 2182 | * @ch: channel (left=0 or right=1) |
| 2183 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2184 | * @idx: the index value (only for input direction) |
| 2185 | * @mask: bit mask to set |
| 2186 | * @val: the bits value to set |
| 2187 | * |
| 2188 | * Update the AMP value with a bit mask. |
| 2189 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2190 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2191 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2192 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2194 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2196 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2198 | /** |
| 2199 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2200 | * @codec: HD-audio codec |
| 2201 | * @nid: NID to read the AMP value |
| 2202 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2203 | * @idx: the index value (only for input direction) |
| 2204 | * @mask: bit mask to set |
| 2205 | * @val: the bits value to set |
| 2206 | * |
| 2207 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2208 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2209 | */ |
| 2210 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2211 | int direction, int idx, int mask, int val) |
| 2212 | { |
| 2213 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2214 | |
| 2215 | if (snd_BUG_ON(mask & ~0xff)) |
| 2216 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2217 | for (ch = 0; ch < 2; ch++) |
| 2218 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2219 | idx, mask, val); |
| 2220 | return ret; |
| 2221 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2222 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2223 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2224 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2225 | * the first access. If the amp was already initialized / updated beforehand, |
| 2226 | * this does nothing. |
| 2227 | */ |
| 2228 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2229 | int dir, int idx, int mask, int val) |
| 2230 | { |
| 2231 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2232 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2233 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2234 | |
| 2235 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2236 | int dir, int idx, int mask, int val) |
| 2237 | { |
| 2238 | int ch, ret = 0; |
| 2239 | |
| 2240 | if (snd_BUG_ON(mask & ~0xff)) |
| 2241 | mask &= 0xff; |
| 2242 | for (ch = 0; ch < 2; ch++) |
| 2243 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2244 | idx, mask, val); |
| 2245 | return ret; |
| 2246 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2247 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2248 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2249 | /** |
| 2250 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2251 | * @codec: HD-audio codec |
| 2252 | * |
| 2253 | * Resume the all amp commands from the cache. |
| 2254 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2255 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2256 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2257 | int i; |
| 2258 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2259 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2260 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2261 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2262 | struct hda_amp_info *buffer; |
| 2263 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2264 | hda_nid_t nid; |
| 2265 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2266 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2267 | |
| 2268 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2269 | if (!buffer->head.dirty) |
| 2270 | continue; |
| 2271 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2272 | info = *buffer; |
| 2273 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2274 | if (!key) |
| 2275 | continue; |
| 2276 | nid = key & 0xff; |
| 2277 | idx = (key >> 16) & 0xff; |
| 2278 | dir = (key >> 24) & 0xff; |
| 2279 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2280 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2281 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2282 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2283 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2284 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2285 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2288 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2289 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2290 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2292 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2293 | unsigned int ofs) |
| 2294 | { |
| 2295 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2296 | /* get num steps */ |
| 2297 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2298 | if (ofs < caps) |
| 2299 | caps -= ofs; |
| 2300 | return caps; |
| 2301 | } |
| 2302 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2303 | /** |
| 2304 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2305 | * |
| 2306 | * The control element is supposed to have the private_value field |
| 2307 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2308 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2309 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2310 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | { |
| 2312 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2313 | u16 nid = get_amp_nid(kcontrol); |
| 2314 | u8 chs = get_amp_channels(kcontrol); |
| 2315 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2316 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2318 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2319 | uinfo->count = chs == 3 ? 2 : 1; |
| 2320 | uinfo->value.integer.min = 0; |
| 2321 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2322 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2323 | codec_warn(codec, |
| 2324 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2325 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | return -EINVAL; |
| 2327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2330 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2332 | |
| 2333 | static inline unsigned int |
| 2334 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2335 | int ch, int dir, int idx, unsigned int ofs) |
| 2336 | { |
| 2337 | unsigned int val; |
| 2338 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2339 | val &= HDA_AMP_VOLMASK; |
| 2340 | if (val >= ofs) |
| 2341 | val -= ofs; |
| 2342 | else |
| 2343 | val = 0; |
| 2344 | return val; |
| 2345 | } |
| 2346 | |
| 2347 | static inline int |
| 2348 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2349 | int ch, int dir, int idx, unsigned int ofs, |
| 2350 | unsigned int val) |
| 2351 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2352 | unsigned int maxval; |
| 2353 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2354 | if (val > 0) |
| 2355 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2356 | /* ofs = 0: raw max value */ |
| 2357 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2358 | if (val > maxval) |
| 2359 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2360 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2361 | HDA_AMP_VOLMASK, val); |
| 2362 | } |
| 2363 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2364 | /** |
| 2365 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2366 | * |
| 2367 | * The control element is supposed to have the private_value field |
| 2368 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2369 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2370 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2371 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | { |
| 2373 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2374 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2375 | int chs = get_amp_channels(kcontrol); |
| 2376 | int dir = get_amp_direction(kcontrol); |
| 2377 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2378 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | long *valp = ucontrol->value.integer.value; |
| 2380 | |
| 2381 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2382 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2384 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | return 0; |
| 2386 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2387 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2389 | /** |
| 2390 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2391 | * |
| 2392 | * The control element is supposed to have the private_value field |
| 2393 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2394 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2395 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2396 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | { |
| 2398 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2399 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2400 | int chs = get_amp_channels(kcontrol); |
| 2401 | int dir = get_amp_direction(kcontrol); |
| 2402 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2403 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | long *valp = ucontrol->value.integer.value; |
| 2405 | int change = 0; |
| 2406 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2407 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2408 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2409 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2410 | valp++; |
| 2411 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2412 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2413 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2414 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | return change; |
| 2416 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2417 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2419 | /** |
| 2420 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2421 | * |
| 2422 | * The control element is supposed to have the private_value field |
| 2423 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2424 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2425 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2426 | unsigned int size, unsigned int __user *_tlv) |
| 2427 | { |
| 2428 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2429 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2430 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2431 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2432 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2433 | u32 caps, val1, val2; |
| 2434 | |
| 2435 | if (size < 4 * sizeof(unsigned int)) |
| 2436 | return -ENOMEM; |
| 2437 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2438 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2439 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2440 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2441 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2442 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2443 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2444 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2445 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2446 | return -EFAULT; |
| 2447 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2448 | return -EFAULT; |
| 2449 | if (put_user(val1, _tlv + 2)) |
| 2450 | return -EFAULT; |
| 2451 | if (put_user(val2, _tlv + 3)) |
| 2452 | return -EFAULT; |
| 2453 | return 0; |
| 2454 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2455 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2456 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2457 | /** |
| 2458 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2459 | * @codec: HD-audio codec |
| 2460 | * @nid: NID of a reference widget |
| 2461 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2462 | * @tlv: TLV data to be stored, at least 4 elements |
| 2463 | * |
| 2464 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2465 | * obtained from the reference NID. |
| 2466 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2467 | */ |
| 2468 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2469 | unsigned int *tlv) |
| 2470 | { |
| 2471 | u32 caps; |
| 2472 | int nums, step; |
| 2473 | |
| 2474 | caps = query_amp_caps(codec, nid, dir); |
| 2475 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2476 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2477 | step = (step + 1) * 25; |
| 2478 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2479 | tlv[1] = 2 * sizeof(unsigned int); |
| 2480 | tlv[2] = -nums * step; |
| 2481 | tlv[3] = step; |
| 2482 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2483 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2484 | |
| 2485 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2486 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2487 | 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] | 2488 | { |
| 2489 | struct snd_ctl_elem_id id; |
| 2490 | memset(&id, 0, sizeof(id)); |
| 2491 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2492 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2493 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2494 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2495 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2496 | strcpy(id.name, name); |
| 2497 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2498 | } |
| 2499 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2500 | /** |
| 2501 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2502 | * @codec: HD-audio codec |
| 2503 | * @name: ctl id name string |
| 2504 | * |
| 2505 | * Get the control element with the given id string and IFACE_MIXER. |
| 2506 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2507 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2508 | const char *name) |
| 2509 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2510 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2511 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2512 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2513 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2514 | 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] | 2515 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2516 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2517 | int i, idx; |
| 2518 | /* 16 ctlrs should be large enough */ |
| 2519 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2520 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2521 | return idx; |
| 2522 | } |
| 2523 | return -EBUSY; |
| 2524 | } |
| 2525 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2526 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2527 | * 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] | 2528 | * @codec: HD-audio codec |
| 2529 | * @nid: corresponding NID (optional) |
| 2530 | * @kctl: the control element to assign |
| 2531 | * |
| 2532 | * Add the given control element to an array inside the codec instance. |
| 2533 | * All control elements belonging to a codec are supposed to be added |
| 2534 | * by this function so that a proper clean-up works at the free or |
| 2535 | * reconfiguration time. |
| 2536 | * |
| 2537 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2538 | * The assignment is shown in the codec proc file. |
| 2539 | * |
| 2540 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2541 | * #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] | 2542 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2543 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2544 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2545 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2546 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2547 | { |
| 2548 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2549 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2550 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2551 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2552 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2553 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2554 | if (nid == 0) |
| 2555 | nid = get_amp_nid_(kctl->private_value); |
| 2556 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2557 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2558 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2559 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2560 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2561 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2562 | if (err < 0) |
| 2563 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2564 | item = snd_array_new(&codec->mixers); |
| 2565 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2566 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2567 | item->kctl = kctl; |
| 2568 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2569 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2570 | return 0; |
| 2571 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2572 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2573 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2574 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2575 | * snd_hda_add_nid - Assign a NID to a control element |
| 2576 | * @codec: HD-audio codec |
| 2577 | * @nid: corresponding NID (optional) |
| 2578 | * @kctl: the control element to assign |
| 2579 | * @index: index to kctl |
| 2580 | * |
| 2581 | * Add the given control element to an array inside the codec instance. |
| 2582 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2583 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2584 | */ |
| 2585 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2586 | unsigned int index, hda_nid_t nid) |
| 2587 | { |
| 2588 | struct hda_nid_item *item; |
| 2589 | |
| 2590 | if (nid > 0) { |
| 2591 | item = snd_array_new(&codec->nids); |
| 2592 | if (!item) |
| 2593 | return -ENOMEM; |
| 2594 | item->kctl = kctl; |
| 2595 | item->index = index; |
| 2596 | item->nid = nid; |
| 2597 | return 0; |
| 2598 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2599 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2600 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2601 | return -EINVAL; |
| 2602 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2603 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2604 | |
| 2605 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2606 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2607 | * @codec: HD-audio codec |
| 2608 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2609 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2610 | { |
| 2611 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2612 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2613 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2614 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2615 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2616 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2619 | /* pseudo device locking |
| 2620 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2621 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2622 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2623 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2624 | struct snd_card *card = bus->card; |
| 2625 | struct hda_codec *codec; |
| 2626 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2627 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2628 | if (card->shutdown) |
| 2629 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2630 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2631 | if (!list_empty(&card->ctl_files)) |
| 2632 | goto err_clear; |
| 2633 | |
| 2634 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2635 | int pcm; |
| 2636 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2637 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2638 | if (!cpcm->pcm) |
| 2639 | continue; |
| 2640 | if (cpcm->pcm->streams[0].substream_opened || |
| 2641 | cpcm->pcm->streams[1].substream_opened) |
| 2642 | goto err_clear; |
| 2643 | } |
| 2644 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2645 | spin_unlock(&card->files_lock); |
| 2646 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2647 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2648 | err_clear: |
| 2649 | card->shutdown = 0; |
| 2650 | err_unlock: |
| 2651 | spin_unlock(&card->files_lock); |
| 2652 | return -EINVAL; |
| 2653 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2654 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2655 | |
| 2656 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2657 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2658 | struct snd_card *card = bus->card; |
| 2659 | |
| 2660 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2661 | spin_lock(&card->files_lock); |
| 2662 | card->shutdown = 0; |
| 2663 | spin_unlock(&card->files_lock); |
| 2664 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2665 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2666 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2667 | /** |
| 2668 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2669 | * @codec: HD-audio codec |
| 2670 | * |
| 2671 | * This frees the all PCM and control elements assigned to the codec, and |
| 2672 | * clears the caches and restores the pin default configurations. |
| 2673 | * |
| 2674 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2675 | * returns zero. |
| 2676 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2677 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2678 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2679 | struct hda_bus *bus = codec->bus; |
| 2680 | struct snd_card *card = bus->card; |
| 2681 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2682 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2683 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2684 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2685 | |
| 2686 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2687 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2688 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2689 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2690 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2691 | #endif |
| 2692 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2693 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2694 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2695 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2696 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2697 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2698 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2699 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2700 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2701 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2702 | if (codec->patch_ops.free) |
| 2703 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2704 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2705 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2706 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2707 | codec->spec = NULL; |
| 2708 | free_hda_cache(&codec->amp_cache); |
| 2709 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2710 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2711 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2712 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2713 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2714 | snd_array_free(&codec->cvt_setups); |
| 2715 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2716 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2717 | codec->num_pcms = 0; |
| 2718 | codec->pcm_info = NULL; |
| 2719 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2720 | codec->slave_dig_outs = NULL; |
| 2721 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2722 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2723 | module_put(codec->owner); |
| 2724 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2727 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2728 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2729 | } |
| 2730 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2731 | 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] | 2732 | |
| 2733 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2734 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2735 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2736 | { |
| 2737 | struct hda_nid_item *items; |
| 2738 | const char * const *s; |
| 2739 | int i, err; |
| 2740 | |
| 2741 | items = codec->mixers.list; |
| 2742 | for (i = 0; i < codec->mixers.used; i++) { |
| 2743 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2744 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2745 | continue; |
| 2746 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2747 | char tmpname[sizeof(sctl->id.name)]; |
| 2748 | const char *name = *s; |
| 2749 | if (suffix) { |
| 2750 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2751 | name, suffix); |
| 2752 | name = tmpname; |
| 2753 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2754 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2755 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2756 | if (err) |
| 2757 | return err; |
| 2758 | break; |
| 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2765 | static int check_slave_present(struct hda_codec *codec, |
| 2766 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2767 | { |
| 2768 | return 1; |
| 2769 | } |
| 2770 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2771 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2772 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2773 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2774 | { |
| 2775 | int _tlv[4]; |
| 2776 | const int *tlv = NULL; |
| 2777 | int val = -1; |
| 2778 | |
| 2779 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2780 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2781 | mm_segment_t fs = get_fs(); |
| 2782 | set_fs(get_ds()); |
| 2783 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2784 | tlv = _tlv; |
| 2785 | set_fs(fs); |
| 2786 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2787 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2788 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2789 | int step = tlv[3]; |
| 2790 | step &= ~TLV_DB_SCALE_MUTE; |
| 2791 | if (!step) |
| 2792 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2793 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2794 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2795 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2796 | return -1; |
| 2797 | } |
| 2798 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2799 | val = -tlv[2] / step; |
| 2800 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2801 | return val; |
| 2802 | } |
| 2803 | |
| 2804 | /* call kctl->put with the given value(s) */ |
| 2805 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2806 | { |
| 2807 | struct snd_ctl_elem_value *ucontrol; |
| 2808 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2809 | if (!ucontrol) |
| 2810 | return -ENOMEM; |
| 2811 | ucontrol->value.integer.value[0] = val; |
| 2812 | ucontrol->value.integer.value[1] = val; |
| 2813 | kctl->put(kctl, ucontrol); |
| 2814 | kfree(ucontrol); |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
| 2818 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2819 | static int init_slave_0dB(struct hda_codec *codec, |
| 2820 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2821 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2822 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2823 | if (offset > 0) |
| 2824 | put_kctl_with_value(slave, offset); |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2829 | static int init_slave_unmute(struct hda_codec *codec, |
| 2830 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2831 | { |
| 2832 | return put_kctl_with_value(slave, 1); |
| 2833 | } |
| 2834 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2835 | static int add_slave(struct hda_codec *codec, |
| 2836 | void *data, struct snd_kcontrol *slave) |
| 2837 | { |
| 2838 | return snd_ctl_add_slave(data, slave); |
| 2839 | } |
| 2840 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2841 | /** |
| 2842 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2843 | * @codec: HD-audio codec |
| 2844 | * @name: vmaster control name |
| 2845 | * @tlv: TLV data (optional) |
| 2846 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2847 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2848 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2849 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2850 | * |
| 2851 | * Create a virtual master control with the given name. The TLV data |
| 2852 | * must be either NULL or a valid data. |
| 2853 | * |
| 2854 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2855 | * slave control name. All controls with these names are assigned to |
| 2856 | * the new virtual master control. |
| 2857 | * |
| 2858 | * This function returns zero if successful or a negative error code. |
| 2859 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2860 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2861 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2862 | const char *suffix, bool init_slave_vol, |
| 2863 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2864 | { |
| 2865 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2866 | int err; |
| 2867 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2868 | if (ctl_ret) |
| 2869 | *ctl_ret = NULL; |
| 2870 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2871 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2872 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2873 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2874 | return 0; |
| 2875 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2876 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2877 | if (!kctl) |
| 2878 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2879 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2880 | if (err < 0) |
| 2881 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2882 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2883 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2884 | if (err < 0) |
| 2885 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2886 | |
| 2887 | /* init with master mute & zero volume */ |
| 2888 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2889 | if (init_slave_vol) { |
| 2890 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2891 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2892 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2893 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2894 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2895 | if (ctl_ret) |
| 2896 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2897 | return 0; |
| 2898 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2899 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2900 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2901 | /* |
| 2902 | * mute-LED control using vmaster |
| 2903 | */ |
| 2904 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2905 | struct snd_ctl_elem_info *uinfo) |
| 2906 | { |
| 2907 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2908 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2909 | }; |
| 2910 | unsigned int index; |
| 2911 | |
| 2912 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2913 | uinfo->count = 1; |
| 2914 | uinfo->value.enumerated.items = 3; |
| 2915 | index = uinfo->value.enumerated.item; |
| 2916 | if (index >= 3) |
| 2917 | index = 2; |
| 2918 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2923 | struct snd_ctl_elem_value *ucontrol) |
| 2924 | { |
| 2925 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2926 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2927 | return 0; |
| 2928 | } |
| 2929 | |
| 2930 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2931 | struct snd_ctl_elem_value *ucontrol) |
| 2932 | { |
| 2933 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2934 | unsigned int old_mode = hook->mute_mode; |
| 2935 | |
| 2936 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2937 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2938 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2939 | if (old_mode == hook->mute_mode) |
| 2940 | return 0; |
| 2941 | snd_hda_sync_vmaster_hook(hook); |
| 2942 | return 1; |
| 2943 | } |
| 2944 | |
| 2945 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2946 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2947 | .name = "Mute-LED Mode", |
| 2948 | .info = vmaster_mute_mode_info, |
| 2949 | .get = vmaster_mute_mode_get, |
| 2950 | .put = vmaster_mute_mode_put, |
| 2951 | }; |
| 2952 | |
| 2953 | /* |
| 2954 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2955 | * "Mute-LED Mode" control is automatically created and associated with |
| 2956 | * the given hook. |
| 2957 | */ |
| 2958 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2959 | struct hda_vmaster_mute_hook *hook, |
| 2960 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2961 | { |
| 2962 | struct snd_kcontrol *kctl; |
| 2963 | |
| 2964 | if (!hook->hook || !hook->sw_kctl) |
| 2965 | return 0; |
| 2966 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2967 | hook->codec = codec; |
| 2968 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2969 | if (!expose_enum_ctl) |
| 2970 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2971 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2972 | if (!kctl) |
| 2973 | return -ENOMEM; |
| 2974 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2975 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2976 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* |
| 2979 | * Call the hook with the current value for synchronization |
| 2980 | * Should be called in init callback |
| 2981 | */ |
| 2982 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2983 | { |
| 2984 | if (!hook->hook || !hook->codec) |
| 2985 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2986 | /* don't call vmaster hook in the destructor since it might have |
| 2987 | * been already destroyed |
| 2988 | */ |
| 2989 | if (hook->codec->bus->shutdown) |
| 2990 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2991 | switch (hook->mute_mode) { |
| 2992 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2993 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2994 | break; |
| 2995 | default: |
| 2996 | hook->hook(hook->codec, hook->mute_mode); |
| 2997 | break; |
| 2998 | } |
| 2999 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3000 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 3001 | |
| 3002 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3003 | /** |
| 3004 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 3005 | * |
| 3006 | * The control element is supposed to have the private_value field |
| 3007 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3008 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3009 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 3010 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | { |
| 3012 | int chs = get_amp_channels(kcontrol); |
| 3013 | |
| 3014 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 3015 | uinfo->count = chs == 3 ? 2 : 1; |
| 3016 | uinfo->value.integer.min = 0; |
| 3017 | uinfo->value.integer.max = 1; |
| 3018 | return 0; |
| 3019 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3020 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3022 | /** |
| 3023 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 3024 | * |
| 3025 | * The control element is supposed to have the private_value field |
| 3026 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3027 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3028 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 3029 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | { |
| 3031 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3032 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3033 | int chs = get_amp_channels(kcontrol); |
| 3034 | int dir = get_amp_direction(kcontrol); |
| 3035 | int idx = get_amp_index(kcontrol); |
| 3036 | long *valp = ucontrol->value.integer.value; |
| 3037 | |
| 3038 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3039 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3040 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3042 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3043 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | return 0; |
| 3045 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3046 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3048 | /** |
| 3049 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 3050 | * |
| 3051 | * The control element is supposed to have the private_value field |
| 3052 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3053 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3054 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3055 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | { |
| 3057 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3058 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3059 | int chs = get_amp_channels(kcontrol); |
| 3060 | int dir = get_amp_direction(kcontrol); |
| 3061 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | long *valp = ucontrol->value.integer.value; |
| 3063 | int change = 0; |
| 3064 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3065 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3066 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3067 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3068 | HDA_AMP_MUTE, |
| 3069 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3070 | valp++; |
| 3071 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3072 | if (chs & 2) |
| 3073 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3074 | HDA_AMP_MUTE, |
| 3075 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3076 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3077 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | return change; |
| 3079 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3080 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | |
| 3082 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3083 | * bound volume controls |
| 3084 | * |
| 3085 | * bind multiple volumes (# indices, from 0) |
| 3086 | */ |
| 3087 | |
| 3088 | #define AMP_VAL_IDX_SHIFT 19 |
| 3089 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3090 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3091 | /** |
| 3092 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 3093 | * |
| 3094 | * The control element is supposed to have the private_value field |
| 3095 | * set up via HDA_BIND_MUTE*() macros. |
| 3096 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3097 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3098 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3099 | { |
| 3100 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3101 | unsigned long pval; |
| 3102 | int err; |
| 3103 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3104 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3105 | pval = kcontrol->private_value; |
| 3106 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3107 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3108 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3109 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3110 | return err; |
| 3111 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3112 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3113 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3114 | /** |
| 3115 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 3116 | * |
| 3117 | * The control element is supposed to have the private_value field |
| 3118 | * set up via HDA_BIND_MUTE*() macros. |
| 3119 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3120 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3121 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3122 | { |
| 3123 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3124 | unsigned long pval; |
| 3125 | int i, indices, err = 0, change = 0; |
| 3126 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3127 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3128 | pval = kcontrol->private_value; |
| 3129 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3130 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3131 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3132 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3133 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3134 | if (err < 0) |
| 3135 | break; |
| 3136 | change |= err; |
| 3137 | } |
| 3138 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3139 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3140 | return err < 0 ? err : change; |
| 3141 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3142 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3143 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3144 | /** |
| 3145 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 3146 | * |
| 3147 | * The control element is supposed to have the private_value field |
| 3148 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3149 | */ |
| 3150 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3151 | struct snd_ctl_elem_info *uinfo) |
| 3152 | { |
| 3153 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3154 | struct hda_bind_ctls *c; |
| 3155 | int err; |
| 3156 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3157 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3158 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3159 | kcontrol->private_value = *c->values; |
| 3160 | err = c->ops->info(kcontrol, uinfo); |
| 3161 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3162 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3163 | return err; |
| 3164 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3165 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3166 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3167 | /** |
| 3168 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 3169 | * |
| 3170 | * The control element is supposed to have the private_value field |
| 3171 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3172 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3173 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3174 | struct snd_ctl_elem_value *ucontrol) |
| 3175 | { |
| 3176 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3177 | struct hda_bind_ctls *c; |
| 3178 | int err; |
| 3179 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3180 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3181 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3182 | kcontrol->private_value = *c->values; |
| 3183 | err = c->ops->get(kcontrol, ucontrol); |
| 3184 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3185 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3186 | return err; |
| 3187 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3188 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3189 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3190 | /** |
| 3191 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3192 | * |
| 3193 | * The control element is supposed to have the private_value field |
| 3194 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3195 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3196 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3197 | struct snd_ctl_elem_value *ucontrol) |
| 3198 | { |
| 3199 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3200 | struct hda_bind_ctls *c; |
| 3201 | unsigned long *vals; |
| 3202 | int err = 0, change = 0; |
| 3203 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3204 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3205 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3206 | for (vals = c->values; *vals; vals++) { |
| 3207 | kcontrol->private_value = *vals; |
| 3208 | err = c->ops->put(kcontrol, ucontrol); |
| 3209 | if (err < 0) |
| 3210 | break; |
| 3211 | change |= err; |
| 3212 | } |
| 3213 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3214 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3215 | return err < 0 ? err : change; |
| 3216 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3217 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3218 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3219 | /** |
| 3220 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3221 | * |
| 3222 | * The control element is supposed to have the private_value field |
| 3223 | * set up via HDA_BIND_VOL() macro. |
| 3224 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3225 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3226 | unsigned int size, unsigned int __user *tlv) |
| 3227 | { |
| 3228 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3229 | struct hda_bind_ctls *c; |
| 3230 | int err; |
| 3231 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3232 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3233 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3234 | kcontrol->private_value = *c->values; |
| 3235 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3236 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3237 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3238 | return err; |
| 3239 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3240 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3241 | |
| 3242 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3243 | .info = snd_hda_mixer_amp_volume_info, |
| 3244 | .get = snd_hda_mixer_amp_volume_get, |
| 3245 | .put = snd_hda_mixer_amp_volume_put, |
| 3246 | .tlv = snd_hda_mixer_amp_tlv |
| 3247 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3248 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3249 | |
| 3250 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3251 | .info = snd_hda_mixer_amp_switch_info, |
| 3252 | .get = snd_hda_mixer_amp_switch_get, |
| 3253 | .put = snd_hda_mixer_amp_switch_put, |
| 3254 | .tlv = snd_hda_mixer_amp_tlv |
| 3255 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3256 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3257 | |
| 3258 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | * SPDIF out controls |
| 3260 | */ |
| 3261 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3262 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3263 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | { |
| 3265 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3266 | uinfo->count = 1; |
| 3267 | return 0; |
| 3268 | } |
| 3269 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3270 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3271 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | { |
| 3273 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3274 | IEC958_AES0_NONAUDIO | |
| 3275 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3276 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3277 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3278 | IEC958_AES1_CON_ORIGINAL; |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3282 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3283 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | { |
| 3285 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3286 | IEC958_AES0_NONAUDIO | |
| 3287 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3291 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3292 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | { |
| 3294 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3295 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3296 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3298 | mutex_lock(&codec->spdif_mutex); |
| 3299 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3300 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3301 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3302 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3303 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3304 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
| 3309 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3310 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3311 | */ |
| 3312 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3313 | { |
| 3314 | unsigned short val = 0; |
| 3315 | |
| 3316 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3317 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3319 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3321 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3322 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3323 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3325 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3326 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3327 | val |= AC_DIG1_EMPHASIS; |
| 3328 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3329 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3331 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3333 | } |
| 3334 | return val; |
| 3335 | } |
| 3336 | |
| 3337 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3338 | */ |
| 3339 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3340 | { |
| 3341 | unsigned int sbits = 0; |
| 3342 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3343 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3345 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3347 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3348 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3350 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3351 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3353 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3355 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3357 | sbits |= val & (0x7f << 8); |
| 3358 | } |
| 3359 | return sbits; |
| 3360 | } |
| 3361 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3362 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3363 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3364 | int verb, int val) |
| 3365 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3366 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3367 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3368 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3369 | d = codec->slave_dig_outs; |
| 3370 | if (!d) |
| 3371 | return; |
| 3372 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3373 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3377 | int dig1, int dig2) |
| 3378 | { |
| 3379 | if (dig1 != -1) |
| 3380 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3381 | if (dig2 != -1) |
| 3382 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3383 | } |
| 3384 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3385 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3386 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | { |
| 3388 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3389 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3390 | struct hda_spdif_out *spdif; |
| 3391 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | unsigned short val; |
| 3393 | int change; |
| 3394 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3395 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3396 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3397 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3398 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3400 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3401 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3402 | val = convert_from_spdif_status(spdif->status); |
| 3403 | val |= spdif->ctls & 1; |
| 3404 | change = spdif->ctls != val; |
| 3405 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3406 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3407 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3408 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | return change; |
| 3410 | } |
| 3411 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3412 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3414 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3415 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | { |
| 3417 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3418 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3419 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3421 | mutex_lock(&codec->spdif_mutex); |
| 3422 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3423 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3424 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | return 0; |
| 3426 | } |
| 3427 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3428 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3429 | int dig1, int dig2) |
| 3430 | { |
| 3431 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3432 | /* unmute amp switch (if any) */ |
| 3433 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3434 | (dig1 & AC_DIG1_ENABLE)) |
| 3435 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3436 | HDA_AMP_MUTE, 0); |
| 3437 | } |
| 3438 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3439 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3440 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | { |
| 3442 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3443 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3444 | struct hda_spdif_out *spdif; |
| 3445 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | unsigned short val; |
| 3447 | int change; |
| 3448 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3449 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3450 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3451 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3452 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3454 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3455 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3456 | spdif->ctls = val; |
| 3457 | if (change && nid != (u16)-1) |
| 3458 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3459 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | return change; |
| 3461 | } |
| 3462 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3463 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | { |
| 3465 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3466 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3467 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | .info = snd_hda_spdif_mask_info, |
| 3469 | .get = snd_hda_spdif_cmask_get, |
| 3470 | }, |
| 3471 | { |
| 3472 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3473 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3474 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | .info = snd_hda_spdif_mask_info, |
| 3476 | .get = snd_hda_spdif_pmask_get, |
| 3477 | }, |
| 3478 | { |
| 3479 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3480 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | .info = snd_hda_spdif_mask_info, |
| 3482 | .get = snd_hda_spdif_default_get, |
| 3483 | .put = snd_hda_spdif_default_put, |
| 3484 | }, |
| 3485 | { |
| 3486 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3487 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | .info = snd_hda_spdif_out_switch_info, |
| 3489 | .get = snd_hda_spdif_out_switch_get, |
| 3490 | .put = snd_hda_spdif_out_switch_put, |
| 3491 | }, |
| 3492 | { } /* end */ |
| 3493 | }; |
| 3494 | |
| 3495 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3496 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3498 | * @associated_nid: NID that new ctls associated with |
| 3499 | * @cvt_nid: converter NID |
| 3500 | * @type: HDA_PCM_TYPE_* |
| 3501 | * Creates controls related with the digital output. |
| 3502 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | * |
| 3504 | * Returns 0 if successful, or a negative error code. |
| 3505 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3506 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3507 | hda_nid_t associated_nid, |
| 3508 | hda_nid_t cvt_nid, |
| 3509 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | { |
| 3511 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3512 | struct snd_kcontrol *kctl; |
| 3513 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3514 | int idx = 0; |
| 3515 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3516 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3517 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3519 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3520 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3521 | idx = spdif_index; |
| 3522 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3523 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3524 | /* suppose a single SPDIF device */ |
| 3525 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3526 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3527 | if (!kctl) |
| 3528 | break; |
| 3529 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3530 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3531 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3532 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3533 | if (!bus->primary_dig_out_type) |
| 3534 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3535 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3536 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3537 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3538 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3539 | return -EBUSY; |
| 3540 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3541 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3542 | if (!spdif) |
| 3543 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3545 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3546 | if (!kctl) |
| 3547 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3548 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3549 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3550 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3551 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | return err; |
| 3553 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3554 | spdif->nid = cvt_nid; |
| 3555 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3556 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3557 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | return 0; |
| 3559 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3560 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3562 | /* get the hda_spdif_out entry from the given NID |
| 3563 | * call within spdif_mutex lock |
| 3564 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3565 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3566 | hda_nid_t nid) |
| 3567 | { |
| 3568 | int i; |
| 3569 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3570 | struct hda_spdif_out *spdif = |
| 3571 | snd_array_elem(&codec->spdif_out, i); |
| 3572 | if (spdif->nid == nid) |
| 3573 | return spdif; |
| 3574 | } |
| 3575 | return NULL; |
| 3576 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3577 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3578 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3579 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3580 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3581 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3582 | |
| 3583 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3584 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3585 | spdif->nid = (u16)-1; |
| 3586 | mutex_unlock(&codec->spdif_mutex); |
| 3587 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3588 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3589 | |
| 3590 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3591 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3592 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3593 | unsigned short val; |
| 3594 | |
| 3595 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3596 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3597 | if (spdif->nid != nid) { |
| 3598 | spdif->nid = nid; |
| 3599 | val = spdif->ctls; |
| 3600 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3601 | } |
| 3602 | mutex_unlock(&codec->spdif_mutex); |
| 3603 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3604 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3607 | * SPDIF sharing with analog output |
| 3608 | */ |
| 3609 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3610 | struct snd_ctl_elem_value *ucontrol) |
| 3611 | { |
| 3612 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3613 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3618 | struct snd_ctl_elem_value *ucontrol) |
| 3619 | { |
| 3620 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3621 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3622 | return 0; |
| 3623 | } |
| 3624 | |
| 3625 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3626 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3627 | .name = "IEC958 Default PCM Playback Switch", |
| 3628 | .info = snd_ctl_boolean_mono_info, |
| 3629 | .get = spdif_share_sw_get, |
| 3630 | .put = spdif_share_sw_put, |
| 3631 | }; |
| 3632 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3633 | /** |
| 3634 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3635 | * @codec: the HDA codec |
| 3636 | * @mout: multi-out instance |
| 3637 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3638 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3639 | struct hda_multi_out *mout) |
| 3640 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3641 | struct snd_kcontrol *kctl; |
| 3642 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3643 | if (!mout->dig_out_nid) |
| 3644 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3645 | |
| 3646 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3647 | if (!kctl) |
| 3648 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3649 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3650 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3651 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3652 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3653 | |
| 3654 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | * SPDIF input |
| 3656 | */ |
| 3657 | |
| 3658 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3659 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3660 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3661 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
| 3663 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3664 | |
| 3665 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3669 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3670 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | { |
| 3672 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3673 | hda_nid_t nid = kcontrol->private_value; |
| 3674 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3675 | int change; |
| 3676 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3677 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3679 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3681 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3682 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3684 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | return change; |
| 3686 | } |
| 3687 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3688 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3689 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | { |
| 3691 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3692 | hda_nid_t nid = kcontrol->private_value; |
| 3693 | unsigned short val; |
| 3694 | unsigned int sbits; |
| 3695 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3696 | 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] | 3697 | sbits = convert_to_spdif_status(val); |
| 3698 | ucontrol->value.iec958.status[0] = sbits; |
| 3699 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3700 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3701 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3702 | return 0; |
| 3703 | } |
| 3704 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3705 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | { |
| 3707 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3708 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | .info = snd_hda_spdif_in_switch_info, |
| 3710 | .get = snd_hda_spdif_in_switch_get, |
| 3711 | .put = snd_hda_spdif_in_switch_put, |
| 3712 | }, |
| 3713 | { |
| 3714 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3715 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3716 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | .info = snd_hda_spdif_mask_info, |
| 3718 | .get = snd_hda_spdif_in_status_get, |
| 3719 | }, |
| 3720 | { } /* end */ |
| 3721 | }; |
| 3722 | |
| 3723 | /** |
| 3724 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3725 | * @codec: the HDA codec |
| 3726 | * @nid: audio in widget NID |
| 3727 | * |
| 3728 | * Creates controls related with the SPDIF input. |
| 3729 | * Called from each patch supporting the SPDIF in. |
| 3730 | * |
| 3731 | * Returns 0 if successful, or a negative error code. |
| 3732 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3733 | 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] | 3734 | { |
| 3735 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3736 | struct snd_kcontrol *kctl; |
| 3737 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3738 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3740 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3741 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3742 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3743 | return -EBUSY; |
| 3744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3746 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3747 | if (!kctl) |
| 3748 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3750 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3751 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | return err; |
| 3753 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3754 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3755 | snd_hda_codec_read(codec, nid, 0, |
| 3756 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3757 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | return 0; |
| 3759 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3760 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3762 | /* |
| 3763 | * command cache |
| 3764 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3766 | /* 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] | 3767 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3768 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3769 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3770 | |
| 3771 | /** |
| 3772 | * snd_hda_codec_write_cache - send a single command with caching |
| 3773 | * @codec: the HDA codec |
| 3774 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3775 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3776 | * @verb: the verb to send |
| 3777 | * @parm: the parameter for the verb |
| 3778 | * |
| 3779 | * Send a single command without waiting for response. |
| 3780 | * |
| 3781 | * Returns 0 if successful, or a negative error code. |
| 3782 | */ |
| 3783 | 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] | 3784 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3785 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3786 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3787 | struct hda_cache_head *c; |
| 3788 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3789 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3790 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3791 | cache_only = codec->cached_write; |
| 3792 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3793 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3794 | if (err < 0) |
| 3795 | return err; |
| 3796 | } |
| 3797 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3798 | /* parm may contain the verb stuff for get/set amp */ |
| 3799 | verb = verb | (parm >> 8); |
| 3800 | parm &= 0xff; |
| 3801 | key = build_cmd_cache_key(nid, verb); |
| 3802 | mutex_lock(&codec->bus->cmd_mutex); |
| 3803 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3804 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3805 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3806 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3807 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3808 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3809 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3810 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3811 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3812 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3813 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3814 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3815 | * @codec: the HDA codec |
| 3816 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3817 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3818 | * @verb: the verb to send |
| 3819 | * @parm: the parameter for the verb |
| 3820 | * |
| 3821 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3822 | * command if the parameter is already identical with the cached value. |
| 3823 | * If not, it sends the command and refreshes the cache. |
| 3824 | * |
| 3825 | * Returns 0 if successful, or a negative error code. |
| 3826 | */ |
| 3827 | 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] | 3828 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3829 | { |
| 3830 | struct hda_cache_head *c; |
| 3831 | u32 key; |
| 3832 | |
| 3833 | /* parm may contain the verb stuff for get/set amp */ |
| 3834 | verb = verb | (parm >> 8); |
| 3835 | parm &= 0xff; |
| 3836 | key = build_cmd_cache_key(nid, verb); |
| 3837 | mutex_lock(&codec->bus->cmd_mutex); |
| 3838 | c = get_hash(&codec->cmd_cache, key); |
| 3839 | if (c && c->val == parm) { |
| 3840 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3841 | return 0; |
| 3842 | } |
| 3843 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3844 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3845 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3846 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3847 | |
| 3848 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3849 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3850 | * @codec: HD-audio codec |
| 3851 | * |
| 3852 | * Execute all verbs recorded in the command caches to resume. |
| 3853 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3854 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3855 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3856 | int i; |
| 3857 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3858 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3859 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3860 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3861 | struct hda_cache_head *buffer; |
| 3862 | u32 key; |
| 3863 | |
| 3864 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3865 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3866 | if (!key) |
| 3867 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3868 | if (!buffer->dirty) |
| 3869 | continue; |
| 3870 | buffer->dirty = 0; |
| 3871 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3872 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3873 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3874 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3875 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3876 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3877 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3878 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3879 | |
| 3880 | /** |
| 3881 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3882 | * @codec: the HDA codec |
| 3883 | * @seq: VERB array to send |
| 3884 | * |
| 3885 | * Send the commands sequentially from the given array. |
| 3886 | * Thte commands are recorded on cache for power-save and resume. |
| 3887 | * The array must be terminated with NID=0. |
| 3888 | */ |
| 3889 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3890 | const struct hda_verb *seq) |
| 3891 | { |
| 3892 | for (; seq->nid; seq++) |
| 3893 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3894 | seq->param); |
| 3895 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3896 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3897 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3898 | /** |
| 3899 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3900 | * @codec: HD-audio codec |
| 3901 | */ |
| 3902 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3903 | { |
| 3904 | snd_hda_codec_resume_amp(codec); |
| 3905 | snd_hda_codec_resume_cache(codec); |
| 3906 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3907 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3908 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3909 | 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] | 3910 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3911 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3912 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3913 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3914 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3915 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3916 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3917 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3918 | if (!(wcaps & AC_WCAP_POWER)) |
| 3919 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3920 | if (codec->power_filter) { |
| 3921 | state = codec->power_filter(codec, nid, power_state); |
| 3922 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3923 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3924 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3925 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3926 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3927 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3928 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3929 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3930 | |
| 3931 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3932 | * supported power states check |
| 3933 | */ |
| 3934 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3935 | unsigned int power_state) |
| 3936 | { |
| 3937 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3938 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3939 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3940 | return false; |
| 3941 | if (sup & power_state) |
| 3942 | return true; |
| 3943 | else |
| 3944 | return false; |
| 3945 | } |
| 3946 | |
| 3947 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3948 | * wait until the state is reached, returns the current state |
| 3949 | */ |
| 3950 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3951 | hda_nid_t fg, |
| 3952 | unsigned int power_state) |
| 3953 | { |
| 3954 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3955 | unsigned int state, actual_state; |
| 3956 | |
| 3957 | for (;;) { |
| 3958 | state = snd_hda_codec_read(codec, fg, 0, |
| 3959 | AC_VERB_GET_POWER_STATE, 0); |
| 3960 | if (state & AC_PWRST_ERROR) |
| 3961 | break; |
| 3962 | actual_state = (state >> 4) & 0x0f; |
| 3963 | if (actual_state == power_state) |
| 3964 | break; |
| 3965 | if (time_after_eq(jiffies, end_time)) |
| 3966 | break; |
| 3967 | /* wait until the codec reachs to the target state */ |
| 3968 | msleep(1); |
| 3969 | } |
| 3970 | return state; |
| 3971 | } |
| 3972 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3973 | /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3974 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3975 | hda_nid_t nid, |
| 3976 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3977 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3978 | if (nid == codec->afg || nid == codec->mfg) |
| 3979 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3980 | if (power_state == AC_PWRST_D3 && |
| 3981 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3982 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3983 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3984 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3985 | if (eapd & 0x02) |
| 3986 | return AC_PWRST_D0; |
| 3987 | } |
| 3988 | return power_state; |
| 3989 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3990 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3991 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3992 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3993 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3994 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3995 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3996 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3997 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3998 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3999 | int count; |
| 4000 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4001 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4002 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4003 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4004 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 4005 | if (codec->depop_delay < 0) |
| 4006 | msleep(codec->epss ? 10 : 100); |
| 4007 | else if (codec->depop_delay > 0) |
| 4008 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 4009 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 4010 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4011 | |
| 4012 | /* repeat power states setting at most 10 times*/ |
| 4013 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4014 | if (codec->patch_ops.set_power_state) |
| 4015 | codec->patch_ops.set_power_state(codec, fg, |
| 4016 | power_state); |
| 4017 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4018 | state = power_state; |
| 4019 | if (codec->power_filter) |
| 4020 | state = codec->power_filter(codec, fg, state); |
| 4021 | if (state == power_state || power_state != AC_PWRST_D3) |
| 4022 | snd_hda_codec_read(codec, fg, flags, |
| 4023 | AC_VERB_SET_POWER_STATE, |
| 4024 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 4025 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 4026 | } |
| 4027 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 4028 | if (!(state & AC_PWRST_ERROR)) |
| 4029 | break; |
| 4030 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 4031 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4032 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4033 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4034 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4035 | /* sync power states of all widgets; |
| 4036 | * this is called at the end of codec parsing |
| 4037 | */ |
| 4038 | static void sync_power_up_states(struct hda_codec *codec) |
| 4039 | { |
| 4040 | hda_nid_t nid = codec->start_nid; |
| 4041 | int i; |
| 4042 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4043 | /* don't care if no filter is used */ |
| 4044 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4045 | return; |
| 4046 | |
| 4047 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4048 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4049 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4050 | if (!(wcaps & AC_WCAP_POWER)) |
| 4051 | continue; |
| 4052 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4053 | if (target == AC_PWRST_D0) |
| 4054 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4055 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4056 | snd_hda_codec_write(codec, nid, 0, |
| 4057 | AC_VERB_SET_POWER_STATE, target); |
| 4058 | } |
| 4059 | } |
| 4060 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 4061 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4062 | /* execute additional init verbs */ |
| 4063 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4064 | { |
| 4065 | if (codec->init_verbs.list) |
| 4066 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4067 | } |
| 4068 | #else |
| 4069 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4070 | #endif |
| 4071 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4072 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4073 | /* |
| 4074 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4075 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4076 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4077 | 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] | 4078 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4079 | unsigned int state; |
| 4080 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4081 | codec->in_pm = 1; |
| 4082 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4083 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4084 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4085 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4086 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4087 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4088 | if (!in_wq) |
| 4089 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4090 | spin_lock(&codec->power_lock); |
| 4091 | snd_hda_update_power_acct(codec); |
| 4092 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4093 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4094 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4095 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4096 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4097 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4098 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4099 | } |
| 4100 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4101 | /* mark all entries of cmd and amp caches dirty */ |
| 4102 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4103 | { |
| 4104 | int i; |
| 4105 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4106 | struct hda_cache_head *cmd; |
| 4107 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4108 | cmd->dirty = 1; |
| 4109 | } |
| 4110 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4111 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4112 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4113 | amp->head.dirty = 1; |
| 4114 | } |
| 4115 | } |
| 4116 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4117 | /* |
| 4118 | * kick up codec; used both from PM and power-save |
| 4119 | */ |
| 4120 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4121 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4122 | codec->in_pm = 1; |
| 4123 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4124 | hda_mark_cmd_cache_dirty(codec); |
| 4125 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4126 | /* set as if powered on for avoiding re-entering the resume |
| 4127 | * in the resume / power-save sequence |
| 4128 | */ |
| 4129 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4130 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4131 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4132 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4133 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4134 | if (codec->patch_ops.resume) |
| 4135 | codec->patch_ops.resume(codec); |
| 4136 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4137 | if (codec->patch_ops.init) |
| 4138 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4139 | snd_hda_codec_resume_amp(codec); |
| 4140 | snd_hda_codec_resume_cache(codec); |
| 4141 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4142 | |
| 4143 | if (codec->jackpoll_interval) |
| 4144 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4145 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4146 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4147 | |
| 4148 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4149 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4150 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4151 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4152 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | /** |
| 4155 | * snd_hda_build_controls - build mixer controls |
| 4156 | * @bus: the BUS |
| 4157 | * |
| 4158 | * Creates mixer controls for each codec included in the bus. |
| 4159 | * |
| 4160 | * Returns 0 if successful, otherwise a negative error code. |
| 4161 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4162 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4164 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4166 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4167 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4168 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4169 | codec_err(codec, |
| 4170 | "cannot build controls for #%d (error %d)\n", |
| 4171 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4172 | err = snd_hda_codec_reset(codec); |
| 4173 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4174 | codec_err(codec, |
| 4175 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4176 | return err; |
| 4177 | } |
| 4178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4180 | return 0; |
| 4181 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4182 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4184 | /* |
| 4185 | * add standard channel maps if not specified |
| 4186 | */ |
| 4187 | static int add_std_chmaps(struct hda_codec *codec) |
| 4188 | { |
| 4189 | int i, str, err; |
| 4190 | |
| 4191 | for (i = 0; i < codec->num_pcms; i++) { |
| 4192 | for (str = 0; str < 2; str++) { |
| 4193 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4194 | struct hda_pcm_stream *hinfo = |
| 4195 | &codec->pcm_info[i].stream[str]; |
| 4196 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4197 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4198 | |
| 4199 | if (codec->pcm_info[i].own_chmap) |
| 4200 | continue; |
| 4201 | if (!pcm || !hinfo->substreams) |
| 4202 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4203 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4204 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4205 | hinfo->channels_max, |
| 4206 | 0, &chmap); |
| 4207 | if (err < 0) |
| 4208 | return err; |
| 4209 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4210 | } |
| 4211 | } |
| 4212 | return 0; |
| 4213 | } |
| 4214 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4215 | /* default channel maps for 2.1 speakers; |
| 4216 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4217 | */ |
| 4218 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4219 | { .channels = 2, |
| 4220 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4221 | { .channels = 4, |
| 4222 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4223 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4224 | { } |
| 4225 | }; |
| 4226 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4227 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4228 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4229 | { |
| 4230 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4231 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4232 | /* continue to initialize... */ |
| 4233 | if (codec->patch_ops.init) |
| 4234 | err = codec->patch_ops.init(codec); |
| 4235 | if (!err && codec->patch_ops.build_controls) |
| 4236 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4237 | if (err < 0) |
| 4238 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4239 | |
| 4240 | /* we create chmaps here instead of build_pcms */ |
| 4241 | err = add_std_chmaps(codec); |
| 4242 | if (err < 0) |
| 4243 | return err; |
| 4244 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4245 | if (codec->jackpoll_interval) |
| 4246 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4247 | else |
| 4248 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4249 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | return 0; |
| 4251 | } |
| 4252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | /* |
| 4254 | * stream formats |
| 4255 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4256 | struct hda_rate_tbl { |
| 4257 | unsigned int hz; |
| 4258 | unsigned int alsa_bits; |
| 4259 | unsigned int hda_fmt; |
| 4260 | }; |
| 4261 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4262 | /* rate = base * mult / div */ |
| 4263 | #define HDA_RATE(base, mult, div) \ |
| 4264 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4265 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4266 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4267 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4269 | |
| 4270 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4271 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4272 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4273 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4274 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4275 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4276 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4277 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4278 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4279 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4280 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4281 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4282 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4283 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4284 | |
| 4285 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4286 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4287 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4288 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | }; |
| 4290 | |
| 4291 | /** |
| 4292 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4293 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | * @rate: the sample rate |
| 4295 | * @channels: the number of channels |
| 4296 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4297 | * @maxbps: the max. bps |
| 4298 | * |
| 4299 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4300 | * |
| 4301 | * Return zero if invalid. |
| 4302 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4303 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4304 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | unsigned int channels, |
| 4306 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4307 | unsigned int maxbps, |
| 4308 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | { |
| 4310 | int i; |
| 4311 | unsigned int val = 0; |
| 4312 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4313 | for (i = 0; rate_bits[i].hz; i++) |
| 4314 | if (rate_bits[i].hz == rate) { |
| 4315 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | break; |
| 4317 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4318 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4319 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | return 0; |
| 4321 | } |
| 4322 | |
| 4323 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4324 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | return 0; |
| 4326 | } |
| 4327 | val |= channels - 1; |
| 4328 | |
| 4329 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4330 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4331 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4332 | break; |
| 4333 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4334 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4335 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | case 20: |
| 4337 | case 24: |
| 4338 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4339 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4340 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4342 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4344 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | break; |
| 4346 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4347 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4348 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | return 0; |
| 4350 | } |
| 4351 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4352 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4353 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | return val; |
| 4356 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4357 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4359 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4360 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4361 | { |
| 4362 | unsigned int val = 0; |
| 4363 | if (nid != codec->afg && |
| 4364 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4365 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4366 | if (!val || val == -1) |
| 4367 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4368 | if (!val || val == -1) |
| 4369 | return 0; |
| 4370 | return val; |
| 4371 | } |
| 4372 | |
| 4373 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4374 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4375 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4376 | get_pcm_param); |
| 4377 | } |
| 4378 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4379 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4380 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4381 | { |
| 4382 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4383 | if (!streams || streams == -1) |
| 4384 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4385 | if (!streams || streams == -1) |
| 4386 | return 0; |
| 4387 | return streams; |
| 4388 | } |
| 4389 | |
| 4390 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4391 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4392 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4393 | get_stream_param); |
| 4394 | } |
| 4395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | /** |
| 4397 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4398 | * @codec: the HDA codec |
| 4399 | * @nid: NID to query |
| 4400 | * @ratesp: the pointer to store the detected rate bitflags |
| 4401 | * @formatsp: the pointer to store the detected formats |
| 4402 | * @bpsp: the pointer to store the detected format widths |
| 4403 | * |
| 4404 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4405 | * or @bsps argument is ignored. |
| 4406 | * |
| 4407 | * Returns 0 if successful, otherwise a negative error code. |
| 4408 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4409 | 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] | 4410 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4411 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4412 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4414 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4415 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | |
| 4417 | if (ratesp) { |
| 4418 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4419 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4421 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4423 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4424 | codec_err(codec, |
| 4425 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4426 | nid, val, |
| 4427 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4428 | return -EIO; |
| 4429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | *ratesp = rates; |
| 4431 | } |
| 4432 | |
| 4433 | if (formatsp || bpsp) { |
| 4434 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4435 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4437 | streams = query_stream_param(codec, nid); |
| 4438 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | |
| 4441 | bps = 0; |
| 4442 | if (streams & AC_SUPFMT_PCM) { |
| 4443 | if (val & AC_SUPPCM_BITS_8) { |
| 4444 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4445 | bps = 8; |
| 4446 | } |
| 4447 | if (val & AC_SUPPCM_BITS_16) { |
| 4448 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4449 | bps = 16; |
| 4450 | } |
| 4451 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4452 | if (val & AC_SUPPCM_BITS_32) |
| 4453 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4454 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4455 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4456 | if (val & AC_SUPPCM_BITS_24) |
| 4457 | bps = 24; |
| 4458 | else if (val & AC_SUPPCM_BITS_20) |
| 4459 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4460 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4461 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4463 | if (val & AC_SUPPCM_BITS_32) |
| 4464 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | else if (val & AC_SUPPCM_BITS_24) |
| 4466 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4467 | else if (val & AC_SUPPCM_BITS_20) |
| 4468 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | } |
| 4470 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4471 | #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] | 4472 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4474 | if (!bps) |
| 4475 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4476 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4477 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4478 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4479 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | /* temporary hack: we have still no proper support |
| 4481 | * for the direct AC3 stream... |
| 4482 | */ |
| 4483 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4484 | bps = 8; |
| 4485 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4486 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4487 | codec_err(codec, |
| 4488 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4489 | nid, val, |
| 4490 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4491 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4492 | return -EIO; |
| 4493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | if (formatsp) |
| 4495 | *formatsp = formats; |
| 4496 | if (bpsp) |
| 4497 | *bpsp = bps; |
| 4498 | } |
| 4499 | |
| 4500 | return 0; |
| 4501 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4502 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
| 4504 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4505 | * snd_hda_is_supported_format - Check the validity of the format |
| 4506 | * @codec: HD-audio codec |
| 4507 | * @nid: NID to check |
| 4508 | * @format: the HD-audio format value to check |
| 4509 | * |
| 4510 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | * |
| 4512 | * Returns 1 if supported, 0 if not. |
| 4513 | */ |
| 4514 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4515 | unsigned int format) |
| 4516 | { |
| 4517 | int i; |
| 4518 | unsigned int val = 0, rate, stream; |
| 4519 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4520 | val = query_pcm_param(codec, nid); |
| 4521 | if (!val) |
| 4522 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | |
| 4524 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4525 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4526 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | if (val & (1 << i)) |
| 4528 | break; |
| 4529 | return 0; |
| 4530 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4531 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | return 0; |
| 4533 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4534 | stream = query_stream_param(codec, nid); |
| 4535 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | return 0; |
| 4537 | |
| 4538 | if (stream & AC_SUPFMT_PCM) { |
| 4539 | switch (format & 0xf0) { |
| 4540 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4541 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | return 0; |
| 4543 | break; |
| 4544 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4545 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | break; |
| 4548 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4549 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | return 0; |
| 4551 | break; |
| 4552 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4553 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | return 0; |
| 4555 | break; |
| 4556 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4557 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | return 0; |
| 4559 | break; |
| 4560 | default: |
| 4561 | return 0; |
| 4562 | } |
| 4563 | } else { |
| 4564 | /* FIXME: check for float32 and AC3? */ |
| 4565 | } |
| 4566 | |
| 4567 | return 1; |
| 4568 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4569 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | |
| 4571 | /* |
| 4572 | * PCM stuff |
| 4573 | */ |
| 4574 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4575 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4576 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | { |
| 4578 | return 0; |
| 4579 | } |
| 4580 | |
| 4581 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4582 | struct hda_codec *codec, |
| 4583 | unsigned int stream_tag, |
| 4584 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4585 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | { |
| 4587 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
| 4591 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4592 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4593 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4595 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | return 0; |
| 4597 | } |
| 4598 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4599 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4600 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4602 | int err; |
| 4603 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4604 | /* query support PCM information from the given NID */ |
| 4605 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4606 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4607 | info->rates ? NULL : &info->rates, |
| 4608 | info->formats ? NULL : &info->formats, |
| 4609 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4610 | if (err < 0) |
| 4611 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | } |
| 4613 | if (info->ops.open == NULL) |
| 4614 | info->ops.open = hda_pcm_default_open_close; |
| 4615 | if (info->ops.close == NULL) |
| 4616 | info->ops.close = hda_pcm_default_open_close; |
| 4617 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4618 | if (snd_BUG_ON(!info->nid)) |
| 4619 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | info->ops.prepare = hda_pcm_default_prepare; |
| 4621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4623 | if (snd_BUG_ON(!info->nid)) |
| 4624 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4626 | } |
| 4627 | return 0; |
| 4628 | } |
| 4629 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4630 | /* |
| 4631 | * codec prepare/cleanup entries |
| 4632 | */ |
| 4633 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4634 | struct hda_pcm_stream *hinfo, |
| 4635 | unsigned int stream, |
| 4636 | unsigned int format, |
| 4637 | struct snd_pcm_substream *substream) |
| 4638 | { |
| 4639 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4640 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4641 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4642 | if (ret >= 0) |
| 4643 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4644 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4645 | return ret; |
| 4646 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4647 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4648 | |
| 4649 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4650 | struct hda_pcm_stream *hinfo, |
| 4651 | struct snd_pcm_substream *substream) |
| 4652 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4653 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4654 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4655 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4656 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4657 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4658 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4659 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4660 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4661 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4662 | }; |
| 4663 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4664 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4665 | * get the empty PCM device number to assign |
| 4666 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4667 | 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] | 4668 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4669 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4670 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4671 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4672 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4673 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4674 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4675 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4676 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4677 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4678 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4679 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4680 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4681 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4682 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4683 | return -EINVAL; |
| 4684 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4685 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4686 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4687 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4688 | if (audio_idx[type][i] >= 8) |
| 4689 | break; |
| 4690 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4691 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4692 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4693 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4694 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4695 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4696 | /* non-fixed slots starting from 10 */ |
| 4697 | for (i = 10; i < 32; i++) { |
| 4698 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4699 | return i; |
| 4700 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4701 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4702 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4703 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4704 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4705 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4706 | dev_warn(bus->card->dev, |
| 4707 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4708 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4709 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4713 | * attach a new PCM stream |
| 4714 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4715 | 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] | 4716 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4717 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4718 | struct hda_pcm_stream *info; |
| 4719 | int stream, err; |
| 4720 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4721 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4722 | return -EINVAL; |
| 4723 | for (stream = 0; stream < 2; stream++) { |
| 4724 | info = &pcm->stream[stream]; |
| 4725 | if (info->substreams) { |
| 4726 | err = set_pcm_default_values(codec, info); |
| 4727 | if (err < 0) |
| 4728 | return err; |
| 4729 | } |
| 4730 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4731 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4732 | } |
| 4733 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4734 | /* assign all PCMs of the given codec */ |
| 4735 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4736 | { |
| 4737 | unsigned int pcm; |
| 4738 | int err; |
| 4739 | |
| 4740 | if (!codec->num_pcms) { |
| 4741 | if (!codec->patch_ops.build_pcms) |
| 4742 | return 0; |
| 4743 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4744 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4745 | codec_err(codec, |
| 4746 | "cannot build PCMs for #%d (error %d)\n", |
| 4747 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4748 | err = snd_hda_codec_reset(codec); |
| 4749 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4750 | codec_err(codec, |
| 4751 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4752 | return err; |
| 4753 | } |
| 4754 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4755 | } |
| 4756 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4757 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4758 | int dev; |
| 4759 | |
| 4760 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4761 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4762 | |
| 4763 | if (!cpcm->pcm) { |
| 4764 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4765 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4766 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4767 | cpcm->device = dev; |
| 4768 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4769 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4770 | codec_err(codec, |
| 4771 | "cannot attach PCM stream %d for codec #%d\n", |
| 4772 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4773 | continue; /* no fatal error */ |
| 4774 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | return 0; |
| 4778 | } |
| 4779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | /** |
| 4781 | * snd_hda_build_pcms - build PCM information |
| 4782 | * @bus: the BUS |
| 4783 | * |
| 4784 | * Create PCM information for each codec included in the bus. |
| 4785 | * |
| 4786 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4787 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4788 | * to create its PCM instances. |
| 4789 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4790 | * callback. |
| 4791 | * |
| 4792 | * At least, substreams, channels_min and channels_max must be filled for |
| 4793 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4794 | * When rates and/or formats are zero, the supported values are queried |
| 4795 | * from the given nid. The nid is used also by the default ops.prepare |
| 4796 | * and ops.cleanup callbacks. |
| 4797 | * |
| 4798 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4799 | * ops.close is supposed to be called in the close callback. |
| 4800 | * ops.prepare should be called in the prepare or hw_params callback |
| 4801 | * with the proper parameters for set up. |
| 4802 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4803 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4804 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4806 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4808 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4810 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4811 | int err = snd_hda_codec_build_pcms(codec); |
| 4812 | if (err < 0) |
| 4813 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | } |
| 4815 | return 0; |
| 4816 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4817 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | /** |
| 4820 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4821 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4822 | * @num_configs: number of config enums |
| 4823 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | * @tbl: configuration table, terminated by null entries |
| 4825 | * |
| 4826 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4827 | * given configuration table. If a matching entry is found, returns its |
| 4828 | * config value (supposed to be 0 or positive). |
| 4829 | * |
| 4830 | * If no entries are matching, the function returns a negative value. |
| 4831 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4832 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4833 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4834 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4836 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4837 | int i; |
| 4838 | for (i = 0; i < num_configs; i++) { |
| 4839 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4840 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4841 | codec_info(codec, "model '%s' is selected\n", |
| 4842 | models[i]); |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4843 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | } |
| 4847 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4848 | if (!codec->bus->pci || !tbl) |
| 4849 | return -1; |
| 4850 | |
| 4851 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4852 | if (!tbl) |
| 4853 | return -1; |
| 4854 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4855 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4856 | char tmp[10]; |
| 4857 | const char *model = NULL; |
| 4858 | if (models) |
| 4859 | model = models[tbl->value]; |
| 4860 | if (!model) { |
| 4861 | sprintf(tmp, "#%d", tbl->value); |
| 4862 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4864 | codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n", |
| 4865 | model, tbl->subvendor, tbl->subdevice, |
| 4866 | (tbl->name ? tbl->name : "Unknown device")); |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4867 | #endif |
| 4868 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | } |
| 4870 | return -1; |
| 4871 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4872 | EXPORT_SYMBOL_GPL(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | |
| 4874 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4875 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4876 | subsystem ID with the |
| 4877 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4878 | |
| 4879 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4880 | where the vendor ID of the PCI device is: |
| 4881 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4882 | and the vendor/subvendor are found only at the codec. |
| 4883 | |
| 4884 | * @codec: the HDA codec |
| 4885 | * @num_configs: number of config enums |
| 4886 | * @models: array of model name strings |
| 4887 | * @tbl: configuration table, terminated by null entries |
| 4888 | * |
| 4889 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4890 | * given configuration table. If a matching entry is found, returns its |
| 4891 | * config value (supposed to be 0 or positive). |
| 4892 | * |
| 4893 | * If no entries are matching, the function returns a negative value. |
| 4894 | */ |
| 4895 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4896 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4897 | const struct snd_pci_quirk *tbl) |
| 4898 | { |
| 4899 | const struct snd_pci_quirk *q; |
| 4900 | |
| 4901 | /* Search for codec ID */ |
| 4902 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4903 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4904 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4905 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | |
| 4909 | if (!q->subvendor) |
| 4910 | return -1; |
| 4911 | |
| 4912 | tbl = q; |
| 4913 | |
| 4914 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4915 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4916 | char tmp[10]; |
| 4917 | const char *model = NULL; |
| 4918 | if (models) |
| 4919 | model = models[tbl->value]; |
| 4920 | if (!model) { |
| 4921 | sprintf(tmp, "#%d", tbl->value); |
| 4922 | model = tmp; |
| 4923 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4924 | codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n", |
| 4925 | model, tbl->subvendor, tbl->subdevice, |
| 4926 | (tbl->name ? tbl->name : "Unknown device")); |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4927 | #endif |
| 4928 | return tbl->value; |
| 4929 | } |
| 4930 | return -1; |
| 4931 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4932 | EXPORT_SYMBOL_GPL(snd_hda_check_board_codec_sid_config); |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4933 | |
| 4934 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | * snd_hda_add_new_ctls - create controls from the array |
| 4936 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4937 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | * |
| 4939 | * This helper function creates and add new controls in the given array. |
| 4940 | * The array must be terminated with an empty entry as terminator. |
| 4941 | * |
| 4942 | * Returns 0 if successful, or a negative error code. |
| 4943 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4944 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4945 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4947 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
| 4949 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4950 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4951 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4952 | if (knew->iface == -1) /* skip this codec private value */ |
| 4953 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4954 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4955 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4956 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4957 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4958 | if (addr > 0) |
| 4959 | kctl->id.device = addr; |
| 4960 | if (idx > 0) |
| 4961 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4962 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4963 | if (!err) |
| 4964 | break; |
| 4965 | /* try first with another device index corresponding to |
| 4966 | * the codec addr; if it still fails (or it's the |
| 4967 | * primary codec), then try another control index |
| 4968 | */ |
| 4969 | if (!addr && codec->addr) |
| 4970 | addr = codec->addr; |
| 4971 | else if (!idx && !knew->index) { |
| 4972 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4973 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4974 | if (idx <= 0) |
| 4975 | return err; |
| 4976 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4977 | return err; |
| 4978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | } |
| 4980 | return 0; |
| 4981 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4982 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4984 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4985 | static void hda_power_work(struct work_struct *work) |
| 4986 | { |
| 4987 | struct hda_codec *codec = |
| 4988 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4989 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4990 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4991 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4992 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4993 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4994 | spin_unlock(&codec->power_lock); |
| 4995 | return; |
| 4996 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4997 | if (!codec->power_on || codec->power_count) { |
| 4998 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4999 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5000 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 5001 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5002 | spin_unlock(&codec->power_lock); |
| 5003 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 5004 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5005 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 5006 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | static void hda_keep_power_on(struct hda_codec *codec) |
| 5010 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5011 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5012 | codec->power_count++; |
| 5013 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5014 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5015 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 5016 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5019 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5020 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 5021 | { |
| 5022 | unsigned long delta = jiffies - codec->power_jiffies; |
| 5023 | if (codec->power_on) |
| 5024 | codec->power_on_acct += delta; |
| 5025 | else |
| 5026 | codec->power_off_acct += delta; |
| 5027 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5028 | } |
| 5029 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5030 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 5031 | * transition to D3 to complete. A pending D3 transition is indicated |
| 5032 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5033 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5034 | 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] | 5035 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5036 | /* Return if power_on or transitioning to power_on, unless currently |
| 5037 | * powering down. */ |
| 5038 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5039 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5040 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5041 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5042 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5043 | cancel_delayed_work_sync(&codec->power_work); |
| 5044 | |
| 5045 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5046 | /* If the power down delayed work was cancelled above before starting, |
| 5047 | * then there is no need to go through power up here. |
| 5048 | */ |
| 5049 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5050 | if (codec->power_transition < 0) |
| 5051 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5052 | return; |
| 5053 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5054 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5055 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5056 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5057 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5058 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5059 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5060 | spin_unlock(&codec->power_lock); |
| 5061 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5062 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5063 | |
| 5064 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5065 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5066 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5067 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5068 | #define power_save(codec) \ |
| 5069 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5070 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5071 | /* Transition to powered down */ |
| 5072 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5073 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5074 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5075 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5076 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5077 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5078 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5079 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5080 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5081 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | /** |
| 5085 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5086 | * @codec: HD-audio codec |
| 5087 | * @delta: the counter delta to change |
| 5088 | * |
| 5089 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5090 | * appropriately. For the power-down, queue to the delayed action. |
| 5091 | * Passing zero to @delta means to synchronize the power state. |
| 5092 | */ |
| 5093 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5094 | { |
| 5095 | spin_lock(&codec->power_lock); |
| 5096 | codec->power_count += delta; |
| 5097 | trace_hda_power_count(codec); |
| 5098 | if (delta > 0) |
| 5099 | __snd_hda_power_up(codec, d3wait); |
| 5100 | else |
| 5101 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5102 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5103 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5104 | EXPORT_SYMBOL_GPL(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5105 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5106 | /** |
| 5107 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5108 | * @codec: HD-audio codec |
| 5109 | * @check: the object containing an AMP list and the status |
| 5110 | * @nid: NID to check / update |
| 5111 | * |
| 5112 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5113 | * check the current AMP status, and update the the power-status according |
| 5114 | * to the mute status. |
| 5115 | * |
| 5116 | * This function is supposed to be set or called from the check_power_status |
| 5117 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5118 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5119 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5120 | struct hda_loopback_check *check, |
| 5121 | hda_nid_t nid) |
| 5122 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5123 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5124 | int ch, v; |
| 5125 | |
| 5126 | if (!check->amplist) |
| 5127 | return 0; |
| 5128 | for (p = check->amplist; p->nid; p++) { |
| 5129 | if (p->nid == nid) |
| 5130 | break; |
| 5131 | } |
| 5132 | if (!p->nid) |
| 5133 | return 0; /* nothing changed */ |
| 5134 | |
| 5135 | for (p = check->amplist; p->nid; p++) { |
| 5136 | for (ch = 0; ch < 2; ch++) { |
| 5137 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5138 | p->idx); |
| 5139 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5140 | if (!check->power_on) { |
| 5141 | check->power_on = 1; |
| 5142 | snd_hda_power_up(codec); |
| 5143 | } |
| 5144 | return 1; |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | if (check->power_on) { |
| 5149 | check->power_on = 0; |
| 5150 | snd_hda_power_down(codec); |
| 5151 | } |
| 5152 | return 0; |
| 5153 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5154 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5155 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5157 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5158 | * Channel mode helper |
| 5159 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5160 | |
| 5161 | /** |
| 5162 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 5163 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5164 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5165 | struct snd_ctl_elem_info *uinfo, |
| 5166 | const struct hda_channel_mode *chmode, |
| 5167 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5168 | { |
| 5169 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5170 | uinfo->count = 1; |
| 5171 | uinfo->value.enumerated.items = num_chmodes; |
| 5172 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5173 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5174 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5175 | chmode[uinfo->value.enumerated.item].channels); |
| 5176 | return 0; |
| 5177 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5178 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5179 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5180 | /** |
| 5181 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 5182 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5183 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5184 | struct snd_ctl_elem_value *ucontrol, |
| 5185 | const struct hda_channel_mode *chmode, |
| 5186 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5187 | int max_channels) |
| 5188 | { |
| 5189 | int i; |
| 5190 | |
| 5191 | for (i = 0; i < num_chmodes; i++) { |
| 5192 | if (max_channels == chmode[i].channels) { |
| 5193 | ucontrol->value.enumerated.item[0] = i; |
| 5194 | break; |
| 5195 | } |
| 5196 | } |
| 5197 | return 0; |
| 5198 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5199 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5200 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5201 | /** |
| 5202 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 5203 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5204 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5205 | struct snd_ctl_elem_value *ucontrol, |
| 5206 | const struct hda_channel_mode *chmode, |
| 5207 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5208 | int *max_channelsp) |
| 5209 | { |
| 5210 | unsigned int mode; |
| 5211 | |
| 5212 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5213 | if (mode >= num_chmodes) |
| 5214 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5215 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5216 | return 0; |
| 5217 | /* change the current channel setting */ |
| 5218 | *max_channelsp = chmode[mode].channels; |
| 5219 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5220 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5221 | return 1; |
| 5222 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5223 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | /* |
| 5226 | * input MUX helper |
| 5227 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5228 | |
| 5229 | /** |
| 5230 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5231 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5232 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5233 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | { |
| 5235 | unsigned int index; |
| 5236 | |
| 5237 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5238 | uinfo->count = 1; |
| 5239 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5240 | if (!imux->num_items) |
| 5241 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | index = uinfo->value.enumerated.item; |
| 5243 | if (index >= imux->num_items) |
| 5244 | index = imux->num_items - 1; |
| 5245 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5246 | return 0; |
| 5247 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5248 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5250 | /** |
| 5251 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5252 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5253 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5254 | const struct hda_input_mux *imux, |
| 5255 | struct snd_ctl_elem_value *ucontrol, |
| 5256 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | unsigned int *cur_val) |
| 5258 | { |
| 5259 | unsigned int idx; |
| 5260 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5261 | if (!imux->num_items) |
| 5262 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | idx = ucontrol->value.enumerated.item[0]; |
| 5264 | if (idx >= imux->num_items) |
| 5265 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5266 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5268 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5269 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | *cur_val = idx; |
| 5271 | return 1; |
| 5272 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5273 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | |
| 5275 | |
| 5276 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5277 | * process kcontrol info callback of a simple string enum array |
| 5278 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5279 | */ |
| 5280 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5281 | struct snd_ctl_elem_info *uinfo, |
| 5282 | int num_items, const char * const *texts) |
| 5283 | { |
| 5284 | static const char * const texts_default[] = { |
| 5285 | "Disabled", "Enabled" |
| 5286 | }; |
| 5287 | |
| 5288 | if (!texts || !num_items) { |
| 5289 | num_items = 2; |
| 5290 | texts = texts_default; |
| 5291 | } |
| 5292 | |
| 5293 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5294 | uinfo->count = 1; |
| 5295 | uinfo->value.enumerated.items = num_items; |
| 5296 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5297 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5298 | strcpy(uinfo->value.enumerated.name, |
| 5299 | texts[uinfo->value.enumerated.item]); |
| 5300 | return 0; |
| 5301 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5302 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5303 | |
| 5304 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | * Multi-channel / digital-out PCM helper functions |
| 5306 | */ |
| 5307 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5308 | /* setup SPDIF output stream */ |
| 5309 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5310 | unsigned int stream_tag, unsigned int format) |
| 5311 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5312 | struct hda_spdif_out *spdif; |
| 5313 | unsigned int curr_fmt; |
| 5314 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5315 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5316 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5317 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5318 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5319 | reset = codec->spdif_status_reset && |
| 5320 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5321 | curr_fmt != format; |
| 5322 | |
| 5323 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5324 | updated */ |
| 5325 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5326 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5327 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5328 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5329 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5330 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5331 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5332 | for (d = codec->slave_dig_outs; *d; d++) |
| 5333 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5334 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5335 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5336 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5337 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5338 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5339 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5340 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5341 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5342 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5343 | { |
| 5344 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5345 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5346 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5347 | for (d = codec->slave_dig_outs; *d; d++) |
| 5348 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5349 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5350 | } |
| 5351 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5352 | /** |
| 5353 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5354 | * @bus: HD-audio bus |
| 5355 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5356 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5357 | { |
| 5358 | struct hda_codec *codec; |
| 5359 | |
| 5360 | if (!bus) |
| 5361 | return; |
| 5362 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5363 | if (hda_codec_is_power_on(codec) && |
| 5364 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5365 | codec->patch_ops.reboot_notify(codec); |
| 5366 | } |
| 5367 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5368 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5369 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5370 | /** |
| 5371 | * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5373 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5374 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5376 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5377 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5378 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5379 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5381 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | return 0; |
| 5383 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5384 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5386 | /** |
| 5387 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5388 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5389 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5390 | struct hda_multi_out *mout, |
| 5391 | unsigned int stream_tag, |
| 5392 | unsigned int format, |
| 5393 | struct snd_pcm_substream *substream) |
| 5394 | { |
| 5395 | mutex_lock(&codec->spdif_mutex); |
| 5396 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5397 | mutex_unlock(&codec->spdif_mutex); |
| 5398 | return 0; |
| 5399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5400 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5401 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5402 | /** |
| 5403 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5404 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5405 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5406 | struct hda_multi_out *mout) |
| 5407 | { |
| 5408 | mutex_lock(&codec->spdif_mutex); |
| 5409 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5410 | mutex_unlock(&codec->spdif_mutex); |
| 5411 | return 0; |
| 5412 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5413 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5414 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5415 | /** |
| 5416 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5418 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5419 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5421 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5423 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | return 0; |
| 5425 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5426 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5428 | /** |
| 5429 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5430 | * |
| 5431 | * Open analog outputs and set up the hw-constraints. |
| 5432 | * If the digital outputs can be opened as slave, open the digital |
| 5433 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5435 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5436 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5437 | struct snd_pcm_substream *substream, |
| 5438 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5440 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5441 | runtime->hw.channels_max = mout->max_channels; |
| 5442 | if (mout->dig_out_nid) { |
| 5443 | if (!mout->analog_rates) { |
| 5444 | mout->analog_rates = hinfo->rates; |
| 5445 | mout->analog_formats = hinfo->formats; |
| 5446 | mout->analog_maxbps = hinfo->maxbps; |
| 5447 | } else { |
| 5448 | runtime->hw.rates = mout->analog_rates; |
| 5449 | runtime->hw.formats = mout->analog_formats; |
| 5450 | hinfo->maxbps = mout->analog_maxbps; |
| 5451 | } |
| 5452 | if (!mout->spdif_rates) { |
| 5453 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5454 | &mout->spdif_rates, |
| 5455 | &mout->spdif_formats, |
| 5456 | &mout->spdif_maxbps); |
| 5457 | } |
| 5458 | mutex_lock(&codec->spdif_mutex); |
| 5459 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5460 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5461 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5462 | runtime->hw.rates &= mout->spdif_rates; |
| 5463 | runtime->hw.formats &= mout->spdif_formats; |
| 5464 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5465 | hinfo->maxbps = mout->spdif_maxbps; |
| 5466 | } else { |
| 5467 | mout->share_spdif = 0; |
| 5468 | /* FIXME: need notify? */ |
| 5469 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5470 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5471 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5474 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5475 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5476 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5478 | /** |
| 5479 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5480 | * |
| 5481 | * Set up the i/o for analog out. |
| 5482 | * 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] | 5483 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5484 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5485 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | unsigned int stream_tag, |
| 5487 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5488 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5490 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5492 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | int i; |
| 5494 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5495 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5496 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5497 | if (mout->dig_out_nid && mout->share_spdif && |
| 5498 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5500 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5501 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5502 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5504 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5505 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | } else { |
| 5507 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5508 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | } |
| 5510 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5511 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | |
| 5513 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5514 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5515 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5516 | if (!mout->no_share_stream && |
| 5517 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5519 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5520 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5521 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5522 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5523 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5524 | snd_hda_codec_setup_stream(codec, |
| 5525 | mout->hp_out_nid[i], |
| 5526 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | /* surrounds */ |
| 5529 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5530 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5531 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5532 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5533 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5534 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5535 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5537 | |
| 5538 | /* extra surrounds */ |
| 5539 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5540 | int ch = 0; |
| 5541 | if (!mout->extra_out_nid[i]) |
| 5542 | break; |
| 5543 | if (chs >= (i + 1) * 2) |
| 5544 | ch = i * 2; |
| 5545 | else if (!mout->no_share_stream) |
| 5546 | break; |
| 5547 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5548 | stream_tag, ch, format); |
| 5549 | } |
| 5550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | return 0; |
| 5552 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5553 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5555 | /** |
| 5556 | * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5558 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5559 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5561 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | int i; |
| 5563 | |
| 5564 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5565 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5567 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5568 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5569 | if (mout->hp_out_nid[i]) |
| 5570 | snd_hda_codec_cleanup_stream(codec, |
| 5571 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5572 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5573 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5574 | snd_hda_codec_cleanup_stream(codec, |
| 5575 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5576 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | 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] | 5578 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | mout->dig_out_used = 0; |
| 5580 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5581 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | return 0; |
| 5583 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5584 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5586 | /** |
| 5587 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5588 | * |
| 5589 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5590 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5591 | */ |
| 5592 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5593 | { |
| 5594 | unsigned int pincap; |
| 5595 | unsigned int oldval; |
| 5596 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5597 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5598 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5599 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5600 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5601 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5602 | return AC_PINCTL_VREF_80; |
| 5603 | else if (pincap & AC_PINCAP_VREF_50) |
| 5604 | return AC_PINCTL_VREF_50; |
| 5605 | else if (pincap & AC_PINCAP_VREF_100) |
| 5606 | return AC_PINCTL_VREF_100; |
| 5607 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5608 | return AC_PINCTL_VREF_GRD; |
| 5609 | return AC_PINCTL_VREF_HIZ; |
| 5610 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5611 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5612 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5613 | /* correct the pin ctl value for matching with the pin cap */ |
| 5614 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5615 | hda_nid_t pin, unsigned int val) |
| 5616 | { |
| 5617 | static unsigned int cap_lists[][2] = { |
| 5618 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5619 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5620 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5621 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5622 | }; |
| 5623 | unsigned int cap; |
| 5624 | |
| 5625 | if (!val) |
| 5626 | return 0; |
| 5627 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5628 | if (!cap) |
| 5629 | return val; /* don't know what to do... */ |
| 5630 | |
| 5631 | if (val & AC_PINCTL_OUT_EN) { |
| 5632 | if (!(cap & AC_PINCAP_OUT)) |
| 5633 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5634 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5635 | val &= ~AC_PINCTL_HP_EN; |
| 5636 | } |
| 5637 | |
| 5638 | if (val & AC_PINCTL_IN_EN) { |
| 5639 | if (!(cap & AC_PINCAP_IN)) |
| 5640 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5641 | else { |
| 5642 | unsigned int vcap, vref; |
| 5643 | int i; |
| 5644 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5645 | vref = val & AC_PINCTL_VREFEN; |
| 5646 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5647 | if (vref == cap_lists[i][0] && |
| 5648 | !(vcap & cap_lists[i][1])) { |
| 5649 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5650 | vref = AC_PINCTL_VREF_HIZ; |
| 5651 | else |
| 5652 | vref = cap_lists[i + 1][0]; |
| 5653 | } |
| 5654 | } |
| 5655 | val &= ~AC_PINCTL_VREFEN; |
| 5656 | val |= vref; |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | return val; |
| 5661 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5662 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5663 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5664 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5665 | unsigned int val, bool cached) |
| 5666 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5667 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5668 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5669 | if (cached) |
| 5670 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5671 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5672 | else |
| 5673 | return snd_hda_codec_write(codec, pin, 0, |
| 5674 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5675 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5676 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5677 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5678 | /** |
| 5679 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5680 | * |
| 5681 | * When the same label is used already in the existing items, the number |
| 5682 | * suffix is appended to the label. This label index number is stored |
| 5683 | * to type_idx when non-NULL pointer is given. |
| 5684 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5685 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5686 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5687 | int index, int *type_idx) |
| 5688 | { |
| 5689 | int i, label_idx = 0; |
| 5690 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5691 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5692 | return -EINVAL; |
| 5693 | } |
| 5694 | for (i = 0; i < imux->num_items; i++) { |
| 5695 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5696 | label_idx++; |
| 5697 | } |
| 5698 | if (type_idx) |
| 5699 | *type_idx = label_idx; |
| 5700 | if (label_idx > 0) |
| 5701 | snprintf(imux->items[imux->num_items].label, |
| 5702 | sizeof(imux->items[imux->num_items].label), |
| 5703 | "%s %d", label, label_idx); |
| 5704 | else |
| 5705 | strlcpy(imux->items[imux->num_items].label, label, |
| 5706 | sizeof(imux->items[imux->num_items].label)); |
| 5707 | imux->items[imux->num_items].index = index; |
| 5708 | imux->num_items++; |
| 5709 | return 0; |
| 5710 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5711 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5712 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | #ifdef CONFIG_PM |
| 5715 | /* |
| 5716 | * power management |
| 5717 | */ |
| 5718 | |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5719 | |
| 5720 | static void hda_async_suspend(void *data, async_cookie_t cookie) |
| 5721 | { |
| 5722 | hda_call_codec_suspend(data, false); |
| 5723 | } |
| 5724 | |
| 5725 | static void hda_async_resume(void *data, async_cookie_t cookie) |
| 5726 | { |
| 5727 | hda_call_codec_resume(data); |
| 5728 | } |
| 5729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | /** |
| 5731 | * snd_hda_suspend - suspend the codecs |
| 5732 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | * |
| 5734 | * Returns 0 if successful. |
| 5735 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5736 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5738 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5739 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5741 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5742 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5743 | if (hda_codec_is_power_on(codec)) { |
| 5744 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5745 | async_schedule_domain(hda_async_suspend, codec, |
| 5746 | &domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5747 | else |
| 5748 | hda_call_codec_suspend(codec, false); |
| 5749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5751 | |
| 5752 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5753 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | return 0; |
| 5756 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5757 | EXPORT_SYMBOL_GPL(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | |
| 5759 | /** |
| 5760 | * snd_hda_resume - resume the codecs |
| 5761 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | * |
| 5763 | * Returns 0 if successful. |
| 5764 | */ |
| 5765 | int snd_hda_resume(struct hda_bus *bus) |
| 5766 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5767 | struct hda_codec *codec; |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5768 | ASYNC_DOMAIN_EXCLUSIVE(domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5770 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5771 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5772 | async_schedule_domain(hda_async_resume, codec, &domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5773 | else |
| 5774 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5776 | |
| 5777 | if (bus->num_codecs > 1) |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 5778 | async_synchronize_full_domain(&domain); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | return 0; |
| 5781 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5782 | EXPORT_SYMBOL_GPL(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5783 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5784 | |
| 5785 | /* |
| 5786 | * generic arrays |
| 5787 | */ |
| 5788 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5789 | /** |
| 5790 | * snd_array_new - get a new element from the given array |
| 5791 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5792 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5793 | * Get a new element from the given array. If it exceeds the |
| 5794 | * pre-allocated array size, re-allocate the array. |
| 5795 | * |
| 5796 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5797 | */ |
| 5798 | void *snd_array_new(struct snd_array *array) |
| 5799 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5800 | if (snd_BUG_ON(!array->elem_size)) |
| 5801 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5802 | if (array->used >= array->alloced) { |
| 5803 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5804 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5805 | void *nlist; |
| 5806 | if (snd_BUG_ON(num >= 4096)) |
| 5807 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5808 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5809 | if (!nlist) |
| 5810 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5811 | array->list = nlist; |
| 5812 | array->alloced = num; |
| 5813 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5814 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5815 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5816 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5817 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5818 | /** |
| 5819 | * snd_array_free - free the given array elements |
| 5820 | * @array: the array object |
| 5821 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5822 | void snd_array_free(struct snd_array *array) |
| 5823 | { |
| 5824 | kfree(array->list); |
| 5825 | array->used = 0; |
| 5826 | array->alloced = 0; |
| 5827 | array->list = NULL; |
| 5828 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5829 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5830 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5831 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5832 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5833 | * @pcm: PCM caps bits |
| 5834 | * @buf: the string buffer to write |
| 5835 | * @buflen: the max buffer length |
| 5836 | * |
| 5837 | * used by hda_proc.c and hda_eld.c |
| 5838 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5839 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5840 | { |
| 5841 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5842 | int i, j; |
| 5843 | |
| 5844 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5845 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5846 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5847 | |
| 5848 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5849 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5850 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5851 | |
| 5852 | MODULE_DESCRIPTION("HDA codec core"); |
| 5853 | MODULE_LICENSE("GPL"); |