3 * hda_intel.c - Implementation of primary alsa driver code base
6 * Copyright(c) 2004 Intel Corporation. All rights reserved.
8 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9 * PeiSen Hou <pshou@realtek.com.tw>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, write to the Free Software Foundation, Inc., 59
23 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * Matt Jared matt.jared@intel.com
28 * Andy Kopp andy.kopp@intel.com
29 * Dan Kogan dan.d.kogan@intel.com
33 * 2004.12.01 Major rewrite by tiwai, merged the work of pshou
37 #include <linux/delay.h>
38 #include <linux/interrupt.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/moduleparam.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/pci.h>
46 #include <linux/mutex.h>
47 #include <linux/reboot.h>
49 #include <linux/pm_runtime.h>
50 #include <linux/clocksource.h>
51 #include <linux/time.h>
52 #include <linux/completion.h>
55 /* for snoop control */
56 #include <asm/pgtable.h>
57 #include <asm/cacheflush.h>
59 #include <sound/core.h>
60 #include <sound/initval.h>
61 #include <linux/vgaarb.h>
62 #include <linux/vga_switcheroo.h>
63 #include <linux/firmware.h>
64 #include "hda_codec.h"
65 #include "hda_controller.h"
70 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
71 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
72 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
73 static char *model[SNDRV_CARDS];
74 static int position_fix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
75 static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
76 static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
77 static int probe_only[SNDRV_CARDS];
78 static int jackpoll_ms[SNDRV_CARDS];
79 static bool single_cmd;
80 static int enable_msi = -1;
81 #ifdef CONFIG_SND_HDA_PATCH_LOADER
82 static char *patch[SNDRV_CARDS];
84 #ifdef CONFIG_SND_HDA_INPUT_BEEP
85 static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
86 CONFIG_SND_HDA_INPUT_BEEP_MODE};
89 module_param_array(index, int, NULL, 0444);
90 MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
91 module_param_array(id, charp, NULL, 0444);
92 MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
93 module_param_array(enable, bool, NULL, 0444);
94 MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
95 module_param_array(model, charp, NULL, 0444);
96 MODULE_PARM_DESC(model, "Use the given board model.");
97 module_param_array(position_fix, int, NULL, 0444);
98 MODULE_PARM_DESC(position_fix, "DMA pointer read method."
99 "(-1 = system default, 0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO, 4 = COMBO).");
100 module_param_array(bdl_pos_adj, int, NULL, 0644);
101 MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
102 module_param_array(probe_mask, int, NULL, 0444);
103 MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
104 module_param_array(probe_only, int, NULL, 0444);
105 MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
106 module_param_array(jackpoll_ms, int, NULL, 0444);
107 MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
108 module_param(single_cmd, bool, 0444);
109 MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
110 "(for debugging only).");
111 module_param(enable_msi, bint, 0444);
112 MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
113 #ifdef CONFIG_SND_HDA_PATCH_LOADER
114 module_param_array(patch, charp, NULL, 0444);
115 MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
117 #ifdef CONFIG_SND_HDA_INPUT_BEEP
118 module_param_array(beep_mode, bool, NULL, 0444);
119 MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
120 "(0=off, 1=on) (default=1).");
124 static int param_set_xint(const char *val, const struct kernel_param *kp);
125 static struct kernel_param_ops param_ops_xint = {
126 .set = param_set_xint,
127 .get = param_get_int,
129 #define param_check_xint param_check_int
131 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
132 static int *power_save_addr = &power_save;
133 module_param(power_save, xint, 0644);
134 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
135 "(in second, 0 = disable).");
137 /* reset the HD-audio controller in power save mode.
138 * this may give more power-saving, but will take longer time to
141 static bool power_save_controller = 1;
142 module_param(power_save_controller, bool, 0644);
143 MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
145 static int *power_save_addr;
146 #endif /* CONFIG_PM */
148 static int align_buffer_size = -1;
149 module_param(align_buffer_size, bint, 0644);
150 MODULE_PARM_DESC(align_buffer_size,
151 "Force buffer and period sizes to be multiple of 128 bytes.");
154 static bool hda_snoop = true;
155 module_param_named(snoop, hda_snoop, bool, 0444);
156 MODULE_PARM_DESC(snoop, "Enable/disable snooping");
158 #define hda_snoop true
162 MODULE_LICENSE("GPL");
163 MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
195 MODULE_DESCRIPTION("Intel HDA driver");
197 #if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
198 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
199 #define SUPPORT_VGA_SWITCHEROO
215 AZX_DRIVER_ATIHDMI_NS,
224 AZX_NUM_DRIVERS, /* keep this as last entry */
227 /* quirks for Intel PCH */
228 #define AZX_DCAPS_INTEL_PCH_NOPM \
229 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_BUFSIZE | \
230 AZX_DCAPS_COUNT_LPIB_DELAY)
232 #define AZX_DCAPS_INTEL_PCH \
233 (AZX_DCAPS_INTEL_PCH_NOPM | AZX_DCAPS_PM_RUNTIME)
235 #define AZX_DCAPS_INTEL_HASWELL \
236 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_ALIGN_BUFSIZE | \
237 AZX_DCAPS_COUNT_LPIB_DELAY | AZX_DCAPS_PM_RUNTIME | \
238 AZX_DCAPS_I915_POWERWELL)
240 /* Broadwell HDMI can't use position buffer reliably, force to use LPIB */
241 #define AZX_DCAPS_INTEL_BROADWELL \
242 (AZX_DCAPS_SCH_SNOOP | AZX_DCAPS_ALIGN_BUFSIZE | \
243 AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_PM_RUNTIME | \
244 AZX_DCAPS_I915_POWERWELL)
246 /* quirks for ATI SB / AMD Hudson */
247 #define AZX_DCAPS_PRESET_ATI_SB \
248 (AZX_DCAPS_ATI_SNOOP | AZX_DCAPS_NO_TCSEL | \
249 AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
251 /* quirks for ATI/AMD HDMI */
252 #define AZX_DCAPS_PRESET_ATI_HDMI \
253 (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB)
255 /* quirks for Nvidia */
256 #define AZX_DCAPS_PRESET_NVIDIA \
257 (AZX_DCAPS_NVIDIA_SNOOP | AZX_DCAPS_RIRB_DELAY | AZX_DCAPS_NO_MSI |\
258 AZX_DCAPS_ALIGN_BUFSIZE | AZX_DCAPS_NO_64BIT |\
259 AZX_DCAPS_CORBRP_SELF_CLEAR)
261 #define AZX_DCAPS_PRESET_CTHDA \
262 (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
265 * VGA-switcher support
267 #ifdef SUPPORT_VGA_SWITCHEROO
268 #define use_vga_switcheroo(chip) ((chip)->use_vga_switcheroo)
270 #define use_vga_switcheroo(chip) 0
273 static char *driver_short_names[] = {
274 [AZX_DRIVER_ICH] = "HDA Intel",
275 [AZX_DRIVER_PCH] = "HDA Intel PCH",
276 [AZX_DRIVER_SCH] = "HDA Intel MID",
277 [AZX_DRIVER_HDMI] = "HDA Intel HDMI",
278 [AZX_DRIVER_ATI] = "HDA ATI SB",
279 [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
280 [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
281 [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
282 [AZX_DRIVER_SIS] = "HDA SIS966",
283 [AZX_DRIVER_ULI] = "HDA ULI M5461",
284 [AZX_DRIVER_NVIDIA] = "HDA NVidia",
285 [AZX_DRIVER_TERA] = "HDA Teradici",
286 [AZX_DRIVER_CTX] = "HDA Creative",
287 [AZX_DRIVER_CTHDA] = "HDA Creative",
288 [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
297 static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
303 if (!dmab || !dmab->area || !dmab->bytes)
306 #ifdef CONFIG_SND_DMA_SGBUF
307 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
308 struct snd_sg_buf *sgbuf = dmab->private_data;
310 set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
312 set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
317 pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
319 set_memory_wc((unsigned long)dmab->area, pages);
321 set_memory_wb((unsigned long)dmab->area, pages);
324 static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
327 __mark_pages_wc(chip, buf, on);
329 static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
330 struct snd_pcm_substream *substream, bool on)
332 if (azx_dev->wc_marked != on) {
333 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
334 azx_dev->wc_marked = on;
338 /* NOP for other archs */
339 static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
343 static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
344 struct snd_pcm_substream *substream, bool on)
349 static int azx_acquire_irq(struct azx *chip, int do_disconnect);
352 * initialize the PCI registers
354 /* update bits in a PCI register byte */
355 static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
356 unsigned char mask, unsigned char val)
360 pci_read_config_byte(pci, reg, &data);
362 data |= (val & mask);
363 pci_write_config_byte(pci, reg, data);
366 static void azx_init_pci(struct azx *chip)
368 /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
369 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
370 * Ensuring these bits are 0 clears playback static on some HD Audio
372 * The PCI register TCSEL is defined in the Intel manuals.
374 if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
375 dev_dbg(chip->card->dev, "Clearing TCSEL\n");
376 update_pci_byte(chip->pci, ICH6_PCIREG_TCSEL, 0x07, 0);
379 /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
380 * we need to enable snoop.
382 if (chip->driver_caps & AZX_DCAPS_ATI_SNOOP) {
383 dev_dbg(chip->card->dev, "Setting ATI snoop: %d\n",
385 update_pci_byte(chip->pci,
386 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
387 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
390 /* For NVIDIA HDA, enable snoop */
391 if (chip->driver_caps & AZX_DCAPS_NVIDIA_SNOOP) {
392 dev_dbg(chip->card->dev, "Setting Nvidia snoop: %d\n",
394 update_pci_byte(chip->pci,
395 NVIDIA_HDA_TRANSREG_ADDR,
396 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
397 update_pci_byte(chip->pci,
398 NVIDIA_HDA_ISTRM_COH,
399 0x01, NVIDIA_HDA_ENABLE_COHBIT);
400 update_pci_byte(chip->pci,
401 NVIDIA_HDA_OSTRM_COH,
402 0x01, NVIDIA_HDA_ENABLE_COHBIT);
405 /* Enable SCH/PCH snoop if needed */
406 if (chip->driver_caps & AZX_DCAPS_SCH_SNOOP) {
407 unsigned short snoop;
408 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
409 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
410 (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
411 snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
412 if (!azx_snoop(chip))
413 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
414 pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
415 pci_read_config_word(chip->pci,
416 INTEL_SCH_HDA_DEVC, &snoop);
418 dev_dbg(chip->card->dev, "SCH snoop: %s\n",
419 (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) ?
420 "Disabled" : "Enabled");
424 static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
426 /* called from IRQ */
427 static int azx_position_check(struct azx *chip, struct azx_dev *azx_dev)
431 ok = azx_position_ok(chip, azx_dev);
433 azx_dev->irq_pending = 0;
435 } else if (ok == 0 && chip->bus && chip->bus->workq) {
436 /* bogus IRQ, process it later */
437 azx_dev->irq_pending = 1;
438 queue_work(chip->bus->workq, &chip->irq_pending_work);
444 * Check whether the current DMA position is acceptable for updating
445 * periods. Returns non-zero if it's OK.
447 * Many HD-audio controllers appear pretty inaccurate about
448 * the update-IRQ timing. The IRQ is issued before actually the
449 * data is processed. So, we need to process it afterwords in a
452 static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
457 wallclk = azx_readl(chip, WALLCLK) - azx_dev->start_wallclk;
458 if (wallclk < (azx_dev->period_wallclk * 2) / 3)
459 return -1; /* bogus (too early) interrupt */
461 pos = azx_get_position(chip, azx_dev, true);
463 if (WARN_ONCE(!azx_dev->period_bytes,
464 "hda-intel: zero azx_dev->period_bytes"))
465 return -1; /* this shouldn't happen! */
466 if (wallclk < (azx_dev->period_wallclk * 5) / 4 &&
467 pos % azx_dev->period_bytes > azx_dev->period_bytes / 2)
468 /* NG - it's below the first next period boundary */
469 return chip->bdl_pos_adj[chip->dev_index] ? 0 : -1;
470 azx_dev->start_wallclk += wallclk;
471 return 1; /* OK, it's fine */
475 * The work for pending PCM period updates.
477 static void azx_irq_pending_work(struct work_struct *work)
479 struct azx *chip = container_of(work, struct azx, irq_pending_work);
482 if (!chip->irq_pending_warned) {
483 dev_info(chip->card->dev,
484 "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
486 chip->irq_pending_warned = 1;
491 spin_lock_irq(&chip->reg_lock);
492 for (i = 0; i < chip->num_streams; i++) {
493 struct azx_dev *azx_dev = &chip->azx_dev[i];
494 if (!azx_dev->irq_pending ||
495 !azx_dev->substream ||
498 ok = azx_position_ok(chip, azx_dev);
500 azx_dev->irq_pending = 0;
501 spin_unlock(&chip->reg_lock);
502 snd_pcm_period_elapsed(azx_dev->substream);
503 spin_lock(&chip->reg_lock);
505 pending = 0; /* too early */
509 spin_unlock_irq(&chip->reg_lock);
516 /* clear irq_pending flags and assure no on-going workq */
517 static void azx_clear_irq_pending(struct azx *chip)
521 spin_lock_irq(&chip->reg_lock);
522 for (i = 0; i < chip->num_streams; i++)
523 chip->azx_dev[i].irq_pending = 0;
524 spin_unlock_irq(&chip->reg_lock);
527 static int azx_acquire_irq(struct azx *chip, int do_disconnect)
529 if (request_irq(chip->pci->irq, azx_interrupt,
530 chip->msi ? 0 : IRQF_SHARED,
531 KBUILD_MODNAME, chip)) {
532 dev_err(chip->card->dev,
533 "unable to grab IRQ %d, disabling device\n",
536 snd_card_disconnect(chip->card);
539 chip->irq = chip->pci->irq;
540 pci_intx(chip->pci, !chip->msi);
545 static DEFINE_MUTEX(card_list_lock);
546 static LIST_HEAD(card_list);
548 static void azx_add_card_list(struct azx *chip)
550 mutex_lock(&card_list_lock);
551 list_add(&chip->list, &card_list);
552 mutex_unlock(&card_list_lock);
555 static void azx_del_card_list(struct azx *chip)
557 mutex_lock(&card_list_lock);
558 list_del_init(&chip->list);
559 mutex_unlock(&card_list_lock);
562 /* trigger power-save check at writing parameter */
563 static int param_set_xint(const char *val, const struct kernel_param *kp)
567 int prev = power_save;
568 int ret = param_set_int(val, kp);
570 if (ret || prev == power_save)
573 mutex_lock(&card_list_lock);
574 list_for_each_entry(chip, &card_list, list) {
575 if (!chip->bus || chip->disabled)
577 list_for_each_entry(c, &chip->bus->codec_list, list)
578 snd_hda_power_sync(c);
580 mutex_unlock(&card_list_lock);
584 #define azx_add_card_list(chip) /* NOP */
585 #define azx_del_card_list(chip) /* NOP */
586 #endif /* CONFIG_PM */
588 #if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
592 static int azx_suspend(struct device *dev)
594 struct pci_dev *pci = to_pci_dev(dev);
595 struct snd_card *card = dev_get_drvdata(dev);
596 struct azx *chip = card->private_data;
602 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
603 azx_clear_irq_pending(chip);
604 list_for_each_entry(p, &chip->pcm_list, list)
605 snd_pcm_suspend_all(p->pcm);
606 if (chip->initialized)
607 snd_hda_suspend(chip->bus);
609 azx_enter_link_reset(chip);
610 if (chip->irq >= 0) {
611 free_irq(chip->irq, chip);
616 pci_disable_msi(chip->pci);
617 pci_disable_device(pci);
619 pci_set_power_state(pci, PCI_D3hot);
620 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
621 hda_display_power(false);
625 static int azx_resume(struct device *dev)
627 struct pci_dev *pci = to_pci_dev(dev);
628 struct snd_card *card = dev_get_drvdata(dev);
629 struct azx *chip = card->private_data;
634 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
635 hda_display_power(true);
636 haswell_set_bclk(chip);
638 pci_set_power_state(pci, PCI_D0);
639 pci_restore_state(pci);
640 if (pci_enable_device(pci) < 0) {
641 dev_err(chip->card->dev,
642 "pci_enable_device failed, disabling device\n");
643 snd_card_disconnect(card);
648 if (pci_enable_msi(pci) < 0)
650 if (azx_acquire_irq(chip, 1) < 0)
654 azx_init_chip(chip, true);
656 snd_hda_resume(chip->bus);
657 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
660 #endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
662 #ifdef CONFIG_PM_RUNTIME
663 static int azx_runtime_suspend(struct device *dev)
665 struct snd_card *card = dev_get_drvdata(dev);
666 struct azx *chip = card->private_data;
671 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
674 /* enable controller wake up event */
675 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
679 azx_enter_link_reset(chip);
680 azx_clear_irq_pending(chip);
681 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
682 hda_display_power(false);
687 static int azx_runtime_resume(struct device *dev)
689 struct snd_card *card = dev_get_drvdata(dev);
690 struct azx *chip = card->private_data;
692 struct hda_codec *codec;
698 if (!(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
701 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
702 hda_display_power(true);
703 haswell_set_bclk(chip);
706 /* Read STATESTS before controller reset */
707 status = azx_readw(chip, STATESTS);
710 azx_init_chip(chip, true);
714 list_for_each_entry(codec, &bus->codec_list, list)
715 if (status & (1 << codec->addr))
716 queue_delayed_work(codec->bus->workq,
717 &codec->jackpoll_work, codec->jackpoll_interval);
720 /* disable controller Wake Up event*/
721 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
727 static int azx_runtime_idle(struct device *dev)
729 struct snd_card *card = dev_get_drvdata(dev);
730 struct azx *chip = card->private_data;
735 if (!power_save_controller ||
736 !(chip->driver_caps & AZX_DCAPS_PM_RUNTIME))
742 #endif /* CONFIG_PM_RUNTIME */
745 static const struct dev_pm_ops azx_pm = {
746 SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
747 SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
750 #define AZX_PM_OPS &azx_pm
752 #define AZX_PM_OPS NULL
753 #endif /* CONFIG_PM */
757 * reboot notifier for hang-up problem at power-down
759 static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
761 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
762 snd_hda_bus_reboot_notify(chip->bus);
767 static void azx_notifier_register(struct azx *chip)
769 chip->reboot_notifier.notifier_call = azx_halt;
770 register_reboot_notifier(&chip->reboot_notifier);
773 static void azx_notifier_unregister(struct azx *chip)
775 if (chip->reboot_notifier.notifier_call)
776 unregister_reboot_notifier(&chip->reboot_notifier);
779 static int azx_probe_continue(struct azx *chip);
781 #ifdef SUPPORT_VGA_SWITCHEROO
782 static struct pci_dev *get_bound_vga(struct pci_dev *pci);
784 static void azx_vs_set_state(struct pci_dev *pci,
785 enum vga_switcheroo_state state)
787 struct snd_card *card = pci_get_drvdata(pci);
788 struct azx *chip = card->private_data;
791 wait_for_completion(&chip->probe_wait);
792 if (chip->init_failed)
795 disabled = (state == VGA_SWITCHEROO_OFF);
796 if (chip->disabled == disabled)
800 chip->disabled = disabled;
802 dev_info(chip->card->dev,
803 "Start delayed initialization\n");
804 if (azx_probe_continue(chip) < 0) {
805 dev_err(chip->card->dev, "initialization error\n");
806 chip->init_failed = true;
810 dev_info(chip->card->dev, "%s via VGA-switcheroo\n",
811 disabled ? "Disabling" : "Enabling");
813 pm_runtime_put_sync_suspend(card->dev);
814 azx_suspend(card->dev);
815 /* when we get suspended by vga switcheroo we end up in D3cold,
816 * however we have no ACPI handle, so pci/acpi can't put us there,
817 * put ourselves there */
818 pci->current_state = PCI_D3cold;
819 chip->disabled = true;
820 if (snd_hda_lock_devices(chip->bus))
821 dev_warn(chip->card->dev,
822 "Cannot lock devices!\n");
824 snd_hda_unlock_devices(chip->bus);
825 pm_runtime_get_noresume(card->dev);
826 chip->disabled = false;
827 azx_resume(card->dev);
832 static bool azx_vs_can_switch(struct pci_dev *pci)
834 struct snd_card *card = pci_get_drvdata(pci);
835 struct azx *chip = card->private_data;
837 wait_for_completion(&chip->probe_wait);
838 if (chip->init_failed)
840 if (chip->disabled || !chip->bus)
842 if (snd_hda_lock_devices(chip->bus))
844 snd_hda_unlock_devices(chip->bus);
848 static void init_vga_switcheroo(struct azx *chip)
850 struct pci_dev *p = get_bound_vga(chip->pci);
852 dev_info(chip->card->dev,
853 "Handle VGA-switcheroo audio client\n");
854 chip->use_vga_switcheroo = 1;
859 static const struct vga_switcheroo_client_ops azx_vs_ops = {
860 .set_gpu_state = azx_vs_set_state,
861 .can_switch = azx_vs_can_switch,
864 static int register_vga_switcheroo(struct azx *chip)
868 if (!chip->use_vga_switcheroo)
870 /* FIXME: currently only handling DIS controller
871 * is there any machine with two switchable HDMI audio controllers?
873 err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
878 chip->vga_switcheroo_registered = 1;
880 /* register as an optimus hdmi audio power domain */
881 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(chip->card->dev,
882 &chip->hdmi_pm_domain);
886 #define init_vga_switcheroo(chip) /* NOP */
887 #define register_vga_switcheroo(chip) 0
888 #define check_hdmi_disabled(pci) false
889 #endif /* SUPPORT_VGA_SWITCHER */
894 static int azx_free(struct azx *chip)
896 struct pci_dev *pci = chip->pci;
897 struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
901 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME)
903 pm_runtime_get_noresume(&pci->dev);
905 azx_del_card_list(chip);
907 azx_notifier_unregister(chip);
909 chip->init_failed = 1; /* to be sure */
910 complete_all(&chip->probe_wait);
912 if (use_vga_switcheroo(chip)) {
913 if (chip->disabled && chip->bus)
914 snd_hda_unlock_devices(chip->bus);
915 if (chip->vga_switcheroo_registered)
916 vga_switcheroo_unregister_client(chip->pci);
919 if (chip->initialized) {
920 azx_clear_irq_pending(chip);
921 for (i = 0; i < chip->num_streams; i++)
922 azx_stream_stop(chip, &chip->azx_dev[i]);
927 free_irq(chip->irq, (void*)chip);
929 pci_disable_msi(chip->pci);
930 if (chip->remap_addr)
931 iounmap(chip->remap_addr);
933 azx_free_stream_pages(chip);
934 if (chip->region_requested)
935 pci_release_regions(chip->pci);
936 pci_disable_device(chip->pci);
937 kfree(chip->azx_dev);
938 #ifdef CONFIG_SND_HDA_PATCH_LOADER
940 release_firmware(chip->fw);
942 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
943 hda_display_power(false);
951 static int azx_dev_free(struct snd_device *device)
953 return azx_free(device->device_data);
956 #ifdef SUPPORT_VGA_SWITCHEROO
958 * Check of disabled HDMI controller by vga-switcheroo
960 static struct pci_dev *get_bound_vga(struct pci_dev *pci)
964 /* check only discrete GPU */
965 switch (pci->vendor) {
966 case PCI_VENDOR_ID_ATI:
967 case PCI_VENDOR_ID_AMD:
968 case PCI_VENDOR_ID_NVIDIA:
969 if (pci->devfn == 1) {
970 p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
971 pci->bus->number, 0);
973 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
983 static bool check_hdmi_disabled(struct pci_dev *pci)
985 bool vga_inactive = false;
986 struct pci_dev *p = get_bound_vga(pci);
989 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
995 #endif /* SUPPORT_VGA_SWITCHEROO */
998 * white/black-listing for position_fix
1000 static struct snd_pci_quirk position_fix_list[] = {
1001 SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
1002 SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
1003 SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
1004 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
1005 SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
1006 SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
1007 SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
1008 SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
1009 SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
1010 SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
1011 SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
1012 SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
1013 SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
1014 SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
1018 static int check_position_fix(struct azx *chip, int fix)
1020 const struct snd_pci_quirk *q;
1025 case POS_FIX_POSBUF:
1026 case POS_FIX_VIACOMBO:
1031 q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
1033 dev_info(chip->card->dev,
1034 "position_fix set to %d for device %04x:%04x\n",
1035 q->value, q->subvendor, q->subdevice);
1039 /* Check VIA/ATI HD Audio Controller exist */
1040 if (chip->driver_caps & AZX_DCAPS_POSFIX_VIA) {
1041 dev_dbg(chip->card->dev, "Using VIACOMBO position fix\n");
1042 return POS_FIX_VIACOMBO;
1044 if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
1045 dev_dbg(chip->card->dev, "Using LPIB position fix\n");
1046 return POS_FIX_LPIB;
1048 return POS_FIX_AUTO;
1052 * black-lists for probe_mask
1054 static struct snd_pci_quirk probe_mask_list[] = {
1055 /* Thinkpad often breaks the controller communication when accessing
1056 * to the non-working (or non-existing) modem codec slot.
1058 SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
1059 SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
1060 SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
1062 SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
1063 /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
1064 SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
1065 /* forced codec slots */
1066 SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
1067 SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
1068 /* WinFast VP200 H (Teradici) user reported broken communication */
1069 SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
1073 #define AZX_FORCE_CODEC_MASK 0x100
1075 static void check_probe_mask(struct azx *chip, int dev)
1077 const struct snd_pci_quirk *q;
1079 chip->codec_probe_mask = probe_mask[dev];
1080 if (chip->codec_probe_mask == -1) {
1081 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
1083 dev_info(chip->card->dev,
1084 "probe_mask set to 0x%x for device %04x:%04x\n",
1085 q->value, q->subvendor, q->subdevice);
1086 chip->codec_probe_mask = q->value;
1090 /* check forced option */
1091 if (chip->codec_probe_mask != -1 &&
1092 (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
1093 chip->codec_mask = chip->codec_probe_mask & 0xff;
1094 dev_info(chip->card->dev, "codec_mask forced to 0x%x\n",
1100 * white/black-list for enable_msi
1102 static struct snd_pci_quirk msi_black_list[] = {
1103 SND_PCI_QUIRK(0x103c, 0x2191, "HP", 0), /* AMD Hudson */
1104 SND_PCI_QUIRK(0x103c, 0x2192, "HP", 0), /* AMD Hudson */
1105 SND_PCI_QUIRK(0x103c, 0x21f7, "HP", 0), /* AMD Hudson */
1106 SND_PCI_QUIRK(0x103c, 0x21fa, "HP", 0), /* AMD Hudson */
1107 SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
1108 SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
1109 SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
1110 SND_PCI_QUIRK(0x1179, 0xfb44, "Toshiba Satellite C870", 0), /* AMD Hudson */
1111 SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
1112 SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
1116 static void check_msi(struct azx *chip)
1118 const struct snd_pci_quirk *q;
1120 if (enable_msi >= 0) {
1121 chip->msi = !!enable_msi;
1124 chip->msi = 1; /* enable MSI as default */
1125 q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
1127 dev_info(chip->card->dev,
1128 "msi for device %04x:%04x set to %d\n",
1129 q->subvendor, q->subdevice, q->value);
1130 chip->msi = q->value;
1134 /* NVidia chipsets seem to cause troubles with MSI */
1135 if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
1136 dev_info(chip->card->dev, "Disabling MSI\n");
1141 /* check the snoop mode availability */
1142 static void azx_check_snoop_available(struct azx *chip)
1144 bool snoop = chip->snoop;
1146 switch (chip->driver_type) {
1147 case AZX_DRIVER_VIA:
1148 /* force to non-snoop mode for a new VIA controller
1153 pci_read_config_byte(chip->pci, 0x42, &val);
1154 if (!(val & 0x80) && chip->pci->revision == 0x30)
1158 case AZX_DRIVER_ATIHDMI_NS:
1159 /* new ATI HDMI requires non-snoop */
1162 case AZX_DRIVER_CTHDA:
1167 if (snoop != chip->snoop) {
1168 dev_info(chip->card->dev, "Force to %s mode\n",
1169 snoop ? "snoop" : "non-snoop");
1170 chip->snoop = snoop;
1174 static void azx_probe_work(struct work_struct *work)
1176 azx_probe_continue(container_of(work, struct azx, probe_work));
1182 static int azx_create(struct snd_card *card, struct pci_dev *pci,
1183 int dev, unsigned int driver_caps,
1184 const struct hda_controller_ops *hda_ops,
1187 static struct snd_device_ops ops = {
1188 .dev_free = azx_dev_free,
1190 struct hda_intel *hda;
1196 err = pci_enable_device(pci);
1200 hda = kzalloc(sizeof(*hda), GFP_KERNEL);
1202 dev_err(card->dev, "Cannot allocate hda\n");
1203 pci_disable_device(pci);
1208 spin_lock_init(&chip->reg_lock);
1209 mutex_init(&chip->open_mutex);
1212 chip->ops = hda_ops;
1214 chip->driver_caps = driver_caps;
1215 chip->driver_type = driver_caps & 0xff;
1217 chip->dev_index = dev;
1218 chip->jackpoll_ms = jackpoll_ms;
1219 INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
1220 INIT_LIST_HEAD(&chip->pcm_list);
1221 INIT_LIST_HEAD(&chip->list);
1222 init_vga_switcheroo(chip);
1223 init_completion(&chip->probe_wait);
1225 chip->position_fix[0] = chip->position_fix[1] =
1226 check_position_fix(chip, position_fix[dev]);
1227 /* combo mode uses LPIB for playback */
1228 if (chip->position_fix[0] == POS_FIX_COMBO) {
1229 chip->position_fix[0] = POS_FIX_LPIB;
1230 chip->position_fix[1] = POS_FIX_AUTO;
1233 check_probe_mask(chip, dev);
1235 chip->single_cmd = single_cmd;
1236 chip->snoop = hda_snoop;
1237 azx_check_snoop_available(chip);
1239 if (bdl_pos_adj[dev] < 0) {
1240 switch (chip->driver_type) {
1241 case AZX_DRIVER_ICH:
1242 case AZX_DRIVER_PCH:
1243 bdl_pos_adj[dev] = 1;
1246 bdl_pos_adj[dev] = 32;
1250 chip->bdl_pos_adj = bdl_pos_adj;
1252 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1254 dev_err(card->dev, "Error creating device [card]!\n");
1259 /* continue probing in work context as may trigger request module */
1260 INIT_WORK(&chip->probe_work, azx_probe_work);
1267 static int azx_first_init(struct azx *chip)
1269 int dev = chip->dev_index;
1270 struct pci_dev *pci = chip->pci;
1271 struct snd_card *card = chip->card;
1273 unsigned short gcap;
1275 #if BITS_PER_LONG != 64
1276 /* Fix up base address on ULI M5461 */
1277 if (chip->driver_type == AZX_DRIVER_ULI) {
1279 pci_read_config_word(pci, 0x40, &tmp3);
1280 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
1281 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
1285 err = pci_request_regions(pci, "ICH HD audio");
1288 chip->region_requested = 1;
1290 chip->addr = pci_resource_start(pci, 0);
1291 chip->remap_addr = pci_ioremap_bar(pci, 0);
1292 if (chip->remap_addr == NULL) {
1293 dev_err(card->dev, "ioremap error\n");
1298 if (pci_enable_msi(pci) < 0)
1301 if (azx_acquire_irq(chip, 0) < 0)
1304 pci_set_master(pci);
1305 synchronize_irq(chip->irq);
1307 gcap = azx_readw(chip, GCAP);
1308 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
1310 /* disable SB600 64bit support for safety */
1311 if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
1312 struct pci_dev *p_smbus;
1313 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
1314 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
1317 if (p_smbus->revision < 0x30)
1318 gcap &= ~ICH6_GCAP_64OK;
1319 pci_dev_put(p_smbus);
1323 /* disable 64bit DMA address on some devices */
1324 if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
1325 dev_dbg(card->dev, "Disabling 64bit DMA\n");
1326 gcap &= ~ICH6_GCAP_64OK;
1329 /* disable buffer size rounding to 128-byte multiples if supported */
1330 if (align_buffer_size >= 0)
1331 chip->align_buffer_size = !!align_buffer_size;
1333 if (chip->driver_caps & AZX_DCAPS_BUFSIZE)
1334 chip->align_buffer_size = 0;
1335 else if (chip->driver_caps & AZX_DCAPS_ALIGN_BUFSIZE)
1336 chip->align_buffer_size = 1;
1338 chip->align_buffer_size = 1;
1341 /* allow 64bit DMA address if supported by H/W */
1342 if ((gcap & ICH6_GCAP_64OK) && !pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
1343 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
1345 pci_set_dma_mask(pci, DMA_BIT_MASK(32));
1346 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
1349 /* read number of streams from GCAP register instead of using
1352 chip->capture_streams = (gcap >> 8) & 0x0f;
1353 chip->playback_streams = (gcap >> 12) & 0x0f;
1354 if (!chip->playback_streams && !chip->capture_streams) {
1355 /* gcap didn't give any info, switching to old method */
1357 switch (chip->driver_type) {
1358 case AZX_DRIVER_ULI:
1359 chip->playback_streams = ULI_NUM_PLAYBACK;
1360 chip->capture_streams = ULI_NUM_CAPTURE;
1362 case AZX_DRIVER_ATIHDMI:
1363 case AZX_DRIVER_ATIHDMI_NS:
1364 chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
1365 chip->capture_streams = ATIHDMI_NUM_CAPTURE;
1367 case AZX_DRIVER_GENERIC:
1369 chip->playback_streams = ICH6_NUM_PLAYBACK;
1370 chip->capture_streams = ICH6_NUM_CAPTURE;
1374 chip->capture_index_offset = 0;
1375 chip->playback_index_offset = chip->capture_streams;
1376 chip->num_streams = chip->playback_streams + chip->capture_streams;
1377 chip->azx_dev = kcalloc(chip->num_streams, sizeof(*chip->azx_dev),
1379 if (!chip->azx_dev) {
1380 dev_err(card->dev, "cannot malloc azx_dev\n");
1384 err = azx_alloc_stream_pages(chip);
1388 /* initialize streams */
1389 azx_init_stream(chip);
1391 /* initialize chip */
1394 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
1395 haswell_set_bclk(chip);
1397 azx_init_chip(chip, (probe_only[dev] & 2) == 0);
1399 /* codec detection */
1400 if (!chip->codec_mask) {
1401 dev_err(card->dev, "no codecs found!\n");
1405 strcpy(card->driver, "HDA-Intel");
1406 strlcpy(card->shortname, driver_short_names[chip->driver_type],
1407 sizeof(card->shortname));
1408 snprintf(card->longname, sizeof(card->longname),
1409 "%s at 0x%lx irq %i",
1410 card->shortname, chip->addr, chip->irq);
1415 static void power_down_all_codecs(struct azx *chip)
1418 /* The codecs were powered up in snd_hda_codec_new().
1419 * Now all initialization done, so turn them down if possible
1421 struct hda_codec *codec;
1422 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1423 snd_hda_power_down(codec);
1428 #ifdef CONFIG_SND_HDA_PATCH_LOADER
1429 /* callback from request_firmware_nowait() */
1430 static void azx_firmware_cb(const struct firmware *fw, void *context)
1432 struct snd_card *card = context;
1433 struct azx *chip = card->private_data;
1434 struct pci_dev *pci = chip->pci;
1437 dev_err(card->dev, "Cannot load firmware, aborting\n");
1442 if (!chip->disabled) {
1443 /* continue probing */
1444 if (azx_probe_continue(chip))
1450 snd_card_free(card);
1451 pci_set_drvdata(pci, NULL);
1456 * HDA controller ops.
1459 /* PCI register access. */
1460 static void pci_azx_writel(u32 value, u32 __iomem *addr)
1462 writel(value, addr);
1465 static u32 pci_azx_readl(u32 __iomem *addr)
1470 static void pci_azx_writew(u16 value, u16 __iomem *addr)
1472 writew(value, addr);
1475 static u16 pci_azx_readw(u16 __iomem *addr)
1480 static void pci_azx_writeb(u8 value, u8 __iomem *addr)
1482 writeb(value, addr);
1485 static u8 pci_azx_readb(u8 __iomem *addr)
1490 static int disable_msi_reset_irq(struct azx *chip)
1494 free_irq(chip->irq, chip);
1496 pci_disable_msi(chip->pci);
1498 err = azx_acquire_irq(chip, 1);
1505 /* DMA page allocation helpers. */
1506 static int dma_alloc_pages(struct azx *chip,
1509 struct snd_dma_buffer *buf)
1513 err = snd_dma_alloc_pages(type,
1518 mark_pages_wc(chip, buf, true);
1522 static void dma_free_pages(struct azx *chip, struct snd_dma_buffer *buf)
1524 mark_pages_wc(chip, buf, false);
1525 snd_dma_free_pages(buf);
1528 static int substream_alloc_pages(struct azx *chip,
1529 struct snd_pcm_substream *substream,
1532 struct azx_dev *azx_dev = get_azx_dev(substream);
1535 mark_runtime_wc(chip, azx_dev, substream, false);
1536 azx_dev->bufsize = 0;
1537 azx_dev->period_bytes = 0;
1538 azx_dev->format_val = 0;
1539 ret = snd_pcm_lib_malloc_pages(substream, size);
1542 mark_runtime_wc(chip, azx_dev, substream, true);
1546 static int substream_free_pages(struct azx *chip,
1547 struct snd_pcm_substream *substream)
1549 struct azx_dev *azx_dev = get_azx_dev(substream);
1550 mark_runtime_wc(chip, azx_dev, substream, false);
1551 return snd_pcm_lib_free_pages(substream);
1554 static void pcm_mmap_prepare(struct snd_pcm_substream *substream,
1555 struct vm_area_struct *area)
1558 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1559 struct azx *chip = apcm->chip;
1560 if (!azx_snoop(chip))
1561 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
1565 static const struct hda_controller_ops pci_hda_ops = {
1566 .reg_writel = pci_azx_writel,
1567 .reg_readl = pci_azx_readl,
1568 .reg_writew = pci_azx_writew,
1569 .reg_readw = pci_azx_readw,
1570 .reg_writeb = pci_azx_writeb,
1571 .reg_readb = pci_azx_readb,
1572 .disable_msi_reset_irq = disable_msi_reset_irq,
1573 .dma_alloc_pages = dma_alloc_pages,
1574 .dma_free_pages = dma_free_pages,
1575 .substream_alloc_pages = substream_alloc_pages,
1576 .substream_free_pages = substream_free_pages,
1577 .pcm_mmap_prepare = pcm_mmap_prepare,
1578 .position_check = azx_position_check,
1581 static int azx_probe(struct pci_dev *pci,
1582 const struct pci_device_id *pci_id)
1585 struct snd_card *card;
1587 bool schedule_probe;
1590 if (dev >= SNDRV_CARDS)
1597 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1600 dev_err(&pci->dev, "Error creating card!\n");
1604 err = azx_create(card, pci, dev, pci_id->driver_data,
1605 &pci_hda_ops, &chip);
1608 card->private_data = chip;
1610 pci_set_drvdata(pci, card);
1612 err = register_vga_switcheroo(chip);
1614 dev_err(card->dev, "Error registering VGA-switcheroo client\n");
1618 if (check_hdmi_disabled(pci)) {
1619 dev_info(card->dev, "VGA controller is disabled\n");
1620 dev_info(card->dev, "Delaying initialization\n");
1621 chip->disabled = true;
1624 schedule_probe = !chip->disabled;
1626 #ifdef CONFIG_SND_HDA_PATCH_LOADER
1627 if (patch[dev] && *patch[dev]) {
1628 dev_info(card->dev, "Applying patch firmware '%s'\n",
1630 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
1631 &pci->dev, GFP_KERNEL, card,
1635 schedule_probe = false; /* continued in azx_firmware_cb() */
1637 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
1639 #ifndef CONFIG_SND_HDA_I915
1640 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
1641 dev_err(card->dev, "Haswell must build in CONFIG_SND_HDA_I915\n");
1645 schedule_work(&chip->probe_work);
1649 complete_all(&chip->probe_wait);
1653 snd_card_free(card);
1657 /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
1658 static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
1659 [AZX_DRIVER_NVIDIA] = 8,
1660 [AZX_DRIVER_TERA] = 1,
1663 static int azx_probe_continue(struct azx *chip)
1665 struct pci_dev *pci = chip->pci;
1666 int dev = chip->dev_index;
1669 /* Request power well for Haswell HDA controller and codec */
1670 if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
1671 #ifdef CONFIG_SND_HDA_I915
1672 err = hda_i915_init();
1674 dev_err(chip->card->dev,
1675 "Error request power-well from i915\n");
1678 err = hda_display_power(true);
1680 dev_err(chip->card->dev,
1681 "Cannot turn on display power on i915\n");
1687 err = azx_first_init(chip);
1691 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1692 chip->beep_mode = beep_mode[dev];
1695 /* create codec instances */
1696 err = azx_codec_create(chip, model[dev],
1697 azx_max_codecs[chip->driver_type],
1702 #ifdef CONFIG_SND_HDA_PATCH_LOADER
1704 err = snd_hda_load_patch(chip->bus, chip->fw->size,
1709 release_firmware(chip->fw); /* no longer needed */
1714 if ((probe_only[dev] & 1) == 0) {
1715 err = azx_codec_configure(chip);
1720 /* create PCM streams */
1721 err = snd_hda_build_pcms(chip->bus);
1725 /* create mixer controls */
1726 err = azx_mixer_create(chip);
1730 err = snd_card_register(chip->card);
1735 power_down_all_codecs(chip);
1736 azx_notifier_register(chip);
1737 azx_add_card_list(chip);
1738 if ((chip->driver_caps & AZX_DCAPS_PM_RUNTIME) || chip->use_vga_switcheroo)
1739 pm_runtime_put_noidle(&pci->dev);
1743 chip->init_failed = 1;
1744 complete_all(&chip->probe_wait);
1748 static void azx_remove(struct pci_dev *pci)
1750 struct snd_card *card = pci_get_drvdata(pci);
1753 snd_card_free(card);
1757 static const struct pci_device_id azx_ids[] = {
1759 { PCI_DEVICE(0x8086, 0x1c20),
1760 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
1762 { PCI_DEVICE(0x8086, 0x1d20),
1763 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
1765 { PCI_DEVICE(0x8086, 0x1e20),
1766 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1768 { PCI_DEVICE(0x8086, 0x8c20),
1769 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1771 { PCI_DEVICE(0x8086, 0x8ca0),
1772 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1774 { PCI_DEVICE(0x8086, 0x8d20),
1775 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1776 { PCI_DEVICE(0x8086, 0x8d21),
1777 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1779 { PCI_DEVICE(0x8086, 0x9c20),
1780 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1782 { PCI_DEVICE(0x8086, 0x9c21),
1783 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1784 /* Wildcat Point-LP */
1785 { PCI_DEVICE(0x8086, 0x9ca0),
1786 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
1788 { PCI_DEVICE(0x8086, 0x0a0c),
1789 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
1790 { PCI_DEVICE(0x8086, 0x0c0c),
1791 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
1792 { PCI_DEVICE(0x8086, 0x0d0c),
1793 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
1795 { PCI_DEVICE(0x8086, 0x160c),
1796 .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_BROADWELL },
1798 { PCI_DEVICE(0x8086, 0x3b56),
1799 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
1801 { PCI_DEVICE(0x8086, 0x811b),
1802 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
1804 { PCI_DEVICE(0x8086, 0x080a),
1805 .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
1807 { PCI_DEVICE(0x8086, 0x0f04),
1808 .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
1810 { PCI_DEVICE(0x8086, 0x2668),
1811 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1812 AZX_DCAPS_BUFSIZE }, /* ICH6 */
1813 { PCI_DEVICE(0x8086, 0x27d8),
1814 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1815 AZX_DCAPS_BUFSIZE }, /* ICH7 */
1816 { PCI_DEVICE(0x8086, 0x269a),
1817 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1818 AZX_DCAPS_BUFSIZE }, /* ESB2 */
1819 { PCI_DEVICE(0x8086, 0x284b),
1820 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1821 AZX_DCAPS_BUFSIZE }, /* ICH8 */
1822 { PCI_DEVICE(0x8086, 0x293e),
1823 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1824 AZX_DCAPS_BUFSIZE }, /* ICH9 */
1825 { PCI_DEVICE(0x8086, 0x293f),
1826 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1827 AZX_DCAPS_BUFSIZE }, /* ICH9 */
1828 { PCI_DEVICE(0x8086, 0x3a3e),
1829 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1830 AZX_DCAPS_BUFSIZE }, /* ICH10 */
1831 { PCI_DEVICE(0x8086, 0x3a6e),
1832 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_OLD_SSYNC |
1833 AZX_DCAPS_BUFSIZE }, /* ICH10 */
1835 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
1836 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
1837 .class_mask = 0xffffff,
1838 .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_BUFSIZE },
1839 /* ATI SB 450/600/700/800/900 */
1840 { PCI_DEVICE(0x1002, 0x437b),
1841 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
1842 { PCI_DEVICE(0x1002, 0x4383),
1843 .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
1845 { PCI_DEVICE(0x1022, 0x780d),
1846 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
1848 { PCI_DEVICE(0x1002, 0x793b),
1849 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1850 { PCI_DEVICE(0x1002, 0x7919),
1851 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1852 { PCI_DEVICE(0x1002, 0x960f),
1853 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1854 { PCI_DEVICE(0x1002, 0x970f),
1855 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1856 { PCI_DEVICE(0x1002, 0xaa00),
1857 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1858 { PCI_DEVICE(0x1002, 0xaa08),
1859 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1860 { PCI_DEVICE(0x1002, 0xaa10),
1861 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1862 { PCI_DEVICE(0x1002, 0xaa18),
1863 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1864 { PCI_DEVICE(0x1002, 0xaa20),
1865 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1866 { PCI_DEVICE(0x1002, 0xaa28),
1867 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1868 { PCI_DEVICE(0x1002, 0xaa30),
1869 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1870 { PCI_DEVICE(0x1002, 0xaa38),
1871 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1872 { PCI_DEVICE(0x1002, 0xaa40),
1873 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1874 { PCI_DEVICE(0x1002, 0xaa48),
1875 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1876 { PCI_DEVICE(0x1002, 0xaa50),
1877 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1878 { PCI_DEVICE(0x1002, 0xaa58),
1879 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1880 { PCI_DEVICE(0x1002, 0xaa60),
1881 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1882 { PCI_DEVICE(0x1002, 0xaa68),
1883 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1884 { PCI_DEVICE(0x1002, 0xaa80),
1885 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1886 { PCI_DEVICE(0x1002, 0xaa88),
1887 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1888 { PCI_DEVICE(0x1002, 0xaa90),
1889 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1890 { PCI_DEVICE(0x1002, 0xaa98),
1891 .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
1892 { PCI_DEVICE(0x1002, 0x9902),
1893 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
1894 { PCI_DEVICE(0x1002, 0xaaa0),
1895 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
1896 { PCI_DEVICE(0x1002, 0xaaa8),
1897 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
1898 { PCI_DEVICE(0x1002, 0xaab0),
1899 .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI },
1900 /* VIA VT8251/VT8237A */
1901 { PCI_DEVICE(0x1106, 0x3288),
1902 .driver_data = AZX_DRIVER_VIA | AZX_DCAPS_POSFIX_VIA },
1903 /* VIA GFX VT7122/VX900 */
1904 { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
1905 /* VIA GFX VT6122/VX11 */
1906 { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
1908 { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
1910 { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
1912 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1913 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
1914 .class_mask = 0xffffff,
1915 .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
1917 { PCI_DEVICE(0x6549, 0x1200),
1918 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
1919 { PCI_DEVICE(0x6549, 0x2200),
1920 .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
1921 /* Creative X-Fi (CA0110-IBG) */
1923 { PCI_DEVICE(0x1102, 0x0010),
1924 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
1925 { PCI_DEVICE(0x1102, 0x0012),
1926 .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
1927 #if !IS_ENABLED(CONFIG_SND_CTXFI)
1928 /* the following entry conflicts with snd-ctxfi driver,
1929 * as ctxfi driver mutates from HD-audio to native mode with
1930 * a special command sequence.
1932 { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
1933 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
1934 .class_mask = 0xffffff,
1935 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
1936 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
1938 /* this entry seems still valid -- i.e. without emu20kx chip */
1939 { PCI_DEVICE(0x1102, 0x0009),
1940 .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
1941 AZX_DCAPS_RIRB_PRE_DELAY | AZX_DCAPS_POSFIX_LPIB },
1944 { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
1945 /* VMware HDAudio */
1946 { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
1947 /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
1948 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
1949 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
1950 .class_mask = 0xffffff,
1951 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
1952 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
1953 .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
1954 .class_mask = 0xffffff,
1955 .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
1958 MODULE_DEVICE_TABLE(pci, azx_ids);
1960 /* pci_driver definition */
1961 static struct pci_driver azx_driver = {
1962 .name = KBUILD_MODNAME,
1963 .id_table = azx_ids,
1965 .remove = azx_remove,
1971 module_pci_driver(azx_driver);