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