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 <linux/init.h> 23 #include <linux/delay.h> 24 #include <linux/slab.h> 25 #include <linux/mutex.h> 26 #include <linux/module.h> 27 #include <linux/pm.h> 28 #include <linux/pm_runtime.h> 29 #include <sound/core.h> 30 #include <sound/hda_codec.h> 31 #include <sound/asoundef.h> 32 #include <sound/tlv.h> 33 #include <sound/initval.h> 34 #include <sound/jack.h> 35 #include "hda_local.h" 36 #include "hda_beep.h" 37 #include "hda_jack.h" 38 #include <sound/hda_hwdep.h> 39 #include <sound/hda_component.h> 40 41 #define codec_in_pm(codec) snd_hdac_is_in_pm(&codec->core) 42 #define hda_codec_is_power_on(codec) snd_hdac_is_power_on(&codec->core) 43 #define codec_has_epss(codec) \ 44 ((codec)->core.power_caps & AC_PWRST_EPSS) 45 #define codec_has_clkstop(codec) \ 46 ((codec)->core.power_caps & AC_PWRST_CLKSTOP) 47 48 /* 49 * Send and receive a verb - passed to exec_verb override for hdac_device 50 */ 51 static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd, 52 unsigned int flags, unsigned int *res) 53 { 54 struct hda_codec *codec = container_of(dev, struct hda_codec, core); 55 struct hda_bus *bus = codec->bus; 56 int err; 57 58 if (cmd == ~0) 59 return -1; 60 61 again: 62 snd_hda_power_up_pm(codec); 63 mutex_lock(&bus->core.cmd_mutex); 64 if (flags & HDA_RW_NO_RESPONSE_FALLBACK) 65 bus->no_response_fallback = 1; 66 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr, 67 cmd, res); 68 bus->no_response_fallback = 0; 69 mutex_unlock(&bus->core.cmd_mutex); 70 snd_hda_power_down_pm(codec); 71 if (!codec_in_pm(codec) && res && err == -EAGAIN) { 72 if (bus->response_reset) { 73 codec_dbg(codec, 74 "resetting BUS due to fatal communication error\n"); 75 snd_hda_bus_reset(bus); 76 } 77 goto again; 78 } 79 /* clear reset-flag when the communication gets recovered */ 80 if (!err || codec_in_pm(codec)) 81 bus->response_reset = 0; 82 return err; 83 } 84 85 /** 86 * snd_hda_sequence_write - sequence writes 87 * @codec: the HDA codec 88 * @seq: VERB array to send 89 * 90 * Send the commands sequentially from the given array. 91 * The array must be terminated with NID=0. 92 */ 93 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) 94 { 95 for (; seq->nid; seq++) 96 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); 97 } 98 EXPORT_SYMBOL_GPL(snd_hda_sequence_write); 99 100 /* connection list element */ 101 struct hda_conn_list { 102 struct list_head list; 103 int len; 104 hda_nid_t nid; 105 hda_nid_t conns[0]; 106 }; 107 108 /* look up the cached results */ 109 static struct hda_conn_list * 110 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) 111 { 112 struct hda_conn_list *p; 113 list_for_each_entry(p, &codec->conn_list, list) { 114 if (p->nid == nid) 115 return p; 116 } 117 return NULL; 118 } 119 120 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, 121 const hda_nid_t *list) 122 { 123 struct hda_conn_list *p; 124 125 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); 126 if (!p) 127 return -ENOMEM; 128 p->len = len; 129 p->nid = nid; 130 memcpy(p->conns, list, len * sizeof(hda_nid_t)); 131 list_add(&p->list, &codec->conn_list); 132 return 0; 133 } 134 135 static void remove_conn_list(struct hda_codec *codec) 136 { 137 while (!list_empty(&codec->conn_list)) { 138 struct hda_conn_list *p; 139 p = list_first_entry(&codec->conn_list, typeof(*p), list); 140 list_del(&p->list); 141 kfree(p); 142 } 143 } 144 145 /* read the connection and add to the cache */ 146 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) 147 { 148 hda_nid_t list[32]; 149 hda_nid_t *result = list; 150 int len; 151 152 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); 153 if (len == -ENOSPC) { 154 len = snd_hda_get_num_raw_conns(codec, nid); 155 result = kmalloc_array(len, sizeof(hda_nid_t), GFP_KERNEL); 156 if (!result) 157 return -ENOMEM; 158 len = snd_hda_get_raw_connections(codec, nid, result, len); 159 } 160 if (len >= 0) 161 len = snd_hda_override_conn_list(codec, nid, len, result); 162 if (result != list) 163 kfree(result); 164 return len; 165 } 166 167 /** 168 * snd_hda_get_conn_list - get connection list 169 * @codec: the HDA codec 170 * @nid: NID to parse 171 * @listp: the pointer to store NID list 172 * 173 * Parses the connection list of the given widget and stores the pointer 174 * to the list of NIDs. 175 * 176 * Returns the number of connections, or a negative error code. 177 * 178 * Note that the returned pointer isn't protected against the list 179 * modification. If snd_hda_override_conn_list() might be called 180 * concurrently, protect with a mutex appropriately. 181 */ 182 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, 183 const hda_nid_t **listp) 184 { 185 bool added = false; 186 187 for (;;) { 188 int err; 189 const struct hda_conn_list *p; 190 191 /* if the connection-list is already cached, read it */ 192 p = lookup_conn_list(codec, nid); 193 if (p) { 194 if (listp) 195 *listp = p->conns; 196 return p->len; 197 } 198 if (snd_BUG_ON(added)) 199 return -EINVAL; 200 201 err = read_and_add_raw_conns(codec, nid); 202 if (err < 0) 203 return err; 204 added = true; 205 } 206 } 207 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); 208 209 /** 210 * snd_hda_get_connections - copy connection list 211 * @codec: the HDA codec 212 * @nid: NID to parse 213 * @conn_list: connection list array; when NULL, checks only the size 214 * @max_conns: max. number of connections to store 215 * 216 * Parses the connection list of the given widget and stores the list 217 * of NIDs. 218 * 219 * Returns the number of connections, or a negative error code. 220 */ 221 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, 222 hda_nid_t *conn_list, int max_conns) 223 { 224 const hda_nid_t *list; 225 int len = snd_hda_get_conn_list(codec, nid, &list); 226 227 if (len > 0 && conn_list) { 228 if (len > max_conns) { 229 codec_err(codec, "Too many connections %d for NID 0x%x\n", 230 len, nid); 231 return -EINVAL; 232 } 233 memcpy(conn_list, list, len * sizeof(hda_nid_t)); 234 } 235 236 return len; 237 } 238 EXPORT_SYMBOL_GPL(snd_hda_get_connections); 239 240 /** 241 * snd_hda_override_conn_list - add/modify the connection-list to cache 242 * @codec: the HDA codec 243 * @nid: NID to parse 244 * @len: number of connection list entries 245 * @list: the list of connection entries 246 * 247 * Add or modify the given connection-list to the cache. If the corresponding 248 * cache already exists, invalidate it and append a new one. 249 * 250 * Returns zero or a negative error code. 251 */ 252 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, 253 const hda_nid_t *list) 254 { 255 struct hda_conn_list *p; 256 257 p = lookup_conn_list(codec, nid); 258 if (p) { 259 list_del(&p->list); 260 kfree(p); 261 } 262 263 return add_conn_list(codec, nid, len, list); 264 } 265 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); 266 267 /** 268 * snd_hda_get_conn_index - get the connection index of the given NID 269 * @codec: the HDA codec 270 * @mux: NID containing the list 271 * @nid: NID to select 272 * @recursive: 1 when searching NID recursively, otherwise 0 273 * 274 * Parses the connection list of the widget @mux and checks whether the 275 * widget @nid is present. If it is, return the connection index. 276 * Otherwise it returns -1. 277 */ 278 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, 279 hda_nid_t nid, int recursive) 280 { 281 const hda_nid_t *conn; 282 int i, nums; 283 284 nums = snd_hda_get_conn_list(codec, mux, &conn); 285 for (i = 0; i < nums; i++) 286 if (conn[i] == nid) 287 return i; 288 if (!recursive) 289 return -1; 290 if (recursive > 10) { 291 codec_dbg(codec, "too deep connection for 0x%x\n", nid); 292 return -1; 293 } 294 recursive++; 295 for (i = 0; i < nums; i++) { 296 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); 297 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) 298 continue; 299 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) 300 return i; 301 } 302 return -1; 303 } 304 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); 305 306 /** 307 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget 308 * @codec: the HDA codec 309 * @nid: NID of the pin to parse 310 * 311 * Get the device entry number on the given widget. This is a feature of 312 * DP MST audio. Each pin can have several device entries in it. 313 */ 314 unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid) 315 { 316 unsigned int wcaps = get_wcaps(codec, nid); 317 unsigned int parm; 318 319 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || 320 get_wcaps_type(wcaps) != AC_WID_PIN) 321 return 0; 322 323 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN); 324 if (parm == -1) 325 parm = 0; 326 return parm & AC_DEV_LIST_LEN_MASK; 327 } 328 EXPORT_SYMBOL_GPL(snd_hda_get_num_devices); 329 330 /** 331 * snd_hda_get_devices - copy device list without cache 332 * @codec: the HDA codec 333 * @nid: NID of the pin to parse 334 * @dev_list: device list array 335 * @max_devices: max. number of devices to store 336 * 337 * Copy the device list. This info is dynamic and so not cached. 338 * Currently called only from hda_proc.c, so not exported. 339 */ 340 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, 341 u8 *dev_list, int max_devices) 342 { 343 unsigned int parm; 344 int i, dev_len, devices; 345 346 parm = snd_hda_get_num_devices(codec, nid); 347 if (!parm) /* not multi-stream capable */ 348 return 0; 349 350 dev_len = parm + 1; 351 dev_len = dev_len < max_devices ? dev_len : max_devices; 352 353 devices = 0; 354 while (devices < dev_len) { 355 if (snd_hdac_read(&codec->core, nid, 356 AC_VERB_GET_DEVICE_LIST, devices, &parm)) 357 break; /* error */ 358 359 for (i = 0; i < 8; i++) { 360 dev_list[devices] = (u8)parm; 361 parm >>= 4; 362 devices++; 363 if (devices >= dev_len) 364 break; 365 } 366 } 367 return devices; 368 } 369 370 /** 371 * snd_hda_get_dev_select - get device entry select on the pin 372 * @codec: the HDA codec 373 * @nid: NID of the pin to get device entry select 374 * 375 * Get the devcie entry select on the pin. Return the device entry 376 * id selected on the pin. Return 0 means the first device entry 377 * is selected or MST is not supported. 378 */ 379 int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid) 380 { 381 /* not support dp_mst will always return 0, using first dev_entry */ 382 if (!codec->dp_mst) 383 return 0; 384 385 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0); 386 } 387 EXPORT_SYMBOL_GPL(snd_hda_get_dev_select); 388 389 /** 390 * snd_hda_set_dev_select - set device entry select on the pin 391 * @codec: the HDA codec 392 * @nid: NID of the pin to set device entry select 393 * @dev_id: device entry id to be set 394 * 395 * Set the device entry select on the pin nid. 396 */ 397 int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id) 398 { 399 int ret, num_devices; 400 401 /* not support dp_mst will always return 0, using first dev_entry */ 402 if (!codec->dp_mst) 403 return 0; 404 405 /* AC_PAR_DEVLIST_LEN is 0 based. */ 406 num_devices = snd_hda_get_num_devices(codec, nid) + 1; 407 /* If Device List Length is 0 (num_device = 1), 408 * the pin is not multi stream capable. 409 * Do nothing in this case. 410 */ 411 if (num_devices == 1) 412 return 0; 413 414 /* Behavior of setting index being equal to or greater than 415 * Device List Length is not predictable 416 */ 417 if (num_devices <= dev_id) 418 return -EINVAL; 419 420 ret = snd_hda_codec_write(codec, nid, 0, 421 AC_VERB_SET_DEVICE_SEL, dev_id); 422 423 return ret; 424 } 425 EXPORT_SYMBOL_GPL(snd_hda_set_dev_select); 426 427 /* 428 * read widget caps for each widget and store in cache 429 */ 430 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) 431 { 432 int i; 433 hda_nid_t nid; 434 435 codec->wcaps = kmalloc_array(codec->core.num_nodes, 4, GFP_KERNEL); 436 if (!codec->wcaps) 437 return -ENOMEM; 438 nid = codec->core.start_nid; 439 for (i = 0; i < codec->core.num_nodes; i++, nid++) 440 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core, 441 nid, AC_PAR_AUDIO_WIDGET_CAP); 442 return 0; 443 } 444 445 /* read all pin default configurations and save codec->init_pins */ 446 static int read_pin_defaults(struct hda_codec *codec) 447 { 448 hda_nid_t nid; 449 450 for_each_hda_codec_node(nid, codec) { 451 struct hda_pincfg *pin; 452 unsigned int wcaps = get_wcaps(codec, nid); 453 unsigned int wid_type = get_wcaps_type(wcaps); 454 if (wid_type != AC_WID_PIN) 455 continue; 456 pin = snd_array_new(&codec->init_pins); 457 if (!pin) 458 return -ENOMEM; 459 pin->nid = nid; 460 pin->cfg = snd_hda_codec_read(codec, nid, 0, 461 AC_VERB_GET_CONFIG_DEFAULT, 0); 462 /* 463 * all device entries are the same widget control so far 464 * fixme: if any codec is different, need fix here 465 */ 466 pin->ctrl = snd_hda_codec_read(codec, nid, 0, 467 AC_VERB_GET_PIN_WIDGET_CONTROL, 468 0); 469 } 470 return 0; 471 } 472 473 /* look up the given pin config list and return the item matching with NID */ 474 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, 475 struct snd_array *array, 476 hda_nid_t nid) 477 { 478 struct hda_pincfg *pin; 479 int i; 480 481 snd_array_for_each(array, i, pin) { 482 if (pin->nid == nid) 483 return pin; 484 } 485 return NULL; 486 } 487 488 /* set the current pin config value for the given NID. 489 * the value is cached, and read via snd_hda_codec_get_pincfg() 490 */ 491 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, 492 hda_nid_t nid, unsigned int cfg) 493 { 494 struct hda_pincfg *pin; 495 496 /* the check below may be invalid when pins are added by a fixup 497 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled 498 * for now 499 */ 500 /* 501 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) 502 return -EINVAL; 503 */ 504 505 pin = look_up_pincfg(codec, list, nid); 506 if (!pin) { 507 pin = snd_array_new(list); 508 if (!pin) 509 return -ENOMEM; 510 pin->nid = nid; 511 } 512 pin->cfg = cfg; 513 return 0; 514 } 515 516 /** 517 * snd_hda_codec_set_pincfg - Override a pin default configuration 518 * @codec: the HDA codec 519 * @nid: NID to set the pin config 520 * @cfg: the pin default config value 521 * 522 * Override a pin default configuration value in the cache. 523 * This value can be read by snd_hda_codec_get_pincfg() in a higher 524 * priority than the real hardware value. 525 */ 526 int snd_hda_codec_set_pincfg(struct hda_codec *codec, 527 hda_nid_t nid, unsigned int cfg) 528 { 529 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); 530 } 531 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); 532 533 /** 534 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration 535 * @codec: the HDA codec 536 * @nid: NID to get the pin config 537 * 538 * Get the current pin config value of the given pin NID. 539 * If the pincfg value is cached or overridden via sysfs or driver, 540 * returns the cached value. 541 */ 542 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) 543 { 544 struct hda_pincfg *pin; 545 546 #ifdef CONFIG_SND_HDA_RECONFIG 547 { 548 unsigned int cfg = 0; 549 mutex_lock(&codec->user_mutex); 550 pin = look_up_pincfg(codec, &codec->user_pins, nid); 551 if (pin) 552 cfg = pin->cfg; 553 mutex_unlock(&codec->user_mutex); 554 if (cfg) 555 return cfg; 556 } 557 #endif 558 pin = look_up_pincfg(codec, &codec->driver_pins, nid); 559 if (pin) 560 return pin->cfg; 561 pin = look_up_pincfg(codec, &codec->init_pins, nid); 562 if (pin) 563 return pin->cfg; 564 return 0; 565 } 566 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); 567 568 /** 569 * snd_hda_codec_set_pin_target - remember the current pinctl target value 570 * @codec: the HDA codec 571 * @nid: pin NID 572 * @val: assigned pinctl value 573 * 574 * This function stores the given value to a pinctl target value in the 575 * pincfg table. This isn't always as same as the actually written value 576 * but can be referred at any time via snd_hda_codec_get_pin_target(). 577 */ 578 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, 579 unsigned int val) 580 { 581 struct hda_pincfg *pin; 582 583 pin = look_up_pincfg(codec, &codec->init_pins, nid); 584 if (!pin) 585 return -EINVAL; 586 pin->target = val; 587 return 0; 588 } 589 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); 590 591 /** 592 * snd_hda_codec_get_pin_target - return the current pinctl target value 593 * @codec: the HDA codec 594 * @nid: pin NID 595 */ 596 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) 597 { 598 struct hda_pincfg *pin; 599 600 pin = look_up_pincfg(codec, &codec->init_pins, nid); 601 if (!pin) 602 return 0; 603 return pin->target; 604 } 605 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); 606 607 /** 608 * snd_hda_shutup_pins - Shut up all pins 609 * @codec: the HDA codec 610 * 611 * Clear all pin controls to shup up before suspend for avoiding click noise. 612 * The controls aren't cached so that they can be resumed properly. 613 */ 614 void snd_hda_shutup_pins(struct hda_codec *codec) 615 { 616 const struct hda_pincfg *pin; 617 int i; 618 619 /* don't shut up pins when unloading the driver; otherwise it breaks 620 * the default pin setup at the next load of the driver 621 */ 622 if (codec->bus->shutdown) 623 return; 624 snd_array_for_each(&codec->init_pins, i, pin) { 625 /* use read here for syncing after issuing each verb */ 626 snd_hda_codec_read(codec, pin->nid, 0, 627 AC_VERB_SET_PIN_WIDGET_CONTROL, 0); 628 } 629 codec->pins_shutup = 1; 630 } 631 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); 632 633 #ifdef CONFIG_PM 634 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ 635 static void restore_shutup_pins(struct hda_codec *codec) 636 { 637 const struct hda_pincfg *pin; 638 int i; 639 640 if (!codec->pins_shutup) 641 return; 642 if (codec->bus->shutdown) 643 return; 644 snd_array_for_each(&codec->init_pins, i, pin) { 645 snd_hda_codec_write(codec, pin->nid, 0, 646 AC_VERB_SET_PIN_WIDGET_CONTROL, 647 pin->ctrl); 648 } 649 codec->pins_shutup = 0; 650 } 651 #endif 652 653 static void hda_jackpoll_work(struct work_struct *work) 654 { 655 struct hda_codec *codec = 656 container_of(work, struct hda_codec, jackpoll_work.work); 657 658 snd_hda_jack_set_dirty_all(codec); 659 snd_hda_jack_poll_all(codec); 660 661 if (!codec->jackpoll_interval) 662 return; 663 664 schedule_delayed_work(&codec->jackpoll_work, 665 codec->jackpoll_interval); 666 } 667 668 /* release all pincfg lists */ 669 static void free_init_pincfgs(struct hda_codec *codec) 670 { 671 snd_array_free(&codec->driver_pins); 672 #ifdef CONFIG_SND_HDA_RECONFIG 673 snd_array_free(&codec->user_pins); 674 #endif 675 snd_array_free(&codec->init_pins); 676 } 677 678 /* 679 * audio-converter setup caches 680 */ 681 struct hda_cvt_setup { 682 hda_nid_t nid; 683 u8 stream_tag; 684 u8 channel_id; 685 u16 format_id; 686 unsigned char active; /* cvt is currently used */ 687 unsigned char dirty; /* setups should be cleared */ 688 }; 689 690 /* get or create a cache entry for the given audio converter NID */ 691 static struct hda_cvt_setup * 692 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) 693 { 694 struct hda_cvt_setup *p; 695 int i; 696 697 snd_array_for_each(&codec->cvt_setups, i, p) { 698 if (p->nid == nid) 699 return p; 700 } 701 p = snd_array_new(&codec->cvt_setups); 702 if (p) 703 p->nid = nid; 704 return p; 705 } 706 707 /* 708 * PCM device 709 */ 710 static void release_pcm(struct kref *kref) 711 { 712 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref); 713 714 if (pcm->pcm) 715 snd_device_free(pcm->codec->card, pcm->pcm); 716 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits); 717 kfree(pcm->name); 718 kfree(pcm); 719 } 720 721 void snd_hda_codec_pcm_put(struct hda_pcm *pcm) 722 { 723 kref_put(&pcm->kref, release_pcm); 724 } 725 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put); 726 727 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec, 728 const char *fmt, ...) 729 { 730 struct hda_pcm *pcm; 731 va_list args; 732 733 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); 734 if (!pcm) 735 return NULL; 736 737 pcm->codec = codec; 738 kref_init(&pcm->kref); 739 va_start(args, fmt); 740 pcm->name = kvasprintf(GFP_KERNEL, fmt, args); 741 va_end(args); 742 if (!pcm->name) { 743 kfree(pcm); 744 return NULL; 745 } 746 747 list_add_tail(&pcm->list, &codec->pcm_list_head); 748 return pcm; 749 } 750 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new); 751 752 /* 753 * codec destructor 754 */ 755 static void codec_release_pcms(struct hda_codec *codec) 756 { 757 struct hda_pcm *pcm, *n; 758 759 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) { 760 list_del_init(&pcm->list); 761 if (pcm->pcm) 762 snd_device_disconnect(codec->card, pcm->pcm); 763 snd_hda_codec_pcm_put(pcm); 764 } 765 } 766 767 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec) 768 { 769 if (codec->registered) { 770 /* pm_runtime_put() is called in snd_hdac_device_exit() */ 771 pm_runtime_get_noresume(hda_codec_dev(codec)); 772 pm_runtime_disable(hda_codec_dev(codec)); 773 codec->registered = 0; 774 } 775 776 cancel_delayed_work_sync(&codec->jackpoll_work); 777 if (!codec->in_freeing) 778 snd_hda_ctls_clear(codec); 779 codec_release_pcms(codec); 780 snd_hda_detach_beep_device(codec); 781 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); 782 snd_hda_jack_tbl_clear(codec); 783 codec->proc_widget_hook = NULL; 784 codec->spec = NULL; 785 786 /* free only driver_pins so that init_pins + user_pins are restored */ 787 snd_array_free(&codec->driver_pins); 788 snd_array_free(&codec->cvt_setups); 789 snd_array_free(&codec->spdif_out); 790 snd_array_free(&codec->verbs); 791 codec->preset = NULL; 792 codec->slave_dig_outs = NULL; 793 codec->spdif_status_reset = 0; 794 snd_array_free(&codec->mixers); 795 snd_array_free(&codec->nids); 796 remove_conn_list(codec); 797 snd_hdac_regmap_exit(&codec->core); 798 } 799 800 static unsigned int hda_set_power_state(struct hda_codec *codec, 801 unsigned int power_state); 802 803 /* enable/disable display power per codec */ 804 static void codec_display_power(struct hda_codec *codec, bool enable) 805 { 806 if (codec->display_power_control) 807 snd_hdac_display_power(&codec->bus->core, codec->addr, enable); 808 } 809 810 /* also called from hda_bind.c */ 811 void snd_hda_codec_register(struct hda_codec *codec) 812 { 813 if (codec->registered) 814 return; 815 if (device_is_registered(hda_codec_dev(codec))) { 816 codec_display_power(codec, true); 817 pm_runtime_enable(hda_codec_dev(codec)); 818 /* it was powered up in snd_hda_codec_new(), now all done */ 819 snd_hda_power_down(codec); 820 codec->registered = 1; 821 } 822 } 823 824 static int snd_hda_codec_dev_register(struct snd_device *device) 825 { 826 snd_hda_codec_register(device->device_data); 827 return 0; 828 } 829 830 static int snd_hda_codec_dev_free(struct snd_device *device) 831 { 832 struct hda_codec *codec = device->device_data; 833 834 codec->in_freeing = 1; 835 snd_hdac_device_unregister(&codec->core); 836 codec_display_power(codec, false); 837 put_device(hda_codec_dev(codec)); 838 return 0; 839 } 840 841 static void snd_hda_codec_dev_release(struct device *dev) 842 { 843 struct hda_codec *codec = dev_to_hda_codec(dev); 844 845 free_init_pincfgs(codec); 846 snd_hdac_device_exit(&codec->core); 847 snd_hda_sysfs_clear(codec); 848 kfree(codec->modelname); 849 kfree(codec->wcaps); 850 kfree(codec); 851 } 852 853 #define DEV_NAME_LEN 31 854 855 static int snd_hda_codec_device_init(struct hda_bus *bus, struct snd_card *card, 856 unsigned int codec_addr, struct hda_codec **codecp) 857 { 858 char name[DEV_NAME_LEN]; 859 struct hda_codec *codec; 860 int err; 861 862 dev_dbg(card->dev, "%s: entry\n", __func__); 863 864 if (snd_BUG_ON(!bus)) 865 return -EINVAL; 866 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) 867 return -EINVAL; 868 869 codec = kzalloc(sizeof(*codec), GFP_KERNEL); 870 if (!codec) 871 return -ENOMEM; 872 873 sprintf(name, "hdaudioC%dD%d", card->number, codec_addr); 874 err = snd_hdac_device_init(&codec->core, &bus->core, name, codec_addr); 875 if (err < 0) { 876 kfree(codec); 877 return err; 878 } 879 880 codec->core.type = HDA_DEV_LEGACY; 881 *codecp = codec; 882 883 return err; 884 } 885 886 /** 887 * snd_hda_codec_new - create a HDA codec 888 * @bus: the bus to assign 889 * @codec_addr: the codec address 890 * @codecp: the pointer to store the generated codec 891 * 892 * Returns 0 if successful, or a negative error code. 893 */ 894 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card, 895 unsigned int codec_addr, struct hda_codec **codecp) 896 { 897 int ret; 898 899 ret = snd_hda_codec_device_init(bus, card, codec_addr, codecp); 900 if (ret < 0) 901 return ret; 902 903 return snd_hda_codec_device_new(bus, card, codec_addr, *codecp); 904 } 905 EXPORT_SYMBOL_GPL(snd_hda_codec_new); 906 907 int snd_hda_codec_device_new(struct hda_bus *bus, struct snd_card *card, 908 unsigned int codec_addr, struct hda_codec *codec) 909 { 910 char component[31]; 911 hda_nid_t fg; 912 int err; 913 static struct snd_device_ops dev_ops = { 914 .dev_register = snd_hda_codec_dev_register, 915 .dev_free = snd_hda_codec_dev_free, 916 }; 917 918 dev_dbg(card->dev, "%s: entry\n", __func__); 919 920 if (snd_BUG_ON(!bus)) 921 return -EINVAL; 922 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) 923 return -EINVAL; 924 925 codec->core.dev.release = snd_hda_codec_dev_release; 926 codec->core.exec_verb = codec_exec_verb; 927 928 codec->bus = bus; 929 codec->card = card; 930 codec->addr = codec_addr; 931 mutex_init(&codec->spdif_mutex); 932 mutex_init(&codec->control_mutex); 933 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); 934 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); 935 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); 936 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); 937 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); 938 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); 939 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); 940 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); 941 INIT_LIST_HEAD(&codec->conn_list); 942 INIT_LIST_HEAD(&codec->pcm_list_head); 943 944 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); 945 codec->depop_delay = -1; 946 codec->fixup_id = HDA_FIXUP_ID_NOT_SET; 947 948 #ifdef CONFIG_PM 949 codec->power_jiffies = jiffies; 950 #endif 951 952 snd_hda_sysfs_init(codec); 953 954 if (codec->bus->modelname) { 955 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); 956 if (!codec->modelname) { 957 err = -ENOMEM; 958 goto error; 959 } 960 } 961 962 fg = codec->core.afg ? codec->core.afg : codec->core.mfg; 963 err = read_widget_caps(codec, fg); 964 if (err < 0) 965 goto error; 966 err = read_pin_defaults(codec); 967 if (err < 0) 968 goto error; 969 970 /* power-up all before initialization */ 971 hda_set_power_state(codec, AC_PWRST_D0); 972 codec->core.dev.power.power_state = PMSG_ON; 973 974 snd_hda_codec_proc_new(codec); 975 976 snd_hda_create_hwdep(codec); 977 978 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id, 979 codec->core.subsystem_id, codec->core.revision_id); 980 snd_component_add(card, component); 981 982 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops); 983 if (err < 0) 984 goto error; 985 986 return 0; 987 988 error: 989 put_device(hda_codec_dev(codec)); 990 return err; 991 } 992 EXPORT_SYMBOL_GPL(snd_hda_codec_device_new); 993 994 /** 995 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults 996 * @codec: the HDA codec 997 * 998 * Forcibly refresh the all widget caps and the init pin configurations of 999 * the given codec. 1000 */ 1001 int snd_hda_codec_update_widgets(struct hda_codec *codec) 1002 { 1003 hda_nid_t fg; 1004 int err; 1005 1006 err = snd_hdac_refresh_widgets(&codec->core, true); 1007 if (err < 0) 1008 return err; 1009 1010 /* Assume the function group node does not change, 1011 * only the widget nodes may change. 1012 */ 1013 kfree(codec->wcaps); 1014 fg = codec->core.afg ? codec->core.afg : codec->core.mfg; 1015 err = read_widget_caps(codec, fg); 1016 if (err < 0) 1017 return err; 1018 1019 snd_array_free(&codec->init_pins); 1020 err = read_pin_defaults(codec); 1021 1022 return err; 1023 } 1024 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); 1025 1026 /* update the stream-id if changed */ 1027 static void update_pcm_stream_id(struct hda_codec *codec, 1028 struct hda_cvt_setup *p, hda_nid_t nid, 1029 u32 stream_tag, int channel_id) 1030 { 1031 unsigned int oldval, newval; 1032 1033 if (p->stream_tag != stream_tag || p->channel_id != channel_id) { 1034 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); 1035 newval = (stream_tag << 4) | channel_id; 1036 if (oldval != newval) 1037 snd_hda_codec_write(codec, nid, 0, 1038 AC_VERB_SET_CHANNEL_STREAMID, 1039 newval); 1040 p->stream_tag = stream_tag; 1041 p->channel_id = channel_id; 1042 } 1043 } 1044 1045 /* update the format-id if changed */ 1046 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, 1047 hda_nid_t nid, int format) 1048 { 1049 unsigned int oldval; 1050 1051 if (p->format_id != format) { 1052 oldval = snd_hda_codec_read(codec, nid, 0, 1053 AC_VERB_GET_STREAM_FORMAT, 0); 1054 if (oldval != format) { 1055 msleep(1); 1056 snd_hda_codec_write(codec, nid, 0, 1057 AC_VERB_SET_STREAM_FORMAT, 1058 format); 1059 } 1060 p->format_id = format; 1061 } 1062 } 1063 1064 /** 1065 * snd_hda_codec_setup_stream - set up the codec for streaming 1066 * @codec: the CODEC to set up 1067 * @nid: the NID to set up 1068 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. 1069 * @channel_id: channel id to pass, zero based. 1070 * @format: stream format. 1071 */ 1072 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, 1073 u32 stream_tag, 1074 int channel_id, int format) 1075 { 1076 struct hda_codec *c; 1077 struct hda_cvt_setup *p; 1078 int type; 1079 int i; 1080 1081 if (!nid) 1082 return; 1083 1084 codec_dbg(codec, 1085 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", 1086 nid, stream_tag, channel_id, format); 1087 p = get_hda_cvt_setup(codec, nid); 1088 if (!p) 1089 return; 1090 1091 if (codec->patch_ops.stream_pm) 1092 codec->patch_ops.stream_pm(codec, nid, true); 1093 if (codec->pcm_format_first) 1094 update_pcm_format(codec, p, nid, format); 1095 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); 1096 if (!codec->pcm_format_first) 1097 update_pcm_format(codec, p, nid, format); 1098 1099 p->active = 1; 1100 p->dirty = 0; 1101 1102 /* make other inactive cvts with the same stream-tag dirty */ 1103 type = get_wcaps_type(get_wcaps(codec, nid)); 1104 list_for_each_codec(c, codec->bus) { 1105 snd_array_for_each(&c->cvt_setups, i, p) { 1106 if (!p->active && p->stream_tag == stream_tag && 1107 get_wcaps_type(get_wcaps(c, p->nid)) == type) 1108 p->dirty = 1; 1109 } 1110 } 1111 } 1112 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); 1113 1114 static void really_cleanup_stream(struct hda_codec *codec, 1115 struct hda_cvt_setup *q); 1116 1117 /** 1118 * __snd_hda_codec_cleanup_stream - clean up the codec for closing 1119 * @codec: the CODEC to clean up 1120 * @nid: the NID to clean up 1121 * @do_now: really clean up the stream instead of clearing the active flag 1122 */ 1123 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, 1124 int do_now) 1125 { 1126 struct hda_cvt_setup *p; 1127 1128 if (!nid) 1129 return; 1130 1131 if (codec->no_sticky_stream) 1132 do_now = 1; 1133 1134 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); 1135 p = get_hda_cvt_setup(codec, nid); 1136 if (p) { 1137 /* here we just clear the active flag when do_now isn't set; 1138 * actual clean-ups will be done later in 1139 * purify_inactive_streams() called from snd_hda_codec_prpapre() 1140 */ 1141 if (do_now) 1142 really_cleanup_stream(codec, p); 1143 else 1144 p->active = 0; 1145 } 1146 } 1147 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); 1148 1149 static void really_cleanup_stream(struct hda_codec *codec, 1150 struct hda_cvt_setup *q) 1151 { 1152 hda_nid_t nid = q->nid; 1153 if (q->stream_tag || q->channel_id) 1154 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); 1155 if (q->format_id) 1156 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 1157 ); 1158 memset(q, 0, sizeof(*q)); 1159 q->nid = nid; 1160 if (codec->patch_ops.stream_pm) 1161 codec->patch_ops.stream_pm(codec, nid, false); 1162 } 1163 1164 /* clean up the all conflicting obsolete streams */ 1165 static void purify_inactive_streams(struct hda_codec *codec) 1166 { 1167 struct hda_codec *c; 1168 struct hda_cvt_setup *p; 1169 int i; 1170 1171 list_for_each_codec(c, codec->bus) { 1172 snd_array_for_each(&c->cvt_setups, i, p) { 1173 if (p->dirty) 1174 really_cleanup_stream(c, p); 1175 } 1176 } 1177 } 1178 1179 #ifdef CONFIG_PM 1180 /* clean up all streams; called from suspend */ 1181 static void hda_cleanup_all_streams(struct hda_codec *codec) 1182 { 1183 struct hda_cvt_setup *p; 1184 int i; 1185 1186 snd_array_for_each(&codec->cvt_setups, i, p) { 1187 if (p->stream_tag) 1188 really_cleanup_stream(codec, p); 1189 } 1190 } 1191 #endif 1192 1193 /* 1194 * amp access functions 1195 */ 1196 1197 /** 1198 * query_amp_caps - query AMP capabilities 1199 * @codec: the HD-auio codec 1200 * @nid: the NID to query 1201 * @direction: either #HDA_INPUT or #HDA_OUTPUT 1202 * 1203 * Query AMP capabilities for the given widget and direction. 1204 * Returns the obtained capability bits. 1205 * 1206 * When cap bits have been already read, this doesn't read again but 1207 * returns the cached value. 1208 */ 1209 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) 1210 { 1211 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) 1212 nid = codec->core.afg; 1213 return snd_hda_param_read(codec, nid, 1214 direction == HDA_OUTPUT ? 1215 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); 1216 } 1217 EXPORT_SYMBOL_GPL(query_amp_caps); 1218 1219 /** 1220 * snd_hda_check_amp_caps - query AMP capabilities 1221 * @codec: the HD-audio codec 1222 * @nid: the NID to query 1223 * @dir: either #HDA_INPUT or #HDA_OUTPUT 1224 * @bits: bit mask to check the result 1225 * 1226 * Check whether the widget has the given amp capability for the direction. 1227 */ 1228 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, 1229 int dir, unsigned int bits) 1230 { 1231 if (!nid) 1232 return false; 1233 if (get_wcaps(codec, nid) & (1 << (dir + 1))) 1234 if (query_amp_caps(codec, nid, dir) & bits) 1235 return true; 1236 return false; 1237 } 1238 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); 1239 1240 /** 1241 * snd_hda_override_amp_caps - Override the AMP capabilities 1242 * @codec: the CODEC to clean up 1243 * @nid: the NID to clean up 1244 * @dir: either #HDA_INPUT or #HDA_OUTPUT 1245 * @caps: the capability bits to set 1246 * 1247 * Override the cached AMP caps bits value by the given one. 1248 * This function is useful if the driver needs to adjust the AMP ranges, 1249 * e.g. limit to 0dB, etc. 1250 * 1251 * Returns zero if successful or a negative error code. 1252 */ 1253 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, 1254 unsigned int caps) 1255 { 1256 unsigned int parm; 1257 1258 snd_hda_override_wcaps(codec, nid, 1259 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD); 1260 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP; 1261 return snd_hdac_override_parm(&codec->core, nid, parm, caps); 1262 } 1263 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); 1264 1265 /** 1266 * snd_hda_codec_amp_update - update the AMP mono value 1267 * @codec: HD-audio codec 1268 * @nid: NID to read the AMP value 1269 * @ch: channel to update (0 or 1) 1270 * @dir: #HDA_INPUT or #HDA_OUTPUT 1271 * @idx: the index value (only for input direction) 1272 * @mask: bit mask to set 1273 * @val: the bits value to set 1274 * 1275 * Update the AMP values for the given channel, direction and index. 1276 */ 1277 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, 1278 int ch, int dir, int idx, int mask, int val) 1279 { 1280 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx); 1281 1282 /* enable fake mute if no h/w mute but min=mute */ 1283 if ((query_amp_caps(codec, nid, dir) & 1284 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE) 1285 cmd |= AC_AMP_FAKE_MUTE; 1286 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val); 1287 } 1288 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); 1289 1290 /** 1291 * snd_hda_codec_amp_stereo - update the AMP stereo values 1292 * @codec: HD-audio codec 1293 * @nid: NID to read the AMP value 1294 * @direction: #HDA_INPUT or #HDA_OUTPUT 1295 * @idx: the index value (only for input direction) 1296 * @mask: bit mask to set 1297 * @val: the bits value to set 1298 * 1299 * Update the AMP values like snd_hda_codec_amp_update(), but for a 1300 * stereo widget with the same mask and value. 1301 */ 1302 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, 1303 int direction, int idx, int mask, int val) 1304 { 1305 int ch, ret = 0; 1306 1307 if (snd_BUG_ON(mask & ~0xff)) 1308 mask &= 0xff; 1309 for (ch = 0; ch < 2; ch++) 1310 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, 1311 idx, mask, val); 1312 return ret; 1313 } 1314 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); 1315 1316 /** 1317 * snd_hda_codec_amp_init - initialize the AMP value 1318 * @codec: the HDA codec 1319 * @nid: NID to read the AMP value 1320 * @ch: channel (left=0 or right=1) 1321 * @dir: #HDA_INPUT or #HDA_OUTPUT 1322 * @idx: the index value (only for input direction) 1323 * @mask: bit mask to set 1324 * @val: the bits value to set 1325 * 1326 * Works like snd_hda_codec_amp_update() but it writes the value only at 1327 * the first access. If the amp was already initialized / updated beforehand, 1328 * this does nothing. 1329 */ 1330 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, 1331 int dir, int idx, int mask, int val) 1332 { 1333 int orig; 1334 1335 if (!codec->core.regmap) 1336 return -EINVAL; 1337 regcache_cache_only(codec->core.regmap, true); 1338 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); 1339 regcache_cache_only(codec->core.regmap, false); 1340 if (orig >= 0) 1341 return 0; 1342 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val); 1343 } 1344 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); 1345 1346 /** 1347 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value 1348 * @codec: the HDA codec 1349 * @nid: NID to read the AMP value 1350 * @dir: #HDA_INPUT or #HDA_OUTPUT 1351 * @idx: the index value (only for input direction) 1352 * @mask: bit mask to set 1353 * @val: the bits value to set 1354 * 1355 * Call snd_hda_codec_amp_init() for both stereo channels. 1356 */ 1357 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, 1358 int dir, int idx, int mask, int val) 1359 { 1360 int ch, ret = 0; 1361 1362 if (snd_BUG_ON(mask & ~0xff)) 1363 mask &= 0xff; 1364 for (ch = 0; ch < 2; ch++) 1365 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, 1366 idx, mask, val); 1367 return ret; 1368 } 1369 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); 1370 1371 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, 1372 unsigned int ofs) 1373 { 1374 u32 caps = query_amp_caps(codec, nid, dir); 1375 /* get num steps */ 1376 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 1377 if (ofs < caps) 1378 caps -= ofs; 1379 return caps; 1380 } 1381 1382 /** 1383 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer 1384 * @kcontrol: referred ctl element 1385 * @uinfo: pointer to get/store the data 1386 * 1387 * The control element is supposed to have the private_value field 1388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 1389 */ 1390 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, 1391 struct snd_ctl_elem_info *uinfo) 1392 { 1393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1394 u16 nid = get_amp_nid(kcontrol); 1395 u8 chs = get_amp_channels(kcontrol); 1396 int dir = get_amp_direction(kcontrol); 1397 unsigned int ofs = get_amp_offset(kcontrol); 1398 1399 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1400 uinfo->count = chs == 3 ? 2 : 1; 1401 uinfo->value.integer.min = 0; 1402 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); 1403 if (!uinfo->value.integer.max) { 1404 codec_warn(codec, 1405 "num_steps = 0 for NID=0x%x (ctl = %s)\n", 1406 nid, kcontrol->id.name); 1407 return -EINVAL; 1408 } 1409 return 0; 1410 } 1411 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); 1412 1413 1414 static inline unsigned int 1415 read_amp_value(struct hda_codec *codec, hda_nid_t nid, 1416 int ch, int dir, int idx, unsigned int ofs) 1417 { 1418 unsigned int val; 1419 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); 1420 val &= HDA_AMP_VOLMASK; 1421 if (val >= ofs) 1422 val -= ofs; 1423 else 1424 val = 0; 1425 return val; 1426 } 1427 1428 static inline int 1429 update_amp_value(struct hda_codec *codec, hda_nid_t nid, 1430 int ch, int dir, int idx, unsigned int ofs, 1431 unsigned int val) 1432 { 1433 unsigned int maxval; 1434 1435 if (val > 0) 1436 val += ofs; 1437 /* ofs = 0: raw max value */ 1438 maxval = get_amp_max_value(codec, nid, dir, 0); 1439 if (val > maxval) 1440 val = maxval; 1441 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, 1442 HDA_AMP_VOLMASK, val); 1443 } 1444 1445 /** 1446 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume 1447 * @kcontrol: ctl element 1448 * @ucontrol: pointer to get/store the data 1449 * 1450 * The control element is supposed to have the private_value field 1451 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 1452 */ 1453 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, 1454 struct snd_ctl_elem_value *ucontrol) 1455 { 1456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1457 hda_nid_t nid = get_amp_nid(kcontrol); 1458 int chs = get_amp_channels(kcontrol); 1459 int dir = get_amp_direction(kcontrol); 1460 int idx = get_amp_index(kcontrol); 1461 unsigned int ofs = get_amp_offset(kcontrol); 1462 long *valp = ucontrol->value.integer.value; 1463 1464 if (chs & 1) 1465 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); 1466 if (chs & 2) 1467 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); 1468 return 0; 1469 } 1470 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); 1471 1472 /** 1473 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume 1474 * @kcontrol: ctl element 1475 * @ucontrol: pointer to get/store the data 1476 * 1477 * The control element is supposed to have the private_value field 1478 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 1479 */ 1480 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, 1481 struct snd_ctl_elem_value *ucontrol) 1482 { 1483 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1484 hda_nid_t nid = get_amp_nid(kcontrol); 1485 int chs = get_amp_channels(kcontrol); 1486 int dir = get_amp_direction(kcontrol); 1487 int idx = get_amp_index(kcontrol); 1488 unsigned int ofs = get_amp_offset(kcontrol); 1489 long *valp = ucontrol->value.integer.value; 1490 int change = 0; 1491 1492 if (chs & 1) { 1493 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); 1494 valp++; 1495 } 1496 if (chs & 2) 1497 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); 1498 return change; 1499 } 1500 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); 1501 1502 /* inquiry the amp caps and convert to TLV */ 1503 static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv) 1504 { 1505 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 1506 hda_nid_t nid = get_amp_nid(kcontrol); 1507 int dir = get_amp_direction(kcontrol); 1508 unsigned int ofs = get_amp_offset(kcontrol); 1509 bool min_mute = get_amp_min_mute(kcontrol); 1510 u32 caps, val1, val2; 1511 1512 caps = query_amp_caps(codec, nid, dir); 1513 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; 1514 val2 = (val2 + 1) * 25; 1515 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); 1516 val1 += ofs; 1517 val1 = ((int)val1) * ((int)val2); 1518 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) 1519 val2 |= TLV_DB_SCALE_MUTE; 1520 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE; 1521 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int); 1522 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = val1; 1523 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = val2; 1524 } 1525 1526 /** 1527 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume 1528 * @kcontrol: ctl element 1529 * @op_flag: operation flag 1530 * @size: byte size of input TLV 1531 * @_tlv: TLV data 1532 * 1533 * The control element is supposed to have the private_value field 1534 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 1535 */ 1536 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, 1537 unsigned int size, unsigned int __user *_tlv) 1538 { 1539 unsigned int tlv[4]; 1540 1541 if (size < 4 * sizeof(unsigned int)) 1542 return -ENOMEM; 1543 get_ctl_amp_tlv(kcontrol, tlv); 1544 if (copy_to_user(_tlv, tlv, sizeof(tlv))) 1545 return -EFAULT; 1546 return 0; 1547 } 1548 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); 1549 1550 /** 1551 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control 1552 * @codec: HD-audio codec 1553 * @nid: NID of a reference widget 1554 * @dir: #HDA_INPUT or #HDA_OUTPUT 1555 * @tlv: TLV data to be stored, at least 4 elements 1556 * 1557 * Set (static) TLV data for a virtual master volume using the AMP caps 1558 * obtained from the reference NID. 1559 * The volume range is recalculated as if the max volume is 0dB. 1560 */ 1561 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, 1562 unsigned int *tlv) 1563 { 1564 u32 caps; 1565 int nums, step; 1566 1567 caps = query_amp_caps(codec, nid, dir); 1568 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 1569 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; 1570 step = (step + 1) * 25; 1571 tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE; 1572 tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int); 1573 tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = -nums * step; 1574 tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = step; 1575 } 1576 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); 1577 1578 /* find a mixer control element with the given name */ 1579 static struct snd_kcontrol * 1580 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) 1581 { 1582 struct snd_ctl_elem_id id; 1583 memset(&id, 0, sizeof(id)); 1584 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1585 id.device = dev; 1586 id.index = idx; 1587 if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) 1588 return NULL; 1589 strcpy(id.name, name); 1590 return snd_ctl_find_id(codec->card, &id); 1591 } 1592 1593 /** 1594 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name 1595 * @codec: HD-audio codec 1596 * @name: ctl id name string 1597 * 1598 * Get the control element with the given id string and IFACE_MIXER. 1599 */ 1600 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, 1601 const char *name) 1602 { 1603 return find_mixer_ctl(codec, name, 0, 0); 1604 } 1605 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); 1606 1607 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, 1608 int start_idx) 1609 { 1610 int i, idx; 1611 /* 16 ctlrs should be large enough */ 1612 for (i = 0, idx = start_idx; i < 16; i++, idx++) { 1613 if (!find_mixer_ctl(codec, name, 0, idx)) 1614 return idx; 1615 } 1616 return -EBUSY; 1617 } 1618 1619 /** 1620 * snd_hda_ctl_add - Add a control element and assign to the codec 1621 * @codec: HD-audio codec 1622 * @nid: corresponding NID (optional) 1623 * @kctl: the control element to assign 1624 * 1625 * Add the given control element to an array inside the codec instance. 1626 * All control elements belonging to a codec are supposed to be added 1627 * by this function so that a proper clean-up works at the free or 1628 * reconfiguration time. 1629 * 1630 * If non-zero @nid is passed, the NID is assigned to the control element. 1631 * The assignment is shown in the codec proc file. 1632 * 1633 * snd_hda_ctl_add() checks the control subdev id field whether 1634 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower 1635 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit 1636 * specifies if kctl->private_value is a HDA amplifier value. 1637 */ 1638 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, 1639 struct snd_kcontrol *kctl) 1640 { 1641 int err; 1642 unsigned short flags = 0; 1643 struct hda_nid_item *item; 1644 1645 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { 1646 flags |= HDA_NID_ITEM_AMP; 1647 if (nid == 0) 1648 nid = get_amp_nid_(kctl->private_value); 1649 } 1650 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) 1651 nid = kctl->id.subdevice & 0xffff; 1652 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) 1653 kctl->id.subdevice = 0; 1654 err = snd_ctl_add(codec->card, kctl); 1655 if (err < 0) 1656 return err; 1657 item = snd_array_new(&codec->mixers); 1658 if (!item) 1659 return -ENOMEM; 1660 item->kctl = kctl; 1661 item->nid = nid; 1662 item->flags = flags; 1663 return 0; 1664 } 1665 EXPORT_SYMBOL_GPL(snd_hda_ctl_add); 1666 1667 /** 1668 * snd_hda_add_nid - Assign a NID to a control element 1669 * @codec: HD-audio codec 1670 * @nid: corresponding NID (optional) 1671 * @kctl: the control element to assign 1672 * @index: index to kctl 1673 * 1674 * Add the given control element to an array inside the codec instance. 1675 * This function is used when #snd_hda_ctl_add cannot be used for 1:1 1676 * NID:KCTL mapping - for example "Capture Source" selector. 1677 */ 1678 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, 1679 unsigned int index, hda_nid_t nid) 1680 { 1681 struct hda_nid_item *item; 1682 1683 if (nid > 0) { 1684 item = snd_array_new(&codec->nids); 1685 if (!item) 1686 return -ENOMEM; 1687 item->kctl = kctl; 1688 item->index = index; 1689 item->nid = nid; 1690 return 0; 1691 } 1692 codec_err(codec, "no NID for mapping control %s:%d:%d\n", 1693 kctl->id.name, kctl->id.index, index); 1694 return -EINVAL; 1695 } 1696 EXPORT_SYMBOL_GPL(snd_hda_add_nid); 1697 1698 /** 1699 * snd_hda_ctls_clear - Clear all controls assigned to the given codec 1700 * @codec: HD-audio codec 1701 */ 1702 void snd_hda_ctls_clear(struct hda_codec *codec) 1703 { 1704 int i; 1705 struct hda_nid_item *items = codec->mixers.list; 1706 for (i = 0; i < codec->mixers.used; i++) 1707 snd_ctl_remove(codec->card, items[i].kctl); 1708 snd_array_free(&codec->mixers); 1709 snd_array_free(&codec->nids); 1710 } 1711 1712 /** 1713 * snd_hda_lock_devices - pseudo device locking 1714 * @bus: the BUS 1715 * 1716 * toggle card->shutdown to allow/disallow the device access (as a hack) 1717 */ 1718 int snd_hda_lock_devices(struct hda_bus *bus) 1719 { 1720 struct snd_card *card = bus->card; 1721 struct hda_codec *codec; 1722 1723 spin_lock(&card->files_lock); 1724 if (card->shutdown) 1725 goto err_unlock; 1726 card->shutdown = 1; 1727 if (!list_empty(&card->ctl_files)) 1728 goto err_clear; 1729 1730 list_for_each_codec(codec, bus) { 1731 struct hda_pcm *cpcm; 1732 list_for_each_entry(cpcm, &codec->pcm_list_head, list) { 1733 if (!cpcm->pcm) 1734 continue; 1735 if (cpcm->pcm->streams[0].substream_opened || 1736 cpcm->pcm->streams[1].substream_opened) 1737 goto err_clear; 1738 } 1739 } 1740 spin_unlock(&card->files_lock); 1741 return 0; 1742 1743 err_clear: 1744 card->shutdown = 0; 1745 err_unlock: 1746 spin_unlock(&card->files_lock); 1747 return -EINVAL; 1748 } 1749 EXPORT_SYMBOL_GPL(snd_hda_lock_devices); 1750 1751 /** 1752 * snd_hda_unlock_devices - pseudo device unlocking 1753 * @bus: the BUS 1754 */ 1755 void snd_hda_unlock_devices(struct hda_bus *bus) 1756 { 1757 struct snd_card *card = bus->card; 1758 1759 spin_lock(&card->files_lock); 1760 card->shutdown = 0; 1761 spin_unlock(&card->files_lock); 1762 } 1763 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); 1764 1765 /** 1766 * snd_hda_codec_reset - Clear all objects assigned to the codec 1767 * @codec: HD-audio codec 1768 * 1769 * This frees the all PCM and control elements assigned to the codec, and 1770 * clears the caches and restores the pin default configurations. 1771 * 1772 * When a device is being used, it returns -EBSY. If successfully freed, 1773 * returns zero. 1774 */ 1775 int snd_hda_codec_reset(struct hda_codec *codec) 1776 { 1777 struct hda_bus *bus = codec->bus; 1778 1779 if (snd_hda_lock_devices(bus) < 0) 1780 return -EBUSY; 1781 1782 /* OK, let it free */ 1783 snd_hdac_device_unregister(&codec->core); 1784 1785 /* allow device access again */ 1786 snd_hda_unlock_devices(bus); 1787 return 0; 1788 } 1789 1790 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); 1791 1792 /* apply the function to all matching slave ctls in the mixer list */ 1793 static int map_slaves(struct hda_codec *codec, const char * const *slaves, 1794 const char *suffix, map_slave_func_t func, void *data) 1795 { 1796 struct hda_nid_item *items; 1797 const char * const *s; 1798 int i, err; 1799 1800 items = codec->mixers.list; 1801 for (i = 0; i < codec->mixers.used; i++) { 1802 struct snd_kcontrol *sctl = items[i].kctl; 1803 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) 1804 continue; 1805 for (s = slaves; *s; s++) { 1806 char tmpname[sizeof(sctl->id.name)]; 1807 const char *name = *s; 1808 if (suffix) { 1809 snprintf(tmpname, sizeof(tmpname), "%s %s", 1810 name, suffix); 1811 name = tmpname; 1812 } 1813 if (!strcmp(sctl->id.name, name)) { 1814 err = func(codec, data, sctl); 1815 if (err) 1816 return err; 1817 break; 1818 } 1819 } 1820 } 1821 return 0; 1822 } 1823 1824 static int check_slave_present(struct hda_codec *codec, 1825 void *data, struct snd_kcontrol *sctl) 1826 { 1827 return 1; 1828 } 1829 1830 /* call kctl->put with the given value(s) */ 1831 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) 1832 { 1833 struct snd_ctl_elem_value *ucontrol; 1834 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); 1835 if (!ucontrol) 1836 return -ENOMEM; 1837 ucontrol->value.integer.value[0] = val; 1838 ucontrol->value.integer.value[1] = val; 1839 kctl->put(kctl, ucontrol); 1840 kfree(ucontrol); 1841 return 0; 1842 } 1843 1844 struct slave_init_arg { 1845 struct hda_codec *codec; 1846 int step; 1847 }; 1848 1849 /* initialize the slave volume with 0dB via snd_ctl_apply_vmaster_slaves() */ 1850 static int init_slave_0dB(struct snd_kcontrol *slave, 1851 struct snd_kcontrol *kctl, 1852 void *_arg) 1853 { 1854 struct slave_init_arg *arg = _arg; 1855 int _tlv[4]; 1856 const int *tlv = NULL; 1857 int step; 1858 int val; 1859 1860 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 1861 if (kctl->tlv.c != snd_hda_mixer_amp_tlv) { 1862 codec_err(arg->codec, 1863 "Unexpected TLV callback for slave %s:%d\n", 1864 kctl->id.name, kctl->id.index); 1865 return 0; /* ignore */ 1866 } 1867 get_ctl_amp_tlv(kctl, _tlv); 1868 tlv = _tlv; 1869 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) 1870 tlv = kctl->tlv.p; 1871 1872 if (!tlv || tlv[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE) 1873 return 0; 1874 1875 step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP]; 1876 step &= ~TLV_DB_SCALE_MUTE; 1877 if (!step) 1878 return 0; 1879 if (arg->step && arg->step != step) { 1880 codec_err(arg->codec, 1881 "Mismatching dB step for vmaster slave (%d!=%d)\n", 1882 arg->step, step); 1883 return 0; 1884 } 1885 1886 arg->step = step; 1887 val = -tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] / step; 1888 if (val > 0) { 1889 put_kctl_with_value(slave, val); 1890 return val; 1891 } 1892 1893 return 0; 1894 } 1895 1896 /* unmute the slave via snd_ctl_apply_vmaster_slaves() */ 1897 static int init_slave_unmute(struct snd_kcontrol *slave, 1898 struct snd_kcontrol *kctl, 1899 void *_arg) 1900 { 1901 return put_kctl_with_value(slave, 1); 1902 } 1903 1904 static int add_slave(struct hda_codec *codec, 1905 void *data, struct snd_kcontrol *slave) 1906 { 1907 return snd_ctl_add_slave(data, slave); 1908 } 1909 1910 /** 1911 * __snd_hda_add_vmaster - create a virtual master control and add slaves 1912 * @codec: HD-audio codec 1913 * @name: vmaster control name 1914 * @tlv: TLV data (optional) 1915 * @slaves: slave control names (optional) 1916 * @suffix: suffix string to each slave name (optional) 1917 * @init_slave_vol: initialize slaves to unmute/0dB 1918 * @ctl_ret: store the vmaster kcontrol in return 1919 * 1920 * Create a virtual master control with the given name. The TLV data 1921 * must be either NULL or a valid data. 1922 * 1923 * @slaves is a NULL-terminated array of strings, each of which is a 1924 * slave control name. All controls with these names are assigned to 1925 * the new virtual master control. 1926 * 1927 * This function returns zero if successful or a negative error code. 1928 */ 1929 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, 1930 unsigned int *tlv, const char * const *slaves, 1931 const char *suffix, bool init_slave_vol, 1932 struct snd_kcontrol **ctl_ret) 1933 { 1934 struct snd_kcontrol *kctl; 1935 int err; 1936 1937 if (ctl_ret) 1938 *ctl_ret = NULL; 1939 1940 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); 1941 if (err != 1) { 1942 codec_dbg(codec, "No slave found for %s\n", name); 1943 return 0; 1944 } 1945 kctl = snd_ctl_make_virtual_master(name, tlv); 1946 if (!kctl) 1947 return -ENOMEM; 1948 err = snd_hda_ctl_add(codec, 0, kctl); 1949 if (err < 0) 1950 return err; 1951 1952 err = map_slaves(codec, slaves, suffix, add_slave, kctl); 1953 if (err < 0) 1954 return err; 1955 1956 /* init with master mute & zero volume */ 1957 put_kctl_with_value(kctl, 0); 1958 if (init_slave_vol) { 1959 struct slave_init_arg arg = { 1960 .codec = codec, 1961 .step = 0, 1962 }; 1963 snd_ctl_apply_vmaster_slaves(kctl, 1964 tlv ? init_slave_0dB : init_slave_unmute, 1965 &arg); 1966 } 1967 1968 if (ctl_ret) 1969 *ctl_ret = kctl; 1970 return 0; 1971 } 1972 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); 1973 1974 /* 1975 * mute-LED control using vmaster 1976 */ 1977 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, 1978 struct snd_ctl_elem_info *uinfo) 1979 { 1980 static const char * const texts[] = { 1981 "On", "Off", "Follow Master" 1982 }; 1983 1984 return snd_ctl_enum_info(uinfo, 1, 3, texts); 1985 } 1986 1987 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, 1988 struct snd_ctl_elem_value *ucontrol) 1989 { 1990 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); 1991 ucontrol->value.enumerated.item[0] = hook->mute_mode; 1992 return 0; 1993 } 1994 1995 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, 1996 struct snd_ctl_elem_value *ucontrol) 1997 { 1998 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); 1999 unsigned int old_mode = hook->mute_mode; 2000 2001 hook->mute_mode = ucontrol->value.enumerated.item[0]; 2002 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) 2003 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; 2004 if (old_mode == hook->mute_mode) 2005 return 0; 2006 snd_hda_sync_vmaster_hook(hook); 2007 return 1; 2008 } 2009 2010 static const struct snd_kcontrol_new vmaster_mute_mode = { 2011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2012 .name = "Mute-LED Mode", 2013 .info = vmaster_mute_mode_info, 2014 .get = vmaster_mute_mode_get, 2015 .put = vmaster_mute_mode_put, 2016 }; 2017 2018 /* meta hook to call each driver's vmaster hook */ 2019 static void vmaster_hook(void *private_data, int enabled) 2020 { 2021 struct hda_vmaster_mute_hook *hook = private_data; 2022 2023 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER) 2024 enabled = hook->mute_mode; 2025 hook->hook(hook->codec, enabled); 2026 } 2027 2028 /** 2029 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED 2030 * @codec: the HDA codec 2031 * @hook: the vmaster hook object 2032 * @expose_enum_ctl: flag to create an enum ctl 2033 * 2034 * Add a mute-LED hook with the given vmaster switch kctl. 2035 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically 2036 * created and associated with the given hook. 2037 */ 2038 int snd_hda_add_vmaster_hook(struct hda_codec *codec, 2039 struct hda_vmaster_mute_hook *hook, 2040 bool expose_enum_ctl) 2041 { 2042 struct snd_kcontrol *kctl; 2043 2044 if (!hook->hook || !hook->sw_kctl) 2045 return 0; 2046 hook->codec = codec; 2047 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; 2048 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook); 2049 if (!expose_enum_ctl) 2050 return 0; 2051 kctl = snd_ctl_new1(&vmaster_mute_mode, hook); 2052 if (!kctl) 2053 return -ENOMEM; 2054 return snd_hda_ctl_add(codec, 0, kctl); 2055 } 2056 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); 2057 2058 /** 2059 * snd_hda_sync_vmaster_hook - Sync vmaster hook 2060 * @hook: the vmaster hook 2061 * 2062 * Call the hook with the current value for synchronization. 2063 * Should be called in init callback. 2064 */ 2065 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) 2066 { 2067 if (!hook->hook || !hook->codec) 2068 return; 2069 /* don't call vmaster hook in the destructor since it might have 2070 * been already destroyed 2071 */ 2072 if (hook->codec->bus->shutdown) 2073 return; 2074 snd_ctl_sync_vmaster_hook(hook->sw_kctl); 2075 } 2076 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); 2077 2078 2079 /** 2080 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch 2081 * @kcontrol: referred ctl element 2082 * @uinfo: pointer to get/store the data 2083 * 2084 * The control element is supposed to have the private_value field 2085 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2086 */ 2087 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, 2088 struct snd_ctl_elem_info *uinfo) 2089 { 2090 int chs = get_amp_channels(kcontrol); 2091 2092 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2093 uinfo->count = chs == 3 ? 2 : 1; 2094 uinfo->value.integer.min = 0; 2095 uinfo->value.integer.max = 1; 2096 return 0; 2097 } 2098 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); 2099 2100 /** 2101 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch 2102 * @kcontrol: ctl element 2103 * @ucontrol: pointer to get/store the data 2104 * 2105 * The control element is supposed to have the private_value field 2106 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2107 */ 2108 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, 2109 struct snd_ctl_elem_value *ucontrol) 2110 { 2111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2112 hda_nid_t nid = get_amp_nid(kcontrol); 2113 int chs = get_amp_channels(kcontrol); 2114 int dir = get_amp_direction(kcontrol); 2115 int idx = get_amp_index(kcontrol); 2116 long *valp = ucontrol->value.integer.value; 2117 2118 if (chs & 1) 2119 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 2120 HDA_AMP_MUTE) ? 0 : 1; 2121 if (chs & 2) 2122 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 2123 HDA_AMP_MUTE) ? 0 : 1; 2124 return 0; 2125 } 2126 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); 2127 2128 /** 2129 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch 2130 * @kcontrol: ctl element 2131 * @ucontrol: pointer to get/store the data 2132 * 2133 * The control element is supposed to have the private_value field 2134 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2135 */ 2136 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, 2137 struct snd_ctl_elem_value *ucontrol) 2138 { 2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2140 hda_nid_t nid = get_amp_nid(kcontrol); 2141 int chs = get_amp_channels(kcontrol); 2142 int dir = get_amp_direction(kcontrol); 2143 int idx = get_amp_index(kcontrol); 2144 long *valp = ucontrol->value.integer.value; 2145 int change = 0; 2146 2147 if (chs & 1) { 2148 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, 2149 HDA_AMP_MUTE, 2150 *valp ? 0 : HDA_AMP_MUTE); 2151 valp++; 2152 } 2153 if (chs & 2) 2154 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, 2155 HDA_AMP_MUTE, 2156 *valp ? 0 : HDA_AMP_MUTE); 2157 hda_call_check_power_status(codec, nid); 2158 return change; 2159 } 2160 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); 2161 2162 /* 2163 * SPDIF out controls 2164 */ 2165 2166 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, 2167 struct snd_ctl_elem_info *uinfo) 2168 { 2169 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 2170 uinfo->count = 1; 2171 return 0; 2172 } 2173 2174 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, 2175 struct snd_ctl_elem_value *ucontrol) 2176 { 2177 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 2178 IEC958_AES0_NONAUDIO | 2179 IEC958_AES0_CON_EMPHASIS_5015 | 2180 IEC958_AES0_CON_NOT_COPYRIGHT; 2181 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | 2182 IEC958_AES1_CON_ORIGINAL; 2183 return 0; 2184 } 2185 2186 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, 2187 struct snd_ctl_elem_value *ucontrol) 2188 { 2189 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 2190 IEC958_AES0_NONAUDIO | 2191 IEC958_AES0_PRO_EMPHASIS_5015; 2192 return 0; 2193 } 2194 2195 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, 2196 struct snd_ctl_elem_value *ucontrol) 2197 { 2198 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2199 int idx = kcontrol->private_value; 2200 struct hda_spdif_out *spdif; 2201 2202 if (WARN_ON(codec->spdif_out.used <= idx)) 2203 return -EINVAL; 2204 mutex_lock(&codec->spdif_mutex); 2205 spdif = snd_array_elem(&codec->spdif_out, idx); 2206 ucontrol->value.iec958.status[0] = spdif->status & 0xff; 2207 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; 2208 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; 2209 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; 2210 mutex_unlock(&codec->spdif_mutex); 2211 2212 return 0; 2213 } 2214 2215 /* convert from SPDIF status bits to HDA SPDIF bits 2216 * bit 0 (DigEn) is always set zero (to be filled later) 2217 */ 2218 static unsigned short convert_from_spdif_status(unsigned int sbits) 2219 { 2220 unsigned short val = 0; 2221 2222 if (sbits & IEC958_AES0_PROFESSIONAL) 2223 val |= AC_DIG1_PROFESSIONAL; 2224 if (sbits & IEC958_AES0_NONAUDIO) 2225 val |= AC_DIG1_NONAUDIO; 2226 if (sbits & IEC958_AES0_PROFESSIONAL) { 2227 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == 2228 IEC958_AES0_PRO_EMPHASIS_5015) 2229 val |= AC_DIG1_EMPHASIS; 2230 } else { 2231 if ((sbits & IEC958_AES0_CON_EMPHASIS) == 2232 IEC958_AES0_CON_EMPHASIS_5015) 2233 val |= AC_DIG1_EMPHASIS; 2234 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) 2235 val |= AC_DIG1_COPYRIGHT; 2236 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) 2237 val |= AC_DIG1_LEVEL; 2238 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); 2239 } 2240 return val; 2241 } 2242 2243 /* convert to SPDIF status bits from HDA SPDIF bits 2244 */ 2245 static unsigned int convert_to_spdif_status(unsigned short val) 2246 { 2247 unsigned int sbits = 0; 2248 2249 if (val & AC_DIG1_NONAUDIO) 2250 sbits |= IEC958_AES0_NONAUDIO; 2251 if (val & AC_DIG1_PROFESSIONAL) 2252 sbits |= IEC958_AES0_PROFESSIONAL; 2253 if (sbits & IEC958_AES0_PROFESSIONAL) { 2254 if (val & AC_DIG1_EMPHASIS) 2255 sbits |= IEC958_AES0_PRO_EMPHASIS_5015; 2256 } else { 2257 if (val & AC_DIG1_EMPHASIS) 2258 sbits |= IEC958_AES0_CON_EMPHASIS_5015; 2259 if (!(val & AC_DIG1_COPYRIGHT)) 2260 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; 2261 if (val & AC_DIG1_LEVEL) 2262 sbits |= (IEC958_AES1_CON_ORIGINAL << 8); 2263 sbits |= val & (0x7f << 8); 2264 } 2265 return sbits; 2266 } 2267 2268 /* set digital convert verbs both for the given NID and its slaves */ 2269 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, 2270 int mask, int val) 2271 { 2272 const hda_nid_t *d; 2273 2274 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1, 2275 mask, val); 2276 d = codec->slave_dig_outs; 2277 if (!d) 2278 return; 2279 for (; *d; d++) 2280 snd_hdac_regmap_update(&codec->core, *d, 2281 AC_VERB_SET_DIGI_CONVERT_1, mask, val); 2282 } 2283 2284 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, 2285 int dig1, int dig2) 2286 { 2287 unsigned int mask = 0; 2288 unsigned int val = 0; 2289 2290 if (dig1 != -1) { 2291 mask |= 0xff; 2292 val = dig1; 2293 } 2294 if (dig2 != -1) { 2295 mask |= 0xff00; 2296 val |= dig2 << 8; 2297 } 2298 set_dig_out(codec, nid, mask, val); 2299 } 2300 2301 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, 2302 struct snd_ctl_elem_value *ucontrol) 2303 { 2304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2305 int idx = kcontrol->private_value; 2306 struct hda_spdif_out *spdif; 2307 hda_nid_t nid; 2308 unsigned short val; 2309 int change; 2310 2311 if (WARN_ON(codec->spdif_out.used <= idx)) 2312 return -EINVAL; 2313 mutex_lock(&codec->spdif_mutex); 2314 spdif = snd_array_elem(&codec->spdif_out, idx); 2315 nid = spdif->nid; 2316 spdif->status = ucontrol->value.iec958.status[0] | 2317 ((unsigned int)ucontrol->value.iec958.status[1] << 8) | 2318 ((unsigned int)ucontrol->value.iec958.status[2] << 16) | 2319 ((unsigned int)ucontrol->value.iec958.status[3] << 24); 2320 val = convert_from_spdif_status(spdif->status); 2321 val |= spdif->ctls & 1; 2322 change = spdif->ctls != val; 2323 spdif->ctls = val; 2324 if (change && nid != (u16)-1) 2325 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); 2326 mutex_unlock(&codec->spdif_mutex); 2327 return change; 2328 } 2329 2330 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info 2331 2332 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, 2333 struct snd_ctl_elem_value *ucontrol) 2334 { 2335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2336 int idx = kcontrol->private_value; 2337 struct hda_spdif_out *spdif; 2338 2339 if (WARN_ON(codec->spdif_out.used <= idx)) 2340 return -EINVAL; 2341 mutex_lock(&codec->spdif_mutex); 2342 spdif = snd_array_elem(&codec->spdif_out, idx); 2343 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; 2344 mutex_unlock(&codec->spdif_mutex); 2345 return 0; 2346 } 2347 2348 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, 2349 int dig1, int dig2) 2350 { 2351 set_dig_out_convert(codec, nid, dig1, dig2); 2352 /* unmute amp switch (if any) */ 2353 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && 2354 (dig1 & AC_DIG1_ENABLE)) 2355 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, 2356 HDA_AMP_MUTE, 0); 2357 } 2358 2359 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, 2360 struct snd_ctl_elem_value *ucontrol) 2361 { 2362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2363 int idx = kcontrol->private_value; 2364 struct hda_spdif_out *spdif; 2365 hda_nid_t nid; 2366 unsigned short val; 2367 int change; 2368 2369 if (WARN_ON(codec->spdif_out.used <= idx)) 2370 return -EINVAL; 2371 mutex_lock(&codec->spdif_mutex); 2372 spdif = snd_array_elem(&codec->spdif_out, idx); 2373 nid = spdif->nid; 2374 val = spdif->ctls & ~AC_DIG1_ENABLE; 2375 if (ucontrol->value.integer.value[0]) 2376 val |= AC_DIG1_ENABLE; 2377 change = spdif->ctls != val; 2378 spdif->ctls = val; 2379 if (change && nid != (u16)-1) 2380 set_spdif_ctls(codec, nid, val & 0xff, -1); 2381 mutex_unlock(&codec->spdif_mutex); 2382 return change; 2383 } 2384 2385 static struct snd_kcontrol_new dig_mixes[] = { 2386 { 2387 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2388 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2389 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), 2390 .info = snd_hda_spdif_mask_info, 2391 .get = snd_hda_spdif_cmask_get, 2392 }, 2393 { 2394 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2396 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), 2397 .info = snd_hda_spdif_mask_info, 2398 .get = snd_hda_spdif_pmask_get, 2399 }, 2400 { 2401 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2402 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 2403 .info = snd_hda_spdif_mask_info, 2404 .get = snd_hda_spdif_default_get, 2405 .put = snd_hda_spdif_default_put, 2406 }, 2407 { 2408 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2409 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), 2410 .info = snd_hda_spdif_out_switch_info, 2411 .get = snd_hda_spdif_out_switch_get, 2412 .put = snd_hda_spdif_out_switch_put, 2413 }, 2414 { } /* end */ 2415 }; 2416 2417 /** 2418 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls 2419 * @codec: the HDA codec 2420 * @associated_nid: NID that new ctls associated with 2421 * @cvt_nid: converter NID 2422 * @type: HDA_PCM_TYPE_* 2423 * Creates controls related with the digital output. 2424 * Called from each patch supporting the digital out. 2425 * 2426 * Returns 0 if successful, or a negative error code. 2427 */ 2428 int snd_hda_create_dig_out_ctls(struct hda_codec *codec, 2429 hda_nid_t associated_nid, 2430 hda_nid_t cvt_nid, 2431 int type) 2432 { 2433 int err; 2434 struct snd_kcontrol *kctl; 2435 struct snd_kcontrol_new *dig_mix; 2436 int idx = 0; 2437 int val = 0; 2438 const int spdif_index = 16; 2439 struct hda_spdif_out *spdif; 2440 struct hda_bus *bus = codec->bus; 2441 2442 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && 2443 type == HDA_PCM_TYPE_SPDIF) { 2444 idx = spdif_index; 2445 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && 2446 type == HDA_PCM_TYPE_HDMI) { 2447 /* suppose a single SPDIF device */ 2448 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 2449 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); 2450 if (!kctl) 2451 break; 2452 kctl->id.index = spdif_index; 2453 } 2454 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; 2455 } 2456 if (!bus->primary_dig_out_type) 2457 bus->primary_dig_out_type = type; 2458 2459 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); 2460 if (idx < 0) { 2461 codec_err(codec, "too many IEC958 outputs\n"); 2462 return -EBUSY; 2463 } 2464 spdif = snd_array_new(&codec->spdif_out); 2465 if (!spdif) 2466 return -ENOMEM; 2467 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 2468 kctl = snd_ctl_new1(dig_mix, codec); 2469 if (!kctl) 2470 return -ENOMEM; 2471 kctl->id.index = idx; 2472 kctl->private_value = codec->spdif_out.used - 1; 2473 err = snd_hda_ctl_add(codec, associated_nid, kctl); 2474 if (err < 0) 2475 return err; 2476 } 2477 spdif->nid = cvt_nid; 2478 snd_hdac_regmap_read(&codec->core, cvt_nid, 2479 AC_VERB_GET_DIGI_CONVERT_1, &val); 2480 spdif->ctls = val; 2481 spdif->status = convert_to_spdif_status(spdif->ctls); 2482 return 0; 2483 } 2484 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); 2485 2486 /** 2487 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID 2488 * @codec: the HDA codec 2489 * @nid: widget NID 2490 * 2491 * call within spdif_mutex lock 2492 */ 2493 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, 2494 hda_nid_t nid) 2495 { 2496 struct hda_spdif_out *spdif; 2497 int i; 2498 2499 snd_array_for_each(&codec->spdif_out, i, spdif) { 2500 if (spdif->nid == nid) 2501 return spdif; 2502 } 2503 return NULL; 2504 } 2505 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); 2506 2507 /** 2508 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl 2509 * @codec: the HDA codec 2510 * @idx: the SPDIF ctl index 2511 * 2512 * Unassign the widget from the given SPDIF control. 2513 */ 2514 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) 2515 { 2516 struct hda_spdif_out *spdif; 2517 2518 if (WARN_ON(codec->spdif_out.used <= idx)) 2519 return; 2520 mutex_lock(&codec->spdif_mutex); 2521 spdif = snd_array_elem(&codec->spdif_out, idx); 2522 spdif->nid = (u16)-1; 2523 mutex_unlock(&codec->spdif_mutex); 2524 } 2525 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); 2526 2527 /** 2528 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID 2529 * @codec: the HDA codec 2530 * @idx: the SPDIF ctl idx 2531 * @nid: widget NID 2532 * 2533 * Assign the widget to the SPDIF control with the given index. 2534 */ 2535 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) 2536 { 2537 struct hda_spdif_out *spdif; 2538 unsigned short val; 2539 2540 if (WARN_ON(codec->spdif_out.used <= idx)) 2541 return; 2542 mutex_lock(&codec->spdif_mutex); 2543 spdif = snd_array_elem(&codec->spdif_out, idx); 2544 if (spdif->nid != nid) { 2545 spdif->nid = nid; 2546 val = spdif->ctls; 2547 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); 2548 } 2549 mutex_unlock(&codec->spdif_mutex); 2550 } 2551 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); 2552 2553 /* 2554 * SPDIF sharing with analog output 2555 */ 2556 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, 2557 struct snd_ctl_elem_value *ucontrol) 2558 { 2559 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); 2560 ucontrol->value.integer.value[0] = mout->share_spdif; 2561 return 0; 2562 } 2563 2564 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, 2565 struct snd_ctl_elem_value *ucontrol) 2566 { 2567 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); 2568 mout->share_spdif = !!ucontrol->value.integer.value[0]; 2569 return 0; 2570 } 2571 2572 static const struct snd_kcontrol_new spdif_share_sw = { 2573 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2574 .name = "IEC958 Default PCM Playback Switch", 2575 .info = snd_ctl_boolean_mono_info, 2576 .get = spdif_share_sw_get, 2577 .put = spdif_share_sw_put, 2578 }; 2579 2580 /** 2581 * snd_hda_create_spdif_share_sw - create Default PCM switch 2582 * @codec: the HDA codec 2583 * @mout: multi-out instance 2584 */ 2585 int snd_hda_create_spdif_share_sw(struct hda_codec *codec, 2586 struct hda_multi_out *mout) 2587 { 2588 struct snd_kcontrol *kctl; 2589 2590 if (!mout->dig_out_nid) 2591 return 0; 2592 2593 kctl = snd_ctl_new1(&spdif_share_sw, mout); 2594 if (!kctl) 2595 return -ENOMEM; 2596 /* ATTENTION: here mout is passed as private_data, instead of codec */ 2597 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); 2598 } 2599 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); 2600 2601 /* 2602 * SPDIF input 2603 */ 2604 2605 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info 2606 2607 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, 2608 struct snd_ctl_elem_value *ucontrol) 2609 { 2610 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2611 2612 ucontrol->value.integer.value[0] = codec->spdif_in_enable; 2613 return 0; 2614 } 2615 2616 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, 2617 struct snd_ctl_elem_value *ucontrol) 2618 { 2619 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2620 hda_nid_t nid = kcontrol->private_value; 2621 unsigned int val = !!ucontrol->value.integer.value[0]; 2622 int change; 2623 2624 mutex_lock(&codec->spdif_mutex); 2625 change = codec->spdif_in_enable != val; 2626 if (change) { 2627 codec->spdif_in_enable = val; 2628 snd_hdac_regmap_write(&codec->core, nid, 2629 AC_VERB_SET_DIGI_CONVERT_1, val); 2630 } 2631 mutex_unlock(&codec->spdif_mutex); 2632 return change; 2633 } 2634 2635 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, 2636 struct snd_ctl_elem_value *ucontrol) 2637 { 2638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2639 hda_nid_t nid = kcontrol->private_value; 2640 unsigned int val; 2641 unsigned int sbits; 2642 2643 snd_hdac_regmap_read(&codec->core, nid, 2644 AC_VERB_GET_DIGI_CONVERT_1, &val); 2645 sbits = convert_to_spdif_status(val); 2646 ucontrol->value.iec958.status[0] = sbits; 2647 ucontrol->value.iec958.status[1] = sbits >> 8; 2648 ucontrol->value.iec958.status[2] = sbits >> 16; 2649 ucontrol->value.iec958.status[3] = sbits >> 24; 2650 return 0; 2651 } 2652 2653 static struct snd_kcontrol_new dig_in_ctls[] = { 2654 { 2655 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2656 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), 2657 .info = snd_hda_spdif_in_switch_info, 2658 .get = snd_hda_spdif_in_switch_get, 2659 .put = snd_hda_spdif_in_switch_put, 2660 }, 2661 { 2662 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2664 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), 2665 .info = snd_hda_spdif_mask_info, 2666 .get = snd_hda_spdif_in_status_get, 2667 }, 2668 { } /* end */ 2669 }; 2670 2671 /** 2672 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls 2673 * @codec: the HDA codec 2674 * @nid: audio in widget NID 2675 * 2676 * Creates controls related with the SPDIF input. 2677 * Called from each patch supporting the SPDIF in. 2678 * 2679 * Returns 0 if successful, or a negative error code. 2680 */ 2681 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) 2682 { 2683 int err; 2684 struct snd_kcontrol *kctl; 2685 struct snd_kcontrol_new *dig_mix; 2686 int idx; 2687 2688 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); 2689 if (idx < 0) { 2690 codec_err(codec, "too many IEC958 inputs\n"); 2691 return -EBUSY; 2692 } 2693 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { 2694 kctl = snd_ctl_new1(dig_mix, codec); 2695 if (!kctl) 2696 return -ENOMEM; 2697 kctl->private_value = nid; 2698 err = snd_hda_ctl_add(codec, nid, kctl); 2699 if (err < 0) 2700 return err; 2701 } 2702 codec->spdif_in_enable = 2703 snd_hda_codec_read(codec, nid, 0, 2704 AC_VERB_GET_DIGI_CONVERT_1, 0) & 2705 AC_DIG1_ENABLE; 2706 return 0; 2707 } 2708 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); 2709 2710 /** 2711 * snd_hda_codec_set_power_to_all - Set the power state to all widgets 2712 * @codec: the HDA codec 2713 * @fg: function group (not used now) 2714 * @power_state: the power state to set (AC_PWRST_*) 2715 * 2716 * Set the given power state to all widgets that have the power control. 2717 * If the codec has power_filter set, it evaluates the power state and 2718 * filter out if it's unchanged as D3. 2719 */ 2720 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, 2721 unsigned int power_state) 2722 { 2723 hda_nid_t nid; 2724 2725 for_each_hda_codec_node(nid, codec) { 2726 unsigned int wcaps = get_wcaps(codec, nid); 2727 unsigned int state = power_state; 2728 if (!(wcaps & AC_WCAP_POWER)) 2729 continue; 2730 if (codec->power_filter) { 2731 state = codec->power_filter(codec, nid, power_state); 2732 if (state != power_state && power_state == AC_PWRST_D3) 2733 continue; 2734 } 2735 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, 2736 state); 2737 } 2738 } 2739 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); 2740 2741 /** 2742 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD 2743 * @codec: the HDA codec 2744 * @nid: widget NID 2745 * @power_state: power state to evalue 2746 * 2747 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. 2748 * This can be used a codec power_filter callback. 2749 */ 2750 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, 2751 hda_nid_t nid, 2752 unsigned int power_state) 2753 { 2754 if (nid == codec->core.afg || nid == codec->core.mfg) 2755 return power_state; 2756 if (power_state == AC_PWRST_D3 && 2757 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && 2758 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { 2759 int eapd = snd_hda_codec_read(codec, nid, 0, 2760 AC_VERB_GET_EAPD_BTLENABLE, 0); 2761 if (eapd & 0x02) 2762 return AC_PWRST_D0; 2763 } 2764 return power_state; 2765 } 2766 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); 2767 2768 /* 2769 * set power state of the codec, and return the power state 2770 */ 2771 static unsigned int hda_set_power_state(struct hda_codec *codec, 2772 unsigned int power_state) 2773 { 2774 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg; 2775 int count; 2776 unsigned int state; 2777 int flags = 0; 2778 2779 /* this delay seems necessary to avoid click noise at power-down */ 2780 if (power_state == AC_PWRST_D3) { 2781 if (codec->depop_delay < 0) 2782 msleep(codec_has_epss(codec) ? 10 : 100); 2783 else if (codec->depop_delay > 0) 2784 msleep(codec->depop_delay); 2785 flags = HDA_RW_NO_RESPONSE_FALLBACK; 2786 } 2787 2788 /* repeat power states setting at most 10 times*/ 2789 for (count = 0; count < 10; count++) { 2790 if (codec->patch_ops.set_power_state) 2791 codec->patch_ops.set_power_state(codec, fg, 2792 power_state); 2793 else { 2794 state = power_state; 2795 if (codec->power_filter) 2796 state = codec->power_filter(codec, fg, state); 2797 if (state == power_state || power_state != AC_PWRST_D3) 2798 snd_hda_codec_read(codec, fg, flags, 2799 AC_VERB_SET_POWER_STATE, 2800 state); 2801 snd_hda_codec_set_power_to_all(codec, fg, power_state); 2802 } 2803 state = snd_hda_sync_power_state(codec, fg, power_state); 2804 if (!(state & AC_PWRST_ERROR)) 2805 break; 2806 } 2807 2808 return state; 2809 } 2810 2811 /* sync power states of all widgets; 2812 * this is called at the end of codec parsing 2813 */ 2814 static void sync_power_up_states(struct hda_codec *codec) 2815 { 2816 hda_nid_t nid; 2817 2818 /* don't care if no filter is used */ 2819 if (!codec->power_filter) 2820 return; 2821 2822 for_each_hda_codec_node(nid, codec) { 2823 unsigned int wcaps = get_wcaps(codec, nid); 2824 unsigned int target; 2825 if (!(wcaps & AC_WCAP_POWER)) 2826 continue; 2827 target = codec->power_filter(codec, nid, AC_PWRST_D0); 2828 if (target == AC_PWRST_D0) 2829 continue; 2830 if (!snd_hda_check_power_state(codec, nid, target)) 2831 snd_hda_codec_write(codec, nid, 0, 2832 AC_VERB_SET_POWER_STATE, target); 2833 } 2834 } 2835 2836 #ifdef CONFIG_SND_HDA_RECONFIG 2837 /* execute additional init verbs */ 2838 static void hda_exec_init_verbs(struct hda_codec *codec) 2839 { 2840 if (codec->init_verbs.list) 2841 snd_hda_sequence_write(codec, codec->init_verbs.list); 2842 } 2843 #else 2844 static inline void hda_exec_init_verbs(struct hda_codec *codec) {} 2845 #endif 2846 2847 #ifdef CONFIG_PM 2848 /* update the power on/off account with the current jiffies */ 2849 static void update_power_acct(struct hda_codec *codec, bool on) 2850 { 2851 unsigned long delta = jiffies - codec->power_jiffies; 2852 2853 if (on) 2854 codec->power_on_acct += delta; 2855 else 2856 codec->power_off_acct += delta; 2857 codec->power_jiffies += delta; 2858 } 2859 2860 void snd_hda_update_power_acct(struct hda_codec *codec) 2861 { 2862 update_power_acct(codec, hda_codec_is_power_on(codec)); 2863 } 2864 2865 /* 2866 * call suspend and power-down; used both from PM and power-save 2867 * this function returns the power state in the end 2868 */ 2869 static unsigned int hda_call_codec_suspend(struct hda_codec *codec) 2870 { 2871 unsigned int state; 2872 2873 snd_hdac_enter_pm(&codec->core); 2874 if (codec->patch_ops.suspend) 2875 codec->patch_ops.suspend(codec); 2876 hda_cleanup_all_streams(codec); 2877 state = hda_set_power_state(codec, AC_PWRST_D3); 2878 update_power_acct(codec, true); 2879 snd_hdac_leave_pm(&codec->core); 2880 return state; 2881 } 2882 2883 /* 2884 * kick up codec; used both from PM and power-save 2885 */ 2886 static void hda_call_codec_resume(struct hda_codec *codec) 2887 { 2888 snd_hdac_enter_pm(&codec->core); 2889 if (codec->core.regmap) 2890 regcache_mark_dirty(codec->core.regmap); 2891 2892 codec->power_jiffies = jiffies; 2893 2894 hda_set_power_state(codec, AC_PWRST_D0); 2895 restore_shutup_pins(codec); 2896 hda_exec_init_verbs(codec); 2897 snd_hda_jack_set_dirty_all(codec); 2898 if (codec->patch_ops.resume) 2899 codec->patch_ops.resume(codec); 2900 else { 2901 if (codec->patch_ops.init) 2902 codec->patch_ops.init(codec); 2903 if (codec->core.regmap) 2904 regcache_sync(codec->core.regmap); 2905 } 2906 2907 if (codec->jackpoll_interval) 2908 hda_jackpoll_work(&codec->jackpoll_work.work); 2909 else 2910 snd_hda_jack_report_sync(codec); 2911 codec->core.dev.power.power_state = PMSG_ON; 2912 snd_hdac_leave_pm(&codec->core); 2913 } 2914 2915 static int hda_codec_runtime_suspend(struct device *dev) 2916 { 2917 struct hda_codec *codec = dev_to_hda_codec(dev); 2918 unsigned int state; 2919 2920 cancel_delayed_work_sync(&codec->jackpoll_work); 2921 state = hda_call_codec_suspend(codec); 2922 if (codec->link_down_at_suspend || 2923 (codec_has_clkstop(codec) && codec_has_epss(codec) && 2924 (state & AC_PWRST_CLK_STOP_OK))) 2925 snd_hdac_codec_link_down(&codec->core); 2926 codec_display_power(codec, false); 2927 return 0; 2928 } 2929 2930 static int hda_codec_runtime_resume(struct device *dev) 2931 { 2932 struct hda_codec *codec = dev_to_hda_codec(dev); 2933 2934 codec_display_power(codec, true); 2935 snd_hdac_codec_link_up(&codec->core); 2936 hda_call_codec_resume(codec); 2937 pm_runtime_mark_last_busy(dev); 2938 return 0; 2939 } 2940 #endif /* CONFIG_PM */ 2941 2942 #ifdef CONFIG_PM_SLEEP 2943 static int hda_codec_force_resume(struct device *dev) 2944 { 2945 int ret; 2946 2947 /* The get/put pair below enforces the runtime resume even if the 2948 * device hasn't been used at suspend time. This trick is needed to 2949 * update the jack state change during the sleep. 2950 */ 2951 pm_runtime_get_noresume(dev); 2952 ret = pm_runtime_force_resume(dev); 2953 pm_runtime_put(dev); 2954 return ret; 2955 } 2956 2957 static int hda_codec_pm_suspend(struct device *dev) 2958 { 2959 dev->power.power_state = PMSG_SUSPEND; 2960 return pm_runtime_force_suspend(dev); 2961 } 2962 2963 static int hda_codec_pm_resume(struct device *dev) 2964 { 2965 dev->power.power_state = PMSG_RESUME; 2966 return hda_codec_force_resume(dev); 2967 } 2968 2969 static int hda_codec_pm_freeze(struct device *dev) 2970 { 2971 dev->power.power_state = PMSG_FREEZE; 2972 return pm_runtime_force_suspend(dev); 2973 } 2974 2975 static int hda_codec_pm_thaw(struct device *dev) 2976 { 2977 dev->power.power_state = PMSG_THAW; 2978 return hda_codec_force_resume(dev); 2979 } 2980 2981 static int hda_codec_pm_restore(struct device *dev) 2982 { 2983 dev->power.power_state = PMSG_RESTORE; 2984 return hda_codec_force_resume(dev); 2985 } 2986 #endif /* CONFIG_PM_SLEEP */ 2987 2988 /* referred in hda_bind.c */ 2989 const struct dev_pm_ops hda_codec_driver_pm = { 2990 #ifdef CONFIG_PM_SLEEP 2991 .suspend = hda_codec_pm_suspend, 2992 .resume = hda_codec_pm_resume, 2993 .freeze = hda_codec_pm_freeze, 2994 .thaw = hda_codec_pm_thaw, 2995 .poweroff = hda_codec_pm_suspend, 2996 .restore = hda_codec_pm_restore, 2997 #endif /* CONFIG_PM_SLEEP */ 2998 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, 2999 NULL) 3000 }; 3001 3002 /* 3003 * add standard channel maps if not specified 3004 */ 3005 static int add_std_chmaps(struct hda_codec *codec) 3006 { 3007 struct hda_pcm *pcm; 3008 int str, err; 3009 3010 list_for_each_entry(pcm, &codec->pcm_list_head, list) { 3011 for (str = 0; str < 2; str++) { 3012 struct hda_pcm_stream *hinfo = &pcm->stream[str]; 3013 struct snd_pcm_chmap *chmap; 3014 const struct snd_pcm_chmap_elem *elem; 3015 3016 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams) 3017 continue; 3018 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; 3019 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem, 3020 hinfo->channels_max, 3021 0, &chmap); 3022 if (err < 0) 3023 return err; 3024 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; 3025 } 3026 } 3027 return 0; 3028 } 3029 3030 /* default channel maps for 2.1 speakers; 3031 * since HD-audio supports only stereo, odd number channels are omitted 3032 */ 3033 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { 3034 { .channels = 2, 3035 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, 3036 { .channels = 4, 3037 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 3038 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, 3039 { } 3040 }; 3041 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); 3042 3043 int snd_hda_codec_build_controls(struct hda_codec *codec) 3044 { 3045 int err = 0; 3046 hda_exec_init_verbs(codec); 3047 /* continue to initialize... */ 3048 if (codec->patch_ops.init) 3049 err = codec->patch_ops.init(codec); 3050 if (!err && codec->patch_ops.build_controls) 3051 err = codec->patch_ops.build_controls(codec); 3052 if (err < 0) 3053 return err; 3054 3055 /* we create chmaps here instead of build_pcms */ 3056 err = add_std_chmaps(codec); 3057 if (err < 0) 3058 return err; 3059 3060 if (codec->jackpoll_interval) 3061 hda_jackpoll_work(&codec->jackpoll_work.work); 3062 else 3063 snd_hda_jack_report_sync(codec); /* call at the last init point */ 3064 sync_power_up_states(codec); 3065 return 0; 3066 } 3067 EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls); 3068 3069 /* 3070 * PCM stuff 3071 */ 3072 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, 3073 struct hda_codec *codec, 3074 struct snd_pcm_substream *substream) 3075 { 3076 return 0; 3077 } 3078 3079 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, 3080 struct hda_codec *codec, 3081 unsigned int stream_tag, 3082 unsigned int format, 3083 struct snd_pcm_substream *substream) 3084 { 3085 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 3086 return 0; 3087 } 3088 3089 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, 3090 struct hda_codec *codec, 3091 struct snd_pcm_substream *substream) 3092 { 3093 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 3094 return 0; 3095 } 3096 3097 static int set_pcm_default_values(struct hda_codec *codec, 3098 struct hda_pcm_stream *info) 3099 { 3100 int err; 3101 3102 /* query support PCM information from the given NID */ 3103 if (info->nid && (!info->rates || !info->formats)) { 3104 err = snd_hda_query_supported_pcm(codec, info->nid, 3105 info->rates ? NULL : &info->rates, 3106 info->formats ? NULL : &info->formats, 3107 info->maxbps ? NULL : &info->maxbps); 3108 if (err < 0) 3109 return err; 3110 } 3111 if (info->ops.open == NULL) 3112 info->ops.open = hda_pcm_default_open_close; 3113 if (info->ops.close == NULL) 3114 info->ops.close = hda_pcm_default_open_close; 3115 if (info->ops.prepare == NULL) { 3116 if (snd_BUG_ON(!info->nid)) 3117 return -EINVAL; 3118 info->ops.prepare = hda_pcm_default_prepare; 3119 } 3120 if (info->ops.cleanup == NULL) { 3121 if (snd_BUG_ON(!info->nid)) 3122 return -EINVAL; 3123 info->ops.cleanup = hda_pcm_default_cleanup; 3124 } 3125 return 0; 3126 } 3127 3128 /* 3129 * codec prepare/cleanup entries 3130 */ 3131 /** 3132 * snd_hda_codec_prepare - Prepare a stream 3133 * @codec: the HDA codec 3134 * @hinfo: PCM information 3135 * @stream: stream tag to assign 3136 * @format: format id to assign 3137 * @substream: PCM substream to assign 3138 * 3139 * Calls the prepare callback set by the codec with the given arguments. 3140 * Clean up the inactive streams when successful. 3141 */ 3142 int snd_hda_codec_prepare(struct hda_codec *codec, 3143 struct hda_pcm_stream *hinfo, 3144 unsigned int stream, 3145 unsigned int format, 3146 struct snd_pcm_substream *substream) 3147 { 3148 int ret; 3149 mutex_lock(&codec->bus->prepare_mutex); 3150 if (hinfo->ops.prepare) 3151 ret = hinfo->ops.prepare(hinfo, codec, stream, format, 3152 substream); 3153 else 3154 ret = -ENODEV; 3155 if (ret >= 0) 3156 purify_inactive_streams(codec); 3157 mutex_unlock(&codec->bus->prepare_mutex); 3158 return ret; 3159 } 3160 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); 3161 3162 /** 3163 * snd_hda_codec_cleanup - Prepare a stream 3164 * @codec: the HDA codec 3165 * @hinfo: PCM information 3166 * @substream: PCM substream 3167 * 3168 * Calls the cleanup callback set by the codec with the given arguments. 3169 */ 3170 void snd_hda_codec_cleanup(struct hda_codec *codec, 3171 struct hda_pcm_stream *hinfo, 3172 struct snd_pcm_substream *substream) 3173 { 3174 mutex_lock(&codec->bus->prepare_mutex); 3175 if (hinfo->ops.cleanup) 3176 hinfo->ops.cleanup(hinfo, codec, substream); 3177 mutex_unlock(&codec->bus->prepare_mutex); 3178 } 3179 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); 3180 3181 /* global */ 3182 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { 3183 "Audio", "SPDIF", "HDMI", "Modem" 3184 }; 3185 3186 /* 3187 * get the empty PCM device number to assign 3188 */ 3189 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) 3190 { 3191 /* audio device indices; not linear to keep compatibility */ 3192 /* assigned to static slots up to dev#10; if more needed, assign 3193 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) 3194 */ 3195 static int audio_idx[HDA_PCM_NTYPES][5] = { 3196 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, 3197 [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, 3198 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, 3199 [HDA_PCM_TYPE_MODEM] = { 6, -1 }, 3200 }; 3201 int i; 3202 3203 if (type >= HDA_PCM_NTYPES) { 3204 dev_err(bus->card->dev, "Invalid PCM type %d\n", type); 3205 return -EINVAL; 3206 } 3207 3208 for (i = 0; audio_idx[type][i] >= 0; i++) { 3209 #ifndef CONFIG_SND_DYNAMIC_MINORS 3210 if (audio_idx[type][i] >= 8) 3211 break; 3212 #endif 3213 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) 3214 return audio_idx[type][i]; 3215 } 3216 3217 #ifdef CONFIG_SND_DYNAMIC_MINORS 3218 /* non-fixed slots starting from 10 */ 3219 for (i = 10; i < 32; i++) { 3220 if (!test_and_set_bit(i, bus->pcm_dev_bits)) 3221 return i; 3222 } 3223 #endif 3224 3225 dev_warn(bus->card->dev, "Too many %s devices\n", 3226 snd_hda_pcm_type_name[type]); 3227 #ifndef CONFIG_SND_DYNAMIC_MINORS 3228 dev_warn(bus->card->dev, 3229 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); 3230 #endif 3231 return -EAGAIN; 3232 } 3233 3234 /* call build_pcms ops of the given codec and set up the default parameters */ 3235 int snd_hda_codec_parse_pcms(struct hda_codec *codec) 3236 { 3237 struct hda_pcm *cpcm; 3238 int err; 3239 3240 if (!list_empty(&codec->pcm_list_head)) 3241 return 0; /* already parsed */ 3242 3243 if (!codec->patch_ops.build_pcms) 3244 return 0; 3245 3246 err = codec->patch_ops.build_pcms(codec); 3247 if (err < 0) { 3248 codec_err(codec, "cannot build PCMs for #%d (error %d)\n", 3249 codec->core.addr, err); 3250 return err; 3251 } 3252 3253 list_for_each_entry(cpcm, &codec->pcm_list_head, list) { 3254 int stream; 3255 3256 for (stream = 0; stream < 2; stream++) { 3257 struct hda_pcm_stream *info = &cpcm->stream[stream]; 3258 3259 if (!info->substreams) 3260 continue; 3261 err = set_pcm_default_values(codec, info); 3262 if (err < 0) { 3263 codec_warn(codec, 3264 "fail to setup default for PCM %s\n", 3265 cpcm->name); 3266 return err; 3267 } 3268 } 3269 } 3270 3271 return 0; 3272 } 3273 EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms); 3274 3275 /* assign all PCMs of the given codec */ 3276 int snd_hda_codec_build_pcms(struct hda_codec *codec) 3277 { 3278 struct hda_bus *bus = codec->bus; 3279 struct hda_pcm *cpcm; 3280 int dev, err; 3281 3282 err = snd_hda_codec_parse_pcms(codec); 3283 if (err < 0) 3284 return err; 3285 3286 /* attach a new PCM streams */ 3287 list_for_each_entry(cpcm, &codec->pcm_list_head, list) { 3288 if (cpcm->pcm) 3289 continue; /* already attached */ 3290 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) 3291 continue; /* no substreams assigned */ 3292 3293 dev = get_empty_pcm_device(bus, cpcm->pcm_type); 3294 if (dev < 0) { 3295 cpcm->device = SNDRV_PCM_INVALID_DEVICE; 3296 continue; /* no fatal error */ 3297 } 3298 cpcm->device = dev; 3299 err = snd_hda_attach_pcm_stream(bus, codec, cpcm); 3300 if (err < 0) { 3301 codec_err(codec, 3302 "cannot attach PCM stream %d for codec #%d\n", 3303 dev, codec->core.addr); 3304 continue; /* no fatal error */ 3305 } 3306 } 3307 3308 return 0; 3309 } 3310 3311 /** 3312 * snd_hda_add_new_ctls - create controls from the array 3313 * @codec: the HDA codec 3314 * @knew: the array of struct snd_kcontrol_new 3315 * 3316 * This helper function creates and add new controls in the given array. 3317 * The array must be terminated with an empty entry as terminator. 3318 * 3319 * Returns 0 if successful, or a negative error code. 3320 */ 3321 int snd_hda_add_new_ctls(struct hda_codec *codec, 3322 const struct snd_kcontrol_new *knew) 3323 { 3324 int err; 3325 3326 for (; knew->name; knew++) { 3327 struct snd_kcontrol *kctl; 3328 int addr = 0, idx = 0; 3329 if (knew->iface == (__force snd_ctl_elem_iface_t)-1) 3330 continue; /* skip this codec private value */ 3331 for (;;) { 3332 kctl = snd_ctl_new1(knew, codec); 3333 if (!kctl) 3334 return -ENOMEM; 3335 if (addr > 0) 3336 kctl->id.device = addr; 3337 if (idx > 0) 3338 kctl->id.index = idx; 3339 err = snd_hda_ctl_add(codec, 0, kctl); 3340 if (!err) 3341 break; 3342 /* try first with another device index corresponding to 3343 * the codec addr; if it still fails (or it's the 3344 * primary codec), then try another control index 3345 */ 3346 if (!addr && codec->core.addr) 3347 addr = codec->core.addr; 3348 else if (!idx && !knew->index) { 3349 idx = find_empty_mixer_ctl_idx(codec, 3350 knew->name, 0); 3351 if (idx <= 0) 3352 return err; 3353 } else 3354 return err; 3355 } 3356 } 3357 return 0; 3358 } 3359 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); 3360 3361 #ifdef CONFIG_PM 3362 static void codec_set_power_save(struct hda_codec *codec, int delay) 3363 { 3364 struct device *dev = hda_codec_dev(codec); 3365 3366 if (delay == 0 && codec->auto_runtime_pm) 3367 delay = 3000; 3368 3369 if (delay > 0) { 3370 pm_runtime_set_autosuspend_delay(dev, delay); 3371 pm_runtime_use_autosuspend(dev); 3372 pm_runtime_allow(dev); 3373 if (!pm_runtime_suspended(dev)) 3374 pm_runtime_mark_last_busy(dev); 3375 } else { 3376 pm_runtime_dont_use_autosuspend(dev); 3377 pm_runtime_forbid(dev); 3378 } 3379 } 3380 3381 /** 3382 * snd_hda_set_power_save - reprogram autosuspend for the given delay 3383 * @bus: HD-audio bus 3384 * @delay: autosuspend delay in msec, 0 = off 3385 * 3386 * Synchronize the runtime PM autosuspend state from the power_save option. 3387 */ 3388 void snd_hda_set_power_save(struct hda_bus *bus, int delay) 3389 { 3390 struct hda_codec *c; 3391 3392 list_for_each_codec(c, bus) 3393 codec_set_power_save(c, delay); 3394 } 3395 EXPORT_SYMBOL_GPL(snd_hda_set_power_save); 3396 3397 /** 3398 * snd_hda_check_amp_list_power - Check the amp list and update the power 3399 * @codec: HD-audio codec 3400 * @check: the object containing an AMP list and the status 3401 * @nid: NID to check / update 3402 * 3403 * Check whether the given NID is in the amp list. If it's in the list, 3404 * check the current AMP status, and update the the power-status according 3405 * to the mute status. 3406 * 3407 * This function is supposed to be set or called from the check_power_status 3408 * patch ops. 3409 */ 3410 int snd_hda_check_amp_list_power(struct hda_codec *codec, 3411 struct hda_loopback_check *check, 3412 hda_nid_t nid) 3413 { 3414 const struct hda_amp_list *p; 3415 int ch, v; 3416 3417 if (!check->amplist) 3418 return 0; 3419 for (p = check->amplist; p->nid; p++) { 3420 if (p->nid == nid) 3421 break; 3422 } 3423 if (!p->nid) 3424 return 0; /* nothing changed */ 3425 3426 for (p = check->amplist; p->nid; p++) { 3427 for (ch = 0; ch < 2; ch++) { 3428 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, 3429 p->idx); 3430 if (!(v & HDA_AMP_MUTE) && v > 0) { 3431 if (!check->power_on) { 3432 check->power_on = 1; 3433 snd_hda_power_up_pm(codec); 3434 } 3435 return 1; 3436 } 3437 } 3438 } 3439 if (check->power_on) { 3440 check->power_on = 0; 3441 snd_hda_power_down_pm(codec); 3442 } 3443 return 0; 3444 } 3445 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); 3446 #endif 3447 3448 /* 3449 * input MUX helper 3450 */ 3451 3452 /** 3453 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum 3454 * @imux: imux helper object 3455 * @uinfo: pointer to get/store the data 3456 */ 3457 int snd_hda_input_mux_info(const struct hda_input_mux *imux, 3458 struct snd_ctl_elem_info *uinfo) 3459 { 3460 unsigned int index; 3461 3462 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 3463 uinfo->count = 1; 3464 uinfo->value.enumerated.items = imux->num_items; 3465 if (!imux->num_items) 3466 return 0; 3467 index = uinfo->value.enumerated.item; 3468 if (index >= imux->num_items) 3469 index = imux->num_items - 1; 3470 strcpy(uinfo->value.enumerated.name, imux->items[index].label); 3471 return 0; 3472 } 3473 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); 3474 3475 /** 3476 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum 3477 * @codec: the HDA codec 3478 * @imux: imux helper object 3479 * @ucontrol: pointer to get/store the data 3480 * @nid: input mux NID 3481 * @cur_val: pointer to get/store the current imux value 3482 */ 3483 int snd_hda_input_mux_put(struct hda_codec *codec, 3484 const struct hda_input_mux *imux, 3485 struct snd_ctl_elem_value *ucontrol, 3486 hda_nid_t nid, 3487 unsigned int *cur_val) 3488 { 3489 unsigned int idx; 3490 3491 if (!imux->num_items) 3492 return 0; 3493 idx = ucontrol->value.enumerated.item[0]; 3494 if (idx >= imux->num_items) 3495 idx = imux->num_items - 1; 3496 if (*cur_val == idx) 3497 return 0; 3498 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, 3499 imux->items[idx].index); 3500 *cur_val = idx; 3501 return 1; 3502 } 3503 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); 3504 3505 3506 /** 3507 * snd_hda_enum_helper_info - Helper for simple enum ctls 3508 * @kcontrol: ctl element 3509 * @uinfo: pointer to get/store the data 3510 * @num_items: number of enum items 3511 * @texts: enum item string array 3512 * 3513 * process kcontrol info callback of a simple string enum array 3514 * when @num_items is 0 or @texts is NULL, assume a boolean enum array 3515 */ 3516 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, 3517 struct snd_ctl_elem_info *uinfo, 3518 int num_items, const char * const *texts) 3519 { 3520 static const char * const texts_default[] = { 3521 "Disabled", "Enabled" 3522 }; 3523 3524 if (!texts || !num_items) { 3525 num_items = 2; 3526 texts = texts_default; 3527 } 3528 3529 return snd_ctl_enum_info(uinfo, 1, num_items, texts); 3530 } 3531 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); 3532 3533 /* 3534 * Multi-channel / digital-out PCM helper functions 3535 */ 3536 3537 /* setup SPDIF output stream */ 3538 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, 3539 unsigned int stream_tag, unsigned int format) 3540 { 3541 struct hda_spdif_out *spdif; 3542 unsigned int curr_fmt; 3543 bool reset; 3544 3545 spdif = snd_hda_spdif_out_of_nid(codec, nid); 3546 /* Add sanity check to pass klockwork check. 3547 * This should never happen. 3548 */ 3549 if (WARN_ON(spdif == NULL)) 3550 return; 3551 3552 curr_fmt = snd_hda_codec_read(codec, nid, 0, 3553 AC_VERB_GET_STREAM_FORMAT, 0); 3554 reset = codec->spdif_status_reset && 3555 (spdif->ctls & AC_DIG1_ENABLE) && 3556 curr_fmt != format; 3557 3558 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be 3559 updated */ 3560 if (reset) 3561 set_dig_out_convert(codec, nid, 3562 spdif->ctls & ~AC_DIG1_ENABLE & 0xff, 3563 -1); 3564 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); 3565 if (codec->slave_dig_outs) { 3566 const hda_nid_t *d; 3567 for (d = codec->slave_dig_outs; *d; d++) 3568 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, 3569 format); 3570 } 3571 /* turn on again (if needed) */ 3572 if (reset) 3573 set_dig_out_convert(codec, nid, 3574 spdif->ctls & 0xff, -1); 3575 } 3576 3577 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) 3578 { 3579 snd_hda_codec_cleanup_stream(codec, nid); 3580 if (codec->slave_dig_outs) { 3581 const hda_nid_t *d; 3582 for (d = codec->slave_dig_outs; *d; d++) 3583 snd_hda_codec_cleanup_stream(codec, *d); 3584 } 3585 } 3586 3587 /** 3588 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode 3589 * @codec: the HDA codec 3590 * @mout: hda_multi_out object 3591 */ 3592 int snd_hda_multi_out_dig_open(struct hda_codec *codec, 3593 struct hda_multi_out *mout) 3594 { 3595 mutex_lock(&codec->spdif_mutex); 3596 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) 3597 /* already opened as analog dup; reset it once */ 3598 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3599 mout->dig_out_used = HDA_DIG_EXCLUSIVE; 3600 mutex_unlock(&codec->spdif_mutex); 3601 return 0; 3602 } 3603 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); 3604 3605 /** 3606 * snd_hda_multi_out_dig_prepare - prepare the digital out stream 3607 * @codec: the HDA codec 3608 * @mout: hda_multi_out object 3609 * @stream_tag: stream tag to assign 3610 * @format: format id to assign 3611 * @substream: PCM substream to assign 3612 */ 3613 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, 3614 struct hda_multi_out *mout, 3615 unsigned int stream_tag, 3616 unsigned int format, 3617 struct snd_pcm_substream *substream) 3618 { 3619 mutex_lock(&codec->spdif_mutex); 3620 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); 3621 mutex_unlock(&codec->spdif_mutex); 3622 return 0; 3623 } 3624 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); 3625 3626 /** 3627 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream 3628 * @codec: the HDA codec 3629 * @mout: hda_multi_out object 3630 */ 3631 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, 3632 struct hda_multi_out *mout) 3633 { 3634 mutex_lock(&codec->spdif_mutex); 3635 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3636 mutex_unlock(&codec->spdif_mutex); 3637 return 0; 3638 } 3639 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); 3640 3641 /** 3642 * snd_hda_multi_out_dig_close - release the digital out stream 3643 * @codec: the HDA codec 3644 * @mout: hda_multi_out object 3645 */ 3646 int snd_hda_multi_out_dig_close(struct hda_codec *codec, 3647 struct hda_multi_out *mout) 3648 { 3649 mutex_lock(&codec->spdif_mutex); 3650 mout->dig_out_used = 0; 3651 mutex_unlock(&codec->spdif_mutex); 3652 return 0; 3653 } 3654 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); 3655 3656 /** 3657 * snd_hda_multi_out_analog_open - open analog outputs 3658 * @codec: the HDA codec 3659 * @mout: hda_multi_out object 3660 * @substream: PCM substream to assign 3661 * @hinfo: PCM information to assign 3662 * 3663 * Open analog outputs and set up the hw-constraints. 3664 * If the digital outputs can be opened as slave, open the digital 3665 * outputs, too. 3666 */ 3667 int snd_hda_multi_out_analog_open(struct hda_codec *codec, 3668 struct hda_multi_out *mout, 3669 struct snd_pcm_substream *substream, 3670 struct hda_pcm_stream *hinfo) 3671 { 3672 struct snd_pcm_runtime *runtime = substream->runtime; 3673 runtime->hw.channels_max = mout->max_channels; 3674 if (mout->dig_out_nid) { 3675 if (!mout->analog_rates) { 3676 mout->analog_rates = hinfo->rates; 3677 mout->analog_formats = hinfo->formats; 3678 mout->analog_maxbps = hinfo->maxbps; 3679 } else { 3680 runtime->hw.rates = mout->analog_rates; 3681 runtime->hw.formats = mout->analog_formats; 3682 hinfo->maxbps = mout->analog_maxbps; 3683 } 3684 if (!mout->spdif_rates) { 3685 snd_hda_query_supported_pcm(codec, mout->dig_out_nid, 3686 &mout->spdif_rates, 3687 &mout->spdif_formats, 3688 &mout->spdif_maxbps); 3689 } 3690 mutex_lock(&codec->spdif_mutex); 3691 if (mout->share_spdif) { 3692 if ((runtime->hw.rates & mout->spdif_rates) && 3693 (runtime->hw.formats & mout->spdif_formats)) { 3694 runtime->hw.rates &= mout->spdif_rates; 3695 runtime->hw.formats &= mout->spdif_formats; 3696 if (mout->spdif_maxbps < hinfo->maxbps) 3697 hinfo->maxbps = mout->spdif_maxbps; 3698 } else { 3699 mout->share_spdif = 0; 3700 /* FIXME: need notify? */ 3701 } 3702 } 3703 mutex_unlock(&codec->spdif_mutex); 3704 } 3705 return snd_pcm_hw_constraint_step(substream->runtime, 0, 3706 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 3707 } 3708 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); 3709 3710 /** 3711 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. 3712 * @codec: the HDA codec 3713 * @mout: hda_multi_out object 3714 * @stream_tag: stream tag to assign 3715 * @format: format id to assign 3716 * @substream: PCM substream to assign 3717 * 3718 * Set up the i/o for analog out. 3719 * When the digital out is available, copy the front out to digital out, too. 3720 */ 3721 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, 3722 struct hda_multi_out *mout, 3723 unsigned int stream_tag, 3724 unsigned int format, 3725 struct snd_pcm_substream *substream) 3726 { 3727 const hda_nid_t *nids = mout->dac_nids; 3728 int chs = substream->runtime->channels; 3729 struct hda_spdif_out *spdif; 3730 int i; 3731 3732 mutex_lock(&codec->spdif_mutex); 3733 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); 3734 if (mout->dig_out_nid && mout->share_spdif && 3735 mout->dig_out_used != HDA_DIG_EXCLUSIVE) { 3736 if (chs == 2 && spdif != NULL && 3737 snd_hda_is_supported_format(codec, mout->dig_out_nid, 3738 format) && 3739 !(spdif->status & IEC958_AES0_NONAUDIO)) { 3740 mout->dig_out_used = HDA_DIG_ANALOG_DUP; 3741 setup_dig_out_stream(codec, mout->dig_out_nid, 3742 stream_tag, format); 3743 } else { 3744 mout->dig_out_used = 0; 3745 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3746 } 3747 } 3748 mutex_unlock(&codec->spdif_mutex); 3749 3750 /* front */ 3751 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 3752 0, format); 3753 if (!mout->no_share_stream && 3754 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) 3755 /* headphone out will just decode front left/right (stereo) */ 3756 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 3757 0, format); 3758 /* extra outputs copied from front */ 3759 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 3760 if (!mout->no_share_stream && mout->hp_out_nid[i]) 3761 snd_hda_codec_setup_stream(codec, 3762 mout->hp_out_nid[i], 3763 stream_tag, 0, format); 3764 3765 /* surrounds */ 3766 for (i = 1; i < mout->num_dacs; i++) { 3767 if (chs >= (i + 1) * 2) /* independent out */ 3768 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 3769 i * 2, format); 3770 else if (!mout->no_share_stream) /* copy front */ 3771 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 3772 0, format); 3773 } 3774 3775 /* extra surrounds */ 3776 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { 3777 int ch = 0; 3778 if (!mout->extra_out_nid[i]) 3779 break; 3780 if (chs >= (i + 1) * 2) 3781 ch = i * 2; 3782 else if (!mout->no_share_stream) 3783 break; 3784 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], 3785 stream_tag, ch, format); 3786 } 3787 3788 return 0; 3789 } 3790 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); 3791 3792 /** 3793 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out 3794 * @codec: the HDA codec 3795 * @mout: hda_multi_out object 3796 */ 3797 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, 3798 struct hda_multi_out *mout) 3799 { 3800 const hda_nid_t *nids = mout->dac_nids; 3801 int i; 3802 3803 for (i = 0; i < mout->num_dacs; i++) 3804 snd_hda_codec_cleanup_stream(codec, nids[i]); 3805 if (mout->hp_nid) 3806 snd_hda_codec_cleanup_stream(codec, mout->hp_nid); 3807 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 3808 if (mout->hp_out_nid[i]) 3809 snd_hda_codec_cleanup_stream(codec, 3810 mout->hp_out_nid[i]); 3811 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) 3812 if (mout->extra_out_nid[i]) 3813 snd_hda_codec_cleanup_stream(codec, 3814 mout->extra_out_nid[i]); 3815 mutex_lock(&codec->spdif_mutex); 3816 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { 3817 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3818 mout->dig_out_used = 0; 3819 } 3820 mutex_unlock(&codec->spdif_mutex); 3821 return 0; 3822 } 3823 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); 3824 3825 /** 3826 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits 3827 * @codec: the HDA codec 3828 * @pin: referred pin NID 3829 * 3830 * Guess the suitable VREF pin bits to be set as the pin-control value. 3831 * Note: the function doesn't set the AC_PINCTL_IN_EN bit. 3832 */ 3833 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) 3834 { 3835 unsigned int pincap; 3836 unsigned int oldval; 3837 oldval = snd_hda_codec_read(codec, pin, 0, 3838 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 3839 pincap = snd_hda_query_pin_caps(codec, pin); 3840 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 3841 /* Exception: if the default pin setup is vref50, we give it priority */ 3842 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) 3843 return AC_PINCTL_VREF_80; 3844 else if (pincap & AC_PINCAP_VREF_50) 3845 return AC_PINCTL_VREF_50; 3846 else if (pincap & AC_PINCAP_VREF_100) 3847 return AC_PINCTL_VREF_100; 3848 else if (pincap & AC_PINCAP_VREF_GRD) 3849 return AC_PINCTL_VREF_GRD; 3850 return AC_PINCTL_VREF_HIZ; 3851 } 3852 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); 3853 3854 /** 3855 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap 3856 * @codec: the HDA codec 3857 * @pin: referred pin NID 3858 * @val: pin ctl value to audit 3859 */ 3860 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, 3861 hda_nid_t pin, unsigned int val) 3862 { 3863 static unsigned int cap_lists[][2] = { 3864 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, 3865 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, 3866 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, 3867 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, 3868 }; 3869 unsigned int cap; 3870 3871 if (!val) 3872 return 0; 3873 cap = snd_hda_query_pin_caps(codec, pin); 3874 if (!cap) 3875 return val; /* don't know what to do... */ 3876 3877 if (val & AC_PINCTL_OUT_EN) { 3878 if (!(cap & AC_PINCAP_OUT)) 3879 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); 3880 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) 3881 val &= ~AC_PINCTL_HP_EN; 3882 } 3883 3884 if (val & AC_PINCTL_IN_EN) { 3885 if (!(cap & AC_PINCAP_IN)) 3886 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); 3887 else { 3888 unsigned int vcap, vref; 3889 int i; 3890 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 3891 vref = val & AC_PINCTL_VREFEN; 3892 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { 3893 if (vref == cap_lists[i][0] && 3894 !(vcap & cap_lists[i][1])) { 3895 if (i == ARRAY_SIZE(cap_lists) - 1) 3896 vref = AC_PINCTL_VREF_HIZ; 3897 else 3898 vref = cap_lists[i + 1][0]; 3899 } 3900 } 3901 val &= ~AC_PINCTL_VREFEN; 3902 val |= vref; 3903 } 3904 } 3905 3906 return val; 3907 } 3908 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); 3909 3910 /** 3911 * _snd_hda_pin_ctl - Helper to set pin ctl value 3912 * @codec: the HDA codec 3913 * @pin: referred pin NID 3914 * @val: pin control value to set 3915 * @cached: access over codec pinctl cache or direct write 3916 * 3917 * This function is a helper to set a pin ctl value more safely. 3918 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the 3919 * value in pin target array via snd_hda_codec_set_pin_target(), then 3920 * actually writes the value via either snd_hda_codec_write_cache() or 3921 * snd_hda_codec_write() depending on @cached flag. 3922 */ 3923 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, 3924 unsigned int val, bool cached) 3925 { 3926 val = snd_hda_correct_pin_ctl(codec, pin, val); 3927 snd_hda_codec_set_pin_target(codec, pin, val); 3928 if (cached) 3929 return snd_hda_codec_write_cache(codec, pin, 0, 3930 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 3931 else 3932 return snd_hda_codec_write(codec, pin, 0, 3933 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 3934 } 3935 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); 3936 3937 /** 3938 * snd_hda_add_imux_item - Add an item to input_mux 3939 * @codec: the HDA codec 3940 * @imux: imux helper object 3941 * @label: the name of imux item to assign 3942 * @index: index number of imux item to assign 3943 * @type_idx: pointer to store the resultant label index 3944 * 3945 * When the same label is used already in the existing items, the number 3946 * suffix is appended to the label. This label index number is stored 3947 * to type_idx when non-NULL pointer is given. 3948 */ 3949 int snd_hda_add_imux_item(struct hda_codec *codec, 3950 struct hda_input_mux *imux, const char *label, 3951 int index, int *type_idx) 3952 { 3953 int i, label_idx = 0; 3954 if (imux->num_items >= HDA_MAX_NUM_INPUTS) { 3955 codec_err(codec, "hda_codec: Too many imux items!\n"); 3956 return -EINVAL; 3957 } 3958 for (i = 0; i < imux->num_items; i++) { 3959 if (!strncmp(label, imux->items[i].label, strlen(label))) 3960 label_idx++; 3961 } 3962 if (type_idx) 3963 *type_idx = label_idx; 3964 if (label_idx > 0) 3965 snprintf(imux->items[imux->num_items].label, 3966 sizeof(imux->items[imux->num_items].label), 3967 "%s %d", label, label_idx); 3968 else 3969 strlcpy(imux->items[imux->num_items].label, label, 3970 sizeof(imux->items[imux->num_items].label)); 3971 imux->items[imux->num_items].index = index; 3972 imux->num_items++; 3973 return 0; 3974 } 3975 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); 3976 3977 /** 3978 * snd_hda_bus_reset_codecs - Reset the bus 3979 * @bus: HD-audio bus 3980 */ 3981 void snd_hda_bus_reset_codecs(struct hda_bus *bus) 3982 { 3983 struct hda_codec *codec; 3984 3985 list_for_each_codec(codec, bus) { 3986 /* FIXME: maybe a better way needed for forced reset */ 3987 if (current_work() != &codec->jackpoll_work.work) 3988 cancel_delayed_work_sync(&codec->jackpoll_work); 3989 #ifdef CONFIG_PM 3990 if (hda_codec_is_power_on(codec)) { 3991 hda_call_codec_suspend(codec); 3992 hda_call_codec_resume(codec); 3993 } 3994 #endif 3995 } 3996 } 3997 3998 /** 3999 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer 4000 * @pcm: PCM caps bits 4001 * @buf: the string buffer to write 4002 * @buflen: the max buffer length 4003 * 4004 * used by hda_proc.c and hda_eld.c 4005 */ 4006 void snd_print_pcm_bits(int pcm, char *buf, int buflen) 4007 { 4008 static unsigned int bits[] = { 8, 16, 20, 24, 32 }; 4009 int i, j; 4010 4011 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) 4012 if (pcm & (AC_SUPPCM_BITS_8 << i)) 4013 j += snprintf(buf + j, buflen - j, " %d", bits[i]); 4014 4015 buf[j] = '\0'; /* necessary when j == 0 */ 4016 } 4017 EXPORT_SYMBOL_GPL(snd_print_pcm_bits); 4018 4019 MODULE_DESCRIPTION("HDA codec core"); 4020 MODULE_LICENSE("GPL"); 4021