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