Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Interface for Intel High Definition Audio Codec |
| 3 | * |
| 4 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * |
| 7 | * This driver is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This driver is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/pci.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
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" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { 0x434d, "C-Media" }, |
Takashi Iwai | 74c6113 | 2008-12-18 09:11:33 +0100 | [diff] [blame] | 71 | { 0x8086, "Intel" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 72 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | {} /* terminator */ |
| 74 | }; |
| 75 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 76 | static DEFINE_MUTEX(preset_mutex); |
| 77 | static LIST_HEAD(hda_preset_tables); |
| 78 | |
| 79 | int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) |
| 80 | { |
| 81 | mutex_lock(&preset_mutex); |
| 82 | list_add_tail(&preset->list, &hda_preset_tables); |
| 83 | mutex_unlock(&preset_mutex); |
| 84 | return 0; |
| 85 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 86 | EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 87 | |
| 88 | int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) |
| 89 | { |
| 90 | mutex_lock(&preset_mutex); |
| 91 | list_del(&preset->list); |
| 92 | mutex_unlock(&preset_mutex); |
| 93 | return 0; |
| 94 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 95 | EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_PM |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 98 | #define codec_in_pm(codec) ((codec)->in_pm) |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 99 | static void hda_suspend_work(struct work_struct *work); |
| 100 | static void hda_resume_work(struct work_struct *work); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 101 | static void hda_power_work(struct work_struct *work); |
| 102 | static void hda_keep_power_on(struct hda_codec *codec); |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 103 | #define hda_codec_is_power_on(codec) ((codec)->power_on) |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 104 | |
| 105 | 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] | 106 | { |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 107 | struct hda_bus *bus = codec->bus; |
| 108 | |
| 109 | if ((power_up && codec->pm_up_notified) || |
| 110 | (!power_up && !codec->pm_up_notified)) |
| 111 | return; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 112 | if (bus->ops.pm_notify) |
| 113 | bus->ops.pm_notify(bus, power_up); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 114 | codec->pm_up_notified = power_up; |
Takashi Iwai | 68467f5 | 2012-08-28 09:14:29 -0700 | [diff] [blame] | 115 | } |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 116 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 117 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 118 | #define codec_in_pm(codec) 0 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 119 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 120 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 121 | #define hda_call_pm_notify(codec, state) {} |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 122 | #endif |
| 123 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 124 | /** |
| 125 | * snd_hda_get_jack_location - Give a location string of the jack |
| 126 | * @cfg: pin default config value |
| 127 | * |
| 128 | * Parse the pin default config value and returns the string of the |
| 129 | * jack location, e.g. "Rear", "Front", etc. |
| 130 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 131 | const char *snd_hda_get_jack_location(u32 cfg) |
| 132 | { |
| 133 | static char *bases[7] = { |
| 134 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 135 | }; |
| 136 | static unsigned char specials_idx[] = { |
| 137 | 0x07, 0x08, |
| 138 | 0x17, 0x18, 0x19, |
| 139 | 0x37, 0x38 |
| 140 | }; |
| 141 | static char *specials[] = { |
| 142 | "Rear Panel", "Drive Bar", |
| 143 | "Riser", "HDMI", "ATAPI", |
| 144 | "Mobile-In", "Mobile-Out" |
| 145 | }; |
| 146 | int i; |
| 147 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 148 | if ((cfg & 0x0f) < 7) |
| 149 | return bases[cfg & 0x0f]; |
| 150 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 151 | if (cfg == specials_idx[i]) |
| 152 | return specials[i]; |
| 153 | } |
| 154 | return "UNKNOWN"; |
| 155 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 156 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 158 | /** |
| 159 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 160 | * @cfg: pin default config value |
| 161 | * |
| 162 | * Parse the pin default config value and returns the string of the |
| 163 | * jack connectivity, i.e. external or internal connection. |
| 164 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 165 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 166 | { |
| 167 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 168 | |
| 169 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 170 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 171 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 172 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 173 | /** |
| 174 | * snd_hda_get_jack_type - Give a type string of the jack |
| 175 | * @cfg: pin default config value |
| 176 | * |
| 177 | * Parse the pin default config value and returns the string of the |
| 178 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 179 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 180 | const char *snd_hda_get_jack_type(u32 cfg) |
| 181 | { |
| 182 | static char *jack_types[16] = { |
| 183 | "Line Out", "Speaker", "HP Out", "CD", |
| 184 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 185 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 186 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 190 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 191 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 192 | EXPORT_SYMBOL_HDA(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 193 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 196 | */ |
| 197 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 198 | 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] | 199 | unsigned int verb, unsigned int parm) |
| 200 | { |
| 201 | u32 val; |
| 202 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 203 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 204 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 205 | printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n", |
| 206 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 207 | return ~0; |
| 208 | } |
| 209 | |
| 210 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 211 | val |= (u32)nid << 20; |
| 212 | val |= verb << 8; |
| 213 | val |= parm; |
| 214 | return val; |
| 215 | } |
| 216 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 217 | /* |
| 218 | * Send and receive a verb |
| 219 | */ |
| 220 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 221 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 222 | { |
| 223 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 224 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 225 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 226 | if (cmd == ~0) |
| 227 | return -1; |
| 228 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 229 | if (res) |
| 230 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 231 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 232 | snd_hda_power_up(codec); |
| 233 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 234 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 235 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 236 | for (;;) { |
| 237 | trace_hda_send_cmd(codec, cmd); |
| 238 | err = bus->ops.command(bus, cmd); |
| 239 | if (err != -EAGAIN) |
| 240 | break; |
| 241 | /* process pending verbs */ |
| 242 | bus->ops.get_response(bus, codec->addr); |
| 243 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 244 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 245 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 246 | trace_hda_get_response(codec, *res); |
| 247 | } |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 248 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 249 | mutex_unlock(&bus->cmd_mutex); |
| 250 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 251 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 252 | if (bus->response_reset) { |
| 253 | snd_printd("hda_codec: resetting BUS due to " |
| 254 | "fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 255 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 256 | bus->ops.bus_reset(bus); |
| 257 | } |
| 258 | goto again; |
| 259 | } |
| 260 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 261 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 262 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 263 | return err; |
| 264 | } |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /** |
| 267 | * snd_hda_codec_read - send a command and get the response |
| 268 | * @codec: the HDA codec |
| 269 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 270 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | * @verb: the verb to send |
| 272 | * @parm: the parameter for the verb |
| 273 | * |
| 274 | * Send a single command and read the corresponding response. |
| 275 | * |
| 276 | * Returns the obtained response value, or -1 for an error. |
| 277 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 278 | 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] | 279 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | unsigned int verb, unsigned int parm) |
| 281 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 282 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 283 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 284 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 285 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return res; |
| 287 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 288 | EXPORT_SYMBOL_HDA(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * snd_hda_codec_write - send a single command without waiting for response |
| 292 | * @codec: the HDA codec |
| 293 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 294 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * @verb: the verb to send |
| 296 | * @parm: the parameter for the verb |
| 297 | * |
| 298 | * Send a single command without waiting for response. |
| 299 | * |
| 300 | * Returns 0 if successful, or a negative error code. |
| 301 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 302 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 303 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 305 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 306 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 307 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 308 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 310 | EXPORT_SYMBOL_HDA(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /** |
| 313 | * snd_hda_sequence_write - sequence writes |
| 314 | * @codec: the HDA codec |
| 315 | * @seq: VERB array to send |
| 316 | * |
| 317 | * Send the commands sequentially from the given array. |
| 318 | * The array must be terminated with NID=0. |
| 319 | */ |
| 320 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 321 | { |
| 322 | for (; seq->nid; seq++) |
| 323 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 324 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 325 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 329 | * @codec: the HDA codec |
| 330 | * @nid: NID to parse |
| 331 | * @start_id: the pointer to store the start NID |
| 332 | * |
| 333 | * Parse the NID and store the start NID of its sub-nodes. |
| 334 | * Returns the number of sub-nodes. |
| 335 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 336 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 337 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | unsigned int parm; |
| 340 | |
| 341 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 342 | if (parm == -1) |
| 343 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | *start_id = (parm >> 16) & 0x7fff; |
| 345 | return (int)(parm & 0x7fff); |
| 346 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 347 | EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | /* connection list element */ |
| 350 | struct hda_conn_list { |
| 351 | struct list_head list; |
| 352 | int len; |
| 353 | hda_nid_t nid; |
| 354 | hda_nid_t conns[0]; |
| 355 | }; |
| 356 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 357 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 358 | static struct hda_conn_list * |
| 359 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 360 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 361 | struct hda_conn_list *p; |
| 362 | list_for_each_entry(p, &codec->conn_list, list) { |
| 363 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 364 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 365 | } |
| 366 | return NULL; |
| 367 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 368 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 369 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 370 | const hda_nid_t *list) |
| 371 | { |
| 372 | struct hda_conn_list *p; |
| 373 | |
| 374 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 375 | if (!p) |
| 376 | return -ENOMEM; |
| 377 | p->len = len; |
| 378 | p->nid = nid; |
| 379 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 380 | list_add(&p->list, &codec->conn_list); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static void remove_conn_list(struct hda_codec *codec) |
| 385 | { |
| 386 | while (!list_empty(&codec->conn_list)) { |
| 387 | struct hda_conn_list *p; |
| 388 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 389 | list_del(&p->list); |
| 390 | kfree(p); |
| 391 | } |
| 392 | } |
| 393 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | /* read the connection and add to the cache */ |
| 395 | 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] | 396 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 397 | hda_nid_t list[32]; |
| 398 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 399 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 400 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 401 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 402 | if (len == -ENOSPC) { |
| 403 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 404 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 405 | if (!result) |
| 406 | return -ENOMEM; |
| 407 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 408 | } |
| 409 | if (len >= 0) |
| 410 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 411 | if (result != list) |
| 412 | kfree(result); |
| 413 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 414 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 415 | |
| 416 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 417 | * snd_hda_get_conn_list - get connection list |
| 418 | * @codec: the HDA codec |
| 419 | * @nid: NID to parse |
| 420 | * @len: number of connection list entries |
| 421 | * @listp: the pointer to store NID list |
| 422 | * |
| 423 | * Parses the connection list of the given widget and stores the pointer |
| 424 | * to the list of NIDs. |
| 425 | * |
| 426 | * Returns the number of connections, or a negative error code. |
| 427 | * |
| 428 | * Note that the returned pointer isn't protected against the list |
| 429 | * modification. If snd_hda_override_conn_list() might be called |
| 430 | * concurrently, protect with a mutex appropriately. |
| 431 | */ |
| 432 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 433 | const hda_nid_t **listp) |
| 434 | { |
| 435 | bool added = false; |
| 436 | |
| 437 | for (;;) { |
| 438 | int err; |
| 439 | const struct hda_conn_list *p; |
| 440 | |
| 441 | /* if the connection-list is already cached, read it */ |
| 442 | p = lookup_conn_list(codec, nid); |
| 443 | if (p) { |
| 444 | if (listp) |
| 445 | *listp = p->conns; |
| 446 | return p->len; |
| 447 | } |
| 448 | if (snd_BUG_ON(added)) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | err = read_and_add_raw_conns(codec, nid); |
| 452 | if (err < 0) |
| 453 | return err; |
| 454 | added = true; |
| 455 | } |
| 456 | } |
| 457 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_list); |
| 458 | |
| 459 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 460 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | * @codec: the HDA codec |
| 462 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 463 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | * @max_conns: max. number of connections to store |
| 465 | * |
| 466 | * Parses the connection list of the given widget and stores the list |
| 467 | * of NIDs. |
| 468 | * |
| 469 | * Returns the number of connections, or a negative error code. |
| 470 | */ |
| 471 | 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] | 472 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 473 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 474 | const hda_nid_t *list; |
| 475 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 476 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 477 | if (len > 0 && conn_list) { |
| 478 | if (len > max_conns) { |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 479 | snd_printk(KERN_ERR "hda_codec: " |
| 480 | "Too many connections %d for NID 0x%x\n", |
| 481 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 482 | return -EINVAL; |
| 483 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 484 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 485 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 486 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 487 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 488 | } |
| 489 | EXPORT_SYMBOL_HDA(snd_hda_get_connections); |
| 490 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 491 | /* return CONNLIST_LEN parameter of the given widget */ |
| 492 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 493 | { |
| 494 | unsigned int wcaps = get_wcaps(codec, nid); |
| 495 | unsigned int parm; |
| 496 | |
| 497 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 498 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 499 | return 0; |
| 500 | |
| 501 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 502 | if (parm == -1) |
| 503 | parm = 0; |
| 504 | return parm; |
| 505 | } |
| 506 | |
| 507 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 508 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 509 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 512 | /** |
| 513 | * snd_hda_get_raw_connections - copy connection list without cache |
| 514 | * @codec: the HDA codec |
| 515 | * @nid: NID to parse |
| 516 | * @conn_list: connection list array |
| 517 | * @max_conns: max. number of connections to store |
| 518 | * |
| 519 | * Like snd_hda_get_connections(), copy the connection list but without |
| 520 | * checking through the connection-list cache. |
| 521 | * Currently called only from hda_proc.c, so not exported. |
| 522 | */ |
| 523 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 524 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 527 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 529 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 530 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 532 | parm = get_num_conns(codec, nid); |
| 533 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 534 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | if (parm & AC_CLIST_LONG) { |
| 537 | /* long form */ |
| 538 | shift = 16; |
| 539 | num_elems = 2; |
| 540 | } else { |
| 541 | /* short form */ |
| 542 | shift = 8; |
| 543 | num_elems = 4; |
| 544 | } |
| 545 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | mask = (1 << (shift-1)) - 1; |
| 547 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 548 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return 0; /* no connection */ |
| 550 | |
| 551 | if (conn_len == 1) { |
| 552 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 553 | parm = snd_hda_codec_read(codec, nid, 0, |
| 554 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 555 | if (parm == -1 && codec->bus->rirb_error) |
| 556 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 557 | if (conn_list) |
| 558 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | /* multi connection */ |
| 563 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 564 | prev_nid = 0; |
| 565 | for (i = 0; i < conn_len; i++) { |
| 566 | int range_val; |
| 567 | hda_nid_t val, n; |
| 568 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 569 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 570 | parm = snd_hda_codec_read(codec, nid, 0, |
| 571 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 572 | if (parm == -1 && codec->bus->rirb_error) |
| 573 | return -EIO; |
| 574 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 575 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 576 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 577 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4758fed | 2013-10-17 17:56:25 +0200 | [diff] [blame] | 578 | snd_printdd("hda_codec: " |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 579 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
| 580 | nid, i, parm); |
| 581 | return 0; |
| 582 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 583 | parm >>= shift; |
| 584 | if (range_val) { |
| 585 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 586 | if (!prev_nid || prev_nid >= val) { |
| 587 | snd_printk(KERN_WARNING "hda_codec: " |
| 588 | "invalid dep_range_val %x:%x\n", |
| 589 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 590 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 592 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 593 | if (conn_list) { |
| 594 | if (conns >= max_conns) |
| 595 | return -ENOSPC; |
| 596 | conn_list[conns] = n; |
| 597 | } |
| 598 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 599 | } |
| 600 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 601 | if (conn_list) { |
| 602 | if (conns >= max_conns) |
| 603 | return -ENOSPC; |
| 604 | conn_list[conns] = val; |
| 605 | } |
| 606 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 608 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | return conns; |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 614 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 615 | * @codec: the HDA codec |
| 616 | * @nid: NID to parse |
| 617 | * @len: number of connection list entries |
| 618 | * @list: the list of connection entries |
| 619 | * |
| 620 | * Add or modify the given connection-list to the cache. If the corresponding |
| 621 | * cache already exists, invalidate it and append a new one. |
| 622 | * |
| 623 | * Returns zero or a negative error code. |
| 624 | */ |
| 625 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 626 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 628 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 629 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 630 | p = lookup_conn_list(codec, nid); |
| 631 | if (p) { |
| 632 | list_del(&p->list); |
| 633 | kfree(p); |
| 634 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 635 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 636 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 638 | EXPORT_SYMBOL_HDA(snd_hda_override_conn_list); |
| 639 | |
| 640 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 641 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 642 | * @codec: the HDA codec |
| 643 | * @mux: NID containing the list |
| 644 | * @nid: NID to select |
| 645 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 646 | * |
| 647 | * Parses the connection list of the widget @mux and checks whether the |
| 648 | * widget @nid is present. If it is, return the connection index. |
| 649 | * Otherwise it returns -1. |
| 650 | */ |
| 651 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 652 | hda_nid_t nid, int recursive) |
| 653 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 654 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 655 | int i, nums; |
| 656 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 657 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 658 | for (i = 0; i < nums; i++) |
| 659 | if (conn[i] == nid) |
| 660 | return i; |
| 661 | if (!recursive) |
| 662 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 663 | if (recursive > 10) { |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 664 | snd_printd("hda_codec: too deep connection for 0x%x\n", nid); |
| 665 | return -1; |
| 666 | } |
| 667 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 668 | for (i = 0; i < nums; i++) { |
| 669 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 670 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 671 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 672 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 673 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 674 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | EXPORT_SYMBOL_HDA(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 679 | |
| 680 | /* return DEVLIST_LEN parameter of the given widget */ |
| 681 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 682 | { |
| 683 | unsigned int wcaps = get_wcaps(codec, nid); |
| 684 | unsigned int parm; |
| 685 | |
| 686 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 687 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 688 | return 0; |
| 689 | |
| 690 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 691 | if (parm == -1 && codec->bus->rirb_error) |
| 692 | parm = 0; |
| 693 | return parm & AC_DEV_LIST_LEN_MASK; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * snd_hda_get_devices - copy device list without cache |
| 698 | * @codec: the HDA codec |
| 699 | * @nid: NID of the pin to parse |
| 700 | * @dev_list: device list array |
| 701 | * @max_devices: max. number of devices to store |
| 702 | * |
| 703 | * Copy the device list. This info is dynamic and so not cached. |
| 704 | * Currently called only from hda_proc.c, so not exported. |
| 705 | */ |
| 706 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 707 | u8 *dev_list, int max_devices) |
| 708 | { |
| 709 | unsigned int parm; |
| 710 | int i, dev_len, devices; |
| 711 | |
| 712 | parm = get_num_devices(codec, nid); |
| 713 | if (!parm) /* not multi-stream capable */ |
| 714 | return 0; |
| 715 | |
| 716 | dev_len = parm + 1; |
| 717 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 718 | |
| 719 | devices = 0; |
| 720 | while (devices < dev_len) { |
| 721 | parm = snd_hda_codec_read(codec, nid, 0, |
| 722 | AC_VERB_GET_DEVICE_LIST, devices); |
| 723 | if (parm == -1 && codec->bus->rirb_error) |
| 724 | break; |
| 725 | |
| 726 | for (i = 0; i < 8; i++) { |
| 727 | dev_list[devices] = (u8)parm; |
| 728 | parm >>= 4; |
| 729 | devices++; |
| 730 | if (devices >= dev_len) |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | return devices; |
| 735 | } |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | /** |
| 738 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 739 | * @bus: the BUS |
| 740 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 741 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 742 | * |
| 743 | * Adds the given event to the queue. The events are processed in |
| 744 | * the workqueue asynchronously. Call this function in the interrupt |
| 745 | * hanlder when RIRB receives an unsolicited event. |
| 746 | * |
| 747 | * Returns 0 if successful, or a negative error code. |
| 748 | */ |
| 749 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 750 | { |
| 751 | struct hda_bus_unsolicited *unsol; |
| 752 | unsigned int wp; |
| 753 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 754 | if (!bus || !bus->workq) |
| 755 | return 0; |
| 756 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 757 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 758 | unsol = bus->unsol; |
| 759 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | |
| 762 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 763 | unsol->wp = wp; |
| 764 | |
| 765 | wp <<= 1; |
| 766 | unsol->queue[wp] = res; |
| 767 | unsol->queue[wp + 1] = res_ex; |
| 768 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 769 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | return 0; |
| 772 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 773 | EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 776 | * process queued unsolicited events |
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 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 780 | struct hda_bus_unsolicited *unsol = |
| 781 | container_of(work, struct hda_bus_unsolicited, work); |
| 782 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | struct hda_codec *codec; |
| 784 | unsigned int rp, caddr, res; |
| 785 | |
| 786 | while (unsol->rp != unsol->wp) { |
| 787 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 788 | unsol->rp = rp; |
| 789 | rp <<= 1; |
| 790 | res = unsol->queue[rp]; |
| 791 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 792 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | continue; |
| 794 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 795 | if (codec && codec->patch_ops.unsol_event) |
| 796 | codec->patch_ops.unsol_event(codec, res); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * initialize unsolicited queue |
| 802 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 803 | static int init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
| 805 | struct hda_bus_unsolicited *unsol; |
| 806 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 807 | if (bus->unsol) /* already initialized */ |
| 808 | return 0; |
| 809 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 810 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 811 | if (!unsol) { |
| 812 | snd_printk(KERN_ERR "hda_codec: " |
| 813 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return -ENOMEM; |
| 815 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 816 | INIT_WORK(&unsol->work, process_unsol_events); |
| 817 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | bus->unsol = unsol; |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * destructor |
| 824 | */ |
| 825 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 826 | |
| 827 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 828 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 829 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 831 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return 0; |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 833 | if (bus->workq) |
| 834 | flush_workqueue(bus->workq); |
| 835 | if (bus->unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | kfree(bus->unsol); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 837 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | snd_hda_codec_free(codec); |
| 839 | } |
| 840 | if (bus->ops.private_free) |
| 841 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 842 | if (bus->workq) |
| 843 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 844 | |
| 845 | #ifdef CONFIG_PM |
| 846 | if (bus->pm_wq) |
| 847 | destroy_workqueue(bus->pm_wq); |
| 848 | #endif |
| 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | kfree(bus); |
| 851 | return 0; |
| 852 | } |
| 853 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 854 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
| 856 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 857 | bus->shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return snd_hda_bus_free(bus); |
| 859 | } |
| 860 | |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 861 | #ifdef CONFIG_SND_HDA_HWDEP |
| 862 | static int snd_hda_bus_dev_register(struct snd_device *device) |
| 863 | { |
| 864 | struct hda_bus *bus = device->device_data; |
| 865 | struct hda_codec *codec; |
| 866 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 867 | snd_hda_hwdep_add_sysfs(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 868 | snd_hda_hwdep_add_power_sysfs(codec); |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 869 | } |
| 870 | return 0; |
| 871 | } |
| 872 | #else |
| 873 | #define snd_hda_bus_dev_register NULL |
| 874 | #endif |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | /** |
| 877 | * snd_hda_bus_new - create a HDA bus |
| 878 | * @card: the card entry |
| 879 | * @temp: the template for hda_bus information |
| 880 | * @busp: the pointer to store the created bus instance |
| 881 | * |
| 882 | * Returns 0 if successful, or a negative error code. |
| 883 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 884 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 885 | const struct hda_bus_template *temp, |
| 886 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
| 888 | struct hda_bus *bus; |
| 889 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 890 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | d7ffba1 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 891 | .dev_register = snd_hda_bus_dev_register, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | .dev_free = snd_hda_bus_dev_free, |
| 893 | }; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 894 | #ifdef CONFIG_PM |
| 895 | char wqname[16]; |
| 896 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 898 | if (snd_BUG_ON(!temp)) |
| 899 | return -EINVAL; |
| 900 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 901 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | if (busp) |
| 904 | *busp = NULL; |
| 905 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 906 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | if (bus == NULL) { |
| 908 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 909 | return -ENOMEM; |
| 910 | } |
| 911 | |
| 912 | bus->card = card; |
| 913 | bus->private_data = temp->private_data; |
| 914 | bus->pci = temp->pci; |
| 915 | bus->modelname = temp->modelname; |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 916 | bus->power_save = temp->power_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | bus->ops = temp->ops; |
| 918 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 919 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 920 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | INIT_LIST_HEAD(&bus->codec_list); |
| 922 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 923 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 924 | "hd-audio%d", card->number); |
| 925 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 926 | if (!bus->workq) { |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 927 | snd_printk(KERN_ERR "cannot create workqueue %s\n", |
| 928 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 929 | kfree(bus); |
| 930 | return -ENOMEM; |
| 931 | } |
| 932 | |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 933 | #ifdef CONFIG_PM |
| 934 | sprintf(wqname, "hda-pm-wq-%d", card->number); |
| 935 | bus->pm_wq = create_workqueue(wqname); |
| 936 | if (!bus->pm_wq) { |
| 937 | snd_printk(KERN_ERR "cannot create PM workqueue\n"); |
| 938 | snd_hda_bus_free(bus); |
| 939 | return -ENOMEM; |
| 940 | } |
| 941 | #endif |
| 942 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 943 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 944 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | snd_hda_bus_free(bus); |
| 946 | return err; |
| 947 | } |
| 948 | if (busp) |
| 949 | *busp = bus; |
| 950 | return 0; |
| 951 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 952 | EXPORT_SYMBOL_HDA(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 954 | #ifdef CONFIG_SND_HDA_GENERIC |
| 955 | #define is_generic_config(codec) \ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 956 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 957 | #else |
| 958 | #define is_generic_config(codec) 0 |
| 959 | #endif |
| 960 | |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 961 | #ifdef MODULE |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 962 | #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ |
| 963 | #else |
Takashi Iwai | 645f10c | 2008-11-28 15:07:37 +0100 | [diff] [blame] | 964 | #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 965 | #endif |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | /* |
| 968 | * find a matching codec preset |
| 969 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 970 | static const struct hda_codec_preset * |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 971 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 973 | struct hda_codec_preset_list *tbl; |
| 974 | const struct hda_codec_preset *preset; |
Takashi Iwai | 5d908ab | 2012-08-24 18:40:10 +0200 | [diff] [blame] | 975 | unsigned int mod_requested = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 977 | again: |
| 978 | mutex_lock(&preset_mutex); |
| 979 | list_for_each_entry(tbl, &hda_preset_tables, list) { |
| 980 | if (!try_module_get(tbl->owner)) { |
| 981 | snd_printk(KERN_ERR "hda_codec: cannot module_get\n"); |
| 982 | continue; |
| 983 | } |
| 984 | for (preset = tbl->preset; preset->id; preset++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 986 | if (preset->afg && preset->afg != codec->afg) |
| 987 | continue; |
| 988 | if (preset->mfg && preset->mfg != codec->mfg) |
| 989 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 990 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 992 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 993 | (!preset->rev || |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 994 | preset->rev == codec->revision_id)) { |
| 995 | mutex_unlock(&preset_mutex); |
| 996 | codec->owner = tbl->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return preset; |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1000 | module_put(tbl->owner); |
| 1001 | } |
| 1002 | mutex_unlock(&preset_mutex); |
| 1003 | |
| 1004 | if (mod_requested < HDA_MODREQ_MAX_COUNT) { |
| 1005 | char name[32]; |
| 1006 | if (!mod_requested) |
| 1007 | snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", |
| 1008 | codec->vendor_id); |
| 1009 | else |
| 1010 | snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", |
| 1011 | (codec->vendor_id >> 16) & 0xffff); |
| 1012 | request_module(name); |
| 1013 | mod_requested++; |
| 1014 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1020 | * get_codec_name - store the codec name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | */ |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1022 | static int get_codec_name(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | { |
| 1024 | const struct hda_vendor_id *c; |
| 1025 | const char *vendor = NULL; |
| 1026 | u16 vendor_id = codec->vendor_id >> 16; |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1027 | char tmp[16]; |
| 1028 | |
| 1029 | if (codec->vendor_name) |
| 1030 | goto get_chip_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
| 1032 | for (c = hda_vendor_ids; c->id; c++) { |
| 1033 | if (c->id == vendor_id) { |
| 1034 | vendor = c->name; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1038 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | sprintf(tmp, "Generic %04x", vendor_id); |
| 1040 | vendor = tmp; |
| 1041 | } |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1042 | codec->vendor_name = kstrdup(vendor, GFP_KERNEL); |
| 1043 | if (!codec->vendor_name) |
| 1044 | return -ENOMEM; |
| 1045 | |
| 1046 | get_chip_name: |
| 1047 | if (codec->chip_name) |
| 1048 | return 0; |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | if (codec->preset && codec->preset->name) |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1051 | codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); |
| 1052 | else { |
| 1053 | sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); |
| 1054 | codec->chip_name = kstrdup(tmp, GFP_KERNEL); |
| 1055 | } |
| 1056 | if (!codec->chip_name) |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1057 | return -ENOMEM; |
| 1058 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1062 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1064 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1066 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | hda_nid_t nid; |
| 1068 | |
| 1069 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 1070 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 1071 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1072 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 1073 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1074 | case AC_GRP_AUDIO_FUNCTION: |
| 1075 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1076 | codec->afg_function_id = function_id & 0xff; |
| 1077 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1078 | break; |
| 1079 | case AC_GRP_MODEM_FUNCTION: |
| 1080 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 1081 | codec->mfg_function_id = function_id & 0xff; |
| 1082 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
| 1085 | break; |
| 1086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1091 | * read widget caps for each widget and store in cache |
| 1092 | */ |
| 1093 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 1094 | { |
| 1095 | int i; |
| 1096 | hda_nid_t nid; |
| 1097 | |
| 1098 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 1099 | &codec->start_nid); |
| 1100 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1101 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1102 | return -ENOMEM; |
| 1103 | nid = codec->start_nid; |
| 1104 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 1105 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 1106 | AC_PAR_AUDIO_WIDGET_CAP); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1110 | /* read all pin default configurations and save codec->init_pins */ |
| 1111 | static int read_pin_defaults(struct hda_codec *codec) |
| 1112 | { |
| 1113 | int i; |
| 1114 | hda_nid_t nid = codec->start_nid; |
| 1115 | |
| 1116 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1117 | struct hda_pincfg *pin; |
| 1118 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 1119 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1120 | if (wid_type != AC_WID_PIN) |
| 1121 | continue; |
| 1122 | pin = snd_array_new(&codec->init_pins); |
| 1123 | if (!pin) |
| 1124 | return -ENOMEM; |
| 1125 | pin->nid = nid; |
| 1126 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 1127 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1128 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 1129 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 1130 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /* look up the given pin config list and return the item matching with NID */ |
| 1136 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 1137 | struct snd_array *array, |
| 1138 | hda_nid_t nid) |
| 1139 | { |
| 1140 | int i; |
| 1141 | for (i = 0; i < array->used; i++) { |
| 1142 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 1143 | if (pin->nid == nid) |
| 1144 | return pin; |
| 1145 | } |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1149 | /* set the current pin config value for the given NID. |
| 1150 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 1151 | */ |
| 1152 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 1153 | hda_nid_t nid, unsigned int cfg) |
| 1154 | { |
| 1155 | struct hda_pincfg *pin; |
| 1156 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1157 | /* the check below may be invalid when pins are added by a fixup |
| 1158 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 1159 | * for now |
| 1160 | */ |
| 1161 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1162 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 1163 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 1164 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 1165 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1166 | pin = look_up_pincfg(codec, list, nid); |
| 1167 | if (!pin) { |
| 1168 | pin = snd_array_new(list); |
| 1169 | if (!pin) |
| 1170 | return -ENOMEM; |
| 1171 | pin->nid = nid; |
| 1172 | } |
| 1173 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1174 | return 0; |
| 1175 | } |
| 1176 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1177 | /** |
| 1178 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 1179 | * @codec: the HDA codec |
| 1180 | * @nid: NID to set the pin config |
| 1181 | * @cfg: the pin default config value |
| 1182 | * |
| 1183 | * Override a pin default configuration value in the cache. |
| 1184 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 1185 | * priority than the real hardware value. |
| 1186 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1187 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 1188 | hda_nid_t nid, unsigned int cfg) |
| 1189 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1190 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1191 | } |
| 1192 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg); |
| 1193 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1194 | /** |
| 1195 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 1196 | * @codec: the HDA codec |
| 1197 | * @nid: NID to get the pin config |
| 1198 | * |
| 1199 | * Get the current pin config value of the given pin NID. |
| 1200 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 1201 | * returns the cached value. |
| 1202 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1203 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 1204 | { |
| 1205 | struct hda_pincfg *pin; |
| 1206 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1207 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 1208 | { |
| 1209 | unsigned int cfg = 0; |
| 1210 | mutex_lock(&codec->user_mutex); |
| 1211 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 1212 | if (pin) |
| 1213 | cfg = pin->cfg; |
| 1214 | mutex_unlock(&codec->user_mutex); |
| 1215 | if (cfg) |
| 1216 | return cfg; |
| 1217 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1218 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 1219 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 1220 | if (pin) |
| 1221 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1222 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1223 | if (pin) |
| 1224 | return pin->cfg; |
| 1225 | return 0; |
| 1226 | } |
| 1227 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg); |
| 1228 | |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1229 | /* remember the current pinctl target value */ |
| 1230 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 1231 | unsigned int val) |
| 1232 | { |
| 1233 | struct hda_pincfg *pin; |
| 1234 | |
| 1235 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1236 | if (!pin) |
| 1237 | return -EINVAL; |
| 1238 | pin->target = val; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target); |
| 1242 | |
| 1243 | /* return the current pinctl target value */ |
| 1244 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1245 | { |
| 1246 | struct hda_pincfg *pin; |
| 1247 | |
| 1248 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1249 | if (!pin) |
| 1250 | return 0; |
| 1251 | return pin->target; |
| 1252 | } |
| 1253 | EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target); |
| 1254 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1255 | /** |
| 1256 | * snd_hda_shutup_pins - Shut up all pins |
| 1257 | * @codec: the HDA codec |
| 1258 | * |
| 1259 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1260 | * The controls aren't cached so that they can be resumed properly. |
| 1261 | */ |
| 1262 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1263 | { |
| 1264 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1265 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1266 | * the default pin setup at the next load of the driver |
| 1267 | */ |
| 1268 | if (codec->bus->shutdown) |
| 1269 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1270 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1271 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1272 | /* use read here for syncing after issuing each verb */ |
| 1273 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1274 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1275 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1276 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1277 | } |
| 1278 | EXPORT_SYMBOL_HDA(snd_hda_shutup_pins); |
| 1279 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1280 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1281 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1282 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1283 | { |
| 1284 | int i; |
| 1285 | if (!codec->pins_shutup) |
| 1286 | return; |
| 1287 | if (codec->bus->shutdown) |
| 1288 | return; |
| 1289 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1290 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1291 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1292 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1293 | pin->ctrl); |
| 1294 | } |
| 1295 | codec->pins_shutup = 0; |
| 1296 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1297 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1298 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1299 | static void hda_jackpoll_work(struct work_struct *work) |
| 1300 | { |
| 1301 | struct hda_codec *codec = |
| 1302 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1303 | |
| 1304 | snd_hda_jack_set_dirty_all(codec); |
| 1305 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1306 | |
| 1307 | if (!codec->jackpoll_interval) |
| 1308 | return; |
| 1309 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1310 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1311 | codec->jackpoll_interval); |
| 1312 | } |
| 1313 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1314 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1315 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1316 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1317 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1318 | /* release all pincfg lists */ |
| 1319 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1320 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1321 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1322 | #ifdef CONFIG_SND_HDA_HWDEP |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1323 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1324 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1325 | snd_array_free(&codec->init_pins); |
| 1326 | } |
| 1327 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1328 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1329 | * audio-converter setup caches |
| 1330 | */ |
| 1331 | struct hda_cvt_setup { |
| 1332 | hda_nid_t nid; |
| 1333 | u8 stream_tag; |
| 1334 | u8 channel_id; |
| 1335 | u16 format_id; |
| 1336 | unsigned char active; /* cvt is currently used */ |
| 1337 | unsigned char dirty; /* setups should be cleared */ |
| 1338 | }; |
| 1339 | |
| 1340 | /* get or create a cache entry for the given audio converter NID */ |
| 1341 | static struct hda_cvt_setup * |
| 1342 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1343 | { |
| 1344 | struct hda_cvt_setup *p; |
| 1345 | int i; |
| 1346 | |
| 1347 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1348 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1349 | if (p->nid == nid) |
| 1350 | return p; |
| 1351 | } |
| 1352 | p = snd_array_new(&codec->cvt_setups); |
| 1353 | if (p) |
| 1354 | p->nid = nid; |
| 1355 | return p; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1359 | * Dynamic symbol binding for the codec parsers |
| 1360 | */ |
| 1361 | #ifdef MODULE |
| 1362 | #define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym)) |
| 1363 | #define unload_parser_addr(addr) symbol_put_addr(addr) |
| 1364 | #else |
| 1365 | #define load_parser_sym(sym) (sym) |
| 1366 | #define unload_parser_addr(addr) do {} while (0) |
| 1367 | #endif |
| 1368 | |
| 1369 | #define load_parser(codec, sym) \ |
| 1370 | ((codec)->parser = load_parser_sym(sym)) |
| 1371 | |
| 1372 | static void unload_parser(struct hda_codec *codec) |
| 1373 | { |
| 1374 | if (codec->parser) { |
| 1375 | unload_parser_addr(codec->parser); |
| 1376 | codec->parser = NULL; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | * codec destructor |
| 1382 | */ |
| 1383 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1384 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1385 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1387 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1388 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1389 | free_init_pincfgs(codec); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1390 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1391 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1392 | flush_workqueue(codec->bus->workq); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1393 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1395 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1396 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1397 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1398 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1399 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 1401 | if (codec->patch_ops.free) |
| 1402 | codec->patch_ops.free(codec); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 1403 | hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1404 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 1405 | module_put(codec->owner); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1406 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1407 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1408 | kfree(codec->vendor_name); |
| 1409 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1410 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1411 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1412 | codec->bus->num_codecs--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | kfree(codec); |
| 1414 | } |
| 1415 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1416 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1417 | hda_nid_t fg, unsigned int power_state); |
| 1418 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1419 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1420 | unsigned int power_state); |
| 1421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /** |
| 1423 | * snd_hda_codec_new - create a HDA codec |
| 1424 | * @bus: the bus to assign |
| 1425 | * @codec_addr: the codec address |
| 1426 | * @codecp: the pointer to store the generated codec |
| 1427 | * |
| 1428 | * Returns 0 if successful, or a negative error code. |
| 1429 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1430 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1431 | unsigned int codec_addr, |
| 1432 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | { |
| 1434 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1435 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1436 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | int err; |
| 1438 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1439 | if (snd_BUG_ON(!bus)) |
| 1440 | return -EINVAL; |
| 1441 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1442 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
| 1444 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1445 | snd_printk(KERN_ERR "hda_codec: " |
| 1446 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | return -EBUSY; |
| 1448 | } |
| 1449 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1450 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | if (codec == NULL) { |
| 1452 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 1453 | return -ENOMEM; |
| 1454 | } |
| 1455 | |
| 1456 | codec->bus = bus; |
| 1457 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1458 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1459 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1460 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1461 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1462 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1463 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1464 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1465 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1466 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1467 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1468 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1469 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1470 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1471 | INIT_LIST_HEAD(&codec->conn_list); |
| 1472 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1473 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame^] | 1474 | codec->depop_delay = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1476 | #ifdef CONFIG_PM |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 1477 | spin_lock_init(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1478 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 1479 | INIT_WORK(&codec->suspend_work, hda_suspend_work); |
| 1480 | INIT_WORK(&codec->resume_work, hda_resume_work); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1481 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1482 | * the caller has to power down appropriatley after initialization |
| 1483 | * phase. |
| 1484 | */ |
| 1485 | hda_keep_power_on(codec); |
| 1486 | #endif |
| 1487 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1488 | if (codec->bus->modelname) { |
| 1489 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1490 | if (!codec->modelname) { |
| 1491 | snd_hda_codec_free(codec); |
| 1492 | return -ENODEV; |
| 1493 | } |
| 1494 | } |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1497 | bus->num_codecs++; |
| 1498 | #ifdef CONFIG_PM |
| 1499 | workqueue_set_max_active(bus->pm_wq, bus->num_codecs); |
| 1500 | #endif |
| 1501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | bus->caddr_tbl[codec_addr] = codec; |
| 1503 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1504 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1505 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1506 | if (codec->vendor_id == -1) |
| 1507 | /* read again, hopefully the access method was corrected |
| 1508 | * in the last read... |
| 1509 | */ |
| 1510 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1511 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1512 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1513 | AC_PAR_SUBSYSTEM_ID); |
| 1514 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1515 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1517 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1518 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1519 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1520 | err = -ENODEV; |
| 1521 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1524 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1525 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1526 | if (err < 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1527 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1528 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1529 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1530 | err = read_pin_defaults(codec); |
| 1531 | if (err < 0) |
| 1532 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1533 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1534 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1535 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1536 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1537 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1540 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1541 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1542 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1543 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1544 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1545 | AC_PWRST_EPSS); |
Takashi Iwai | 3e9bc58 | 2013-11-26 09:58:46 +0100 | [diff] [blame] | 1546 | #ifdef CONFIG_PM |
| 1547 | if (!codec->d3_stop_clk || !codec->epss) |
| 1548 | bus->power_keep_link_on = 1; |
| 1549 | #endif |
| 1550 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1551 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1552 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1553 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1554 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1555 | snd_hda_codec_proc_new(codec); |
| 1556 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1557 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1558 | |
| 1559 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1560 | codec->subsystem_id, codec->revision_id); |
| 1561 | snd_component_add(codec->bus->card, component); |
| 1562 | |
| 1563 | if (codecp) |
| 1564 | *codecp = codec; |
| 1565 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1566 | |
| 1567 | error: |
| 1568 | snd_hda_codec_free(codec); |
| 1569 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1570 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1571 | EXPORT_SYMBOL_HDA(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1572 | |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1573 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1574 | { |
| 1575 | hda_nid_t fg; |
| 1576 | int err; |
| 1577 | |
| 1578 | /* Assume the function group node does not change, |
| 1579 | * only the widget nodes may change. |
| 1580 | */ |
| 1581 | kfree(codec->wcaps); |
| 1582 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1583 | err = read_widget_caps(codec, fg); |
| 1584 | if (err < 0) { |
| 1585 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 1586 | return err; |
| 1587 | } |
| 1588 | |
| 1589 | snd_array_free(&codec->init_pins); |
| 1590 | err = read_pin_defaults(codec); |
| 1591 | |
| 1592 | return err; |
| 1593 | } |
| 1594 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets); |
| 1595 | |
| 1596 | |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1597 | #ifdef CONFIG_SND_HDA_CODEC_HDMI |
| 1598 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 1599 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 1600 | { |
| 1601 | hda_nid_t nid = codec->start_nid; |
| 1602 | int i; |
| 1603 | |
| 1604 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1605 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1606 | switch (get_wcaps_type(wcaps)) { |
| 1607 | case AC_WID_AUD_IN: |
| 1608 | return false; /* HDMI parser supports only HDMI out */ |
| 1609 | case AC_WID_AUD_OUT: |
| 1610 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 1611 | return false; |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | return true; |
| 1616 | } |
| 1617 | #else |
| 1618 | /* no HDMI codec parser support */ |
| 1619 | #define is_likely_hdmi_codec(codec) false |
| 1620 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 1621 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1622 | /** |
| 1623 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 1624 | * @codec: the HDA codec |
| 1625 | * |
| 1626 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 1627 | * patch instance. |
| 1628 | * |
| 1629 | * Returns 0 if successful or a negative error code. |
| 1630 | */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1631 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 1632 | { |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1633 | int (*patch)(struct hda_codec *) = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1634 | int err; |
| 1635 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 1636 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1637 | if (!codec->vendor_name || !codec->chip_name) { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1638 | err = get_codec_name(codec); |
| 1639 | if (err < 0) |
| 1640 | return err; |
| 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1643 | if (!is_generic_config(codec) && codec->preset) |
| 1644 | patch = codec->preset->patch; |
| 1645 | if (!patch) { |
| 1646 | unload_parser(codec); /* to be sure */ |
Takashi Iwai | f063927 | 2013-11-18 12:07:29 +0100 | [diff] [blame] | 1647 | if (is_likely_hdmi_codec(codec)) |
| 1648 | patch = load_parser(codec, snd_hda_parse_hdmi_codec); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1649 | #ifdef CONFIG_SND_HDA_GENERIC |
| 1650 | if (!patch) |
| 1651 | patch = load_parser(codec, snd_hda_parse_generic_codec); |
| 1652 | #endif |
| 1653 | if (!patch) { |
| 1654 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
| 1655 | return -ENODEV; |
| 1656 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1659 | err = patch(codec); |
| 1660 | if (err < 0) { |
| 1661 | unload_parser(codec); |
| 1662 | return err; |
| 1663 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 1664 | |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1665 | if (codec->patch_ops.unsol_event) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1666 | err = init_unsol_queue(codec->bus); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1667 | if (err < 0) |
| 1668 | return err; |
| 1669 | } |
| 1670 | |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1671 | /* audio codec should override the mixer name */ |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1672 | if (codec->afg || !*codec->bus->card->mixername) |
Takashi Iwai | f62faed | 2009-12-23 09:27:51 +0100 | [diff] [blame] | 1673 | snprintf(codec->bus->card->mixername, |
| 1674 | sizeof(codec->bus->card->mixername), |
| 1675 | "%s %s", codec->vendor_name, codec->chip_name); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 1676 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
Takashi Iwai | a1e21c9 | 2009-06-17 09:33:52 +0200 | [diff] [blame] | 1678 | EXPORT_SYMBOL_HDA(snd_hda_codec_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1680 | /* update the stream-id if changed */ |
| 1681 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1682 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1683 | u32 stream_tag, int channel_id) |
| 1684 | { |
| 1685 | unsigned int oldval, newval; |
| 1686 | |
| 1687 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1688 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1689 | newval = (stream_tag << 4) | channel_id; |
| 1690 | if (oldval != newval) |
| 1691 | snd_hda_codec_write(codec, nid, 0, |
| 1692 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1693 | newval); |
| 1694 | p->stream_tag = stream_tag; |
| 1695 | p->channel_id = channel_id; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | /* update the format-id if changed */ |
| 1700 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1701 | hda_nid_t nid, int format) |
| 1702 | { |
| 1703 | unsigned int oldval; |
| 1704 | |
| 1705 | if (p->format_id != format) { |
| 1706 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1707 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1708 | if (oldval != format) { |
| 1709 | msleep(1); |
| 1710 | snd_hda_codec_write(codec, nid, 0, |
| 1711 | AC_VERB_SET_STREAM_FORMAT, |
| 1712 | format); |
| 1713 | } |
| 1714 | p->format_id = format; |
| 1715 | } |
| 1716 | } |
| 1717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | /** |
| 1719 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1720 | * @codec: the CODEC to set up |
| 1721 | * @nid: the NID to set up |
| 1722 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1723 | * @channel_id: channel id to pass, zero based. |
| 1724 | * @format: stream format. |
| 1725 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1726 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1727 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | int channel_id, int format) |
| 1729 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1730 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1731 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1732 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1733 | int i; |
| 1734 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1735 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1736 | return; |
| 1737 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1738 | snd_printdd("hda_codec_setup_stream: " |
| 1739 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1741 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1742 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1743 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1744 | |
| 1745 | if (codec->pcm_format_first) |
| 1746 | update_pcm_format(codec, p, nid, format); |
| 1747 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1748 | if (!codec->pcm_format_first) |
| 1749 | update_pcm_format(codec, p, nid, format); |
| 1750 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1751 | p->active = 1; |
| 1752 | p->dirty = 0; |
| 1753 | |
| 1754 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1755 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1756 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1757 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1758 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1759 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1760 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1761 | p->dirty = 1; |
| 1762 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1765 | EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1767 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1768 | struct hda_cvt_setup *q); |
| 1769 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1770 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1771 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1772 | * @codec: the CODEC to clean up |
| 1773 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1774 | * @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] | 1775 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1776 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1777 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1778 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1779 | struct hda_cvt_setup *p; |
| 1780 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1781 | if (!nid) |
| 1782 | return; |
| 1783 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1784 | if (codec->no_sticky_stream) |
| 1785 | do_now = 1; |
| 1786 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1787 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1788 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1789 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1790 | /* here we just clear the active flag when do_now isn't set; |
| 1791 | * actual clean-ups will be done later in |
| 1792 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1793 | */ |
| 1794 | if (do_now) |
| 1795 | really_cleanup_stream(codec, p); |
| 1796 | else |
| 1797 | p->active = 0; |
| 1798 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1799 | } |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1800 | EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1801 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1802 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1803 | struct hda_cvt_setup *q) |
| 1804 | { |
| 1805 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1806 | if (q->stream_tag || q->channel_id) |
| 1807 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1808 | if (q->format_id) |
| 1809 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1810 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1811 | memset(q, 0, sizeof(*q)); |
| 1812 | q->nid = nid; |
| 1813 | } |
| 1814 | |
| 1815 | /* clean up the all conflicting obsolete streams */ |
| 1816 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1817 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1818 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1819 | int i; |
| 1820 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1821 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1822 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1823 | struct hda_cvt_setup *p; |
| 1824 | p = snd_array_elem(&c->cvt_setups, i); |
| 1825 | if (p->dirty) |
| 1826 | really_cleanup_stream(c, p); |
| 1827 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1831 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1832 | /* clean up all streams; called from suspend */ |
| 1833 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1834 | { |
| 1835 | int i; |
| 1836 | |
| 1837 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1838 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1839 | if (p->stream_tag) |
| 1840 | really_cleanup_stream(codec, p); |
| 1841 | } |
| 1842 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1843 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | /* |
| 1846 | * amp access functions |
| 1847 | */ |
| 1848 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1849 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1850 | #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] | 1851 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1852 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1853 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1855 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
| 1857 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1858 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1859 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1861 | memset(cache, 0, sizeof(*cache)); |
| 1862 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1863 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1864 | } |
| 1865 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1866 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1867 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1868 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1872 | 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] | 1873 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1874 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1875 | u16 cur = cache->hash[idx]; |
| 1876 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | |
| 1878 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1879 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | if (info->key == key) |
| 1881 | return info; |
| 1882 | cur = info->next; |
| 1883 | } |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1884 | return NULL; |
| 1885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1887 | /* query the hash. allocate an entry if not found. */ |
| 1888 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1889 | u32 key) |
| 1890 | { |
| 1891 | struct hda_cache_head *info = get_hash(cache, key); |
| 1892 | if (!info) { |
| 1893 | u16 idx, cur; |
| 1894 | /* add a new hash entry */ |
| 1895 | info = snd_array_new(&cache->buf); |
| 1896 | if (!info) |
| 1897 | return NULL; |
| 1898 | cur = snd_array_index(&cache->buf, info); |
| 1899 | info->key = key; |
| 1900 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1901 | info->dirty = 0; |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1902 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1903 | info->next = cache->hash[idx]; |
| 1904 | cache->hash[idx] = cur; |
| 1905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | return info; |
| 1907 | } |
| 1908 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1909 | /* query and allocate an amp hash entry */ |
| 1910 | static inline struct hda_amp_info * |
| 1911 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1912 | { |
| 1913 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1914 | } |
| 1915 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1916 | /* overwrite the value with the key in the caps hash */ |
| 1917 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1918 | { |
| 1919 | struct hda_amp_info *info; |
| 1920 | |
| 1921 | mutex_lock(&codec->hash_mutex); |
| 1922 | info = get_alloc_amp_hash(codec, key); |
| 1923 | if (!info) { |
| 1924 | mutex_unlock(&codec->hash_mutex); |
| 1925 | return -EINVAL; |
| 1926 | } |
| 1927 | info->amp_caps = val; |
| 1928 | info->head.val |= INFO_AMP_CAPS; |
| 1929 | mutex_unlock(&codec->hash_mutex); |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | /* query the value from the caps hash; if not found, fetch the current |
| 1934 | * value from the given function and store in the hash |
| 1935 | */ |
| 1936 | static unsigned int |
| 1937 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1938 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1939 | { |
| 1940 | struct hda_amp_info *info; |
| 1941 | unsigned int val; |
| 1942 | |
| 1943 | mutex_lock(&codec->hash_mutex); |
| 1944 | info = get_alloc_amp_hash(codec, key); |
| 1945 | if (!info) { |
| 1946 | mutex_unlock(&codec->hash_mutex); |
| 1947 | return 0; |
| 1948 | } |
| 1949 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1950 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1951 | val = func(codec, nid, dir); |
| 1952 | write_caps_hash(codec, key, val); |
| 1953 | } else { |
| 1954 | val = info->amp_caps; |
| 1955 | mutex_unlock(&codec->hash_mutex); |
| 1956 | } |
| 1957 | return val; |
| 1958 | } |
| 1959 | |
| 1960 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1961 | int direction) |
| 1962 | { |
| 1963 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1964 | nid = codec->afg; |
| 1965 | return snd_hda_param_read(codec, nid, |
| 1966 | direction == HDA_OUTPUT ? |
| 1967 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1968 | } |
| 1969 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1970 | /** |
| 1971 | * query_amp_caps - query AMP capabilities |
| 1972 | * @codec: the HD-auio codec |
| 1973 | * @nid: the NID to query |
| 1974 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1975 | * |
| 1976 | * Query AMP capabilities for the given widget and direction. |
| 1977 | * Returns the obtained capability bits. |
| 1978 | * |
| 1979 | * When cap bits have been already read, this doesn't read again but |
| 1980 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1982 | 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] | 1983 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1984 | return query_caps_hash(codec, nid, direction, |
| 1985 | HDA_HASH_KEY(nid, direction, 0), |
| 1986 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 1988 | EXPORT_SYMBOL_HDA(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1990 | /** |
| 1991 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1992 | * @codec: the CODEC to clean up |
| 1993 | * @nid: the NID to clean up |
| 1994 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1995 | * @caps: the capability bits to set |
| 1996 | * |
| 1997 | * Override the cached AMP caps bits value by the given one. |
| 1998 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1999 | * e.g. limit to 0dB, etc. |
| 2000 | * |
| 2001 | * Returns zero if successful or a negative error code. |
| 2002 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2003 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2004 | unsigned int caps) |
| 2005 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2006 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2007 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2008 | EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 2009 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2010 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 2011 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2012 | { |
| 2013 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 2014 | } |
| 2015 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2016 | /** |
| 2017 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 2018 | * @codec: the HD-auio codec |
| 2019 | * @nid: the NID to query |
| 2020 | * |
| 2021 | * Query PIN capabilities for the given widget. |
| 2022 | * Returns the obtained capability bits. |
| 2023 | * |
| 2024 | * When cap bits have been already read, this doesn't read again but |
| 2025 | * returns the cached value. |
| 2026 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2027 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 2028 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2029 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 2030 | read_pin_cap); |
| 2031 | } |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 2032 | EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps); |
| 2033 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 2034 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2035 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 2036 | * @codec: the CODEC |
| 2037 | * @nid: the NID to override |
| 2038 | * @caps: the capability bits to set |
| 2039 | * |
| 2040 | * Override the cached PIN capabilitiy bits value by the given one. |
| 2041 | * |
| 2042 | * Returns zero if successful or a negative error code. |
| 2043 | */ |
| 2044 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 2045 | unsigned int caps) |
| 2046 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2047 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 2048 | } |
| 2049 | EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps); |
| 2050 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2051 | /* read or sync the hash value with the current value; |
| 2052 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2054 | static struct hda_amp_info * |
| 2055 | 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] | 2056 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2058 | struct hda_amp_info *info; |
| 2059 | unsigned int parm, val = 0; |
| 2060 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2062 | retry: |
| 2063 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 2064 | if (!info) |
| 2065 | return NULL; |
| 2066 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 2067 | if (!val_read) { |
| 2068 | mutex_unlock(&codec->hash_mutex); |
| 2069 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 2070 | parm |= direction == HDA_OUTPUT ? |
| 2071 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 2072 | parm |= index; |
| 2073 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2074 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2075 | val &= 0xff; |
| 2076 | val_read = true; |
| 2077 | mutex_lock(&codec->hash_mutex); |
| 2078 | goto retry; |
| 2079 | } |
| 2080 | info->vol[ch] = val; |
| 2081 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2082 | } else if (init_only) |
| 2083 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2084 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2088 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2090 | 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] | 2091 | hda_nid_t nid, int ch, int direction, int index, |
| 2092 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
| 2094 | u32 parm; |
| 2095 | |
| 2096 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 2097 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 2098 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2099 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 2100 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2101 | ; /* set the zero value as a fake mute */ |
| 2102 | else |
| 2103 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 2105 | } |
| 2106 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2107 | /** |
| 2108 | * snd_hda_codec_amp_read - Read AMP value |
| 2109 | * @codec: HD-audio codec |
| 2110 | * @nid: NID to read the AMP value |
| 2111 | * @ch: channel (left=0 or right=1) |
| 2112 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2113 | * @index: the index value (only for input direction) |
| 2114 | * |
| 2115 | * 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] | 2116 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2117 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2118 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2120 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2121 | unsigned int val = 0; |
| 2122 | |
| 2123 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2124 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 2125 | if (info) |
| 2126 | val = info->vol[ch]; |
| 2127 | mutex_unlock(&codec->hash_mutex); |
| 2128 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2130 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2132 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2133 | int direction, int idx, int mask, int val, |
| 2134 | bool init_only) |
| 2135 | { |
| 2136 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2137 | unsigned int caps; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2138 | unsigned int cache_only; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2139 | |
| 2140 | if (snd_BUG_ON(mask & ~0xff)) |
| 2141 | mask &= 0xff; |
| 2142 | val &= mask; |
| 2143 | |
| 2144 | mutex_lock(&codec->hash_mutex); |
| 2145 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 2146 | if (!info) { |
| 2147 | mutex_unlock(&codec->hash_mutex); |
| 2148 | return 0; |
| 2149 | } |
| 2150 | val |= info->vol[ch] & ~mask; |
| 2151 | if (info->vol[ch] == val) { |
| 2152 | mutex_unlock(&codec->hash_mutex); |
| 2153 | return 0; |
| 2154 | } |
| 2155 | info->vol[ch] = val; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2156 | cache_only = info->head.dirty = codec->cached_write; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2157 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2158 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 2159 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2160 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2161 | return 1; |
| 2162 | } |
| 2163 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2164 | /** |
| 2165 | * snd_hda_codec_amp_update - update the AMP value |
| 2166 | * @codec: HD-audio codec |
| 2167 | * @nid: NID to read the AMP value |
| 2168 | * @ch: channel (left=0 or right=1) |
| 2169 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2170 | * @idx: the index value (only for input direction) |
| 2171 | * @mask: bit mask to set |
| 2172 | * @val: the bits value to set |
| 2173 | * |
| 2174 | * Update the AMP value with a bit mask. |
| 2175 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2176 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 2177 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2178 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2180 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2182 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2184 | /** |
| 2185 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 2186 | * @codec: HD-audio codec |
| 2187 | * @nid: NID to read the AMP value |
| 2188 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 2189 | * @idx: the index value (only for input direction) |
| 2190 | * @mask: bit mask to set |
| 2191 | * @val: the bits value to set |
| 2192 | * |
| 2193 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 2194 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2195 | */ |
| 2196 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2197 | int direction, int idx, int mask, int val) |
| 2198 | { |
| 2199 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 2200 | |
| 2201 | if (snd_BUG_ON(mask & ~0xff)) |
| 2202 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2203 | for (ch = 0; ch < 2; ch++) |
| 2204 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 2205 | idx, mask, val); |
| 2206 | return ret; |
| 2207 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2208 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2209 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2210 | /* Works like snd_hda_codec_amp_update() but it writes the value only at |
| 2211 | * the first access. If the amp was already initialized / updated beforehand, |
| 2212 | * this does nothing. |
| 2213 | */ |
| 2214 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2215 | int dir, int idx, int mask, int val) |
| 2216 | { |
| 2217 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); |
| 2218 | } |
| 2219 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init); |
| 2220 | |
| 2221 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2222 | int dir, int idx, int mask, int val) |
| 2223 | { |
| 2224 | int ch, ret = 0; |
| 2225 | |
| 2226 | if (snd_BUG_ON(mask & ~0xff)) |
| 2227 | mask &= 0xff; |
| 2228 | for (ch = 0; ch < 2; ch++) |
| 2229 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2230 | idx, mask, val); |
| 2231 | return ret; |
| 2232 | } |
| 2233 | EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo); |
| 2234 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2235 | /** |
| 2236 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2237 | * @codec: HD-audio codec |
| 2238 | * |
| 2239 | * Resume the all amp commands from the cache. |
| 2240 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2241 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2242 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2243 | int i; |
| 2244 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2245 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2246 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2247 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2248 | struct hda_amp_info *buffer; |
| 2249 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2250 | hda_nid_t nid; |
| 2251 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2252 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2253 | |
| 2254 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2255 | if (!buffer->head.dirty) |
| 2256 | continue; |
| 2257 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2258 | info = *buffer; |
| 2259 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2260 | if (!key) |
| 2261 | continue; |
| 2262 | nid = key & 0xff; |
| 2263 | idx = (key >> 16) & 0xff; |
| 2264 | dir = (key >> 24) & 0xff; |
| 2265 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2266 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2267 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2268 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2269 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2270 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2271 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2272 | } |
| 2273 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2274 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2275 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2276 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2278 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2279 | unsigned int ofs) |
| 2280 | { |
| 2281 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2282 | /* get num steps */ |
| 2283 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2284 | if (ofs < caps) |
| 2285 | caps -= ofs; |
| 2286 | return caps; |
| 2287 | } |
| 2288 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2289 | /** |
| 2290 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
| 2291 | * |
| 2292 | * The control element is supposed to have the private_value field |
| 2293 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2294 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2295 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2296 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | { |
| 2298 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2299 | u16 nid = get_amp_nid(kcontrol); |
| 2300 | u8 chs = get_amp_channels(kcontrol); |
| 2301 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2302 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2304 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2305 | uinfo->count = chs == 3 ? 2 : 1; |
| 2306 | uinfo->value.integer.min = 0; |
| 2307 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2308 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2309 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 2310 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 2311 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | return -EINVAL; |
| 2313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | return 0; |
| 2315 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2316 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2318 | |
| 2319 | static inline unsigned int |
| 2320 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2321 | int ch, int dir, int idx, unsigned int ofs) |
| 2322 | { |
| 2323 | unsigned int val; |
| 2324 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2325 | val &= HDA_AMP_VOLMASK; |
| 2326 | if (val >= ofs) |
| 2327 | val -= ofs; |
| 2328 | else |
| 2329 | val = 0; |
| 2330 | return val; |
| 2331 | } |
| 2332 | |
| 2333 | static inline int |
| 2334 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2335 | int ch, int dir, int idx, unsigned int ofs, |
| 2336 | unsigned int val) |
| 2337 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2338 | unsigned int maxval; |
| 2339 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2340 | if (val > 0) |
| 2341 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2342 | /* ofs = 0: raw max value */ |
| 2343 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2344 | if (val > maxval) |
| 2345 | val = maxval; |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2346 | return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, |
| 2347 | HDA_AMP_VOLMASK, val); |
| 2348 | } |
| 2349 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2350 | /** |
| 2351 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
| 2352 | * |
| 2353 | * The control element is supposed to have the private_value field |
| 2354 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2355 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2356 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2357 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | { |
| 2359 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2360 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2361 | int chs = get_amp_channels(kcontrol); |
| 2362 | int dir = get_amp_direction(kcontrol); |
| 2363 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2364 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | long *valp = ucontrol->value.integer.value; |
| 2366 | |
| 2367 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2368 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2370 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | return 0; |
| 2372 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2373 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2375 | /** |
| 2376 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
| 2377 | * |
| 2378 | * The control element is supposed to have the private_value field |
| 2379 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2380 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2381 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2382 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | { |
| 2384 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2385 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2386 | int chs = get_amp_channels(kcontrol); |
| 2387 | int dir = get_amp_direction(kcontrol); |
| 2388 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2389 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | long *valp = ucontrol->value.integer.value; |
| 2391 | int change = 0; |
| 2392 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2393 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2394 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2395 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2396 | valp++; |
| 2397 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2398 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2399 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2400 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | return change; |
| 2402 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2403 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2405 | /** |
| 2406 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
| 2407 | * |
| 2408 | * The control element is supposed to have the private_value field |
| 2409 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2410 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2411 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2412 | unsigned int size, unsigned int __user *_tlv) |
| 2413 | { |
| 2414 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2415 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2416 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2417 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2418 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2419 | u32 caps, val1, val2; |
| 2420 | |
| 2421 | if (size < 4 * sizeof(unsigned int)) |
| 2422 | return -ENOMEM; |
| 2423 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2424 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2425 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2426 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2427 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2428 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2429 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2430 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2431 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2432 | return -EFAULT; |
| 2433 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2434 | return -EFAULT; |
| 2435 | if (put_user(val1, _tlv + 2)) |
| 2436 | return -EFAULT; |
| 2437 | if (put_user(val2, _tlv + 3)) |
| 2438 | return -EFAULT; |
| 2439 | return 0; |
| 2440 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2441 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2442 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2443 | /** |
| 2444 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2445 | * @codec: HD-audio codec |
| 2446 | * @nid: NID of a reference widget |
| 2447 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2448 | * @tlv: TLV data to be stored, at least 4 elements |
| 2449 | * |
| 2450 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2451 | * obtained from the reference NID. |
| 2452 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2453 | */ |
| 2454 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2455 | unsigned int *tlv) |
| 2456 | { |
| 2457 | u32 caps; |
| 2458 | int nums, step; |
| 2459 | |
| 2460 | caps = query_amp_caps(codec, nid, dir); |
| 2461 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2462 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2463 | step = (step + 1) * 25; |
| 2464 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2465 | tlv[1] = 2 * sizeof(unsigned int); |
| 2466 | tlv[2] = -nums * step; |
| 2467 | tlv[3] = step; |
| 2468 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2469 | EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2472 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2473 | 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] | 2474 | { |
| 2475 | struct snd_ctl_elem_id id; |
| 2476 | memset(&id, 0, sizeof(id)); |
| 2477 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2478 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2479 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2480 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2481 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2482 | strcpy(id.name, name); |
| 2483 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2484 | } |
| 2485 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2486 | /** |
| 2487 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2488 | * @codec: HD-audio codec |
| 2489 | * @name: ctl id name string |
| 2490 | * |
| 2491 | * Get the control element with the given id string and IFACE_MIXER. |
| 2492 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2493 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2494 | const char *name) |
| 2495 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2496 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2497 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2498 | EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2499 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2500 | 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] | 2501 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2502 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2503 | int i, idx; |
| 2504 | /* 16 ctlrs should be large enough */ |
| 2505 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2506 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2507 | return idx; |
| 2508 | } |
| 2509 | return -EBUSY; |
| 2510 | } |
| 2511 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2512 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2513 | * 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] | 2514 | * @codec: HD-audio codec |
| 2515 | * @nid: corresponding NID (optional) |
| 2516 | * @kctl: the control element to assign |
| 2517 | * |
| 2518 | * Add the given control element to an array inside the codec instance. |
| 2519 | * All control elements belonging to a codec are supposed to be added |
| 2520 | * by this function so that a proper clean-up works at the free or |
| 2521 | * reconfiguration time. |
| 2522 | * |
| 2523 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2524 | * The assignment is shown in the codec proc file. |
| 2525 | * |
| 2526 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2527 | * #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] | 2528 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2529 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2530 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2531 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2532 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2533 | { |
| 2534 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2535 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2536 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2537 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2538 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2539 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2540 | if (nid == 0) |
| 2541 | nid = get_amp_nid_(kctl->private_value); |
| 2542 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2543 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2544 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2545 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2546 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2547 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2548 | if (err < 0) |
| 2549 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2550 | item = snd_array_new(&codec->mixers); |
| 2551 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2552 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2553 | item->kctl = kctl; |
| 2554 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2555 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2556 | return 0; |
| 2557 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2558 | EXPORT_SYMBOL_HDA(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2559 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2560 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2561 | * snd_hda_add_nid - Assign a NID to a control element |
| 2562 | * @codec: HD-audio codec |
| 2563 | * @nid: corresponding NID (optional) |
| 2564 | * @kctl: the control element to assign |
| 2565 | * @index: index to kctl |
| 2566 | * |
| 2567 | * Add the given control element to an array inside the codec instance. |
| 2568 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2569 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2570 | */ |
| 2571 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2572 | unsigned int index, hda_nid_t nid) |
| 2573 | { |
| 2574 | struct hda_nid_item *item; |
| 2575 | |
| 2576 | if (nid > 0) { |
| 2577 | item = snd_array_new(&codec->nids); |
| 2578 | if (!item) |
| 2579 | return -ENOMEM; |
| 2580 | item->kctl = kctl; |
| 2581 | item->index = index; |
| 2582 | item->nid = nid; |
| 2583 | return 0; |
| 2584 | } |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 2585 | printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n", |
| 2586 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2587 | return -EINVAL; |
| 2588 | } |
| 2589 | EXPORT_SYMBOL_HDA(snd_hda_add_nid); |
| 2590 | |
| 2591 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2592 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2593 | * @codec: HD-audio codec |
| 2594 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2595 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2596 | { |
| 2597 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2598 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2599 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2600 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2601 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2602 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2605 | /* pseudo device locking |
| 2606 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2607 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2608 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2609 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2610 | struct snd_card *card = bus->card; |
| 2611 | struct hda_codec *codec; |
| 2612 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2613 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2614 | if (card->shutdown) |
| 2615 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2616 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2617 | if (!list_empty(&card->ctl_files)) |
| 2618 | goto err_clear; |
| 2619 | |
| 2620 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2621 | int pcm; |
| 2622 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2623 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2624 | if (!cpcm->pcm) |
| 2625 | continue; |
| 2626 | if (cpcm->pcm->streams[0].substream_opened || |
| 2627 | cpcm->pcm->streams[1].substream_opened) |
| 2628 | goto err_clear; |
| 2629 | } |
| 2630 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2631 | spin_unlock(&card->files_lock); |
| 2632 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2633 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2634 | err_clear: |
| 2635 | card->shutdown = 0; |
| 2636 | err_unlock: |
| 2637 | spin_unlock(&card->files_lock); |
| 2638 | return -EINVAL; |
| 2639 | } |
| 2640 | EXPORT_SYMBOL_HDA(snd_hda_lock_devices); |
| 2641 | |
| 2642 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2643 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2644 | struct snd_card *card = bus->card; |
| 2645 | |
| 2646 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2647 | spin_lock(&card->files_lock); |
| 2648 | card->shutdown = 0; |
| 2649 | spin_unlock(&card->files_lock); |
| 2650 | } |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2651 | EXPORT_SYMBOL_HDA(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2652 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2653 | /** |
| 2654 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2655 | * @codec: HD-audio codec |
| 2656 | * |
| 2657 | * This frees the all PCM and control elements assigned to the codec, and |
| 2658 | * clears the caches and restores the pin default configurations. |
| 2659 | * |
| 2660 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2661 | * returns zero. |
| 2662 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2663 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2664 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2665 | struct hda_bus *bus = codec->bus; |
| 2666 | struct snd_card *card = bus->card; |
| 2667 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2668 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2669 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2670 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2671 | |
| 2672 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2673 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 2674 | #ifdef CONFIG_PM |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 2675 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2676 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2677 | #endif |
| 2678 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2679 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2680 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2681 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2682 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2683 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2684 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2685 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2686 | } |
| 2687 | if (codec->patch_ops.free) |
| 2688 | codec->patch_ops.free(codec); |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2689 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2690 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2691 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2692 | codec->spec = NULL; |
| 2693 | free_hda_cache(&codec->amp_cache); |
| 2694 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2695 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2696 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2697 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2698 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2699 | snd_array_free(&codec->cvt_setups); |
| 2700 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2701 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2702 | codec->num_pcms = 0; |
| 2703 | codec->pcm_info = NULL; |
| 2704 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2705 | codec->slave_dig_outs = NULL; |
| 2706 | codec->spdif_status_reset = 0; |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 2707 | unload_parser(codec); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 2708 | module_put(codec->owner); |
| 2709 | codec->owner = NULL; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2710 | |
| 2711 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2712 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2713 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2714 | } |
| 2715 | |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2716 | typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *); |
| 2717 | |
| 2718 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2719 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2720 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2721 | { |
| 2722 | struct hda_nid_item *items; |
| 2723 | const char * const *s; |
| 2724 | int i, err; |
| 2725 | |
| 2726 | items = codec->mixers.list; |
| 2727 | for (i = 0; i < codec->mixers.used; i++) { |
| 2728 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2729 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2730 | continue; |
| 2731 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2732 | char tmpname[sizeof(sctl->id.name)]; |
| 2733 | const char *name = *s; |
| 2734 | if (suffix) { |
| 2735 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2736 | name, suffix); |
| 2737 | name = tmpname; |
| 2738 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2739 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2740 | err = func(data, sctl); |
| 2741 | if (err) |
| 2742 | return err; |
| 2743 | break; |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
| 2750 | static int check_slave_present(void *data, struct snd_kcontrol *sctl) |
| 2751 | { |
| 2752 | return 1; |
| 2753 | } |
| 2754 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2755 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2756 | static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2757 | { |
| 2758 | int _tlv[4]; |
| 2759 | const int *tlv = NULL; |
| 2760 | int val = -1; |
| 2761 | |
| 2762 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2763 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2764 | mm_segment_t fs = get_fs(); |
| 2765 | set_fs(get_ds()); |
| 2766 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2767 | tlv = _tlv; |
| 2768 | set_fs(fs); |
| 2769 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2770 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2771 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2772 | int step = tlv[3]; |
| 2773 | step &= ~TLV_DB_SCALE_MUTE; |
| 2774 | if (!step) |
| 2775 | return -1; |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2776 | if (*step_to_check && *step_to_check != step) { |
| 2777 | snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n", |
| 2778 | *step_to_check, step); |
| 2779 | return -1; |
| 2780 | } |
| 2781 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2782 | val = -tlv[2] / step; |
| 2783 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2784 | return val; |
| 2785 | } |
| 2786 | |
| 2787 | /* call kctl->put with the given value(s) */ |
| 2788 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2789 | { |
| 2790 | struct snd_ctl_elem_value *ucontrol; |
| 2791 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2792 | if (!ucontrol) |
| 2793 | return -ENOMEM; |
| 2794 | ucontrol->value.integer.value[0] = val; |
| 2795 | ucontrol->value.integer.value[1] = val; |
| 2796 | kctl->put(kctl, ucontrol); |
| 2797 | kfree(ucontrol); |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
| 2801 | /* initialize the slave volume with 0dB */ |
| 2802 | static int init_slave_0dB(void *data, struct snd_kcontrol *slave) |
| 2803 | { |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2804 | int offset = get_kctl_0dB_offset(slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2805 | if (offset > 0) |
| 2806 | put_kctl_with_value(slave, offset); |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | /* unmute the slave */ |
| 2811 | static int init_slave_unmute(void *data, struct snd_kcontrol *slave) |
| 2812 | { |
| 2813 | return put_kctl_with_value(slave, 1); |
| 2814 | } |
| 2815 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2816 | /** |
| 2817 | * snd_hda_add_vmaster - create a virtual master control and add slaves |
| 2818 | * @codec: HD-audio codec |
| 2819 | * @name: vmaster control name |
| 2820 | * @tlv: TLV data (optional) |
| 2821 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2822 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2823 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2824 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2825 | * |
| 2826 | * Create a virtual master control with the given name. The TLV data |
| 2827 | * must be either NULL or a valid data. |
| 2828 | * |
| 2829 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2830 | * slave control name. All controls with these names are assigned to |
| 2831 | * the new virtual master control. |
| 2832 | * |
| 2833 | * This function returns zero if successful or a negative error code. |
| 2834 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2835 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2836 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2837 | const char *suffix, bool init_slave_vol, |
| 2838 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2839 | { |
| 2840 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2841 | int err; |
| 2842 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2843 | if (ctl_ret) |
| 2844 | *ctl_ret = NULL; |
| 2845 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2846 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2847 | if (err != 1) { |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2848 | snd_printdd("No slave found for %s\n", name); |
| 2849 | return 0; |
| 2850 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2851 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2852 | if (!kctl) |
| 2853 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2854 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2855 | if (err < 0) |
| 2856 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2857 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2858 | err = map_slaves(codec, slaves, suffix, |
| 2859 | (map_slave_func_t)snd_ctl_add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2860 | if (err < 0) |
| 2861 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2862 | |
| 2863 | /* init with master mute & zero volume */ |
| 2864 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2865 | if (init_slave_vol) { |
| 2866 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2867 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2868 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2869 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2870 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2871 | if (ctl_ret) |
| 2872 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2873 | return 0; |
| 2874 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2875 | EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2876 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2877 | /* |
| 2878 | * mute-LED control using vmaster |
| 2879 | */ |
| 2880 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2881 | struct snd_ctl_elem_info *uinfo) |
| 2882 | { |
| 2883 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2884 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2885 | }; |
| 2886 | unsigned int index; |
| 2887 | |
| 2888 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2889 | uinfo->count = 1; |
| 2890 | uinfo->value.enumerated.items = 3; |
| 2891 | index = uinfo->value.enumerated.item; |
| 2892 | if (index >= 3) |
| 2893 | index = 2; |
| 2894 | strcpy(uinfo->value.enumerated.name, texts[index]); |
| 2895 | return 0; |
| 2896 | } |
| 2897 | |
| 2898 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2899 | struct snd_ctl_elem_value *ucontrol) |
| 2900 | { |
| 2901 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2902 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2903 | return 0; |
| 2904 | } |
| 2905 | |
| 2906 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2907 | struct snd_ctl_elem_value *ucontrol) |
| 2908 | { |
| 2909 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2910 | unsigned int old_mode = hook->mute_mode; |
| 2911 | |
| 2912 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2913 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2914 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2915 | if (old_mode == hook->mute_mode) |
| 2916 | return 0; |
| 2917 | snd_hda_sync_vmaster_hook(hook); |
| 2918 | return 1; |
| 2919 | } |
| 2920 | |
| 2921 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2922 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2923 | .name = "Mute-LED Mode", |
| 2924 | .info = vmaster_mute_mode_info, |
| 2925 | .get = vmaster_mute_mode_get, |
| 2926 | .put = vmaster_mute_mode_put, |
| 2927 | }; |
| 2928 | |
| 2929 | /* |
| 2930 | * Add a mute-LED hook with the given vmaster switch kctl |
| 2931 | * "Mute-LED Mode" control is automatically created and associated with |
| 2932 | * the given hook. |
| 2933 | */ |
| 2934 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2935 | struct hda_vmaster_mute_hook *hook, |
| 2936 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2937 | { |
| 2938 | struct snd_kcontrol *kctl; |
| 2939 | |
| 2940 | if (!hook->hook || !hook->sw_kctl) |
| 2941 | return 0; |
| 2942 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2943 | hook->codec = codec; |
| 2944 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2945 | if (!expose_enum_ctl) |
| 2946 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2947 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2948 | if (!kctl) |
| 2949 | return -ENOMEM; |
| 2950 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2951 | } |
| 2952 | EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook); |
| 2953 | |
| 2954 | /* |
| 2955 | * Call the hook with the current value for synchronization |
| 2956 | * Should be called in init callback |
| 2957 | */ |
| 2958 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2959 | { |
| 2960 | if (!hook->hook || !hook->codec) |
| 2961 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2962 | /* don't call vmaster hook in the destructor since it might have |
| 2963 | * been already destroyed |
| 2964 | */ |
| 2965 | if (hook->codec->bus->shutdown) |
| 2966 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2967 | switch (hook->mute_mode) { |
| 2968 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2969 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2970 | break; |
| 2971 | default: |
| 2972 | hook->hook(hook->codec, hook->mute_mode); |
| 2973 | break; |
| 2974 | } |
| 2975 | } |
| 2976 | EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook); |
| 2977 | |
| 2978 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2979 | /** |
| 2980 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
| 2981 | * |
| 2982 | * The control element is supposed to have the private_value field |
| 2983 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2984 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2985 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2986 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | { |
| 2988 | int chs = get_amp_channels(kcontrol); |
| 2989 | |
| 2990 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2991 | uinfo->count = chs == 3 ? 2 : 1; |
| 2992 | uinfo->value.integer.min = 0; |
| 2993 | uinfo->value.integer.max = 1; |
| 2994 | return 0; |
| 2995 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 2996 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2998 | /** |
| 2999 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
| 3000 | * |
| 3001 | * The control element is supposed to have the private_value field |
| 3002 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3003 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3004 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 3005 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | { |
| 3007 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3008 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3009 | int chs = get_amp_channels(kcontrol); |
| 3010 | int dir = get_amp_direction(kcontrol); |
| 3011 | int idx = get_amp_index(kcontrol); |
| 3012 | long *valp = ucontrol->value.integer.value; |
| 3013 | |
| 3014 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3015 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3016 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3018 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3019 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | return 0; |
| 3021 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3022 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3024 | /** |
| 3025 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
| 3026 | * |
| 3027 | * The control element is supposed to have the private_value field |
| 3028 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 3029 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3030 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 3031 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | { |
| 3033 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3034 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 3035 | int chs = get_amp_channels(kcontrol); |
| 3036 | int dir = get_amp_direction(kcontrol); |
| 3037 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | long *valp = ucontrol->value.integer.value; |
| 3039 | int change = 0; |
| 3040 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3041 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3042 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3043 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3044 | HDA_AMP_MUTE, |
| 3045 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 3046 | valp++; |
| 3047 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 3048 | if (chs & 2) |
| 3049 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 3050 | HDA_AMP_MUTE, |
| 3051 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 3052 | hda_call_check_power_status(codec, nid); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3053 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | return change; |
| 3055 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3056 | EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
| 3058 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3059 | * bound volume controls |
| 3060 | * |
| 3061 | * bind multiple volumes (# indices, from 0) |
| 3062 | */ |
| 3063 | |
| 3064 | #define AMP_VAL_IDX_SHIFT 19 |
| 3065 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 3066 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3067 | /** |
| 3068 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
| 3069 | * |
| 3070 | * The control element is supposed to have the private_value field |
| 3071 | * set up via HDA_BIND_MUTE*() macros. |
| 3072 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3073 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 3074 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3075 | { |
| 3076 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3077 | unsigned long pval; |
| 3078 | int err; |
| 3079 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3080 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3081 | pval = kcontrol->private_value; |
| 3082 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 3083 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 3084 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3085 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3086 | return err; |
| 3087 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3088 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3089 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3090 | /** |
| 3091 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
| 3092 | * |
| 3093 | * The control element is supposed to have the private_value field |
| 3094 | * set up via HDA_BIND_MUTE*() macros. |
| 3095 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3096 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 3097 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3098 | { |
| 3099 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3100 | unsigned long pval; |
| 3101 | int i, indices, err = 0, change = 0; |
| 3102 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3103 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3104 | pval = kcontrol->private_value; |
| 3105 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 3106 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3107 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 3108 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3109 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 3110 | if (err < 0) |
| 3111 | break; |
| 3112 | change |= err; |
| 3113 | } |
| 3114 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3115 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3116 | return err < 0 ? err : change; |
| 3117 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3118 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 3119 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3120 | /** |
| 3121 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
| 3122 | * |
| 3123 | * The control element is supposed to have the private_value field |
| 3124 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3125 | */ |
| 3126 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 3127 | struct snd_ctl_elem_info *uinfo) |
| 3128 | { |
| 3129 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3130 | struct hda_bind_ctls *c; |
| 3131 | int err; |
| 3132 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3133 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3134 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3135 | kcontrol->private_value = *c->values; |
| 3136 | err = c->ops->info(kcontrol, uinfo); |
| 3137 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3138 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3139 | return err; |
| 3140 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3141 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3142 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3143 | /** |
| 3144 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
| 3145 | * |
| 3146 | * The control element is supposed to have the private_value field |
| 3147 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3148 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3149 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 3150 | struct snd_ctl_elem_value *ucontrol) |
| 3151 | { |
| 3152 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3153 | struct hda_bind_ctls *c; |
| 3154 | int err; |
| 3155 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3156 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3157 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3158 | kcontrol->private_value = *c->values; |
| 3159 | err = c->ops->get(kcontrol, ucontrol); |
| 3160 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3161 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3162 | return err; |
| 3163 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3164 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3165 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3166 | /** |
| 3167 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
| 3168 | * |
| 3169 | * The control element is supposed to have the private_value field |
| 3170 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 3171 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3172 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 3173 | struct snd_ctl_elem_value *ucontrol) |
| 3174 | { |
| 3175 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3176 | struct hda_bind_ctls *c; |
| 3177 | unsigned long *vals; |
| 3178 | int err = 0, change = 0; |
| 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 | for (vals = c->values; *vals; vals++) { |
| 3183 | kcontrol->private_value = *vals; |
| 3184 | err = c->ops->put(kcontrol, ucontrol); |
| 3185 | if (err < 0) |
| 3186 | break; |
| 3187 | change |= err; |
| 3188 | } |
| 3189 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3190 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3191 | return err < 0 ? err : change; |
| 3192 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3193 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3194 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3195 | /** |
| 3196 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
| 3197 | * |
| 3198 | * The control element is supposed to have the private_value field |
| 3199 | * set up via HDA_BIND_VOL() macro. |
| 3200 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3201 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3202 | unsigned int size, unsigned int __user *tlv) |
| 3203 | { |
| 3204 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3205 | struct hda_bind_ctls *c; |
| 3206 | int err; |
| 3207 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3208 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3209 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3210 | kcontrol->private_value = *c->values; |
| 3211 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3212 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3213 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3214 | return err; |
| 3215 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3216 | EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3217 | |
| 3218 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3219 | .info = snd_hda_mixer_amp_volume_info, |
| 3220 | .get = snd_hda_mixer_amp_volume_get, |
| 3221 | .put = snd_hda_mixer_amp_volume_put, |
| 3222 | .tlv = snd_hda_mixer_amp_tlv |
| 3223 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3224 | EXPORT_SYMBOL_HDA(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3225 | |
| 3226 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3227 | .info = snd_hda_mixer_amp_switch_info, |
| 3228 | .get = snd_hda_mixer_amp_switch_get, |
| 3229 | .put = snd_hda_mixer_amp_switch_put, |
| 3230 | .tlv = snd_hda_mixer_amp_tlv |
| 3231 | }; |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3232 | EXPORT_SYMBOL_HDA(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3233 | |
| 3234 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | * SPDIF out controls |
| 3236 | */ |
| 3237 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3238 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3239 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | { |
| 3241 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3242 | uinfo->count = 1; |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3246 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3247 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | { |
| 3249 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3250 | IEC958_AES0_NONAUDIO | |
| 3251 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3252 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3253 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3254 | IEC958_AES1_CON_ORIGINAL; |
| 3255 | return 0; |
| 3256 | } |
| 3257 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3258 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3259 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | { |
| 3261 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3262 | IEC958_AES0_NONAUDIO | |
| 3263 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3267 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3268 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | { |
| 3270 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3271 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3272 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3274 | mutex_lock(&codec->spdif_mutex); |
| 3275 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3276 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3277 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3278 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3279 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3280 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | |
| 3282 | return 0; |
| 3283 | } |
| 3284 | |
| 3285 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3286 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3287 | */ |
| 3288 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3289 | { |
| 3290 | unsigned short val = 0; |
| 3291 | |
| 3292 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3293 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3295 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3297 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3298 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3299 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3301 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3302 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3303 | val |= AC_DIG1_EMPHASIS; |
| 3304 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3305 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3307 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3309 | } |
| 3310 | return val; |
| 3311 | } |
| 3312 | |
| 3313 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3314 | */ |
| 3315 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3316 | { |
| 3317 | unsigned int sbits = 0; |
| 3318 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3319 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3321 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3323 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3324 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3326 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3327 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3329 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3331 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3333 | sbits |= val & (0x7f << 8); |
| 3334 | } |
| 3335 | return sbits; |
| 3336 | } |
| 3337 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3338 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3339 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3340 | int verb, int val) |
| 3341 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3342 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3343 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3344 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3345 | d = codec->slave_dig_outs; |
| 3346 | if (!d) |
| 3347 | return; |
| 3348 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3349 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3353 | int dig1, int dig2) |
| 3354 | { |
| 3355 | if (dig1 != -1) |
| 3356 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3357 | if (dig2 != -1) |
| 3358 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3359 | } |
| 3360 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3361 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3362 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | { |
| 3364 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3365 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3366 | struct hda_spdif_out *spdif; |
| 3367 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | unsigned short val; |
| 3369 | int change; |
| 3370 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3371 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3372 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3373 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3374 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3376 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3377 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3378 | val = convert_from_spdif_status(spdif->status); |
| 3379 | val |= spdif->ctls & 1; |
| 3380 | change = spdif->ctls != val; |
| 3381 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3382 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3383 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3384 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | return change; |
| 3386 | } |
| 3387 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3388 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3390 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3391 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | { |
| 3393 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3394 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3395 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3397 | mutex_lock(&codec->spdif_mutex); |
| 3398 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3399 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3400 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | return 0; |
| 3402 | } |
| 3403 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3404 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3405 | int dig1, int dig2) |
| 3406 | { |
| 3407 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3408 | /* unmute amp switch (if any) */ |
| 3409 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3410 | (dig1 & AC_DIG1_ENABLE)) |
| 3411 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3412 | HDA_AMP_MUTE, 0); |
| 3413 | } |
| 3414 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3415 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3416 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | { |
| 3418 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3419 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3420 | struct hda_spdif_out *spdif; |
| 3421 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | unsigned short val; |
| 3423 | int change; |
| 3424 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3425 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3426 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3427 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3428 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3430 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3431 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3432 | spdif->ctls = val; |
| 3433 | if (change && nid != (u16)-1) |
| 3434 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3435 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | return change; |
| 3437 | } |
| 3438 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3439 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | { |
| 3441 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3442 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3443 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | .info = snd_hda_spdif_mask_info, |
| 3445 | .get = snd_hda_spdif_cmask_get, |
| 3446 | }, |
| 3447 | { |
| 3448 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3449 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3450 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | .info = snd_hda_spdif_mask_info, |
| 3452 | .get = snd_hda_spdif_pmask_get, |
| 3453 | }, |
| 3454 | { |
| 3455 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3456 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | .info = snd_hda_spdif_mask_info, |
| 3458 | .get = snd_hda_spdif_default_get, |
| 3459 | .put = snd_hda_spdif_default_put, |
| 3460 | }, |
| 3461 | { |
| 3462 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3463 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | .info = snd_hda_spdif_out_switch_info, |
| 3465 | .get = snd_hda_spdif_out_switch_get, |
| 3466 | .put = snd_hda_spdif_out_switch_put, |
| 3467 | }, |
| 3468 | { } /* end */ |
| 3469 | }; |
| 3470 | |
| 3471 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3472 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3474 | * @associated_nid: NID that new ctls associated with |
| 3475 | * @cvt_nid: converter NID |
| 3476 | * @type: HDA_PCM_TYPE_* |
| 3477 | * Creates controls related with the digital output. |
| 3478 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | * |
| 3480 | * Returns 0 if successful, or a negative error code. |
| 3481 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3482 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3483 | hda_nid_t associated_nid, |
| 3484 | hda_nid_t cvt_nid, |
| 3485 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | { |
| 3487 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3488 | struct snd_kcontrol *kctl; |
| 3489 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3490 | int idx = 0; |
| 3491 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3492 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3493 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3495 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3496 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3497 | idx = spdif_index; |
| 3498 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3499 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3500 | /* suppose a single SPDIF device */ |
| 3501 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3502 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3503 | if (!kctl) |
| 3504 | break; |
| 3505 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3506 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3507 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3508 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3509 | if (!bus->primary_dig_out_type) |
| 3510 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3511 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3512 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3513 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3514 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 3515 | return -EBUSY; |
| 3516 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3517 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3518 | if (!spdif) |
| 3519 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3521 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3522 | if (!kctl) |
| 3523 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3524 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3525 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3526 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3527 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | return err; |
| 3529 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3530 | spdif->nid = cvt_nid; |
| 3531 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3532 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3533 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | return 0; |
| 3535 | } |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3536 | EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3538 | /* get the hda_spdif_out entry from the given NID |
| 3539 | * call within spdif_mutex lock |
| 3540 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3541 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3542 | hda_nid_t nid) |
| 3543 | { |
| 3544 | int i; |
| 3545 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3546 | struct hda_spdif_out *spdif = |
| 3547 | snd_array_elem(&codec->spdif_out, i); |
| 3548 | if (spdif->nid == nid) |
| 3549 | return spdif; |
| 3550 | } |
| 3551 | return NULL; |
| 3552 | } |
| 3553 | EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid); |
| 3554 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3555 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3556 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3557 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3558 | |
| 3559 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3560 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3561 | spdif->nid = (u16)-1; |
| 3562 | mutex_unlock(&codec->spdif_mutex); |
| 3563 | } |
| 3564 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign); |
| 3565 | |
| 3566 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3567 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3568 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3569 | unsigned short val; |
| 3570 | |
| 3571 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3572 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3573 | if (spdif->nid != nid) { |
| 3574 | spdif->nid = nid; |
| 3575 | val = spdif->ctls; |
| 3576 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3577 | } |
| 3578 | mutex_unlock(&codec->spdif_mutex); |
| 3579 | } |
| 3580 | EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign); |
| 3581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3583 | * SPDIF sharing with analog output |
| 3584 | */ |
| 3585 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3586 | struct snd_ctl_elem_value *ucontrol) |
| 3587 | { |
| 3588 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3589 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3590 | return 0; |
| 3591 | } |
| 3592 | |
| 3593 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3594 | struct snd_ctl_elem_value *ucontrol) |
| 3595 | { |
| 3596 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3597 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3598 | return 0; |
| 3599 | } |
| 3600 | |
| 3601 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3602 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3603 | .name = "IEC958 Default PCM Playback Switch", |
| 3604 | .info = snd_ctl_boolean_mono_info, |
| 3605 | .get = spdif_share_sw_get, |
| 3606 | .put = spdif_share_sw_put, |
| 3607 | }; |
| 3608 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3609 | /** |
| 3610 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3611 | * @codec: the HDA codec |
| 3612 | * @mout: multi-out instance |
| 3613 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3614 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3615 | struct hda_multi_out *mout) |
| 3616 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3617 | struct snd_kcontrol *kctl; |
| 3618 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3619 | if (!mout->dig_out_nid) |
| 3620 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3621 | |
| 3622 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3623 | if (!kctl) |
| 3624 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3625 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3626 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3627 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3628 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3629 | |
| 3630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | * SPDIF input |
| 3632 | */ |
| 3633 | |
| 3634 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3635 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3636 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3637 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | { |
| 3639 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3640 | |
| 3641 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3642 | return 0; |
| 3643 | } |
| 3644 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3645 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3646 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | { |
| 3648 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3649 | hda_nid_t nid = kcontrol->private_value; |
| 3650 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3651 | int change; |
| 3652 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3653 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3655 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3657 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3658 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3660 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | return change; |
| 3662 | } |
| 3663 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3664 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3665 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | { |
| 3667 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3668 | hda_nid_t nid = kcontrol->private_value; |
| 3669 | unsigned short val; |
| 3670 | unsigned int sbits; |
| 3671 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3672 | 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] | 3673 | sbits = convert_to_spdif_status(val); |
| 3674 | ucontrol->value.iec958.status[0] = sbits; |
| 3675 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3676 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3677 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3678 | return 0; |
| 3679 | } |
| 3680 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3681 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | { |
| 3683 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3684 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | .info = snd_hda_spdif_in_switch_info, |
| 3686 | .get = snd_hda_spdif_in_switch_get, |
| 3687 | .put = snd_hda_spdif_in_switch_put, |
| 3688 | }, |
| 3689 | { |
| 3690 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3691 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3692 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | .info = snd_hda_spdif_mask_info, |
| 3694 | .get = snd_hda_spdif_in_status_get, |
| 3695 | }, |
| 3696 | { } /* end */ |
| 3697 | }; |
| 3698 | |
| 3699 | /** |
| 3700 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3701 | * @codec: the HDA codec |
| 3702 | * @nid: audio in widget NID |
| 3703 | * |
| 3704 | * Creates controls related with the SPDIF input. |
| 3705 | * Called from each patch supporting the SPDIF in. |
| 3706 | * |
| 3707 | * Returns 0 if successful, or a negative error code. |
| 3708 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3709 | 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] | 3710 | { |
| 3711 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3712 | struct snd_kcontrol *kctl; |
| 3713 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3714 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3716 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3717 | if (idx < 0) { |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3718 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 3719 | return -EBUSY; |
| 3720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3722 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3723 | if (!kctl) |
| 3724 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3726 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3727 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | return err; |
| 3729 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3730 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3731 | snd_hda_codec_read(codec, nid, 0, |
| 3732 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3733 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | return 0; |
| 3735 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3736 | EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3738 | /* |
| 3739 | * command cache |
| 3740 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3742 | /* 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] | 3743 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3744 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3745 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3746 | |
| 3747 | /** |
| 3748 | * snd_hda_codec_write_cache - send a single command with caching |
| 3749 | * @codec: the HDA codec |
| 3750 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3751 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3752 | * @verb: the verb to send |
| 3753 | * @parm: the parameter for the verb |
| 3754 | * |
| 3755 | * Send a single command without waiting for response. |
| 3756 | * |
| 3757 | * Returns 0 if successful, or a negative error code. |
| 3758 | */ |
| 3759 | 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] | 3760 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3761 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3762 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3763 | struct hda_cache_head *c; |
| 3764 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3765 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3766 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3767 | cache_only = codec->cached_write; |
| 3768 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3769 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3770 | if (err < 0) |
| 3771 | return err; |
| 3772 | } |
| 3773 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3774 | /* parm may contain the verb stuff for get/set amp */ |
| 3775 | verb = verb | (parm >> 8); |
| 3776 | parm &= 0xff; |
| 3777 | key = build_cmd_cache_key(nid, verb); |
| 3778 | mutex_lock(&codec->bus->cmd_mutex); |
| 3779 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3780 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3781 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3782 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3783 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3784 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3785 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3786 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3787 | EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3788 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3789 | /** |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3790 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3791 | * @codec: the HDA codec |
| 3792 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3793 | * @flags: optional bit flags |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3794 | * @verb: the verb to send |
| 3795 | * @parm: the parameter for the verb |
| 3796 | * |
| 3797 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3798 | * command if the parameter is already identical with the cached value. |
| 3799 | * If not, it sends the command and refreshes the cache. |
| 3800 | * |
| 3801 | * Returns 0 if successful, or a negative error code. |
| 3802 | */ |
| 3803 | 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] | 3804 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3805 | { |
| 3806 | struct hda_cache_head *c; |
| 3807 | u32 key; |
| 3808 | |
| 3809 | /* parm may contain the verb stuff for get/set amp */ |
| 3810 | verb = verb | (parm >> 8); |
| 3811 | parm &= 0xff; |
| 3812 | key = build_cmd_cache_key(nid, verb); |
| 3813 | mutex_lock(&codec->bus->cmd_mutex); |
| 3814 | c = get_hash(&codec->cmd_cache, key); |
| 3815 | if (c && c->val == parm) { |
| 3816 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3817 | return 0; |
| 3818 | } |
| 3819 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3820 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3821 | } |
| 3822 | EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache); |
| 3823 | |
| 3824 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3825 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3826 | * @codec: HD-audio codec |
| 3827 | * |
| 3828 | * Execute all verbs recorded in the command caches to resume. |
| 3829 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3830 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3831 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3832 | int i; |
| 3833 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3834 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3835 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3836 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3837 | struct hda_cache_head *buffer; |
| 3838 | u32 key; |
| 3839 | |
| 3840 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3841 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3842 | if (!key) |
| 3843 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3844 | if (!buffer->dirty) |
| 3845 | continue; |
| 3846 | buffer->dirty = 0; |
| 3847 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3848 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3849 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3850 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3851 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3852 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3853 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3854 | EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3855 | |
| 3856 | /** |
| 3857 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3858 | * @codec: the HDA codec |
| 3859 | * @seq: VERB array to send |
| 3860 | * |
| 3861 | * Send the commands sequentially from the given array. |
| 3862 | * Thte commands are recorded on cache for power-save and resume. |
| 3863 | * The array must be terminated with NID=0. |
| 3864 | */ |
| 3865 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3866 | const struct hda_verb *seq) |
| 3867 | { |
| 3868 | for (; seq->nid; seq++) |
| 3869 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3870 | seq->param); |
| 3871 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 3872 | EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3873 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3874 | /** |
| 3875 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3876 | * @codec: HD-audio codec |
| 3877 | */ |
| 3878 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3879 | { |
| 3880 | snd_hda_codec_resume_amp(codec); |
| 3881 | snd_hda_codec_resume_cache(codec); |
| 3882 | } |
| 3883 | EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache); |
| 3884 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3885 | 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] | 3886 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3887 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3888 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3889 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3890 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3891 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3892 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3893 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3894 | if (!(wcaps & AC_WCAP_POWER)) |
| 3895 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3896 | if (codec->power_filter) { |
| 3897 | state = codec->power_filter(codec, nid, power_state); |
| 3898 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3899 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3900 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3901 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3902 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3903 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3904 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3905 | EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all); |
| 3906 | |
| 3907 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3908 | * supported power states check |
| 3909 | */ |
| 3910 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3911 | unsigned int power_state) |
| 3912 | { |
| 3913 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3914 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3915 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3916 | return false; |
| 3917 | if (sup & power_state) |
| 3918 | return true; |
| 3919 | else |
| 3920 | return false; |
| 3921 | } |
| 3922 | |
| 3923 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3924 | * wait until the state is reached, returns the current state |
| 3925 | */ |
| 3926 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3927 | hda_nid_t fg, |
| 3928 | unsigned int power_state) |
| 3929 | { |
| 3930 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3931 | unsigned int state, actual_state; |
| 3932 | |
| 3933 | for (;;) { |
| 3934 | state = snd_hda_codec_read(codec, fg, 0, |
| 3935 | AC_VERB_GET_POWER_STATE, 0); |
| 3936 | if (state & AC_PWRST_ERROR) |
| 3937 | break; |
| 3938 | actual_state = (state >> 4) & 0x0f; |
| 3939 | if (actual_state == power_state) |
| 3940 | break; |
| 3941 | if (time_after_eq(jiffies, end_time)) |
| 3942 | break; |
| 3943 | /* wait until the codec reachs to the target state */ |
| 3944 | msleep(1); |
| 3945 | } |
| 3946 | return state; |
| 3947 | } |
| 3948 | |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3949 | /* 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] | 3950 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3951 | hda_nid_t nid, |
| 3952 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3953 | { |
| 3954 | if (power_state == AC_PWRST_D3 && |
| 3955 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3956 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3957 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3958 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3959 | if (eapd & 0x02) |
| 3960 | return AC_PWRST_D0; |
| 3961 | } |
| 3962 | return power_state; |
| 3963 | } |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3964 | EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3965 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3966 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3967 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3968 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3969 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3970 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3971 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3972 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3973 | int count; |
| 3974 | unsigned int state; |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3975 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3976 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3977 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3978 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame^] | 3979 | if (codec->depop_delay < 0) |
| 3980 | msleep(codec->epss ? 10 : 100); |
| 3981 | else if (codec->depop_delay > 0) |
| 3982 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3983 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3984 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3985 | |
| 3986 | /* repeat power states setting at most 10 times*/ |
| 3987 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3988 | if (codec->patch_ops.set_power_state) |
| 3989 | codec->patch_ops.set_power_state(codec, fg, |
| 3990 | power_state); |
| 3991 | else { |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3992 | snd_hda_codec_read(codec, fg, flags, |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3993 | AC_VERB_SET_POWER_STATE, |
| 3994 | power_state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3995 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3996 | } |
| 3997 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3998 | if (!(state & AC_PWRST_ERROR)) |
| 3999 | break; |
| 4000 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 4001 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4002 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 4003 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4004 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4005 | /* sync power states of all widgets; |
| 4006 | * this is called at the end of codec parsing |
| 4007 | */ |
| 4008 | static void sync_power_up_states(struct hda_codec *codec) |
| 4009 | { |
| 4010 | hda_nid_t nid = codec->start_nid; |
| 4011 | int i; |
| 4012 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 4013 | /* don't care if no filter is used */ |
| 4014 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4015 | return; |
| 4016 | |
| 4017 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 4018 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4019 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4020 | if (!(wcaps & AC_WCAP_POWER)) |
| 4021 | continue; |
| 4022 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 4023 | if (target == AC_PWRST_D0) |
| 4024 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 4025 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4026 | snd_hda_codec_write(codec, nid, 0, |
| 4027 | AC_VERB_SET_POWER_STATE, target); |
| 4028 | } |
| 4029 | } |
| 4030 | |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4031 | #ifdef CONFIG_SND_HDA_HWDEP |
| 4032 | /* execute additional init verbs */ |
| 4033 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 4034 | { |
| 4035 | if (codec->init_verbs.list) |
| 4036 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 4037 | } |
| 4038 | #else |
| 4039 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 4040 | #endif |
| 4041 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4042 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4043 | /* |
| 4044 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4045 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4046 | */ |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4047 | 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] | 4048 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4049 | unsigned int state; |
| 4050 | |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4051 | codec->in_pm = 1; |
| 4052 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4053 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 4054 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4055 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4056 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4057 | /* Cancel delayed work if we aren't currently running from it. */ |
| 4058 | if (!in_wq) |
| 4059 | cancel_delayed_work_sync(&codec->power_work); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4060 | spin_lock(&codec->power_lock); |
| 4061 | snd_hda_update_power_acct(codec); |
| 4062 | trace_hda_power_down(codec); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 4063 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 4064 | codec->power_transition = 0; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4065 | codec->power_jiffies = jiffies; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4066 | spin_unlock(&codec->power_lock); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4067 | codec->in_pm = 0; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4068 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4069 | } |
| 4070 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4071 | /* mark all entries of cmd and amp caches dirty */ |
| 4072 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 4073 | { |
| 4074 | int i; |
| 4075 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 4076 | struct hda_cache_head *cmd; |
| 4077 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 4078 | cmd->dirty = 1; |
| 4079 | } |
| 4080 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 4081 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 4082 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4083 | amp->head.dirty = 1; |
| 4084 | } |
| 4085 | } |
| 4086 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4087 | /* |
| 4088 | * kick up codec; used both from PM and power-save |
| 4089 | */ |
| 4090 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 4091 | { |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4092 | codec->in_pm = 1; |
| 4093 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 4094 | hda_mark_cmd_cache_dirty(codec); |
| 4095 | |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4096 | /* set as if powered on for avoiding re-entering the resume |
| 4097 | * in the resume / power-save sequence |
| 4098 | */ |
| 4099 | hda_keep_power_on(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 4100 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 4101 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4102 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4103 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4104 | if (codec->patch_ops.resume) |
| 4105 | codec->patch_ops.resume(codec); |
| 4106 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 4107 | if (codec->patch_ops.init) |
| 4108 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4109 | snd_hda_codec_resume_amp(codec); |
| 4110 | snd_hda_codec_resume_cache(codec); |
| 4111 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4112 | |
| 4113 | if (codec->jackpoll_interval) |
| 4114 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 4115 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4116 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 4117 | |
| 4118 | codec->in_pm = 0; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 4119 | snd_hda_power_down(codec); /* flag down before returning */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4120 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4121 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4122 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | /** |
| 4125 | * snd_hda_build_controls - build mixer controls |
| 4126 | * @bus: the BUS |
| 4127 | * |
| 4128 | * Creates mixer controls for each codec included in the bus. |
| 4129 | * |
| 4130 | * Returns 0 if successful, otherwise a negative error code. |
| 4131 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4132 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4134 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4136 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4137 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4138 | if (err < 0) { |
Takashi Iwai | 28d1a85 | 2010-03-15 09:05:46 +0100 | [diff] [blame] | 4139 | printk(KERN_ERR "hda_codec: cannot build controls " |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4140 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4141 | err = snd_hda_codec_reset(codec); |
| 4142 | if (err < 0) { |
| 4143 | printk(KERN_ERR |
| 4144 | "hda_codec: cannot revert codec\n"); |
| 4145 | return err; |
| 4146 | } |
| 4147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4149 | return 0; |
| 4150 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4151 | EXPORT_SYMBOL_HDA(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4153 | /* |
| 4154 | * add standard channel maps if not specified |
| 4155 | */ |
| 4156 | static int add_std_chmaps(struct hda_codec *codec) |
| 4157 | { |
| 4158 | int i, str, err; |
| 4159 | |
| 4160 | for (i = 0; i < codec->num_pcms; i++) { |
| 4161 | for (str = 0; str < 2; str++) { |
| 4162 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4163 | struct hda_pcm_stream *hinfo = |
| 4164 | &codec->pcm_info[i].stream[str]; |
| 4165 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4166 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4167 | |
| 4168 | if (codec->pcm_info[i].own_chmap) |
| 4169 | continue; |
| 4170 | if (!pcm || !hinfo->substreams) |
| 4171 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4172 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4173 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4174 | hinfo->channels_max, |
| 4175 | 0, &chmap); |
| 4176 | if (err < 0) |
| 4177 | return err; |
| 4178 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4179 | } |
| 4180 | } |
| 4181 | return 0; |
| 4182 | } |
| 4183 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4184 | /* default channel maps for 2.1 speakers; |
| 4185 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4186 | */ |
| 4187 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4188 | { .channels = 2, |
| 4189 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4190 | { .channels = 4, |
| 4191 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4192 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4193 | { } |
| 4194 | }; |
| 4195 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4196 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4197 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4198 | { |
| 4199 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4200 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4201 | /* continue to initialize... */ |
| 4202 | if (codec->patch_ops.init) |
| 4203 | err = codec->patch_ops.init(codec); |
| 4204 | if (!err && codec->patch_ops.build_controls) |
| 4205 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4206 | if (err < 0) |
| 4207 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4208 | |
| 4209 | /* we create chmaps here instead of build_pcms */ |
| 4210 | err = add_std_chmaps(codec); |
| 4211 | if (err < 0) |
| 4212 | return err; |
| 4213 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4214 | if (codec->jackpoll_interval) |
| 4215 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4216 | else |
| 4217 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4218 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | return 0; |
| 4220 | } |
| 4221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | /* |
| 4223 | * stream formats |
| 4224 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4225 | struct hda_rate_tbl { |
| 4226 | unsigned int hz; |
| 4227 | unsigned int alsa_bits; |
| 4228 | unsigned int hda_fmt; |
| 4229 | }; |
| 4230 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4231 | /* rate = base * mult / div */ |
| 4232 | #define HDA_RATE(base, mult, div) \ |
| 4233 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4234 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4235 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4236 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4238 | |
| 4239 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4240 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4241 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4242 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4243 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4244 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4245 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4246 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4247 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4248 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4249 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4250 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4251 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4252 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4253 | |
| 4254 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4255 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4256 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4257 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | }; |
| 4259 | |
| 4260 | /** |
| 4261 | * snd_hda_calc_stream_format - calculate format bitset |
| 4262 | * @rate: the sample rate |
| 4263 | * @channels: the number of channels |
| 4264 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4265 | * @maxbps: the max. bps |
| 4266 | * |
| 4267 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4268 | * |
| 4269 | * Return zero if invalid. |
| 4270 | */ |
| 4271 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 4272 | unsigned int channels, |
| 4273 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4274 | unsigned int maxbps, |
| 4275 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | { |
| 4277 | int i; |
| 4278 | unsigned int val = 0; |
| 4279 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4280 | for (i = 0; rate_bits[i].hz; i++) |
| 4281 | if (rate_bits[i].hz == rate) { |
| 4282 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | break; |
| 4284 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4285 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | snd_printdd("invalid rate %d\n", rate); |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
| 4290 | if (channels == 0 || channels > 8) { |
| 4291 | snd_printdd("invalid channels %d\n", channels); |
| 4292 | return 0; |
| 4293 | } |
| 4294 | val |= channels - 1; |
| 4295 | |
| 4296 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4297 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4298 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4299 | break; |
| 4300 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4301 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4302 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | case 20: |
| 4304 | case 24: |
| 4305 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4306 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4307 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4309 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4311 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | break; |
| 4313 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4314 | snd_printdd("invalid format width %d\n", |
| 4315 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | return 0; |
| 4317 | } |
| 4318 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4319 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4320 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | return val; |
| 4323 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4324 | EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4326 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4327 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4328 | { |
| 4329 | unsigned int val = 0; |
| 4330 | if (nid != codec->afg && |
| 4331 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4332 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4333 | if (!val || val == -1) |
| 4334 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4335 | if (!val || val == -1) |
| 4336 | return 0; |
| 4337 | return val; |
| 4338 | } |
| 4339 | |
| 4340 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4341 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4342 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4343 | get_pcm_param); |
| 4344 | } |
| 4345 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4346 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4347 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4348 | { |
| 4349 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4350 | if (!streams || streams == -1) |
| 4351 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4352 | if (!streams || streams == -1) |
| 4353 | return 0; |
| 4354 | return streams; |
| 4355 | } |
| 4356 | |
| 4357 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4358 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4359 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4360 | get_stream_param); |
| 4361 | } |
| 4362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | /** |
| 4364 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4365 | * @codec: the HDA codec |
| 4366 | * @nid: NID to query |
| 4367 | * @ratesp: the pointer to store the detected rate bitflags |
| 4368 | * @formatsp: the pointer to store the detected formats |
| 4369 | * @bpsp: the pointer to store the detected format widths |
| 4370 | * |
| 4371 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4372 | * or @bsps argument is ignored. |
| 4373 | * |
| 4374 | * Returns 0 if successful, otherwise a negative error code. |
| 4375 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4376 | 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] | 4377 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4378 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4379 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4381 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4382 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | |
| 4384 | if (ratesp) { |
| 4385 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4386 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4388 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4390 | if (rates == 0) { |
| 4391 | snd_printk(KERN_ERR "hda_codec: rates == 0 " |
| 4392 | "(nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4393 | nid, val, |
| 4394 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
| 4395 | return -EIO; |
| 4396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | *ratesp = rates; |
| 4398 | } |
| 4399 | |
| 4400 | if (formatsp || bpsp) { |
| 4401 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4402 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4404 | streams = query_stream_param(codec, nid); |
| 4405 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | |
| 4408 | bps = 0; |
| 4409 | if (streams & AC_SUPFMT_PCM) { |
| 4410 | if (val & AC_SUPPCM_BITS_8) { |
| 4411 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4412 | bps = 8; |
| 4413 | } |
| 4414 | if (val & AC_SUPPCM_BITS_16) { |
| 4415 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4416 | bps = 16; |
| 4417 | } |
| 4418 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4419 | if (val & AC_SUPPCM_BITS_32) |
| 4420 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4421 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4422 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4423 | if (val & AC_SUPPCM_BITS_24) |
| 4424 | bps = 24; |
| 4425 | else if (val & AC_SUPPCM_BITS_20) |
| 4426 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4427 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4428 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4430 | if (val & AC_SUPPCM_BITS_32) |
| 4431 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | else if (val & AC_SUPPCM_BITS_24) |
| 4433 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4434 | else if (val & AC_SUPPCM_BITS_20) |
| 4435 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | } |
| 4437 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4438 | #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] | 4439 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4441 | if (!bps) |
| 4442 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4443 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4444 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4445 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4446 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | /* temporary hack: we have still no proper support |
| 4448 | * for the direct AC3 stream... |
| 4449 | */ |
| 4450 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4451 | bps = 8; |
| 4452 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4453 | if (formats == 0) { |
| 4454 | snd_printk(KERN_ERR "hda_codec: formats == 0 " |
| 4455 | "(nid=0x%x, val=0x%x, ovrd=%i, " |
| 4456 | "streams=0x%x)\n", |
| 4457 | nid, val, |
| 4458 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4459 | streams); |
| 4460 | return -EIO; |
| 4461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | if (formatsp) |
| 4463 | *formatsp = formats; |
| 4464 | if (bpsp) |
| 4465 | *bpsp = bps; |
| 4466 | } |
| 4467 | |
| 4468 | return 0; |
| 4469 | } |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4470 | EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | |
| 4472 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4473 | * snd_hda_is_supported_format - Check the validity of the format |
| 4474 | * @codec: HD-audio codec |
| 4475 | * @nid: NID to check |
| 4476 | * @format: the HD-audio format value to check |
| 4477 | * |
| 4478 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | * |
| 4480 | * Returns 1 if supported, 0 if not. |
| 4481 | */ |
| 4482 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4483 | unsigned int format) |
| 4484 | { |
| 4485 | int i; |
| 4486 | unsigned int val = 0, rate, stream; |
| 4487 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4488 | val = query_pcm_param(codec, nid); |
| 4489 | if (!val) |
| 4490 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | |
| 4492 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4493 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4494 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | if (val & (1 << i)) |
| 4496 | break; |
| 4497 | return 0; |
| 4498 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4499 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | return 0; |
| 4501 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4502 | stream = query_stream_param(codec, nid); |
| 4503 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | return 0; |
| 4505 | |
| 4506 | if (stream & AC_SUPFMT_PCM) { |
| 4507 | switch (format & 0xf0) { |
| 4508 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4509 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | return 0; |
| 4511 | break; |
| 4512 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4513 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | return 0; |
| 4515 | break; |
| 4516 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4517 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | return 0; |
| 4519 | break; |
| 4520 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4521 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | return 0; |
| 4523 | break; |
| 4524 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4525 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | return 0; |
| 4527 | break; |
| 4528 | default: |
| 4529 | return 0; |
| 4530 | } |
| 4531 | } else { |
| 4532 | /* FIXME: check for float32 and AC3? */ |
| 4533 | } |
| 4534 | |
| 4535 | return 1; |
| 4536 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4537 | EXPORT_SYMBOL_HDA(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | |
| 4539 | /* |
| 4540 | * PCM stuff |
| 4541 | */ |
| 4542 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4543 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4544 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | { |
| 4546 | return 0; |
| 4547 | } |
| 4548 | |
| 4549 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4550 | struct hda_codec *codec, |
| 4551 | unsigned int stream_tag, |
| 4552 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4553 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | { |
| 4555 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4556 | return 0; |
| 4557 | } |
| 4558 | |
| 4559 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4560 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4561 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4563 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | return 0; |
| 4565 | } |
| 4566 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4567 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4568 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4570 | int err; |
| 4571 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4572 | /* query support PCM information from the given NID */ |
| 4573 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4574 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4575 | info->rates ? NULL : &info->rates, |
| 4576 | info->formats ? NULL : &info->formats, |
| 4577 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4578 | if (err < 0) |
| 4579 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | } |
| 4581 | if (info->ops.open == NULL) |
| 4582 | info->ops.open = hda_pcm_default_open_close; |
| 4583 | if (info->ops.close == NULL) |
| 4584 | info->ops.close = hda_pcm_default_open_close; |
| 4585 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4586 | if (snd_BUG_ON(!info->nid)) |
| 4587 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | info->ops.prepare = hda_pcm_default_prepare; |
| 4589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4591 | if (snd_BUG_ON(!info->nid)) |
| 4592 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4594 | } |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4598 | /* |
| 4599 | * codec prepare/cleanup entries |
| 4600 | */ |
| 4601 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4602 | struct hda_pcm_stream *hinfo, |
| 4603 | unsigned int stream, |
| 4604 | unsigned int format, |
| 4605 | struct snd_pcm_substream *substream) |
| 4606 | { |
| 4607 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4608 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4609 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4610 | if (ret >= 0) |
| 4611 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4612 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4613 | return ret; |
| 4614 | } |
| 4615 | EXPORT_SYMBOL_HDA(snd_hda_codec_prepare); |
| 4616 | |
| 4617 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4618 | struct hda_pcm_stream *hinfo, |
| 4619 | struct snd_pcm_substream *substream) |
| 4620 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4621 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4622 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4623 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4624 | } |
| 4625 | EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup); |
| 4626 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4627 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4628 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4629 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4630 | }; |
| 4631 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4632 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4633 | * get the empty PCM device number to assign |
| 4634 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4635 | 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] | 4636 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4637 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4638 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4639 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4640 | */ |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4641 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4642 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4643 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4644 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4645 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4646 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4647 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4648 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4649 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4650 | snd_printk(KERN_WARNING "Invalid PCM type %d\n", type); |
| 4651 | return -EINVAL; |
| 4652 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4653 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4654 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4655 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4656 | if (audio_idx[type][i] >= 8) |
| 4657 | break; |
| 4658 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4659 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4660 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4661 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4662 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4663 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4664 | /* non-fixed slots starting from 10 */ |
| 4665 | for (i = 10; i < 32; i++) { |
| 4666 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4667 | return i; |
| 4668 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4669 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4670 | |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4671 | snd_printk(KERN_WARNING "Too many %s devices\n", |
| 4672 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4673 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4674 | snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
| 4675 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4676 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4680 | * attach a new PCM stream |
| 4681 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4682 | 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] | 4683 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4684 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4685 | struct hda_pcm_stream *info; |
| 4686 | int stream, err; |
| 4687 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4688 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4689 | return -EINVAL; |
| 4690 | for (stream = 0; stream < 2; stream++) { |
| 4691 | info = &pcm->stream[stream]; |
| 4692 | if (info->substreams) { |
| 4693 | err = set_pcm_default_values(codec, info); |
| 4694 | if (err < 0) |
| 4695 | return err; |
| 4696 | } |
| 4697 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4698 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4699 | } |
| 4700 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4701 | /* assign all PCMs of the given codec */ |
| 4702 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4703 | { |
| 4704 | unsigned int pcm; |
| 4705 | int err; |
| 4706 | |
| 4707 | if (!codec->num_pcms) { |
| 4708 | if (!codec->patch_ops.build_pcms) |
| 4709 | return 0; |
| 4710 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4711 | if (err < 0) { |
| 4712 | printk(KERN_ERR "hda_codec: cannot build PCMs" |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4713 | "for #%d (error %d)\n", codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4714 | err = snd_hda_codec_reset(codec); |
| 4715 | if (err < 0) { |
| 4716 | printk(KERN_ERR |
| 4717 | "hda_codec: cannot revert codec\n"); |
| 4718 | return err; |
| 4719 | } |
| 4720 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4721 | } |
| 4722 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4723 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4724 | int dev; |
| 4725 | |
| 4726 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4727 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4728 | |
| 4729 | if (!cpcm->pcm) { |
| 4730 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4731 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4732 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4733 | cpcm->device = dev; |
| 4734 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4735 | if (err < 0) { |
| 4736 | printk(KERN_ERR "hda_codec: cannot attach " |
| 4737 | "PCM stream %d for codec #%d\n", |
| 4738 | dev, codec->addr); |
| 4739 | continue; /* no fatal error */ |
| 4740 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4741 | } |
| 4742 | } |
| 4743 | return 0; |
| 4744 | } |
| 4745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | /** |
| 4747 | * snd_hda_build_pcms - build PCM information |
| 4748 | * @bus: the BUS |
| 4749 | * |
| 4750 | * Create PCM information for each codec included in the bus. |
| 4751 | * |
| 4752 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4753 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4754 | * to create its PCM instances. |
| 4755 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4756 | * callback. |
| 4757 | * |
| 4758 | * At least, substreams, channels_min and channels_max must be filled for |
| 4759 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4760 | * When rates and/or formats are zero, the supported values are queried |
| 4761 | * from the given nid. The nid is used also by the default ops.prepare |
| 4762 | * and ops.cleanup callbacks. |
| 4763 | * |
| 4764 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4765 | * ops.close is supposed to be called in the close callback. |
| 4766 | * ops.prepare should be called in the prepare or hw_params callback |
| 4767 | * with the proper parameters for set up. |
| 4768 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4769 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4770 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4772 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4774 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4776 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4777 | int err = snd_hda_codec_build_pcms(codec); |
| 4778 | if (err < 0) |
| 4779 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | } |
| 4781 | return 0; |
| 4782 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4783 | EXPORT_SYMBOL_HDA(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | /** |
| 4786 | * snd_hda_check_board_config - compare the current codec with the config table |
| 4787 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4788 | * @num_configs: number of config enums |
| 4789 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | * @tbl: configuration table, terminated by null entries |
| 4791 | * |
| 4792 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4793 | * given configuration table. If a matching entry is found, returns its |
| 4794 | * config value (supposed to be 0 or positive). |
| 4795 | * |
| 4796 | * If no entries are matching, the function returns a negative value. |
| 4797 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4798 | int snd_hda_check_board_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4799 | int num_configs, const char * const *models, |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 4800 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | { |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4802 | if (codec->modelname && models) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4803 | int i; |
| 4804 | for (i = 0; i < num_configs; i++) { |
| 4805 | if (models[i] && |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4806 | !strcmp(codec->modelname, models[i])) { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4807 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 4808 | "selected\n", models[i]); |
| 4809 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4814 | if (!codec->bus->pci || !tbl) |
| 4815 | return -1; |
| 4816 | |
| 4817 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 4818 | if (!tbl) |
| 4819 | return -1; |
| 4820 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 4821 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4822 | char tmp[10]; |
| 4823 | const char *model = NULL; |
| 4824 | if (models) |
| 4825 | model = models[tbl->value]; |
| 4826 | if (!model) { |
| 4827 | sprintf(tmp, "#%d", tbl->value); |
| 4828 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 4830 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4831 | "for config %x:%x (%s)\n", |
| 4832 | model, tbl->subvendor, tbl->subdevice, |
| 4833 | (tbl->name ? tbl->name : "Unknown device")); |
| 4834 | #endif |
| 4835 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | } |
| 4837 | return -1; |
| 4838 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4839 | EXPORT_SYMBOL_HDA(snd_hda_check_board_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
| 4841 | /** |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4842 | * snd_hda_check_board_codec_sid_config - compare the current codec |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4843 | subsystem ID with the |
| 4844 | config table |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4845 | |
| 4846 | This is important for Gateway notebooks with SB450 HDA Audio |
| 4847 | where the vendor ID of the PCI device is: |
| 4848 | ATI Technologies Inc SB450 HDA Audio [1002:437b] |
| 4849 | and the vendor/subvendor are found only at the codec. |
| 4850 | |
| 4851 | * @codec: the HDA codec |
| 4852 | * @num_configs: number of config enums |
| 4853 | * @models: array of model name strings |
| 4854 | * @tbl: configuration table, terminated by null entries |
| 4855 | * |
| 4856 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 4857 | * given configuration table. If a matching entry is found, returns its |
| 4858 | * config value (supposed to be 0 or positive). |
| 4859 | * |
| 4860 | * If no entries are matching, the function returns a negative value. |
| 4861 | */ |
| 4862 | int snd_hda_check_board_codec_sid_config(struct hda_codec *codec, |
Takashi Iwai | ea73496 | 2011-01-17 11:29:34 +0100 | [diff] [blame] | 4863 | int num_configs, const char * const *models, |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4864 | const struct snd_pci_quirk *tbl) |
| 4865 | { |
| 4866 | const struct snd_pci_quirk *q; |
| 4867 | |
| 4868 | /* Search for codec ID */ |
| 4869 | for (q = tbl; q->subvendor; q++) { |
Takashi Iwai | e2301a4 | 2011-11-22 19:58:56 +0100 | [diff] [blame] | 4870 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 4871 | unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask; |
| 4872 | if ((codec->subsystem_id & mask) == id) |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4873 | break; |
| 4874 | } |
| 4875 | |
| 4876 | if (!q->subvendor) |
| 4877 | return -1; |
| 4878 | |
| 4879 | tbl = q; |
| 4880 | |
| 4881 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | d94ff6b | 2009-09-02 00:20:21 +0200 | [diff] [blame] | 4882 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Mauro Carvalho Chehab | 2eda344 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 4883 | char tmp[10]; |
| 4884 | const char *model = NULL; |
| 4885 | if (models) |
| 4886 | model = models[tbl->value]; |
| 4887 | if (!model) { |
| 4888 | sprintf(tmp, "#%d", tbl->value); |
| 4889 | model = tmp; |
| 4890 | } |
| 4891 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 4892 | "for config %x:%x (%s)\n", |
| 4893 | model, tbl->subvendor, tbl->subdevice, |
| 4894 | (tbl->name ? tbl->name : "Unknown device")); |
| 4895 | #endif |
| 4896 | return tbl->value; |
| 4897 | } |
| 4898 | return -1; |
| 4899 | } |
| 4900 | EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config); |
| 4901 | |
| 4902 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | * snd_hda_add_new_ctls - create controls from the array |
| 4904 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4905 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | * |
| 4907 | * This helper function creates and add new controls in the given array. |
| 4908 | * The array must be terminated with an empty entry as terminator. |
| 4909 | * |
| 4910 | * Returns 0 if successful, or a negative error code. |
| 4911 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4912 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4913 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4915 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | |
| 4917 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4918 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4919 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4920 | if (knew->iface == -1) /* skip this codec private value */ |
| 4921 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4922 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4923 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4924 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4925 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4926 | if (addr > 0) |
| 4927 | kctl->id.device = addr; |
| 4928 | if (idx > 0) |
| 4929 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4930 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4931 | if (!err) |
| 4932 | break; |
| 4933 | /* try first with another device index corresponding to |
| 4934 | * the codec addr; if it still fails (or it's the |
| 4935 | * primary codec), then try another control index |
| 4936 | */ |
| 4937 | if (!addr && codec->addr) |
| 4938 | addr = codec->addr; |
| 4939 | else if (!idx && !knew->index) { |
| 4940 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4941 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4942 | if (idx <= 0) |
| 4943 | return err; |
| 4944 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4945 | return err; |
| 4946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
| 4948 | return 0; |
| 4949 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 4950 | EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4952 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4953 | static void hda_power_work(struct work_struct *work) |
| 4954 | { |
| 4955 | struct hda_codec *codec = |
| 4956 | container_of(work, struct hda_codec, power_work.work); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4957 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 4958 | unsigned int state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4959 | |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4960 | spin_lock(&codec->power_lock); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4961 | if (codec->power_transition > 0) { /* during power-up sequence? */ |
| 4962 | spin_unlock(&codec->power_lock); |
| 4963 | return; |
| 4964 | } |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4965 | if (!codec->power_on || codec->power_count) { |
| 4966 | codec->power_transition = 0; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4967 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4968 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 4969 | } |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4970 | spin_unlock(&codec->power_lock); |
| 4971 | |
Dylan Reid | d17344b | 2012-09-28 15:57:01 -0700 | [diff] [blame] | 4972 | state = hda_call_codec_suspend(codec, true); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4973 | if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) |
| 4974 | hda_call_pm_notify(codec, false); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | static void hda_keep_power_on(struct hda_codec *codec) |
| 4978 | { |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4979 | spin_lock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4980 | codec->power_count++; |
| 4981 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4982 | codec->power_jiffies = jiffies; |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 4983 | spin_unlock(&codec->power_lock); |
Takashi Iwai | a40e0a8 | 2013-11-20 12:41:20 +0100 | [diff] [blame] | 4984 | hda_call_pm_notify(codec, true); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4985 | } |
| 4986 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4987 | /* update the power on/off account with the current jiffies */ |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 4988 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 4989 | { |
| 4990 | unsigned long delta = jiffies - codec->power_jiffies; |
| 4991 | if (codec->power_on) |
| 4992 | codec->power_on_acct += delta; |
| 4993 | else |
| 4994 | codec->power_off_acct += delta; |
| 4995 | codec->power_jiffies += delta; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4996 | } |
| 4997 | |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 4998 | /* Transition to powered up, if wait_power_down then wait for a pending |
| 4999 | * transition to D3 to complete. A pending D3 transition is indicated |
| 5000 | * with power_transition == -1. */ |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5001 | /* call this with codec->power_lock held! */ |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5002 | 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] | 5003 | { |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5004 | /* Return if power_on or transitioning to power_on, unless currently |
| 5005 | * powering down. */ |
| 5006 | if ((codec->power_on || codec->power_transition > 0) && |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5007 | !(wait_power_down && codec->power_transition < 0)) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5008 | return; |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5009 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5010 | |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5011 | cancel_delayed_work_sync(&codec->power_work); |
| 5012 | |
| 5013 | spin_lock(&codec->power_lock); |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5014 | /* If the power down delayed work was cancelled above before starting, |
| 5015 | * then there is no need to go through power up here. |
| 5016 | */ |
| 5017 | if (codec->power_on) { |
Takashi Iwai | 535b6c5 | 2012-08-20 21:25:22 +0200 | [diff] [blame] | 5018 | if (codec->power_transition < 0) |
| 5019 | codec->power_transition = 0; |
Dylan Reid | b43d224 | 2012-06-21 21:51:22 -0700 | [diff] [blame] | 5020 | return; |
| 5021 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5022 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 5023 | trace_hda_power_up(codec); |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5024 | snd_hda_update_power_acct(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5025 | codec->power_on = 1; |
Takashi Iwai | a2f6309 | 2009-11-11 09:34:25 +0100 | [diff] [blame] | 5026 | codec->power_jiffies = jiffies; |
Takashi Iwai | 7f30830 | 2012-05-08 16:52:23 +0200 | [diff] [blame] | 5027 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5028 | spin_unlock(&codec->power_lock); |
| 5029 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5030 | hda_call_codec_resume(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5031 | |
| 5032 | spin_lock(&codec->power_lock); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5033 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5034 | } |
Dylan Reid | b4a91cf | 2012-06-15 19:36:23 -0700 | [diff] [blame] | 5035 | |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5036 | #define power_save(codec) \ |
| 5037 | ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5038 | |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5039 | /* Transition to powered down */ |
| 5040 | static void __snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5041 | { |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5042 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5043 | return; |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5044 | |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5045 | if (power_save(codec)) { |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 5046 | codec->power_transition = -1; /* avoid reentrance */ |
Takashi Iwai | c107b41 | 2009-01-13 17:46:37 +0100 | [diff] [blame] | 5047 | queue_delayed_work(codec->bus->workq, &codec->power_work, |
Takashi Iwai | fee2fba | 2008-11-27 12:43:28 +0100 | [diff] [blame] | 5048 | msecs_to_jiffies(power_save(codec) * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 5049 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | /** |
| 5053 | * snd_hda_power_save - Power-up/down/sync the codec |
| 5054 | * @codec: HD-audio codec |
| 5055 | * @delta: the counter delta to change |
| 5056 | * |
| 5057 | * Change the power-up counter via @delta, and power up or down the hardware |
| 5058 | * appropriately. For the power-down, queue to the delayed action. |
| 5059 | * Passing zero to @delta means to synchronize the power state. |
| 5060 | */ |
| 5061 | void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) |
| 5062 | { |
| 5063 | spin_lock(&codec->power_lock); |
| 5064 | codec->power_count += delta; |
| 5065 | trace_hda_power_count(codec); |
| 5066 | if (delta > 0) |
| 5067 | __snd_hda_power_up(codec, d3wait); |
| 5068 | else |
| 5069 | __snd_hda_power_down(codec); |
Takashi Iwai | 5536c6d | 2012-05-08 17:08:10 +0200 | [diff] [blame] | 5070 | spin_unlock(&codec->power_lock); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5071 | } |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 5072 | EXPORT_SYMBOL_HDA(snd_hda_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5073 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5074 | /** |
| 5075 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 5076 | * @codec: HD-audio codec |
| 5077 | * @check: the object containing an AMP list and the status |
| 5078 | * @nid: NID to check / update |
| 5079 | * |
| 5080 | * Check whether the given NID is in the amp list. If it's in the list, |
| 5081 | * check the current AMP status, and update the the power-status according |
| 5082 | * to the mute status. |
| 5083 | * |
| 5084 | * This function is supposed to be set or called from the check_power_status |
| 5085 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5086 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5087 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 5088 | struct hda_loopback_check *check, |
| 5089 | hda_nid_t nid) |
| 5090 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5091 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5092 | int ch, v; |
| 5093 | |
| 5094 | if (!check->amplist) |
| 5095 | return 0; |
| 5096 | for (p = check->amplist; p->nid; p++) { |
| 5097 | if (p->nid == nid) |
| 5098 | break; |
| 5099 | } |
| 5100 | if (!p->nid) |
| 5101 | return 0; /* nothing changed */ |
| 5102 | |
| 5103 | for (p = check->amplist; p->nid; p++) { |
| 5104 | for (ch = 0; ch < 2; ch++) { |
| 5105 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 5106 | p->idx); |
| 5107 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 5108 | if (!check->power_on) { |
| 5109 | check->power_on = 1; |
| 5110 | snd_hda_power_up(codec); |
| 5111 | } |
| 5112 | return 1; |
| 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | if (check->power_on) { |
| 5117 | check->power_on = 0; |
| 5118 | snd_hda_power_down(codec); |
| 5119 | } |
| 5120 | return 0; |
| 5121 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5122 | EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5123 | |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5124 | static void hda_suspend_work(struct work_struct *work) |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5125 | { |
| 5126 | struct hda_codec *codec = |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5127 | container_of(work, struct hda_codec, suspend_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5128 | |
| 5129 | hda_call_codec_suspend(codec, false); |
| 5130 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5131 | |
| 5132 | static void hda_resume_work(struct work_struct *work) |
| 5133 | { |
| 5134 | struct hda_codec *codec = |
| 5135 | container_of(work, struct hda_codec, resume_work); |
| 5136 | |
| 5137 | hda_call_codec_resume(codec); |
| 5138 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 5139 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5141 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5142 | * Channel mode helper |
| 5143 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5144 | |
| 5145 | /** |
| 5146 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
| 5147 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5148 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 5149 | struct snd_ctl_elem_info *uinfo, |
| 5150 | const struct hda_channel_mode *chmode, |
| 5151 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5152 | { |
| 5153 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5154 | uinfo->count = 1; |
| 5155 | uinfo->value.enumerated.items = num_chmodes; |
| 5156 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 5157 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 5158 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 5159 | chmode[uinfo->value.enumerated.item].channels); |
| 5160 | return 0; |
| 5161 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5162 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5163 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5164 | /** |
| 5165 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
| 5166 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5167 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 5168 | struct snd_ctl_elem_value *ucontrol, |
| 5169 | const struct hda_channel_mode *chmode, |
| 5170 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5171 | int max_channels) |
| 5172 | { |
| 5173 | int i; |
| 5174 | |
| 5175 | for (i = 0; i < num_chmodes; i++) { |
| 5176 | if (max_channels == chmode[i].channels) { |
| 5177 | ucontrol->value.enumerated.item[0] = i; |
| 5178 | break; |
| 5179 | } |
| 5180 | } |
| 5181 | return 0; |
| 5182 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5183 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5184 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5185 | /** |
| 5186 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
| 5187 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5188 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 5189 | struct snd_ctl_elem_value *ucontrol, |
| 5190 | const struct hda_channel_mode *chmode, |
| 5191 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5192 | int *max_channelsp) |
| 5193 | { |
| 5194 | unsigned int mode; |
| 5195 | |
| 5196 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 5197 | if (mode >= num_chmodes) |
| 5198 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5199 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5200 | return 0; |
| 5201 | /* change the current channel setting */ |
| 5202 | *max_channelsp = chmode[mode].channels; |
| 5203 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5204 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5205 | return 1; |
| 5206 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5207 | EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 5208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | /* |
| 5210 | * input MUX helper |
| 5211 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5212 | |
| 5213 | /** |
| 5214 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
| 5215 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5216 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 5217 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | { |
| 5219 | unsigned int index; |
| 5220 | |
| 5221 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5222 | uinfo->count = 1; |
| 5223 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5224 | if (!imux->num_items) |
| 5225 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | index = uinfo->value.enumerated.item; |
| 5227 | if (index >= imux->num_items) |
| 5228 | index = imux->num_items - 1; |
| 5229 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 5230 | return 0; |
| 5231 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5232 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5234 | /** |
| 5235 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
| 5236 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5237 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 5238 | const struct hda_input_mux *imux, |
| 5239 | struct snd_ctl_elem_value *ucontrol, |
| 5240 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | unsigned int *cur_val) |
| 5242 | { |
| 5243 | unsigned int idx; |
| 5244 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 5245 | if (!imux->num_items) |
| 5246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | idx = ucontrol->value.enumerated.item[0]; |
| 5248 | if (idx >= imux->num_items) |
| 5249 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5250 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 5252 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 5253 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | *cur_val = idx; |
| 5255 | return 1; |
| 5256 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5257 | EXPORT_SYMBOL_HDA(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | |
| 5259 | |
| 5260 | /* |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5261 | * process kcontrol info callback of a simple string enum array |
| 5262 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 5263 | */ |
| 5264 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 5265 | struct snd_ctl_elem_info *uinfo, |
| 5266 | int num_items, const char * const *texts) |
| 5267 | { |
| 5268 | static const char * const texts_default[] = { |
| 5269 | "Disabled", "Enabled" |
| 5270 | }; |
| 5271 | |
| 5272 | if (!texts || !num_items) { |
| 5273 | num_items = 2; |
| 5274 | texts = texts_default; |
| 5275 | } |
| 5276 | |
| 5277 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 5278 | uinfo->count = 1; |
| 5279 | uinfo->value.enumerated.items = num_items; |
| 5280 | if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) |
| 5281 | uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; |
| 5282 | strcpy(uinfo->value.enumerated.name, |
| 5283 | texts[uinfo->value.enumerated.item]); |
| 5284 | return 0; |
| 5285 | } |
| 5286 | EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info); |
| 5287 | |
| 5288 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | * Multi-channel / digital-out PCM helper functions |
| 5290 | */ |
| 5291 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5292 | /* setup SPDIF output stream */ |
| 5293 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5294 | unsigned int stream_tag, unsigned int format) |
| 5295 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5296 | struct hda_spdif_out *spdif; |
| 5297 | unsigned int curr_fmt; |
| 5298 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5299 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5300 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5301 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5302 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5303 | reset = codec->spdif_status_reset && |
| 5304 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5305 | curr_fmt != format; |
| 5306 | |
| 5307 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5308 | updated */ |
| 5309 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5310 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5311 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5312 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5313 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5314 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5315 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5316 | for (d = codec->slave_dig_outs; *d; d++) |
| 5317 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5318 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5319 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5320 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5321 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5322 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5323 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5324 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5325 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5326 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5327 | { |
| 5328 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5329 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5330 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5331 | for (d = codec->slave_dig_outs; *d; d++) |
| 5332 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5333 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5334 | } |
| 5335 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5336 | /** |
| 5337 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5338 | * @bus: HD-audio bus |
| 5339 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5340 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5341 | { |
| 5342 | struct hda_codec *codec; |
| 5343 | |
| 5344 | if (!bus) |
| 5345 | return; |
| 5346 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5347 | if (hda_codec_is_power_on(codec) && |
| 5348 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5349 | codec->patch_ops.reboot_notify(codec); |
| 5350 | } |
| 5351 | } |
Takashi Iwai | 8f217a2 | 2009-11-10 18:26:12 +0100 | [diff] [blame] | 5352 | EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5353 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5354 | /** |
| 5355 | * 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] | 5356 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5357 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5358 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5360 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5361 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5362 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5363 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5365 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | return 0; |
| 5367 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5368 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5370 | /** |
| 5371 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
| 5372 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5373 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5374 | struct hda_multi_out *mout, |
| 5375 | unsigned int stream_tag, |
| 5376 | unsigned int format, |
| 5377 | struct snd_pcm_substream *substream) |
| 5378 | { |
| 5379 | mutex_lock(&codec->spdif_mutex); |
| 5380 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5381 | mutex_unlock(&codec->spdif_mutex); |
| 5382 | return 0; |
| 5383 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5384 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5385 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5386 | /** |
| 5387 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
| 5388 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5389 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5390 | struct hda_multi_out *mout) |
| 5391 | { |
| 5392 | mutex_lock(&codec->spdif_mutex); |
| 5393 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5394 | mutex_unlock(&codec->spdif_mutex); |
| 5395 | return 0; |
| 5396 | } |
| 5397 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup); |
| 5398 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5399 | /** |
| 5400 | * snd_hda_multi_out_dig_close - release the digital out stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5402 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5403 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5405 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5407 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | return 0; |
| 5409 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5410 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5412 | /** |
| 5413 | * snd_hda_multi_out_analog_open - open analog outputs |
| 5414 | * |
| 5415 | * Open analog outputs and set up the hw-constraints. |
| 5416 | * If the digital outputs can be opened as slave, open the digital |
| 5417 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5419 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5420 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5421 | struct snd_pcm_substream *substream, |
| 5422 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5424 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5425 | runtime->hw.channels_max = mout->max_channels; |
| 5426 | if (mout->dig_out_nid) { |
| 5427 | if (!mout->analog_rates) { |
| 5428 | mout->analog_rates = hinfo->rates; |
| 5429 | mout->analog_formats = hinfo->formats; |
| 5430 | mout->analog_maxbps = hinfo->maxbps; |
| 5431 | } else { |
| 5432 | runtime->hw.rates = mout->analog_rates; |
| 5433 | runtime->hw.formats = mout->analog_formats; |
| 5434 | hinfo->maxbps = mout->analog_maxbps; |
| 5435 | } |
| 5436 | if (!mout->spdif_rates) { |
| 5437 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5438 | &mout->spdif_rates, |
| 5439 | &mout->spdif_formats, |
| 5440 | &mout->spdif_maxbps); |
| 5441 | } |
| 5442 | mutex_lock(&codec->spdif_mutex); |
| 5443 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5444 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5445 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5446 | runtime->hw.rates &= mout->spdif_rates; |
| 5447 | runtime->hw.formats &= mout->spdif_formats; |
| 5448 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5449 | hinfo->maxbps = mout->spdif_maxbps; |
| 5450 | } else { |
| 5451 | mout->share_spdif = 0; |
| 5452 | /* FIXME: need notify? */ |
| 5453 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5454 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5455 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5458 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5459 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5460 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5462 | /** |
| 5463 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
| 5464 | * |
| 5465 | * Set up the i/o for analog out. |
| 5466 | * 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] | 5467 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5468 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5469 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | unsigned int stream_tag, |
| 5471 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5472 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5474 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5476 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | int i; |
| 5478 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5479 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5480 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5481 | if (mout->dig_out_nid && mout->share_spdif && |
| 5482 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5484 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5485 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5486 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5488 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5489 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | } else { |
| 5491 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5492 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | } |
| 5494 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5495 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
| 5497 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5498 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5499 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5500 | if (!mout->no_share_stream && |
| 5501 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5503 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5504 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5505 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5506 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5507 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5508 | snd_hda_codec_setup_stream(codec, |
| 5509 | mout->hp_out_nid[i], |
| 5510 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | /* surrounds */ |
| 5513 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5514 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5515 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5516 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5517 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5518 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5519 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5521 | |
| 5522 | /* extra surrounds */ |
| 5523 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5524 | int ch = 0; |
| 5525 | if (!mout->extra_out_nid[i]) |
| 5526 | break; |
| 5527 | if (chs >= (i + 1) * 2) |
| 5528 | ch = i * 2; |
| 5529 | else if (!mout->no_share_stream) |
| 5530 | break; |
| 5531 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5532 | stream_tag, ch, format); |
| 5533 | } |
| 5534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5537 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5539 | /** |
| 5540 | * 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] | 5541 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5542 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5543 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5544 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5545 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | int i; |
| 5547 | |
| 5548 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5549 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5551 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5552 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5553 | if (mout->hp_out_nid[i]) |
| 5554 | snd_hda_codec_cleanup_stream(codec, |
| 5555 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5556 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5557 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5558 | snd_hda_codec_cleanup_stream(codec, |
| 5559 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5560 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | 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] | 5562 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | mout->dig_out_used = 0; |
| 5564 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5565 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | return 0; |
| 5567 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5568 | EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5570 | /** |
| 5571 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
| 5572 | * |
| 5573 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5574 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5575 | */ |
| 5576 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5577 | { |
| 5578 | unsigned int pincap; |
| 5579 | unsigned int oldval; |
| 5580 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5581 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5582 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5583 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5584 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5585 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5586 | return AC_PINCTL_VREF_80; |
| 5587 | else if (pincap & AC_PINCAP_VREF_50) |
| 5588 | return AC_PINCTL_VREF_50; |
| 5589 | else if (pincap & AC_PINCAP_VREF_100) |
| 5590 | return AC_PINCTL_VREF_100; |
| 5591 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5592 | return AC_PINCTL_VREF_GRD; |
| 5593 | return AC_PINCTL_VREF_HIZ; |
| 5594 | } |
| 5595 | EXPORT_SYMBOL_HDA(snd_hda_get_default_vref); |
| 5596 | |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5597 | /* correct the pin ctl value for matching with the pin cap */ |
| 5598 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5599 | hda_nid_t pin, unsigned int val) |
| 5600 | { |
| 5601 | static unsigned int cap_lists[][2] = { |
| 5602 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5603 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5604 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5605 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5606 | }; |
| 5607 | unsigned int cap; |
| 5608 | |
| 5609 | if (!val) |
| 5610 | return 0; |
| 5611 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5612 | if (!cap) |
| 5613 | return val; /* don't know what to do... */ |
| 5614 | |
| 5615 | if (val & AC_PINCTL_OUT_EN) { |
| 5616 | if (!(cap & AC_PINCAP_OUT)) |
| 5617 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5618 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5619 | val &= ~AC_PINCTL_HP_EN; |
| 5620 | } |
| 5621 | |
| 5622 | if (val & AC_PINCTL_IN_EN) { |
| 5623 | if (!(cap & AC_PINCAP_IN)) |
| 5624 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5625 | else { |
| 5626 | unsigned int vcap, vref; |
| 5627 | int i; |
| 5628 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5629 | vref = val & AC_PINCTL_VREFEN; |
| 5630 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5631 | if (vref == cap_lists[i][0] && |
| 5632 | !(vcap & cap_lists[i][1])) { |
| 5633 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5634 | vref = AC_PINCTL_VREF_HIZ; |
| 5635 | else |
| 5636 | vref = cap_lists[i + 1][0]; |
| 5637 | } |
| 5638 | } |
| 5639 | val &= ~AC_PINCTL_VREFEN; |
| 5640 | val |= vref; |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | return val; |
| 5645 | } |
| 5646 | EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl); |
| 5647 | |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5648 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5649 | unsigned int val, bool cached) |
| 5650 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5651 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5652 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5653 | if (cached) |
| 5654 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5655 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5656 | else |
| 5657 | return snd_hda_codec_write(codec, pin, 0, |
| 5658 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5659 | } |
| 5660 | EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl); |
| 5661 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5662 | /** |
| 5663 | * snd_hda_add_imux_item - Add an item to input_mux |
| 5664 | * |
| 5665 | * When the same label is used already in the existing items, the number |
| 5666 | * suffix is appended to the label. This label index number is stored |
| 5667 | * to type_idx when non-NULL pointer is given. |
| 5668 | */ |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5669 | int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label, |
| 5670 | int index, int *type_idx) |
| 5671 | { |
| 5672 | int i, label_idx = 0; |
| 5673 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
| 5674 | snd_printd(KERN_ERR "hda_codec: Too many imux items!\n"); |
| 5675 | return -EINVAL; |
| 5676 | } |
| 5677 | for (i = 0; i < imux->num_items; i++) { |
| 5678 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5679 | label_idx++; |
| 5680 | } |
| 5681 | if (type_idx) |
| 5682 | *type_idx = label_idx; |
| 5683 | if (label_idx > 0) |
| 5684 | snprintf(imux->items[imux->num_items].label, |
| 5685 | sizeof(imux->items[imux->num_items].label), |
| 5686 | "%s %d", label, label_idx); |
| 5687 | else |
| 5688 | strlcpy(imux->items[imux->num_items].label, label, |
| 5689 | sizeof(imux->items[imux->num_items].label)); |
| 5690 | imux->items[imux->num_items].index = index; |
| 5691 | imux->num_items++; |
| 5692 | return 0; |
| 5693 | } |
| 5694 | EXPORT_SYMBOL_HDA(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5695 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 5696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | #ifdef CONFIG_PM |
| 5698 | /* |
| 5699 | * power management |
| 5700 | */ |
| 5701 | |
| 5702 | /** |
| 5703 | * snd_hda_suspend - suspend the codecs |
| 5704 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | * |
| 5706 | * Returns 0 if successful. |
| 5707 | */ |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 5708 | int snd_hda_suspend(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5710 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5712 | list_for_each_entry(codec, &bus->codec_list, list) { |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5713 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5714 | if (hda_codec_is_power_on(codec)) { |
| 5715 | if (bus->num_codecs > 1) |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5716 | queue_work(bus->pm_wq, &codec->suspend_work); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5717 | else |
| 5718 | hda_call_codec_suspend(codec, false); |
| 5719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | } |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5721 | |
| 5722 | if (bus->num_codecs > 1) |
| 5723 | flush_workqueue(bus->pm_wq); |
| 5724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | return 0; |
| 5726 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5727 | EXPORT_SYMBOL_HDA(snd_hda_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | |
| 5729 | /** |
| 5730 | * snd_hda_resume - resume the codecs |
| 5731 | * @bus: the HDA bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | * |
| 5733 | * Returns 0 if successful. |
| 5734 | */ |
| 5735 | int snd_hda_resume(struct hda_bus *bus) |
| 5736 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5737 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5739 | list_for_each_entry(codec, &bus->codec_list, list) { |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5740 | if (bus->num_codecs > 1) |
| 5741 | queue_work(bus->pm_wq, &codec->resume_work); |
| 5742 | else |
| 5743 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | } |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5745 | |
| 5746 | if (bus->num_codecs > 1) |
| 5747 | flush_workqueue(bus->pm_wq); |
| 5748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | return 0; |
| 5750 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5751 | EXPORT_SYMBOL_HDA(snd_hda_resume); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5752 | #endif /* CONFIG_PM */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5753 | |
| 5754 | /* |
| 5755 | * generic arrays |
| 5756 | */ |
| 5757 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5758 | /** |
| 5759 | * snd_array_new - get a new element from the given array |
| 5760 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5761 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5762 | * Get a new element from the given array. If it exceeds the |
| 5763 | * pre-allocated array size, re-allocate the array. |
| 5764 | * |
| 5765 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5766 | */ |
| 5767 | void *snd_array_new(struct snd_array *array) |
| 5768 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5769 | if (snd_BUG_ON(!array->elem_size)) |
| 5770 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5771 | if (array->used >= array->alloced) { |
| 5772 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5773 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5774 | void *nlist; |
| 5775 | if (snd_BUG_ON(num >= 4096)) |
| 5776 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5777 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5778 | if (!nlist) |
| 5779 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5780 | array->list = nlist; |
| 5781 | array->alloced = num; |
| 5782 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5783 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5784 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5785 | EXPORT_SYMBOL_HDA(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5786 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5787 | /** |
| 5788 | * snd_array_free - free the given array elements |
| 5789 | * @array: the array object |
| 5790 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5791 | void snd_array_free(struct snd_array *array) |
| 5792 | { |
| 5793 | kfree(array->list); |
| 5794 | array->used = 0; |
| 5795 | array->alloced = 0; |
| 5796 | array->list = NULL; |
| 5797 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5798 | EXPORT_SYMBOL_HDA(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5799 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5800 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5801 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5802 | * @pcm: PCM caps bits |
| 5803 | * @buf: the string buffer to write |
| 5804 | * @buflen: the max buffer length |
| 5805 | * |
| 5806 | * used by hda_proc.c and hda_eld.c |
| 5807 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5808 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5809 | { |
| 5810 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5811 | int i, j; |
| 5812 | |
| 5813 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5814 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5815 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5816 | |
| 5817 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5818 | } |
Takashi Iwai | ff7a326 | 2008-11-28 15:17:06 +0100 | [diff] [blame] | 5819 | EXPORT_SYMBOL_HDA(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5820 | |
| 5821 | MODULE_DESCRIPTION("HDA codec core"); |
| 5822 | MODULE_LICENSE("GPL"); |