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