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