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