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