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