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 | 63e51fd7 | 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 | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 178 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 179 | mutex_unlock(&bus->cmd_mutex); |
| 180 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 181 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 182 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 183 | codec_dbg(codec, |
| 184 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 185 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 186 | bus->ops.bus_reset(bus); |
| 187 | } |
| 188 | goto again; |
| 189 | } |
| 190 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 191 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 192 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 193 | return err; |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /** |
| 197 | * snd_hda_codec_read - send a command and get the response |
| 198 | * @codec: the HDA codec |
| 199 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 200 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * @verb: the verb to send |
| 202 | * @parm: the parameter for the verb |
| 203 | * |
| 204 | * Send a single command and read the corresponding response. |
| 205 | * |
| 206 | * Returns the obtained response value, or -1 for an error. |
| 207 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 208 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 209 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned int verb, unsigned int parm) |
| 211 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 212 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 213 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 214 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 215 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return res; |
| 217 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * snd_hda_codec_write - send a single command without waiting for response |
| 222 | * @codec: the HDA codec |
| 223 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 224 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * @verb: the verb to send |
| 226 | * @parm: the parameter for the verb |
| 227 | * |
| 228 | * Send a single command without waiting for response. |
| 229 | * |
| 230 | * Returns 0 if successful, or a negative error code. |
| 231 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 232 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 233 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 235 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 236 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 237 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 238 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * snd_hda_sequence_write - sequence writes |
| 244 | * @codec: the HDA codec |
| 245 | * @seq: VERB array to send |
| 246 | * |
| 247 | * Send the commands sequentially from the given array. |
| 248 | * The array must be terminated with NID=0. |
| 249 | */ |
| 250 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 251 | { |
| 252 | for (; seq->nid; seq++) |
| 253 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 259 | * @codec: the HDA codec |
| 260 | * @nid: NID to parse |
| 261 | * @start_id: the pointer to store the start NID |
| 262 | * |
| 263 | * Parse the NID and store the start NID of its sub-nodes. |
| 264 | * Returns the number of sub-nodes. |
| 265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 266 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 267 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | unsigned int parm; |
| 270 | |
| 271 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 272 | if (parm == -1) { |
| 273 | *start_id = 0; |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 274 | return 0; |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | *start_id = (parm >> 16) & 0x7fff; |
| 277 | return (int)(parm & 0x7fff); |
| 278 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 281 | /* connection list element */ |
| 282 | struct hda_conn_list { |
| 283 | struct list_head list; |
| 284 | int len; |
| 285 | hda_nid_t nid; |
| 286 | hda_nid_t conns[0]; |
| 287 | }; |
| 288 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 289 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 290 | static struct hda_conn_list * |
| 291 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 292 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 293 | struct hda_conn_list *p; |
| 294 | list_for_each_entry(p, &codec->conn_list, list) { |
| 295 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 296 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 297 | } |
| 298 | return NULL; |
| 299 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 300 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 301 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 302 | const hda_nid_t *list) |
| 303 | { |
| 304 | struct hda_conn_list *p; |
| 305 | |
| 306 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 307 | if (!p) |
| 308 | return -ENOMEM; |
| 309 | p->len = len; |
| 310 | p->nid = nid; |
| 311 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 312 | list_add(&p->list, &codec->conn_list); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void remove_conn_list(struct hda_codec *codec) |
| 317 | { |
| 318 | while (!list_empty(&codec->conn_list)) { |
| 319 | struct hda_conn_list *p; |
| 320 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 321 | list_del(&p->list); |
| 322 | kfree(p); |
| 323 | } |
| 324 | } |
| 325 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 326 | /* read the connection and add to the cache */ |
| 327 | static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 328 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 329 | hda_nid_t list[32]; |
| 330 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 331 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 332 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 333 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 334 | if (len == -ENOSPC) { |
| 335 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 336 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 337 | if (!result) |
| 338 | return -ENOMEM; |
| 339 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 340 | } |
| 341 | if (len >= 0) |
| 342 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 343 | if (result != list) |
| 344 | kfree(result); |
| 345 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 346 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 347 | |
| 348 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | * snd_hda_get_conn_list - get connection list |
| 350 | * @codec: the HDA codec |
| 351 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 352 | * @listp: the pointer to store NID list |
| 353 | * |
| 354 | * Parses the connection list of the given widget and stores the pointer |
| 355 | * to the list of NIDs. |
| 356 | * |
| 357 | * Returns the number of connections, or a negative error code. |
| 358 | * |
| 359 | * Note that the returned pointer isn't protected against the list |
| 360 | * modification. If snd_hda_override_conn_list() might be called |
| 361 | * concurrently, protect with a mutex appropriately. |
| 362 | */ |
| 363 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 364 | const hda_nid_t **listp) |
| 365 | { |
| 366 | bool added = false; |
| 367 | |
| 368 | for (;;) { |
| 369 | int err; |
| 370 | const struct hda_conn_list *p; |
| 371 | |
| 372 | /* if the connection-list is already cached, read it */ |
| 373 | p = lookup_conn_list(codec, nid); |
| 374 | if (p) { |
| 375 | if (listp) |
| 376 | *listp = p->conns; |
| 377 | return p->len; |
| 378 | } |
| 379 | if (snd_BUG_ON(added)) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | err = read_and_add_raw_conns(codec, nid); |
| 383 | if (err < 0) |
| 384 | return err; |
| 385 | added = true; |
| 386 | } |
| 387 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 388 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 389 | |
| 390 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 391 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | * @codec: the HDA codec |
| 393 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * @max_conns: max. number of connections to store |
| 396 | * |
| 397 | * Parses the connection list of the given widget and stores the list |
| 398 | * of NIDs. |
| 399 | * |
| 400 | * Returns the number of connections, or a negative error code. |
| 401 | */ |
| 402 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 403 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 404 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 405 | const hda_nid_t *list; |
| 406 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 407 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 408 | if (len > 0 && conn_list) { |
| 409 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 410 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 411 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 412 | return -EINVAL; |
| 413 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 414 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 415 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 416 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 417 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 418 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 419 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 420 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 421 | /* return CONNLIST_LEN parameter of the given widget */ |
| 422 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 423 | { |
| 424 | unsigned int wcaps = get_wcaps(codec, nid); |
| 425 | unsigned int parm; |
| 426 | |
| 427 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 428 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 429 | return 0; |
| 430 | |
| 431 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 432 | if (parm == -1) |
| 433 | parm = 0; |
| 434 | return parm; |
| 435 | } |
| 436 | |
| 437 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 438 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 439 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 442 | /** |
| 443 | * snd_hda_get_raw_connections - copy connection list without cache |
| 444 | * @codec: the HDA codec |
| 445 | * @nid: NID to parse |
| 446 | * @conn_list: connection list array |
| 447 | * @max_conns: max. number of connections to store |
| 448 | * |
| 449 | * Like snd_hda_get_connections(), copy the connection list but without |
| 450 | * checking through the connection-list cache. |
| 451 | * Currently called only from hda_proc.c, so not exported. |
| 452 | */ |
| 453 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 454 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 457 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 459 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 460 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 462 | parm = get_num_conns(codec, nid); |
| 463 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 464 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (parm & AC_CLIST_LONG) { |
| 467 | /* long form */ |
| 468 | shift = 16; |
| 469 | num_elems = 2; |
| 470 | } else { |
| 471 | /* short form */ |
| 472 | shift = 8; |
| 473 | num_elems = 4; |
| 474 | } |
| 475 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | mask = (1 << (shift-1)) - 1; |
| 477 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 478 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return 0; /* no connection */ |
| 480 | |
| 481 | if (conn_len == 1) { |
| 482 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 483 | parm = snd_hda_codec_read(codec, nid, 0, |
| 484 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 485 | if (parm == -1 && codec->bus->rirb_error) |
| 486 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 487 | if (conn_list) |
| 488 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | /* multi connection */ |
| 493 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 494 | prev_nid = 0; |
| 495 | for (i = 0; i < conn_len; i++) { |
| 496 | int range_val; |
| 497 | hda_nid_t val, n; |
| 498 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 499 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 500 | parm = snd_hda_codec_read(codec, nid, 0, |
| 501 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 502 | if (parm == -1 && codec->bus->rirb_error) |
| 503 | return -EIO; |
| 504 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 505 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 506 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 507 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 508 | codec_dbg(codec, |
| 509 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 510 | nid, i, parm); |
| 511 | return 0; |
| 512 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 513 | parm >>= shift; |
| 514 | if (range_val) { |
| 515 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 516 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 517 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 518 | "invalid dep_range_val %x:%x\n", |
| 519 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 520 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 522 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 523 | if (conn_list) { |
| 524 | if (conns >= max_conns) |
| 525 | return -ENOSPC; |
| 526 | conn_list[conns] = n; |
| 527 | } |
| 528 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 529 | } |
| 530 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 531 | if (conn_list) { |
| 532 | if (conns >= max_conns) |
| 533 | return -ENOSPC; |
| 534 | conn_list[conns] = val; |
| 535 | } |
| 536 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 538 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | return conns; |
| 541 | } |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 544 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 545 | * @codec: the HDA codec |
| 546 | * @nid: NID to parse |
| 547 | * @len: number of connection list entries |
| 548 | * @list: the list of connection entries |
| 549 | * |
| 550 | * Add or modify the given connection-list to the cache. If the corresponding |
| 551 | * cache already exists, invalidate it and append a new one. |
| 552 | * |
| 553 | * Returns zero or a negative error code. |
| 554 | */ |
| 555 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 556 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 558 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 559 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 560 | p = lookup_conn_list(codec, nid); |
| 561 | if (p) { |
| 562 | list_del(&p->list); |
| 563 | kfree(p); |
| 564 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 565 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 566 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 569 | |
| 570 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 571 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 572 | * @codec: the HDA codec |
| 573 | * @mux: NID containing the list |
| 574 | * @nid: NID to select |
| 575 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 576 | * |
| 577 | * Parses the connection list of the widget @mux and checks whether the |
| 578 | * widget @nid is present. If it is, return the connection index. |
| 579 | * Otherwise it returns -1. |
| 580 | */ |
| 581 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 582 | hda_nid_t nid, int recursive) |
| 583 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 584 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 585 | int i, nums; |
| 586 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 587 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 588 | for (i = 0; i < nums; i++) |
| 589 | if (conn[i] == nid) |
| 590 | return i; |
| 591 | if (!recursive) |
| 592 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 593 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 594 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 598 | for (i = 0; i < nums; i++) { |
| 599 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 600 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 601 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 602 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 603 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 604 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 605 | return -1; |
| 606 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 609 | |
| 610 | /* return DEVLIST_LEN parameter of the given widget */ |
| 611 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 612 | { |
| 613 | unsigned int wcaps = get_wcaps(codec, nid); |
| 614 | unsigned int parm; |
| 615 | |
| 616 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 617 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 618 | return 0; |
| 619 | |
| 620 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 621 | if (parm == -1 && codec->bus->rirb_error) |
| 622 | parm = 0; |
| 623 | return parm & AC_DEV_LIST_LEN_MASK; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * snd_hda_get_devices - copy device list without cache |
| 628 | * @codec: the HDA codec |
| 629 | * @nid: NID of the pin to parse |
| 630 | * @dev_list: device list array |
| 631 | * @max_devices: max. number of devices to store |
| 632 | * |
| 633 | * Copy the device list. This info is dynamic and so not cached. |
| 634 | * Currently called only from hda_proc.c, so not exported. |
| 635 | */ |
| 636 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 637 | u8 *dev_list, int max_devices) |
| 638 | { |
| 639 | unsigned int parm; |
| 640 | int i, dev_len, devices; |
| 641 | |
| 642 | parm = get_num_devices(codec, nid); |
| 643 | if (!parm) /* not multi-stream capable */ |
| 644 | return 0; |
| 645 | |
| 646 | dev_len = parm + 1; |
| 647 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 648 | |
| 649 | devices = 0; |
| 650 | while (devices < dev_len) { |
| 651 | parm = snd_hda_codec_read(codec, nid, 0, |
| 652 | AC_VERB_GET_DEVICE_LIST, devices); |
| 653 | if (parm == -1 && codec->bus->rirb_error) |
| 654 | break; |
| 655 | |
| 656 | for (i = 0; i < 8; i++) { |
| 657 | dev_list[devices] = (u8)parm; |
| 658 | parm >>= 4; |
| 659 | devices++; |
| 660 | if (devices >= dev_len) |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | return devices; |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | /** |
| 668 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 669 | * @bus: the BUS |
| 670 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 671 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 672 | * |
| 673 | * Adds the given event to the queue. The events are processed in |
| 674 | * the workqueue asynchronously. Call this function in the interrupt |
| 675 | * hanlder when RIRB receives an unsolicited event. |
| 676 | * |
| 677 | * Returns 0 if successful, or a negative error code. |
| 678 | */ |
| 679 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 680 | { |
| 681 | struct hda_bus_unsolicited *unsol; |
| 682 | unsigned int wp; |
| 683 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 684 | if (!bus || !bus->workq) |
| 685 | return 0; |
| 686 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 687 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 688 | unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 690 | unsol->wp = wp; |
| 691 | |
| 692 | wp <<= 1; |
| 693 | unsol->queue[wp] = res; |
| 694 | unsol->queue[wp + 1] = res_ex; |
| 695 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 696 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | return 0; |
| 699 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 700 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 703 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 705 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 707 | struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work); |
| 708 | struct hda_bus_unsolicited *unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | struct hda_codec *codec; |
| 710 | unsigned int rp, caddr, res; |
| 711 | |
| 712 | while (unsol->rp != unsol->wp) { |
| 713 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 714 | unsol->rp = rp; |
| 715 | rp <<= 1; |
| 716 | res = unsol->queue[rp]; |
| 717 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 718 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | continue; |
| 720 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 721 | if (codec && codec->patch_ops.unsol_event) |
| 722 | codec->patch_ops.unsol_event(codec, res); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * destructor |
| 728 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 729 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 731 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 732 | return; |
| 733 | |
| 734 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 735 | if (bus->workq) |
| 736 | flush_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (bus->ops.private_free) |
| 738 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 739 | if (bus->workq) |
| 740 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 745 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 747 | snd_hda_bus_free(device->device_data); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 752 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 754 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 755 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
| 759 | * snd_hda_bus_new - create a HDA bus |
| 760 | * @card: the card entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | * @busp: the pointer to store the created bus instance |
| 762 | * |
| 763 | * Returns 0 if successful, or a negative error code. |
| 764 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 765 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | ef74497 | 2015-02-17 13:56:29 +0100 | [diff] [blame] | 766 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | struct hda_bus *bus; |
| 769 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 770 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 771 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | .dev_free = snd_hda_bus_dev_free, |
| 773 | }; |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (busp) |
| 776 | *busp = NULL; |
| 777 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 778 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 780 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return -ENOMEM; |
| 782 | } |
| 783 | |
| 784 | bus->card = card; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 785 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 786 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | INIT_LIST_HEAD(&bus->codec_list); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 788 | INIT_WORK(&bus->unsol.work, process_unsol_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 790 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 791 | "hd-audio%d", card->number); |
| 792 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 793 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 794 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 795 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 796 | kfree(bus); |
| 797 | return -ENOMEM; |
| 798 | } |
| 799 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 800 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 801 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | snd_hda_bus_free(bus); |
| 803 | return err; |
| 804 | } |
| 805 | if (busp) |
| 806 | *busp = bus; |
| 807 | return 0; |
| 808 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 809 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 812 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 814 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 816 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | hda_nid_t nid; |
| 818 | |
| 819 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 820 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 821 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 822 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 823 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 824 | case AC_GRP_AUDIO_FUNCTION: |
| 825 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 826 | codec->afg_function_id = function_id & 0xff; |
| 827 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 828 | break; |
| 829 | case AC_GRP_MODEM_FUNCTION: |
| 830 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 831 | codec->mfg_function_id = function_id & 0xff; |
| 832 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 833 | break; |
| 834 | default: |
| 835 | break; |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 841 | * read widget caps for each widget and store in cache |
| 842 | */ |
| 843 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 844 | { |
| 845 | int i; |
| 846 | hda_nid_t nid; |
| 847 | |
| 848 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 849 | &codec->start_nid); |
| 850 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 851 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 852 | return -ENOMEM; |
| 853 | nid = codec->start_nid; |
| 854 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 855 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 856 | AC_PAR_AUDIO_WIDGET_CAP); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 860 | /* read all pin default configurations and save codec->init_pins */ |
| 861 | static int read_pin_defaults(struct hda_codec *codec) |
| 862 | { |
| 863 | int i; |
| 864 | hda_nid_t nid = codec->start_nid; |
| 865 | |
| 866 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 867 | struct hda_pincfg *pin; |
| 868 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 869 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 870 | if (wid_type != AC_WID_PIN) |
| 871 | continue; |
| 872 | pin = snd_array_new(&codec->init_pins); |
| 873 | if (!pin) |
| 874 | return -ENOMEM; |
| 875 | pin->nid = nid; |
| 876 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 877 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 878 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 879 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 880 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 881 | } |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* look up the given pin config list and return the item matching with NID */ |
| 886 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 887 | struct snd_array *array, |
| 888 | hda_nid_t nid) |
| 889 | { |
| 890 | int i; |
| 891 | for (i = 0; i < array->used; i++) { |
| 892 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 893 | if (pin->nid == nid) |
| 894 | return pin; |
| 895 | } |
| 896 | return NULL; |
| 897 | } |
| 898 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 899 | /* set the current pin config value for the given NID. |
| 900 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 901 | */ |
| 902 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 903 | hda_nid_t nid, unsigned int cfg) |
| 904 | { |
| 905 | struct hda_pincfg *pin; |
| 906 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 907 | /* the check below may be invalid when pins are added by a fixup |
| 908 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 909 | * for now |
| 910 | */ |
| 911 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 912 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 913 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 914 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 915 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 916 | pin = look_up_pincfg(codec, list, nid); |
| 917 | if (!pin) { |
| 918 | pin = snd_array_new(list); |
| 919 | if (!pin) |
| 920 | return -ENOMEM; |
| 921 | pin->nid = nid; |
| 922 | } |
| 923 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 927 | /** |
| 928 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 929 | * @codec: the HDA codec |
| 930 | * @nid: NID to set the pin config |
| 931 | * @cfg: the pin default config value |
| 932 | * |
| 933 | * Override a pin default configuration value in the cache. |
| 934 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 935 | * priority than the real hardware value. |
| 936 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 937 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 938 | hda_nid_t nid, unsigned int cfg) |
| 939 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 940 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 941 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 942 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 943 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 944 | /** |
| 945 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 946 | * @codec: the HDA codec |
| 947 | * @nid: NID to get the pin config |
| 948 | * |
| 949 | * Get the current pin config value of the given pin NID. |
| 950 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 951 | * returns the cached value. |
| 952 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 953 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 954 | { |
| 955 | struct hda_pincfg *pin; |
| 956 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 957 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 958 | { |
| 959 | unsigned int cfg = 0; |
| 960 | mutex_lock(&codec->user_mutex); |
| 961 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 962 | if (pin) |
| 963 | cfg = pin->cfg; |
| 964 | mutex_unlock(&codec->user_mutex); |
| 965 | if (cfg) |
| 966 | return cfg; |
| 967 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 968 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 969 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 970 | if (pin) |
| 971 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 972 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 973 | if (pin) |
| 974 | return pin->cfg; |
| 975 | return 0; |
| 976 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 977 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 978 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 979 | /** |
| 980 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 981 | * @codec: the HDA codec |
| 982 | * @nid: pin NID |
| 983 | * @val: assigned pinctl value |
| 984 | * |
| 985 | * This function stores the given value to a pinctl target value in the |
| 986 | * pincfg table. This isn't always as same as the actually written value |
| 987 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 988 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 989 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 990 | unsigned int val) |
| 991 | { |
| 992 | struct hda_pincfg *pin; |
| 993 | |
| 994 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 995 | if (!pin) |
| 996 | return -EINVAL; |
| 997 | pin->target = val; |
| 998 | return 0; |
| 999 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1000 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1001 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1004 | * @codec: the HDA codec |
| 1005 | * @nid: pin NID |
| 1006 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1007 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1008 | { |
| 1009 | struct hda_pincfg *pin; |
| 1010 | |
| 1011 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1012 | if (!pin) |
| 1013 | return 0; |
| 1014 | return pin->target; |
| 1015 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1017 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1018 | /** |
| 1019 | * snd_hda_shutup_pins - Shut up all pins |
| 1020 | * @codec: the HDA codec |
| 1021 | * |
| 1022 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1023 | * The controls aren't cached so that they can be resumed properly. |
| 1024 | */ |
| 1025 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1026 | { |
| 1027 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1028 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1029 | * the default pin setup at the next load of the driver |
| 1030 | */ |
| 1031 | if (codec->bus->shutdown) |
| 1032 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1033 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1034 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1035 | /* use read here for syncing after issuing each verb */ |
| 1036 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1037 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1038 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1039 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1040 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1042 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1043 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1044 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1045 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1046 | { |
| 1047 | int i; |
| 1048 | if (!codec->pins_shutup) |
| 1049 | return; |
| 1050 | if (codec->bus->shutdown) |
| 1051 | return; |
| 1052 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1053 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1054 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1055 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1056 | pin->ctrl); |
| 1057 | } |
| 1058 | codec->pins_shutup = 0; |
| 1059 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1060 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1061 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1062 | static void hda_jackpoll_work(struct work_struct *work) |
| 1063 | { |
| 1064 | struct hda_codec *codec = |
| 1065 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1066 | |
| 1067 | snd_hda_jack_set_dirty_all(codec); |
| 1068 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1069 | |
| 1070 | if (!codec->jackpoll_interval) |
| 1071 | return; |
| 1072 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1073 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1074 | codec->jackpoll_interval); |
| 1075 | } |
| 1076 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1077 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1078 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1079 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1080 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1081 | /* release all pincfg lists */ |
| 1082 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1083 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1084 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1085 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1086 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1087 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1088 | snd_array_free(&codec->init_pins); |
| 1089 | } |
| 1090 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1091 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1092 | * audio-converter setup caches |
| 1093 | */ |
| 1094 | struct hda_cvt_setup { |
| 1095 | hda_nid_t nid; |
| 1096 | u8 stream_tag; |
| 1097 | u8 channel_id; |
| 1098 | u16 format_id; |
| 1099 | unsigned char active; /* cvt is currently used */ |
| 1100 | unsigned char dirty; /* setups should be cleared */ |
| 1101 | }; |
| 1102 | |
| 1103 | /* get or create a cache entry for the given audio converter NID */ |
| 1104 | static struct hda_cvt_setup * |
| 1105 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1106 | { |
| 1107 | struct hda_cvt_setup *p; |
| 1108 | int i; |
| 1109 | |
| 1110 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1111 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1112 | if (p->nid == nid) |
| 1113 | return p; |
| 1114 | } |
| 1115 | p = snd_array_new(&codec->cvt_setups); |
| 1116 | if (p) |
| 1117 | p->nid = nid; |
| 1118 | return p; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | * codec destructor |
| 1123 | */ |
| 1124 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1125 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1126 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1128 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1129 | if (device_is_registered(hda_codec_dev(codec))) |
| 1130 | device_del(hda_codec_dev(codec)); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1131 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1132 | free_init_pincfgs(codec); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1133 | flush_workqueue(codec->bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1135 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1136 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1137 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1138 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1139 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | codec->bus->caddr_tbl[codec->addr] = NULL; |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1141 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1142 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1143 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1144 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1145 | kfree(codec->vendor_name); |
| 1146 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1147 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1148 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1149 | codec->bus->num_codecs--; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1150 | put_device(hda_codec_dev(codec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1153 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1154 | hda_nid_t fg, unsigned int power_state); |
| 1155 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1156 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1157 | unsigned int power_state); |
| 1158 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1159 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1160 | { |
| 1161 | struct hda_codec *codec = device->device_data; |
| 1162 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1163 | snd_hda_register_beep_device(codec); |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 1164 | if (device_is_registered(hda_codec_dev(codec))) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1165 | pm_runtime_enable(hda_codec_dev(codec)); |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame] | 1166 | /* it was powered up in snd_hda_codec_new(), now all done */ |
| 1167 | snd_hda_power_down(codec); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1168 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1172 | { |
| 1173 | struct hda_codec *codec = device->device_data; |
| 1174 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1175 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1179 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1180 | { |
| 1181 | snd_hda_codec_free(device->device_data); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1185 | /* just free the container */ |
| 1186 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1187 | { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1188 | kfree(dev_to_hda_codec(dev)); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /** |
| 1192 | * snd_hda_codec_new - create a HDA codec |
| 1193 | * @bus: the bus to assign |
| 1194 | * @codec_addr: the codec address |
| 1195 | * @codecp: the pointer to store the generated codec |
| 1196 | * |
| 1197 | * Returns 0 if successful, or a negative error code. |
| 1198 | */ |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1199 | int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card, |
| 1200 | unsigned int codec_addr, struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
| 1202 | struct hda_codec *codec; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1203 | struct device *dev; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1204 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1205 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1207 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1208 | .dev_register = snd_hda_codec_dev_register, |
| 1209 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1210 | .dev_free = snd_hda_codec_dev_free, |
| 1211 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1213 | if (snd_BUG_ON(!bus)) |
| 1214 | return -EINVAL; |
| 1215 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1216 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1219 | dev_err(card->dev, |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1220 | "address 0x%x is already occupied\n", |
| 1221 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | return -EBUSY; |
| 1223 | } |
| 1224 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1225 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | if (codec == NULL) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1227 | dev_err(card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return -ENOMEM; |
| 1229 | } |
| 1230 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1231 | dev = hda_codec_dev(codec); |
| 1232 | device_initialize(dev); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1233 | dev->parent = card->dev; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1234 | dev->bus = &snd_hda_bus_type; |
| 1235 | dev->release = snd_hda_codec_dev_release; |
| 1236 | dev->groups = snd_hda_dev_attr_groups; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1237 | dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1238 | dev_set_drvdata(dev, codec); /* for sysfs */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 1239 | device_enable_async_suspend(dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | codec->bus = bus; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1242 | codec->card = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1244 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1245 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1246 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1247 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1248 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1249 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1250 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1251 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1252 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1253 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1254 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1255 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1256 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1257 | INIT_LIST_HEAD(&codec->conn_list); |
| 1258 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1259 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1260 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1261 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1263 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1264 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
Takashi Iwai | 709949f | 2015-02-20 09:58:14 +0100 | [diff] [blame] | 1265 | * it's powered down later in snd_hda_codec_dev_register(). |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1266 | */ |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 1267 | set_bit(codec->addr, &bus->codec_powered); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1268 | pm_runtime_set_active(hda_codec_dev(codec)); |
| 1269 | pm_runtime_get_noresume(hda_codec_dev(codec)); |
| 1270 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1271 | #endif |
| 1272 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1273 | snd_hda_sysfs_init(codec); |
| 1274 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1275 | if (codec->bus->modelname) { |
| 1276 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1277 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1278 | err = -ENODEV; |
| 1279 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1284 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | bus->caddr_tbl[codec_addr] = codec; |
| 1287 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1288 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1289 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1290 | if (codec->vendor_id == -1) |
| 1291 | /* read again, hopefully the access method was corrected |
| 1292 | * in the last read... |
| 1293 | */ |
| 1294 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1295 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1296 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1297 | AC_PAR_SUBSYSTEM_ID); |
| 1298 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1299 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1301 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1302 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1303 | dev_err(card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1304 | err = -ENODEV; |
| 1305 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1308 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1309 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1310 | if (err < 0) { |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1311 | dev_err(card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1312 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1313 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1314 | err = read_pin_defaults(codec); |
| 1315 | if (err < 0) |
| 1316 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1317 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1318 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1319 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1320 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1321 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1324 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1325 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1326 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1327 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1328 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1329 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1330 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1331 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1332 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1333 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1334 | snd_hda_codec_proc_new(codec); |
| 1335 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1336 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1337 | |
| 1338 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1339 | codec->subsystem_id, codec->revision_id); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1340 | snd_component_add(card, component); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1341 | |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 1342 | err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops); |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1343 | if (err < 0) |
| 1344 | goto error; |
| 1345 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1346 | if (codecp) |
| 1347 | *codecp = codec; |
| 1348 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1349 | |
| 1350 | error: |
| 1351 | snd_hda_codec_free(codec); |
| 1352 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1353 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1354 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1355 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1356 | /** |
| 1357 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1358 | * @codec: the HDA codec |
| 1359 | * |
| 1360 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1361 | * the given codec. |
| 1362 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1363 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1364 | { |
| 1365 | hda_nid_t fg; |
| 1366 | int err; |
| 1367 | |
| 1368 | /* Assume the function group node does not change, |
| 1369 | * only the widget nodes may change. |
| 1370 | */ |
| 1371 | kfree(codec->wcaps); |
| 1372 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1373 | err = read_widget_caps(codec, fg); |
| 1374 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1375 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1376 | return err; |
| 1377 | } |
| 1378 | |
| 1379 | snd_array_free(&codec->init_pins); |
| 1380 | err = read_pin_defaults(codec); |
| 1381 | |
| 1382 | return err; |
| 1383 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1384 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1385 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1386 | /* update the stream-id if changed */ |
| 1387 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1388 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1389 | u32 stream_tag, int channel_id) |
| 1390 | { |
| 1391 | unsigned int oldval, newval; |
| 1392 | |
| 1393 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1394 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1395 | newval = (stream_tag << 4) | channel_id; |
| 1396 | if (oldval != newval) |
| 1397 | snd_hda_codec_write(codec, nid, 0, |
| 1398 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1399 | newval); |
| 1400 | p->stream_tag = stream_tag; |
| 1401 | p->channel_id = channel_id; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* update the format-id if changed */ |
| 1406 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1407 | hda_nid_t nid, int format) |
| 1408 | { |
| 1409 | unsigned int oldval; |
| 1410 | |
| 1411 | if (p->format_id != format) { |
| 1412 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1413 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1414 | if (oldval != format) { |
| 1415 | msleep(1); |
| 1416 | snd_hda_codec_write(codec, nid, 0, |
| 1417 | AC_VERB_SET_STREAM_FORMAT, |
| 1418 | format); |
| 1419 | } |
| 1420 | p->format_id = format; |
| 1421 | } |
| 1422 | } |
| 1423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | /** |
| 1425 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1426 | * @codec: the CODEC to set up |
| 1427 | * @nid: the NID to set up |
| 1428 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1429 | * @channel_id: channel id to pass, zero based. |
| 1430 | * @format: stream format. |
| 1431 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1432 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1433 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | int channel_id, int format) |
| 1435 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1436 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1437 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1438 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1439 | int i; |
| 1440 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1441 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1442 | return; |
| 1443 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1444 | codec_dbg(codec, |
| 1445 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1446 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1447 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1448 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1449 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1450 | |
| 1451 | if (codec->pcm_format_first) |
| 1452 | update_pcm_format(codec, p, nid, format); |
| 1453 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1454 | if (!codec->pcm_format_first) |
| 1455 | update_pcm_format(codec, p, nid, format); |
| 1456 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1457 | p->active = 1; |
| 1458 | p->dirty = 0; |
| 1459 | |
| 1460 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1461 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1462 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1463 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1464 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1465 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1466 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1467 | p->dirty = 1; |
| 1468 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1471 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1473 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1474 | struct hda_cvt_setup *q); |
| 1475 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1476 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1477 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1478 | * @codec: the CODEC to clean up |
| 1479 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1480 | * @do_now: really clean up the stream instead of clearing the active flag |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1481 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1482 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1483 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1484 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1485 | struct hda_cvt_setup *p; |
| 1486 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1487 | if (!nid) |
| 1488 | return; |
| 1489 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1490 | if (codec->no_sticky_stream) |
| 1491 | do_now = 1; |
| 1492 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1493 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1494 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1495 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1496 | /* here we just clear the active flag when do_now isn't set; |
| 1497 | * actual clean-ups will be done later in |
| 1498 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1499 | */ |
| 1500 | if (do_now) |
| 1501 | really_cleanup_stream(codec, p); |
| 1502 | else |
| 1503 | p->active = 0; |
| 1504 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1505 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1506 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1507 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1508 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1509 | struct hda_cvt_setup *q) |
| 1510 | { |
| 1511 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1512 | if (q->stream_tag || q->channel_id) |
| 1513 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1514 | if (q->format_id) |
| 1515 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1516 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1517 | memset(q, 0, sizeof(*q)); |
| 1518 | q->nid = nid; |
| 1519 | } |
| 1520 | |
| 1521 | /* clean up the all conflicting obsolete streams */ |
| 1522 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1523 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1524 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1525 | int i; |
| 1526 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1527 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1528 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1529 | struct hda_cvt_setup *p; |
| 1530 | p = snd_array_elem(&c->cvt_setups, i); |
| 1531 | if (p->dirty) |
| 1532 | really_cleanup_stream(c, p); |
| 1533 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1537 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1538 | /* clean up all streams; called from suspend */ |
| 1539 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1540 | { |
| 1541 | int i; |
| 1542 | |
| 1543 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1544 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1545 | if (p->stream_tag) |
| 1546 | really_cleanup_stream(codec, p); |
| 1547 | } |
| 1548 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1549 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* |
| 1552 | * amp access functions |
| 1553 | */ |
| 1554 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1555 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1556 | #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1557 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1558 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1559 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1561 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
| 1563 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1564 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1565 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1567 | memset(cache, 0, sizeof(*cache)); |
| 1568 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1569 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1572 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1573 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1574 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1578 | static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1580 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1581 | u16 cur = cache->hash[idx]; |
| 1582 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1585 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | if (info->key == key) |
| 1587 | return info; |
| 1588 | cur = info->next; |
| 1589 | } |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1590 | return NULL; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1593 | /* query the hash. allocate an entry if not found. */ |
| 1594 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1595 | u32 key) |
| 1596 | { |
| 1597 | struct hda_cache_head *info = get_hash(cache, key); |
| 1598 | if (!info) { |
| 1599 | u16 idx, cur; |
| 1600 | /* add a new hash entry */ |
| 1601 | info = snd_array_new(&cache->buf); |
| 1602 | if (!info) |
| 1603 | return NULL; |
| 1604 | cur = snd_array_index(&cache->buf, info); |
| 1605 | info->key = key; |
| 1606 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1607 | info->dirty = 0; |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1608 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1609 | info->next = cache->hash[idx]; |
| 1610 | cache->hash[idx] = cur; |
| 1611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | return info; |
| 1613 | } |
| 1614 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1615 | /* query and allocate an amp hash entry */ |
| 1616 | static inline struct hda_amp_info * |
| 1617 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1618 | { |
| 1619 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1620 | } |
| 1621 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1622 | /* overwrite the value with the key in the caps hash */ |
| 1623 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1624 | { |
| 1625 | struct hda_amp_info *info; |
| 1626 | |
| 1627 | mutex_lock(&codec->hash_mutex); |
| 1628 | info = get_alloc_amp_hash(codec, key); |
| 1629 | if (!info) { |
| 1630 | mutex_unlock(&codec->hash_mutex); |
| 1631 | return -EINVAL; |
| 1632 | } |
| 1633 | info->amp_caps = val; |
| 1634 | info->head.val |= INFO_AMP_CAPS; |
| 1635 | mutex_unlock(&codec->hash_mutex); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* query the value from the caps hash; if not found, fetch the current |
| 1640 | * value from the given function and store in the hash |
| 1641 | */ |
| 1642 | static unsigned int |
| 1643 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1644 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1645 | { |
| 1646 | struct hda_amp_info *info; |
| 1647 | unsigned int val; |
| 1648 | |
| 1649 | mutex_lock(&codec->hash_mutex); |
| 1650 | info = get_alloc_amp_hash(codec, key); |
| 1651 | if (!info) { |
| 1652 | mutex_unlock(&codec->hash_mutex); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1656 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1657 | val = func(codec, nid, dir); |
| 1658 | write_caps_hash(codec, key, val); |
| 1659 | } else { |
| 1660 | val = info->amp_caps; |
| 1661 | mutex_unlock(&codec->hash_mutex); |
| 1662 | } |
| 1663 | return val; |
| 1664 | } |
| 1665 | |
| 1666 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1667 | int direction) |
| 1668 | { |
| 1669 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1670 | nid = codec->afg; |
| 1671 | return snd_hda_param_read(codec, nid, |
| 1672 | direction == HDA_OUTPUT ? |
| 1673 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1674 | } |
| 1675 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1676 | /** |
| 1677 | * query_amp_caps - query AMP capabilities |
| 1678 | * @codec: the HD-auio codec |
| 1679 | * @nid: the NID to query |
| 1680 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1681 | * |
| 1682 | * Query AMP capabilities for the given widget and direction. |
| 1683 | * Returns the obtained capability bits. |
| 1684 | * |
| 1685 | * When cap bits have been already read, this doesn't read again but |
| 1686 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1688 | u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1690 | return query_caps_hash(codec, nid, direction, |
| 1691 | HDA_HASH_KEY(nid, direction, 0), |
| 1692 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1694 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1696 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1697 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1698 | * @codec: the HD-audio codec |
| 1699 | * @nid: the NID to query |
| 1700 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1701 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1702 | * |
| 1703 | * Check whether the widget has the given amp capability for the direction. |
| 1704 | */ |
| 1705 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1706 | int dir, unsigned int bits) |
| 1707 | { |
| 1708 | if (!nid) |
| 1709 | return false; |
| 1710 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 1711 | if (query_amp_caps(codec, nid, dir) & bits) |
| 1712 | return true; |
| 1713 | return false; |
| 1714 | } |
| 1715 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 1716 | |
| 1717 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1718 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1719 | * @codec: the CODEC to clean up |
| 1720 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1721 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1722 | * @caps: the capability bits to set |
| 1723 | * |
| 1724 | * Override the cached AMP caps bits value by the given one. |
| 1725 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1726 | * e.g. limit to 0dB, etc. |
| 1727 | * |
| 1728 | * Returns zero if successful or a negative error code. |
| 1729 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1730 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1731 | unsigned int caps) |
| 1732 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1733 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1734 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1735 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1736 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1737 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1738 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1739 | { |
| 1740 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1741 | } |
| 1742 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1743 | /** |
| 1744 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1745 | * @codec: the HD-auio codec |
| 1746 | * @nid: the NID to query |
| 1747 | * |
| 1748 | * Query PIN capabilities for the given widget. |
| 1749 | * Returns the obtained capability bits. |
| 1750 | * |
| 1751 | * When cap bits have been already read, this doesn't read again but |
| 1752 | * returns the cached value. |
| 1753 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1754 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1755 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1756 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1757 | read_pin_cap); |
| 1758 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1759 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1760 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1761 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1762 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1763 | * @codec: the CODEC |
| 1764 | * @nid: the NID to override |
| 1765 | * @caps: the capability bits to set |
| 1766 | * |
| 1767 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1768 | * |
| 1769 | * Returns zero if successful or a negative error code. |
| 1770 | */ |
| 1771 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1772 | unsigned int caps) |
| 1773 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1774 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1775 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1776 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1777 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1778 | /* read or sync the hash value with the current value; |
| 1779 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1781 | static struct hda_amp_info * |
| 1782 | update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1783 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1785 | struct hda_amp_info *info; |
| 1786 | unsigned int parm, val = 0; |
| 1787 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1789 | retry: |
| 1790 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1791 | if (!info) |
| 1792 | return NULL; |
| 1793 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1794 | if (!val_read) { |
| 1795 | mutex_unlock(&codec->hash_mutex); |
| 1796 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1797 | parm |= direction == HDA_OUTPUT ? |
| 1798 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1799 | parm |= index; |
| 1800 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1801 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1802 | val &= 0xff; |
| 1803 | val_read = true; |
| 1804 | mutex_lock(&codec->hash_mutex); |
| 1805 | goto retry; |
| 1806 | } |
| 1807 | info->vol[ch] = val; |
| 1808 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1809 | } else if (init_only) |
| 1810 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1811 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1815 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1817 | static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1818 | hda_nid_t nid, int ch, int direction, int index, |
| 1819 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | { |
| 1821 | u32 parm; |
| 1822 | |
| 1823 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1824 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1825 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1826 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1827 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1828 | ; /* set the zero value as a fake mute */ |
| 1829 | else |
| 1830 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1832 | } |
| 1833 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1834 | /** |
| 1835 | * snd_hda_codec_amp_read - Read AMP value |
| 1836 | * @codec: HD-audio codec |
| 1837 | * @nid: NID to read the AMP value |
| 1838 | * @ch: channel (left=0 or right=1) |
| 1839 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1840 | * @index: the index value (only for input direction) |
| 1841 | * |
| 1842 | * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1844 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1845 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1847 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1848 | unsigned int val = 0; |
| 1849 | |
| 1850 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1851 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1852 | if (info) |
| 1853 | val = info->vol[ch]; |
| 1854 | mutex_unlock(&codec->hash_mutex); |
| 1855 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1857 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1859 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1860 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1861 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1862 | { |
| 1863 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1864 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1865 | |
| 1866 | if (snd_BUG_ON(mask & ~0xff)) |
| 1867 | mask &= 0xff; |
| 1868 | val &= mask; |
| 1869 | |
| 1870 | mutex_lock(&codec->hash_mutex); |
| 1871 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1872 | if (!info) { |
| 1873 | mutex_unlock(&codec->hash_mutex); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | val |= info->vol[ch] & ~mask; |
| 1877 | if (info->vol[ch] == val) { |
| 1878 | mutex_unlock(&codec->hash_mutex); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1882 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1883 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1884 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1885 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1886 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1887 | return 1; |
| 1888 | } |
| 1889 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1890 | /** |
| 1891 | * snd_hda_codec_amp_update - update the AMP value |
| 1892 | * @codec: HD-audio codec |
| 1893 | * @nid: NID to read the AMP value |
| 1894 | * @ch: channel (left=0 or right=1) |
| 1895 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1896 | * @idx: the index value (only for input direction) |
| 1897 | * @mask: bit mask to set |
| 1898 | * @val: the bits value to set |
| 1899 | * |
| 1900 | * Update the AMP value with a bit mask. |
| 1901 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1902 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1903 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1904 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1906 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 1907 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1909 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1911 | /** |
| 1912 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1913 | * @codec: HD-audio codec |
| 1914 | * @nid: NID to read the AMP value |
| 1915 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1916 | * @idx: the index value (only for input direction) |
| 1917 | * @mask: bit mask to set |
| 1918 | * @val: the bits value to set |
| 1919 | * |
| 1920 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1921 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1922 | */ |
| 1923 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1924 | int direction, int idx, int mask, int val) |
| 1925 | { |
| 1926 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1927 | |
| 1928 | if (snd_BUG_ON(mask & ~0xff)) |
| 1929 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1930 | for (ch = 0; ch < 2; ch++) |
| 1931 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1932 | idx, mask, val); |
| 1933 | return ret; |
| 1934 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1935 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1936 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1937 | /** |
| 1938 | * snd_hda_codec_amp_init - initialize the AMP value |
| 1939 | * @codec: the HDA codec |
| 1940 | * @nid: NID to read the AMP value |
| 1941 | * @ch: channel (left=0 or right=1) |
| 1942 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1943 | * @idx: the index value (only for input direction) |
| 1944 | * @mask: bit mask to set |
| 1945 | * @val: the bits value to set |
| 1946 | * |
| 1947 | * Works like snd_hda_codec_amp_update() but it writes the value only at |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1948 | * the first access. If the amp was already initialized / updated beforehand, |
| 1949 | * this does nothing. |
| 1950 | */ |
| 1951 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1952 | int dir, int idx, int mask, int val) |
| 1953 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1954 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 1955 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1956 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1957 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1958 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1959 | /** |
| 1960 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 1961 | * @codec: the HDA codec |
| 1962 | * @nid: NID to read the AMP value |
| 1963 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1964 | * @idx: the index value (only for input direction) |
| 1965 | * @mask: bit mask to set |
| 1966 | * @val: the bits value to set |
| 1967 | * |
| 1968 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 1969 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1970 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1971 | int dir, int idx, int mask, int val) |
| 1972 | { |
| 1973 | int ch, ret = 0; |
| 1974 | |
| 1975 | if (snd_BUG_ON(mask & ~0xff)) |
| 1976 | mask &= 0xff; |
| 1977 | for (ch = 0; ch < 2; ch++) |
| 1978 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 1979 | idx, mask, val); |
| 1980 | return ret; |
| 1981 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1982 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1983 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1984 | /** |
| 1985 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1986 | * @codec: HD-audio codec |
| 1987 | * |
| 1988 | * Resume the all amp commands from the cache. |
| 1989 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1990 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1991 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1992 | int i; |
| 1993 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1994 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 1995 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1996 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 1997 | struct hda_amp_info *buffer; |
| 1998 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1999 | hda_nid_t nid; |
| 2000 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2001 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2002 | |
| 2003 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2004 | if (!buffer->head.dirty) |
| 2005 | continue; |
| 2006 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2007 | info = *buffer; |
| 2008 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2009 | if (!key) |
| 2010 | continue; |
| 2011 | nid = key & 0xff; |
| 2012 | idx = (key >> 16) & 0xff; |
| 2013 | dir = (key >> 24) & 0xff; |
| 2014 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2015 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2016 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2017 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2018 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2019 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2020 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2021 | } |
| 2022 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2023 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2024 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2025 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2027 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2028 | unsigned int ofs) |
| 2029 | { |
| 2030 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2031 | /* get num steps */ |
| 2032 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2033 | if (ofs < caps) |
| 2034 | caps -= ofs; |
| 2035 | return caps; |
| 2036 | } |
| 2037 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2038 | /** |
| 2039 | * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2040 | * @kcontrol: referred ctl element |
| 2041 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2042 | * |
| 2043 | * The control element is supposed to have the private_value field |
| 2044 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2045 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2046 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2047 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | { |
| 2049 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2050 | u16 nid = get_amp_nid(kcontrol); |
| 2051 | u8 chs = get_amp_channels(kcontrol); |
| 2052 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2053 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2055 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2056 | uinfo->count = chs == 3 ? 2 : 1; |
| 2057 | uinfo->value.integer.min = 0; |
| 2058 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2059 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2060 | codec_warn(codec, |
| 2061 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2062 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | return -EINVAL; |
| 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | return 0; |
| 2066 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2067 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2069 | |
| 2070 | static inline unsigned int |
| 2071 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2072 | int ch, int dir, int idx, unsigned int ofs) |
| 2073 | { |
| 2074 | unsigned int val; |
| 2075 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2076 | val &= HDA_AMP_VOLMASK; |
| 2077 | if (val >= ofs) |
| 2078 | val -= ofs; |
| 2079 | else |
| 2080 | val = 0; |
| 2081 | return val; |
| 2082 | } |
| 2083 | |
| 2084 | static inline int |
| 2085 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2086 | int ch, int dir, int idx, unsigned int ofs, |
| 2087 | unsigned int val) |
| 2088 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2089 | unsigned int maxval; |
| 2090 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2091 | if (val > 0) |
| 2092 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2093 | /* ofs = 0: raw max value */ |
| 2094 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2095 | if (val > maxval) |
| 2096 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2097 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2098 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2099 | } |
| 2100 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2101 | /** |
| 2102 | * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2103 | * @kcontrol: ctl element |
| 2104 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2105 | * |
| 2106 | * The control element is supposed to have the private_value field |
| 2107 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2108 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2109 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2110 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | { |
| 2112 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2113 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2114 | int chs = get_amp_channels(kcontrol); |
| 2115 | int dir = get_amp_direction(kcontrol); |
| 2116 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2117 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | long *valp = ucontrol->value.integer.value; |
| 2119 | |
| 2120 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2121 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2123 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2126 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2128 | /** |
| 2129 | * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2130 | * @kcontrol: ctl element |
| 2131 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2132 | * |
| 2133 | * The control element is supposed to have the private_value field |
| 2134 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2135 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2136 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2137 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
| 2139 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2140 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2141 | int chs = get_amp_channels(kcontrol); |
| 2142 | int dir = get_amp_direction(kcontrol); |
| 2143 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2144 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | long *valp = ucontrol->value.integer.value; |
| 2146 | int change = 0; |
| 2147 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2148 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2149 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2150 | valp++; |
| 2151 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2152 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2153 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | return change; |
| 2155 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2156 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2158 | /** |
| 2159 | * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2160 | * @kcontrol: ctl element |
| 2161 | * @op_flag: operation flag |
| 2162 | * @size: byte size of input TLV |
| 2163 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2164 | * |
| 2165 | * The control element is supposed to have the private_value field |
| 2166 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2167 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2168 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2169 | unsigned int size, unsigned int __user *_tlv) |
| 2170 | { |
| 2171 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2172 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2173 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2174 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2175 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2176 | u32 caps, val1, val2; |
| 2177 | |
| 2178 | if (size < 4 * sizeof(unsigned int)) |
| 2179 | return -ENOMEM; |
| 2180 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2181 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2182 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2183 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2184 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2185 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2186 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2187 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2188 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2189 | return -EFAULT; |
| 2190 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2191 | return -EFAULT; |
| 2192 | if (put_user(val1, _tlv + 2)) |
| 2193 | return -EFAULT; |
| 2194 | if (put_user(val2, _tlv + 3)) |
| 2195 | return -EFAULT; |
| 2196 | return 0; |
| 2197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2198 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2199 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2200 | /** |
| 2201 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2202 | * @codec: HD-audio codec |
| 2203 | * @nid: NID of a reference widget |
| 2204 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2205 | * @tlv: TLV data to be stored, at least 4 elements |
| 2206 | * |
| 2207 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2208 | * obtained from the reference NID. |
| 2209 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2210 | */ |
| 2211 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2212 | unsigned int *tlv) |
| 2213 | { |
| 2214 | u32 caps; |
| 2215 | int nums, step; |
| 2216 | |
| 2217 | caps = query_amp_caps(codec, nid, dir); |
| 2218 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2219 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2220 | step = (step + 1) * 25; |
| 2221 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2222 | tlv[1] = 2 * sizeof(unsigned int); |
| 2223 | tlv[2] = -nums * step; |
| 2224 | tlv[3] = step; |
| 2225 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2226 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2227 | |
| 2228 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2229 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2230 | find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2231 | { |
| 2232 | struct snd_ctl_elem_id id; |
| 2233 | memset(&id, 0, sizeof(id)); |
| 2234 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2235 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2236 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2237 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2238 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2239 | strcpy(id.name, name); |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 2240 | return snd_ctl_find_id(codec->card, &id); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2243 | /** |
| 2244 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2245 | * @codec: HD-audio codec |
| 2246 | * @name: ctl id name string |
| 2247 | * |
| 2248 | * Get the control element with the given id string and IFACE_MIXER. |
| 2249 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2250 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2251 | const char *name) |
| 2252 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2253 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2255 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2256 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2257 | static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2258 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2259 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2260 | int i, idx; |
| 2261 | /* 16 ctlrs should be large enough */ |
| 2262 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2263 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2264 | return idx; |
| 2265 | } |
| 2266 | return -EBUSY; |
| 2267 | } |
| 2268 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2269 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2270 | * snd_hda_ctl_add - Add a control element and assign to the codec |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2271 | * @codec: HD-audio codec |
| 2272 | * @nid: corresponding NID (optional) |
| 2273 | * @kctl: the control element to assign |
| 2274 | * |
| 2275 | * Add the given control element to an array inside the codec instance. |
| 2276 | * All control elements belonging to a codec are supposed to be added |
| 2277 | * by this function so that a proper clean-up works at the free or |
| 2278 | * reconfiguration time. |
| 2279 | * |
| 2280 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2281 | * The assignment is shown in the codec proc file. |
| 2282 | * |
| 2283 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2284 | * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2285 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2286 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2287 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2288 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2289 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2290 | { |
| 2291 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2292 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2293 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2294 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2295 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2296 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2297 | if (nid == 0) |
| 2298 | nid = get_amp_nid_(kctl->private_value); |
| 2299 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2300 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2301 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2302 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2303 | kctl->id.subdevice = 0; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 2304 | err = snd_ctl_add(codec->card, kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2305 | if (err < 0) |
| 2306 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2307 | item = snd_array_new(&codec->mixers); |
| 2308 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2309 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2310 | item->kctl = kctl; |
| 2311 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2312 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2315 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2316 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2317 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2318 | * snd_hda_add_nid - Assign a NID to a control element |
| 2319 | * @codec: HD-audio codec |
| 2320 | * @nid: corresponding NID (optional) |
| 2321 | * @kctl: the control element to assign |
| 2322 | * @index: index to kctl |
| 2323 | * |
| 2324 | * Add the given control element to an array inside the codec instance. |
| 2325 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2326 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2327 | */ |
| 2328 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2329 | unsigned int index, hda_nid_t nid) |
| 2330 | { |
| 2331 | struct hda_nid_item *item; |
| 2332 | |
| 2333 | if (nid > 0) { |
| 2334 | item = snd_array_new(&codec->nids); |
| 2335 | if (!item) |
| 2336 | return -ENOMEM; |
| 2337 | item->kctl = kctl; |
| 2338 | item->index = index; |
| 2339 | item->nid = nid; |
| 2340 | return 0; |
| 2341 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2342 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2343 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2344 | return -EINVAL; |
| 2345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2346 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2347 | |
| 2348 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2349 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2350 | * @codec: HD-audio codec |
| 2351 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2352 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2353 | { |
| 2354 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2355 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2356 | for (i = 0; i < codec->mixers.used; i++) |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 2357 | snd_ctl_remove(codec->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2358 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2359 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2362 | /** |
| 2363 | * snd_hda_lock_devices - pseudo device locking |
| 2364 | * @bus: the BUS |
| 2365 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2366 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2367 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2368 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2369 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2370 | struct snd_card *card = bus->card; |
| 2371 | struct hda_codec *codec; |
| 2372 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2373 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2374 | if (card->shutdown) |
| 2375 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2376 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2377 | if (!list_empty(&card->ctl_files)) |
| 2378 | goto err_clear; |
| 2379 | |
| 2380 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2381 | int pcm; |
| 2382 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2383 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2384 | if (!cpcm->pcm) |
| 2385 | continue; |
| 2386 | if (cpcm->pcm->streams[0].substream_opened || |
| 2387 | cpcm->pcm->streams[1].substream_opened) |
| 2388 | goto err_clear; |
| 2389 | } |
| 2390 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2391 | spin_unlock(&card->files_lock); |
| 2392 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2393 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2394 | err_clear: |
| 2395 | card->shutdown = 0; |
| 2396 | err_unlock: |
| 2397 | spin_unlock(&card->files_lock); |
| 2398 | return -EINVAL; |
| 2399 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2400 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2401 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2402 | /** |
| 2403 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2404 | * @bus: the BUS |
| 2405 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2406 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2407 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2408 | struct snd_card *card = bus->card; |
| 2409 | |
| 2410 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2411 | spin_lock(&card->files_lock); |
| 2412 | card->shutdown = 0; |
| 2413 | spin_unlock(&card->files_lock); |
| 2414 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2415 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2416 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2417 | /** |
| 2418 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2419 | * @codec: HD-audio codec |
| 2420 | * |
| 2421 | * This frees the all PCM and control elements assigned to the codec, and |
| 2422 | * clears the caches and restores the pin default configurations. |
| 2423 | * |
| 2424 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2425 | * returns zero. |
| 2426 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2427 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2428 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2429 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame^] | 2430 | struct snd_card *card = codec->card; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2431 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2432 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2433 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2434 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2435 | |
| 2436 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2437 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2438 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2439 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2440 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2441 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2442 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2443 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2444 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2445 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2446 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2447 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2448 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 2449 | if (device_is_registered(hda_codec_dev(codec))) |
| 2450 | device_del(hda_codec_dev(codec)); |
| 2451 | |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2452 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2453 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2454 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2455 | codec->spec = NULL; |
| 2456 | free_hda_cache(&codec->amp_cache); |
| 2457 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2458 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2459 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2460 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2461 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2462 | snd_array_free(&codec->cvt_setups); |
| 2463 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2464 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2465 | codec->num_pcms = 0; |
| 2466 | codec->pcm_info = NULL; |
| 2467 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2468 | codec->slave_dig_outs = NULL; |
| 2469 | codec->spdif_status_reset = 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2472 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2473 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2476 | typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2477 | |
| 2478 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2479 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2480 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2481 | { |
| 2482 | struct hda_nid_item *items; |
| 2483 | const char * const *s; |
| 2484 | int i, err; |
| 2485 | |
| 2486 | items = codec->mixers.list; |
| 2487 | for (i = 0; i < codec->mixers.used; i++) { |
| 2488 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2489 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2490 | continue; |
| 2491 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2492 | char tmpname[sizeof(sctl->id.name)]; |
| 2493 | const char *name = *s; |
| 2494 | if (suffix) { |
| 2495 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2496 | name, suffix); |
| 2497 | name = tmpname; |
| 2498 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2499 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2500 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2501 | if (err) |
| 2502 | return err; |
| 2503 | break; |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2510 | static int check_slave_present(struct hda_codec *codec, |
| 2511 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2512 | { |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2516 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2517 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2518 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2519 | { |
| 2520 | int _tlv[4]; |
| 2521 | const int *tlv = NULL; |
| 2522 | int val = -1; |
| 2523 | |
| 2524 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2525 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2526 | mm_segment_t fs = get_fs(); |
| 2527 | set_fs(get_ds()); |
| 2528 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2529 | tlv = _tlv; |
| 2530 | set_fs(fs); |
| 2531 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2532 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2533 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2534 | int step = tlv[3]; |
| 2535 | step &= ~TLV_DB_SCALE_MUTE; |
| 2536 | if (!step) |
| 2537 | return -1; |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2538 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2539 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2540 | - *step_to_check, step); |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2541 | return -1; |
| 2542 | } |
| 2543 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2544 | val = -tlv[2] / step; |
| 2545 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2546 | return val; |
| 2547 | } |
| 2548 | |
| 2549 | /* call kctl->put with the given value(s) */ |
| 2550 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2551 | { |
| 2552 | struct snd_ctl_elem_value *ucontrol; |
| 2553 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2554 | if (!ucontrol) |
| 2555 | return -ENOMEM; |
| 2556 | ucontrol->value.integer.value[0] = val; |
| 2557 | ucontrol->value.integer.value[1] = val; |
| 2558 | kctl->put(kctl, ucontrol); |
| 2559 | kfree(ucontrol); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
| 2563 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2564 | static int init_slave_0dB(struct hda_codec *codec, |
| 2565 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2566 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2567 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2568 | if (offset > 0) |
| 2569 | put_kctl_with_value(slave, offset); |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2574 | static int init_slave_unmute(struct hda_codec *codec, |
| 2575 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2576 | { |
| 2577 | return put_kctl_with_value(slave, 1); |
| 2578 | } |
| 2579 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2580 | static int add_slave(struct hda_codec *codec, |
| 2581 | void *data, struct snd_kcontrol *slave) |
| 2582 | { |
| 2583 | return snd_ctl_add_slave(data, slave); |
| 2584 | } |
| 2585 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2586 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2587 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2588 | * @codec: HD-audio codec |
| 2589 | * @name: vmaster control name |
| 2590 | * @tlv: TLV data (optional) |
| 2591 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2592 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2593 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2594 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2595 | * |
| 2596 | * Create a virtual master control with the given name. The TLV data |
| 2597 | * must be either NULL or a valid data. |
| 2598 | * |
| 2599 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2600 | * slave control name. All controls with these names are assigned to |
| 2601 | * the new virtual master control. |
| 2602 | * |
| 2603 | * This function returns zero if successful or a negative error code. |
| 2604 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2605 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2606 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2607 | const char *suffix, bool init_slave_vol, |
| 2608 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2609 | { |
| 2610 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2611 | int err; |
| 2612 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2613 | if (ctl_ret) |
| 2614 | *ctl_ret = NULL; |
| 2615 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2616 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2617 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2618 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2619 | return 0; |
| 2620 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2621 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2622 | if (!kctl) |
| 2623 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2624 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2625 | if (err < 0) |
| 2626 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2627 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2628 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2629 | if (err < 0) |
| 2630 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2631 | |
| 2632 | /* init with master mute & zero volume */ |
| 2633 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2634 | if (init_slave_vol) { |
| 2635 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2636 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0c | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2637 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2638 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2639 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2640 | if (ctl_ret) |
| 2641 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2642 | return 0; |
| 2643 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2644 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2645 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2646 | /* |
| 2647 | * mute-LED control using vmaster |
| 2648 | */ |
| 2649 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2650 | struct snd_ctl_elem_info *uinfo) |
| 2651 | { |
| 2652 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2653 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2654 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2655 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2656 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2660 | struct snd_ctl_elem_value *ucontrol) |
| 2661 | { |
| 2662 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2663 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2668 | struct snd_ctl_elem_value *ucontrol) |
| 2669 | { |
| 2670 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2671 | unsigned int old_mode = hook->mute_mode; |
| 2672 | |
| 2673 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2674 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2675 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2676 | if (old_mode == hook->mute_mode) |
| 2677 | return 0; |
| 2678 | snd_hda_sync_vmaster_hook(hook); |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
| 2682 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2683 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2684 | .name = "Mute-LED Mode", |
| 2685 | .info = vmaster_mute_mode_info, |
| 2686 | .get = vmaster_mute_mode_get, |
| 2687 | .put = vmaster_mute_mode_put, |
| 2688 | }; |
| 2689 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2690 | /** |
| 2691 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2692 | * @codec: the HDA codec |
| 2693 | * @hook: the vmaster hook object |
| 2694 | * @expose_enum_ctl: flag to create an enum ctl |
| 2695 | * |
| 2696 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2697 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2698 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2699 | */ |
| 2700 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2701 | struct hda_vmaster_mute_hook *hook, |
| 2702 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2703 | { |
| 2704 | struct snd_kcontrol *kctl; |
| 2705 | |
| 2706 | if (!hook->hook || !hook->sw_kctl) |
| 2707 | return 0; |
| 2708 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2709 | hook->codec = codec; |
| 2710 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735cb | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2711 | if (!expose_enum_ctl) |
| 2712 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2713 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2714 | if (!kctl) |
| 2715 | return -ENOMEM; |
| 2716 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2717 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2718 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2719 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2720 | /** |
| 2721 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 2722 | * @hook: the vmaster hook |
| 2723 | * |
| 2724 | * Call the hook with the current value for synchronization. |
| 2725 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2726 | */ |
| 2727 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2728 | { |
| 2729 | if (!hook->hook || !hook->codec) |
| 2730 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2731 | /* don't call vmaster hook in the destructor since it might have |
| 2732 | * been already destroyed |
| 2733 | */ |
| 2734 | if (hook->codec->bus->shutdown) |
| 2735 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2736 | switch (hook->mute_mode) { |
| 2737 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2738 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2739 | break; |
| 2740 | default: |
| 2741 | hook->hook(hook->codec, hook->mute_mode); |
| 2742 | break; |
| 2743 | } |
| 2744 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2745 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2746 | |
| 2747 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2748 | /** |
| 2749 | * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2750 | * @kcontrol: referred ctl element |
| 2751 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2752 | * |
| 2753 | * The control element is supposed to have the private_value field |
| 2754 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2755 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2756 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2757 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | { |
| 2759 | int chs = get_amp_channels(kcontrol); |
| 2760 | |
| 2761 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2762 | uinfo->count = chs == 3 ? 2 : 1; |
| 2763 | uinfo->value.integer.min = 0; |
| 2764 | uinfo->value.integer.max = 1; |
| 2765 | return 0; |
| 2766 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2767 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2769 | /** |
| 2770 | * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2771 | * @kcontrol: ctl element |
| 2772 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2773 | * |
| 2774 | * The control element is supposed to have the private_value field |
| 2775 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2776 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2777 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2778 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | { |
| 2780 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2781 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2782 | int chs = get_amp_channels(kcontrol); |
| 2783 | int dir = get_amp_direction(kcontrol); |
| 2784 | int idx = get_amp_index(kcontrol); |
| 2785 | long *valp = ucontrol->value.integer.value; |
| 2786 | |
| 2787 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2788 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2789 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2791 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2792 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2795 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2797 | /** |
| 2798 | * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2799 | * @kcontrol: ctl element |
| 2800 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2801 | * |
| 2802 | * The control element is supposed to have the private_value field |
| 2803 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2804 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2805 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2806 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | { |
| 2808 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2809 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2810 | int chs = get_amp_channels(kcontrol); |
| 2811 | int dir = get_amp_direction(kcontrol); |
| 2812 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | long *valp = ucontrol->value.integer.value; |
| 2814 | int change = 0; |
| 2815 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2816 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2817 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 2818 | HDA_AMP_MUTE, |
| 2819 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2820 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2821 | valp++; |
| 2822 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2823 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2824 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 2825 | HDA_AMP_MUTE, |
| 2826 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2827 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2828 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | return change; |
| 2830 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2831 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2834 | * bound volume controls |
| 2835 | * |
| 2836 | * bind multiple volumes (# indices, from 0) |
| 2837 | */ |
| 2838 | |
| 2839 | #define AMP_VAL_IDX_SHIFT 19 |
| 2840 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2841 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2842 | /** |
| 2843 | * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2844 | * @kcontrol: ctl element |
| 2845 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2846 | * |
| 2847 | * The control element is supposed to have the private_value field |
| 2848 | * set up via HDA_BIND_MUTE*() macros. |
| 2849 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2850 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2851 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2852 | { |
| 2853 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2854 | unsigned long pval; |
| 2855 | int err; |
| 2856 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2857 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2858 | pval = kcontrol->private_value; |
| 2859 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2860 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2861 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2862 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2863 | return err; |
| 2864 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2865 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2866 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2867 | /** |
| 2868 | * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2869 | * @kcontrol: ctl element |
| 2870 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2871 | * |
| 2872 | * The control element is supposed to have the private_value field |
| 2873 | * set up via HDA_BIND_MUTE*() macros. |
| 2874 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2875 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2876 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2877 | { |
| 2878 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2879 | unsigned long pval; |
| 2880 | int i, indices, err = 0, change = 0; |
| 2881 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2882 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2883 | pval = kcontrol->private_value; |
| 2884 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2885 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2886 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2887 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2888 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2889 | if (err < 0) |
| 2890 | break; |
| 2891 | change |= err; |
| 2892 | } |
| 2893 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2894 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2895 | return err < 0 ? err : change; |
| 2896 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2897 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2898 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2899 | /** |
| 2900 | * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2901 | * @kcontrol: referred ctl element |
| 2902 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2903 | * |
| 2904 | * The control element is supposed to have the private_value field |
| 2905 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2906 | */ |
| 2907 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2908 | struct snd_ctl_elem_info *uinfo) |
| 2909 | { |
| 2910 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2911 | struct hda_bind_ctls *c; |
| 2912 | int err; |
| 2913 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2914 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2915 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2916 | kcontrol->private_value = *c->values; |
| 2917 | err = c->ops->info(kcontrol, uinfo); |
| 2918 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2919 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2920 | return err; |
| 2921 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2922 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2923 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2924 | /** |
| 2925 | * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2926 | * @kcontrol: ctl element |
| 2927 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2928 | * |
| 2929 | * The control element is supposed to have the private_value field |
| 2930 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2931 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2932 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2933 | struct snd_ctl_elem_value *ucontrol) |
| 2934 | { |
| 2935 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2936 | struct hda_bind_ctls *c; |
| 2937 | int err; |
| 2938 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2939 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2940 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2941 | kcontrol->private_value = *c->values; |
| 2942 | err = c->ops->get(kcontrol, ucontrol); |
| 2943 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2944 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2945 | return err; |
| 2946 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2947 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2948 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2949 | /** |
| 2950 | * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2951 | * @kcontrol: ctl element |
| 2952 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2953 | * |
| 2954 | * The control element is supposed to have the private_value field |
| 2955 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2956 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2957 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2958 | struct snd_ctl_elem_value *ucontrol) |
| 2959 | { |
| 2960 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2961 | struct hda_bind_ctls *c; |
| 2962 | unsigned long *vals; |
| 2963 | int err = 0, change = 0; |
| 2964 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2965 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2966 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2967 | for (vals = c->values; *vals; vals++) { |
| 2968 | kcontrol->private_value = *vals; |
| 2969 | err = c->ops->put(kcontrol, ucontrol); |
| 2970 | if (err < 0) |
| 2971 | break; |
| 2972 | change |= err; |
| 2973 | } |
| 2974 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2975 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2976 | return err < 0 ? err : change; |
| 2977 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2978 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2979 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2980 | /** |
| 2981 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2982 | * @kcontrol: ctl element |
| 2983 | * @op_flag: operation flag |
| 2984 | * @size: byte size of input TLV |
| 2985 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2986 | * |
| 2987 | * The control element is supposed to have the private_value field |
| 2988 | * set up via HDA_BIND_VOL() macro. |
| 2989 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2990 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2991 | unsigned int size, unsigned int __user *tlv) |
| 2992 | { |
| 2993 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2994 | struct hda_bind_ctls *c; |
| 2995 | int err; |
| 2996 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2997 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2998 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2999 | kcontrol->private_value = *c->values; |
| 3000 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3001 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3002 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3003 | return err; |
| 3004 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3005 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3006 | |
| 3007 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3008 | .info = snd_hda_mixer_amp_volume_info, |
| 3009 | .get = snd_hda_mixer_amp_volume_get, |
| 3010 | .put = snd_hda_mixer_amp_volume_put, |
| 3011 | .tlv = snd_hda_mixer_amp_tlv |
| 3012 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3013 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3014 | |
| 3015 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3016 | .info = snd_hda_mixer_amp_switch_info, |
| 3017 | .get = snd_hda_mixer_amp_switch_get, |
| 3018 | .put = snd_hda_mixer_amp_switch_put, |
| 3019 | .tlv = snd_hda_mixer_amp_tlv |
| 3020 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3021 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3022 | |
| 3023 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | * SPDIF out controls |
| 3025 | */ |
| 3026 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3027 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3028 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | { |
| 3030 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3031 | uinfo->count = 1; |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3035 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3036 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | { |
| 3038 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3039 | IEC958_AES0_NONAUDIO | |
| 3040 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3041 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3042 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3043 | IEC958_AES1_CON_ORIGINAL; |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3047 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3048 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | { |
| 3050 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3051 | IEC958_AES0_NONAUDIO | |
| 3052 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3053 | return 0; |
| 3054 | } |
| 3055 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3056 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3057 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | { |
| 3059 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3060 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3061 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3063 | mutex_lock(&codec->spdif_mutex); |
| 3064 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3065 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3066 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3067 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3068 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3069 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | |
| 3071 | return 0; |
| 3072 | } |
| 3073 | |
| 3074 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3075 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3076 | */ |
| 3077 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3078 | { |
| 3079 | unsigned short val = 0; |
| 3080 | |
| 3081 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3082 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3084 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3086 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3087 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3088 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3090 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3091 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3092 | val |= AC_DIG1_EMPHASIS; |
| 3093 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3094 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3096 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3098 | } |
| 3099 | return val; |
| 3100 | } |
| 3101 | |
| 3102 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3103 | */ |
| 3104 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3105 | { |
| 3106 | unsigned int sbits = 0; |
| 3107 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3108 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3110 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3112 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3113 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3115 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3116 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3118 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3120 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3122 | sbits |= val & (0x7f << 8); |
| 3123 | } |
| 3124 | return sbits; |
| 3125 | } |
| 3126 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3127 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3128 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3129 | int verb, int val) |
| 3130 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3131 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3132 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3133 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3134 | d = codec->slave_dig_outs; |
| 3135 | if (!d) |
| 3136 | return; |
| 3137 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3138 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3142 | int dig1, int dig2) |
| 3143 | { |
| 3144 | if (dig1 != -1) |
| 3145 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3146 | if (dig2 != -1) |
| 3147 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3148 | } |
| 3149 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3150 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3151 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | { |
| 3153 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3154 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3155 | struct hda_spdif_out *spdif; |
| 3156 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | unsigned short val; |
| 3158 | int change; |
| 3159 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3160 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3161 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3162 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3163 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3165 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3166 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3167 | val = convert_from_spdif_status(spdif->status); |
| 3168 | val |= spdif->ctls & 1; |
| 3169 | change = spdif->ctls != val; |
| 3170 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3171 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3172 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3173 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | return change; |
| 3175 | } |
| 3176 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3177 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3179 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3180 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
| 3182 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3183 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3184 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3186 | mutex_lock(&codec->spdif_mutex); |
| 3187 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3188 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3189 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3193 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3194 | int dig1, int dig2) |
| 3195 | { |
| 3196 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3197 | /* unmute amp switch (if any) */ |
| 3198 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3199 | (dig1 & AC_DIG1_ENABLE)) |
| 3200 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3201 | HDA_AMP_MUTE, 0); |
| 3202 | } |
| 3203 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3204 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3205 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | { |
| 3207 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3208 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3209 | struct hda_spdif_out *spdif; |
| 3210 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | unsigned short val; |
| 3212 | int change; |
| 3213 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3214 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3215 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3216 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3217 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3219 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3220 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3221 | spdif->ctls = val; |
| 3222 | if (change && nid != (u16)-1) |
| 3223 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3224 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | return change; |
| 3226 | } |
| 3227 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3228 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | { |
| 3230 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3231 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3232 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | .info = snd_hda_spdif_mask_info, |
| 3234 | .get = snd_hda_spdif_cmask_get, |
| 3235 | }, |
| 3236 | { |
| 3237 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3238 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3239 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | .info = snd_hda_spdif_mask_info, |
| 3241 | .get = snd_hda_spdif_pmask_get, |
| 3242 | }, |
| 3243 | { |
| 3244 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3245 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | .info = snd_hda_spdif_mask_info, |
| 3247 | .get = snd_hda_spdif_default_get, |
| 3248 | .put = snd_hda_spdif_default_put, |
| 3249 | }, |
| 3250 | { |
| 3251 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3252 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | .info = snd_hda_spdif_out_switch_info, |
| 3254 | .get = snd_hda_spdif_out_switch_get, |
| 3255 | .put = snd_hda_spdif_out_switch_put, |
| 3256 | }, |
| 3257 | { } /* end */ |
| 3258 | }; |
| 3259 | |
| 3260 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3261 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3263 | * @associated_nid: NID that new ctls associated with |
| 3264 | * @cvt_nid: converter NID |
| 3265 | * @type: HDA_PCM_TYPE_* |
| 3266 | * Creates controls related with the digital output. |
| 3267 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | * |
| 3269 | * Returns 0 if successful, or a negative error code. |
| 3270 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3271 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3272 | hda_nid_t associated_nid, |
| 3273 | hda_nid_t cvt_nid, |
| 3274 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | { |
| 3276 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3277 | struct snd_kcontrol *kctl; |
| 3278 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3279 | int idx = 0; |
| 3280 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3281 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3282 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3284 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3285 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3286 | idx = spdif_index; |
| 3287 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3288 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3289 | /* suppose a single SPDIF device */ |
| 3290 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3291 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3292 | if (!kctl) |
| 3293 | break; |
| 3294 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3295 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3296 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3297 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3298 | if (!bus->primary_dig_out_type) |
| 3299 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3300 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3301 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3302 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3303 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3304 | return -EBUSY; |
| 3305 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3306 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3307 | if (!spdif) |
| 3308 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3310 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3311 | if (!kctl) |
| 3312 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3313 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3314 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3315 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3316 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | return err; |
| 3318 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3319 | spdif->nid = cvt_nid; |
| 3320 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3321 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3322 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | return 0; |
| 3324 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3325 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3327 | /** |
| 3328 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3329 | * @codec: the HDA codec |
| 3330 | * @nid: widget NID |
| 3331 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3332 | * call within spdif_mutex lock |
| 3333 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3334 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3335 | hda_nid_t nid) |
| 3336 | { |
| 3337 | int i; |
| 3338 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3339 | struct hda_spdif_out *spdif = |
| 3340 | snd_array_elem(&codec->spdif_out, i); |
| 3341 | if (spdif->nid == nid) |
| 3342 | return spdif; |
| 3343 | } |
| 3344 | return NULL; |
| 3345 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3346 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3347 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3348 | /** |
| 3349 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3350 | * @codec: the HDA codec |
| 3351 | * @idx: the SPDIF ctl index |
| 3352 | * |
| 3353 | * Unassign the widget from the given SPDIF control. |
| 3354 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3355 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3356 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3357 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3358 | |
| 3359 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3360 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3361 | spdif->nid = (u16)-1; |
| 3362 | mutex_unlock(&codec->spdif_mutex); |
| 3363 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3364 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3365 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3366 | /** |
| 3367 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3368 | * @codec: the HDA codec |
| 3369 | * @idx: the SPDIF ctl idx |
| 3370 | * @nid: widget NID |
| 3371 | * |
| 3372 | * Assign the widget to the SPDIF control with the given index. |
| 3373 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3374 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3375 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3376 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3377 | unsigned short val; |
| 3378 | |
| 3379 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3380 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3381 | if (spdif->nid != nid) { |
| 3382 | spdif->nid = nid; |
| 3383 | val = spdif->ctls; |
| 3384 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3385 | } |
| 3386 | mutex_unlock(&codec->spdif_mutex); |
| 3387 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3388 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3391 | * SPDIF sharing with analog output |
| 3392 | */ |
| 3393 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3394 | struct snd_ctl_elem_value *ucontrol) |
| 3395 | { |
| 3396 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3397 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3398 | return 0; |
| 3399 | } |
| 3400 | |
| 3401 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3402 | struct snd_ctl_elem_value *ucontrol) |
| 3403 | { |
| 3404 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3405 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
| 3409 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3410 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3411 | .name = "IEC958 Default PCM Playback Switch", |
| 3412 | .info = snd_ctl_boolean_mono_info, |
| 3413 | .get = spdif_share_sw_get, |
| 3414 | .put = spdif_share_sw_put, |
| 3415 | }; |
| 3416 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3417 | /** |
| 3418 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3419 | * @codec: the HDA codec |
| 3420 | * @mout: multi-out instance |
| 3421 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3422 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3423 | struct hda_multi_out *mout) |
| 3424 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3425 | struct snd_kcontrol *kctl; |
| 3426 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3427 | if (!mout->dig_out_nid) |
| 3428 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3429 | |
| 3430 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3431 | if (!kctl) |
| 3432 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3433 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3434 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3435 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3436 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | * SPDIF input |
| 3440 | */ |
| 3441 | |
| 3442 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3443 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3444 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3445 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | { |
| 3447 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3448 | |
| 3449 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3453 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3454 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | { |
| 3456 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3457 | hda_nid_t nid = kcontrol->private_value; |
| 3458 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3459 | int change; |
| 3460 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3461 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3463 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3465 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3466 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3468 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | return change; |
| 3470 | } |
| 3471 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3472 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3473 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | { |
| 3475 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3476 | hda_nid_t nid = kcontrol->private_value; |
| 3477 | unsigned short val; |
| 3478 | unsigned int sbits; |
| 3479 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3480 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | sbits = convert_to_spdif_status(val); |
| 3482 | ucontrol->value.iec958.status[0] = sbits; |
| 3483 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3484 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3485 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3489 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | { |
| 3491 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3492 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | .info = snd_hda_spdif_in_switch_info, |
| 3494 | .get = snd_hda_spdif_in_switch_get, |
| 3495 | .put = snd_hda_spdif_in_switch_put, |
| 3496 | }, |
| 3497 | { |
| 3498 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3499 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3500 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | .info = snd_hda_spdif_mask_info, |
| 3502 | .get = snd_hda_spdif_in_status_get, |
| 3503 | }, |
| 3504 | { } /* end */ |
| 3505 | }; |
| 3506 | |
| 3507 | /** |
| 3508 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3509 | * @codec: the HDA codec |
| 3510 | * @nid: audio in widget NID |
| 3511 | * |
| 3512 | * Creates controls related with the SPDIF input. |
| 3513 | * Called from each patch supporting the SPDIF in. |
| 3514 | * |
| 3515 | * Returns 0 if successful, or a negative error code. |
| 3516 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3517 | int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | { |
| 3519 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3520 | struct snd_kcontrol *kctl; |
| 3521 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3522 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3524 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3525 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3526 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3527 | return -EBUSY; |
| 3528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3530 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3531 | if (!kctl) |
| 3532 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3534 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3535 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | return err; |
| 3537 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3538 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3539 | snd_hda_codec_read(codec, nid, 0, |
| 3540 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3541 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | return 0; |
| 3543 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3544 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3546 | /* |
| 3547 | * command cache |
| 3548 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3550 | /* build a 31bit cache key with the widget id and the command parameter */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3551 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3552 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3553 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3554 | |
| 3555 | /** |
| 3556 | * snd_hda_codec_write_cache - send a single command with caching |
| 3557 | * @codec: the HDA codec |
| 3558 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3559 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3560 | * @verb: the verb to send |
| 3561 | * @parm: the parameter for the verb |
| 3562 | * |
| 3563 | * Send a single command without waiting for response. |
| 3564 | * |
| 3565 | * Returns 0 if successful, or a negative error code. |
| 3566 | */ |
| 3567 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3568 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3569 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3570 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3571 | struct hda_cache_head *c; |
| 3572 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3573 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3574 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3575 | cache_only = codec->cached_write; |
| 3576 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3577 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3578 | if (err < 0) |
| 3579 | return err; |
| 3580 | } |
| 3581 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3582 | /* parm may contain the verb stuff for get/set amp */ |
| 3583 | verb = verb | (parm >> 8); |
| 3584 | parm &= 0xff; |
| 3585 | key = build_cmd_cache_key(nid, verb); |
| 3586 | mutex_lock(&codec->bus->cmd_mutex); |
| 3587 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3588 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3589 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3590 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3591 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3592 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3593 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3594 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3595 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3596 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3597 | /** |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3598 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3599 | * @codec: the HDA codec |
| 3600 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3601 | * @flags: optional bit flags |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3602 | * @verb: the verb to send |
| 3603 | * @parm: the parameter for the verb |
| 3604 | * |
| 3605 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3606 | * command if the parameter is already identical with the cached value. |
| 3607 | * If not, it sends the command and refreshes the cache. |
| 3608 | * |
| 3609 | * Returns 0 if successful, or a negative error code. |
| 3610 | */ |
| 3611 | int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3612 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3613 | { |
| 3614 | struct hda_cache_head *c; |
| 3615 | u32 key; |
| 3616 | |
| 3617 | /* parm may contain the verb stuff for get/set amp */ |
| 3618 | verb = verb | (parm >> 8); |
| 3619 | parm &= 0xff; |
| 3620 | key = build_cmd_cache_key(nid, verb); |
| 3621 | mutex_lock(&codec->bus->cmd_mutex); |
| 3622 | c = get_hash(&codec->cmd_cache, key); |
| 3623 | if (c && c->val == parm) { |
| 3624 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3625 | return 0; |
| 3626 | } |
| 3627 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3628 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3629 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3630 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a54 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3631 | |
| 3632 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3633 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3634 | * @codec: HD-audio codec |
| 3635 | * |
| 3636 | * Execute all verbs recorded in the command caches to resume. |
| 3637 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3638 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3639 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3640 | int i; |
| 3641 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3642 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3643 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3644 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3645 | struct hda_cache_head *buffer; |
| 3646 | u32 key; |
| 3647 | |
| 3648 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3649 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3650 | if (!key) |
| 3651 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3652 | if (!buffer->dirty) |
| 3653 | continue; |
| 3654 | buffer->dirty = 0; |
| 3655 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3656 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3657 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3658 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3659 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3660 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3661 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3662 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3663 | |
| 3664 | /** |
| 3665 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3666 | * @codec: the HDA codec |
| 3667 | * @seq: VERB array to send |
| 3668 | * |
| 3669 | * Send the commands sequentially from the given array. |
| 3670 | * Thte commands are recorded on cache for power-save and resume. |
| 3671 | * The array must be terminated with NID=0. |
| 3672 | */ |
| 3673 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3674 | const struct hda_verb *seq) |
| 3675 | { |
| 3676 | for (; seq->nid; seq++) |
| 3677 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3678 | seq->param); |
| 3679 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3680 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3681 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3682 | /** |
| 3683 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3684 | * @codec: HD-audio codec |
| 3685 | */ |
| 3686 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3687 | { |
| 3688 | snd_hda_codec_resume_amp(codec); |
| 3689 | snd_hda_codec_resume_cache(codec); |
| 3690 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3691 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3692 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3693 | /** |
| 3694 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3695 | * @codec: the HDA codec |
| 3696 | * @fg: function group (not used now) |
| 3697 | * @power_state: the power state to set (AC_PWRST_*) |
| 3698 | * |
| 3699 | * Set the given power state to all widgets that have the power control. |
| 3700 | * If the codec has power_filter set, it evaluates the power state and |
| 3701 | * filter out if it's unchanged as D3. |
| 3702 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3703 | void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3704 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3705 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3706 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3707 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3708 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3709 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3710 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3711 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3712 | if (!(wcaps & AC_WCAP_POWER)) |
| 3713 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3714 | if (codec->power_filter) { |
| 3715 | state = codec->power_filter(codec, nid, power_state); |
| 3716 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3717 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3718 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3719 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3720 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3721 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3722 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3723 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3724 | |
| 3725 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3726 | * supported power states check |
| 3727 | */ |
| 3728 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3729 | unsigned int power_state) |
| 3730 | { |
| 3731 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3732 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3733 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3734 | return false; |
| 3735 | if (sup & power_state) |
| 3736 | return true; |
| 3737 | else |
| 3738 | return false; |
| 3739 | } |
| 3740 | |
| 3741 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3742 | * wait until the state is reached, returns the current state |
| 3743 | */ |
| 3744 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3745 | hda_nid_t fg, |
| 3746 | unsigned int power_state) |
| 3747 | { |
| 3748 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3749 | unsigned int state, actual_state; |
| 3750 | |
| 3751 | for (;;) { |
| 3752 | state = snd_hda_codec_read(codec, fg, 0, |
| 3753 | AC_VERB_GET_POWER_STATE, 0); |
| 3754 | if (state & AC_PWRST_ERROR) |
| 3755 | break; |
| 3756 | actual_state = (state >> 4) & 0x0f; |
| 3757 | if (actual_state == power_state) |
| 3758 | break; |
| 3759 | if (time_after_eq(jiffies, end_time)) |
| 3760 | break; |
| 3761 | /* wait until the codec reachs to the target state */ |
| 3762 | msleep(1); |
| 3763 | } |
| 3764 | return state; |
| 3765 | } |
| 3766 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3767 | /** |
| 3768 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 3769 | * @codec: the HDA codec |
| 3770 | * @nid: widget NID |
| 3771 | * @power_state: power state to evalue |
| 3772 | * |
| 3773 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 3774 | * This can be used a codec power_filter callback. |
| 3775 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3776 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3777 | hda_nid_t nid, |
| 3778 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3779 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3780 | if (nid == codec->afg || nid == codec->mfg) |
| 3781 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3782 | if (power_state == AC_PWRST_D3 && |
| 3783 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3784 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3785 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3786 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3787 | if (eapd & 0x02) |
| 3788 | return AC_PWRST_D0; |
| 3789 | } |
| 3790 | return power_state; |
| 3791 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3792 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3793 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3794 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3795 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3796 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3797 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3798 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3799 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3800 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3801 | int count; |
| 3802 | unsigned int state; |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3803 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3804 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3805 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3806 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3807 | if (codec->depop_delay < 0) |
| 3808 | msleep(codec->epss ? 10 : 100); |
| 3809 | else if (codec->depop_delay > 0) |
| 3810 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd7 | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3811 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3812 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3813 | |
| 3814 | /* repeat power states setting at most 10 times*/ |
| 3815 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3816 | if (codec->patch_ops.set_power_state) |
| 3817 | codec->patch_ops.set_power_state(codec, fg, |
| 3818 | power_state); |
| 3819 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3820 | state = power_state; |
| 3821 | if (codec->power_filter) |
| 3822 | state = codec->power_filter(codec, fg, state); |
| 3823 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3824 | snd_hda_codec_read(codec, fg, flags, |
| 3825 | AC_VERB_SET_POWER_STATE, |
| 3826 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3827 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3828 | } |
| 3829 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3830 | if (!(state & AC_PWRST_ERROR)) |
| 3831 | break; |
| 3832 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3833 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3834 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3835 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3836 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3837 | /* sync power states of all widgets; |
| 3838 | * this is called at the end of codec parsing |
| 3839 | */ |
| 3840 | static void sync_power_up_states(struct hda_codec *codec) |
| 3841 | { |
| 3842 | hda_nid_t nid = codec->start_nid; |
| 3843 | int i; |
| 3844 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3845 | /* don't care if no filter is used */ |
| 3846 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3847 | return; |
| 3848 | |
| 3849 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3850 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3851 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3852 | if (!(wcaps & AC_WCAP_POWER)) |
| 3853 | continue; |
| 3854 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3855 | if (target == AC_PWRST_D0) |
| 3856 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3857 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3858 | snd_hda_codec_write(codec, nid, 0, |
| 3859 | AC_VERB_SET_POWER_STATE, target); |
| 3860 | } |
| 3861 | } |
| 3862 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 3863 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3864 | /* execute additional init verbs */ |
| 3865 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3866 | { |
| 3867 | if (codec->init_verbs.list) |
| 3868 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3869 | } |
| 3870 | #else |
| 3871 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3872 | #endif |
| 3873 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3874 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3875 | /* update the power on/off account with the current jiffies */ |
| 3876 | static void update_power_acct(struct hda_codec *codec, bool on) |
| 3877 | { |
| 3878 | unsigned long delta = jiffies - codec->power_jiffies; |
| 3879 | |
| 3880 | if (on) |
| 3881 | codec->power_on_acct += delta; |
| 3882 | else |
| 3883 | codec->power_off_acct += delta; |
| 3884 | codec->power_jiffies += delta; |
| 3885 | } |
| 3886 | |
| 3887 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 3888 | { |
| 3889 | update_power_acct(codec, hda_codec_is_power_on(codec)); |
| 3890 | } |
| 3891 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3892 | /* |
| 3893 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3894 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3895 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3896 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3897 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3898 | unsigned int state; |
| 3899 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3900 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3901 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3902 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3903 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3904 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3905 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3906 | trace_hda_power_down(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3907 | update_power_acct(codec, true); |
| 3908 | atomic_dec(&codec->in_pm); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3909 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3910 | } |
| 3911 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3912 | /* mark all entries of cmd and amp caches dirty */ |
| 3913 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3914 | { |
| 3915 | int i; |
| 3916 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3917 | struct hda_cache_head *cmd; |
| 3918 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3919 | cmd->dirty = 1; |
| 3920 | } |
| 3921 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3922 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3923 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3924 | amp->head.dirty = 1; |
| 3925 | } |
| 3926 | } |
| 3927 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3928 | /* |
| 3929 | * kick up codec; used both from PM and power-save |
| 3930 | */ |
| 3931 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3932 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3933 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3934 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3935 | trace_hda_power_up(codec); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3936 | hda_mark_cmd_cache_dirty(codec); |
| 3937 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3938 | codec->power_jiffies = jiffies; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3939 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3940 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3941 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3942 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3943 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3944 | if (codec->patch_ops.resume) |
| 3945 | codec->patch_ops.resume(codec); |
| 3946 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3947 | if (codec->patch_ops.init) |
| 3948 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3949 | snd_hda_codec_resume_amp(codec); |
| 3950 | snd_hda_codec_resume_cache(codec); |
| 3951 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3952 | |
| 3953 | if (codec->jackpoll_interval) |
| 3954 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3955 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3956 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3957 | atomic_dec(&codec->in_pm); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3958 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3959 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3960 | static int hda_codec_runtime_suspend(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3961 | { |
| 3962 | struct hda_codec *codec = dev_to_hda_codec(dev); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3963 | unsigned int state; |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3964 | int i; |
| 3965 | |
| 3966 | cancel_delayed_work_sync(&codec->jackpoll_work); |
| 3967 | for (i = 0; i < codec->num_pcms; i++) |
| 3968 | snd_pcm_suspend_all(codec->pcm_info[i].pcm); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3969 | state = hda_call_codec_suspend(codec); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3970 | if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK)) |
| 3971 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3972 | return 0; |
| 3973 | } |
| 3974 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3975 | static int hda_codec_runtime_resume(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3976 | { |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame] | 3977 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 3978 | |
| 3979 | set_bit(codec->addr, &codec->bus->codec_powered); |
| 3980 | hda_call_codec_resume(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3981 | pm_runtime_mark_last_busy(dev); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3982 | return 0; |
| 3983 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3984 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3985 | |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3986 | /* referred in hda_bind.c */ |
| 3987 | const struct dev_pm_ops hda_codec_driver_pm = { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3988 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 3989 | pm_runtime_force_resume) |
| 3990 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, |
| 3991 | NULL) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3992 | }; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | /** |
| 3995 | * snd_hda_build_controls - build mixer controls |
| 3996 | * @bus: the BUS |
| 3997 | * |
| 3998 | * Creates mixer controls for each codec included in the bus. |
| 3999 | * |
| 4000 | * Returns 0 if successful, otherwise a negative error code. |
| 4001 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4002 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4004 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4006 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4007 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4008 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4009 | codec_err(codec, |
| 4010 | "cannot build controls for #%d (error %d)\n", |
| 4011 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4012 | err = snd_hda_codec_reset(codec); |
| 4013 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4014 | codec_err(codec, |
| 4015 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4016 | return err; |
| 4017 | } |
| 4018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4020 | return 0; |
| 4021 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4022 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4024 | /* |
| 4025 | * add standard channel maps if not specified |
| 4026 | */ |
| 4027 | static int add_std_chmaps(struct hda_codec *codec) |
| 4028 | { |
| 4029 | int i, str, err; |
| 4030 | |
| 4031 | for (i = 0; i < codec->num_pcms; i++) { |
| 4032 | for (str = 0; str < 2; str++) { |
| 4033 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4034 | struct hda_pcm_stream *hinfo = |
| 4035 | &codec->pcm_info[i].stream[str]; |
| 4036 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4037 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4038 | |
| 4039 | if (codec->pcm_info[i].own_chmap) |
| 4040 | continue; |
| 4041 | if (!pcm || !hinfo->substreams) |
| 4042 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4043 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4044 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4045 | hinfo->channels_max, |
| 4046 | 0, &chmap); |
| 4047 | if (err < 0) |
| 4048 | return err; |
| 4049 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4050 | } |
| 4051 | } |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4055 | /* default channel maps for 2.1 speakers; |
| 4056 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4057 | */ |
| 4058 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4059 | { .channels = 2, |
| 4060 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4061 | { .channels = 4, |
| 4062 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4063 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4064 | { } |
| 4065 | }; |
| 4066 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4067 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4068 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4069 | { |
| 4070 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4071 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4072 | /* continue to initialize... */ |
| 4073 | if (codec->patch_ops.init) |
| 4074 | err = codec->patch_ops.init(codec); |
| 4075 | if (!err && codec->patch_ops.build_controls) |
| 4076 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4077 | if (err < 0) |
| 4078 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4079 | |
| 4080 | /* we create chmaps here instead of build_pcms */ |
| 4081 | err = add_std_chmaps(codec); |
| 4082 | if (err < 0) |
| 4083 | return err; |
| 4084 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4085 | if (codec->jackpoll_interval) |
| 4086 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4087 | else |
| 4088 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4089 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | return 0; |
| 4091 | } |
| 4092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | /* |
| 4094 | * stream formats |
| 4095 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4096 | struct hda_rate_tbl { |
| 4097 | unsigned int hz; |
| 4098 | unsigned int alsa_bits; |
| 4099 | unsigned int hda_fmt; |
| 4100 | }; |
| 4101 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4102 | /* rate = base * mult / div */ |
| 4103 | #define HDA_RATE(base, mult, div) \ |
| 4104 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4105 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4106 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4107 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4109 | |
| 4110 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4111 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4112 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4113 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4114 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4115 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4116 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4117 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4118 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4119 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4120 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4121 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4122 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4123 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4124 | |
| 4125 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4126 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4127 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4128 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | }; |
| 4130 | |
| 4131 | /** |
| 4132 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4133 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | * @rate: the sample rate |
| 4135 | * @channels: the number of channels |
| 4136 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4137 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4138 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | * |
| 4140 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4141 | * |
| 4142 | * Return zero if invalid. |
| 4143 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4144 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4145 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | unsigned int channels, |
| 4147 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4148 | unsigned int maxbps, |
| 4149 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | { |
| 4151 | int i; |
| 4152 | unsigned int val = 0; |
| 4153 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4154 | for (i = 0; rate_bits[i].hz; i++) |
| 4155 | if (rate_bits[i].hz == rate) { |
| 4156 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | break; |
| 4158 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4159 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4160 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4165 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | return 0; |
| 4167 | } |
| 4168 | val |= channels - 1; |
| 4169 | |
| 4170 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4171 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4172 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4173 | break; |
| 4174 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4175 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4176 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | case 20: |
| 4178 | case 24: |
| 4179 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4180 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4181 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4183 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4185 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | break; |
| 4187 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4188 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4189 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | return 0; |
| 4191 | } |
| 4192 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4193 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4194 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | return val; |
| 4197 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4198 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4200 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4201 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4202 | { |
| 4203 | unsigned int val = 0; |
| 4204 | if (nid != codec->afg && |
| 4205 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4206 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4207 | if (!val || val == -1) |
| 4208 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4209 | if (!val || val == -1) |
| 4210 | return 0; |
| 4211 | return val; |
| 4212 | } |
| 4213 | |
| 4214 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4215 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4216 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4217 | get_pcm_param); |
| 4218 | } |
| 4219 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4220 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4221 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4222 | { |
| 4223 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4224 | if (!streams || streams == -1) |
| 4225 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4226 | if (!streams || streams == -1) |
| 4227 | return 0; |
| 4228 | return streams; |
| 4229 | } |
| 4230 | |
| 4231 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4232 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4233 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4234 | get_stream_param); |
| 4235 | } |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | /** |
| 4238 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4239 | * @codec: the HDA codec |
| 4240 | * @nid: NID to query |
| 4241 | * @ratesp: the pointer to store the detected rate bitflags |
| 4242 | * @formatsp: the pointer to store the detected formats |
| 4243 | * @bpsp: the pointer to store the detected format widths |
| 4244 | * |
| 4245 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4246 | * or @bsps argument is ignored. |
| 4247 | * |
| 4248 | * Returns 0 if successful, otherwise a negative error code. |
| 4249 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4250 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4252 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4253 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4255 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4256 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
| 4258 | if (ratesp) { |
| 4259 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4260 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4262 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4264 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4265 | codec_err(codec, |
| 4266 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4267 | nid, val, |
| 4268 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4269 | return -EIO; |
| 4270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | *ratesp = rates; |
| 4272 | } |
| 4273 | |
| 4274 | if (formatsp || bpsp) { |
| 4275 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4276 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4278 | streams = query_stream_param(codec, nid); |
| 4279 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | |
| 4282 | bps = 0; |
| 4283 | if (streams & AC_SUPFMT_PCM) { |
| 4284 | if (val & AC_SUPPCM_BITS_8) { |
| 4285 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4286 | bps = 8; |
| 4287 | } |
| 4288 | if (val & AC_SUPPCM_BITS_16) { |
| 4289 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4290 | bps = 16; |
| 4291 | } |
| 4292 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4293 | if (val & AC_SUPPCM_BITS_32) |
| 4294 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4295 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4296 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4297 | if (val & AC_SUPPCM_BITS_24) |
| 4298 | bps = 24; |
| 4299 | else if (val & AC_SUPPCM_BITS_20) |
| 4300 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4301 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4302 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4304 | if (val & AC_SUPPCM_BITS_32) |
| 4305 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | else if (val & AC_SUPPCM_BITS_24) |
| 4307 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4308 | else if (val & AC_SUPPCM_BITS_20) |
| 4309 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | } |
| 4311 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4312 | #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4313 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4315 | if (!bps) |
| 4316 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4317 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4318 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4319 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4320 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | /* temporary hack: we have still no proper support |
| 4322 | * for the direct AC3 stream... |
| 4323 | */ |
| 4324 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4325 | bps = 8; |
| 4326 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4327 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4328 | codec_err(codec, |
| 4329 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4330 | nid, val, |
| 4331 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4332 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4333 | return -EIO; |
| 4334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | if (formatsp) |
| 4336 | *formatsp = formats; |
| 4337 | if (bpsp) |
| 4338 | *bpsp = bps; |
| 4339 | } |
| 4340 | |
| 4341 | return 0; |
| 4342 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4343 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | |
| 4345 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4346 | * snd_hda_is_supported_format - Check the validity of the format |
| 4347 | * @codec: HD-audio codec |
| 4348 | * @nid: NID to check |
| 4349 | * @format: the HD-audio format value to check |
| 4350 | * |
| 4351 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | * |
| 4353 | * Returns 1 if supported, 0 if not. |
| 4354 | */ |
| 4355 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4356 | unsigned int format) |
| 4357 | { |
| 4358 | int i; |
| 4359 | unsigned int val = 0, rate, stream; |
| 4360 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4361 | val = query_pcm_param(codec, nid); |
| 4362 | if (!val) |
| 4363 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | |
| 4365 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4366 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4367 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | if (val & (1 << i)) |
| 4369 | break; |
| 4370 | return 0; |
| 4371 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4372 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | return 0; |
| 4374 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4375 | stream = query_stream_param(codec, nid); |
| 4376 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | return 0; |
| 4378 | |
| 4379 | if (stream & AC_SUPFMT_PCM) { |
| 4380 | switch (format & 0xf0) { |
| 4381 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4382 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | return 0; |
| 4384 | break; |
| 4385 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4386 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | return 0; |
| 4388 | break; |
| 4389 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4390 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | return 0; |
| 4392 | break; |
| 4393 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4394 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | return 0; |
| 4396 | break; |
| 4397 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4398 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | return 0; |
| 4400 | break; |
| 4401 | default: |
| 4402 | return 0; |
| 4403 | } |
| 4404 | } else { |
| 4405 | /* FIXME: check for float32 and AC3? */ |
| 4406 | } |
| 4407 | |
| 4408 | return 1; |
| 4409 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4410 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * PCM stuff |
| 4414 | */ |
| 4415 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4416 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4417 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | { |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
| 4422 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4423 | struct hda_codec *codec, |
| 4424 | unsigned int stream_tag, |
| 4425 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4426 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | { |
| 4428 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4429 | return 0; |
| 4430 | } |
| 4431 | |
| 4432 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4433 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4434 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4436 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4440 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4441 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4443 | int err; |
| 4444 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4445 | /* query support PCM information from the given NID */ |
| 4446 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4447 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4448 | info->rates ? NULL : &info->rates, |
| 4449 | info->formats ? NULL : &info->formats, |
| 4450 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4451 | if (err < 0) |
| 4452 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | } |
| 4454 | if (info->ops.open == NULL) |
| 4455 | info->ops.open = hda_pcm_default_open_close; |
| 4456 | if (info->ops.close == NULL) |
| 4457 | info->ops.close = hda_pcm_default_open_close; |
| 4458 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4459 | if (snd_BUG_ON(!info->nid)) |
| 4460 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | info->ops.prepare = hda_pcm_default_prepare; |
| 4462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4464 | if (snd_BUG_ON(!info->nid)) |
| 4465 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4467 | } |
| 4468 | return 0; |
| 4469 | } |
| 4470 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4471 | /* |
| 4472 | * codec prepare/cleanup entries |
| 4473 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4474 | /** |
| 4475 | * snd_hda_codec_prepare - Prepare a stream |
| 4476 | * @codec: the HDA codec |
| 4477 | * @hinfo: PCM information |
| 4478 | * @stream: stream tag to assign |
| 4479 | * @format: format id to assign |
| 4480 | * @substream: PCM substream to assign |
| 4481 | * |
| 4482 | * Calls the prepare callback set by the codec with the given arguments. |
| 4483 | * Clean up the inactive streams when successful. |
| 4484 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4485 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4486 | struct hda_pcm_stream *hinfo, |
| 4487 | unsigned int stream, |
| 4488 | unsigned int format, |
| 4489 | struct snd_pcm_substream *substream) |
| 4490 | { |
| 4491 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4492 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4493 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4494 | if (ret >= 0) |
| 4495 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4496 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4497 | return ret; |
| 4498 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4499 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4500 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4501 | /** |
| 4502 | * snd_hda_codec_cleanup - Prepare a stream |
| 4503 | * @codec: the HDA codec |
| 4504 | * @hinfo: PCM information |
| 4505 | * @substream: PCM substream |
| 4506 | * |
| 4507 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4508 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4509 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4510 | struct hda_pcm_stream *hinfo, |
| 4511 | struct snd_pcm_substream *substream) |
| 4512 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4513 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4514 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4515 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4516 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4517 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4518 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4519 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4520 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4521 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4522 | }; |
| 4523 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4524 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4525 | * get the empty PCM device number to assign |
| 4526 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4527 | static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4528 | { |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4529 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4530 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4531 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4532 | */ |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4533 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4534 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4535 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4536 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4537 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4538 | }; |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4539 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4540 | |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4541 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4542 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4543 | return -EINVAL; |
| 4544 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4545 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4546 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4547 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4548 | if (audio_idx[type][i] >= 8) |
| 4549 | break; |
| 4550 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4551 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4552 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4553 | } |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4554 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4555 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4556 | /* non-fixed slots starting from 10 */ |
| 4557 | for (i = 10; i < 32; i++) { |
| 4558 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4559 | return i; |
| 4560 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4561 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4562 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4563 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4564 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4565 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4566 | dev_warn(bus->card->dev, |
| 4567 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4568 | #endif |
Wu Fengguang | f5d6def5 | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4569 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4570 | } |
| 4571 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4572 | /* call build_pcms ops of the given codec and set up the default parameters */ |
| 4573 | int snd_hda_codec_parse_pcms(struct hda_codec *codec) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4574 | { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4575 | unsigned int pcm; |
| 4576 | int err; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4577 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4578 | if (codec->num_pcms) |
| 4579 | return 0; /* already parsed */ |
| 4580 | |
| 4581 | if (!codec->patch_ops.build_pcms) |
| 4582 | return 0; |
| 4583 | |
| 4584 | err = codec->patch_ops.build_pcms(codec); |
| 4585 | if (err < 0) { |
| 4586 | codec_err(codec, "cannot build PCMs for #%d (error %d)\n", |
| 4587 | codec->addr, err); |
| 4588 | return err; |
| 4589 | } |
| 4590 | |
| 4591 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4592 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4593 | int stream; |
| 4594 | |
| 4595 | for (stream = 0; stream < 2; stream++) { |
| 4596 | struct hda_pcm_stream *info = &cpcm->stream[stream]; |
| 4597 | |
| 4598 | if (!info->substreams) |
| 4599 | continue; |
| 4600 | if (snd_BUG_ON(!cpcm->name)) |
| 4601 | return -EINVAL; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4602 | err = set_pcm_default_values(codec, info); |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4603 | if (err < 0) { |
| 4604 | codec_warn(codec, |
| 4605 | "fail to setup default for PCM %s\n", |
| 4606 | cpcm->name); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4607 | return err; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4608 | } |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4609 | } |
| 4610 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4611 | |
| 4612 | return 0; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4613 | } |
| 4614 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4615 | /* assign all PCMs of the given codec */ |
| 4616 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4617 | { |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4618 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4619 | unsigned int pcm; |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4620 | int dev, err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4621 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4622 | if (snd_BUG_ON(!bus->ops.attach_pcm)) |
| 4623 | return -EINVAL; |
| 4624 | |
| 4625 | err = snd_hda_codec_parse_pcms(codec); |
| 4626 | if (err < 0) { |
| 4627 | snd_hda_codec_reset(codec); |
| 4628 | return err; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4629 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4630 | |
| 4631 | /* attach a new PCM streams */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4632 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4633 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4634 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4635 | if (cpcm->pcm) |
| 4636 | continue; /* already attached */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4637 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4638 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4639 | |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4640 | dev = get_empty_pcm_device(bus, cpcm->pcm_type); |
| 4641 | if (dev < 0) |
| 4642 | continue; /* no fatal error */ |
| 4643 | cpcm->device = dev; |
| 4644 | err = bus->ops.attach_pcm(bus, codec, cpcm); |
| 4645 | if (err < 0) { |
| 4646 | codec_err(codec, |
| 4647 | "cannot attach PCM stream %d for codec #%d\n", |
| 4648 | dev, codec->addr); |
| 4649 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4650 | } |
| 4651 | } |
Takashi Iwai | 1a4ba30 | 2015-02-24 11:50:11 +0100 | [diff] [blame] | 4652 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4653 | return 0; |
| 4654 | } |
| 4655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | /** |
| 4657 | * snd_hda_build_pcms - build PCM information |
| 4658 | * @bus: the BUS |
| 4659 | * |
| 4660 | * Create PCM information for each codec included in the bus. |
| 4661 | * |
| 4662 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4663 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4664 | * to create its PCM instances. |
| 4665 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4666 | * callback. |
| 4667 | * |
| 4668 | * At least, substreams, channels_min and channels_max must be filled for |
| 4669 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4670 | * When rates and/or formats are zero, the supported values are queried |
| 4671 | * from the given nid. The nid is used also by the default ops.prepare |
| 4672 | * and ops.cleanup callbacks. |
| 4673 | * |
| 4674 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4675 | * ops.close is supposed to be called in the close callback. |
| 4676 | * ops.prepare should be called in the prepare or hw_params callback |
| 4677 | * with the proper parameters for set up. |
| 4678 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4679 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4680 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4682 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4684 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4686 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4687 | int err = snd_hda_codec_build_pcms(codec); |
| 4688 | if (err < 0) |
| 4689 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | } |
| 4691 | return 0; |
| 4692 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4693 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | * snd_hda_add_new_ctls - create controls from the array |
| 4697 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4698 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | * |
| 4700 | * This helper function creates and add new controls in the given array. |
| 4701 | * The array must be terminated with an empty entry as terminator. |
| 4702 | * |
| 4703 | * Returns 0 if successful, or a negative error code. |
| 4704 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4705 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4706 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4708 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | |
| 4710 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4711 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4712 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4713 | if (knew->iface == -1) /* skip this codec private value */ |
| 4714 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4715 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4716 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4717 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4718 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4719 | if (addr > 0) |
| 4720 | kctl->id.device = addr; |
| 4721 | if (idx > 0) |
| 4722 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4723 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4724 | if (!err) |
| 4725 | break; |
| 4726 | /* try first with another device index corresponding to |
| 4727 | * the codec addr; if it still fails (or it's the |
| 4728 | * primary codec), then try another control index |
| 4729 | */ |
| 4730 | if (!addr && codec->addr) |
| 4731 | addr = codec->addr; |
| 4732 | else if (!idx && !knew->index) { |
| 4733 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4734 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4735 | if (idx <= 0) |
| 4736 | return err; |
| 4737 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4738 | return err; |
| 4739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | } |
| 4741 | return 0; |
| 4742 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4743 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4745 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4746 | /** |
| 4747 | * snd_hda_power_up - Power-up the codec |
| 4748 | * @codec: HD-audio codec |
| 4749 | * |
| 4750 | * Increment the usage counter and resume the device if not done yet. |
| 4751 | */ |
| 4752 | void snd_hda_power_up(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4753 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4754 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4755 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4756 | if (codec_in_pm(codec)) |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4757 | return; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4758 | pm_runtime_get_sync(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4759 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4760 | EXPORT_SYMBOL_GPL(snd_hda_power_up); |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4761 | |
| 4762 | /** |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4763 | * snd_hda_power_down - Power-down the codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4764 | * @codec: HD-audio codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4765 | * |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4766 | * Decrement the usage counter and schedules the autosuspend if none used. |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4767 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4768 | void snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4769 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4770 | struct device *dev = hda_codec_dev(codec); |
| 4771 | |
| 4772 | if (codec_in_pm(codec)) |
| 4773 | return; |
| 4774 | pm_runtime_mark_last_busy(dev); |
| 4775 | pm_runtime_put_autosuspend(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4776 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4777 | EXPORT_SYMBOL_GPL(snd_hda_power_down); |
| 4778 | |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4779 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4780 | { |
| 4781 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4782 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4783 | if (delay > 0) { |
| 4784 | pm_runtime_set_autosuspend_delay(dev, delay); |
| 4785 | pm_runtime_use_autosuspend(dev); |
| 4786 | pm_runtime_allow(dev); |
| 4787 | if (!pm_runtime_suspended(dev)) |
| 4788 | pm_runtime_mark_last_busy(dev); |
| 4789 | } else { |
| 4790 | pm_runtime_dont_use_autosuspend(dev); |
| 4791 | pm_runtime_forbid(dev); |
| 4792 | } |
| 4793 | } |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4794 | |
| 4795 | /** |
| 4796 | * snd_hda_set_power_save - reprogram autosuspend for the given delay |
| 4797 | * @bus: HD-audio bus |
| 4798 | * @delay: autosuspend delay in msec, 0 = off |
| 4799 | * |
| 4800 | * Synchronize the runtime PM autosuspend state from the power_save option. |
| 4801 | */ |
| 4802 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) |
| 4803 | { |
| 4804 | struct hda_codec *c; |
| 4805 | |
| 4806 | list_for_each_entry(c, &bus->codec_list, list) |
| 4807 | codec_set_power_save(c, delay); |
| 4808 | } |
| 4809 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4810 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4811 | /** |
| 4812 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4813 | * @codec: HD-audio codec |
| 4814 | * @check: the object containing an AMP list and the status |
| 4815 | * @nid: NID to check / update |
| 4816 | * |
| 4817 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4818 | * check the current AMP status, and update the the power-status according |
| 4819 | * to the mute status. |
| 4820 | * |
| 4821 | * This function is supposed to be set or called from the check_power_status |
| 4822 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4823 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4824 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4825 | struct hda_loopback_check *check, |
| 4826 | hda_nid_t nid) |
| 4827 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4828 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4829 | int ch, v; |
| 4830 | |
| 4831 | if (!check->amplist) |
| 4832 | return 0; |
| 4833 | for (p = check->amplist; p->nid; p++) { |
| 4834 | if (p->nid == nid) |
| 4835 | break; |
| 4836 | } |
| 4837 | if (!p->nid) |
| 4838 | return 0; /* nothing changed */ |
| 4839 | |
| 4840 | for (p = check->amplist; p->nid; p++) { |
| 4841 | for (ch = 0; ch < 2; ch++) { |
| 4842 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4843 | p->idx); |
| 4844 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4845 | if (!check->power_on) { |
| 4846 | check->power_on = 1; |
| 4847 | snd_hda_power_up(codec); |
| 4848 | } |
| 4849 | return 1; |
| 4850 | } |
| 4851 | } |
| 4852 | } |
| 4853 | if (check->power_on) { |
| 4854 | check->power_on = 0; |
| 4855 | snd_hda_power_down(codec); |
| 4856 | } |
| 4857 | return 0; |
| 4858 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4859 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4860 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4862 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | * input MUX helper |
| 4864 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4865 | |
| 4866 | /** |
| 4867 | * 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] | 4868 | * @imux: imux helper object |
| 4869 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4870 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4871 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4872 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | { |
| 4874 | unsigned int index; |
| 4875 | |
| 4876 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4877 | uinfo->count = 1; |
| 4878 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4879 | if (!imux->num_items) |
| 4880 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | index = uinfo->value.enumerated.item; |
| 4882 | if (index >= imux->num_items) |
| 4883 | index = imux->num_items - 1; |
| 4884 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4885 | return 0; |
| 4886 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4887 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4889 | /** |
| 4890 | * 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] | 4891 | * @codec: the HDA codec |
| 4892 | * @imux: imux helper object |
| 4893 | * @ucontrol: pointer to get/store the data |
| 4894 | * @nid: input mux NID |
| 4895 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4896 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4897 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4898 | const struct hda_input_mux *imux, |
| 4899 | struct snd_ctl_elem_value *ucontrol, |
| 4900 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | unsigned int *cur_val) |
| 4902 | { |
| 4903 | unsigned int idx; |
| 4904 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4905 | if (!imux->num_items) |
| 4906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | idx = ucontrol->value.enumerated.item[0]; |
| 4908 | if (idx >= imux->num_items) |
| 4909 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4910 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4912 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4913 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | *cur_val = idx; |
| 4915 | return 1; |
| 4916 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4917 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | |
| 4919 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4920 | /** |
| 4921 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 4922 | * @kcontrol: ctl element |
| 4923 | * @uinfo: pointer to get/store the data |
| 4924 | * @num_items: number of enum items |
| 4925 | * @texts: enum item string array |
| 4926 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4927 | * process kcontrol info callback of a simple string enum array |
| 4928 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 4929 | */ |
| 4930 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 4931 | struct snd_ctl_elem_info *uinfo, |
| 4932 | int num_items, const char * const *texts) |
| 4933 | { |
| 4934 | static const char * const texts_default[] = { |
| 4935 | "Disabled", "Enabled" |
| 4936 | }; |
| 4937 | |
| 4938 | if (!texts || !num_items) { |
| 4939 | num_items = 2; |
| 4940 | texts = texts_default; |
| 4941 | } |
| 4942 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 4943 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4944 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4945 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4946 | |
| 4947 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | * Multi-channel / digital-out PCM helper functions |
| 4949 | */ |
| 4950 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4951 | /* setup SPDIF output stream */ |
| 4952 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 4953 | unsigned int stream_tag, unsigned int format) |
| 4954 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4955 | struct hda_spdif_out *spdif; |
| 4956 | unsigned int curr_fmt; |
| 4957 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4958 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4959 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 4960 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 4961 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 4962 | reset = codec->spdif_status_reset && |
| 4963 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 4964 | curr_fmt != format; |
| 4965 | |
| 4966 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 4967 | updated */ |
| 4968 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4969 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4970 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4971 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4972 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4973 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4974 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4975 | for (d = codec->slave_dig_outs; *d; d++) |
| 4976 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 4977 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4978 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4979 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 4980 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4981 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 4982 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4983 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 4984 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4985 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 4986 | { |
| 4987 | snd_hda_codec_cleanup_stream(codec, nid); |
| 4988 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4989 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 4990 | for (d = codec->slave_dig_outs; *d; d++) |
| 4991 | snd_hda_codec_cleanup_stream(codec, *d); |
| 4992 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4995 | /** |
| 4996 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 4997 | * @bus: HD-audio bus |
| 4998 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 4999 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5000 | { |
| 5001 | struct hda_codec *codec; |
| 5002 | |
| 5003 | if (!bus) |
| 5004 | return; |
| 5005 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5006 | if (hda_codec_is_power_on(codec) && |
| 5007 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5008 | codec->patch_ops.reboot_notify(codec); |
| 5009 | } |
| 5010 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5011 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5012 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5013 | /** |
| 5014 | * 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] | 5015 | * @codec: the HDA codec |
| 5016 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5018 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5019 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5021 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5022 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5023 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5024 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5026 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | return 0; |
| 5028 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5029 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5031 | /** |
| 5032 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5033 | * @codec: the HDA codec |
| 5034 | * @mout: hda_multi_out object |
| 5035 | * @stream_tag: stream tag to assign |
| 5036 | * @format: format id to assign |
| 5037 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5038 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5039 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5040 | struct hda_multi_out *mout, |
| 5041 | unsigned int stream_tag, |
| 5042 | unsigned int format, |
| 5043 | struct snd_pcm_substream *substream) |
| 5044 | { |
| 5045 | mutex_lock(&codec->spdif_mutex); |
| 5046 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5047 | mutex_unlock(&codec->spdif_mutex); |
| 5048 | return 0; |
| 5049 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5050 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5051 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5052 | /** |
| 5053 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5054 | * @codec: the HDA codec |
| 5055 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5056 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5057 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5058 | struct hda_multi_out *mout) |
| 5059 | { |
| 5060 | mutex_lock(&codec->spdif_mutex); |
| 5061 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5062 | mutex_unlock(&codec->spdif_mutex); |
| 5063 | return 0; |
| 5064 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5065 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5066 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5067 | /** |
| 5068 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5069 | * @codec: the HDA codec |
| 5070 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5072 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5073 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5075 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5077 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | return 0; |
| 5079 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5080 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5082 | /** |
| 5083 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5084 | * @codec: the HDA codec |
| 5085 | * @mout: hda_multi_out object |
| 5086 | * @substream: PCM substream to assign |
| 5087 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5088 | * |
| 5089 | * Open analog outputs and set up the hw-constraints. |
| 5090 | * If the digital outputs can be opened as slave, open the digital |
| 5091 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5093 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5094 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5095 | struct snd_pcm_substream *substream, |
| 5096 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5098 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5099 | runtime->hw.channels_max = mout->max_channels; |
| 5100 | if (mout->dig_out_nid) { |
| 5101 | if (!mout->analog_rates) { |
| 5102 | mout->analog_rates = hinfo->rates; |
| 5103 | mout->analog_formats = hinfo->formats; |
| 5104 | mout->analog_maxbps = hinfo->maxbps; |
| 5105 | } else { |
| 5106 | runtime->hw.rates = mout->analog_rates; |
| 5107 | runtime->hw.formats = mout->analog_formats; |
| 5108 | hinfo->maxbps = mout->analog_maxbps; |
| 5109 | } |
| 5110 | if (!mout->spdif_rates) { |
| 5111 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5112 | &mout->spdif_rates, |
| 5113 | &mout->spdif_formats, |
| 5114 | &mout->spdif_maxbps); |
| 5115 | } |
| 5116 | mutex_lock(&codec->spdif_mutex); |
| 5117 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5118 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5119 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5120 | runtime->hw.rates &= mout->spdif_rates; |
| 5121 | runtime->hw.formats &= mout->spdif_formats; |
| 5122 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5123 | hinfo->maxbps = mout->spdif_maxbps; |
| 5124 | } else { |
| 5125 | mout->share_spdif = 0; |
| 5126 | /* FIXME: need notify? */ |
| 5127 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5128 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5129 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5132 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5133 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5134 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5136 | /** |
| 5137 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5138 | * @codec: the HDA codec |
| 5139 | * @mout: hda_multi_out object |
| 5140 | * @stream_tag: stream tag to assign |
| 5141 | * @format: format id to assign |
| 5142 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5143 | * |
| 5144 | * Set up the i/o for analog out. |
| 5145 | * 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] | 5146 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5147 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5148 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | unsigned int stream_tag, |
| 5150 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5151 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5153 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5155 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | int i; |
| 5157 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5158 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5159 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5160 | if (mout->dig_out_nid && mout->share_spdif && |
| 5161 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5163 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5164 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5165 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5167 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5168 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | } else { |
| 5170 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5171 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | } |
| 5173 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5174 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | |
| 5176 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5177 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5178 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5179 | if (!mout->no_share_stream && |
| 5180 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5182 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5183 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5184 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5185 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5186 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5187 | snd_hda_codec_setup_stream(codec, |
| 5188 | mout->hp_out_nid[i], |
| 5189 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | /* surrounds */ |
| 5192 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5193 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5194 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5195 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5196 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5197 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5198 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5200 | |
| 5201 | /* extra surrounds */ |
| 5202 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5203 | int ch = 0; |
| 5204 | if (!mout->extra_out_nid[i]) |
| 5205 | break; |
| 5206 | if (chs >= (i + 1) * 2) |
| 5207 | ch = i * 2; |
| 5208 | else if (!mout->no_share_stream) |
| 5209 | break; |
| 5210 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5211 | stream_tag, ch, format); |
| 5212 | } |
| 5213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | return 0; |
| 5215 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5216 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5218 | /** |
| 5219 | * 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] | 5220 | * @codec: the HDA codec |
| 5221 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5223 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5224 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5226 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | int i; |
| 5228 | |
| 5229 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5230 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5232 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5233 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5234 | if (mout->hp_out_nid[i]) |
| 5235 | snd_hda_codec_cleanup_stream(codec, |
| 5236 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5237 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5238 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5239 | snd_hda_codec_cleanup_stream(codec, |
| 5240 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5241 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | 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] | 5243 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | mout->dig_out_used = 0; |
| 5245 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5246 | mutex_unlock(&codec->spdif_mutex); |
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_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5251 | /** |
| 5252 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5253 | * @codec: the HDA codec |
| 5254 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5255 | * |
| 5256 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5257 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5258 | */ |
| 5259 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5260 | { |
| 5261 | unsigned int pincap; |
| 5262 | unsigned int oldval; |
| 5263 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5264 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5265 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5266 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5267 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5268 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5269 | return AC_PINCTL_VREF_80; |
| 5270 | else if (pincap & AC_PINCAP_VREF_50) |
| 5271 | return AC_PINCTL_VREF_50; |
| 5272 | else if (pincap & AC_PINCAP_VREF_100) |
| 5273 | return AC_PINCTL_VREF_100; |
| 5274 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5275 | return AC_PINCTL_VREF_GRD; |
| 5276 | return AC_PINCTL_VREF_HIZ; |
| 5277 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5278 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5279 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5280 | /** |
| 5281 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5282 | * @codec: the HDA codec |
| 5283 | * @pin: referred pin NID |
| 5284 | * @val: pin ctl value to audit |
| 5285 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5286 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5287 | hda_nid_t pin, unsigned int val) |
| 5288 | { |
| 5289 | static unsigned int cap_lists[][2] = { |
| 5290 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5291 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5292 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5293 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5294 | }; |
| 5295 | unsigned int cap; |
| 5296 | |
| 5297 | if (!val) |
| 5298 | return 0; |
| 5299 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5300 | if (!cap) |
| 5301 | return val; /* don't know what to do... */ |
| 5302 | |
| 5303 | if (val & AC_PINCTL_OUT_EN) { |
| 5304 | if (!(cap & AC_PINCAP_OUT)) |
| 5305 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5306 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5307 | val &= ~AC_PINCTL_HP_EN; |
| 5308 | } |
| 5309 | |
| 5310 | if (val & AC_PINCTL_IN_EN) { |
| 5311 | if (!(cap & AC_PINCAP_IN)) |
| 5312 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5313 | else { |
| 5314 | unsigned int vcap, vref; |
| 5315 | int i; |
| 5316 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5317 | vref = val & AC_PINCTL_VREFEN; |
| 5318 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5319 | if (vref == cap_lists[i][0] && |
| 5320 | !(vcap & cap_lists[i][1])) { |
| 5321 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5322 | vref = AC_PINCTL_VREF_HIZ; |
| 5323 | else |
| 5324 | vref = cap_lists[i + 1][0]; |
| 5325 | } |
| 5326 | } |
| 5327 | val &= ~AC_PINCTL_VREFEN; |
| 5328 | val |= vref; |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | return val; |
| 5333 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5334 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5335 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5336 | /** |
| 5337 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5338 | * @codec: the HDA codec |
| 5339 | * @pin: referred pin NID |
| 5340 | * @val: pin control value to set |
| 5341 | * @cached: access over codec pinctl cache or direct write |
| 5342 | * |
| 5343 | * This function is a helper to set a pin ctl value more safely. |
| 5344 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5345 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5346 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5347 | * snd_hda_codec_write() depending on @cached flag. |
| 5348 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5349 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5350 | unsigned int val, bool cached) |
| 5351 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5352 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5353 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5354 | if (cached) |
| 5355 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5356 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5357 | else |
| 5358 | return snd_hda_codec_write(codec, pin, 0, |
| 5359 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5360 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5361 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5362 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5363 | /** |
| 5364 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5365 | * @codec: the HDA codec |
| 5366 | * @imux: imux helper object |
| 5367 | * @label: the name of imux item to assign |
| 5368 | * @index: index number of imux item to assign |
| 5369 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5370 | * |
| 5371 | * When the same label is used already in the existing items, the number |
| 5372 | * suffix is appended to the label. This label index number is stored |
| 5373 | * to type_idx when non-NULL pointer is given. |
| 5374 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5375 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5376 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5377 | int index, int *type_idx) |
| 5378 | { |
| 5379 | int i, label_idx = 0; |
| 5380 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5381 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5382 | return -EINVAL; |
| 5383 | } |
| 5384 | for (i = 0; i < imux->num_items; i++) { |
| 5385 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5386 | label_idx++; |
| 5387 | } |
| 5388 | if (type_idx) |
| 5389 | *type_idx = label_idx; |
| 5390 | if (label_idx > 0) |
| 5391 | snprintf(imux->items[imux->num_items].label, |
| 5392 | sizeof(imux->items[imux->num_items].label), |
| 5393 | "%s %d", label, label_idx); |
| 5394 | else |
| 5395 | strlcpy(imux->items[imux->num_items].label, label, |
| 5396 | sizeof(imux->items[imux->num_items].label)); |
| 5397 | imux->items[imux->num_items].index = index; |
| 5398 | imux->num_items++; |
| 5399 | return 0; |
| 5400 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5401 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | /** |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5404 | * snd_hda_bus_reset - Reset the bus |
| 5405 | * @bus: HD-audio bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5407 | void snd_hda_bus_reset(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5409 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5411 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5412 | /* FIXME: maybe a better way needed for forced reset */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5413 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5414 | #ifdef CONFIG_PM |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5415 | if (hda_codec_is_power_on(codec)) { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 5416 | hda_call_codec_suspend(codec); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5417 | hda_call_codec_resume(codec); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5418 | } |
| 5419 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5422 | EXPORT_SYMBOL_GPL(snd_hda_bus_reset); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5423 | |
| 5424 | /* |
| 5425 | * generic arrays |
| 5426 | */ |
| 5427 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5428 | /** |
| 5429 | * snd_array_new - get a new element from the given array |
| 5430 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5431 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5432 | * Get a new element from the given array. If it exceeds the |
| 5433 | * pre-allocated array size, re-allocate the array. |
| 5434 | * |
| 5435 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5436 | */ |
| 5437 | void *snd_array_new(struct snd_array *array) |
| 5438 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5439 | if (snd_BUG_ON(!array->elem_size)) |
| 5440 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5441 | if (array->used >= array->alloced) { |
| 5442 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5443 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5444 | void *nlist; |
| 5445 | if (snd_BUG_ON(num >= 4096)) |
| 5446 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5447 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5448 | if (!nlist) |
| 5449 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5450 | array->list = nlist; |
| 5451 | array->alloced = num; |
| 5452 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5453 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5454 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5455 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5456 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5457 | /** |
| 5458 | * snd_array_free - free the given array elements |
| 5459 | * @array: the array object |
| 5460 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5461 | void snd_array_free(struct snd_array *array) |
| 5462 | { |
| 5463 | kfree(array->list); |
| 5464 | array->used = 0; |
| 5465 | array->alloced = 0; |
| 5466 | array->list = NULL; |
| 5467 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5468 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5469 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5470 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5471 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5472 | * @pcm: PCM caps bits |
| 5473 | * @buf: the string buffer to write |
| 5474 | * @buflen: the max buffer length |
| 5475 | * |
| 5476 | * used by hda_proc.c and hda_eld.c |
| 5477 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5478 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5479 | { |
| 5480 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5481 | int i, j; |
| 5482 | |
| 5483 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5484 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5485 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5486 | |
| 5487 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5488 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5489 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5490 | |
| 5491 | MODULE_DESCRIPTION("HDA codec core"); |
| 5492 | MODULE_LICENSE("GPL"); |