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