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