Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Interface for Intel High Definition Audio Codec |
| 3 | * |
| 4 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * |
| 7 | * This driver is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This driver is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/pci.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include "hda_codec.h" |
| 29 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 30 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/initval.h> |
| 32 | #include "hda_local.h" |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 33 | #include <sound/hda_hwdep.h> |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 34 | #include "hda_patch.h" /* codec presets */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 37 | /* define this option here to hide as static */ |
Takashi Iwai | 7a5a27c | 2007-09-17 19:07:46 +0200 | [diff] [blame] | 38 | static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 39 | module_param(power_save, int, 0644); |
| 40 | MODULE_PARM_DESC(power_save, "Automatic power-saving timeout " |
| 41 | "(in second, 0 = disable)."); |
| 42 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * vendor / preset table |
| 46 | */ |
| 47 | |
| 48 | struct hda_vendor_id { |
| 49 | unsigned int id; |
| 50 | const char *name; |
| 51 | }; |
| 52 | |
| 53 | /* codec vendor labels */ |
| 54 | static struct hda_vendor_id hda_vendor_ids[] = { |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 55 | { 0x1002, "ATI" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 56 | { 0x1057, "Motorola" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 57 | { 0x1095, "Silicon Image" }, |
| 58 | { 0x10ec, "Realtek" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 59 | { 0x1106, "VIA" }, |
Matthew Ranostay | 7f16859 | 2007-10-18 17:38:17 +0200 | [diff] [blame] | 60 | { 0x111d, "IDT" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 61 | { 0x11c1, "LSI" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 62 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 64 | { 0x14f1, "Conexant" }, |
Takashi Iwai | c8cd128 | 2008-02-13 16:59:29 +0100 | [diff] [blame] | 65 | { 0x17e8, "Chrontel" }, |
| 66 | { 0x1854, "LG" }, |
Mark Brown | 8199de3 | 2008-10-28 14:50:13 +0000 | [diff] [blame] | 67 | { 0x1aec, "Wolfson Microelectronics" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { 0x434d, "C-Media" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 69 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | {} /* terminator */ |
| 71 | }; |
| 72 | |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 73 | static const struct hda_codec_preset *hda_preset_tables[] = { |
| 74 | #ifdef CONFIG_SND_HDA_CODEC_REALTEK |
| 75 | snd_hda_preset_realtek, |
| 76 | #endif |
| 77 | #ifdef CONFIG_SND_HDA_CODEC_CMEDIA |
| 78 | snd_hda_preset_cmedia, |
| 79 | #endif |
| 80 | #ifdef CONFIG_SND_HDA_CODEC_ANALOG |
| 81 | snd_hda_preset_analog, |
| 82 | #endif |
| 83 | #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL |
| 84 | snd_hda_preset_sigmatel, |
| 85 | #endif |
| 86 | #ifdef CONFIG_SND_HDA_CODEC_SI3054 |
| 87 | snd_hda_preset_si3054, |
| 88 | #endif |
| 89 | #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI |
| 90 | snd_hda_preset_atihdmi, |
| 91 | #endif |
| 92 | #ifdef CONFIG_SND_HDA_CODEC_CONEXANT |
| 93 | snd_hda_preset_conexant, |
| 94 | #endif |
| 95 | #ifdef CONFIG_SND_HDA_CODEC_VIA |
| 96 | snd_hda_preset_via, |
| 97 | #endif |
Wei Ni | 9a10eb2 | 2008-09-26 13:45:46 +0800 | [diff] [blame] | 98 | #ifdef CONFIG_SND_HDA_CODEC_NVHDMI |
| 99 | snd_hda_preset_nvhdmi, |
| 100 | #endif |
Harvey Harrison | 3c9a320 | 2008-02-29 11:59:26 +0100 | [diff] [blame] | 101 | NULL |
| 102 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 104 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 105 | static void hda_power_work(struct work_struct *work); |
| 106 | static void hda_keep_power_on(struct hda_codec *codec); |
| 107 | #else |
| 108 | static inline void hda_keep_power_on(struct hda_codec *codec) {} |
| 109 | #endif |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /** |
| 112 | * snd_hda_codec_read - send a command and get the response |
| 113 | * @codec: the HDA codec |
| 114 | * @nid: NID to send the command |
| 115 | * @direct: direct flag |
| 116 | * @verb: the verb to send |
| 117 | * @parm: the parameter for the verb |
| 118 | * |
| 119 | * Send a single command and read the corresponding response. |
| 120 | * |
| 121 | * Returns the obtained response value, or -1 for an error. |
| 122 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 123 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 124 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | unsigned int verb, unsigned int parm) |
| 126 | { |
| 127 | unsigned int res; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 128 | snd_hda_power_up(codec); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 129 | mutex_lock(&codec->bus->cmd_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 130 | if (!codec->bus->ops.command(codec, nid, direct, verb, parm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | res = codec->bus->ops.get_response(codec); |
| 132 | else |
| 133 | res = (unsigned int)-1; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 134 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 135 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return res; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * snd_hda_codec_write - send a single command without waiting for response |
| 141 | * @codec: the HDA codec |
| 142 | * @nid: NID to send the command |
| 143 | * @direct: direct flag |
| 144 | * @verb: the verb to send |
| 145 | * @parm: the parameter for the verb |
| 146 | * |
| 147 | * Send a single command without waiting for response. |
| 148 | * |
| 149 | * Returns 0 if successful, or a negative error code. |
| 150 | */ |
| 151 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 152 | unsigned int verb, unsigned int parm) |
| 153 | { |
| 154 | int err; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 155 | snd_hda_power_up(codec); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 156 | mutex_lock(&codec->bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 158 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 159 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return err; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * snd_hda_sequence_write - sequence writes |
| 165 | * @codec: the HDA codec |
| 166 | * @seq: VERB array to send |
| 167 | * |
| 168 | * Send the commands sequentially from the given array. |
| 169 | * The array must be terminated with NID=0. |
| 170 | */ |
| 171 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 172 | { |
| 173 | for (; seq->nid; seq++) |
| 174 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 179 | * @codec: the HDA codec |
| 180 | * @nid: NID to parse |
| 181 | * @start_id: the pointer to store the start NID |
| 182 | * |
| 183 | * Parse the NID and store the start NID of its sub-nodes. |
| 184 | * Returns the number of sub-nodes. |
| 185 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 186 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 187 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | unsigned int parm; |
| 190 | |
| 191 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
Danny Tholen | e8a7f13 | 2007-09-11 21:41:56 +0200 | [diff] [blame] | 192 | if (parm == -1) |
| 193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | *start_id = (parm >> 16) & 0x7fff; |
| 195 | return (int)(parm & 0x7fff); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * snd_hda_get_connections - get connection list |
| 200 | * @codec: the HDA codec |
| 201 | * @nid: NID to parse |
| 202 | * @conn_list: connection list array |
| 203 | * @max_conns: max. number of connections to store |
| 204 | * |
| 205 | * Parses the connection list of the given widget and stores the list |
| 206 | * of NIDs. |
| 207 | * |
| 208 | * Returns the number of connections, or a negative error code. |
| 209 | */ |
| 210 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
| 211 | hda_nid_t *conn_list, int max_conns) |
| 212 | { |
| 213 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 214 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 216 | hda_nid_t prev_nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 218 | if (snd_BUG_ON(!conn_list || max_conns <= 0)) |
| 219 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 222 | if (parm & AC_CLIST_LONG) { |
| 223 | /* long form */ |
| 224 | shift = 16; |
| 225 | num_elems = 2; |
| 226 | } else { |
| 227 | /* short form */ |
| 228 | shift = 8; |
| 229 | num_elems = 4; |
| 230 | } |
| 231 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mask = (1 << (shift-1)) - 1; |
| 233 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 234 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return 0; /* no connection */ |
| 236 | |
| 237 | if (conn_len == 1) { |
| 238 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 239 | parm = snd_hda_codec_read(codec, nid, 0, |
| 240 | AC_VERB_GET_CONNECT_LIST, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | conn_list[0] = parm & mask; |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | /* multi connection */ |
| 246 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 247 | prev_nid = 0; |
| 248 | for (i = 0; i < conn_len; i++) { |
| 249 | int range_val; |
| 250 | hda_nid_t val, n; |
| 251 | |
| 252 | if (i % num_elems == 0) |
| 253 | parm = snd_hda_codec_read(codec, nid, 0, |
| 254 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 255 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 256 | val = parm & mask; |
| 257 | parm >>= shift; |
| 258 | if (range_val) { |
| 259 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 260 | if (!prev_nid || prev_nid >= val) { |
| 261 | snd_printk(KERN_WARNING "hda_codec: " |
| 262 | "invalid dep_range_val %x:%x\n", |
| 263 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 264 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 266 | for (n = prev_nid + 1; n <= val; n++) { |
| 267 | if (conns >= max_conns) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 268 | snd_printk(KERN_ERR |
| 269 | "Too many connections\n"); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | } |
| 272 | conn_list[conns++] = n; |
| 273 | } |
| 274 | } else { |
| 275 | if (conns >= max_conns) { |
| 276 | snd_printk(KERN_ERR "Too many connections\n"); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | conn_list[conns++] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 281 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | return conns; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 289 | * @bus: the BUS |
| 290 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 291 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 292 | * |
| 293 | * Adds the given event to the queue. The events are processed in |
| 294 | * the workqueue asynchronously. Call this function in the interrupt |
| 295 | * hanlder when RIRB receives an unsolicited event. |
| 296 | * |
| 297 | * Returns 0 if successful, or a negative error code. |
| 298 | */ |
| 299 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 300 | { |
| 301 | struct hda_bus_unsolicited *unsol; |
| 302 | unsigned int wp; |
| 303 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 304 | unsol = bus->unsol; |
| 305 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | |
| 308 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 309 | unsol->wp = wp; |
| 310 | |
| 311 | wp <<= 1; |
| 312 | unsol->queue[wp] = res; |
| 313 | unsol->queue[wp + 1] = res_ex; |
| 314 | |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 315 | schedule_work(&unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* |
Wu Fengguang | 5c1d1a9 | 2008-10-07 14:17:53 +0800 | [diff] [blame] | 321 | * process queued unsolicited events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 323 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 325 | struct hda_bus_unsolicited *unsol = |
| 326 | container_of(work, struct hda_bus_unsolicited, work); |
| 327 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | struct hda_codec *codec; |
| 329 | unsigned int rp, caddr, res; |
| 330 | |
| 331 | while (unsol->rp != unsol->wp) { |
| 332 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 333 | unsol->rp = rp; |
| 334 | rp <<= 1; |
| 335 | res = unsol->queue[rp]; |
| 336 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 337 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | continue; |
| 339 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 340 | if (codec && codec->patch_ops.unsol_event) |
| 341 | codec->patch_ops.unsol_event(codec, res); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * initialize unsolicited queue |
| 347 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 348 | static int __devinit init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | struct hda_bus_unsolicited *unsol; |
| 351 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 352 | if (bus->unsol) /* already initialized */ |
| 353 | return 0; |
| 354 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 355 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 356 | if (!unsol) { |
| 357 | snd_printk(KERN_ERR "hda_codec: " |
| 358 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return -ENOMEM; |
| 360 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 361 | INIT_WORK(&unsol->work, process_unsol_events); |
| 362 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | bus->unsol = unsol; |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * destructor |
| 369 | */ |
| 370 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 371 | |
| 372 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 373 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 374 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 376 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | if (bus->unsol) { |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 379 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | kfree(bus->unsol); |
| 381 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 382 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | snd_hda_codec_free(codec); |
| 384 | } |
| 385 | if (bus->ops.private_free) |
| 386 | bus->ops.private_free(bus); |
| 387 | kfree(bus); |
| 388 | return 0; |
| 389 | } |
| 390 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 391 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
| 393 | struct hda_bus *bus = device->device_data; |
| 394 | return snd_hda_bus_free(bus); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * snd_hda_bus_new - create a HDA bus |
| 399 | * @card: the card entry |
| 400 | * @temp: the template for hda_bus information |
| 401 | * @busp: the pointer to store the created bus instance |
| 402 | * |
| 403 | * Returns 0 if successful, or a negative error code. |
| 404 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 405 | int __devinit snd_hda_bus_new(struct snd_card *card, |
| 406 | const struct hda_bus_template *temp, |
| 407 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
| 409 | struct hda_bus *bus; |
| 410 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 411 | static struct snd_device_ops dev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | .dev_free = snd_hda_bus_dev_free, |
| 413 | }; |
| 414 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 415 | if (snd_BUG_ON(!temp)) |
| 416 | return -EINVAL; |
| 417 | if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) |
| 418 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | if (busp) |
| 421 | *busp = NULL; |
| 422 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 423 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (bus == NULL) { |
| 425 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 426 | return -ENOMEM; |
| 427 | } |
| 428 | |
| 429 | bus->card = card; |
| 430 | bus->private_data = temp->private_data; |
| 431 | bus->pci = temp->pci; |
| 432 | bus->modelname = temp->modelname; |
| 433 | bus->ops = temp->ops; |
| 434 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 435 | mutex_init(&bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | INIT_LIST_HEAD(&bus->codec_list); |
| 437 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 438 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 439 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | snd_hda_bus_free(bus); |
| 441 | return err; |
| 442 | } |
| 443 | if (busp) |
| 444 | *busp = bus; |
| 445 | return 0; |
| 446 | } |
| 447 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 448 | #ifdef CONFIG_SND_HDA_GENERIC |
| 449 | #define is_generic_config(codec) \ |
| 450 | (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic")) |
| 451 | #else |
| 452 | #define is_generic_config(codec) 0 |
| 453 | #endif |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * find a matching codec preset |
| 457 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 458 | static const struct hda_codec_preset __devinit * |
| 459 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | const struct hda_codec_preset **tbl, *preset; |
| 462 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 463 | if (is_generic_config(codec)) |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 464 | return NULL; /* use the generic parser */ |
| 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | for (tbl = hda_preset_tables; *tbl; tbl++) { |
| 467 | for (preset = *tbl; preset->id; preset++) { |
| 468 | u32 mask = preset->mask; |
Marc Boucher | ca7cfae | 2008-01-22 15:32:25 +0100 | [diff] [blame] | 469 | if (preset->afg && preset->afg != codec->afg) |
| 470 | continue; |
| 471 | if (preset->mfg && preset->mfg != codec->mfg) |
| 472 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 473 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 475 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 476 | (!preset->rev || |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 477 | preset->rev == codec->revision_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return preset; |
| 479 | } |
| 480 | } |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * snd_hda_get_codec_name - store the codec name |
| 486 | */ |
| 487 | void snd_hda_get_codec_name(struct hda_codec *codec, |
| 488 | char *name, int namelen) |
| 489 | { |
| 490 | const struct hda_vendor_id *c; |
| 491 | const char *vendor = NULL; |
| 492 | u16 vendor_id = codec->vendor_id >> 16; |
| 493 | char tmp[16]; |
| 494 | |
| 495 | for (c = hda_vendor_ids; c->id; c++) { |
| 496 | if (c->id == vendor_id) { |
| 497 | vendor = c->name; |
| 498 | break; |
| 499 | } |
| 500 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 501 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | sprintf(tmp, "Generic %04x", vendor_id); |
| 503 | vendor = tmp; |
| 504 | } |
| 505 | if (codec->preset && codec->preset->name) |
| 506 | snprintf(name, namelen, "%s %s", vendor, codec->preset->name); |
| 507 | else |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 508 | snprintf(name, namelen, "%s ID %x", vendor, |
| 509 | codec->vendor_id & 0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 513 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 515 | static void __devinit setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | int i, total_nodes; |
| 518 | hda_nid_t nid; |
| 519 | |
| 520 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 521 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 522 | unsigned int func; |
| 523 | func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE); |
| 524 | switch (func & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 525 | case AC_GRP_AUDIO_FUNCTION: |
| 526 | codec->afg = nid; |
| 527 | break; |
| 528 | case AC_GRP_MODEM_FUNCTION: |
| 529 | codec->mfg = nid; |
| 530 | break; |
| 531 | default: |
| 532 | break; |
| 533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 538 | * read widget caps for each widget and store in cache |
| 539 | */ |
| 540 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 541 | { |
| 542 | int i; |
| 543 | hda_nid_t nid; |
| 544 | |
| 545 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 546 | &codec->start_nid); |
| 547 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 548 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 549 | return -ENOMEM; |
| 550 | nid = codec->start_nid; |
| 551 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 552 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 553 | AC_PAR_AUDIO_WIDGET_CAP); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 558 | static void init_hda_cache(struct hda_cache_rec *cache, |
| 559 | unsigned int record_size); |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 560 | static void free_hda_cache(struct hda_cache_rec *cache); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 561 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 562 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | * codec destructor |
| 564 | */ |
| 565 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 566 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 567 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 569 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 570 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 2525fdc | 2007-08-15 22:18:22 +0200 | [diff] [blame] | 571 | flush_scheduled_work(); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 572 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | list_del(&codec->list); |
| 574 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 575 | if (codec->patch_ops.free) |
| 576 | codec->patch_ops.free(codec); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 577 | free_hda_cache(&codec->amp_cache); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 578 | free_hda_cache(&codec->cmd_cache); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 579 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | kfree(codec); |
| 581 | } |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | /** |
| 584 | * snd_hda_codec_new - create a HDA codec |
| 585 | * @bus: the bus to assign |
| 586 | * @codec_addr: the codec address |
| 587 | * @codecp: the pointer to store the generated codec |
| 588 | * |
| 589 | * Returns 0 if successful, or a negative error code. |
| 590 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 591 | int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr, |
| 592 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
| 594 | struct hda_codec *codec; |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 595 | char component[31]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | int err; |
| 597 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 598 | if (snd_BUG_ON(!bus)) |
| 599 | return -EINVAL; |
| 600 | if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) |
| 601 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 604 | snd_printk(KERN_ERR "hda_codec: " |
| 605 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return -EBUSY; |
| 607 | } |
| 608 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 609 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (codec == NULL) { |
| 611 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 612 | return -ENOMEM; |
| 613 | } |
| 614 | |
| 615 | codec->bus = bus; |
| 616 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 617 | mutex_init(&codec->spdif_mutex); |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 618 | init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 619 | init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 621 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 622 | INIT_DELAYED_WORK(&codec->power_work, hda_power_work); |
| 623 | /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. |
| 624 | * the caller has to power down appropriatley after initialization |
| 625 | * phase. |
| 626 | */ |
| 627 | hda_keep_power_on(codec); |
| 628 | #endif |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | list_add_tail(&codec->list, &bus->codec_list); |
| 631 | bus->caddr_tbl[codec_addr] = codec; |
| 632 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 633 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 634 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 635 | if (codec->vendor_id == -1) |
| 636 | /* read again, hopefully the access method was corrected |
| 637 | * in the last read... |
| 638 | */ |
| 639 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 640 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 641 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 642 | AC_PAR_SUBSYSTEM_ID); |
| 643 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 644 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 646 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 647 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 648 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | snd_hda_codec_free(codec); |
| 650 | return -ENODEV; |
| 651 | } |
| 652 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 653 | if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) { |
| 654 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 655 | snd_hda_codec_free(codec); |
| 656 | return -ENOMEM; |
| 657 | } |
| 658 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 659 | if (!codec->subsystem_id) { |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 660 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 661 | codec->subsystem_id = |
| 662 | snd_hda_codec_read(codec, nid, 0, |
| 663 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 666 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 43ea1d4 | 2007-04-26 19:12:08 +0200 | [diff] [blame] | 667 | /* audio codec should override the mixer name */ |
| 668 | if (codec->afg || !*bus->card->mixername) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | snd_hda_get_codec_name(codec, bus->card->mixername, |
| 670 | sizeof(bus->card->mixername)); |
| 671 | |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 672 | if (is_generic_config(codec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 674 | goto patched; |
| 675 | } |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 676 | if (codec->preset && codec->preset->patch) { |
| 677 | err = codec->preset->patch(codec); |
| 678 | goto patched; |
| 679 | } |
| 680 | |
| 681 | /* call the default parser */ |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 682 | err = snd_hda_parse_generic_codec(codec); |
Takashi Iwai | 35a1e0c | 2007-10-19 08:13:40 +0200 | [diff] [blame] | 683 | if (err < 0) |
| 684 | printk(KERN_ERR "hda-codec: No codec parser is available\n"); |
Takashi Iwai | 8246761 | 2007-07-27 19:15:54 +0200 | [diff] [blame] | 685 | |
| 686 | patched: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | if (err < 0) { |
| 688 | snd_hda_codec_free(codec); |
| 689 | return err; |
| 690 | } |
| 691 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 692 | if (codec->patch_ops.unsol_event) |
| 693 | init_unsol_queue(bus); |
| 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | snd_hda_codec_proc_new(codec); |
Takashi Iwai | 2807314 | 2007-07-27 18:58:06 +0200 | [diff] [blame] | 696 | #ifdef CONFIG_SND_HDA_HWDEP |
| 697 | snd_hda_create_hwdep(codec); |
| 698 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Jaroslav Kysela | ba44368 | 2008-08-13 20:55:32 +0200 | [diff] [blame] | 700 | sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | snd_component_add(codec->bus->card, component); |
| 702 | |
| 703 | if (codecp) |
| 704 | *codecp = codec; |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 710 | * @codec: the CODEC to set up |
| 711 | * @nid: the NID to set up |
| 712 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 713 | * @channel_id: channel id to pass, zero based. |
| 714 | * @format: stream format. |
| 715 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 716 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 717 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | int channel_id, int format) |
| 719 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 720 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 721 | return; |
| 722 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 723 | snd_printdd("hda_codec_setup_stream: " |
| 724 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | nid, stream_tag, channel_id, format); |
| 726 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, |
| 727 | (stream_tag << 4) | channel_id); |
| 728 | msleep(1); |
| 729 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format); |
| 730 | } |
| 731 | |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 732 | void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid) |
| 733 | { |
| 734 | if (!nid) |
| 735 | return; |
| 736 | |
| 737 | snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid); |
| 738 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); |
| 739 | #if 0 /* keep the format */ |
| 740 | msleep(1); |
| 741 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0); |
| 742 | #endif |
| 743 | } |
| 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | /* |
| 746 | * amp access functions |
| 747 | */ |
| 748 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 749 | /* FIXME: more better hash key? */ |
| 750 | #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 752 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | /* initialize the hash table */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 755 | static void __devinit init_hda_cache(struct hda_cache_rec *cache, |
| 756 | unsigned int record_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 758 | memset(cache, 0, sizeof(*cache)); |
| 759 | memset(cache->hash, 0xff, sizeof(cache->hash)); |
| 760 | cache->record_size = record_size; |
| 761 | } |
| 762 | |
Takashi Iwai | 1fcaee6 | 2007-08-23 00:01:09 +0200 | [diff] [blame] | 763 | static void free_hda_cache(struct hda_cache_rec *cache) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 764 | { |
| 765 | kfree(cache->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* query the hash. allocate an entry if not found. */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 769 | static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, |
| 770 | u32 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 772 | u16 idx = key % (u16)ARRAY_SIZE(cache->hash); |
| 773 | u16 cur = cache->hash[idx]; |
| 774 | struct hda_cache_head *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | while (cur != 0xffff) { |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 777 | info = (struct hda_cache_head *)(cache->buffer + |
| 778 | cur * cache->record_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (info->key == key) |
| 780 | return info; |
| 781 | cur = info->next; |
| 782 | } |
| 783 | |
| 784 | /* add a new hash entry */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 785 | if (cache->num_entries >= cache->size) { |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 786 | /* reallocate the array */ |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 787 | unsigned int new_size = cache->size + 64; |
| 788 | void *new_buffer; |
| 789 | new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL); |
| 790 | if (!new_buffer) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 791 | snd_printk(KERN_ERR "hda_codec: " |
| 792 | "can't malloc amp_info\n"); |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 795 | if (cache->buffer) { |
| 796 | memcpy(new_buffer, cache->buffer, |
| 797 | cache->size * cache->record_size); |
| 798 | kfree(cache->buffer); |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 799 | } |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 800 | cache->size = new_size; |
| 801 | cache->buffer = new_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 803 | cur = cache->num_entries++; |
| 804 | info = (struct hda_cache_head *)(cache->buffer + |
| 805 | cur * cache->record_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | info->key = key; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 807 | info->val = 0; |
| 808 | info->next = cache->hash[idx]; |
| 809 | cache->hash[idx] = cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | return info; |
| 812 | } |
| 813 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 814 | /* query and allocate an amp hash entry */ |
| 815 | static inline struct hda_amp_info * |
| 816 | get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 817 | { |
| 818 | return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); |
| 819 | } |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | /* |
| 822 | * query AMP capabilities for the given widget and direction |
| 823 | */ |
Matthew Ranostay | 09a9995 | 2008-01-24 11:49:21 +0100 | [diff] [blame] | 824 | 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] | 825 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 826 | struct hda_amp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 828 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
| 829 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return 0; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 831 | if (!(info->head.val & INFO_AMP_CAPS)) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 832 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | nid = codec->afg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 834 | info->amp_caps = snd_hda_param_read(codec, nid, |
| 835 | direction == HDA_OUTPUT ? |
| 836 | AC_PAR_AMP_OUT_CAP : |
| 837 | AC_PAR_AMP_IN_CAP); |
Takashi Iwai | b75e53f | 2007-05-10 16:56:09 +0200 | [diff] [blame] | 838 | if (info->amp_caps) |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 839 | info->head.val |= INFO_AMP_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | return info->amp_caps; |
| 842 | } |
| 843 | |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 844 | int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 845 | unsigned int caps) |
| 846 | { |
| 847 | struct hda_amp_info *info; |
| 848 | |
| 849 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0)); |
| 850 | if (!info) |
| 851 | return -EINVAL; |
| 852 | info->amp_caps = caps; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 853 | info->head.val |= INFO_AMP_CAPS; |
Takashi Iwai | 897cc18 | 2007-05-29 19:01:37 +0200 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | /* |
| 858 | * read the current volume to info |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 859 | * if the cache exists, read the cache value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 861 | static unsigned int get_vol_mute(struct hda_codec *codec, |
| 862 | struct hda_amp_info *info, hda_nid_t nid, |
| 863 | int ch, int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
| 865 | u32 val, parm; |
| 866 | |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 867 | if (info->head.val & INFO_AMP_VOL(ch)) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 868 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
| 870 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 871 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 872 | parm |= index; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 873 | val = snd_hda_codec_read(codec, nid, 0, |
| 874 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | info->vol[ch] = val & 0xff; |
Takashi Iwai | 01751f5 | 2007-08-10 16:59:39 +0200 | [diff] [blame] | 876 | info->head.val |= INFO_AMP_VOL(ch); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 877 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 881 | * write the current volume in info to the h/w and update the cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | */ |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 883 | static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 884 | hda_nid_t nid, int ch, int direction, int index, |
| 885 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | { |
| 887 | u32 parm; |
| 888 | |
| 889 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 890 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 891 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
| 892 | parm |= val; |
| 893 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 894 | info->vol[ch] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 898 | * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 900 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 901 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 903 | struct hda_amp_info *info; |
| 904 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 905 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 907 | return get_vol_mute(codec, info, nid, ch, direction, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 910 | /* |
| 911 | * update the AMP value, mask = bit mask to set, val = the value |
| 912 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 913 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 914 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 916 | struct hda_amp_info *info; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 917 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 918 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
| 919 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 921 | val &= mask; |
| 922 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 923 | if (info->vol[ch] == val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 925 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return 1; |
| 927 | } |
| 928 | |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 929 | /* |
| 930 | * update the AMP stereo with the same mask and value |
| 931 | */ |
| 932 | int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, |
| 933 | int direction, int idx, int mask, int val) |
| 934 | { |
| 935 | int ch, ret = 0; |
| 936 | for (ch = 0; ch < 2; ch++) |
| 937 | ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, |
| 938 | idx, mask, val); |
| 939 | return ret; |
| 940 | } |
| 941 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 942 | #ifdef SND_HDA_NEEDS_RESUME |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 943 | /* resume the all amp commands from the cache */ |
| 944 | void snd_hda_codec_resume_amp(struct hda_codec *codec) |
| 945 | { |
| 946 | struct hda_amp_info *buffer = codec->amp_cache.buffer; |
| 947 | int i; |
| 948 | |
| 949 | for (i = 0; i < codec->amp_cache.size; i++, buffer++) { |
| 950 | u32 key = buffer->head.key; |
| 951 | hda_nid_t nid; |
| 952 | unsigned int idx, dir, ch; |
| 953 | if (!key) |
| 954 | continue; |
| 955 | nid = key & 0xff; |
| 956 | idx = (key >> 16) & 0xff; |
| 957 | dir = (key >> 24) & 0xff; |
| 958 | for (ch = 0; ch < 2; ch++) { |
| 959 | if (!(buffer->head.val & INFO_AMP_VOL(ch))) |
| 960 | continue; |
| 961 | put_vol_mute(codec, buffer, nid, ch, dir, idx, |
| 962 | buffer->vol[ch]); |
| 963 | } |
| 964 | } |
| 965 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 966 | #endif /* SND_HDA_NEEDS_RESUME */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | /* volume */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 969 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 970 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | { |
| 972 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 973 | u16 nid = get_amp_nid(kcontrol); |
| 974 | u8 chs = get_amp_channels(kcontrol); |
| 975 | int dir = get_amp_direction(kcontrol); |
| 976 | u32 caps; |
| 977 | |
| 978 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 979 | /* num steps */ |
| 980 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 981 | if (!caps) { |
| 982 | printk(KERN_WARNING "hda_codec: " |
Takashi Iwai | 9c8f2ab | 2008-01-11 16:12:23 +0100 | [diff] [blame] | 983 | "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid, |
| 984 | kcontrol->id.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | return -EINVAL; |
| 986 | } |
| 987 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 988 | uinfo->count = chs == 3 ? 2 : 1; |
| 989 | uinfo->value.integer.min = 0; |
| 990 | uinfo->value.integer.max = caps; |
| 991 | return 0; |
| 992 | } |
| 993 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 994 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 995 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | { |
| 997 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 998 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 999 | int chs = get_amp_channels(kcontrol); |
| 1000 | int dir = get_amp_direction(kcontrol); |
| 1001 | int idx = get_amp_index(kcontrol); |
| 1002 | long *valp = ucontrol->value.integer.value; |
| 1003 | |
| 1004 | if (chs & 1) |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1005 | *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) |
| 1006 | & HDA_AMP_VOLMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | if (chs & 2) |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1008 | *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) |
| 1009 | & HDA_AMP_VOLMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
| 1012 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1013 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 1014 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | { |
| 1016 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1017 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1018 | int chs = get_amp_channels(kcontrol); |
| 1019 | int dir = get_amp_direction(kcontrol); |
| 1020 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | long *valp = ucontrol->value.integer.value; |
| 1022 | int change = 0; |
| 1023 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1024 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1025 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1026 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
| 1027 | 0x7f, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1028 | valp++; |
| 1029 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1030 | if (chs & 2) |
| 1031 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1032 | 0x7f, *valp); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1033 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | return change; |
| 1035 | } |
| 1036 | |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1037 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 1038 | unsigned int size, unsigned int __user *_tlv) |
| 1039 | { |
| 1040 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1041 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1042 | int dir = get_amp_direction(kcontrol); |
| 1043 | u32 caps, val1, val2; |
| 1044 | |
| 1045 | if (size < 4 * sizeof(unsigned int)) |
| 1046 | return -ENOMEM; |
| 1047 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1048 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 1049 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1050 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
| 1051 | val1 = ((int)val1) * ((int)val2); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 1052 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 1053 | return -EFAULT; |
| 1054 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 1055 | return -EFAULT; |
| 1056 | if (put_user(val1, _tlv + 2)) |
| 1057 | return -EFAULT; |
| 1058 | if (put_user(val2, _tlv + 3)) |
| 1059 | return -EFAULT; |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1063 | /* |
| 1064 | * set (static) TLV for virtual master volume; recalculated as max 0dB |
| 1065 | */ |
| 1066 | void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 1067 | unsigned int *tlv) |
| 1068 | { |
| 1069 | u32 caps; |
| 1070 | int nums, step; |
| 1071 | |
| 1072 | caps = query_amp_caps(codec, nid, dir); |
| 1073 | nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 1074 | step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 1075 | step = (step + 1) * 25; |
| 1076 | tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 1077 | tlv[1] = 2 * sizeof(unsigned int); |
| 1078 | tlv[2] = -nums * step; |
| 1079 | tlv[3] = step; |
| 1080 | } |
| 1081 | |
| 1082 | /* find a mixer control element with the given name */ |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1083 | static struct snd_kcontrol * |
| 1084 | _snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 1085 | const char *name, int idx) |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1086 | { |
| 1087 | struct snd_ctl_elem_id id; |
| 1088 | memset(&id, 0, sizeof(id)); |
| 1089 | id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1090 | id.index = idx; |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1091 | strcpy(id.name, name); |
| 1092 | return snd_ctl_find_id(codec->bus->card, &id); |
| 1093 | } |
| 1094 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1095 | struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, |
| 1096 | const char *name) |
| 1097 | { |
| 1098 | return _snd_hda_find_mixer_ctl(codec, name, 0); |
| 1099 | } |
| 1100 | |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1101 | /* create a virtual master control and add slaves */ |
| 1102 | int snd_hda_add_vmaster(struct hda_codec *codec, char *name, |
| 1103 | unsigned int *tlv, const char **slaves) |
| 1104 | { |
| 1105 | struct snd_kcontrol *kctl; |
| 1106 | const char **s; |
| 1107 | int err; |
| 1108 | |
Takashi Iwai | 2f08554 | 2008-02-22 18:43:50 +0100 | [diff] [blame] | 1109 | for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++) |
| 1110 | ; |
| 1111 | if (!*s) { |
| 1112 | snd_printdd("No slave found for %s\n", name); |
| 1113 | return 0; |
| 1114 | } |
Takashi Iwai | 2134ea4 | 2008-01-10 16:53:55 +0100 | [diff] [blame] | 1115 | kctl = snd_ctl_make_virtual_master(name, tlv); |
| 1116 | if (!kctl) |
| 1117 | return -ENOMEM; |
| 1118 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1119 | if (err < 0) |
| 1120 | return err; |
| 1121 | |
| 1122 | for (s = slaves; *s; s++) { |
| 1123 | struct snd_kcontrol *sctl; |
| 1124 | |
| 1125 | sctl = snd_hda_find_mixer_ctl(codec, *s); |
| 1126 | if (!sctl) { |
| 1127 | snd_printdd("Cannot find slave %s, skipped\n", *s); |
| 1128 | continue; |
| 1129 | } |
| 1130 | err = snd_ctl_add_slave(kctl, sctl); |
| 1131 | if (err < 0) |
| 1132 | return err; |
| 1133 | } |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /* switch */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1138 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 1139 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
| 1141 | int chs = get_amp_channels(kcontrol); |
| 1142 | |
| 1143 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1144 | uinfo->count = chs == 3 ? 2 : 1; |
| 1145 | uinfo->value.integer.min = 0; |
| 1146 | uinfo->value.integer.max = 1; |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1150 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 1151 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | { |
| 1153 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1154 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1155 | int chs = get_amp_channels(kcontrol); |
| 1156 | int dir = get_amp_direction(kcontrol); |
| 1157 | int idx = get_amp_index(kcontrol); |
| 1158 | long *valp = ucontrol->value.integer.value; |
| 1159 | |
| 1160 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1161 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1162 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1164 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1165 | HDA_AMP_MUTE) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1169 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 1170 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
| 1172 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1173 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 1174 | int chs = get_amp_channels(kcontrol); |
| 1175 | int dir = get_amp_direction(kcontrol); |
| 1176 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | long *valp = ucontrol->value.integer.value; |
| 1178 | int change = 0; |
| 1179 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1180 | snd_hda_power_up(codec); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1181 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1182 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1183 | HDA_AMP_MUTE, |
| 1184 | *valp ? 0 : HDA_AMP_MUTE); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 1185 | valp++; |
| 1186 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 1187 | if (chs & 2) |
| 1188 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1189 | HDA_AMP_MUTE, |
| 1190 | *valp ? 0 : HDA_AMP_MUTE); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1191 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1192 | if (codec->patch_ops.check_power_status) |
| 1193 | codec->patch_ops.check_power_status(codec, nid); |
| 1194 | #endif |
| 1195 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | return change; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1200 | * bound volume controls |
| 1201 | * |
| 1202 | * bind multiple volumes (# indices, from 0) |
| 1203 | */ |
| 1204 | |
| 1205 | #define AMP_VAL_IDX_SHIFT 19 |
| 1206 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 1207 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1208 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 1209 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1210 | { |
| 1211 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1212 | unsigned long pval; |
| 1213 | int err; |
| 1214 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1215 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1216 | pval = kcontrol->private_value; |
| 1217 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 1218 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 1219 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1220 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1221 | return err; |
| 1222 | } |
| 1223 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1224 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 1225 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1226 | { |
| 1227 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1228 | unsigned long pval; |
| 1229 | int i, indices, err = 0, change = 0; |
| 1230 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1231 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1232 | pval = kcontrol->private_value; |
| 1233 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 1234 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1235 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 1236 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1237 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 1238 | if (err < 0) |
| 1239 | break; |
| 1240 | change |= err; |
| 1241 | } |
| 1242 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1243 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 1244 | return err < 0 ? err : change; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1248 | * generic bound volume/swtich controls |
| 1249 | */ |
| 1250 | int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, |
| 1251 | struct snd_ctl_elem_info *uinfo) |
| 1252 | { |
| 1253 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1254 | struct hda_bind_ctls *c; |
| 1255 | int err; |
| 1256 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1257 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1258 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1259 | kcontrol->private_value = *c->values; |
| 1260 | err = c->ops->info(kcontrol, uinfo); |
| 1261 | kcontrol->private_value = (long)c; |
| 1262 | mutex_unlock(&codec->spdif_mutex); |
| 1263 | return err; |
| 1264 | } |
| 1265 | |
| 1266 | int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, |
| 1267 | struct snd_ctl_elem_value *ucontrol) |
| 1268 | { |
| 1269 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1270 | struct hda_bind_ctls *c; |
| 1271 | int err; |
| 1272 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1273 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1274 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1275 | kcontrol->private_value = *c->values; |
| 1276 | err = c->ops->get(kcontrol, ucontrol); |
| 1277 | kcontrol->private_value = (long)c; |
| 1278 | mutex_unlock(&codec->spdif_mutex); |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
| 1282 | int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, |
| 1283 | struct snd_ctl_elem_value *ucontrol) |
| 1284 | { |
| 1285 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1286 | struct hda_bind_ctls *c; |
| 1287 | unsigned long *vals; |
| 1288 | int err = 0, change = 0; |
| 1289 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1290 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1291 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1292 | for (vals = c->values; *vals; vals++) { |
| 1293 | kcontrol->private_value = *vals; |
| 1294 | err = c->ops->put(kcontrol, ucontrol); |
| 1295 | if (err < 0) |
| 1296 | break; |
| 1297 | change |= err; |
| 1298 | } |
| 1299 | kcontrol->private_value = (long)c; |
| 1300 | mutex_unlock(&codec->spdif_mutex); |
| 1301 | return err < 0 ? err : change; |
| 1302 | } |
| 1303 | |
| 1304 | int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 1305 | unsigned int size, unsigned int __user *tlv) |
| 1306 | { |
| 1307 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1308 | struct hda_bind_ctls *c; |
| 1309 | int err; |
| 1310 | |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1311 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Serge A. Suchkov | 14c65f9 | 2008-02-22 18:43:16 +0100 | [diff] [blame] | 1312 | c = (struct hda_bind_ctls *)kcontrol->private_value; |
Takashi Iwai | 532d538 | 2007-07-27 19:02:40 +0200 | [diff] [blame] | 1313 | kcontrol->private_value = *c->values; |
| 1314 | err = c->ops->tlv(kcontrol, op_flag, size, tlv); |
| 1315 | kcontrol->private_value = (long)c; |
| 1316 | mutex_unlock(&codec->spdif_mutex); |
| 1317 | return err; |
| 1318 | } |
| 1319 | |
| 1320 | struct hda_ctl_ops snd_hda_bind_vol = { |
| 1321 | .info = snd_hda_mixer_amp_volume_info, |
| 1322 | .get = snd_hda_mixer_amp_volume_get, |
| 1323 | .put = snd_hda_mixer_amp_volume_put, |
| 1324 | .tlv = snd_hda_mixer_amp_tlv |
| 1325 | }; |
| 1326 | |
| 1327 | struct hda_ctl_ops snd_hda_bind_sw = { |
| 1328 | .info = snd_hda_mixer_amp_switch_info, |
| 1329 | .get = snd_hda_mixer_amp_switch_get, |
| 1330 | .put = snd_hda_mixer_amp_switch_put, |
| 1331 | .tlv = snd_hda_mixer_amp_tlv |
| 1332 | }; |
| 1333 | |
| 1334 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | * SPDIF out controls |
| 1336 | */ |
| 1337 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1338 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 1339 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | { |
| 1341 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 1342 | uinfo->count = 1; |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1346 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 1347 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | { |
| 1349 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1350 | IEC958_AES0_NONAUDIO | |
| 1351 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 1352 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 1353 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 1354 | IEC958_AES1_CON_ORIGINAL; |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1358 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 1359 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
| 1361 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1362 | IEC958_AES0_NONAUDIO | |
| 1363 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1367 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 1368 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | { |
| 1370 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1371 | |
| 1372 | ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff; |
| 1373 | ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff; |
| 1374 | ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff; |
| 1375 | ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff; |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 1381 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 1382 | */ |
| 1383 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 1384 | { |
| 1385 | unsigned short val = 0; |
| 1386 | |
| 1387 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1388 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1390 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1392 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 1393 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 1394 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1396 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 1397 | IEC958_AES0_CON_EMPHASIS_5015) |
| 1398 | val |= AC_DIG1_EMPHASIS; |
| 1399 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 1400 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1402 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 1404 | } |
| 1405 | return val; |
| 1406 | } |
| 1407 | |
| 1408 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 1409 | */ |
| 1410 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 1411 | { |
| 1412 | unsigned int sbits = 0; |
| 1413 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1414 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1416 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 1418 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1419 | if (sbits & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 1421 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1422 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1424 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1426 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 1428 | sbits |= val & (0x7f << 8); |
| 1429 | } |
| 1430 | return sbits; |
| 1431 | } |
| 1432 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1433 | /* set digital convert verbs both for the given NID and its slaves */ |
| 1434 | static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, |
| 1435 | int verb, int val) |
| 1436 | { |
| 1437 | hda_nid_t *d; |
| 1438 | |
| 1439 | snd_hda_codec_write(codec, nid, 0, verb, val); |
| 1440 | d = codec->slave_dig_outs; |
| 1441 | if (!d) |
| 1442 | return; |
| 1443 | for (; *d; d++) |
| 1444 | snd_hda_codec_write(codec, *d, 0, verb, val); |
| 1445 | } |
| 1446 | |
| 1447 | static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, |
| 1448 | int dig1, int dig2) |
| 1449 | { |
| 1450 | if (dig1 != -1) |
| 1451 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); |
| 1452 | if (dig2 != -1) |
| 1453 | set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); |
| 1454 | } |
| 1455 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1456 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 1457 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | { |
| 1459 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1460 | hda_nid_t nid = kcontrol->private_value; |
| 1461 | unsigned short val; |
| 1462 | int change; |
| 1463 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1464 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | codec->spdif_status = ucontrol->value.iec958.status[0] | |
| 1466 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 1467 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 1468 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
| 1469 | val = convert_from_spdif_status(codec->spdif_status); |
| 1470 | val |= codec->spdif_ctls & 1; |
| 1471 | change = codec->spdif_ctls != val; |
| 1472 | codec->spdif_ctls = val; |
| 1473 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1474 | if (change) |
| 1475 | set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1477 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | return change; |
| 1479 | } |
| 1480 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 1481 | #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1483 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 1484 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | { |
| 1486 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1487 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1488 | ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1492 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 1493 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
| 1495 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1496 | hda_nid_t nid = kcontrol->private_value; |
| 1497 | unsigned short val; |
| 1498 | int change; |
| 1499 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1500 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1501 | val = codec->spdif_ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1503 | val |= AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | change = codec->spdif_ctls != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1505 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | codec->spdif_ctls = val; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 1507 | set_dig_out_convert(codec, nid, val & 0xff, -1); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1508 | /* unmute amp switch (if any) */ |
| 1509 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
Takashi Iwai | 47fd830 | 2007-08-10 17:11:07 +0200 | [diff] [blame] | 1510 | (val & AC_DIG1_ENABLE)) |
| 1511 | snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, |
| 1512 | HDA_AMP_MUTE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1514 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | return change; |
| 1516 | } |
| 1517 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1518 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | { |
| 1520 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1521 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1522 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), |
| 1523 | .info = snd_hda_spdif_mask_info, |
| 1524 | .get = snd_hda_spdif_cmask_get, |
| 1525 | }, |
| 1526 | { |
| 1527 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1528 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1529 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), |
| 1530 | .info = snd_hda_spdif_mask_info, |
| 1531 | .get = snd_hda_spdif_pmask_get, |
| 1532 | }, |
| 1533 | { |
| 1534 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1535 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 1536 | .info = snd_hda_spdif_mask_info, |
| 1537 | .get = snd_hda_spdif_default_get, |
| 1538 | .put = snd_hda_spdif_default_put, |
| 1539 | }, |
| 1540 | { |
| 1541 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1542 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), |
| 1543 | .info = snd_hda_spdif_out_switch_info, |
| 1544 | .get = snd_hda_spdif_out_switch_get, |
| 1545 | .put = snd_hda_spdif_out_switch_put, |
| 1546 | }, |
| 1547 | { } /* end */ |
| 1548 | }; |
| 1549 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1550 | #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */ |
| 1551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | /** |
| 1553 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls |
| 1554 | * @codec: the HDA codec |
| 1555 | * @nid: audio out widget NID |
| 1556 | * |
| 1557 | * Creates controls related with the SPDIF output. |
| 1558 | * Called from each patch supporting the SPDIF out. |
| 1559 | * |
| 1560 | * Returns 0 if successful, or a negative error code. |
| 1561 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 1562 | int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
| 1564 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1565 | struct snd_kcontrol *kctl; |
| 1566 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1567 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1569 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
| 1570 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch", |
| 1571 | idx)) |
| 1572 | break; |
| 1573 | } |
| 1574 | if (idx >= SPDIF_MAX_IDX) { |
| 1575 | printk(KERN_ERR "hda_codec: too many IEC958 outputs\n"); |
| 1576 | return -EBUSY; |
| 1577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 1579 | kctl = snd_ctl_new1(dig_mix, codec); |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1580 | kctl->id.index = idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | kctl->private_value = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1582 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1583 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | return err; |
| 1585 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1586 | codec->spdif_ctls = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1587 | snd_hda_codec_read(codec, nid, 0, |
| 1588 | AC_VERB_GET_DIGI_CONVERT_1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 1594 | * SPDIF sharing with analog output |
| 1595 | */ |
| 1596 | static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, |
| 1597 | struct snd_ctl_elem_value *ucontrol) |
| 1598 | { |
| 1599 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 1600 | ucontrol->value.integer.value[0] = mout->share_spdif; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, |
| 1605 | struct snd_ctl_elem_value *ucontrol) |
| 1606 | { |
| 1607 | struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); |
| 1608 | mout->share_spdif = !!ucontrol->value.integer.value[0]; |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | static struct snd_kcontrol_new spdif_share_sw = { |
| 1613 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1614 | .name = "IEC958 Default PCM Playback Switch", |
| 1615 | .info = snd_ctl_boolean_mono_info, |
| 1616 | .get = spdif_share_sw_get, |
| 1617 | .put = spdif_share_sw_put, |
| 1618 | }; |
| 1619 | |
| 1620 | int snd_hda_create_spdif_share_sw(struct hda_codec *codec, |
| 1621 | struct hda_multi_out *mout) |
| 1622 | { |
| 1623 | if (!mout->dig_out_nid) |
| 1624 | return 0; |
| 1625 | /* ATTENTION: here mout is passed as private_data, instead of codec */ |
| 1626 | return snd_ctl_add(codec->bus->card, |
| 1627 | snd_ctl_new1(&spdif_share_sw, mout)); |
| 1628 | } |
| 1629 | |
| 1630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | * SPDIF input |
| 1632 | */ |
| 1633 | |
| 1634 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 1635 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1636 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 1637 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
| 1639 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1640 | |
| 1641 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1645 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 1646 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | { |
| 1648 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1649 | hda_nid_t nid = kcontrol->private_value; |
| 1650 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 1651 | int change; |
| 1652 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1653 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | change = codec->spdif_in_enable != val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1655 | if (change) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | codec->spdif_in_enable = val; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1657 | snd_hda_codec_write_cache(codec, nid, 0, |
| 1658 | AC_VERB_SET_DIGI_CONVERT_1, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1660 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | return change; |
| 1662 | } |
| 1663 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1664 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 1665 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1668 | hda_nid_t nid = kcontrol->private_value; |
| 1669 | unsigned short val; |
| 1670 | unsigned int sbits; |
| 1671 | |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1672 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | sbits = convert_to_spdif_status(val); |
| 1674 | ucontrol->value.iec958.status[0] = sbits; |
| 1675 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 1676 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 1677 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1681 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | { |
| 1683 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1684 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), |
| 1685 | .info = snd_hda_spdif_in_switch_info, |
| 1686 | .get = snd_hda_spdif_in_switch_get, |
| 1687 | .put = snd_hda_spdif_in_switch_put, |
| 1688 | }, |
| 1689 | { |
| 1690 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1691 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1692 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 1693 | .info = snd_hda_spdif_mask_info, |
| 1694 | .get = snd_hda_spdif_in_status_get, |
| 1695 | }, |
| 1696 | { } /* end */ |
| 1697 | }; |
| 1698 | |
| 1699 | /** |
| 1700 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 1701 | * @codec: the HDA codec |
| 1702 | * @nid: audio in widget NID |
| 1703 | * |
| 1704 | * Creates controls related with the SPDIF input. |
| 1705 | * Called from each patch supporting the SPDIF in. |
| 1706 | * |
| 1707 | * Returns 0 if successful, or a negative error code. |
| 1708 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 1709 | 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] | 1710 | { |
| 1711 | int err; |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1712 | struct snd_kcontrol *kctl; |
| 1713 | struct snd_kcontrol_new *dig_mix; |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1714 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
Takashi Iwai | 09f9970 | 2008-02-04 12:31:13 +0100 | [diff] [blame] | 1716 | for (idx = 0; idx < SPDIF_MAX_IDX; idx++) { |
| 1717 | if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch", |
| 1718 | idx)) |
| 1719 | break; |
| 1720 | } |
| 1721 | if (idx >= SPDIF_MAX_IDX) { |
| 1722 | printk(KERN_ERR "hda_codec: too many IEC958 inputs\n"); |
| 1723 | return -EBUSY; |
| 1724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 1726 | kctl = snd_ctl_new1(dig_mix, codec); |
| 1727 | kctl->private_value = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1728 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1729 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | return err; |
| 1731 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1732 | codec->spdif_in_enable = |
Andrew Paprocki | 3982d17 | 2007-12-19 12:13:44 +0100 | [diff] [blame] | 1733 | snd_hda_codec_read(codec, nid, 0, |
| 1734 | AC_VERB_GET_DIGI_CONVERT_1, 0) & |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1735 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | return 0; |
| 1737 | } |
| 1738 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1739 | #ifdef SND_HDA_NEEDS_RESUME |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 1740 | /* |
| 1741 | * command cache |
| 1742 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1744 | /* build a 32bit cache key with the widget id and the command parameter */ |
| 1745 | #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) |
| 1746 | #define get_cmd_cache_nid(key) ((key) & 0xff) |
| 1747 | #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) |
| 1748 | |
| 1749 | /** |
| 1750 | * snd_hda_codec_write_cache - send a single command with caching |
| 1751 | * @codec: the HDA codec |
| 1752 | * @nid: NID to send the command |
| 1753 | * @direct: direct flag |
| 1754 | * @verb: the verb to send |
| 1755 | * @parm: the parameter for the verb |
| 1756 | * |
| 1757 | * Send a single command without waiting for response. |
| 1758 | * |
| 1759 | * Returns 0 if successful, or a negative error code. |
| 1760 | */ |
| 1761 | int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, |
| 1762 | int direct, unsigned int verb, unsigned int parm) |
| 1763 | { |
| 1764 | int err; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1765 | snd_hda_power_up(codec); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1766 | mutex_lock(&codec->bus->cmd_mutex); |
| 1767 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
| 1768 | if (!err) { |
| 1769 | struct hda_cache_head *c; |
| 1770 | u32 key = build_cmd_cache_key(nid, verb); |
| 1771 | c = get_alloc_hash(&codec->cmd_cache, key); |
| 1772 | if (c) |
| 1773 | c->val = parm; |
| 1774 | } |
| 1775 | mutex_unlock(&codec->bus->cmd_mutex); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1776 | snd_hda_power_down(codec); |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1777 | return err; |
| 1778 | } |
| 1779 | |
| 1780 | /* resume the all commands from the cache */ |
| 1781 | void snd_hda_codec_resume_cache(struct hda_codec *codec) |
| 1782 | { |
| 1783 | struct hda_cache_head *buffer = codec->cmd_cache.buffer; |
| 1784 | int i; |
| 1785 | |
| 1786 | for (i = 0; i < codec->cmd_cache.size; i++, buffer++) { |
| 1787 | u32 key = buffer->key; |
| 1788 | if (!key) |
| 1789 | continue; |
| 1790 | snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, |
| 1791 | get_cmd_cache_cmd(key), buffer->val); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * snd_hda_sequence_write_cache - sequence writes with caching |
| 1797 | * @codec: the HDA codec |
| 1798 | * @seq: VERB array to send |
| 1799 | * |
| 1800 | * Send the commands sequentially from the given array. |
| 1801 | * Thte commands are recorded on cache for power-save and resume. |
| 1802 | * The array must be terminated with NID=0. |
| 1803 | */ |
| 1804 | void snd_hda_sequence_write_cache(struct hda_codec *codec, |
| 1805 | const struct hda_verb *seq) |
| 1806 | { |
| 1807 | for (; seq->nid; seq++) |
| 1808 | snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, |
| 1809 | seq->param); |
| 1810 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1811 | #endif /* SND_HDA_NEEDS_RESUME */ |
Takashi Iwai | b3ac563 | 2007-08-10 17:03:40 +0200 | [diff] [blame] | 1812 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1813 | /* |
| 1814 | * set power state of the codec |
| 1815 | */ |
| 1816 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 1817 | unsigned int power_state) |
| 1818 | { |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1819 | hda_nid_t nid; |
| 1820 | int i; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1821 | |
| 1822 | snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE, |
| 1823 | power_state); |
Marc Boucher | d2595d8 | 2008-01-22 15:23:30 +0100 | [diff] [blame] | 1824 | msleep(10); /* partial workaround for "azx_get_response timeout" */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1825 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1826 | nid = codec->start_nid; |
| 1827 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
Takashi Iwai | 7eba5c9 | 2007-11-14 14:53:42 +0100 | [diff] [blame] | 1828 | unsigned int wcaps = get_wcaps(codec, nid); |
| 1829 | if (wcaps & AC_WCAP_POWER) { |
| 1830 | unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >> |
| 1831 | AC_WCAP_TYPE_SHIFT; |
| 1832 | if (wid_type == AC_WID_PIN) { |
| 1833 | unsigned int pincap; |
| 1834 | /* |
| 1835 | * don't power down the widget if it controls |
| 1836 | * eapd and EAPD_BTLENABLE is set. |
| 1837 | */ |
| 1838 | pincap = snd_hda_param_read(codec, nid, |
| 1839 | AC_PAR_PIN_CAP); |
| 1840 | if (pincap & AC_PINCAP_EAPD) { |
| 1841 | int eapd = snd_hda_codec_read(codec, |
| 1842 | nid, 0, |
| 1843 | AC_VERB_GET_EAPD_BTLENABLE, 0); |
| 1844 | eapd &= 0x02; |
| 1845 | if (power_state == AC_PWRST_D3 && eapd) |
| 1846 | continue; |
| 1847 | } |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 1848 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1849 | snd_hda_codec_write(codec, nid, 0, |
| 1850 | AC_VERB_SET_POWER_STATE, |
| 1851 | power_state); |
Takashi Iwai | 1194b5b | 2007-10-10 10:04:26 +0200 | [diff] [blame] | 1852 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1855 | if (power_state == AC_PWRST_D0) { |
| 1856 | unsigned long end_time; |
| 1857 | int state; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1858 | msleep(10); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1859 | /* wait until the codec reachs to D0 */ |
| 1860 | end_time = jiffies + msecs_to_jiffies(500); |
| 1861 | do { |
| 1862 | state = snd_hda_codec_read(codec, fg, 0, |
| 1863 | AC_VERB_GET_POWER_STATE, 0); |
| 1864 | if (state == power_state) |
| 1865 | break; |
| 1866 | msleep(1); |
| 1867 | } while (time_after_eq(end_time, jiffies)); |
| 1868 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1871 | #ifdef SND_HDA_NEEDS_RESUME |
| 1872 | /* |
| 1873 | * call suspend and power-down; used both from PM and power-save |
| 1874 | */ |
| 1875 | static void hda_call_codec_suspend(struct hda_codec *codec) |
| 1876 | { |
| 1877 | if (codec->patch_ops.suspend) |
| 1878 | codec->patch_ops.suspend(codec, PMSG_SUSPEND); |
| 1879 | hda_set_power_state(codec, |
| 1880 | codec->afg ? codec->afg : codec->mfg, |
| 1881 | AC_PWRST_D3); |
| 1882 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 1883 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | 95e99fd | 2007-08-13 15:29:04 +0200 | [diff] [blame] | 1884 | codec->power_on = 0; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 1885 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1886 | #endif |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * kick up codec; used both from PM and power-save |
| 1891 | */ |
| 1892 | static void hda_call_codec_resume(struct hda_codec *codec) |
| 1893 | { |
| 1894 | hda_set_power_state(codec, |
| 1895 | codec->afg ? codec->afg : codec->mfg, |
| 1896 | AC_PWRST_D0); |
| 1897 | if (codec->patch_ops.resume) |
| 1898 | codec->patch_ops.resume(codec); |
| 1899 | else { |
Takashi Iwai | 9d99f31 | 2007-08-14 15:15:52 +0200 | [diff] [blame] | 1900 | if (codec->patch_ops.init) |
| 1901 | codec->patch_ops.init(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1902 | snd_hda_codec_resume_amp(codec); |
| 1903 | snd_hda_codec_resume_cache(codec); |
| 1904 | } |
| 1905 | } |
| 1906 | #endif /* SND_HDA_NEEDS_RESUME */ |
| 1907 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | /** |
| 1910 | * snd_hda_build_controls - build mixer controls |
| 1911 | * @bus: the BUS |
| 1912 | * |
| 1913 | * Creates mixer controls for each codec included in the bus. |
| 1914 | * |
| 1915 | * Returns 0 if successful, otherwise a negative error code. |
| 1916 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1917 | int __devinit snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1919 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1921 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1922 | int err = 0; |
| 1923 | /* fake as if already powered-on */ |
| 1924 | hda_keep_power_on(codec); |
| 1925 | /* then fire up */ |
| 1926 | hda_set_power_state(codec, |
| 1927 | codec->afg ? codec->afg : codec->mfg, |
| 1928 | AC_PWRST_D0); |
| 1929 | /* continue to initialize... */ |
| 1930 | if (codec->patch_ops.init) |
| 1931 | err = codec->patch_ops.init(codec); |
| 1932 | if (!err && codec->patch_ops.build_controls) |
| 1933 | err = codec->patch_ops.build_controls(codec); |
| 1934 | snd_hda_power_down(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | if (err < 0) |
| 1936 | return err; |
| 1937 | } |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | return 0; |
| 1940 | } |
| 1941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | /* |
| 1943 | * stream formats |
| 1944 | */ |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1945 | struct hda_rate_tbl { |
| 1946 | unsigned int hz; |
| 1947 | unsigned int alsa_bits; |
| 1948 | unsigned int hda_fmt; |
| 1949 | }; |
| 1950 | |
| 1951 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */ |
| 1956 | { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */ |
| 1957 | { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */ |
| 1958 | { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */ |
| 1959 | { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */ |
| 1960 | { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */ |
| 1961 | { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */ |
| 1962 | { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */ |
| 1963 | { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */ |
| 1964 | { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */ |
| 1965 | { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */ |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 1966 | #define AC_PAR_PCM_RATE_BITS 11 |
| 1967 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 1968 | |
| 1969 | /* not autodetected value */ |
| 1970 | { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 1971 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1972 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | }; |
| 1974 | |
| 1975 | /** |
| 1976 | * snd_hda_calc_stream_format - calculate format bitset |
| 1977 | * @rate: the sample rate |
| 1978 | * @channels: the number of channels |
| 1979 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 1980 | * @maxbps: the max. bps |
| 1981 | * |
| 1982 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 1983 | * |
| 1984 | * Return zero if invalid. |
| 1985 | */ |
| 1986 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 1987 | unsigned int channels, |
| 1988 | unsigned int format, |
| 1989 | unsigned int maxbps) |
| 1990 | { |
| 1991 | int i; |
| 1992 | unsigned int val = 0; |
| 1993 | |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1994 | for (i = 0; rate_bits[i].hz; i++) |
| 1995 | if (rate_bits[i].hz == rate) { |
| 1996 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | break; |
| 1998 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1999 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | snd_printdd("invalid rate %d\n", rate); |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | if (channels == 0 || channels > 8) { |
| 2005 | snd_printdd("invalid channels %d\n", channels); |
| 2006 | return 0; |
| 2007 | } |
| 2008 | val |= channels - 1; |
| 2009 | |
| 2010 | switch (snd_pcm_format_width(format)) { |
| 2011 | case 8: val |= 0x00; break; |
| 2012 | case 16: val |= 0x10; break; |
| 2013 | case 20: |
| 2014 | case 24: |
| 2015 | case 32: |
| 2016 | if (maxbps >= 32) |
| 2017 | val |= 0x40; |
| 2018 | else if (maxbps >= 24) |
| 2019 | val |= 0x30; |
| 2020 | else |
| 2021 | val |= 0x20; |
| 2022 | break; |
| 2023 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2024 | snd_printdd("invalid format width %d\n", |
| 2025 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | return 0; |
| 2027 | } |
| 2028 | |
| 2029 | return val; |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 2034 | * @codec: the HDA codec |
| 2035 | * @nid: NID to query |
| 2036 | * @ratesp: the pointer to store the detected rate bitflags |
| 2037 | * @formatsp: the pointer to store the detected formats |
| 2038 | * @bpsp: the pointer to store the detected format widths |
| 2039 | * |
| 2040 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 2041 | * or @bsps argument is ignored. |
| 2042 | * |
| 2043 | * Returns 0 if successful, otherwise a negative error code. |
| 2044 | */ |
| 2045 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
| 2046 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 2047 | { |
| 2048 | int i; |
| 2049 | unsigned int val, streams; |
| 2050 | |
| 2051 | val = 0; |
| 2052 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2053 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 2055 | if (val == -1) |
| 2056 | return -EIO; |
| 2057 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2058 | if (!val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 2060 | |
| 2061 | if (ratesp) { |
| 2062 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2063 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | if (val & (1 << i)) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2065 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | } |
| 2067 | *ratesp = rates; |
| 2068 | } |
| 2069 | |
| 2070 | if (formatsp || bpsp) { |
| 2071 | u64 formats = 0; |
| 2072 | unsigned int bps; |
| 2073 | unsigned int wcaps; |
| 2074 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2075 | wcaps = get_wcaps(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 2077 | if (streams == -1) |
| 2078 | return -EIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2079 | if (!streams) { |
| 2080 | streams = snd_hda_param_read(codec, codec->afg, |
| 2081 | AC_PAR_STREAM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | if (streams == -1) |
| 2083 | return -EIO; |
| 2084 | } |
| 2085 | |
| 2086 | bps = 0; |
| 2087 | if (streams & AC_SUPFMT_PCM) { |
| 2088 | if (val & AC_SUPPCM_BITS_8) { |
| 2089 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 2090 | bps = 8; |
| 2091 | } |
| 2092 | if (val & AC_SUPPCM_BITS_16) { |
| 2093 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 2094 | bps = 16; |
| 2095 | } |
| 2096 | if (wcaps & AC_WCAP_DIGITAL) { |
| 2097 | if (val & AC_SUPPCM_BITS_32) |
| 2098 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 2099 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 2100 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 2101 | if (val & AC_SUPPCM_BITS_24) |
| 2102 | bps = 24; |
| 2103 | else if (val & AC_SUPPCM_BITS_20) |
| 2104 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2105 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 2106 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 2108 | if (val & AC_SUPPCM_BITS_32) |
| 2109 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | else if (val & AC_SUPPCM_BITS_24) |
| 2111 | bps = 24; |
Nicolas Graziano | 33ef765 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 2112 | else if (val & AC_SUPPCM_BITS_20) |
| 2113 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2116 | else if (streams == AC_SUPFMT_FLOAT32) { |
| 2117 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
| 2119 | bps = 32; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2120 | } else if (streams == AC_SUPFMT_AC3) { |
| 2121 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | /* temporary hack: we have still no proper support |
| 2123 | * for the direct AC3 stream... |
| 2124 | */ |
| 2125 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 2126 | bps = 8; |
| 2127 | } |
| 2128 | if (formatsp) |
| 2129 | *formatsp = formats; |
| 2130 | if (bpsp) |
| 2131 | *bpsp = bps; |
| 2132 | } |
| 2133 | |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | /** |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2138 | * snd_hda_is_supported_format - check whether the given node supports |
| 2139 | * the format val |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | * |
| 2141 | * Returns 1 if supported, 0 if not. |
| 2142 | */ |
| 2143 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 2144 | unsigned int format) |
| 2145 | { |
| 2146 | int i; |
| 2147 | unsigned int val = 0, rate, stream; |
| 2148 | |
| 2149 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2150 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 2152 | if (val == -1) |
| 2153 | return 0; |
| 2154 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2155 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 2157 | if (val == -1) |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2162 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf31 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 2163 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | if (val & (1 << i)) |
| 2165 | break; |
| 2166 | return 0; |
| 2167 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 2168 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | return 0; |
| 2170 | |
| 2171 | stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 2172 | if (stream == -1) |
| 2173 | return 0; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2174 | if (!stream && nid != codec->afg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2176 | if (!stream || stream == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | return 0; |
| 2178 | |
| 2179 | if (stream & AC_SUPFMT_PCM) { |
| 2180 | switch (format & 0xf0) { |
| 2181 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2182 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | return 0; |
| 2184 | break; |
| 2185 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2186 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | return 0; |
| 2188 | break; |
| 2189 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2190 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | return 0; |
| 2192 | break; |
| 2193 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2194 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | return 0; |
| 2196 | break; |
| 2197 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2198 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | return 0; |
| 2200 | break; |
| 2201 | default: |
| 2202 | return 0; |
| 2203 | } |
| 2204 | } else { |
| 2205 | /* FIXME: check for float32 and AC3? */ |
| 2206 | } |
| 2207 | |
| 2208 | return 1; |
| 2209 | } |
| 2210 | |
| 2211 | /* |
| 2212 | * PCM stuff |
| 2213 | */ |
| 2214 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 2215 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2216 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | { |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 2222 | struct hda_codec *codec, |
| 2223 | unsigned int stream_tag, |
| 2224 | unsigned int format, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2225 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | { |
| 2227 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 2228 | return 0; |
| 2229 | } |
| 2230 | |
| 2231 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 2232 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2233 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2235 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | return 0; |
| 2237 | } |
| 2238 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2239 | static int __devinit set_pcm_default_values(struct hda_codec *codec, |
| 2240 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2242 | /* query support PCM information from the given NID */ |
| 2243 | if (info->nid && (!info->rates || !info->formats)) { |
| 2244 | snd_hda_query_supported_pcm(codec, info->nid, |
| 2245 | info->rates ? NULL : &info->rates, |
| 2246 | info->formats ? NULL : &info->formats, |
| 2247 | info->maxbps ? NULL : &info->maxbps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | } |
| 2249 | if (info->ops.open == NULL) |
| 2250 | info->ops.open = hda_pcm_default_open_close; |
| 2251 | if (info->ops.close == NULL) |
| 2252 | info->ops.close = hda_pcm_default_open_close; |
| 2253 | if (info->ops.prepare == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 2254 | if (snd_BUG_ON(!info->nid)) |
| 2255 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | info->ops.prepare = hda_pcm_default_prepare; |
| 2257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | if (info->ops.cleanup == NULL) { |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 2259 | if (snd_BUG_ON(!info->nid)) |
| 2260 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 2262 | } |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | /** |
| 2267 | * snd_hda_build_pcms - build PCM information |
| 2268 | * @bus: the BUS |
| 2269 | * |
| 2270 | * Create PCM information for each codec included in the bus. |
| 2271 | * |
| 2272 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 2273 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 2274 | * to create its PCM instances. |
| 2275 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 2276 | * callback. |
| 2277 | * |
| 2278 | * At least, substreams, channels_min and channels_max must be filled for |
| 2279 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 2280 | * When rates and/or formats are zero, the supported values are queried |
| 2281 | * from the given nid. The nid is used also by the default ops.prepare |
| 2282 | * and ops.cleanup callbacks. |
| 2283 | * |
| 2284 | * The driver needs to call ops.open in its open callback. Similarly, |
| 2285 | * ops.close is supposed to be called in the close callback. |
| 2286 | * ops.prepare should be called in the prepare or hw_params callback |
| 2287 | * with the proper parameters for set up. |
| 2288 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 2289 | * |
| 2290 | * This function returns 0 if successfull, or a negative error code. |
| 2291 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 2292 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2294 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2296 | list_for_each_entry(codec, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | unsigned int pcm, s; |
| 2298 | int err; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2299 | if (!codec->patch_ops.build_pcms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | continue; |
| 2301 | err = codec->patch_ops.build_pcms(codec); |
| 2302 | if (err < 0) |
| 2303 | return err; |
| 2304 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 2305 | for (s = 0; s < 2; s++) { |
| 2306 | struct hda_pcm_stream *info; |
| 2307 | info = &codec->pcm_info[pcm].stream[s]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2308 | if (!info->substreams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | continue; |
| 2310 | err = set_pcm_default_values(codec, info); |
| 2311 | if (err < 0) |
| 2312 | return err; |
| 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | /** |
| 2320 | * snd_hda_check_board_config - compare the current codec with the config table |
| 2321 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2322 | * @num_configs: number of config enums |
| 2323 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | * @tbl: configuration table, terminated by null entries |
| 2325 | * |
| 2326 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 2327 | * given configuration table. If a matching entry is found, returns its |
| 2328 | * config value (supposed to be 0 or positive). |
| 2329 | * |
| 2330 | * If no entries are matching, the function returns a negative value. |
| 2331 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2332 | int snd_hda_check_board_config(struct hda_codec *codec, |
| 2333 | int num_configs, const char **models, |
| 2334 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2336 | if (codec->bus->modelname && models) { |
| 2337 | int i; |
| 2338 | for (i = 0; i < num_configs; i++) { |
| 2339 | if (models[i] && |
| 2340 | !strcmp(codec->bus->modelname, models[i])) { |
| 2341 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 2342 | "selected\n", models[i]); |
| 2343 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | } |
| 2347 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2348 | if (!codec->bus->pci || !tbl) |
| 2349 | return -1; |
| 2350 | |
| 2351 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 2352 | if (!tbl) |
| 2353 | return -1; |
| 2354 | if (tbl->value >= 0 && tbl->value < num_configs) { |
Takashi Iwai | 62cf872 | 2008-05-20 12:15:15 +0200 | [diff] [blame] | 2355 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2356 | char tmp[10]; |
| 2357 | const char *model = NULL; |
| 2358 | if (models) |
| 2359 | model = models[tbl->value]; |
| 2360 | if (!model) { |
| 2361 | sprintf(tmp, "#%d", tbl->value); |
| 2362 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 2364 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 2365 | "for config %x:%x (%s)\n", |
| 2366 | model, tbl->subvendor, tbl->subdevice, |
| 2367 | (tbl->name ? tbl->name : "Unknown device")); |
| 2368 | #endif |
| 2369 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | } |
| 2371 | return -1; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * snd_hda_add_new_ctls - create controls from the array |
| 2376 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2377 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | * |
| 2379 | * This helper function creates and add new controls in the given array. |
| 2380 | * The array must be terminated with an empty entry as terminator. |
| 2381 | * |
| 2382 | * Returns 0 if successful, or a negative error code. |
| 2383 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2384 | int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | { |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2386 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
| 2388 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2389 | struct snd_kcontrol *kctl; |
| 2390 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2391 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2392 | return -ENOMEM; |
| 2393 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2394 | if (err < 0) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2395 | if (!codec->addr) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2396 | return err; |
| 2397 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2398 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2399 | return -ENOMEM; |
| 2400 | kctl->id.device = codec->addr; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2401 | err = snd_ctl_add(codec->bus->card, kctl); |
| 2402 | if (err < 0) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2403 | return err; |
| 2404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | } |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2409 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 2410 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 2411 | unsigned int power_state); |
| 2412 | |
| 2413 | static void hda_power_work(struct work_struct *work) |
| 2414 | { |
| 2415 | struct hda_codec *codec = |
| 2416 | container_of(work, struct hda_codec, power_work.work); |
| 2417 | |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 2418 | if (!codec->power_on || codec->power_count) { |
| 2419 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2420 | return; |
Maxim Levitsky | 2e49246 | 2007-09-03 15:26:57 +0200 | [diff] [blame] | 2421 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2422 | |
| 2423 | hda_call_codec_suspend(codec); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2424 | if (codec->bus->ops.pm_notify) |
| 2425 | codec->bus->ops.pm_notify(codec); |
| 2426 | } |
| 2427 | |
| 2428 | static void hda_keep_power_on(struct hda_codec *codec) |
| 2429 | { |
| 2430 | codec->power_count++; |
| 2431 | codec->power_on = 1; |
| 2432 | } |
| 2433 | |
| 2434 | void snd_hda_power_up(struct hda_codec *codec) |
| 2435 | { |
| 2436 | codec->power_count++; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2437 | if (codec->power_on || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2438 | return; |
| 2439 | |
| 2440 | codec->power_on = 1; |
| 2441 | if (codec->bus->ops.pm_notify) |
| 2442 | codec->bus->ops.pm_notify(codec); |
| 2443 | hda_call_codec_resume(codec); |
| 2444 | cancel_delayed_work(&codec->power_work); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2445 | codec->power_transition = 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | void snd_hda_power_down(struct hda_codec *codec) |
| 2449 | { |
| 2450 | --codec->power_count; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2451 | if (!codec->power_on || codec->power_count || codec->power_transition) |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2452 | return; |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2453 | if (power_save) { |
| 2454 | codec->power_transition = 1; /* avoid reentrance */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2455 | schedule_delayed_work(&codec->power_work, |
| 2456 | msecs_to_jiffies(power_save * 1000)); |
Takashi Iwai | a221e28 | 2007-08-16 16:35:33 +0200 | [diff] [blame] | 2457 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | int snd_hda_check_amp_list_power(struct hda_codec *codec, |
| 2461 | struct hda_loopback_check *check, |
| 2462 | hda_nid_t nid) |
| 2463 | { |
| 2464 | struct hda_amp_list *p; |
| 2465 | int ch, v; |
| 2466 | |
| 2467 | if (!check->amplist) |
| 2468 | return 0; |
| 2469 | for (p = check->amplist; p->nid; p++) { |
| 2470 | if (p->nid == nid) |
| 2471 | break; |
| 2472 | } |
| 2473 | if (!p->nid) |
| 2474 | return 0; /* nothing changed */ |
| 2475 | |
| 2476 | for (p = check->amplist; p->nid; p++) { |
| 2477 | for (ch = 0; ch < 2; ch++) { |
| 2478 | v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, |
| 2479 | p->idx); |
| 2480 | if (!(v & HDA_AMP_MUTE) && v > 0) { |
| 2481 | if (!check->power_on) { |
| 2482 | check->power_on = 1; |
| 2483 | snd_hda_power_up(codec); |
| 2484 | } |
| 2485 | return 1; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | if (check->power_on) { |
| 2490 | check->power_on = 0; |
| 2491 | snd_hda_power_down(codec); |
| 2492 | } |
| 2493 | return 0; |
| 2494 | } |
| 2495 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2497 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2498 | * Channel mode helper |
| 2499 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2500 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 2501 | struct snd_ctl_elem_info *uinfo, |
| 2502 | const struct hda_channel_mode *chmode, |
| 2503 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2504 | { |
| 2505 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2506 | uinfo->count = 1; |
| 2507 | uinfo->value.enumerated.items = num_chmodes; |
| 2508 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 2509 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 2510 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 2511 | chmode[uinfo->value.enumerated.item].channels); |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2515 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 2516 | struct snd_ctl_elem_value *ucontrol, |
| 2517 | const struct hda_channel_mode *chmode, |
| 2518 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2519 | int max_channels) |
| 2520 | { |
| 2521 | int i; |
| 2522 | |
| 2523 | for (i = 0; i < num_chmodes; i++) { |
| 2524 | if (max_channels == chmode[i].channels) { |
| 2525 | ucontrol->value.enumerated.item[0] = i; |
| 2526 | break; |
| 2527 | } |
| 2528 | } |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2532 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 2533 | struct snd_ctl_elem_value *ucontrol, |
| 2534 | const struct hda_channel_mode *chmode, |
| 2535 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2536 | int *max_channelsp) |
| 2537 | { |
| 2538 | unsigned int mode; |
| 2539 | |
| 2540 | mode = ucontrol->value.enumerated.item[0]; |
Takashi Iwai | 68ea7b2 | 2007-11-15 15:54:38 +0100 | [diff] [blame] | 2541 | if (mode >= num_chmodes) |
| 2542 | return -EINVAL; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2543 | if (*max_channelsp == chmode[mode].channels) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2544 | return 0; |
| 2545 | /* change the current channel setting */ |
| 2546 | *max_channelsp = chmode[mode].channels; |
| 2547 | if (chmode[mode].sequence) |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2548 | snd_hda_sequence_write_cache(codec, chmode[mode].sequence); |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 2549 | return 1; |
| 2550 | } |
| 2551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | /* |
| 2553 | * input MUX helper |
| 2554 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2555 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 2556 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | { |
| 2558 | unsigned int index; |
| 2559 | |
| 2560 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 2561 | uinfo->count = 1; |
| 2562 | uinfo->value.enumerated.items = imux->num_items; |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 2563 | if (!imux->num_items) |
| 2564 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | index = uinfo->value.enumerated.item; |
| 2566 | if (index >= imux->num_items) |
| 2567 | index = imux->num_items - 1; |
| 2568 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2572 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 2573 | const struct hda_input_mux *imux, |
| 2574 | struct snd_ctl_elem_value *ucontrol, |
| 2575 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | unsigned int *cur_val) |
| 2577 | { |
| 2578 | unsigned int idx; |
| 2579 | |
Takashi Iwai | 5513b0c | 2007-10-09 11:58:41 +0200 | [diff] [blame] | 2580 | if (!imux->num_items) |
| 2581 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | idx = ucontrol->value.enumerated.item[0]; |
| 2583 | if (idx >= imux->num_items) |
| 2584 | idx = imux->num_items - 1; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2585 | if (*cur_val == idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | return 0; |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 2587 | snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 2588 | imux->items[idx].index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | *cur_val = idx; |
| 2590 | return 1; |
| 2591 | } |
| 2592 | |
| 2593 | |
| 2594 | /* |
| 2595 | * Multi-channel / digital-out PCM helper functions |
| 2596 | */ |
| 2597 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2598 | /* setup SPDIF output stream */ |
| 2599 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 2600 | unsigned int stream_tag, unsigned int format) |
| 2601 | { |
| 2602 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2603 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
| 2604 | set_dig_out_convert(codec, nid, |
| 2605 | codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff, |
| 2606 | -1); |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2607 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2608 | if (codec->slave_dig_outs) { |
| 2609 | hda_nid_t *d; |
| 2610 | for (d = codec->slave_dig_outs; *d; d++) |
| 2611 | snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, |
| 2612 | format); |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 2613 | } |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2614 | /* turn on again (if needed) */ |
| 2615 | if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) |
| 2616 | set_dig_out_convert(codec, nid, |
| 2617 | codec->spdif_ctls & 0xff, -1); |
| 2618 | } |
Matthew Ranostay | de51ca1 | 2008-09-07 14:31:40 -0400 | [diff] [blame] | 2619 | |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2620 | static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) |
| 2621 | { |
| 2622 | snd_hda_codec_cleanup_stream(codec, nid); |
| 2623 | if (codec->slave_dig_outs) { |
| 2624 | hda_nid_t *d; |
| 2625 | for (d = codec->slave_dig_outs; *d; d++) |
| 2626 | snd_hda_codec_cleanup_stream(codec, *d); |
| 2627 | } |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2628 | } |
| 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | /* |
| 2631 | * open the digital out in the exclusive mode |
| 2632 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2633 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 2634 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2636 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 2637 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 2638 | /* already opened as analog dup; reset it once */ |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2639 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2641 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | return 0; |
| 2643 | } |
| 2644 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2645 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 2646 | struct hda_multi_out *mout, |
| 2647 | unsigned int stream_tag, |
| 2648 | unsigned int format, |
| 2649 | struct snd_pcm_substream *substream) |
| 2650 | { |
| 2651 | mutex_lock(&codec->spdif_mutex); |
| 2652 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 2653 | mutex_unlock(&codec->spdif_mutex); |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | /* |
| 2658 | * release the digital out |
| 2659 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2660 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 2661 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2663 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2665 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | return 0; |
| 2667 | } |
| 2668 | |
| 2669 | /* |
| 2670 | * set up more restrictions for analog out |
| 2671 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2672 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 2673 | struct hda_multi_out *mout, |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2674 | struct snd_pcm_substream *substream, |
| 2675 | struct hda_pcm_stream *hinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | { |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2677 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2678 | runtime->hw.channels_max = mout->max_channels; |
| 2679 | if (mout->dig_out_nid) { |
| 2680 | if (!mout->analog_rates) { |
| 2681 | mout->analog_rates = hinfo->rates; |
| 2682 | mout->analog_formats = hinfo->formats; |
| 2683 | mout->analog_maxbps = hinfo->maxbps; |
| 2684 | } else { |
| 2685 | runtime->hw.rates = mout->analog_rates; |
| 2686 | runtime->hw.formats = mout->analog_formats; |
| 2687 | hinfo->maxbps = mout->analog_maxbps; |
| 2688 | } |
| 2689 | if (!mout->spdif_rates) { |
| 2690 | snd_hda_query_supported_pcm(codec, mout->dig_out_nid, |
| 2691 | &mout->spdif_rates, |
| 2692 | &mout->spdif_formats, |
| 2693 | &mout->spdif_maxbps); |
| 2694 | } |
| 2695 | mutex_lock(&codec->spdif_mutex); |
| 2696 | if (mout->share_spdif) { |
| 2697 | runtime->hw.rates &= mout->spdif_rates; |
| 2698 | runtime->hw.formats &= mout->spdif_formats; |
| 2699 | if (mout->spdif_maxbps < hinfo->maxbps) |
| 2700 | hinfo->maxbps = mout->spdif_maxbps; |
| 2701 | } |
Frederik Deweerdt | eaa9985 | 2008-04-14 13:11:44 +0200 | [diff] [blame] | 2702 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 2705 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * set up the i/o for analog out |
| 2710 | * when the digital out is available, copy the front out to digital out, too. |
| 2711 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2712 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 2713 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | unsigned int stream_tag, |
| 2715 | unsigned int format, |
Takashi Iwai | c8b6bf9b | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2716 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | { |
| 2718 | hda_nid_t *nids = mout->dac_nids; |
| 2719 | int chs = substream->runtime->channels; |
| 2720 | int i; |
| 2721 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2722 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 9a08160 | 2008-02-12 18:37:26 +0100 | [diff] [blame] | 2723 | if (mout->dig_out_nid && mout->share_spdif && |
| 2724 | mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2726 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 2727 | format) && |
| 2728 | !(codec->spdif_status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2730 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 2731 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | } else { |
| 2733 | mout->dig_out_used = 0; |
Takashi Iwai | 2f72853 | 2008-09-25 16:32:41 +0200 | [diff] [blame] | 2734 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2737 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | |
| 2739 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2740 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 2741 | 0, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2742 | if (!mout->no_share_stream && |
| 2743 | mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2745 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 2746 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2747 | /* extra outputs copied from front */ |
| 2748 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2749 | if (!mout->no_share_stream && mout->extra_out_nid[i]) |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2750 | snd_hda_codec_setup_stream(codec, |
| 2751 | mout->extra_out_nid[i], |
| 2752 | stream_tag, 0, format); |
| 2753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | /* surrounds */ |
| 2755 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 2756 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2757 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2758 | i * 2, format); |
Takashi Iwai | d29240c | 2007-10-26 12:35:56 +0200 | [diff] [blame] | 2759 | else if (!mout->no_share_stream) /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2760 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2761 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
| 2763 | return 0; |
| 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * clean up the setting for analog out |
| 2768 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2769 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 2770 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | { |
| 2772 | hda_nid_t *nids = mout->dac_nids; |
| 2773 | int i; |
| 2774 | |
| 2775 | for (i = 0; i < mout->num_dacs; i++) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2776 | snd_hda_codec_cleanup_stream(codec, nids[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | if (mout->hp_nid) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2778 | snd_hda_codec_cleanup_stream(codec, mout->hp_nid); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2779 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 2780 | if (mout->extra_out_nid[i]) |
Takashi Iwai | 888afa1 | 2008-03-18 09:57:50 +0100 | [diff] [blame] | 2781 | snd_hda_codec_cleanup_stream(codec, |
| 2782 | mout->extra_out_nid[i]); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2783 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | 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] | 2785 | cleanup_dig_out_stream(codec, mout->dig_out_nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | mout->dig_out_used = 0; |
| 2787 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2788 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | return 0; |
| 2790 | } |
| 2791 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2792 | /* |
Wu Fengguang | 6b34500 | 2008-10-07 14:21:41 +0800 | [diff] [blame] | 2793 | * Helper for automatic pin configuration |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2794 | */ |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2795 | |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2796 | static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list) |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2797 | { |
| 2798 | for (; *list; list++) |
| 2799 | if (*list == nid) |
| 2800 | return 1; |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2804 | |
| 2805 | /* |
| 2806 | * Sort an associated group of pins according to their sequence numbers. |
| 2807 | */ |
| 2808 | static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences, |
| 2809 | int num_pins) |
| 2810 | { |
| 2811 | int i, j; |
| 2812 | short seq; |
| 2813 | hda_nid_t nid; |
| 2814 | |
| 2815 | for (i = 0; i < num_pins; i++) { |
| 2816 | for (j = i + 1; j < num_pins; j++) { |
| 2817 | if (sequences[i] > sequences[j]) { |
| 2818 | seq = sequences[i]; |
| 2819 | sequences[i] = sequences[j]; |
| 2820 | sequences[j] = seq; |
| 2821 | nid = pins[i]; |
| 2822 | pins[i] = pins[j]; |
| 2823 | pins[j] = nid; |
| 2824 | } |
| 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2830 | /* |
| 2831 | * Parse all pin widgets and store the useful pin nids to cfg |
| 2832 | * |
| 2833 | * The number of line-outs or any primary output is stored in line_outs, |
| 2834 | * and the corresponding output pins are assigned to line_out_pins[], |
| 2835 | * in the order of front, rear, CLFE, side, ... |
| 2836 | * |
| 2837 | * If more extra outputs (speaker and headphone) are found, the pins are |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2838 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2839 | * is detected, one of speaker of HP pins is assigned as the primary |
| 2840 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 2841 | * if any analog output exists. |
| 2842 | * |
| 2843 | * The analog input pins are assigned to input_pins array. |
| 2844 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 2845 | * respectively. |
| 2846 | */ |
Takashi Iwai | 12f288b | 2007-08-02 15:51:59 +0200 | [diff] [blame] | 2847 | int snd_hda_parse_pin_def_config(struct hda_codec *codec, |
| 2848 | struct auto_pin_cfg *cfg, |
| 2849 | hda_nid_t *ignore_nids) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2850 | { |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 2851 | hda_nid_t nid, end_nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2852 | short seq, assoc_line_out, assoc_speaker; |
| 2853 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 2854 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2855 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2856 | |
| 2857 | memset(cfg, 0, sizeof(*cfg)); |
| 2858 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2859 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 2860 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2861 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2862 | assoc_line_out = assoc_speaker = 0; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2863 | |
Takashi Iwai | 0ef6ce7 | 2008-01-22 15:35:37 +0100 | [diff] [blame] | 2864 | end_nid = codec->start_nid + codec->num_nodes; |
| 2865 | for (nid = codec->start_nid; nid < end_nid; nid++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2866 | unsigned int wid_caps = get_wcaps(codec, nid); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2867 | unsigned int wid_type = |
| 2868 | (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2869 | unsigned int def_conf; |
| 2870 | short assoc, loc; |
| 2871 | |
| 2872 | /* read all default configuration for pin complex */ |
| 2873 | if (wid_type != AC_WID_PIN) |
| 2874 | continue; |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2875 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 2876 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 2877 | continue; |
| 2878 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2879 | def_conf = snd_hda_codec_read(codec, nid, 0, |
| 2880 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2881 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
| 2882 | continue; |
| 2883 | loc = get_defcfg_location(def_conf); |
| 2884 | switch (get_defcfg_device(def_conf)) { |
| 2885 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2886 | seq = get_defcfg_sequence(def_conf); |
| 2887 | assoc = get_defcfg_association(def_conf); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 2888 | |
| 2889 | if (!(wid_caps & AC_WCAP_STEREO)) |
| 2890 | if (!cfg->mono_out_pin) |
| 2891 | cfg->mono_out_pin = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2892 | if (!assoc) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2893 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2894 | if (!assoc_line_out) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2895 | assoc_line_out = assoc; |
| 2896 | else if (assoc_line_out != assoc) |
| 2897 | continue; |
| 2898 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 2899 | continue; |
| 2900 | cfg->line_out_pins[cfg->line_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2901 | sequences_line_out[cfg->line_outs] = seq; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2902 | cfg->line_outs++; |
| 2903 | break; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 2904 | case AC_JACK_SPEAKER: |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2905 | seq = get_defcfg_sequence(def_conf); |
| 2906 | assoc = get_defcfg_association(def_conf); |
| 2907 | if (! assoc) |
| 2908 | continue; |
| 2909 | if (! assoc_speaker) |
| 2910 | assoc_speaker = assoc; |
| 2911 | else if (assoc_speaker != assoc) |
| 2912 | continue; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2913 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 2914 | continue; |
| 2915 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2916 | sequences_speaker[cfg->speaker_outs] = seq; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2917 | cfg->speaker_outs++; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 2918 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2919 | case AC_JACK_HP_OUT: |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2920 | seq = get_defcfg_sequence(def_conf); |
| 2921 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2922 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 2923 | continue; |
| 2924 | cfg->hp_pins[cfg->hp_outs] = nid; |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2925 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2926 | cfg->hp_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2927 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 2928 | case AC_JACK_MIC_IN: { |
| 2929 | int preferred, alt; |
| 2930 | if (loc == AC_JACK_LOC_FRONT) { |
| 2931 | preferred = AUTO_PIN_FRONT_MIC; |
| 2932 | alt = AUTO_PIN_MIC; |
| 2933 | } else { |
| 2934 | preferred = AUTO_PIN_MIC; |
| 2935 | alt = AUTO_PIN_FRONT_MIC; |
| 2936 | } |
| 2937 | if (!cfg->input_pins[preferred]) |
| 2938 | cfg->input_pins[preferred] = nid; |
| 2939 | else if (!cfg->input_pins[alt]) |
| 2940 | cfg->input_pins[alt] = nid; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2941 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 2942 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2943 | case AC_JACK_LINE_IN: |
| 2944 | if (loc == AC_JACK_LOC_FRONT) |
| 2945 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid; |
| 2946 | else |
| 2947 | cfg->input_pins[AUTO_PIN_LINE] = nid; |
| 2948 | break; |
| 2949 | case AC_JACK_CD: |
| 2950 | cfg->input_pins[AUTO_PIN_CD] = nid; |
| 2951 | break; |
| 2952 | case AC_JACK_AUX: |
| 2953 | cfg->input_pins[AUTO_PIN_AUX] = nid; |
| 2954 | break; |
| 2955 | case AC_JACK_SPDIF_OUT: |
| 2956 | cfg->dig_out_pin = nid; |
| 2957 | break; |
| 2958 | case AC_JACK_SPDIF_IN: |
| 2959 | cfg->dig_in_pin = nid; |
| 2960 | break; |
| 2961 | } |
| 2962 | } |
| 2963 | |
Takashi Iwai | 5832fcf | 2008-02-12 18:30:12 +0100 | [diff] [blame] | 2964 | /* FIX-UP: |
| 2965 | * If no line-out is defined but multiple HPs are found, |
| 2966 | * some of them might be the real line-outs. |
| 2967 | */ |
| 2968 | if (!cfg->line_outs && cfg->hp_outs > 1) { |
| 2969 | int i = 0; |
| 2970 | while (i < cfg->hp_outs) { |
| 2971 | /* The real HPs should have the sequence 0x0f */ |
| 2972 | if ((sequences_hp[i] & 0x0f) == 0x0f) { |
| 2973 | i++; |
| 2974 | continue; |
| 2975 | } |
| 2976 | /* Move it to the line-out table */ |
| 2977 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; |
| 2978 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; |
| 2979 | cfg->line_outs++; |
| 2980 | cfg->hp_outs--; |
| 2981 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, |
| 2982 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); |
| 2983 | memmove(sequences_hp + i - 1, sequences_hp + i, |
| 2984 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); |
| 2985 | } |
| 2986 | } |
| 2987 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2988 | /* sort by sequence */ |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2989 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 2990 | cfg->line_outs); |
| 2991 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 2992 | cfg->speaker_outs); |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2993 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
| 2994 | cfg->hp_outs); |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2995 | |
Takashi Iwai | f889fa9 | 2007-10-31 15:49:32 +0100 | [diff] [blame] | 2996 | /* if we have only one mic, make it AUTO_PIN_MIC */ |
| 2997 | if (!cfg->input_pins[AUTO_PIN_MIC] && |
| 2998 | cfg->input_pins[AUTO_PIN_FRONT_MIC]) { |
| 2999 | cfg->input_pins[AUTO_PIN_MIC] = |
| 3000 | cfg->input_pins[AUTO_PIN_FRONT_MIC]; |
| 3001 | cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0; |
| 3002 | } |
| 3003 | /* ditto for line-in */ |
| 3004 | if (!cfg->input_pins[AUTO_PIN_LINE] && |
| 3005 | cfg->input_pins[AUTO_PIN_FRONT_LINE]) { |
| 3006 | cfg->input_pins[AUTO_PIN_LINE] = |
| 3007 | cfg->input_pins[AUTO_PIN_FRONT_LINE]; |
| 3008 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0; |
| 3009 | } |
| 3010 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 3011 | /* |
| 3012 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 3013 | * as a primary output |
| 3014 | */ |
| 3015 | if (!cfg->line_outs) { |
| 3016 | if (cfg->speaker_outs) { |
| 3017 | cfg->line_outs = cfg->speaker_outs; |
| 3018 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 3019 | sizeof(cfg->speaker_pins)); |
| 3020 | cfg->speaker_outs = 0; |
| 3021 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 3022 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 3023 | } else if (cfg->hp_outs) { |
| 3024 | cfg->line_outs = cfg->hp_outs; |
| 3025 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 3026 | sizeof(cfg->hp_pins)); |
| 3027 | cfg->hp_outs = 0; |
| 3028 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 3029 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 3030 | } |
| 3031 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3032 | |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3033 | /* Reorder the surround channels |
| 3034 | * ALSA sequence is front/surr/clfe/side |
| 3035 | * HDA sequence is: |
| 3036 | * 4-ch: front/surr => OK as it is |
| 3037 | * 6-ch: front/clfe/surr |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 3038 | * 8-ch: front/clfe/rear/side|fc |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3039 | */ |
| 3040 | switch (cfg->line_outs) { |
| 3041 | case 3: |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3042 | case 4: |
| 3043 | nid = cfg->line_out_pins[1]; |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 3044 | cfg->line_out_pins[1] = cfg->line_out_pins[2]; |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 3045 | cfg->line_out_pins[2] = nid; |
| 3046 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3047 | } |
| 3048 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3049 | /* |
| 3050 | * debug prints of the parsed results |
| 3051 | */ |
| 3052 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3053 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 3054 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
| 3055 | cfg->line_out_pins[4]); |
| 3056 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3057 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 3058 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 3059 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 3060 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 3061 | cfg->hp_outs, cfg->hp_pins[0], |
| 3062 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 3063 | cfg->hp_pins[3], cfg->hp_pins[4]); |
Matthew Ranostay | 90da78b | 2008-01-24 11:48:01 +0100 | [diff] [blame] | 3064 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 3065 | snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x," |
| 3066 | " cd=0x%x, aux=0x%x\n", |
| 3067 | cfg->input_pins[AUTO_PIN_MIC], |
| 3068 | cfg->input_pins[AUTO_PIN_FRONT_MIC], |
| 3069 | cfg->input_pins[AUTO_PIN_LINE], |
| 3070 | cfg->input_pins[AUTO_PIN_FRONT_LINE], |
| 3071 | cfg->input_pins[AUTO_PIN_CD], |
| 3072 | cfg->input_pins[AUTO_PIN_AUX]); |
| 3073 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 3074 | return 0; |
| 3075 | } |
| 3076 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 3077 | /* labels for input pins */ |
| 3078 | const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = { |
| 3079 | "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux" |
| 3080 | }; |
| 3081 | |
| 3082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | #ifdef CONFIG_PM |
| 3084 | /* |
| 3085 | * power management |
| 3086 | */ |
| 3087 | |
| 3088 | /** |
| 3089 | * snd_hda_suspend - suspend the codecs |
| 3090 | * @bus: the HDA bus |
| 3091 | * @state: suspsend state |
| 3092 | * |
| 3093 | * Returns 0 if successful. |
| 3094 | */ |
| 3095 | int snd_hda_suspend(struct hda_bus *bus, pm_message_t state) |
| 3096 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3097 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3099 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 0b7a2e9 | 2007-08-14 15:18:26 +0200 | [diff] [blame] | 3100 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 3101 | if (!codec->power_on) |
| 3102 | continue; |
| 3103 | #endif |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3104 | hda_call_codec_suspend(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | } |
| 3106 | return 0; |
| 3107 | } |
| 3108 | |
| 3109 | /** |
| 3110 | * snd_hda_resume - resume the codecs |
| 3111 | * @bus: the HDA bus |
| 3112 | * @state: resume state |
| 3113 | * |
| 3114 | * Returns 0 if successful. |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3115 | * |
| 3116 | * This fucntion is defined only when POWER_SAVE isn't set. |
| 3117 | * In the power-save mode, the codec is resumed dynamically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | */ |
| 3119 | int snd_hda_resume(struct hda_bus *bus) |
| 3120 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3121 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 3123 | list_for_each_entry(codec, &bus->codec_list, list) { |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3124 | if (snd_hda_codec_needs_resume(codec)) |
| 3125 | hda_call_codec_resume(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | return 0; |
| 3128 | } |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3129 | #ifdef CONFIG_SND_HDA_POWER_SAVE |
| 3130 | int snd_hda_codecs_inuse(struct hda_bus *bus) |
| 3131 | { |
| 3132 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | |
Maxim Levitsky | d804ad9 | 2007-09-03 15:28:04 +0200 | [diff] [blame] | 3134 | list_for_each_entry(codec, &bus->codec_list, list) { |
| 3135 | if (snd_hda_codec_needs_resume(codec)) |
| 3136 | return 1; |
| 3137 | } |
| 3138 | return 0; |
| 3139 | } |
| 3140 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | #endif |