blob: 5c95933e739a43bc5cd30829e43c0381cff1e989 [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 Iwai029d92c2018-12-08 17:31:49 +010072 if (enable)
73 set_bit(idx, &bus->display_power_status);
74 else
75 clear_bit(idx, &bus->display_power_status);
Takashi Iwaia57942b2018-07-11 16:23:16 +020076
Takashi Iwai029d92c2018-12-08 17:31:49 +010077 if (!acomp || !acomp->ops)
Takashi Iwai4f799e72018-12-09 10:04:25 +010078 return;
Takashi Iwai029d92c2018-12-08 17:31:49 +010079
80 if (bus->display_power_status) {
81 if (!bus->display_power_active) {
Takashi Iwaia57942b2018-07-11 16:23:16 +020082 if (acomp->ops->get_power)
83 acomp->ops->get_power(acomp->dev);
84 snd_hdac_set_codec_wakeup(bus, true);
85 snd_hdac_set_codec_wakeup(bus, false);
Takashi Iwai029d92c2018-12-08 17:31:49 +010086 bus->display_power_active = true;
Takashi Iwaia57942b2018-07-11 16:23:16 +020087 }
88 } else {
Takashi Iwai029d92c2018-12-08 17:31:49 +010089 if (bus->display_power_active) {
Takashi Iwaia57942b2018-07-11 16:23:16 +020090 if (acomp->ops->put_power)
91 acomp->ops->put_power(acomp->dev);
Takashi Iwai029d92c2018-12-08 17:31:49 +010092 bus->display_power_active = false;
93 }
Takashi Iwaia57942b2018-07-11 16:23:16 +020094 }
Takashi Iwaia57942b2018-07-11 16:23:16 +020095}
96EXPORT_SYMBOL_GPL(snd_hdac_display_power);
97
98/**
99 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
100 * @codec: HDA codec
101 * @nid: the pin widget NID
102 * @dev_id: device identifier
103 * @rate: the sample rate to set
104 *
105 * This function is supposed to be used only by a HD-audio controller
106 * driver that needs the interaction with graphics driver.
107 *
108 * This function sets N/CTS value based on the given sample rate.
109 * Returns zero for success, or a negative error code.
110 */
111int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
112 int dev_id, int rate)
113{
114 struct hdac_bus *bus = codec->bus;
115 struct drm_audio_component *acomp = bus->audio_component;
116 int port, pipe;
117
118 if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
119 return -ENODEV;
120 port = nid;
121 if (acomp->audio_ops && acomp->audio_ops->pin2port) {
122 port = acomp->audio_ops->pin2port(codec, nid);
123 if (port < 0)
124 return -EINVAL;
125 }
126 pipe = dev_id;
127 return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
128}
129EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
130
131/**
132 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
133 * @codec: HDA codec
134 * @nid: the pin widget NID
135 * @dev_id: device identifier
136 * @audio_enabled: the pointer to store the current audio state
137 * @buffer: the buffer pointer to store ELD bytes
138 * @max_bytes: the max bytes to be stored on @buffer
139 *
140 * This function is supposed to be used only by a HD-audio controller
141 * driver that needs the interaction with graphics driver.
142 *
143 * This function queries the current state of the audio on the given
144 * digital port and fetches the ELD bytes onto the given buffer.
145 * It returns the number of bytes for the total ELD data, zero for
146 * invalid ELD, or a negative error code.
147 *
148 * The return size is the total bytes required for the whole ELD bytes,
149 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
150 * that only a part of ELD bytes have been fetched.
151 */
152int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
153 bool *audio_enabled, char *buffer, int max_bytes)
154{
155 struct hdac_bus *bus = codec->bus;
156 struct drm_audio_component *acomp = bus->audio_component;
157 int port, pipe;
158
159 if (!acomp || !acomp->ops || !acomp->ops->get_eld)
160 return -ENODEV;
161
162 port = nid;
163 if (acomp->audio_ops && acomp->audio_ops->pin2port) {
164 port = acomp->audio_ops->pin2port(codec, nid);
165 if (port < 0)
166 return -EINVAL;
167 }
168 pipe = dev_id;
169 return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
170 buffer, max_bytes);
171}
172EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
173
174static int hdac_component_master_bind(struct device *dev)
175{
176 struct drm_audio_component *acomp = hdac_get_acomp(dev);
177 int ret;
178
179 if (WARN_ON(!acomp))
180 return -EINVAL;
181
182 ret = component_bind_all(dev, acomp);
183 if (ret < 0)
184 return ret;
185
186 if (WARN_ON(!(acomp->dev && acomp->ops))) {
187 ret = -EINVAL;
188 goto out_unbind;
189 }
190
191 /* pin the module to avoid dynamic unbinding, but only if given */
192 if (!try_module_get(acomp->ops->owner)) {
193 ret = -ENODEV;
194 goto out_unbind;
195 }
196
197 if (acomp->audio_ops && acomp->audio_ops->master_bind) {
198 ret = acomp->audio_ops->master_bind(dev, acomp);
199 if (ret < 0)
200 goto module_put;
201 }
202
203 return 0;
204
205 module_put:
206 module_put(acomp->ops->owner);
207out_unbind:
208 component_unbind_all(dev, acomp);
209
210 return ret;
211}
212
213static void hdac_component_master_unbind(struct device *dev)
214{
215 struct drm_audio_component *acomp = hdac_get_acomp(dev);
216
217 if (acomp->audio_ops && acomp->audio_ops->master_unbind)
218 acomp->audio_ops->master_unbind(dev, acomp);
219 module_put(acomp->ops->owner);
220 component_unbind_all(dev, acomp);
221 WARN_ON(acomp->ops || acomp->dev);
222}
223
224static const struct component_master_ops hdac_component_master_ops = {
225 .bind = hdac_component_master_bind,
226 .unbind = hdac_component_master_unbind,
227};
228
229/**
230 * snd_hdac_acomp_register_notifier - Register audio component ops
231 * @bus: HDA core bus
232 * @aops: audio component ops
233 *
234 * This function is supposed to be used only by a HD-audio controller
235 * driver that needs the interaction with graphics driver.
236 *
237 * This function sets the given ops to be called by the graphics driver.
238 *
239 * Returns zero for success or a negative error code.
240 */
241int snd_hdac_acomp_register_notifier(struct hdac_bus *bus,
242 const struct drm_audio_component_audio_ops *aops)
243{
244 if (!bus->audio_component)
245 return -ENODEV;
246
247 bus->audio_component->audio_ops = aops;
248 return 0;
249}
250EXPORT_SYMBOL_GPL(snd_hdac_acomp_register_notifier);
251
252/**
253 * snd_hdac_acomp_init - Initialize audio component
254 * @bus: HDA core bus
255 * @match_master: match function for finding components
256 * @extra_size: Extra bytes to allocate
257 *
258 * This function is supposed to be used only by a HD-audio controller
259 * driver that needs the interaction with graphics driver.
260 *
261 * This function initializes and sets up the audio component to communicate
262 * with graphics driver.
263 *
264 * Unlike snd_hdac_i915_init(), this function doesn't synchronize with the
265 * binding with the DRM component. Each caller needs to sync via master_bind
266 * audio_ops.
267 *
268 * Returns zero for success or a negative error code.
269 */
270int snd_hdac_acomp_init(struct hdac_bus *bus,
271 const struct drm_audio_component_audio_ops *aops,
Daniel Vetter8857c7d2019-02-08 00:27:59 +0100272 int (*match_master)(struct device *, int, void *),
Takashi Iwaia57942b2018-07-11 16:23:16 +0200273 size_t extra_size)
274{
275 struct component_match *match = NULL;
276 struct device *dev = bus->dev;
277 struct drm_audio_component *acomp;
278 int ret;
279
280 if (WARN_ON(hdac_get_acomp(dev)))
281 return -EBUSY;
282
283 acomp = devres_alloc(hdac_acomp_release, sizeof(*acomp) + extra_size,
284 GFP_KERNEL);
285 if (!acomp)
286 return -ENOMEM;
287 acomp->audio_ops = aops;
288 bus->audio_component = acomp;
289 devres_add(dev, acomp);
290
Daniel Vetter8857c7d2019-02-08 00:27:59 +0100291 component_match_add_typed(dev, &match, match_master, bus);
Takashi Iwaia57942b2018-07-11 16:23:16 +0200292 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
293 match);
294 if (ret < 0)
295 goto out_err;
296
297 return 0;
298
299out_err:
300 bus->audio_component = NULL;
301 devres_destroy(dev, hdac_acomp_release, NULL, NULL);
302 dev_info(dev, "failed to add audio component master (%d)\n", ret);
303
304 return ret;
305}
306EXPORT_SYMBOL_GPL(snd_hdac_acomp_init);
307
308/**
309 * snd_hdac_acomp_exit - Finalize audio component
310 * @bus: HDA core bus
311 *
312 * This function is supposed to be used only by a HD-audio controller
313 * driver that needs the interaction with graphics driver.
314 *
315 * This function releases the audio component that has been used.
316 *
317 * Returns zero for success or a negative error code.
318 */
319int snd_hdac_acomp_exit(struct hdac_bus *bus)
320{
321 struct device *dev = bus->dev;
322 struct drm_audio_component *acomp = bus->audio_component;
323
324 if (!acomp)
325 return 0;
326
Takashi Iwai029d92c2018-12-08 17:31:49 +0100327 if (WARN_ON(bus->display_power_active) && acomp->ops)
Takashi Iwaia57942b2018-07-11 16:23:16 +0200328 acomp->ops->put_power(acomp->dev);
329
Takashi Iwai029d92c2018-12-08 17:31:49 +0100330 bus->display_power_active = false;
331 bus->display_power_status = 0;
332
Takashi Iwaia57942b2018-07-11 16:23:16 +0200333 component_master_del(dev, &hdac_component_master_ops);
334
335 bus->audio_component = NULL;
336 devres_destroy(dev, hdac_acomp_release, NULL, NULL);
337
338 return 0;
339}
340EXPORT_SYMBOL_GPL(snd_hdac_acomp_exit);