2 * hdac_i915.c - routines for sync between HD-A core and i915 display driver
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/component.h>
19 #include <drm/i915_component.h>
20 #include <sound/core.h>
21 #include <sound/hdaudio.h>
22 #include <sound/hda_i915.h>
23 #include <sound/hda_register.h>
25 static struct i915_audio_component
*hdac_acomp
;
28 * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
30 * @enable: enable or disable the wakeup
32 * This function is supposed to be used only by a HD-audio controller
33 * driver that needs the interaction with i915 graphics.
35 * This function should be called during the chip reset, also called at
36 * resume for updating STATESTS register read.
38 * Returns zero for success or a negative error code.
40 int snd_hdac_set_codec_wakeup(struct hdac_bus
*bus
, bool enable
)
42 struct i915_audio_component
*acomp
= bus
->audio_component
;
44 if (!acomp
|| !acomp
->ops
)
47 if (!acomp
->ops
->codec_wake_override
) {
49 "Invalid codec wake callback\n");
53 dev_dbg(bus
->dev
, "%s codec wakeup\n",
54 enable
? "enable" : "disable");
56 acomp
->ops
->codec_wake_override(acomp
->dev
, enable
);
60 EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup
);
63 * snd_hdac_display_power - Power up / down the power refcount
65 * @enable: power up or down
67 * This function is supposed to be used only by a HD-audio controller
68 * driver that needs the interaction with i915 graphics.
70 * This function manages a refcount and calls the i915 get_power() and
71 * put_power() ops accordingly, toggling the codec wakeup, too.
73 * Returns zero for success or a negative error code.
75 int snd_hdac_display_power(struct hdac_bus
*bus
, bool enable
)
77 struct i915_audio_component
*acomp
= bus
->audio_component
;
79 if (!acomp
|| !acomp
->ops
)
82 dev_dbg(bus
->dev
, "display power %s\n",
83 enable
? "enable" : "disable");
86 if (!bus
->i915_power_refcount
++) {
87 acomp
->ops
->get_power(acomp
->dev
);
88 snd_hdac_set_codec_wakeup(bus
, true);
89 snd_hdac_set_codec_wakeup(bus
, false);
92 WARN_ON(!bus
->i915_power_refcount
);
93 if (!--bus
->i915_power_refcount
)
94 acomp
->ops
->put_power(acomp
->dev
);
99 EXPORT_SYMBOL_GPL(snd_hdac_display_power
);
101 #define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
102 ((pci)->device == 0x0c0c) || \
103 ((pci)->device == 0x0d0c) || \
104 ((pci)->device == 0x160c))
107 * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
110 * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
111 * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
112 * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
113 * BCLK = CDCLK * M / N
114 * The values will be lost when the display power well is disabled and need to
115 * be restored to avoid abnormal playback speed.
117 * Call this function at initializing and changing power well, as well as
118 * at ELD notifier for the hotplug.
120 void snd_hdac_i915_set_bclk(struct hdac_bus
*bus
)
122 struct i915_audio_component
*acomp
= bus
->audio_component
;
123 struct pci_dev
*pci
= to_pci_dev(bus
->dev
);
125 unsigned int bclk_m
, bclk_n
;
127 if (!acomp
|| !acomp
->ops
|| !acomp
->ops
->get_cdclk_freq
)
128 return; /* only for i915 binding */
129 if (!CONTROLLER_IN_GPU(pci
))
130 return; /* only HSW/BDW */
132 cdclk_freq
= acomp
->ops
->get_cdclk_freq(acomp
->dev
);
133 switch (cdclk_freq
) {
140 default: /* default CDCLK 450MHz */
156 snd_hdac_chip_writew(bus
, HSW_EM4
, bclk_m
);
157 snd_hdac_chip_writew(bus
, HSW_EM5
, bclk_n
);
159 EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk
);
161 /* There is a fixed mapping between audio pin node and display port.
162 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
163 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
164 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
165 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
168 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
169 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
170 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
172 static int pin2port(struct hdac_device
*codec
, hda_nid_t pin_nid
)
176 switch (codec
->vendor_id
) {
177 case 0x80860054: /* ILK */
178 case 0x80862804: /* ILK */
179 case 0x80862882: /* VLV */
187 if (WARN_ON(pin_nid
<= base_nid
|| pin_nid
> base_nid
+ 3))
189 return pin_nid
- base_nid
;
193 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
195 * @nid: the pin widget NID
196 * @rate: the sample rate to set
198 * This function is supposed to be used only by a HD-audio controller
199 * driver that needs the interaction with i915 graphics.
201 * This function sets N/CTS value based on the given sample rate.
202 * Returns zero for success, or a negative error code.
204 int snd_hdac_sync_audio_rate(struct hdac_device
*codec
, hda_nid_t nid
, int rate
)
206 struct hdac_bus
*bus
= codec
->bus
;
207 struct i915_audio_component
*acomp
= bus
->audio_component
;
210 if (!acomp
|| !acomp
->ops
|| !acomp
->ops
->sync_audio_rate
)
212 port
= pin2port(codec
, nid
);
215 return acomp
->ops
->sync_audio_rate(acomp
->dev
, port
, rate
);
217 EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate
);
220 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
222 * @nid: the pin widget NID
223 * @audio_enabled: the pointer to store the current audio state
224 * @buffer: the buffer pointer to store ELD bytes
225 * @max_bytes: the max bytes to be stored on @buffer
227 * This function is supposed to be used only by a HD-audio controller
228 * driver that needs the interaction with i915 graphics.
230 * This function queries the current state of the audio on the given
231 * digital port and fetches the ELD bytes onto the given buffer.
232 * It returns the number of bytes for the total ELD data, zero for
233 * invalid ELD, or a negative error code.
235 * The return size is the total bytes required for the whole ELD bytes,
236 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
237 * that only a part of ELD bytes have been fetched.
239 int snd_hdac_acomp_get_eld(struct hdac_device
*codec
, hda_nid_t nid
,
240 bool *audio_enabled
, char *buffer
, int max_bytes
)
242 struct hdac_bus
*bus
= codec
->bus
;
243 struct i915_audio_component
*acomp
= bus
->audio_component
;
246 if (!acomp
|| !acomp
->ops
|| !acomp
->ops
->get_eld
)
249 port
= pin2port(codec
, nid
);
252 return acomp
->ops
->get_eld(acomp
->dev
, port
, audio_enabled
,
255 EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld
);
257 static int hdac_component_master_bind(struct device
*dev
)
259 struct i915_audio_component
*acomp
= hdac_acomp
;
262 ret
= component_bind_all(dev
, acomp
);
266 if (WARN_ON(!(acomp
->dev
&& acomp
->ops
&& acomp
->ops
->get_power
&&
267 acomp
->ops
->put_power
&& acomp
->ops
->get_cdclk_freq
))) {
273 * Atm, we don't support dynamic unbinding initiated by the child
274 * component, so pin its containing module until we unbind.
276 if (!try_module_get(acomp
->ops
->owner
)) {
284 component_unbind_all(dev
, acomp
);
289 static void hdac_component_master_unbind(struct device
*dev
)
291 struct i915_audio_component
*acomp
= hdac_acomp
;
293 module_put(acomp
->ops
->owner
);
294 component_unbind_all(dev
, acomp
);
295 WARN_ON(acomp
->ops
|| acomp
->dev
);
298 static const struct component_master_ops hdac_component_master_ops
= {
299 .bind
= hdac_component_master_bind
,
300 .unbind
= hdac_component_master_unbind
,
303 static int hdac_component_master_match(struct device
*dev
, void *data
)
305 /* i915 is the only supported component */
306 return !strcmp(dev
->driver
->name
, "i915");
310 * snd_hdac_i915_register_notifier - Register i915 audio component ops
311 * @aops: i915 audio component ops
313 * This function is supposed to be used only by a HD-audio controller
314 * driver that needs the interaction with i915 graphics.
316 * This function sets the given ops to be called by the i915 graphics driver.
318 * Returns zero for success or a negative error code.
320 int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops
*aops
)
325 hdac_acomp
->audio_ops
= aops
;
328 EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier
);
330 /* check whether intel graphics is present */
331 static bool i915_gfx_present(void)
333 static struct pci_device_id ids
[] = {
334 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_ANY_ID
),
335 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
336 .class_mask
= 0xff << 16 },
339 return pci_dev_present(ids
);
343 * snd_hdac_i915_init - Initialize i915 audio component
346 * This function is supposed to be used only by a HD-audio controller
347 * driver that needs the interaction with i915 graphics.
349 * This function initializes and sets up the audio component to communicate
350 * with i915 graphics driver.
352 * Returns zero for success or a negative error code.
354 int snd_hdac_i915_init(struct hdac_bus
*bus
)
356 struct component_match
*match
= NULL
;
357 struct device
*dev
= bus
->dev
;
358 struct i915_audio_component
*acomp
;
361 if (WARN_ON(hdac_acomp
))
364 if (!i915_gfx_present())
367 acomp
= kzalloc(sizeof(*acomp
), GFP_KERNEL
);
370 bus
->audio_component
= acomp
;
373 component_match_add(dev
, &match
, hdac_component_master_match
, bus
);
374 ret
= component_master_add_with_match(dev
, &hdac_component_master_ops
,
380 * Atm, we don't support deferring the component binding, so make sure
381 * i915 is loaded and that the binding successfully completes.
383 request_module("i915");
389 dev_dbg(dev
, "bound to i915 component master\n");
393 component_master_del(dev
, &hdac_component_master_ops
);
396 bus
->audio_component
= NULL
;
398 dev_info(dev
, "failed to add i915 component master (%d)\n", ret
);
402 EXPORT_SYMBOL_GPL(snd_hdac_i915_init
);
405 * snd_hdac_i915_exit - Finalize i915 audio component
408 * This function is supposed to be used only by a HD-audio controller
409 * driver that needs the interaction with i915 graphics.
411 * This function releases the i915 audio component that has been used.
413 * Returns zero for success or a negative error code.
415 int snd_hdac_i915_exit(struct hdac_bus
*bus
)
417 struct device
*dev
= bus
->dev
;
418 struct i915_audio_component
*acomp
= bus
->audio_component
;
423 WARN_ON(bus
->i915_power_refcount
);
424 if (bus
->i915_power_refcount
> 0 && acomp
->ops
)
425 acomp
->ops
->put_power(acomp
->dev
);
427 component_master_del(dev
, &hdac_component_master_ops
);
430 bus
->audio_component
= NULL
;
435 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit
);