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); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 779 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | bus->card = card; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 783 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 784 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | INIT_LIST_HEAD(&bus->codec_list); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 786 | INIT_WORK(&bus->unsol.work, process_unsol_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 788 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 789 | "hd-audio%d", card->number); |
| 790 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 791 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 792 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 793 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 794 | kfree(bus); |
| 795 | return -ENOMEM; |
| 796 | } |
| 797 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 798 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 799 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | snd_hda_bus_free(bus); |
| 801 | return err; |
| 802 | } |
| 803 | if (busp) |
| 804 | *busp = bus; |
| 805 | return 0; |
| 806 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 807 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 810 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 812 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 814 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | hda_nid_t nid; |
| 816 | |
| 817 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 818 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 819 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 820 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 821 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 822 | case AC_GRP_AUDIO_FUNCTION: |
| 823 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 824 | codec->afg_function_id = function_id & 0xff; |
| 825 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 826 | break; |
| 827 | case AC_GRP_MODEM_FUNCTION: |
| 828 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 829 | codec->mfg_function_id = function_id & 0xff; |
| 830 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 831 | break; |
| 832 | default: |
| 833 | break; |
| 834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 839 | * read widget caps for each widget and store in cache |
| 840 | */ |
| 841 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 842 | { |
| 843 | int i; |
| 844 | hda_nid_t nid; |
| 845 | |
| 846 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 847 | &codec->start_nid); |
| 848 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 849 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 850 | return -ENOMEM; |
| 851 | nid = codec->start_nid; |
| 852 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 853 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 854 | AC_PAR_AUDIO_WIDGET_CAP); |
| 855 | return 0; |
| 856 | } |
| 857 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 858 | /* read all pin default configurations and save codec->init_pins */ |
| 859 | static int read_pin_defaults(struct hda_codec *codec) |
| 860 | { |
| 861 | int i; |
| 862 | hda_nid_t nid = codec->start_nid; |
| 863 | |
| 864 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 865 | struct hda_pincfg *pin; |
| 866 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 867 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 868 | if (wid_type != AC_WID_PIN) |
| 869 | continue; |
| 870 | pin = snd_array_new(&codec->init_pins); |
| 871 | if (!pin) |
| 872 | return -ENOMEM; |
| 873 | pin->nid = nid; |
| 874 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 875 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 876 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 877 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 878 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 879 | } |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | /* look up the given pin config list and return the item matching with NID */ |
| 884 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 885 | struct snd_array *array, |
| 886 | hda_nid_t nid) |
| 887 | { |
| 888 | int i; |
| 889 | for (i = 0; i < array->used; i++) { |
| 890 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 891 | if (pin->nid == nid) |
| 892 | return pin; |
| 893 | } |
| 894 | return NULL; |
| 895 | } |
| 896 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 897 | /* set the current pin config value for the given NID. |
| 898 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 899 | */ |
| 900 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 901 | hda_nid_t nid, unsigned int cfg) |
| 902 | { |
| 903 | struct hda_pincfg *pin; |
| 904 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 905 | /* the check below may be invalid when pins are added by a fixup |
| 906 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 907 | * for now |
| 908 | */ |
| 909 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 910 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 911 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 912 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 913 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 914 | pin = look_up_pincfg(codec, list, nid); |
| 915 | if (!pin) { |
| 916 | pin = snd_array_new(list); |
| 917 | if (!pin) |
| 918 | return -ENOMEM; |
| 919 | pin->nid = nid; |
| 920 | } |
| 921 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 922 | return 0; |
| 923 | } |
| 924 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 925 | /** |
| 926 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 927 | * @codec: the HDA codec |
| 928 | * @nid: NID to set the pin config |
| 929 | * @cfg: the pin default config value |
| 930 | * |
| 931 | * Override a pin default configuration value in the cache. |
| 932 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 933 | * priority than the real hardware value. |
| 934 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 935 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 936 | hda_nid_t nid, unsigned int cfg) |
| 937 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 938 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 939 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 940 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 941 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 942 | /** |
| 943 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 944 | * @codec: the HDA codec |
| 945 | * @nid: NID to get the pin config |
| 946 | * |
| 947 | * Get the current pin config value of the given pin NID. |
| 948 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 949 | * returns the cached value. |
| 950 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 951 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 952 | { |
| 953 | struct hda_pincfg *pin; |
| 954 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 955 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 956 | { |
| 957 | unsigned int cfg = 0; |
| 958 | mutex_lock(&codec->user_mutex); |
| 959 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 960 | if (pin) |
| 961 | cfg = pin->cfg; |
| 962 | mutex_unlock(&codec->user_mutex); |
| 963 | if (cfg) |
| 964 | return cfg; |
| 965 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 966 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 967 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 968 | if (pin) |
| 969 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 970 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 971 | if (pin) |
| 972 | return pin->cfg; |
| 973 | return 0; |
| 974 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 975 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 976 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 977 | /** |
| 978 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 979 | * @codec: the HDA codec |
| 980 | * @nid: pin NID |
| 981 | * @val: assigned pinctl value |
| 982 | * |
| 983 | * This function stores the given value to a pinctl target value in the |
| 984 | * pincfg table. This isn't always as same as the actually written value |
| 985 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 986 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 987 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 988 | unsigned int val) |
| 989 | { |
| 990 | struct hda_pincfg *pin; |
| 991 | |
| 992 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 993 | if (!pin) |
| 994 | return -EINVAL; |
| 995 | pin->target = val; |
| 996 | return 0; |
| 997 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 998 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 999 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1000 | /** |
| 1001 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1002 | * @codec: the HDA codec |
| 1003 | * @nid: pin NID |
| 1004 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1005 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1006 | { |
| 1007 | struct hda_pincfg *pin; |
| 1008 | |
| 1009 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1010 | if (!pin) |
| 1011 | return 0; |
| 1012 | return pin->target; |
| 1013 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1014 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1015 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1016 | /** |
| 1017 | * snd_hda_shutup_pins - Shut up all pins |
| 1018 | * @codec: the HDA codec |
| 1019 | * |
| 1020 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1021 | * The controls aren't cached so that they can be resumed properly. |
| 1022 | */ |
| 1023 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1024 | { |
| 1025 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1026 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1027 | * the default pin setup at the next load of the driver |
| 1028 | */ |
| 1029 | if (codec->bus->shutdown) |
| 1030 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1031 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1032 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1033 | /* use read here for syncing after issuing each verb */ |
| 1034 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1035 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1036 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1037 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1038 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1040 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1041 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1042 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1043 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1044 | { |
| 1045 | int i; |
| 1046 | if (!codec->pins_shutup) |
| 1047 | return; |
| 1048 | if (codec->bus->shutdown) |
| 1049 | return; |
| 1050 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1051 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1052 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1053 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1054 | pin->ctrl); |
| 1055 | } |
| 1056 | codec->pins_shutup = 0; |
| 1057 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1058 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1059 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1060 | static void hda_jackpoll_work(struct work_struct *work) |
| 1061 | { |
| 1062 | struct hda_codec *codec = |
| 1063 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1064 | |
| 1065 | snd_hda_jack_set_dirty_all(codec); |
| 1066 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1067 | |
| 1068 | if (!codec->jackpoll_interval) |
| 1069 | return; |
| 1070 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1071 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1072 | codec->jackpoll_interval); |
| 1073 | } |
| 1074 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1075 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1076 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1077 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1078 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1079 | /* release all pincfg lists */ |
| 1080 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1081 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1082 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1083 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1084 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1085 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1086 | snd_array_free(&codec->init_pins); |
| 1087 | } |
| 1088 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1089 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1090 | * audio-converter setup caches |
| 1091 | */ |
| 1092 | struct hda_cvt_setup { |
| 1093 | hda_nid_t nid; |
| 1094 | u8 stream_tag; |
| 1095 | u8 channel_id; |
| 1096 | u16 format_id; |
| 1097 | unsigned char active; /* cvt is currently used */ |
| 1098 | unsigned char dirty; /* setups should be cleared */ |
| 1099 | }; |
| 1100 | |
| 1101 | /* get or create a cache entry for the given audio converter NID */ |
| 1102 | static struct hda_cvt_setup * |
| 1103 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1104 | { |
| 1105 | struct hda_cvt_setup *p; |
| 1106 | int i; |
| 1107 | |
| 1108 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1109 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1110 | if (p->nid == nid) |
| 1111 | return p; |
| 1112 | } |
| 1113 | p = snd_array_new(&codec->cvt_setups); |
| 1114 | if (p) |
| 1115 | p->nid = nid; |
| 1116 | return p; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1120 | * PCM device |
| 1121 | */ |
| 1122 | static void release_pcm(struct kref *kref) |
| 1123 | { |
| 1124 | struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref); |
| 1125 | |
| 1126 | if (pcm->pcm) |
| 1127 | snd_device_free(pcm->codec->card, pcm->pcm); |
| 1128 | clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits); |
| 1129 | kfree(pcm->name); |
| 1130 | kfree(pcm); |
| 1131 | } |
| 1132 | |
| 1133 | void snd_hda_codec_pcm_put(struct hda_pcm *pcm) |
| 1134 | { |
| 1135 | kref_put(&pcm->kref, release_pcm); |
| 1136 | } |
| 1137 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put); |
| 1138 | |
| 1139 | struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec, |
| 1140 | const char *fmt, ...) |
| 1141 | { |
| 1142 | struct hda_pcm *pcm; |
| 1143 | va_list args; |
| 1144 | |
| 1145 | va_start(args, fmt); |
| 1146 | pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); |
| 1147 | if (!pcm) |
| 1148 | return NULL; |
| 1149 | |
| 1150 | pcm->codec = codec; |
| 1151 | kref_init(&pcm->kref); |
| 1152 | pcm->name = kvasprintf(GFP_KERNEL, fmt, args); |
| 1153 | if (!pcm->name) { |
| 1154 | kfree(pcm); |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | |
| 1158 | list_add_tail(&pcm->list, &codec->pcm_list_head); |
| 1159 | return pcm; |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new); |
| 1162 | |
| 1163 | static void codec_release_pcms(struct hda_codec *codec) |
| 1164 | { |
| 1165 | struct hda_pcm *pcm, *n; |
| 1166 | |
| 1167 | list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) { |
| 1168 | list_del_init(&pcm->list); |
| 1169 | snd_hda_codec_pcm_put(pcm); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | * codec destructor |
| 1175 | */ |
| 1176 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1177 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1178 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1180 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1181 | codec_release_pcms(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1182 | if (device_is_registered(hda_codec_dev(codec))) |
| 1183 | device_del(hda_codec_dev(codec)); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1184 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1185 | free_init_pincfgs(codec); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1186 | flush_workqueue(codec->bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1188 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1189 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1190 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1191 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1192 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | codec->bus->caddr_tbl[codec->addr] = NULL; |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1194 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1195 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1196 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1197 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1198 | kfree(codec->vendor_name); |
| 1199 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1200 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1201 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1202 | codec->bus->num_codecs--; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1203 | put_device(hda_codec_dev(codec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1206 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1207 | hda_nid_t fg, unsigned int power_state); |
| 1208 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1209 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1210 | unsigned int power_state); |
| 1211 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1212 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1213 | { |
| 1214 | struct hda_codec *codec = device->device_data; |
| 1215 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1216 | snd_hda_register_beep_device(codec); |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 1217 | if (device_is_registered(hda_codec_dev(codec))) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1218 | pm_runtime_enable(hda_codec_dev(codec)); |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame] | 1219 | /* it was powered up in snd_hda_codec_new(), now all done */ |
| 1220 | snd_hda_power_down(codec); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1221 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1225 | { |
| 1226 | struct hda_codec *codec = device->device_data; |
| 1227 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1228 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1229 | return 0; |
| 1230 | } |
| 1231 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1232 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1233 | { |
| 1234 | snd_hda_codec_free(device->device_data); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1238 | /* just free the container */ |
| 1239 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1240 | { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1241 | kfree(dev_to_hda_codec(dev)); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /** |
| 1245 | * snd_hda_codec_new - create a HDA codec |
| 1246 | * @bus: the bus to assign |
| 1247 | * @codec_addr: the codec address |
| 1248 | * @codecp: the pointer to store the generated codec |
| 1249 | * |
| 1250 | * Returns 0 if successful, or a negative error code. |
| 1251 | */ |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1252 | int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card, |
| 1253 | unsigned int codec_addr, struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | { |
| 1255 | struct hda_codec *codec; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1256 | struct device *dev; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1257 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1258 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1260 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1261 | .dev_register = snd_hda_codec_dev_register, |
| 1262 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1263 | .dev_free = snd_hda_codec_dev_free, |
| 1264 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1266 | if (snd_BUG_ON(!bus)) |
| 1267 | return -EINVAL; |
| 1268 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1269 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1272 | dev_err(card->dev, |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1273 | "address 0x%x is already occupied\n", |
| 1274 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | return -EBUSY; |
| 1276 | } |
| 1277 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1278 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1279 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1282 | dev = hda_codec_dev(codec); |
| 1283 | device_initialize(dev); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1284 | dev->parent = card->dev; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1285 | dev->bus = &snd_hda_bus_type; |
| 1286 | dev->release = snd_hda_codec_dev_release; |
| 1287 | dev->groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1288 | dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1289 | dev_set_drvdata(dev, codec); /* for sysfs */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 1290 | device_enable_async_suspend(dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | codec->bus = bus; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1293 | codec->card = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1295 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1296 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1297 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1298 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1299 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1300 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1301 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1302 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1303 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1304 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1305 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1306 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1307 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1308 | INIT_LIST_HEAD(&codec->conn_list); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 1309 | INIT_LIST_HEAD(&codec->pcm_list_head); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1310 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1311 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1312 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1313 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1315 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1316 | /* 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] | 1317 | * it's powered down later in snd_hda_codec_dev_register(). |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1318 | */ |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1319 | set_bit(codec->addr, &bus->codec_powered); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1320 | pm_runtime_set_active(hda_codec_dev(codec)); |
| 1321 | pm_runtime_get_noresume(hda_codec_dev(codec)); |
| 1322 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1323 | #endif |
| 1324 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1325 | snd_hda_sysfs_init(codec); |
| 1326 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1327 | if (codec->bus->modelname) { |
| 1328 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1329 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1330 | err = -ENODEV; |
| 1331 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1336 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | bus->caddr_tbl[codec_addr] = codec; |
| 1339 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1340 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1341 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1342 | if (codec->vendor_id == -1) |
| 1343 | /* read again, hopefully the access method was corrected |
| 1344 | * in the last read... |
| 1345 | */ |
| 1346 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1347 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1348 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1349 | AC_PAR_SUBSYSTEM_ID); |
| 1350 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1351 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1353 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1354 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1355 | dev_err(card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1356 | err = -ENODEV; |
| 1357 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1360 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1361 | err = read_widget_caps(codec, fg); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1362 | if (err < 0) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1363 | goto error; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1364 | err = read_pin_defaults(codec); |
| 1365 | if (err < 0) |
| 1366 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1367 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1368 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1369 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1370 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1371 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1374 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1375 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1376 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1377 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1378 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1379 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1380 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1381 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1382 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1383 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1384 | snd_hda_codec_proc_new(codec); |
| 1385 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1386 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1387 | |
| 1388 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1389 | codec->subsystem_id, codec->revision_id); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1390 | snd_component_add(card, component); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1391 | |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 1392 | err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops); |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1393 | if (err < 0) |
| 1394 | goto error; |
| 1395 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1396 | if (codecp) |
| 1397 | *codecp = codec; |
| 1398 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1399 | |
| 1400 | error: |
| 1401 | snd_hda_codec_free(codec); |
| 1402 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1403 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1404 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1405 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1406 | /** |
| 1407 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1408 | * @codec: the HDA codec |
| 1409 | * |
| 1410 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1411 | * the given codec. |
| 1412 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1413 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1414 | { |
| 1415 | hda_nid_t fg; |
| 1416 | int err; |
| 1417 | |
| 1418 | /* Assume the function group node does not change, |
| 1419 | * only the widget nodes may change. |
| 1420 | */ |
| 1421 | kfree(codec->wcaps); |
| 1422 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1423 | err = read_widget_caps(codec, fg); |
Takashi Iwai | f4de8fe | 2015-02-27 16:17:18 +0100 | [diff] [blame] | 1424 | if (err < 0) |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1425 | return err; |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1426 | |
| 1427 | snd_array_free(&codec->init_pins); |
| 1428 | err = read_pin_defaults(codec); |
| 1429 | |
| 1430 | return err; |
| 1431 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1432 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1433 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1434 | /* update the stream-id if changed */ |
| 1435 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1436 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1437 | u32 stream_tag, int channel_id) |
| 1438 | { |
| 1439 | unsigned int oldval, newval; |
| 1440 | |
| 1441 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1442 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1443 | newval = (stream_tag << 4) | channel_id; |
| 1444 | if (oldval != newval) |
| 1445 | snd_hda_codec_write(codec, nid, 0, |
| 1446 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1447 | newval); |
| 1448 | p->stream_tag = stream_tag; |
| 1449 | p->channel_id = channel_id; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* update the format-id if changed */ |
| 1454 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1455 | hda_nid_t nid, int format) |
| 1456 | { |
| 1457 | unsigned int oldval; |
| 1458 | |
| 1459 | if (p->format_id != format) { |
| 1460 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1461 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1462 | if (oldval != format) { |
| 1463 | msleep(1); |
| 1464 | snd_hda_codec_write(codec, nid, 0, |
| 1465 | AC_VERB_SET_STREAM_FORMAT, |
| 1466 | format); |
| 1467 | } |
| 1468 | p->format_id = format; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /** |
| 1473 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1474 | * @codec: the CODEC to set up |
| 1475 | * @nid: the NID to set up |
| 1476 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1477 | * @channel_id: channel id to pass, zero based. |
| 1478 | * @format: stream format. |
| 1479 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1480 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1481 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | int channel_id, int format) |
| 1483 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1484 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1485 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1486 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1487 | int i; |
| 1488 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1489 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1490 | return; |
| 1491 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1492 | codec_dbg(codec, |
| 1493 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1494 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1495 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1496 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1497 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1498 | |
| 1499 | if (codec->pcm_format_first) |
| 1500 | update_pcm_format(codec, p, nid, format); |
| 1501 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1502 | if (!codec->pcm_format_first) |
| 1503 | update_pcm_format(codec, p, nid, format); |
| 1504 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1505 | p->active = 1; |
| 1506 | p->dirty = 0; |
| 1507 | |
| 1508 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1509 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1510 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1511 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1512 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1513 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1514 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1515 | p->dirty = 1; |
| 1516 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1519 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1521 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1522 | struct hda_cvt_setup *q); |
| 1523 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1524 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1525 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1526 | * @codec: the CODEC to clean up |
| 1527 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1528 | * @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] | 1529 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1530 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1531 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1532 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1533 | struct hda_cvt_setup *p; |
| 1534 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1535 | if (!nid) |
| 1536 | return; |
| 1537 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1538 | if (codec->no_sticky_stream) |
| 1539 | do_now = 1; |
| 1540 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1541 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1542 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1543 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1544 | /* here we just clear the active flag when do_now isn't set; |
| 1545 | * actual clean-ups will be done later in |
| 1546 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1547 | */ |
| 1548 | if (do_now) |
| 1549 | really_cleanup_stream(codec, p); |
| 1550 | else |
| 1551 | p->active = 0; |
| 1552 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1553 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1554 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1555 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1556 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1557 | struct hda_cvt_setup *q) |
| 1558 | { |
| 1559 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1560 | if (q->stream_tag || q->channel_id) |
| 1561 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1562 | if (q->format_id) |
| 1563 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1564 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1565 | memset(q, 0, sizeof(*q)); |
| 1566 | q->nid = nid; |
| 1567 | } |
| 1568 | |
| 1569 | /* clean up the all conflicting obsolete streams */ |
| 1570 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1571 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1572 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1573 | int i; |
| 1574 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1575 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1576 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1577 | struct hda_cvt_setup *p; |
| 1578 | p = snd_array_elem(&c->cvt_setups, i); |
| 1579 | if (p->dirty) |
| 1580 | really_cleanup_stream(c, p); |
| 1581 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1585 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1586 | /* clean up all streams; called from suspend */ |
| 1587 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1588 | { |
| 1589 | int i; |
| 1590 | |
| 1591 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1592 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1593 | if (p->stream_tag) |
| 1594 | really_cleanup_stream(codec, p); |
| 1595 | } |
| 1596 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1597 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | /* |
| 1600 | * amp access functions |
| 1601 | */ |
| 1602 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1603 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1604 | #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] | 1605 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1606 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1607 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1609 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
| 1611 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1612 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1613 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1615 | memset(cache, 0, sizeof(*cache)); |
| 1616 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1617 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1620 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1621 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1622 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1626 | 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] | 1627 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1628 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1629 | u16 cur = cache->hash[idx]; |
| 1630 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1633 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | if (info->key == key) |
| 1635 | return info; |
| 1636 | cur = info->next; |
| 1637 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1638 | return NULL; |
| 1639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1641 | /* query the hash. allocate an entry if not found. */ |
| 1642 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1643 | u32 key) |
| 1644 | { |
| 1645 | struct hda_cache_head *info = get_hash(cache, key); |
| 1646 | if (!info) { |
| 1647 | u16 idx, cur; |
| 1648 | /* add a new hash entry */ |
| 1649 | info = snd_array_new(&cache->buf); |
| 1650 | if (!info) |
| 1651 | return NULL; |
| 1652 | cur = snd_array_index(&cache->buf, info); |
| 1653 | info->key = key; |
| 1654 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1655 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1656 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1657 | info->next = cache->hash[idx]; |
| 1658 | cache->hash[idx] = cur; |
| 1659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | return info; |
| 1661 | } |
| 1662 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1663 | /* query and allocate an amp hash entry */ |
| 1664 | static inline struct hda_amp_info * |
| 1665 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1666 | { |
| 1667 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1668 | } |
| 1669 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1670 | /* overwrite the value with the key in the caps hash */ |
| 1671 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1672 | { |
| 1673 | struct hda_amp_info *info; |
| 1674 | |
| 1675 | mutex_lock(&codec->hash_mutex); |
| 1676 | info = get_alloc_amp_hash(codec, key); |
| 1677 | if (!info) { |
| 1678 | mutex_unlock(&codec->hash_mutex); |
| 1679 | return -EINVAL; |
| 1680 | } |
| 1681 | info->amp_caps = val; |
| 1682 | info->head.val |= INFO_AMP_CAPS; |
| 1683 | mutex_unlock(&codec->hash_mutex); |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| 1687 | /* query the value from the caps hash; if not found, fetch the current |
| 1688 | * value from the given function and store in the hash |
| 1689 | */ |
| 1690 | static unsigned int |
| 1691 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1692 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1693 | { |
| 1694 | struct hda_amp_info *info; |
| 1695 | unsigned int val; |
| 1696 | |
| 1697 | mutex_lock(&codec->hash_mutex); |
| 1698 | info = get_alloc_amp_hash(codec, key); |
| 1699 | if (!info) { |
| 1700 | mutex_unlock(&codec->hash_mutex); |
| 1701 | return 0; |
| 1702 | } |
| 1703 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1704 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1705 | val = func(codec, nid, dir); |
| 1706 | write_caps_hash(codec, key, val); |
| 1707 | } else { |
| 1708 | val = info->amp_caps; |
| 1709 | mutex_unlock(&codec->hash_mutex); |
| 1710 | } |
| 1711 | return val; |
| 1712 | } |
| 1713 | |
| 1714 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1715 | int direction) |
| 1716 | { |
| 1717 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1718 | nid = codec->afg; |
| 1719 | return snd_hda_param_read(codec, nid, |
| 1720 | direction == HDA_OUTPUT ? |
| 1721 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1722 | } |
| 1723 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1724 | /** |
| 1725 | * query_amp_caps - query AMP capabilities |
| 1726 | * @codec: the HD-auio codec |
| 1727 | * @nid: the NID to query |
| 1728 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1729 | * |
| 1730 | * Query AMP capabilities for the given widget and direction. |
| 1731 | * Returns the obtained capability bits. |
| 1732 | * |
| 1733 | * When cap bits have been already read, this doesn't read again but |
| 1734 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1736 | 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] | 1737 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1738 | return query_caps_hash(codec, nid, direction, |
| 1739 | HDA_HASH_KEY(nid, direction, 0), |
| 1740 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1742 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1744 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1745 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1746 | * @codec: the HD-audio codec |
| 1747 | * @nid: the NID to query |
| 1748 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1749 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1750 | * |
| 1751 | * Check whether the widget has the given amp capability for the direction. |
| 1752 | */ |
| 1753 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1754 | int dir, unsigned int bits) |
| 1755 | { |
| 1756 | if (!nid) |
| 1757 | return false; |
| 1758 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 1759 | if (query_amp_caps(codec, nid, dir) & bits) |
| 1760 | return true; |
| 1761 | return false; |
| 1762 | } |
| 1763 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 1764 | |
| 1765 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1766 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1767 | * @codec: the CODEC to clean up |
| 1768 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1769 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1770 | * @caps: the capability bits to set |
| 1771 | * |
| 1772 | * Override the cached AMP caps bits value by the given one. |
| 1773 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1774 | * e.g. limit to 0dB, etc. |
| 1775 | * |
| 1776 | * Returns zero if successful or a negative error code. |
| 1777 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1778 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1779 | unsigned int caps) |
| 1780 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1781 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1782 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1783 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1784 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1785 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1786 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1787 | { |
| 1788 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1789 | } |
| 1790 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1791 | /** |
| 1792 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1793 | * @codec: the HD-auio codec |
| 1794 | * @nid: the NID to query |
| 1795 | * |
| 1796 | * Query PIN capabilities for the given widget. |
| 1797 | * Returns the obtained capability bits. |
| 1798 | * |
| 1799 | * When cap bits have been already read, this doesn't read again but |
| 1800 | * returns the cached value. |
| 1801 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1802 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1803 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1804 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1805 | read_pin_cap); |
| 1806 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1807 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1808 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1809 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1810 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1811 | * @codec: the CODEC |
| 1812 | * @nid: the NID to override |
| 1813 | * @caps: the capability bits to set |
| 1814 | * |
| 1815 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1816 | * |
| 1817 | * Returns zero if successful or a negative error code. |
| 1818 | */ |
| 1819 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1820 | unsigned int caps) |
| 1821 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1822 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1823 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1824 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1825 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1826 | /* read or sync the hash value with the current value; |
| 1827 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1829 | static struct hda_amp_info * |
| 1830 | 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] | 1831 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1833 | struct hda_amp_info *info; |
| 1834 | unsigned int parm, val = 0; |
| 1835 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1837 | retry: |
| 1838 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1839 | if (!info) |
| 1840 | return NULL; |
| 1841 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1842 | if (!val_read) { |
| 1843 | mutex_unlock(&codec->hash_mutex); |
| 1844 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1845 | parm |= direction == HDA_OUTPUT ? |
| 1846 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1847 | parm |= index; |
| 1848 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1849 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1850 | val &= 0xff; |
| 1851 | val_read = true; |
| 1852 | mutex_lock(&codec->hash_mutex); |
| 1853 | goto retry; |
| 1854 | } |
| 1855 | info->vol[ch] = val; |
| 1856 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1857 | } else if (init_only) |
| 1858 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1859 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1863 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1865 | 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] | 1866 | hda_nid_t nid, int ch, int direction, int index, |
| 1867 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
| 1869 | u32 parm; |
| 1870 | |
| 1871 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1872 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1873 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1874 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1875 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1876 | ; /* set the zero value as a fake mute */ |
| 1877 | else |
| 1878 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1880 | } |
| 1881 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1882 | /** |
| 1883 | * snd_hda_codec_amp_read - Read AMP value |
| 1884 | * @codec: HD-audio codec |
| 1885 | * @nid: NID to read the AMP value |
| 1886 | * @ch: channel (left=0 or right=1) |
| 1887 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1888 | * @index: the index value (only for input direction) |
| 1889 | * |
| 1890 | * 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] | 1891 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1892 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1893 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1895 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1896 | unsigned int val = 0; |
| 1897 | |
| 1898 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1899 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1900 | if (info) |
| 1901 | val = info->vol[ch]; |
| 1902 | mutex_unlock(&codec->hash_mutex); |
| 1903 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1905 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1907 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1908 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1909 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1910 | { |
| 1911 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1912 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1913 | |
| 1914 | if (snd_BUG_ON(mask & ~0xff)) |
| 1915 | mask &= 0xff; |
| 1916 | val &= mask; |
| 1917 | |
| 1918 | mutex_lock(&codec->hash_mutex); |
| 1919 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1920 | if (!info) { |
| 1921 | mutex_unlock(&codec->hash_mutex); |
| 1922 | return 0; |
| 1923 | } |
| 1924 | val |= info->vol[ch] & ~mask; |
| 1925 | if (info->vol[ch] == val) { |
| 1926 | mutex_unlock(&codec->hash_mutex); |
| 1927 | return 0; |
| 1928 | } |
| 1929 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1930 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1931 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1932 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1933 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1934 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1935 | return 1; |
| 1936 | } |
| 1937 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1938 | /** |
| 1939 | * snd_hda_codec_amp_update - update the AMP value |
| 1940 | * @codec: HD-audio codec |
| 1941 | * @nid: NID to read the AMP value |
| 1942 | * @ch: channel (left=0 or right=1) |
| 1943 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1944 | * @idx: the index value (only for input direction) |
| 1945 | * @mask: bit mask to set |
| 1946 | * @val: the bits value to set |
| 1947 | * |
| 1948 | * Update the AMP value with a bit mask. |
| 1949 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1950 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1951 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1952 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1954 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 1955 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1957 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1959 | /** |
| 1960 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1961 | * @codec: HD-audio codec |
| 1962 | * @nid: NID to read the AMP value |
| 1963 | * @direction: #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 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1969 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1970 | */ |
| 1971 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1972 | int direction, int idx, int mask, int val) |
| 1973 | { |
| 1974 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1975 | |
| 1976 | if (snd_BUG_ON(mask & ~0xff)) |
| 1977 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1978 | for (ch = 0; ch < 2; ch++) |
| 1979 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1980 | idx, mask, val); |
| 1981 | return ret; |
| 1982 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1983 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1984 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1985 | /** |
| 1986 | * snd_hda_codec_amp_init - initialize the AMP value |
| 1987 | * @codec: the HDA codec |
| 1988 | * @nid: NID to read the AMP value |
| 1989 | * @ch: channel (left=0 or right=1) |
| 1990 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1991 | * @idx: the index value (only for input direction) |
| 1992 | * @mask: bit mask to set |
| 1993 | * @val: the bits value to set |
| 1994 | * |
| 1995 | * 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] | 1996 | * the first access. If the amp was already initialized / updated beforehand, |
| 1997 | * this does nothing. |
| 1998 | */ |
| 1999 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 2000 | int dir, int idx, int mask, int val) |
| 2001 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2002 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 2003 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2004 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2005 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2006 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2007 | /** |
| 2008 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 2009 | * @codec: the HDA codec |
| 2010 | * @nid: NID to read the AMP value |
| 2011 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2012 | * @idx: the index value (only for input direction) |
| 2013 | * @mask: bit mask to set |
| 2014 | * @val: the bits value to set |
| 2015 | * |
| 2016 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 2017 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2018 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 2019 | int dir, int idx, int mask, int val) |
| 2020 | { |
| 2021 | int ch, ret = 0; |
| 2022 | |
| 2023 | if (snd_BUG_ON(mask & ~0xff)) |
| 2024 | mask &= 0xff; |
| 2025 | for (ch = 0; ch < 2; ch++) |
| 2026 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 2027 | idx, mask, val); |
| 2028 | return ret; |
| 2029 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2030 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 2031 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2032 | /** |
| 2033 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 2034 | * @codec: HD-audio codec |
| 2035 | * |
| 2036 | * Resume the all amp commands from the cache. |
| 2037 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2038 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 2039 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2040 | int i; |
| 2041 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2042 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 2043 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2044 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 2045 | struct hda_amp_info *buffer; |
| 2046 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2047 | hda_nid_t nid; |
| 2048 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2049 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2050 | |
| 2051 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2052 | if (!buffer->head.dirty) |
| 2053 | continue; |
| 2054 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2055 | info = *buffer; |
| 2056 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2057 | if (!key) |
| 2058 | continue; |
| 2059 | nid = key & 0xff; |
| 2060 | idx = (key >> 16) & 0xff; |
| 2061 | dir = (key >> 24) & 0xff; |
| 2062 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2063 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2064 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2065 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2066 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2067 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2068 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2069 | } |
| 2070 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2071 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2072 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2073 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2075 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2076 | unsigned int ofs) |
| 2077 | { |
| 2078 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2079 | /* get num steps */ |
| 2080 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2081 | if (ofs < caps) |
| 2082 | caps -= ofs; |
| 2083 | return caps; |
| 2084 | } |
| 2085 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2086 | /** |
| 2087 | * 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] | 2088 | * @kcontrol: referred ctl element |
| 2089 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2090 | * |
| 2091 | * The control element is supposed to have the private_value field |
| 2092 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2093 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2094 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2095 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | { |
| 2097 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2098 | u16 nid = get_amp_nid(kcontrol); |
| 2099 | u8 chs = get_amp_channels(kcontrol); |
| 2100 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2101 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2103 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2104 | uinfo->count = chs == 3 ? 2 : 1; |
| 2105 | uinfo->value.integer.min = 0; |
| 2106 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2107 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2108 | codec_warn(codec, |
| 2109 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2110 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | return -EINVAL; |
| 2112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | return 0; |
| 2114 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2115 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2117 | |
| 2118 | static inline unsigned int |
| 2119 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2120 | int ch, int dir, int idx, unsigned int ofs) |
| 2121 | { |
| 2122 | unsigned int val; |
| 2123 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2124 | val &= HDA_AMP_VOLMASK; |
| 2125 | if (val >= ofs) |
| 2126 | val -= ofs; |
| 2127 | else |
| 2128 | val = 0; |
| 2129 | return val; |
| 2130 | } |
| 2131 | |
| 2132 | static inline int |
| 2133 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2134 | int ch, int dir, int idx, unsigned int ofs, |
| 2135 | unsigned int val) |
| 2136 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2137 | unsigned int maxval; |
| 2138 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2139 | if (val > 0) |
| 2140 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2141 | /* ofs = 0: raw max value */ |
| 2142 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2143 | if (val > maxval) |
| 2144 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2145 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2146 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2147 | } |
| 2148 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2149 | /** |
| 2150 | * 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] | 2151 | * @kcontrol: ctl element |
| 2152 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2153 | * |
| 2154 | * The control element is supposed to have the private_value field |
| 2155 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2156 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2157 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2158 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | { |
| 2160 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2161 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2162 | int chs = get_amp_channels(kcontrol); |
| 2163 | int dir = get_amp_direction(kcontrol); |
| 2164 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2165 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | long *valp = ucontrol->value.integer.value; |
| 2167 | |
| 2168 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2169 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2171 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | return 0; |
| 2173 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2174 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2176 | /** |
| 2177 | * 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] | 2178 | * @kcontrol: ctl element |
| 2179 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2180 | * |
| 2181 | * The control element is supposed to have the private_value field |
| 2182 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2183 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2184 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2185 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
| 2187 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2188 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2189 | int chs = get_amp_channels(kcontrol); |
| 2190 | int dir = get_amp_direction(kcontrol); |
| 2191 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2192 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | long *valp = ucontrol->value.integer.value; |
| 2194 | int change = 0; |
| 2195 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2196 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2197 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2198 | valp++; |
| 2199 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2200 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2201 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | return change; |
| 2203 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2204 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2206 | /** |
| 2207 | * 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] | 2208 | * @kcontrol: ctl element |
| 2209 | * @op_flag: operation flag |
| 2210 | * @size: byte size of input TLV |
| 2211 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2212 | * |
| 2213 | * The control element is supposed to have the private_value field |
| 2214 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2215 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2216 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2217 | unsigned int size, unsigned int __user *_tlv) |
| 2218 | { |
| 2219 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2220 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2221 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2222 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2223 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2224 | u32 caps, val1, val2; |
| 2225 | |
| 2226 | if (size < 4 * sizeof(unsigned int)) |
| 2227 | return -ENOMEM; |
| 2228 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2229 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2230 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2231 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2232 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2233 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2234 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2235 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2236 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2237 | return -EFAULT; |
| 2238 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2239 | return -EFAULT; |
| 2240 | if (put_user(val1, _tlv + 2)) |
| 2241 | return -EFAULT; |
| 2242 | if (put_user(val2, _tlv + 3)) |
| 2243 | return -EFAULT; |
| 2244 | return 0; |
| 2245 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2246 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2247 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2248 | /** |
| 2249 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2250 | * @codec: HD-audio codec |
| 2251 | * @nid: NID of a reference widget |
| 2252 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2253 | * @tlv: TLV data to be stored, at least 4 elements |
| 2254 | * |
| 2255 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2256 | * obtained from the reference NID. |
| 2257 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2258 | */ |
| 2259 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2260 | unsigned int *tlv) |
| 2261 | { |
| 2262 | u32 caps; |
| 2263 | int nums, step; |
| 2264 | |
| 2265 | caps = query_amp_caps(codec, nid, dir); |
| 2266 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2267 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2268 | step = (step + 1) * 25; |
| 2269 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2270 | tlv[1] = 2 * sizeof(unsigned int); |
| 2271 | tlv[2] = -nums * step; |
| 2272 | tlv[3] = step; |
| 2273 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2274 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2275 | |
| 2276 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2277 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2278 | 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] | 2279 | { |
| 2280 | struct snd_ctl_elem_id id; |
| 2281 | memset(&id, 0, sizeof(id)); |
| 2282 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2283 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2284 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2285 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2286 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2287 | strcpy(id.name, name); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2288 | return snd_ctl_find_id(codec->card, &id); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2289 | } |
| 2290 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2291 | /** |
| 2292 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2293 | * @codec: HD-audio codec |
| 2294 | * @name: ctl id name string |
| 2295 | * |
| 2296 | * Get the control element with the given id string and IFACE_MIXER. |
| 2297 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2298 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2299 | const char *name) |
| 2300 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2301 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2302 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2303 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2304 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2305 | 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] | 2306 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2307 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2308 | int i, idx; |
| 2309 | /* 16 ctlrs should be large enough */ |
| 2310 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2311 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2312 | return idx; |
| 2313 | } |
| 2314 | return -EBUSY; |
| 2315 | } |
| 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_ctl_add - Add a control element and assign to the codec |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2319 | * @codec: HD-audio codec |
| 2320 | * @nid: corresponding NID (optional) |
| 2321 | * @kctl: the control element to assign |
| 2322 | * |
| 2323 | * Add the given control element to an array inside the codec instance. |
| 2324 | * All control elements belonging to a codec are supposed to be added |
| 2325 | * by this function so that a proper clean-up works at the free or |
| 2326 | * reconfiguration time. |
| 2327 | * |
| 2328 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2329 | * The assignment is shown in the codec proc file. |
| 2330 | * |
| 2331 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2332 | * #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] | 2333 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2334 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2335 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2336 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2337 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2338 | { |
| 2339 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2340 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2341 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2342 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2343 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2344 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2345 | if (nid == 0) |
| 2346 | nid = get_amp_nid_(kctl->private_value); |
| 2347 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2348 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2349 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2350 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2351 | kctl->id.subdevice = 0; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2352 | err = snd_ctl_add(codec->card, kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2353 | if (err < 0) |
| 2354 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2355 | item = snd_array_new(&codec->mixers); |
| 2356 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2357 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2358 | item->kctl = kctl; |
| 2359 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2360 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2361 | return 0; |
| 2362 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2363 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2364 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2365 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2366 | * snd_hda_add_nid - Assign a NID to a control element |
| 2367 | * @codec: HD-audio codec |
| 2368 | * @nid: corresponding NID (optional) |
| 2369 | * @kctl: the control element to assign |
| 2370 | * @index: index to kctl |
| 2371 | * |
| 2372 | * Add the given control element to an array inside the codec instance. |
| 2373 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2374 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2375 | */ |
| 2376 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2377 | unsigned int index, hda_nid_t nid) |
| 2378 | { |
| 2379 | struct hda_nid_item *item; |
| 2380 | |
| 2381 | if (nid > 0) { |
| 2382 | item = snd_array_new(&codec->nids); |
| 2383 | if (!item) |
| 2384 | return -ENOMEM; |
| 2385 | item->kctl = kctl; |
| 2386 | item->index = index; |
| 2387 | item->nid = nid; |
| 2388 | return 0; |
| 2389 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2390 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2391 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2392 | return -EINVAL; |
| 2393 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2394 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2395 | |
| 2396 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2397 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2398 | * @codec: HD-audio codec |
| 2399 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2400 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2401 | { |
| 2402 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2403 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2404 | for (i = 0; i < codec->mixers.used; i++) |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 2405 | snd_ctl_remove(codec->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2406 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2407 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2410 | /** |
| 2411 | * snd_hda_lock_devices - pseudo device locking |
| 2412 | * @bus: the BUS |
| 2413 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2414 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2415 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2416 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2417 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2418 | struct snd_card *card = bus->card; |
| 2419 | struct hda_codec *codec; |
| 2420 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2421 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2422 | if (card->shutdown) |
| 2423 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2424 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2425 | if (!list_empty(&card->ctl_files)) |
| 2426 | goto err_clear; |
| 2427 | |
| 2428 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 2429 | struct hda_pcm *cpcm; |
| 2430 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2431 | if (!cpcm->pcm) |
| 2432 | continue; |
| 2433 | if (cpcm->pcm->streams[0].substream_opened || |
| 2434 | cpcm->pcm->streams[1].substream_opened) |
| 2435 | goto err_clear; |
| 2436 | } |
| 2437 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2438 | spin_unlock(&card->files_lock); |
| 2439 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2440 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2441 | err_clear: |
| 2442 | card->shutdown = 0; |
| 2443 | err_unlock: |
| 2444 | spin_unlock(&card->files_lock); |
| 2445 | return -EINVAL; |
| 2446 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2447 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2448 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2449 | /** |
| 2450 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2451 | * @bus: the BUS |
| 2452 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2453 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2454 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2455 | struct snd_card *card = bus->card; |
| 2456 | |
| 2457 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2458 | spin_lock(&card->files_lock); |
| 2459 | card->shutdown = 0; |
| 2460 | spin_unlock(&card->files_lock); |
| 2461 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2462 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2463 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2464 | /** |
| 2465 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2466 | * @codec: HD-audio codec |
| 2467 | * |
| 2468 | * This frees the all PCM and control elements assigned to the codec, and |
| 2469 | * clears the caches and restores the pin default configurations. |
| 2470 | * |
| 2471 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2472 | * returns zero. |
| 2473 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2474 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2475 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2476 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2477 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2478 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2479 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2480 | |
| 2481 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2482 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2483 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2484 | snd_hda_ctls_clear(codec); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 2485 | codec_release_pcms(codec); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2486 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 2487 | if (device_is_registered(hda_codec_dev(codec))) |
| 2488 | device_del(hda_codec_dev(codec)); |
| 2489 | |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2490 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2491 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2492 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2493 | codec->spec = NULL; |
| 2494 | free_hda_cache(&codec->amp_cache); |
| 2495 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2496 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2497 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2498 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2499 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2500 | snd_array_free(&codec->cvt_setups); |
| 2501 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2502 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2503 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2504 | codec->slave_dig_outs = NULL; |
| 2505 | codec->spdif_status_reset = 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2508 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2509 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2510 | } |
| 2511 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2512 | 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] | 2513 | |
| 2514 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2515 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2516 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2517 | { |
| 2518 | struct hda_nid_item *items; |
| 2519 | const char * const *s; |
| 2520 | int i, err; |
| 2521 | |
| 2522 | items = codec->mixers.list; |
| 2523 | for (i = 0; i < codec->mixers.used; i++) { |
| 2524 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2525 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2526 | continue; |
| 2527 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2528 | char tmpname[sizeof(sctl->id.name)]; |
| 2529 | const char *name = *s; |
| 2530 | if (suffix) { |
| 2531 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2532 | name, suffix); |
| 2533 | name = tmpname; |
| 2534 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2535 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2536 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2537 | if (err) |
| 2538 | return err; |
| 2539 | break; |
| 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2546 | static int check_slave_present(struct hda_codec *codec, |
| 2547 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2548 | { |
| 2549 | return 1; |
| 2550 | } |
| 2551 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2552 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2553 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2554 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2555 | { |
| 2556 | int _tlv[4]; |
| 2557 | const int *tlv = NULL; |
| 2558 | int val = -1; |
| 2559 | |
| 2560 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2561 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2562 | mm_segment_t fs = get_fs(); |
| 2563 | set_fs(get_ds()); |
| 2564 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2565 | tlv = _tlv; |
| 2566 | set_fs(fs); |
| 2567 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2568 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2569 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2570 | int step = tlv[3]; |
| 2571 | step &= ~TLV_DB_SCALE_MUTE; |
| 2572 | if (!step) |
| 2573 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2574 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2575 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2576 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2577 | return -1; |
| 2578 | } |
| 2579 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2580 | val = -tlv[2] / step; |
| 2581 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2582 | return val; |
| 2583 | } |
| 2584 | |
| 2585 | /* call kctl->put with the given value(s) */ |
| 2586 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2587 | { |
| 2588 | struct snd_ctl_elem_value *ucontrol; |
| 2589 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2590 | if (!ucontrol) |
| 2591 | return -ENOMEM; |
| 2592 | ucontrol->value.integer.value[0] = val; |
| 2593 | ucontrol->value.integer.value[1] = val; |
| 2594 | kctl->put(kctl, ucontrol); |
| 2595 | kfree(ucontrol); |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
| 2599 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2600 | static int init_slave_0dB(struct hda_codec *codec, |
| 2601 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2602 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2603 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2604 | if (offset > 0) |
| 2605 | put_kctl_with_value(slave, offset); |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
| 2609 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2610 | static int init_slave_unmute(struct hda_codec *codec, |
| 2611 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2612 | { |
| 2613 | return put_kctl_with_value(slave, 1); |
| 2614 | } |
| 2615 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2616 | static int add_slave(struct hda_codec *codec, |
| 2617 | void *data, struct snd_kcontrol *slave) |
| 2618 | { |
| 2619 | return snd_ctl_add_slave(data, slave); |
| 2620 | } |
| 2621 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2622 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2623 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2624 | * @codec: HD-audio codec |
| 2625 | * @name: vmaster control name |
| 2626 | * @tlv: TLV data (optional) |
| 2627 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2628 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2629 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2630 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2631 | * |
| 2632 | * Create a virtual master control with the given name. The TLV data |
| 2633 | * must be either NULL or a valid data. |
| 2634 | * |
| 2635 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2636 | * slave control name. All controls with these names are assigned to |
| 2637 | * the new virtual master control. |
| 2638 | * |
| 2639 | * This function returns zero if successful or a negative error code. |
| 2640 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2641 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2642 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2643 | const char *suffix, bool init_slave_vol, |
| 2644 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2645 | { |
| 2646 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2647 | int err; |
| 2648 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2649 | if (ctl_ret) |
| 2650 | *ctl_ret = NULL; |
| 2651 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2652 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2653 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2654 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2655 | return 0; |
| 2656 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2657 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2658 | if (!kctl) |
| 2659 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2660 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2661 | if (err < 0) |
| 2662 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2663 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2664 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2665 | if (err < 0) |
| 2666 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2667 | |
| 2668 | /* init with master mute & zero volume */ |
| 2669 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2670 | if (init_slave_vol) { |
| 2671 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2672 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2673 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2674 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2675 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2676 | if (ctl_ret) |
| 2677 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2678 | return 0; |
| 2679 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2680 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2681 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2682 | /* |
| 2683 | * mute-LED control using vmaster |
| 2684 | */ |
| 2685 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2686 | struct snd_ctl_elem_info *uinfo) |
| 2687 | { |
| 2688 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2689 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2690 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2691 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2692 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2696 | struct snd_ctl_elem_value *ucontrol) |
| 2697 | { |
| 2698 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2699 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2704 | struct snd_ctl_elem_value *ucontrol) |
| 2705 | { |
| 2706 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2707 | unsigned int old_mode = hook->mute_mode; |
| 2708 | |
| 2709 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2710 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2711 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2712 | if (old_mode == hook->mute_mode) |
| 2713 | return 0; |
| 2714 | snd_hda_sync_vmaster_hook(hook); |
| 2715 | return 1; |
| 2716 | } |
| 2717 | |
| 2718 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2719 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2720 | .name = "Mute-LED Mode", |
| 2721 | .info = vmaster_mute_mode_info, |
| 2722 | .get = vmaster_mute_mode_get, |
| 2723 | .put = vmaster_mute_mode_put, |
| 2724 | }; |
| 2725 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2726 | /** |
| 2727 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2728 | * @codec: the HDA codec |
| 2729 | * @hook: the vmaster hook object |
| 2730 | * @expose_enum_ctl: flag to create an enum ctl |
| 2731 | * |
| 2732 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2733 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2734 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2735 | */ |
| 2736 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2737 | struct hda_vmaster_mute_hook *hook, |
| 2738 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2739 | { |
| 2740 | struct snd_kcontrol *kctl; |
| 2741 | |
| 2742 | if (!hook->hook || !hook->sw_kctl) |
| 2743 | return 0; |
| 2744 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2745 | hook->codec = codec; |
| 2746 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2747 | if (!expose_enum_ctl) |
| 2748 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2749 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2750 | if (!kctl) |
| 2751 | return -ENOMEM; |
| 2752 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2753 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2754 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2755 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2756 | /** |
| 2757 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 2758 | * @hook: the vmaster hook |
| 2759 | * |
| 2760 | * Call the hook with the current value for synchronization. |
| 2761 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2762 | */ |
| 2763 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2764 | { |
| 2765 | if (!hook->hook || !hook->codec) |
| 2766 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2767 | /* don't call vmaster hook in the destructor since it might have |
| 2768 | * been already destroyed |
| 2769 | */ |
| 2770 | if (hook->codec->bus->shutdown) |
| 2771 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2772 | switch (hook->mute_mode) { |
| 2773 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2774 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2775 | break; |
| 2776 | default: |
| 2777 | hook->hook(hook->codec, hook->mute_mode); |
| 2778 | break; |
| 2779 | } |
| 2780 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2781 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2782 | |
| 2783 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2784 | /** |
| 2785 | * 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] | 2786 | * @kcontrol: referred ctl element |
| 2787 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2788 | * |
| 2789 | * The control element is supposed to have the private_value field |
| 2790 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2791 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2792 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2793 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | { |
| 2795 | int chs = get_amp_channels(kcontrol); |
| 2796 | |
| 2797 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2798 | uinfo->count = chs == 3 ? 2 : 1; |
| 2799 | uinfo->value.integer.min = 0; |
| 2800 | uinfo->value.integer.max = 1; |
| 2801 | return 0; |
| 2802 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2803 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2805 | /** |
| 2806 | * 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] | 2807 | * @kcontrol: ctl element |
| 2808 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2809 | * |
| 2810 | * The control element is supposed to have the private_value field |
| 2811 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2812 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2813 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2814 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | { |
| 2816 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2817 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2818 | int chs = get_amp_channels(kcontrol); |
| 2819 | int dir = get_amp_direction(kcontrol); |
| 2820 | int idx = get_amp_index(kcontrol); |
| 2821 | long *valp = ucontrol->value.integer.value; |
| 2822 | |
| 2823 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2824 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2825 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2827 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2828 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | return 0; |
| 2830 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2831 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2833 | /** |
| 2834 | * 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] | 2835 | * @kcontrol: ctl element |
| 2836 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2837 | * |
| 2838 | * The control element is supposed to have the private_value field |
| 2839 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2840 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2841 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2842 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | { |
| 2844 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2845 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2846 | int chs = get_amp_channels(kcontrol); |
| 2847 | int dir = get_amp_direction(kcontrol); |
| 2848 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | long *valp = ucontrol->value.integer.value; |
| 2850 | int change = 0; |
| 2851 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2852 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2853 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 2854 | HDA_AMP_MUTE, |
| 2855 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2856 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2857 | valp++; |
| 2858 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2859 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2860 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 2861 | HDA_AMP_MUTE, |
| 2862 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2863 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2864 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | return change; |
| 2866 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2867 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
| 2869 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2870 | * bound volume controls |
| 2871 | * |
| 2872 | * bind multiple volumes (# indices, from 0) |
| 2873 | */ |
| 2874 | |
| 2875 | #define AMP_VAL_IDX_SHIFT 19 |
| 2876 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2877 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2878 | /** |
| 2879 | * 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] | 2880 | * @kcontrol: ctl element |
| 2881 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2882 | * |
| 2883 | * The control element is supposed to have the private_value field |
| 2884 | * set up via HDA_BIND_MUTE*() macros. |
| 2885 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2886 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2887 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2888 | { |
| 2889 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2890 | unsigned long pval; |
| 2891 | int err; |
| 2892 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2893 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2894 | pval = kcontrol->private_value; |
| 2895 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2896 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2897 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2898 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2899 | return err; |
| 2900 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2901 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2902 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2903 | /** |
| 2904 | * 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] | 2905 | * @kcontrol: ctl element |
| 2906 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2907 | * |
| 2908 | * The control element is supposed to have the private_value field |
| 2909 | * set up via HDA_BIND_MUTE*() macros. |
| 2910 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2911 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2912 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2913 | { |
| 2914 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2915 | unsigned long pval; |
| 2916 | int i, indices, err = 0, change = 0; |
| 2917 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2918 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2919 | pval = kcontrol->private_value; |
| 2920 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2921 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2922 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2923 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2924 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2925 | if (err < 0) |
| 2926 | break; |
| 2927 | change |= err; |
| 2928 | } |
| 2929 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2930 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2931 | return err < 0 ? err : change; |
| 2932 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2933 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2934 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2935 | /** |
| 2936 | * 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] | 2937 | * @kcontrol: referred ctl element |
| 2938 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2939 | * |
| 2940 | * The control element is supposed to have the private_value field |
| 2941 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2942 | */ |
| 2943 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2944 | struct snd_ctl_elem_info *uinfo) |
| 2945 | { |
| 2946 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2947 | struct hda_bind_ctls *c; |
| 2948 | int err; |
| 2949 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2950 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2951 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2952 | kcontrol->private_value = *c->values; |
| 2953 | err = c->ops->info(kcontrol, uinfo); |
| 2954 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2955 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2956 | return err; |
| 2957 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2958 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2959 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2960 | /** |
| 2961 | * 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] | 2962 | * @kcontrol: ctl element |
| 2963 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2964 | * |
| 2965 | * The control element is supposed to have the private_value field |
| 2966 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2967 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2968 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2969 | struct snd_ctl_elem_value *ucontrol) |
| 2970 | { |
| 2971 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2972 | struct hda_bind_ctls *c; |
| 2973 | int err; |
| 2974 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2975 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2976 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2977 | kcontrol->private_value = *c->values; |
| 2978 | err = c->ops->get(kcontrol, ucontrol); |
| 2979 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2980 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2981 | return err; |
| 2982 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2983 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2984 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2985 | /** |
| 2986 | * 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] | 2987 | * @kcontrol: ctl element |
| 2988 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2989 | * |
| 2990 | * The control element is supposed to have the private_value field |
| 2991 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2992 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2993 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2994 | struct snd_ctl_elem_value *ucontrol) |
| 2995 | { |
| 2996 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2997 | struct hda_bind_ctls *c; |
| 2998 | unsigned long *vals; |
| 2999 | int err = 0, change = 0; |
| 3000 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3001 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3002 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3003 | for (vals = c->values; *vals; vals++) { |
| 3004 | kcontrol->private_value = *vals; |
| 3005 | err = c->ops->put(kcontrol, ucontrol); |
| 3006 | if (err < 0) |
| 3007 | break; |
| 3008 | change |= err; |
| 3009 | } |
| 3010 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3011 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3012 | return err < 0 ? err : change; |
| 3013 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3014 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3015 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3016 | /** |
| 3017 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 3018 | * @kcontrol: ctl element |
| 3019 | * @op_flag: operation flag |
| 3020 | * @size: byte size of input TLV |
| 3021 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3022 | * |
| 3023 | * The control element is supposed to have the private_value field |
| 3024 | * set up via HDA_BIND_VOL() macro. |
| 3025 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3026 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 3027 | unsigned int size, unsigned int __user *tlv) |
| 3028 | { |
| 3029 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3030 | struct hda_bind_ctls *c; |
| 3031 | int err; |
| 3032 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3033 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 3034 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3035 | kcontrol->private_value = *c->values; |
| 3036 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3037 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3038 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3039 | return err; |
| 3040 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3041 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3042 | |
| 3043 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3044 | .info = snd_hda_mixer_amp_volume_info, |
| 3045 | .get = snd_hda_mixer_amp_volume_get, |
| 3046 | .put = snd_hda_mixer_amp_volume_put, |
| 3047 | .tlv = snd_hda_mixer_amp_tlv |
| 3048 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3049 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3050 | |
| 3051 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3052 | .info = snd_hda_mixer_amp_switch_info, |
| 3053 | .get = snd_hda_mixer_amp_switch_get, |
| 3054 | .put = snd_hda_mixer_amp_switch_put, |
| 3055 | .tlv = snd_hda_mixer_amp_tlv |
| 3056 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3057 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3058 | |
| 3059 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | * SPDIF out controls |
| 3061 | */ |
| 3062 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3063 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3064 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | { |
| 3066 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3067 | uinfo->count = 1; |
| 3068 | return 0; |
| 3069 | } |
| 3070 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3071 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3072 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | { |
| 3074 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3075 | IEC958_AES0_NONAUDIO | |
| 3076 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3077 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3078 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3079 | IEC958_AES1_CON_ORIGINAL; |
| 3080 | return 0; |
| 3081 | } |
| 3082 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3084 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | { |
| 3086 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3087 | IEC958_AES0_NONAUDIO | |
| 3088 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3092 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3093 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | { |
| 3095 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3096 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3097 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3099 | mutex_lock(&codec->spdif_mutex); |
| 3100 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3101 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3102 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3103 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3104 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3105 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | return 0; |
| 3108 | } |
| 3109 | |
| 3110 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3111 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3112 | */ |
| 3113 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3114 | { |
| 3115 | unsigned short val = 0; |
| 3116 | |
| 3117 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3118 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3120 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3122 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3123 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3124 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3126 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3127 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3128 | val |= AC_DIG1_EMPHASIS; |
| 3129 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3130 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3132 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3134 | } |
| 3135 | return val; |
| 3136 | } |
| 3137 | |
| 3138 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3139 | */ |
| 3140 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3141 | { |
| 3142 | unsigned int sbits = 0; |
| 3143 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3144 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3146 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3148 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3149 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3151 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3152 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3154 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3156 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3158 | sbits |= val & (0x7f << 8); |
| 3159 | } |
| 3160 | return sbits; |
| 3161 | } |
| 3162 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3163 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3164 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3165 | int verb, int val) |
| 3166 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3167 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3168 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3169 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3170 | d = codec->slave_dig_outs; |
| 3171 | if (!d) |
| 3172 | return; |
| 3173 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3174 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3178 | int dig1, int dig2) |
| 3179 | { |
| 3180 | if (dig1 != -1) |
| 3181 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3182 | if (dig2 != -1) |
| 3183 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3184 | } |
| 3185 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3186 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3187 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | { |
| 3189 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3190 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3191 | struct hda_spdif_out *spdif; |
| 3192 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | unsigned short val; |
| 3194 | int change; |
| 3195 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3196 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3197 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3198 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3199 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3201 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3202 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3203 | val = convert_from_spdif_status(spdif->status); |
| 3204 | val |= spdif->ctls & 1; |
| 3205 | change = spdif->ctls != val; |
| 3206 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3207 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3208 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3209 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | return change; |
| 3211 | } |
| 3212 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3213 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3215 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3216 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | { |
| 3218 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3219 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3220 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3222 | mutex_lock(&codec->spdif_mutex); |
| 3223 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3224 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3225 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | return 0; |
| 3227 | } |
| 3228 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3229 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3230 | int dig1, int dig2) |
| 3231 | { |
| 3232 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3233 | /* unmute amp switch (if any) */ |
| 3234 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3235 | (dig1 & AC_DIG1_ENABLE)) |
| 3236 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3237 | HDA_AMP_MUTE, 0); |
| 3238 | } |
| 3239 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3240 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3241 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | { |
| 3243 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3244 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3245 | struct hda_spdif_out *spdif; |
| 3246 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | unsigned short val; |
| 3248 | int change; |
| 3249 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3250 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3251 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3252 | nid = spdif->nid; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3253 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3255 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3256 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3257 | spdif->ctls = val; |
| 3258 | if (change && nid != (u16)-1) |
| 3259 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3260 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | return change; |
| 3262 | } |
| 3263 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3264 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
| 3266 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3267 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3268 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | .info = snd_hda_spdif_mask_info, |
| 3270 | .get = snd_hda_spdif_cmask_get, |
| 3271 | }, |
| 3272 | { |
| 3273 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3274 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3275 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | .info = snd_hda_spdif_mask_info, |
| 3277 | .get = snd_hda_spdif_pmask_get, |
| 3278 | }, |
| 3279 | { |
| 3280 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3281 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | .info = snd_hda_spdif_mask_info, |
| 3283 | .get = snd_hda_spdif_default_get, |
| 3284 | .put = snd_hda_spdif_default_put, |
| 3285 | }, |
| 3286 | { |
| 3287 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3288 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | .info = snd_hda_spdif_out_switch_info, |
| 3290 | .get = snd_hda_spdif_out_switch_get, |
| 3291 | .put = snd_hda_spdif_out_switch_put, |
| 3292 | }, |
| 3293 | { } /* end */ |
| 3294 | }; |
| 3295 | |
| 3296 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3297 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3299 | * @associated_nid: NID that new ctls associated with |
| 3300 | * @cvt_nid: converter NID |
| 3301 | * @type: HDA_PCM_TYPE_* |
| 3302 | * Creates controls related with the digital output. |
| 3303 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | * |
| 3305 | * Returns 0 if successful, or a negative error code. |
| 3306 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3307 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3308 | hda_nid_t associated_nid, |
| 3309 | hda_nid_t cvt_nid, |
| 3310 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | { |
| 3312 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3313 | struct snd_kcontrol *kctl; |
| 3314 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3315 | int idx = 0; |
| 3316 | const int spdif_index = 16; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3317 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3318 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3320 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3321 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3322 | idx = spdif_index; |
| 3323 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3324 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3325 | /* suppose a single SPDIF device */ |
| 3326 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3327 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3328 | if (!kctl) |
| 3329 | break; |
| 3330 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3331 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3332 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3333 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3334 | if (!bus->primary_dig_out_type) |
| 3335 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3336 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3337 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3338 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3339 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3340 | return -EBUSY; |
| 3341 | } |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3342 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3343 | if (!spdif) |
| 3344 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3346 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3347 | if (!kctl) |
| 3348 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3349 | kctl->id.index = idx; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3350 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3351 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3352 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | return err; |
| 3354 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3355 | spdif->nid = cvt_nid; |
| 3356 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3357 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3358 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | return 0; |
| 3360 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3361 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3363 | /** |
| 3364 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3365 | * @codec: the HDA codec |
| 3366 | * @nid: widget NID |
| 3367 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3368 | * call within spdif_mutex lock |
| 3369 | */ |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3370 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3371 | hda_nid_t nid) |
| 3372 | { |
| 3373 | int i; |
| 3374 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3375 | struct hda_spdif_out *spdif = |
| 3376 | snd_array_elem(&codec->spdif_out, i); |
| 3377 | if (spdif->nid == nid) |
| 3378 | return spdif; |
| 3379 | } |
| 3380 | return NULL; |
| 3381 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3382 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3383 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3384 | /** |
| 3385 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3386 | * @codec: the HDA codec |
| 3387 | * @idx: the SPDIF ctl index |
| 3388 | * |
| 3389 | * Unassign the widget from the given SPDIF control. |
| 3390 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3391 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3392 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3393 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3394 | |
| 3395 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3396 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3397 | spdif->nid = (u16)-1; |
| 3398 | mutex_unlock(&codec->spdif_mutex); |
| 3399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3400 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3401 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3402 | /** |
| 3403 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3404 | * @codec: the HDA codec |
| 3405 | * @idx: the SPDIF ctl idx |
| 3406 | * @nid: widget NID |
| 3407 | * |
| 3408 | * Assign the widget to the SPDIF control with the given index. |
| 3409 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3410 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3411 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3412 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3413 | unsigned short val; |
| 3414 | |
| 3415 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3416 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3417 | if (spdif->nid != nid) { |
| 3418 | spdif->nid = nid; |
| 3419 | val = spdif->ctls; |
| 3420 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3421 | } |
| 3422 | mutex_unlock(&codec->spdif_mutex); |
| 3423 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3424 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3427 | * SPDIF sharing with analog output |
| 3428 | */ |
| 3429 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3430 | struct snd_ctl_elem_value *ucontrol) |
| 3431 | { |
| 3432 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3433 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3434 | return 0; |
| 3435 | } |
| 3436 | |
| 3437 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3438 | struct snd_ctl_elem_value *ucontrol) |
| 3439 | { |
| 3440 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3441 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3442 | return 0; |
| 3443 | } |
| 3444 | |
| 3445 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3446 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3447 | .name = "IEC958 Default PCM Playback Switch", |
| 3448 | .info = snd_ctl_boolean_mono_info, |
| 3449 | .get = spdif_share_sw_get, |
| 3450 | .put = spdif_share_sw_put, |
| 3451 | }; |
| 3452 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3453 | /** |
| 3454 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3455 | * @codec: the HDA codec |
| 3456 | * @mout: multi-out instance |
| 3457 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3458 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3459 | struct hda_multi_out *mout) |
| 3460 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3461 | struct snd_kcontrol *kctl; |
| 3462 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3463 | if (!mout->dig_out_nid) |
| 3464 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3465 | |
| 3466 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3467 | if (!kctl) |
| 3468 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3469 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3470 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3471 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3472 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3473 | |
| 3474 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | * SPDIF input |
| 3476 | */ |
| 3477 | |
| 3478 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3479 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3480 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3481 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | { |
| 3483 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3484 | |
| 3485 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3489 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3490 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | { |
| 3492 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3493 | hda_nid_t nid = kcontrol->private_value; |
| 3494 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3495 | int change; |
| 3496 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3497 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3499 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3501 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3502 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3504 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | return change; |
| 3506 | } |
| 3507 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3508 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3509 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | { |
| 3511 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3512 | hda_nid_t nid = kcontrol->private_value; |
| 3513 | unsigned short val; |
| 3514 | unsigned int sbits; |
| 3515 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3516 | 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] | 3517 | sbits = convert_to_spdif_status(val); |
| 3518 | ucontrol->value.iec958.status[0] = sbits; |
| 3519 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3520 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3521 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3522 | return 0; |
| 3523 | } |
| 3524 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3525 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | { |
| 3527 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3528 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | .info = snd_hda_spdif_in_switch_info, |
| 3530 | .get = snd_hda_spdif_in_switch_get, |
| 3531 | .put = snd_hda_spdif_in_switch_put, |
| 3532 | }, |
| 3533 | { |
| 3534 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3535 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3536 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | .info = snd_hda_spdif_mask_info, |
| 3538 | .get = snd_hda_spdif_in_status_get, |
| 3539 | }, |
| 3540 | { } /* end */ |
| 3541 | }; |
| 3542 | |
| 3543 | /** |
| 3544 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3545 | * @codec: the HDA codec |
| 3546 | * @nid: audio in widget NID |
| 3547 | * |
| 3548 | * Creates controls related with the SPDIF input. |
| 3549 | * Called from each patch supporting the SPDIF in. |
| 3550 | * |
| 3551 | * Returns 0 if successful, or a negative error code. |
| 3552 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3553 | 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] | 3554 | { |
| 3555 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3556 | struct snd_kcontrol *kctl; |
| 3557 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3558 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3560 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3561 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3562 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3563 | return -EBUSY; |
| 3564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3566 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3567 | if (!kctl) |
| 3568 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3570 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3571 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | return err; |
| 3573 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3574 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3575 | snd_hda_codec_read(codec, nid, 0, |
| 3576 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3577 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | return 0; |
| 3579 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3580 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3582 | /* |
| 3583 | * command cache |
| 3584 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3586 | /* 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] | 3587 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3588 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3589 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3590 | |
| 3591 | /** |
| 3592 | * snd_hda_codec_write_cache - send a single command with caching |
| 3593 | * @codec: the HDA codec |
| 3594 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3595 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3596 | * @verb: the verb to send |
| 3597 | * @parm: the parameter for the verb |
| 3598 | * |
| 3599 | * Send a single command without waiting for response. |
| 3600 | * |
| 3601 | * Returns 0 if successful, or a negative error code. |
| 3602 | */ |
| 3603 | 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] | 3604 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3605 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3606 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3607 | struct hda_cache_head *c; |
| 3608 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3609 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3610 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3611 | cache_only = codec->cached_write; |
| 3612 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3613 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3614 | if (err < 0) |
| 3615 | return err; |
| 3616 | } |
| 3617 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3618 | /* parm may contain the verb stuff for get/set amp */ |
| 3619 | verb = verb | (parm >> 8); |
| 3620 | parm &= 0xff; |
| 3621 | key = build_cmd_cache_key(nid, verb); |
| 3622 | mutex_lock(&codec->bus->cmd_mutex); |
| 3623 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3624 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3625 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3626 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3627 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3628 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3629 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3630 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3631 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3632 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3633 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3634 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3635 | * @codec: the HDA codec |
| 3636 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3637 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3638 | * @verb: the verb to send |
| 3639 | * @parm: the parameter for the verb |
| 3640 | * |
| 3641 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3642 | * command if the parameter is already identical with the cached value. |
| 3643 | * If not, it sends the command and refreshes the cache. |
| 3644 | * |
| 3645 | * Returns 0 if successful, or a negative error code. |
| 3646 | */ |
| 3647 | 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] | 3648 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3649 | { |
| 3650 | struct hda_cache_head *c; |
| 3651 | u32 key; |
| 3652 | |
| 3653 | /* parm may contain the verb stuff for get/set amp */ |
| 3654 | verb = verb | (parm >> 8); |
| 3655 | parm &= 0xff; |
| 3656 | key = build_cmd_cache_key(nid, verb); |
| 3657 | mutex_lock(&codec->bus->cmd_mutex); |
| 3658 | c = get_hash(&codec->cmd_cache, key); |
| 3659 | if (c && c->val == parm) { |
| 3660 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3661 | return 0; |
| 3662 | } |
| 3663 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3664 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3665 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3666 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3667 | |
| 3668 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3669 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3670 | * @codec: HD-audio codec |
| 3671 | * |
| 3672 | * Execute all verbs recorded in the command caches to resume. |
| 3673 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3674 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3675 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3676 | int i; |
| 3677 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3678 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3679 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3680 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3681 | struct hda_cache_head *buffer; |
| 3682 | u32 key; |
| 3683 | |
| 3684 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3685 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3686 | if (!key) |
| 3687 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3688 | if (!buffer->dirty) |
| 3689 | continue; |
| 3690 | buffer->dirty = 0; |
| 3691 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3692 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3693 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3694 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3695 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3696 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3697 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3698 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3699 | |
| 3700 | /** |
| 3701 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3702 | * @codec: the HDA codec |
| 3703 | * @seq: VERB array to send |
| 3704 | * |
| 3705 | * Send the commands sequentially from the given array. |
| 3706 | * Thte commands are recorded on cache for power-save and resume. |
| 3707 | * The array must be terminated with NID=0. |
| 3708 | */ |
| 3709 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3710 | const struct hda_verb *seq) |
| 3711 | { |
| 3712 | for (; seq->nid; seq++) |
| 3713 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3714 | seq->param); |
| 3715 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3716 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3717 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3718 | /** |
| 3719 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3720 | * @codec: HD-audio codec |
| 3721 | */ |
| 3722 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3723 | { |
| 3724 | snd_hda_codec_resume_amp(codec); |
| 3725 | snd_hda_codec_resume_cache(codec); |
| 3726 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3727 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3728 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3729 | /** |
| 3730 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3731 | * @codec: the HDA codec |
| 3732 | * @fg: function group (not used now) |
| 3733 | * @power_state: the power state to set (AC_PWRST_*) |
| 3734 | * |
| 3735 | * Set the given power state to all widgets that have the power control. |
| 3736 | * If the codec has power_filter set, it evaluates the power state and |
| 3737 | * filter out if it's unchanged as D3. |
| 3738 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3739 | 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] | 3740 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3741 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3742 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3743 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3744 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3745 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3746 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3747 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3748 | if (!(wcaps & AC_WCAP_POWER)) |
| 3749 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3750 | if (codec->power_filter) { |
| 3751 | state = codec->power_filter(codec, nid, power_state); |
| 3752 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3753 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3754 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3755 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3756 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3757 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3758 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3759 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3760 | |
| 3761 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3762 | * supported power states check |
| 3763 | */ |
| 3764 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3765 | unsigned int power_state) |
| 3766 | { |
| 3767 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3768 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3769 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3770 | return false; |
| 3771 | if (sup & power_state) |
| 3772 | return true; |
| 3773 | else |
| 3774 | return false; |
| 3775 | } |
| 3776 | |
| 3777 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3778 | * wait until the state is reached, returns the current state |
| 3779 | */ |
| 3780 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3781 | hda_nid_t fg, |
| 3782 | unsigned int power_state) |
| 3783 | { |
| 3784 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3785 | unsigned int state, actual_state; |
| 3786 | |
| 3787 | for (;;) { |
| 3788 | state = snd_hda_codec_read(codec, fg, 0, |
| 3789 | AC_VERB_GET_POWER_STATE, 0); |
| 3790 | if (state & AC_PWRST_ERROR) |
| 3791 | break; |
| 3792 | actual_state = (state >> 4) & 0x0f; |
| 3793 | if (actual_state == power_state) |
| 3794 | break; |
| 3795 | if (time_after_eq(jiffies, end_time)) |
| 3796 | break; |
| 3797 | /* wait until the codec reachs to the target state */ |
| 3798 | msleep(1); |
| 3799 | } |
| 3800 | return state; |
| 3801 | } |
| 3802 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3803 | /** |
| 3804 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 3805 | * @codec: the HDA codec |
| 3806 | * @nid: widget NID |
| 3807 | * @power_state: power state to evalue |
| 3808 | * |
| 3809 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 3810 | * This can be used a codec power_filter callback. |
| 3811 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3812 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3813 | hda_nid_t nid, |
| 3814 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3815 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3816 | if (nid == codec->afg || nid == codec->mfg) |
| 3817 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3818 | if (power_state == AC_PWRST_D3 && |
| 3819 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3820 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3821 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3822 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3823 | if (eapd & 0x02) |
| 3824 | return AC_PWRST_D0; |
| 3825 | } |
| 3826 | return power_state; |
| 3827 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3828 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3829 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3830 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3831 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3832 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3833 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3834 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3835 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3836 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3837 | int count; |
| 3838 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3839 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3840 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3841 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3842 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3843 | if (codec->depop_delay < 0) |
| 3844 | msleep(codec->epss ? 10 : 100); |
| 3845 | else if (codec->depop_delay > 0) |
| 3846 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3847 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3848 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3849 | |
| 3850 | /* repeat power states setting at most 10 times*/ |
| 3851 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3852 | if (codec->patch_ops.set_power_state) |
| 3853 | codec->patch_ops.set_power_state(codec, fg, |
| 3854 | power_state); |
| 3855 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3856 | state = power_state; |
| 3857 | if (codec->power_filter) |
| 3858 | state = codec->power_filter(codec, fg, state); |
| 3859 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3860 | snd_hda_codec_read(codec, fg, flags, |
| 3861 | AC_VERB_SET_POWER_STATE, |
| 3862 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3863 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3864 | } |
| 3865 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3866 | if (!(state & AC_PWRST_ERROR)) |
| 3867 | break; |
| 3868 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3869 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3870 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3871 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3872 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3873 | /* sync power states of all widgets; |
| 3874 | * this is called at the end of codec parsing |
| 3875 | */ |
| 3876 | static void sync_power_up_states(struct hda_codec *codec) |
| 3877 | { |
| 3878 | hda_nid_t nid = codec->start_nid; |
| 3879 | int i; |
| 3880 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3881 | /* don't care if no filter is used */ |
| 3882 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3883 | return; |
| 3884 | |
| 3885 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3886 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3887 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3888 | if (!(wcaps & AC_WCAP_POWER)) |
| 3889 | continue; |
| 3890 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3891 | if (target == AC_PWRST_D0) |
| 3892 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3893 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3894 | snd_hda_codec_write(codec, nid, 0, |
| 3895 | AC_VERB_SET_POWER_STATE, target); |
| 3896 | } |
| 3897 | } |
| 3898 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 3899 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3900 | /* execute additional init verbs */ |
| 3901 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3902 | { |
| 3903 | if (codec->init_verbs.list) |
| 3904 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3905 | } |
| 3906 | #else |
| 3907 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3908 | #endif |
| 3909 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3910 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3911 | /* update the power on/off account with the current jiffies */ |
| 3912 | static void update_power_acct(struct hda_codec *codec, bool on) |
| 3913 | { |
| 3914 | unsigned long delta = jiffies - codec->power_jiffies; |
| 3915 | |
| 3916 | if (on) |
| 3917 | codec->power_on_acct += delta; |
| 3918 | else |
| 3919 | codec->power_off_acct += delta; |
| 3920 | codec->power_jiffies += delta; |
| 3921 | } |
| 3922 | |
| 3923 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 3924 | { |
| 3925 | update_power_acct(codec, hda_codec_is_power_on(codec)); |
| 3926 | } |
| 3927 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3928 | /* |
| 3929 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3930 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3931 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3932 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3933 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3934 | unsigned int state; |
| 3935 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3936 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3937 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3938 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3939 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3940 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3941 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3942 | trace_hda_power_down(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3943 | update_power_acct(codec, true); |
| 3944 | atomic_dec(&codec->in_pm); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3945 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3948 | /* mark all entries of cmd and amp caches dirty */ |
| 3949 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3950 | { |
| 3951 | int i; |
| 3952 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3953 | struct hda_cache_head *cmd; |
| 3954 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3955 | cmd->dirty = 1; |
| 3956 | } |
| 3957 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3958 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3959 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3960 | amp->head.dirty = 1; |
| 3961 | } |
| 3962 | } |
| 3963 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3964 | /* |
| 3965 | * kick up codec; used both from PM and power-save |
| 3966 | */ |
| 3967 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3968 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3969 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3970 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3971 | trace_hda_power_up(codec); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3972 | hda_mark_cmd_cache_dirty(codec); |
| 3973 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3974 | codec->power_jiffies = jiffies; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3975 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3976 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3977 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3978 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3979 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3980 | if (codec->patch_ops.resume) |
| 3981 | codec->patch_ops.resume(codec); |
| 3982 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3983 | if (codec->patch_ops.init) |
| 3984 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3985 | snd_hda_codec_resume_amp(codec); |
| 3986 | snd_hda_codec_resume_cache(codec); |
| 3987 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3988 | |
| 3989 | if (codec->jackpoll_interval) |
| 3990 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3991 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3992 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3993 | atomic_dec(&codec->in_pm); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3994 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3995 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3996 | static int hda_codec_runtime_suspend(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3997 | { |
| 3998 | struct hda_codec *codec = dev_to_hda_codec(dev); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 3999 | struct hda_pcm *pcm; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4000 | unsigned int state; |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4001 | |
| 4002 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4003 | list_for_each_entry(pcm, &codec->pcm_list_head, list) |
| 4004 | snd_pcm_suspend_all(pcm->pcm); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4005 | state = hda_call_codec_suspend(codec); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 4006 | if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK)) |
| 4007 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4008 | return 0; |
| 4009 | } |
| 4010 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4011 | static int hda_codec_runtime_resume(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4012 | { |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 4013 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 4014 | |
| 4015 | set_bit(codec->addr, &codec->bus->codec_powered); |
| 4016 | hda_call_codec_resume(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4017 | pm_runtime_mark_last_busy(dev); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4018 | return 0; |
| 4019 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 4020 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4021 | |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4022 | /* referred in hda_bind.c */ |
| 4023 | const struct dev_pm_ops hda_codec_driver_pm = { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4024 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 4025 | pm_runtime_force_resume) |
| 4026 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, |
| 4027 | NULL) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 4028 | }; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | /** |
| 4031 | * snd_hda_build_controls - build mixer controls |
| 4032 | * @bus: the BUS |
| 4033 | * |
| 4034 | * Creates mixer controls for each codec included in the bus. |
| 4035 | * |
| 4036 | * Returns 0 if successful, otherwise a negative error code. |
| 4037 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4038 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4040 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4042 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4043 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4044 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4045 | codec_err(codec, |
| 4046 | "cannot build controls for #%d (error %d)\n", |
| 4047 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4048 | err = snd_hda_codec_reset(codec); |
| 4049 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4050 | codec_err(codec, |
| 4051 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4052 | return err; |
| 4053 | } |
| 4054 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4056 | return 0; |
| 4057 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4058 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4060 | /* |
| 4061 | * add standard channel maps if not specified |
| 4062 | */ |
| 4063 | static int add_std_chmaps(struct hda_codec *codec) |
| 4064 | { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4065 | struct hda_pcm *pcm; |
| 4066 | int str, err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4067 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4068 | list_for_each_entry(pcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4069 | for (str = 0; str < 2; str++) { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4070 | struct hda_pcm_stream *hinfo = &pcm->stream[str]; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4071 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4072 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4073 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4074 | if (pcm->own_chmap) |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4075 | continue; |
| 4076 | if (!pcm || !hinfo->substreams) |
| 4077 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4078 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4079 | err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4080 | hinfo->channels_max, |
| 4081 | 0, &chmap); |
| 4082 | if (err < 0) |
| 4083 | return err; |
| 4084 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4085 | } |
| 4086 | } |
| 4087 | return 0; |
| 4088 | } |
| 4089 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4090 | /* default channel maps for 2.1 speakers; |
| 4091 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4092 | */ |
| 4093 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4094 | { .channels = 2, |
| 4095 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4096 | { .channels = 4, |
| 4097 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4098 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4099 | { } |
| 4100 | }; |
| 4101 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4102 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4103 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4104 | { |
| 4105 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4106 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4107 | /* continue to initialize... */ |
| 4108 | if (codec->patch_ops.init) |
| 4109 | err = codec->patch_ops.init(codec); |
| 4110 | if (!err && codec->patch_ops.build_controls) |
| 4111 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4112 | if (err < 0) |
| 4113 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4114 | |
| 4115 | /* we create chmaps here instead of build_pcms */ |
| 4116 | err = add_std_chmaps(codec); |
| 4117 | if (err < 0) |
| 4118 | return err; |
| 4119 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4120 | if (codec->jackpoll_interval) |
| 4121 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4122 | else |
| 4123 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4124 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | return 0; |
| 4126 | } |
| 4127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | /* |
| 4129 | * stream formats |
| 4130 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4131 | struct hda_rate_tbl { |
| 4132 | unsigned int hz; |
| 4133 | unsigned int alsa_bits; |
| 4134 | unsigned int hda_fmt; |
| 4135 | }; |
| 4136 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4137 | /* rate = base * mult / div */ |
| 4138 | #define HDA_RATE(base, mult, div) \ |
| 4139 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4140 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4141 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4142 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4144 | |
| 4145 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4146 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4147 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4148 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4149 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4150 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4151 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4152 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4153 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4154 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4155 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4156 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4157 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4158 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4159 | |
| 4160 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4161 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4162 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4163 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | }; |
| 4165 | |
| 4166 | /** |
| 4167 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4168 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | * @rate: the sample rate |
| 4170 | * @channels: the number of channels |
| 4171 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4172 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4173 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | * |
| 4175 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4176 | * |
| 4177 | * Return zero if invalid. |
| 4178 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4179 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4180 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | unsigned int channels, |
| 4182 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4183 | unsigned int maxbps, |
| 4184 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | { |
| 4186 | int i; |
| 4187 | unsigned int val = 0; |
| 4188 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4189 | for (i = 0; rate_bits[i].hz; i++) |
| 4190 | if (rate_bits[i].hz == rate) { |
| 4191 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | break; |
| 4193 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4194 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4195 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | return 0; |
| 4197 | } |
| 4198 | |
| 4199 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4200 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | return 0; |
| 4202 | } |
| 4203 | val |= channels - 1; |
| 4204 | |
| 4205 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4206 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4207 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4208 | break; |
| 4209 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4210 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4211 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | case 20: |
| 4213 | case 24: |
| 4214 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4215 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4216 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4218 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4220 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | break; |
| 4222 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4223 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4224 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | return 0; |
| 4226 | } |
| 4227 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4228 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4229 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | return val; |
| 4232 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4233 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4235 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4236 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4237 | { |
| 4238 | unsigned int val = 0; |
| 4239 | if (nid != codec->afg && |
| 4240 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4241 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4242 | if (!val || val == -1) |
| 4243 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4244 | if (!val || val == -1) |
| 4245 | return 0; |
| 4246 | return val; |
| 4247 | } |
| 4248 | |
| 4249 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4250 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4251 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4252 | get_pcm_param); |
| 4253 | } |
| 4254 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4255 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4256 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4257 | { |
| 4258 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4259 | if (!streams || streams == -1) |
| 4260 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4261 | if (!streams || streams == -1) |
| 4262 | return 0; |
| 4263 | return streams; |
| 4264 | } |
| 4265 | |
| 4266 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4267 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4268 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4269 | get_stream_param); |
| 4270 | } |
| 4271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | /** |
| 4273 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4274 | * @codec: the HDA codec |
| 4275 | * @nid: NID to query |
| 4276 | * @ratesp: the pointer to store the detected rate bitflags |
| 4277 | * @formatsp: the pointer to store the detected formats |
| 4278 | * @bpsp: the pointer to store the detected format widths |
| 4279 | * |
| 4280 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4281 | * or @bsps argument is ignored. |
| 4282 | * |
| 4283 | * Returns 0 if successful, otherwise a negative error code. |
| 4284 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4285 | 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] | 4286 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4287 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4288 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4290 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4291 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | |
| 4293 | if (ratesp) { |
| 4294 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4295 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4297 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4299 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4300 | codec_err(codec, |
| 4301 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4302 | nid, val, |
| 4303 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4304 | return -EIO; |
| 4305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | *ratesp = rates; |
| 4307 | } |
| 4308 | |
| 4309 | if (formatsp || bpsp) { |
| 4310 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4311 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4313 | streams = query_stream_param(codec, nid); |
| 4314 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | |
| 4317 | bps = 0; |
| 4318 | if (streams & AC_SUPFMT_PCM) { |
| 4319 | if (val & AC_SUPPCM_BITS_8) { |
| 4320 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4321 | bps = 8; |
| 4322 | } |
| 4323 | if (val & AC_SUPPCM_BITS_16) { |
| 4324 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4325 | bps = 16; |
| 4326 | } |
| 4327 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4328 | if (val & AC_SUPPCM_BITS_32) |
| 4329 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4330 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4331 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4332 | if (val & AC_SUPPCM_BITS_24) |
| 4333 | bps = 24; |
| 4334 | else if (val & AC_SUPPCM_BITS_20) |
| 4335 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4336 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4337 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4339 | if (val & AC_SUPPCM_BITS_32) |
| 4340 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | else if (val & AC_SUPPCM_BITS_24) |
| 4342 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4343 | else if (val & AC_SUPPCM_BITS_20) |
| 4344 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | } |
| 4346 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4347 | #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] | 4348 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4350 | if (!bps) |
| 4351 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4352 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4353 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4354 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4355 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | /* temporary hack: we have still no proper support |
| 4357 | * for the direct AC3 stream... |
| 4358 | */ |
| 4359 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4360 | bps = 8; |
| 4361 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4362 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4363 | codec_err(codec, |
| 4364 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4365 | nid, val, |
| 4366 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4367 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4368 | return -EIO; |
| 4369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | if (formatsp) |
| 4371 | *formatsp = formats; |
| 4372 | if (bpsp) |
| 4373 | *bpsp = bps; |
| 4374 | } |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4378 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
| 4380 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4381 | * snd_hda_is_supported_format - Check the validity of the format |
| 4382 | * @codec: HD-audio codec |
| 4383 | * @nid: NID to check |
| 4384 | * @format: the HD-audio format value to check |
| 4385 | * |
| 4386 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | * |
| 4388 | * Returns 1 if supported, 0 if not. |
| 4389 | */ |
| 4390 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4391 | unsigned int format) |
| 4392 | { |
| 4393 | int i; |
| 4394 | unsigned int val = 0, rate, stream; |
| 4395 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4396 | val = query_pcm_param(codec, nid); |
| 4397 | if (!val) |
| 4398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
| 4400 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4401 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4402 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | if (val & (1 << i)) |
| 4404 | break; |
| 4405 | return 0; |
| 4406 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4407 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | return 0; |
| 4409 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4410 | stream = query_stream_param(codec, nid); |
| 4411 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | return 0; |
| 4413 | |
| 4414 | if (stream & AC_SUPFMT_PCM) { |
| 4415 | switch (format & 0xf0) { |
| 4416 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4417 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | return 0; |
| 4419 | break; |
| 4420 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4421 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | return 0; |
| 4423 | break; |
| 4424 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4425 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | return 0; |
| 4427 | break; |
| 4428 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4429 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | return 0; |
| 4431 | break; |
| 4432 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4433 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | return 0; |
| 4435 | break; |
| 4436 | default: |
| 4437 | return 0; |
| 4438 | } |
| 4439 | } else { |
| 4440 | /* FIXME: check for float32 and AC3? */ |
| 4441 | } |
| 4442 | |
| 4443 | return 1; |
| 4444 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4445 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | |
| 4447 | /* |
| 4448 | * PCM stuff |
| 4449 | */ |
| 4450 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4451 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4452 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | { |
| 4454 | return 0; |
| 4455 | } |
| 4456 | |
| 4457 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4458 | struct hda_codec *codec, |
| 4459 | unsigned int stream_tag, |
| 4460 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4461 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | { |
| 4463 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
| 4467 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4468 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4469 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4471 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | return 0; |
| 4473 | } |
| 4474 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4475 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4476 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4478 | int err; |
| 4479 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4480 | /* query support PCM information from the given NID */ |
| 4481 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4482 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4483 | info->rates ? NULL : &info->rates, |
| 4484 | info->formats ? NULL : &info->formats, |
| 4485 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4486 | if (err < 0) |
| 4487 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | } |
| 4489 | if (info->ops.open == NULL) |
| 4490 | info->ops.open = hda_pcm_default_open_close; |
| 4491 | if (info->ops.close == NULL) |
| 4492 | info->ops.close = hda_pcm_default_open_close; |
| 4493 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4494 | if (snd_BUG_ON(!info->nid)) |
| 4495 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | info->ops.prepare = hda_pcm_default_prepare; |
| 4497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4499 | if (snd_BUG_ON(!info->nid)) |
| 4500 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4502 | } |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4506 | /* |
| 4507 | * codec prepare/cleanup entries |
| 4508 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4509 | /** |
| 4510 | * snd_hda_codec_prepare - Prepare a stream |
| 4511 | * @codec: the HDA codec |
| 4512 | * @hinfo: PCM information |
| 4513 | * @stream: stream tag to assign |
| 4514 | * @format: format id to assign |
| 4515 | * @substream: PCM substream to assign |
| 4516 | * |
| 4517 | * Calls the prepare callback set by the codec with the given arguments. |
| 4518 | * Clean up the inactive streams when successful. |
| 4519 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4520 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4521 | struct hda_pcm_stream *hinfo, |
| 4522 | unsigned int stream, |
| 4523 | unsigned int format, |
| 4524 | struct snd_pcm_substream *substream) |
| 4525 | { |
| 4526 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4527 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | 61ca410 | 2015-02-27 17:57:55 +0100 | [diff] [blame^] | 4528 | if (hinfo->ops.prepare) |
| 4529 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, |
| 4530 | substream); |
| 4531 | else |
| 4532 | ret = -ENODEV; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4533 | if (ret >= 0) |
| 4534 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4535 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4536 | return ret; |
| 4537 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4538 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4539 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4540 | /** |
| 4541 | * snd_hda_codec_cleanup - Prepare a stream |
| 4542 | * @codec: the HDA codec |
| 4543 | * @hinfo: PCM information |
| 4544 | * @substream: PCM substream |
| 4545 | * |
| 4546 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4547 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4548 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4549 | struct hda_pcm_stream *hinfo, |
| 4550 | struct snd_pcm_substream *substream) |
| 4551 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4552 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | 61ca410 | 2015-02-27 17:57:55 +0100 | [diff] [blame^] | 4553 | if (hinfo->ops.cleanup) |
| 4554 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4555 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4556 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4557 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4558 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4559 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4560 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4561 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4562 | }; |
| 4563 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4564 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4565 | * get the empty PCM device number to assign |
| 4566 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4567 | 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] | 4568 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4569 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4570 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4571 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4572 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4573 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4574 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4575 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4576 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4577 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4578 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4579 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4580 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4581 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4582 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4583 | return -EINVAL; |
| 4584 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4585 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4586 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4587 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4588 | if (audio_idx[type][i] >= 8) |
| 4589 | break; |
| 4590 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4591 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4592 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4593 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4594 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4595 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4596 | /* non-fixed slots starting from 10 */ |
| 4597 | for (i = 10; i < 32; i++) { |
| 4598 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4599 | return i; |
| 4600 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4601 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4602 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4603 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4604 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4605 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4606 | dev_warn(bus->card->dev, |
| 4607 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4608 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4609 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4610 | } |
| 4611 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4612 | /* call build_pcms ops of the given codec and set up the default parameters */ |
| 4613 | int snd_hda_codec_parse_pcms(struct hda_codec *codec) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4614 | { |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4615 | struct hda_pcm *cpcm; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4616 | int err; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4617 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4618 | if (!list_empty(&codec->pcm_list_head)) |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4619 | return 0; /* already parsed */ |
| 4620 | |
| 4621 | if (!codec->patch_ops.build_pcms) |
| 4622 | return 0; |
| 4623 | |
| 4624 | err = codec->patch_ops.build_pcms(codec); |
| 4625 | if (err < 0) { |
| 4626 | codec_err(codec, "cannot build PCMs for #%d (error %d)\n", |
| 4627 | codec->addr, err); |
| 4628 | return err; |
| 4629 | } |
| 4630 | |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4631 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4632 | int stream; |
| 4633 | |
| 4634 | for (stream = 0; stream < 2; stream++) { |
| 4635 | struct hda_pcm_stream *info = &cpcm->stream[stream]; |
| 4636 | |
| 4637 | if (!info->substreams) |
| 4638 | continue; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4639 | err = set_pcm_default_values(codec, info); |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4640 | if (err < 0) { |
| 4641 | codec_warn(codec, |
| 4642 | "fail to setup default for PCM %s\n", |
| 4643 | cpcm->name); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4644 | return err; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4645 | } |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4646 | } |
| 4647 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4648 | |
| 4649 | return 0; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4650 | } |
| 4651 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4652 | /* assign all PCMs of the given codec */ |
| 4653 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4654 | { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4655 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4656 | struct hda_pcm *cpcm; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4657 | int dev, err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4658 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4659 | if (snd_BUG_ON(!bus->ops.attach_pcm)) |
| 4660 | return -EINVAL; |
| 4661 | |
| 4662 | err = snd_hda_codec_parse_pcms(codec); |
| 4663 | if (err < 0) { |
| 4664 | snd_hda_codec_reset(codec); |
| 4665 | return err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4666 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4667 | |
| 4668 | /* attach a new PCM streams */ |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4669 | list_for_each_entry(cpcm, &codec->pcm_list_head, list) { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4670 | if (cpcm->pcm) |
| 4671 | continue; /* already attached */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4672 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4673 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4674 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4675 | dev = get_empty_pcm_device(bus, cpcm->pcm_type); |
| 4676 | if (dev < 0) |
| 4677 | continue; /* no fatal error */ |
| 4678 | cpcm->device = dev; |
| 4679 | err = bus->ops.attach_pcm(bus, codec, cpcm); |
| 4680 | if (err < 0) { |
| 4681 | codec_err(codec, |
| 4682 | "cannot attach PCM stream %d for codec #%d\n", |
| 4683 | dev, codec->addr); |
| 4684 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4685 | } |
| 4686 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4687 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4688 | return 0; |
| 4689 | } |
| 4690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | /** |
| 4692 | * snd_hda_build_pcms - build PCM information |
| 4693 | * @bus: the BUS |
| 4694 | * |
| 4695 | * Create PCM information for each codec included in the bus. |
| 4696 | * |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 4697 | * The build_pcms codec patch is requested to create and assign new |
| 4698 | * hda_pcm objects. The codec is responsible to call snd_hda_codec_pcm_new() |
| 4699 | * and fills the fields. Later they are instantiated by this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | * |
| 4701 | * At least, substreams, channels_min and channels_max must be filled for |
| 4702 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4703 | * When rates and/or formats are zero, the supported values are queried |
| 4704 | * from the given nid. The nid is used also by the default ops.prepare |
| 4705 | * and ops.cleanup callbacks. |
| 4706 | * |
| 4707 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4708 | * ops.close is supposed to be called in the close callback. |
| 4709 | * ops.prepare should be called in the prepare or hw_params callback |
| 4710 | * with the proper parameters for set up. |
| 4711 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4712 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4713 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4715 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4717 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4719 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4720 | int err = snd_hda_codec_build_pcms(codec); |
| 4721 | if (err < 0) |
| 4722 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | } |
| 4724 | return 0; |
| 4725 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4726 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | * snd_hda_add_new_ctls - create controls from the array |
| 4730 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4731 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | * |
| 4733 | * This helper function creates and add new controls in the given array. |
| 4734 | * The array must be terminated with an empty entry as terminator. |
| 4735 | * |
| 4736 | * Returns 0 if successful, or a negative error code. |
| 4737 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4738 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4739 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4741 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | |
| 4743 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4744 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4745 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4746 | if (knew->iface == -1) /* skip this codec private value */ |
| 4747 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4748 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4749 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4750 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4751 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4752 | if (addr > 0) |
| 4753 | kctl->id.device = addr; |
| 4754 | if (idx > 0) |
| 4755 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4756 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4757 | if (!err) |
| 4758 | break; |
| 4759 | /* try first with another device index corresponding to |
| 4760 | * the codec addr; if it still fails (or it's the |
| 4761 | * primary codec), then try another control index |
| 4762 | */ |
| 4763 | if (!addr && codec->addr) |
| 4764 | addr = codec->addr; |
| 4765 | else if (!idx && !knew->index) { |
| 4766 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4767 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4768 | if (idx <= 0) |
| 4769 | return err; |
| 4770 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4771 | return err; |
| 4772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | } |
| 4774 | return 0; |
| 4775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4776 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4778 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4779 | /** |
| 4780 | * snd_hda_power_up - Power-up the codec |
| 4781 | * @codec: HD-audio codec |
| 4782 | * |
| 4783 | * Increment the usage counter and resume the device if not done yet. |
| 4784 | */ |
| 4785 | void snd_hda_power_up(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4786 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4787 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4788 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4789 | if (codec_in_pm(codec)) |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4790 | return; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4791 | pm_runtime_get_sync(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4792 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4793 | EXPORT_SYMBOL_GPL(snd_hda_power_up); |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4794 | |
| 4795 | /** |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4796 | * snd_hda_power_down - Power-down the codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4797 | * @codec: HD-audio codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4798 | * |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4799 | * Decrement the usage counter and schedules the autosuspend if none used. |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4800 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4801 | void snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4802 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4803 | struct device *dev = hda_codec_dev(codec); |
| 4804 | |
| 4805 | if (codec_in_pm(codec)) |
| 4806 | return; |
| 4807 | pm_runtime_mark_last_busy(dev); |
| 4808 | pm_runtime_put_autosuspend(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4809 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4810 | EXPORT_SYMBOL_GPL(snd_hda_power_down); |
| 4811 | |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4812 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4813 | { |
| 4814 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4815 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4816 | if (delay > 0) { |
| 4817 | pm_runtime_set_autosuspend_delay(dev, delay); |
| 4818 | pm_runtime_use_autosuspend(dev); |
| 4819 | pm_runtime_allow(dev); |
| 4820 | if (!pm_runtime_suspended(dev)) |
| 4821 | pm_runtime_mark_last_busy(dev); |
| 4822 | } else { |
| 4823 | pm_runtime_dont_use_autosuspend(dev); |
| 4824 | pm_runtime_forbid(dev); |
| 4825 | } |
| 4826 | } |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4827 | |
| 4828 | /** |
| 4829 | * snd_hda_set_power_save - reprogram autosuspend for the given delay |
| 4830 | * @bus: HD-audio bus |
| 4831 | * @delay: autosuspend delay in msec, 0 = off |
| 4832 | * |
| 4833 | * Synchronize the runtime PM autosuspend state from the power_save option. |
| 4834 | */ |
| 4835 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) |
| 4836 | { |
| 4837 | struct hda_codec *c; |
| 4838 | |
| 4839 | list_for_each_entry(c, &bus->codec_list, list) |
| 4840 | codec_set_power_save(c, delay); |
| 4841 | } |
| 4842 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4843 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4844 | /** |
| 4845 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4846 | * @codec: HD-audio codec |
| 4847 | * @check: the object containing an AMP list and the status |
| 4848 | * @nid: NID to check / update |
| 4849 | * |
| 4850 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4851 | * check the current AMP status, and update the the power-status according |
| 4852 | * to the mute status. |
| 4853 | * |
| 4854 | * This function is supposed to be set or called from the check_power_status |
| 4855 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4856 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4857 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4858 | struct hda_loopback_check *check, |
| 4859 | hda_nid_t nid) |
| 4860 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4861 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4862 | int ch, v; |
| 4863 | |
| 4864 | if (!check->amplist) |
| 4865 | return 0; |
| 4866 | for (p = check->amplist; p->nid; p++) { |
| 4867 | if (p->nid == nid) |
| 4868 | break; |
| 4869 | } |
| 4870 | if (!p->nid) |
| 4871 | return 0; /* nothing changed */ |
| 4872 | |
| 4873 | for (p = check->amplist; p->nid; p++) { |
| 4874 | for (ch = 0; ch < 2; ch++) { |
| 4875 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4876 | p->idx); |
| 4877 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4878 | if (!check->power_on) { |
| 4879 | check->power_on = 1; |
| 4880 | snd_hda_power_up(codec); |
| 4881 | } |
| 4882 | return 1; |
| 4883 | } |
| 4884 | } |
| 4885 | } |
| 4886 | if (check->power_on) { |
| 4887 | check->power_on = 0; |
| 4888 | snd_hda_power_down(codec); |
| 4889 | } |
| 4890 | return 0; |
| 4891 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4892 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4893 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4895 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | * input MUX helper |
| 4897 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4898 | |
| 4899 | /** |
| 4900 | * 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] | 4901 | * @imux: imux helper object |
| 4902 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4903 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4904 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4905 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | { |
| 4907 | unsigned int index; |
| 4908 | |
| 4909 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4910 | uinfo->count = 1; |
| 4911 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4912 | if (!imux->num_items) |
| 4913 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | index = uinfo->value.enumerated.item; |
| 4915 | if (index >= imux->num_items) |
| 4916 | index = imux->num_items - 1; |
| 4917 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4918 | return 0; |
| 4919 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4920 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4922 | /** |
| 4923 | * 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] | 4924 | * @codec: the HDA codec |
| 4925 | * @imux: imux helper object |
| 4926 | * @ucontrol: pointer to get/store the data |
| 4927 | * @nid: input mux NID |
| 4928 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4929 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4930 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4931 | const struct hda_input_mux *imux, |
| 4932 | struct snd_ctl_elem_value *ucontrol, |
| 4933 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | unsigned int *cur_val) |
| 4935 | { |
| 4936 | unsigned int idx; |
| 4937 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4938 | if (!imux->num_items) |
| 4939 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | idx = ucontrol->value.enumerated.item[0]; |
| 4941 | if (idx >= imux->num_items) |
| 4942 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4943 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4945 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4946 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | *cur_val = idx; |
| 4948 | return 1; |
| 4949 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4950 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
| 4952 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4953 | /** |
| 4954 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 4955 | * @kcontrol: ctl element |
| 4956 | * @uinfo: pointer to get/store the data |
| 4957 | * @num_items: number of enum items |
| 4958 | * @texts: enum item string array |
| 4959 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4960 | * process kcontrol info callback of a simple string enum array |
| 4961 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 4962 | */ |
| 4963 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 4964 | struct snd_ctl_elem_info *uinfo, |
| 4965 | int num_items, const char * const *texts) |
| 4966 | { |
| 4967 | static const char * const texts_default[] = { |
| 4968 | "Disabled", "Enabled" |
| 4969 | }; |
| 4970 | |
| 4971 | if (!texts || !num_items) { |
| 4972 | num_items = 2; |
| 4973 | texts = texts_default; |
| 4974 | } |
| 4975 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 4976 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4977 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4978 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4979 | |
| 4980 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | * Multi-channel / digital-out PCM helper functions |
| 4982 | */ |
| 4983 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4984 | /* setup SPDIF output stream */ |
| 4985 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 4986 | unsigned int stream_tag, unsigned int format) |
| 4987 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4988 | struct hda_spdif_out *spdif; |
| 4989 | unsigned int curr_fmt; |
| 4990 | bool reset; |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4991 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4992 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 4993 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 4994 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 4995 | reset = codec->spdif_status_reset && |
| 4996 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 4997 | curr_fmt != format; |
| 4998 | |
| 4999 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5000 | updated */ |
| 5001 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5002 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5003 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5004 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5005 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5006 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5007 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5008 | for (d = codec->slave_dig_outs; *d; d++) |
| 5009 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5010 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5011 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5012 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5013 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5014 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5015 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5016 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5017 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5018 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5019 | { |
| 5020 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5021 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5022 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5023 | for (d = codec->slave_dig_outs; *d; d++) |
| 5024 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5025 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5028 | /** |
| 5029 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5030 | * @bus: HD-audio bus |
| 5031 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5032 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5033 | { |
| 5034 | struct hda_codec *codec; |
| 5035 | |
| 5036 | if (!bus) |
| 5037 | return; |
| 5038 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5039 | if (hda_codec_is_power_on(codec) && |
| 5040 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5041 | codec->patch_ops.reboot_notify(codec); |
| 5042 | } |
| 5043 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5044 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5045 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5046 | /** |
| 5047 | * 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] | 5048 | * @codec: the HDA codec |
| 5049 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5051 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5052 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5054 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5055 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5056 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5057 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5059 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | return 0; |
| 5061 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5062 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5064 | /** |
| 5065 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5066 | * @codec: the HDA codec |
| 5067 | * @mout: hda_multi_out object |
| 5068 | * @stream_tag: stream tag to assign |
| 5069 | * @format: format id to assign |
| 5070 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5071 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5072 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5073 | struct hda_multi_out *mout, |
| 5074 | unsigned int stream_tag, |
| 5075 | unsigned int format, |
| 5076 | struct snd_pcm_substream *substream) |
| 5077 | { |
| 5078 | mutex_lock(&codec->spdif_mutex); |
| 5079 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5080 | mutex_unlock(&codec->spdif_mutex); |
| 5081 | return 0; |
| 5082 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5083 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5084 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5085 | /** |
| 5086 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5087 | * @codec: the HDA codec |
| 5088 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5089 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5090 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5091 | struct hda_multi_out *mout) |
| 5092 | { |
| 5093 | mutex_lock(&codec->spdif_mutex); |
| 5094 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5095 | mutex_unlock(&codec->spdif_mutex); |
| 5096 | return 0; |
| 5097 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5098 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5099 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5100 | /** |
| 5101 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5102 | * @codec: the HDA codec |
| 5103 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5105 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5106 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5108 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5110 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | return 0; |
| 5112 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5113 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5115 | /** |
| 5116 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5117 | * @codec: the HDA codec |
| 5118 | * @mout: hda_multi_out object |
| 5119 | * @substream: PCM substream to assign |
| 5120 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5121 | * |
| 5122 | * Open analog outputs and set up the hw-constraints. |
| 5123 | * If the digital outputs can be opened as slave, open the digital |
| 5124 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5126 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5127 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5128 | struct snd_pcm_substream *substream, |
| 5129 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5131 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5132 | runtime->hw.channels_max = mout->max_channels; |
| 5133 | if (mout->dig_out_nid) { |
| 5134 | if (!mout->analog_rates) { |
| 5135 | mout->analog_rates = hinfo->rates; |
| 5136 | mout->analog_formats = hinfo->formats; |
| 5137 | mout->analog_maxbps = hinfo->maxbps; |
| 5138 | } else { |
| 5139 | runtime->hw.rates = mout->analog_rates; |
| 5140 | runtime->hw.formats = mout->analog_formats; |
| 5141 | hinfo->maxbps = mout->analog_maxbps; |
| 5142 | } |
| 5143 | if (!mout->spdif_rates) { |
| 5144 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5145 | &mout->spdif_rates, |
| 5146 | &mout->spdif_formats, |
| 5147 | &mout->spdif_maxbps); |
| 5148 | } |
| 5149 | mutex_lock(&codec->spdif_mutex); |
| 5150 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5151 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5152 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5153 | runtime->hw.rates &= mout->spdif_rates; |
| 5154 | runtime->hw.formats &= mout->spdif_formats; |
| 5155 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5156 | hinfo->maxbps = mout->spdif_maxbps; |
| 5157 | } else { |
| 5158 | mout->share_spdif = 0; |
| 5159 | /* FIXME: need notify? */ |
| 5160 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5161 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5162 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5165 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5166 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5167 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5169 | /** |
| 5170 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5171 | * @codec: the HDA codec |
| 5172 | * @mout: hda_multi_out object |
| 5173 | * @stream_tag: stream tag to assign |
| 5174 | * @format: format id to assign |
| 5175 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5176 | * |
| 5177 | * Set up the i/o for analog out. |
| 5178 | * 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] | 5179 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5180 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5181 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | unsigned int stream_tag, |
| 5183 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5184 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5186 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5188 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | int i; |
| 5190 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5191 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5192 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5193 | if (mout->dig_out_nid && mout->share_spdif && |
| 5194 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5196 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5197 | format) && |
Stephen Warren | 7c935976 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5198 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5200 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5201 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | } else { |
| 5203 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5204 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | } |
| 5206 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5207 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | |
| 5209 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5210 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5211 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5212 | if (!mout->no_share_stream && |
| 5213 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5215 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5216 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5217 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5218 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5219 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5220 | snd_hda_codec_setup_stream(codec, |
| 5221 | mout->hp_out_nid[i], |
| 5222 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | /* surrounds */ |
| 5225 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5226 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5227 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5228 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5229 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5230 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5231 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5233 | |
| 5234 | /* extra surrounds */ |
| 5235 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5236 | int ch = 0; |
| 5237 | if (!mout->extra_out_nid[i]) |
| 5238 | break; |
| 5239 | if (chs >= (i + 1) * 2) |
| 5240 | ch = i * 2; |
| 5241 | else if (!mout->no_share_stream) |
| 5242 | break; |
| 5243 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5244 | stream_tag, ch, format); |
| 5245 | } |
| 5246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | return 0; |
| 5248 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5249 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5251 | /** |
| 5252 | * 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] | 5253 | * @codec: the HDA codec |
| 5254 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5256 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5257 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5259 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | int i; |
| 5261 | |
| 5262 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5263 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5265 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5266 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5267 | if (mout->hp_out_nid[i]) |
| 5268 | snd_hda_codec_cleanup_stream(codec, |
| 5269 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5270 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5271 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5272 | snd_hda_codec_cleanup_stream(codec, |
| 5273 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5274 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | 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] | 5276 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | mout->dig_out_used = 0; |
| 5278 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5279 | mutex_unlock(&codec->spdif_mutex); |
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_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5284 | /** |
| 5285 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5286 | * @codec: the HDA codec |
| 5287 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5288 | * |
| 5289 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5290 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5291 | */ |
| 5292 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5293 | { |
| 5294 | unsigned int pincap; |
| 5295 | unsigned int oldval; |
| 5296 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5297 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5298 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5299 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5300 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5301 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5302 | return AC_PINCTL_VREF_80; |
| 5303 | else if (pincap & AC_PINCAP_VREF_50) |
| 5304 | return AC_PINCTL_VREF_50; |
| 5305 | else if (pincap & AC_PINCAP_VREF_100) |
| 5306 | return AC_PINCTL_VREF_100; |
| 5307 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5308 | return AC_PINCTL_VREF_GRD; |
| 5309 | return AC_PINCTL_VREF_HIZ; |
| 5310 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5311 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5312 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5313 | /** |
| 5314 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5315 | * @codec: the HDA codec |
| 5316 | * @pin: referred pin NID |
| 5317 | * @val: pin ctl value to audit |
| 5318 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5319 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5320 | hda_nid_t pin, unsigned int val) |
| 5321 | { |
| 5322 | static unsigned int cap_lists[][2] = { |
| 5323 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5324 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5325 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5326 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5327 | }; |
| 5328 | unsigned int cap; |
| 5329 | |
| 5330 | if (!val) |
| 5331 | return 0; |
| 5332 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5333 | if (!cap) |
| 5334 | return val; /* don't know what to do... */ |
| 5335 | |
| 5336 | if (val & AC_PINCTL_OUT_EN) { |
| 5337 | if (!(cap & AC_PINCAP_OUT)) |
| 5338 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5339 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5340 | val &= ~AC_PINCTL_HP_EN; |
| 5341 | } |
| 5342 | |
| 5343 | if (val & AC_PINCTL_IN_EN) { |
| 5344 | if (!(cap & AC_PINCAP_IN)) |
| 5345 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5346 | else { |
| 5347 | unsigned int vcap, vref; |
| 5348 | int i; |
| 5349 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5350 | vref = val & AC_PINCTL_VREFEN; |
| 5351 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5352 | if (vref == cap_lists[i][0] && |
| 5353 | !(vcap & cap_lists[i][1])) { |
| 5354 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5355 | vref = AC_PINCTL_VREF_HIZ; |
| 5356 | else |
| 5357 | vref = cap_lists[i + 1][0]; |
| 5358 | } |
| 5359 | } |
| 5360 | val &= ~AC_PINCTL_VREFEN; |
| 5361 | val |= vref; |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | return val; |
| 5366 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5367 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5368 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5369 | /** |
| 5370 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5371 | * @codec: the HDA codec |
| 5372 | * @pin: referred pin NID |
| 5373 | * @val: pin control value to set |
| 5374 | * @cached: access over codec pinctl cache or direct write |
| 5375 | * |
| 5376 | * This function is a helper to set a pin ctl value more safely. |
| 5377 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5378 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5379 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5380 | * snd_hda_codec_write() depending on @cached flag. |
| 5381 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5382 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5383 | unsigned int val, bool cached) |
| 5384 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5385 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5386 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5387 | if (cached) |
| 5388 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5389 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5390 | else |
| 5391 | return snd_hda_codec_write(codec, pin, 0, |
| 5392 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5393 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5394 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5395 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5396 | /** |
| 5397 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5398 | * @codec: the HDA codec |
| 5399 | * @imux: imux helper object |
| 5400 | * @label: the name of imux item to assign |
| 5401 | * @index: index number of imux item to assign |
| 5402 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5403 | * |
| 5404 | * When the same label is used already in the existing items, the number |
| 5405 | * suffix is appended to the label. This label index number is stored |
| 5406 | * to type_idx when non-NULL pointer is given. |
| 5407 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5408 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5409 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5410 | int index, int *type_idx) |
| 5411 | { |
| 5412 | int i, label_idx = 0; |
| 5413 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5414 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5415 | return -EINVAL; |
| 5416 | } |
| 5417 | for (i = 0; i < imux->num_items; i++) { |
| 5418 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5419 | label_idx++; |
| 5420 | } |
| 5421 | if (type_idx) |
| 5422 | *type_idx = label_idx; |
| 5423 | if (label_idx > 0) |
| 5424 | snprintf(imux->items[imux->num_items].label, |
| 5425 | sizeof(imux->items[imux->num_items].label), |
| 5426 | "%s %d", label, label_idx); |
| 5427 | else |
| 5428 | strlcpy(imux->items[imux->num_items].label, label, |
| 5429 | sizeof(imux->items[imux->num_items].label)); |
| 5430 | imux->items[imux->num_items].index = index; |
| 5431 | imux->num_items++; |
| 5432 | return 0; |
| 5433 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5434 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | /** |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5437 | * snd_hda_bus_reset - Reset the bus |
| 5438 | * @bus: HD-audio bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5440 | void snd_hda_bus_reset(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5442 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5444 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5445 | /* FIXME: maybe a better way needed for forced reset */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5446 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5447 | #ifdef CONFIG_PM |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5448 | if (hda_codec_is_power_on(codec)) { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 5449 | hda_call_codec_suspend(codec); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5450 | hda_call_codec_resume(codec); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5451 | } |
| 5452 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5455 | EXPORT_SYMBOL_GPL(snd_hda_bus_reset); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5456 | |
| 5457 | /* |
| 5458 | * generic arrays |
| 5459 | */ |
| 5460 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5461 | /** |
| 5462 | * snd_array_new - get a new element from the given array |
| 5463 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5464 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5465 | * Get a new element from the given array. If it exceeds the |
| 5466 | * pre-allocated array size, re-allocate the array. |
| 5467 | * |
| 5468 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5469 | */ |
| 5470 | void *snd_array_new(struct snd_array *array) |
| 5471 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5472 | if (snd_BUG_ON(!array->elem_size)) |
| 5473 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5474 | if (array->used >= array->alloced) { |
| 5475 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5476 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5477 | void *nlist; |
| 5478 | if (snd_BUG_ON(num >= 4096)) |
| 5479 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5480 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5481 | if (!nlist) |
| 5482 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5483 | array->list = nlist; |
| 5484 | array->alloced = num; |
| 5485 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5486 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5487 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5488 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5489 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5490 | /** |
| 5491 | * snd_array_free - free the given array elements |
| 5492 | * @array: the array object |
| 5493 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5494 | void snd_array_free(struct snd_array *array) |
| 5495 | { |
| 5496 | kfree(array->list); |
| 5497 | array->used = 0; |
| 5498 | array->alloced = 0; |
| 5499 | array->list = NULL; |
| 5500 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5501 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5502 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5503 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5504 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5505 | * @pcm: PCM caps bits |
| 5506 | * @buf: the string buffer to write |
| 5507 | * @buflen: the max buffer length |
| 5508 | * |
| 5509 | * used by hda_proc.c and hda_eld.c |
| 5510 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5511 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5512 | { |
| 5513 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5514 | int i, j; |
| 5515 | |
| 5516 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5517 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5518 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5519 | |
| 5520 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5521 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5522 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5523 | |
| 5524 | MODULE_DESCRIPTION("HDA codec core"); |
| 5525 | MODULE_LICENSE("GPL"); |