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