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