1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * hdac_i915.c - routines for sync between HD-A core and i915 display driver
4 */
5
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 #include <sound/core.h>
10 #include <sound/hdaudio.h>
11 #include <sound/hda_i915.h>
12 #include <sound/hda_register.h>
13
14 /**
15 * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
16 * @bus: HDA core bus
17 *
18 * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
19 * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
20 * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
21 * BCLK = CDCLK * M / N
22 * The values will be lost when the display power well is disabled and need to
23 * be restored to avoid abnormal playback speed.
24 *
25 * Call this function at initializing and changing power well, as well as
26 * at ELD notifier for the hotplug.
27 */
snd_hdac_i915_set_bclk(struct hdac_bus * bus)28 void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
29 {
30 struct drm_audio_component *acomp = bus->audio_component;
31 struct pci_dev *pci = to_pci_dev(bus->dev);
32 int cdclk_freq;
33 unsigned int bclk_m, bclk_n;
34
35 if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
36 return; /* only for i915 binding */
37 if (!HDA_CONTROLLER_IS_HSW(pci))
38 return; /* only HSW/BDW */
39
40 cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
41 switch (cdclk_freq) {
42 case 337500:
43 bclk_m = 16;
44 bclk_n = 225;
45 break;
46
47 case 450000:
48 default: /* default CDCLK 450MHz */
49 bclk_m = 4;
50 bclk_n = 75;
51 break;
52
53 case 540000:
54 bclk_m = 4;
55 bclk_n = 90;
56 break;
57
58 case 675000:
59 bclk_m = 8;
60 bclk_n = 225;
61 break;
62 }
63
64 snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
65 snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
66 }
67 EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
68
69 /* returns true if the devices can be connected for audio */
connectivity_check(struct pci_dev * i915,struct pci_dev * hdac)70 static bool connectivity_check(struct pci_dev *i915, struct pci_dev *hdac)
71 {
72 struct pci_bus *bus_a = i915->bus, *bus_b = hdac->bus;
73
74 /* directly connected on the same bus */
75 if (bus_a == bus_b)
76 return true;
77
78 bus_a = bus_a->parent;
79 bus_b = bus_b->parent;
80
81 /* connected via parent bus (may be NULL!) */
82 if (bus_a == bus_b)
83 return true;
84
85 if (!bus_a || !bus_b)
86 return false;
87
88 /*
89 * on i915 discrete GPUs with embedded HDA audio, the two
90 * devices are connected via 2nd level PCI bridge
91 */
92 bus_a = bus_a->parent;
93 bus_b = bus_b->parent;
94 if (bus_a && bus_a == bus_b)
95 return true;
96
97 return false;
98 }
99
i915_component_master_match(struct device * dev,int subcomponent,void * data)100 static int i915_component_master_match(struct device *dev, int subcomponent,
101 void *data)
102 {
103 struct pci_dev *hdac_pci, *i915_pci;
104 struct hdac_bus *bus = data;
105
106 if (!dev_is_pci(dev))
107 return 0;
108
109 hdac_pci = to_pci_dev(bus->dev);
110 i915_pci = to_pci_dev(dev);
111
112 if (!strcmp(dev->driver->name, "i915") &&
113 subcomponent == I915_COMPONENT_AUDIO &&
114 connectivity_check(i915_pci, hdac_pci))
115 return 1;
116
117 return 0;
118 }
119
120 /* check whether Intel graphics is present and reachable */
i915_gfx_present(struct pci_dev * hdac_pci)121 static int i915_gfx_present(struct pci_dev *hdac_pci)
122 {
123 struct pci_dev *display_dev = NULL;
124
125 for_each_pci_dev(display_dev) {
126 if (display_dev->vendor == PCI_VENDOR_ID_INTEL &&
127 (display_dev->class >> 16) == PCI_BASE_CLASS_DISPLAY &&
128 connectivity_check(display_dev, hdac_pci)) {
129 pci_dev_put(display_dev);
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 /**
138 * snd_hdac_i915_init - Initialize i915 audio component
139 * @bus: HDA core bus
140 *
141 * This function is supposed to be used only by a HD-audio controller
142 * driver that needs the interaction with i915 graphics.
143 *
144 * This function initializes and sets up the audio component to communicate
145 * with i915 graphics driver.
146 *
147 * Returns zero for success or a negative error code.
148 */
snd_hdac_i915_init(struct hdac_bus * bus)149 int snd_hdac_i915_init(struct hdac_bus *bus)
150 {
151 struct drm_audio_component *acomp;
152 int err;
153
154 if (!i915_gfx_present(to_pci_dev(bus->dev)))
155 return -ENODEV;
156
157 err = snd_hdac_acomp_init(bus, NULL,
158 i915_component_master_match,
159 sizeof(struct i915_audio_component) - sizeof(*acomp));
160 if (err < 0)
161 return err;
162 acomp = bus->audio_component;
163 if (!acomp)
164 return -ENODEV;
165 if (!acomp->ops) {
166 if (!IS_ENABLED(CONFIG_MODULES) ||
167 !request_module("i915")) {
168 /* 60s timeout */
169 wait_for_completion_killable_timeout(&acomp->master_bind_complete,
170 msecs_to_jiffies(60 * 1000));
171 }
172 }
173 if (!acomp->ops) {
174 dev_info(bus->dev, "couldn't bind with audio component\n");
175 snd_hdac_acomp_exit(bus);
176 return -ENODEV;
177 }
178 return 0;
179 }
180 EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
181