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