xref: /linux/sound/hda/hdac_i915.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
298d8fc6cSMengdong Lin /*
398d8fc6cSMengdong Lin  *  hdac_i915.c - routines for sync between HD-A core and i915 display driver
498d8fc6cSMengdong Lin  */
598d8fc6cSMengdong Lin 
698d8fc6cSMengdong Lin #include <linux/init.h>
798d8fc6cSMengdong Lin #include <linux/module.h>
898d8fc6cSMengdong Lin #include <linux/pci.h>
998d8fc6cSMengdong Lin #include <sound/core.h>
1098d8fc6cSMengdong Lin #include <sound/hdaudio.h>
1198d8fc6cSMengdong Lin #include <sound/hda_i915.h>
12bb03ed21STakashi Iwai #include <sound/hda_register.h>
132e8c9038SMaarten Lankhorst #include <video/nomodeset.h>
142e8c9038SMaarten Lankhorst 
152e8c9038SMaarten Lankhorst static int gpu_bind = -1;
162e8c9038SMaarten Lankhorst module_param(gpu_bind, int, 0644);
172e8c9038SMaarten Lankhorst MODULE_PARM_DESC(gpu_bind, "Whether to bind sound component to GPU "
182e8c9038SMaarten Lankhorst 			   "(1=always, 0=never, -1=on nomodeset(default))");
1998d8fc6cSMengdong Lin 
2078dd5e21STakashi Iwai /**
21bb03ed21STakashi Iwai  * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
2278dd5e21STakashi Iwai  * @bus: HDA core bus
2378dd5e21STakashi Iwai  *
24bb03ed21STakashi Iwai  * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
25bb03ed21STakashi Iwai  * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
26bb03ed21STakashi Iwai  * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
27bb03ed21STakashi Iwai  * BCLK = CDCLK * M / N
28bb03ed21STakashi Iwai  * The values will be lost when the display power well is disabled and need to
29bb03ed21STakashi Iwai  * be restored to avoid abnormal playback speed.
3078dd5e21STakashi Iwai  *
31bb03ed21STakashi Iwai  * Call this function at initializing and changing power well, as well as
32bb03ed21STakashi Iwai  * at ELD notifier for the hotplug.
3378dd5e21STakashi Iwai  */
snd_hdac_i915_set_bclk(struct hdac_bus * bus)34bb03ed21STakashi Iwai void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
3598d8fc6cSMengdong Lin {
36ae891abeSTakashi Iwai 	struct drm_audio_component *acomp = bus->audio_component;
37bb03ed21STakashi Iwai 	struct pci_dev *pci = to_pci_dev(bus->dev);
38bb03ed21STakashi Iwai 	int cdclk_freq;
39bb03ed21STakashi Iwai 	unsigned int bclk_m, bclk_n;
4098d8fc6cSMengdong Lin 
41bb03ed21STakashi Iwai 	if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
42bb03ed21STakashi Iwai 		return; /* only for i915 binding */
43fd6f3a84SAmadeusz Sławiński 	if (!HDA_CONTROLLER_IS_HSW(pci))
44bb03ed21STakashi Iwai 		return; /* only HSW/BDW */
4598d8fc6cSMengdong Lin 
46bb03ed21STakashi Iwai 	cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
47bb03ed21STakashi Iwai 	switch (cdclk_freq) {
48bb03ed21STakashi Iwai 	case 337500:
49bb03ed21STakashi Iwai 		bclk_m = 16;
50bb03ed21STakashi Iwai 		bclk_n = 225;
51bb03ed21STakashi Iwai 		break;
52bb03ed21STakashi Iwai 
53bb03ed21STakashi Iwai 	case 450000:
54bb03ed21STakashi Iwai 	default: /* default CDCLK 450MHz */
55bb03ed21STakashi Iwai 		bclk_m = 4;
56bb03ed21STakashi Iwai 		bclk_n = 75;
57bb03ed21STakashi Iwai 		break;
58bb03ed21STakashi Iwai 
59bb03ed21STakashi Iwai 	case 540000:
60bb03ed21STakashi Iwai 		bclk_m = 4;
61bb03ed21STakashi Iwai 		bclk_n = 90;
62bb03ed21STakashi Iwai 		break;
63bb03ed21STakashi Iwai 
64bb03ed21STakashi Iwai 	case 675000:
65bb03ed21STakashi Iwai 		bclk_m = 8;
66bb03ed21STakashi Iwai 		bclk_n = 225;
67bb03ed21STakashi Iwai 		break;
6898d8fc6cSMengdong Lin 	}
69bb03ed21STakashi Iwai 
70bb03ed21STakashi Iwai 	snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
71bb03ed21STakashi Iwai 	snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
72bb03ed21STakashi Iwai }
73bb03ed21STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
7498d8fc6cSMengdong Lin 
75502f389aSKai Vehmanen /* returns true if the devices can be connected for audio */
connectivity_check(struct pci_dev * i915,struct pci_dev * hdac)767b882fe3SKai Vehmanen static bool connectivity_check(struct pci_dev *i915, struct pci_dev *hdac)
777b882fe3SKai Vehmanen {
787b882fe3SKai Vehmanen 	struct pci_bus *bus_a = i915->bus, *bus_b = hdac->bus;
797b882fe3SKai Vehmanen 
807b882fe3SKai Vehmanen 	/* directly connected on the same bus */
817b882fe3SKai Vehmanen 	if (bus_a == bus_b)
827b882fe3SKai Vehmanen 		return true;
837b882fe3SKai Vehmanen 
843f8c530fSKai Vehmanen 	bus_a = bus_a->parent;
853f8c530fSKai Vehmanen 	bus_b = bus_b->parent;
863f8c530fSKai Vehmanen 
873f8c530fSKai Vehmanen 	/* connected via parent bus (may be NULL!) */
883f8c530fSKai Vehmanen 	if (bus_a == bus_b)
893f8c530fSKai Vehmanen 		return true;
903f8c530fSKai Vehmanen 
913f8c530fSKai Vehmanen 	if (!bus_a || !bus_b)
923f8c530fSKai Vehmanen 		return false;
933f8c530fSKai Vehmanen 
947b882fe3SKai Vehmanen 	/*
957b882fe3SKai Vehmanen 	 * on i915 discrete GPUs with embedded HDA audio, the two
967b882fe3SKai Vehmanen 	 * devices are connected via 2nd level PCI bridge
977b882fe3SKai Vehmanen 	 */
987b882fe3SKai Vehmanen 	bus_a = bus_a->parent;
997b882fe3SKai Vehmanen 	bus_b = bus_b->parent;
1007b882fe3SKai Vehmanen 	if (bus_a && bus_a == bus_b)
1017b882fe3SKai Vehmanen 		return true;
1027b882fe3SKai Vehmanen 
1037b882fe3SKai Vehmanen 	return false;
1047b882fe3SKai Vehmanen }
1057b882fe3SKai Vehmanen 
i915_component_master_match(struct device * dev,int subcomponent,void * data)1068857c7d0SDaniel Vetter static int i915_component_master_match(struct device *dev, int subcomponent,
1078857c7d0SDaniel Vetter 				       void *data)
108e2dc7d7dSTakashi Iwai {
1097b882fe3SKai Vehmanen 	struct pci_dev *hdac_pci, *i915_pci;
1107b882fe3SKai Vehmanen 	struct hdac_bus *bus = data;
1117b882fe3SKai Vehmanen 
1127b882fe3SKai Vehmanen 	if (!dev_is_pci(dev))
1137b882fe3SKai Vehmanen 		return 0;
1147b882fe3SKai Vehmanen 
1157b882fe3SKai Vehmanen 	hdac_pci = to_pci_dev(bus->dev);
1167b882fe3SKai Vehmanen 	i915_pci = to_pci_dev(dev);
1177b882fe3SKai Vehmanen 
118e8e3f869SMaarten Lankhorst 	if ((!strcmp(dev->driver->name, "i915") ||
119e8e3f869SMaarten Lankhorst 		 !strcmp(dev->driver->name, "xe")) &&
1207b882fe3SKai Vehmanen 	    subcomponent == I915_COMPONENT_AUDIO &&
1217b882fe3SKai Vehmanen 	    connectivity_check(i915_pci, hdac_pci))
1227b882fe3SKai Vehmanen 		return 1;
1237b882fe3SKai Vehmanen 
1247b882fe3SKai Vehmanen 	return 0;
12598d8fc6cSMengdong Lin }
12698d8fc6cSMengdong Lin 
127c9db8a30SKai Vehmanen /* check whether Intel graphics is present and reachable */
i915_gfx_present(struct pci_dev * hdac_pci)128c9db8a30SKai Vehmanen static int i915_gfx_present(struct pci_dev *hdac_pci)
129bfa5fb14STakashi Iwai {
130*bd6e4c4aSCezary Rojewski 	/* List of known platforms with no i915 support. */
131*bd6e4c4aSCezary Rojewski 	static const struct pci_device_id denylist[] = {
132*bd6e4c4aSCezary Rojewski 		/* CNL */
133*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a40), 0x030000, 0xff0000 },
134*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a41), 0x030000, 0xff0000 },
135*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a42), 0x030000, 0xff0000 },
136*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a44), 0x030000, 0xff0000 },
137*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a49), 0x030000, 0xff0000 },
138*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a4a), 0x030000, 0xff0000 },
139*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a4c), 0x030000, 0xff0000 },
140*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a50), 0x030000, 0xff0000 },
141*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a51), 0x030000, 0xff0000 },
142*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a52), 0x030000, 0xff0000 },
143*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a54), 0x030000, 0xff0000 },
144*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a59), 0x030000, 0xff0000 },
145*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a5a), 0x030000, 0xff0000 },
146*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a5c), 0x030000, 0xff0000 },
147*bd6e4c4aSCezary Rojewski 		/* LKF */
148*bd6e4c4aSCezary Rojewski 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x9840), 0x030000, 0xff0000 },
149*bd6e4c4aSCezary Rojewski 		{}
150*bd6e4c4aSCezary Rojewski 	};
151c9db8a30SKai Vehmanen 	struct pci_dev *display_dev = NULL;
152c9db8a30SKai Vehmanen 
1532e8c9038SMaarten Lankhorst 	if (!gpu_bind || (gpu_bind < 0 && video_firmware_drivers_only()))
1542e8c9038SMaarten Lankhorst 		return false;
1552e8c9038SMaarten Lankhorst 
15636a38c53STakashi Iwai 	for_each_pci_dev(display_dev) {
157*bd6e4c4aSCezary Rojewski 		if (display_dev->vendor != PCI_VENDOR_ID_INTEL ||
158*bd6e4c4aSCezary Rojewski 		    (display_dev->class >> 16) != PCI_BASE_CLASS_DISPLAY)
159*bd6e4c4aSCezary Rojewski 			continue;
160*bd6e4c4aSCezary Rojewski 
161*bd6e4c4aSCezary Rojewski 		if (pci_match_id(denylist, display_dev))
162*bd6e4c4aSCezary Rojewski 			continue;
163*bd6e4c4aSCezary Rojewski 
164*bd6e4c4aSCezary Rojewski 		if (connectivity_check(display_dev, hdac_pci)) {
165c9db8a30SKai Vehmanen 			pci_dev_put(display_dev);
16636a38c53STakashi Iwai 			return true;
16700fd7cfaSLucas De Marchi 		}
16836a38c53STakashi Iwai 	}
169c9db8a30SKai Vehmanen 
17036a38c53STakashi Iwai 	return false;
171bfa5fb14STakashi Iwai }
172bfa5fb14STakashi Iwai 
17378dd5e21STakashi Iwai /**
17478dd5e21STakashi Iwai  * snd_hdac_i915_init - Initialize i915 audio component
17578dd5e21STakashi Iwai  * @bus: HDA core bus
17678dd5e21STakashi Iwai  *
17778dd5e21STakashi Iwai  * This function is supposed to be used only by a HD-audio controller
17878dd5e21STakashi Iwai  * driver that needs the interaction with i915 graphics.
17978dd5e21STakashi Iwai  *
18078dd5e21STakashi Iwai  * This function initializes and sets up the audio component to communicate
18178dd5e21STakashi Iwai  * with i915 graphics driver.
18278dd5e21STakashi Iwai  *
18378dd5e21STakashi Iwai  * Returns zero for success or a negative error code.
18478dd5e21STakashi Iwai  */
snd_hdac_i915_init(struct hdac_bus * bus)185e6d0c13eSMaarten Lankhorst int snd_hdac_i915_init(struct hdac_bus *bus)
18698d8fc6cSMengdong Lin {
187ae891abeSTakashi Iwai 	struct drm_audio_component *acomp;
188a57942bfSTakashi Iwai 	int err;
189d745f5e7STakashi Iwai 
190c9db8a30SKai Vehmanen 	if (!i915_gfx_present(to_pci_dev(bus->dev)))
191bfa5fb14STakashi Iwai 		return -ENODEV;
192bfa5fb14STakashi Iwai 
19396e503f9STakashi Iwai 	err = snd_hdac_acomp_init(bus, NULL,
194a57942bfSTakashi Iwai 				  i915_component_master_match,
195a57942bfSTakashi Iwai 				  sizeof(struct i915_audio_component) - sizeof(*acomp));
196a57942bfSTakashi Iwai 	if (err < 0)
197a57942bfSTakashi Iwai 		return err;
198a57942bfSTakashi Iwai 	acomp = bus->audio_component;
199a57942bfSTakashi Iwai 	if (!acomp)
200a57942bfSTakashi Iwai 		return -ENODEV;
20198d8fc6cSMengdong Lin 	if (!acomp->ops) {
202a57942bfSTakashi Iwai 		snd_hdac_acomp_exit(bus);
203e6d0c13eSMaarten Lankhorst 		return dev_err_probe(bus->dev, -EPROBE_DEFER,
204e6d0c13eSMaarten Lankhorst 				     "couldn't bind with audio component\n");
20598d8fc6cSMengdong Lin 	}
20698d8fc6cSMengdong Lin 	return 0;
20798d8fc6cSMengdong Lin }
20898d8fc6cSMengdong Lin EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
209