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