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 | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 42 | #define CREATE_TRACE_POINTS |
| 43 | #include "hda_trace.h" |
| 44 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 46 | #define codec_in_pm(codec) atomic_read(&(codec)->in_pm) |
| 47 | #define hda_codec_is_power_on(codec) \ |
| 48 | (!pm_runtime_suspended(hda_codec_dev(codec))) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 49 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 50 | #define codec_in_pm(codec) 0 |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 51 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 54 | /** |
| 55 | * snd_hda_get_jack_location - Give a location string of the jack |
| 56 | * @cfg: pin default config value |
| 57 | * |
| 58 | * Parse the pin default config value and returns the string of the |
| 59 | * jack location, e.g. "Rear", "Front", etc. |
| 60 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 61 | const char *snd_hda_get_jack_location(u32 cfg) |
| 62 | { |
| 63 | static char *bases[7] = { |
| 64 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 65 | }; |
| 66 | static unsigned char specials_idx[] = { |
| 67 | 0x07, 0x08, |
| 68 | 0x17, 0x18, 0x19, |
| 69 | 0x37, 0x38 |
| 70 | }; |
| 71 | static char *specials[] = { |
| 72 | "Rear Panel", "Drive Bar", |
| 73 | "Riser", "HDMI", "ATAPI", |
| 74 | "Mobile-In", "Mobile-Out" |
| 75 | }; |
| 76 | int i; |
| 77 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 78 | if ((cfg & 0x0f) < 7) |
| 79 | return bases[cfg & 0x0f]; |
| 80 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 81 | if (cfg == specials_idx[i]) |
| 82 | return specials[i]; |
| 83 | } |
| 84 | return "UNKNOWN"; |
| 85 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 87 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 88 | /** |
| 89 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 90 | * @cfg: pin default config value |
| 91 | * |
| 92 | * Parse the pin default config value and returns the string of the |
| 93 | * jack connectivity, i.e. external or internal connection. |
| 94 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 95 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 96 | { |
| 97 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 98 | |
| 99 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 100 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 102 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 103 | /** |
| 104 | * snd_hda_get_jack_type - Give a type string of the jack |
| 105 | * @cfg: pin default config value |
| 106 | * |
| 107 | * Parse the pin default config value and returns the string of the |
| 108 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 109 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 110 | const char *snd_hda_get_jack_type(u32 cfg) |
| 111 | { |
| 112 | static char *jack_types[16] = { |
| 113 | "Line Out", "Speaker", "HP Out", "CD", |
| 114 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 115 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 116 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 120 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 121 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 123 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 124 | /* |
| 125 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 126 | */ |
| 127 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 128 | 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] | 129 | unsigned int verb, unsigned int parm) |
| 130 | { |
| 131 | u32 val; |
| 132 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 133 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 134 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 135 | codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n", |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 136 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 137 | return ~0; |
| 138 | } |
| 139 | |
| 140 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 141 | val |= (u32)nid << 20; |
| 142 | val |= verb << 8; |
| 143 | val |= parm; |
| 144 | return val; |
| 145 | } |
| 146 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 147 | /* |
| 148 | * Send and receive a verb |
| 149 | */ |
| 150 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 151 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 152 | { |
| 153 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 154 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 155 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 156 | if (cmd == ~0) |
| 157 | return -1; |
| 158 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 159 | if (res) |
| 160 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 161 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 162 | snd_hda_power_up(codec); |
| 163 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 164 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 165 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 166 | for (;;) { |
| 167 | trace_hda_send_cmd(codec, cmd); |
| 168 | err = bus->ops.command(bus, cmd); |
| 169 | if (err != -EAGAIN) |
| 170 | break; |
| 171 | /* process pending verbs */ |
| 172 | bus->ops.get_response(bus, codec->addr); |
| 173 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 174 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 175 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 176 | trace_hda_get_response(codec, *res); |
| 177 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 178 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 179 | mutex_unlock(&bus->cmd_mutex); |
| 180 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 181 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 182 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 183 | codec_dbg(codec, |
| 184 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 185 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 186 | bus->ops.bus_reset(bus); |
| 187 | } |
| 188 | goto again; |
| 189 | } |
| 190 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 191 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 192 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 193 | return err; |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /** |
| 197 | * snd_hda_codec_read - send a command and get the response |
| 198 | * @codec: the HDA codec |
| 199 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 200 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * @verb: the verb to send |
| 202 | * @parm: the parameter for the verb |
| 203 | * |
| 204 | * Send a single command and read the corresponding response. |
| 205 | * |
| 206 | * Returns the obtained response value, or -1 for an error. |
| 207 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 208 | 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] | 209 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned int verb, unsigned int parm) |
| 211 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 212 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 213 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 214 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 215 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return res; |
| 217 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * snd_hda_codec_write - send a single command without waiting for response |
| 222 | * @codec: the HDA codec |
| 223 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 224 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * @verb: the verb to send |
| 226 | * @parm: the parameter for the verb |
| 227 | * |
| 228 | * Send a single command without waiting for response. |
| 229 | * |
| 230 | * Returns 0 if successful, or a negative error code. |
| 231 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 232 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 233 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 235 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 236 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 237 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 238 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * snd_hda_sequence_write - sequence writes |
| 244 | * @codec: the HDA codec |
| 245 | * @seq: VERB array to send |
| 246 | * |
| 247 | * Send the commands sequentially from the given array. |
| 248 | * The array must be terminated with NID=0. |
| 249 | */ |
| 250 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 251 | { |
| 252 | for (; seq->nid; seq++) |
| 253 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 259 | * @codec: the HDA codec |
| 260 | * @nid: NID to parse |
| 261 | * @start_id: the pointer to store the start NID |
| 262 | * |
| 263 | * Parse the NID and store the start NID of its sub-nodes. |
| 264 | * Returns the number of sub-nodes. |
| 265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 266 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 267 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | unsigned int parm; |
| 270 | |
| 271 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 272 | if (parm == -1) { |
| 273 | *start_id = 0; |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 274 | return 0; |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | *start_id = (parm >> 16) & 0x7fff; |
| 277 | return (int)(parm & 0x7fff); |
| 278 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 281 | /* connection list element */ |
| 282 | struct hda_conn_list { |
| 283 | struct list_head list; |
| 284 | int len; |
| 285 | hda_nid_t nid; |
| 286 | hda_nid_t conns[0]; |
| 287 | }; |
| 288 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 289 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 290 | static struct hda_conn_list * |
| 291 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 292 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 293 | struct hda_conn_list *p; |
| 294 | list_for_each_entry(p, &codec->conn_list, list) { |
| 295 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 296 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 297 | } |
| 298 | return NULL; |
| 299 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 300 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 301 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 302 | const hda_nid_t *list) |
| 303 | { |
| 304 | struct hda_conn_list *p; |
| 305 | |
| 306 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 307 | if (!p) |
| 308 | return -ENOMEM; |
| 309 | p->len = len; |
| 310 | p->nid = nid; |
| 311 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 312 | list_add(&p->list, &codec->conn_list); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void remove_conn_list(struct hda_codec *codec) |
| 317 | { |
| 318 | while (!list_empty(&codec->conn_list)) { |
| 319 | struct hda_conn_list *p; |
| 320 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 321 | list_del(&p->list); |
| 322 | kfree(p); |
| 323 | } |
| 324 | } |
| 325 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 326 | /* read the connection and add to the cache */ |
| 327 | 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] | 328 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 329 | hda_nid_t list[32]; |
| 330 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 331 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 332 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 333 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 334 | if (len == -ENOSPC) { |
| 335 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 336 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 337 | if (!result) |
| 338 | return -ENOMEM; |
| 339 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 340 | } |
| 341 | if (len >= 0) |
| 342 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 343 | if (result != list) |
| 344 | kfree(result); |
| 345 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 346 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 347 | |
| 348 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | * snd_hda_get_conn_list - get connection list |
| 350 | * @codec: the HDA codec |
| 351 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 352 | * @listp: the pointer to store NID list |
| 353 | * |
| 354 | * Parses the connection list of the given widget and stores the pointer |
| 355 | * to the list of NIDs. |
| 356 | * |
| 357 | * Returns the number of connections, or a negative error code. |
| 358 | * |
| 359 | * Note that the returned pointer isn't protected against the list |
| 360 | * modification. If snd_hda_override_conn_list() might be called |
| 361 | * concurrently, protect with a mutex appropriately. |
| 362 | */ |
| 363 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 364 | const hda_nid_t **listp) |
| 365 | { |
| 366 | bool added = false; |
| 367 | |
| 368 | for (;;) { |
| 369 | int err; |
| 370 | const struct hda_conn_list *p; |
| 371 | |
| 372 | /* if the connection-list is already cached, read it */ |
| 373 | p = lookup_conn_list(codec, nid); |
| 374 | if (p) { |
| 375 | if (listp) |
| 376 | *listp = p->conns; |
| 377 | return p->len; |
| 378 | } |
| 379 | if (snd_BUG_ON(added)) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | err = read_and_add_raw_conns(codec, nid); |
| 383 | if (err < 0) |
| 384 | return err; |
| 385 | added = true; |
| 386 | } |
| 387 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 388 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 389 | |
| 390 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 391 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | * @codec: the HDA codec |
| 393 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * @max_conns: max. number of connections to store |
| 396 | * |
| 397 | * Parses the connection list of the given widget and stores the list |
| 398 | * of NIDs. |
| 399 | * |
| 400 | * Returns the number of connections, or a negative error code. |
| 401 | */ |
| 402 | 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] | 403 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 404 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 405 | const hda_nid_t *list; |
| 406 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 407 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 408 | if (len > 0 && conn_list) { |
| 409 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 410 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 411 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 412 | return -EINVAL; |
| 413 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 414 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 415 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 416 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 417 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 418 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 419 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 420 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 421 | /* return CONNLIST_LEN parameter of the given widget */ |
| 422 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 423 | { |
| 424 | unsigned int wcaps = get_wcaps(codec, nid); |
| 425 | unsigned int parm; |
| 426 | |
| 427 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 428 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 429 | return 0; |
| 430 | |
| 431 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 432 | if (parm == -1) |
| 433 | parm = 0; |
| 434 | return parm; |
| 435 | } |
| 436 | |
| 437 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 438 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 439 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 442 | /** |
| 443 | * snd_hda_get_raw_connections - copy connection list without cache |
| 444 | * @codec: the HDA codec |
| 445 | * @nid: NID to parse |
| 446 | * @conn_list: connection list array |
| 447 | * @max_conns: max. number of connections to store |
| 448 | * |
| 449 | * Like snd_hda_get_connections(), copy the connection list but without |
| 450 | * checking through the connection-list cache. |
| 451 | * Currently called only from hda_proc.c, so not exported. |
| 452 | */ |
| 453 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 454 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 457 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 459 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 460 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 462 | parm = get_num_conns(codec, nid); |
| 463 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 464 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (parm & AC_CLIST_LONG) { |
| 467 | /* long form */ |
| 468 | shift = 16; |
| 469 | num_elems = 2; |
| 470 | } else { |
| 471 | /* short form */ |
| 472 | shift = 8; |
| 473 | num_elems = 4; |
| 474 | } |
| 475 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | mask = (1 << (shift-1)) - 1; |
| 477 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 478 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return 0; /* no connection */ |
| 480 | |
| 481 | if (conn_len == 1) { |
| 482 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 483 | parm = snd_hda_codec_read(codec, nid, 0, |
| 484 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 485 | if (parm == -1 && codec->bus->rirb_error) |
| 486 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 487 | if (conn_list) |
| 488 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | /* multi connection */ |
| 493 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 494 | prev_nid = 0; |
| 495 | for (i = 0; i < conn_len; i++) { |
| 496 | int range_val; |
| 497 | hda_nid_t val, n; |
| 498 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 499 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 500 | parm = snd_hda_codec_read(codec, nid, 0, |
| 501 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 502 | if (parm == -1 && codec->bus->rirb_error) |
| 503 | return -EIO; |
| 504 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 505 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 506 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 507 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 508 | codec_dbg(codec, |
| 509 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 510 | nid, i, parm); |
| 511 | return 0; |
| 512 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 513 | parm >>= shift; |
| 514 | if (range_val) { |
| 515 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 516 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 517 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 518 | "invalid dep_range_val %x:%x\n", |
| 519 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 520 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 522 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 523 | if (conn_list) { |
| 524 | if (conns >= max_conns) |
| 525 | return -ENOSPC; |
| 526 | conn_list[conns] = n; |
| 527 | } |
| 528 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 529 | } |
| 530 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 531 | if (conn_list) { |
| 532 | if (conns >= max_conns) |
| 533 | return -ENOSPC; |
| 534 | conn_list[conns] = val; |
| 535 | } |
| 536 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 538 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | return conns; |
| 541 | } |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 544 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 545 | * @codec: the HDA codec |
| 546 | * @nid: NID to parse |
| 547 | * @len: number of connection list entries |
| 548 | * @list: the list of connection entries |
| 549 | * |
| 550 | * Add or modify the given connection-list to the cache. If the corresponding |
| 551 | * cache already exists, invalidate it and append a new one. |
| 552 | * |
| 553 | * Returns zero or a negative error code. |
| 554 | */ |
| 555 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 556 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 558 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 559 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 560 | p = lookup_conn_list(codec, nid); |
| 561 | if (p) { |
| 562 | list_del(&p->list); |
| 563 | kfree(p); |
| 564 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 565 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 566 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 569 | |
| 570 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 571 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 572 | * @codec: the HDA codec |
| 573 | * @mux: NID containing the list |
| 574 | * @nid: NID to select |
| 575 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 576 | * |
| 577 | * Parses the connection list of the widget @mux and checks whether the |
| 578 | * widget @nid is present. If it is, return the connection index. |
| 579 | * Otherwise it returns -1. |
| 580 | */ |
| 581 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 582 | hda_nid_t nid, int recursive) |
| 583 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 584 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 585 | int i, nums; |
| 586 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 587 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 588 | for (i = 0; i < nums; i++) |
| 589 | if (conn[i] == nid) |
| 590 | return i; |
| 591 | if (!recursive) |
| 592 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 593 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 594 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 598 | for (i = 0; i < nums; i++) { |
| 599 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 600 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 601 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 602 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 603 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 604 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 605 | return -1; |
| 606 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 609 | |
| 610 | /* return DEVLIST_LEN parameter of the given widget */ |
| 611 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 612 | { |
| 613 | unsigned int wcaps = get_wcaps(codec, nid); |
| 614 | unsigned int parm; |
| 615 | |
| 616 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 617 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 618 | return 0; |
| 619 | |
| 620 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 621 | if (parm == -1 && codec->bus->rirb_error) |
| 622 | parm = 0; |
| 623 | return parm & AC_DEV_LIST_LEN_MASK; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * snd_hda_get_devices - copy device list without cache |
| 628 | * @codec: the HDA codec |
| 629 | * @nid: NID of the pin to parse |
| 630 | * @dev_list: device list array |
| 631 | * @max_devices: max. number of devices to store |
| 632 | * |
| 633 | * Copy the device list. This info is dynamic and so not cached. |
| 634 | * Currently called only from hda_proc.c, so not exported. |
| 635 | */ |
| 636 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 637 | u8 *dev_list, int max_devices) |
| 638 | { |
| 639 | unsigned int parm; |
| 640 | int i, dev_len, devices; |
| 641 | |
| 642 | parm = get_num_devices(codec, nid); |
| 643 | if (!parm) /* not multi-stream capable */ |
| 644 | return 0; |
| 645 | |
| 646 | dev_len = parm + 1; |
| 647 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 648 | |
| 649 | devices = 0; |
| 650 | while (devices < dev_len) { |
| 651 | parm = snd_hda_codec_read(codec, nid, 0, |
| 652 | AC_VERB_GET_DEVICE_LIST, devices); |
| 653 | if (parm == -1 && codec->bus->rirb_error) |
| 654 | break; |
| 655 | |
| 656 | for (i = 0; i < 8; i++) { |
| 657 | dev_list[devices] = (u8)parm; |
| 658 | parm >>= 4; |
| 659 | devices++; |
| 660 | if (devices >= dev_len) |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | return devices; |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | /** |
| 668 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 669 | * @bus: the BUS |
| 670 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 671 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 672 | * |
| 673 | * Adds the given event to the queue. The events are processed in |
| 674 | * the workqueue asynchronously. Call this function in the interrupt |
| 675 | * hanlder when RIRB receives an unsolicited event. |
| 676 | * |
| 677 | * Returns 0 if successful, or a negative error code. |
| 678 | */ |
| 679 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 680 | { |
| 681 | struct hda_bus_unsolicited *unsol; |
| 682 | unsigned int wp; |
| 683 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 684 | if (!bus || !bus->workq) |
| 685 | return 0; |
| 686 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 687 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 688 | unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 690 | unsol->wp = wp; |
| 691 | |
| 692 | wp <<= 1; |
| 693 | unsol->queue[wp] = res; |
| 694 | unsol->queue[wp + 1] = res_ex; |
| 695 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 696 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | return 0; |
| 699 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 700 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 703 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 705 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 707 | struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work); |
| 708 | struct hda_bus_unsolicited *unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | struct hda_codec *codec; |
| 710 | unsigned int rp, caddr, res; |
| 711 | |
| 712 | while (unsol->rp != unsol->wp) { |
| 713 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 714 | unsol->rp = rp; |
| 715 | rp <<= 1; |
| 716 | res = unsol->queue[rp]; |
| 717 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 718 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | continue; |
| 720 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 721 | if (codec && codec->patch_ops.unsol_event) |
| 722 | codec->patch_ops.unsol_event(codec, res); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * destructor |
| 728 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 729 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 731 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 732 | return; |
| 733 | |
| 734 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 735 | if (bus->workq) |
| 736 | flush_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (bus->ops.private_free) |
| 738 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 739 | if (bus->workq) |
| 740 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 745 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 747 | snd_hda_bus_free(device->device_data); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 752 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 754 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 755 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
| 759 | * snd_hda_bus_new - create a HDA bus |
| 760 | * @card: the card entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | * @busp: the pointer to store the created bus instance |
| 762 | * |
| 763 | * Returns 0 if successful, or a negative error code. |
| 764 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 765 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | ef74497 | 2015-02-17 13:56:29 +0100 | [diff] [blame] | 766 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | struct hda_bus *bus; |
| 769 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 770 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 771 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | .dev_free = snd_hda_bus_dev_free, |
| 773 | }; |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (busp) |
| 776 | *busp = NULL; |
| 777 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 778 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 780 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return -ENOMEM; |
| 782 | } |
| 783 | |
| 784 | bus->card = card; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 785 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 786 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | INIT_LIST_HEAD(&bus->codec_list); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 788 | INIT_WORK(&bus->unsol.work, process_unsol_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 790 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 791 | "hd-audio%d", card->number); |
| 792 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 793 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 794 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 795 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 796 | kfree(bus); |
| 797 | return -ENOMEM; |
| 798 | } |
| 799 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 800 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 801 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | snd_hda_bus_free(bus); |
| 803 | return err; |
| 804 | } |
| 805 | if (busp) |
| 806 | *busp = bus; |
| 807 | return 0; |
| 808 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 809 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 812 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 814 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 816 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | hda_nid_t nid; |
| 818 | |
| 819 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 820 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 821 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 822 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 823 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 824 | case AC_GRP_AUDIO_FUNCTION: |
| 825 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 826 | codec->afg_function_id = function_id & 0xff; |
| 827 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 828 | break; |
| 829 | case AC_GRP_MODEM_FUNCTION: |
| 830 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 831 | codec->mfg_function_id = function_id & 0xff; |
| 832 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 833 | break; |
| 834 | default: |
| 835 | break; |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 841 | * read widget caps for each widget and store in cache |
| 842 | */ |
| 843 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 844 | { |
| 845 | int i; |
| 846 | hda_nid_t nid; |
| 847 | |
| 848 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 849 | &codec->start_nid); |
| 850 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 851 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 852 | return -ENOMEM; |
| 853 | nid = codec->start_nid; |
| 854 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 855 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 856 | AC_PAR_AUDIO_WIDGET_CAP); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 860 | /* read all pin default configurations and save codec->init_pins */ |
| 861 | static int read_pin_defaults(struct hda_codec *codec) |
| 862 | { |
| 863 | int i; |
| 864 | hda_nid_t nid = codec->start_nid; |
| 865 | |
| 866 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 867 | struct hda_pincfg *pin; |
| 868 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 869 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 870 | if (wid_type != AC_WID_PIN) |
| 871 | continue; |
| 872 | pin = snd_array_new(&codec->init_pins); |
| 873 | if (!pin) |
| 874 | return -ENOMEM; |
| 875 | pin->nid = nid; |
| 876 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 877 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 878 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 879 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 880 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 881 | } |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* look up the given pin config list and return the item matching with NID */ |
| 886 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 887 | struct snd_array *array, |
| 888 | hda_nid_t nid) |
| 889 | { |
| 890 | int i; |
| 891 | for (i = 0; i < array->used; i++) { |
| 892 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 893 | if (pin->nid == nid) |
| 894 | return pin; |
| 895 | } |
| 896 | return NULL; |
| 897 | } |
| 898 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 899 | /* set the current pin config value for the given NID. |
| 900 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 901 | */ |
| 902 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 903 | hda_nid_t nid, unsigned int cfg) |
| 904 | { |
| 905 | struct hda_pincfg *pin; |
| 906 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 907 | /* the check below may be invalid when pins are added by a fixup |
| 908 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 909 | * for now |
| 910 | */ |
| 911 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 912 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 913 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 914 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 915 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 916 | pin = look_up_pincfg(codec, list, nid); |
| 917 | if (!pin) { |
| 918 | pin = snd_array_new(list); |
| 919 | if (!pin) |
| 920 | return -ENOMEM; |
| 921 | pin->nid = nid; |
| 922 | } |
| 923 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 927 | /** |
| 928 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 929 | * @codec: the HDA codec |
| 930 | * @nid: NID to set the pin config |
| 931 | * @cfg: the pin default config value |
| 932 | * |
| 933 | * Override a pin default configuration value in the cache. |
| 934 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 935 | * priority than the real hardware value. |
| 936 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 937 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 938 | hda_nid_t nid, unsigned int cfg) |
| 939 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 940 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 941 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 942 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 943 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 944 | /** |
| 945 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 946 | * @codec: the HDA codec |
| 947 | * @nid: NID to get the pin config |
| 948 | * |
| 949 | * Get the current pin config value of the given pin NID. |
| 950 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 951 | * returns the cached value. |
| 952 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 953 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 954 | { |
| 955 | struct hda_pincfg *pin; |
| 956 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 957 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 958 | { |
| 959 | unsigned int cfg = 0; |
| 960 | mutex_lock(&codec->user_mutex); |
| 961 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 962 | if (pin) |
| 963 | cfg = pin->cfg; |
| 964 | mutex_unlock(&codec->user_mutex); |
| 965 | if (cfg) |
| 966 | return cfg; |
| 967 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 968 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 969 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 970 | if (pin) |
| 971 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 972 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 973 | if (pin) |
| 974 | return pin->cfg; |
| 975 | return 0; |
| 976 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 977 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 978 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 979 | /** |
| 980 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 981 | * @codec: the HDA codec |
| 982 | * @nid: pin NID |
| 983 | * @val: assigned pinctl value |
| 984 | * |
| 985 | * This function stores the given value to a pinctl target value in the |
| 986 | * pincfg table. This isn't always as same as the actually written value |
| 987 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 988 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 989 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 990 | unsigned int val) |
| 991 | { |
| 992 | struct hda_pincfg *pin; |
| 993 | |
| 994 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 995 | if (!pin) |
| 996 | return -EINVAL; |
| 997 | pin->target = val; |
| 998 | return 0; |
| 999 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1000 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1001 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1004 | * @codec: the HDA codec |
| 1005 | * @nid: pin NID |
| 1006 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1007 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1008 | { |
| 1009 | struct hda_pincfg *pin; |
| 1010 | |
| 1011 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1012 | if (!pin) |
| 1013 | return 0; |
| 1014 | return pin->target; |
| 1015 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1017 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1018 | /** |
| 1019 | * snd_hda_shutup_pins - Shut up all pins |
| 1020 | * @codec: the HDA codec |
| 1021 | * |
| 1022 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1023 | * The controls aren't cached so that they can be resumed properly. |
| 1024 | */ |
| 1025 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1026 | { |
| 1027 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1028 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1029 | * the default pin setup at the next load of the driver |
| 1030 | */ |
| 1031 | if (codec->bus->shutdown) |
| 1032 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1033 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1034 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1035 | /* use read here for syncing after issuing each verb */ |
| 1036 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1037 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1038 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1039 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1040 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1042 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1043 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1044 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1045 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1046 | { |
| 1047 | int i; |
| 1048 | if (!codec->pins_shutup) |
| 1049 | return; |
| 1050 | if (codec->bus->shutdown) |
| 1051 | return; |
| 1052 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1053 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1054 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1055 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1056 | pin->ctrl); |
| 1057 | } |
| 1058 | codec->pins_shutup = 0; |
| 1059 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1060 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1061 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1062 | static void hda_jackpoll_work(struct work_struct *work) |
| 1063 | { |
| 1064 | struct hda_codec *codec = |
| 1065 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1066 | |
| 1067 | snd_hda_jack_set_dirty_all(codec); |
| 1068 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1069 | |
| 1070 | if (!codec->jackpoll_interval) |
| 1071 | return; |
| 1072 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1073 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1074 | codec->jackpoll_interval); |
| 1075 | } |
| 1076 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1077 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1078 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1079 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1080 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1081 | /* release all pincfg lists */ |
| 1082 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1083 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1084 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1085 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1086 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1087 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1088 | snd_array_free(&codec->init_pins); |
| 1089 | } |
| 1090 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1091 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1092 | * audio-converter setup caches |
| 1093 | */ |
| 1094 | struct hda_cvt_setup { |
| 1095 | hda_nid_t nid; |
| 1096 | u8 stream_tag; |
| 1097 | u8 channel_id; |
| 1098 | u16 format_id; |
| 1099 | unsigned char active; /* cvt is currently used */ |
| 1100 | unsigned char dirty; /* setups should be cleared */ |
| 1101 | }; |
| 1102 | |
| 1103 | /* get or create a cache entry for the given audio converter NID */ |
| 1104 | static struct hda_cvt_setup * |
| 1105 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1106 | { |
| 1107 | struct hda_cvt_setup *p; |
| 1108 | int i; |
| 1109 | |
| 1110 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1111 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1112 | if (p->nid == nid) |
| 1113 | return p; |
| 1114 | } |
| 1115 | p = snd_array_new(&codec->cvt_setups); |
| 1116 | if (p) |
| 1117 | p->nid = nid; |
| 1118 | return p; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | * codec destructor |
| 1123 | */ |
| 1124 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1125 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1126 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1128 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1129 | if (device_is_registered(hda_codec_dev(codec))) |
| 1130 | device_del(hda_codec_dev(codec)); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1131 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1132 | free_init_pincfgs(codec); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1133 | flush_workqueue(codec->bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1135 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1136 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1137 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1138 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1139 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | codec->bus->caddr_tbl[codec->addr] = NULL; |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1141 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1142 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1143 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1144 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1145 | kfree(codec->vendor_name); |
| 1146 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1147 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1148 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1149 | codec->bus->num_codecs--; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1150 | put_device(hda_codec_dev(codec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1153 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1154 | hda_nid_t fg, unsigned int power_state); |
| 1155 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1156 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1157 | unsigned int power_state); |
| 1158 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1159 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1160 | { |
| 1161 | struct hda_codec *codec = device->device_data; |
| 1162 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1163 | snd_hda_register_beep_device(codec); |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 1164 | if (device_is_registered(hda_codec_dev(codec))) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1165 | pm_runtime_enable(hda_codec_dev(codec)); |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame^] | 1166 | /* it was powered up in snd_hda_codec_new(), now all done */ |
| 1167 | snd_hda_power_down(codec); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1168 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1172 | { |
| 1173 | struct hda_codec *codec = device->device_data; |
| 1174 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1175 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1179 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1180 | { |
| 1181 | snd_hda_codec_free(device->device_data); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1185 | /* just free the container */ |
| 1186 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1187 | { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1188 | kfree(dev_to_hda_codec(dev)); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /** |
| 1192 | * snd_hda_codec_new - create a HDA codec |
| 1193 | * @bus: the bus to assign |
| 1194 | * @codec_addr: the codec address |
| 1195 | * @codecp: the pointer to store the generated codec |
| 1196 | * |
| 1197 | * Returns 0 if successful, or a negative error code. |
| 1198 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1199 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1200 | unsigned int codec_addr, |
| 1201 | 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 | |
| 1219 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1220 | dev_err(bus->card->dev, |
| 1221 | "address 0x%x is already occupied\n", |
| 1222 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | return -EBUSY; |
| 1224 | } |
| 1225 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1226 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1228 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | return -ENOMEM; |
| 1230 | } |
| 1231 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1232 | dev = hda_codec_dev(codec); |
| 1233 | device_initialize(dev); |
| 1234 | dev->parent = bus->card->dev; |
| 1235 | dev->bus = &snd_hda_bus_type; |
| 1236 | dev->release = snd_hda_codec_dev_release; |
| 1237 | dev->groups = snd_hda_dev_attr_groups; |
| 1238 | dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr); |
| 1239 | dev_set_drvdata(dev, codec); /* for sysfs */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 1240 | device_enable_async_suspend(dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | codec->bus = bus; |
| 1243 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1244 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1245 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1246 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1247 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1248 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1249 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1250 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1251 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1252 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1253 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1254 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1255 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1256 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1257 | INIT_LIST_HEAD(&codec->conn_list); |
| 1258 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1259 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1260 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1261 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1263 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1264 | /* 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^] | 1265 | * it's powered down later in snd_hda_codec_dev_register(). |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1266 | */ |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1267 | set_bit(codec->addr, &bus->codec_powered); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1268 | pm_runtime_set_active(hda_codec_dev(codec)); |
| 1269 | pm_runtime_get_noresume(hda_codec_dev(codec)); |
| 1270 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1271 | #endif |
| 1272 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1273 | snd_hda_sysfs_init(codec); |
| 1274 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1275 | if (codec->bus->modelname) { |
| 1276 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1277 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1278 | err = -ENODEV; |
| 1279 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1284 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | bus->caddr_tbl[codec_addr] = codec; |
| 1287 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1288 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1289 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1290 | if (codec->vendor_id == -1) |
| 1291 | /* read again, hopefully the access method was corrected |
| 1292 | * in the last read... |
| 1293 | */ |
| 1294 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1295 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1296 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1297 | AC_PAR_SUBSYSTEM_ID); |
| 1298 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1299 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1301 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1302 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1303 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1304 | err = -ENODEV; |
| 1305 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1308 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1309 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1310 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1311 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1312 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1313 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1314 | err = read_pin_defaults(codec); |
| 1315 | if (err < 0) |
| 1316 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1317 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1318 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1319 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1320 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1321 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1324 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1325 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1326 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1327 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1328 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1329 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1330 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1331 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1332 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1333 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1334 | snd_hda_codec_proc_new(codec); |
| 1335 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1336 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1337 | |
| 1338 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1339 | codec->subsystem_id, codec->revision_id); |
| 1340 | snd_component_add(codec->bus->card, component); |
| 1341 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1342 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1343 | if (err < 0) |
| 1344 | goto error; |
| 1345 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1346 | if (codecp) |
| 1347 | *codecp = codec; |
| 1348 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1349 | |
| 1350 | error: |
| 1351 | snd_hda_codec_free(codec); |
| 1352 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1353 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1354 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1355 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1356 | /** |
| 1357 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1358 | * @codec: the HDA codec |
| 1359 | * |
| 1360 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1361 | * the given codec. |
| 1362 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1363 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1364 | { |
| 1365 | hda_nid_t fg; |
| 1366 | int err; |
| 1367 | |
| 1368 | /* Assume the function group node does not change, |
| 1369 | * only the widget nodes may change. |
| 1370 | */ |
| 1371 | kfree(codec->wcaps); |
| 1372 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1373 | err = read_widget_caps(codec, fg); |
| 1374 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1375 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1376 | return err; |
| 1377 | } |
| 1378 | |
| 1379 | snd_array_free(&codec->init_pins); |
| 1380 | err = read_pin_defaults(codec); |
| 1381 | |
| 1382 | return err; |
| 1383 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1384 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1385 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1386 | /* update the stream-id if changed */ |
| 1387 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1388 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1389 | u32 stream_tag, int channel_id) |
| 1390 | { |
| 1391 | unsigned int oldval, newval; |
| 1392 | |
| 1393 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1394 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1395 | newval = (stream_tag << 4) | channel_id; |
| 1396 | if (oldval != newval) |
| 1397 | snd_hda_codec_write(codec, nid, 0, |
| 1398 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1399 | newval); |
| 1400 | p->stream_tag = stream_tag; |
| 1401 | p->channel_id = channel_id; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* update the format-id if changed */ |
| 1406 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1407 | hda_nid_t nid, int format) |
| 1408 | { |
| 1409 | unsigned int oldval; |
| 1410 | |
| 1411 | if (p->format_id != format) { |
| 1412 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1413 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1414 | if (oldval != format) { |
| 1415 | msleep(1); |
| 1416 | snd_hda_codec_write(codec, nid, 0, |
| 1417 | AC_VERB_SET_STREAM_FORMAT, |
| 1418 | format); |
| 1419 | } |
| 1420 | p->format_id = format; |
| 1421 | } |
| 1422 | } |
| 1423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | /** |
| 1425 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1426 | * @codec: the CODEC to set up |
| 1427 | * @nid: the NID to set up |
| 1428 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1429 | * @channel_id: channel id to pass, zero based. |
| 1430 | * @format: stream format. |
| 1431 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1432 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1433 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | int channel_id, int format) |
| 1435 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1436 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1437 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1438 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1439 | int i; |
| 1440 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1441 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1442 | return; |
| 1443 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1444 | codec_dbg(codec, |
| 1445 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1446 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1447 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1448 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1449 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1450 | |
| 1451 | if (codec->pcm_format_first) |
| 1452 | update_pcm_format(codec, p, nid, format); |
| 1453 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1454 | if (!codec->pcm_format_first) |
| 1455 | update_pcm_format(codec, p, nid, format); |
| 1456 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1457 | p->active = 1; |
| 1458 | p->dirty = 0; |
| 1459 | |
| 1460 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1461 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1462 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1463 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1464 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1465 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1466 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1467 | p->dirty = 1; |
| 1468 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1471 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1473 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1474 | struct hda_cvt_setup *q); |
| 1475 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1476 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1477 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1478 | * @codec: the CODEC to clean up |
| 1479 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1480 | * @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] | 1481 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1482 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1483 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1484 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1485 | struct hda_cvt_setup *p; |
| 1486 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1487 | if (!nid) |
| 1488 | return; |
| 1489 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1490 | if (codec->no_sticky_stream) |
| 1491 | do_now = 1; |
| 1492 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1493 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1494 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1495 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1496 | /* here we just clear the active flag when do_now isn't set; |
| 1497 | * actual clean-ups will be done later in |
| 1498 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1499 | */ |
| 1500 | if (do_now) |
| 1501 | really_cleanup_stream(codec, p); |
| 1502 | else |
| 1503 | p->active = 0; |
| 1504 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1505 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1506 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1507 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1508 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1509 | struct hda_cvt_setup *q) |
| 1510 | { |
| 1511 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1512 | if (q->stream_tag || q->channel_id) |
| 1513 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1514 | if (q->format_id) |
| 1515 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1516 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1517 | memset(q, 0, sizeof(*q)); |
| 1518 | q->nid = nid; |
| 1519 | } |
| 1520 | |
| 1521 | /* clean up the all conflicting obsolete streams */ |
| 1522 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1523 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1524 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1525 | int i; |
| 1526 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1527 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1528 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1529 | struct hda_cvt_setup *p; |
| 1530 | p = snd_array_elem(&c->cvt_setups, i); |
| 1531 | if (p->dirty) |
| 1532 | really_cleanup_stream(c, p); |
| 1533 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1537 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1538 | /* clean up all streams; called from suspend */ |
| 1539 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1540 | { |
| 1541 | int i; |
| 1542 | |
| 1543 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1544 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1545 | if (p->stream_tag) |
| 1546 | really_cleanup_stream(codec, p); |
| 1547 | } |
| 1548 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1549 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* |
| 1552 | * amp access functions |
| 1553 | */ |
| 1554 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1555 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1556 | #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] | 1557 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1558 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1559 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1561 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
| 1563 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1564 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1565 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1567 | memset(cache, 0, sizeof(*cache)); |
| 1568 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1569 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1572 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1573 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1574 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1578 | 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] | 1579 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1580 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1581 | u16 cur = cache->hash[idx]; |
| 1582 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1585 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | if (info->key == key) |
| 1587 | return info; |
| 1588 | cur = info->next; |
| 1589 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1590 | return NULL; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1593 | /* query the hash. allocate an entry if not found. */ |
| 1594 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1595 | u32 key) |
| 1596 | { |
| 1597 | struct hda_cache_head *info = get_hash(cache, key); |
| 1598 | if (!info) { |
| 1599 | u16 idx, cur; |
| 1600 | /* add a new hash entry */ |
| 1601 | info = snd_array_new(&cache->buf); |
| 1602 | if (!info) |
| 1603 | return NULL; |
| 1604 | cur = snd_array_index(&cache->buf, info); |
| 1605 | info->key = key; |
| 1606 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1607 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1608 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1609 | info->next = cache->hash[idx]; |
| 1610 | cache->hash[idx] = cur; |
| 1611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | return info; |
| 1613 | } |
| 1614 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1615 | /* query and allocate an amp hash entry */ |
| 1616 | static inline struct hda_amp_info * |
| 1617 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1618 | { |
| 1619 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1620 | } |
| 1621 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1622 | /* overwrite the value with the key in the caps hash */ |
| 1623 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1624 | { |
| 1625 | struct hda_amp_info *info; |
| 1626 | |
| 1627 | mutex_lock(&codec->hash_mutex); |
| 1628 | info = get_alloc_amp_hash(codec, key); |
| 1629 | if (!info) { |
| 1630 | mutex_unlock(&codec->hash_mutex); |
| 1631 | return -EINVAL; |
| 1632 | } |
| 1633 | info->amp_caps = val; |
| 1634 | info->head.val |= INFO_AMP_CAPS; |
| 1635 | mutex_unlock(&codec->hash_mutex); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* query the value from the caps hash; if not found, fetch the current |
| 1640 | * value from the given function and store in the hash |
| 1641 | */ |
| 1642 | static unsigned int |
| 1643 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1644 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1645 | { |
| 1646 | struct hda_amp_info *info; |
| 1647 | unsigned int val; |
| 1648 | |
| 1649 | mutex_lock(&codec->hash_mutex); |
| 1650 | info = get_alloc_amp_hash(codec, key); |
| 1651 | if (!info) { |
| 1652 | mutex_unlock(&codec->hash_mutex); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1656 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1657 | val = func(codec, nid, dir); |
| 1658 | write_caps_hash(codec, key, val); |
| 1659 | } else { |
| 1660 | val = info->amp_caps; |
| 1661 | mutex_unlock(&codec->hash_mutex); |
| 1662 | } |
| 1663 | return val; |
| 1664 | } |
| 1665 | |
| 1666 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1667 | int direction) |
| 1668 | { |
| 1669 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1670 | nid = codec->afg; |
| 1671 | return snd_hda_param_read(codec, nid, |
| 1672 | direction == HDA_OUTPUT ? |
| 1673 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1674 | } |
| 1675 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1676 | /** |
| 1677 | * query_amp_caps - query AMP capabilities |
| 1678 | * @codec: the HD-auio codec |
| 1679 | * @nid: the NID to query |
| 1680 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1681 | * |
| 1682 | * Query AMP capabilities for the given widget and direction. |
| 1683 | * Returns the obtained capability bits. |
| 1684 | * |
| 1685 | * When cap bits have been already read, this doesn't read again but |
| 1686 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1688 | 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] | 1689 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1690 | return query_caps_hash(codec, nid, direction, |
| 1691 | HDA_HASH_KEY(nid, direction, 0), |
| 1692 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1694 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1696 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1697 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1698 | * @codec: the HD-audio codec |
| 1699 | * @nid: the NID to query |
| 1700 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1701 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1702 | * |
| 1703 | * Check whether the widget has the given amp capability for the direction. |
| 1704 | */ |
| 1705 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1706 | int dir, unsigned int bits) |
| 1707 | { |
| 1708 | if (!nid) |
| 1709 | return false; |
| 1710 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 1711 | if (query_amp_caps(codec, nid, dir) & bits) |
| 1712 | return true; |
| 1713 | return false; |
| 1714 | } |
| 1715 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 1716 | |
| 1717 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1718 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1719 | * @codec: the CODEC to clean up |
| 1720 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1721 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1722 | * @caps: the capability bits to set |
| 1723 | * |
| 1724 | * Override the cached AMP caps bits value by the given one. |
| 1725 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1726 | * e.g. limit to 0dB, etc. |
| 1727 | * |
| 1728 | * Returns zero if successful or a negative error code. |
| 1729 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1730 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1731 | unsigned int caps) |
| 1732 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1733 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1734 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1735 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1736 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1737 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1738 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1739 | { |
| 1740 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1741 | } |
| 1742 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1743 | /** |
| 1744 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1745 | * @codec: the HD-auio codec |
| 1746 | * @nid: the NID to query |
| 1747 | * |
| 1748 | * Query PIN capabilities for the given widget. |
| 1749 | * Returns the obtained capability bits. |
| 1750 | * |
| 1751 | * When cap bits have been already read, this doesn't read again but |
| 1752 | * returns the cached value. |
| 1753 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1754 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1755 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1756 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1757 | read_pin_cap); |
| 1758 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1759 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1760 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1761 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1762 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1763 | * @codec: the CODEC |
| 1764 | * @nid: the NID to override |
| 1765 | * @caps: the capability bits to set |
| 1766 | * |
| 1767 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1768 | * |
| 1769 | * Returns zero if successful or a negative error code. |
| 1770 | */ |
| 1771 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1772 | unsigned int caps) |
| 1773 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1774 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1776 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1777 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1778 | /* read or sync the hash value with the current value; |
| 1779 | * call within hash_mutex |
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 | static struct hda_amp_info * |
| 1782 | 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] | 1783 | int direction, int index, bool init_only) |
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 | struct hda_amp_info *info; |
| 1786 | unsigned int parm, val = 0; |
| 1787 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1789 | retry: |
| 1790 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1791 | if (!info) |
| 1792 | return NULL; |
| 1793 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1794 | if (!val_read) { |
| 1795 | mutex_unlock(&codec->hash_mutex); |
| 1796 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1797 | parm |= direction == HDA_OUTPUT ? |
| 1798 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1799 | parm |= index; |
| 1800 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1801 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1802 | val &= 0xff; |
| 1803 | val_read = true; |
| 1804 | mutex_lock(&codec->hash_mutex); |
| 1805 | goto retry; |
| 1806 | } |
| 1807 | info->vol[ch] = val; |
| 1808 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1809 | } else if (init_only) |
| 1810 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1811 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1815 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1817 | 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] | 1818 | hda_nid_t nid, int ch, int direction, int index, |
| 1819 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | { |
| 1821 | u32 parm; |
| 1822 | |
| 1823 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1824 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1825 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1826 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1827 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1828 | ; /* set the zero value as a fake mute */ |
| 1829 | else |
| 1830 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1832 | } |
| 1833 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1834 | /** |
| 1835 | * snd_hda_codec_amp_read - Read AMP value |
| 1836 | * @codec: HD-audio codec |
| 1837 | * @nid: NID to read the AMP value |
| 1838 | * @ch: channel (left=0 or right=1) |
| 1839 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1840 | * @index: the index value (only for input direction) |
| 1841 | * |
| 1842 | * 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] | 1843 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1844 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1845 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1847 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1848 | unsigned int val = 0; |
| 1849 | |
| 1850 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1851 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1852 | if (info) |
| 1853 | val = info->vol[ch]; |
| 1854 | mutex_unlock(&codec->hash_mutex); |
| 1855 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1857 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1859 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1860 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1861 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1862 | { |
| 1863 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1864 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1865 | |
| 1866 | if (snd_BUG_ON(mask & ~0xff)) |
| 1867 | mask &= 0xff; |
| 1868 | val &= mask; |
| 1869 | |
| 1870 | mutex_lock(&codec->hash_mutex); |
| 1871 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1872 | if (!info) { |
| 1873 | mutex_unlock(&codec->hash_mutex); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | val |= info->vol[ch] & ~mask; |
| 1877 | if (info->vol[ch] == val) { |
| 1878 | mutex_unlock(&codec->hash_mutex); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1882 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1883 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1884 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1885 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1886 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1887 | return 1; |
| 1888 | } |
| 1889 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1890 | /** |
| 1891 | * snd_hda_codec_amp_update - update the AMP value |
| 1892 | * @codec: HD-audio codec |
| 1893 | * @nid: NID to read the AMP value |
| 1894 | * @ch: channel (left=0 or right=1) |
| 1895 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1896 | * @idx: the index value (only for input direction) |
| 1897 | * @mask: bit mask to set |
| 1898 | * @val: the bits value to set |
| 1899 | * |
| 1900 | * Update the AMP value with a bit mask. |
| 1901 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1902 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1903 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1904 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1906 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 1907 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1909 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1911 | /** |
| 1912 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1913 | * @codec: HD-audio codec |
| 1914 | * @nid: NID to read the AMP value |
| 1915 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1916 | * @idx: the index value (only for input direction) |
| 1917 | * @mask: bit mask to set |
| 1918 | * @val: the bits value to set |
| 1919 | * |
| 1920 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1921 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1922 | */ |
| 1923 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1924 | int direction, int idx, int mask, int val) |
| 1925 | { |
| 1926 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1927 | |
| 1928 | if (snd_BUG_ON(mask & ~0xff)) |
| 1929 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1930 | for (ch = 0; ch < 2; ch++) |
| 1931 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1932 | idx, mask, val); |
| 1933 | return ret; |
| 1934 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1935 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1936 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1937 | /** |
| 1938 | * snd_hda_codec_amp_init - initialize the AMP value |
| 1939 | * @codec: the HDA codec |
| 1940 | * @nid: NID to read the AMP value |
| 1941 | * @ch: channel (left=0 or right=1) |
| 1942 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1943 | * @idx: the index value (only for input direction) |
| 1944 | * @mask: bit mask to set |
| 1945 | * @val: the bits value to set |
| 1946 | * |
| 1947 | * 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] | 1948 | * the first access. If the amp was already initialized / updated beforehand, |
| 1949 | * this does nothing. |
| 1950 | */ |
| 1951 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1952 | int dir, int idx, int mask, int val) |
| 1953 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1954 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 1955 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1956 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1957 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1958 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1959 | /** |
| 1960 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 1961 | * @codec: the HDA codec |
| 1962 | * @nid: NID to read the AMP value |
| 1963 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1964 | * @idx: the index value (only for input direction) |
| 1965 | * @mask: bit mask to set |
| 1966 | * @val: the bits value to set |
| 1967 | * |
| 1968 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 1969 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1970 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1971 | int dir, int idx, int mask, int val) |
| 1972 | { |
| 1973 | int ch, ret = 0; |
| 1974 | |
| 1975 | if (snd_BUG_ON(mask & ~0xff)) |
| 1976 | mask &= 0xff; |
| 1977 | for (ch = 0; ch < 2; ch++) |
| 1978 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 1979 | idx, mask, val); |
| 1980 | return ret; |
| 1981 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1982 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1983 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1984 | /** |
| 1985 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1986 | * @codec: HD-audio codec |
| 1987 | * |
| 1988 | * Resume the all amp commands from the cache. |
| 1989 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1990 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1991 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1992 | int i; |
| 1993 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1994 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 1995 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1996 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 1997 | struct hda_amp_info *buffer; |
| 1998 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1999 | hda_nid_t nid; |
| 2000 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2001 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2002 | |
| 2003 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2004 | if (!buffer->head.dirty) |
| 2005 | continue; |
| 2006 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2007 | info = *buffer; |
| 2008 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2009 | if (!key) |
| 2010 | continue; |
| 2011 | nid = key & 0xff; |
| 2012 | idx = (key >> 16) & 0xff; |
| 2013 | dir = (key >> 24) & 0xff; |
| 2014 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2015 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2016 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2017 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2018 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2019 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2020 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2021 | } |
| 2022 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2023 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2024 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2025 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2027 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2028 | unsigned int ofs) |
| 2029 | { |
| 2030 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2031 | /* get num steps */ |
| 2032 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2033 | if (ofs < caps) |
| 2034 | caps -= ofs; |
| 2035 | return caps; |
| 2036 | } |
| 2037 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2038 | /** |
| 2039 | * 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] | 2040 | * @kcontrol: referred ctl element |
| 2041 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2042 | * |
| 2043 | * The control element is supposed to have the private_value field |
| 2044 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2045 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2046 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2047 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | { |
| 2049 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2050 | u16 nid = get_amp_nid(kcontrol); |
| 2051 | u8 chs = get_amp_channels(kcontrol); |
| 2052 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2053 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2055 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2056 | uinfo->count = chs == 3 ? 2 : 1; |
| 2057 | uinfo->value.integer.min = 0; |
| 2058 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2059 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2060 | codec_warn(codec, |
| 2061 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2062 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | return -EINVAL; |
| 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | return 0; |
| 2066 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2067 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2069 | |
| 2070 | static inline unsigned int |
| 2071 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2072 | int ch, int dir, int idx, unsigned int ofs) |
| 2073 | { |
| 2074 | unsigned int val; |
| 2075 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2076 | val &= HDA_AMP_VOLMASK; |
| 2077 | if (val >= ofs) |
| 2078 | val -= ofs; |
| 2079 | else |
| 2080 | val = 0; |
| 2081 | return val; |
| 2082 | } |
| 2083 | |
| 2084 | static inline int |
| 2085 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2086 | int ch, int dir, int idx, unsigned int ofs, |
| 2087 | unsigned int val) |
| 2088 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2089 | unsigned int maxval; |
| 2090 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2091 | if (val > 0) |
| 2092 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2093 | /* ofs = 0: raw max value */ |
| 2094 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2095 | if (val > maxval) |
| 2096 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2097 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2098 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2099 | } |
| 2100 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2101 | /** |
| 2102 | * 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] | 2103 | * @kcontrol: ctl element |
| 2104 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2105 | * |
| 2106 | * The control element is supposed to have the private_value field |
| 2107 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2108 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2109 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2110 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | { |
| 2112 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2113 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2114 | int chs = get_amp_channels(kcontrol); |
| 2115 | int dir = get_amp_direction(kcontrol); |
| 2116 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2117 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | long *valp = ucontrol->value.integer.value; |
| 2119 | |
| 2120 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2121 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2123 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2126 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2128 | /** |
| 2129 | * 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] | 2130 | * @kcontrol: ctl element |
| 2131 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2132 | * |
| 2133 | * The control element is supposed to have the private_value field |
| 2134 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2135 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2136 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2137 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
| 2139 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2140 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2141 | int chs = get_amp_channels(kcontrol); |
| 2142 | int dir = get_amp_direction(kcontrol); |
| 2143 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2144 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | long *valp = ucontrol->value.integer.value; |
| 2146 | int change = 0; |
| 2147 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2148 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2149 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2150 | valp++; |
| 2151 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2152 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2153 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | return change; |
| 2155 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2156 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2158 | /** |
| 2159 | * 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] | 2160 | * @kcontrol: ctl element |
| 2161 | * @op_flag: operation flag |
| 2162 | * @size: byte size of input TLV |
| 2163 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2164 | * |
| 2165 | * The control element is supposed to have the private_value field |
| 2166 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2167 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2168 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2169 | unsigned int size, unsigned int __user *_tlv) |
| 2170 | { |
| 2171 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2172 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2173 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2174 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2175 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2176 | u32 caps, val1, val2; |
| 2177 | |
| 2178 | if (size < 4 * sizeof(unsigned int)) |
| 2179 | return -ENOMEM; |
| 2180 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2181 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2182 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2183 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2184 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2185 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2186 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2187 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2188 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2189 | return -EFAULT; |
| 2190 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2191 | return -EFAULT; |
| 2192 | if (put_user(val1, _tlv + 2)) |
| 2193 | return -EFAULT; |
| 2194 | if (put_user(val2, _tlv + 3)) |
| 2195 | return -EFAULT; |
| 2196 | return 0; |
| 2197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2198 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2199 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2200 | /** |
| 2201 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2202 | * @codec: HD-audio codec |
| 2203 | * @nid: NID of a reference widget |
| 2204 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2205 | * @tlv: TLV data to be stored, at least 4 elements |
| 2206 | * |
| 2207 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2208 | * obtained from the reference NID. |
| 2209 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2210 | */ |
| 2211 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2212 | unsigned int *tlv) |
| 2213 | { |
| 2214 | u32 caps; |
| 2215 | int nums, step; |
| 2216 | |
| 2217 | caps = query_amp_caps(codec, nid, dir); |
| 2218 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2219 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2220 | step = (step + 1) * 25; |
| 2221 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2222 | tlv[1] = 2 * sizeof(unsigned int); |
| 2223 | tlv[2] = -nums * step; |
| 2224 | tlv[3] = step; |
| 2225 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2226 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2227 | |
| 2228 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2229 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2230 | 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] | 2231 | { |
| 2232 | struct snd_ctl_elem_id id; |
| 2233 | memset(&id, 0, sizeof(id)); |
| 2234 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2235 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2236 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2237 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2238 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2239 | strcpy(id.name, name); |
| 2240 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2241 | } |
| 2242 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2243 | /** |
| 2244 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2245 | * @codec: HD-audio codec |
| 2246 | * @name: ctl id name string |
| 2247 | * |
| 2248 | * Get the control element with the given id string and IFACE_MIXER. |
| 2249 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2250 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2251 | const char *name) |
| 2252 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2253 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2255 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2256 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2257 | 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] | 2258 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2259 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2260 | int i, idx; |
| 2261 | /* 16 ctlrs should be large enough */ |
| 2262 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2263 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2264 | return idx; |
| 2265 | } |
| 2266 | return -EBUSY; |
| 2267 | } |
| 2268 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2269 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2270 | * 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] | 2271 | * @codec: HD-audio codec |
| 2272 | * @nid: corresponding NID (optional) |
| 2273 | * @kctl: the control element to assign |
| 2274 | * |
| 2275 | * Add the given control element to an array inside the codec instance. |
| 2276 | * All control elements belonging to a codec are supposed to be added |
| 2277 | * by this function so that a proper clean-up works at the free or |
| 2278 | * reconfiguration time. |
| 2279 | * |
| 2280 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2281 | * The assignment is shown in the codec proc file. |
| 2282 | * |
| 2283 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2284 | * #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] | 2285 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2286 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2287 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2288 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2289 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2290 | { |
| 2291 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2292 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2293 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2294 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2295 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2296 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2297 | if (nid == 0) |
| 2298 | nid = get_amp_nid_(kctl->private_value); |
| 2299 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2300 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2301 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2302 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2303 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2304 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2305 | if (err < 0) |
| 2306 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2307 | item = snd_array_new(&codec->mixers); |
| 2308 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2309 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2310 | item->kctl = kctl; |
| 2311 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2312 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2315 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2316 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2317 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2318 | * snd_hda_add_nid - Assign a NID to a control element |
| 2319 | * @codec: HD-audio codec |
| 2320 | * @nid: corresponding NID (optional) |
| 2321 | * @kctl: the control element to assign |
| 2322 | * @index: index to kctl |
| 2323 | * |
| 2324 | * Add the given control element to an array inside the codec instance. |
| 2325 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2326 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2327 | */ |
| 2328 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2329 | unsigned int index, hda_nid_t nid) |
| 2330 | { |
| 2331 | struct hda_nid_item *item; |
| 2332 | |
| 2333 | if (nid > 0) { |
| 2334 | item = snd_array_new(&codec->nids); |
| 2335 | if (!item) |
| 2336 | return -ENOMEM; |
| 2337 | item->kctl = kctl; |
| 2338 | item->index = index; |
| 2339 | item->nid = nid; |
| 2340 | return 0; |
| 2341 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2342 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2343 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2344 | return -EINVAL; |
| 2345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2346 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2347 | |
| 2348 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2349 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2350 | * @codec: HD-audio codec |
| 2351 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2352 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2353 | { |
| 2354 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2355 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2356 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2357 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2358 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2359 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2362 | /** |
| 2363 | * snd_hda_lock_devices - pseudo device locking |
| 2364 | * @bus: the BUS |
| 2365 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2366 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2367 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2368 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2369 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2370 | struct snd_card *card = bus->card; |
| 2371 | struct hda_codec *codec; |
| 2372 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2373 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2374 | if (card->shutdown) |
| 2375 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2376 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2377 | if (!list_empty(&card->ctl_files)) |
| 2378 | goto err_clear; |
| 2379 | |
| 2380 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2381 | int pcm; |
| 2382 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2383 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2384 | if (!cpcm->pcm) |
| 2385 | continue; |
| 2386 | if (cpcm->pcm->streams[0].substream_opened || |
| 2387 | cpcm->pcm->streams[1].substream_opened) |
| 2388 | goto err_clear; |
| 2389 | } |
| 2390 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2391 | spin_unlock(&card->files_lock); |
| 2392 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2393 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2394 | err_clear: |
| 2395 | card->shutdown = 0; |
| 2396 | err_unlock: |
| 2397 | spin_unlock(&card->files_lock); |
| 2398 | return -EINVAL; |
| 2399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2400 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2401 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2402 | /** |
| 2403 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2404 | * @bus: the BUS |
| 2405 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2406 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2407 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2408 | struct snd_card *card = bus->card; |
| 2409 | |
| 2410 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2411 | spin_lock(&card->files_lock); |
| 2412 | card->shutdown = 0; |
| 2413 | spin_unlock(&card->files_lock); |
| 2414 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2415 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2416 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2417 | /** |
| 2418 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2419 | * @codec: HD-audio codec |
| 2420 | * |
| 2421 | * This frees the all PCM and control elements assigned to the codec, and |
| 2422 | * clears the caches and restores the pin default configurations. |
| 2423 | * |
| 2424 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2425 | * returns zero. |
| 2426 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2427 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2428 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2429 | struct hda_bus *bus = codec->bus; |
| 2430 | struct snd_card *card = bus->card; |
| 2431 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2432 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2433 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2434 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2435 | |
| 2436 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2437 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2438 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2439 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2440 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2441 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2442 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2443 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2444 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2445 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2446 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2447 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2448 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 2449 | if (device_is_registered(hda_codec_dev(codec))) |
| 2450 | device_del(hda_codec_dev(codec)); |
| 2451 | |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2452 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2453 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2454 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2455 | codec->spec = NULL; |
| 2456 | free_hda_cache(&codec->amp_cache); |
| 2457 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2458 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2459 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2460 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2461 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2462 | snd_array_free(&codec->cvt_setups); |
| 2463 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2464 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2465 | codec->num_pcms = 0; |
| 2466 | codec->pcm_info = NULL; |
| 2467 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2468 | codec->slave_dig_outs = NULL; |
| 2469 | codec->spdif_status_reset = 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2472 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2473 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2476 | 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] | 2477 | |
| 2478 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2479 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2480 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2481 | { |
| 2482 | struct hda_nid_item *items; |
| 2483 | const char * const *s; |
| 2484 | int i, err; |
| 2485 | |
| 2486 | items = codec->mixers.list; |
| 2487 | for (i = 0; i < codec->mixers.used; i++) { |
| 2488 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2489 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2490 | continue; |
| 2491 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2492 | char tmpname[sizeof(sctl->id.name)]; |
| 2493 | const char *name = *s; |
| 2494 | if (suffix) { |
| 2495 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2496 | name, suffix); |
| 2497 | name = tmpname; |
| 2498 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2499 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2500 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2501 | if (err) |
| 2502 | return err; |
| 2503 | break; |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2510 | static int check_slave_present(struct hda_codec *codec, |
| 2511 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2512 | { |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2516 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2517 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2518 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2519 | { |
| 2520 | int _tlv[4]; |
| 2521 | const int *tlv = NULL; |
| 2522 | int val = -1; |
| 2523 | |
| 2524 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2525 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2526 | mm_segment_t fs = get_fs(); |
| 2527 | set_fs(get_ds()); |
| 2528 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2529 | tlv = _tlv; |
| 2530 | set_fs(fs); |
| 2531 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2532 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2533 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2534 | int step = tlv[3]; |
| 2535 | step &= ~TLV_DB_SCALE_MUTE; |
| 2536 | if (!step) |
| 2537 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2538 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2539 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2540 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2541 | return -1; |
| 2542 | } |
| 2543 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2544 | val = -tlv[2] / step; |
| 2545 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2546 | return val; |
| 2547 | } |
| 2548 | |
| 2549 | /* call kctl->put with the given value(s) */ |
| 2550 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2551 | { |
| 2552 | struct snd_ctl_elem_value *ucontrol; |
| 2553 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2554 | if (!ucontrol) |
| 2555 | return -ENOMEM; |
| 2556 | ucontrol->value.integer.value[0] = val; |
| 2557 | ucontrol->value.integer.value[1] = val; |
| 2558 | kctl->put(kctl, ucontrol); |
| 2559 | kfree(ucontrol); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
| 2563 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2564 | static int init_slave_0dB(struct hda_codec *codec, |
| 2565 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2566 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2567 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2568 | if (offset > 0) |
| 2569 | put_kctl_with_value(slave, offset); |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2574 | static int init_slave_unmute(struct hda_codec *codec, |
| 2575 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2576 | { |
| 2577 | return put_kctl_with_value(slave, 1); |
| 2578 | } |
| 2579 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2580 | static int add_slave(struct hda_codec *codec, |
| 2581 | void *data, struct snd_kcontrol *slave) |
| 2582 | { |
| 2583 | return snd_ctl_add_slave(data, slave); |
| 2584 | } |
| 2585 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2586 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2587 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2588 | * @codec: HD-audio codec |
| 2589 | * @name: vmaster control name |
| 2590 | * @tlv: TLV data (optional) |
| 2591 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2592 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2593 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2594 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2595 | * |
| 2596 | * Create a virtual master control with the given name. The TLV data |
| 2597 | * must be either NULL or a valid data. |
| 2598 | * |
| 2599 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2600 | * slave control name. All controls with these names are assigned to |
| 2601 | * the new virtual master control. |
| 2602 | * |
| 2603 | * This function returns zero if successful or a negative error code. |
| 2604 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2605 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2606 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2607 | const char *suffix, bool init_slave_vol, |
| 2608 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2609 | { |
| 2610 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2611 | int err; |
| 2612 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2613 | if (ctl_ret) |
| 2614 | *ctl_ret = NULL; |
| 2615 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2616 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2617 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2618 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2619 | return 0; |
| 2620 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2621 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2622 | if (!kctl) |
| 2623 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2624 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2625 | if (err < 0) |
| 2626 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2627 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2628 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2629 | if (err < 0) |
| 2630 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2631 | |
| 2632 | /* init with master mute & zero volume */ |
| 2633 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2634 | if (init_slave_vol) { |
| 2635 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2636 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2637 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2638 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2639 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2640 | if (ctl_ret) |
| 2641 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2642 | return 0; |
| 2643 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2644 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2645 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2646 | /* |
| 2647 | * mute-LED control using vmaster |
| 2648 | */ |
| 2649 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2650 | struct snd_ctl_elem_info *uinfo) |
| 2651 | { |
| 2652 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2653 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2654 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2655 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2656 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2660 | struct snd_ctl_elem_value *ucontrol) |
| 2661 | { |
| 2662 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2663 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2668 | struct snd_ctl_elem_value *ucontrol) |
| 2669 | { |
| 2670 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2671 | unsigned int old_mode = hook->mute_mode; |
| 2672 | |
| 2673 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2674 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2675 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2676 | if (old_mode == hook->mute_mode) |
| 2677 | return 0; |
| 2678 | snd_hda_sync_vmaster_hook(hook); |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
| 2682 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2683 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2684 | .name = "Mute-LED Mode", |
| 2685 | .info = vmaster_mute_mode_info, |
| 2686 | .get = vmaster_mute_mode_get, |
| 2687 | .put = vmaster_mute_mode_put, |
| 2688 | }; |
| 2689 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2690 | /** |
| 2691 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2692 | * @codec: the HDA codec |
| 2693 | * @hook: the vmaster hook object |
| 2694 | * @expose_enum_ctl: flag to create an enum ctl |
| 2695 | * |
| 2696 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2697 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2698 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2699 | */ |
| 2700 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2701 | struct hda_vmaster_mute_hook *hook, |
| 2702 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2703 | { |
| 2704 | struct snd_kcontrol *kctl; |
| 2705 | |
| 2706 | if (!hook->hook || !hook->sw_kctl) |
| 2707 | return 0; |
| 2708 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2709 | hook->codec = codec; |
| 2710 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2711 | if (!expose_enum_ctl) |
| 2712 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2713 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2714 | if (!kctl) |
| 2715 | return -ENOMEM; |
| 2716 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2717 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2718 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2719 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2720 | /** |
| 2721 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 2722 | * @hook: the vmaster hook |
| 2723 | * |
| 2724 | * Call the hook with the current value for synchronization. |
| 2725 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2726 | */ |
| 2727 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2728 | { |
| 2729 | if (!hook->hook || !hook->codec) |
| 2730 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2731 | /* don't call vmaster hook in the destructor since it might have |
| 2732 | * been already destroyed |
| 2733 | */ |
| 2734 | if (hook->codec->bus->shutdown) |
| 2735 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2736 | switch (hook->mute_mode) { |
| 2737 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2738 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2739 | break; |
| 2740 | default: |
| 2741 | hook->hook(hook->codec, hook->mute_mode); |
| 2742 | break; |
| 2743 | } |
| 2744 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2745 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2746 | |
| 2747 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2748 | /** |
| 2749 | * 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] | 2750 | * @kcontrol: referred ctl element |
| 2751 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2752 | * |
| 2753 | * The control element is supposed to have the private_value field |
| 2754 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2755 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2756 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2757 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | { |
| 2759 | int chs = get_amp_channels(kcontrol); |
| 2760 | |
| 2761 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2762 | uinfo->count = chs == 3 ? 2 : 1; |
| 2763 | uinfo->value.integer.min = 0; |
| 2764 | uinfo->value.integer.max = 1; |
| 2765 | return 0; |
| 2766 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2767 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2769 | /** |
| 2770 | * 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] | 2771 | * @kcontrol: ctl element |
| 2772 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2773 | * |
| 2774 | * The control element is supposed to have the private_value field |
| 2775 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2776 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2777 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2778 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | { |
| 2780 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2781 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2782 | int chs = get_amp_channels(kcontrol); |
| 2783 | int dir = get_amp_direction(kcontrol); |
| 2784 | int idx = get_amp_index(kcontrol); |
| 2785 | long *valp = ucontrol->value.integer.value; |
| 2786 | |
| 2787 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2788 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2789 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2791 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2792 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2795 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2797 | /** |
| 2798 | * 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] | 2799 | * @kcontrol: ctl element |
| 2800 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2801 | * |
| 2802 | * The control element is supposed to have the private_value field |
| 2803 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2804 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2805 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2806 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | { |
| 2808 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2809 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2810 | int chs = get_amp_channels(kcontrol); |
| 2811 | int dir = get_amp_direction(kcontrol); |
| 2812 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | long *valp = ucontrol->value.integer.value; |
| 2814 | int change = 0; |
| 2815 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2816 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2817 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 2818 | HDA_AMP_MUTE, |
| 2819 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2820 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2821 | valp++; |
| 2822 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2823 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2824 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 2825 | HDA_AMP_MUTE, |
| 2826 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2827 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2828 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | return change; |
| 2830 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2831 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2834 | * bound volume controls |
| 2835 | * |
| 2836 | * bind multiple volumes (# indices, from 0) |
| 2837 | */ |
| 2838 | |
| 2839 | #define AMP_VAL_IDX_SHIFT 19 |
| 2840 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2841 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2842 | /** |
| 2843 | * 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] | 2844 | * @kcontrol: ctl element |
| 2845 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2846 | * |
| 2847 | * The control element is supposed to have the private_value field |
| 2848 | * set up via HDA_BIND_MUTE*() macros. |
| 2849 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2850 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2851 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2852 | { |
| 2853 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2854 | unsigned long pval; |
| 2855 | int err; |
| 2856 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2857 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2858 | pval = kcontrol->private_value; |
| 2859 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2860 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2861 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2862 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2863 | return err; |
| 2864 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2865 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2866 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2867 | /** |
| 2868 | * 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] | 2869 | * @kcontrol: ctl element |
| 2870 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2871 | * |
| 2872 | * The control element is supposed to have the private_value field |
| 2873 | * set up via HDA_BIND_MUTE*() macros. |
| 2874 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2875 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2876 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2877 | { |
| 2878 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2879 | unsigned long pval; |
| 2880 | int i, indices, err = 0, change = 0; |
| 2881 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2882 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2883 | pval = kcontrol->private_value; |
| 2884 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2885 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2886 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2887 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2888 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2889 | if (err < 0) |
| 2890 | break; |
| 2891 | change |= err; |
| 2892 | } |
| 2893 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2894 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2895 | return err < 0 ? err : change; |
| 2896 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2897 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2898 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2899 | /** |
| 2900 | * 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] | 2901 | * @kcontrol: referred ctl element |
| 2902 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2903 | * |
| 2904 | * The control element is supposed to have the private_value field |
| 2905 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2906 | */ |
| 2907 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2908 | struct snd_ctl_elem_info *uinfo) |
| 2909 | { |
| 2910 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2911 | struct hda_bind_ctls *c; |
| 2912 | int err; |
| 2913 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2914 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2915 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2916 | kcontrol->private_value = *c->values; |
| 2917 | err = c->ops->info(kcontrol, uinfo); |
| 2918 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2919 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2920 | return err; |
| 2921 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2922 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2923 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2924 | /** |
| 2925 | * 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] | 2926 | * @kcontrol: ctl element |
| 2927 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2928 | * |
| 2929 | * The control element is supposed to have the private_value field |
| 2930 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2931 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2932 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2933 | struct snd_ctl_elem_value *ucontrol) |
| 2934 | { |
| 2935 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2936 | struct hda_bind_ctls *c; |
| 2937 | int err; |
| 2938 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2939 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2940 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2941 | kcontrol->private_value = *c->values; |
| 2942 | err = c->ops->get(kcontrol, ucontrol); |
| 2943 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2944 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2945 | return err; |
| 2946 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2947 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2948 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2949 | /** |
| 2950 | * 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] | 2951 | * @kcontrol: ctl element |
| 2952 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2953 | * |
| 2954 | * The control element is supposed to have the private_value field |
| 2955 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2956 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2957 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2958 | struct snd_ctl_elem_value *ucontrol) |
| 2959 | { |
| 2960 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2961 | struct hda_bind_ctls *c; |
| 2962 | unsigned long *vals; |
| 2963 | int err = 0, change = 0; |
| 2964 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2965 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2966 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2967 | for (vals = c->values; *vals; vals++) { |
| 2968 | kcontrol->private_value = *vals; |
| 2969 | err = c->ops->put(kcontrol, ucontrol); |
| 2970 | if (err < 0) |
| 2971 | break; |
| 2972 | change |= err; |
| 2973 | } |
| 2974 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2975 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2976 | return err < 0 ? err : change; |
| 2977 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2978 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2979 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2980 | /** |
| 2981 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2982 | * @kcontrol: ctl element |
| 2983 | * @op_flag: operation flag |
| 2984 | * @size: byte size of input TLV |
| 2985 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2986 | * |
| 2987 | * The control element is supposed to have the private_value field |
| 2988 | * set up via HDA_BIND_VOL() macro. |
| 2989 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2990 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2991 | unsigned int size, unsigned int __user *tlv) |
| 2992 | { |
| 2993 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2994 | struct hda_bind_ctls *c; |
| 2995 | int err; |
| 2996 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2997 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2998 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2999 | kcontrol->private_value = *c->values; |
| 3000 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3001 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3002 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3003 | return err; |
| 3004 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3005 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3006 | |
| 3007 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3008 | .info = snd_hda_mixer_amp_volume_info, |
| 3009 | .get = snd_hda_mixer_amp_volume_get, |
| 3010 | .put = snd_hda_mixer_amp_volume_put, |
| 3011 | .tlv = snd_hda_mixer_amp_tlv |
| 3012 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3013 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3014 | |
| 3015 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3016 | .info = snd_hda_mixer_amp_switch_info, |
| 3017 | .get = snd_hda_mixer_amp_switch_get, |
| 3018 | .put = snd_hda_mixer_amp_switch_put, |
| 3019 | .tlv = snd_hda_mixer_amp_tlv |
| 3020 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3021 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3022 | |
| 3023 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | * SPDIF out controls |
| 3025 | */ |
| 3026 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3027 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3028 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | { |
| 3030 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3031 | uinfo->count = 1; |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3035 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3036 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | { |
| 3038 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3039 | IEC958_AES0_NONAUDIO | |
| 3040 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3041 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3042 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3043 | IEC958_AES1_CON_ORIGINAL; |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3047 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3048 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | { |
| 3050 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3051 | IEC958_AES0_NONAUDIO | |
| 3052 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3053 | return 0; |
| 3054 | } |
| 3055 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3056 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3057 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | { |
| 3059 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3060 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3061 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3063 | mutex_lock(&codec->spdif_mutex); |
| 3064 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3065 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3066 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3067 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3068 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3069 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | |
| 3071 | return 0; |
| 3072 | } |
| 3073 | |
| 3074 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3075 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3076 | */ |
| 3077 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3078 | { |
| 3079 | unsigned short val = 0; |
| 3080 | |
| 3081 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3082 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3084 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3086 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3087 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3088 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3090 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3091 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3092 | val |= AC_DIG1_EMPHASIS; |
| 3093 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3094 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3096 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3098 | } |
| 3099 | return val; |
| 3100 | } |
| 3101 | |
| 3102 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3103 | */ |
| 3104 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3105 | { |
| 3106 | unsigned int sbits = 0; |
| 3107 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3108 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3110 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3112 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3113 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3115 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3116 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3118 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3120 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3122 | sbits |= val & (0x7f << 8); |
| 3123 | } |
| 3124 | return sbits; |
| 3125 | } |
| 3126 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3127 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3128 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3129 | int verb, int val) |
| 3130 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3131 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3132 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3133 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3134 | d = codec->slave_dig_outs; |
| 3135 | if (!d) |
| 3136 | return; |
| 3137 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3138 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3142 | int dig1, int dig2) |
| 3143 | { |
| 3144 | if (dig1 != -1) |
| 3145 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3146 | if (dig2 != -1) |
| 3147 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3148 | } |
| 3149 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3150 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3151 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | { |
| 3153 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3154 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3155 | struct hda_spdif_out *spdif; |
| 3156 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | unsigned short val; |
| 3158 | int change; |
| 3159 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3160 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3161 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3162 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3163 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3165 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3166 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3167 | val = convert_from_spdif_status(spdif->status); |
| 3168 | val |= spdif->ctls & 1; |
| 3169 | change = spdif->ctls != val; |
| 3170 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3171 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3172 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3173 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | return change; |
| 3175 | } |
| 3176 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3177 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3179 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3180 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
| 3182 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3183 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3184 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3186 | mutex_lock(&codec->spdif_mutex); |
| 3187 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3188 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3189 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3193 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3194 | int dig1, int dig2) |
| 3195 | { |
| 3196 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3197 | /* unmute amp switch (if any) */ |
| 3198 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3199 | (dig1 & AC_DIG1_ENABLE)) |
| 3200 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3201 | HDA_AMP_MUTE, 0); |
| 3202 | } |
| 3203 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3204 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3205 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | { |
| 3207 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3208 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3209 | struct hda_spdif_out *spdif; |
| 3210 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | unsigned short val; |
| 3212 | int change; |
| 3213 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3214 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3215 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3216 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3217 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3219 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3220 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3221 | spdif->ctls = val; |
| 3222 | if (change && nid != (u16)-1) |
| 3223 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3224 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | return change; |
| 3226 | } |
| 3227 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3228 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | { |
| 3230 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3231 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3232 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | .info = snd_hda_spdif_mask_info, |
| 3234 | .get = snd_hda_spdif_cmask_get, |
| 3235 | }, |
| 3236 | { |
| 3237 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3238 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3239 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | .info = snd_hda_spdif_mask_info, |
| 3241 | .get = snd_hda_spdif_pmask_get, |
| 3242 | }, |
| 3243 | { |
| 3244 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3245 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | .info = snd_hda_spdif_mask_info, |
| 3247 | .get = snd_hda_spdif_default_get, |
| 3248 | .put = snd_hda_spdif_default_put, |
| 3249 | }, |
| 3250 | { |
| 3251 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3252 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | .info = snd_hda_spdif_out_switch_info, |
| 3254 | .get = snd_hda_spdif_out_switch_get, |
| 3255 | .put = snd_hda_spdif_out_switch_put, |
| 3256 | }, |
| 3257 | { } /* end */ |
| 3258 | }; |
| 3259 | |
| 3260 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3261 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3263 | * @associated_nid: NID that new ctls associated with |
| 3264 | * @cvt_nid: converter NID |
| 3265 | * @type: HDA_PCM_TYPE_* |
| 3266 | * Creates controls related with the digital output. |
| 3267 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | * |
| 3269 | * Returns 0 if successful, or a negative error code. |
| 3270 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3271 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3272 | hda_nid_t associated_nid, |
| 3273 | hda_nid_t cvt_nid, |
| 3274 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | { |
| 3276 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3277 | struct snd_kcontrol *kctl; |
| 3278 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3279 | int idx = 0; |
| 3280 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3281 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3282 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3284 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3285 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3286 | idx = spdif_index; |
| 3287 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3288 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3289 | /* suppose a single SPDIF device */ |
| 3290 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3291 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3292 | if (!kctl) |
| 3293 | break; |
| 3294 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3295 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3296 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3297 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3298 | if (!bus->primary_dig_out_type) |
| 3299 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3300 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3301 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3302 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3303 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3304 | return -EBUSY; |
| 3305 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3306 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3307 | if (!spdif) |
| 3308 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3310 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3311 | if (!kctl) |
| 3312 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3313 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3314 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3315 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3316 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | return err; |
| 3318 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3319 | spdif->nid = cvt_nid; |
| 3320 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3321 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3322 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | return 0; |
| 3324 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3325 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3327 | /** |
| 3328 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3329 | * @codec: the HDA codec |
| 3330 | * @nid: widget NID |
| 3331 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3332 | * call within spdif_mutex lock |
| 3333 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3334 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3335 | hda_nid_t nid) |
| 3336 | { |
| 3337 | int i; |
| 3338 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3339 | struct hda_spdif_out *spdif = |
| 3340 | snd_array_elem(&codec->spdif_out, i); |
| 3341 | if (spdif->nid == nid) |
| 3342 | return spdif; |
| 3343 | } |
| 3344 | return NULL; |
| 3345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3346 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3347 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3348 | /** |
| 3349 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3350 | * @codec: the HDA codec |
| 3351 | * @idx: the SPDIF ctl index |
| 3352 | * |
| 3353 | * Unassign the widget from the given SPDIF control. |
| 3354 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3355 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3356 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3357 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3358 | |
| 3359 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3360 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3361 | spdif->nid = (u16)-1; |
| 3362 | mutex_unlock(&codec->spdif_mutex); |
| 3363 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3364 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3365 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3366 | /** |
| 3367 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3368 | * @codec: the HDA codec |
| 3369 | * @idx: the SPDIF ctl idx |
| 3370 | * @nid: widget NID |
| 3371 | * |
| 3372 | * Assign the widget to the SPDIF control with the given index. |
| 3373 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3374 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3375 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3376 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3377 | unsigned short val; |
| 3378 | |
| 3379 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3380 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3381 | if (spdif->nid != nid) { |
| 3382 | spdif->nid = nid; |
| 3383 | val = spdif->ctls; |
| 3384 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3385 | } |
| 3386 | mutex_unlock(&codec->spdif_mutex); |
| 3387 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3388 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3391 | * SPDIF sharing with analog output |
| 3392 | */ |
| 3393 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3394 | struct snd_ctl_elem_value *ucontrol) |
| 3395 | { |
| 3396 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3397 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3398 | return 0; |
| 3399 | } |
| 3400 | |
| 3401 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3402 | struct snd_ctl_elem_value *ucontrol) |
| 3403 | { |
| 3404 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3405 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
| 3409 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3410 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3411 | .name = "IEC958 Default PCM Playback Switch", |
| 3412 | .info = snd_ctl_boolean_mono_info, |
| 3413 | .get = spdif_share_sw_get, |
| 3414 | .put = spdif_share_sw_put, |
| 3415 | }; |
| 3416 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3417 | /** |
| 3418 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3419 | * @codec: the HDA codec |
| 3420 | * @mout: multi-out instance |
| 3421 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3422 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3423 | struct hda_multi_out *mout) |
| 3424 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3425 | struct snd_kcontrol *kctl; |
| 3426 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3427 | if (!mout->dig_out_nid) |
| 3428 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3429 | |
| 3430 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3431 | if (!kctl) |
| 3432 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3433 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3434 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3435 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3436 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | * SPDIF input |
| 3440 | */ |
| 3441 | |
| 3442 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3443 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3444 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3445 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | { |
| 3447 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3448 | |
| 3449 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3453 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3454 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | { |
| 3456 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3457 | hda_nid_t nid = kcontrol->private_value; |
| 3458 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3459 | int change; |
| 3460 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3461 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3463 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3465 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3466 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3468 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | return change; |
| 3470 | } |
| 3471 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3472 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3473 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | { |
| 3475 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3476 | hda_nid_t nid = kcontrol->private_value; |
| 3477 | unsigned short val; |
| 3478 | unsigned int sbits; |
| 3479 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3480 | 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] | 3481 | sbits = convert_to_spdif_status(val); |
| 3482 | ucontrol->value.iec958.status[0] = sbits; |
| 3483 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3484 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3485 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3489 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | { |
| 3491 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3492 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | .info = snd_hda_spdif_in_switch_info, |
| 3494 | .get = snd_hda_spdif_in_switch_get, |
| 3495 | .put = snd_hda_spdif_in_switch_put, |
| 3496 | }, |
| 3497 | { |
| 3498 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3499 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3500 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | .info = snd_hda_spdif_mask_info, |
| 3502 | .get = snd_hda_spdif_in_status_get, |
| 3503 | }, |
| 3504 | { } /* end */ |
| 3505 | }; |
| 3506 | |
| 3507 | /** |
| 3508 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3509 | * @codec: the HDA codec |
| 3510 | * @nid: audio in widget NID |
| 3511 | * |
| 3512 | * Creates controls related with the SPDIF input. |
| 3513 | * Called from each patch supporting the SPDIF in. |
| 3514 | * |
| 3515 | * Returns 0 if successful, or a negative error code. |
| 3516 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3517 | 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] | 3518 | { |
| 3519 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3520 | struct snd_kcontrol *kctl; |
| 3521 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3522 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3524 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3525 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3526 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3527 | return -EBUSY; |
| 3528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3530 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3531 | if (!kctl) |
| 3532 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3534 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3535 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | return err; |
| 3537 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3538 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3539 | snd_hda_codec_read(codec, nid, 0, |
| 3540 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3541 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | return 0; |
| 3543 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3544 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3546 | /* |
| 3547 | * command cache |
| 3548 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3550 | /* 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] | 3551 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3552 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3553 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3554 | |
| 3555 | /** |
| 3556 | * snd_hda_codec_write_cache - send a single command with caching |
| 3557 | * @codec: the HDA codec |
| 3558 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3559 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3560 | * @verb: the verb to send |
| 3561 | * @parm: the parameter for the verb |
| 3562 | * |
| 3563 | * Send a single command without waiting for response. |
| 3564 | * |
| 3565 | * Returns 0 if successful, or a negative error code. |
| 3566 | */ |
| 3567 | 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] | 3568 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3569 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3570 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3571 | struct hda_cache_head *c; |
| 3572 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3573 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3574 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3575 | cache_only = codec->cached_write; |
| 3576 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3577 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3578 | if (err < 0) |
| 3579 | return err; |
| 3580 | } |
| 3581 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3582 | /* parm may contain the verb stuff for get/set amp */ |
| 3583 | verb = verb | (parm >> 8); |
| 3584 | parm &= 0xff; |
| 3585 | key = build_cmd_cache_key(nid, verb); |
| 3586 | mutex_lock(&codec->bus->cmd_mutex); |
| 3587 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3588 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3589 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3590 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3591 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3592 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3593 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3594 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3595 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3596 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3597 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3598 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3599 | * @codec: the HDA codec |
| 3600 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3601 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3602 | * @verb: the verb to send |
| 3603 | * @parm: the parameter for the verb |
| 3604 | * |
| 3605 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3606 | * command if the parameter is already identical with the cached value. |
| 3607 | * If not, it sends the command and refreshes the cache. |
| 3608 | * |
| 3609 | * Returns 0 if successful, or a negative error code. |
| 3610 | */ |
| 3611 | 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] | 3612 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3613 | { |
| 3614 | struct hda_cache_head *c; |
| 3615 | u32 key; |
| 3616 | |
| 3617 | /* parm may contain the verb stuff for get/set amp */ |
| 3618 | verb = verb | (parm >> 8); |
| 3619 | parm &= 0xff; |
| 3620 | key = build_cmd_cache_key(nid, verb); |
| 3621 | mutex_lock(&codec->bus->cmd_mutex); |
| 3622 | c = get_hash(&codec->cmd_cache, key); |
| 3623 | if (c && c->val == parm) { |
| 3624 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3625 | return 0; |
| 3626 | } |
| 3627 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3628 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3629 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3630 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3631 | |
| 3632 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3633 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3634 | * @codec: HD-audio codec |
| 3635 | * |
| 3636 | * Execute all verbs recorded in the command caches to resume. |
| 3637 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3638 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3639 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3640 | int i; |
| 3641 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3642 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3643 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3644 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3645 | struct hda_cache_head *buffer; |
| 3646 | u32 key; |
| 3647 | |
| 3648 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3649 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3650 | if (!key) |
| 3651 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3652 | if (!buffer->dirty) |
| 3653 | continue; |
| 3654 | buffer->dirty = 0; |
| 3655 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3656 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3657 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3658 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3659 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3660 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3661 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3662 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3663 | |
| 3664 | /** |
| 3665 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3666 | * @codec: the HDA codec |
| 3667 | * @seq: VERB array to send |
| 3668 | * |
| 3669 | * Send the commands sequentially from the given array. |
| 3670 | * Thte commands are recorded on cache for power-save and resume. |
| 3671 | * The array must be terminated with NID=0. |
| 3672 | */ |
| 3673 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3674 | const struct hda_verb *seq) |
| 3675 | { |
| 3676 | for (; seq->nid; seq++) |
| 3677 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3678 | seq->param); |
| 3679 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3680 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3681 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3682 | /** |
| 3683 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3684 | * @codec: HD-audio codec |
| 3685 | */ |
| 3686 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3687 | { |
| 3688 | snd_hda_codec_resume_amp(codec); |
| 3689 | snd_hda_codec_resume_cache(codec); |
| 3690 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3691 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3692 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3693 | /** |
| 3694 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3695 | * @codec: the HDA codec |
| 3696 | * @fg: function group (not used now) |
| 3697 | * @power_state: the power state to set (AC_PWRST_*) |
| 3698 | * |
| 3699 | * Set the given power state to all widgets that have the power control. |
| 3700 | * If the codec has power_filter set, it evaluates the power state and |
| 3701 | * filter out if it's unchanged as D3. |
| 3702 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3703 | 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] | 3704 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3705 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3706 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3707 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3708 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3709 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3710 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3711 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3712 | if (!(wcaps & AC_WCAP_POWER)) |
| 3713 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3714 | if (codec->power_filter) { |
| 3715 | state = codec->power_filter(codec, nid, power_state); |
| 3716 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3717 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3718 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3719 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3720 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3721 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3722 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3723 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3724 | |
| 3725 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3726 | * supported power states check |
| 3727 | */ |
| 3728 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3729 | unsigned int power_state) |
| 3730 | { |
| 3731 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3732 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3733 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3734 | return false; |
| 3735 | if (sup & power_state) |
| 3736 | return true; |
| 3737 | else |
| 3738 | return false; |
| 3739 | } |
| 3740 | |
| 3741 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3742 | * wait until the state is reached, returns the current state |
| 3743 | */ |
| 3744 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3745 | hda_nid_t fg, |
| 3746 | unsigned int power_state) |
| 3747 | { |
| 3748 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3749 | unsigned int state, actual_state; |
| 3750 | |
| 3751 | for (;;) { |
| 3752 | state = snd_hda_codec_read(codec, fg, 0, |
| 3753 | AC_VERB_GET_POWER_STATE, 0); |
| 3754 | if (state & AC_PWRST_ERROR) |
| 3755 | break; |
| 3756 | actual_state = (state >> 4) & 0x0f; |
| 3757 | if (actual_state == power_state) |
| 3758 | break; |
| 3759 | if (time_after_eq(jiffies, end_time)) |
| 3760 | break; |
| 3761 | /* wait until the codec reachs to the target state */ |
| 3762 | msleep(1); |
| 3763 | } |
| 3764 | return state; |
| 3765 | } |
| 3766 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3767 | /** |
| 3768 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 3769 | * @codec: the HDA codec |
| 3770 | * @nid: widget NID |
| 3771 | * @power_state: power state to evalue |
| 3772 | * |
| 3773 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 3774 | * This can be used a codec power_filter callback. |
| 3775 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3776 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3777 | hda_nid_t nid, |
| 3778 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3779 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3780 | if (nid == codec->afg || nid == codec->mfg) |
| 3781 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3782 | if (power_state == AC_PWRST_D3 && |
| 3783 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3784 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3785 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3786 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3787 | if (eapd & 0x02) |
| 3788 | return AC_PWRST_D0; |
| 3789 | } |
| 3790 | return power_state; |
| 3791 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3792 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3793 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3794 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3795 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3796 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3797 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3798 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3799 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3800 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3801 | int count; |
| 3802 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3803 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3804 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3805 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3806 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3807 | if (codec->depop_delay < 0) |
| 3808 | msleep(codec->epss ? 10 : 100); |
| 3809 | else if (codec->depop_delay > 0) |
| 3810 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3811 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3812 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3813 | |
| 3814 | /* repeat power states setting at most 10 times*/ |
| 3815 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3816 | if (codec->patch_ops.set_power_state) |
| 3817 | codec->patch_ops.set_power_state(codec, fg, |
| 3818 | power_state); |
| 3819 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3820 | state = power_state; |
| 3821 | if (codec->power_filter) |
| 3822 | state = codec->power_filter(codec, fg, state); |
| 3823 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3824 | snd_hda_codec_read(codec, fg, flags, |
| 3825 | AC_VERB_SET_POWER_STATE, |
| 3826 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3827 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3828 | } |
| 3829 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3830 | if (!(state & AC_PWRST_ERROR)) |
| 3831 | break; |
| 3832 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3833 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3834 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3835 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3836 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3837 | /* sync power states of all widgets; |
| 3838 | * this is called at the end of codec parsing |
| 3839 | */ |
| 3840 | static void sync_power_up_states(struct hda_codec *codec) |
| 3841 | { |
| 3842 | hda_nid_t nid = codec->start_nid; |
| 3843 | int i; |
| 3844 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3845 | /* don't care if no filter is used */ |
| 3846 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3847 | return; |
| 3848 | |
| 3849 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3850 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3851 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3852 | if (!(wcaps & AC_WCAP_POWER)) |
| 3853 | continue; |
| 3854 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3855 | if (target == AC_PWRST_D0) |
| 3856 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3857 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3858 | snd_hda_codec_write(codec, nid, 0, |
| 3859 | AC_VERB_SET_POWER_STATE, target); |
| 3860 | } |
| 3861 | } |
| 3862 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 3863 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3864 | /* execute additional init verbs */ |
| 3865 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3866 | { |
| 3867 | if (codec->init_verbs.list) |
| 3868 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3869 | } |
| 3870 | #else |
| 3871 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3872 | #endif |
| 3873 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3874 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3875 | /* update the power on/off account with the current jiffies */ |
| 3876 | static void update_power_acct(struct hda_codec *codec, bool on) |
| 3877 | { |
| 3878 | unsigned long delta = jiffies - codec->power_jiffies; |
| 3879 | |
| 3880 | if (on) |
| 3881 | codec->power_on_acct += delta; |
| 3882 | else |
| 3883 | codec->power_off_acct += delta; |
| 3884 | codec->power_jiffies += delta; |
| 3885 | } |
| 3886 | |
| 3887 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 3888 | { |
| 3889 | update_power_acct(codec, hda_codec_is_power_on(codec)); |
| 3890 | } |
| 3891 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3892 | /* |
| 3893 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3894 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3895 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3896 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3897 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3898 | unsigned int state; |
| 3899 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3900 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3901 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3902 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3903 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3904 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3905 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3906 | trace_hda_power_down(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3907 | update_power_acct(codec, true); |
| 3908 | atomic_dec(&codec->in_pm); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3909 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3910 | } |
| 3911 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3912 | /* mark all entries of cmd and amp caches dirty */ |
| 3913 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3914 | { |
| 3915 | int i; |
| 3916 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3917 | struct hda_cache_head *cmd; |
| 3918 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3919 | cmd->dirty = 1; |
| 3920 | } |
| 3921 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3922 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3923 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3924 | amp->head.dirty = 1; |
| 3925 | } |
| 3926 | } |
| 3927 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3928 | /* |
| 3929 | * kick up codec; used both from PM and power-save |
| 3930 | */ |
| 3931 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3932 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3933 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3934 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3935 | trace_hda_power_up(codec); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3936 | hda_mark_cmd_cache_dirty(codec); |
| 3937 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3938 | codec->power_jiffies = jiffies; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3939 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3940 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3941 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3942 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3943 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3944 | if (codec->patch_ops.resume) |
| 3945 | codec->patch_ops.resume(codec); |
| 3946 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3947 | if (codec->patch_ops.init) |
| 3948 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3949 | snd_hda_codec_resume_amp(codec); |
| 3950 | snd_hda_codec_resume_cache(codec); |
| 3951 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3952 | |
| 3953 | if (codec->jackpoll_interval) |
| 3954 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3955 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3956 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3957 | atomic_dec(&codec->in_pm); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3958 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3959 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3960 | static int hda_codec_runtime_suspend(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3961 | { |
| 3962 | struct hda_codec *codec = dev_to_hda_codec(dev); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3963 | unsigned int state; |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3964 | int i; |
| 3965 | |
| 3966 | cancel_delayed_work_sync(&codec->jackpoll_work); |
| 3967 | for (i = 0; i < codec->num_pcms; i++) |
| 3968 | snd_pcm_suspend_all(codec->pcm_info[i].pcm); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3969 | state = hda_call_codec_suspend(codec); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3970 | if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK)) |
| 3971 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3972 | return 0; |
| 3973 | } |
| 3974 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3975 | static int hda_codec_runtime_resume(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3976 | { |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3977 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 3978 | |
| 3979 | set_bit(codec->addr, &codec->bus->codec_powered); |
| 3980 | hda_call_codec_resume(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3981 | pm_runtime_mark_last_busy(dev); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3982 | return 0; |
| 3983 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3984 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3985 | |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3986 | /* referred in hda_bind.c */ |
| 3987 | const struct dev_pm_ops hda_codec_driver_pm = { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3988 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 3989 | pm_runtime_force_resume) |
| 3990 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, |
| 3991 | NULL) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3992 | }; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | /** |
| 3995 | * snd_hda_build_controls - build mixer controls |
| 3996 | * @bus: the BUS |
| 3997 | * |
| 3998 | * Creates mixer controls for each codec included in the bus. |
| 3999 | * |
| 4000 | * Returns 0 if successful, otherwise a negative error code. |
| 4001 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4002 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4004 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4006 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4007 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4008 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4009 | codec_err(codec, |
| 4010 | "cannot build controls for #%d (error %d)\n", |
| 4011 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4012 | err = snd_hda_codec_reset(codec); |
| 4013 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4014 | codec_err(codec, |
| 4015 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4016 | return err; |
| 4017 | } |
| 4018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4020 | return 0; |
| 4021 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4022 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4024 | /* |
| 4025 | * add standard channel maps if not specified |
| 4026 | */ |
| 4027 | static int add_std_chmaps(struct hda_codec *codec) |
| 4028 | { |
| 4029 | int i, str, err; |
| 4030 | |
| 4031 | for (i = 0; i < codec->num_pcms; i++) { |
| 4032 | for (str = 0; str < 2; str++) { |
| 4033 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4034 | struct hda_pcm_stream *hinfo = |
| 4035 | &codec->pcm_info[i].stream[str]; |
| 4036 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4037 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4038 | |
| 4039 | if (codec->pcm_info[i].own_chmap) |
| 4040 | continue; |
| 4041 | if (!pcm || !hinfo->substreams) |
| 4042 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4043 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4044 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4045 | hinfo->channels_max, |
| 4046 | 0, &chmap); |
| 4047 | if (err < 0) |
| 4048 | return err; |
| 4049 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4050 | } |
| 4051 | } |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4055 | /* default channel maps for 2.1 speakers; |
| 4056 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4057 | */ |
| 4058 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4059 | { .channels = 2, |
| 4060 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4061 | { .channels = 4, |
| 4062 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4063 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4064 | { } |
| 4065 | }; |
| 4066 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4067 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4068 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4069 | { |
| 4070 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4071 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4072 | /* continue to initialize... */ |
| 4073 | if (codec->patch_ops.init) |
| 4074 | err = codec->patch_ops.init(codec); |
| 4075 | if (!err && codec->patch_ops.build_controls) |
| 4076 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4077 | if (err < 0) |
| 4078 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4079 | |
| 4080 | /* we create chmaps here instead of build_pcms */ |
| 4081 | err = add_std_chmaps(codec); |
| 4082 | if (err < 0) |
| 4083 | return err; |
| 4084 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4085 | if (codec->jackpoll_interval) |
| 4086 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4087 | else |
| 4088 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4089 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | return 0; |
| 4091 | } |
| 4092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | /* |
| 4094 | * stream formats |
| 4095 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4096 | struct hda_rate_tbl { |
| 4097 | unsigned int hz; |
| 4098 | unsigned int alsa_bits; |
| 4099 | unsigned int hda_fmt; |
| 4100 | }; |
| 4101 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4102 | /* rate = base * mult / div */ |
| 4103 | #define HDA_RATE(base, mult, div) \ |
| 4104 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4105 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4106 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4107 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4109 | |
| 4110 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4111 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4112 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4113 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4114 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4115 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4116 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4117 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4118 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4119 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4120 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4121 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4122 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4123 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4124 | |
| 4125 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4126 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4127 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4128 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | }; |
| 4130 | |
| 4131 | /** |
| 4132 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4133 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | * @rate: the sample rate |
| 4135 | * @channels: the number of channels |
| 4136 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4137 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4138 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | * |
| 4140 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4141 | * |
| 4142 | * Return zero if invalid. |
| 4143 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4144 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4145 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | unsigned int channels, |
| 4147 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4148 | unsigned int maxbps, |
| 4149 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | { |
| 4151 | int i; |
| 4152 | unsigned int val = 0; |
| 4153 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4154 | for (i = 0; rate_bits[i].hz; i++) |
| 4155 | if (rate_bits[i].hz == rate) { |
| 4156 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | break; |
| 4158 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4159 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4160 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4165 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | return 0; |
| 4167 | } |
| 4168 | val |= channels - 1; |
| 4169 | |
| 4170 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4171 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4172 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4173 | break; |
| 4174 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4175 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4176 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | case 20: |
| 4178 | case 24: |
| 4179 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4180 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4181 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4183 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4185 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | break; |
| 4187 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4188 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4189 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | return 0; |
| 4191 | } |
| 4192 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4193 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4194 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | return val; |
| 4197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4198 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4200 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4201 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4202 | { |
| 4203 | unsigned int val = 0; |
| 4204 | if (nid != codec->afg && |
| 4205 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4206 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4207 | if (!val || val == -1) |
| 4208 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4209 | if (!val || val == -1) |
| 4210 | return 0; |
| 4211 | return val; |
| 4212 | } |
| 4213 | |
| 4214 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4215 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4216 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4217 | get_pcm_param); |
| 4218 | } |
| 4219 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4220 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4221 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4222 | { |
| 4223 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4224 | if (!streams || streams == -1) |
| 4225 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4226 | if (!streams || streams == -1) |
| 4227 | return 0; |
| 4228 | return streams; |
| 4229 | } |
| 4230 | |
| 4231 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4232 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4233 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4234 | get_stream_param); |
| 4235 | } |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | /** |
| 4238 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4239 | * @codec: the HDA codec |
| 4240 | * @nid: NID to query |
| 4241 | * @ratesp: the pointer to store the detected rate bitflags |
| 4242 | * @formatsp: the pointer to store the detected formats |
| 4243 | * @bpsp: the pointer to store the detected format widths |
| 4244 | * |
| 4245 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4246 | * or @bsps argument is ignored. |
| 4247 | * |
| 4248 | * Returns 0 if successful, otherwise a negative error code. |
| 4249 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4250 | 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] | 4251 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4252 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4253 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4255 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4256 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
| 4258 | if (ratesp) { |
| 4259 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4260 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4262 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4264 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4265 | codec_err(codec, |
| 4266 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4267 | nid, val, |
| 4268 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4269 | return -EIO; |
| 4270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | *ratesp = rates; |
| 4272 | } |
| 4273 | |
| 4274 | if (formatsp || bpsp) { |
| 4275 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4276 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4278 | streams = query_stream_param(codec, nid); |
| 4279 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | |
| 4282 | bps = 0; |
| 4283 | if (streams & AC_SUPFMT_PCM) { |
| 4284 | if (val & AC_SUPPCM_BITS_8) { |
| 4285 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4286 | bps = 8; |
| 4287 | } |
| 4288 | if (val & AC_SUPPCM_BITS_16) { |
| 4289 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4290 | bps = 16; |
| 4291 | } |
| 4292 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4293 | if (val & AC_SUPPCM_BITS_32) |
| 4294 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4295 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4296 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4297 | if (val & AC_SUPPCM_BITS_24) |
| 4298 | bps = 24; |
| 4299 | else if (val & AC_SUPPCM_BITS_20) |
| 4300 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4301 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4302 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4304 | if (val & AC_SUPPCM_BITS_32) |
| 4305 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | else if (val & AC_SUPPCM_BITS_24) |
| 4307 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4308 | else if (val & AC_SUPPCM_BITS_20) |
| 4309 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | } |
| 4311 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4312 | #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] | 4313 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4315 | if (!bps) |
| 4316 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4317 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4318 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4319 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4320 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | /* temporary hack: we have still no proper support |
| 4322 | * for the direct AC3 stream... |
| 4323 | */ |
| 4324 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4325 | bps = 8; |
| 4326 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4327 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4328 | codec_err(codec, |
| 4329 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4330 | nid, val, |
| 4331 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4332 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4333 | return -EIO; |
| 4334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | if (formatsp) |
| 4336 | *formatsp = formats; |
| 4337 | if (bpsp) |
| 4338 | *bpsp = bps; |
| 4339 | } |
| 4340 | |
| 4341 | return 0; |
| 4342 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4343 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | |
| 4345 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4346 | * snd_hda_is_supported_format - Check the validity of the format |
| 4347 | * @codec: HD-audio codec |
| 4348 | * @nid: NID to check |
| 4349 | * @format: the HD-audio format value to check |
| 4350 | * |
| 4351 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | * |
| 4353 | * Returns 1 if supported, 0 if not. |
| 4354 | */ |
| 4355 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4356 | unsigned int format) |
| 4357 | { |
| 4358 | int i; |
| 4359 | unsigned int val = 0, rate, stream; |
| 4360 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4361 | val = query_pcm_param(codec, nid); |
| 4362 | if (!val) |
| 4363 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | |
| 4365 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4366 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4367 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | if (val & (1 << i)) |
| 4369 | break; |
| 4370 | return 0; |
| 4371 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4372 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | return 0; |
| 4374 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4375 | stream = query_stream_param(codec, nid); |
| 4376 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | return 0; |
| 4378 | |
| 4379 | if (stream & AC_SUPFMT_PCM) { |
| 4380 | switch (format & 0xf0) { |
| 4381 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4382 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | return 0; |
| 4384 | break; |
| 4385 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4386 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | return 0; |
| 4388 | break; |
| 4389 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4390 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | return 0; |
| 4392 | break; |
| 4393 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4394 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | return 0; |
| 4396 | break; |
| 4397 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4398 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | return 0; |
| 4400 | break; |
| 4401 | default: |
| 4402 | return 0; |
| 4403 | } |
| 4404 | } else { |
| 4405 | /* FIXME: check for float32 and AC3? */ |
| 4406 | } |
| 4407 | |
| 4408 | return 1; |
| 4409 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4410 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * PCM stuff |
| 4414 | */ |
| 4415 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4416 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4417 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | { |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
| 4422 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4423 | struct hda_codec *codec, |
| 4424 | unsigned int stream_tag, |
| 4425 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4426 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | { |
| 4428 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4429 | return 0; |
| 4430 | } |
| 4431 | |
| 4432 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4433 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4434 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4436 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4440 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4441 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4443 | int err; |
| 4444 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4445 | /* query support PCM information from the given NID */ |
| 4446 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4447 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4448 | info->rates ? NULL : &info->rates, |
| 4449 | info->formats ? NULL : &info->formats, |
| 4450 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4451 | if (err < 0) |
| 4452 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | } |
| 4454 | if (info->ops.open == NULL) |
| 4455 | info->ops.open = hda_pcm_default_open_close; |
| 4456 | if (info->ops.close == NULL) |
| 4457 | info->ops.close = hda_pcm_default_open_close; |
| 4458 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4459 | if (snd_BUG_ON(!info->nid)) |
| 4460 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | info->ops.prepare = hda_pcm_default_prepare; |
| 4462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4464 | if (snd_BUG_ON(!info->nid)) |
| 4465 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4467 | } |
| 4468 | return 0; |
| 4469 | } |
| 4470 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4471 | /* |
| 4472 | * codec prepare/cleanup entries |
| 4473 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4474 | /** |
| 4475 | * snd_hda_codec_prepare - Prepare a stream |
| 4476 | * @codec: the HDA codec |
| 4477 | * @hinfo: PCM information |
| 4478 | * @stream: stream tag to assign |
| 4479 | * @format: format id to assign |
| 4480 | * @substream: PCM substream to assign |
| 4481 | * |
| 4482 | * Calls the prepare callback set by the codec with the given arguments. |
| 4483 | * Clean up the inactive streams when successful. |
| 4484 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4485 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4486 | struct hda_pcm_stream *hinfo, |
| 4487 | unsigned int stream, |
| 4488 | unsigned int format, |
| 4489 | struct snd_pcm_substream *substream) |
| 4490 | { |
| 4491 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4492 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4493 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4494 | if (ret >= 0) |
| 4495 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4496 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4497 | return ret; |
| 4498 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4499 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4500 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4501 | /** |
| 4502 | * snd_hda_codec_cleanup - Prepare a stream |
| 4503 | * @codec: the HDA codec |
| 4504 | * @hinfo: PCM information |
| 4505 | * @substream: PCM substream |
| 4506 | * |
| 4507 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4508 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4509 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4510 | struct hda_pcm_stream *hinfo, |
| 4511 | struct snd_pcm_substream *substream) |
| 4512 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4513 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4514 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4515 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4516 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4517 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4518 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4519 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4520 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4521 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4522 | }; |
| 4523 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4524 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4525 | * get the empty PCM device number to assign |
| 4526 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4527 | 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] | 4528 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4529 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4530 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4531 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4532 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4533 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4534 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4535 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4536 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4537 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4538 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4539 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4540 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4541 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4542 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4543 | return -EINVAL; |
| 4544 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4545 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4546 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4547 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4548 | if (audio_idx[type][i] >= 8) |
| 4549 | break; |
| 4550 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4551 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4552 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4553 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4554 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4555 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4556 | /* non-fixed slots starting from 10 */ |
| 4557 | for (i = 10; i < 32; i++) { |
| 4558 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4559 | return i; |
| 4560 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4561 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4562 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4563 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4564 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4565 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4566 | dev_warn(bus->card->dev, |
| 4567 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4568 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4569 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4573 | * attach a new PCM stream |
| 4574 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4575 | static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4576 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4577 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4578 | struct hda_pcm_stream *info; |
| 4579 | int stream, err; |
| 4580 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4581 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4582 | return -EINVAL; |
| 4583 | for (stream = 0; stream < 2; stream++) { |
| 4584 | info = &pcm->stream[stream]; |
| 4585 | if (info->substreams) { |
| 4586 | err = set_pcm_default_values(codec, info); |
| 4587 | if (err < 0) |
| 4588 | return err; |
| 4589 | } |
| 4590 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4591 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4592 | } |
| 4593 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4594 | /* assign all PCMs of the given codec */ |
| 4595 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4596 | { |
| 4597 | unsigned int pcm; |
| 4598 | int err; |
| 4599 | |
| 4600 | if (!codec->num_pcms) { |
| 4601 | if (!codec->patch_ops.build_pcms) |
| 4602 | return 0; |
| 4603 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4604 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4605 | codec_err(codec, |
| 4606 | "cannot build PCMs for #%d (error %d)\n", |
| 4607 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4608 | err = snd_hda_codec_reset(codec); |
| 4609 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4610 | codec_err(codec, |
| 4611 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4612 | return err; |
| 4613 | } |
| 4614 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4615 | } |
| 4616 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4617 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4618 | int dev; |
| 4619 | |
| 4620 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4621 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4622 | |
| 4623 | if (!cpcm->pcm) { |
| 4624 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4625 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4626 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4627 | cpcm->device = dev; |
| 4628 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4629 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4630 | codec_err(codec, |
| 4631 | "cannot attach PCM stream %d for codec #%d\n", |
| 4632 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4633 | continue; /* no fatal error */ |
| 4634 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | return 0; |
| 4638 | } |
| 4639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | /** |
| 4641 | * snd_hda_build_pcms - build PCM information |
| 4642 | * @bus: the BUS |
| 4643 | * |
| 4644 | * Create PCM information for each codec included in the bus. |
| 4645 | * |
| 4646 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4647 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4648 | * to create its PCM instances. |
| 4649 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4650 | * callback. |
| 4651 | * |
| 4652 | * At least, substreams, channels_min and channels_max must be filled for |
| 4653 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4654 | * When rates and/or formats are zero, the supported values are queried |
| 4655 | * from the given nid. The nid is used also by the default ops.prepare |
| 4656 | * and ops.cleanup callbacks. |
| 4657 | * |
| 4658 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4659 | * ops.close is supposed to be called in the close callback. |
| 4660 | * ops.prepare should be called in the prepare or hw_params callback |
| 4661 | * with the proper parameters for set up. |
| 4662 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4663 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4664 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4666 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4668 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4670 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4671 | int err = snd_hda_codec_build_pcms(codec); |
| 4672 | if (err < 0) |
| 4673 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | } |
| 4675 | return 0; |
| 4676 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4677 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | * snd_hda_add_new_ctls - create controls from the array |
| 4681 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4682 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | * |
| 4684 | * This helper function creates and add new controls in the given array. |
| 4685 | * The array must be terminated with an empty entry as terminator. |
| 4686 | * |
| 4687 | * Returns 0 if successful, or a negative error code. |
| 4688 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4689 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4690 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4692 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | |
| 4694 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4695 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4696 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4697 | if (knew->iface == -1) /* skip this codec private value */ |
| 4698 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4699 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4700 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4701 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4702 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4703 | if (addr > 0) |
| 4704 | kctl->id.device = addr; |
| 4705 | if (idx > 0) |
| 4706 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4707 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4708 | if (!err) |
| 4709 | break; |
| 4710 | /* try first with another device index corresponding to |
| 4711 | * the codec addr; if it still fails (or it's the |
| 4712 | * primary codec), then try another control index |
| 4713 | */ |
| 4714 | if (!addr && codec->addr) |
| 4715 | addr = codec->addr; |
| 4716 | else if (!idx && !knew->index) { |
| 4717 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4718 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4719 | if (idx <= 0) |
| 4720 | return err; |
| 4721 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4722 | return err; |
| 4723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | } |
| 4725 | return 0; |
| 4726 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4727 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4729 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4730 | /** |
| 4731 | * snd_hda_power_up - Power-up the codec |
| 4732 | * @codec: HD-audio codec |
| 4733 | * |
| 4734 | * Increment the usage counter and resume the device if not done yet. |
| 4735 | */ |
| 4736 | void snd_hda_power_up(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4737 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4738 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4739 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4740 | if (codec_in_pm(codec)) |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4741 | return; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4742 | pm_runtime_get_sync(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4743 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4744 | EXPORT_SYMBOL_GPL(snd_hda_power_up); |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4745 | |
| 4746 | /** |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4747 | * snd_hda_power_down - Power-down the codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4748 | * @codec: HD-audio codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4749 | * |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4750 | * Decrement the usage counter and schedules the autosuspend if none used. |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4751 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4752 | void snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4753 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4754 | struct device *dev = hda_codec_dev(codec); |
| 4755 | |
| 4756 | if (codec_in_pm(codec)) |
| 4757 | return; |
| 4758 | pm_runtime_mark_last_busy(dev); |
| 4759 | pm_runtime_put_autosuspend(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4760 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4761 | EXPORT_SYMBOL_GPL(snd_hda_power_down); |
| 4762 | |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4763 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4764 | { |
| 4765 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4766 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4767 | if (delay > 0) { |
| 4768 | pm_runtime_set_autosuspend_delay(dev, delay); |
| 4769 | pm_runtime_use_autosuspend(dev); |
| 4770 | pm_runtime_allow(dev); |
| 4771 | if (!pm_runtime_suspended(dev)) |
| 4772 | pm_runtime_mark_last_busy(dev); |
| 4773 | } else { |
| 4774 | pm_runtime_dont_use_autosuspend(dev); |
| 4775 | pm_runtime_forbid(dev); |
| 4776 | } |
| 4777 | } |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4778 | |
| 4779 | /** |
| 4780 | * snd_hda_set_power_save - reprogram autosuspend for the given delay |
| 4781 | * @bus: HD-audio bus |
| 4782 | * @delay: autosuspend delay in msec, 0 = off |
| 4783 | * |
| 4784 | * Synchronize the runtime PM autosuspend state from the power_save option. |
| 4785 | */ |
| 4786 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) |
| 4787 | { |
| 4788 | struct hda_codec *c; |
| 4789 | |
| 4790 | list_for_each_entry(c, &bus->codec_list, list) |
| 4791 | codec_set_power_save(c, delay); |
| 4792 | } |
| 4793 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4794 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4795 | /** |
| 4796 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4797 | * @codec: HD-audio codec |
| 4798 | * @check: the object containing an AMP list and the status |
| 4799 | * @nid: NID to check / update |
| 4800 | * |
| 4801 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4802 | * check the current AMP status, and update the the power-status according |
| 4803 | * to the mute status. |
| 4804 | * |
| 4805 | * This function is supposed to be set or called from the check_power_status |
| 4806 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4807 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4808 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4809 | struct hda_loopback_check *check, |
| 4810 | hda_nid_t nid) |
| 4811 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4812 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4813 | int ch, v; |
| 4814 | |
| 4815 | if (!check->amplist) |
| 4816 | return 0; |
| 4817 | for (p = check->amplist; p->nid; p++) { |
| 4818 | if (p->nid == nid) |
| 4819 | break; |
| 4820 | } |
| 4821 | if (!p->nid) |
| 4822 | return 0; /* nothing changed */ |
| 4823 | |
| 4824 | for (p = check->amplist; p->nid; p++) { |
| 4825 | for (ch = 0; ch < 2; ch++) { |
| 4826 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4827 | p->idx); |
| 4828 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4829 | if (!check->power_on) { |
| 4830 | check->power_on = 1; |
| 4831 | snd_hda_power_up(codec); |
| 4832 | } |
| 4833 | return 1; |
| 4834 | } |
| 4835 | } |
| 4836 | } |
| 4837 | if (check->power_on) { |
| 4838 | check->power_on = 0; |
| 4839 | snd_hda_power_down(codec); |
| 4840 | } |
| 4841 | return 0; |
| 4842 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4843 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4844 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4846 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4847 | * Channel mode helper |
| 4848 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4849 | |
| 4850 | /** |
| 4851 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4852 | * @codec: the HDA codec |
| 4853 | * @uinfo: pointer to get/store the data |
| 4854 | * @chmode: channel mode array |
| 4855 | * @num_chmodes: channel mode array size |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4856 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4857 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4858 | struct snd_ctl_elem_info *uinfo, |
| 4859 | const struct hda_channel_mode *chmode, |
| 4860 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4861 | { |
| 4862 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4863 | uinfo->count = 1; |
| 4864 | uinfo->value.enumerated.items = num_chmodes; |
| 4865 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4866 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4867 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4868 | chmode[uinfo->value.enumerated.item].channels); |
| 4869 | return 0; |
| 4870 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4871 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4872 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4873 | /** |
| 4874 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4875 | * @codec: the HDA codec |
| 4876 | * @ucontrol: pointer to get/store the data |
| 4877 | * @chmode: channel mode array |
| 4878 | * @num_chmodes: channel mode array size |
| 4879 | * @max_channels: max number of channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4880 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4881 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4882 | struct snd_ctl_elem_value *ucontrol, |
| 4883 | const struct hda_channel_mode *chmode, |
| 4884 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4885 | int max_channels) |
| 4886 | { |
| 4887 | int i; |
| 4888 | |
| 4889 | for (i = 0; i < num_chmodes; i++) { |
| 4890 | if (max_channels == chmode[i].channels) { |
| 4891 | ucontrol->value.enumerated.item[0] = i; |
| 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | return 0; |
| 4896 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4897 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4898 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4899 | /** |
| 4900 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4901 | * @codec: the HDA codec |
| 4902 | * @ucontrol: pointer to get/store the data |
| 4903 | * @chmode: channel mode array |
| 4904 | * @num_chmodes: channel mode array size |
| 4905 | * @max_channelsp: pointer to store the max channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4906 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4907 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 4908 | struct snd_ctl_elem_value *ucontrol, |
| 4909 | const struct hda_channel_mode *chmode, |
| 4910 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4911 | int *max_channelsp) |
| 4912 | { |
| 4913 | unsigned int mode; |
| 4914 | |
| 4915 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 4916 | if (mode >= num_chmodes) |
| 4917 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4918 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4919 | return 0; |
| 4920 | /* change the current channel setting */ |
| 4921 | *max_channelsp = chmode[mode].channels; |
| 4922 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4923 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4924 | return 1; |
| 4925 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4926 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | /* |
| 4929 | * input MUX helper |
| 4930 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4931 | |
| 4932 | /** |
| 4933 | * 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] | 4934 | * @imux: imux helper object |
| 4935 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4936 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4937 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4938 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | { |
| 4940 | unsigned int index; |
| 4941 | |
| 4942 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4943 | uinfo->count = 1; |
| 4944 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4945 | if (!imux->num_items) |
| 4946 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | index = uinfo->value.enumerated.item; |
| 4948 | if (index >= imux->num_items) |
| 4949 | index = imux->num_items - 1; |
| 4950 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4951 | return 0; |
| 4952 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4953 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4955 | /** |
| 4956 | * 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] | 4957 | * @codec: the HDA codec |
| 4958 | * @imux: imux helper object |
| 4959 | * @ucontrol: pointer to get/store the data |
| 4960 | * @nid: input mux NID |
| 4961 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4962 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4963 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4964 | const struct hda_input_mux *imux, |
| 4965 | struct snd_ctl_elem_value *ucontrol, |
| 4966 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | unsigned int *cur_val) |
| 4968 | { |
| 4969 | unsigned int idx; |
| 4970 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4971 | if (!imux->num_items) |
| 4972 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | idx = ucontrol->value.enumerated.item[0]; |
| 4974 | if (idx >= imux->num_items) |
| 4975 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4976 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4978 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4979 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | *cur_val = idx; |
| 4981 | return 1; |
| 4982 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4983 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | |
| 4985 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4986 | /** |
| 4987 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 4988 | * @kcontrol: ctl element |
| 4989 | * @uinfo: pointer to get/store the data |
| 4990 | * @num_items: number of enum items |
| 4991 | * @texts: enum item string array |
| 4992 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4993 | * process kcontrol info callback of a simple string enum array |
| 4994 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 4995 | */ |
| 4996 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 4997 | struct snd_ctl_elem_info *uinfo, |
| 4998 | int num_items, const char * const *texts) |
| 4999 | { |
| 5000 | static const char * const texts_default[] = { |
| 5001 | "Disabled", "Enabled" |
| 5002 | }; |
| 5003 | |
| 5004 | if (!texts || !num_items) { |
| 5005 | num_items = 2; |
| 5006 | texts = texts_default; |
| 5007 | } |
| 5008 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 5009 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5010 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5011 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5012 | |
| 5013 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | * Multi-channel / digital-out PCM helper functions |
| 5015 | */ |
| 5016 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5017 | /* setup SPDIF output stream */ |
| 5018 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5019 | unsigned int stream_tag, unsigned int format) |
| 5020 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5021 | struct hda_spdif_out *spdif; |
| 5022 | unsigned int curr_fmt; |
| 5023 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5024 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5025 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5026 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5027 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5028 | reset = codec->spdif_status_reset && |
| 5029 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5030 | curr_fmt != format; |
| 5031 | |
| 5032 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5033 | updated */ |
| 5034 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5035 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5036 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5037 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5038 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5039 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5040 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5041 | for (d = codec->slave_dig_outs; *d; d++) |
| 5042 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5043 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5044 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5045 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5046 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5047 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5048 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5049 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5050 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5051 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5052 | { |
| 5053 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5054 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5055 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5056 | for (d = codec->slave_dig_outs; *d; d++) |
| 5057 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5058 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5059 | } |
| 5060 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5061 | /** |
| 5062 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5063 | * @bus: HD-audio bus |
| 5064 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5065 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5066 | { |
| 5067 | struct hda_codec *codec; |
| 5068 | |
| 5069 | if (!bus) |
| 5070 | return; |
| 5071 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5072 | if (hda_codec_is_power_on(codec) && |
| 5073 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5074 | codec->patch_ops.reboot_notify(codec); |
| 5075 | } |
| 5076 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5077 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5078 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5079 | /** |
| 5080 | * 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] | 5081 | * @codec: the HDA codec |
| 5082 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5084 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5085 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5087 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5088 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5089 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5090 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5092 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | return 0; |
| 5094 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5095 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5097 | /** |
| 5098 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5099 | * @codec: the HDA codec |
| 5100 | * @mout: hda_multi_out object |
| 5101 | * @stream_tag: stream tag to assign |
| 5102 | * @format: format id to assign |
| 5103 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5104 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5105 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5106 | struct hda_multi_out *mout, |
| 5107 | unsigned int stream_tag, |
| 5108 | unsigned int format, |
| 5109 | struct snd_pcm_substream *substream) |
| 5110 | { |
| 5111 | mutex_lock(&codec->spdif_mutex); |
| 5112 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5113 | mutex_unlock(&codec->spdif_mutex); |
| 5114 | return 0; |
| 5115 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5116 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5117 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5118 | /** |
| 5119 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5120 | * @codec: the HDA codec |
| 5121 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5122 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5123 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5124 | struct hda_multi_out *mout) |
| 5125 | { |
| 5126 | mutex_lock(&codec->spdif_mutex); |
| 5127 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5128 | mutex_unlock(&codec->spdif_mutex); |
| 5129 | return 0; |
| 5130 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5131 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5132 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5133 | /** |
| 5134 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5135 | * @codec: the HDA codec |
| 5136 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5138 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5139 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5141 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5143 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5146 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5148 | /** |
| 5149 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5150 | * @codec: the HDA codec |
| 5151 | * @mout: hda_multi_out object |
| 5152 | * @substream: PCM substream to assign |
| 5153 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5154 | * |
| 5155 | * Open analog outputs and set up the hw-constraints. |
| 5156 | * If the digital outputs can be opened as slave, open the digital |
| 5157 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5159 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5160 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5161 | struct snd_pcm_substream *substream, |
| 5162 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5164 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5165 | runtime->hw.channels_max = mout->max_channels; |
| 5166 | if (mout->dig_out_nid) { |
| 5167 | if (!mout->analog_rates) { |
| 5168 | mout->analog_rates = hinfo->rates; |
| 5169 | mout->analog_formats = hinfo->formats; |
| 5170 | mout->analog_maxbps = hinfo->maxbps; |
| 5171 | } else { |
| 5172 | runtime->hw.rates = mout->analog_rates; |
| 5173 | runtime->hw.formats = mout->analog_formats; |
| 5174 | hinfo->maxbps = mout->analog_maxbps; |
| 5175 | } |
| 5176 | if (!mout->spdif_rates) { |
| 5177 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5178 | &mout->spdif_rates, |
| 5179 | &mout->spdif_formats, |
| 5180 | &mout->spdif_maxbps); |
| 5181 | } |
| 5182 | mutex_lock(&codec->spdif_mutex); |
| 5183 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5184 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5185 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5186 | runtime->hw.rates &= mout->spdif_rates; |
| 5187 | runtime->hw.formats &= mout->spdif_formats; |
| 5188 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5189 | hinfo->maxbps = mout->spdif_maxbps; |
| 5190 | } else { |
| 5191 | mout->share_spdif = 0; |
| 5192 | /* FIXME: need notify? */ |
| 5193 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5194 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5195 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5198 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5199 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5200 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5202 | /** |
| 5203 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5204 | * @codec: the HDA codec |
| 5205 | * @mout: hda_multi_out object |
| 5206 | * @stream_tag: stream tag to assign |
| 5207 | * @format: format id to assign |
| 5208 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5209 | * |
| 5210 | * Set up the i/o for analog out. |
| 5211 | * 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] | 5212 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5213 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5214 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | unsigned int stream_tag, |
| 5216 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5217 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5219 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5221 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | int i; |
| 5223 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5224 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5225 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5226 | if (mout->dig_out_nid && mout->share_spdif && |
| 5227 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5229 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5230 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5231 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5233 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5234 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | } else { |
| 5236 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5237 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | } |
| 5239 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5240 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
| 5242 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5243 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5244 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5245 | if (!mout->no_share_stream && |
| 5246 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5248 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5249 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5250 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5251 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5252 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5253 | snd_hda_codec_setup_stream(codec, |
| 5254 | mout->hp_out_nid[i], |
| 5255 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | /* surrounds */ |
| 5258 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5259 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5260 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5261 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5262 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5263 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5264 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5266 | |
| 5267 | /* extra surrounds */ |
| 5268 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5269 | int ch = 0; |
| 5270 | if (!mout->extra_out_nid[i]) |
| 5271 | break; |
| 5272 | if (chs >= (i + 1) * 2) |
| 5273 | ch = i * 2; |
| 5274 | else if (!mout->no_share_stream) |
| 5275 | break; |
| 5276 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5277 | stream_tag, ch, format); |
| 5278 | } |
| 5279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | return 0; |
| 5281 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5282 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5284 | /** |
| 5285 | * 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] | 5286 | * @codec: the HDA codec |
| 5287 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5289 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5290 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5292 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | int i; |
| 5294 | |
| 5295 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5296 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5298 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5299 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5300 | if (mout->hp_out_nid[i]) |
| 5301 | snd_hda_codec_cleanup_stream(codec, |
| 5302 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5303 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5304 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5305 | snd_hda_codec_cleanup_stream(codec, |
| 5306 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5307 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | 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] | 5309 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | mout->dig_out_used = 0; |
| 5311 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5312 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | return 0; |
| 5314 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5315 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5317 | /** |
| 5318 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5319 | * @codec: the HDA codec |
| 5320 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5321 | * |
| 5322 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5323 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5324 | */ |
| 5325 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5326 | { |
| 5327 | unsigned int pincap; |
| 5328 | unsigned int oldval; |
| 5329 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5330 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5331 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5332 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5333 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5334 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5335 | return AC_PINCTL_VREF_80; |
| 5336 | else if (pincap & AC_PINCAP_VREF_50) |
| 5337 | return AC_PINCTL_VREF_50; |
| 5338 | else if (pincap & AC_PINCAP_VREF_100) |
| 5339 | return AC_PINCTL_VREF_100; |
| 5340 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5341 | return AC_PINCTL_VREF_GRD; |
| 5342 | return AC_PINCTL_VREF_HIZ; |
| 5343 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5344 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5345 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5346 | /** |
| 5347 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5348 | * @codec: the HDA codec |
| 5349 | * @pin: referred pin NID |
| 5350 | * @val: pin ctl value to audit |
| 5351 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5352 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5353 | hda_nid_t pin, unsigned int val) |
| 5354 | { |
| 5355 | static unsigned int cap_lists[][2] = { |
| 5356 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5357 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5358 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5359 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5360 | }; |
| 5361 | unsigned int cap; |
| 5362 | |
| 5363 | if (!val) |
| 5364 | return 0; |
| 5365 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5366 | if (!cap) |
| 5367 | return val; /* don't know what to do... */ |
| 5368 | |
| 5369 | if (val & AC_PINCTL_OUT_EN) { |
| 5370 | if (!(cap & AC_PINCAP_OUT)) |
| 5371 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5372 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5373 | val &= ~AC_PINCTL_HP_EN; |
| 5374 | } |
| 5375 | |
| 5376 | if (val & AC_PINCTL_IN_EN) { |
| 5377 | if (!(cap & AC_PINCAP_IN)) |
| 5378 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5379 | else { |
| 5380 | unsigned int vcap, vref; |
| 5381 | int i; |
| 5382 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5383 | vref = val & AC_PINCTL_VREFEN; |
| 5384 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5385 | if (vref == cap_lists[i][0] && |
| 5386 | !(vcap & cap_lists[i][1])) { |
| 5387 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5388 | vref = AC_PINCTL_VREF_HIZ; |
| 5389 | else |
| 5390 | vref = cap_lists[i + 1][0]; |
| 5391 | } |
| 5392 | } |
| 5393 | val &= ~AC_PINCTL_VREFEN; |
| 5394 | val |= vref; |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | return val; |
| 5399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5400 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5401 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5402 | /** |
| 5403 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5404 | * @codec: the HDA codec |
| 5405 | * @pin: referred pin NID |
| 5406 | * @val: pin control value to set |
| 5407 | * @cached: access over codec pinctl cache or direct write |
| 5408 | * |
| 5409 | * This function is a helper to set a pin ctl value more safely. |
| 5410 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5411 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5412 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5413 | * snd_hda_codec_write() depending on @cached flag. |
| 5414 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5415 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5416 | unsigned int val, bool cached) |
| 5417 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5418 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5419 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5420 | if (cached) |
| 5421 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5422 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5423 | else |
| 5424 | return snd_hda_codec_write(codec, pin, 0, |
| 5425 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5426 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5427 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5428 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5429 | /** |
| 5430 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5431 | * @codec: the HDA codec |
| 5432 | * @imux: imux helper object |
| 5433 | * @label: the name of imux item to assign |
| 5434 | * @index: index number of imux item to assign |
| 5435 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5436 | * |
| 5437 | * When the same label is used already in the existing items, the number |
| 5438 | * suffix is appended to the label. This label index number is stored |
| 5439 | * to type_idx when non-NULL pointer is given. |
| 5440 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5441 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5442 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5443 | int index, int *type_idx) |
| 5444 | { |
| 5445 | int i, label_idx = 0; |
| 5446 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5447 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5448 | return -EINVAL; |
| 5449 | } |
| 5450 | for (i = 0; i < imux->num_items; i++) { |
| 5451 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5452 | label_idx++; |
| 5453 | } |
| 5454 | if (type_idx) |
| 5455 | *type_idx = label_idx; |
| 5456 | if (label_idx > 0) |
| 5457 | snprintf(imux->items[imux->num_items].label, |
| 5458 | sizeof(imux->items[imux->num_items].label), |
| 5459 | "%s %d", label, label_idx); |
| 5460 | else |
| 5461 | strlcpy(imux->items[imux->num_items].label, label, |
| 5462 | sizeof(imux->items[imux->num_items].label)); |
| 5463 | imux->items[imux->num_items].index = index; |
| 5464 | imux->num_items++; |
| 5465 | return 0; |
| 5466 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5467 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | /** |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5470 | * snd_hda_bus_reset - Reset the bus |
| 5471 | * @bus: HD-audio bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5473 | void snd_hda_bus_reset(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5475 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5477 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5478 | /* FIXME: maybe a better way needed for forced reset */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5479 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5480 | #ifdef CONFIG_PM |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5481 | if (hda_codec_is_power_on(codec)) { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 5482 | hda_call_codec_suspend(codec); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5483 | hda_call_codec_resume(codec); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5484 | } |
| 5485 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5488 | EXPORT_SYMBOL_GPL(snd_hda_bus_reset); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5489 | |
| 5490 | /* |
| 5491 | * generic arrays |
| 5492 | */ |
| 5493 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5494 | /** |
| 5495 | * snd_array_new - get a new element from the given array |
| 5496 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5497 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5498 | * Get a new element from the given array. If it exceeds the |
| 5499 | * pre-allocated array size, re-allocate the array. |
| 5500 | * |
| 5501 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5502 | */ |
| 5503 | void *snd_array_new(struct snd_array *array) |
| 5504 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5505 | if (snd_BUG_ON(!array->elem_size)) |
| 5506 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5507 | if (array->used >= array->alloced) { |
| 5508 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5509 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5510 | void *nlist; |
| 5511 | if (snd_BUG_ON(num >= 4096)) |
| 5512 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5513 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5514 | if (!nlist) |
| 5515 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5516 | array->list = nlist; |
| 5517 | array->alloced = num; |
| 5518 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5519 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5520 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5521 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5522 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5523 | /** |
| 5524 | * snd_array_free - free the given array elements |
| 5525 | * @array: the array object |
| 5526 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5527 | void snd_array_free(struct snd_array *array) |
| 5528 | { |
| 5529 | kfree(array->list); |
| 5530 | array->used = 0; |
| 5531 | array->alloced = 0; |
| 5532 | array->list = NULL; |
| 5533 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5534 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5535 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5536 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5537 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5538 | * @pcm: PCM caps bits |
| 5539 | * @buf: the string buffer to write |
| 5540 | * @buflen: the max buffer length |
| 5541 | * |
| 5542 | * used by hda_proc.c and hda_eld.c |
| 5543 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5544 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5545 | { |
| 5546 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5547 | int i, j; |
| 5548 | |
| 5549 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5550 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5551 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5552 | |
| 5553 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5554 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5555 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5556 | |
| 5557 | MODULE_DESCRIPTION("HDA codec core"); |
| 5558 | MODULE_LICENSE("GPL"); |