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