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 int old_status; 2281 unsigned short val; 2282 int change; 2283 2284 if (WARN_ON(codec->spdif_out.used <= idx)) 2285 return -EINVAL; 2286 guard(mutex)(&codec->spdif_mutex); 2287 spdif = snd_array_elem(&codec->spdif_out, idx); 2288 nid = spdif->nid; 2289 old_status = spdif->status; 2290 spdif->status = ucontrol->value.iec958.status[0] | 2291 ((unsigned int)ucontrol->value.iec958.status[1] << 8) | 2292 ((unsigned int)ucontrol->value.iec958.status[2] << 16) | 2293 ((unsigned int)ucontrol->value.iec958.status[3] << 24); 2294 val = convert_from_spdif_status(spdif->status); 2295 val |= spdif->ctls & 1; 2296 change = spdif->ctls != val; 2297 spdif->ctls = val; 2298 if (change && nid != (u16)-1) 2299 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); 2300 return change || spdif->status != old_status; 2301 } 2302 2303 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info 2304 2305 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, 2306 struct snd_ctl_elem_value *ucontrol) 2307 { 2308 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2309 int idx = kcontrol->private_value; 2310 struct hda_spdif_out *spdif; 2311 2312 if (WARN_ON(codec->spdif_out.used <= idx)) 2313 return -EINVAL; 2314 guard(mutex)(&codec->spdif_mutex); 2315 spdif = snd_array_elem(&codec->spdif_out, idx); 2316 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; 2317 return 0; 2318 } 2319 2320 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, 2321 int dig1, int dig2) 2322 { 2323 set_dig_out_convert(codec, nid, dig1, dig2); 2324 /* unmute amp switch (if any) */ 2325 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && 2326 (dig1 & AC_DIG1_ENABLE)) 2327 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, 2328 HDA_AMP_MUTE, 0); 2329 } 2330 2331 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, 2332 struct snd_ctl_elem_value *ucontrol) 2333 { 2334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2335 int idx = kcontrol->private_value; 2336 struct hda_spdif_out *spdif; 2337 hda_nid_t nid; 2338 unsigned short val; 2339 int change; 2340 2341 if (WARN_ON(codec->spdif_out.used <= idx)) 2342 return -EINVAL; 2343 guard(mutex)(&codec->spdif_mutex); 2344 spdif = snd_array_elem(&codec->spdif_out, idx); 2345 nid = spdif->nid; 2346 val = spdif->ctls & ~AC_DIG1_ENABLE; 2347 if (ucontrol->value.integer.value[0]) 2348 val |= AC_DIG1_ENABLE; 2349 change = spdif->ctls != val; 2350 spdif->ctls = val; 2351 if (change && nid != (u16)-1) 2352 set_spdif_ctls(codec, nid, val & 0xff, -1); 2353 return change; 2354 } 2355 2356 static const struct snd_kcontrol_new dig_mixes[] = { 2357 { 2358 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2360 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), 2361 .info = snd_hda_spdif_mask_info, 2362 .get = snd_hda_spdif_cmask_get, 2363 }, 2364 { 2365 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2367 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), 2368 .info = snd_hda_spdif_mask_info, 2369 .get = snd_hda_spdif_pmask_get, 2370 }, 2371 { 2372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2373 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 2374 .info = snd_hda_spdif_mask_info, 2375 .get = snd_hda_spdif_default_get, 2376 .put = snd_hda_spdif_default_put, 2377 }, 2378 { 2379 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2380 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), 2381 .info = snd_hda_spdif_out_switch_info, 2382 .get = snd_hda_spdif_out_switch_get, 2383 .put = snd_hda_spdif_out_switch_put, 2384 }, 2385 { } /* end */ 2386 }; 2387 2388 /** 2389 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls 2390 * @codec: the HDA codec 2391 * @associated_nid: NID that new ctls associated with 2392 * @cvt_nid: converter NID 2393 * @type: HDA_PCM_TYPE_* 2394 * Creates controls related with the digital output. 2395 * Called from each codec driver supporting the digital out. 2396 * 2397 * Returns 0 if successful, or a negative error code. 2398 */ 2399 int snd_hda_create_dig_out_ctls(struct hda_codec *codec, 2400 hda_nid_t associated_nid, 2401 hda_nid_t cvt_nid, 2402 int type) 2403 { 2404 int err; 2405 struct snd_kcontrol *kctl; 2406 const struct snd_kcontrol_new *dig_mix; 2407 int idx = 0; 2408 int val = 0; 2409 const int spdif_index = 16; 2410 struct hda_spdif_out *spdif; 2411 struct hda_bus *bus = codec->bus; 2412 2413 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && 2414 type == HDA_PCM_TYPE_SPDIF) { 2415 idx = spdif_index; 2416 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && 2417 type == HDA_PCM_TYPE_HDMI) { 2418 /* suppose a single SPDIF device */ 2419 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 2420 struct snd_ctl_elem_id id; 2421 2422 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); 2423 if (!kctl) 2424 break; 2425 id = kctl->id; 2426 id.index = spdif_index; 2427 err = snd_ctl_rename_id(codec->card, &kctl->id, &id); 2428 if (err < 0) 2429 return err; 2430 } 2431 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; 2432 } 2433 if (!bus->primary_dig_out_type) 2434 bus->primary_dig_out_type = type; 2435 2436 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); 2437 if (idx < 0) { 2438 codec_err(codec, "too many IEC958 outputs\n"); 2439 return -EBUSY; 2440 } 2441 spdif = snd_array_new(&codec->spdif_out); 2442 if (!spdif) 2443 return -ENOMEM; 2444 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 2445 kctl = snd_ctl_new1(dig_mix, codec); 2446 if (!kctl) 2447 return -ENOMEM; 2448 kctl->id.index = idx; 2449 kctl->private_value = codec->spdif_out.used - 1; 2450 err = snd_hda_ctl_add(codec, associated_nid, kctl); 2451 if (err < 0) 2452 return err; 2453 } 2454 spdif->nid = cvt_nid; 2455 snd_hdac_regmap_read(&codec->core, cvt_nid, 2456 AC_VERB_GET_DIGI_CONVERT_1, &val); 2457 spdif->ctls = val; 2458 spdif->status = convert_to_spdif_status(spdif->ctls); 2459 return 0; 2460 } 2461 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); 2462 2463 /** 2464 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID 2465 * @codec: the HDA codec 2466 * @nid: widget NID 2467 * 2468 * call within spdif_mutex lock 2469 */ 2470 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, 2471 hda_nid_t nid) 2472 { 2473 struct hda_spdif_out *spdif; 2474 int i; 2475 2476 snd_array_for_each(&codec->spdif_out, i, spdif) { 2477 if (spdif->nid == nid) 2478 return spdif; 2479 } 2480 return NULL; 2481 } 2482 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); 2483 2484 /** 2485 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl 2486 * @codec: the HDA codec 2487 * @idx: the SPDIF ctl index 2488 * 2489 * Unassign the widget from the given SPDIF control. 2490 */ 2491 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) 2492 { 2493 struct hda_spdif_out *spdif; 2494 2495 if (WARN_ON(codec->spdif_out.used <= idx)) 2496 return; 2497 guard(mutex)(&codec->spdif_mutex); 2498 spdif = snd_array_elem(&codec->spdif_out, idx); 2499 spdif->nid = (u16)-1; 2500 } 2501 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); 2502 2503 /** 2504 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID 2505 * @codec: the HDA codec 2506 * @idx: the SPDIF ctl idx 2507 * @nid: widget NID 2508 * 2509 * Assign the widget to the SPDIF control with the given index. 2510 */ 2511 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) 2512 { 2513 struct hda_spdif_out *spdif; 2514 unsigned short val; 2515 2516 if (WARN_ON(codec->spdif_out.used <= idx)) 2517 return; 2518 guard(mutex)(&codec->spdif_mutex); 2519 spdif = snd_array_elem(&codec->spdif_out, idx); 2520 if (spdif->nid != nid) { 2521 spdif->nid = nid; 2522 val = spdif->ctls; 2523 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); 2524 } 2525 } 2526 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); 2527 2528 /* 2529 * SPDIF sharing with analog output 2530 */ 2531 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, 2532 struct snd_ctl_elem_value *ucontrol) 2533 { 2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2535 struct hda_multi_out *mout = (void *)kcontrol->private_value; 2536 2537 guard(mutex)(&codec->spdif_mutex); 2538 ucontrol->value.integer.value[0] = mout->share_spdif; 2539 return 0; 2540 } 2541 2542 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, 2543 struct snd_ctl_elem_value *ucontrol) 2544 { 2545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2546 struct hda_multi_out *mout = (void *)kcontrol->private_value; 2547 bool val = !!ucontrol->value.integer.value[0]; 2548 int change; 2549 2550 guard(mutex)(&codec->spdif_mutex); 2551 change = mout->share_spdif != val; 2552 mout->share_spdif = val; 2553 return change; 2554 } 2555 2556 static const struct snd_kcontrol_new spdif_share_sw = { 2557 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2558 .name = "IEC958 Default PCM Playback Switch", 2559 .info = snd_ctl_boolean_mono_info, 2560 .get = spdif_share_sw_get, 2561 .put = spdif_share_sw_put, 2562 }; 2563 2564 static void notify_spdif_share_sw(struct hda_codec *codec, 2565 struct hda_multi_out *mout) 2566 { 2567 if (mout->share_spdif_kctl) 2568 snd_ctl_notify_one(codec->card, SNDRV_CTL_EVENT_MASK_VALUE, 2569 mout->share_spdif_kctl, 0); 2570 } 2571 2572 /** 2573 * snd_hda_create_spdif_share_sw - create Default PCM switch 2574 * @codec: the HDA codec 2575 * @mout: multi-out instance 2576 */ 2577 int snd_hda_create_spdif_share_sw(struct hda_codec *codec, 2578 struct hda_multi_out *mout) 2579 { 2580 struct snd_kcontrol *kctl; 2581 int err; 2582 2583 if (!mout->dig_out_nid) 2584 return 0; 2585 2586 kctl = snd_ctl_new1(&spdif_share_sw, codec); 2587 if (!kctl) 2588 return -ENOMEM; 2589 /* snd_ctl_new1() stores @codec in private_data; stash @mout in 2590 * private_value for the share-switch callbacks and cache the 2591 * assigned control for forced-disable notifications. 2592 */ 2593 kctl->private_value = (unsigned long)mout; 2594 err = snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); 2595 if (err < 0) 2596 return err; 2597 mout->share_spdif_kctl = kctl; 2598 return 0; 2599 } 2600 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); 2601 2602 /* 2603 * SPDIF input 2604 */ 2605 2606 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info 2607 2608 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, 2609 struct snd_ctl_elem_value *ucontrol) 2610 { 2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2612 2613 ucontrol->value.integer.value[0] = codec->spdif_in_enable; 2614 return 0; 2615 } 2616 2617 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, 2618 struct snd_ctl_elem_value *ucontrol) 2619 { 2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2621 hda_nid_t nid = kcontrol->private_value; 2622 unsigned int val = !!ucontrol->value.integer.value[0]; 2623 int change; 2624 2625 guard(mutex)(&codec->spdif_mutex); 2626 change = codec->spdif_in_enable != val; 2627 if (change) { 2628 codec->spdif_in_enable = val; 2629 snd_hdac_regmap_write(&codec->core, nid, 2630 AC_VERB_SET_DIGI_CONVERT_1, val); 2631 } 2632 return change; 2633 } 2634 2635 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, 2636 struct snd_ctl_elem_value *ucontrol) 2637 { 2638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2639 hda_nid_t nid = kcontrol->private_value; 2640 unsigned int val; 2641 unsigned int sbits; 2642 2643 snd_hdac_regmap_read(&codec->core, nid, 2644 AC_VERB_GET_DIGI_CONVERT_1, &val); 2645 sbits = convert_to_spdif_status(val); 2646 ucontrol->value.iec958.status[0] = sbits; 2647 ucontrol->value.iec958.status[1] = sbits >> 8; 2648 ucontrol->value.iec958.status[2] = sbits >> 16; 2649 ucontrol->value.iec958.status[3] = sbits >> 24; 2650 return 0; 2651 } 2652 2653 static const struct snd_kcontrol_new dig_in_ctls[] = { 2654 { 2655 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2656 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), 2657 .info = snd_hda_spdif_in_switch_info, 2658 .get = snd_hda_spdif_in_switch_get, 2659 .put = snd_hda_spdif_in_switch_put, 2660 }, 2661 { 2662 .access = SNDRV_CTL_ELEM_ACCESS_READ, 2663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2664 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), 2665 .info = snd_hda_spdif_mask_info, 2666 .get = snd_hda_spdif_in_status_get, 2667 }, 2668 { } /* end */ 2669 }; 2670 2671 /** 2672 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls 2673 * @codec: the HDA codec 2674 * @nid: audio in widget NID 2675 * 2676 * Creates controls related with the SPDIF input. 2677 * Called from each codec driver supporting the SPDIF in. 2678 * 2679 * Returns 0 if successful, or a negative error code. 2680 */ 2681 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) 2682 { 2683 int err; 2684 struct snd_kcontrol *kctl; 2685 const struct snd_kcontrol_new *dig_mix; 2686 int idx; 2687 2688 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); 2689 if (idx < 0) { 2690 codec_err(codec, "too many IEC958 inputs\n"); 2691 return -EBUSY; 2692 } 2693 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { 2694 kctl = snd_ctl_new1(dig_mix, codec); 2695 if (!kctl) 2696 return -ENOMEM; 2697 kctl->private_value = nid; 2698 err = snd_hda_ctl_add(codec, nid, kctl); 2699 if (err < 0) 2700 return err; 2701 } 2702 codec->spdif_in_enable = 2703 snd_hda_codec_read(codec, nid, 0, 2704 AC_VERB_GET_DIGI_CONVERT_1, 0) & 2705 AC_DIG1_ENABLE; 2706 return 0; 2707 } 2708 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); 2709 2710 /** 2711 * snd_hda_codec_set_power_to_all - Set the power state to all widgets 2712 * @codec: the HDA codec 2713 * @fg: function group (not used now) 2714 * @power_state: the power state to set (AC_PWRST_*) 2715 * 2716 * Set the given power state to all widgets that have the power control. 2717 * If the codec has power_filter set, it evaluates the power state and 2718 * filter out if it's unchanged as D3. 2719 */ 2720 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, 2721 unsigned int power_state) 2722 { 2723 hda_nid_t nid; 2724 2725 for_each_hda_codec_node(nid, codec) { 2726 unsigned int wcaps = get_wcaps(codec, nid); 2727 unsigned int state = power_state; 2728 if (!(wcaps & AC_WCAP_POWER)) 2729 continue; 2730 if (codec->power_filter) { 2731 state = codec->power_filter(codec, nid, power_state); 2732 if (state != power_state && power_state == AC_PWRST_D3) 2733 continue; 2734 } 2735 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, 2736 state); 2737 } 2738 } 2739 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); 2740 2741 /** 2742 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD 2743 * @codec: the HDA codec 2744 * @nid: widget NID 2745 * @power_state: power state to evalue 2746 * 2747 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set. 2748 * This can be used a codec power_filter callback. 2749 */ 2750 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, 2751 hda_nid_t nid, 2752 unsigned int power_state) 2753 { 2754 if (nid == codec->core.afg || nid == codec->core.mfg) 2755 return power_state; 2756 if (power_state == AC_PWRST_D3 && 2757 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && 2758 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { 2759 int eapd = snd_hda_codec_read(codec, nid, 0, 2760 AC_VERB_GET_EAPD_BTLENABLE, 0); 2761 if (eapd & 0x02) 2762 return AC_PWRST_D0; 2763 } 2764 return power_state; 2765 } 2766 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); 2767 2768 /* 2769 * set power state of the codec, and return the power state 2770 */ 2771 static unsigned int hda_set_power_state(struct hda_codec *codec, 2772 unsigned int power_state) 2773 { 2774 struct hda_codec_driver *driver = hda_codec_to_driver(codec); 2775 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg; 2776 int count; 2777 unsigned int state; 2778 int flags = 0; 2779 2780 /* this delay seems necessary to avoid click noise at power-down */ 2781 if (power_state == AC_PWRST_D3) { 2782 if (codec->depop_delay < 0) 2783 msleep(codec_has_epss(codec) ? 10 : 100); 2784 else if (codec->depop_delay > 0) 2785 msleep(codec->depop_delay); 2786 flags = HDA_RW_NO_RESPONSE_FALLBACK; 2787 } 2788 2789 /* repeat power states setting at most 10 times*/ 2790 for (count = 0; count < 10; count++) { 2791 /* might be called before binding to driver, too */ 2792 if (driver && driver->ops && driver->ops->set_power_state) 2793 driver->ops->set_power_state(codec, fg, power_state); 2794 else { 2795 state = power_state; 2796 if (codec->power_filter) 2797 state = codec->power_filter(codec, fg, state); 2798 if (state == power_state || power_state != AC_PWRST_D3) 2799 snd_hda_codec_write_sync(codec, fg, flags, 2800 AC_VERB_SET_POWER_STATE, 2801 state); 2802 snd_hda_codec_set_power_to_all(codec, fg, power_state); 2803 } 2804 state = snd_hda_sync_power_state(codec, fg, power_state); 2805 if (!(state & AC_PWRST_ERROR)) 2806 break; 2807 } 2808 2809 return state; 2810 } 2811 2812 /* sync power states of all widgets; 2813 * this is called at the end of codec parsing 2814 */ 2815 static void sync_power_up_states(struct hda_codec *codec) 2816 { 2817 hda_nid_t nid; 2818 2819 /* don't care if no filter is used */ 2820 if (!codec->power_filter) 2821 return; 2822 2823 for_each_hda_codec_node(nid, codec) { 2824 unsigned int wcaps = get_wcaps(codec, nid); 2825 unsigned int target; 2826 if (!(wcaps & AC_WCAP_POWER)) 2827 continue; 2828 target = codec->power_filter(codec, nid, AC_PWRST_D0); 2829 if (target == AC_PWRST_D0) 2830 continue; 2831 if (!snd_hda_check_power_state(codec, nid, target)) 2832 snd_hda_codec_write(codec, nid, 0, 2833 AC_VERB_SET_POWER_STATE, target); 2834 } 2835 } 2836 2837 #ifdef CONFIG_SND_HDA_RECONFIG 2838 /* execute additional init verbs */ 2839 static void hda_exec_init_verbs(struct hda_codec *codec) 2840 { 2841 if (codec->init_verbs.list) 2842 snd_hda_sequence_write(codec, codec->init_verbs.list); 2843 } 2844 #else 2845 static inline void hda_exec_init_verbs(struct hda_codec *codec) {} 2846 #endif 2847 2848 /* update the power on/off account with the current jiffies */ 2849 static void update_power_acct(struct hda_codec *codec, bool on) 2850 { 2851 unsigned long delta = jiffies - codec->power_jiffies; 2852 2853 if (on) 2854 codec->power_on_acct += delta; 2855 else 2856 codec->power_off_acct += delta; 2857 codec->power_jiffies += delta; 2858 } 2859 2860 void snd_hda_update_power_acct(struct hda_codec *codec) 2861 { 2862 update_power_acct(codec, hda_codec_is_power_on(codec)); 2863 } 2864 2865 /* 2866 * call suspend and power-down; used both from PM and power-save 2867 * this function returns the power state in the end 2868 */ 2869 static unsigned int hda_call_codec_suspend(struct hda_codec *codec) 2870 { 2871 struct hda_codec_driver *driver = hda_codec_to_driver(codec); 2872 unsigned int state; 2873 2874 snd_hdac_enter_pm(&codec->core); 2875 if (driver->ops->suspend) 2876 driver->ops->suspend(codec); 2877 if (!codec->no_stream_clean_at_suspend) 2878 hda_cleanup_all_streams(codec); 2879 state = hda_set_power_state(codec, AC_PWRST_D3); 2880 update_power_acct(codec, true); 2881 snd_hdac_leave_pm(&codec->core); 2882 return state; 2883 } 2884 2885 /* 2886 * kick up codec; used both from PM and power-save 2887 */ 2888 static void hda_call_codec_resume(struct hda_codec *codec) 2889 { 2890 struct hda_codec_driver *driver = hda_codec_to_driver(codec); 2891 2892 snd_hdac_enter_pm(&codec->core); 2893 if (codec->core.regmap) 2894 regcache_mark_dirty(codec->core.regmap); 2895 2896 codec->power_jiffies = jiffies; 2897 2898 hda_set_power_state(codec, AC_PWRST_D0); 2899 restore_shutup_pins(codec); 2900 hda_exec_init_verbs(codec); 2901 snd_hda_jack_set_dirty_all(codec); 2902 if (driver->ops->resume) 2903 driver->ops->resume(codec); 2904 else { 2905 snd_hda_codec_init(codec); 2906 snd_hda_regmap_sync(codec); 2907 } 2908 2909 snd_hda_jack_report_sync(codec); 2910 codec->core.dev.power.power_state = PMSG_ON; 2911 snd_hdac_leave_pm(&codec->core); 2912 if (codec->jackpoll_interval) 2913 schedule_delayed_work(&codec->jackpoll_work, 2914 codec->jackpoll_interval); 2915 } 2916 2917 static int hda_codec_runtime_suspend(struct device *dev) 2918 { 2919 struct hda_codec *codec = dev_to_hda_codec(dev); 2920 unsigned int state; 2921 2922 /* Nothing to do if card registration fails and the component driver never probes */ 2923 if (!codec->card) 2924 return 0; 2925 2926 state = hda_call_codec_suspend(codec); 2927 if (codec->link_down_at_suspend || 2928 (codec_has_clkstop(codec) && codec_has_epss(codec) && 2929 (state & AC_PWRST_CLK_STOP_OK))) 2930 snd_hdac_codec_link_down(&codec->core); 2931 snd_hda_codec_display_power(codec, false); 2932 2933 return 0; 2934 } 2935 2936 static int hda_codec_runtime_resume(struct device *dev) 2937 { 2938 struct hda_codec *codec = dev_to_hda_codec(dev); 2939 2940 /* Nothing to do if card registration fails and the component driver never probes */ 2941 if (!codec->card) 2942 return 0; 2943 2944 snd_hda_codec_display_power(codec, true); 2945 snd_hdac_codec_link_up(&codec->core); 2946 hda_call_codec_resume(codec); 2947 pm_runtime_mark_last_busy(dev); 2948 return 0; 2949 } 2950 2951 static int hda_codec_runtime_idle(struct device *dev) 2952 { 2953 struct hda_codec *codec = dev_to_hda_codec(dev); 2954 2955 if (codec->jackpoll_interval && !codec->bus->jackpoll_in_suspend) 2956 return -EBUSY; 2957 return 0; 2958 } 2959 2960 static int hda_codec_pm_prepare(struct device *dev) 2961 { 2962 struct hda_codec *codec = dev_to_hda_codec(dev); 2963 2964 cancel_delayed_work_sync(&codec->jackpoll_work); 2965 dev->power.power_state = PMSG_SUSPEND; 2966 return pm_runtime_suspended(dev); 2967 } 2968 2969 static void hda_codec_pm_complete(struct device *dev) 2970 { 2971 struct hda_codec *codec = dev_to_hda_codec(dev); 2972 2973 /* If no other pm-functions are called between prepare() and complete() */ 2974 if (dev->power.power_state.event == PM_EVENT_SUSPEND) 2975 dev->power.power_state = PMSG_RESUME; 2976 2977 if (pm_runtime_suspended(dev) && (codec->jackpoll_interval || 2978 hda_codec_need_resume(codec) || codec->forced_resume || 2979 codec->acomp_requested_resume)) { 2980 codec->acomp_requested_resume = 0; 2981 pm_request_resume(dev); 2982 } 2983 } 2984 2985 static int hda_codec_pm_suspend(struct device *dev) 2986 { 2987 dev->power.power_state = PMSG_SUSPEND; 2988 return pm_runtime_force_suspend(dev); 2989 } 2990 2991 static int hda_codec_pm_resume(struct device *dev) 2992 { 2993 dev->power.power_state = PMSG_RESUME; 2994 return pm_runtime_force_resume(dev); 2995 } 2996 2997 static int hda_codec_pm_freeze(struct device *dev) 2998 { 2999 struct hda_codec *codec = dev_to_hda_codec(dev); 3000 3001 cancel_delayed_work_sync(&codec->jackpoll_work); 3002 dev->power.power_state = PMSG_FREEZE; 3003 return pm_runtime_force_suspend(dev); 3004 } 3005 3006 static int hda_codec_pm_thaw(struct device *dev) 3007 { 3008 dev->power.power_state = PMSG_THAW; 3009 return pm_runtime_force_resume(dev); 3010 } 3011 3012 static int hda_codec_pm_restore(struct device *dev) 3013 { 3014 dev->power.power_state = PMSG_RESTORE; 3015 return pm_runtime_force_resume(dev); 3016 } 3017 3018 /* referred in hda_bind.c */ 3019 const struct dev_pm_ops hda_codec_driver_pm = { 3020 .prepare = pm_sleep_ptr(hda_codec_pm_prepare), 3021 .complete = pm_sleep_ptr(hda_codec_pm_complete), 3022 .suspend = pm_sleep_ptr(hda_codec_pm_suspend), 3023 .resume = pm_sleep_ptr(hda_codec_pm_resume), 3024 .freeze = pm_sleep_ptr(hda_codec_pm_freeze), 3025 .thaw = pm_sleep_ptr(hda_codec_pm_thaw), 3026 .poweroff = pm_sleep_ptr(hda_codec_pm_suspend), 3027 .restore = pm_sleep_ptr(hda_codec_pm_restore), 3028 RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume, 3029 hda_codec_runtime_idle) 3030 }; 3031 3032 /* suspend the codec at shutdown; called from driver's shutdown callback */ 3033 void snd_hda_codec_shutdown(struct hda_codec *codec) 3034 { 3035 struct hda_pcm *cpcm; 3036 3037 /* Skip the shutdown if codec is not registered */ 3038 if (!codec->core.registered) 3039 return; 3040 3041 codec->jackpoll_interval = 0; /* don't poll any longer */ 3042 cancel_delayed_work_sync(&codec->jackpoll_work); 3043 list_for_each_entry(cpcm, &codec->pcm_list_head, list) 3044 snd_pcm_suspend_all(cpcm->pcm); 3045 3046 pm_runtime_force_suspend(hda_codec_dev(codec)); 3047 pm_runtime_disable(hda_codec_dev(codec)); 3048 } 3049 3050 /* 3051 * add standard channel maps if not specified 3052 */ 3053 static int add_std_chmaps(struct hda_codec *codec) 3054 { 3055 struct hda_pcm *pcm; 3056 int str, err; 3057 3058 list_for_each_entry(pcm, &codec->pcm_list_head, list) { 3059 for (str = 0; str < 2; str++) { 3060 struct hda_pcm_stream *hinfo = &pcm->stream[str]; 3061 struct snd_pcm_chmap *chmap; 3062 const struct snd_pcm_chmap_elem *elem; 3063 3064 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams) 3065 continue; 3066 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; 3067 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem, 3068 hinfo->channels_max, 3069 0, &chmap); 3070 if (err < 0) 3071 return err; 3072 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; 3073 } 3074 } 3075 return 0; 3076 } 3077 3078 /* default channel maps for 2.1 speakers; 3079 * since HD-audio supports only stereo, odd number channels are omitted 3080 */ 3081 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { 3082 { .channels = 2, 3083 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, 3084 { .channels = 4, 3085 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 3086 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, 3087 { } 3088 }; 3089 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); 3090 3091 int snd_hda_codec_build_controls(struct hda_codec *codec) 3092 { 3093 struct hda_codec_driver *driver = hda_codec_to_driver(codec); 3094 int err; 3095 3096 hda_exec_init_verbs(codec); 3097 /* continue to initialize... */ 3098 err = snd_hda_codec_init(codec); 3099 if (err < 0) 3100 return err; 3101 3102 if (driver->ops->build_controls) { 3103 err = driver->ops->build_controls(codec); 3104 if (err < 0) 3105 return err; 3106 } 3107 3108 /* we create chmaps here instead of build_pcms */ 3109 err = add_std_chmaps(codec); 3110 if (err < 0) 3111 return err; 3112 3113 snd_hda_jack_report_sync(codec); /* call at the last init point */ 3114 if (codec->jackpoll_interval) 3115 schedule_delayed_work(&codec->jackpoll_work, 3116 codec->jackpoll_interval); 3117 3118 sync_power_up_states(codec); 3119 return 0; 3120 } 3121 EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls); 3122 3123 /* 3124 * PCM stuff 3125 */ 3126 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, 3127 struct hda_codec *codec, 3128 struct snd_pcm_substream *substream) 3129 { 3130 return 0; 3131 } 3132 3133 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, 3134 struct hda_codec *codec, 3135 unsigned int stream_tag, 3136 unsigned int format, 3137 struct snd_pcm_substream *substream) 3138 { 3139 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 3140 return 0; 3141 } 3142 3143 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, 3144 struct hda_codec *codec, 3145 struct snd_pcm_substream *substream) 3146 { 3147 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 3148 return 0; 3149 } 3150 3151 static int set_pcm_default_values(struct hda_codec *codec, 3152 struct hda_pcm_stream *info) 3153 { 3154 int err; 3155 3156 /* query support PCM information from the given NID */ 3157 if (info->nid && (!info->rates || !info->formats)) { 3158 err = snd_hda_query_supported_pcm(codec, info->nid, 3159 info->rates ? NULL : &info->rates, 3160 info->formats ? NULL : &info->formats, 3161 info->subformats ? NULL : &info->subformats, 3162 info->maxbps ? NULL : &info->maxbps); 3163 if (err < 0) 3164 return err; 3165 } 3166 if (info->ops.open == NULL) 3167 info->ops.open = hda_pcm_default_open_close; 3168 if (info->ops.close == NULL) 3169 info->ops.close = hda_pcm_default_open_close; 3170 if (info->ops.prepare == NULL) { 3171 if (snd_BUG_ON(!info->nid)) 3172 return -EINVAL; 3173 info->ops.prepare = hda_pcm_default_prepare; 3174 } 3175 if (info->ops.cleanup == NULL) { 3176 if (snd_BUG_ON(!info->nid)) 3177 return -EINVAL; 3178 info->ops.cleanup = hda_pcm_default_cleanup; 3179 } 3180 return 0; 3181 } 3182 3183 /* 3184 * codec prepare/cleanup entries 3185 */ 3186 /** 3187 * snd_hda_codec_prepare - Prepare a stream 3188 * @codec: the HDA codec 3189 * @hinfo: PCM information 3190 * @stream: stream tag to assign 3191 * @format: format id to assign 3192 * @substream: PCM substream to assign 3193 * 3194 * Calls the prepare callback set by the codec with the given arguments. 3195 * Clean up the inactive streams when successful. 3196 */ 3197 int snd_hda_codec_prepare(struct hda_codec *codec, 3198 struct hda_pcm_stream *hinfo, 3199 unsigned int stream, 3200 unsigned int format, 3201 struct snd_pcm_substream *substream) 3202 { 3203 int ret; 3204 3205 guard(mutex)(&codec->bus->prepare_mutex); 3206 if (hinfo->ops.prepare) 3207 ret = hinfo->ops.prepare(hinfo, codec, stream, format, 3208 substream); 3209 else 3210 ret = -ENODEV; 3211 if (ret >= 0) 3212 purify_inactive_streams(codec); 3213 return ret; 3214 } 3215 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); 3216 3217 /** 3218 * snd_hda_codec_cleanup - Clean up stream resources 3219 * @codec: the HDA codec 3220 * @hinfo: PCM information 3221 * @substream: PCM substream 3222 * 3223 * Calls the cleanup callback set by the codec with the given arguments. 3224 */ 3225 void snd_hda_codec_cleanup(struct hda_codec *codec, 3226 struct hda_pcm_stream *hinfo, 3227 struct snd_pcm_substream *substream) 3228 { 3229 guard(mutex)(&codec->bus->prepare_mutex); 3230 if (hinfo->ops.cleanup) 3231 hinfo->ops.cleanup(hinfo, codec, substream); 3232 } 3233 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); 3234 3235 /* global */ 3236 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { 3237 "Audio", "SPDIF", "HDMI", "Modem" 3238 }; 3239 3240 /* 3241 * get the empty PCM device number to assign 3242 */ 3243 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) 3244 { 3245 /* audio device indices; not linear to keep compatibility */ 3246 /* assigned to static slots up to dev#10; if more needed, assign 3247 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) 3248 */ 3249 static const int audio_idx[HDA_PCM_NTYPES][5] = { 3250 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, 3251 [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, 3252 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, 3253 [HDA_PCM_TYPE_MODEM] = { 6, -1 }, 3254 }; 3255 int i; 3256 3257 if (type >= HDA_PCM_NTYPES) { 3258 dev_err(bus->card->dev, "Invalid PCM type %d\n", type); 3259 return -EINVAL; 3260 } 3261 3262 for (i = 0; audio_idx[type][i] >= 0; i++) { 3263 #ifndef CONFIG_SND_DYNAMIC_MINORS 3264 if (audio_idx[type][i] >= 8) 3265 break; 3266 #endif 3267 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) 3268 return audio_idx[type][i]; 3269 } 3270 3271 #ifdef CONFIG_SND_DYNAMIC_MINORS 3272 /* non-fixed slots starting from 10 */ 3273 for (i = 10; i < 32; i++) { 3274 if (!test_and_set_bit(i, bus->pcm_dev_bits)) 3275 return i; 3276 } 3277 #endif 3278 3279 dev_warn(bus->card->dev, "Too many %s devices\n", 3280 snd_hda_pcm_type_name[type]); 3281 #ifndef CONFIG_SND_DYNAMIC_MINORS 3282 dev_warn(bus->card->dev, 3283 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); 3284 #endif 3285 return -EAGAIN; 3286 } 3287 3288 /* call build_pcms ops of the given codec and set up the default parameters */ 3289 int snd_hda_codec_parse_pcms(struct hda_codec *codec) 3290 { 3291 struct hda_codec_driver *driver = hda_codec_to_driver(codec); 3292 struct hda_pcm *cpcm; 3293 int err; 3294 3295 if (!list_empty(&codec->pcm_list_head)) 3296 return 0; /* already parsed */ 3297 3298 if (!driver->ops->build_pcms) 3299 return 0; 3300 3301 err = driver->ops->build_pcms(codec); 3302 if (err < 0) { 3303 codec_err(codec, "cannot build PCMs for #%d (error %d)\n", 3304 codec->core.addr, err); 3305 return err; 3306 } 3307 3308 list_for_each_entry(cpcm, &codec->pcm_list_head, list) { 3309 int stream; 3310 3311 for_each_pcm_streams(stream) { 3312 struct hda_pcm_stream *info = &cpcm->stream[stream]; 3313 3314 if (!info->substreams) 3315 continue; 3316 err = set_pcm_default_values(codec, info); 3317 if (err < 0) { 3318 codec_warn(codec, 3319 "fail to setup default for PCM %s\n", 3320 cpcm->name); 3321 return err; 3322 } 3323 } 3324 } 3325 3326 return 0; 3327 } 3328 EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms); 3329 3330 /* assign all PCMs of the given codec */ 3331 int snd_hda_codec_build_pcms(struct hda_codec *codec) 3332 { 3333 struct hda_bus *bus = codec->bus; 3334 struct hda_pcm *cpcm; 3335 int dev, err; 3336 3337 err = snd_hda_codec_parse_pcms(codec); 3338 if (err < 0) 3339 return err; 3340 3341 /* attach a new PCM streams */ 3342 list_for_each_entry(cpcm, &codec->pcm_list_head, list) { 3343 if (cpcm->pcm) 3344 continue; /* already attached */ 3345 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) 3346 continue; /* no substreams assigned */ 3347 3348 dev = get_empty_pcm_device(bus, cpcm->pcm_type); 3349 if (dev < 0) { 3350 cpcm->device = SNDRV_PCM_INVALID_DEVICE; 3351 continue; /* no fatal error */ 3352 } 3353 cpcm->device = dev; 3354 err = snd_hda_attach_pcm_stream(bus, codec, cpcm); 3355 if (err < 0) { 3356 codec_err(codec, 3357 "cannot attach PCM stream %d for codec #%d\n", 3358 dev, codec->core.addr); 3359 continue; /* no fatal error */ 3360 } 3361 } 3362 3363 return 0; 3364 } 3365 3366 /** 3367 * snd_hda_add_new_ctls - create controls from the array 3368 * @codec: the HDA codec 3369 * @knew: the array of struct snd_kcontrol_new 3370 * 3371 * This helper function creates and add new controls in the given array. 3372 * The array must be terminated with an empty entry as terminator. 3373 * 3374 * Returns 0 if successful, or a negative error code. 3375 */ 3376 int snd_hda_add_new_ctls(struct hda_codec *codec, 3377 const struct snd_kcontrol_new *knew) 3378 { 3379 int err; 3380 3381 for (; knew->name; knew++) { 3382 struct snd_kcontrol *kctl; 3383 int addr = 0, idx = 0; 3384 if (knew->iface == -1) 3385 continue; /* skip this codec private value */ 3386 for (;;) { 3387 kctl = snd_ctl_new1(knew, codec); 3388 if (!kctl) 3389 return -ENOMEM; 3390 /* Do not use the id.device field for MIXER elements. 3391 * This field is for real device numbers (like PCM) but codecs 3392 * are hidden components from the user space view (unrelated 3393 * to the mixer element identification). 3394 */ 3395 if (addr > 0 && codec->ctl_dev_id) 3396 kctl->id.device = addr; 3397 if (idx > 0) 3398 kctl->id.index = idx; 3399 err = snd_hda_ctl_add(codec, 0, kctl); 3400 if (!err) 3401 break; 3402 /* try first with another device index corresponding to 3403 * the codec addr; if it still fails (or it's the 3404 * primary codec), then try another control index 3405 */ 3406 if (!addr && codec->core.addr) { 3407 addr = codec->core.addr; 3408 if (!codec->ctl_dev_id) 3409 idx += 10 * addr; 3410 } else if (!idx && !knew->index) { 3411 idx = find_empty_mixer_ctl_idx(codec, 3412 knew->name, 0); 3413 if (idx <= 0) 3414 return err; 3415 } else 3416 return err; 3417 } 3418 } 3419 return 0; 3420 } 3421 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); 3422 3423 /** 3424 * snd_hda_codec_set_power_save - Configure codec's runtime PM 3425 * @codec: codec device to configure 3426 * @delay: autosuspend delay 3427 */ 3428 void snd_hda_codec_set_power_save(struct hda_codec *codec, int delay) 3429 { 3430 struct device *dev = hda_codec_dev(codec); 3431 3432 if (delay == 0 && codec->auto_runtime_pm) 3433 delay = 3000; 3434 3435 if (delay > 0) { 3436 pm_runtime_set_autosuspend_delay(dev, delay); 3437 pm_runtime_use_autosuspend(dev); 3438 pm_runtime_allow(dev); 3439 if (!pm_runtime_suspended(dev)) 3440 pm_runtime_mark_last_busy(dev); 3441 } else { 3442 pm_runtime_dont_use_autosuspend(dev); 3443 pm_runtime_forbid(dev); 3444 } 3445 } 3446 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_save); 3447 3448 /** 3449 * snd_hda_set_power_save - reprogram autosuspend for the given delay 3450 * @bus: HD-audio bus 3451 * @delay: autosuspend delay in msec, 0 = off 3452 * 3453 * Synchronize the runtime PM autosuspend state from the power_save option. 3454 */ 3455 void snd_hda_set_power_save(struct hda_bus *bus, int delay) 3456 { 3457 struct hda_codec *c; 3458 3459 list_for_each_codec(c, bus) 3460 snd_hda_codec_set_power_save(c, delay); 3461 } 3462 EXPORT_SYMBOL_GPL(snd_hda_set_power_save); 3463 3464 /** 3465 * snd_hda_check_amp_list_power - Check the amp list and update the power 3466 * @codec: HD-audio codec 3467 * @check: the object containing an AMP list and the status 3468 * @nid: NID to check / update 3469 * 3470 * Check whether the given NID is in the amp list. If it's in the list, 3471 * check the current AMP status, and update the power-status according 3472 * to the mute status. 3473 * 3474 * This function is supposed to be set or called from the check_power_status 3475 * patch ops. 3476 */ 3477 int snd_hda_check_amp_list_power(struct hda_codec *codec, 3478 struct hda_loopback_check *check, 3479 hda_nid_t nid) 3480 { 3481 const struct hda_amp_list *p; 3482 int ch, v; 3483 3484 if (!check->amplist) 3485 return 0; 3486 for (p = check->amplist; p->nid; p++) { 3487 if (p->nid == nid) 3488 break; 3489 } 3490 if (!p->nid) 3491 return 0; /* nothing changed */ 3492 3493 for (p = check->amplist; p->nid; p++) { 3494 for (ch = 0; ch < 2; ch++) { 3495 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, 3496 p->idx); 3497 if (!(v & HDA_AMP_MUTE) && v > 0) { 3498 if (!check->power_on) { 3499 check->power_on = 1; 3500 snd_hda_power_up_pm(codec); 3501 } 3502 return 1; 3503 } 3504 } 3505 } 3506 if (check->power_on) { 3507 check->power_on = 0; 3508 snd_hda_power_down_pm(codec); 3509 } 3510 return 0; 3511 } 3512 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); 3513 3514 /* 3515 * input MUX helper 3516 */ 3517 3518 /** 3519 * snd_hda_input_mux_info - Info callback helper for the input-mux enum 3520 * @imux: imux helper object 3521 * @uinfo: pointer to get/store the data 3522 */ 3523 int snd_hda_input_mux_info(const struct hda_input_mux *imux, 3524 struct snd_ctl_elem_info *uinfo) 3525 { 3526 unsigned int index; 3527 3528 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 3529 uinfo->count = 1; 3530 uinfo->value.enumerated.items = imux->num_items; 3531 if (!imux->num_items) 3532 return 0; 3533 index = uinfo->value.enumerated.item; 3534 if (index >= imux->num_items) 3535 index = imux->num_items - 1; 3536 strscpy(uinfo->value.enumerated.name, imux->items[index].label); 3537 return 0; 3538 } 3539 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); 3540 3541 /** 3542 * snd_hda_input_mux_put - Put callback helper for the input-mux enum 3543 * @codec: the HDA codec 3544 * @imux: imux helper object 3545 * @ucontrol: pointer to get/store the data 3546 * @nid: input mux NID 3547 * @cur_val: pointer to get/store the current imux value 3548 */ 3549 int snd_hda_input_mux_put(struct hda_codec *codec, 3550 const struct hda_input_mux *imux, 3551 struct snd_ctl_elem_value *ucontrol, 3552 hda_nid_t nid, 3553 unsigned int *cur_val) 3554 { 3555 unsigned int idx; 3556 3557 if (!imux->num_items) 3558 return 0; 3559 idx = ucontrol->value.enumerated.item[0]; 3560 if (idx >= imux->num_items) 3561 idx = imux->num_items - 1; 3562 if (*cur_val == idx) 3563 return 0; 3564 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, 3565 imux->items[idx].index); 3566 *cur_val = idx; 3567 return 1; 3568 } 3569 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); 3570 3571 3572 /** 3573 * snd_hda_enum_helper_info - Helper for simple enum ctls 3574 * @kcontrol: ctl element 3575 * @uinfo: pointer to get/store the data 3576 * @num_items: number of enum items 3577 * @texts: enum item string array 3578 * 3579 * process kcontrol info callback of a simple string enum array 3580 * when @num_items is 0 or @texts is NULL, assume a boolean enum array 3581 */ 3582 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, 3583 struct snd_ctl_elem_info *uinfo, 3584 int num_items, const char * const *texts) 3585 { 3586 static const char * const texts_default[] = { 3587 "Disabled", "Enabled" 3588 }; 3589 3590 if (!texts || !num_items) { 3591 num_items = 2; 3592 texts = texts_default; 3593 } 3594 3595 return snd_ctl_enum_info(uinfo, 1, num_items, texts); 3596 } 3597 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); 3598 3599 /* 3600 * Multi-channel / digital-out PCM helper functions 3601 */ 3602 3603 /* setup SPDIF output stream */ 3604 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, 3605 unsigned int stream_tag, unsigned int format) 3606 { 3607 struct hda_spdif_out *spdif; 3608 unsigned int curr_fmt; 3609 bool reset; 3610 3611 spdif = snd_hda_spdif_out_of_nid(codec, nid); 3612 /* Add sanity check to pass klockwork check. 3613 * This should never happen. 3614 */ 3615 if (WARN_ON(spdif == NULL)) 3616 return; 3617 3618 curr_fmt = snd_hda_codec_read(codec, nid, 0, 3619 AC_VERB_GET_STREAM_FORMAT, 0); 3620 reset = codec->spdif_status_reset && 3621 (spdif->ctls & AC_DIG1_ENABLE) && 3622 curr_fmt != format; 3623 3624 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be 3625 updated */ 3626 if (reset) 3627 set_dig_out_convert(codec, nid, 3628 spdif->ctls & ~AC_DIG1_ENABLE & 0xff, 3629 -1); 3630 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); 3631 if (codec->follower_dig_outs) { 3632 const hda_nid_t *d; 3633 for (d = codec->follower_dig_outs; *d; d++) 3634 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, 3635 format); 3636 } 3637 /* turn on again (if needed) */ 3638 if (reset) 3639 set_dig_out_convert(codec, nid, 3640 spdif->ctls & 0xff, -1); 3641 } 3642 3643 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) 3644 { 3645 snd_hda_codec_cleanup_stream(codec, nid); 3646 if (codec->follower_dig_outs) { 3647 const hda_nid_t *d; 3648 for (d = codec->follower_dig_outs; *d; d++) 3649 snd_hda_codec_cleanup_stream(codec, *d); 3650 } 3651 } 3652 3653 /** 3654 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode 3655 * @codec: the HDA codec 3656 * @mout: hda_multi_out object 3657 */ 3658 int snd_hda_multi_out_dig_open(struct hda_codec *codec, 3659 struct hda_multi_out *mout) 3660 { 3661 guard(mutex)(&codec->spdif_mutex); 3662 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) 3663 /* already opened as analog dup; reset it once */ 3664 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3665 mout->dig_out_used = HDA_DIG_EXCLUSIVE; 3666 return 0; 3667 } 3668 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); 3669 3670 /** 3671 * snd_hda_multi_out_dig_prepare - prepare the digital out stream 3672 * @codec: the HDA codec 3673 * @mout: hda_multi_out object 3674 * @stream_tag: stream tag to assign 3675 * @format: format id to assign 3676 * @substream: PCM substream to assign 3677 */ 3678 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, 3679 struct hda_multi_out *mout, 3680 unsigned int stream_tag, 3681 unsigned int format, 3682 struct snd_pcm_substream *substream) 3683 { 3684 guard(mutex)(&codec->spdif_mutex); 3685 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); 3686 return 0; 3687 } 3688 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); 3689 3690 /** 3691 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream 3692 * @codec: the HDA codec 3693 * @mout: hda_multi_out object 3694 */ 3695 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, 3696 struct hda_multi_out *mout) 3697 { 3698 guard(mutex)(&codec->spdif_mutex); 3699 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3700 return 0; 3701 } 3702 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); 3703 3704 /** 3705 * snd_hda_multi_out_dig_close - release the digital out stream 3706 * @codec: the HDA codec 3707 * @mout: hda_multi_out object 3708 */ 3709 int snd_hda_multi_out_dig_close(struct hda_codec *codec, 3710 struct hda_multi_out *mout) 3711 { 3712 guard(mutex)(&codec->spdif_mutex); 3713 mout->dig_out_used = 0; 3714 return 0; 3715 } 3716 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); 3717 3718 /** 3719 * snd_hda_multi_out_analog_open - open analog outputs 3720 * @codec: the HDA codec 3721 * @mout: hda_multi_out object 3722 * @substream: PCM substream to assign 3723 * @hinfo: PCM information to assign 3724 * 3725 * Open analog outputs and set up the hw-constraints. 3726 * If the digital outputs can be opened as follower, open the digital 3727 * outputs, too. 3728 */ 3729 int snd_hda_multi_out_analog_open(struct hda_codec *codec, 3730 struct hda_multi_out *mout, 3731 struct snd_pcm_substream *substream, 3732 struct hda_pcm_stream *hinfo) 3733 { 3734 struct snd_pcm_runtime *runtime = substream->runtime; 3735 bool notify_share_sw = false; 3736 3737 runtime->hw.channels_max = mout->max_channels; 3738 if (mout->dig_out_nid) { 3739 if (!mout->analog_rates) { 3740 mout->analog_rates = hinfo->rates; 3741 mout->analog_formats = hinfo->formats; 3742 mout->analog_maxbps = hinfo->maxbps; 3743 } else { 3744 runtime->hw.rates = mout->analog_rates; 3745 runtime->hw.formats = mout->analog_formats; 3746 hinfo->maxbps = mout->analog_maxbps; 3747 } 3748 if (!mout->spdif_rates) { 3749 snd_hda_query_supported_pcm(codec, mout->dig_out_nid, 3750 &mout->spdif_rates, 3751 &mout->spdif_formats, 3752 NULL, 3753 &mout->spdif_maxbps); 3754 } 3755 guard(mutex)(&codec->spdif_mutex); 3756 if (mout->share_spdif) { 3757 if ((runtime->hw.rates & mout->spdif_rates) && 3758 (runtime->hw.formats & mout->spdif_formats)) { 3759 runtime->hw.rates &= mout->spdif_rates; 3760 runtime->hw.formats &= mout->spdif_formats; 3761 if (mout->spdif_maxbps < hinfo->maxbps) 3762 hinfo->maxbps = mout->spdif_maxbps; 3763 } else { 3764 mout->share_spdif = 0; 3765 notify_share_sw = true; 3766 } 3767 } 3768 } 3769 if (notify_share_sw) 3770 notify_spdif_share_sw(codec, mout); 3771 return snd_pcm_hw_constraint_step(substream->runtime, 0, 3772 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 3773 } 3774 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); 3775 3776 /** 3777 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. 3778 * @codec: the HDA codec 3779 * @mout: hda_multi_out object 3780 * @stream_tag: stream tag to assign 3781 * @format: format id to assign 3782 * @substream: PCM substream to assign 3783 * 3784 * Set up the i/o for analog out. 3785 * When the digital out is available, copy the front out to digital out, too. 3786 */ 3787 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, 3788 struct hda_multi_out *mout, 3789 unsigned int stream_tag, 3790 unsigned int format, 3791 struct snd_pcm_substream *substream) 3792 { 3793 const hda_nid_t *nids = mout->dac_nids; 3794 int chs = substream->runtime->channels; 3795 struct hda_spdif_out *spdif; 3796 int i; 3797 3798 scoped_guard(mutex, &codec->spdif_mutex) { 3799 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); 3800 if (mout->dig_out_nid && mout->share_spdif && 3801 mout->dig_out_used != HDA_DIG_EXCLUSIVE) { 3802 if (chs == 2 && spdif != NULL && 3803 snd_hda_is_supported_format(codec, mout->dig_out_nid, 3804 format) && 3805 !(spdif->status & IEC958_AES0_NONAUDIO)) { 3806 mout->dig_out_used = HDA_DIG_ANALOG_DUP; 3807 setup_dig_out_stream(codec, mout->dig_out_nid, 3808 stream_tag, format); 3809 } else { 3810 mout->dig_out_used = 0; 3811 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3812 } 3813 } 3814 } 3815 3816 /* front */ 3817 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 3818 0, format); 3819 if (!mout->no_share_stream && 3820 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) 3821 /* headphone out will just decode front left/right (stereo) */ 3822 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 3823 0, format); 3824 /* extra outputs copied from front */ 3825 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 3826 if (!mout->no_share_stream && mout->hp_out_nid[i]) 3827 snd_hda_codec_setup_stream(codec, 3828 mout->hp_out_nid[i], 3829 stream_tag, 0, format); 3830 3831 /* surrounds */ 3832 for (i = 1; i < mout->num_dacs; i++) { 3833 if (chs >= (i + 1) * 2) /* independent out */ 3834 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 3835 i * 2, format); 3836 else if (!mout->no_share_stream) /* copy front */ 3837 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 3838 0, format); 3839 } 3840 3841 /* extra surrounds */ 3842 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { 3843 int ch = 0; 3844 if (!mout->extra_out_nid[i]) 3845 break; 3846 if (chs >= (i + 1) * 2) 3847 ch = i * 2; 3848 else if (!mout->no_share_stream) 3849 break; 3850 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], 3851 stream_tag, ch, format); 3852 } 3853 3854 return 0; 3855 } 3856 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); 3857 3858 /** 3859 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out 3860 * @codec: the HDA codec 3861 * @mout: hda_multi_out object 3862 */ 3863 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, 3864 struct hda_multi_out *mout) 3865 { 3866 const hda_nid_t *nids = mout->dac_nids; 3867 int i; 3868 3869 for (i = 0; i < mout->num_dacs; i++) 3870 snd_hda_codec_cleanup_stream(codec, nids[i]); 3871 if (mout->hp_nid) 3872 snd_hda_codec_cleanup_stream(codec, mout->hp_nid); 3873 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 3874 if (mout->hp_out_nid[i]) 3875 snd_hda_codec_cleanup_stream(codec, 3876 mout->hp_out_nid[i]); 3877 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) 3878 if (mout->extra_out_nid[i]) 3879 snd_hda_codec_cleanup_stream(codec, 3880 mout->extra_out_nid[i]); 3881 guard(mutex)(&codec->spdif_mutex); 3882 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { 3883 cleanup_dig_out_stream(codec, mout->dig_out_nid); 3884 mout->dig_out_used = 0; 3885 } 3886 return 0; 3887 } 3888 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); 3889 3890 /** 3891 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits 3892 * @codec: the HDA codec 3893 * @pin: referred pin NID 3894 * 3895 * Guess the suitable VREF pin bits to be set as the pin-control value. 3896 * Note: the function doesn't set the AC_PINCTL_IN_EN bit. 3897 */ 3898 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) 3899 { 3900 unsigned int pincap; 3901 unsigned int oldval; 3902 oldval = snd_hda_codec_read(codec, pin, 0, 3903 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 3904 pincap = snd_hda_query_pin_caps(codec, pin); 3905 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 3906 /* Exception: if the default pin setup is vref50, we give it priority */ 3907 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) 3908 return AC_PINCTL_VREF_80; 3909 else if (pincap & AC_PINCAP_VREF_50) 3910 return AC_PINCTL_VREF_50; 3911 else if (pincap & AC_PINCAP_VREF_100) 3912 return AC_PINCTL_VREF_100; 3913 else if (pincap & AC_PINCAP_VREF_GRD) 3914 return AC_PINCTL_VREF_GRD; 3915 return AC_PINCTL_VREF_HIZ; 3916 } 3917 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); 3918 3919 /** 3920 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap 3921 * @codec: the HDA codec 3922 * @pin: referred pin NID 3923 * @val: pin ctl value to audit 3924 */ 3925 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, 3926 hda_nid_t pin, unsigned int val) 3927 { 3928 static const unsigned int cap_lists[][2] = { 3929 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, 3930 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, 3931 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, 3932 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, 3933 }; 3934 unsigned int cap; 3935 3936 if (!val) 3937 return 0; 3938 cap = snd_hda_query_pin_caps(codec, pin); 3939 if (!cap) 3940 return val; /* don't know what to do... */ 3941 3942 if (val & AC_PINCTL_OUT_EN) { 3943 if (!(cap & AC_PINCAP_OUT)) 3944 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); 3945 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) 3946 val &= ~AC_PINCTL_HP_EN; 3947 } 3948 3949 if (val & AC_PINCTL_IN_EN) { 3950 if (!(cap & AC_PINCAP_IN)) 3951 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); 3952 else { 3953 unsigned int vcap, vref; 3954 int i; 3955 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 3956 vref = val & AC_PINCTL_VREFEN; 3957 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { 3958 if (vref == cap_lists[i][0] && 3959 !(vcap & cap_lists[i][1])) { 3960 if (i == ARRAY_SIZE(cap_lists) - 1) 3961 vref = AC_PINCTL_VREF_HIZ; 3962 else 3963 vref = cap_lists[i + 1][0]; 3964 } 3965 } 3966 val &= ~AC_PINCTL_VREFEN; 3967 val |= vref; 3968 } 3969 } 3970 3971 return val; 3972 } 3973 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); 3974 3975 /** 3976 * _snd_hda_set_pin_ctl - Helper to set pin ctl value 3977 * @codec: the HDA codec 3978 * @pin: referred pin NID 3979 * @val: pin control value to set 3980 * @cached: access over codec pinctl cache or direct write 3981 * 3982 * This function is a helper to set a pin ctl value more safely. 3983 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the 3984 * value in pin target array via snd_hda_codec_set_pin_target(), then 3985 * actually writes the value via either snd_hda_codec_write_cache() or 3986 * snd_hda_codec_write() depending on @cached flag. 3987 */ 3988 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, 3989 unsigned int val, bool cached) 3990 { 3991 val = snd_hda_correct_pin_ctl(codec, pin, val); 3992 snd_hda_codec_set_pin_target(codec, pin, val); 3993 if (cached) 3994 return snd_hda_codec_write_cache(codec, pin, 0, 3995 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 3996 else 3997 return snd_hda_codec_write(codec, pin, 0, 3998 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 3999 } 4000 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); 4001 4002 /** 4003 * snd_hda_add_imux_item - Add an item to input_mux 4004 * @codec: the HDA codec 4005 * @imux: imux helper object 4006 * @label: the name of imux item to assign 4007 * @index: index number of imux item to assign 4008 * @type_idx: pointer to store the resultant label index 4009 * 4010 * When the same label is used already in the existing items, the number 4011 * suffix is appended to the label. This label index number is stored 4012 * to type_idx when non-NULL pointer is given. 4013 */ 4014 int snd_hda_add_imux_item(struct hda_codec *codec, 4015 struct hda_input_mux *imux, const char *label, 4016 int index, int *type_idx) 4017 { 4018 int i, label_idx = 0; 4019 if (imux->num_items >= HDA_MAX_NUM_INPUTS) { 4020 codec_err(codec, "hda_codec: Too many imux items!\n"); 4021 return -EINVAL; 4022 } 4023 for (i = 0; i < imux->num_items; i++) { 4024 if (!strncmp(label, imux->items[i].label, strlen(label))) 4025 label_idx++; 4026 } 4027 if (type_idx) 4028 *type_idx = label_idx; 4029 if (label_idx > 0) 4030 snprintf(imux->items[imux->num_items].label, 4031 sizeof(imux->items[imux->num_items].label), 4032 "%s %d", label, label_idx); 4033 else 4034 strscpy(imux->items[imux->num_items].label, label, 4035 sizeof(imux->items[imux->num_items].label)); 4036 imux->items[imux->num_items].index = index; 4037 imux->num_items++; 4038 return 0; 4039 } 4040 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); 4041 4042 /** 4043 * snd_hda_bus_reset_codecs - Reset the bus 4044 * @bus: HD-audio bus 4045 */ 4046 void snd_hda_bus_reset_codecs(struct hda_bus *bus) 4047 { 4048 struct hda_codec *codec; 4049 4050 list_for_each_codec(codec, bus) { 4051 /* FIXME: maybe a better way needed for forced reset */ 4052 if (current_work() != &codec->jackpoll_work.work) 4053 cancel_delayed_work_sync(&codec->jackpoll_work); 4054 if (hda_codec_is_power_on(codec)) { 4055 hda_call_codec_suspend(codec); 4056 hda_call_codec_resume(codec); 4057 } 4058 } 4059 } 4060 4061 /** 4062 * snd_hda_codec_set_gpio - Set up GPIO bits for AFG 4063 * @codec: the HDA codec 4064 * @mask: GPIO bitmask 4065 * @dir: GPIO direction bits 4066 * @data: GPIO data bits 4067 * @delay: the delay in msec before writing GPIO data bits 4068 */ 4069 void snd_hda_codec_set_gpio(struct hda_codec *codec, unsigned int mask, 4070 unsigned int dir, unsigned int data, 4071 unsigned int delay) 4072 { 4073 snd_hda_codec_write(codec, codec->core.afg, 0, 4074 AC_VERB_SET_GPIO_MASK, mask); 4075 if (delay) { 4076 snd_hda_codec_write_sync(codec, codec->core.afg, 0, 4077 AC_VERB_SET_GPIO_DIRECTION, dir); 4078 msleep(delay); 4079 snd_hda_codec_write_sync(codec, codec->core.afg, 0, 4080 AC_VERB_SET_GPIO_DATA, data); 4081 } else { 4082 snd_hda_codec_write(codec, codec->core.afg, 0, 4083 AC_VERB_SET_GPIO_DIRECTION, dir); 4084 snd_hda_codec_write(codec, codec->core.afg, 0, 4085 AC_VERB_SET_GPIO_DATA, data); 4086 } 4087 } 4088 EXPORT_SYMBOL_GPL(snd_hda_codec_set_gpio); 4089 4090 /** 4091 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer 4092 * @pcm: PCM caps bits 4093 * @buf: the string buffer to write 4094 * @buflen: the max buffer length 4095 * 4096 * used by hda_proc.c and hda_eld.c 4097 */ 4098 void snd_print_pcm_bits(int pcm, char *buf, int buflen) 4099 { 4100 static const unsigned int bits[] = { 8, 16, 20, 24, 32 }; 4101 int i, j; 4102 4103 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) 4104 if (pcm & (AC_SUPPCM_BITS_8 << i)) 4105 j += scnprintf(buf + j, buflen - j, " %d", bits[i]); 4106 4107 buf[j] = '\0'; /* necessary when j == 0 */ 4108 } 4109 EXPORT_SYMBOL_GPL(snd_print_pcm_bits); 4110 4111 MODULE_DESCRIPTION("HDA codec core"); 4112 MODULE_LICENSE("GPL"); 4113