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