xref: /openbmc/linux/sound/hda/hdac_component.c (revision 3557b3fd)
1 // 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 
13 static void hdac_acomp_release(struct device *dev, void *res)
14 {
15 }
16 
17 static 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  */
35 int 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 }
52 EXPORT_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
57  * @idx: HDA codec address, pass HDA_CODEC_IDX_CONTROLLER for controller
58  * @enable: power up or down
59  *
60  * This function is used by either HD-audio controller or codec driver that
61  * needs the interaction with graphics driver.
62  *
63  * This function updates the power status, and calls the get_power() and
64  * put_power() ops accordingly, toggling the codec wakeup, too.
65  */
66 void snd_hdac_display_power(struct hdac_bus *bus, unsigned int idx, bool enable)
67 {
68 	struct drm_audio_component *acomp = bus->audio_component;
69 
70 	dev_dbg(bus->dev, "display power %s\n",
71 		enable ? "enable" : "disable");
72 
73 	mutex_lock(&bus->lock);
74 	if (enable)
75 		set_bit(idx, &bus->display_power_status);
76 	else
77 		clear_bit(idx, &bus->display_power_status);
78 
79 	if (!acomp || !acomp->ops)
80 		goto unlock;
81 
82 	if (bus->display_power_status) {
83 		if (!bus->display_power_active) {
84 			if (acomp->ops->get_power)
85 				acomp->ops->get_power(acomp->dev);
86 			snd_hdac_set_codec_wakeup(bus, true);
87 			snd_hdac_set_codec_wakeup(bus, false);
88 			bus->display_power_active = true;
89 		}
90 	} else {
91 		if (bus->display_power_active) {
92 			if (acomp->ops->put_power)
93 				acomp->ops->put_power(acomp->dev);
94 			bus->display_power_active = false;
95 		}
96 	}
97  unlock:
98 	mutex_unlock(&bus->lock);
99 }
100 EXPORT_SYMBOL_GPL(snd_hdac_display_power);
101 
102 /**
103  * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
104  * @codec: HDA codec
105  * @nid: the pin widget NID
106  * @dev_id: device identifier
107  * @rate: the sample rate to set
108  *
109  * This function is supposed to be used only by a HD-audio controller
110  * driver that needs the interaction with graphics driver.
111  *
112  * This function sets N/CTS value based on the given sample rate.
113  * Returns zero for success, or a negative error code.
114  */
115 int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
116 			     int dev_id, int rate)
117 {
118 	struct hdac_bus *bus = codec->bus;
119 	struct drm_audio_component *acomp = bus->audio_component;
120 	int port, pipe;
121 
122 	if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
123 		return -ENODEV;
124 	port = nid;
125 	if (acomp->audio_ops && acomp->audio_ops->pin2port) {
126 		port = acomp->audio_ops->pin2port(codec, nid);
127 		if (port < 0)
128 			return -EINVAL;
129 	}
130 	pipe = dev_id;
131 	return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
132 }
133 EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
134 
135 /**
136  * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
137  * @codec: HDA codec
138  * @nid: the pin widget NID
139  * @dev_id: device identifier
140  * @audio_enabled: the pointer to store the current audio state
141  * @buffer: the buffer pointer to store ELD bytes
142  * @max_bytes: the max bytes to be stored on @buffer
143  *
144  * This function is supposed to be used only by a HD-audio controller
145  * driver that needs the interaction with graphics driver.
146  *
147  * This function queries the current state of the audio on the given
148  * digital port and fetches the ELD bytes onto the given buffer.
149  * It returns the number of bytes for the total ELD data, zero for
150  * invalid ELD, or a negative error code.
151  *
152  * The return size is the total bytes required for the whole ELD bytes,
153  * thus it may be over @max_bytes.  If it's over @max_bytes, it implies
154  * that only a part of ELD bytes have been fetched.
155  */
156 int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
157 			   bool *audio_enabled, char *buffer, int max_bytes)
158 {
159 	struct hdac_bus *bus = codec->bus;
160 	struct drm_audio_component *acomp = bus->audio_component;
161 	int port, pipe;
162 
163 	if (!acomp || !acomp->ops || !acomp->ops->get_eld)
164 		return -ENODEV;
165 
166 	port = nid;
167 	if (acomp->audio_ops && acomp->audio_ops->pin2port) {
168 		port = acomp->audio_ops->pin2port(codec, nid);
169 		if (port < 0)
170 			return -EINVAL;
171 	}
172 	pipe = dev_id;
173 	return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
174 				   buffer, max_bytes);
175 }
176 EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
177 
178 static int hdac_component_master_bind(struct device *dev)
179 {
180 	struct drm_audio_component *acomp = hdac_get_acomp(dev);
181 	int ret;
182 
183 	if (WARN_ON(!acomp))
184 		return -EINVAL;
185 
186 	ret = component_bind_all(dev, acomp);
187 	if (ret < 0)
188 		return ret;
189 
190 	if (WARN_ON(!(acomp->dev && acomp->ops))) {
191 		ret = -EINVAL;
192 		goto out_unbind;
193 	}
194 
195 	/* pin the module to avoid dynamic unbinding, but only if given */
196 	if (!try_module_get(acomp->ops->owner)) {
197 		ret = -ENODEV;
198 		goto out_unbind;
199 	}
200 
201 	if (acomp->audio_ops && acomp->audio_ops->master_bind) {
202 		ret = acomp->audio_ops->master_bind(dev, acomp);
203 		if (ret < 0)
204 			goto module_put;
205 	}
206 
207 	return 0;
208 
209  module_put:
210 	module_put(acomp->ops->owner);
211 out_unbind:
212 	component_unbind_all(dev, acomp);
213 
214 	return ret;
215 }
216 
217 static void hdac_component_master_unbind(struct device *dev)
218 {
219 	struct drm_audio_component *acomp = hdac_get_acomp(dev);
220 
221 	if (acomp->audio_ops && acomp->audio_ops->master_unbind)
222 		acomp->audio_ops->master_unbind(dev, acomp);
223 	module_put(acomp->ops->owner);
224 	component_unbind_all(dev, acomp);
225 	WARN_ON(acomp->ops || acomp->dev);
226 }
227 
228 static const struct component_master_ops hdac_component_master_ops = {
229 	.bind = hdac_component_master_bind,
230 	.unbind = hdac_component_master_unbind,
231 };
232 
233 /**
234  * snd_hdac_acomp_register_notifier - Register audio component ops
235  * @bus: HDA core bus
236  * @aops: audio component ops
237  *
238  * This function is supposed to be used only by a HD-audio controller
239  * driver that needs the interaction with graphics driver.
240  *
241  * This function sets the given ops to be called by the graphics driver.
242  *
243  * Returns zero for success or a negative error code.
244  */
245 int snd_hdac_acomp_register_notifier(struct hdac_bus *bus,
246 				    const struct drm_audio_component_audio_ops *aops)
247 {
248 	if (!bus->audio_component)
249 		return -ENODEV;
250 
251 	bus->audio_component->audio_ops = aops;
252 	return 0;
253 }
254 EXPORT_SYMBOL_GPL(snd_hdac_acomp_register_notifier);
255 
256 /**
257  * snd_hdac_acomp_init - Initialize audio component
258  * @bus: HDA core bus
259  * @match_master: match function for finding components
260  * @extra_size: Extra bytes to allocate
261  *
262  * This function is supposed to be used only by a HD-audio controller
263  * driver that needs the interaction with graphics driver.
264  *
265  * This function initializes and sets up the audio component to communicate
266  * with graphics driver.
267  *
268  * Unlike snd_hdac_i915_init(), this function doesn't synchronize with the
269  * binding with the DRM component.  Each caller needs to sync via master_bind
270  * audio_ops.
271  *
272  * Returns zero for success or a negative error code.
273  */
274 int snd_hdac_acomp_init(struct hdac_bus *bus,
275 			const struct drm_audio_component_audio_ops *aops,
276 			int (*match_master)(struct device *, int, void *),
277 			size_t extra_size)
278 {
279 	struct component_match *match = NULL;
280 	struct device *dev = bus->dev;
281 	struct drm_audio_component *acomp;
282 	int ret;
283 
284 	if (WARN_ON(hdac_get_acomp(dev)))
285 		return -EBUSY;
286 
287 	acomp = devres_alloc(hdac_acomp_release, sizeof(*acomp) + extra_size,
288 			     GFP_KERNEL);
289 	if (!acomp)
290 		return -ENOMEM;
291 	acomp->audio_ops = aops;
292 	bus->audio_component = acomp;
293 	devres_add(dev, acomp);
294 
295 	component_match_add_typed(dev, &match, match_master, bus);
296 	ret = component_master_add_with_match(dev, &hdac_component_master_ops,
297 					      match);
298 	if (ret < 0)
299 		goto out_err;
300 
301 	return 0;
302 
303 out_err:
304 	bus->audio_component = NULL;
305 	devres_destroy(dev, hdac_acomp_release, NULL, NULL);
306 	dev_info(dev, "failed to add audio component master (%d)\n", ret);
307 
308 	return ret;
309 }
310 EXPORT_SYMBOL_GPL(snd_hdac_acomp_init);
311 
312 /**
313  * snd_hdac_acomp_exit - Finalize audio component
314  * @bus: HDA core bus
315  *
316  * This function is supposed to be used only by a HD-audio controller
317  * driver that needs the interaction with graphics driver.
318  *
319  * This function releases the audio component that has been used.
320  *
321  * Returns zero for success or a negative error code.
322  */
323 int snd_hdac_acomp_exit(struct hdac_bus *bus)
324 {
325 	struct device *dev = bus->dev;
326 	struct drm_audio_component *acomp = bus->audio_component;
327 
328 	if (!acomp)
329 		return 0;
330 
331 	if (WARN_ON(bus->display_power_active) && acomp->ops)
332 		acomp->ops->put_power(acomp->dev);
333 
334 	bus->display_power_active = false;
335 	bus->display_power_status = 0;
336 
337 	component_master_del(dev, &hdac_component_master_ops);
338 
339 	bus->audio_component = NULL;
340 	devres_destroy(dev, hdac_acomp_release, NULL, NULL);
341 
342 	return 0;
343 }
344 EXPORT_SYMBOL_GPL(snd_hdac_acomp_exit);
345