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