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