blob: bb37e7e0bd790c0156c5807f44848fd2f44271cd [file] [log] [blame]
Takashi Iwaia57942b2018-07-11 16:23:16 +02001// SPDX-License-Identifier: GPL-2.0
2// hdac_component.c - routines for sync between HD-A core and DRM driver
3
4#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/pci.h>
7#include <linux/component.h>
8#include <sound/core.h>
9#include <sound/hdaudio.h>
10#include <sound/hda_component.h>
11#include <sound/hda_register.h>
12
13static void hdac_acomp_release(struct device *dev, void *res)
14{
15}
16
17static struct drm_audio_component *hdac_get_acomp(struct device *dev)
18{
19 return devres_find(dev, hdac_acomp_release, NULL, NULL);
20}
21
22/**
23 * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
24 * @bus: HDA core bus
25 * @enable: enable or disable the wakeup
26 *
27 * This function is supposed to be used only by a HD-audio controller
28 * driver that needs the interaction with graphics driver.
29 *
30 * This function should be called during the chip reset, also called at
31 * resume for updating STATESTS register read.
32 *
33 * Returns zero for success or a negative error code.
34 */
35int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
36{
37 struct drm_audio_component *acomp = bus->audio_component;
38
39 if (!acomp || !acomp->ops)
40 return -ENODEV;
41
42 if (!acomp->ops->codec_wake_override)
43 return 0;
44
45 dev_dbg(bus->dev, "%s codec wakeup\n",
46 enable ? "enable" : "disable");
47
48 acomp->ops->codec_wake_override(acomp->dev, enable);
49
50 return 0;
51}
52EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
53
54/**
55 * snd_hdac_display_power - Power up / down the power refcount
56 * @bus: HDA core bus
Takashi Iwai029d92c2018-12-08 17:31:49 +010057 * @idx: HDA codec address, pass HDA_CODEC_IDX_CONTROLLER for controller
Takashi Iwaia57942b2018-07-11 16:23:16 +020058 * @enable: power up or down
59 *
Takashi Iwai029d92c2018-12-08 17:31:49 +010060 * This function is used by either HD-audio controller or codec driver that
61 * needs the interaction with graphics driver.
Takashi Iwaia57942b2018-07-11 16:23:16 +020062 *
Takashi Iwai029d92c2018-12-08 17:31:49 +010063 * This function updates the power status, and calls the get_power() and
Takashi Iwaia57942b2018-07-11 16:23:16 +020064 * put_power() ops accordingly, toggling the codec wakeup, too.
Takashi Iwaia57942b2018-07-11 16:23:16 +020065 */
Takashi Iwai4f799e72018-12-09 10:04:25 +010066void snd_hdac_display_power(struct hdac_bus *bus, unsigned int idx, bool enable)
Takashi Iwaia57942b2018-07-11 16:23:16 +020067{
68 struct drm_audio_component *acomp = bus->audio_component;
69
Takashi Iwaia57942b2018-07-11 16:23:16 +020070 dev_dbg(bus->dev, "display power %s\n",
71 enable ? "enable" : "disable");
Takashi Iwaid7a181d2019-04-10 12:49:55 +020072
73 mutex_lock(&bus->lock);
Takashi Iwai029d92c2018-12-08 17:31:49 +010074 if (enable)
75 set_bit(idx, &bus->display_power_status);
76 else
77 clear_bit(idx, &bus->display_power_status);
Takashi Iwaia57942b2018-07-11 16:23:16 +020078
Takashi Iwai029d92c2018-12-08 17:31:49 +010079 if (!acomp || !acomp->ops)
Takashi Iwaid7a181d2019-04-10 12:49:55 +020080 goto unlock;
Takashi Iwai029d92c2018-12-08 17:31:49 +010081
82 if (bus->display_power_status) {
83 if (!bus->display_power_active) {
Chris Wilsond31c85f2019-02-13 15:21:09 +000084 unsigned long cookie = -1;
85
Takashi Iwaia57942b2018-07-11 16:23:16 +020086 if (acomp->ops->get_power)
Chris Wilsond31c85f2019-02-13 15:21:09 +000087 cookie = acomp->ops->get_power(acomp->dev);
88
Takashi Iwaia57942b2018-07-11 16:23:16 +020089 snd_hdac_set_codec_wakeup(bus, true);
90 snd_hdac_set_codec_wakeup(bus, false);
Chris Wilsond31c85f2019-02-13 15:21:09 +000091 bus->display_power_active = cookie;
Takashi Iwaia57942b2018-07-11 16:23:16 +020092 }
93 } else {
Takashi Iwai029d92c2018-12-08 17:31:49 +010094 if (bus->display_power_active) {
Chris Wilsond31c85f2019-02-13 15:21:09 +000095 unsigned long cookie = bus->display_power_active;
96
Takashi Iwaia57942b2018-07-11 16:23:16 +020097 if (acomp->ops->put_power)
Chris Wilsond31c85f2019-02-13 15:21:09 +000098 acomp->ops->put_power(acomp->dev, cookie);
99
100 bus->display_power_active = 0;
Takashi Iwai029d92c2018-12-08 17:31:49 +0100101 }
Takashi Iwaia57942b2018-07-11 16:23:16 +0200102 }
Takashi Iwaid7a181d2019-04-10 12:49:55 +0200103 unlock:
104 mutex_unlock(&bus->lock);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200105}
106EXPORT_SYMBOL_GPL(snd_hdac_display_power);
107
108/**
109 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
110 * @codec: HDA codec
111 * @nid: the pin widget NID
112 * @dev_id: device identifier
113 * @rate: the sample rate to set
114 *
115 * This function is supposed to be used only by a HD-audio controller
116 * driver that needs the interaction with graphics driver.
117 *
118 * This function sets N/CTS value based on the given sample rate.
119 * Returns zero for success, or a negative error code.
120 */
121int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
122 int dev_id, int rate)
123{
124 struct hdac_bus *bus = codec->bus;
125 struct drm_audio_component *acomp = bus->audio_component;
126 int port, pipe;
127
128 if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
129 return -ENODEV;
130 port = nid;
131 if (acomp->audio_ops && acomp->audio_ops->pin2port) {
132 port = acomp->audio_ops->pin2port(codec, nid);
133 if (port < 0)
134 return -EINVAL;
135 }
136 pipe = dev_id;
137 return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
138}
139EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
140
141/**
142 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
143 * @codec: HDA codec
144 * @nid: the pin widget NID
145 * @dev_id: device identifier
146 * @audio_enabled: the pointer to store the current audio state
147 * @buffer: the buffer pointer to store ELD bytes
148 * @max_bytes: the max bytes to be stored on @buffer
149 *
150 * This function is supposed to be used only by a HD-audio controller
151 * driver that needs the interaction with graphics driver.
152 *
153 * This function queries the current state of the audio on the given
154 * digital port and fetches the ELD bytes onto the given buffer.
155 * It returns the number of bytes for the total ELD data, zero for
156 * invalid ELD, or a negative error code.
157 *
158 * The return size is the total bytes required for the whole ELD bytes,
159 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
160 * that only a part of ELD bytes have been fetched.
161 */
162int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
163 bool *audio_enabled, char *buffer, int max_bytes)
164{
165 struct hdac_bus *bus = codec->bus;
166 struct drm_audio_component *acomp = bus->audio_component;
167 int port, pipe;
168
169 if (!acomp || !acomp->ops || !acomp->ops->get_eld)
170 return -ENODEV;
171
172 port = nid;
173 if (acomp->audio_ops && acomp->audio_ops->pin2port) {
174 port = acomp->audio_ops->pin2port(codec, nid);
175 if (port < 0)
176 return -EINVAL;
177 }
178 pipe = dev_id;
179 return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
180 buffer, max_bytes);
181}
182EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
183
184static int hdac_component_master_bind(struct device *dev)
185{
186 struct drm_audio_component *acomp = hdac_get_acomp(dev);
187 int ret;
188
189 if (WARN_ON(!acomp))
190 return -EINVAL;
191
192 ret = component_bind_all(dev, acomp);
193 if (ret < 0)
194 return ret;
195
196 if (WARN_ON(!(acomp->dev && acomp->ops))) {
197 ret = -EINVAL;
198 goto out_unbind;
199 }
200
201 /* pin the module to avoid dynamic unbinding, but only if given */
202 if (!try_module_get(acomp->ops->owner)) {
203 ret = -ENODEV;
204 goto out_unbind;
205 }
206
207 if (acomp->audio_ops && acomp->audio_ops->master_bind) {
208 ret = acomp->audio_ops->master_bind(dev, acomp);
209 if (ret < 0)
210 goto module_put;
211 }
212
Takashi Iwai96e503f2020-10-06 19:17:22 +0300213 complete_all(&acomp->master_bind_complete);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200214 return 0;
215
216 module_put:
217 module_put(acomp->ops->owner);
218out_unbind:
219 component_unbind_all(dev, acomp);
Takashi Iwai96e503f2020-10-06 19:17:22 +0300220 complete_all(&acomp->master_bind_complete);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200221
222 return ret;
223}
224
225static void hdac_component_master_unbind(struct device *dev)
226{
227 struct drm_audio_component *acomp = hdac_get_acomp(dev);
228
229 if (acomp->audio_ops && acomp->audio_ops->master_unbind)
230 acomp->audio_ops->master_unbind(dev, acomp);
231 module_put(acomp->ops->owner);
232 component_unbind_all(dev, acomp);
233 WARN_ON(acomp->ops || acomp->dev);
234}
235
236static const struct component_master_ops hdac_component_master_ops = {
237 .bind = hdac_component_master_bind,
238 .unbind = hdac_component_master_unbind,
239};
240
241/**
242 * snd_hdac_acomp_register_notifier - Register audio component ops
243 * @bus: HDA core bus
244 * @aops: audio component ops
245 *
246 * This function is supposed to be used only by a HD-audio controller
247 * driver that needs the interaction with graphics driver.
248 *
249 * This function sets the given ops to be called by the graphics driver.
250 *
251 * Returns zero for success or a negative error code.
252 */
253int snd_hdac_acomp_register_notifier(struct hdac_bus *bus,
254 const struct drm_audio_component_audio_ops *aops)
255{
256 if (!bus->audio_component)
257 return -ENODEV;
258
259 bus->audio_component->audio_ops = aops;
260 return 0;
261}
262EXPORT_SYMBOL_GPL(snd_hdac_acomp_register_notifier);
263
264/**
265 * snd_hdac_acomp_init - Initialize audio component
266 * @bus: HDA core bus
Keyon Jie6e57188f2020-01-13 14:56:38 -0600267 * @aops: audio component ops
Takashi Iwaia57942b2018-07-11 16:23:16 +0200268 * @match_master: match function for finding components
269 * @extra_size: Extra bytes to allocate
270 *
271 * This function is supposed to be used only by a HD-audio controller
272 * driver that needs the interaction with graphics driver.
273 *
274 * This function initializes and sets up the audio component to communicate
275 * with graphics driver.
276 *
277 * Unlike snd_hdac_i915_init(), this function doesn't synchronize with the
278 * binding with the DRM component. Each caller needs to sync via master_bind
279 * audio_ops.
280 *
281 * Returns zero for success or a negative error code.
282 */
283int snd_hdac_acomp_init(struct hdac_bus *bus,
284 const struct drm_audio_component_audio_ops *aops,
Daniel Vetter8857c7d2019-02-08 00:27:59 +0100285 int (*match_master)(struct device *, int, void *),
Takashi Iwaia57942b2018-07-11 16:23:16 +0200286 size_t extra_size)
287{
288 struct component_match *match = NULL;
289 struct device *dev = bus->dev;
290 struct drm_audio_component *acomp;
291 int ret;
292
293 if (WARN_ON(hdac_get_acomp(dev)))
294 return -EBUSY;
295
296 acomp = devres_alloc(hdac_acomp_release, sizeof(*acomp) + extra_size,
297 GFP_KERNEL);
298 if (!acomp)
299 return -ENOMEM;
300 acomp->audio_ops = aops;
Takashi Iwai96e503f2020-10-06 19:17:22 +0300301 init_completion(&acomp->master_bind_complete);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200302 bus->audio_component = acomp;
303 devres_add(dev, acomp);
304
Daniel Vetter8857c7d2019-02-08 00:27:59 +0100305 component_match_add_typed(dev, &match, match_master, bus);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200306 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
307 match);
308 if (ret < 0)
309 goto out_err;
310
311 return 0;
312
313out_err:
314 bus->audio_component = NULL;
315 devres_destroy(dev, hdac_acomp_release, NULL, NULL);
316 dev_info(dev, "failed to add audio component master (%d)\n", ret);
317
318 return ret;
319}
320EXPORT_SYMBOL_GPL(snd_hdac_acomp_init);
321
322/**
323 * snd_hdac_acomp_exit - Finalize audio component
324 * @bus: HDA core bus
325 *
326 * This function is supposed to be used only by a HD-audio controller
327 * driver that needs the interaction with graphics driver.
328 *
329 * This function releases the audio component that has been used.
330 *
331 * Returns zero for success or a negative error code.
332 */
333int snd_hdac_acomp_exit(struct hdac_bus *bus)
334{
335 struct device *dev = bus->dev;
336 struct drm_audio_component *acomp = bus->audio_component;
337
338 if (!acomp)
339 return 0;
340
Takashi Iwai029d92c2018-12-08 17:31:49 +0100341 if (WARN_ON(bus->display_power_active) && acomp->ops)
Chris Wilsond31c85f2019-02-13 15:21:09 +0000342 acomp->ops->put_power(acomp->dev, bus->display_power_active);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200343
Chris Wilsond31c85f2019-02-13 15:21:09 +0000344 bus->display_power_active = 0;
Takashi Iwai029d92c2018-12-08 17:31:49 +0100345 bus->display_power_status = 0;
346
Takashi Iwaia57942b2018-07-11 16:23:16 +0200347 component_master_del(dev, &hdac_component_master_ops);
348
349 bus->audio_component = NULL;
350 devres_destroy(dev, hdac_acomp_release, NULL, NULL);
351
352 return 0;
353}
354EXPORT_SYMBOL_GPL(snd_hdac_acomp_exit);