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 | |
| 22 | #include <sound/driver.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/pci.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include "hda_codec.h" |
| 30 | #include <sound/asoundef.h> |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 31 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <sound/initval.h> |
| 33 | #include "hda_local.h" |
| 34 | |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * vendor / preset table |
| 38 | */ |
| 39 | |
| 40 | struct hda_vendor_id { |
| 41 | unsigned int id; |
| 42 | const char *name; |
| 43 | }; |
| 44 | |
| 45 | /* codec vendor labels */ |
| 46 | static struct hda_vendor_id hda_vendor_ids[] = { |
| 47 | { 0x10ec, "Realtek" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 48 | { 0x1057, "Motorola" }, |
Joseph Chan | c577b8a | 2006-11-29 15:29:40 +0100 | [diff] [blame] | 49 | { 0x1106, "VIA" }, |
Takashi Iwai | 54b903e | 2005-05-15 14:30:10 +0200 | [diff] [blame] | 50 | { 0x11d4, "Analog Devices" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { 0x13f6, "C-Media" }, |
Takashi Iwai | a922625 | 2006-09-17 22:05:54 +0200 | [diff] [blame] | 52 | { 0x14f1, "Conexant" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { 0x434d, "C-Media" }, |
Matt | 2f2f425 | 2005-04-13 14:45:30 +0200 | [diff] [blame] | 54 | { 0x8384, "SigmaTel" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | {} /* terminator */ |
| 56 | }; |
| 57 | |
| 58 | /* codec presets */ |
| 59 | #include "hda_patch.h" |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * snd_hda_codec_read - send a command and get the response |
| 64 | * @codec: the HDA codec |
| 65 | * @nid: NID to send the command |
| 66 | * @direct: direct flag |
| 67 | * @verb: the verb to send |
| 68 | * @parm: the parameter for the verb |
| 69 | * |
| 70 | * Send a single command and read the corresponding response. |
| 71 | * |
| 72 | * Returns the obtained response value, or -1 for an error. |
| 73 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 74 | unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, |
| 75 | int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | unsigned int verb, unsigned int parm) |
| 77 | { |
| 78 | unsigned int res; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 79 | mutex_lock(&codec->bus->cmd_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 80 | if (!codec->bus->ops.command(codec, nid, direct, verb, parm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | res = codec->bus->ops.get_response(codec); |
| 82 | else |
| 83 | res = (unsigned int)-1; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 84 | mutex_unlock(&codec->bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return res; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * snd_hda_codec_write - send a single command without waiting for response |
| 90 | * @codec: the HDA codec |
| 91 | * @nid: NID to send the command |
| 92 | * @direct: direct flag |
| 93 | * @verb: the verb to send |
| 94 | * @parm: the parameter for the verb |
| 95 | * |
| 96 | * Send a single command without waiting for response. |
| 97 | * |
| 98 | * Returns 0 if successful, or a negative error code. |
| 99 | */ |
| 100 | int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct, |
| 101 | unsigned int verb, unsigned int parm) |
| 102 | { |
| 103 | int err; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 104 | mutex_lock(&codec->bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | err = codec->bus->ops.command(codec, nid, direct, verb, parm); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 106 | mutex_unlock(&codec->bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return err; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * snd_hda_sequence_write - sequence writes |
| 112 | * @codec: the HDA codec |
| 113 | * @seq: VERB array to send |
| 114 | * |
| 115 | * Send the commands sequentially from the given array. |
| 116 | * The array must be terminated with NID=0. |
| 117 | */ |
| 118 | void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) |
| 119 | { |
| 120 | for (; seq->nid; seq++) |
| 121 | snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * snd_hda_get_sub_nodes - get the range of sub nodes |
| 126 | * @codec: the HDA codec |
| 127 | * @nid: NID to parse |
| 128 | * @start_id: the pointer to store the start NID |
| 129 | * |
| 130 | * Parse the NID and store the start NID of its sub-nodes. |
| 131 | * Returns the number of sub-nodes. |
| 132 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 133 | int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, |
| 134 | hda_nid_t *start_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | unsigned int parm; |
| 137 | |
| 138 | parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); |
| 139 | *start_id = (parm >> 16) & 0x7fff; |
| 140 | return (int)(parm & 0x7fff); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * snd_hda_get_connections - get connection list |
| 145 | * @codec: the HDA codec |
| 146 | * @nid: NID to parse |
| 147 | * @conn_list: connection list array |
| 148 | * @max_conns: max. number of connections to store |
| 149 | * |
| 150 | * Parses the connection list of the given widget and stores the list |
| 151 | * of NIDs. |
| 152 | * |
| 153 | * Returns the number of connections, or a negative error code. |
| 154 | */ |
| 155 | int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, |
| 156 | hda_nid_t *conn_list, int max_conns) |
| 157 | { |
| 158 | unsigned int parm; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 159 | int i, conn_len, conns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | unsigned int shift, num_elems, mask; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 161 | hda_nid_t prev_nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | snd_assert(conn_list && max_conns > 0, return -EINVAL); |
| 164 | |
| 165 | parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); |
| 166 | if (parm & AC_CLIST_LONG) { |
| 167 | /* long form */ |
| 168 | shift = 16; |
| 169 | num_elems = 2; |
| 170 | } else { |
| 171 | /* short form */ |
| 172 | shift = 8; |
| 173 | num_elems = 4; |
| 174 | } |
| 175 | conn_len = parm & AC_CLIST_LENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | mask = (1 << (shift-1)) - 1; |
| 177 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 178 | if (!conn_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 0; /* no connection */ |
| 180 | |
| 181 | if (conn_len == 1) { |
| 182 | /* single connection */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 183 | parm = snd_hda_codec_read(codec, nid, 0, |
| 184 | AC_VERB_GET_CONNECT_LIST, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | conn_list[0] = parm & mask; |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* multi connection */ |
| 190 | conns = 0; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 191 | prev_nid = 0; |
| 192 | for (i = 0; i < conn_len; i++) { |
| 193 | int range_val; |
| 194 | hda_nid_t val, n; |
| 195 | |
| 196 | if (i % num_elems == 0) |
| 197 | parm = snd_hda_codec_read(codec, nid, 0, |
| 198 | AC_VERB_GET_CONNECT_LIST, i); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 199 | range_val = !!(parm & (1 << (shift-1))); /* ranges */ |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 200 | val = parm & mask; |
| 201 | parm >>= shift; |
| 202 | if (range_val) { |
| 203 | /* ranges between the previous and this one */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 204 | if (!prev_nid || prev_nid >= val) { |
| 205 | snd_printk(KERN_WARNING "hda_codec: " |
| 206 | "invalid dep_range_val %x:%x\n", |
| 207 | prev_nid, val); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 208 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 210 | for (n = prev_nid + 1; n <= val; n++) { |
| 211 | if (conns >= max_conns) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 212 | snd_printk(KERN_ERR |
| 213 | "Too many connections\n"); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | conn_list[conns++] = n; |
| 217 | } |
| 218 | } else { |
| 219 | if (conns >= max_conns) { |
| 220 | snd_printk(KERN_ERR "Too many connections\n"); |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | conn_list[conns++] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 225 | prev_nid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | return conns; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * snd_hda_queue_unsol_event - add an unsolicited event to queue |
| 233 | * @bus: the BUS |
| 234 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 235 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 236 | * |
| 237 | * Adds the given event to the queue. The events are processed in |
| 238 | * the workqueue asynchronously. Call this function in the interrupt |
| 239 | * hanlder when RIRB receives an unsolicited event. |
| 240 | * |
| 241 | * Returns 0 if successful, or a negative error code. |
| 242 | */ |
| 243 | int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) |
| 244 | { |
| 245 | struct hda_bus_unsolicited *unsol; |
| 246 | unsigned int wp; |
| 247 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 248 | unsol = bus->unsol; |
| 249 | if (!unsol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | |
| 252 | wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 253 | unsol->wp = wp; |
| 254 | |
| 255 | wp <<= 1; |
| 256 | unsol->queue[wp] = res; |
| 257 | unsol->queue[wp + 1] = res_ex; |
| 258 | |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 259 | schedule_work(&unsol->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * process queueud unsolicited events |
| 266 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 267 | static void process_unsol_events(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 269 | struct hda_bus_unsolicited *unsol = |
| 270 | container_of(work, struct hda_bus_unsolicited, work); |
| 271 | struct hda_bus *bus = unsol->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | struct hda_codec *codec; |
| 273 | unsigned int rp, caddr, res; |
| 274 | |
| 275 | while (unsol->rp != unsol->wp) { |
| 276 | rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 277 | unsol->rp = rp; |
| 278 | rp <<= 1; |
| 279 | res = unsol->queue[rp]; |
| 280 | caddr = unsol->queue[rp + 1]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 281 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | continue; |
| 283 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 284 | if (codec && codec->patch_ops.unsol_event) |
| 285 | codec->patch_ops.unsol_event(codec, res); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * initialize unsolicited queue |
| 291 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 292 | static int __devinit init_unsol_queue(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | struct hda_bus_unsolicited *unsol; |
| 295 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 296 | if (bus->unsol) /* already initialized */ |
| 297 | return 0; |
| 298 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 299 | unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 300 | if (!unsol) { |
| 301 | snd_printk(KERN_ERR "hda_codec: " |
| 302 | "can't allocate unsolicited queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return -ENOMEM; |
| 304 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 305 | INIT_WORK(&unsol->work, process_unsol_events); |
| 306 | unsol->bus = bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | bus->unsol = unsol; |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * destructor |
| 313 | */ |
| 314 | static void snd_hda_codec_free(struct hda_codec *codec); |
| 315 | |
| 316 | static int snd_hda_bus_free(struct hda_bus *bus) |
| 317 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 318 | struct hda_codec *codec, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 320 | if (!bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return 0; |
| 322 | if (bus->unsol) { |
Takashi Iwai | e250af2 | 2006-12-19 17:08:52 +0100 | [diff] [blame] | 323 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | kfree(bus->unsol); |
| 325 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 326 | list_for_each_entry_safe(codec, n, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | snd_hda_codec_free(codec); |
| 328 | } |
| 329 | if (bus->ops.private_free) |
| 330 | bus->ops.private_free(bus); |
| 331 | kfree(bus); |
| 332 | return 0; |
| 333 | } |
| 334 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 335 | static int snd_hda_bus_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
| 337 | struct hda_bus *bus = device->device_data; |
| 338 | return snd_hda_bus_free(bus); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * snd_hda_bus_new - create a HDA bus |
| 343 | * @card: the card entry |
| 344 | * @temp: the template for hda_bus information |
| 345 | * @busp: the pointer to store the created bus instance |
| 346 | * |
| 347 | * Returns 0 if successful, or a negative error code. |
| 348 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 349 | int __devinit snd_hda_bus_new(struct snd_card *card, |
| 350 | const struct hda_bus_template *temp, |
| 351 | struct hda_bus **busp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | struct hda_bus *bus; |
| 354 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 355 | static struct snd_device_ops dev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | .dev_free = snd_hda_bus_dev_free, |
| 357 | }; |
| 358 | |
| 359 | snd_assert(temp, return -EINVAL); |
| 360 | snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL); |
| 361 | |
| 362 | if (busp) |
| 363 | *busp = NULL; |
| 364 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 365 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (bus == NULL) { |
| 367 | snd_printk(KERN_ERR "can't allocate struct hda_bus\n"); |
| 368 | return -ENOMEM; |
| 369 | } |
| 370 | |
| 371 | bus->card = card; |
| 372 | bus->private_data = temp->private_data; |
| 373 | bus->pci = temp->pci; |
| 374 | bus->modelname = temp->modelname; |
| 375 | bus->ops = temp->ops; |
| 376 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 377 | mutex_init(&bus->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | INIT_LIST_HEAD(&bus->codec_list); |
| 379 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 380 | err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); |
| 381 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | snd_hda_bus_free(bus); |
| 383 | return err; |
| 384 | } |
| 385 | if (busp) |
| 386 | *busp = bus; |
| 387 | return 0; |
| 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* |
| 391 | * find a matching codec preset |
| 392 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 393 | static const struct hda_codec_preset __devinit * |
| 394 | find_codec_preset(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | const struct hda_codec_preset **tbl, *preset; |
| 397 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 398 | if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic")) |
| 399 | return NULL; /* use the generic parser */ |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | for (tbl = hda_preset_tables; *tbl; tbl++) { |
| 402 | for (preset = *tbl; preset->id; preset++) { |
| 403 | u32 mask = preset->mask; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 404 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | mask = ~0; |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 406 | if (preset->id == (codec->vendor_id & mask) && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 407 | (!preset->rev || |
Takashi Iwai | 9c7f852 | 2006-06-28 15:08:22 +0200 | [diff] [blame] | 408 | preset->rev == codec->revision_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return preset; |
| 410 | } |
| 411 | } |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * snd_hda_get_codec_name - store the codec name |
| 417 | */ |
| 418 | void snd_hda_get_codec_name(struct hda_codec *codec, |
| 419 | char *name, int namelen) |
| 420 | { |
| 421 | const struct hda_vendor_id *c; |
| 422 | const char *vendor = NULL; |
| 423 | u16 vendor_id = codec->vendor_id >> 16; |
| 424 | char tmp[16]; |
| 425 | |
| 426 | for (c = hda_vendor_ids; c->id; c++) { |
| 427 | if (c->id == vendor_id) { |
| 428 | vendor = c->name; |
| 429 | break; |
| 430 | } |
| 431 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 432 | if (!vendor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | sprintf(tmp, "Generic %04x", vendor_id); |
| 434 | vendor = tmp; |
| 435 | } |
| 436 | if (codec->preset && codec->preset->name) |
| 437 | snprintf(name, namelen, "%s %s", vendor, codec->preset->name); |
| 438 | else |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 439 | snprintf(name, namelen, "%s ID %x", vendor, |
| 440 | codec->vendor_id & 0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 444 | * look for an AFG and MFG nodes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 446 | static void __devinit setup_fg_nodes(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | int i, total_nodes; |
| 449 | hda_nid_t nid; |
| 450 | |
| 451 | total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); |
| 452 | for (i = 0; i < total_nodes; i++, nid++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 453 | unsigned int func; |
| 454 | func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE); |
| 455 | switch (func & 0xff) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 456 | case AC_GRP_AUDIO_FUNCTION: |
| 457 | codec->afg = nid; |
| 458 | break; |
| 459 | case AC_GRP_MODEM_FUNCTION: |
| 460 | codec->mfg = nid; |
| 461 | break; |
| 462 | default: |
| 463 | break; |
| 464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 469 | * read widget caps for each widget and store in cache |
| 470 | */ |
| 471 | static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) |
| 472 | { |
| 473 | int i; |
| 474 | hda_nid_t nid; |
| 475 | |
| 476 | codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, |
| 477 | &codec->start_nid); |
| 478 | codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 479 | if (!codec->wcaps) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 480 | return -ENOMEM; |
| 481 | nid = codec->start_nid; |
| 482 | for (i = 0; i < codec->num_nodes; i++, nid++) |
| 483 | codec->wcaps[i] = snd_hda_param_read(codec, nid, |
| 484 | AC_PAR_AUDIO_WIDGET_CAP); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | * codec destructor |
| 491 | */ |
| 492 | static void snd_hda_codec_free(struct hda_codec *codec) |
| 493 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 494 | if (!codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return; |
| 496 | list_del(&codec->list); |
| 497 | codec->bus->caddr_tbl[codec->addr] = NULL; |
| 498 | if (codec->patch_ops.free) |
| 499 | codec->patch_ops.free(codec); |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 500 | kfree(codec->amp_info); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 501 | kfree(codec->wcaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | kfree(codec); |
| 503 | } |
| 504 | |
| 505 | static void init_amp_hash(struct hda_codec *codec); |
| 506 | |
| 507 | /** |
| 508 | * snd_hda_codec_new - create a HDA codec |
| 509 | * @bus: the bus to assign |
| 510 | * @codec_addr: the codec address |
| 511 | * @codecp: the pointer to store the generated codec |
| 512 | * |
| 513 | * Returns 0 if successful, or a negative error code. |
| 514 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 515 | int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr, |
| 516 | struct hda_codec **codecp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
| 518 | struct hda_codec *codec; |
| 519 | char component[13]; |
| 520 | int err; |
| 521 | |
| 522 | snd_assert(bus, return -EINVAL); |
| 523 | snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL); |
| 524 | |
| 525 | if (bus->caddr_tbl[codec_addr]) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 526 | snd_printk(KERN_ERR "hda_codec: " |
| 527 | "address 0x%x is already occupied\n", codec_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return -EBUSY; |
| 529 | } |
| 530 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 531 | codec = kzalloc(sizeof(*codec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (codec == NULL) { |
| 533 | snd_printk(KERN_ERR "can't allocate struct hda_codec\n"); |
| 534 | return -ENOMEM; |
| 535 | } |
| 536 | |
| 537 | codec->bus = bus; |
| 538 | codec->addr = codec_addr; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 539 | mutex_init(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | init_amp_hash(codec); |
| 541 | |
| 542 | list_add_tail(&codec->list, &bus->codec_list); |
| 543 | bus->caddr_tbl[codec_addr] = codec; |
| 544 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 545 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 546 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 111d3af | 2006-02-16 18:17:58 +0100 | [diff] [blame] | 547 | if (codec->vendor_id == -1) |
| 548 | /* read again, hopefully the access method was corrected |
| 549 | * in the last read... |
| 550 | */ |
| 551 | codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 552 | AC_PAR_VENDOR_ID); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 553 | codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 554 | AC_PAR_SUBSYSTEM_ID); |
| 555 | codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, |
| 556 | AC_PAR_REV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 558 | setup_fg_nodes(codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 559 | if (!codec->afg && !codec->mfg) { |
Sasha Khapyorsky | 673b683 | 2005-08-11 11:00:16 +0200 | [diff] [blame] | 560 | snd_printdd("hda_codec: no AFG or MFG node found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | snd_hda_codec_free(codec); |
| 562 | return -ENODEV; |
| 563 | } |
| 564 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 565 | if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) { |
| 566 | snd_printk(KERN_ERR "hda_codec: cannot malloc\n"); |
| 567 | snd_hda_codec_free(codec); |
| 568 | return -ENOMEM; |
| 569 | } |
| 570 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 571 | if (!codec->subsystem_id) { |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 572 | hda_nid_t nid = codec->afg ? codec->afg : codec->mfg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 573 | codec->subsystem_id = |
| 574 | snd_hda_codec_read(codec, nid, 0, |
| 575 | AC_VERB_GET_SUBSYSTEM_ID, 0); |
Takashi Iwai | 86284e4 | 2005-10-11 15:05:54 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Takashi Iwai | d5ad630 | 2007-03-07 15:55:59 +0100 | [diff] [blame] | 578 | codec->preset = find_codec_preset(codec); |
Takashi Iwai | 43ea1d4 | 2007-04-26 19:12:08 +0200 | [diff] [blame] | 579 | /* audio codec should override the mixer name */ |
| 580 | if (codec->afg || !*bus->card->mixername) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | snd_hda_get_codec_name(codec, bus->card->mixername, |
| 582 | sizeof(bus->card->mixername)); |
| 583 | |
| 584 | if (codec->preset && codec->preset->patch) |
| 585 | err = codec->preset->patch(codec); |
| 586 | else |
| 587 | err = snd_hda_parse_generic_codec(codec); |
| 588 | if (err < 0) { |
| 589 | snd_hda_codec_free(codec); |
| 590 | return err; |
| 591 | } |
| 592 | |
Takashi Iwai | 9f146bb | 2005-11-17 11:07:49 +0100 | [diff] [blame] | 593 | if (codec->patch_ops.unsol_event) |
| 594 | init_unsol_queue(bus); |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | snd_hda_codec_proc_new(codec); |
| 597 | |
| 598 | sprintf(component, "HDA:%08x", codec->vendor_id); |
| 599 | snd_component_add(codec->bus->card, component); |
| 600 | |
| 601 | if (codecp) |
| 602 | *codecp = codec; |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * snd_hda_codec_setup_stream - set up the codec for streaming |
| 608 | * @codec: the CODEC to set up |
| 609 | * @nid: the NID to set up |
| 610 | * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. |
| 611 | * @channel_id: channel id to pass, zero based. |
| 612 | * @format: stream format. |
| 613 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 614 | void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
| 615 | u32 stream_tag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | int channel_id, int format) |
| 617 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 618 | if (!nid) |
Takashi Iwai | d21b37e | 2005-04-20 13:45:55 +0200 | [diff] [blame] | 619 | return; |
| 620 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 621 | snd_printdd("hda_codec_setup_stream: " |
| 622 | "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | nid, stream_tag, channel_id, format); |
| 624 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, |
| 625 | (stream_tag << 4) | channel_id); |
| 626 | msleep(1); |
| 627 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format); |
| 628 | } |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | /* |
| 631 | * amp access functions |
| 632 | */ |
| 633 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 634 | /* FIXME: more better hash key? */ |
| 635 | #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] | 636 | #define INFO_AMP_CAPS (1<<0) |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 637 | #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | /* initialize the hash table */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 640 | static void __devinit init_amp_hash(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
| 642 | memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash)); |
| 643 | codec->num_amp_entries = 0; |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 644 | codec->amp_info_size = 0; |
| 645 | codec->amp_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* query the hash. allocate an entry if not found. */ |
| 649 | static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key) |
| 650 | { |
| 651 | u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash); |
| 652 | u16 cur = codec->amp_hash[idx]; |
| 653 | struct hda_amp_info *info; |
| 654 | |
| 655 | while (cur != 0xffff) { |
| 656 | info = &codec->amp_info[cur]; |
| 657 | if (info->key == key) |
| 658 | return info; |
| 659 | cur = info->next; |
| 660 | } |
| 661 | |
| 662 | /* add a new hash entry */ |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 663 | if (codec->num_amp_entries >= codec->amp_info_size) { |
| 664 | /* reallocate the array */ |
| 665 | int new_size = codec->amp_info_size + 64; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 666 | struct hda_amp_info *new_info; |
| 667 | new_info = kcalloc(new_size, sizeof(struct hda_amp_info), |
| 668 | GFP_KERNEL); |
| 669 | if (!new_info) { |
| 670 | snd_printk(KERN_ERR "hda_codec: " |
| 671 | "can't malloc amp_info\n"); |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | if (codec->amp_info) { |
| 675 | memcpy(new_info, codec->amp_info, |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 676 | codec->amp_info_size * |
| 677 | sizeof(struct hda_amp_info)); |
Takashi Iwai | d031166 | 2005-11-07 14:38:44 +0100 | [diff] [blame] | 678 | kfree(codec->amp_info); |
| 679 | } |
| 680 | codec->amp_info_size = new_size; |
| 681 | codec->amp_info = new_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | cur = codec->num_amp_entries++; |
| 684 | info = &codec->amp_info[cur]; |
| 685 | info->key = key; |
| 686 | info->status = 0; /* not initialized yet */ |
| 687 | info->next = codec->amp_hash[idx]; |
| 688 | codec->amp_hash[idx] = cur; |
| 689 | |
| 690 | return info; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * query AMP capabilities for the given widget and direction |
| 695 | */ |
| 696 | static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) |
| 697 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 698 | struct hda_amp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 700 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0)); |
| 701 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | return 0; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 703 | if (!(info->status & INFO_AMP_CAPS)) { |
| 704 | if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | nid = codec->afg; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 706 | info->amp_caps = snd_hda_param_read(codec, nid, |
| 707 | direction == HDA_OUTPUT ? |
| 708 | AC_PAR_AMP_OUT_CAP : |
| 709 | AC_PAR_AMP_IN_CAP); |
Takashi Iwai | b75e53f | 2007-05-10 16:56:09 +0200 | [diff] [blame^] | 710 | if (info->amp_caps) |
| 711 | info->status |= INFO_AMP_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | return info->amp_caps; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * read the current volume to info |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 718 | * if the cache exists, read the cache value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 720 | static unsigned int get_vol_mute(struct hda_codec *codec, |
| 721 | struct hda_amp_info *info, hda_nid_t nid, |
| 722 | int ch, int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
| 724 | u32 val, parm; |
| 725 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 726 | if (info->status & INFO_AMP_VOL(ch)) |
| 727 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; |
| 730 | parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; |
| 731 | parm |= index; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 732 | val = snd_hda_codec_read(codec, nid, 0, |
| 733 | AC_VERB_GET_AMP_GAIN_MUTE, parm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | info->vol[ch] = val & 0xff; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 735 | info->status |= INFO_AMP_VOL(ch); |
| 736 | return info->vol[ch]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 740 | * 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] | 741 | */ |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 742 | 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] | 743 | hda_nid_t nid, int ch, int direction, int index, |
| 744 | int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
| 746 | u32 parm; |
| 747 | |
| 748 | parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; |
| 749 | parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; |
| 750 | parm |= index << AC_AMP_SET_INDEX_SHIFT; |
| 751 | parm |= val; |
| 752 | 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] | 753 | info->vol[ch] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 757 | * 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] | 758 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 759 | int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 760 | int direction, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 762 | struct hda_amp_info *info; |
| 763 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); |
| 764 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 766 | return get_vol_mute(codec, info, nid, ch, direction, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 769 | /* |
| 770 | * update the AMP value, mask = bit mask to set, val = the value |
| 771 | */ |
Takashi Iwai | 834be88 | 2006-03-01 14:16:17 +0100 | [diff] [blame] | 772 | int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, |
| 773 | int direction, int idx, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 775 | struct hda_amp_info *info; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 776 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 777 | info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx)); |
| 778 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 780 | val &= mask; |
| 781 | val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 782 | if (info->vol[ch] == val && !codec->in_resume) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | return 0; |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 784 | put_vol_mute(codec, info, nid, ch, direction, idx, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return 1; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /* |
| 790 | * AMP control callbacks |
| 791 | */ |
| 792 | /* retrieve parameters from private_value */ |
| 793 | #define get_amp_nid(kc) ((kc)->private_value & 0xffff) |
| 794 | #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3) |
| 795 | #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1) |
| 796 | #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf) |
| 797 | |
| 798 | /* volume */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 799 | int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, |
| 800 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
| 802 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 803 | u16 nid = get_amp_nid(kcontrol); |
| 804 | u8 chs = get_amp_channels(kcontrol); |
| 805 | int dir = get_amp_direction(kcontrol); |
| 806 | u32 caps; |
| 807 | |
| 808 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 809 | /* num steps */ |
| 810 | caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; |
| 811 | if (!caps) { |
| 812 | printk(KERN_WARNING "hda_codec: " |
| 813 | "num_steps = 0 for NID=0x%x\n", nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return -EINVAL; |
| 815 | } |
| 816 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 817 | uinfo->count = chs == 3 ? 2 : 1; |
| 818 | uinfo->value.integer.min = 0; |
| 819 | uinfo->value.integer.max = caps; |
| 820 | return 0; |
| 821 | } |
| 822 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 823 | int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, |
| 824 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
| 826 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 827 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 828 | int chs = get_amp_channels(kcontrol); |
| 829 | int dir = get_amp_direction(kcontrol); |
| 830 | int idx = get_amp_index(kcontrol); |
| 831 | long *valp = ucontrol->value.integer.value; |
| 832 | |
| 833 | if (chs & 1) |
| 834 | *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f; |
| 835 | if (chs & 2) |
| 836 | *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f; |
| 837 | return 0; |
| 838 | } |
| 839 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 840 | int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, |
| 841 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
| 843 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 844 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 845 | int chs = get_amp_channels(kcontrol); |
| 846 | int dir = get_amp_direction(kcontrol); |
| 847 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | long *valp = ucontrol->value.integer.value; |
| 849 | int change = 0; |
| 850 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 851 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 852 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
| 853 | 0x7f, *valp); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 854 | valp++; |
| 855 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 856 | if (chs & 2) |
| 857 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 858 | 0x7f, *valp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | return change; |
| 860 | } |
| 861 | |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 862 | int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 863 | unsigned int size, unsigned int __user *_tlv) |
| 864 | { |
| 865 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 866 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 867 | int dir = get_amp_direction(kcontrol); |
| 868 | u32 caps, val1, val2; |
| 869 | |
| 870 | if (size < 4 * sizeof(unsigned int)) |
| 871 | return -ENOMEM; |
| 872 | caps = query_amp_caps(codec, nid, dir); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 873 | val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; |
| 874 | val2 = (val2 + 1) * 25; |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 875 | val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); |
| 876 | val1 = ((int)val1) * ((int)val2); |
Jaroslav Kysela | 302e9c5 | 2006-07-05 17:39:49 +0200 | [diff] [blame] | 877 | if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) |
| 878 | return -EFAULT; |
| 879 | if (put_user(2 * sizeof(unsigned int), _tlv + 1)) |
| 880 | return -EFAULT; |
| 881 | if (put_user(val1, _tlv + 2)) |
| 882 | return -EFAULT; |
| 883 | if (put_user(val2, _tlv + 3)) |
| 884 | return -EFAULT; |
| 885 | return 0; |
| 886 | } |
| 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /* switch */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 889 | int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, |
| 890 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { |
| 892 | int chs = get_amp_channels(kcontrol); |
| 893 | |
| 894 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 895 | uinfo->count = chs == 3 ? 2 : 1; |
| 896 | uinfo->value.integer.min = 0; |
| 897 | uinfo->value.integer.max = 1; |
| 898 | return 0; |
| 899 | } |
| 900 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 901 | int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, |
| 902 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
| 904 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 905 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 906 | int chs = get_amp_channels(kcontrol); |
| 907 | int dir = get_amp_direction(kcontrol); |
| 908 | int idx = get_amp_index(kcontrol); |
| 909 | long *valp = ucontrol->value.integer.value; |
| 910 | |
| 911 | if (chs & 1) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 912 | *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & |
| 913 | 0x80) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if (chs & 2) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 915 | *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & |
| 916 | 0x80) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 920 | int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, |
| 921 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | { |
| 923 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 924 | hda_nid_t nid = get_amp_nid(kcontrol); |
| 925 | int chs = get_amp_channels(kcontrol); |
| 926 | int dir = get_amp_direction(kcontrol); |
| 927 | int idx = get_amp_index(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | long *valp = ucontrol->value.integer.value; |
| 929 | int change = 0; |
| 930 | |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 931 | if (chs & 1) { |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 932 | change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, |
| 933 | 0x80, *valp ? 0 : 0x80); |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 934 | valp++; |
| 935 | } |
Takashi Iwai | 4a19fae | 2005-06-08 14:43:58 +0200 | [diff] [blame] | 936 | if (chs & 2) |
| 937 | change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, |
Nicolas Graziano | b9f5a89 | 2005-07-29 12:17:20 +0200 | [diff] [blame] | 938 | 0x80, *valp ? 0 : 0x80); |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return change; |
| 941 | } |
| 942 | |
| 943 | /* |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 944 | * bound volume controls |
| 945 | * |
| 946 | * bind multiple volumes (# indices, from 0) |
| 947 | */ |
| 948 | |
| 949 | #define AMP_VAL_IDX_SHIFT 19 |
| 950 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 951 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 952 | int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, |
| 953 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 954 | { |
| 955 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 956 | unsigned long pval; |
| 957 | int err; |
| 958 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 959 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 960 | pval = kcontrol->private_value; |
| 961 | kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ |
| 962 | err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); |
| 963 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 964 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 965 | return err; |
| 966 | } |
| 967 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 968 | int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, |
| 969 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 970 | { |
| 971 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 972 | unsigned long pval; |
| 973 | int i, indices, err = 0, change = 0; |
| 974 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 975 | mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */ |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 976 | pval = kcontrol->private_value; |
| 977 | indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; |
| 978 | for (i = 0; i < indices; i++) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 979 | kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | |
| 980 | (i << AMP_VAL_IDX_SHIFT); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 981 | err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 982 | if (err < 0) |
| 983 | break; |
| 984 | change |= err; |
| 985 | } |
| 986 | kcontrol->private_value = pval; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 987 | mutex_unlock(&codec->spdif_mutex); |
Takashi Iwai | 985be54 | 2005-11-02 18:26:49 +0100 | [diff] [blame] | 988 | return err < 0 ? err : change; |
| 989 | } |
| 990 | |
| 991 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | * SPDIF out controls |
| 993 | */ |
| 994 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 995 | static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 996 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
| 998 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 999 | uinfo->count = 1; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1003 | static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, |
| 1004 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
| 1006 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1007 | IEC958_AES0_NONAUDIO | |
| 1008 | IEC958_AES0_CON_EMPHASIS_5015 | |
| 1009 | IEC958_AES0_CON_NOT_COPYRIGHT; |
| 1010 | ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | |
| 1011 | IEC958_AES1_CON_ORIGINAL; |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1015 | static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, |
| 1016 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | { |
| 1018 | ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | |
| 1019 | IEC958_AES0_NONAUDIO | |
| 1020 | IEC958_AES0_PRO_EMPHASIS_5015; |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1024 | static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 1025 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | { |
| 1027 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1028 | |
| 1029 | ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff; |
| 1030 | ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff; |
| 1031 | ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff; |
| 1032 | ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff; |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | /* convert from SPDIF status bits to HDA SPDIF bits |
| 1038 | * bit 0 (DigEn) is always set zero (to be filled later) |
| 1039 | */ |
| 1040 | static unsigned short convert_from_spdif_status(unsigned int sbits) |
| 1041 | { |
| 1042 | unsigned short val = 0; |
| 1043 | |
| 1044 | if (sbits & IEC958_AES0_PROFESSIONAL) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1045 | val |= AC_DIG1_PROFESSIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | if (sbits & IEC958_AES0_NONAUDIO) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1047 | val |= AC_DIG1_NONAUDIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1049 | if ((sbits & IEC958_AES0_PRO_EMPHASIS) == |
| 1050 | IEC958_AES0_PRO_EMPHASIS_5015) |
| 1051 | val |= AC_DIG1_EMPHASIS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1053 | if ((sbits & IEC958_AES0_CON_EMPHASIS) == |
| 1054 | IEC958_AES0_CON_EMPHASIS_5015) |
| 1055 | val |= AC_DIG1_EMPHASIS; |
| 1056 | if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) |
| 1057 | val |= AC_DIG1_COPYRIGHT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1059 | val |= AC_DIG1_LEVEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); |
| 1061 | } |
| 1062 | return val; |
| 1063 | } |
| 1064 | |
| 1065 | /* convert to SPDIF status bits from HDA SPDIF bits |
| 1066 | */ |
| 1067 | static unsigned int convert_to_spdif_status(unsigned short val) |
| 1068 | { |
| 1069 | unsigned int sbits = 0; |
| 1070 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1071 | if (val & AC_DIG1_NONAUDIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | sbits |= IEC958_AES0_NONAUDIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1073 | if (val & AC_DIG1_PROFESSIONAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | sbits |= IEC958_AES0_PROFESSIONAL; |
| 1075 | if (sbits & IEC958_AES0_PROFESSIONAL) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1076 | if (sbits & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | sbits |= IEC958_AES0_PRO_EMPHASIS_5015; |
| 1078 | } else { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1079 | if (val & AC_DIG1_EMPHASIS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | sbits |= IEC958_AES0_CON_EMPHASIS_5015; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1081 | if (!(val & AC_DIG1_COPYRIGHT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1083 | if (val & AC_DIG1_LEVEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | sbits |= (IEC958_AES1_CON_ORIGINAL << 8); |
| 1085 | sbits |= val & (0x7f << 8); |
| 1086 | } |
| 1087 | return sbits; |
| 1088 | } |
| 1089 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1090 | static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 1091 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | { |
| 1093 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1094 | hda_nid_t nid = kcontrol->private_value; |
| 1095 | unsigned short val; |
| 1096 | int change; |
| 1097 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1098 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | codec->spdif_status = ucontrol->value.iec958.status[0] | |
| 1100 | ((unsigned int)ucontrol->value.iec958.status[1] << 8) | |
| 1101 | ((unsigned int)ucontrol->value.iec958.status[2] << 16) | |
| 1102 | ((unsigned int)ucontrol->value.iec958.status[3] << 24); |
| 1103 | val = convert_from_spdif_status(codec->spdif_status); |
| 1104 | val |= codec->spdif_ctls & 1; |
| 1105 | change = codec->spdif_ctls != val; |
| 1106 | codec->spdif_ctls = val; |
| 1107 | |
| 1108 | if (change || codec->in_resume) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1109 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
| 1110 | val & 0xff); |
| 1111 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, |
| 1112 | val >> 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1115 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | return change; |
| 1117 | } |
| 1118 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1119 | static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, |
| 1120 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | { |
| 1122 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1123 | uinfo->count = 1; |
| 1124 | uinfo->value.integer.min = 0; |
| 1125 | uinfo->value.integer.max = 1; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1129 | static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, |
| 1130 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
| 1132 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1133 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1134 | ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1138 | static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, |
| 1139 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
| 1141 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1142 | hda_nid_t nid = kcontrol->private_value; |
| 1143 | unsigned short val; |
| 1144 | int change; |
| 1145 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1146 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1147 | val = codec->spdif_ctls & ~AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | if (ucontrol->value.integer.value[0]) |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1149 | val |= AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | change = codec->spdif_ctls != val; |
| 1151 | if (change || codec->in_resume) { |
| 1152 | codec->spdif_ctls = val; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 1153 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
| 1154 | val & 0xff); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1155 | /* unmute amp switch (if any) */ |
| 1156 | if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && |
| 1157 | (val & AC_DIG1_ENABLE)) |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 1158 | snd_hda_codec_write(codec, nid, 0, |
| 1159 | AC_VERB_SET_AMP_GAIN_MUTE, |
| 1160 | AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1161 | AC_AMP_SET_OUTPUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1163 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | return change; |
| 1165 | } |
| 1166 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1167 | static struct snd_kcontrol_new dig_mixes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
| 1169 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1170 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1171 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK), |
| 1172 | .info = snd_hda_spdif_mask_info, |
| 1173 | .get = snd_hda_spdif_cmask_get, |
| 1174 | }, |
| 1175 | { |
| 1176 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1177 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1178 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK), |
| 1179 | .info = snd_hda_spdif_mask_info, |
| 1180 | .get = snd_hda_spdif_pmask_get, |
| 1181 | }, |
| 1182 | { |
| 1183 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1184 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 1185 | .info = snd_hda_spdif_mask_info, |
| 1186 | .get = snd_hda_spdif_default_get, |
| 1187 | .put = snd_hda_spdif_default_put, |
| 1188 | }, |
| 1189 | { |
| 1190 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1191 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), |
| 1192 | .info = snd_hda_spdif_out_switch_info, |
| 1193 | .get = snd_hda_spdif_out_switch_get, |
| 1194 | .put = snd_hda_spdif_out_switch_put, |
| 1195 | }, |
| 1196 | { } /* end */ |
| 1197 | }; |
| 1198 | |
| 1199 | /** |
| 1200 | * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls |
| 1201 | * @codec: the HDA codec |
| 1202 | * @nid: audio out widget NID |
| 1203 | * |
| 1204 | * Creates controls related with the SPDIF output. |
| 1205 | * Called from each patch supporting the SPDIF out. |
| 1206 | * |
| 1207 | * Returns 0 if successful, or a negative error code. |
| 1208 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1209 | int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec, |
| 1210 | hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
| 1212 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1213 | struct snd_kcontrol *kctl; |
| 1214 | struct snd_kcontrol_new *dig_mix; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
| 1216 | for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { |
| 1217 | kctl = snd_ctl_new1(dig_mix, codec); |
| 1218 | kctl->private_value = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1219 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1220 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return err; |
| 1222 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1223 | codec->spdif_ctls = |
| 1224 | snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * SPDIF input |
| 1231 | */ |
| 1232 | |
| 1233 | #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info |
| 1234 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1235 | static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, |
| 1236 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | { |
| 1238 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1239 | |
| 1240 | ucontrol->value.integer.value[0] = codec->spdif_in_enable; |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1244 | static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, |
| 1245 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | { |
| 1247 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1248 | hda_nid_t nid = kcontrol->private_value; |
| 1249 | unsigned int val = !!ucontrol->value.integer.value[0]; |
| 1250 | int change; |
| 1251 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1252 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | change = codec->spdif_in_enable != val; |
| 1254 | if (change || codec->in_resume) { |
| 1255 | codec->spdif_in_enable = val; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1256 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
| 1257 | val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1259 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | return change; |
| 1261 | } |
| 1262 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1263 | static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, |
| 1264 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
| 1266 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1267 | hda_nid_t nid = kcontrol->private_value; |
| 1268 | unsigned short val; |
| 1269 | unsigned int sbits; |
| 1270 | |
| 1271 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0); |
| 1272 | sbits = convert_to_spdif_status(val); |
| 1273 | ucontrol->value.iec958.status[0] = sbits; |
| 1274 | ucontrol->value.iec958.status[1] = sbits >> 8; |
| 1275 | ucontrol->value.iec958.status[2] = sbits >> 16; |
| 1276 | ucontrol->value.iec958.status[3] = sbits >> 24; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1280 | static struct snd_kcontrol_new dig_in_ctls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | { |
| 1282 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1283 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), |
| 1284 | .info = snd_hda_spdif_in_switch_info, |
| 1285 | .get = snd_hda_spdif_in_switch_get, |
| 1286 | .put = snd_hda_spdif_in_switch_put, |
| 1287 | }, |
| 1288 | { |
| 1289 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1290 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1291 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 1292 | .info = snd_hda_spdif_mask_info, |
| 1293 | .get = snd_hda_spdif_in_status_get, |
| 1294 | }, |
| 1295 | { } /* end */ |
| 1296 | }; |
| 1297 | |
| 1298 | /** |
| 1299 | * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls |
| 1300 | * @codec: the HDA codec |
| 1301 | * @nid: audio in widget NID |
| 1302 | * |
| 1303 | * Creates controls related with the SPDIF input. |
| 1304 | * Called from each patch supporting the SPDIF in. |
| 1305 | * |
| 1306 | * Returns 0 if successful, or a negative error code. |
| 1307 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1308 | int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec, |
| 1309 | hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | { |
| 1311 | int err; |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1312 | struct snd_kcontrol *kctl; |
| 1313 | struct snd_kcontrol_new *dig_mix; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
| 1315 | for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { |
| 1316 | kctl = snd_ctl_new1(dig_mix, codec); |
| 1317 | kctl->private_value = nid; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1318 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1319 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | return err; |
| 1321 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1322 | codec->spdif_in_enable = |
| 1323 | snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & |
| 1324 | AC_DIG1_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1329 | /* |
| 1330 | * set power state of the codec |
| 1331 | */ |
| 1332 | static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg, |
| 1333 | unsigned int power_state) |
| 1334 | { |
| 1335 | hda_nid_t nid, nid_start; |
| 1336 | int nodes; |
| 1337 | |
| 1338 | snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE, |
| 1339 | power_state); |
| 1340 | |
| 1341 | nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start); |
| 1342 | for (nid = nid_start; nid < nodes + nid_start; nid++) { |
| 1343 | if (get_wcaps(codec, nid) & AC_WCAP_POWER) |
| 1344 | snd_hda_codec_write(codec, nid, 0, |
| 1345 | AC_VERB_SET_POWER_STATE, |
| 1346 | power_state); |
| 1347 | } |
| 1348 | |
| 1349 | if (power_state == AC_PWRST_D0) |
| 1350 | msleep(10); |
| 1351 | } |
| 1352 | |
| 1353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | /** |
| 1355 | * snd_hda_build_controls - build mixer controls |
| 1356 | * @bus: the BUS |
| 1357 | * |
| 1358 | * Creates mixer controls for each codec included in the bus. |
| 1359 | * |
| 1360 | * Returns 0 if successful, otherwise a negative error code. |
| 1361 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1362 | int __devinit snd_hda_build_controls(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1364 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* build controls */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1367 | list_for_each_entry(codec, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | int err; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1369 | if (!codec->patch_ops.build_controls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | continue; |
| 1371 | err = codec->patch_ops.build_controls(codec); |
| 1372 | if (err < 0) |
| 1373 | return err; |
| 1374 | } |
| 1375 | |
| 1376 | /* initialize */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1377 | list_for_each_entry(codec, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | int err; |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1379 | hda_set_power_state(codec, |
| 1380 | codec->afg ? codec->afg : codec->mfg, |
| 1381 | AC_PWRST_D0); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1382 | if (!codec->patch_ops.init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | continue; |
| 1384 | err = codec->patch_ops.init(codec); |
| 1385 | if (err < 0) |
| 1386 | return err; |
| 1387 | } |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * stream formats |
| 1393 | */ |
Takashi Iwai | befdf316 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1394 | struct hda_rate_tbl { |
| 1395 | unsigned int hz; |
| 1396 | unsigned int alsa_bits; |
| 1397 | unsigned int hda_fmt; |
| 1398 | }; |
| 1399 | |
| 1400 | static struct hda_rate_tbl rate_bits[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | /* rate in Hz, ALSA rate bitmask, HDA format value */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* autodetected value used in snd_hda_query_supported_pcm */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */ |
| 1405 | { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */ |
| 1406 | { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */ |
| 1407 | { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */ |
| 1408 | { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */ |
| 1409 | { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */ |
| 1410 | { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */ |
| 1411 | { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */ |
| 1412 | { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */ |
| 1413 | { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */ |
| 1414 | { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */ |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 1415 | #define AC_PAR_PCM_RATE_BITS 11 |
| 1416 | /* up to bits 10, 384kHZ isn't supported properly */ |
| 1417 | |
| 1418 | /* not autodetected value */ |
| 1419 | { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */ |
Nicolas Graziano | 9d8f53f | 2005-08-22 13:47:16 +0200 | [diff] [blame] | 1420 | |
Takashi Iwai | befdf316 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1421 | { 0 } /* terminator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | }; |
| 1423 | |
| 1424 | /** |
| 1425 | * snd_hda_calc_stream_format - calculate format bitset |
| 1426 | * @rate: the sample rate |
| 1427 | * @channels: the number of channels |
| 1428 | * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) |
| 1429 | * @maxbps: the max. bps |
| 1430 | * |
| 1431 | * Calculate the format bitset from the given rate, channels and th PCM format. |
| 1432 | * |
| 1433 | * Return zero if invalid. |
| 1434 | */ |
| 1435 | unsigned int snd_hda_calc_stream_format(unsigned int rate, |
| 1436 | unsigned int channels, |
| 1437 | unsigned int format, |
| 1438 | unsigned int maxbps) |
| 1439 | { |
| 1440 | int i; |
| 1441 | unsigned int val = 0; |
| 1442 | |
Takashi Iwai | befdf316 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1443 | for (i = 0; rate_bits[i].hz; i++) |
| 1444 | if (rate_bits[i].hz == rate) { |
| 1445 | val = rate_bits[i].hda_fmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | break; |
| 1447 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1448 | if (!rate_bits[i].hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | snd_printdd("invalid rate %d\n", rate); |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | if (channels == 0 || channels > 8) { |
| 1454 | snd_printdd("invalid channels %d\n", channels); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | val |= channels - 1; |
| 1458 | |
| 1459 | switch (snd_pcm_format_width(format)) { |
| 1460 | case 8: val |= 0x00; break; |
| 1461 | case 16: val |= 0x10; break; |
| 1462 | case 20: |
| 1463 | case 24: |
| 1464 | case 32: |
| 1465 | if (maxbps >= 32) |
| 1466 | val |= 0x40; |
| 1467 | else if (maxbps >= 24) |
| 1468 | val |= 0x30; |
| 1469 | else |
| 1470 | val |= 0x20; |
| 1471 | break; |
| 1472 | default: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1473 | snd_printdd("invalid format width %d\n", |
| 1474 | snd_pcm_format_width(format)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | return val; |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * snd_hda_query_supported_pcm - query the supported PCM rates and formats |
| 1483 | * @codec: the HDA codec |
| 1484 | * @nid: NID to query |
| 1485 | * @ratesp: the pointer to store the detected rate bitflags |
| 1486 | * @formatsp: the pointer to store the detected formats |
| 1487 | * @bpsp: the pointer to store the detected format widths |
| 1488 | * |
| 1489 | * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp |
| 1490 | * or @bsps argument is ignored. |
| 1491 | * |
| 1492 | * Returns 0 if successful, otherwise a negative error code. |
| 1493 | */ |
| 1494 | int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, |
| 1495 | u32 *ratesp, u64 *formatsp, unsigned int *bpsp) |
| 1496 | { |
| 1497 | int i; |
| 1498 | unsigned int val, streams; |
| 1499 | |
| 1500 | val = 0; |
| 1501 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1502 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 1504 | if (val == -1) |
| 1505 | return -EIO; |
| 1506 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1507 | if (!val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 1509 | |
| 1510 | if (ratesp) { |
| 1511 | u32 rates = 0; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 1512 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | if (val & (1 << i)) |
Takashi Iwai | befdf316 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1514 | rates |= rate_bits[i].alsa_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | *ratesp = rates; |
| 1517 | } |
| 1518 | |
| 1519 | if (formatsp || bpsp) { |
| 1520 | u64 formats = 0; |
| 1521 | unsigned int bps; |
| 1522 | unsigned int wcaps; |
| 1523 | |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1524 | wcaps = get_wcaps(codec, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 1526 | if (streams == -1) |
| 1527 | return -EIO; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1528 | if (!streams) { |
| 1529 | streams = snd_hda_param_read(codec, codec->afg, |
| 1530 | AC_PAR_STREAM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | if (streams == -1) |
| 1532 | return -EIO; |
| 1533 | } |
| 1534 | |
| 1535 | bps = 0; |
| 1536 | if (streams & AC_SUPFMT_PCM) { |
| 1537 | if (val & AC_SUPPCM_BITS_8) { |
| 1538 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 1539 | bps = 8; |
| 1540 | } |
| 1541 | if (val & AC_SUPPCM_BITS_16) { |
| 1542 | formats |= SNDRV_PCM_FMTBIT_S16_LE; |
| 1543 | bps = 16; |
| 1544 | } |
| 1545 | if (wcaps & AC_WCAP_DIGITAL) { |
| 1546 | if (val & AC_SUPPCM_BITS_32) |
| 1547 | formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; |
| 1548 | if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) |
| 1549 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 1550 | if (val & AC_SUPPCM_BITS_24) |
| 1551 | bps = 24; |
| 1552 | else if (val & AC_SUPPCM_BITS_20) |
| 1553 | bps = 20; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1554 | } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| |
| 1555 | AC_SUPPCM_BITS_32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | formats |= SNDRV_PCM_FMTBIT_S32_LE; |
| 1557 | if (val & AC_SUPPCM_BITS_32) |
| 1558 | bps = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | else if (val & AC_SUPPCM_BITS_24) |
| 1560 | bps = 24; |
Nicolas Graziano | 33ef7651 | 2006-09-19 14:23:14 +0200 | [diff] [blame] | 1561 | else if (val & AC_SUPPCM_BITS_20) |
| 1562 | bps = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1565 | else if (streams == AC_SUPFMT_FLOAT32) { |
| 1566 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; |
| 1568 | bps = 32; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1569 | } else if (streams == AC_SUPFMT_AC3) { |
| 1570 | /* should be exclusive */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | /* temporary hack: we have still no proper support |
| 1572 | * for the direct AC3 stream... |
| 1573 | */ |
| 1574 | formats |= SNDRV_PCM_FMTBIT_U8; |
| 1575 | bps = 8; |
| 1576 | } |
| 1577 | if (formatsp) |
| 1578 | *formatsp = formats; |
| 1579 | if (bpsp) |
| 1580 | *bpsp = bps; |
| 1581 | } |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | /** |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1587 | * snd_hda_is_supported_format - check whether the given node supports |
| 1588 | * the format val |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | * |
| 1590 | * Returns 1 if supported, 0 if not. |
| 1591 | */ |
| 1592 | int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, |
| 1593 | unsigned int format) |
| 1594 | { |
| 1595 | int i; |
| 1596 | unsigned int val = 0, rate, stream; |
| 1597 | |
| 1598 | if (nid != codec->afg && |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1599 | (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | val = snd_hda_param_read(codec, nid, AC_PAR_PCM); |
| 1601 | if (val == -1) |
| 1602 | return 0; |
| 1603 | } |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1604 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); |
| 1606 | if (val == -1) |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | rate = format & 0xff00; |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 1611 | for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) |
Takashi Iwai | befdf316 | 2005-08-22 13:57:55 +0200 | [diff] [blame] | 1612 | if (rate_bits[i].hda_fmt == rate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | if (val & (1 << i)) |
| 1614 | break; |
| 1615 | return 0; |
| 1616 | } |
Takashi Iwai | a961f9f | 2007-04-12 13:08:09 +0200 | [diff] [blame] | 1617 | if (i >= AC_PAR_PCM_RATE_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | return 0; |
| 1619 | |
| 1620 | stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM); |
| 1621 | if (stream == -1) |
| 1622 | return 0; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1623 | if (!stream && nid != codec->afg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1625 | if (!stream || stream == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return 0; |
| 1627 | |
| 1628 | if (stream & AC_SUPFMT_PCM) { |
| 1629 | switch (format & 0xf0) { |
| 1630 | case 0x00: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1631 | if (!(val & AC_SUPPCM_BITS_8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | return 0; |
| 1633 | break; |
| 1634 | case 0x10: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1635 | if (!(val & AC_SUPPCM_BITS_16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return 0; |
| 1637 | break; |
| 1638 | case 0x20: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1639 | if (!(val & AC_SUPPCM_BITS_20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | return 0; |
| 1641 | break; |
| 1642 | case 0x30: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1643 | if (!(val & AC_SUPPCM_BITS_24)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return 0; |
| 1645 | break; |
| 1646 | case 0x40: |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1647 | if (!(val & AC_SUPPCM_BITS_32)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | return 0; |
| 1649 | break; |
| 1650 | default: |
| 1651 | return 0; |
| 1652 | } |
| 1653 | } else { |
| 1654 | /* FIXME: check for float32 and AC3? */ |
| 1655 | } |
| 1656 | |
| 1657 | return 1; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | * PCM stuff |
| 1662 | */ |
| 1663 | static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, |
| 1664 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1665 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, |
| 1671 | struct hda_codec *codec, |
| 1672 | unsigned int stream_tag, |
| 1673 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1674 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, |
| 1681 | struct hda_codec *codec, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1682 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | { |
| 1684 | snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0); |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1688 | static int __devinit set_pcm_default_values(struct hda_codec *codec, |
| 1689 | struct hda_pcm_stream *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1691 | /* query support PCM information from the given NID */ |
| 1692 | if (info->nid && (!info->rates || !info->formats)) { |
| 1693 | snd_hda_query_supported_pcm(codec, info->nid, |
| 1694 | info->rates ? NULL : &info->rates, |
| 1695 | info->formats ? NULL : &info->formats, |
| 1696 | info->maxbps ? NULL : &info->maxbps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
| 1698 | if (info->ops.open == NULL) |
| 1699 | info->ops.open = hda_pcm_default_open_close; |
| 1700 | if (info->ops.close == NULL) |
| 1701 | info->ops.close = hda_pcm_default_open_close; |
| 1702 | if (info->ops.prepare == NULL) { |
| 1703 | snd_assert(info->nid, return -EINVAL); |
| 1704 | info->ops.prepare = hda_pcm_default_prepare; |
| 1705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | if (info->ops.cleanup == NULL) { |
| 1707 | snd_assert(info->nid, return -EINVAL); |
| 1708 | info->ops.cleanup = hda_pcm_default_cleanup; |
| 1709 | } |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | /** |
| 1714 | * snd_hda_build_pcms - build PCM information |
| 1715 | * @bus: the BUS |
| 1716 | * |
| 1717 | * Create PCM information for each codec included in the bus. |
| 1718 | * |
| 1719 | * The build_pcms codec patch is requested to set up codec->num_pcms and |
| 1720 | * codec->pcm_info properly. The array is referred by the top-level driver |
| 1721 | * to create its PCM instances. |
| 1722 | * The allocated codec->pcm_info should be released in codec->patch_ops.free |
| 1723 | * callback. |
| 1724 | * |
| 1725 | * At least, substreams, channels_min and channels_max must be filled for |
| 1726 | * each stream. substreams = 0 indicates that the stream doesn't exist. |
| 1727 | * When rates and/or formats are zero, the supported values are queried |
| 1728 | * from the given nid. The nid is used also by the default ops.prepare |
| 1729 | * and ops.cleanup callbacks. |
| 1730 | * |
| 1731 | * The driver needs to call ops.open in its open callback. Similarly, |
| 1732 | * ops.close is supposed to be called in the close callback. |
| 1733 | * ops.prepare should be called in the prepare or hw_params callback |
| 1734 | * with the proper parameters for set up. |
| 1735 | * ops.cleanup should be called in hw_free for clean up of streams. |
| 1736 | * |
| 1737 | * This function returns 0 if successfull, or a negative error code. |
| 1738 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1739 | int __devinit snd_hda_build_pcms(struct hda_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1741 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1743 | list_for_each_entry(codec, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | unsigned int pcm, s; |
| 1745 | int err; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1746 | if (!codec->patch_ops.build_pcms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | continue; |
| 1748 | err = codec->patch_ops.build_pcms(codec); |
| 1749 | if (err < 0) |
| 1750 | return err; |
| 1751 | for (pcm = 0; pcm < codec->num_pcms; pcm++) { |
| 1752 | for (s = 0; s < 2; s++) { |
| 1753 | struct hda_pcm_stream *info; |
| 1754 | info = &codec->pcm_info[pcm].stream[s]; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1755 | if (!info->substreams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | continue; |
| 1757 | err = set_pcm_default_values(codec, info); |
| 1758 | if (err < 0) |
| 1759 | return err; |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /** |
| 1767 | * snd_hda_check_board_config - compare the current codec with the config table |
| 1768 | * @codec: the HDA codec |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 1769 | * @num_configs: number of config enums |
| 1770 | * @models: array of model name strings |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | * @tbl: configuration table, terminated by null entries |
| 1772 | * |
| 1773 | * Compares the modelname or PCI subsystem id of the current codec with the |
| 1774 | * given configuration table. If a matching entry is found, returns its |
| 1775 | * config value (supposed to be 0 or positive). |
| 1776 | * |
| 1777 | * If no entries are matching, the function returns a negative value. |
| 1778 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1779 | int __devinit snd_hda_check_board_config(struct hda_codec *codec, |
| 1780 | int num_configs, const char **models, |
| 1781 | const struct snd_pci_quirk *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | { |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 1783 | if (codec->bus->modelname && models) { |
| 1784 | int i; |
| 1785 | for (i = 0; i < num_configs; i++) { |
| 1786 | if (models[i] && |
| 1787 | !strcmp(codec->bus->modelname, models[i])) { |
| 1788 | snd_printd(KERN_INFO "hda_codec: model '%s' is " |
| 1789 | "selected\n", models[i]); |
| 1790 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 1795 | if (!codec->bus->pci || !tbl) |
| 1796 | return -1; |
| 1797 | |
| 1798 | tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl); |
| 1799 | if (!tbl) |
| 1800 | return -1; |
| 1801 | if (tbl->value >= 0 && tbl->value < num_configs) { |
| 1802 | #ifdef CONFIG_SND_DEBUG_DETECT |
| 1803 | char tmp[10]; |
| 1804 | const char *model = NULL; |
| 1805 | if (models) |
| 1806 | model = models[tbl->value]; |
| 1807 | if (!model) { |
| 1808 | sprintf(tmp, "#%d", tbl->value); |
| 1809 | model = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
Takashi Iwai | f5fcc13 | 2006-11-24 17:07:44 +0100 | [diff] [blame] | 1811 | snd_printdd(KERN_INFO "hda_codec: model '%s' is selected " |
| 1812 | "for config %x:%x (%s)\n", |
| 1813 | model, tbl->subvendor, tbl->subdevice, |
| 1814 | (tbl->name ? tbl->name : "Unknown device")); |
| 1815 | #endif |
| 1816 | return tbl->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
| 1818 | return -1; |
| 1819 | } |
| 1820 | |
| 1821 | /** |
| 1822 | * snd_hda_add_new_ctls - create controls from the array |
| 1823 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1824 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | * |
| 1826 | * This helper function creates and add new controls in the given array. |
| 1827 | * The array must be terminated with an empty entry as terminator. |
| 1828 | * |
| 1829 | * Returns 0 if successful, or a negative error code. |
| 1830 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 1831 | int __devinit snd_hda_add_new_ctls(struct hda_codec *codec, |
| 1832 | struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | { |
| 1834 | int err; |
| 1835 | |
| 1836 | for (; knew->name; knew++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1837 | struct snd_kcontrol *kctl; |
| 1838 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1839 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1840 | return -ENOMEM; |
| 1841 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1842 | if (err < 0) { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1843 | if (!codec->addr) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1844 | return err; |
| 1845 | kctl = snd_ctl_new1(knew, codec); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1846 | if (!kctl) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1847 | return -ENOMEM; |
| 1848 | kctl->id.device = codec->addr; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1849 | err = snd_ctl_add(codec->bus->card, kctl); |
| 1850 | if (err < 0) |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 1851 | return err; |
| 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 1858 | /* |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 1859 | * Channel mode helper |
| 1860 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1861 | int snd_hda_ch_mode_info(struct hda_codec *codec, |
| 1862 | struct snd_ctl_elem_info *uinfo, |
| 1863 | const struct hda_channel_mode *chmode, |
| 1864 | int num_chmodes) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 1865 | { |
| 1866 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1867 | uinfo->count = 1; |
| 1868 | uinfo->value.enumerated.items = num_chmodes; |
| 1869 | if (uinfo->value.enumerated.item >= num_chmodes) |
| 1870 | uinfo->value.enumerated.item = num_chmodes - 1; |
| 1871 | sprintf(uinfo->value.enumerated.name, "%dch", |
| 1872 | chmode[uinfo->value.enumerated.item].channels); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1876 | int snd_hda_ch_mode_get(struct hda_codec *codec, |
| 1877 | struct snd_ctl_elem_value *ucontrol, |
| 1878 | const struct hda_channel_mode *chmode, |
| 1879 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 1880 | int max_channels) |
| 1881 | { |
| 1882 | int i; |
| 1883 | |
| 1884 | for (i = 0; i < num_chmodes; i++) { |
| 1885 | if (max_channels == chmode[i].channels) { |
| 1886 | ucontrol->value.enumerated.item[0] = i; |
| 1887 | break; |
| 1888 | } |
| 1889 | } |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1893 | int snd_hda_ch_mode_put(struct hda_codec *codec, |
| 1894 | struct snd_ctl_elem_value *ucontrol, |
| 1895 | const struct hda_channel_mode *chmode, |
| 1896 | int num_chmodes, |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 1897 | int *max_channelsp) |
| 1898 | { |
| 1899 | unsigned int mode; |
| 1900 | |
| 1901 | mode = ucontrol->value.enumerated.item[0]; |
| 1902 | snd_assert(mode < num_chmodes, return -EINVAL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1903 | if (*max_channelsp == chmode[mode].channels && !codec->in_resume) |
Takashi Iwai | d2a6d7d | 2005-11-17 11:06:29 +0100 | [diff] [blame] | 1904 | return 0; |
| 1905 | /* change the current channel setting */ |
| 1906 | *max_channelsp = chmode[mode].channels; |
| 1907 | if (chmode[mode].sequence) |
| 1908 | snd_hda_sequence_write(codec, chmode[mode].sequence); |
| 1909 | return 1; |
| 1910 | } |
| 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * input MUX helper |
| 1914 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1915 | int snd_hda_input_mux_info(const struct hda_input_mux *imux, |
| 1916 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | { |
| 1918 | unsigned int index; |
| 1919 | |
| 1920 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1921 | uinfo->count = 1; |
| 1922 | uinfo->value.enumerated.items = imux->num_items; |
| 1923 | index = uinfo->value.enumerated.item; |
| 1924 | if (index >= imux->num_items) |
| 1925 | index = imux->num_items - 1; |
| 1926 | strcpy(uinfo->value.enumerated.name, imux->items[index].label); |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1930 | int snd_hda_input_mux_put(struct hda_codec *codec, |
| 1931 | const struct hda_input_mux *imux, |
| 1932 | struct snd_ctl_elem_value *ucontrol, |
| 1933 | hda_nid_t nid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | unsigned int *cur_val) |
| 1935 | { |
| 1936 | unsigned int idx; |
| 1937 | |
| 1938 | idx = ucontrol->value.enumerated.item[0]; |
| 1939 | if (idx >= imux->num_items) |
| 1940 | idx = imux->num_items - 1; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1941 | if (*cur_val == idx && !codec->in_resume) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return 0; |
| 1943 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, |
| 1944 | imux->items[idx].index); |
| 1945 | *cur_val = idx; |
| 1946 | return 1; |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | /* |
| 1951 | * Multi-channel / digital-out PCM helper functions |
| 1952 | */ |
| 1953 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 1954 | /* setup SPDIF output stream */ |
| 1955 | static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, |
| 1956 | unsigned int stream_tag, unsigned int format) |
| 1957 | { |
| 1958 | /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ |
| 1959 | if (codec->spdif_ctls & AC_DIG1_ENABLE) |
| 1960 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
| 1961 | codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff); |
| 1962 | snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); |
| 1963 | /* turn on again (if needed) */ |
| 1964 | if (codec->spdif_ctls & AC_DIG1_ENABLE) |
| 1965 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, |
| 1966 | codec->spdif_ctls & 0xff); |
| 1967 | } |
| 1968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | /* |
| 1970 | * open the digital out in the exclusive mode |
| 1971 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1972 | int snd_hda_multi_out_dig_open(struct hda_codec *codec, |
| 1973 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1975 | mutex_lock(&codec->spdif_mutex); |
Takashi Iwai | 5930ca4 | 2007-04-16 11:23:56 +0200 | [diff] [blame] | 1976 | if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) |
| 1977 | /* already opened as analog dup; reset it once */ |
| 1978 | 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] | 1979 | mout->dig_out_used = HDA_DIG_EXCLUSIVE; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 1980 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | return 0; |
| 1982 | } |
| 1983 | |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 1984 | int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, |
| 1985 | struct hda_multi_out *mout, |
| 1986 | unsigned int stream_tag, |
| 1987 | unsigned int format, |
| 1988 | struct snd_pcm_substream *substream) |
| 1989 | { |
| 1990 | mutex_lock(&codec->spdif_mutex); |
| 1991 | setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); |
| 1992 | mutex_unlock(&codec->spdif_mutex); |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | /* |
| 1997 | * release the digital out |
| 1998 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 1999 | int snd_hda_multi_out_dig_close(struct hda_codec *codec, |
| 2000 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | { |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2002 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | mout->dig_out_used = 0; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2004 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * set up more restrictions for analog out |
| 2010 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2011 | int snd_hda_multi_out_analog_open(struct hda_codec *codec, |
| 2012 | struct hda_multi_out *mout, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2013 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | { |
| 2015 | substream->runtime->hw.channels_max = mout->max_channels; |
| 2016 | return snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 2017 | SNDRV_PCM_HW_PARAM_CHANNELS, 2); |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * set up the i/o for analog out |
| 2022 | * when the digital out is available, copy the front out to digital out, too. |
| 2023 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2024 | int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, |
| 2025 | struct hda_multi_out *mout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | unsigned int stream_tag, |
| 2027 | unsigned int format, |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2028 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | { |
| 2030 | hda_nid_t *nids = mout->dac_nids; |
| 2031 | int chs = substream->runtime->channels; |
| 2032 | int i; |
| 2033 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2034 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) { |
| 2036 | if (chs == 2 && |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2037 | snd_hda_is_supported_format(codec, mout->dig_out_nid, |
| 2038 | format) && |
| 2039 | !(codec->spdif_status & IEC958_AES0_NONAUDIO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | mout->dig_out_used = HDA_DIG_ANALOG_DUP; |
Takashi Iwai | 6b97eb4 | 2007-04-05 14:51:48 +0200 | [diff] [blame] | 2041 | setup_dig_out_stream(codec, mout->dig_out_nid, |
| 2042 | stream_tag, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | } else { |
| 2044 | mout->dig_out_used = 0; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2045 | snd_hda_codec_setup_stream(codec, mout->dig_out_nid, |
| 2046 | 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2049 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
| 2051 | /* front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2052 | snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, |
| 2053 | 0, format); |
Takashi Iwai | 35aec4e | 2006-07-28 14:44:31 +0200 | [diff] [blame] | 2054 | if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | /* headphone out will just decode front left/right (stereo) */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2056 | snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, |
| 2057 | 0, format); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2058 | /* extra outputs copied from front */ |
| 2059 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 2060 | if (mout->extra_out_nid[i]) |
| 2061 | snd_hda_codec_setup_stream(codec, |
| 2062 | mout->extra_out_nid[i], |
| 2063 | stream_tag, 0, format); |
| 2064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | /* surrounds */ |
| 2066 | for (i = 1; i < mout->num_dacs; i++) { |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 2067 | if (chs >= (i + 1) * 2) /* independent out */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2068 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2069 | i * 2, format); |
Takashi Iwai | 4b3acaf | 2005-06-10 19:48:10 +0200 | [diff] [blame] | 2070 | else /* copy front */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2071 | snd_hda_codec_setup_stream(codec, nids[i], stream_tag, |
| 2072 | 0, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | } |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * clean up the setting for analog out |
| 2079 | */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2080 | int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, |
| 2081 | struct hda_multi_out *mout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | { |
| 2083 | hda_nid_t *nids = mout->dac_nids; |
| 2084 | int i; |
| 2085 | |
| 2086 | for (i = 0; i < mout->num_dacs; i++) |
| 2087 | snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0); |
| 2088 | if (mout->hp_nid) |
| 2089 | snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2090 | for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) |
| 2091 | if (mout->extra_out_nid[i]) |
| 2092 | snd_hda_codec_setup_stream(codec, |
| 2093 | mout->extra_out_nid[i], |
| 2094 | 0, 0, 0); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2095 | mutex_lock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { |
| 2097 | snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0); |
| 2098 | mout->dig_out_used = 0; |
| 2099 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 2100 | mutex_unlock(&codec->spdif_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2104 | /* |
| 2105 | * Helper for automatic ping configuration |
| 2106 | */ |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2107 | |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 2108 | static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list) |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2109 | { |
| 2110 | for (; *list; list++) |
| 2111 | if (*list == nid) |
| 2112 | return 1; |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2116 | |
| 2117 | /* |
| 2118 | * Sort an associated group of pins according to their sequence numbers. |
| 2119 | */ |
| 2120 | static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences, |
| 2121 | int num_pins) |
| 2122 | { |
| 2123 | int i, j; |
| 2124 | short seq; |
| 2125 | hda_nid_t nid; |
| 2126 | |
| 2127 | for (i = 0; i < num_pins; i++) { |
| 2128 | for (j = i + 1; j < num_pins; j++) { |
| 2129 | if (sequences[i] > sequences[j]) { |
| 2130 | seq = sequences[i]; |
| 2131 | sequences[i] = sequences[j]; |
| 2132 | sequences[j] = seq; |
| 2133 | nid = pins[i]; |
| 2134 | pins[i] = pins[j]; |
| 2135 | pins[j] = nid; |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2142 | /* |
| 2143 | * Parse all pin widgets and store the useful pin nids to cfg |
| 2144 | * |
| 2145 | * The number of line-outs or any primary output is stored in line_outs, |
| 2146 | * and the corresponding output pins are assigned to line_out_pins[], |
| 2147 | * in the order of front, rear, CLFE, side, ... |
| 2148 | * |
| 2149 | * If more extra outputs (speaker and headphone) are found, the pins are |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2150 | * 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] | 2151 | * is detected, one of speaker of HP pins is assigned as the primary |
| 2152 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 2153 | * if any analog output exists. |
| 2154 | * |
| 2155 | * The analog input pins are assigned to input_pins array. |
| 2156 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 2157 | * respectively. |
| 2158 | */ |
Takashi Iwai | 756e2b0 | 2007-04-16 11:27:07 +0200 | [diff] [blame] | 2159 | int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec, |
| 2160 | struct auto_pin_cfg *cfg, |
| 2161 | hda_nid_t *ignore_nids) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2162 | { |
| 2163 | hda_nid_t nid, nid_start; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2164 | int nodes; |
| 2165 | short seq, assoc_line_out, assoc_speaker; |
| 2166 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 2167 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2168 | |
| 2169 | memset(cfg, 0, sizeof(*cfg)); |
| 2170 | |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2171 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 2172 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
| 2173 | assoc_line_out = assoc_speaker = 0; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2174 | |
| 2175 | nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start); |
| 2176 | for (nid = nid_start; nid < nodes + nid_start; nid++) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2177 | unsigned int wid_caps = get_wcaps(codec, nid); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2178 | unsigned int wid_type = |
| 2179 | (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2180 | unsigned int def_conf; |
| 2181 | short assoc, loc; |
| 2182 | |
| 2183 | /* read all default configuration for pin complex */ |
| 2184 | if (wid_type != AC_WID_PIN) |
| 2185 | continue; |
Kailang Yang | df694da | 2005-12-05 19:42:22 +0100 | [diff] [blame] | 2186 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 2187 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 2188 | continue; |
| 2189 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2190 | def_conf = snd_hda_codec_read(codec, nid, 0, |
| 2191 | AC_VERB_GET_CONFIG_DEFAULT, 0); |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2192 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
| 2193 | continue; |
| 2194 | loc = get_defcfg_location(def_conf); |
| 2195 | switch (get_defcfg_device(def_conf)) { |
| 2196 | case AC_JACK_LINE_OUT: |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2197 | seq = get_defcfg_sequence(def_conf); |
| 2198 | assoc = get_defcfg_association(def_conf); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2199 | if (!assoc) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2200 | continue; |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2201 | if (!assoc_line_out) |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2202 | assoc_line_out = assoc; |
| 2203 | else if (assoc_line_out != assoc) |
| 2204 | continue; |
| 2205 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 2206 | continue; |
| 2207 | cfg->line_out_pins[cfg->line_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2208 | sequences_line_out[cfg->line_outs] = seq; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2209 | cfg->line_outs++; |
| 2210 | break; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 2211 | case AC_JACK_SPEAKER: |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2212 | seq = get_defcfg_sequence(def_conf); |
| 2213 | assoc = get_defcfg_association(def_conf); |
| 2214 | if (! assoc) |
| 2215 | continue; |
| 2216 | if (! assoc_speaker) |
| 2217 | assoc_speaker = assoc; |
| 2218 | else if (assoc_speaker != assoc) |
| 2219 | continue; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2220 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 2221 | continue; |
| 2222 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2223 | sequences_speaker[cfg->speaker_outs] = seq; |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2224 | cfg->speaker_outs++; |
Takashi Iwai | 8d88bc3 | 2005-11-17 11:09:23 +0100 | [diff] [blame] | 2225 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2226 | case AC_JACK_HP_OUT: |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2227 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 2228 | continue; |
| 2229 | cfg->hp_pins[cfg->hp_outs] = nid; |
| 2230 | cfg->hp_outs++; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2231 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 2232 | case AC_JACK_MIC_IN: { |
| 2233 | int preferred, alt; |
| 2234 | if (loc == AC_JACK_LOC_FRONT) { |
| 2235 | preferred = AUTO_PIN_FRONT_MIC; |
| 2236 | alt = AUTO_PIN_MIC; |
| 2237 | } else { |
| 2238 | preferred = AUTO_PIN_MIC; |
| 2239 | alt = AUTO_PIN_FRONT_MIC; |
| 2240 | } |
| 2241 | if (!cfg->input_pins[preferred]) |
| 2242 | cfg->input_pins[preferred] = nid; |
| 2243 | else if (!cfg->input_pins[alt]) |
| 2244 | cfg->input_pins[alt] = nid; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2245 | break; |
Takashi Iwai | 314634b | 2006-09-21 11:56:18 +0200 | [diff] [blame] | 2246 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2247 | case AC_JACK_LINE_IN: |
| 2248 | if (loc == AC_JACK_LOC_FRONT) |
| 2249 | cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid; |
| 2250 | else |
| 2251 | cfg->input_pins[AUTO_PIN_LINE] = nid; |
| 2252 | break; |
| 2253 | case AC_JACK_CD: |
| 2254 | cfg->input_pins[AUTO_PIN_CD] = nid; |
| 2255 | break; |
| 2256 | case AC_JACK_AUX: |
| 2257 | cfg->input_pins[AUTO_PIN_AUX] = nid; |
| 2258 | break; |
| 2259 | case AC_JACK_SPDIF_OUT: |
| 2260 | cfg->dig_out_pin = nid; |
| 2261 | break; |
| 2262 | case AC_JACK_SPDIF_IN: |
| 2263 | cfg->dig_in_pin = nid; |
| 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | /* sort by sequence */ |
Steve Longerbeam | 81937d3 | 2007-05-08 15:33:03 +0200 | [diff] [blame] | 2269 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 2270 | cfg->line_outs); |
| 2271 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 2272 | cfg->speaker_outs); |
| 2273 | |
| 2274 | /* |
| 2275 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 2276 | * as a primary output |
| 2277 | */ |
| 2278 | if (!cfg->line_outs) { |
| 2279 | if (cfg->speaker_outs) { |
| 2280 | cfg->line_outs = cfg->speaker_outs; |
| 2281 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 2282 | sizeof(cfg->speaker_pins)); |
| 2283 | cfg->speaker_outs = 0; |
| 2284 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 2285 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 2286 | } else if (cfg->hp_outs) { |
| 2287 | cfg->line_outs = cfg->hp_outs; |
| 2288 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 2289 | sizeof(cfg->hp_pins)); |
| 2290 | cfg->hp_outs = 0; |
| 2291 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 2292 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 2293 | } |
| 2294 | } |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2295 | |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 2296 | /* Reorder the surround channels |
| 2297 | * ALSA sequence is front/surr/clfe/side |
| 2298 | * HDA sequence is: |
| 2299 | * 4-ch: front/surr => OK as it is |
| 2300 | * 6-ch: front/clfe/surr |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 2301 | * 8-ch: front/clfe/rear/side|fc |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 2302 | */ |
| 2303 | switch (cfg->line_outs) { |
| 2304 | case 3: |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 2305 | case 4: |
| 2306 | nid = cfg->line_out_pins[1]; |
Takashi Iwai | 9422db4 | 2007-04-20 16:11:43 +0200 | [diff] [blame] | 2307 | cfg->line_out_pins[1] = cfg->line_out_pins[2]; |
Takashi Iwai | cb8e2f8 | 2005-07-29 11:54:32 +0200 | [diff] [blame] | 2308 | cfg->line_out_pins[2] = nid; |
| 2309 | break; |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2312 | /* |
| 2313 | * debug prints of the parsed results |
| 2314 | */ |
| 2315 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 2316 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 2317 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
| 2318 | cfg->line_out_pins[4]); |
| 2319 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 2320 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 2321 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 2322 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
Takashi Iwai | eb06ed8 | 2006-09-20 17:10:27 +0200 | [diff] [blame] | 2323 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 2324 | cfg->hp_outs, cfg->hp_pins[0], |
| 2325 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 2326 | cfg->hp_pins[3], cfg->hp_pins[4]); |
Takashi Iwai | 82bc955 | 2006-03-21 11:24:42 +0100 | [diff] [blame] | 2327 | snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x," |
| 2328 | " cd=0x%x, aux=0x%x\n", |
| 2329 | cfg->input_pins[AUTO_PIN_MIC], |
| 2330 | cfg->input_pins[AUTO_PIN_FRONT_MIC], |
| 2331 | cfg->input_pins[AUTO_PIN_LINE], |
| 2332 | cfg->input_pins[AUTO_PIN_FRONT_LINE], |
| 2333 | cfg->input_pins[AUTO_PIN_CD], |
| 2334 | cfg->input_pins[AUTO_PIN_AUX]); |
| 2335 | |
Takashi Iwai | e9edcee | 2005-06-13 14:16:38 +0200 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
Takashi Iwai | 4a471b7 | 2005-12-07 13:56:29 +0100 | [diff] [blame] | 2339 | /* labels for input pins */ |
| 2340 | const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = { |
| 2341 | "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux" |
| 2342 | }; |
| 2343 | |
| 2344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | #ifdef CONFIG_PM |
| 2346 | /* |
| 2347 | * power management |
| 2348 | */ |
| 2349 | |
| 2350 | /** |
| 2351 | * snd_hda_suspend - suspend the codecs |
| 2352 | * @bus: the HDA bus |
| 2353 | * @state: suspsend state |
| 2354 | * |
| 2355 | * Returns 0 if successful. |
| 2356 | */ |
| 2357 | int snd_hda_suspend(struct hda_bus *bus, pm_message_t state) |
| 2358 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2359 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | |
| 2361 | /* FIXME: should handle power widget capabilities */ |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2362 | list_for_each_entry(codec, &bus->codec_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | if (codec->patch_ops.suspend) |
| 2364 | codec->patch_ops.suspend(codec, state); |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2365 | hda_set_power_state(codec, |
| 2366 | codec->afg ? codec->afg : codec->mfg, |
| 2367 | AC_PWRST_D3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | } |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | /** |
| 2373 | * snd_hda_resume - resume the codecs |
| 2374 | * @bus: the HDA bus |
| 2375 | * @state: resume state |
| 2376 | * |
| 2377 | * Returns 0 if successful. |
| 2378 | */ |
| 2379 | int snd_hda_resume(struct hda_bus *bus) |
| 2380 | { |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2381 | struct hda_codec *codec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2383 | list_for_each_entry(codec, &bus->codec_list, list) { |
Takashi Iwai | 54d1740 | 2005-11-21 16:33:22 +0100 | [diff] [blame] | 2384 | hda_set_power_state(codec, |
| 2385 | codec->afg ? codec->afg : codec->mfg, |
| 2386 | AC_PWRST_D0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | if (codec->patch_ops.resume) |
| 2388 | codec->patch_ops.resume(codec); |
| 2389 | } |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | /** |
| 2394 | * snd_hda_resume_ctls - resume controls in the new control list |
| 2395 | * @codec: the HDA codec |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2396 | * @knew: the array of struct snd_kcontrol_new |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | * |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2398 | * This function resumes the mixer controls in the struct snd_kcontrol_new array, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | * originally for snd_hda_add_new_ctls(). |
| 2400 | * The array must be terminated with an empty entry as terminator. |
| 2401 | */ |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2402 | int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | { |
Takashi Iwai | c8b6bf9 | 2005-11-17 14:57:47 +0100 | [diff] [blame] | 2404 | struct snd_ctl_elem_value *val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
| 2406 | val = kmalloc(sizeof(*val), GFP_KERNEL); |
Takashi Iwai | 0ba2176 | 2007-04-16 11:29:14 +0200 | [diff] [blame] | 2407 | if (!val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | return -ENOMEM; |
| 2409 | codec->in_resume = 1; |
| 2410 | for (; knew->name; knew++) { |
| 2411 | int i, count; |
| 2412 | count = knew->count ? knew->count : 1; |
| 2413 | for (i = 0; i < count; i++) { |
| 2414 | memset(val, 0, sizeof(*val)); |
| 2415 | val->id.iface = knew->iface; |
| 2416 | val->id.device = knew->device; |
| 2417 | val->id.subdevice = knew->subdevice; |
| 2418 | strcpy(val->id.name, knew->name); |
| 2419 | val->id.index = knew->index ? knew->index : i; |
| 2420 | /* Assume that get callback reads only from cache, |
| 2421 | * not accessing to the real hardware |
| 2422 | */ |
| 2423 | if (snd_ctl_elem_read(codec->bus->card, val) < 0) |
| 2424 | continue; |
| 2425 | snd_ctl_elem_write(codec->bus->card, NULL, val); |
| 2426 | } |
| 2427 | } |
| 2428 | codec->in_resume = 0; |
| 2429 | kfree(val); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * snd_hda_resume_spdif_out - resume the digital out |
| 2435 | * @codec: the HDA codec |
| 2436 | */ |
| 2437 | int snd_hda_resume_spdif_out(struct hda_codec *codec) |
| 2438 | { |
| 2439 | return snd_hda_resume_ctls(codec, dig_mixes); |
| 2440 | } |
| 2441 | |
| 2442 | /** |
| 2443 | * snd_hda_resume_spdif_in - resume the digital in |
| 2444 | * @codec: the HDA codec |
| 2445 | */ |
| 2446 | int snd_hda_resume_spdif_in(struct hda_codec *codec) |
| 2447 | { |
| 2448 | return snd_hda_resume_ctls(codec, dig_in_ctls); |
| 2449 | } |
| 2450 | #endif |