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>
24 static struct i915_audio_component
*hdac_acomp
;
27 * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
29 * @enable: enable or disable the wakeup
31 * This function is supposed to be used only by a HD-audio controller
32 * driver that needs the interaction with i915 graphics.
34 * This function should be called during the chip reset, also called at
35 * resume for updating STATESTS register read.
37 * Returns zero for success or a negative error code.
39 int snd_hdac_set_codec_wakeup(struct hdac_bus
*bus
, bool enable
)
41 struct i915_audio_component
*acomp
= bus
->audio_component
;
43 if (!acomp
|| !acomp
->ops
)
46 if (!acomp
->ops
->codec_wake_override
) {
48 "Invalid codec wake callback\n");
52 dev_dbg(bus
->dev
, "%s codec wakeup\n",
53 enable
? "enable" : "disable");
55 acomp
->ops
->codec_wake_override(acomp
->dev
, enable
);
59 EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup
);
62 * snd_hdac_display_power - Power up / down the power refcount
64 * @enable: power up or down
66 * This function is supposed to be used only by a HD-audio controller
67 * driver that needs the interaction with i915 graphics.
69 * This function manages a refcount and calls the i915 get_power() and
70 * put_power() ops accordingly, toggling the codec wakeup, too.
72 * Returns zero for success or a negative error code.
74 int snd_hdac_display_power(struct hdac_bus
*bus
, bool enable
)
76 struct i915_audio_component
*acomp
= bus
->audio_component
;
78 if (!acomp
|| !acomp
->ops
)
81 dev_dbg(bus
->dev
, "display power %s\n",
82 enable
? "enable" : "disable");
85 if (!bus
->i915_power_refcount
++) {
86 acomp
->ops
->get_power(acomp
->dev
);
87 snd_hdac_set_codec_wakeup(bus
, true);
88 snd_hdac_set_codec_wakeup(bus
, false);
91 WARN_ON(!bus
->i915_power_refcount
);
92 if (!--bus
->i915_power_refcount
)
93 acomp
->ops
->put_power(acomp
->dev
);
98 EXPORT_SYMBOL_GPL(snd_hdac_display_power
);
101 * snd_hdac_get_display_clk - Get CDCLK in kHz
104 * This function is supposed to be used only by a HD-audio controller
105 * driver that needs the interaction with i915 graphics.
107 * This function queries CDCLK value in kHz from the graphics driver and
108 * returns the value. A negative code is returned in error.
110 int snd_hdac_get_display_clk(struct hdac_bus
*bus
)
112 struct i915_audio_component
*acomp
= bus
->audio_component
;
114 if (!acomp
|| !acomp
->ops
)
117 return acomp
->ops
->get_cdclk_freq(acomp
->dev
);
119 EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk
);
121 /* There is a fixed mapping between audio pin node and display port
122 * on current Intel platforms:
123 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
124 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
125 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
127 static int pin2port(hda_nid_t pin_nid
)
133 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
135 * @nid: the pin widget NID
136 * @rate: the sample rate to set
138 * This function is supposed to be used only by a HD-audio controller
139 * driver that needs the interaction with i915 graphics.
141 * This function sets N/CTS value based on the given sample rate.
142 * Returns zero for success, or a negative error code.
144 int snd_hdac_sync_audio_rate(struct hdac_bus
*bus
, hda_nid_t nid
, int rate
)
146 struct i915_audio_component
*acomp
= bus
->audio_component
;
148 if (!acomp
|| !acomp
->ops
|| !acomp
->ops
->sync_audio_rate
)
150 return acomp
->ops
->sync_audio_rate(acomp
->dev
, pin2port(nid
), rate
);
152 EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate
);
155 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
157 * @nid: the pin widget NID
158 * @audio_enabled: the pointer to store the current audio state
159 * @buffer: the buffer pointer to store ELD bytes
160 * @max_bytes: the max bytes to be stored on @buffer
162 * This function is supposed to be used only by a HD-audio controller
163 * driver that needs the interaction with i915 graphics.
165 * This function queries the current state of the audio on the given
166 * digital port and fetches the ELD bytes onto the given buffer.
167 * It returns the number of bytes for the total ELD data, zero for
168 * invalid ELD, or a negative error code.
170 * The return size is the total bytes required for the whole ELD bytes,
171 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
172 * that only a part of ELD bytes have been fetched.
174 int snd_hdac_acomp_get_eld(struct hdac_bus
*bus
, hda_nid_t nid
,
175 bool *audio_enabled
, char *buffer
, int max_bytes
)
177 struct i915_audio_component
*acomp
= bus
->audio_component
;
179 if (!acomp
|| !acomp
->ops
|| !acomp
->ops
->get_eld
)
182 return acomp
->ops
->get_eld(acomp
->dev
, pin2port(nid
), audio_enabled
,
185 EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld
);
187 static int hdac_component_master_bind(struct device
*dev
)
189 struct i915_audio_component
*acomp
= hdac_acomp
;
192 ret
= component_bind_all(dev
, acomp
);
196 if (WARN_ON(!(acomp
->dev
&& acomp
->ops
&& acomp
->ops
->get_power
&&
197 acomp
->ops
->put_power
&& acomp
->ops
->get_cdclk_freq
))) {
203 * Atm, we don't support dynamic unbinding initiated by the child
204 * component, so pin its containing module until we unbind.
206 if (!try_module_get(acomp
->ops
->owner
)) {
214 component_unbind_all(dev
, acomp
);
219 static void hdac_component_master_unbind(struct device
*dev
)
221 struct i915_audio_component
*acomp
= hdac_acomp
;
223 module_put(acomp
->ops
->owner
);
224 component_unbind_all(dev
, acomp
);
225 WARN_ON(acomp
->ops
|| acomp
->dev
);
228 static const struct component_master_ops hdac_component_master_ops
= {
229 .bind
= hdac_component_master_bind
,
230 .unbind
= hdac_component_master_unbind
,
233 static int hdac_component_master_match(struct device
*dev
, void *data
)
235 /* i915 is the only supported component */
236 return !strcmp(dev
->driver
->name
, "i915");
240 * snd_hdac_i915_register_notifier - Register i915 audio component ops
241 * @aops: i915 audio component ops
243 * This function is supposed to be used only by a HD-audio controller
244 * driver that needs the interaction with i915 graphics.
246 * This function sets the given ops to be called by the i915 graphics driver.
248 * Returns zero for success or a negative error code.
250 int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops
*aops
)
252 if (WARN_ON(!hdac_acomp
))
255 hdac_acomp
->audio_ops
= aops
;
258 EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier
);
261 * snd_hdac_i915_init - Initialize i915 audio component
264 * This function is supposed to be used only by a HD-audio controller
265 * driver that needs the interaction with i915 graphics.
267 * This function initializes and sets up the audio component to communicate
268 * with i915 graphics driver.
270 * Returns zero for success or a negative error code.
272 int snd_hdac_i915_init(struct hdac_bus
*bus
)
274 struct component_match
*match
= NULL
;
275 struct device
*dev
= bus
->dev
;
276 struct i915_audio_component
*acomp
;
279 acomp
= kzalloc(sizeof(*acomp
), GFP_KERNEL
);
282 bus
->audio_component
= acomp
;
285 component_match_add(dev
, &match
, hdac_component_master_match
, bus
);
286 ret
= component_master_add_with_match(dev
, &hdac_component_master_ops
,
292 * Atm, we don't support deferring the component binding, so make sure
293 * i915 is loaded and that the binding successfully completes.
295 request_module("i915");
301 dev_dbg(dev
, "bound to i915 component master\n");
305 component_master_del(dev
, &hdac_component_master_ops
);
308 bus
->audio_component
= NULL
;
309 dev_info(dev
, "failed to add i915 component master (%d)\n", ret
);
313 EXPORT_SYMBOL_GPL(snd_hdac_i915_init
);
316 * snd_hdac_i915_exit - Finalize i915 audio component
319 * This function is supposed to be used only by a HD-audio controller
320 * driver that needs the interaction with i915 graphics.
322 * This function releases the i915 audio component that has been used.
324 * Returns zero for success or a negative error code.
326 int snd_hdac_i915_exit(struct hdac_bus
*bus
)
328 struct device
*dev
= bus
->dev
;
329 struct i915_audio_component
*acomp
= bus
->audio_component
;
334 WARN_ON(bus
->i915_power_refcount
);
335 if (bus
->i915_power_refcount
> 0 && acomp
->ops
)
336 acomp
->ops
->put_power(acomp
->dev
);
338 component_master_del(dev
, &hdac_component_master_ops
);
341 bus
->audio_component
= NULL
;
345 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit
);