Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Interface for Intel High Definition Audio Codec |
| 3 | * |
| 4 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * |
| 7 | * This driver is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This driver is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/slab.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 28 | #include <linux/async.h> |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 29 | #include <linux/pm.h> |
| 30 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/core.h> |
| 32 | #include "hda_codec.h" |
| 33 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 34 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 36 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 38 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 39 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 40 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 43 | #define codec_in_pm(codec) atomic_read(&(codec)->in_pm) |
| 44 | #define hda_codec_is_power_on(codec) \ |
| 45 | (!pm_runtime_suspended(hda_codec_dev(codec))) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 46 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 47 | #define codec_in_pm(codec) 0 |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 48 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 51 | /** |
| 52 | * snd_hda_get_jack_location - Give a location string of the jack |
| 53 | * @cfg: pin default config value |
| 54 | * |
| 55 | * Parse the pin default config value and returns the string of the |
| 56 | * jack location, e.g. "Rear", "Front", etc. |
| 57 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 58 | const char *snd_hda_get_jack_location(u32 cfg) |
| 59 | { |
| 60 | static char *bases[7] = { |
| 61 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 62 | }; |
| 63 | static unsigned char specials_idx[] = { |
| 64 | 0x07, 0x08, |
| 65 | 0x17, 0x18, 0x19, |
| 66 | 0x37, 0x38 |
| 67 | }; |
| 68 | static char *specials[] = { |
| 69 | "Rear Panel", "Drive Bar", |
| 70 | "Riser", "HDMI", "ATAPI", |
| 71 | "Mobile-In", "Mobile-Out" |
| 72 | }; |
| 73 | int i; |
| 74 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 75 | if ((cfg & 0x0f) < 7) |
| 76 | return bases[cfg & 0x0f]; |
| 77 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 78 | if (cfg == specials_idx[i]) |
| 79 | return specials[i]; |
| 80 | } |
| 81 | return "UNKNOWN"; |
| 82 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 84 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 85 | /** |
| 86 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 87 | * @cfg: pin default config value |
| 88 | * |
| 89 | * Parse the pin default config value and returns the string of the |
| 90 | * jack connectivity, i.e. external or internal connection. |
| 91 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 92 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 93 | { |
| 94 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 95 | |
| 96 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 97 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 98 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 99 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 100 | /** |
| 101 | * snd_hda_get_jack_type - Give a type string of the jack |
| 102 | * @cfg: pin default config value |
| 103 | * |
| 104 | * Parse the pin default config value and returns the string of the |
| 105 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 106 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 107 | const char *snd_hda_get_jack_type(u32 cfg) |
| 108 | { |
| 109 | static char *jack_types[16] = { |
| 110 | "Line Out", "Speaker", "HP Out", "CD", |
| 111 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 112 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 113 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 117 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 118 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 119 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 120 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 121 | /* |
| 122 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 123 | */ |
| 124 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 125 | 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] | 126 | unsigned int verb, unsigned int parm) |
| 127 | { |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 128 | unsigned int addr = codec->core.addr; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 129 | u32 val; |
| 130 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 131 | if ((addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 132 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 133 | codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n", |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 134 | addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 135 | return ~0; |
| 136 | } |
| 137 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 138 | val = (u32)addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 139 | val |= (u32)nid << 20; |
| 140 | val |= verb << 8; |
| 141 | val |= parm; |
| 142 | return val; |
| 143 | } |
| 144 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Send and receive a verb |
| 147 | */ |
| 148 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 149 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 150 | { |
| 151 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 152 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 153 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 154 | if (cmd == ~0) |
| 155 | return -1; |
| 156 | |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 157 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 158 | snd_hda_power_up(codec); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 159 | mutex_lock(&bus->core.cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 160 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 161 | bus->no_response_fallback = 1; |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 162 | err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr, |
| 163 | cmd, res); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 164 | bus->no_response_fallback = 0; |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 165 | mutex_unlock(&bus->core.cmd_mutex); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 166 | snd_hda_power_down(codec); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 167 | if (!codec_in_pm(codec) && res && err < 0 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 168 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 169 | codec_dbg(codec, |
| 170 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 171 | bus->ops.bus_reset(bus); |
| 172 | } |
| 173 | goto again; |
| 174 | } |
| 175 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 176 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 177 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 178 | return err; |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /** |
| 182 | * snd_hda_codec_read - send a command and get the response |
| 183 | * @codec: the HDA codec |
| 184 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 185 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * @verb: the verb to send |
| 187 | * @parm: the parameter for the verb |
| 188 | * |
| 189 | * Send a single command and read the corresponding response. |
| 190 | * |
| 191 | * Returns the obtained response value, or -1 for an error. |
| 192 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 193 | 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] | 194 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | unsigned int verb, unsigned int parm) |
| 196 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 197 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 198 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 199 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 200 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return res; |
| 202 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 203 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * snd_hda_codec_write - send a single command without waiting for response |
| 207 | * @codec: the HDA codec |
| 208 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 209 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * @verb: the verb to send |
| 211 | * @parm: the parameter for the verb |
| 212 | * |
| 213 | * Send a single command without waiting for response. |
| 214 | * |
| 215 | * Returns 0 if successful, or a negative error code. |
| 216 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 217 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 218 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 220 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 221 | return codec_exec_verb(codec, cmd, flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * snd_hda_sequence_write - sequence writes |
| 227 | * @codec: the HDA codec |
| 228 | * @seq: VERB array to send |
| 229 | * |
| 230 | * Send the commands sequentially from the given array. |
| 231 | * The array must be terminated with NID=0. |
| 232 | */ |
| 233 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 234 | { |
| 235 | for (; seq->nid; seq++) |
| 236 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 237 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 242 | * @codec: the HDA codec |
| 243 | * @nid: NID to parse |
| 244 | * @start_id: the pointer to store the start NID |
| 245 | * |
| 246 | * Parse the NID and store the start NID of its sub-nodes. |
| 247 | * Returns the number of sub-nodes. |
| 248 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 249 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 250 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | unsigned int parm; |
| 253 | |
| 254 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 255 | if (parm == -1) { |
| 256 | *start_id = 0; |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 257 | return 0; |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | *start_id = (parm >> 16) & 0x7fff; |
| 260 | return (int)(parm & 0x7fff); |
| 261 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 264 | /* connection list element */ |
| 265 | struct hda_conn_list { |
| 266 | struct list_head list; |
| 267 | int len; |
| 268 | hda_nid_t nid; |
| 269 | hda_nid_t conns[0]; |
| 270 | }; |
| 271 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 272 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 273 | static struct hda_conn_list * |
| 274 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 275 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 276 | struct hda_conn_list *p; |
| 277 | list_for_each_entry(p, &codec->conn_list, list) { |
| 278 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 279 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 280 | } |
| 281 | return NULL; |
| 282 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 283 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 284 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 285 | const hda_nid_t *list) |
| 286 | { |
| 287 | struct hda_conn_list *p; |
| 288 | |
| 289 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 290 | if (!p) |
| 291 | return -ENOMEM; |
| 292 | p->len = len; |
| 293 | p->nid = nid; |
| 294 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 295 | list_add(&p->list, &codec->conn_list); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static void remove_conn_list(struct hda_codec *codec) |
| 300 | { |
| 301 | while (!list_empty(&codec->conn_list)) { |
| 302 | struct hda_conn_list *p; |
| 303 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 304 | list_del(&p->list); |
| 305 | kfree(p); |
| 306 | } |
| 307 | } |
| 308 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 309 | /* read the connection and add to the cache */ |
| 310 | 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] | 311 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 312 | hda_nid_t list[32]; |
| 313 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 314 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 315 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 316 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 317 | if (len == -ENOSPC) { |
| 318 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 319 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 320 | if (!result) |
| 321 | return -ENOMEM; |
| 322 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 323 | } |
| 324 | if (len >= 0) |
| 325 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 326 | if (result != list) |
| 327 | kfree(result); |
| 328 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 329 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 330 | |
| 331 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 332 | * snd_hda_get_conn_list - get connection list |
| 333 | * @codec: the HDA codec |
| 334 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 335 | * @listp: the pointer to store NID list |
| 336 | * |
| 337 | * Parses the connection list of the given widget and stores the pointer |
| 338 | * to the list of NIDs. |
| 339 | * |
| 340 | * Returns the number of connections, or a negative error code. |
| 341 | * |
| 342 | * Note that the returned pointer isn't protected against the list |
| 343 | * modification. If snd_hda_override_conn_list() might be called |
| 344 | * concurrently, protect with a mutex appropriately. |
| 345 | */ |
| 346 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 347 | const hda_nid_t **listp) |
| 348 | { |
| 349 | bool added = false; |
| 350 | |
| 351 | for (;;) { |
| 352 | int err; |
| 353 | const struct hda_conn_list *p; |
| 354 | |
| 355 | /* if the connection-list is already cached, read it */ |
| 356 | p = lookup_conn_list(codec, nid); |
| 357 | if (p) { |
| 358 | if (listp) |
| 359 | *listp = p->conns; |
| 360 | return p->len; |
| 361 | } |
| 362 | if (snd_BUG_ON(added)) |
| 363 | return -EINVAL; |
| 364 | |
| 365 | err = read_and_add_raw_conns(codec, nid); |
| 366 | if (err < 0) |
| 367 | return err; |
| 368 | added = true; |
| 369 | } |
| 370 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 372 | |
| 373 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 374 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * @codec: the HDA codec |
| 376 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 377 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | * @max_conns: max. number of connections to store |
| 379 | * |
| 380 | * Parses the connection list of the given widget and stores the list |
| 381 | * of NIDs. |
| 382 | * |
| 383 | * Returns the number of connections, or a negative error code. |
| 384 | */ |
| 385 | 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] | 386 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 387 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 388 | const hda_nid_t *list; |
| 389 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 390 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 391 | if (len > 0 && conn_list) { |
| 392 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 393 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 395 | return -EINVAL; |
| 396 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 397 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 398 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 399 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 400 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 401 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 402 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 403 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 404 | /* return CONNLIST_LEN parameter of the given widget */ |
| 405 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 406 | { |
| 407 | unsigned int wcaps = get_wcaps(codec, nid); |
| 408 | unsigned int parm; |
| 409 | |
| 410 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 411 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 412 | return 0; |
| 413 | |
| 414 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 415 | if (parm == -1) |
| 416 | parm = 0; |
| 417 | return parm; |
| 418 | } |
| 419 | |
| 420 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 421 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 422 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 425 | /** |
| 426 | * snd_hda_get_raw_connections - copy connection list without cache |
| 427 | * @codec: the HDA codec |
| 428 | * @nid: NID to parse |
| 429 | * @conn_list: connection list array |
| 430 | * @max_conns: max. number of connections to store |
| 431 | * |
| 432 | * Like snd_hda_get_connections(), copy the connection list but without |
| 433 | * checking through the connection-list cache. |
| 434 | * Currently called only from hda_proc.c, so not exported. |
| 435 | */ |
| 436 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 437 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
| 439 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 440 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 442 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 443 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 445 | parm = get_num_conns(codec, nid); |
| 446 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 447 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (parm & AC_CLIST_LONG) { |
| 450 | /* long form */ |
| 451 | shift = 16; |
| 452 | num_elems = 2; |
| 453 | } else { |
| 454 | /* short form */ |
| 455 | shift = 8; |
| 456 | num_elems = 4; |
| 457 | } |
| 458 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | mask = (1 << (shift-1)) - 1; |
| 460 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 461 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return 0; /* no connection */ |
| 463 | |
| 464 | if (conn_len == 1) { |
| 465 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 466 | parm = snd_hda_codec_read(codec, nid, 0, |
| 467 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 468 | if (parm == -1 && codec->bus->rirb_error) |
| 469 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 470 | if (conn_list) |
| 471 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | /* multi connection */ |
| 476 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 477 | prev_nid = 0; |
| 478 | for (i = 0; i < conn_len; i++) { |
| 479 | int range_val; |
| 480 | hda_nid_t val, n; |
| 481 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 482 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 483 | parm = snd_hda_codec_read(codec, nid, 0, |
| 484 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 485 | if (parm == -1 && codec->bus->rirb_error) |
| 486 | return -EIO; |
| 487 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 488 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 489 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 490 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 491 | codec_dbg(codec, |
| 492 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 493 | nid, i, parm); |
| 494 | return 0; |
| 495 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 496 | parm >>= shift; |
| 497 | if (range_val) { |
| 498 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 499 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 500 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 501 | "invalid dep_range_val %x:%x\n", |
| 502 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 503 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 505 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 506 | if (conn_list) { |
| 507 | if (conns >= max_conns) |
| 508 | return -ENOSPC; |
| 509 | conn_list[conns] = n; |
| 510 | } |
| 511 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 512 | } |
| 513 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 514 | if (conn_list) { |
| 515 | if (conns >= max_conns) |
| 516 | return -ENOSPC; |
| 517 | conn_list[conns] = val; |
| 518 | } |
| 519 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 521 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | return conns; |
| 524 | } |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 527 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 528 | * @codec: the HDA codec |
| 529 | * @nid: NID to parse |
| 530 | * @len: number of connection list entries |
| 531 | * @list: the list of connection entries |
| 532 | * |
| 533 | * Add or modify the given connection-list to the cache. If the corresponding |
| 534 | * cache already exists, invalidate it and append a new one. |
| 535 | * |
| 536 | * Returns zero or a negative error code. |
| 537 | */ |
| 538 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 539 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 541 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 542 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 543 | p = lookup_conn_list(codec, nid); |
| 544 | if (p) { |
| 545 | list_del(&p->list); |
| 546 | kfree(p); |
| 547 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 548 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 549 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 551 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 552 | |
| 553 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 554 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 555 | * @codec: the HDA codec |
| 556 | * @mux: NID containing the list |
| 557 | * @nid: NID to select |
| 558 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 559 | * |
| 560 | * Parses the connection list of the widget @mux and checks whether the |
| 561 | * widget @nid is present. If it is, return the connection index. |
| 562 | * Otherwise it returns -1. |
| 563 | */ |
| 564 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 565 | hda_nid_t nid, int recursive) |
| 566 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 567 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 568 | int i, nums; |
| 569 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 570 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 571 | for (i = 0; i < nums; i++) |
| 572 | if (conn[i] == nid) |
| 573 | return i; |
| 574 | if (!recursive) |
| 575 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 576 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 577 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 578 | return -1; |
| 579 | } |
| 580 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 581 | for (i = 0; i < nums; i++) { |
| 582 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 583 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 584 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 585 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 586 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 587 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 588 | return -1; |
| 589 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 590 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 592 | |
| 593 | /* return DEVLIST_LEN parameter of the given widget */ |
| 594 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 595 | { |
| 596 | unsigned int wcaps = get_wcaps(codec, nid); |
| 597 | unsigned int parm; |
| 598 | |
| 599 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 600 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 601 | return 0; |
| 602 | |
| 603 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 604 | if (parm == -1 && codec->bus->rirb_error) |
| 605 | parm = 0; |
| 606 | return parm & AC_DEV_LIST_LEN_MASK; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * snd_hda_get_devices - copy device list without cache |
| 611 | * @codec: the HDA codec |
| 612 | * @nid: NID of the pin to parse |
| 613 | * @dev_list: device list array |
| 614 | * @max_devices: max. number of devices to store |
| 615 | * |
| 616 | * Copy the device list. This info is dynamic and so not cached. |
| 617 | * Currently called only from hda_proc.c, so not exported. |
| 618 | */ |
| 619 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 620 | u8 *dev_list, int max_devices) |
| 621 | { |
| 622 | unsigned int parm; |
| 623 | int i, dev_len, devices; |
| 624 | |
| 625 | parm = get_num_devices(codec, nid); |
| 626 | if (!parm) /* not multi-stream capable */ |
| 627 | return 0; |
| 628 | |
| 629 | dev_len = parm + 1; |
| 630 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 631 | |
| 632 | devices = 0; |
| 633 | while (devices < dev_len) { |
| 634 | parm = snd_hda_codec_read(codec, nid, 0, |
| 635 | AC_VERB_GET_DEVICE_LIST, devices); |
| 636 | if (parm == -1 && codec->bus->rirb_error) |
| 637 | break; |
| 638 | |
| 639 | for (i = 0; i < 8; i++) { |
| 640 | dev_list[devices] = (u8)parm; |
| 641 | parm >>= 4; |
| 642 | devices++; |
| 643 | if (devices >= dev_len) |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | return devices; |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | * destructor |
| 652 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 653 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 655 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 656 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (bus->ops.private_free) |
| 658 | bus->ops.private_free(bus); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 659 | snd_hdac_bus_exit(&bus->core); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 663 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 665 | snd_hda_bus_free(device->device_data); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 670 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 672 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 673 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 676 | /* hdac_bus_ops translations */ |
| 677 | static int _hda_bus_command(struct hdac_bus *_bus, unsigned int cmd) |
| 678 | { |
| 679 | struct hda_bus *bus = container_of(_bus, struct hda_bus, core); |
| 680 | return bus->ops.command(bus, cmd); |
| 681 | } |
| 682 | |
| 683 | static int _hda_bus_get_response(struct hdac_bus *_bus, unsigned int addr, |
| 684 | unsigned int *res) |
| 685 | { |
| 686 | struct hda_bus *bus = container_of(_bus, struct hda_bus, core); |
| 687 | *res = bus->ops.get_response(bus, addr); |
| 688 | return bus->rirb_error ? -EIO : 0; |
| 689 | } |
| 690 | |
| 691 | static const struct hdac_bus_ops bus_ops = { |
| 692 | .command = _hda_bus_command, |
| 693 | .get_response = _hda_bus_get_response, |
| 694 | }; |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | /** |
| 697 | * snd_hda_bus_new - create a HDA bus |
| 698 | * @card: the card entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | * @busp: the pointer to store the created bus instance |
| 700 | * |
| 701 | * Returns 0 if successful, or a negative error code. |
| 702 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 703 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | ef74497 | 2015-02-17 13:56:29 +0100 | [diff] [blame] | 704 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
| 706 | struct hda_bus *bus; |
| 707 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 708 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 709 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | .dev_free = snd_hda_bus_dev_free, |
| 711 | }; |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | if (busp) |
| 714 | *busp = NULL; |
| 715 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 716 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 717 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 720 | err = snd_hdac_bus_init(&bus->core, card->dev, &bus_ops); |
| 721 | if (err < 0) { |
| 722 | kfree(bus); |
| 723 | return err; |
| 724 | } |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | bus->card = card; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 727 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 729 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 730 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | snd_hda_bus_free(bus); |
| 732 | return err; |
| 733 | } |
| 734 | if (busp) |
| 735 | *busp = bus; |
| 736 | return 0; |
| 737 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 738 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 741 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 743 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 745 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | hda_nid_t nid; |
| 747 | |
| 748 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 749 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 750 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 751 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 752 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 753 | case AC_GRP_AUDIO_FUNCTION: |
| 754 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 755 | codec->afg_function_id = function_id & 0xff; |
| 756 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 757 | break; |
| 758 | case AC_GRP_MODEM_FUNCTION: |
| 759 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 760 | codec->mfg_function_id = function_id & 0xff; |
| 761 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 762 | break; |
| 763 | default: |
| 764 | break; |
| 765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 770 | * read widget caps for each widget and store in cache |
| 771 | */ |
| 772 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 773 | { |
| 774 | int i; |
| 775 | hda_nid_t nid; |
| 776 | |
| 777 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 778 | &codec->start_nid); |
| 779 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 780 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 781 | return -ENOMEM; |
| 782 | nid = codec->start_nid; |
| 783 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 784 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 785 | AC_PAR_AUDIO_WIDGET_CAP); |
| 786 | return 0; |
| 787 | } |
| 788 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 789 | /* read all pin default configurations and save codec->init_pins */ |
| 790 | static int read_pin_defaults(struct hda_codec *codec) |
| 791 | { |
| 792 | int i; |
| 793 | hda_nid_t nid = codec->start_nid; |
| 794 | |
| 795 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 796 | struct hda_pincfg *pin; |
| 797 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 798 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 799 | if (wid_type != AC_WID_PIN) |
| 800 | continue; |
| 801 | pin = snd_array_new(&codec->init_pins); |
| 802 | if (!pin) |
| 803 | return -ENOMEM; |
| 804 | pin->nid = nid; |
| 805 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 806 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 807 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 808 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 809 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 810 | } |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | /* look up the given pin config list and return the item matching with NID */ |
| 815 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 816 | struct snd_array *array, |
| 817 | hda_nid_t nid) |
| 818 | { |
| 819 | int i; |
| 820 | for (i = 0; i < array->used; i++) { |
| 821 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 822 | if (pin->nid == nid) |
| 823 | return pin; |
| 824 | } |
| 825 | return NULL; |
| 826 | } |
| 827 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 828 | /* set the current pin config value for the given NID. |
| 829 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 830 | */ |
| 831 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 832 | hda_nid_t nid, unsigned int cfg) |
| 833 | { |
| 834 | struct hda_pincfg *pin; |
| 835 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 836 | /* the check below may be invalid when pins are added by a fixup |
| 837 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 838 | * for now |
| 839 | */ |
| 840 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 841 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 842 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 843 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 844 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 845 | pin = look_up_pincfg(codec, list, nid); |
| 846 | if (!pin) { |
| 847 | pin = snd_array_new(list); |
| 848 | if (!pin) |
| 849 | return -ENOMEM; |
| 850 | pin->nid = nid; |
| 851 | } |
| 852 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 856 | /** |
| 857 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 858 | * @codec: the HDA codec |
| 859 | * @nid: NID to set the pin config |
| 860 | * @cfg: the pin default config value |
| 861 | * |
| 862 | * Override a pin default configuration value in the cache. |
| 863 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 864 | * priority than the real hardware value. |
| 865 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 866 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 867 | hda_nid_t nid, unsigned int cfg) |
| 868 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 869 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 870 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 871 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 872 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 873 | /** |
| 874 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 875 | * @codec: the HDA codec |
| 876 | * @nid: NID to get the pin config |
| 877 | * |
| 878 | * Get the current pin config value of the given pin NID. |
| 879 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 880 | * returns the cached value. |
| 881 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 882 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 883 | { |
| 884 | struct hda_pincfg *pin; |
| 885 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 886 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 887 | { |
| 888 | unsigned int cfg = 0; |
| 889 | mutex_lock(&codec->user_mutex); |
| 890 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 891 | if (pin) |
| 892 | cfg = pin->cfg; |
| 893 | mutex_unlock(&codec->user_mutex); |
| 894 | if (cfg) |
| 895 | return cfg; |
| 896 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 897 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 898 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 899 | if (pin) |
| 900 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 901 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 902 | if (pin) |
| 903 | return pin->cfg; |
| 904 | return 0; |
| 905 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 906 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 907 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 908 | /** |
| 909 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 910 | * @codec: the HDA codec |
| 911 | * @nid: pin NID |
| 912 | * @val: assigned pinctl value |
| 913 | * |
| 914 | * This function stores the given value to a pinctl target value in the |
| 915 | * pincfg table. This isn't always as same as the actually written value |
| 916 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 917 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 918 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 919 | unsigned int val) |
| 920 | { |
| 921 | struct hda_pincfg *pin; |
| 922 | |
| 923 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 924 | if (!pin) |
| 925 | return -EINVAL; |
| 926 | pin->target = val; |
| 927 | return 0; |
| 928 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 929 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 930 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 931 | /** |
| 932 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 933 | * @codec: the HDA codec |
| 934 | * @nid: pin NID |
| 935 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 936 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 937 | { |
| 938 | struct hda_pincfg *pin; |
| 939 | |
| 940 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 941 | if (!pin) |
| 942 | return 0; |
| 943 | return pin->target; |
| 944 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 945 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 946 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 947 | /** |
| 948 | * snd_hda_shutup_pins - Shut up all pins |
| 949 | * @codec: the HDA codec |
| 950 | * |
| 951 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 952 | * The controls aren't cached so that they can be resumed properly. |
| 953 | */ |
| 954 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 955 | { |
| 956 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 957 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 958 | * the default pin setup at the next load of the driver |
| 959 | */ |
| 960 | if (codec->bus->shutdown) |
| 961 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 962 | for (i = 0; i < codec->init_pins.used; i++) { |
| 963 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 964 | /* use read here for syncing after issuing each verb */ |
| 965 | snd_hda_codec_read(codec, pin->nid, 0, |
| 966 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 967 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 968 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 969 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 970 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 971 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 972 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 973 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 974 | static void restore_shutup_pins(struct hda_codec *codec) |
| 975 | { |
| 976 | int i; |
| 977 | if (!codec->pins_shutup) |
| 978 | return; |
| 979 | if (codec->bus->shutdown) |
| 980 | return; |
| 981 | for (i = 0; i < codec->init_pins.used; i++) { |
| 982 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 983 | snd_hda_codec_write(codec, pin->nid, 0, |
| 984 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 985 | pin->ctrl); |
| 986 | } |
| 987 | codec->pins_shutup = 0; |
| 988 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 989 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 990 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 991 | static void hda_jackpoll_work(struct work_struct *work) |
| 992 | { |
| 993 | struct hda_codec *codec = |
| 994 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 995 | |
| 996 | snd_hda_jack_set_dirty_all(codec); |
| 997 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 998 | |
| 999 | if (!codec->jackpoll_interval) |
| 1000 | return; |
| 1001 | |
Takashi Iwai | 2f35c63 | 2015-02-27 22:43:26 +0100 | [diff] [blame] | 1002 | schedule_delayed_work(&codec->jackpoll_work, |
| 1003 | codec->jackpoll_interval); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1006 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1007 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1008 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1009 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1010 | /* release all pincfg lists */ |
| 1011 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1012 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1013 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1014 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1015 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1016 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1017 | snd_array_free(&codec->init_pins); |
| 1018 | } |
| 1019 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1020 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1021 | * audio-converter setup caches |
| 1022 | */ |
| 1023 | struct hda_cvt_setup { |
| 1024 | hda_nid_t nid; |
| 1025 | u8 stream_tag; |
| 1026 | u8 channel_id; |
| 1027 | u16 format_id; |
| 1028 | unsigned char active; /* cvt is currently used */ |
| 1029 | unsigned char dirty; /* setups should be cleared */ |
| 1030 | }; |
| 1031 | |
| 1032 | /* get or create a cache entry for the given audio converter NID */ |
| 1033 | static struct hda_cvt_setup * |
| 1034 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1035 | { |
| 1036 | struct hda_cvt_setup *p; |
| 1037 | int i; |
| 1038 | |
| 1039 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1040 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1041 | if (p->nid == nid) |
| 1042 | return p; |
| 1043 | } |
| 1044 | p = snd_array_new(&codec->cvt_setups); |
| 1045 | if (p) |
| 1046 | p->nid = nid; |
| 1047 | return p; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1051 | * PCM device |
| 1052 | */ |
| 1053 | static void release_pcm(struct kref *kref) |
| 1054 | { |
| 1055 | struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref); |
| 1056 | |
| 1057 | if (pcm->pcm) |
| 1058 | snd_device_free(pcm->codec->card, pcm->pcm); |
| 1059 | clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits); |
| 1060 | kfree(pcm->name); |
| 1061 | kfree(pcm); |
| 1062 | } |
| 1063 | |
| 1064 | void snd_hda_codec_pcm_put(struct hda_pcm *pcm) |
| 1065 | { |
| 1066 | kref_put(&pcm->kref, release_pcm); |
| 1067 | } |
| 1068 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put); |
| 1069 | |
| 1070 | struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec, |
| 1071 | const char *fmt, ...) |
| 1072 | { |
| 1073 | struct hda_pcm *pcm; |
| 1074 | va_list args; |
| 1075 | |
| 1076 | va_start(args, fmt); |
| 1077 | pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); |
| 1078 | if (!pcm) |
| 1079 | return NULL; |
| 1080 | |
| 1081 | pcm->codec = codec; |
| 1082 | kref_init(&pcm->kref); |
| 1083 | pcm->name = kvasprintf(GFP_KERNEL, fmt, args); |
| 1084 | if (!pcm->name) { |
| 1085 | kfree(pcm); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | list_add_tail(&pcm->list, &codec->pcm_list_head); |
| 1090 | return pcm; |
| 1091 | } |
| 1092 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new); |
| 1093 | |
Takashi Iwai | 9a6246f | 2015-02-27 18:17:28 +0100 | [diff] [blame] | 1094 | /* |
| 1095 | * codec destructor |
| 1096 | */ |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1097 | static void codec_release_pcms(struct hda_codec *codec) |
| 1098 | { |
| 1099 | struct hda_pcm *pcm, *n; |
| 1100 | |
| 1101 | list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) { |
| 1102 | list_del_init(&pcm->list); |
Takashi Iwai | 9a6246f | 2015-02-27 18:17:28 +0100 | [diff] [blame] | 1103 | if (pcm->pcm) |
| 1104 | snd_device_disconnect(codec->card, pcm->pcm); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1105 | snd_hda_codec_pcm_put(pcm); |
| 1106 | } |
| 1107 | } |
| 1108 | |
Takashi Iwai | 9a6246f | 2015-02-27 18:17:28 +0100 | [diff] [blame] | 1109 | void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec) |
| 1110 | { |
| 1111 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 9a6246f | 2015-02-27 18:17:28 +0100 | [diff] [blame] | 1112 | if (!codec->in_freeing) |
| 1113 | snd_hda_ctls_clear(codec); |
| 1114 | codec_release_pcms(codec); |
| 1115 | snd_hda_detach_beep_device(codec); |
| 1116 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
| 1117 | snd_hda_jack_tbl_clear(codec); |
| 1118 | codec->proc_widget_hook = NULL; |
| 1119 | codec->spec = NULL; |
| 1120 | |
| 1121 | free_hda_cache(&codec->amp_cache); |
| 1122 | free_hda_cache(&codec->cmd_cache); |
| 1123 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 1124 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
| 1125 | |
| 1126 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 1127 | snd_array_free(&codec->driver_pins); |
| 1128 | snd_array_free(&codec->cvt_setups); |
| 1129 | snd_array_free(&codec->spdif_out); |
| 1130 | snd_array_free(&codec->verbs); |
| 1131 | codec->preset = NULL; |
| 1132 | codec->slave_dig_outs = NULL; |
| 1133 | codec->spdif_status_reset = 0; |
| 1134 | snd_array_free(&codec->mixers); |
| 1135 | snd_array_free(&codec->nids); |
| 1136 | remove_conn_list(codec); |
| 1137 | } |
| 1138 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1139 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1140 | hda_nid_t fg, unsigned int power_state); |
| 1141 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1142 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1143 | unsigned int power_state); |
| 1144 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1145 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1146 | { |
| 1147 | struct hda_codec *codec = device->device_data; |
| 1148 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1149 | snd_hda_register_beep_device(codec); |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 1150 | if (device_is_registered(hda_codec_dev(codec))) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1151 | pm_runtime_enable(hda_codec_dev(codec)); |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame] | 1152 | /* it was powered up in snd_hda_codec_new(), now all done */ |
| 1153 | snd_hda_power_down(codec); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1154 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1158 | { |
| 1159 | struct hda_codec *codec = device->device_data; |
| 1160 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1161 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1165 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1166 | { |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1167 | struct hda_codec *codec = device->device_data; |
| 1168 | |
| 1169 | codec->in_freeing = 1; |
| 1170 | if (device_is_registered(hda_codec_dev(codec))) |
| 1171 | device_del(hda_codec_dev(codec)); |
| 1172 | put_device(hda_codec_dev(codec)); |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1176 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1177 | { |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1178 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 1179 | |
| 1180 | free_init_pincfgs(codec); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1181 | snd_hdac_bus_remove_device(&codec->bus->core, &codec->core); |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1182 | snd_hda_sysfs_clear(codec); |
| 1183 | free_hda_cache(&codec->amp_cache); |
| 1184 | free_hda_cache(&codec->cmd_cache); |
| 1185 | kfree(codec->vendor_name); |
| 1186 | kfree(codec->chip_name); |
| 1187 | kfree(codec->modelname); |
| 1188 | kfree(codec->wcaps); |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1189 | kfree(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | /** |
| 1193 | * snd_hda_codec_new - create a HDA codec |
| 1194 | * @bus: the bus to assign |
| 1195 | * @codec_addr: the codec address |
| 1196 | * @codecp: the pointer to store the generated codec |
| 1197 | * |
| 1198 | * Returns 0 if successful, or a negative error code. |
| 1199 | */ |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1200 | int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card, |
| 1201 | unsigned int codec_addr, struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | { |
| 1203 | struct hda_codec *codec; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1204 | struct device *dev; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1205 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1206 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1208 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1209 | .dev_register = snd_hda_codec_dev_register, |
| 1210 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1211 | .dev_free = snd_hda_codec_dev_free, |
| 1212 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1214 | if (snd_BUG_ON(!bus)) |
| 1215 | return -EINVAL; |
| 1216 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1217 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1219 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1220 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1223 | codec->core.bus = &bus->core; |
| 1224 | codec->core.addr = codec_addr; |
| 1225 | codec->core.type = HDA_DEV_LEGACY; |
| 1226 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1227 | dev = hda_codec_dev(codec); |
| 1228 | device_initialize(dev); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1229 | dev->parent = bus->core.dev; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1230 | dev->bus = &snd_hda_bus_type; |
| 1231 | dev->release = snd_hda_codec_dev_release; |
| 1232 | dev->groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1233 | dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1234 | dev_set_drvdata(dev, codec); /* for sysfs */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 1235 | device_enable_async_suspend(dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | codec->bus = bus; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1238 | codec->card = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1240 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1241 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1242 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1243 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1244 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1245 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1246 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1247 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1248 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1249 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1250 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1251 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1252 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1253 | INIT_LIST_HEAD(&codec->conn_list); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1254 | INIT_LIST_HEAD(&codec->pcm_list_head); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1255 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1256 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1257 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1258 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1260 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1261 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame] | 1262 | * it's powered down later in snd_hda_codec_dev_register(). |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1263 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1264 | set_bit(codec->core.addr, &bus->core.codec_powered); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1265 | pm_runtime_set_active(hda_codec_dev(codec)); |
| 1266 | pm_runtime_get_noresume(hda_codec_dev(codec)); |
| 1267 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1268 | #endif |
| 1269 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1270 | snd_hda_sysfs_init(codec); |
| 1271 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1272 | if (codec->bus->modelname) { |
| 1273 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1274 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1275 | err = -ENODEV; |
| 1276 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1280 | err = snd_hdac_bus_add_device(&bus->core, &codec->core); |
| 1281 | if (err < 0) |
| 1282 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1284 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1285 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1286 | if (codec->vendor_id == -1) |
| 1287 | /* read again, hopefully the access method was corrected |
| 1288 | * in the last read... |
| 1289 | */ |
| 1290 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1291 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1292 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1293 | AC_PAR_SUBSYSTEM_ID); |
| 1294 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1295 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1297 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1298 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1299 | codec_err(codec, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1300 | err = -ENODEV; |
| 1301 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1304 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1305 | err = read_widget_caps(codec, fg); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1306 | if (err < 0) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1307 | goto error; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1308 | err = read_pin_defaults(codec); |
| 1309 | if (err < 0) |
| 1310 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1311 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1312 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1313 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1314 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1315 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1318 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1319 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1320 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1321 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1322 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1323 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1324 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1325 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1326 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1327 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1328 | snd_hda_codec_proc_new(codec); |
| 1329 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1330 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1331 | |
| 1332 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1333 | codec->subsystem_id, codec->revision_id); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1334 | snd_component_add(card, component); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1335 | |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1336 | err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops); |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1337 | if (err < 0) |
| 1338 | goto error; |
| 1339 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1340 | if (codecp) |
| 1341 | *codecp = codec; |
| 1342 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1343 | |
| 1344 | error: |
Takashi Iwai | d56db74 | 2015-02-27 23:25:35 +0100 | [diff] [blame] | 1345 | put_device(hda_codec_dev(codec)); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1346 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1347 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1348 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1349 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1350 | /** |
| 1351 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1352 | * @codec: the HDA codec |
| 1353 | * |
| 1354 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1355 | * the given codec. |
| 1356 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1357 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1358 | { |
| 1359 | hda_nid_t fg; |
| 1360 | int err; |
| 1361 | |
| 1362 | /* Assume the function group node does not change, |
| 1363 | * only the widget nodes may change. |
| 1364 | */ |
| 1365 | kfree(codec->wcaps); |
| 1366 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1367 | err = read_widget_caps(codec, fg); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1368 | if (err < 0) |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1369 | return err; |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1370 | |
| 1371 | snd_array_free(&codec->init_pins); |
| 1372 | err = read_pin_defaults(codec); |
| 1373 | |
| 1374 | return err; |
| 1375 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1376 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1377 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1378 | /* update the stream-id if changed */ |
| 1379 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1380 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1381 | u32 stream_tag, int channel_id) |
| 1382 | { |
| 1383 | unsigned int oldval, newval; |
| 1384 | |
| 1385 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1386 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1387 | newval = (stream_tag << 4) | channel_id; |
| 1388 | if (oldval != newval) |
| 1389 | snd_hda_codec_write(codec, nid, 0, |
| 1390 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1391 | newval); |
| 1392 | p->stream_tag = stream_tag; |
| 1393 | p->channel_id = channel_id; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /* update the format-id if changed */ |
| 1398 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1399 | hda_nid_t nid, int format) |
| 1400 | { |
| 1401 | unsigned int oldval; |
| 1402 | |
| 1403 | if (p->format_id != format) { |
| 1404 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1405 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1406 | if (oldval != format) { |
| 1407 | msleep(1); |
| 1408 | snd_hda_codec_write(codec, nid, 0, |
| 1409 | AC_VERB_SET_STREAM_FORMAT, |
| 1410 | format); |
| 1411 | } |
| 1412 | p->format_id = format; |
| 1413 | } |
| 1414 | } |
| 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | /** |
| 1417 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1418 | * @codec: the CODEC to set up |
| 1419 | * @nid: the NID to set up |
| 1420 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1421 | * @channel_id: channel id to pass, zero based. |
| 1422 | * @format: stream format. |
| 1423 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1424 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1425 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | int channel_id, int format) |
| 1427 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1428 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1429 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1430 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1431 | int i; |
| 1432 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1433 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1434 | return; |
| 1435 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1436 | codec_dbg(codec, |
| 1437 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1438 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1439 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1440 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1441 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1442 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 1443 | if (codec->patch_ops.stream_pm) |
| 1444 | codec->patch_ops.stream_pm(codec, nid, true); |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1445 | if (codec->pcm_format_first) |
| 1446 | update_pcm_format(codec, p, nid, format); |
| 1447 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1448 | if (!codec->pcm_format_first) |
| 1449 | update_pcm_format(codec, p, nid, format); |
| 1450 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1451 | p->active = 1; |
| 1452 | p->dirty = 0; |
| 1453 | |
| 1454 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1455 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1456 | list_for_each_codec(c, codec->bus) { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1457 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1458 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1459 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1460 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1461 | p->dirty = 1; |
| 1462 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1465 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1467 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1468 | struct hda_cvt_setup *q); |
| 1469 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1470 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1471 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1472 | * @codec: the CODEC to clean up |
| 1473 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1474 | * @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] | 1475 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1476 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1477 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1478 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1479 | struct hda_cvt_setup *p; |
| 1480 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1481 | if (!nid) |
| 1482 | return; |
| 1483 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1484 | if (codec->no_sticky_stream) |
| 1485 | do_now = 1; |
| 1486 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1487 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1488 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1489 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1490 | /* here we just clear the active flag when do_now isn't set; |
| 1491 | * actual clean-ups will be done later in |
| 1492 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1493 | */ |
| 1494 | if (do_now) |
| 1495 | really_cleanup_stream(codec, p); |
| 1496 | else |
| 1497 | p->active = 0; |
| 1498 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1499 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1500 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1501 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1502 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1503 | struct hda_cvt_setup *q) |
| 1504 | { |
| 1505 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1506 | if (q->stream_tag || q->channel_id) |
| 1507 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1508 | if (q->format_id) |
| 1509 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1510 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1511 | memset(q, 0, sizeof(*q)); |
| 1512 | q->nid = nid; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 1513 | if (codec->patch_ops.stream_pm) |
| 1514 | codec->patch_ops.stream_pm(codec, nid, false); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* clean up the all conflicting obsolete streams */ |
| 1518 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1519 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1520 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1521 | int i; |
| 1522 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 1523 | list_for_each_codec(c, codec->bus) { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1524 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1525 | struct hda_cvt_setup *p; |
| 1526 | p = snd_array_elem(&c->cvt_setups, i); |
| 1527 | if (p->dirty) |
| 1528 | really_cleanup_stream(c, p); |
| 1529 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1533 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1534 | /* clean up all streams; called from suspend */ |
| 1535 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1536 | { |
| 1537 | int i; |
| 1538 | |
| 1539 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1540 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1541 | if (p->stream_tag) |
| 1542 | really_cleanup_stream(codec, p); |
| 1543 | } |
| 1544 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1545 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | /* |
| 1548 | * amp access functions |
| 1549 | */ |
| 1550 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1551 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1552 | #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] | 1553 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1554 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1555 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1557 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1560 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1561 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1563 | memset(cache, 0, sizeof(*cache)); |
| 1564 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1565 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1568 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1569 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1570 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1574 | 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] | 1575 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1576 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1577 | u16 cur = cache->hash[idx]; |
| 1578 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
| 1580 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1581 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | if (info->key == key) |
| 1583 | return info; |
| 1584 | cur = info->next; |
| 1585 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1586 | return NULL; |
| 1587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1589 | /* query the hash. allocate an entry if not found. */ |
| 1590 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1591 | u32 key) |
| 1592 | { |
| 1593 | struct hda_cache_head *info = get_hash(cache, key); |
| 1594 | if (!info) { |
| 1595 | u16 idx, cur; |
| 1596 | /* add a new hash entry */ |
| 1597 | info = snd_array_new(&cache->buf); |
| 1598 | if (!info) |
| 1599 | return NULL; |
| 1600 | cur = snd_array_index(&cache->buf, info); |
| 1601 | info->key = key; |
| 1602 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1603 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1604 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1605 | info->next = cache->hash[idx]; |
| 1606 | cache->hash[idx] = cur; |
| 1607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | return info; |
| 1609 | } |
| 1610 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1611 | /* query and allocate an amp hash entry */ |
| 1612 | static inline struct hda_amp_info * |
| 1613 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1614 | { |
| 1615 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1616 | } |
| 1617 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1618 | /* overwrite the value with the key in the caps hash */ |
| 1619 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1620 | { |
| 1621 | struct hda_amp_info *info; |
| 1622 | |
| 1623 | mutex_lock(&codec->hash_mutex); |
| 1624 | info = get_alloc_amp_hash(codec, key); |
| 1625 | if (!info) { |
| 1626 | mutex_unlock(&codec->hash_mutex); |
| 1627 | return -EINVAL; |
| 1628 | } |
| 1629 | info->amp_caps = val; |
| 1630 | info->head.val |= INFO_AMP_CAPS; |
| 1631 | mutex_unlock(&codec->hash_mutex); |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | /* query the value from the caps hash; if not found, fetch the current |
| 1636 | * value from the given function and store in the hash |
| 1637 | */ |
| 1638 | static unsigned int |
| 1639 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1640 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1641 | { |
| 1642 | struct hda_amp_info *info; |
| 1643 | unsigned int val; |
| 1644 | |
| 1645 | mutex_lock(&codec->hash_mutex); |
| 1646 | info = get_alloc_amp_hash(codec, key); |
| 1647 | if (!info) { |
| 1648 | mutex_unlock(&codec->hash_mutex); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1652 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1653 | val = func(codec, nid, dir); |
| 1654 | write_caps_hash(codec, key, val); |
| 1655 | } else { |
| 1656 | val = info->amp_caps; |
| 1657 | mutex_unlock(&codec->hash_mutex); |
| 1658 | } |
| 1659 | return val; |
| 1660 | } |
| 1661 | |
| 1662 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1663 | int direction) |
| 1664 | { |
| 1665 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1666 | nid = codec->afg; |
| 1667 | return snd_hda_param_read(codec, nid, |
| 1668 | direction == HDA_OUTPUT ? |
| 1669 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1670 | } |
| 1671 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1672 | /** |
| 1673 | * query_amp_caps - query AMP capabilities |
| 1674 | * @codec: the HD-auio codec |
| 1675 | * @nid: the NID to query |
| 1676 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1677 | * |
| 1678 | * Query AMP capabilities for the given widget and direction. |
| 1679 | * Returns the obtained capability bits. |
| 1680 | * |
| 1681 | * When cap bits have been already read, this doesn't read again but |
| 1682 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1684 | 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] | 1685 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1686 | return query_caps_hash(codec, nid, direction, |
| 1687 | HDA_HASH_KEY(nid, direction, 0), |
| 1688 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1690 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1692 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1693 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1694 | * @codec: the HD-audio codec |
| 1695 | * @nid: the NID to query |
| 1696 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1697 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1698 | * |
| 1699 | * Check whether the widget has the given amp capability for the direction. |
| 1700 | */ |
| 1701 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1702 | int dir, unsigned int bits) |
| 1703 | { |
| 1704 | if (!nid) |
| 1705 | return false; |
| 1706 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 1707 | if (query_amp_caps(codec, nid, dir) & bits) |
| 1708 | return true; |
| 1709 | return false; |
| 1710 | } |
| 1711 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 1712 | |
| 1713 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1714 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1715 | * @codec: the CODEC to clean up |
| 1716 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1717 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1718 | * @caps: the capability bits to set |
| 1719 | * |
| 1720 | * Override the cached AMP caps bits value by the given one. |
| 1721 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1722 | * e.g. limit to 0dB, etc. |
| 1723 | * |
| 1724 | * Returns zero if successful or a negative error code. |
| 1725 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1726 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1727 | unsigned int caps) |
| 1728 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1729 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1730 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1731 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1732 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1733 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1734 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1735 | { |
| 1736 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1737 | } |
| 1738 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1739 | /** |
| 1740 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1741 | * @codec: the HD-auio codec |
| 1742 | * @nid: the NID to query |
| 1743 | * |
| 1744 | * Query PIN capabilities for the given widget. |
| 1745 | * Returns the obtained capability bits. |
| 1746 | * |
| 1747 | * When cap bits have been already read, this doesn't read again but |
| 1748 | * returns the cached value. |
| 1749 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1750 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1751 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1752 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1753 | read_pin_cap); |
| 1754 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1755 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1756 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1757 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1758 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1759 | * @codec: the CODEC |
| 1760 | * @nid: the NID to override |
| 1761 | * @caps: the capability bits to set |
| 1762 | * |
| 1763 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1764 | * |
| 1765 | * Returns zero if successful or a negative error code. |
| 1766 | */ |
| 1767 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1768 | unsigned int caps) |
| 1769 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1770 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1771 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1772 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1773 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1774 | /* read or sync the hash value with the current value; |
| 1775 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1777 | static struct hda_amp_info * |
| 1778 | 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] | 1779 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1781 | struct hda_amp_info *info; |
| 1782 | unsigned int parm, val = 0; |
| 1783 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1785 | retry: |
| 1786 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1787 | if (!info) |
| 1788 | return NULL; |
| 1789 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1790 | if (!val_read) { |
| 1791 | mutex_unlock(&codec->hash_mutex); |
| 1792 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1793 | parm |= direction == HDA_OUTPUT ? |
| 1794 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1795 | parm |= index; |
| 1796 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1797 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1798 | val &= 0xff; |
| 1799 | val_read = true; |
| 1800 | mutex_lock(&codec->hash_mutex); |
| 1801 | goto retry; |
| 1802 | } |
| 1803 | info->vol[ch] = val; |
| 1804 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1805 | } else if (init_only) |
| 1806 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1807 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1811 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1813 | 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] | 1814 | hda_nid_t nid, int ch, int direction, int index, |
| 1815 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | { |
| 1817 | u32 parm; |
| 1818 | |
| 1819 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1820 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1821 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1822 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1823 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1824 | ; /* set the zero value as a fake mute */ |
| 1825 | else |
| 1826 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1828 | } |
| 1829 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1830 | /** |
| 1831 | * snd_hda_codec_amp_read - Read AMP value |
| 1832 | * @codec: HD-audio codec |
| 1833 | * @nid: NID to read the AMP value |
| 1834 | * @ch: channel (left=0 or right=1) |
| 1835 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1836 | * @index: the index value (only for input direction) |
| 1837 | * |
| 1838 | * 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] | 1839 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1840 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1841 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1843 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1844 | unsigned int val = 0; |
| 1845 | |
| 1846 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1847 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1848 | if (info) |
| 1849 | val = info->vol[ch]; |
| 1850 | mutex_unlock(&codec->hash_mutex); |
| 1851 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1853 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1855 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1856 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1857 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1858 | { |
| 1859 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1860 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1861 | |
| 1862 | if (snd_BUG_ON(mask & ~0xff)) |
| 1863 | mask &= 0xff; |
| 1864 | val &= mask; |
| 1865 | |
| 1866 | mutex_lock(&codec->hash_mutex); |
| 1867 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1868 | if (!info) { |
| 1869 | mutex_unlock(&codec->hash_mutex); |
| 1870 | return 0; |
| 1871 | } |
| 1872 | val |= info->vol[ch] & ~mask; |
| 1873 | if (info->vol[ch] == val) { |
| 1874 | mutex_unlock(&codec->hash_mutex); |
| 1875 | return 0; |
| 1876 | } |
| 1877 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1878 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1879 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1880 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1881 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1882 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1883 | return 1; |
| 1884 | } |
| 1885 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1886 | /** |
| 1887 | * snd_hda_codec_amp_update - update the AMP value |
| 1888 | * @codec: HD-audio codec |
| 1889 | * @nid: NID to read the AMP value |
| 1890 | * @ch: channel (left=0 or right=1) |
| 1891 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1892 | * @idx: the index value (only for input direction) |
| 1893 | * @mask: bit mask to set |
| 1894 | * @val: the bits value to set |
| 1895 | * |
| 1896 | * Update the AMP value with a bit mask. |
| 1897 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1898 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1899 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1900 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1902 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 1903 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1905 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1907 | /** |
| 1908 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1909 | * @codec: HD-audio codec |
| 1910 | * @nid: NID to read the AMP value |
| 1911 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1912 | * @idx: the index value (only for input direction) |
| 1913 | * @mask: bit mask to set |
| 1914 | * @val: the bits value to set |
| 1915 | * |
| 1916 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1917 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1918 | */ |
| 1919 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1920 | int direction, int idx, int mask, int val) |
| 1921 | { |
| 1922 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1923 | |
| 1924 | if (snd_BUG_ON(mask & ~0xff)) |
| 1925 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1926 | for (ch = 0; ch < 2; ch++) |
| 1927 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1928 | idx, mask, val); |
| 1929 | return ret; |
| 1930 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1931 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1932 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1933 | /** |
| 1934 | * snd_hda_codec_amp_init - initialize the AMP value |
| 1935 | * @codec: the HDA codec |
| 1936 | * @nid: NID to read the AMP value |
| 1937 | * @ch: channel (left=0 or right=1) |
| 1938 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1939 | * @idx: the index value (only for input direction) |
| 1940 | * @mask: bit mask to set |
| 1941 | * @val: the bits value to set |
| 1942 | * |
| 1943 | * Works like snd_hda_codec_amp_update() but it writes the value only at |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1944 | * the first access. If the amp was already initialized / updated beforehand, |
| 1945 | * this does nothing. |
| 1946 | */ |
| 1947 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1948 | int dir, int idx, int mask, int val) |
| 1949 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1950 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 1951 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1952 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1953 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1954 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1955 | /** |
| 1956 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 1957 | * @codec: the HDA codec |
| 1958 | * @nid: NID to read the AMP value |
| 1959 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1960 | * @idx: the index value (only for input direction) |
| 1961 | * @mask: bit mask to set |
| 1962 | * @val: the bits value to set |
| 1963 | * |
| 1964 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 1965 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1966 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1967 | int dir, int idx, int mask, int val) |
| 1968 | { |
| 1969 | int ch, ret = 0; |
| 1970 | |
| 1971 | if (snd_BUG_ON(mask & ~0xff)) |
| 1972 | mask &= 0xff; |
| 1973 | for (ch = 0; ch < 2; ch++) |
| 1974 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 1975 | idx, mask, val); |
| 1976 | return ret; |
| 1977 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1978 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1979 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1980 | /** |
| 1981 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1982 | * @codec: HD-audio codec |
| 1983 | * |
| 1984 | * Resume the all amp commands from the cache. |
| 1985 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1986 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1987 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1988 | int i; |
| 1989 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1990 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 1991 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1992 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 1993 | struct hda_amp_info *buffer; |
| 1994 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1995 | hda_nid_t nid; |
| 1996 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1997 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1998 | |
| 1999 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2000 | if (!buffer->head.dirty) |
| 2001 | continue; |
| 2002 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2003 | info = *buffer; |
| 2004 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2005 | if (!key) |
| 2006 | continue; |
| 2007 | nid = key & 0xff; |
| 2008 | idx = (key >> 16) & 0xff; |
| 2009 | dir = (key >> 24) & 0xff; |
| 2010 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2011 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2012 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2013 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2014 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2015 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2016 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2017 | } |
| 2018 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2019 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2020 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2021 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2023 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2024 | unsigned int ofs) |
| 2025 | { |
| 2026 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2027 | /* get num steps */ |
| 2028 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2029 | if (ofs < caps) |
| 2030 | caps -= ofs; |
| 2031 | return caps; |
| 2032 | } |
| 2033 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2034 | /** |
| 2035 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2036 | * @kcontrol: referred ctl element |
| 2037 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2038 | * |
| 2039 | * The control element is supposed to have the private_value field |
| 2040 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2041 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2042 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2043 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | { |
| 2045 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2046 | u16 nid = get_amp_nid(kcontrol); |
| 2047 | u8 chs = get_amp_channels(kcontrol); |
| 2048 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2049 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2051 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2052 | uinfo->count = chs == 3 ? 2 : 1; |
| 2053 | uinfo->value.integer.min = 0; |
| 2054 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2055 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2056 | codec_warn(codec, |
| 2057 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2058 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | return -EINVAL; |
| 2060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | return 0; |
| 2062 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2063 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2065 | |
| 2066 | static inline unsigned int |
| 2067 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2068 | int ch, int dir, int idx, unsigned int ofs) |
| 2069 | { |
| 2070 | unsigned int val; |
| 2071 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2072 | val &= HDA_AMP_VOLMASK; |
| 2073 | if (val >= ofs) |
| 2074 | val -= ofs; |
| 2075 | else |
| 2076 | val = 0; |
| 2077 | return val; |
| 2078 | } |
| 2079 | |
| 2080 | static inline int |
| 2081 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2082 | int ch, int dir, int idx, unsigned int ofs, |
| 2083 | unsigned int val) |
| 2084 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2085 | unsigned int maxval; |
| 2086 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2087 | if (val > 0) |
| 2088 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2089 | /* ofs = 0: raw max value */ |
| 2090 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2091 | if (val > maxval) |
| 2092 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2093 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2094 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2097 | /** |
| 2098 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2099 | * @kcontrol: ctl element |
| 2100 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2101 | * |
| 2102 | * The control element is supposed to have the private_value field |
| 2103 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2104 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2105 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2106 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | { |
| 2108 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2109 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2110 | int chs = get_amp_channels(kcontrol); |
| 2111 | int dir = get_amp_direction(kcontrol); |
| 2112 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2113 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | long *valp = ucontrol->value.integer.value; |
| 2115 | |
| 2116 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2117 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2119 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | return 0; |
| 2121 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2122 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2124 | /** |
| 2125 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2126 | * @kcontrol: ctl element |
| 2127 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2128 | * |
| 2129 | * The control element is supposed to have the private_value field |
| 2130 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2131 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2132 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2133 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | { |
| 2135 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2136 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2137 | int chs = get_amp_channels(kcontrol); |
| 2138 | int dir = get_amp_direction(kcontrol); |
| 2139 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2140 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | long *valp = ucontrol->value.integer.value; |
| 2142 | int change = 0; |
| 2143 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2144 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2145 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2146 | valp++; |
| 2147 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2148 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2149 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | return change; |
| 2151 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2152 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2154 | /** |
| 2155 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2156 | * @kcontrol: ctl element |
| 2157 | * @op_flag: operation flag |
| 2158 | * @size: byte size of input TLV |
| 2159 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2160 | * |
| 2161 | * The control element is supposed to have the private_value field |
| 2162 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2163 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2164 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2165 | unsigned int size, unsigned int __user *_tlv) |
| 2166 | { |
| 2167 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2168 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2169 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2170 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2171 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2172 | u32 caps, val1, val2; |
| 2173 | |
| 2174 | if (size < 4 * sizeof(unsigned int)) |
| 2175 | return -ENOMEM; |
| 2176 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2177 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2178 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2179 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2180 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2181 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2182 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2183 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2184 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2185 | return -EFAULT; |
| 2186 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2187 | return -EFAULT; |
| 2188 | if (put_user(val1, _tlv + 2)) |
| 2189 | return -EFAULT; |
| 2190 | if (put_user(val2, _tlv + 3)) |
| 2191 | return -EFAULT; |
| 2192 | return 0; |
| 2193 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2194 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2195 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2196 | /** |
| 2197 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2198 | * @codec: HD-audio codec |
| 2199 | * @nid: NID of a reference widget |
| 2200 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2201 | * @tlv: TLV data to be stored, at least 4 elements |
| 2202 | * |
| 2203 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2204 | * obtained from the reference NID. |
| 2205 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2206 | */ |
| 2207 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2208 | unsigned int *tlv) |
| 2209 | { |
| 2210 | u32 caps; |
| 2211 | int nums, step; |
| 2212 | |
| 2213 | caps = query_amp_caps(codec, nid, dir); |
| 2214 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2215 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2216 | step = (step + 1) * 25; |
| 2217 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2218 | tlv[1] = 2 * sizeof(unsigned int); |
| 2219 | tlv[2] = -nums * step; |
| 2220 | tlv[3] = step; |
| 2221 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2222 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2223 | |
| 2224 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2225 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2226 | 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] | 2227 | { |
| 2228 | struct snd_ctl_elem_id id; |
| 2229 | memset(&id, 0, sizeof(id)); |
| 2230 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2231 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2232 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2233 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2234 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2235 | strcpy(id.name, name); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2236 | return snd_ctl_find_id(codec->card, &id); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2237 | } |
| 2238 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2239 | /** |
| 2240 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2241 | * @codec: HD-audio codec |
| 2242 | * @name: ctl id name string |
| 2243 | * |
| 2244 | * Get the control element with the given id string and IFACE_MIXER. |
| 2245 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2246 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2247 | const char *name) |
| 2248 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2249 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2250 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2251 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2252 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2253 | 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] | 2254 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2255 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2256 | int i, idx; |
| 2257 | /* 16 ctlrs should be large enough */ |
| 2258 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2259 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2260 | return idx; |
| 2261 | } |
| 2262 | return -EBUSY; |
| 2263 | } |
| 2264 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2265 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2266 | * 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] | 2267 | * @codec: HD-audio codec |
| 2268 | * @nid: corresponding NID (optional) |
| 2269 | * @kctl: the control element to assign |
| 2270 | * |
| 2271 | * Add the given control element to an array inside the codec instance. |
| 2272 | * All control elements belonging to a codec are supposed to be added |
| 2273 | * by this function so that a proper clean-up works at the free or |
| 2274 | * reconfiguration time. |
| 2275 | * |
| 2276 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2277 | * The assignment is shown in the codec proc file. |
| 2278 | * |
| 2279 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2280 | * #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] | 2281 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2282 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2283 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2284 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2285 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2286 | { |
| 2287 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2288 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2289 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2290 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2291 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2292 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2293 | if (nid == 0) |
| 2294 | nid = get_amp_nid_(kctl->private_value); |
| 2295 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2296 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2297 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2298 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2299 | kctl->id.subdevice = 0; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2300 | err = snd_ctl_add(codec->card, kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2301 | if (err < 0) |
| 2302 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2303 | item = snd_array_new(&codec->mixers); |
| 2304 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2305 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2306 | item->kctl = kctl; |
| 2307 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2308 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2309 | return 0; |
| 2310 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2311 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2312 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2313 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2314 | * snd_hda_add_nid - Assign a NID to a control element |
| 2315 | * @codec: HD-audio codec |
| 2316 | * @nid: corresponding NID (optional) |
| 2317 | * @kctl: the control element to assign |
| 2318 | * @index: index to kctl |
| 2319 | * |
| 2320 | * Add the given control element to an array inside the codec instance. |
| 2321 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2322 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2323 | */ |
| 2324 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2325 | unsigned int index, hda_nid_t nid) |
| 2326 | { |
| 2327 | struct hda_nid_item *item; |
| 2328 | |
| 2329 | if (nid > 0) { |
| 2330 | item = snd_array_new(&codec->nids); |
| 2331 | if (!item) |
| 2332 | return -ENOMEM; |
| 2333 | item->kctl = kctl; |
| 2334 | item->index = index; |
| 2335 | item->nid = nid; |
| 2336 | return 0; |
| 2337 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2338 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2339 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2340 | return -EINVAL; |
| 2341 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2342 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2343 | |
| 2344 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2345 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2346 | * @codec: HD-audio codec |
| 2347 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2348 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2349 | { |
| 2350 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2351 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2352 | for (i = 0; i < codec->mixers.used; i++) |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2353 | snd_ctl_remove(codec->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2354 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2355 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2356 | } |
| 2357 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2358 | /** |
| 2359 | * snd_hda_lock_devices - pseudo device locking |
| 2360 | * @bus: the BUS |
| 2361 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2362 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2363 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2364 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2365 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2366 | struct snd_card *card = bus->card; |
| 2367 | struct hda_codec *codec; |
| 2368 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2369 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2370 | if (card->shutdown) |
| 2371 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2372 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2373 | if (!list_empty(&card->ctl_files)) |
| 2374 | goto err_clear; |
| 2375 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 2376 | list_for_each_codec(codec, bus) { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 2377 | struct hda_pcm *cpcm; |
| 2378 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2379 | if (!cpcm->pcm) |
| 2380 | continue; |
| 2381 | if (cpcm->pcm->streams[0].substream_opened || |
| 2382 | cpcm->pcm->streams[1].substream_opened) |
| 2383 | goto err_clear; |
| 2384 | } |
| 2385 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2386 | spin_unlock(&card->files_lock); |
| 2387 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2388 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2389 | err_clear: |
| 2390 | card->shutdown = 0; |
| 2391 | err_unlock: |
| 2392 | spin_unlock(&card->files_lock); |
| 2393 | return -EINVAL; |
| 2394 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2395 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2396 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2397 | /** |
| 2398 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2399 | * @bus: the BUS |
| 2400 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2401 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2402 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2403 | struct snd_card *card = bus->card; |
| 2404 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2405 | spin_lock(&card->files_lock); |
| 2406 | card->shutdown = 0; |
| 2407 | spin_unlock(&card->files_lock); |
| 2408 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2409 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2410 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2411 | /** |
| 2412 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2413 | * @codec: HD-audio codec |
| 2414 | * |
| 2415 | * This frees the all PCM and control elements assigned to the codec, and |
| 2416 | * clears the caches and restores the pin default configurations. |
| 2417 | * |
| 2418 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2419 | * returns zero. |
| 2420 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2421 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2422 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2423 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2424 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2425 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2426 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* OK, let it free */ |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 2429 | if (device_is_registered(hda_codec_dev(codec))) |
| 2430 | device_del(hda_codec_dev(codec)); |
| 2431 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2432 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2433 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2434 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2437 | typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2438 | |
| 2439 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2440 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2441 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2442 | { |
| 2443 | struct hda_nid_item *items; |
| 2444 | const char * const *s; |
| 2445 | int i, err; |
| 2446 | |
| 2447 | items = codec->mixers.list; |
| 2448 | for (i = 0; i < codec->mixers.used; i++) { |
| 2449 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2450 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2451 | continue; |
| 2452 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2453 | char tmpname[sizeof(sctl->id.name)]; |
| 2454 | const char *name = *s; |
| 2455 | if (suffix) { |
| 2456 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2457 | name, suffix); |
| 2458 | name = tmpname; |
| 2459 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2460 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2461 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2462 | if (err) |
| 2463 | return err; |
| 2464 | break; |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | return 0; |
| 2469 | } |
| 2470 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2471 | static int check_slave_present(struct hda_codec *codec, |
| 2472 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2473 | { |
| 2474 | return 1; |
| 2475 | } |
| 2476 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2477 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2478 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2479 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2480 | { |
| 2481 | int _tlv[4]; |
| 2482 | const int *tlv = NULL; |
| 2483 | int val = -1; |
| 2484 | |
| 2485 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2486 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2487 | mm_segment_t fs = get_fs(); |
| 2488 | set_fs(get_ds()); |
| 2489 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2490 | tlv = _tlv; |
| 2491 | set_fs(fs); |
| 2492 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2493 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2494 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2495 | int step = tlv[3]; |
| 2496 | step &= ~TLV_DB_SCALE_MUTE; |
| 2497 | if (!step) |
| 2498 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2499 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2500 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2501 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2502 | return -1; |
| 2503 | } |
| 2504 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2505 | val = -tlv[2] / step; |
| 2506 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2507 | return val; |
| 2508 | } |
| 2509 | |
| 2510 | /* call kctl->put with the given value(s) */ |
| 2511 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2512 | { |
| 2513 | struct snd_ctl_elem_value *ucontrol; |
| 2514 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2515 | if (!ucontrol) |
| 2516 | return -ENOMEM; |
| 2517 | ucontrol->value.integer.value[0] = val; |
| 2518 | ucontrol->value.integer.value[1] = val; |
| 2519 | kctl->put(kctl, ucontrol); |
| 2520 | kfree(ucontrol); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2525 | static int init_slave_0dB(struct hda_codec *codec, |
| 2526 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2527 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2528 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2529 | if (offset > 0) |
| 2530 | put_kctl_with_value(slave, offset); |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2535 | static int init_slave_unmute(struct hda_codec *codec, |
| 2536 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2537 | { |
| 2538 | return put_kctl_with_value(slave, 1); |
| 2539 | } |
| 2540 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2541 | static int add_slave(struct hda_codec *codec, |
| 2542 | void *data, struct snd_kcontrol *slave) |
| 2543 | { |
| 2544 | return snd_ctl_add_slave(data, slave); |
| 2545 | } |
| 2546 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2547 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2548 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2549 | * @codec: HD-audio codec |
| 2550 | * @name: vmaster control name |
| 2551 | * @tlv: TLV data (optional) |
| 2552 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2553 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2554 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2555 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2556 | * |
| 2557 | * Create a virtual master control with the given name. The TLV data |
| 2558 | * must be either NULL or a valid data. |
| 2559 | * |
| 2560 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2561 | * slave control name. All controls with these names are assigned to |
| 2562 | * the new virtual master control. |
| 2563 | * |
| 2564 | * This function returns zero if successful or a negative error code. |
| 2565 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2566 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2567 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2568 | const char *suffix, bool init_slave_vol, |
| 2569 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2570 | { |
| 2571 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2572 | int err; |
| 2573 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2574 | if (ctl_ret) |
| 2575 | *ctl_ret = NULL; |
| 2576 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2577 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2578 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2579 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2580 | return 0; |
| 2581 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2582 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2583 | if (!kctl) |
| 2584 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2585 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2586 | if (err < 0) |
| 2587 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2588 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2589 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2590 | if (err < 0) |
| 2591 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2592 | |
| 2593 | /* init with master mute & zero volume */ |
| 2594 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2595 | if (init_slave_vol) { |
| 2596 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2597 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2598 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2599 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2600 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2601 | if (ctl_ret) |
| 2602 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2605 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2606 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2607 | /* |
| 2608 | * mute-LED control using vmaster |
| 2609 | */ |
| 2610 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2611 | struct snd_ctl_elem_info *uinfo) |
| 2612 | { |
| 2613 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2614 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2615 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2616 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2617 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2621 | struct snd_ctl_elem_value *ucontrol) |
| 2622 | { |
| 2623 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2624 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
| 2628 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2629 | struct snd_ctl_elem_value *ucontrol) |
| 2630 | { |
| 2631 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2632 | unsigned int old_mode = hook->mute_mode; |
| 2633 | |
| 2634 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2635 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2636 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2637 | if (old_mode == hook->mute_mode) |
| 2638 | return 0; |
| 2639 | snd_hda_sync_vmaster_hook(hook); |
| 2640 | return 1; |
| 2641 | } |
| 2642 | |
| 2643 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2644 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2645 | .name = "Mute-LED Mode", |
| 2646 | .info = vmaster_mute_mode_info, |
| 2647 | .get = vmaster_mute_mode_get, |
| 2648 | .put = vmaster_mute_mode_put, |
| 2649 | }; |
| 2650 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2651 | /** |
| 2652 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2653 | * @codec: the HDA codec |
| 2654 | * @hook: the vmaster hook object |
| 2655 | * @expose_enum_ctl: flag to create an enum ctl |
| 2656 | * |
| 2657 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2658 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2659 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2660 | */ |
| 2661 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2662 | struct hda_vmaster_mute_hook *hook, |
| 2663 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2664 | { |
| 2665 | struct snd_kcontrol *kctl; |
| 2666 | |
| 2667 | if (!hook->hook || !hook->sw_kctl) |
| 2668 | return 0; |
| 2669 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2670 | hook->codec = codec; |
| 2671 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2672 | if (!expose_enum_ctl) |
| 2673 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2674 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2675 | if (!kctl) |
| 2676 | return -ENOMEM; |
| 2677 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2678 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2679 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2680 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2681 | /** |
| 2682 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 2683 | * @hook: the vmaster hook |
| 2684 | * |
| 2685 | * Call the hook with the current value for synchronization. |
| 2686 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2687 | */ |
| 2688 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2689 | { |
| 2690 | if (!hook->hook || !hook->codec) |
| 2691 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2692 | /* don't call vmaster hook in the destructor since it might have |
| 2693 | * been already destroyed |
| 2694 | */ |
| 2695 | if (hook->codec->bus->shutdown) |
| 2696 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2697 | switch (hook->mute_mode) { |
| 2698 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2699 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2700 | break; |
| 2701 | default: |
| 2702 | hook->hook(hook->codec, hook->mute_mode); |
| 2703 | break; |
| 2704 | } |
| 2705 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2706 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2707 | |
| 2708 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2709 | /** |
| 2710 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2711 | * @kcontrol: referred ctl element |
| 2712 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2713 | * |
| 2714 | * The control element is supposed to have the private_value field |
| 2715 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2716 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2717 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2718 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | { |
| 2720 | int chs = get_amp_channels(kcontrol); |
| 2721 | |
| 2722 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2723 | uinfo->count = chs == 3 ? 2 : 1; |
| 2724 | uinfo->value.integer.min = 0; |
| 2725 | uinfo->value.integer.max = 1; |
| 2726 | return 0; |
| 2727 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2728 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2730 | /** |
| 2731 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2732 | * @kcontrol: ctl element |
| 2733 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2734 | * |
| 2735 | * The control element is supposed to have the private_value field |
| 2736 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2737 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2738 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2739 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | { |
| 2741 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2742 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2743 | int chs = get_amp_channels(kcontrol); |
| 2744 | int dir = get_amp_direction(kcontrol); |
| 2745 | int idx = get_amp_index(kcontrol); |
| 2746 | long *valp = ucontrol->value.integer.value; |
| 2747 | |
| 2748 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2749 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2750 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2752 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2753 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | return 0; |
| 2755 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2756 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2758 | /** |
| 2759 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2760 | * @kcontrol: ctl element |
| 2761 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2762 | * |
| 2763 | * The control element is supposed to have the private_value field |
| 2764 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2765 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2766 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2767 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | { |
| 2769 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2770 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2771 | int chs = get_amp_channels(kcontrol); |
| 2772 | int dir = get_amp_direction(kcontrol); |
| 2773 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | long *valp = ucontrol->value.integer.value; |
| 2775 | int change = 0; |
| 2776 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2777 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2778 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 2779 | HDA_AMP_MUTE, |
| 2780 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2781 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2782 | valp++; |
| 2783 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2784 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2785 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 2786 | HDA_AMP_MUTE, |
| 2787 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2788 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2789 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | return change; |
| 2791 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2792 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
| 2794 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2795 | * bound volume controls |
| 2796 | * |
| 2797 | * bind multiple volumes (# indices, from 0) |
| 2798 | */ |
| 2799 | |
| 2800 | #define AMP_VAL_IDX_SHIFT 19 |
| 2801 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2802 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2803 | /** |
| 2804 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2805 | * @kcontrol: ctl element |
| 2806 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2807 | * |
| 2808 | * The control element is supposed to have the private_value field |
| 2809 | * set up via HDA_BIND_MUTE*() macros. |
| 2810 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2811 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2812 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2813 | { |
| 2814 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2815 | unsigned long pval; |
| 2816 | int err; |
| 2817 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2818 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2819 | pval = kcontrol->private_value; |
| 2820 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2821 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2822 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2823 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2824 | return err; |
| 2825 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2826 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2827 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2828 | /** |
| 2829 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2830 | * @kcontrol: ctl element |
| 2831 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2832 | * |
| 2833 | * The control element is supposed to have the private_value field |
| 2834 | * set up via HDA_BIND_MUTE*() macros. |
| 2835 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2836 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2837 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2838 | { |
| 2839 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2840 | unsigned long pval; |
| 2841 | int i, indices, err = 0, change = 0; |
| 2842 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2843 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2844 | pval = kcontrol->private_value; |
| 2845 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2846 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2847 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2848 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2849 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2850 | if (err < 0) |
| 2851 | break; |
| 2852 | change |= err; |
| 2853 | } |
| 2854 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2855 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2856 | return err < 0 ? err : change; |
| 2857 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2858 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2859 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2860 | /** |
| 2861 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2862 | * @kcontrol: referred ctl element |
| 2863 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2864 | * |
| 2865 | * The control element is supposed to have the private_value field |
| 2866 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2867 | */ |
| 2868 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2869 | struct snd_ctl_elem_info *uinfo) |
| 2870 | { |
| 2871 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2872 | struct hda_bind_ctls *c; |
| 2873 | int err; |
| 2874 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2875 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2876 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2877 | kcontrol->private_value = *c->values; |
| 2878 | err = c->ops->info(kcontrol, uinfo); |
| 2879 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2880 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2881 | return err; |
| 2882 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2883 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2884 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2885 | /** |
| 2886 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2887 | * @kcontrol: ctl element |
| 2888 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2889 | * |
| 2890 | * The control element is supposed to have the private_value field |
| 2891 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2892 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2893 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2894 | struct snd_ctl_elem_value *ucontrol) |
| 2895 | { |
| 2896 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2897 | struct hda_bind_ctls *c; |
| 2898 | int err; |
| 2899 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2900 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2901 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2902 | kcontrol->private_value = *c->values; |
| 2903 | err = c->ops->get(kcontrol, ucontrol); |
| 2904 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2905 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2906 | return err; |
| 2907 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2908 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2909 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2910 | /** |
| 2911 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2912 | * @kcontrol: ctl element |
| 2913 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2914 | * |
| 2915 | * The control element is supposed to have the private_value field |
| 2916 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2917 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2918 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2919 | struct snd_ctl_elem_value *ucontrol) |
| 2920 | { |
| 2921 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2922 | struct hda_bind_ctls *c; |
| 2923 | unsigned long *vals; |
| 2924 | int err = 0, change = 0; |
| 2925 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2926 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2927 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2928 | for (vals = c->values; *vals; vals++) { |
| 2929 | kcontrol->private_value = *vals; |
| 2930 | err = c->ops->put(kcontrol, ucontrol); |
| 2931 | if (err < 0) |
| 2932 | break; |
| 2933 | change |= err; |
| 2934 | } |
| 2935 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2936 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2937 | return err < 0 ? err : change; |
| 2938 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2939 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2940 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2941 | /** |
| 2942 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2943 | * @kcontrol: ctl element |
| 2944 | * @op_flag: operation flag |
| 2945 | * @size: byte size of input TLV |
| 2946 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2947 | * |
| 2948 | * The control element is supposed to have the private_value field |
| 2949 | * set up via HDA_BIND_VOL() macro. |
| 2950 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2951 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2952 | unsigned int size, unsigned int __user *tlv) |
| 2953 | { |
| 2954 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2955 | struct hda_bind_ctls *c; |
| 2956 | int err; |
| 2957 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2958 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2959 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2960 | kcontrol->private_value = *c->values; |
| 2961 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 2962 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2963 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2964 | return err; |
| 2965 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2966 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2967 | |
| 2968 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 2969 | .info = snd_hda_mixer_amp_volume_info, |
| 2970 | .get = snd_hda_mixer_amp_volume_get, |
| 2971 | .put = snd_hda_mixer_amp_volume_put, |
| 2972 | .tlv = snd_hda_mixer_amp_tlv |
| 2973 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2974 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2975 | |
| 2976 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 2977 | .info = snd_hda_mixer_amp_switch_info, |
| 2978 | .get = snd_hda_mixer_amp_switch_get, |
| 2979 | .put = snd_hda_mixer_amp_switch_put, |
| 2980 | .tlv = snd_hda_mixer_amp_tlv |
| 2981 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2982 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2983 | |
| 2984 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | * SPDIF out controls |
| 2986 | */ |
| 2987 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2988 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 2989 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | { |
| 2991 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 2992 | uinfo->count = 1; |
| 2993 | return 0; |
| 2994 | } |
| 2995 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2996 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 2997 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | { |
| 2999 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3000 | IEC958_AES0_NONAUDIO | |
| 3001 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3002 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3003 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3004 | IEC958_AES1_CON_ORIGINAL; |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3008 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3009 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | { |
| 3011 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3012 | IEC958_AES0_NONAUDIO | |
| 3013 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3017 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3018 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | { |
| 3020 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3021 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3022 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3024 | mutex_lock(&codec->spdif_mutex); |
| 3025 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3026 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3027 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3028 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3029 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3030 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3036 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3037 | */ |
| 3038 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3039 | { |
| 3040 | unsigned short val = 0; |
| 3041 | |
| 3042 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3043 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3045 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3047 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3048 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3049 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3051 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3052 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3053 | val |= AC_DIG1_EMPHASIS; |
| 3054 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3055 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3057 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3059 | } |
| 3060 | return val; |
| 3061 | } |
| 3062 | |
| 3063 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3064 | */ |
| 3065 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3066 | { |
| 3067 | unsigned int sbits = 0; |
| 3068 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3069 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3071 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3073 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3074 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3076 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3077 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3079 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3081 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3083 | sbits |= val & (0x7f << 8); |
| 3084 | } |
| 3085 | return sbits; |
| 3086 | } |
| 3087 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3088 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3089 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3090 | int verb, int val) |
| 3091 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3092 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3093 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3094 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3095 | d = codec->slave_dig_outs; |
| 3096 | if (!d) |
| 3097 | return; |
| 3098 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3099 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3103 | int dig1, int dig2) |
| 3104 | { |
| 3105 | if (dig1 != -1) |
| 3106 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3107 | if (dig2 != -1) |
| 3108 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3109 | } |
| 3110 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3111 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3112 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | { |
| 3114 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3115 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3116 | struct hda_spdif_out *spdif; |
| 3117 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | unsigned short val; |
| 3119 | int change; |
| 3120 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3121 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3122 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3123 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3124 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3126 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3127 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3128 | val = convert_from_spdif_status(spdif->status); |
| 3129 | val |= spdif->ctls & 1; |
| 3130 | change = spdif->ctls != val; |
| 3131 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3132 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3133 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3134 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | return change; |
| 3136 | } |
| 3137 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3138 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3140 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3141 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | { |
| 3143 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3144 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3145 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3147 | mutex_lock(&codec->spdif_mutex); |
| 3148 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3149 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3150 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | return 0; |
| 3152 | } |
| 3153 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3154 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3155 | int dig1, int dig2) |
| 3156 | { |
| 3157 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3158 | /* unmute amp switch (if any) */ |
| 3159 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3160 | (dig1 & AC_DIG1_ENABLE)) |
| 3161 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3162 | HDA_AMP_MUTE, 0); |
| 3163 | } |
| 3164 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3165 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3166 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | { |
| 3168 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3169 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3170 | struct hda_spdif_out *spdif; |
| 3171 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | unsigned short val; |
| 3173 | int change; |
| 3174 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3175 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3176 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3177 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3178 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3180 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3181 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3182 | spdif->ctls = val; |
| 3183 | if (change && nid != (u16)-1) |
| 3184 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3185 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | return change; |
| 3187 | } |
| 3188 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3189 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | { |
| 3191 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3192 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3193 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | .info = snd_hda_spdif_mask_info, |
| 3195 | .get = snd_hda_spdif_cmask_get, |
| 3196 | }, |
| 3197 | { |
| 3198 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3199 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3200 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | .info = snd_hda_spdif_mask_info, |
| 3202 | .get = snd_hda_spdif_pmask_get, |
| 3203 | }, |
| 3204 | { |
| 3205 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3206 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | .info = snd_hda_spdif_mask_info, |
| 3208 | .get = snd_hda_spdif_default_get, |
| 3209 | .put = snd_hda_spdif_default_put, |
| 3210 | }, |
| 3211 | { |
| 3212 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3213 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | .info = snd_hda_spdif_out_switch_info, |
| 3215 | .get = snd_hda_spdif_out_switch_get, |
| 3216 | .put = snd_hda_spdif_out_switch_put, |
| 3217 | }, |
| 3218 | { } /* end */ |
| 3219 | }; |
| 3220 | |
| 3221 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3222 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3224 | * @associated_nid: NID that new ctls associated with |
| 3225 | * @cvt_nid: converter NID |
| 3226 | * @type: HDA_PCM_TYPE_* |
| 3227 | * Creates controls related with the digital output. |
| 3228 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | * |
| 3230 | * Returns 0 if successful, or a negative error code. |
| 3231 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3232 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3233 | hda_nid_t associated_nid, |
| 3234 | hda_nid_t cvt_nid, |
| 3235 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | { |
| 3237 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3238 | struct snd_kcontrol *kctl; |
| 3239 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3240 | int idx = 0; |
| 3241 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3242 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3243 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3245 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3246 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3247 | idx = spdif_index; |
| 3248 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3249 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3250 | /* suppose a single SPDIF device */ |
| 3251 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3252 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3253 | if (!kctl) |
| 3254 | break; |
| 3255 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3256 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3257 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3258 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3259 | if (!bus->primary_dig_out_type) |
| 3260 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3261 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3262 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3263 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3264 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3265 | return -EBUSY; |
| 3266 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3267 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3268 | if (!spdif) |
| 3269 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3271 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3272 | if (!kctl) |
| 3273 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3274 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3275 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3276 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3277 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | return err; |
| 3279 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3280 | spdif->nid = cvt_nid; |
| 3281 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3282 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3283 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | return 0; |
| 3285 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3286 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3288 | /** |
| 3289 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3290 | * @codec: the HDA codec |
| 3291 | * @nid: widget NID |
| 3292 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3293 | * call within spdif_mutex lock |
| 3294 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3295 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3296 | hda_nid_t nid) |
| 3297 | { |
| 3298 | int i; |
| 3299 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3300 | struct hda_spdif_out *spdif = |
| 3301 | snd_array_elem(&codec->spdif_out, i); |
| 3302 | if (spdif->nid == nid) |
| 3303 | return spdif; |
| 3304 | } |
| 3305 | return NULL; |
| 3306 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3307 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3308 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3309 | /** |
| 3310 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3311 | * @codec: the HDA codec |
| 3312 | * @idx: the SPDIF ctl index |
| 3313 | * |
| 3314 | * Unassign the widget from the given SPDIF control. |
| 3315 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3316 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3317 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3318 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3319 | |
| 3320 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3321 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3322 | spdif->nid = (u16)-1; |
| 3323 | mutex_unlock(&codec->spdif_mutex); |
| 3324 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3325 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3326 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3327 | /** |
| 3328 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3329 | * @codec: the HDA codec |
| 3330 | * @idx: the SPDIF ctl idx |
| 3331 | * @nid: widget NID |
| 3332 | * |
| 3333 | * Assign the widget to the SPDIF control with the given index. |
| 3334 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3335 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3336 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3337 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3338 | unsigned short val; |
| 3339 | |
| 3340 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3341 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3342 | if (spdif->nid != nid) { |
| 3343 | spdif->nid = nid; |
| 3344 | val = spdif->ctls; |
| 3345 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3346 | } |
| 3347 | mutex_unlock(&codec->spdif_mutex); |
| 3348 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3349 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3352 | * SPDIF sharing with analog output |
| 3353 | */ |
| 3354 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3355 | struct snd_ctl_elem_value *ucontrol) |
| 3356 | { |
| 3357 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3358 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3359 | return 0; |
| 3360 | } |
| 3361 | |
| 3362 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3363 | struct snd_ctl_elem_value *ucontrol) |
| 3364 | { |
| 3365 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3366 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
| 3370 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3371 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3372 | .name = "IEC958 Default PCM Playback Switch", |
| 3373 | .info = snd_ctl_boolean_mono_info, |
| 3374 | .get = spdif_share_sw_get, |
| 3375 | .put = spdif_share_sw_put, |
| 3376 | }; |
| 3377 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3378 | /** |
| 3379 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3380 | * @codec: the HDA codec |
| 3381 | * @mout: multi-out instance |
| 3382 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3383 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3384 | struct hda_multi_out *mout) |
| 3385 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3386 | struct snd_kcontrol *kctl; |
| 3387 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3388 | if (!mout->dig_out_nid) |
| 3389 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3390 | |
| 3391 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3392 | if (!kctl) |
| 3393 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3394 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3395 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3396 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3397 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3398 | |
| 3399 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | * SPDIF input |
| 3401 | */ |
| 3402 | |
| 3403 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3404 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3405 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3406 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | { |
| 3408 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3409 | |
| 3410 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3414 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3415 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | { |
| 3417 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3418 | hda_nid_t nid = kcontrol->private_value; |
| 3419 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3420 | int change; |
| 3421 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3422 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3424 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3426 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3427 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3429 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | return change; |
| 3431 | } |
| 3432 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3433 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3434 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | { |
| 3436 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3437 | hda_nid_t nid = kcontrol->private_value; |
| 3438 | unsigned short val; |
| 3439 | unsigned int sbits; |
| 3440 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3441 | 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] | 3442 | sbits = convert_to_spdif_status(val); |
| 3443 | ucontrol->value.iec958.status[0] = sbits; |
| 3444 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3445 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3446 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3450 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | { |
| 3452 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3453 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | .info = snd_hda_spdif_in_switch_info, |
| 3455 | .get = snd_hda_spdif_in_switch_get, |
| 3456 | .put = snd_hda_spdif_in_switch_put, |
| 3457 | }, |
| 3458 | { |
| 3459 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3460 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3461 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | .info = snd_hda_spdif_mask_info, |
| 3463 | .get = snd_hda_spdif_in_status_get, |
| 3464 | }, |
| 3465 | { } /* end */ |
| 3466 | }; |
| 3467 | |
| 3468 | /** |
| 3469 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3470 | * @codec: the HDA codec |
| 3471 | * @nid: audio in widget NID |
| 3472 | * |
| 3473 | * Creates controls related with the SPDIF input. |
| 3474 | * Called from each patch supporting the SPDIF in. |
| 3475 | * |
| 3476 | * Returns 0 if successful, or a negative error code. |
| 3477 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3478 | 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] | 3479 | { |
| 3480 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3481 | struct snd_kcontrol *kctl; |
| 3482 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3483 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3485 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3486 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3487 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3488 | return -EBUSY; |
| 3489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3491 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3492 | if (!kctl) |
| 3493 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3495 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3496 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | return err; |
| 3498 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3499 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3500 | snd_hda_codec_read(codec, nid, 0, |
| 3501 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3502 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | return 0; |
| 3504 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3505 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3507 | /* |
| 3508 | * command cache |
| 3509 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3511 | /* 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] | 3512 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3513 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3514 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3515 | |
| 3516 | /** |
| 3517 | * snd_hda_codec_write_cache - send a single command with caching |
| 3518 | * @codec: the HDA codec |
| 3519 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3520 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3521 | * @verb: the verb to send |
| 3522 | * @parm: the parameter for the verb |
| 3523 | * |
| 3524 | * Send a single command without waiting for response. |
| 3525 | * |
| 3526 | * Returns 0 if successful, or a negative error code. |
| 3527 | */ |
| 3528 | 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] | 3529 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3530 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3531 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3532 | struct hda_cache_head *c; |
| 3533 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3534 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3535 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3536 | cache_only = codec->cached_write; |
| 3537 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3538 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3539 | if (err < 0) |
| 3540 | return err; |
| 3541 | } |
| 3542 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3543 | /* parm may contain the verb stuff for get/set amp */ |
| 3544 | verb = verb | (parm >> 8); |
| 3545 | parm &= 0xff; |
| 3546 | key = build_cmd_cache_key(nid, verb); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3547 | mutex_lock(&codec->bus->core.cmd_mutex); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3548 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3549 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3550 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3551 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3552 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3553 | mutex_unlock(&codec->bus->core.cmd_mutex); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3554 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3555 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3556 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3557 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3558 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3559 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3560 | * @codec: the HDA codec |
| 3561 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3562 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3563 | * @verb: the verb to send |
| 3564 | * @parm: the parameter for the verb |
| 3565 | * |
| 3566 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3567 | * command if the parameter is already identical with the cached value. |
| 3568 | * If not, it sends the command and refreshes the cache. |
| 3569 | * |
| 3570 | * Returns 0 if successful, or a negative error code. |
| 3571 | */ |
| 3572 | 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] | 3573 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3574 | { |
| 3575 | struct hda_cache_head *c; |
| 3576 | u32 key; |
| 3577 | |
| 3578 | /* parm may contain the verb stuff for get/set amp */ |
| 3579 | verb = verb | (parm >> 8); |
| 3580 | parm &= 0xff; |
| 3581 | key = build_cmd_cache_key(nid, verb); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3582 | mutex_lock(&codec->bus->core.cmd_mutex); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3583 | c = get_hash(&codec->cmd_cache, key); |
| 3584 | if (c && c->val == parm) { |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3585 | mutex_unlock(&codec->bus->core.cmd_mutex); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3586 | return 0; |
| 3587 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3588 | mutex_unlock(&codec->bus->core.cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3589 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3590 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3591 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3592 | |
| 3593 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3594 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3595 | * @codec: HD-audio codec |
| 3596 | * |
| 3597 | * Execute all verbs recorded in the command caches to resume. |
| 3598 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3599 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3600 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3601 | int i; |
| 3602 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3603 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3604 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3605 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3606 | struct hda_cache_head *buffer; |
| 3607 | u32 key; |
| 3608 | |
| 3609 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3610 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3611 | if (!key) |
| 3612 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3613 | if (!buffer->dirty) |
| 3614 | continue; |
| 3615 | buffer->dirty = 0; |
| 3616 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3617 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3618 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3619 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3620 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3621 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3622 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3623 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3624 | |
| 3625 | /** |
| 3626 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3627 | * @codec: the HDA codec |
| 3628 | * @seq: VERB array to send |
| 3629 | * |
| 3630 | * Send the commands sequentially from the given array. |
| 3631 | * Thte commands are recorded on cache for power-save and resume. |
| 3632 | * The array must be terminated with NID=0. |
| 3633 | */ |
| 3634 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3635 | const struct hda_verb *seq) |
| 3636 | { |
| 3637 | for (; seq->nid; seq++) |
| 3638 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3639 | seq->param); |
| 3640 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3641 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3642 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3643 | /** |
| 3644 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3645 | * @codec: HD-audio codec |
| 3646 | */ |
| 3647 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3648 | { |
| 3649 | snd_hda_codec_resume_amp(codec); |
| 3650 | snd_hda_codec_resume_cache(codec); |
| 3651 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3652 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3653 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3654 | /** |
| 3655 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3656 | * @codec: the HDA codec |
| 3657 | * @fg: function group (not used now) |
| 3658 | * @power_state: the power state to set (AC_PWRST_*) |
| 3659 | * |
| 3660 | * Set the given power state to all widgets that have the power control. |
| 3661 | * If the codec has power_filter set, it evaluates the power state and |
| 3662 | * filter out if it's unchanged as D3. |
| 3663 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3664 | 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] | 3665 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3666 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3667 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3668 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3669 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3670 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3671 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3672 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3673 | if (!(wcaps & AC_WCAP_POWER)) |
| 3674 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3675 | if (codec->power_filter) { |
| 3676 | state = codec->power_filter(codec, nid, power_state); |
| 3677 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3678 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3679 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3680 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3681 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3682 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3683 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3684 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3685 | |
| 3686 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3687 | * supported power states check |
| 3688 | */ |
| 3689 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3690 | unsigned int power_state) |
| 3691 | { |
| 3692 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3693 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3694 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3695 | return false; |
| 3696 | if (sup & power_state) |
| 3697 | return true; |
| 3698 | else |
| 3699 | return false; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3703 | * wait until the state is reached, returns the current state |
| 3704 | */ |
| 3705 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3706 | hda_nid_t fg, |
| 3707 | unsigned int power_state) |
| 3708 | { |
| 3709 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3710 | unsigned int state, actual_state; |
| 3711 | |
| 3712 | for (;;) { |
| 3713 | state = snd_hda_codec_read(codec, fg, 0, |
| 3714 | AC_VERB_GET_POWER_STATE, 0); |
| 3715 | if (state & AC_PWRST_ERROR) |
| 3716 | break; |
| 3717 | actual_state = (state >> 4) & 0x0f; |
| 3718 | if (actual_state == power_state) |
| 3719 | break; |
| 3720 | if (time_after_eq(jiffies, end_time)) |
| 3721 | break; |
| 3722 | /* wait until the codec reachs to the target state */ |
| 3723 | msleep(1); |
| 3724 | } |
| 3725 | return state; |
| 3726 | } |
| 3727 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3728 | /** |
| 3729 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 3730 | * @codec: the HDA codec |
| 3731 | * @nid: widget NID |
| 3732 | * @power_state: power state to evalue |
| 3733 | * |
| 3734 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 3735 | * This can be used a codec power_filter callback. |
| 3736 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3737 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3738 | hda_nid_t nid, |
| 3739 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3740 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3741 | if (nid == codec->afg || nid == codec->mfg) |
| 3742 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3743 | if (power_state == AC_PWRST_D3 && |
| 3744 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3745 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3746 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3747 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3748 | if (eapd & 0x02) |
| 3749 | return AC_PWRST_D0; |
| 3750 | } |
| 3751 | return power_state; |
| 3752 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3753 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3754 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3755 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3756 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3757 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3758 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3759 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3760 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3761 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3762 | int count; |
| 3763 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3764 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3765 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3766 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3767 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3768 | if (codec->depop_delay < 0) |
| 3769 | msleep(codec->epss ? 10 : 100); |
| 3770 | else if (codec->depop_delay > 0) |
| 3771 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3772 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3773 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3774 | |
| 3775 | /* repeat power states setting at most 10 times*/ |
| 3776 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3777 | if (codec->patch_ops.set_power_state) |
| 3778 | codec->patch_ops.set_power_state(codec, fg, |
| 3779 | power_state); |
| 3780 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3781 | state = power_state; |
| 3782 | if (codec->power_filter) |
| 3783 | state = codec->power_filter(codec, fg, state); |
| 3784 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3785 | snd_hda_codec_read(codec, fg, flags, |
| 3786 | AC_VERB_SET_POWER_STATE, |
| 3787 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3788 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3789 | } |
| 3790 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3791 | if (!(state & AC_PWRST_ERROR)) |
| 3792 | break; |
| 3793 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3794 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3795 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3796 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3797 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3798 | /* sync power states of all widgets; |
| 3799 | * this is called at the end of codec parsing |
| 3800 | */ |
| 3801 | static void sync_power_up_states(struct hda_codec *codec) |
| 3802 | { |
| 3803 | hda_nid_t nid = codec->start_nid; |
| 3804 | int i; |
| 3805 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3806 | /* don't care if no filter is used */ |
| 3807 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3808 | return; |
| 3809 | |
| 3810 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3811 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3812 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3813 | if (!(wcaps & AC_WCAP_POWER)) |
| 3814 | continue; |
| 3815 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3816 | if (target == AC_PWRST_D0) |
| 3817 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3818 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3819 | snd_hda_codec_write(codec, nid, 0, |
| 3820 | AC_VERB_SET_POWER_STATE, target); |
| 3821 | } |
| 3822 | } |
| 3823 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 3824 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3825 | /* execute additional init verbs */ |
| 3826 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3827 | { |
| 3828 | if (codec->init_verbs.list) |
| 3829 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3830 | } |
| 3831 | #else |
| 3832 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3833 | #endif |
| 3834 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3835 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3836 | /* update the power on/off account with the current jiffies */ |
| 3837 | static void update_power_acct(struct hda_codec *codec, bool on) |
| 3838 | { |
| 3839 | unsigned long delta = jiffies - codec->power_jiffies; |
| 3840 | |
| 3841 | if (on) |
| 3842 | codec->power_on_acct += delta; |
| 3843 | else |
| 3844 | codec->power_off_acct += delta; |
| 3845 | codec->power_jiffies += delta; |
| 3846 | } |
| 3847 | |
| 3848 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 3849 | { |
| 3850 | update_power_acct(codec, hda_codec_is_power_on(codec)); |
| 3851 | } |
| 3852 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3853 | /* |
| 3854 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3855 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3856 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3857 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3858 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3859 | unsigned int state; |
| 3860 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3861 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3862 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3863 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3864 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3865 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3866 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3867 | update_power_acct(codec, true); |
| 3868 | atomic_dec(&codec->in_pm); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3869 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3872 | /* mark all entries of cmd and amp caches dirty */ |
| 3873 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3874 | { |
| 3875 | int i; |
| 3876 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3877 | struct hda_cache_head *cmd; |
| 3878 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3879 | cmd->dirty = 1; |
| 3880 | } |
| 3881 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3882 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3883 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3884 | amp->head.dirty = 1; |
| 3885 | } |
| 3886 | } |
| 3887 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3888 | /* |
| 3889 | * kick up codec; used both from PM and power-save |
| 3890 | */ |
| 3891 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3892 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3893 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3894 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3895 | hda_mark_cmd_cache_dirty(codec); |
| 3896 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3897 | codec->power_jiffies = jiffies; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3898 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3899 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3900 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3901 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3902 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3903 | if (codec->patch_ops.resume) |
| 3904 | codec->patch_ops.resume(codec); |
| 3905 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3906 | if (codec->patch_ops.init) |
| 3907 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3908 | snd_hda_codec_resume_amp(codec); |
| 3909 | snd_hda_codec_resume_cache(codec); |
| 3910 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3911 | |
| 3912 | if (codec->jackpoll_interval) |
| 3913 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3914 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3915 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3916 | atomic_dec(&codec->in_pm); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3917 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3918 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3919 | static int hda_codec_runtime_suspend(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3920 | { |
| 3921 | struct hda_codec *codec = dev_to_hda_codec(dev); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3922 | struct hda_pcm *pcm; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3923 | unsigned int state; |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3924 | |
| 3925 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3926 | list_for_each_entry(pcm, &codec->pcm_list_head, list) |
| 3927 | snd_pcm_suspend_all(pcm->pcm); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3928 | state = hda_call_codec_suspend(codec); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3929 | if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK)) |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3930 | clear_bit(codec->core.addr, &codec->bus->core.codec_powered); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3931 | return 0; |
| 3932 | } |
| 3933 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3934 | static int hda_codec_runtime_resume(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3935 | { |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3936 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 3937 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 3938 | set_bit(codec->core.addr, &codec->bus->core.codec_powered); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3939 | hda_call_codec_resume(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3940 | pm_runtime_mark_last_busy(dev); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3941 | return 0; |
| 3942 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3943 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3944 | |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3945 | /* referred in hda_bind.c */ |
| 3946 | const struct dev_pm_ops hda_codec_driver_pm = { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3947 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 3948 | pm_runtime_force_resume) |
| 3949 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, |
| 3950 | NULL) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3951 | }; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3952 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3953 | /* |
| 3954 | * add standard channel maps if not specified |
| 3955 | */ |
| 3956 | static int add_std_chmaps(struct hda_codec *codec) |
| 3957 | { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3958 | struct hda_pcm *pcm; |
| 3959 | int str, err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3960 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3961 | list_for_each_entry(pcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3962 | for (str = 0; str < 2; str++) { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3963 | struct hda_pcm_stream *hinfo = &pcm->stream[str]; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3964 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3965 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3966 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3967 | if (pcm->own_chmap) |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3968 | continue; |
| 3969 | if (!pcm || !hinfo->substreams) |
| 3970 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3971 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3972 | err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 3973 | hinfo->channels_max, |
| 3974 | 0, &chmap); |
| 3975 | if (err < 0) |
| 3976 | return err; |
| 3977 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 3978 | } |
| 3979 | } |
| 3980 | return 0; |
| 3981 | } |
| 3982 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 3983 | /* default channel maps for 2.1 speakers; |
| 3984 | * since HD-audio supports only stereo, odd number channels are omitted |
| 3985 | */ |
| 3986 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 3987 | { .channels = 2, |
| 3988 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 3989 | { .channels = 4, |
| 3990 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 3991 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 3992 | { } |
| 3993 | }; |
| 3994 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 3995 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3996 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 3997 | { |
| 3998 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3999 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4000 | /* continue to initialize... */ |
| 4001 | if (codec->patch_ops.init) |
| 4002 | err = codec->patch_ops.init(codec); |
| 4003 | if (!err && codec->patch_ops.build_controls) |
| 4004 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4005 | if (err < 0) |
| 4006 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4007 | |
| 4008 | /* we create chmaps here instead of build_pcms */ |
| 4009 | err = add_std_chmaps(codec); |
| 4010 | if (err < 0) |
| 4011 | return err; |
| 4012 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4013 | if (codec->jackpoll_interval) |
| 4014 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4015 | else |
| 4016 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4017 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | return 0; |
| 4019 | } |
| 4020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | /* |
| 4022 | * stream formats |
| 4023 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4024 | struct hda_rate_tbl { |
| 4025 | unsigned int hz; |
| 4026 | unsigned int alsa_bits; |
| 4027 | unsigned int hda_fmt; |
| 4028 | }; |
| 4029 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4030 | /* rate = base * mult / div */ |
| 4031 | #define HDA_RATE(base, mult, div) \ |
| 4032 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4033 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4034 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4035 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4037 | |
| 4038 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4039 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4040 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4041 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4042 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4043 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4044 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4045 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4046 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4047 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4048 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4049 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4050 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4051 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4052 | |
| 4053 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4054 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4055 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4056 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | }; |
| 4058 | |
| 4059 | /** |
| 4060 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4061 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | * @rate: the sample rate |
| 4063 | * @channels: the number of channels |
| 4064 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4065 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4066 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | * |
| 4068 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4069 | * |
| 4070 | * Return zero if invalid. |
| 4071 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4072 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4073 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | unsigned int channels, |
| 4075 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4076 | unsigned int maxbps, |
| 4077 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | { |
| 4079 | int i; |
| 4080 | unsigned int val = 0; |
| 4081 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4082 | for (i = 0; rate_bits[i].hz; i++) |
| 4083 | if (rate_bits[i].hz == rate) { |
| 4084 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | break; |
| 4086 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4087 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4088 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | return 0; |
| 4090 | } |
| 4091 | |
| 4092 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4093 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | return 0; |
| 4095 | } |
| 4096 | val |= channels - 1; |
| 4097 | |
| 4098 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4099 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4100 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4101 | break; |
| 4102 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4103 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4104 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | case 20: |
| 4106 | case 24: |
| 4107 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4108 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4109 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4111 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4113 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | break; |
| 4115 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4116 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4117 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | return 0; |
| 4119 | } |
| 4120 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4121 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4122 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | return val; |
| 4125 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4126 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4128 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4129 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4130 | { |
| 4131 | unsigned int val = 0; |
| 4132 | if (nid != codec->afg && |
| 4133 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4134 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4135 | if (!val || val == -1) |
| 4136 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4137 | if (!val || val == -1) |
| 4138 | return 0; |
| 4139 | return val; |
| 4140 | } |
| 4141 | |
| 4142 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4143 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4144 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4145 | get_pcm_param); |
| 4146 | } |
| 4147 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4148 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4149 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4150 | { |
| 4151 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4152 | if (!streams || streams == -1) |
| 4153 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4154 | if (!streams || streams == -1) |
| 4155 | return 0; |
| 4156 | return streams; |
| 4157 | } |
| 4158 | |
| 4159 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4160 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4161 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4162 | get_stream_param); |
| 4163 | } |
| 4164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | /** |
| 4166 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4167 | * @codec: the HDA codec |
| 4168 | * @nid: NID to query |
| 4169 | * @ratesp: the pointer to store the detected rate bitflags |
| 4170 | * @formatsp: the pointer to store the detected formats |
| 4171 | * @bpsp: the pointer to store the detected format widths |
| 4172 | * |
| 4173 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4174 | * or @bsps argument is ignored. |
| 4175 | * |
| 4176 | * Returns 0 if successful, otherwise a negative error code. |
| 4177 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4178 | 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] | 4179 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4180 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4181 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4183 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4184 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | |
| 4186 | if (ratesp) { |
| 4187 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4188 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4190 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4192 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4193 | codec_err(codec, |
| 4194 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4195 | nid, val, |
| 4196 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4197 | return -EIO; |
| 4198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | *ratesp = rates; |
| 4200 | } |
| 4201 | |
| 4202 | if (formatsp || bpsp) { |
| 4203 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4204 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4206 | streams = query_stream_param(codec, nid); |
| 4207 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | |
| 4210 | bps = 0; |
| 4211 | if (streams & AC_SUPFMT_PCM) { |
| 4212 | if (val & AC_SUPPCM_BITS_8) { |
| 4213 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4214 | bps = 8; |
| 4215 | } |
| 4216 | if (val & AC_SUPPCM_BITS_16) { |
| 4217 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4218 | bps = 16; |
| 4219 | } |
| 4220 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4221 | if (val & AC_SUPPCM_BITS_32) |
| 4222 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4223 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4224 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4225 | if (val & AC_SUPPCM_BITS_24) |
| 4226 | bps = 24; |
| 4227 | else if (val & AC_SUPPCM_BITS_20) |
| 4228 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4229 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4230 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4232 | if (val & AC_SUPPCM_BITS_32) |
| 4233 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | else if (val & AC_SUPPCM_BITS_24) |
| 4235 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4236 | else if (val & AC_SUPPCM_BITS_20) |
| 4237 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
| 4239 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4240 | #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] | 4241 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4243 | if (!bps) |
| 4244 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4245 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4246 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4247 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4248 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | /* temporary hack: we have still no proper support |
| 4250 | * for the direct AC3 stream... |
| 4251 | */ |
| 4252 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4253 | bps = 8; |
| 4254 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4255 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4256 | codec_err(codec, |
| 4257 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4258 | nid, val, |
| 4259 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4260 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4261 | return -EIO; |
| 4262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | if (formatsp) |
| 4264 | *formatsp = formats; |
| 4265 | if (bpsp) |
| 4266 | *bpsp = bps; |
| 4267 | } |
| 4268 | |
| 4269 | return 0; |
| 4270 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4271 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | |
| 4273 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4274 | * snd_hda_is_supported_format - Check the validity of the format |
| 4275 | * @codec: HD-audio codec |
| 4276 | * @nid: NID to check |
| 4277 | * @format: the HD-audio format value to check |
| 4278 | * |
| 4279 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | * |
| 4281 | * Returns 1 if supported, 0 if not. |
| 4282 | */ |
| 4283 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4284 | unsigned int format) |
| 4285 | { |
| 4286 | int i; |
| 4287 | unsigned int val = 0, rate, stream; |
| 4288 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4289 | val = query_pcm_param(codec, nid); |
| 4290 | if (!val) |
| 4291 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | |
| 4293 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4294 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4295 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | if (val & (1 << i)) |
| 4297 | break; |
| 4298 | return 0; |
| 4299 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4300 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | return 0; |
| 4302 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4303 | stream = query_stream_param(codec, nid); |
| 4304 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | return 0; |
| 4306 | |
| 4307 | if (stream & AC_SUPFMT_PCM) { |
| 4308 | switch (format & 0xf0) { |
| 4309 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4310 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | return 0; |
| 4312 | break; |
| 4313 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4314 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | return 0; |
| 4316 | break; |
| 4317 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4318 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | return 0; |
| 4320 | break; |
| 4321 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4322 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | return 0; |
| 4324 | break; |
| 4325 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4326 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | return 0; |
| 4328 | break; |
| 4329 | default: |
| 4330 | return 0; |
| 4331 | } |
| 4332 | } else { |
| 4333 | /* FIXME: check for float32 and AC3? */ |
| 4334 | } |
| 4335 | |
| 4336 | return 1; |
| 4337 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4338 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
| 4340 | /* |
| 4341 | * PCM stuff |
| 4342 | */ |
| 4343 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4344 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4345 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | { |
| 4347 | return 0; |
| 4348 | } |
| 4349 | |
| 4350 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4351 | struct hda_codec *codec, |
| 4352 | unsigned int stream_tag, |
| 4353 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4354 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | { |
| 4356 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
| 4360 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4361 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4362 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4364 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | return 0; |
| 4366 | } |
| 4367 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4368 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4369 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4371 | int err; |
| 4372 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4373 | /* query support PCM information from the given NID */ |
| 4374 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4375 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4376 | info->rates ? NULL : &info->rates, |
| 4377 | info->formats ? NULL : &info->formats, |
| 4378 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4379 | if (err < 0) |
| 4380 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | } |
| 4382 | if (info->ops.open == NULL) |
| 4383 | info->ops.open = hda_pcm_default_open_close; |
| 4384 | if (info->ops.close == NULL) |
| 4385 | info->ops.close = hda_pcm_default_open_close; |
| 4386 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4387 | if (snd_BUG_ON(!info->nid)) |
| 4388 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | info->ops.prepare = hda_pcm_default_prepare; |
| 4390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4392 | if (snd_BUG_ON(!info->nid)) |
| 4393 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4395 | } |
| 4396 | return 0; |
| 4397 | } |
| 4398 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4399 | /* |
| 4400 | * codec prepare/cleanup entries |
| 4401 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4402 | /** |
| 4403 | * snd_hda_codec_prepare - Prepare a stream |
| 4404 | * @codec: the HDA codec |
| 4405 | * @hinfo: PCM information |
| 4406 | * @stream: stream tag to assign |
| 4407 | * @format: format id to assign |
| 4408 | * @substream: PCM substream to assign |
| 4409 | * |
| 4410 | * Calls the prepare callback set by the codec with the given arguments. |
| 4411 | * Clean up the inactive streams when successful. |
| 4412 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4413 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4414 | struct hda_pcm_stream *hinfo, |
| 4415 | unsigned int stream, |
| 4416 | unsigned int format, |
| 4417 | struct snd_pcm_substream *substream) |
| 4418 | { |
| 4419 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4420 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | 61ca410 | 2015-02-27 17:57:55 +0100 | [diff] [blame] | 4421 | if (hinfo->ops.prepare) |
| 4422 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, |
| 4423 | substream); |
| 4424 | else |
| 4425 | ret = -ENODEV; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4426 | if (ret >= 0) |
| 4427 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4428 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4429 | return ret; |
| 4430 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4431 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4432 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4433 | /** |
| 4434 | * snd_hda_codec_cleanup - Prepare a stream |
| 4435 | * @codec: the HDA codec |
| 4436 | * @hinfo: PCM information |
| 4437 | * @substream: PCM substream |
| 4438 | * |
| 4439 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4440 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4441 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4442 | struct hda_pcm_stream *hinfo, |
| 4443 | struct snd_pcm_substream *substream) |
| 4444 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4445 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | 61ca410 | 2015-02-27 17:57:55 +0100 | [diff] [blame] | 4446 | if (hinfo->ops.cleanup) |
| 4447 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4448 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4449 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4450 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4451 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4452 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4453 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4454 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4455 | }; |
| 4456 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4457 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4458 | * get the empty PCM device number to assign |
| 4459 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4460 | 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] | 4461 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4462 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4463 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4464 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4465 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4466 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4467 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4468 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4469 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4470 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4471 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4472 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4473 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4474 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4475 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4476 | return -EINVAL; |
| 4477 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4478 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4479 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4480 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4481 | if (audio_idx[type][i] >= 8) |
| 4482 | break; |
| 4483 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4484 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4485 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4486 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4487 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4488 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4489 | /* non-fixed slots starting from 10 */ |
| 4490 | for (i = 10; i < 32; i++) { |
| 4491 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4492 | return i; |
| 4493 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4494 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4495 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4496 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4497 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4498 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4499 | dev_warn(bus->card->dev, |
| 4500 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4501 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4502 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4503 | } |
| 4504 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4505 | /* call build_pcms ops of the given codec and set up the default parameters */ |
| 4506 | int snd_hda_codec_parse_pcms(struct hda_codec *codec) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4507 | { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4508 | struct hda_pcm *cpcm; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4509 | int err; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4510 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4511 | if (!list_empty(&codec->pcm_list_head)) |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4512 | return 0; /* already parsed */ |
| 4513 | |
| 4514 | if (!codec->patch_ops.build_pcms) |
| 4515 | return 0; |
| 4516 | |
| 4517 | err = codec->patch_ops.build_pcms(codec); |
| 4518 | if (err < 0) { |
| 4519 | codec_err(codec, "cannot build PCMs for #%d (error %d)\n", |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 4520 | codec->core.addr, err); |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4521 | return err; |
| 4522 | } |
| 4523 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4524 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4525 | int stream; |
| 4526 | |
| 4527 | for (stream = 0; stream < 2; stream++) { |
| 4528 | struct hda_pcm_stream *info = &cpcm->stream[stream]; |
| 4529 | |
| 4530 | if (!info->substreams) |
| 4531 | continue; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4532 | err = set_pcm_default_values(codec, info); |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4533 | if (err < 0) { |
| 4534 | codec_warn(codec, |
| 4535 | "fail to setup default for PCM %s\n", |
| 4536 | cpcm->name); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4537 | return err; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4538 | } |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4539 | } |
| 4540 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4541 | |
| 4542 | return 0; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4543 | } |
| 4544 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4545 | /* assign all PCMs of the given codec */ |
| 4546 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4547 | { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4548 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4549 | struct hda_pcm *cpcm; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4550 | int dev, err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4551 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4552 | if (snd_BUG_ON(!bus->ops.attach_pcm)) |
| 4553 | return -EINVAL; |
| 4554 | |
| 4555 | err = snd_hda_codec_parse_pcms(codec); |
| 4556 | if (err < 0) { |
| 4557 | snd_hda_codec_reset(codec); |
| 4558 | return err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4559 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4560 | |
| 4561 | /* attach a new PCM streams */ |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4562 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4563 | if (cpcm->pcm) |
| 4564 | continue; /* already attached */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4565 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4566 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4567 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4568 | dev = get_empty_pcm_device(bus, cpcm->pcm_type); |
| 4569 | if (dev < 0) |
| 4570 | continue; /* no fatal error */ |
| 4571 | cpcm->device = dev; |
| 4572 | err = bus->ops.attach_pcm(bus, codec, cpcm); |
| 4573 | if (err < 0) { |
| 4574 | codec_err(codec, |
| 4575 | "cannot attach PCM stream %d for codec #%d\n", |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 4576 | dev, codec->core.addr); |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4577 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4578 | } |
| 4579 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4580 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4581 | return 0; |
| 4582 | } |
| 4583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | * snd_hda_add_new_ctls - create controls from the array |
| 4586 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4587 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | * |
| 4589 | * This helper function creates and add new controls in the given array. |
| 4590 | * The array must be terminated with an empty entry as terminator. |
| 4591 | * |
| 4592 | * Returns 0 if successful, or a negative error code. |
| 4593 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4594 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4595 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4597 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | |
| 4599 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4600 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4601 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4602 | if (knew->iface == -1) /* skip this codec private value */ |
| 4603 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4604 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4605 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4606 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4607 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4608 | if (addr > 0) |
| 4609 | kctl->id.device = addr; |
| 4610 | if (idx > 0) |
| 4611 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4612 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4613 | if (!err) |
| 4614 | break; |
| 4615 | /* try first with another device index corresponding to |
| 4616 | * the codec addr; if it still fails (or it's the |
| 4617 | * primary codec), then try another control index |
| 4618 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 4619 | if (!addr && codec->core.addr) |
| 4620 | addr = codec->core.addr; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4621 | else if (!idx && !knew->index) { |
| 4622 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4623 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4624 | if (idx <= 0) |
| 4625 | return err; |
| 4626 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4627 | return err; |
| 4628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | } |
| 4630 | return 0; |
| 4631 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4632 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4634 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4635 | /** |
| 4636 | * snd_hda_power_up - Power-up the codec |
| 4637 | * @codec: HD-audio codec |
| 4638 | * |
| 4639 | * Increment the usage counter and resume the device if not done yet. |
| 4640 | */ |
| 4641 | void snd_hda_power_up(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4642 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4643 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4644 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4645 | if (codec_in_pm(codec)) |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4646 | return; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4647 | pm_runtime_get_sync(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4648 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4649 | EXPORT_SYMBOL_GPL(snd_hda_power_up); |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4650 | |
| 4651 | /** |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4652 | * snd_hda_power_down - Power-down the codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4653 | * @codec: HD-audio codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4654 | * |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4655 | * Decrement the usage counter and schedules the autosuspend if none used. |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4656 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4657 | void snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4658 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4659 | struct device *dev = hda_codec_dev(codec); |
| 4660 | |
| 4661 | if (codec_in_pm(codec)) |
| 4662 | return; |
| 4663 | pm_runtime_mark_last_busy(dev); |
| 4664 | pm_runtime_put_autosuspend(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4665 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4666 | EXPORT_SYMBOL_GPL(snd_hda_power_down); |
| 4667 | |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4668 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4669 | { |
| 4670 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4671 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4672 | if (delay > 0) { |
| 4673 | pm_runtime_set_autosuspend_delay(dev, delay); |
| 4674 | pm_runtime_use_autosuspend(dev); |
| 4675 | pm_runtime_allow(dev); |
| 4676 | if (!pm_runtime_suspended(dev)) |
| 4677 | pm_runtime_mark_last_busy(dev); |
| 4678 | } else { |
| 4679 | pm_runtime_dont_use_autosuspend(dev); |
| 4680 | pm_runtime_forbid(dev); |
| 4681 | } |
| 4682 | } |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4683 | |
| 4684 | /** |
| 4685 | * snd_hda_set_power_save - reprogram autosuspend for the given delay |
| 4686 | * @bus: HD-audio bus |
| 4687 | * @delay: autosuspend delay in msec, 0 = off |
| 4688 | * |
| 4689 | * Synchronize the runtime PM autosuspend state from the power_save option. |
| 4690 | */ |
| 4691 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) |
| 4692 | { |
| 4693 | struct hda_codec *c; |
| 4694 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 4695 | list_for_each_codec(c, bus) |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4696 | codec_set_power_save(c, delay); |
| 4697 | } |
| 4698 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4699 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4700 | /** |
| 4701 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4702 | * @codec: HD-audio codec |
| 4703 | * @check: the object containing an AMP list and the status |
| 4704 | * @nid: NID to check / update |
| 4705 | * |
| 4706 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4707 | * check the current AMP status, and update the the power-status according |
| 4708 | * to the mute status. |
| 4709 | * |
| 4710 | * This function is supposed to be set or called from the check_power_status |
| 4711 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4712 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4713 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4714 | struct hda_loopback_check *check, |
| 4715 | hda_nid_t nid) |
| 4716 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4717 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4718 | int ch, v; |
| 4719 | |
| 4720 | if (!check->amplist) |
| 4721 | return 0; |
| 4722 | for (p = check->amplist; p->nid; p++) { |
| 4723 | if (p->nid == nid) |
| 4724 | break; |
| 4725 | } |
| 4726 | if (!p->nid) |
| 4727 | return 0; /* nothing changed */ |
| 4728 | |
| 4729 | for (p = check->amplist; p->nid; p++) { |
| 4730 | for (ch = 0; ch < 2; ch++) { |
| 4731 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4732 | p->idx); |
| 4733 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4734 | if (!check->power_on) { |
| 4735 | check->power_on = 1; |
| 4736 | snd_hda_power_up(codec); |
| 4737 | } |
| 4738 | return 1; |
| 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | if (check->power_on) { |
| 4743 | check->power_on = 0; |
| 4744 | snd_hda_power_down(codec); |
| 4745 | } |
| 4746 | return 0; |
| 4747 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4748 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4749 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4751 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | * input MUX helper |
| 4753 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4754 | |
| 4755 | /** |
| 4756 | * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4757 | * @imux: imux helper object |
| 4758 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4759 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4760 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4761 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | { |
| 4763 | unsigned int index; |
| 4764 | |
| 4765 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4766 | uinfo->count = 1; |
| 4767 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4768 | if (!imux->num_items) |
| 4769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | index = uinfo->value.enumerated.item; |
| 4771 | if (index >= imux->num_items) |
| 4772 | index = imux->num_items - 1; |
| 4773 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4774 | return 0; |
| 4775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4776 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4778 | /** |
| 4779 | * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4780 | * @codec: the HDA codec |
| 4781 | * @imux: imux helper object |
| 4782 | * @ucontrol: pointer to get/store the data |
| 4783 | * @nid: input mux NID |
| 4784 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4785 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4786 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4787 | const struct hda_input_mux *imux, |
| 4788 | struct snd_ctl_elem_value *ucontrol, |
| 4789 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | unsigned int *cur_val) |
| 4791 | { |
| 4792 | unsigned int idx; |
| 4793 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4794 | if (!imux->num_items) |
| 4795 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | idx = ucontrol->value.enumerated.item[0]; |
| 4797 | if (idx >= imux->num_items) |
| 4798 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4799 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4801 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4802 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | *cur_val = idx; |
| 4804 | return 1; |
| 4805 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4806 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
| 4808 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4809 | /** |
| 4810 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 4811 | * @kcontrol: ctl element |
| 4812 | * @uinfo: pointer to get/store the data |
| 4813 | * @num_items: number of enum items |
| 4814 | * @texts: enum item string array |
| 4815 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4816 | * process kcontrol info callback of a simple string enum array |
| 4817 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 4818 | */ |
| 4819 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 4820 | struct snd_ctl_elem_info *uinfo, |
| 4821 | int num_items, const char * const *texts) |
| 4822 | { |
| 4823 | static const char * const texts_default[] = { |
| 4824 | "Disabled", "Enabled" |
| 4825 | }; |
| 4826 | |
| 4827 | if (!texts || !num_items) { |
| 4828 | num_items = 2; |
| 4829 | texts = texts_default; |
| 4830 | } |
| 4831 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 4832 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4833 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4834 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4835 | |
| 4836 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | * Multi-channel / digital-out PCM helper functions |
| 4838 | */ |
| 4839 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4840 | /* setup SPDIF output stream */ |
| 4841 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 4842 | unsigned int stream_tag, unsigned int format) |
| 4843 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4844 | struct hda_spdif_out *spdif; |
| 4845 | unsigned int curr_fmt; |
| 4846 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4847 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4848 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 4849 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 4850 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 4851 | reset = codec->spdif_status_reset && |
| 4852 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 4853 | curr_fmt != format; |
| 4854 | |
| 4855 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 4856 | updated */ |
| 4857 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4858 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4859 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4860 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4861 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4862 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4863 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4864 | for (d = codec->slave_dig_outs; *d; d++) |
| 4865 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 4866 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4867 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4868 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4869 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4870 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4871 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4872 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4873 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4874 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 4875 | { |
| 4876 | snd_hda_codec_cleanup_stream(codec, nid); |
| 4877 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4878 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4879 | for (d = codec->slave_dig_outs; *d; d++) |
| 4880 | snd_hda_codec_cleanup_stream(codec, *d); |
| 4881 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4884 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4885 | * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4886 | * @codec: the HDA codec |
| 4887 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4889 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 4890 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4892 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 4893 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 4894 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4895 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4897 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | return 0; |
| 4899 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4900 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4902 | /** |
| 4903 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4904 | * @codec: the HDA codec |
| 4905 | * @mout: hda_multi_out object |
| 4906 | * @stream_tag: stream tag to assign |
| 4907 | * @format: format id to assign |
| 4908 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4909 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4910 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 4911 | struct hda_multi_out *mout, |
| 4912 | unsigned int stream_tag, |
| 4913 | unsigned int format, |
| 4914 | struct snd_pcm_substream *substream) |
| 4915 | { |
| 4916 | mutex_lock(&codec->spdif_mutex); |
| 4917 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 4918 | mutex_unlock(&codec->spdif_mutex); |
| 4919 | return 0; |
| 4920 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4921 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4922 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4923 | /** |
| 4924 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4925 | * @codec: the HDA codec |
| 4926 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4927 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 4928 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 4929 | struct hda_multi_out *mout) |
| 4930 | { |
| 4931 | mutex_lock(&codec->spdif_mutex); |
| 4932 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 4933 | mutex_unlock(&codec->spdif_mutex); |
| 4934 | return 0; |
| 4935 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4936 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 4937 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4938 | /** |
| 4939 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4940 | * @codec: the HDA codec |
| 4941 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4943 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 4944 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4946 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 4948 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | return 0; |
| 4950 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4951 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4953 | /** |
| 4954 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4955 | * @codec: the HDA codec |
| 4956 | * @mout: hda_multi_out object |
| 4957 | * @substream: PCM substream to assign |
| 4958 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4959 | * |
| 4960 | * Open analog outputs and set up the hw-constraints. |
| 4961 | * If the digital outputs can be opened as slave, open the digital |
| 4962 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4964 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 4965 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4966 | struct snd_pcm_substream *substream, |
| 4967 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4969 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 4970 | runtime->hw.channels_max = mout->max_channels; |
| 4971 | if (mout->dig_out_nid) { |
| 4972 | if (!mout->analog_rates) { |
| 4973 | mout->analog_rates = hinfo->rates; |
| 4974 | mout->analog_formats = hinfo->formats; |
| 4975 | mout->analog_maxbps = hinfo->maxbps; |
| 4976 | } else { |
| 4977 | runtime->hw.rates = mout->analog_rates; |
| 4978 | runtime->hw.formats = mout->analog_formats; |
| 4979 | hinfo->maxbps = mout->analog_maxbps; |
| 4980 | } |
| 4981 | if (!mout->spdif_rates) { |
| 4982 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 4983 | &mout->spdif_rates, |
| 4984 | &mout->spdif_formats, |
| 4985 | &mout->spdif_maxbps); |
| 4986 | } |
| 4987 | mutex_lock(&codec->spdif_mutex); |
| 4988 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 4989 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 4990 | (runtime->hw.formats & mout->spdif_formats)) { |
| 4991 | runtime->hw.rates &= mout->spdif_rates; |
| 4992 | runtime->hw.formats &= mout->spdif_formats; |
| 4993 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 4994 | hinfo->maxbps = mout->spdif_maxbps; |
| 4995 | } else { |
| 4996 | mout->share_spdif = 0; |
| 4997 | /* FIXME: need notify? */ |
| 4998 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 4999 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5000 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5003 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5004 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5005 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5007 | /** |
| 5008 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5009 | * @codec: the HDA codec |
| 5010 | * @mout: hda_multi_out object |
| 5011 | * @stream_tag: stream tag to assign |
| 5012 | * @format: format id to assign |
| 5013 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5014 | * |
| 5015 | * Set up the i/o for analog out. |
| 5016 | * 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] | 5017 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5018 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5019 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | unsigned int stream_tag, |
| 5021 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5022 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5024 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5026 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | int i; |
| 5028 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5029 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5030 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5031 | if (mout->dig_out_nid && mout->share_spdif && |
| 5032 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5034 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5035 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5036 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5038 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5039 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | } else { |
| 5041 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5042 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | } |
| 5044 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5045 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
| 5047 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5048 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5049 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5050 | if (!mout->no_share_stream && |
| 5051 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5053 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5054 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5055 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5056 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5057 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5058 | snd_hda_codec_setup_stream(codec, |
| 5059 | mout->hp_out_nid[i], |
| 5060 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | /* surrounds */ |
| 5063 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5064 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5065 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5066 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5067 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5068 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5069 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5071 | |
| 5072 | /* extra surrounds */ |
| 5073 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5074 | int ch = 0; |
| 5075 | if (!mout->extra_out_nid[i]) |
| 5076 | break; |
| 5077 | if (chs >= (i + 1) * 2) |
| 5078 | ch = i * 2; |
| 5079 | else if (!mout->no_share_stream) |
| 5080 | break; |
| 5081 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5082 | stream_tag, ch, format); |
| 5083 | } |
| 5084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | return 0; |
| 5086 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5087 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5089 | /** |
| 5090 | * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5091 | * @codec: the HDA codec |
| 5092 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5094 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5095 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5097 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | int i; |
| 5099 | |
| 5100 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5101 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5103 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5104 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5105 | if (mout->hp_out_nid[i]) |
| 5106 | snd_hda_codec_cleanup_stream(codec, |
| 5107 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5108 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5109 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5110 | snd_hda_codec_cleanup_stream(codec, |
| 5111 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5112 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | 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] | 5114 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | mout->dig_out_used = 0; |
| 5116 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5117 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | return 0; |
| 5119 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5120 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5122 | /** |
| 5123 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5124 | * @codec: the HDA codec |
| 5125 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5126 | * |
| 5127 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5128 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5129 | */ |
| 5130 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5131 | { |
| 5132 | unsigned int pincap; |
| 5133 | unsigned int oldval; |
| 5134 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5135 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5136 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5137 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5138 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5139 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5140 | return AC_PINCTL_VREF_80; |
| 5141 | else if (pincap & AC_PINCAP_VREF_50) |
| 5142 | return AC_PINCTL_VREF_50; |
| 5143 | else if (pincap & AC_PINCAP_VREF_100) |
| 5144 | return AC_PINCTL_VREF_100; |
| 5145 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5146 | return AC_PINCTL_VREF_GRD; |
| 5147 | return AC_PINCTL_VREF_HIZ; |
| 5148 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5149 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5150 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5151 | /** |
| 5152 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5153 | * @codec: the HDA codec |
| 5154 | * @pin: referred pin NID |
| 5155 | * @val: pin ctl value to audit |
| 5156 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5157 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5158 | hda_nid_t pin, unsigned int val) |
| 5159 | { |
| 5160 | static unsigned int cap_lists[][2] = { |
| 5161 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5162 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5163 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5164 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5165 | }; |
| 5166 | unsigned int cap; |
| 5167 | |
| 5168 | if (!val) |
| 5169 | return 0; |
| 5170 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5171 | if (!cap) |
| 5172 | return val; /* don't know what to do... */ |
| 5173 | |
| 5174 | if (val & AC_PINCTL_OUT_EN) { |
| 5175 | if (!(cap & AC_PINCAP_OUT)) |
| 5176 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5177 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5178 | val &= ~AC_PINCTL_HP_EN; |
| 5179 | } |
| 5180 | |
| 5181 | if (val & AC_PINCTL_IN_EN) { |
| 5182 | if (!(cap & AC_PINCAP_IN)) |
| 5183 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5184 | else { |
| 5185 | unsigned int vcap, vref; |
| 5186 | int i; |
| 5187 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5188 | vref = val & AC_PINCTL_VREFEN; |
| 5189 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5190 | if (vref == cap_lists[i][0] && |
| 5191 | !(vcap & cap_lists[i][1])) { |
| 5192 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5193 | vref = AC_PINCTL_VREF_HIZ; |
| 5194 | else |
| 5195 | vref = cap_lists[i + 1][0]; |
| 5196 | } |
| 5197 | } |
| 5198 | val &= ~AC_PINCTL_VREFEN; |
| 5199 | val |= vref; |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | return val; |
| 5204 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5205 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5206 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5207 | /** |
| 5208 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5209 | * @codec: the HDA codec |
| 5210 | * @pin: referred pin NID |
| 5211 | * @val: pin control value to set |
| 5212 | * @cached: access over codec pinctl cache or direct write |
| 5213 | * |
| 5214 | * This function is a helper to set a pin ctl value more safely. |
| 5215 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5216 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5217 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5218 | * snd_hda_codec_write() depending on @cached flag. |
| 5219 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5220 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5221 | unsigned int val, bool cached) |
| 5222 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5223 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5224 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5225 | if (cached) |
| 5226 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5227 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5228 | else |
| 5229 | return snd_hda_codec_write(codec, pin, 0, |
| 5230 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5231 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5232 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5233 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5234 | /** |
| 5235 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5236 | * @codec: the HDA codec |
| 5237 | * @imux: imux helper object |
| 5238 | * @label: the name of imux item to assign |
| 5239 | * @index: index number of imux item to assign |
| 5240 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5241 | * |
| 5242 | * When the same label is used already in the existing items, the number |
| 5243 | * suffix is appended to the label. This label index number is stored |
| 5244 | * to type_idx when non-NULL pointer is given. |
| 5245 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5246 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5247 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5248 | int index, int *type_idx) |
| 5249 | { |
| 5250 | int i, label_idx = 0; |
| 5251 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5252 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5253 | return -EINVAL; |
| 5254 | } |
| 5255 | for (i = 0; i < imux->num_items; i++) { |
| 5256 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5257 | label_idx++; |
| 5258 | } |
| 5259 | if (type_idx) |
| 5260 | *type_idx = label_idx; |
| 5261 | if (label_idx > 0) |
| 5262 | snprintf(imux->items[imux->num_items].label, |
| 5263 | sizeof(imux->items[imux->num_items].label), |
| 5264 | "%s %d", label, label_idx); |
| 5265 | else |
| 5266 | strlcpy(imux->items[imux->num_items].label, label, |
| 5267 | sizeof(imux->items[imux->num_items].label)); |
| 5268 | imux->items[imux->num_items].index = index; |
| 5269 | imux->num_items++; |
| 5270 | return 0; |
| 5271 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5272 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | /** |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5275 | * snd_hda_bus_reset - Reset the bus |
| 5276 | * @bus: HD-audio bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5278 | void snd_hda_bus_reset(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5280 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame^] | 5282 | list_for_each_codec(codec, bus) { |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5283 | /* FIXME: maybe a better way needed for forced reset */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5284 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5285 | #ifdef CONFIG_PM |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5286 | if (hda_codec_is_power_on(codec)) { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 5287 | hda_call_codec_suspend(codec); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5288 | hda_call_codec_resume(codec); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5289 | } |
| 5290 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5293 | EXPORT_SYMBOL_GPL(snd_hda_bus_reset); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5294 | |
| 5295 | /* |
| 5296 | * generic arrays |
| 5297 | */ |
| 5298 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5299 | /** |
| 5300 | * snd_array_new - get a new element from the given array |
| 5301 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5302 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5303 | * Get a new element from the given array. If it exceeds the |
| 5304 | * pre-allocated array size, re-allocate the array. |
| 5305 | * |
| 5306 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5307 | */ |
| 5308 | void *snd_array_new(struct snd_array *array) |
| 5309 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5310 | if (snd_BUG_ON(!array->elem_size)) |
| 5311 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5312 | if (array->used >= array->alloced) { |
| 5313 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5314 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5315 | void *nlist; |
| 5316 | if (snd_BUG_ON(num >= 4096)) |
| 5317 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5318 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5319 | if (!nlist) |
| 5320 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5321 | array->list = nlist; |
| 5322 | array->alloced = num; |
| 5323 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5324 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5325 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5326 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5327 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5328 | /** |
| 5329 | * snd_array_free - free the given array elements |
| 5330 | * @array: the array object |
| 5331 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5332 | void snd_array_free(struct snd_array *array) |
| 5333 | { |
| 5334 | kfree(array->list); |
| 5335 | array->used = 0; |
| 5336 | array->alloced = 0; |
| 5337 | array->list = NULL; |
| 5338 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5339 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5340 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5341 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5342 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5343 | * @pcm: PCM caps bits |
| 5344 | * @buf: the string buffer to write |
| 5345 | * @buflen: the max buffer length |
| 5346 | * |
| 5347 | * used by hda_proc.c and hda_eld.c |
| 5348 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5349 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5350 | { |
| 5351 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5352 | int i, j; |
| 5353 | |
| 5354 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5355 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5356 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5357 | |
| 5358 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5359 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5360 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5361 | |
| 5362 | MODULE_DESCRIPTION("HDA codec core"); |
| 5363 | MODULE_LICENSE("GPL"); |