Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Interface for Intel High Definition Audio Codec |
| 3 | * |
| 4 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * |
| 7 | * This driver is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This driver is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/slab.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Takashi Iwai | f4d6a55 | 2013-12-05 11:55:05 +0100 | [diff] [blame] | 28 | #include <linux/async.h> |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 29 | #include <linux/pm.h> |
| 30 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/core.h> |
| 32 | #include "hda_codec.h" |
| 33 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 34 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <sound/initval.h> |
Takashi Iwai | cd372fb | 2011-03-03 14:40:14 +0100 | [diff] [blame] | 36 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "hda_local.h" |
Jaroslav Kysela | 123c07a | 2009-10-21 14:48:23 +0200 | [diff] [blame] | 38 | #include "hda_beep.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 39 | #include "hda_jack.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 40 | #include <sound/hda_hwdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 42 | #define CREATE_TRACE_POINTS |
| 43 | #include "hda_trace.h" |
| 44 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 46 | #define codec_in_pm(codec) atomic_read(&(codec)->in_pm) |
| 47 | #define hda_codec_is_power_on(codec) \ |
| 48 | (!pm_runtime_suspended(hda_codec_dev(codec))) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 49 | #else |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 50 | #define codec_in_pm(codec) 0 |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 51 | #define hda_codec_is_power_on(codec) 1 |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 54 | /** |
| 55 | * snd_hda_get_jack_location - Give a location string of the jack |
| 56 | * @cfg: pin default config value |
| 57 | * |
| 58 | * Parse the pin default config value and returns the string of the |
| 59 | * jack location, e.g. "Rear", "Front", etc. |
| 60 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 61 | const char *snd_hda_get_jack_location(u32 cfg) |
| 62 | { |
| 63 | static char *bases[7] = { |
| 64 | "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", |
| 65 | }; |
| 66 | static unsigned char specials_idx[] = { |
| 67 | 0x07, 0x08, |
| 68 | 0x17, 0x18, 0x19, |
| 69 | 0x37, 0x38 |
| 70 | }; |
| 71 | static char *specials[] = { |
| 72 | "Rear Panel", "Drive Bar", |
| 73 | "Riser", "HDMI", "ATAPI", |
| 74 | "Mobile-In", "Mobile-Out" |
| 75 | }; |
| 76 | int i; |
| 77 | cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; |
| 78 | if ((cfg & 0x0f) < 7) |
| 79 | return bases[cfg & 0x0f]; |
| 80 | for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { |
| 81 | if (cfg == specials_idx[i]) |
| 82 | return specials[i]; |
| 83 | } |
| 84 | return "UNKNOWN"; |
| 85 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 87 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 88 | /** |
| 89 | * snd_hda_get_jack_connectivity - Give a connectivity string of the jack |
| 90 | * @cfg: pin default config value |
| 91 | * |
| 92 | * Parse the pin default config value and returns the string of the |
| 93 | * jack connectivity, i.e. external or internal connection. |
| 94 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 95 | const char *snd_hda_get_jack_connectivity(u32 cfg) |
| 96 | { |
| 97 | static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; |
| 98 | |
| 99 | return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; |
| 100 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 102 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 103 | /** |
| 104 | * snd_hda_get_jack_type - Give a type string of the jack |
| 105 | * @cfg: pin default config value |
| 106 | * |
| 107 | * Parse the pin default config value and returns the string of the |
| 108 | * jack type, i.e. the purpose of the jack, such as Line-Out or CD. |
| 109 | */ |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 110 | const char *snd_hda_get_jack_type(u32 cfg) |
| 111 | { |
| 112 | static char *jack_types[16] = { |
| 113 | "Line Out", "Speaker", "HP Out", "CD", |
| 114 | "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", |
| 115 | "Line In", "Aux", "Mic", "Telephony", |
David Henningsson | aeb3a97 | 2013-04-04 11:47:13 +0200 | [diff] [blame] | 116 | "SPDIF In", "Digital In", "Reserved", "Other" |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | return jack_types[(cfg & AC_DEFCFG_DEVICE) |
| 120 | >> AC_DEFCFG_DEVICE_SHIFT]; |
| 121 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); |
Matthew Ranostay | 50a9f79 | 2008-10-25 01:05:45 -0400 | [diff] [blame] | 123 | |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 124 | /* |
| 125 | * Compose a 32bit command word to be sent to the HD-audio controller |
| 126 | */ |
| 127 | static inline unsigned int |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 128 | make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags, |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 129 | unsigned int verb, unsigned int parm) |
| 130 | { |
| 131 | u32 val; |
| 132 | |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 133 | if ((codec->addr & ~0xf) || (nid & ~0x7f) || |
Takashi Iwai | 82e1b80 | 2009-07-17 12:47:34 +0200 | [diff] [blame] | 134 | (verb & ~0xfff) || (parm & ~0xffff)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 135 | codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n", |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 136 | codec->addr, nid, verb, parm); |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 137 | return ~0; |
| 138 | } |
| 139 | |
| 140 | val = (u32)codec->addr << 28; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 141 | val |= (u32)nid << 20; |
| 142 | val |= verb << 8; |
| 143 | val |= parm; |
| 144 | return val; |
| 145 | } |
| 146 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 147 | /* |
| 148 | * Send and receive a verb |
| 149 | */ |
| 150 | static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 151 | int flags, unsigned int *res) |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 152 | { |
| 153 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 154 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 155 | |
Wu Fengguang | 6430aee | 2009-07-17 16:49:19 +0800 | [diff] [blame] | 156 | if (cmd == ~0) |
| 157 | return -1; |
| 158 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 159 | if (res) |
| 160 | *res = -1; |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 161 | again: |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 162 | snd_hda_power_up(codec); |
| 163 | mutex_lock(&bus->cmd_mutex); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 164 | if (flags & HDA_RW_NO_RESPONSE_FALLBACK) |
| 165 | bus->no_response_fallback = 1; |
Takashi Iwai | 3bcce5c | 2012-12-20 11:17:17 +0100 | [diff] [blame] | 166 | for (;;) { |
| 167 | trace_hda_send_cmd(codec, cmd); |
| 168 | err = bus->ops.command(bus, cmd); |
| 169 | if (err != -EAGAIN) |
| 170 | break; |
| 171 | /* process pending verbs */ |
| 172 | bus->ops.get_response(bus, codec->addr); |
| 173 | } |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 174 | if (!err && res) { |
Wu Fengguang | deadff1 | 2009-08-01 18:45:16 +0800 | [diff] [blame] | 175 | *res = bus->ops.get_response(bus, codec->addr); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 176 | trace_hda_get_response(codec, *res); |
| 177 | } |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 178 | bus->no_response_fallback = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 179 | mutex_unlock(&bus->cmd_mutex); |
| 180 | snd_hda_power_down(codec); |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 181 | if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 182 | if (bus->response_reset) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 183 | codec_dbg(codec, |
| 184 | "resetting BUS due to fatal communication error\n"); |
Takashi Iwai | d66fee5 | 2011-08-02 15:39:31 +0200 | [diff] [blame] | 185 | trace_hda_bus_reset(bus); |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 186 | bus->ops.bus_reset(bus); |
| 187 | } |
| 188 | goto again; |
| 189 | } |
| 190 | /* clear reset-flag when the communication gets recovered */ |
Takashi Iwai | d846b17 | 2012-11-24 11:58:24 +0100 | [diff] [blame] | 191 | if (!err || codec_in_pm(codec)) |
Takashi Iwai | 8dd7833 | 2009-06-02 01:16:07 +0200 | [diff] [blame] | 192 | bus->response_reset = 0; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 193 | return err; |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /** |
| 197 | * snd_hda_codec_read - send a command and get the response |
| 198 | * @codec: the HDA codec |
| 199 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 200 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * @verb: the verb to send |
| 202 | * @parm: the parameter for the verb |
| 203 | * |
| 204 | * Send a single command and read the corresponding response. |
| 205 | * |
| 206 | * Returns the obtained response value, or -1 for an error. |
| 207 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 208 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 209 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned int verb, unsigned int parm) |
| 211 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 212 | unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 213 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 214 | if (codec_exec_verb(codec, cmd, flags, &res)) |
Greg Thelen | 9857edf | 2011-06-13 07:45:45 -0700 | [diff] [blame] | 215 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return res; |
| 217 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(snd_hda_codec_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * snd_hda_codec_write - send a single command without waiting for response |
| 222 | * @codec: the HDA codec |
| 223 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 224 | * @flags: optional bit flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * @verb: the verb to send |
| 226 | * @parm: the parameter for the verb |
| 227 | * |
| 228 | * Send a single command without waiting for response. |
| 229 | * |
| 230 | * Returns 0 if successful, or a negative error code. |
| 231 | */ |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 232 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, |
| 233 | unsigned int verb, unsigned int parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 235 | unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 236 | unsigned int res; |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 237 | return codec_exec_verb(codec, cmd, flags, |
Takashi Iwai | b20f3b8 | 2009-06-02 01:20:22 +0200 | [diff] [blame] | 238 | codec->bus->sync_write ? &res : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(snd_hda_codec_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * snd_hda_sequence_write - sequence writes |
| 244 | * @codec: the HDA codec |
| 245 | * @seq: VERB array to send |
| 246 | * |
| 247 | * Send the commands sequentially from the given array. |
| 248 | * The array must be terminated with NID=0. |
| 249 | */ |
| 250 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 251 | { |
| 252 | for (; seq->nid; seq++) |
| 253 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 254 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 259 | * @codec: the HDA codec |
| 260 | * @nid: NID to parse |
| 261 | * @start_id: the pointer to store the start NID |
| 262 | * |
| 263 | * Parse the NID and store the start NID of its sub-nodes. |
| 264 | * Returns the number of sub-nodes. |
| 265 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 266 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 267 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | unsigned int parm; |
| 270 | |
| 271 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 272 | if (parm == -1) { |
| 273 | *start_id = 0; |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 274 | return 0; |
Dan Carpenter | 69eba10 | 2014-11-27 01:34:43 +0300 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | *start_id = (parm >> 16) & 0x7fff; |
| 277 | return (int)(parm & 0x7fff); |
| 278 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 281 | /* connection list element */ |
| 282 | struct hda_conn_list { |
| 283 | struct list_head list; |
| 284 | int len; |
| 285 | hda_nid_t nid; |
| 286 | hda_nid_t conns[0]; |
| 287 | }; |
| 288 | |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 289 | /* look up the cached results */ |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 290 | static struct hda_conn_list * |
| 291 | lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 292 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 293 | struct hda_conn_list *p; |
| 294 | list_for_each_entry(p, &codec->conn_list, list) { |
| 295 | if (p->nid == nid) |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 296 | return p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 297 | } |
| 298 | return NULL; |
| 299 | } |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 300 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 301 | static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 302 | const hda_nid_t *list) |
| 303 | { |
| 304 | struct hda_conn_list *p; |
| 305 | |
| 306 | p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); |
| 307 | if (!p) |
| 308 | return -ENOMEM; |
| 309 | p->len = len; |
| 310 | p->nid = nid; |
| 311 | memcpy(p->conns, list, len * sizeof(hda_nid_t)); |
| 312 | list_add(&p->list, &codec->conn_list); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void remove_conn_list(struct hda_codec *codec) |
| 317 | { |
| 318 | while (!list_empty(&codec->conn_list)) { |
| 319 | struct hda_conn_list *p; |
| 320 | p = list_first_entry(&codec->conn_list, typeof(*p), list); |
| 321 | list_del(&p->list); |
| 322 | kfree(p); |
| 323 | } |
| 324 | } |
| 325 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 326 | /* read the connection and add to the cache */ |
| 327 | static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 328 | { |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 329 | hda_nid_t list[32]; |
| 330 | hda_nid_t *result = list; |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 331 | int len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 332 | |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 333 | len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 334 | if (len == -ENOSPC) { |
| 335 | len = snd_hda_get_num_raw_conns(codec, nid); |
| 336 | result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); |
| 337 | if (!result) |
| 338 | return -ENOMEM; |
| 339 | len = snd_hda_get_raw_connections(codec, nid, result, len); |
| 340 | } |
| 341 | if (len >= 0) |
| 342 | len = snd_hda_override_conn_list(codec, nid, len, result); |
| 343 | if (result != list) |
| 344 | kfree(result); |
| 345 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 346 | } |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 347 | |
| 348 | /** |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 349 | * snd_hda_get_conn_list - get connection list |
| 350 | * @codec: the HDA codec |
| 351 | * @nid: NID to parse |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 352 | * @listp: the pointer to store NID list |
| 353 | * |
| 354 | * Parses the connection list of the given widget and stores the pointer |
| 355 | * to the list of NIDs. |
| 356 | * |
| 357 | * Returns the number of connections, or a negative error code. |
| 358 | * |
| 359 | * Note that the returned pointer isn't protected against the list |
| 360 | * modification. If snd_hda_override_conn_list() might be called |
| 361 | * concurrently, protect with a mutex appropriately. |
| 362 | */ |
| 363 | int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, |
| 364 | const hda_nid_t **listp) |
| 365 | { |
| 366 | bool added = false; |
| 367 | |
| 368 | for (;;) { |
| 369 | int err; |
| 370 | const struct hda_conn_list *p; |
| 371 | |
| 372 | /* if the connection-list is already cached, read it */ |
| 373 | p = lookup_conn_list(codec, nid); |
| 374 | if (p) { |
| 375 | if (listp) |
| 376 | *listp = p->conns; |
| 377 | return p->len; |
| 378 | } |
| 379 | if (snd_BUG_ON(added)) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | err = read_and_add_raw_conns(codec, nid); |
| 383 | if (err < 0) |
| 384 | return err; |
| 385 | added = true; |
| 386 | } |
| 387 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 388 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 389 | |
| 390 | /** |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 391 | * snd_hda_get_connections - copy connection list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | * @codec: the HDA codec |
| 393 | * @nid: NID to parse |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 394 | * @conn_list: connection list array; when NULL, checks only the size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * @max_conns: max. number of connections to store |
| 396 | * |
| 397 | * Parses the connection list of the given widget and stores the list |
| 398 | * of NIDs. |
| 399 | * |
| 400 | * Returns the number of connections, or a negative error code. |
| 401 | */ |
| 402 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 403 | hda_nid_t *conn_list, int max_conns) |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 404 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 405 | const hda_nid_t *list; |
| 406 | int len = snd_hda_get_conn_list(codec, nid, &list); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 407 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 408 | if (len > 0 && conn_list) { |
| 409 | if (len > max_conns) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 410 | codec_err(codec, "Too many connections %d for NID 0x%x\n", |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 411 | len, nid); |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 412 | return -EINVAL; |
| 413 | } |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 414 | memcpy(conn_list, list, len * sizeof(hda_nid_t)); |
Takashi Iwai | dce2079 | 2011-06-24 14:10:28 +0200 | [diff] [blame] | 415 | } |
Takashi Iwai | 09cf03b | 2012-05-19 17:21:25 +0200 | [diff] [blame] | 416 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 417 | return len; |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 418 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 419 | EXPORT_SYMBOL_GPL(snd_hda_get_connections); |
Takashi Iwai | a12d3e1 | 2011-04-07 15:55:15 +0200 | [diff] [blame] | 420 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 421 | /* return CONNLIST_LEN parameter of the given widget */ |
| 422 | static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) |
| 423 | { |
| 424 | unsigned int wcaps = get_wcaps(codec, nid); |
| 425 | unsigned int parm; |
| 426 | |
| 427 | if (!(wcaps & AC_WCAP_CONN_LIST) && |
| 428 | get_wcaps_type(wcaps) != AC_WID_VOL_KNB) |
| 429 | return 0; |
| 430 | |
| 431 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 432 | if (parm == -1) |
| 433 | parm = 0; |
| 434 | return parm; |
| 435 | } |
| 436 | |
| 437 | int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) |
| 438 | { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 439 | return snd_hda_get_raw_connections(codec, nid, NULL, 0); |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Takashi Iwai | 9e7717c | 2011-07-11 15:42:52 +0200 | [diff] [blame] | 442 | /** |
| 443 | * snd_hda_get_raw_connections - copy connection list without cache |
| 444 | * @codec: the HDA codec |
| 445 | * @nid: NID to parse |
| 446 | * @conn_list: connection list array |
| 447 | * @max_conns: max. number of connections to store |
| 448 | * |
| 449 | * Like snd_hda_get_connections(), copy the connection list but without |
| 450 | * checking through the connection-list cache. |
| 451 | * Currently called only from hda_proc.c, so not exported. |
| 452 | */ |
| 453 | int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, |
| 454 | hda_nid_t *conn_list, int max_conns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 457 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 459 | hda_nid_t prev_nid; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 460 | int null_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Takashi Iwai | 4eea309 | 2013-02-07 18:18:19 +0100 | [diff] [blame] | 462 | parm = get_num_conns(codec, nid); |
| 463 | if (!parm) |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 464 | return 0; |
Jaroslav Kysela | 16a433d | 2009-07-22 16:20:40 +0200 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (parm & AC_CLIST_LONG) { |
| 467 | /* long form */ |
| 468 | shift = 16; |
| 469 | num_elems = 2; |
| 470 | } else { |
| 471 | /* short form */ |
| 472 | shift = 8; |
| 473 | num_elems = 4; |
| 474 | } |
| 475 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | mask = (1 << (shift-1)) - 1; |
| 477 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 478 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return 0; /* no connection */ |
| 480 | |
| 481 | if (conn_len == 1) { |
| 482 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 483 | parm = snd_hda_codec_read(codec, nid, 0, |
| 484 | AC_VERB_GET_CONNECT_LIST, 0); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 485 | if (parm == -1 && codec->bus->rirb_error) |
| 486 | return -EIO; |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 487 | if (conn_list) |
| 488 | conn_list[0] = parm & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | /* multi connection */ |
| 493 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 494 | prev_nid = 0; |
| 495 | for (i = 0; i < conn_len; i++) { |
| 496 | int range_val; |
| 497 | hda_nid_t val, n; |
| 498 | |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 499 | if (i % num_elems == 0) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 500 | parm = snd_hda_codec_read(codec, nid, 0, |
| 501 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 3c6aae4 | 2009-07-10 12:52:27 +0200 | [diff] [blame] | 502 | if (parm == -1 && codec->bus->rirb_error) |
| 503 | return -EIO; |
| 504 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 505 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 506 | val = parm & mask; |
Takashi Iwai | 5fdaecd | 2012-12-20 10:45:55 +0100 | [diff] [blame] | 507 | if (val == 0 && null_count++) { /* no second chance */ |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 508 | codec_dbg(codec, |
| 509 | "invalid CONNECT_LIST verb %x[%i]:%x\n", |
Jaroslav Kysela | 2e9bf24 | 2009-07-18 11:48:19 +0200 | [diff] [blame] | 510 | nid, i, parm); |
| 511 | return 0; |
| 512 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 513 | parm >>= shift; |
| 514 | if (range_val) { |
| 515 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 516 | if (!prev_nid || prev_nid >= val) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 517 | codec_warn(codec, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 518 | "invalid dep_range_val %x:%x\n", |
| 519 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 520 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 522 | for (n = prev_nid + 1; n <= val; n++) { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 523 | if (conn_list) { |
| 524 | if (conns >= max_conns) |
| 525 | return -ENOSPC; |
| 526 | conn_list[conns] = n; |
| 527 | } |
| 528 | conns++; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 529 | } |
| 530 | } else { |
Takashi Iwai | b5f82b1 | 2013-03-12 16:47:30 +0100 | [diff] [blame] | 531 | if (conn_list) { |
| 532 | if (conns >= max_conns) |
| 533 | return -ENOSPC; |
| 534 | conn_list[conns] = val; |
| 535 | } |
| 536 | conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 538 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | return conns; |
| 541 | } |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /** |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 544 | * snd_hda_override_conn_list - add/modify the connection-list to cache |
| 545 | * @codec: the HDA codec |
| 546 | * @nid: NID to parse |
| 547 | * @len: number of connection list entries |
| 548 | * @list: the list of connection entries |
| 549 | * |
| 550 | * Add or modify the given connection-list to the cache. If the corresponding |
| 551 | * cache already exists, invalidate it and append a new one. |
| 552 | * |
| 553 | * Returns zero or a negative error code. |
| 554 | */ |
| 555 | int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, |
| 556 | const hda_nid_t *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 558 | struct hda_conn_list *p; |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 559 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 560 | p = lookup_conn_list(codec, nid); |
| 561 | if (p) { |
| 562 | list_del(&p->list); |
| 563 | kfree(p); |
| 564 | } |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 565 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 566 | return add_conn_list(codec, nid, len, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); |
Takashi Iwai | b2f934a | 2011-07-04 16:23:26 +0200 | [diff] [blame] | 569 | |
| 570 | /** |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 571 | * snd_hda_get_conn_index - get the connection index of the given NID |
| 572 | * @codec: the HDA codec |
| 573 | * @mux: NID containing the list |
| 574 | * @nid: NID to select |
| 575 | * @recursive: 1 when searching NID recursively, otherwise 0 |
| 576 | * |
| 577 | * Parses the connection list of the widget @mux and checks whether the |
| 578 | * widget @nid is present. If it is, return the connection index. |
| 579 | * Otherwise it returns -1. |
| 580 | */ |
| 581 | int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, |
| 582 | hda_nid_t nid, int recursive) |
| 583 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 584 | const hda_nid_t *conn; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 585 | int i, nums; |
| 586 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 587 | nums = snd_hda_get_conn_list(codec, mux, &conn); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 588 | for (i = 0; i < nums; i++) |
| 589 | if (conn[i] == nid) |
| 590 | return i; |
| 591 | if (!recursive) |
| 592 | return -1; |
Takashi Iwai | d94ddd8 | 2012-12-20 14:42:42 +0100 | [diff] [blame] | 593 | if (recursive > 10) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 594 | codec_dbg(codec, "too deep connection for 0x%x\n", nid); |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | recursive++; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 598 | for (i = 0; i < nums; i++) { |
| 599 | unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 600 | if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) |
| 601 | continue; |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 602 | if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) |
| 603 | return i; |
Takashi Iwai | 99e14c9 | 2011-09-13 10:33:16 +0200 | [diff] [blame] | 604 | } |
Takashi Iwai | 8d087c7 | 2011-06-28 12:45:47 +0200 | [diff] [blame] | 605 | return -1; |
| 606 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Mengdong Lin | f1aa068 | 2013-08-26 21:35:21 -0400 | [diff] [blame] | 609 | |
| 610 | /* return DEVLIST_LEN parameter of the given widget */ |
| 611 | static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) |
| 612 | { |
| 613 | unsigned int wcaps = get_wcaps(codec, nid); |
| 614 | unsigned int parm; |
| 615 | |
| 616 | if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || |
| 617 | get_wcaps_type(wcaps) != AC_WID_PIN) |
| 618 | return 0; |
| 619 | |
| 620 | parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); |
| 621 | if (parm == -1 && codec->bus->rirb_error) |
| 622 | parm = 0; |
| 623 | return parm & AC_DEV_LIST_LEN_MASK; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * snd_hda_get_devices - copy device list without cache |
| 628 | * @codec: the HDA codec |
| 629 | * @nid: NID of the pin to parse |
| 630 | * @dev_list: device list array |
| 631 | * @max_devices: max. number of devices to store |
| 632 | * |
| 633 | * Copy the device list. This info is dynamic and so not cached. |
| 634 | * Currently called only from hda_proc.c, so not exported. |
| 635 | */ |
| 636 | int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, |
| 637 | u8 *dev_list, int max_devices) |
| 638 | { |
| 639 | unsigned int parm; |
| 640 | int i, dev_len, devices; |
| 641 | |
| 642 | parm = get_num_devices(codec, nid); |
| 643 | if (!parm) /* not multi-stream capable */ |
| 644 | return 0; |
| 645 | |
| 646 | dev_len = parm + 1; |
| 647 | dev_len = dev_len < max_devices ? dev_len : max_devices; |
| 648 | |
| 649 | devices = 0; |
| 650 | while (devices < dev_len) { |
| 651 | parm = snd_hda_codec_read(codec, nid, 0, |
| 652 | AC_VERB_GET_DEVICE_LIST, devices); |
| 653 | if (parm == -1 && codec->bus->rirb_error) |
| 654 | break; |
| 655 | |
| 656 | for (i = 0; i < 8; i++) { |
| 657 | dev_list[devices] = (u8)parm; |
| 658 | parm >>= 4; |
| 659 | devices++; |
| 660 | if (devices >= dev_len) |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | return devices; |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | /** |
| 668 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 669 | * @bus: the BUS |
| 670 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 671 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 672 | * |
| 673 | * Adds the given event to the queue. The events are processed in |
| 674 | * the workqueue asynchronously. Call this function in the interrupt |
| 675 | * hanlder when RIRB receives an unsolicited event. |
| 676 | * |
| 677 | * Returns 0 if successful, or a negative error code. |
| 678 | */ |
| 679 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 680 | { |
| 681 | struct hda_bus_unsolicited *unsol; |
| 682 | unsigned int wp; |
| 683 | |
Wang YanQing | 2195b06 | 2013-05-07 11:27:33 +0800 | [diff] [blame] | 684 | if (!bus || !bus->workq) |
| 685 | return 0; |
| 686 | |
Takashi Iwai | ecf726f | 2011-08-09 14:22:44 +0200 | [diff] [blame] | 687 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 688 | unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 690 | unsol->wp = wp; |
| 691 | |
| 692 | wp <<= 1; |
| 693 | unsol->queue[wp] = res; |
| 694 | unsol->queue[wp + 1] = res_ex; |
| 695 | |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 696 | queue_work(bus->workq, &unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | return 0; |
| 699 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 700 | EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 703 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 705 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 707 | struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work); |
| 708 | struct hda_bus_unsolicited *unsol = &bus->unsol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | struct hda_codec *codec; |
| 710 | unsigned int rp, caddr, res; |
| 711 | |
| 712 | while (unsol->rp != unsol->wp) { |
| 713 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 714 | unsol->rp = rp; |
| 715 | rp <<= 1; |
| 716 | res = unsol->queue[rp]; |
| 717 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 718 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | continue; |
| 720 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 721 | if (codec && codec->patch_ops.unsol_event) |
| 722 | codec->patch_ops.unsol_event(codec, res); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * destructor |
| 728 | */ |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 729 | static void snd_hda_bus_free(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 731 | if (!bus) |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 732 | return; |
| 733 | |
| 734 | WARN_ON(!list_empty(&bus->codec_list)); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 735 | if (bus->workq) |
| 736 | flush_workqueue(bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (bus->ops.private_free) |
| 738 | bus->ops.private_free(bus); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 739 | if (bus->workq) |
| 740 | destroy_workqueue(bus->workq); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | kfree(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 745 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 747 | snd_hda_bus_free(device->device_data); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int snd_hda_bus_dev_disconnect(struct snd_device *device) |
| 752 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | struct hda_bus *bus = device->device_data; |
Takashi Iwai | b94d3539 | 2008-11-21 09:08:06 +0100 | [diff] [blame] | 754 | bus->shutdown = 1; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 755 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
| 759 | * snd_hda_bus_new - create a HDA bus |
| 760 | * @card: the card entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | * @busp: the pointer to store the created bus instance |
| 762 | * |
| 763 | * Returns 0 if successful, or a negative error code. |
| 764 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 765 | int snd_hda_bus_new(struct snd_card *card, |
Takashi Iwai | ef74497 | 2015-02-17 13:56:29 +0100 | [diff] [blame] | 766 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | struct hda_bus *bus; |
| 769 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 770 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 771 | .dev_disconnect = snd_hda_bus_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | .dev_free = snd_hda_bus_dev_free, |
| 773 | }; |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (busp) |
| 776 | *busp = NULL; |
| 777 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 778 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (bus == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 780 | dev_err(card->dev, "can't allocate struct hda_bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return -ENOMEM; |
| 782 | } |
| 783 | |
| 784 | bus->card = card; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 785 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 786 | mutex_init(&bus->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | INIT_LIST_HEAD(&bus->codec_list); |
Takashi Iwai | 922c88a | 2015-02-17 14:46:40 +0100 | [diff] [blame] | 788 | INIT_WORK(&bus->unsol.work, process_unsol_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 790 | snprintf(bus->workq_name, sizeof(bus->workq_name), |
| 791 | "hd-audio%d", card->number); |
| 792 | bus->workq = create_singlethread_workqueue(bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 793 | if (!bus->workq) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 794 | dev_err(card->dev, "cannot create workqueue %s\n", |
Takashi Iwai | e8c0ee5 | 2009-02-05 07:34:28 +0100 | [diff] [blame] | 795 | bus->workq_name); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 796 | kfree(bus); |
| 797 | return -ENOMEM; |
| 798 | } |
| 799 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 800 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 801 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | snd_hda_bus_free(bus); |
| 803 | return err; |
| 804 | } |
| 805 | if (busp) |
| 806 | *busp = bus; |
| 807 | return 0; |
| 808 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 809 | EXPORT_SYMBOL_GPL(snd_hda_bus_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 812 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 814 | static void setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 816 | int i, total_nodes, function_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | hda_nid_t nid; |
| 818 | |
| 819 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 820 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 93e82ae | 2009-04-17 18:04:41 +0200 | [diff] [blame] | 821 | function_id = snd_hda_param_read(codec, nid, |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 822 | AC_PAR_FUNCTION_TYPE); |
Jaroslav Kysela | cd7643b | 2010-07-20 12:11:25 +0200 | [diff] [blame] | 823 | switch (function_id & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 824 | case AC_GRP_AUDIO_FUNCTION: |
| 825 | codec->afg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 826 | codec->afg_function_id = function_id & 0xff; |
| 827 | codec->afg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 828 | break; |
| 829 | case AC_GRP_MODEM_FUNCTION: |
| 830 | codec->mfg = nid; |
Jaroslav Kysela | 79c944a | 2010-07-19 15:52:39 +0200 | [diff] [blame] | 831 | codec->mfg_function_id = function_id & 0xff; |
| 832 | codec->mfg_unsol = (function_id >> 8) & 1; |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 833 | break; |
| 834 | default: |
| 835 | break; |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 841 | * read widget caps for each widget and store in cache |
| 842 | */ |
| 843 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 844 | { |
| 845 | int i; |
| 846 | hda_nid_t nid; |
| 847 | |
| 848 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 849 | &codec->start_nid); |
| 850 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 851 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 852 | return -ENOMEM; |
| 853 | nid = codec->start_nid; |
| 854 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 855 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 856 | AC_PAR_AUDIO_WIDGET_CAP); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 860 | /* read all pin default configurations and save codec->init_pins */ |
| 861 | static int read_pin_defaults(struct hda_codec *codec) |
| 862 | { |
| 863 | int i; |
| 864 | hda_nid_t nid = codec->start_nid; |
| 865 | |
| 866 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 867 | struct hda_pincfg *pin; |
| 868 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | a22d543 | 2009-07-27 12:54:26 +0200 | [diff] [blame] | 869 | unsigned int wid_type = get_wcaps_type(wcaps); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 870 | if (wid_type != AC_WID_PIN) |
| 871 | continue; |
| 872 | pin = snd_array_new(&codec->init_pins); |
| 873 | if (!pin) |
| 874 | return -ENOMEM; |
| 875 | pin->nid = nid; |
| 876 | pin->cfg = snd_hda_codec_read(codec, nid, 0, |
| 877 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 878 | pin->ctrl = snd_hda_codec_read(codec, nid, 0, |
| 879 | AC_VERB_GET_PIN_WIDGET_CONTROL, |
| 880 | 0); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 881 | } |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* look up the given pin config list and return the item matching with NID */ |
| 886 | static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, |
| 887 | struct snd_array *array, |
| 888 | hda_nid_t nid) |
| 889 | { |
| 890 | int i; |
| 891 | for (i = 0; i < array->used; i++) { |
| 892 | struct hda_pincfg *pin = snd_array_elem(array, i); |
| 893 | if (pin->nid == nid) |
| 894 | return pin; |
| 895 | } |
| 896 | return NULL; |
| 897 | } |
| 898 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 899 | /* set the current pin config value for the given NID. |
| 900 | * the value is cached, and read via snd_hda_codec_get_pincfg() |
| 901 | */ |
| 902 | int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, |
| 903 | hda_nid_t nid, unsigned int cfg) |
| 904 | { |
| 905 | struct hda_pincfg *pin; |
| 906 | |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 907 | /* the check below may be invalid when pins are added by a fixup |
| 908 | * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled |
| 909 | * for now |
| 910 | */ |
| 911 | /* |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 912 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) |
| 913 | return -EINVAL; |
Takashi Iwai | d5657ec | 2013-04-18 09:59:28 +0200 | [diff] [blame] | 914 | */ |
Takashi Iwai | b82855a | 2009-12-27 11:24:56 +0100 | [diff] [blame] | 915 | |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 916 | pin = look_up_pincfg(codec, list, nid); |
| 917 | if (!pin) { |
| 918 | pin = snd_array_new(list); |
| 919 | if (!pin) |
| 920 | return -ENOMEM; |
| 921 | pin->nid = nid; |
| 922 | } |
| 923 | pin->cfg = cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 927 | /** |
| 928 | * snd_hda_codec_set_pincfg - Override a pin default configuration |
| 929 | * @codec: the HDA codec |
| 930 | * @nid: NID to set the pin config |
| 931 | * @cfg: the pin default config value |
| 932 | * |
| 933 | * Override a pin default configuration value in the cache. |
| 934 | * This value can be read by snd_hda_codec_get_pincfg() in a higher |
| 935 | * priority than the real hardware value. |
| 936 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 937 | int snd_hda_codec_set_pincfg(struct hda_codec *codec, |
| 938 | hda_nid_t nid, unsigned int cfg) |
| 939 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 940 | return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 941 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 942 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 943 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 944 | /** |
| 945 | * snd_hda_codec_get_pincfg - Obtain a pin-default configuration |
| 946 | * @codec: the HDA codec |
| 947 | * @nid: NID to get the pin config |
| 948 | * |
| 949 | * Get the current pin config value of the given pin NID. |
| 950 | * If the pincfg value is cached or overridden via sysfs or driver, |
| 951 | * returns the cached value. |
| 952 | */ |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 953 | unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) |
| 954 | { |
| 955 | struct hda_pincfg *pin; |
| 956 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 957 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 09b70e8 | 2013-01-10 18:21:56 +0100 | [diff] [blame] | 958 | { |
| 959 | unsigned int cfg = 0; |
| 960 | mutex_lock(&codec->user_mutex); |
| 961 | pin = look_up_pincfg(codec, &codec->user_pins, nid); |
| 962 | if (pin) |
| 963 | cfg = pin->cfg; |
| 964 | mutex_unlock(&codec->user_mutex); |
| 965 | if (cfg) |
| 966 | return cfg; |
| 967 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 968 | #endif |
Takashi Iwai | 5e7b8e0 | 2009-02-23 09:45:59 +0100 | [diff] [blame] | 969 | pin = look_up_pincfg(codec, &codec->driver_pins, nid); |
| 970 | if (pin) |
| 971 | return pin->cfg; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 972 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 973 | if (pin) |
| 974 | return pin->cfg; |
| 975 | return 0; |
| 976 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 977 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 978 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 979 | /** |
| 980 | * snd_hda_codec_set_pin_target - remember the current pinctl target value |
| 981 | * @codec: the HDA codec |
| 982 | * @nid: pin NID |
| 983 | * @val: assigned pinctl value |
| 984 | * |
| 985 | * This function stores the given value to a pinctl target value in the |
| 986 | * pincfg table. This isn't always as same as the actually written value |
| 987 | * but can be referred at any time via snd_hda_codec_get_pin_target(). |
| 988 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 989 | int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, |
| 990 | unsigned int val) |
| 991 | { |
| 992 | struct hda_pincfg *pin; |
| 993 | |
| 994 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 995 | if (!pin) |
| 996 | return -EINVAL; |
| 997 | pin->target = val; |
| 998 | return 0; |
| 999 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1000 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1001 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * snd_hda_codec_get_pin_target - return the current pinctl target value |
| 1004 | * @codec: the HDA codec |
| 1005 | * @nid: pin NID |
| 1006 | */ |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1007 | int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) |
| 1008 | { |
| 1009 | struct hda_pincfg *pin; |
| 1010 | |
| 1011 | pin = look_up_pincfg(codec, &codec->init_pins, nid); |
| 1012 | if (!pin) |
| 1013 | return 0; |
| 1014 | return pin->target; |
| 1015 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 1017 | |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1018 | /** |
| 1019 | * snd_hda_shutup_pins - Shut up all pins |
| 1020 | * @codec: the HDA codec |
| 1021 | * |
| 1022 | * Clear all pin controls to shup up before suspend for avoiding click noise. |
| 1023 | * The controls aren't cached so that they can be resumed properly. |
| 1024 | */ |
| 1025 | void snd_hda_shutup_pins(struct hda_codec *codec) |
| 1026 | { |
| 1027 | int i; |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1028 | /* don't shut up pins when unloading the driver; otherwise it breaks |
| 1029 | * the default pin setup at the next load of the driver |
| 1030 | */ |
| 1031 | if (codec->bus->shutdown) |
| 1032 | return; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1033 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1034 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1035 | /* use read here for syncing after issuing each verb */ |
| 1036 | snd_hda_codec_read(codec, pin->nid, 0, |
| 1037 | AC_VERB_SET_PIN_WIDGET_CONTROL, 0); |
| 1038 | } |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1039 | codec->pins_shutup = 1; |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1040 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); |
Takashi Iwai | 92ee616 | 2009-12-27 11:18:59 +0100 | [diff] [blame] | 1042 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1043 | #ifdef CONFIG_PM |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1044 | /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ |
| 1045 | static void restore_shutup_pins(struct hda_codec *codec) |
| 1046 | { |
| 1047 | int i; |
| 1048 | if (!codec->pins_shutup) |
| 1049 | return; |
| 1050 | if (codec->bus->shutdown) |
| 1051 | return; |
| 1052 | for (i = 0; i < codec->init_pins.used; i++) { |
| 1053 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 1054 | snd_hda_codec_write(codec, pin->nid, 0, |
| 1055 | AC_VERB_SET_PIN_WIDGET_CONTROL, |
| 1056 | pin->ctrl); |
| 1057 | } |
| 1058 | codec->pins_shutup = 0; |
| 1059 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1060 | #endif |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 1061 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1062 | static void hda_jackpoll_work(struct work_struct *work) |
| 1063 | { |
| 1064 | struct hda_codec *codec = |
| 1065 | container_of(work, struct hda_codec, jackpoll_work.work); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1066 | |
| 1067 | snd_hda_jack_set_dirty_all(codec); |
| 1068 | snd_hda_jack_poll_all(codec); |
Wang Xingchao | 18e6062 | 2013-07-25 23:34:45 -0400 | [diff] [blame] | 1069 | |
| 1070 | if (!codec->jackpoll_interval) |
| 1071 | return; |
| 1072 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1073 | queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, |
| 1074 | codec->jackpoll_interval); |
| 1075 | } |
| 1076 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1077 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 1078 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1079 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1080 | |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1081 | /* release all pincfg lists */ |
| 1082 | static void free_init_pincfgs(struct hda_codec *codec) |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1083 | { |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1084 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1085 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1086 | snd_array_free(&codec->user_pins); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1087 | #endif |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1088 | snd_array_free(&codec->init_pins); |
| 1089 | } |
| 1090 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1091 | /* |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1092 | * audio-converter setup caches |
| 1093 | */ |
| 1094 | struct hda_cvt_setup { |
| 1095 | hda_nid_t nid; |
| 1096 | u8 stream_tag; |
| 1097 | u8 channel_id; |
| 1098 | u16 format_id; |
| 1099 | unsigned char active; /* cvt is currently used */ |
| 1100 | unsigned char dirty; /* setups should be cleared */ |
| 1101 | }; |
| 1102 | |
| 1103 | /* get or create a cache entry for the given audio converter NID */ |
| 1104 | static struct hda_cvt_setup * |
| 1105 | get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) |
| 1106 | { |
| 1107 | struct hda_cvt_setup *p; |
| 1108 | int i; |
| 1109 | |
| 1110 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1111 | p = snd_array_elem(&codec->cvt_setups, i); |
| 1112 | if (p->nid == nid) |
| 1113 | return p; |
| 1114 | } |
| 1115 | p = snd_array_new(&codec->cvt_setups); |
| 1116 | if (p) |
| 1117 | p->nid = nid; |
| 1118 | return p; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | * codec destructor |
| 1123 | */ |
| 1124 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 1125 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1126 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return; |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1128 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1129 | if (device_is_registered(hda_codec_dev(codec))) |
| 1130 | device_del(hda_codec_dev(codec)); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1131 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 3fdf146 | 2012-11-22 08:16:31 +0100 | [diff] [blame] | 1132 | free_init_pincfgs(codec); |
Takashi Iwai | 6acaed3 | 2009-01-12 10:09:24 +0100 | [diff] [blame] | 1133 | flush_workqueue(codec->bus->workq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | list_del(&codec->list); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1135 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1136 | snd_array_free(&codec->nids); |
Takashi Iwai | 59cad16 | 2012-06-26 15:00:20 +0200 | [diff] [blame] | 1137 | snd_array_free(&codec->cvt_setups); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1138 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1139 | remove_conn_list(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | codec->bus->caddr_tbl[codec->addr] = NULL; |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame^] | 1141 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1142 | snd_hda_sysfs_clear(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1143 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1144 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 812a2cc | 2009-05-16 10:00:49 +0200 | [diff] [blame] | 1145 | kfree(codec->vendor_name); |
| 1146 | kfree(codec->chip_name); |
Takashi Iwai | f44ac83 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1147 | kfree(codec->modelname); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1148 | kfree(codec->wcaps); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1149 | codec->bus->num_codecs--; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1150 | put_device(hda_codec_dev(codec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1153 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, |
| 1154 | hda_nid_t fg, unsigned int power_state); |
| 1155 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1156 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1157 | unsigned int power_state); |
| 1158 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1159 | static int snd_hda_codec_dev_register(struct snd_device *device) |
| 1160 | { |
| 1161 | struct hda_codec *codec = device->device_data; |
| 1162 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1163 | snd_hda_register_beep_device(codec); |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 1164 | if (device_is_registered(hda_codec_dev(codec))) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1165 | pm_runtime_enable(hda_codec_dev(codec)); |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1166 | return 0; |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static int snd_hda_codec_dev_disconnect(struct snd_device *device) |
| 1170 | { |
| 1171 | struct hda_codec *codec = device->device_data; |
| 1172 | |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 1173 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1174 | return 0; |
| 1175 | } |
| 1176 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1177 | static int snd_hda_codec_dev_free(struct snd_device *device) |
| 1178 | { |
| 1179 | snd_hda_codec_free(device->device_data); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1183 | /* just free the container */ |
| 1184 | static void snd_hda_codec_dev_release(struct device *dev) |
| 1185 | { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1186 | kfree(dev_to_hda_codec(dev)); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | /** |
| 1190 | * snd_hda_codec_new - create a HDA codec |
| 1191 | * @bus: the bus to assign |
| 1192 | * @codec_addr: the codec address |
| 1193 | * @codecp: the pointer to store the generated codec |
| 1194 | * |
| 1195 | * Returns 0 if successful, or a negative error code. |
| 1196 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1197 | int snd_hda_codec_new(struct hda_bus *bus, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1198 | unsigned int codec_addr, |
| 1199 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | { |
| 1201 | struct hda_codec *codec; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1202 | struct device *dev; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 1203 | char component[31]; |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1204 | hda_nid_t fg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | int err; |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1206 | static struct snd_device_ops dev_ops = { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1207 | .dev_register = snd_hda_codec_dev_register, |
| 1208 | .dev_disconnect = snd_hda_codec_dev_disconnect, |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1209 | .dev_free = snd_hda_codec_dev_free, |
| 1210 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 1212 | if (snd_BUG_ON(!bus)) |
| 1213 | return -EINVAL; |
| 1214 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 1215 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
| 1217 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1218 | dev_err(bus->card->dev, |
| 1219 | "address 0x%x is already occupied\n", |
| 1220 | codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return -EBUSY; |
| 1222 | } |
| 1223 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1224 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | if (codec == NULL) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1226 | dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | return -ENOMEM; |
| 1228 | } |
| 1229 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1230 | dev = hda_codec_dev(codec); |
| 1231 | device_initialize(dev); |
| 1232 | dev->parent = bus->card->dev; |
| 1233 | dev->bus = &snd_hda_bus_type; |
| 1234 | dev->release = snd_hda_codec_dev_release; |
| 1235 | dev->groups = snd_hda_dev_attr_groups; |
| 1236 | dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr); |
| 1237 | dev_set_drvdata(dev, codec); /* for sysfs */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 1238 | device_enable_async_suspend(dev); |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | codec->bus = bus; |
| 1241 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1242 | mutex_init(&codec->spdif_mutex); |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 1243 | mutex_init(&codec->control_mutex); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1244 | mutex_init(&codec->hash_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1245 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1246 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 1247 | snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); |
| 1248 | snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1249 | snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 1250 | snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1251 | snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 1252 | snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); |
Takashi Iwai | 361dab3 | 2012-05-09 14:35:27 +0200 | [diff] [blame] | 1253 | snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 1254 | snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 1255 | INIT_LIST_HEAD(&codec->conn_list); |
| 1256 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 1257 | INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 1258 | codec->depop_delay = -1; |
David Henningsson | f5662e1 | 2014-07-22 14:09:34 +0200 | [diff] [blame] | 1259 | codec->fixup_id = HDA_FIXUP_ID_NOT_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1261 | #ifdef CONFIG_PM |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1262 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 1263 | * the caller has to power down appropriatley after initialization |
| 1264 | * phase. |
| 1265 | */ |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame^] | 1266 | set_bit(codec->addr, &bus->codec_powered); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 1267 | pm_runtime_set_active(hda_codec_dev(codec)); |
| 1268 | pm_runtime_get_noresume(hda_codec_dev(codec)); |
| 1269 | codec->power_jiffies = jiffies; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1270 | #endif |
| 1271 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 1272 | snd_hda_sysfs_init(codec); |
| 1273 | |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1274 | if (codec->bus->modelname) { |
| 1275 | codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); |
| 1276 | if (!codec->modelname) { |
Takashi Iwai | 13aeaf6 | 2014-02-25 07:53:47 +0100 | [diff] [blame] | 1277 | err = -ENODEV; |
| 1278 | goto error; |
Takashi Iwai | c382a9f | 2012-05-07 15:01:02 +0200 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | list_add_tail(&codec->list, &bus->codec_list); |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1283 | bus->num_codecs++; |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | bus->caddr_tbl[codec_addr] = codec; |
| 1286 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1287 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1288 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 1289 | if (codec->vendor_id == -1) |
| 1290 | /* read again, hopefully the access method was corrected |
| 1291 | * in the last read... |
| 1292 | */ |
| 1293 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1294 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1295 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1296 | AC_PAR_SUBSYSTEM_ID); |
| 1297 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 1298 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 1300 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1301 | if (!codec->afg && !codec->mfg) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1302 | dev_err(bus->card->dev, "no AFG or MFG node found\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1303 | err = -ENODEV; |
| 1304 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1307 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1308 | err = read_widget_caps(codec, fg); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1309 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1310 | dev_err(bus->card->dev, "cannot malloc\n"); |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1311 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1312 | } |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1313 | err = read_pin_defaults(codec); |
| 1314 | if (err < 0) |
| 1315 | goto error; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1316 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1317 | if (!codec->subsystem_id) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1318 | codec->subsystem_id = |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1319 | snd_hda_codec_read(codec, fg, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1320 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 1323 | #ifdef CONFIG_PM |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1324 | codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1325 | AC_PWRST_CLKSTOP); |
Mengdong Lin | 5d6147f | 2012-08-24 12:06:30 +0800 | [diff] [blame] | 1326 | #endif |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1327 | codec->epss = snd_hda_codec_get_supported_ps(codec, fg, |
Takashi Iwai | 983f6b9 | 2012-08-28 09:18:01 -0700 | [diff] [blame] | 1328 | AC_PWRST_EPSS); |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 1329 | |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1330 | /* power-up all before initialization */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 1331 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | bb6ac72 | 2009-03-13 09:02:42 +0100 | [diff] [blame] | 1332 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1333 | snd_hda_codec_proc_new(codec); |
| 1334 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1335 | snd_hda_create_hwdep(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1336 | |
| 1337 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, |
| 1338 | codec->subsystem_id, codec->revision_id); |
| 1339 | snd_component_add(codec->bus->card, component); |
| 1340 | |
Takashi Iwai | 2565c89 | 2014-02-19 11:41:09 +0100 | [diff] [blame] | 1341 | err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); |
| 1342 | if (err < 0) |
| 1343 | goto error; |
| 1344 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1345 | if (codecp) |
| 1346 | *codecp = codec; |
| 1347 | return 0; |
Takashi Iwai | 3be1414 | 2009-02-20 14:11:16 +0100 | [diff] [blame] | 1348 | |
| 1349 | error: |
| 1350 | snd_hda_codec_free(codec); |
| 1351 | return err; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1352 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1353 | EXPORT_SYMBOL_GPL(snd_hda_codec_new); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 1354 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1355 | /** |
| 1356 | * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults |
| 1357 | * @codec: the HDA codec |
| 1358 | * |
| 1359 | * Forcibly refresh the all widget caps and the init pin configurations of |
| 1360 | * the given codec. |
| 1361 | */ |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1362 | int snd_hda_codec_update_widgets(struct hda_codec *codec) |
| 1363 | { |
| 1364 | hda_nid_t fg; |
| 1365 | int err; |
| 1366 | |
| 1367 | /* Assume the function group node does not change, |
| 1368 | * only the widget nodes may change. |
| 1369 | */ |
| 1370 | kfree(codec->wcaps); |
| 1371 | fg = codec->afg ? codec->afg : codec->mfg; |
| 1372 | err = read_widget_caps(codec, fg); |
| 1373 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1374 | codec_err(codec, "cannot malloc\n"); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1375 | return err; |
| 1376 | } |
| 1377 | |
| 1378 | snd_array_free(&codec->init_pins); |
| 1379 | err = read_pin_defaults(codec); |
| 1380 | |
| 1381 | return err; |
| 1382 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1383 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); |
Mengdong Lin | a15d05d | 2013-02-08 17:09:31 -0500 | [diff] [blame] | 1384 | |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1385 | /* update the stream-id if changed */ |
| 1386 | static void update_pcm_stream_id(struct hda_codec *codec, |
| 1387 | struct hda_cvt_setup *p, hda_nid_t nid, |
| 1388 | u32 stream_tag, int channel_id) |
| 1389 | { |
| 1390 | unsigned int oldval, newval; |
| 1391 | |
| 1392 | if (p->stream_tag != stream_tag || p->channel_id != channel_id) { |
| 1393 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); |
| 1394 | newval = (stream_tag << 4) | channel_id; |
| 1395 | if (oldval != newval) |
| 1396 | snd_hda_codec_write(codec, nid, 0, |
| 1397 | AC_VERB_SET_CHANNEL_STREAMID, |
| 1398 | newval); |
| 1399 | p->stream_tag = stream_tag; |
| 1400 | p->channel_id = channel_id; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | /* update the format-id if changed */ |
| 1405 | static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, |
| 1406 | hda_nid_t nid, int format) |
| 1407 | { |
| 1408 | unsigned int oldval; |
| 1409 | |
| 1410 | if (p->format_id != format) { |
| 1411 | oldval = snd_hda_codec_read(codec, nid, 0, |
| 1412 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 1413 | if (oldval != format) { |
| 1414 | msleep(1); |
| 1415 | snd_hda_codec_write(codec, nid, 0, |
| 1416 | AC_VERB_SET_STREAM_FORMAT, |
| 1417 | format); |
| 1418 | } |
| 1419 | p->format_id = format; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | /** |
| 1424 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 1425 | * @codec: the CODEC to set up |
| 1426 | * @nid: the NID to set up |
| 1427 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 1428 | * @channel_id: channel id to pass, zero based. |
| 1429 | * @format: stream format. |
| 1430 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1431 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1432 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | int channel_id, int format) |
| 1434 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1435 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1436 | struct hda_cvt_setup *p; |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1437 | int type; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1438 | int i; |
| 1439 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1440 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 1441 | return; |
| 1442 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1443 | codec_dbg(codec, |
| 1444 | "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
| 1445 | nid, stream_tag, channel_id, format); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1446 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1447 | if (!p) |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1448 | return; |
Takashi Iwai | ed36081 | 2012-08-08 17:12:52 +0200 | [diff] [blame] | 1449 | |
| 1450 | if (codec->pcm_format_first) |
| 1451 | update_pcm_format(codec, p, nid, format); |
| 1452 | update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); |
| 1453 | if (!codec->pcm_format_first) |
| 1454 | update_pcm_format(codec, p, nid, format); |
| 1455 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1456 | p->active = 1; |
| 1457 | p->dirty = 0; |
| 1458 | |
| 1459 | /* make other inactive cvts with the same stream-tag dirty */ |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1460 | type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1461 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1462 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1463 | p = snd_array_elem(&c->cvt_setups, i); |
Takashi Iwai | 62b7e5e | 2010-10-22 17:15:47 +0200 | [diff] [blame] | 1464 | if (!p->active && p->stream_tag == stream_tag && |
David Henningsson | 54c2a89 | 2012-02-01 12:05:41 +0100 | [diff] [blame] | 1465 | get_wcaps_type(get_wcaps(c, p->nid)) == type) |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1466 | p->dirty = 1; |
| 1467 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1470 | EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1472 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1473 | struct hda_cvt_setup *q); |
| 1474 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1475 | /** |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1476 | * __snd_hda_codec_cleanup_stream - clean up the codec for closing |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1477 | * @codec: the CODEC to clean up |
| 1478 | * @nid: the NID to clean up |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1479 | * @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] | 1480 | */ |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1481 | void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1482 | int do_now) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1483 | { |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1484 | struct hda_cvt_setup *p; |
| 1485 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1486 | if (!nid) |
| 1487 | return; |
| 1488 | |
Takashi Iwai | 0e7adbe | 2010-10-25 10:37:11 +0200 | [diff] [blame] | 1489 | if (codec->no_sticky_stream) |
| 1490 | do_now = 1; |
| 1491 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1492 | codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1493 | p = get_hda_cvt_setup(codec, nid); |
Takashi Iwai | 6c35ae3 | 2013-05-10 13:39:50 +0200 | [diff] [blame] | 1494 | if (p) { |
Takashi Iwai | f0cea79 | 2010-08-13 11:56:53 +0200 | [diff] [blame] | 1495 | /* here we just clear the active flag when do_now isn't set; |
| 1496 | * actual clean-ups will be done later in |
| 1497 | * purify_inactive_streams() called from snd_hda_codec_prpapre() |
| 1498 | */ |
| 1499 | if (do_now) |
| 1500 | really_cleanup_stream(codec, p); |
| 1501 | else |
| 1502 | p->active = 0; |
| 1503 | } |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1504 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1505 | EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 1506 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1507 | static void really_cleanup_stream(struct hda_codec *codec, |
| 1508 | struct hda_cvt_setup *q) |
| 1509 | { |
| 1510 | hda_nid_t nid = q->nid; |
Takashi Iwai | 218264a | 2011-09-27 17:33:45 +0200 | [diff] [blame] | 1511 | if (q->stream_tag || q->channel_id) |
| 1512 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 1513 | if (q->format_id) |
| 1514 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 |
| 1515 | ); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1516 | memset(q, 0, sizeof(*q)); |
| 1517 | q->nid = nid; |
| 1518 | } |
| 1519 | |
| 1520 | /* clean up the all conflicting obsolete streams */ |
| 1521 | static void purify_inactive_streams(struct hda_codec *codec) |
| 1522 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1523 | struct hda_codec *c; |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1524 | int i; |
| 1525 | |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 1526 | list_for_each_entry(c, &codec->bus->codec_list, list) { |
| 1527 | for (i = 0; i < c->cvt_setups.used; i++) { |
| 1528 | struct hda_cvt_setup *p; |
| 1529 | p = snd_array_elem(&c->cvt_setups, i); |
| 1530 | if (p->dirty) |
| 1531 | really_cleanup_stream(c, p); |
| 1532 | } |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 1536 | #ifdef CONFIG_PM |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1537 | /* clean up all streams; called from suspend */ |
| 1538 | static void hda_cleanup_all_streams(struct hda_codec *codec) |
| 1539 | { |
| 1540 | int i; |
| 1541 | |
| 1542 | for (i = 0; i < codec->cvt_setups.used; i++) { |
| 1543 | struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); |
| 1544 | if (p->stream_tag) |
| 1545 | really_cleanup_stream(codec, p); |
| 1546 | } |
| 1547 | } |
Mike Waychison | 1c7276c | 2011-04-20 12:04:36 -0700 | [diff] [blame] | 1548 | #endif |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 1549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | /* |
| 1551 | * amp access functions |
| 1552 | */ |
| 1553 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1554 | /* FIXME: more better hash key? */ |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 1555 | #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] | 1556 | #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1557 | #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) |
| 1558 | #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1560 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
| 1562 | /* initialize the hash table */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 1563 | static void init_hda_cache(struct hda_cache_rec *cache, |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1564 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1566 | memset(cache, 0, sizeof(*cache)); |
| 1567 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1568 | snd_array_init(&cache->buf, record_size, 64); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 1571 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1572 | { |
Takashi Iwai | 603c401 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 1573 | snd_array_free(&cache->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1577 | 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] | 1578 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1579 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1580 | u16 cur = cache->hash[idx]; |
| 1581 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
| 1583 | while (cur != 0xffff) { |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 1584 | info = snd_array_elem(&cache->buf, cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | if (info->key == key) |
| 1586 | return info; |
| 1587 | cur = info->next; |
| 1588 | } |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1589 | return NULL; |
| 1590 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1592 | /* query the hash. allocate an entry if not found. */ |
| 1593 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 1594 | u32 key) |
| 1595 | { |
| 1596 | struct hda_cache_head *info = get_hash(cache, key); |
| 1597 | if (!info) { |
| 1598 | u16 idx, cur; |
| 1599 | /* add a new hash entry */ |
| 1600 | info = snd_array_new(&cache->buf); |
| 1601 | if (!info) |
| 1602 | return NULL; |
| 1603 | cur = snd_array_index(&cache->buf, info); |
| 1604 | info->key = key; |
| 1605 | info->val = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1606 | info->dirty = 0; |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 1607 | idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 1608 | info->next = cache->hash[idx]; |
| 1609 | cache->hash[idx] = cur; |
| 1610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | return info; |
| 1612 | } |
| 1613 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 1614 | /* query and allocate an amp hash entry */ |
| 1615 | static inline struct hda_amp_info * |
| 1616 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 1617 | { |
| 1618 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 1619 | } |
| 1620 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1621 | /* overwrite the value with the key in the caps hash */ |
| 1622 | static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) |
| 1623 | { |
| 1624 | struct hda_amp_info *info; |
| 1625 | |
| 1626 | mutex_lock(&codec->hash_mutex); |
| 1627 | info = get_alloc_amp_hash(codec, key); |
| 1628 | if (!info) { |
| 1629 | mutex_unlock(&codec->hash_mutex); |
| 1630 | return -EINVAL; |
| 1631 | } |
| 1632 | info->amp_caps = val; |
| 1633 | info->head.val |= INFO_AMP_CAPS; |
| 1634 | mutex_unlock(&codec->hash_mutex); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /* query the value from the caps hash; if not found, fetch the current |
| 1639 | * value from the given function and store in the hash |
| 1640 | */ |
| 1641 | static unsigned int |
| 1642 | query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, |
| 1643 | unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) |
| 1644 | { |
| 1645 | struct hda_amp_info *info; |
| 1646 | unsigned int val; |
| 1647 | |
| 1648 | mutex_lock(&codec->hash_mutex); |
| 1649 | info = get_alloc_amp_hash(codec, key); |
| 1650 | if (!info) { |
| 1651 | mutex_unlock(&codec->hash_mutex); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | if (!(info->head.val & INFO_AMP_CAPS)) { |
| 1655 | mutex_unlock(&codec->hash_mutex); /* for reentrance */ |
| 1656 | val = func(codec, nid, dir); |
| 1657 | write_caps_hash(codec, key, val); |
| 1658 | } else { |
| 1659 | val = info->amp_caps; |
| 1660 | mutex_unlock(&codec->hash_mutex); |
| 1661 | } |
| 1662 | return val; |
| 1663 | } |
| 1664 | |
| 1665 | static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1666 | int direction) |
| 1667 | { |
| 1668 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
| 1669 | nid = codec->afg; |
| 1670 | return snd_hda_param_read(codec, nid, |
| 1671 | direction == HDA_OUTPUT ? |
| 1672 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); |
| 1673 | } |
| 1674 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1675 | /** |
| 1676 | * query_amp_caps - query AMP capabilities |
| 1677 | * @codec: the HD-auio codec |
| 1678 | * @nid: the NID to query |
| 1679 | * @direction: either #HDA_INPUT or #HDA_OUTPUT |
| 1680 | * |
| 1681 | * Query AMP capabilities for the given widget and direction. |
| 1682 | * Returns the obtained capability bits. |
| 1683 | * |
| 1684 | * When cap bits have been already read, this doesn't read again but |
| 1685 | * returns the cached value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 1687 | 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] | 1688 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1689 | return query_caps_hash(codec, nid, direction, |
| 1690 | HDA_HASH_KEY(nid, direction, 0), |
| 1691 | read_amp_cap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1693 | EXPORT_SYMBOL_GPL(query_amp_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1695 | /** |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1696 | * snd_hda_check_amp_caps - query AMP capabilities |
| 1697 | * @codec: the HD-audio codec |
| 1698 | * @nid: the NID to query |
| 1699 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1700 | * @bits: bit mask to check the result |
David Henningsson | 861a04e | 2014-09-23 10:38:17 +0200 | [diff] [blame] | 1701 | * |
| 1702 | * Check whether the widget has the given amp capability for the direction. |
| 1703 | */ |
| 1704 | bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1705 | int dir, unsigned int bits) |
| 1706 | { |
| 1707 | if (!nid) |
| 1708 | return false; |
| 1709 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 1710 | if (query_amp_caps(codec, nid, dir) & bits) |
| 1711 | return true; |
| 1712 | return false; |
| 1713 | } |
| 1714 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); |
| 1715 | |
| 1716 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1717 | * snd_hda_override_amp_caps - Override the AMP capabilities |
| 1718 | * @codec: the CODEC to clean up |
| 1719 | * @nid: the NID to clean up |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 1720 | * @dir: either #HDA_INPUT or #HDA_OUTPUT |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1721 | * @caps: the capability bits to set |
| 1722 | * |
| 1723 | * Override the cached AMP caps bits value by the given one. |
| 1724 | * This function is useful if the driver needs to adjust the AMP ranges, |
| 1725 | * e.g. limit to 0dB, etc. |
| 1726 | * |
| 1727 | * Returns zero if successful or a negative error code. |
| 1728 | */ |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1729 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1730 | unsigned int caps) |
| 1731 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1732 | return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1733 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1734 | EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 1735 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1736 | static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, |
| 1737 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1738 | { |
| 1739 | return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); |
| 1740 | } |
| 1741 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1742 | /** |
| 1743 | * snd_hda_query_pin_caps - Query PIN capabilities |
| 1744 | * @codec: the HD-auio codec |
| 1745 | * @nid: the NID to query |
| 1746 | * |
| 1747 | * Query PIN capabilities for the given widget. |
| 1748 | * Returns the obtained capability bits. |
| 1749 | * |
| 1750 | * When cap bits have been already read, this doesn't read again but |
| 1751 | * returns the cached value. |
| 1752 | */ |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1753 | u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) |
| 1754 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1755 | return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 1756 | read_pin_cap); |
| 1757 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1758 | EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); |
Takashi Iwai | 1327a32 | 2009-03-23 13:07:47 +0100 | [diff] [blame] | 1759 | |
Wu Fengguang | 864f92b | 2009-11-18 12:38:02 +0800 | [diff] [blame] | 1760 | /** |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1761 | * snd_hda_override_pin_caps - Override the pin capabilities |
| 1762 | * @codec: the CODEC |
| 1763 | * @nid: the NID to override |
| 1764 | * @caps: the capability bits to set |
| 1765 | * |
| 1766 | * Override the cached PIN capabilitiy bits value by the given one. |
| 1767 | * |
| 1768 | * Returns zero if successful or a negative error code. |
| 1769 | */ |
| 1770 | int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, |
| 1771 | unsigned int caps) |
| 1772 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1773 | return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1774 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1775 | EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); |
Takashi Iwai | f57c256 | 2011-08-15 12:49:07 +0200 | [diff] [blame] | 1776 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1777 | /* read or sync the hash value with the current value; |
| 1778 | * call within hash_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | */ |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1780 | static struct hda_amp_info * |
| 1781 | 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] | 1782 | int direction, int index, bool init_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1784 | struct hda_amp_info *info; |
| 1785 | unsigned int parm, val = 0; |
| 1786 | bool val_read = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1788 | retry: |
| 1789 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 1790 | if (!info) |
| 1791 | return NULL; |
| 1792 | if (!(info->head.val & INFO_AMP_VOL(ch))) { |
| 1793 | if (!val_read) { |
| 1794 | mutex_unlock(&codec->hash_mutex); |
| 1795 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 1796 | parm |= direction == HDA_OUTPUT ? |
| 1797 | AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 1798 | parm |= index; |
| 1799 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1800 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1801 | val &= 0xff; |
| 1802 | val_read = true; |
| 1803 | mutex_lock(&codec->hash_mutex); |
| 1804 | goto retry; |
| 1805 | } |
| 1806 | info->vol[ch] = val; |
| 1807 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1808 | } else if (init_only) |
| 1809 | return NULL; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1810 | return info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1814 | * write the current volume in info to the h/w |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | */ |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1816 | 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] | 1817 | hda_nid_t nid, int ch, int direction, int index, |
| 1818 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | { |
| 1820 | u32 parm; |
| 1821 | |
| 1822 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 1823 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 1824 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1825 | if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && |
| 1826 | (amp_caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 1827 | ; /* set the zero value as a fake mute */ |
| 1828 | else |
| 1829 | parm |= val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
| 1831 | } |
| 1832 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1833 | /** |
| 1834 | * snd_hda_codec_amp_read - Read AMP value |
| 1835 | * @codec: HD-audio codec |
| 1836 | * @nid: NID to read the AMP value |
| 1837 | * @ch: channel (left=0 or right=1) |
| 1838 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1839 | * @index: the index value (only for input direction) |
| 1840 | * |
| 1841 | * 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] | 1842 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1843 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1844 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1846 | struct hda_amp_info *info; |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1847 | unsigned int val = 0; |
| 1848 | |
| 1849 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1850 | info = update_amp_hash(codec, nid, ch, direction, index, false); |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 1851 | if (info) |
| 1852 | val = info->vol[ch]; |
| 1853 | mutex_unlock(&codec->hash_mutex); |
| 1854 | return val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1856 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1858 | static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1859 | int direction, int idx, int mask, int val, |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1860 | bool init_only, bool cache_only) |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1861 | { |
| 1862 | struct hda_amp_info *info; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1863 | unsigned int caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1864 | |
| 1865 | if (snd_BUG_ON(mask & ~0xff)) |
| 1866 | mask &= 0xff; |
| 1867 | val &= mask; |
| 1868 | |
| 1869 | mutex_lock(&codec->hash_mutex); |
| 1870 | info = update_amp_hash(codec, nid, ch, direction, idx, init_only); |
| 1871 | if (!info) { |
| 1872 | mutex_unlock(&codec->hash_mutex); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | val |= info->vol[ch] & ~mask; |
| 1876 | if (info->vol[ch] == val) { |
| 1877 | mutex_unlock(&codec->hash_mutex); |
| 1878 | return 0; |
| 1879 | } |
| 1880 | info->vol[ch] = val; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1881 | info->head.dirty |= cache_only; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1882 | caps = info->amp_caps; |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1883 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 1884 | if (!cache_only) |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 1885 | put_vol_mute(codec, caps, nid, ch, direction, idx, val); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1886 | return 1; |
| 1887 | } |
| 1888 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1889 | /** |
| 1890 | * snd_hda_codec_amp_update - update the AMP value |
| 1891 | * @codec: HD-audio codec |
| 1892 | * @nid: NID to read the AMP value |
| 1893 | * @ch: channel (left=0 or right=1) |
| 1894 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1895 | * @idx: the index value (only for input direction) |
| 1896 | * @mask: bit mask to set |
| 1897 | * @val: the bits value to set |
| 1898 | * |
| 1899 | * Update the AMP value with a bit mask. |
| 1900 | * Returns 0 if the value is unchanged, 1 if changed. |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1901 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 1902 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1903 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1905 | return codec_amp_update(codec, nid, ch, direction, idx, mask, val, |
| 1906 | false, codec->cached_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1908 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1910 | /** |
| 1911 | * snd_hda_codec_amp_stereo - update the AMP stereo values |
| 1912 | * @codec: HD-audio codec |
| 1913 | * @nid: NID to read the AMP value |
| 1914 | * @direction: #HDA_INPUT or #HDA_OUTPUT |
| 1915 | * @idx: the index value (only for input direction) |
| 1916 | * @mask: bit mask to set |
| 1917 | * @val: the bits value to set |
| 1918 | * |
| 1919 | * Update the AMP values like snd_hda_codec_amp_update(), but for a |
| 1920 | * stereo widget with the same mask and value. |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1921 | */ |
| 1922 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1923 | int direction, int idx, int mask, int val) |
| 1924 | { |
| 1925 | int ch, ret = 0; |
Takashi Iwai | 46712646 | 2010-03-29 09:19:38 +0200 | [diff] [blame] | 1926 | |
| 1927 | if (snd_BUG_ON(mask & ~0xff)) |
| 1928 | mask &= 0xff; |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1929 | for (ch = 0; ch < 2; ch++) |
| 1930 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 1931 | idx, mask, val); |
| 1932 | return ret; |
| 1933 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1934 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1935 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1936 | /** |
| 1937 | * snd_hda_codec_amp_init - initialize the AMP value |
| 1938 | * @codec: the HDA codec |
| 1939 | * @nid: NID to read the AMP value |
| 1940 | * @ch: channel (left=0 or right=1) |
| 1941 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1942 | * @idx: the index value (only for input direction) |
| 1943 | * @mask: bit mask to set |
| 1944 | * @val: the bits value to set |
| 1945 | * |
| 1946 | * 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] | 1947 | * the first access. If the amp was already initialized / updated beforehand, |
| 1948 | * this does nothing. |
| 1949 | */ |
| 1950 | int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 1951 | int dir, int idx, int mask, int val) |
| 1952 | { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 1953 | return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true, |
| 1954 | codec->cached_write); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1955 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1956 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1957 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 1958 | /** |
| 1959 | * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value |
| 1960 | * @codec: the HDA codec |
| 1961 | * @nid: NID to read the AMP value |
| 1962 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 1963 | * @idx: the index value (only for input direction) |
| 1964 | * @mask: bit mask to set |
| 1965 | * @val: the bits value to set |
| 1966 | * |
| 1967 | * Call snd_hda_codec_amp_init() for both stereo channels. |
| 1968 | */ |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1969 | int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 1970 | int dir, int idx, int mask, int val) |
| 1971 | { |
| 1972 | int ch, ret = 0; |
| 1973 | |
| 1974 | if (snd_BUG_ON(mask & ~0xff)) |
| 1975 | mask &= 0xff; |
| 1976 | for (ch = 0; ch < 2; ch++) |
| 1977 | ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, |
| 1978 | idx, mask, val); |
| 1979 | return ret; |
| 1980 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1981 | EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); |
Takashi Iwai | 280e57d | 2012-12-14 10:32:21 +0100 | [diff] [blame] | 1982 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 1983 | /** |
| 1984 | * snd_hda_codec_resume_amp - Resume all AMP commands from the cache |
| 1985 | * @codec: HD-audio codec |
| 1986 | * |
| 1987 | * Resume the all amp commands from the cache. |
| 1988 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1989 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 1990 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1991 | int i; |
| 1992 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1993 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 1994 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 1995 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 1996 | struct hda_amp_info *buffer; |
| 1997 | u32 key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1998 | hda_nid_t nid; |
| 1999 | unsigned int idx, dir, ch; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2000 | struct hda_amp_info info; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2001 | |
| 2002 | buffer = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | 8565f05 | 2012-12-20 12:54:18 +0100 | [diff] [blame] | 2003 | if (!buffer->head.dirty) |
| 2004 | continue; |
| 2005 | buffer->head.dirty = 0; |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2006 | info = *buffer; |
| 2007 | key = info.head.key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2008 | if (!key) |
| 2009 | continue; |
| 2010 | nid = key & 0xff; |
| 2011 | idx = (key >> 16) & 0xff; |
| 2012 | dir = (key >> 24) & 0xff; |
| 2013 | for (ch = 0; ch < 2; ch++) { |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2014 | if (!(info.head.val & INFO_AMP_VOL(ch))) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2015 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2016 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | 2ce4886a | 2012-12-20 12:58:12 +0100 | [diff] [blame] | 2017 | put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, |
| 2018 | info.vol[ch]); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2019 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2020 | } |
| 2021 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 2022 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 2023 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2024 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2026 | static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2027 | unsigned int ofs) |
| 2028 | { |
| 2029 | u32 caps = query_amp_caps(codec, nid, dir); |
| 2030 | /* get num steps */ |
| 2031 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2032 | if (ofs < caps) |
| 2033 | caps -= ofs; |
| 2034 | return caps; |
| 2035 | } |
| 2036 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2037 | /** |
| 2038 | * 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] | 2039 | * @kcontrol: referred ctl element |
| 2040 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2041 | * |
| 2042 | * The control element is supposed to have the private_value field |
| 2043 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2044 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2045 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 2046 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { |
| 2048 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2049 | u16 nid = get_amp_nid(kcontrol); |
| 2050 | u8 chs = get_amp_channels(kcontrol); |
| 2051 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2052 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2054 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2055 | uinfo->count = chs == 3 ? 2 : 1; |
| 2056 | uinfo->value.integer.min = 0; |
| 2057 | uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); |
| 2058 | if (!uinfo->value.integer.max) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2059 | codec_warn(codec, |
| 2060 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", |
| 2061 | nid, kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | return -EINVAL; |
| 2063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | return 0; |
| 2065 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2066 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2068 | |
| 2069 | static inline unsigned int |
| 2070 | read_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2071 | int ch, int dir, int idx, unsigned int ofs) |
| 2072 | { |
| 2073 | unsigned int val; |
| 2074 | val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); |
| 2075 | val &= HDA_AMP_VOLMASK; |
| 2076 | if (val >= ofs) |
| 2077 | val -= ofs; |
| 2078 | else |
| 2079 | val = 0; |
| 2080 | return val; |
| 2081 | } |
| 2082 | |
| 2083 | static inline int |
| 2084 | update_amp_value(struct hda_codec *codec, hda_nid_t nid, |
| 2085 | int ch, int dir, int idx, unsigned int ofs, |
| 2086 | unsigned int val) |
| 2087 | { |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2088 | unsigned int maxval; |
| 2089 | |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2090 | if (val > 0) |
| 2091 | val += ofs; |
Takashi Iwai | 7ccc3ef | 2010-07-26 17:00:15 +0200 | [diff] [blame] | 2092 | /* ofs = 0: raw max value */ |
| 2093 | maxval = get_amp_max_value(codec, nid, dir, 0); |
Takashi Iwai | afbd9b8 | 2010-07-08 18:40:37 +0200 | [diff] [blame] | 2094 | if (val > maxval) |
| 2095 | val = maxval; |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2096 | return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val, |
| 2097 | false, !hda_codec_is_power_on(codec)); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2100 | /** |
| 2101 | * 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] | 2102 | * @kcontrol: ctl element |
| 2103 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2104 | * |
| 2105 | * The control element is supposed to have the private_value field |
| 2106 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2107 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2108 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 2109 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | { |
| 2111 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2112 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2113 | int chs = get_amp_channels(kcontrol); |
| 2114 | int dir = get_amp_direction(kcontrol); |
| 2115 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2116 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | long *valp = ucontrol->value.integer.value; |
| 2118 | |
| 2119 | if (chs & 1) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2120 | *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2122 | *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | return 0; |
| 2124 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2125 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2127 | /** |
| 2128 | * 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] | 2129 | * @kcontrol: ctl element |
| 2130 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2131 | * |
| 2132 | * The control element is supposed to have the private_value field |
| 2133 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2134 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2135 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 2136 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | { |
| 2138 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2139 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2140 | int chs = get_amp_channels(kcontrol); |
| 2141 | int dir = get_amp_direction(kcontrol); |
| 2142 | int idx = get_amp_index(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2143 | unsigned int ofs = get_amp_offset(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | long *valp = ucontrol->value.integer.value; |
| 2145 | int change = 0; |
| 2146 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2147 | if (chs & 1) { |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2148 | change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2149 | valp++; |
| 2150 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2151 | if (chs & 2) |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2152 | change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | return change; |
| 2154 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2155 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2157 | /** |
| 2158 | * 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] | 2159 | * @kcontrol: ctl element |
| 2160 | * @op_flag: operation flag |
| 2161 | * @size: byte size of input TLV |
| 2162 | * @_tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2163 | * |
| 2164 | * The control element is supposed to have the private_value field |
| 2165 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2166 | */ |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2167 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2168 | unsigned int size, unsigned int __user *_tlv) |
| 2169 | { |
| 2170 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2171 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2172 | int dir = get_amp_direction(kcontrol); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2173 | unsigned int ofs = get_amp_offset(kcontrol); |
Clemens Ladisch | de8c85f | 2010-10-15 10:32:50 +0200 | [diff] [blame] | 2174 | bool min_mute = get_amp_min_mute(kcontrol); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2175 | u32 caps, val1, val2; |
| 2176 | |
| 2177 | if (size < 4 * sizeof(unsigned int)) |
| 2178 | return -ENOMEM; |
| 2179 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2180 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2181 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2182 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
Takashi Iwai | 29fdbec | 2009-01-20 13:07:55 +0100 | [diff] [blame] | 2183 | val1 += ofs; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2184 | val1 = ((int)val1) * ((int)val2); |
Takashi Iwai | 3868137 | 2012-02-27 15:00:58 +0100 | [diff] [blame] | 2185 | if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) |
Takashi Iwai | c08d916 | 2010-10-17 10:40:53 +0200 | [diff] [blame] | 2186 | val2 |= TLV_DB_SCALE_MUTE; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2187 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 2188 | return -EFAULT; |
| 2189 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 2190 | return -EFAULT; |
| 2191 | if (put_user(val1, _tlv + 2)) |
| 2192 | return -EFAULT; |
| 2193 | if (put_user(val2, _tlv + 3)) |
| 2194 | return -EFAULT; |
| 2195 | return 0; |
| 2196 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2197 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 2198 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2199 | /** |
| 2200 | * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control |
| 2201 | * @codec: HD-audio codec |
| 2202 | * @nid: NID of a reference widget |
| 2203 | * @dir: #HDA_INPUT or #HDA_OUTPUT |
| 2204 | * @tlv: TLV data to be stored, at least 4 elements |
| 2205 | * |
| 2206 | * Set (static) TLV data for a virtual master volume using the AMP caps |
| 2207 | * obtained from the reference NID. |
| 2208 | * The volume range is recalculated as if the max volume is 0dB. |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2209 | */ |
| 2210 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 2211 | unsigned int *tlv) |
| 2212 | { |
| 2213 | u32 caps; |
| 2214 | int nums, step; |
| 2215 | |
| 2216 | caps = query_amp_caps(codec, nid, dir); |
| 2217 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 2218 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 2219 | step = (step + 1) * 25; |
| 2220 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 2221 | tlv[1] = 2 * sizeof(unsigned int); |
| 2222 | tlv[2] = -nums * step; |
| 2223 | tlv[3] = step; |
| 2224 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2225 | EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2226 | |
| 2227 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2228 | static struct snd_kcontrol * |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2229 | 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] | 2230 | { |
| 2231 | struct snd_ctl_elem_id id; |
| 2232 | memset(&id, 0, sizeof(id)); |
| 2233 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2234 | id.device = dev; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2235 | id.index = idx; |
Takashi Iwai | 18cb710 | 2009-04-16 10:22:24 +0200 | [diff] [blame] | 2236 | if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) |
| 2237 | return NULL; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2238 | strcpy(id.name, name); |
| 2239 | return snd_ctl_find_id(codec->bus->card, &id); |
| 2240 | } |
| 2241 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2242 | /** |
| 2243 | * snd_hda_find_mixer_ctl - Find a mixer control element with the given name |
| 2244 | * @codec: HD-audio codec |
| 2245 | * @name: ctl id name string |
| 2246 | * |
| 2247 | * Get the control element with the given id string and IFACE_MIXER. |
| 2248 | */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2249 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 2250 | const char *name) |
| 2251 | { |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2252 | return find_mixer_ctl(codec, name, 0, 0); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2253 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2254 | EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 2255 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 2256 | 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] | 2257 | int start_idx) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2258 | { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 2259 | int i, idx; |
| 2260 | /* 16 ctlrs should be large enough */ |
| 2261 | for (i = 0, idx = start_idx; i < 16; i++, idx++) { |
| 2262 | if (!find_mixer_ctl(codec, name, 0, idx)) |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 2263 | return idx; |
| 2264 | } |
| 2265 | return -EBUSY; |
| 2266 | } |
| 2267 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2268 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2269 | * 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] | 2270 | * @codec: HD-audio codec |
| 2271 | * @nid: corresponding NID (optional) |
| 2272 | * @kctl: the control element to assign |
| 2273 | * |
| 2274 | * Add the given control element to an array inside the codec instance. |
| 2275 | * All control elements belonging to a codec are supposed to be added |
| 2276 | * by this function so that a proper clean-up works at the free or |
| 2277 | * reconfiguration time. |
| 2278 | * |
| 2279 | * If non-zero @nid is passed, the NID is assigned to the control element. |
| 2280 | * The assignment is shown in the codec proc file. |
| 2281 | * |
| 2282 | * snd_hda_ctl_add() checks the control subdev id field whether |
| 2283 | * #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] | 2284 | * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit |
| 2285 | * specifies if kctl->private_value is a HDA amplifier value. |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2286 | */ |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2287 | int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, |
| 2288 | struct snd_kcontrol *kctl) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2289 | { |
| 2290 | int err; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2291 | unsigned short flags = 0; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2292 | struct hda_nid_item *item; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2293 | |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2294 | if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2295 | flags |= HDA_NID_ITEM_AMP; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2296 | if (nid == 0) |
| 2297 | nid = get_amp_nid_(kctl->private_value); |
| 2298 | } |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2299 | if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) |
| 2300 | nid = kctl->id.subdevice & 0xffff; |
Jaroslav Kysela | 5e26dfd | 2009-12-10 13:57:01 +0100 | [diff] [blame] | 2301 | if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 2302 | kctl->id.subdevice = 0; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2303 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2304 | if (err < 0) |
| 2305 | return err; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2306 | item = snd_array_new(&codec->mixers); |
| 2307 | if (!item) |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2308 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2309 | item->kctl = kctl; |
| 2310 | item->nid = nid; |
Jaroslav Kysela | 9e3fd87 | 2009-12-08 17:45:25 +0100 | [diff] [blame] | 2311 | item->flags = flags; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2312 | return 0; |
| 2313 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2314 | EXPORT_SYMBOL_GPL(snd_hda_ctl_add); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2315 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2316 | /** |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2317 | * snd_hda_add_nid - Assign a NID to a control element |
| 2318 | * @codec: HD-audio codec |
| 2319 | * @nid: corresponding NID (optional) |
| 2320 | * @kctl: the control element to assign |
| 2321 | * @index: index to kctl |
| 2322 | * |
| 2323 | * Add the given control element to an array inside the codec instance. |
| 2324 | * This function is used when #snd_hda_ctl_add cannot be used for 1:1 |
| 2325 | * NID:KCTL mapping - for example "Capture Source" selector. |
| 2326 | */ |
| 2327 | int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, |
| 2328 | unsigned int index, hda_nid_t nid) |
| 2329 | { |
| 2330 | struct hda_nid_item *item; |
| 2331 | |
| 2332 | if (nid > 0) { |
| 2333 | item = snd_array_new(&codec->nids); |
| 2334 | if (!item) |
| 2335 | return -ENOMEM; |
| 2336 | item->kctl = kctl; |
| 2337 | item->index = index; |
| 2338 | item->nid = nid; |
| 2339 | return 0; |
| 2340 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2341 | codec_err(codec, "no NID for mapping control %s:%d:%d\n", |
| 2342 | kctl->id.name, kctl->id.index, index); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2343 | return -EINVAL; |
| 2344 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2345 | EXPORT_SYMBOL_GPL(snd_hda_add_nid); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2346 | |
| 2347 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2348 | * snd_hda_ctls_clear - Clear all controls assigned to the given codec |
| 2349 | * @codec: HD-audio codec |
| 2350 | */ |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2351 | void snd_hda_ctls_clear(struct hda_codec *codec) |
| 2352 | { |
| 2353 | int i; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2354 | struct hda_nid_item *items = codec->mixers.list; |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2355 | for (i = 0; i < codec->mixers.used; i++) |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2356 | snd_ctl_remove(codec->bus->card, items[i].kctl); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2357 | snd_array_free(&codec->mixers); |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 2358 | snd_array_free(&codec->nids); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2361 | /** |
| 2362 | * snd_hda_lock_devices - pseudo device locking |
| 2363 | * @bus: the BUS |
| 2364 | * |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2365 | * toggle card->shutdown to allow/disallow the device access (as a hack) |
| 2366 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2367 | int snd_hda_lock_devices(struct hda_bus *bus) |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2368 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2369 | struct snd_card *card = bus->card; |
| 2370 | struct hda_codec *codec; |
| 2371 | |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2372 | spin_lock(&card->files_lock); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2373 | if (card->shutdown) |
| 2374 | goto err_unlock; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2375 | card->shutdown = 1; |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2376 | if (!list_empty(&card->ctl_files)) |
| 2377 | goto err_clear; |
| 2378 | |
| 2379 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 2380 | int pcm; |
| 2381 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2382 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 2383 | if (!cpcm->pcm) |
| 2384 | continue; |
| 2385 | if (cpcm->pcm->streams[0].substream_opened || |
| 2386 | cpcm->pcm->streams[1].substream_opened) |
| 2387 | goto err_clear; |
| 2388 | } |
| 2389 | } |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2390 | spin_unlock(&card->files_lock); |
| 2391 | return 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2392 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2393 | err_clear: |
| 2394 | card->shutdown = 0; |
| 2395 | err_unlock: |
| 2396 | spin_unlock(&card->files_lock); |
| 2397 | return -EINVAL; |
| 2398 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2399 | EXPORT_SYMBOL_GPL(snd_hda_lock_devices); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2400 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2401 | /** |
| 2402 | * snd_hda_unlock_devices - pseudo device unlocking |
| 2403 | * @bus: the BUS |
| 2404 | */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2405 | void snd_hda_unlock_devices(struct hda_bus *bus) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2406 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2407 | struct snd_card *card = bus->card; |
| 2408 | |
| 2409 | card = bus->card; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2410 | spin_lock(&card->files_lock); |
| 2411 | card->shutdown = 0; |
| 2412 | spin_unlock(&card->files_lock); |
| 2413 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2414 | EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2415 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2416 | /** |
| 2417 | * snd_hda_codec_reset - Clear all objects assigned to the codec |
| 2418 | * @codec: HD-audio codec |
| 2419 | * |
| 2420 | * This frees the all PCM and control elements assigned to the codec, and |
| 2421 | * clears the caches and restores the pin default configurations. |
| 2422 | * |
| 2423 | * When a device is being used, it returns -EBSY. If successfully freed, |
| 2424 | * returns zero. |
| 2425 | */ |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2426 | int snd_hda_codec_reset(struct hda_codec *codec) |
| 2427 | { |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2428 | struct hda_bus *bus = codec->bus; |
| 2429 | struct snd_card *card = bus->card; |
| 2430 | int i; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2431 | |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2432 | if (snd_hda_lock_devices(bus) < 0) |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2433 | return -EBUSY; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2434 | |
| 2435 | /* OK, let it free */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 2436 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2437 | flush_workqueue(bus->workq); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2438 | snd_hda_ctls_clear(codec); |
Geert Uytterhoeven | 83a35e3 | 2013-06-28 11:27:31 +0200 | [diff] [blame] | 2439 | /* release PCMs */ |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2440 | for (i = 0; i < codec->num_pcms; i++) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2441 | if (codec->pcm_info[i].pcm) { |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2442 | snd_device_free(card, codec->pcm_info[i].pcm); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2443 | clear_bit(codec->pcm_info[i].device, |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2444 | bus->pcm_dev_bits); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 2445 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2446 | } |
Takashi Iwai | d604b39 | 2014-02-28 13:42:09 +0100 | [diff] [blame] | 2447 | snd_hda_detach_beep_device(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 2448 | if (device_is_registered(hda_codec_dev(codec))) |
| 2449 | device_del(hda_codec_dev(codec)); |
| 2450 | |
Takashi Iwai | 07dc59f | 2012-09-10 09:39:31 +0200 | [diff] [blame] | 2451 | memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2452 | snd_hda_jack_tbl_clear(codec); |
Takashi Iwai | 56d1771 | 2008-11-28 14:36:23 +0100 | [diff] [blame] | 2453 | codec->proc_widget_hook = NULL; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2454 | codec->spec = NULL; |
| 2455 | free_hda_cache(&codec->amp_cache); |
| 2456 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 827057f | 2008-12-19 10:12:02 +0100 | [diff] [blame] | 2457 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
| 2458 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Takashi Iwai | 346ff70 | 2009-02-23 09:42:57 +0100 | [diff] [blame] | 2459 | /* free only driver_pins so that init_pins + user_pins are restored */ |
| 2460 | snd_array_free(&codec->driver_pins); |
Takashi Iwai | 09a6071 | 2012-06-26 15:01:33 +0200 | [diff] [blame] | 2461 | snd_array_free(&codec->cvt_setups); |
| 2462 | snd_array_free(&codec->spdif_out); |
Takashi Iwai | c9ce6b2 | 2012-12-18 18:12:44 +0100 | [diff] [blame] | 2463 | snd_array_free(&codec->verbs); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2464 | codec->num_pcms = 0; |
| 2465 | codec->pcm_info = NULL; |
| 2466 | codec->preset = NULL; |
Takashi Iwai | d1f1af2 | 2009-03-02 10:35:29 +0100 | [diff] [blame] | 2467 | codec->slave_dig_outs = NULL; |
| 2468 | codec->spdif_status_reset = 0; |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2469 | |
| 2470 | /* allow device access again */ |
Takashi Iwai | d3d020b | 2012-04-26 12:11:44 +0200 | [diff] [blame] | 2471 | snd_hda_unlock_devices(bus); |
Takashi Iwai | a65d629 | 2009-02-23 16:57:04 +0100 | [diff] [blame] | 2472 | return 0; |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2475 | 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] | 2476 | |
| 2477 | /* apply the function to all matching slave ctls in the mixer list */ |
| 2478 | static int map_slaves(struct hda_codec *codec, const char * const *slaves, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2479 | const char *suffix, map_slave_func_t func, void *data) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2480 | { |
| 2481 | struct hda_nid_item *items; |
| 2482 | const char * const *s; |
| 2483 | int i, err; |
| 2484 | |
| 2485 | items = codec->mixers.list; |
| 2486 | for (i = 0; i < codec->mixers.used; i++) { |
| 2487 | struct snd_kcontrol *sctl = items[i].kctl; |
Takashi Iwai | ca16ec0 | 2013-10-28 12:00:35 +0100 | [diff] [blame] | 2488 | if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2489 | continue; |
| 2490 | for (s = slaves; *s; s++) { |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2491 | char tmpname[sizeof(sctl->id.name)]; |
| 2492 | const char *name = *s; |
| 2493 | if (suffix) { |
| 2494 | snprintf(tmpname, sizeof(tmpname), "%s %s", |
| 2495 | name, suffix); |
| 2496 | name = tmpname; |
| 2497 | } |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2498 | if (!strcmp(sctl->id.name, name)) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2499 | err = func(codec, data, sctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2500 | if (err) |
| 2501 | return err; |
| 2502 | break; |
| 2503 | } |
| 2504 | } |
| 2505 | } |
| 2506 | return 0; |
| 2507 | } |
| 2508 | |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2509 | static int check_slave_present(struct hda_codec *codec, |
| 2510 | void *data, struct snd_kcontrol *sctl) |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2511 | { |
| 2512 | return 1; |
| 2513 | } |
| 2514 | |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2515 | /* guess the value corresponding to 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2516 | static int get_kctl_0dB_offset(struct hda_codec *codec, |
| 2517 | struct snd_kcontrol *kctl, int *step_to_check) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2518 | { |
| 2519 | int _tlv[4]; |
| 2520 | const int *tlv = NULL; |
| 2521 | int val = -1; |
| 2522 | |
| 2523 | if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 2524 | /* FIXME: set_fs() hack for obtaining user-space TLV data */ |
| 2525 | mm_segment_t fs = get_fs(); |
| 2526 | set_fs(get_ds()); |
| 2527 | if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) |
| 2528 | tlv = _tlv; |
| 2529 | set_fs(fs); |
| 2530 | } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) |
| 2531 | tlv = kctl->tlv.p; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2532 | if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { |
| 2533 | int step = tlv[3]; |
| 2534 | step &= ~TLV_DB_SCALE_MUTE; |
| 2535 | if (!step) |
| 2536 | return -1; |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2537 | if (*step_to_check && *step_to_check != step) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2538 | codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2539 | - *step_to_check, step); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2540 | return -1; |
| 2541 | } |
| 2542 | *step_to_check = step; |
Takashi Iwai | a4e7a12 | 2013-11-04 15:44:09 +0100 | [diff] [blame] | 2543 | val = -tlv[2] / step; |
| 2544 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2545 | return val; |
| 2546 | } |
| 2547 | |
| 2548 | /* call kctl->put with the given value(s) */ |
| 2549 | static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) |
| 2550 | { |
| 2551 | struct snd_ctl_elem_value *ucontrol; |
| 2552 | ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); |
| 2553 | if (!ucontrol) |
| 2554 | return -ENOMEM; |
| 2555 | ucontrol->value.integer.value[0] = val; |
| 2556 | ucontrol->value.integer.value[1] = val; |
| 2557 | kctl->put(kctl, ucontrol); |
| 2558 | kfree(ucontrol); |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
| 2562 | /* initialize the slave volume with 0dB */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2563 | static int init_slave_0dB(struct hda_codec *codec, |
| 2564 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2565 | { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2566 | int offset = get_kctl_0dB_offset(codec, slave, data); |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2567 | if (offset > 0) |
| 2568 | put_kctl_with_value(slave, offset); |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
| 2572 | /* unmute the slave */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 2573 | static int init_slave_unmute(struct hda_codec *codec, |
| 2574 | void *data, struct snd_kcontrol *slave) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2575 | { |
| 2576 | return put_kctl_with_value(slave, 1); |
| 2577 | } |
| 2578 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2579 | static int add_slave(struct hda_codec *codec, |
| 2580 | void *data, struct snd_kcontrol *slave) |
| 2581 | { |
| 2582 | return snd_ctl_add_slave(data, slave); |
| 2583 | } |
| 2584 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2585 | /** |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2586 | * __snd_hda_add_vmaster - create a virtual master control and add slaves |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2587 | * @codec: HD-audio codec |
| 2588 | * @name: vmaster control name |
| 2589 | * @tlv: TLV data (optional) |
| 2590 | * @slaves: slave control names (optional) |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2591 | * @suffix: suffix string to each slave name (optional) |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2592 | * @init_slave_vol: initialize slaves to unmute/0dB |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2593 | * @ctl_ret: store the vmaster kcontrol in return |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2594 | * |
| 2595 | * Create a virtual master control with the given name. The TLV data |
| 2596 | * must be either NULL or a valid data. |
| 2597 | * |
| 2598 | * @slaves is a NULL-terminated array of strings, each of which is a |
| 2599 | * slave control name. All controls with these names are assigned to |
| 2600 | * the new virtual master control. |
| 2601 | * |
| 2602 | * This function returns zero if successful or a negative error code. |
| 2603 | */ |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2604 | int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2605 | unsigned int *tlv, const char * const *slaves, |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2606 | const char *suffix, bool init_slave_vol, |
| 2607 | struct snd_kcontrol **ctl_ret) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2608 | { |
| 2609 | struct snd_kcontrol *kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2610 | int err; |
| 2611 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2612 | if (ctl_ret) |
| 2613 | *ctl_ret = NULL; |
| 2614 | |
Takashi Iwai | 9322ca5 | 2012-02-03 14:28:01 +0100 | [diff] [blame] | 2615 | err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2616 | if (err != 1) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2617 | codec_dbg(codec, "No slave found for %s\n", name); |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 2618 | return 0; |
| 2619 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2620 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 2621 | if (!kctl) |
| 2622 | return -ENOMEM; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 2623 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2624 | if (err < 0) |
| 2625 | return err; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 2626 | |
Takashi Iwai | e875094 | 2014-06-30 14:02:39 +0200 | [diff] [blame] | 2627 | err = map_slaves(codec, slaves, suffix, add_slave, kctl); |
Takashi Iwai | aeb4b88 | 2011-11-10 12:28:38 +0100 | [diff] [blame] | 2628 | if (err < 0) |
| 2629 | return err; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2630 | |
| 2631 | /* init with master mute & zero volume */ |
| 2632 | put_kctl_with_value(kctl, 0); |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2633 | if (init_slave_vol) { |
| 2634 | int step = 0; |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2635 | map_slaves(codec, slaves, suffix, |
Takashi Iwai | 485e3e0 | 2013-11-04 15:51:00 +0100 | [diff] [blame] | 2636 | tlv ? init_slave_0dB : init_slave_unmute, &step); |
| 2637 | } |
Takashi Iwai | 18478e8 | 2012-03-09 17:51:10 +0100 | [diff] [blame] | 2638 | |
Takashi Iwai | 29e5853 | 2012-03-12 12:25:03 +0100 | [diff] [blame] | 2639 | if (ctl_ret) |
| 2640 | *ctl_ret = kctl; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2641 | return 0; |
| 2642 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2643 | EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 2644 | |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2645 | /* |
| 2646 | * mute-LED control using vmaster |
| 2647 | */ |
| 2648 | static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, |
| 2649 | struct snd_ctl_elem_info *uinfo) |
| 2650 | { |
| 2651 | static const char * const texts[] = { |
David Henningsson | c86c2d4 | 2013-01-03 14:12:29 +0100 | [diff] [blame] | 2652 | "On", "Off", "Follow Master" |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2653 | }; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2654 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 2655 | return snd_ctl_enum_info(uinfo, 1, 3, texts); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, |
| 2659 | struct snd_ctl_elem_value *ucontrol) |
| 2660 | { |
| 2661 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2662 | ucontrol->value.enumerated.item[0] = hook->mute_mode; |
| 2663 | return 0; |
| 2664 | } |
| 2665 | |
| 2666 | static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, |
| 2667 | struct snd_ctl_elem_value *ucontrol) |
| 2668 | { |
| 2669 | struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); |
| 2670 | unsigned int old_mode = hook->mute_mode; |
| 2671 | |
| 2672 | hook->mute_mode = ucontrol->value.enumerated.item[0]; |
| 2673 | if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) |
| 2674 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
| 2675 | if (old_mode == hook->mute_mode) |
| 2676 | return 0; |
| 2677 | snd_hda_sync_vmaster_hook(hook); |
| 2678 | return 1; |
| 2679 | } |
| 2680 | |
| 2681 | static struct snd_kcontrol_new vmaster_mute_mode = { |
| 2682 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2683 | .name = "Mute-LED Mode", |
| 2684 | .info = vmaster_mute_mode_info, |
| 2685 | .get = vmaster_mute_mode_get, |
| 2686 | .put = vmaster_mute_mode_put, |
| 2687 | }; |
| 2688 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2689 | /** |
| 2690 | * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED |
| 2691 | * @codec: the HDA codec |
| 2692 | * @hook: the vmaster hook object |
| 2693 | * @expose_enum_ctl: flag to create an enum ctl |
| 2694 | * |
| 2695 | * Add a mute-LED hook with the given vmaster switch kctl. |
| 2696 | * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically |
| 2697 | * created and associated with the given hook. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2698 | */ |
| 2699 | int snd_hda_add_vmaster_hook(struct hda_codec *codec, |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2700 | struct hda_vmaster_mute_hook *hook, |
| 2701 | bool expose_enum_ctl) |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2702 | { |
| 2703 | struct snd_kcontrol *kctl; |
| 2704 | |
| 2705 | if (!hook->hook || !hook->sw_kctl) |
| 2706 | return 0; |
| 2707 | snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); |
| 2708 | hook->codec = codec; |
| 2709 | hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; |
Takashi Iwai | f29735c | 2012-03-13 07:55:10 +0100 | [diff] [blame] | 2710 | if (!expose_enum_ctl) |
| 2711 | return 0; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2712 | kctl = snd_ctl_new1(&vmaster_mute_mode, hook); |
| 2713 | if (!kctl) |
| 2714 | return -ENOMEM; |
| 2715 | return snd_hda_ctl_add(codec, 0, kctl); |
| 2716 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2717 | EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2718 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 2719 | /** |
| 2720 | * snd_hda_sync_vmaster_hook - Sync vmaster hook |
| 2721 | * @hook: the vmaster hook |
| 2722 | * |
| 2723 | * Call the hook with the current value for synchronization. |
| 2724 | * Should be called in init callback. |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2725 | */ |
| 2726 | void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) |
| 2727 | { |
| 2728 | if (!hook->hook || !hook->codec) |
| 2729 | return; |
Takashi Iwai | 594813f | 2013-04-17 18:16:05 +0200 | [diff] [blame] | 2730 | /* don't call vmaster hook in the destructor since it might have |
| 2731 | * been already destroyed |
| 2732 | */ |
| 2733 | if (hook->codec->bus->shutdown) |
| 2734 | return; |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2735 | switch (hook->mute_mode) { |
| 2736 | case HDA_VMUTE_FOLLOW_MASTER: |
| 2737 | snd_ctl_sync_vmaster_hook(hook->sw_kctl); |
| 2738 | break; |
| 2739 | default: |
| 2740 | hook->hook(hook->codec, hook->mute_mode); |
| 2741 | break; |
| 2742 | } |
| 2743 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2744 | EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); |
Takashi Iwai | d2f344b | 2012-03-12 16:59:58 +0100 | [diff] [blame] | 2745 | |
| 2746 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2747 | /** |
| 2748 | * 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] | 2749 | * @kcontrol: referred ctl element |
| 2750 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2751 | * |
| 2752 | * The control element is supposed to have the private_value field |
| 2753 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2754 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2755 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 2756 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | { |
| 2758 | int chs = get_amp_channels(kcontrol); |
| 2759 | |
| 2760 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2761 | uinfo->count = chs == 3 ? 2 : 1; |
| 2762 | uinfo->value.integer.min = 0; |
| 2763 | uinfo->value.integer.max = 1; |
| 2764 | return 0; |
| 2765 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2766 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2768 | /** |
| 2769 | * 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] | 2770 | * @kcontrol: ctl element |
| 2771 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2772 | * |
| 2773 | * The control element is supposed to have the private_value field |
| 2774 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2775 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2776 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 2777 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | { |
| 2779 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2780 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2781 | int chs = get_amp_channels(kcontrol); |
| 2782 | int dir = get_amp_direction(kcontrol); |
| 2783 | int idx = get_amp_index(kcontrol); |
| 2784 | long *valp = ucontrol->value.integer.value; |
| 2785 | |
| 2786 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2787 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2788 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2790 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 2791 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | return 0; |
| 2793 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2794 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2796 | /** |
| 2797 | * 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] | 2798 | * @kcontrol: ctl element |
| 2799 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2800 | * |
| 2801 | * The control element is supposed to have the private_value field |
| 2802 | * set up via HDA_COMPOSE_AMP_VAL*() or related macros. |
| 2803 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2804 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 2805 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | { |
| 2807 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2808 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 2809 | int chs = get_amp_channels(kcontrol); |
| 2810 | int dir = get_amp_direction(kcontrol); |
| 2811 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | long *valp = ucontrol->value.integer.value; |
| 2813 | int change = 0; |
| 2814 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2815 | if (chs & 1) { |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2816 | change = codec_amp_update(codec, nid, 0, dir, idx, |
| 2817 | HDA_AMP_MUTE, |
| 2818 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2819 | !hda_codec_is_power_on(codec)); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 2820 | valp++; |
| 2821 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 2822 | if (chs & 2) |
Takashi Iwai | 781c7b9 | 2015-02-20 10:26:25 +0100 | [diff] [blame] | 2823 | change |= codec_amp_update(codec, nid, 1, dir, idx, |
| 2824 | HDA_AMP_MUTE, |
| 2825 | *valp ? 0 : HDA_AMP_MUTE, false, |
| 2826 | !hda_codec_is_power_on(codec)); |
Takashi Iwai | 9e5341b | 2010-09-21 09:57:06 +0200 | [diff] [blame] | 2827 | hda_call_check_power_status(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | return change; |
| 2829 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2830 | EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | |
| 2832 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2833 | * bound volume controls |
| 2834 | * |
| 2835 | * bind multiple volumes (# indices, from 0) |
| 2836 | */ |
| 2837 | |
| 2838 | #define AMP_VAL_IDX_SHIFT 19 |
| 2839 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 2840 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2841 | /** |
| 2842 | * 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] | 2843 | * @kcontrol: ctl element |
| 2844 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2845 | * |
| 2846 | * The control element is supposed to have the private_value field |
| 2847 | * set up via HDA_BIND_MUTE*() macros. |
| 2848 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2849 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 2850 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2851 | { |
| 2852 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2853 | unsigned long pval; |
| 2854 | int err; |
| 2855 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2856 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2857 | pval = kcontrol->private_value; |
| 2858 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 2859 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 2860 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2861 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2862 | return err; |
| 2863 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2864 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2865 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2866 | /** |
| 2867 | * 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] | 2868 | * @kcontrol: ctl element |
| 2869 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2870 | * |
| 2871 | * The control element is supposed to have the private_value field |
| 2872 | * set up via HDA_BIND_MUTE*() macros. |
| 2873 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2874 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 2875 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2876 | { |
| 2877 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2878 | unsigned long pval; |
| 2879 | int i, indices, err = 0, change = 0; |
| 2880 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2881 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2882 | pval = kcontrol->private_value; |
| 2883 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 2884 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2885 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 2886 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2887 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 2888 | if (err < 0) |
| 2889 | break; |
| 2890 | change |= err; |
| 2891 | } |
| 2892 | kcontrol->private_value = pval; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2893 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2894 | return err < 0 ? err : change; |
| 2895 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2896 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 2897 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2898 | /** |
| 2899 | * 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] | 2900 | * @kcontrol: referred ctl element |
| 2901 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2902 | * |
| 2903 | * The control element is supposed to have the private_value field |
| 2904 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2905 | */ |
| 2906 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 2907 | struct snd_ctl_elem_info *uinfo) |
| 2908 | { |
| 2909 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2910 | struct hda_bind_ctls *c; |
| 2911 | int err; |
| 2912 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2913 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2914 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2915 | kcontrol->private_value = *c->values; |
| 2916 | err = c->ops->info(kcontrol, uinfo); |
| 2917 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2918 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2919 | return err; |
| 2920 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2921 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2922 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2923 | /** |
| 2924 | * 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] | 2925 | * @kcontrol: ctl element |
| 2926 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2927 | * |
| 2928 | * The control element is supposed to have the private_value field |
| 2929 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2930 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2931 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 2932 | struct snd_ctl_elem_value *ucontrol) |
| 2933 | { |
| 2934 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2935 | struct hda_bind_ctls *c; |
| 2936 | int err; |
| 2937 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2938 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2939 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2940 | kcontrol->private_value = *c->values; |
| 2941 | err = c->ops->get(kcontrol, ucontrol); |
| 2942 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2943 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2944 | return err; |
| 2945 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2946 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2947 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2948 | /** |
| 2949 | * 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] | 2950 | * @kcontrol: ctl element |
| 2951 | * @ucontrol: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2952 | * |
| 2953 | * The control element is supposed to have the private_value field |
| 2954 | * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. |
| 2955 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2956 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 2957 | struct snd_ctl_elem_value *ucontrol) |
| 2958 | { |
| 2959 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2960 | struct hda_bind_ctls *c; |
| 2961 | unsigned long *vals; |
| 2962 | int err = 0, change = 0; |
| 2963 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2964 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2965 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2966 | for (vals = c->values; *vals; vals++) { |
| 2967 | kcontrol->private_value = *vals; |
| 2968 | err = c->ops->put(kcontrol, ucontrol); |
| 2969 | if (err < 0) |
| 2970 | break; |
| 2971 | change |= err; |
| 2972 | } |
| 2973 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2974 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2975 | return err < 0 ? err : change; |
| 2976 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 2977 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2978 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2979 | /** |
| 2980 | * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 2981 | * @kcontrol: ctl element |
| 2982 | * @op_flag: operation flag |
| 2983 | * @size: byte size of input TLV |
| 2984 | * @tlv: TLV data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 2985 | * |
| 2986 | * The control element is supposed to have the private_value field |
| 2987 | * set up via HDA_BIND_VOL() macro. |
| 2988 | */ |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2989 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 2990 | unsigned int size, unsigned int __user *tlv) |
| 2991 | { |
| 2992 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2993 | struct hda_bind_ctls *c; |
| 2994 | int err; |
| 2995 | |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 2996 | mutex_lock(&codec->control_mutex); |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 2997 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 2998 | kcontrol->private_value = *c->values; |
| 2999 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 3000 | kcontrol->private_value = (long)c; |
Wu Fengguang | 5a9e02e | 2009-01-09 16:45:24 +0800 | [diff] [blame] | 3001 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3002 | return err; |
| 3003 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3004 | EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3005 | |
| 3006 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 3007 | .info = snd_hda_mixer_amp_volume_info, |
| 3008 | .get = snd_hda_mixer_amp_volume_get, |
| 3009 | .put = snd_hda_mixer_amp_volume_put, |
| 3010 | .tlv = snd_hda_mixer_amp_tlv |
| 3011 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3012 | EXPORT_SYMBOL_GPL(snd_hda_bind_vol); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3013 | |
| 3014 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 3015 | .info = snd_hda_mixer_amp_switch_info, |
| 3016 | .get = snd_hda_mixer_amp_switch_get, |
| 3017 | .put = snd_hda_mixer_amp_switch_put, |
| 3018 | .tlv = snd_hda_mixer_amp_tlv |
| 3019 | }; |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3020 | EXPORT_SYMBOL_GPL(snd_hda_bind_sw); |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 3021 | |
| 3022 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | * SPDIF out controls |
| 3024 | */ |
| 3025 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3026 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 3027 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | { |
| 3029 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 3030 | uinfo->count = 1; |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3034 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 3035 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | { |
| 3037 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3038 | IEC958_AES0_NONAUDIO | |
| 3039 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 3040 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 3041 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 3042 | IEC958_AES1_CON_ORIGINAL; |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3046 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 3047 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | { |
| 3049 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 3050 | IEC958_AES0_NONAUDIO | |
| 3051 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3055 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 3056 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
| 3058 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3059 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3060 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3062 | mutex_lock(&codec->spdif_mutex); |
| 3063 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3064 | ucontrol->value.iec958.status[0] = spdif->status & 0xff; |
| 3065 | ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; |
| 3066 | ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; |
| 3067 | ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3068 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
| 3073 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 3074 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 3075 | */ |
| 3076 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 3077 | { |
| 3078 | unsigned short val = 0; |
| 3079 | |
| 3080 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3081 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3083 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3085 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 3086 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 3087 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3089 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 3090 | IEC958_AES0_CON_EMPHASIS_5015) |
| 3091 | val |= AC_DIG1_EMPHASIS; |
| 3092 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 3093 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3095 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 3097 | } |
| 3098 | return val; |
| 3099 | } |
| 3100 | |
| 3101 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 3102 | */ |
| 3103 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 3104 | { |
| 3105 | unsigned int sbits = 0; |
| 3106 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3107 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3109 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 3111 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | a686fd1 | 2013-03-20 15:42:00 +0100 | [diff] [blame] | 3112 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 3114 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3115 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3117 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3119 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 3121 | sbits |= val & (0x7f << 8); |
| 3122 | } |
| 3123 | return sbits; |
| 3124 | } |
| 3125 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3126 | /* set digital convert verbs both for the given NID and its slaves */ |
| 3127 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 3128 | int verb, int val) |
| 3129 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 3130 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3131 | |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3132 | snd_hda_codec_write_cache(codec, nid, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3133 | d = codec->slave_dig_outs; |
| 3134 | if (!d) |
| 3135 | return; |
| 3136 | for (; *d; d++) |
Takashi Iwai | 9e97697 | 2008-11-25 08:17:20 +0100 | [diff] [blame] | 3137 | snd_hda_codec_write_cache(codec, *d, 0, verb, val); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 3141 | int dig1, int dig2) |
| 3142 | { |
| 3143 | if (dig1 != -1) |
| 3144 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 3145 | if (dig2 != -1) |
| 3146 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 3147 | } |
| 3148 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3149 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 3150 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | { |
| 3152 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3153 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3154 | struct hda_spdif_out *spdif; |
| 3155 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | unsigned short val; |
| 3157 | int change; |
| 3158 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3159 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3160 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3161 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3162 | spdif->status = ucontrol->value.iec958.status[0] | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 3164 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 3165 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3166 | val = convert_from_spdif_status(spdif->status); |
| 3167 | val |= spdif->ctls & 1; |
| 3168 | change = spdif->ctls != val; |
| 3169 | spdif->ctls = val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3170 | if (change && nid != (u16)-1) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 3171 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3172 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | return change; |
| 3174 | } |
| 3175 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 3176 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3178 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 3179 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | { |
| 3181 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3182 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3183 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3185 | mutex_lock(&codec->spdif_mutex); |
| 3186 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3187 | ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3188 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | return 0; |
| 3190 | } |
| 3191 | |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3192 | static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, |
| 3193 | int dig1, int dig2) |
| 3194 | { |
| 3195 | set_dig_out_convert(codec, nid, dig1, dig2); |
| 3196 | /* unmute amp switch (if any) */ |
| 3197 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 3198 | (dig1 & AC_DIG1_ENABLE)) |
| 3199 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 3200 | HDA_AMP_MUTE, 0); |
| 3201 | } |
| 3202 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3203 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 3204 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | { |
| 3206 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3207 | int idx = kcontrol->private_value; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3208 | struct hda_spdif_out *spdif; |
| 3209 | hda_nid_t nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | unsigned short val; |
| 3211 | int change; |
| 3212 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3213 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3214 | spdif = snd_array_elem(&codec->spdif_out, idx); |
| 3215 | nid = spdif->nid; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3216 | val = spdif->ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3218 | val |= AC_DIG1_ENABLE; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3219 | change = spdif->ctls != val; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3220 | spdif->ctls = val; |
| 3221 | if (change && nid != (u16)-1) |
| 3222 | set_spdif_ctls(codec, nid, val & 0xff, -1); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3223 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | return change; |
| 3225 | } |
| 3226 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3227 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | { |
| 3229 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3230 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3231 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | .info = snd_hda_spdif_mask_info, |
| 3233 | .get = snd_hda_spdif_cmask_get, |
| 3234 | }, |
| 3235 | { |
| 3236 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3237 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3238 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | .info = snd_hda_spdif_mask_info, |
| 3240 | .get = snd_hda_spdif_pmask_get, |
| 3241 | }, |
| 3242 | { |
| 3243 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3244 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | .info = snd_hda_spdif_mask_info, |
| 3246 | .get = snd_hda_spdif_default_get, |
| 3247 | .put = snd_hda_spdif_default_put, |
| 3248 | }, |
| 3249 | { |
| 3250 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3251 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | .info = snd_hda_spdif_out_switch_info, |
| 3253 | .get = snd_hda_spdif_out_switch_get, |
| 3254 | .put = snd_hda_spdif_out_switch_put, |
| 3255 | }, |
| 3256 | { } /* end */ |
| 3257 | }; |
| 3258 | |
| 3259 | /** |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3260 | * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | * @codec: the HDA codec |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3262 | * @associated_nid: NID that new ctls associated with |
| 3263 | * @cvt_nid: converter NID |
| 3264 | * @type: HDA_PCM_TYPE_* |
| 3265 | * Creates controls related with the digital output. |
| 3266 | * Called from each patch supporting the digital out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | * |
| 3268 | * Returns 0 if successful, or a negative error code. |
| 3269 | */ |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3270 | int snd_hda_create_dig_out_ctls(struct hda_codec *codec, |
| 3271 | hda_nid_t associated_nid, |
| 3272 | hda_nid_t cvt_nid, |
| 3273 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | { |
| 3275 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3276 | struct snd_kcontrol *kctl; |
| 3277 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3278 | int idx = 0; |
| 3279 | const int spdif_index = 16; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3280 | struct hda_spdif_out *spdif; |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3281 | struct hda_bus *bus = codec->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3283 | if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3284 | type == HDA_PCM_TYPE_SPDIF) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3285 | idx = spdif_index; |
| 3286 | } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3287 | type == HDA_PCM_TYPE_HDMI) { |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3288 | /* suppose a single SPDIF device */ |
| 3289 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3290 | kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); |
| 3291 | if (!kctl) |
| 3292 | break; |
| 3293 | kctl->id.index = spdif_index; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3294 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3295 | bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3296 | } |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3297 | if (!bus->primary_dig_out_type) |
| 3298 | bus->primary_dig_out_type = type; |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3299 | |
Takashi Iwai | ea9b43a | 2013-02-12 17:02:41 +0100 | [diff] [blame] | 3300 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3301 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3302 | codec_err(codec, "too many IEC958 outputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3303 | return -EBUSY; |
| 3304 | } |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3305 | spdif = snd_array_new(&codec->spdif_out); |
Mengdong Lin | 25336e8 | 2013-03-07 14:10:25 -0500 | [diff] [blame] | 3306 | if (!spdif) |
| 3307 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 3309 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 3310 | if (!kctl) |
| 3311 | return -ENOMEM; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3312 | kctl->id.index = idx; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3313 | kctl->private_value = codec->spdif_out.used - 1; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3314 | err = snd_hda_ctl_add(codec, associated_nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3315 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | return err; |
| 3317 | } |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3318 | spdif->nid = cvt_nid; |
| 3319 | spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3320 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
| 3321 | spdif->status = convert_to_spdif_status(spdif->ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | return 0; |
| 3323 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3324 | EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3326 | /** |
| 3327 | * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID |
| 3328 | * @codec: the HDA codec |
| 3329 | * @nid: widget NID |
| 3330 | * |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3331 | * call within spdif_mutex lock |
| 3332 | */ |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3333 | struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, |
| 3334 | hda_nid_t nid) |
| 3335 | { |
| 3336 | int i; |
| 3337 | for (i = 0; i < codec->spdif_out.used; i++) { |
| 3338 | struct hda_spdif_out *spdif = |
| 3339 | snd_array_elem(&codec->spdif_out, i); |
| 3340 | if (spdif->nid == nid) |
| 3341 | return spdif; |
| 3342 | } |
| 3343 | return NULL; |
| 3344 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3345 | EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 3346 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3347 | /** |
| 3348 | * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl |
| 3349 | * @codec: the HDA codec |
| 3350 | * @idx: the SPDIF ctl index |
| 3351 | * |
| 3352 | * Unassign the widget from the given SPDIF control. |
| 3353 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3354 | void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) |
| 3355 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3356 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3357 | |
| 3358 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3359 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3360 | spdif->nid = (u16)-1; |
| 3361 | mutex_unlock(&codec->spdif_mutex); |
| 3362 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3363 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3364 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3365 | /** |
| 3366 | * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID |
| 3367 | * @codec: the HDA codec |
| 3368 | * @idx: the SPDIF ctl idx |
| 3369 | * @nid: widget NID |
| 3370 | * |
| 3371 | * Assign the widget to the SPDIF control with the given index. |
| 3372 | */ |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3373 | void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) |
| 3374 | { |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3375 | struct hda_spdif_out *spdif; |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3376 | unsigned short val; |
| 3377 | |
| 3378 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 3379 | spdif = snd_array_elem(&codec->spdif_out, idx); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3380 | if (spdif->nid != nid) { |
| 3381 | spdif->nid = nid; |
| 3382 | val = spdif->ctls; |
| 3383 | set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); |
| 3384 | } |
| 3385 | mutex_unlock(&codec->spdif_mutex); |
| 3386 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3387 | EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); |
Stephen Warren | 74b654c | 2011-06-01 11:14:18 -0600 | [diff] [blame] | 3388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3390 | * SPDIF sharing with analog output |
| 3391 | */ |
| 3392 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 3393 | struct snd_ctl_elem_value *ucontrol) |
| 3394 | { |
| 3395 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3396 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 3397 | return 0; |
| 3398 | } |
| 3399 | |
| 3400 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 3401 | struct snd_ctl_elem_value *ucontrol) |
| 3402 | { |
| 3403 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 3404 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 3405 | return 0; |
| 3406 | } |
| 3407 | |
| 3408 | static struct snd_kcontrol_new spdif_share_sw = { |
| 3409 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3410 | .name = "IEC958 Default PCM Playback Switch", |
| 3411 | .info = snd_ctl_boolean_mono_info, |
| 3412 | .get = spdif_share_sw_get, |
| 3413 | .put = spdif_share_sw_put, |
| 3414 | }; |
| 3415 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3416 | /** |
| 3417 | * snd_hda_create_spdif_share_sw - create Default PCM switch |
| 3418 | * @codec: the HDA codec |
| 3419 | * @mout: multi-out instance |
| 3420 | */ |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3421 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 3422 | struct hda_multi_out *mout) |
| 3423 | { |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3424 | struct snd_kcontrol *kctl; |
| 3425 | |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3426 | if (!mout->dig_out_nid) |
| 3427 | return 0; |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3428 | |
| 3429 | kctl = snd_ctl_new1(&spdif_share_sw, mout); |
| 3430 | if (!kctl) |
| 3431 | return -ENOMEM; |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3432 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
Mengdong Lin | 4c7a548 | 2013-03-07 14:11:05 -0500 | [diff] [blame] | 3433 | return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3434 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3435 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 3436 | |
| 3437 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | * SPDIF input |
| 3439 | */ |
| 3440 | |
| 3441 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 3442 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3443 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 3444 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | { |
| 3446 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3447 | |
| 3448 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 3449 | return 0; |
| 3450 | } |
| 3451 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3452 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 3453 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | { |
| 3455 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3456 | hda_nid_t nid = kcontrol->private_value; |
| 3457 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 3458 | int change; |
| 3459 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3460 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3462 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3464 | snd_hda_codec_write_cache(codec, nid, 0, |
| 3465 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 3467 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | return change; |
| 3469 | } |
| 3470 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3471 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 3472 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | { |
| 3474 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3475 | hda_nid_t nid = kcontrol->private_value; |
| 3476 | unsigned short val; |
| 3477 | unsigned int sbits; |
| 3478 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3479 | 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] | 3480 | sbits = convert_to_spdif_status(val); |
| 3481 | ucontrol->value.iec958.status[0] = sbits; |
| 3482 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 3483 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 3484 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 3485 | return 0; |
| 3486 | } |
| 3487 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3488 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | { |
| 3490 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3491 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | .info = snd_hda_spdif_in_switch_info, |
| 3493 | .get = snd_hda_spdif_in_switch_get, |
| 3494 | .put = snd_hda_spdif_in_switch_put, |
| 3495 | }, |
| 3496 | { |
| 3497 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 3498 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 3499 | .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | .info = snd_hda_spdif_mask_info, |
| 3501 | .get = snd_hda_spdif_in_status_get, |
| 3502 | }, |
| 3503 | { } /* end */ |
| 3504 | }; |
| 3505 | |
| 3506 | /** |
| 3507 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 3508 | * @codec: the HDA codec |
| 3509 | * @nid: audio in widget NID |
| 3510 | * |
| 3511 | * Creates controls related with the SPDIF input. |
| 3512 | * Called from each patch supporting the SPDIF in. |
| 3513 | * |
| 3514 | * Returns 0 if successful, or a negative error code. |
| 3515 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 3516 | 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] | 3517 | { |
| 3518 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 3519 | struct snd_kcontrol *kctl; |
| 3520 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3521 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 3523 | idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 3524 | if (idx < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3525 | codec_err(codec, "too many IEC958 inputs\n"); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 3526 | return -EBUSY; |
| 3527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 3529 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | c8dcdf8 | 2009-02-06 16:21:20 +0100 | [diff] [blame] | 3530 | if (!kctl) |
| 3531 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | kctl->private_value = nid; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 3533 | err = snd_hda_ctl_add(codec, nid, kctl); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3534 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | return err; |
| 3536 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3537 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 3538 | snd_hda_codec_read(codec, nid, 0, |
| 3539 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3540 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | return 0; |
| 3542 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3543 | EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 3545 | /* |
| 3546 | * command cache |
| 3547 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3549 | /* 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] | 3550 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 3551 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 3552 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 3553 | |
| 3554 | /** |
| 3555 | * snd_hda_codec_write_cache - send a single command with caching |
| 3556 | * @codec: the HDA codec |
| 3557 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3558 | * @flags: optional bit flags |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3559 | * @verb: the verb to send |
| 3560 | * @parm: the parameter for the verb |
| 3561 | * |
| 3562 | * Send a single command without waiting for response. |
| 3563 | * |
| 3564 | * Returns 0 if successful, or a negative error code. |
| 3565 | */ |
| 3566 | 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] | 3567 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3568 | { |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3569 | int err; |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3570 | struct hda_cache_head *c; |
| 3571 | u32 key; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3572 | unsigned int cache_only; |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 3573 | |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3574 | cache_only = codec->cached_write; |
| 3575 | if (!cache_only) { |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3576 | err = snd_hda_codec_write(codec, nid, flags, verb, parm); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3577 | if (err < 0) |
| 3578 | return err; |
| 3579 | } |
| 3580 | |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3581 | /* parm may contain the verb stuff for get/set amp */ |
| 3582 | verb = verb | (parm >> 8); |
| 3583 | parm &= 0xff; |
| 3584 | key = build_cmd_cache_key(nid, verb); |
| 3585 | mutex_lock(&codec->bus->cmd_mutex); |
| 3586 | c = get_alloc_hash(&codec->cmd_cache, key); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3587 | if (c) { |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3588 | c->val = parm; |
Takashi Iwai | de1e37b | 2012-12-20 11:00:21 +0100 | [diff] [blame] | 3589 | c->dirty = cache_only; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3590 | } |
Takashi Iwai | aa2936f | 2009-05-26 16:07:57 +0200 | [diff] [blame] | 3591 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3592 | return 0; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3593 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3594 | EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3595 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3596 | /** |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3597 | * snd_hda_codec_update_cache - check cache and write the cmd only when needed |
| 3598 | * @codec: the HDA codec |
| 3599 | * @nid: NID to send the command |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3600 | * @flags: optional bit flags |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3601 | * @verb: the verb to send |
| 3602 | * @parm: the parameter for the verb |
| 3603 | * |
| 3604 | * This function works like snd_hda_codec_write_cache(), but it doesn't send |
| 3605 | * command if the parameter is already identical with the cached value. |
| 3606 | * If not, it sends the command and refreshes the cache. |
| 3607 | * |
| 3608 | * Returns 0 if successful, or a negative error code. |
| 3609 | */ |
| 3610 | 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] | 3611 | int flags, unsigned int verb, unsigned int parm) |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3612 | { |
| 3613 | struct hda_cache_head *c; |
| 3614 | u32 key; |
| 3615 | |
| 3616 | /* parm may contain the verb stuff for get/set amp */ |
| 3617 | verb = verb | (parm >> 8); |
| 3618 | parm &= 0xff; |
| 3619 | key = build_cmd_cache_key(nid, verb); |
| 3620 | mutex_lock(&codec->bus->cmd_mutex); |
| 3621 | c = get_hash(&codec->cmd_cache, key); |
| 3622 | if (c && c->val == parm) { |
| 3623 | mutex_unlock(&codec->bus->cmd_mutex); |
| 3624 | return 0; |
| 3625 | } |
| 3626 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | e7ecc27 | 2013-06-06 14:00:23 +0200 | [diff] [blame] | 3627 | return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3628 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3629 | EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); |
Takashi Iwai | a68d5a5 | 2010-03-30 18:03:44 +0200 | [diff] [blame] | 3630 | |
| 3631 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 3632 | * snd_hda_codec_resume_cache - Resume the all commands from the cache |
| 3633 | * @codec: HD-audio codec |
| 3634 | * |
| 3635 | * Execute all verbs recorded in the command caches to resume. |
| 3636 | */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3637 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 3638 | { |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3639 | int i; |
| 3640 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3641 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | aa88a35 | 2012-12-20 11:02:00 +0100 | [diff] [blame] | 3642 | codec->cached_write = 0; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3643 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3644 | struct hda_cache_head *buffer; |
| 3645 | u32 key; |
| 3646 | |
| 3647 | buffer = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3648 | key = buffer->key; |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3649 | if (!key) |
| 3650 | continue; |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3651 | if (!buffer->dirty) |
| 3652 | continue; |
| 3653 | buffer->dirty = 0; |
| 3654 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3655 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 3656 | get_cmd_cache_cmd(key), buffer->val); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3657 | mutex_lock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3658 | } |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3659 | mutex_unlock(&codec->hash_mutex); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3660 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3661 | EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3662 | |
| 3663 | /** |
| 3664 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 3665 | * @codec: the HDA codec |
| 3666 | * @seq: VERB array to send |
| 3667 | * |
| 3668 | * Send the commands sequentially from the given array. |
| 3669 | * Thte commands are recorded on cache for power-save and resume. |
| 3670 | * The array must be terminated with NID=0. |
| 3671 | */ |
| 3672 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 3673 | const struct hda_verb *seq) |
| 3674 | { |
| 3675 | for (; seq->nid; seq++) |
| 3676 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 3677 | seq->param); |
| 3678 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3679 | EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 3680 | |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3681 | /** |
| 3682 | * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs |
| 3683 | * @codec: HD-audio codec |
| 3684 | */ |
| 3685 | void snd_hda_codec_flush_cache(struct hda_codec *codec) |
| 3686 | { |
| 3687 | snd_hda_codec_resume_amp(codec); |
| 3688 | snd_hda_codec_resume_cache(codec); |
| 3689 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3690 | EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); |
Takashi Iwai | dc870f3 | 2013-01-22 15:24:30 +0100 | [diff] [blame] | 3691 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3692 | /** |
| 3693 | * snd_hda_codec_set_power_to_all - Set the power state to all widgets |
| 3694 | * @codec: the HDA codec |
| 3695 | * @fg: function group (not used now) |
| 3696 | * @power_state: the power state to set (AC_PWRST_*) |
| 3697 | * |
| 3698 | * Set the given power state to all widgets that have the power control. |
| 3699 | * If the codec has power_filter set, it evaluates the power state and |
| 3700 | * filter out if it's unchanged as D3. |
| 3701 | */ |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3702 | 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] | 3703 | unsigned int power_state) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3704 | { |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3705 | hda_nid_t nid = codec->start_nid; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3706 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3707 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3708 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 3709 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3710 | unsigned int state = power_state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3711 | if (!(wcaps & AC_WCAP_POWER)) |
| 3712 | continue; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3713 | if (codec->power_filter) { |
| 3714 | state = codec->power_filter(codec, nid, power_state); |
| 3715 | if (state != power_state && power_state == AC_PWRST_D3) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3716 | continue; |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 3717 | } |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3718 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3719 | state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3720 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3721 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3722 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3723 | |
| 3724 | /* |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3725 | * supported power states check |
| 3726 | */ |
| 3727 | static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, |
| 3728 | unsigned int power_state) |
| 3729 | { |
| 3730 | int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); |
| 3731 | |
Mengdong Lin | e037cb4 | 2012-08-10 14:11:58 +0200 | [diff] [blame] | 3732 | if (sup == -1) |
Wang Xingchao | 0c7f46a | 2012-06-06 22:02:48 +0800 | [diff] [blame] | 3733 | return false; |
| 3734 | if (sup & power_state) |
| 3735 | return true; |
| 3736 | else |
| 3737 | return false; |
| 3738 | } |
| 3739 | |
| 3740 | /* |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3741 | * wait until the state is reached, returns the current state |
| 3742 | */ |
| 3743 | static unsigned int hda_sync_power_state(struct hda_codec *codec, |
| 3744 | hda_nid_t fg, |
| 3745 | unsigned int power_state) |
| 3746 | { |
| 3747 | unsigned long end_time = jiffies + msecs_to_jiffies(500); |
| 3748 | unsigned int state, actual_state; |
| 3749 | |
| 3750 | for (;;) { |
| 3751 | state = snd_hda_codec_read(codec, fg, 0, |
| 3752 | AC_VERB_GET_POWER_STATE, 0); |
| 3753 | if (state & AC_PWRST_ERROR) |
| 3754 | break; |
| 3755 | actual_state = (state >> 4) & 0x0f; |
| 3756 | if (actual_state == power_state) |
| 3757 | break; |
| 3758 | if (time_after_eq(jiffies, end_time)) |
| 3759 | break; |
| 3760 | /* wait until the codec reachs to the target state */ |
| 3761 | msleep(1); |
| 3762 | } |
| 3763 | return state; |
| 3764 | } |
| 3765 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 3766 | /** |
| 3767 | * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD |
| 3768 | * @codec: the HDA codec |
| 3769 | * @nid: widget NID |
| 3770 | * @power_state: power state to evalue |
| 3771 | * |
| 3772 | * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. |
| 3773 | * This can be used a codec power_filter callback. |
| 3774 | */ |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3775 | unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, |
| 3776 | hda_nid_t nid, |
| 3777 | unsigned int power_state) |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3778 | { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3779 | if (nid == codec->afg || nid == codec->mfg) |
| 3780 | return power_state; |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3781 | if (power_state == AC_PWRST_D3 && |
| 3782 | get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && |
| 3783 | (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { |
| 3784 | int eapd = snd_hda_codec_read(codec, nid, 0, |
| 3785 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 3786 | if (eapd & 0x02) |
| 3787 | return AC_PWRST_D0; |
| 3788 | } |
| 3789 | return power_state; |
| 3790 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 3791 | EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3792 | |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3793 | /* |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3794 | * set power state of the codec, and return the power state |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3795 | */ |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3796 | static unsigned int hda_set_power_state(struct hda_codec *codec, |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3797 | unsigned int power_state) |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3798 | { |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3799 | hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3800 | int count; |
| 3801 | unsigned int state; |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3802 | int flags = 0; |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3803 | |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3804 | /* this delay seems necessary to avoid click noise at power-down */ |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3805 | if (power_state == AC_PWRST_D3) { |
Mengdong Lin | 7f13292 | 2013-11-29 01:48:45 -0500 | [diff] [blame] | 3806 | if (codec->depop_delay < 0) |
| 3807 | msleep(codec->epss ? 10 : 100); |
| 3808 | else if (codec->depop_delay > 0) |
| 3809 | msleep(codec->depop_delay); |
Takashi Iwai | 63e51fd | 2013-06-06 14:20:19 +0200 | [diff] [blame] | 3810 | flags = HDA_RW_NO_RESPONSE_FALLBACK; |
Wang Xingchao | 0f4ccbb | 2012-06-07 16:51:33 +0800 | [diff] [blame] | 3811 | } |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3812 | |
| 3813 | /* repeat power states setting at most 10 times*/ |
| 3814 | for (count = 0; count < 10; count++) { |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3815 | if (codec->patch_ops.set_power_state) |
| 3816 | codec->patch_ops.set_power_state(codec, fg, |
| 3817 | power_state); |
| 3818 | else { |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 3819 | state = power_state; |
| 3820 | if (codec->power_filter) |
| 3821 | state = codec->power_filter(codec, fg, state); |
| 3822 | if (state == power_state || power_state != AC_PWRST_D3) |
| 3823 | snd_hda_codec_read(codec, fg, flags, |
| 3824 | AC_VERB_SET_POWER_STATE, |
| 3825 | state); |
Takashi Iwai | 9419ab6 | 2013-01-24 17:23:35 +0100 | [diff] [blame] | 3826 | snd_hda_codec_set_power_to_all(codec, fg, power_state); |
Takashi Iwai | 432c641 | 2012-08-28 09:59:20 -0700 | [diff] [blame] | 3827 | } |
| 3828 | state = hda_sync_power_state(codec, fg, power_state); |
Wang Xingchao | 09617ce | 2012-06-08 10:26:08 +0800 | [diff] [blame] | 3829 | if (!(state & AC_PWRST_ERROR)) |
| 3830 | break; |
| 3831 | } |
Mengdong Lin | b8dfc462 | 2012-08-23 17:32:30 +0800 | [diff] [blame] | 3832 | |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3833 | return state; |
Takashi Iwai | 4d7fbdb | 2011-07-26 10:33:10 +0200 | [diff] [blame] | 3834 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3835 | |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3836 | /* sync power states of all widgets; |
| 3837 | * this is called at the end of codec parsing |
| 3838 | */ |
| 3839 | static void sync_power_up_states(struct hda_codec *codec) |
| 3840 | { |
| 3841 | hda_nid_t nid = codec->start_nid; |
| 3842 | int i; |
| 3843 | |
Takashi Iwai | ba615b8 | 2013-03-13 14:47:21 +0100 | [diff] [blame] | 3844 | /* don't care if no filter is used */ |
| 3845 | if (!codec->power_filter) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3846 | return; |
| 3847 | |
| 3848 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 3849 | unsigned int wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3850 | unsigned int target; |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3851 | if (!(wcaps & AC_WCAP_POWER)) |
| 3852 | continue; |
| 3853 | target = codec->power_filter(codec, nid, AC_PWRST_D0); |
| 3854 | if (target == AC_PWRST_D0) |
| 3855 | continue; |
Takashi Iwai | 9040d10 | 2013-01-24 17:47:17 +0100 | [diff] [blame] | 3856 | if (!snd_hda_check_power_state(codec, nid, target)) |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 3857 | snd_hda_codec_write(codec, nid, 0, |
| 3858 | AC_VERB_SET_POWER_STATE, target); |
| 3859 | } |
| 3860 | } |
| 3861 | |
Takashi Iwai | 648a8d2 | 2014-02-25 10:38:13 +0100 | [diff] [blame] | 3862 | #ifdef CONFIG_SND_HDA_RECONFIG |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3863 | /* execute additional init verbs */ |
| 3864 | static void hda_exec_init_verbs(struct hda_codec *codec) |
| 3865 | { |
| 3866 | if (codec->init_verbs.list) |
| 3867 | snd_hda_sequence_write(codec, codec->init_verbs.list); |
| 3868 | } |
| 3869 | #else |
| 3870 | static inline void hda_exec_init_verbs(struct hda_codec *codec) {} |
| 3871 | #endif |
| 3872 | |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3873 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3874 | /* update the power on/off account with the current jiffies */ |
| 3875 | static void update_power_acct(struct hda_codec *codec, bool on) |
| 3876 | { |
| 3877 | unsigned long delta = jiffies - codec->power_jiffies; |
| 3878 | |
| 3879 | if (on) |
| 3880 | codec->power_on_acct += delta; |
| 3881 | else |
| 3882 | codec->power_off_acct += delta; |
| 3883 | codec->power_jiffies += delta; |
| 3884 | } |
| 3885 | |
| 3886 | void snd_hda_update_power_acct(struct hda_codec *codec) |
| 3887 | { |
| 3888 | update_power_acct(codec, hda_codec_is_power_on(codec)); |
| 3889 | } |
| 3890 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3891 | /* |
| 3892 | * call suspend and power-down; used both from PM and power-save |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3893 | * this function returns the power state in the end |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3894 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3895 | static unsigned int hda_call_codec_suspend(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3896 | { |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3897 | unsigned int state; |
| 3898 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3899 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3900 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3901 | if (codec->patch_ops.suspend) |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 3902 | codec->patch_ops.suspend(codec); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 3903 | hda_cleanup_all_streams(codec); |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3904 | state = hda_set_power_state(codec, AC_PWRST_D3); |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 3905 | trace_hda_power_down(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3906 | update_power_acct(codec, true); |
| 3907 | atomic_dec(&codec->in_pm); |
Takashi Iwai | 08fa20a | 2012-08-31 07:46:56 -0700 | [diff] [blame] | 3908 | return state; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3909 | } |
| 3910 | |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3911 | /* mark all entries of cmd and amp caches dirty */ |
| 3912 | static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) |
| 3913 | { |
| 3914 | int i; |
| 3915 | for (i = 0; i < codec->cmd_cache.buf.used; i++) { |
| 3916 | struct hda_cache_head *cmd; |
| 3917 | cmd = snd_array_elem(&codec->cmd_cache.buf, i); |
| 3918 | cmd->dirty = 1; |
| 3919 | } |
| 3920 | for (i = 0; i < codec->amp_cache.buf.used; i++) { |
| 3921 | struct hda_amp_info *amp; |
David Henningsson | f038fca | 2013-01-15 15:27:19 +0100 | [diff] [blame] | 3922 | amp = snd_array_elem(&codec->amp_cache.buf, i); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3923 | amp->head.dirty = 1; |
| 3924 | } |
| 3925 | } |
| 3926 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3927 | /* |
| 3928 | * kick up codec; used both from PM and power-save |
| 3929 | */ |
| 3930 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 3931 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3932 | atomic_inc(&codec->in_pm); |
Takashi Iwai | 989c318 | 2012-11-19 14:14:58 +0100 | [diff] [blame] | 3933 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3934 | trace_hda_power_up(codec); |
Takashi Iwai | c370dd6 | 2012-12-13 18:30:04 +0100 | [diff] [blame] | 3935 | hda_mark_cmd_cache_dirty(codec); |
| 3936 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3937 | codec->power_jiffies = jiffies; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3938 | |
Takashi Iwai | d819387 | 2012-08-31 07:54:38 -0700 | [diff] [blame] | 3939 | hda_set_power_state(codec, AC_PWRST_D0); |
Takashi Iwai | ac0547d | 2010-07-05 16:50:13 +0200 | [diff] [blame] | 3940 | restore_shutup_pins(codec); |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 3941 | hda_exec_init_verbs(codec); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3942 | snd_hda_jack_set_dirty_all(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3943 | if (codec->patch_ops.resume) |
| 3944 | codec->patch_ops.resume(codec); |
| 3945 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 3946 | if (codec->patch_ops.init) |
| 3947 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3948 | snd_hda_codec_resume_amp(codec); |
| 3949 | snd_hda_codec_resume_cache(codec); |
| 3950 | } |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3951 | |
| 3952 | if (codec->jackpoll_interval) |
| 3953 | hda_jackpoll_work(&codec->jackpoll_work.work); |
Takashi Iwai | 31614bb | 2013-01-23 15:58:40 +0100 | [diff] [blame] | 3954 | else |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 3955 | snd_hda_jack_report_sync(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3956 | atomic_dec(&codec->in_pm); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3957 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3958 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3959 | static int hda_codec_runtime_suspend(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3960 | { |
| 3961 | struct hda_codec *codec = dev_to_hda_codec(dev); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3962 | unsigned int state; |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3963 | int i; |
| 3964 | |
| 3965 | cancel_delayed_work_sync(&codec->jackpoll_work); |
| 3966 | for (i = 0; i < codec->num_pcms; i++) |
| 3967 | snd_pcm_suspend_all(codec->pcm_info[i].pcm); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3968 | state = hda_call_codec_suspend(codec); |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame^] | 3969 | if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK)) |
| 3970 | clear_bit(codec->addr, &codec->bus->codec_powered); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3971 | return 0; |
| 3972 | } |
| 3973 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3974 | static int hda_codec_runtime_resume(struct device *dev) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3975 | { |
Takashi Iwai | 55ed9cd | 2015-02-19 17:35:32 +0100 | [diff] [blame^] | 3976 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 3977 | |
| 3978 | set_bit(codec->addr, &codec->bus->codec_powered); |
| 3979 | hda_call_codec_resume(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3980 | pm_runtime_mark_last_busy(dev); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
Takashi Iwai | 2a43952 | 2011-07-26 09:52:50 +0200 | [diff] [blame] | 3983 | #endif /* CONFIG_PM */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3984 | |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3985 | /* referred in hda_bind.c */ |
| 3986 | const struct dev_pm_ops hda_codec_driver_pm = { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 3987 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 3988 | pm_runtime_force_resume) |
| 3989 | SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, |
| 3990 | NULL) |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 3991 | }; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 3992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | /** |
| 3994 | * snd_hda_build_controls - build mixer controls |
| 3995 | * @bus: the BUS |
| 3996 | * |
| 3997 | * Creates mixer controls for each codec included in the bus. |
| 3998 | * |
| 3999 | * Returns 0 if successful, otherwise a negative error code. |
| 4000 | */ |
Takashi Iwai | 6a0f56a | 2012-12-07 07:41:56 +0100 | [diff] [blame] | 4001 | int snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4003 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4005 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4006 | int err = snd_hda_codec_build_controls(codec); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4007 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4008 | codec_err(codec, |
| 4009 | "cannot build controls for #%d (error %d)\n", |
| 4010 | codec->addr, err); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4011 | err = snd_hda_codec_reset(codec); |
| 4012 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4013 | codec_err(codec, |
| 4014 | "cannot revert codec\n"); |
Takashi Iwai | f93d461 | 2009-03-02 10:44:15 +0100 | [diff] [blame] | 4015 | return err; |
| 4016 | } |
| 4017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | } |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4019 | return 0; |
| 4020 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4021 | EXPORT_SYMBOL_GPL(snd_hda_build_controls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4023 | /* |
| 4024 | * add standard channel maps if not specified |
| 4025 | */ |
| 4026 | static int add_std_chmaps(struct hda_codec *codec) |
| 4027 | { |
| 4028 | int i, str, err; |
| 4029 | |
| 4030 | for (i = 0; i < codec->num_pcms; i++) { |
| 4031 | for (str = 0; str < 2; str++) { |
| 4032 | struct snd_pcm *pcm = codec->pcm_info[i].pcm; |
| 4033 | struct hda_pcm_stream *hinfo = |
| 4034 | &codec->pcm_info[i].stream[str]; |
| 4035 | struct snd_pcm_chmap *chmap; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4036 | const struct snd_pcm_chmap_elem *elem; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4037 | |
| 4038 | if (codec->pcm_info[i].own_chmap) |
| 4039 | continue; |
| 4040 | if (!pcm || !hinfo->substreams) |
| 4041 | continue; |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4042 | elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; |
| 4043 | err = snd_pcm_add_chmap_ctls(pcm, str, elem, |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4044 | hinfo->channels_max, |
| 4045 | 0, &chmap); |
| 4046 | if (err < 0) |
| 4047 | return err; |
| 4048 | chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; |
| 4049 | } |
| 4050 | } |
| 4051 | return 0; |
| 4052 | } |
| 4053 | |
Takashi Iwai | ee81abb | 2012-11-08 17:12:10 +0100 | [diff] [blame] | 4054 | /* default channel maps for 2.1 speakers; |
| 4055 | * since HD-audio supports only stereo, odd number channels are omitted |
| 4056 | */ |
| 4057 | const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { |
| 4058 | { .channels = 2, |
| 4059 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, |
| 4060 | { .channels = 4, |
| 4061 | .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, |
| 4062 | SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, |
| 4063 | { } |
| 4064 | }; |
| 4065 | EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); |
| 4066 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4067 | int snd_hda_codec_build_controls(struct hda_codec *codec) |
| 4068 | { |
| 4069 | int err = 0; |
Takashi Iwai | 11aeff0 | 2008-07-30 15:01:46 +0200 | [diff] [blame] | 4070 | hda_exec_init_verbs(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4071 | /* continue to initialize... */ |
| 4072 | if (codec->patch_ops.init) |
| 4073 | err = codec->patch_ops.init(codec); |
| 4074 | if (!err && codec->patch_ops.build_controls) |
| 4075 | err = codec->patch_ops.build_controls(codec); |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4076 | if (err < 0) |
| 4077 | return err; |
Takashi Iwai | 9c9a517 | 2012-07-31 11:35:35 +0200 | [diff] [blame] | 4078 | |
| 4079 | /* we create chmaps here instead of build_pcms */ |
| 4080 | err = add_std_chmaps(codec); |
| 4081 | if (err < 0) |
| 4082 | return err; |
| 4083 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 4084 | if (codec->jackpoll_interval) |
| 4085 | hda_jackpoll_work(&codec->jackpoll_work.work); |
| 4086 | else |
| 4087 | snd_hda_jack_report_sync(codec); /* call at the last init point */ |
Takashi Iwai | b9c590b | 2013-01-24 17:27:32 +0100 | [diff] [blame] | 4088 | sync_power_up_states(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | return 0; |
| 4090 | } |
| 4091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | /* |
| 4093 | * stream formats |
| 4094 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4095 | struct hda_rate_tbl { |
| 4096 | unsigned int hz; |
| 4097 | unsigned int alsa_bits; |
| 4098 | unsigned int hda_fmt; |
| 4099 | }; |
| 4100 | |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4101 | /* rate = base * mult / div */ |
| 4102 | #define HDA_RATE(base, mult, div) \ |
| 4103 | (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ |
| 4104 | (((div) - 1) << AC_FMT_DIV_SHIFT)) |
| 4105 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4106 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4108 | |
| 4109 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4110 | { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, |
| 4111 | { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, |
| 4112 | { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, |
| 4113 | { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, |
| 4114 | { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, |
| 4115 | { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, |
| 4116 | { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, |
| 4117 | { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, |
| 4118 | { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, |
| 4119 | { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, |
| 4120 | { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4121 | #define AC_PAR_PCM_RATE_BITS 11 |
| 4122 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 4123 | |
| 4124 | /* not autodetected value */ |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4125 | { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 4126 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4127 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | }; |
| 4129 | |
| 4130 | /** |
| 4131 | * snd_hda_calc_stream_format - calculate format bitset |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4132 | * @codec: HD-audio codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | * @rate: the sample rate |
| 4134 | * @channels: the number of channels |
| 4135 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 4136 | * @maxbps: the max. bps |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4137 | * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | * |
| 4139 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 4140 | * |
| 4141 | * Return zero if invalid. |
| 4142 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4143 | unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, |
| 4144 | unsigned int rate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | unsigned int channels, |
| 4146 | unsigned int format, |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4147 | unsigned int maxbps, |
| 4148 | unsigned short spdif_ctls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | { |
| 4150 | int i; |
| 4151 | unsigned int val = 0; |
| 4152 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4153 | for (i = 0; rate_bits[i].hz; i++) |
| 4154 | if (rate_bits[i].hz == rate) { |
| 4155 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | break; |
| 4157 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4158 | if (!rate_bits[i].hz) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4159 | codec_dbg(codec, "invalid rate %d\n", rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | return 0; |
| 4161 | } |
| 4162 | |
| 4163 | if (channels == 0 || channels > 8) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4164 | codec_dbg(codec, "invalid channels %d\n", channels); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | return 0; |
| 4166 | } |
| 4167 | val |= channels - 1; |
| 4168 | |
| 4169 | switch (snd_pcm_format_width(format)) { |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4170 | case 8: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4171 | val |= AC_FMT_BITS_8; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4172 | break; |
| 4173 | case 16: |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4174 | val |= AC_FMT_BITS_16; |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4175 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | case 20: |
| 4177 | case 24: |
| 4178 | case 32: |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4179 | if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4180 | val |= AC_FMT_BITS_32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | else if (maxbps >= 24) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4182 | val |= AC_FMT_BITS_24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | else |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4184 | val |= AC_FMT_BITS_20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | break; |
| 4186 | default: |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 4187 | codec_dbg(codec, "invalid format width %d\n", |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4188 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | return 0; |
| 4190 | } |
| 4191 | |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4192 | if (spdif_ctls & AC_DIG1_NONAUDIO) |
Takashi Iwai | 92f10b3 | 2010-08-03 14:21:00 +0200 | [diff] [blame] | 4193 | val |= AC_FMT_TYPE_NON_PCM; |
Anssi Hannula | 32c168c | 2010-08-03 13:28:57 +0300 | [diff] [blame] | 4194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | return val; |
| 4196 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4197 | EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4199 | static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, |
| 4200 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4201 | { |
| 4202 | unsigned int val = 0; |
| 4203 | if (nid != codec->afg && |
| 4204 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) |
| 4205 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 4206 | if (!val || val == -1) |
| 4207 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 4208 | if (!val || val == -1) |
| 4209 | return 0; |
| 4210 | return val; |
| 4211 | } |
| 4212 | |
| 4213 | static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) |
| 4214 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4215 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4216 | get_pcm_param); |
| 4217 | } |
| 4218 | |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4219 | static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, |
| 4220 | int dir) |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4221 | { |
| 4222 | unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 4223 | if (!streams || streams == -1) |
| 4224 | streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
| 4225 | if (!streams || streams == -1) |
| 4226 | return 0; |
| 4227 | return streams; |
| 4228 | } |
| 4229 | |
| 4230 | static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) |
| 4231 | { |
Takashi Iwai | c3b6bcc | 2012-05-10 16:11:15 +0200 | [diff] [blame] | 4232 | return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4233 | get_stream_param); |
| 4234 | } |
| 4235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | /** |
| 4237 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 4238 | * @codec: the HDA codec |
| 4239 | * @nid: NID to query |
| 4240 | * @ratesp: the pointer to store the detected rate bitflags |
| 4241 | * @formatsp: the pointer to store the detected formats |
| 4242 | * @bpsp: the pointer to store the detected format widths |
| 4243 | * |
| 4244 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 4245 | * or @bsps argument is ignored. |
| 4246 | * |
| 4247 | * Returns 0 if successful, otherwise a negative error code. |
| 4248 | */ |
Stephen Warren | 384a48d | 2011-06-01 11:14:21 -0600 | [diff] [blame] | 4249 | 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] | 4250 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 4251 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4252 | unsigned int i, val, wcaps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4254 | wcaps = get_wcaps(codec, nid); |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4255 | val = query_pcm_param(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | |
| 4257 | if (ratesp) { |
| 4258 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4259 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4261 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4263 | if (rates == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4264 | codec_err(codec, |
| 4265 | "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", |
| 4266 | nid, val, |
| 4267 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4268 | return -EIO; |
| 4269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | *ratesp = rates; |
| 4271 | } |
| 4272 | |
| 4273 | if (formatsp || bpsp) { |
| 4274 | u64 formats = 0; |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4275 | unsigned int streams, bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4277 | streams = query_stream_param(codec, nid); |
| 4278 | if (!streams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
| 4281 | bps = 0; |
| 4282 | if (streams & AC_SUPFMT_PCM) { |
| 4283 | if (val & AC_SUPPCM_BITS_8) { |
| 4284 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4285 | bps = 8; |
| 4286 | } |
| 4287 | if (val & AC_SUPPCM_BITS_16) { |
| 4288 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 4289 | bps = 16; |
| 4290 | } |
| 4291 | if (wcaps & AC_WCAP_DIGITAL) { |
| 4292 | if (val & AC_SUPPCM_BITS_32) |
| 4293 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 4294 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 4295 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4296 | if (val & AC_SUPPCM_BITS_24) |
| 4297 | bps = 24; |
| 4298 | else if (val & AC_SUPPCM_BITS_20) |
| 4299 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4300 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 4301 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 4303 | if (val & AC_SUPPCM_BITS_32) |
| 4304 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | else if (val & AC_SUPPCM_BITS_24) |
| 4306 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 4307 | else if (val & AC_SUPPCM_BITS_20) |
| 4308 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | } |
| 4310 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4311 | #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] | 4312 | if (streams & AC_SUPFMT_FLOAT32) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
Takashi Iwai | b0bb3aa | 2009-07-03 23:25:37 +0200 | [diff] [blame] | 4314 | if (!bps) |
| 4315 | bps = 32; |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4316 | } |
Takashi Iwai | 8c7dd89 | 2012-05-12 09:38:05 +0200 | [diff] [blame] | 4317 | #endif |
Takashi Iwai | b5025c5 | 2009-07-01 18:05:27 +0200 | [diff] [blame] | 4318 | if (streams == AC_SUPFMT_AC3) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4319 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | /* temporary hack: we have still no proper support |
| 4321 | * for the direct AC3 stream... |
| 4322 | */ |
| 4323 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 4324 | bps = 8; |
| 4325 | } |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4326 | if (formats == 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4327 | codec_err(codec, |
| 4328 | "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", |
| 4329 | nid, val, |
| 4330 | (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, |
| 4331 | streams); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4332 | return -EIO; |
| 4333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | if (formatsp) |
| 4335 | *formatsp = formats; |
| 4336 | if (bpsp) |
| 4337 | *bpsp = bps; |
| 4338 | } |
| 4339 | |
| 4340 | return 0; |
| 4341 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4342 | EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
| 4344 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4345 | * snd_hda_is_supported_format - Check the validity of the format |
| 4346 | * @codec: HD-audio codec |
| 4347 | * @nid: NID to check |
| 4348 | * @format: the HD-audio format value to check |
| 4349 | * |
| 4350 | * Check whether the given node supports the format value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | * |
| 4352 | * Returns 1 if supported, 0 if not. |
| 4353 | */ |
| 4354 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 4355 | unsigned int format) |
| 4356 | { |
| 4357 | int i; |
| 4358 | unsigned int val = 0, rate, stream; |
| 4359 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4360 | val = query_pcm_param(codec, nid); |
| 4361 | if (!val) |
| 4362 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | |
| 4364 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4365 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 4366 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | if (val & (1 << i)) |
| 4368 | break; |
| 4369 | return 0; |
| 4370 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 4371 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | return 0; |
| 4373 | |
Takashi Iwai | 92c7c8a | 2009-03-24 07:32:14 +0100 | [diff] [blame] | 4374 | stream = query_stream_param(codec, nid); |
| 4375 | if (!stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | return 0; |
| 4377 | |
| 4378 | if (stream & AC_SUPFMT_PCM) { |
| 4379 | switch (format & 0xf0) { |
| 4380 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4381 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | return 0; |
| 4383 | break; |
| 4384 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4385 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | return 0; |
| 4387 | break; |
| 4388 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4389 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | return 0; |
| 4391 | break; |
| 4392 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4393 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | return 0; |
| 4395 | break; |
| 4396 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4397 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | return 0; |
| 4399 | break; |
| 4400 | default: |
| 4401 | return 0; |
| 4402 | } |
| 4403 | } else { |
| 4404 | /* FIXME: check for float32 and AC3? */ |
| 4405 | } |
| 4406 | |
| 4407 | return 1; |
| 4408 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4409 | EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | |
| 4411 | /* |
| 4412 | * PCM stuff |
| 4413 | */ |
| 4414 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 4415 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4416 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 4422 | struct hda_codec *codec, |
| 4423 | unsigned int stream_tag, |
| 4424 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4425 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | { |
| 4427 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 4428 | return 0; |
| 4429 | } |
| 4430 | |
| 4431 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 4432 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4433 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 4435 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | return 0; |
| 4437 | } |
| 4438 | |
Takashi Iwai | 6c1f45e | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 4439 | static int set_pcm_default_values(struct hda_codec *codec, |
| 4440 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4442 | int err; |
| 4443 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4444 | /* query support PCM information from the given NID */ |
| 4445 | if (info->nid && (!info->rates || !info->formats)) { |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4446 | err = snd_hda_query_supported_pcm(codec, info->nid, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4447 | info->rates ? NULL : &info->rates, |
| 4448 | info->formats ? NULL : &info->formats, |
| 4449 | info->maxbps ? NULL : &info->maxbps); |
Jaroslav Kysela | ee50471 | 2009-03-17 14:30:31 +0100 | [diff] [blame] | 4450 | if (err < 0) |
| 4451 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | } |
| 4453 | if (info->ops.open == NULL) |
| 4454 | info->ops.open = hda_pcm_default_open_close; |
| 4455 | if (info->ops.close == NULL) |
| 4456 | info->ops.close = hda_pcm_default_open_close; |
| 4457 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4458 | if (snd_BUG_ON(!info->nid)) |
| 4459 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | info->ops.prepare = hda_pcm_default_prepare; |
| 4461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 4463 | if (snd_BUG_ON(!info->nid)) |
| 4464 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 4466 | } |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4470 | /* |
| 4471 | * codec prepare/cleanup entries |
| 4472 | */ |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4473 | /** |
| 4474 | * snd_hda_codec_prepare - Prepare a stream |
| 4475 | * @codec: the HDA codec |
| 4476 | * @hinfo: PCM information |
| 4477 | * @stream: stream tag to assign |
| 4478 | * @format: format id to assign |
| 4479 | * @substream: PCM substream to assign |
| 4480 | * |
| 4481 | * Calls the prepare callback set by the codec with the given arguments. |
| 4482 | * Clean up the inactive streams when successful. |
| 4483 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4484 | int snd_hda_codec_prepare(struct hda_codec *codec, |
| 4485 | struct hda_pcm_stream *hinfo, |
| 4486 | unsigned int stream, |
| 4487 | unsigned int format, |
| 4488 | struct snd_pcm_substream *substream) |
| 4489 | { |
| 4490 | int ret; |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4491 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4492 | ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); |
| 4493 | if (ret >= 0) |
| 4494 | purify_inactive_streams(codec); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4495 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4496 | return ret; |
| 4497 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4498 | EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4499 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 4500 | /** |
| 4501 | * snd_hda_codec_cleanup - Prepare a stream |
| 4502 | * @codec: the HDA codec |
| 4503 | * @hinfo: PCM information |
| 4504 | * @substream: PCM substream |
| 4505 | * |
| 4506 | * Calls the cleanup callback set by the codec with the given arguments. |
| 4507 | */ |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4508 | void snd_hda_codec_cleanup(struct hda_codec *codec, |
| 4509 | struct hda_pcm_stream *hinfo, |
| 4510 | struct snd_pcm_substream *substream) |
| 4511 | { |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4512 | mutex_lock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4513 | hinfo->ops.cleanup(hinfo, codec, substream); |
Takashi Iwai | 3f50ac6 | 2010-08-20 09:44:36 +0200 | [diff] [blame] | 4514 | mutex_unlock(&codec->bus->prepare_mutex); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4515 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4516 | EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); |
Takashi Iwai | eb54133 | 2010-08-06 13:48:11 +0200 | [diff] [blame] | 4517 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4518 | /* global */ |
Jaroslav Kysela | e330323 | 2009-11-10 14:53:02 +0100 | [diff] [blame] | 4519 | const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { |
| 4520 | "Audio", "SPDIF", "HDMI", "Modem" |
| 4521 | }; |
| 4522 | |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4523 | /* |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4524 | * get the empty PCM device number to assign |
| 4525 | */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4526 | 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] | 4527 | { |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4528 | /* audio device indices; not linear to keep compatibility */ |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4529 | /* assigned to static slots up to dev#10; if more needed, assign |
| 4530 | * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) |
| 4531 | */ |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4532 | static int audio_idx[HDA_PCM_NTYPES][5] = { |
| 4533 | [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, |
| 4534 | [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, |
Wu Fengguang | 92608ba | 2009-10-30 11:40:03 +0100 | [diff] [blame] | 4535 | [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4536 | [HDA_PCM_TYPE_MODEM] = { 6, -1 }, |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4537 | }; |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4538 | int i; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4539 | |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4540 | if (type >= HDA_PCM_NTYPES) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4541 | dev_err(bus->card->dev, "Invalid PCM type %d\n", type); |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4542 | return -EINVAL; |
| 4543 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4544 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4545 | for (i = 0; audio_idx[type][i] >= 0; i++) { |
| 4546 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
| 4547 | if (audio_idx[type][i] >= 8) |
| 4548 | break; |
| 4549 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4550 | if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) |
| 4551 | return audio_idx[type][i]; |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4552 | } |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4553 | |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4554 | #ifdef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4555 | /* non-fixed slots starting from 10 */ |
| 4556 | for (i = 10; i < 32; i++) { |
| 4557 | if (!test_and_set_bit(i, bus->pcm_dev_bits)) |
| 4558 | return i; |
| 4559 | } |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4560 | #endif |
Takashi Iwai | 01b65bf | 2011-11-24 14:31:46 +0100 | [diff] [blame] | 4561 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4562 | dev_warn(bus->card->dev, "Too many %s devices\n", |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4563 | snd_hda_pcm_type_name[type]); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4564 | #ifndef CONFIG_SND_DYNAMIC_MINORS |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4565 | dev_warn(bus->card->dev, |
| 4566 | "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); |
Takashi Iwai | 36bb00d | 2013-06-06 12:10:24 +0200 | [diff] [blame] | 4567 | #endif |
Wu Fengguang | f5d6def | 2009-10-30 11:38:26 +0100 | [diff] [blame] | 4568 | return -EAGAIN; |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | /* |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4572 | * attach a new PCM stream |
| 4573 | */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4574 | static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4575 | { |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4576 | struct hda_bus *bus = codec->bus; |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4577 | struct hda_pcm_stream *info; |
| 4578 | int stream, err; |
| 4579 | |
Takashi Iwai | b91f080 | 2008-11-04 08:43:08 +0100 | [diff] [blame] | 4580 | if (snd_BUG_ON(!pcm->name)) |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4581 | return -EINVAL; |
| 4582 | for (stream = 0; stream < 2; stream++) { |
| 4583 | info = &pcm->stream[stream]; |
| 4584 | if (info->substreams) { |
| 4585 | err = set_pcm_default_values(codec, info); |
| 4586 | if (err < 0) |
| 4587 | return err; |
| 4588 | } |
| 4589 | } |
Takashi Iwai | 33fa35e | 2008-11-06 16:50:40 +0100 | [diff] [blame] | 4590 | return bus->ops.attach_pcm(bus, codec, pcm); |
Takashi Iwai | 176d533 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4593 | /* assign all PCMs of the given codec */ |
| 4594 | int snd_hda_codec_build_pcms(struct hda_codec *codec) |
| 4595 | { |
| 4596 | unsigned int pcm; |
| 4597 | int err; |
| 4598 | |
| 4599 | if (!codec->num_pcms) { |
| 4600 | if (!codec->patch_ops.build_pcms) |
| 4601 | return 0; |
| 4602 | err = codec->patch_ops.build_pcms(codec); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4603 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4604 | codec_err(codec, |
| 4605 | "cannot build PCMs for #%d (error %d)\n", |
| 4606 | codec->addr, err); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4607 | err = snd_hda_codec_reset(codec); |
| 4608 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4609 | codec_err(codec, |
| 4610 | "cannot revert codec\n"); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4611 | return err; |
| 4612 | } |
| 4613 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4614 | } |
| 4615 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 4616 | struct hda_pcm *cpcm = &codec->pcm_info[pcm]; |
| 4617 | int dev; |
| 4618 | |
| 4619 | if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) |
Takashi Iwai | 41b5b01 | 2009-01-20 18:21:23 +0100 | [diff] [blame] | 4620 | continue; /* no substreams assigned */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4621 | |
| 4622 | if (!cpcm->pcm) { |
| 4623 | dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); |
| 4624 | if (dev < 0) |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4625 | continue; /* no fatal error */ |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4626 | cpcm->device = dev; |
| 4627 | err = snd_hda_attach_pcm(codec, cpcm); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4628 | if (err < 0) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4629 | codec_err(codec, |
| 4630 | "cannot attach PCM stream %d for codec #%d\n", |
| 4631 | dev, codec->addr); |
Takashi Iwai | 6e655bf | 2009-03-02 10:46:03 +0100 | [diff] [blame] | 4632 | continue; /* no fatal error */ |
| 4633 | } |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | return 0; |
| 4637 | } |
| 4638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | /** |
| 4640 | * snd_hda_build_pcms - build PCM information |
| 4641 | * @bus: the BUS |
| 4642 | * |
| 4643 | * Create PCM information for each codec included in the bus. |
| 4644 | * |
| 4645 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 4646 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 4647 | * to create its PCM instances. |
| 4648 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 4649 | * callback. |
| 4650 | * |
| 4651 | * At least, substreams, channels_min and channels_max must be filled for |
| 4652 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 4653 | * When rates and/or formats are zero, the supported values are queried |
| 4654 | * from the given nid. The nid is used also by the default ops.prepare |
| 4655 | * and ops.cleanup callbacks. |
| 4656 | * |
| 4657 | * The driver needs to call ops.open in its open callback. Similarly, |
| 4658 | * ops.close is supposed to be called in the close callback. |
| 4659 | * ops.prepare should be called in the prepare or hw_params callback |
| 4660 | * with the proper parameters for set up. |
| 4661 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 4662 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4663 | * This function returns 0 if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | */ |
Takashi Iwai | 5cb543d | 2012-08-09 13:49:23 +0200 | [diff] [blame] | 4665 | int snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4667 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4669 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 529bd6c | 2008-11-27 14:17:01 +0100 | [diff] [blame] | 4670 | int err = snd_hda_codec_build_pcms(codec); |
| 4671 | if (err < 0) |
| 4672 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | } |
| 4674 | return 0; |
| 4675 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4676 | EXPORT_SYMBOL_GPL(snd_hda_build_pcms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | * snd_hda_add_new_ctls - create controls from the array |
| 4680 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4681 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | * |
| 4683 | * This helper function creates and add new controls in the given array. |
| 4684 | * The array must be terminated with an empty entry as terminator. |
| 4685 | * |
| 4686 | * Returns 0 if successful, or a negative error code. |
| 4687 | */ |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4688 | int snd_hda_add_new_ctls(struct hda_codec *codec, |
| 4689 | const struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | { |
Jaroslav Kysela | 4d02d1b | 2009-11-12 10:15:48 +0100 | [diff] [blame] | 4691 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | |
| 4693 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4694 | struct snd_kcontrol *kctl; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4695 | int addr = 0, idx = 0; |
Jaroslav Kysela | 5b0cb1d | 2009-12-08 16:13:32 +0100 | [diff] [blame] | 4696 | if (knew->iface == -1) /* skip this codec private value */ |
| 4697 | continue; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4698 | for (;;) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4699 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4700 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4701 | return -ENOMEM; |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4702 | if (addr > 0) |
| 4703 | kctl->id.device = addr; |
| 4704 | if (idx > 0) |
| 4705 | kctl->id.index = idx; |
Jaroslav Kysela | 3911a4c | 2009-11-11 13:43:01 +0100 | [diff] [blame] | 4706 | err = snd_hda_ctl_add(codec, 0, kctl); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4707 | if (!err) |
| 4708 | break; |
| 4709 | /* try first with another device index corresponding to |
| 4710 | * the codec addr; if it still fails (or it's the |
| 4711 | * primary codec), then try another control index |
| 4712 | */ |
| 4713 | if (!addr && codec->addr) |
| 4714 | addr = codec->addr; |
| 4715 | else if (!idx && !knew->index) { |
| 4716 | idx = find_empty_mixer_ctl_idx(codec, |
Takashi Iwai | dcda580 | 2012-10-12 17:24:51 +0200 | [diff] [blame] | 4717 | knew->name, 0); |
Takashi Iwai | 1afe206 | 2010-12-23 10:17:52 +0100 | [diff] [blame] | 4718 | if (idx <= 0) |
| 4719 | return err; |
| 4720 | } else |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 4721 | return err; |
| 4722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | } |
| 4724 | return 0; |
| 4725 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4726 | EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4728 | #ifdef CONFIG_PM |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4729 | /** |
| 4730 | * snd_hda_power_up - Power-up the codec |
| 4731 | * @codec: HD-audio codec |
| 4732 | * |
| 4733 | * Increment the usage counter and resume the device if not done yet. |
| 4734 | */ |
| 4735 | void snd_hda_power_up(struct hda_codec *codec) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4736 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4737 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4738 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4739 | if (codec_in_pm(codec)) |
Takashi Iwai | a2d96e7 | 2012-05-09 12:36:22 +0200 | [diff] [blame] | 4740 | return; |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4741 | pm_runtime_get_sync(dev); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4742 | } |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4743 | EXPORT_SYMBOL_GPL(snd_hda_power_up); |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4744 | |
| 4745 | /** |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4746 | * snd_hda_power_down - Power-down the codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4747 | * @codec: HD-audio codec |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4748 | * |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4749 | * Decrement the usage counter and schedules the autosuspend if none used. |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4750 | */ |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4751 | void snd_hda_power_down(struct hda_codec *codec) |
Takashi Iwai | c376e2c | 2012-08-14 17:12:47 +0200 | [diff] [blame] | 4752 | { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4753 | struct device *dev = hda_codec_dev(codec); |
| 4754 | |
| 4755 | if (codec_in_pm(codec)) |
| 4756 | return; |
| 4757 | pm_runtime_mark_last_busy(dev); |
| 4758 | pm_runtime_put_autosuspend(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_down); |
| 4761 | |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4762 | static void codec_set_power_save(struct hda_codec *codec, int delay) |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4763 | { |
| 4764 | struct device *dev = hda_codec_dev(codec); |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4765 | |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 4766 | if (delay > 0) { |
| 4767 | pm_runtime_set_autosuspend_delay(dev, delay); |
| 4768 | pm_runtime_use_autosuspend(dev); |
| 4769 | pm_runtime_allow(dev); |
| 4770 | if (!pm_runtime_suspended(dev)) |
| 4771 | pm_runtime_mark_last_busy(dev); |
| 4772 | } else { |
| 4773 | pm_runtime_dont_use_autosuspend(dev); |
| 4774 | pm_runtime_forbid(dev); |
| 4775 | } |
| 4776 | } |
Takashi Iwai | bb57392 | 2015-02-20 09:26:04 +0100 | [diff] [blame] | 4777 | |
| 4778 | /** |
| 4779 | * snd_hda_set_power_save - reprogram autosuspend for the given delay |
| 4780 | * @bus: HD-audio bus |
| 4781 | * @delay: autosuspend delay in msec, 0 = off |
| 4782 | * |
| 4783 | * Synchronize the runtime PM autosuspend state from the power_save option. |
| 4784 | */ |
| 4785 | void snd_hda_set_power_save(struct hda_bus *bus, int delay) |
| 4786 | { |
| 4787 | struct hda_codec *c; |
| 4788 | |
| 4789 | list_for_each_entry(c, &bus->codec_list, list) |
| 4790 | codec_set_power_save(c, delay); |
| 4791 | } |
| 4792 | EXPORT_SYMBOL_GPL(snd_hda_set_power_save); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4793 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4794 | /** |
| 4795 | * snd_hda_check_amp_list_power - Check the amp list and update the power |
| 4796 | * @codec: HD-audio codec |
| 4797 | * @check: the object containing an AMP list and the status |
| 4798 | * @nid: NID to check / update |
| 4799 | * |
| 4800 | * Check whether the given NID is in the amp list. If it's in the list, |
| 4801 | * check the current AMP status, and update the the power-status according |
| 4802 | * to the mute status. |
| 4803 | * |
| 4804 | * This function is supposed to be set or called from the check_power_status |
| 4805 | * patch ops. |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 4806 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4807 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 4808 | struct hda_loopback_check *check, |
| 4809 | hda_nid_t nid) |
| 4810 | { |
Takashi Iwai | 031024e | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 4811 | const struct hda_amp_list *p; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4812 | int ch, v; |
| 4813 | |
| 4814 | if (!check->amplist) |
| 4815 | return 0; |
| 4816 | for (p = check->amplist; p->nid; p++) { |
| 4817 | if (p->nid == nid) |
| 4818 | break; |
| 4819 | } |
| 4820 | if (!p->nid) |
| 4821 | return 0; /* nothing changed */ |
| 4822 | |
| 4823 | for (p = check->amplist; p->nid; p++) { |
| 4824 | for (ch = 0; ch < 2; ch++) { |
| 4825 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 4826 | p->idx); |
| 4827 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 4828 | if (!check->power_on) { |
| 4829 | check->power_on = 1; |
| 4830 | snd_hda_power_up(codec); |
| 4831 | } |
| 4832 | return 1; |
| 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | if (check->power_on) { |
| 4837 | check->power_on = 0; |
| 4838 | snd_hda_power_down(codec); |
| 4839 | } |
| 4840 | return 0; |
| 4841 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4842 | EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4843 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 4845 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4846 | * Channel mode helper |
| 4847 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4848 | |
| 4849 | /** |
| 4850 | * snd_hda_ch_mode_info - Info callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4851 | * @codec: the HDA codec |
| 4852 | * @uinfo: pointer to get/store the data |
| 4853 | * @chmode: channel mode array |
| 4854 | * @num_chmodes: channel mode array size |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4855 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4856 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 4857 | struct snd_ctl_elem_info *uinfo, |
| 4858 | const struct hda_channel_mode *chmode, |
| 4859 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4860 | { |
| 4861 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4862 | uinfo->count = 1; |
| 4863 | uinfo->value.enumerated.items = num_chmodes; |
| 4864 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 4865 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 4866 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 4867 | chmode[uinfo->value.enumerated.item].channels); |
| 4868 | return 0; |
| 4869 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4870 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4871 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4872 | /** |
| 4873 | * snd_hda_ch_mode_get - Get callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4874 | * @codec: the HDA codec |
| 4875 | * @ucontrol: pointer to get/store the data |
| 4876 | * @chmode: channel mode array |
| 4877 | * @num_chmodes: channel mode array size |
| 4878 | * @max_channels: max number of channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4879 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4880 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 4881 | struct snd_ctl_elem_value *ucontrol, |
| 4882 | const struct hda_channel_mode *chmode, |
| 4883 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4884 | int max_channels) |
| 4885 | { |
| 4886 | int i; |
| 4887 | |
| 4888 | for (i = 0; i < num_chmodes; i++) { |
| 4889 | if (max_channels == chmode[i].channels) { |
| 4890 | ucontrol->value.enumerated.item[0] = i; |
| 4891 | break; |
| 4892 | } |
| 4893 | } |
| 4894 | return 0; |
| 4895 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4896 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4897 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4898 | /** |
| 4899 | * snd_hda_ch_mode_put - Put callback helper for the channel mode enum |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4900 | * @codec: the HDA codec |
| 4901 | * @ucontrol: pointer to get/store the data |
| 4902 | * @chmode: channel mode array |
| 4903 | * @num_chmodes: channel mode array size |
| 4904 | * @max_channelsp: pointer to store the max channels |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4905 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4906 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 4907 | struct snd_ctl_elem_value *ucontrol, |
| 4908 | const struct hda_channel_mode *chmode, |
| 4909 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4910 | int *max_channelsp) |
| 4911 | { |
| 4912 | unsigned int mode; |
| 4913 | |
| 4914 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 4915 | if (mode >= num_chmodes) |
| 4916 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4917 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4918 | return 0; |
| 4919 | /* change the current channel setting */ |
| 4920 | *max_channelsp = chmode[mode].channels; |
| 4921 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4922 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4923 | return 1; |
| 4924 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4925 | EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 4926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | /* |
| 4928 | * input MUX helper |
| 4929 | */ |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4930 | |
| 4931 | /** |
| 4932 | * 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] | 4933 | * @imux: imux helper object |
| 4934 | * @uinfo: pointer to get/store the data |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4935 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4936 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 4937 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | { |
| 4939 | unsigned int index; |
| 4940 | |
| 4941 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 4942 | uinfo->count = 1; |
| 4943 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4944 | if (!imux->num_items) |
| 4945 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | index = uinfo->value.enumerated.item; |
| 4947 | if (index >= imux->num_items) |
| 4948 | index = imux->num_items - 1; |
| 4949 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 4950 | return 0; |
| 4951 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4952 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4954 | /** |
| 4955 | * 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] | 4956 | * @codec: the HDA codec |
| 4957 | * @imux: imux helper object |
| 4958 | * @ucontrol: pointer to get/store the data |
| 4959 | * @nid: input mux NID |
| 4960 | * @cur_val: pointer to get/store the current imux value |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 4961 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 4962 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 4963 | const struct hda_input_mux *imux, |
| 4964 | struct snd_ctl_elem_value *ucontrol, |
| 4965 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | unsigned int *cur_val) |
| 4967 | { |
| 4968 | unsigned int idx; |
| 4969 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 4970 | if (!imux->num_items) |
| 4971 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | idx = ucontrol->value.enumerated.item[0]; |
| 4973 | if (idx >= imux->num_items) |
| 4974 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4975 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 4977 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 4978 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | *cur_val = idx; |
| 4980 | return 1; |
| 4981 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4982 | EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | |
| 4984 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 4985 | /** |
| 4986 | * snd_hda_enum_helper_info - Helper for simple enum ctls |
| 4987 | * @kcontrol: ctl element |
| 4988 | * @uinfo: pointer to get/store the data |
| 4989 | * @num_items: number of enum items |
| 4990 | * @texts: enum item string array |
| 4991 | * |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 4992 | * process kcontrol info callback of a simple string enum array |
| 4993 | * when @num_items is 0 or @texts is NULL, assume a boolean enum array |
| 4994 | */ |
| 4995 | int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, |
| 4996 | struct snd_ctl_elem_info *uinfo, |
| 4997 | int num_items, const char * const *texts) |
| 4998 | { |
| 4999 | static const char * const texts_default[] = { |
| 5000 | "Disabled", "Enabled" |
| 5001 | }; |
| 5002 | |
| 5003 | if (!texts || !num_items) { |
| 5004 | num_items = 2; |
| 5005 | texts = texts_default; |
| 5006 | } |
| 5007 | |
Takashi Iwai | 3ff7221 | 2014-10-20 18:17:28 +0200 | [diff] [blame] | 5008 | return snd_ctl_enum_info(uinfo, 1, num_items, texts); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5009 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5010 | EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); |
Takashi Iwai | dda415d | 2012-11-30 18:34:38 +0100 | [diff] [blame] | 5011 | |
| 5012 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | * Multi-channel / digital-out PCM helper functions |
| 5014 | */ |
| 5015 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5016 | /* setup SPDIF output stream */ |
| 5017 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 5018 | unsigned int stream_tag, unsigned int format) |
| 5019 | { |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5020 | struct hda_spdif_out *spdif; |
| 5021 | unsigned int curr_fmt; |
| 5022 | bool reset; |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5023 | |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5024 | spdif = snd_hda_spdif_out_of_nid(codec, nid); |
| 5025 | curr_fmt = snd_hda_codec_read(codec, nid, 0, |
| 5026 | AC_VERB_GET_STREAM_FORMAT, 0); |
| 5027 | reset = codec->spdif_status_reset && |
| 5028 | (spdif->ctls & AC_DIG1_ENABLE) && |
| 5029 | curr_fmt != format; |
| 5030 | |
| 5031 | /* turn off SPDIF if needed; otherwise the IEC958 bits won't be |
| 5032 | updated */ |
| 5033 | if (reset) |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5034 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5035 | spdif->ctls & ~AC_DIG1_ENABLE & 0xff, |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5036 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5037 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5038 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5039 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5040 | for (d = codec->slave_dig_outs; *d; d++) |
| 5041 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 5042 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5043 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5044 | /* turn on again (if needed) */ |
Laurence Darby | 3bef1c3 | 2012-11-03 17:00:06 +0000 | [diff] [blame] | 5045 | if (reset) |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5046 | set_dig_out_convert(codec, nid, |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5047 | spdif->ctls & 0xff, -1); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5048 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 5049 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5050 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 5051 | { |
| 5052 | snd_hda_codec_cleanup_stream(codec, nid); |
| 5053 | if (codec->slave_dig_outs) { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5054 | const hda_nid_t *d; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5055 | for (d = codec->slave_dig_outs; *d; d++) |
| 5056 | snd_hda_codec_cleanup_stream(codec, *d); |
| 5057 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5058 | } |
| 5059 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5060 | /** |
| 5061 | * snd_hda_bus_reboot_notify - call the reboot notifier of each codec |
| 5062 | * @bus: HD-audio bus |
| 5063 | */ |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5064 | void snd_hda_bus_reboot_notify(struct hda_bus *bus) |
| 5065 | { |
| 5066 | struct hda_codec *codec; |
| 5067 | |
| 5068 | if (!bus) |
| 5069 | return; |
| 5070 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | e581f3d | 2011-07-26 10:19:20 +0200 | [diff] [blame] | 5071 | if (hda_codec_is_power_on(codec) && |
| 5072 | codec->patch_ops.reboot_notify) |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5073 | codec->patch_ops.reboot_notify(codec); |
| 5074 | } |
| 5075 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5076 | EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); |
Takashi Iwai | fb8d1a3 | 2009-11-10 16:02:29 +0100 | [diff] [blame] | 5077 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5078 | /** |
| 5079 | * 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] | 5080 | * @codec: the HDA codec |
| 5081 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5083 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 5084 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5086 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 5087 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 5088 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5089 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5091 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | return 0; |
| 5093 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5094 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5096 | /** |
| 5097 | * snd_hda_multi_out_dig_prepare - prepare the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5098 | * @codec: the HDA codec |
| 5099 | * @mout: hda_multi_out object |
| 5100 | * @stream_tag: stream tag to assign |
| 5101 | * @format: format id to assign |
| 5102 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5103 | */ |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5104 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 5105 | struct hda_multi_out *mout, |
| 5106 | unsigned int stream_tag, |
| 5107 | unsigned int format, |
| 5108 | struct snd_pcm_substream *substream) |
| 5109 | { |
| 5110 | mutex_lock(&codec->spdif_mutex); |
| 5111 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 5112 | mutex_unlock(&codec->spdif_mutex); |
| 5113 | return 0; |
| 5114 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5115 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5116 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5117 | /** |
| 5118 | * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5119 | * @codec: the HDA codec |
| 5120 | * @mout: hda_multi_out object |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5121 | */ |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5122 | int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, |
| 5123 | struct hda_multi_out *mout) |
| 5124 | { |
| 5125 | mutex_lock(&codec->spdif_mutex); |
| 5126 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
| 5127 | mutex_unlock(&codec->spdif_mutex); |
| 5128 | return 0; |
| 5129 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5130 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); |
Takashi Iwai | 9411e21 | 2009-02-13 11:32:28 +0100 | [diff] [blame] | 5131 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5132 | /** |
| 5133 | * snd_hda_multi_out_dig_close - release the digital out stream |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5134 | * @codec: the HDA codec |
| 5135 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5137 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 5138 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5140 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5142 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5145 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5147 | /** |
| 5148 | * snd_hda_multi_out_analog_open - open analog outputs |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5149 | * @codec: the HDA codec |
| 5150 | * @mout: hda_multi_out object |
| 5151 | * @substream: PCM substream to assign |
| 5152 | * @hinfo: PCM information to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5153 | * |
| 5154 | * Open analog outputs and set up the hw-constraints. |
| 5155 | * If the digital outputs can be opened as slave, open the digital |
| 5156 | * outputs, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5158 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 5159 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5160 | struct snd_pcm_substream *substream, |
| 5161 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5163 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 5164 | runtime->hw.channels_max = mout->max_channels; |
| 5165 | if (mout->dig_out_nid) { |
| 5166 | if (!mout->analog_rates) { |
| 5167 | mout->analog_rates = hinfo->rates; |
| 5168 | mout->analog_formats = hinfo->formats; |
| 5169 | mout->analog_maxbps = hinfo->maxbps; |
| 5170 | } else { |
| 5171 | runtime->hw.rates = mout->analog_rates; |
| 5172 | runtime->hw.formats = mout->analog_formats; |
| 5173 | hinfo->maxbps = mout->analog_maxbps; |
| 5174 | } |
| 5175 | if (!mout->spdif_rates) { |
| 5176 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 5177 | &mout->spdif_rates, |
| 5178 | &mout->spdif_formats, |
| 5179 | &mout->spdif_maxbps); |
| 5180 | } |
| 5181 | mutex_lock(&codec->spdif_mutex); |
| 5182 | if (mout->share_spdif) { |
Takashi Iwai | 022b466 | 2009-07-03 23:03:30 +0200 | [diff] [blame] | 5183 | if ((runtime->hw.rates & mout->spdif_rates) && |
| 5184 | (runtime->hw.formats & mout->spdif_formats)) { |
| 5185 | runtime->hw.rates &= mout->spdif_rates; |
| 5186 | runtime->hw.formats &= mout->spdif_formats; |
| 5187 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 5188 | hinfo->maxbps = mout->spdif_maxbps; |
| 5189 | } else { |
| 5190 | mout->share_spdif = 0; |
| 5191 | /* FIXME: need notify? */ |
| 5192 | } |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5193 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 5194 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 5197 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 5198 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5199 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5201 | /** |
| 5202 | * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5203 | * @codec: the HDA codec |
| 5204 | * @mout: hda_multi_out object |
| 5205 | * @stream_tag: stream tag to assign |
| 5206 | * @format: format id to assign |
| 5207 | * @substream: PCM substream to assign |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5208 | * |
| 5209 | * Set up the i/o for analog out. |
| 5210 | * 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] | 5211 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5212 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 5213 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | unsigned int stream_tag, |
| 5215 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 5216 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5218 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | int chs = substream->runtime->channels; |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5220 | struct hda_spdif_out *spdif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | int i; |
| 5222 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5223 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | e3245cd | 2012-05-10 10:21:29 +0200 | [diff] [blame] | 5224 | spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 5225 | if (mout->dig_out_nid && mout->share_spdif && |
| 5226 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5228 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 5229 | format) && |
Stephen Warren | 7c93597 | 2011-06-01 11:14:17 -0600 | [diff] [blame] | 5230 | !(spdif->status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 5232 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 5233 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | } else { |
| 5235 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 5236 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | } |
| 5238 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5239 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | |
| 5241 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5242 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 5243 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5244 | if (!mout->no_share_stream && |
| 5245 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5247 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 5248 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5249 | /* extra outputs copied from front */ |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5250 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5251 | if (!mout->no_share_stream && mout->hp_out_nid[i]) |
| 5252 | snd_hda_codec_setup_stream(codec, |
| 5253 | mout->hp_out_nid[i], |
| 5254 | stream_tag, 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | /* surrounds */ |
| 5257 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 5258 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5259 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5260 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 5261 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5262 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 5263 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | } |
David Henningsson | cd4035e | 2013-10-10 09:01:25 +0200 | [diff] [blame] | 5265 | |
| 5266 | /* extra surrounds */ |
| 5267 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { |
| 5268 | int ch = 0; |
| 5269 | if (!mout->extra_out_nid[i]) |
| 5270 | break; |
| 5271 | if (chs >= (i + 1) * 2) |
| 5272 | ch = i * 2; |
| 5273 | else if (!mout->no_share_stream) |
| 5274 | break; |
| 5275 | snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], |
| 5276 | stream_tag, ch, format); |
| 5277 | } |
| 5278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | return 0; |
| 5280 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5281 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5283 | /** |
| 5284 | * 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] | 5285 | * @codec: the HDA codec |
| 5286 | * @mout: hda_multi_out object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5288 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 5289 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | { |
Takashi Iwai | dda1441 | 2011-05-02 11:29:30 +0200 | [diff] [blame] | 5291 | const hda_nid_t *nids = mout->dac_nids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | int i; |
| 5293 | |
| 5294 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5295 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5297 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | a06dbfc | 2011-08-23 18:16:13 +0200 | [diff] [blame] | 5298 | for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) |
| 5299 | if (mout->hp_out_nid[i]) |
| 5300 | snd_hda_codec_cleanup_stream(codec, |
| 5301 | mout->hp_out_nid[i]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 5302 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 5303 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 5304 | snd_hda_codec_cleanup_stream(codec, |
| 5305 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5306 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | 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] | 5308 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | mout->dig_out_used = 0; |
| 5310 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 5311 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | return 0; |
| 5313 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5314 | EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5316 | /** |
| 5317 | * snd_hda_get_default_vref - Get the default (mic) VREF pin bits |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5318 | * @codec: the HDA codec |
| 5319 | * @pin: referred pin NID |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5320 | * |
| 5321 | * Guess the suitable VREF pin bits to be set as the pin-control value. |
| 5322 | * Note: the function doesn't set the AC_PINCTL_IN_EN bit. |
| 5323 | */ |
| 5324 | unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) |
| 5325 | { |
| 5326 | unsigned int pincap; |
| 5327 | unsigned int oldval; |
| 5328 | oldval = snd_hda_codec_read(codec, pin, 0, |
| 5329 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 5330 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 5331 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5332 | /* Exception: if the default pin setup is vref50, we give it priority */ |
| 5333 | if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) |
| 5334 | return AC_PINCTL_VREF_80; |
| 5335 | else if (pincap & AC_PINCAP_VREF_50) |
| 5336 | return AC_PINCTL_VREF_50; |
| 5337 | else if (pincap & AC_PINCAP_VREF_100) |
| 5338 | return AC_PINCTL_VREF_100; |
| 5339 | else if (pincap & AC_PINCAP_VREF_GRD) |
| 5340 | return AC_PINCTL_VREF_GRD; |
| 5341 | return AC_PINCTL_VREF_HIZ; |
| 5342 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5343 | EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); |
Takashi Iwai | 4740860 | 2012-04-20 13:06:53 +0200 | [diff] [blame] | 5344 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5345 | /** |
| 5346 | * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap |
| 5347 | * @codec: the HDA codec |
| 5348 | * @pin: referred pin NID |
| 5349 | * @val: pin ctl value to audit |
| 5350 | */ |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5351 | unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, |
| 5352 | hda_nid_t pin, unsigned int val) |
| 5353 | { |
| 5354 | static unsigned int cap_lists[][2] = { |
| 5355 | { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, |
| 5356 | { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, |
| 5357 | { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, |
| 5358 | { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, |
| 5359 | }; |
| 5360 | unsigned int cap; |
| 5361 | |
| 5362 | if (!val) |
| 5363 | return 0; |
| 5364 | cap = snd_hda_query_pin_caps(codec, pin); |
| 5365 | if (!cap) |
| 5366 | return val; /* don't know what to do... */ |
| 5367 | |
| 5368 | if (val & AC_PINCTL_OUT_EN) { |
| 5369 | if (!(cap & AC_PINCAP_OUT)) |
| 5370 | val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); |
| 5371 | else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) |
| 5372 | val &= ~AC_PINCTL_HP_EN; |
| 5373 | } |
| 5374 | |
| 5375 | if (val & AC_PINCTL_IN_EN) { |
| 5376 | if (!(cap & AC_PINCAP_IN)) |
| 5377 | val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); |
| 5378 | else { |
| 5379 | unsigned int vcap, vref; |
| 5380 | int i; |
| 5381 | vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 5382 | vref = val & AC_PINCTL_VREFEN; |
| 5383 | for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { |
| 5384 | if (vref == cap_lists[i][0] && |
| 5385 | !(vcap & cap_lists[i][1])) { |
| 5386 | if (i == ARRAY_SIZE(cap_lists) - 1) |
| 5387 | vref = AC_PINCTL_VREF_HIZ; |
| 5388 | else |
| 5389 | vref = cap_lists[i + 1][0]; |
| 5390 | } |
| 5391 | } |
| 5392 | val &= ~AC_PINCTL_VREFEN; |
| 5393 | val |= vref; |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | return val; |
| 5398 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5399 | EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5400 | |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5401 | /** |
| 5402 | * _snd_hda_pin_ctl - Helper to set pin ctl value |
| 5403 | * @codec: the HDA codec |
| 5404 | * @pin: referred pin NID |
| 5405 | * @val: pin control value to set |
| 5406 | * @cached: access over codec pinctl cache or direct write |
| 5407 | * |
| 5408 | * This function is a helper to set a pin ctl value more safely. |
| 5409 | * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the |
| 5410 | * value in pin target array via snd_hda_codec_set_pin_target(), then |
| 5411 | * actually writes the value via either snd_hda_codec_update_cache() or |
| 5412 | * snd_hda_codec_write() depending on @cached flag. |
| 5413 | */ |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5414 | int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, |
| 5415 | unsigned int val, bool cached) |
| 5416 | { |
Takashi Iwai | 62f3a2f | 2013-01-10 08:56:46 +0100 | [diff] [blame] | 5417 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
Takashi Iwai | d7fdc00 | 2013-01-10 08:38:04 +0100 | [diff] [blame] | 5418 | snd_hda_codec_set_pin_target(codec, pin, val); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5419 | if (cached) |
| 5420 | return snd_hda_codec_update_cache(codec, pin, 0, |
| 5421 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5422 | else |
| 5423 | return snd_hda_codec_write(codec, pin, 0, |
| 5424 | AC_VERB_SET_PIN_WIDGET_CONTROL, val); |
| 5425 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5426 | EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); |
Takashi Iwai | cdd03ce | 2012-04-20 12:34:50 +0200 | [diff] [blame] | 5427 | |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5428 | /** |
| 5429 | * snd_hda_add_imux_item - Add an item to input_mux |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 5430 | * @codec: the HDA codec |
| 5431 | * @imux: imux helper object |
| 5432 | * @label: the name of imux item to assign |
| 5433 | * @index: index number of imux item to assign |
| 5434 | * @type_idx: pointer to store the resultant label index |
Takashi Iwai | 990061c | 2010-09-09 22:08:44 +0200 | [diff] [blame] | 5435 | * |
| 5436 | * When the same label is used already in the existing items, the number |
| 5437 | * suffix is appended to the label. This label index number is stored |
| 5438 | * to type_idx when non-NULL pointer is given. |
| 5439 | */ |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5440 | int snd_hda_add_imux_item(struct hda_codec *codec, |
| 5441 | struct hda_input_mux *imux, const char *label, |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5442 | int index, int *type_idx) |
| 5443 | { |
| 5444 | int i, label_idx = 0; |
| 5445 | if (imux->num_items >= HDA_MAX_NUM_INPUTS) { |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 5446 | codec_err(codec, "hda_codec: Too many imux items!\n"); |
Takashi Iwai | 10a20af | 2010-09-09 16:28:02 +0200 | [diff] [blame] | 5447 | return -EINVAL; |
| 5448 | } |
| 5449 | for (i = 0; i < imux->num_items; i++) { |
| 5450 | if (!strncmp(label, imux->items[i].label, strlen(label))) |
| 5451 | label_idx++; |
| 5452 | } |
| 5453 | if (type_idx) |
| 5454 | *type_idx = label_idx; |
| 5455 | if (label_idx > 0) |
| 5456 | snprintf(imux->items[imux->num_items].label, |
| 5457 | sizeof(imux->items[imux->num_items].label), |
| 5458 | "%s %d", label, label_idx); |
| 5459 | else |
| 5460 | strlcpy(imux->items[imux->num_items].label, label, |
| 5461 | sizeof(imux->items[imux->num_items].label)); |
| 5462 | imux->items[imux->num_items].index = index; |
| 5463 | imux->num_items++; |
| 5464 | return 0; |
| 5465 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5466 | EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); |
Takashi Iwai | d7b1ae9 | 2010-08-30 13:00:16 +0200 | [diff] [blame] | 5467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | /** |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5469 | * snd_hda_bus_reset - Reset the bus |
| 5470 | * @bus: HD-audio bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | */ |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5472 | void snd_hda_bus_reset(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5474 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 5476 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5477 | /* FIXME: maybe a better way needed for forced reset */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 5478 | cancel_delayed_work_sync(&codec->jackpoll_work); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5479 | #ifdef CONFIG_PM |
Mengdong Lin | 0e24dbb | 2013-11-26 23:00:51 -0500 | [diff] [blame] | 5480 | if (hda_codec_is_power_on(codec)) { |
Takashi Iwai | cc72da7 | 2015-02-19 16:00:22 +0100 | [diff] [blame] | 5481 | hda_call_codec_suspend(codec); |
Mengdong Lin | 12edb89 | 2013-11-26 23:32:23 -0500 | [diff] [blame] | 5482 | hda_call_codec_resume(codec); |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5483 | } |
| 5484 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | } |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 5487 | EXPORT_SYMBOL_GPL(snd_hda_bus_reset); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* |
| 5490 | * generic arrays |
| 5491 | */ |
| 5492 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5493 | /** |
| 5494 | * snd_array_new - get a new element from the given array |
| 5495 | * @array: the array object |
Norberto Lopes | 28aedaf | 2010-02-28 20:16:53 +0100 | [diff] [blame] | 5496 | * |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5497 | * Get a new element from the given array. If it exceeds the |
| 5498 | * pre-allocated array size, re-allocate the array. |
| 5499 | * |
| 5500 | * Returns NULL if allocation failed. |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5501 | */ |
| 5502 | void *snd_array_new(struct snd_array *array) |
| 5503 | { |
Takashi Iwai | 12f1771 | 2012-10-10 08:59:14 +0200 | [diff] [blame] | 5504 | if (snd_BUG_ON(!array->elem_size)) |
| 5505 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5506 | if (array->used >= array->alloced) { |
| 5507 | int num = array->alloced + array->alloc_align; |
Takashi Iwai | 3101ba0 | 2011-07-12 08:05:16 +0200 | [diff] [blame] | 5508 | int size = (num + 1) * array->elem_size; |
Takashi Iwai | b910d9a | 2008-11-07 00:26:52 +0100 | [diff] [blame] | 5509 | void *nlist; |
| 5510 | if (snd_BUG_ON(num >= 4096)) |
| 5511 | return NULL; |
Takashi Iwai | 5265fd9 | 2013-03-13 12:15:28 +0100 | [diff] [blame] | 5512 | nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5513 | if (!nlist) |
| 5514 | return NULL; |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5515 | array->list = nlist; |
| 5516 | array->alloced = num; |
| 5517 | } |
Takashi Iwai | f43aa02 | 2008-11-10 16:24:26 +0100 | [diff] [blame] | 5518 | return snd_array_elem(array, array->used++); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5519 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5520 | EXPORT_SYMBOL_GPL(snd_array_new); |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5521 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5522 | /** |
| 5523 | * snd_array_free - free the given array elements |
| 5524 | * @array: the array object |
| 5525 | */ |
Takashi Iwai | b2e1859 | 2008-07-30 15:01:44 +0200 | [diff] [blame] | 5526 | void snd_array_free(struct snd_array *array) |
| 5527 | { |
| 5528 | kfree(array->list); |
| 5529 | array->used = 0; |
| 5530 | array->alloced = 0; |
| 5531 | array->list = NULL; |
| 5532 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5533 | EXPORT_SYMBOL_GPL(snd_array_free); |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5534 | |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5535 | /** |
Takashi Iwai | d5191e5 | 2009-11-16 14:58:17 +0100 | [diff] [blame] | 5536 | * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer |
| 5537 | * @pcm: PCM caps bits |
| 5538 | * @buf: the string buffer to write |
| 5539 | * @buflen: the max buffer length |
| 5540 | * |
| 5541 | * used by hda_proc.c and hda_eld.c |
| 5542 | */ |
Takashi Iwai | b202226 | 2008-11-21 21:24:03 +0100 | [diff] [blame] | 5543 | void snd_print_pcm_bits(int pcm, char *buf, int buflen) |
| 5544 | { |
| 5545 | static unsigned int bits[] = { 8, 16, 20, 24, 32 }; |
| 5546 | int i, j; |
| 5547 | |
| 5548 | for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) |
| 5549 | if (pcm & (AC_SUPPCM_BITS_8 << i)) |
| 5550 | j += snprintf(buf + j, buflen - j, " %d", bits[i]); |
| 5551 | |
| 5552 | buf[j] = '\0'; /* necessary when j == 0 */ |
| 5553 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5554 | EXPORT_SYMBOL_GPL(snd_print_pcm_bits); |
Takashi Iwai | 1289e9e | 2008-11-27 15:47:11 +0100 | [diff] [blame] | 5555 | |
| 5556 | MODULE_DESCRIPTION("HDA codec core"); |
| 5557 | MODULE_LICENSE("GPL"); |