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