hdac_i915.c 4.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11
/*
 *  hdac_i915.c - routines for sync between HD-A core and i915 display driver
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <sound/core.h>
#include <sound/hdaudio.h>
#include <sound/hda_i915.h>
12
#include <sound/hda_register.h>
13

14 15
static struct completion bind_complete;

16 17 18 19 20
#define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
				((pci)->device == 0x0c0c) || \
				((pci)->device == 0x0d0c) || \
				((pci)->device == 0x160c))

21
/**
22
 * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
23 24
 * @bus: HDA core bus
 *
25 26 27 28 29 30
 * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
 * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
 * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
 * BCLK = CDCLK * M / N
 * The values will be lost when the display power well is disabled and need to
 * be restored to avoid abnormal playback speed.
31
 *
32 33
 * Call this function at initializing and changing power well, as well as
 * at ELD notifier for the hotplug.
34
 */
35
void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
36
{
37
	struct drm_audio_component *acomp = bus->audio_component;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	struct pci_dev *pci = to_pci_dev(bus->dev);
	int cdclk_freq;
	unsigned int bclk_m, bclk_n;

	if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
		return; /* only for i915 binding */
	if (!CONTROLLER_IN_GPU(pci))
		return; /* only HSW/BDW */

	cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
	switch (cdclk_freq) {
	case 337500:
		bclk_m = 16;
		bclk_n = 225;
		break;

	case 450000:
	default: /* default CDCLK 450MHz */
		bclk_m = 4;
		bclk_n = 75;
		break;

	case 540000:
		bclk_m = 4;
		bclk_n = 90;
		break;

	case 675000:
		bclk_m = 8;
		bclk_n = 225;
		break;
	}
70

71 72
	snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
	snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
73
}
74
EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/**
 * Returns true if the devices can be connected for audio.
 */
static bool connectivity_check(struct pci_dev *i915, struct pci_dev *hdac)
{
	struct pci_bus *bus_a = i915->bus, *bus_b = hdac->bus;

	/* directly connected on the same bus */
	if (bus_a == bus_b)
		return true;

	/*
	 * on i915 discrete GPUs with embedded HDA audio, the two
	 * devices are connected via 2nd level PCI bridge
	 */
	bus_a = bus_a->parent;
	bus_b = bus_b->parent;
	if (!bus_a || !bus_b)
		return false;
	bus_a = bus_a->parent;
	bus_b = bus_b->parent;
	if (bus_a && bus_a == bus_b)
		return true;

	return false;
}

103 104
static int i915_component_master_match(struct device *dev, int subcomponent,
				       void *data)
105
{
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	struct pci_dev *hdac_pci, *i915_pci;
	struct hdac_bus *bus = data;

	if (!dev_is_pci(dev))
		return 0;

	hdac_pci = to_pci_dev(bus->dev);
	i915_pci = to_pci_dev(dev);

	if (!strcmp(dev->driver->name, "i915") &&
	    subcomponent == I915_COMPONENT_AUDIO &&
	    connectivity_check(i915_pci, hdac_pci))
		return 1;

	return 0;
121 122
}

123 124 125
/* check whether intel graphics is present */
static bool i915_gfx_present(void)
{
126
	static const struct pci_device_id ids[] = {
127 128 129 130 131 132 133 134
		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
		  .class = PCI_BASE_CLASS_DISPLAY << 16,
		  .class_mask = 0xff << 16 },
		{}
	};
	return pci_dev_present(ids);
}

135 136 137 138 139 140 141 142 143 144 145 146 147
static int i915_master_bind(struct device *dev,
			    struct drm_audio_component *acomp)
{
	complete_all(&bind_complete);
	/* clear audio_ops here as it was needed only for completion call */
	acomp->audio_ops = NULL;
	return 0;
}

static const struct drm_audio_component_audio_ops i915_init_ops = {
	.master_bind = i915_master_bind
};

148 149 150 151 152 153 154 155 156 157 158 159
/**
 * snd_hdac_i915_init - Initialize i915 audio component
 * @bus: HDA core bus
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function initializes and sets up the audio component to communicate
 * with i915 graphics driver.
 *
 * Returns zero for success or a negative error code.
 */
160 161
int snd_hdac_i915_init(struct hdac_bus *bus)
{
162
	struct drm_audio_component *acomp;
163
	int err;
164

165 166 167
	if (!i915_gfx_present())
		return -ENODEV;

168 169 170
	init_completion(&bind_complete);

	err = snd_hdac_acomp_init(bus, &i915_init_ops,
171 172 173 174 175 176 177
				  i915_component_master_match,
				  sizeof(struct i915_audio_component) - sizeof(*acomp));
	if (err < 0)
		return err;
	acomp = bus->audio_component;
	if (!acomp)
		return -ENODEV;
178
	if (!acomp->ops) {
179 180 181 182 183 184
		if (!IS_ENABLED(CONFIG_MODULES) ||
		    !request_module("i915")) {
			/* 60s timeout */
			wait_for_completion_timeout(&bind_complete,
						   msecs_to_jiffies(60 * 1000));
		}
185
	}
186
	if (!acomp->ops) {
187
		dev_info(bus->dev, "couldn't bind with audio component\n");
188 189
		snd_hdac_acomp_exit(bus);
		return -ENODEV;
190 191 192 193
	}
	return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_i915_init);