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