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